Example demonstrating entity querying and connectivity access.This example demonstrates how to:
- Get all entities in the mesh database
- Access entity connectivity (vertex connectivity for elements)
- Query vertex adjacencies (elements connected to vertices)
- Iterate through entities and examine their properties
The program reads a mesh file and reports connectivity information for all non-vertex entities and adjacency information for vertices.
To run: ./GetEntities meshfile
#include <iostream>
#include <memory>
#include <string>
#include <string_view>
namespace
{
}
{
std::cout << "Usage: " << program_name << " [meshfile]\n"
}
int main(
int argc,
char** argv )
{
try
{
if( argc > 1 && ( std::string( argv[1] ) == "-h" || std::string( argv[1] ) == "--help" ) )
{
return 0;
}
const std::string mesh_file =
auto moab = std::make_unique< Core >();
{
std::cerr << "Error: Failed to create MOAB instance\n";
return 1;
}
MB_CHK_SET_ERR(
moab->load_mesh( mesh_file.c_str() ),
"Failed to load mesh file: " << mesh_file );
Range entities;
MB_CHK_SET_ERR(
moab->get_entities_by_handle( 0, entities ),
"Failed to retrieve entities from mesh" );
for( const auto& entity : entities )
{
const auto entity_type =
moab->type_from_handle( entity );
const auto entity_id =
moab->id_from_handle( entity );
{
Range adjacent_elements;
"Failed to get adjacencies for vertex " << entity_id );
std::cout << "Vertex " << entity_id << " is connected to " << adjacent_elements.size() << " elements\n";
if( !adjacent_elements.empty() )
{
std::cout << " Adjacent elements: ";
for( const auto& elem : adjacent_elements )
{
std::cout <<
moab->id_from_handle( elem ) <<
" ";
}
std::cout << "\n";
}
}
{
int num_vertices = 0;
<< entity_id );
<< " vertices: ";
for( int i = 0; i < num_vertices; ++i )
{
std::cout <<
moab->id_from_handle( connectivity[i] );
if( i < num_vertices - 1 ) std::cout << ", ";
}
std::cout << "\n";
}
}
return 0;
}
catch( const std::exception& e )
{
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
catch( ... )
{
std::cerr << "Error: Unknown exception occurred\n";
return 1;
}
}