This example takes a .cub mesh file as input and prints out the total area of meshes in each surface of the model. It works for tri and quad elements. It makes use of CUBIT's reserved tag - GEOM_DIMENSION and GLOBAL_ID tag. Both GLOBAL_ID & GEOM_DIMENSION tag are associated with all the geometric entities in a .cub file. Note: The program would give incorrect result for a non-convex element, since it breaks polygons into triangles for computing the area.
#include <iostream>
int main(
int argc,
char** argv )
{
if( 1 == argc )
{
std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
return 0;
}
const char* tag_geom = "GEOM_DIMENSION";
const char* tag_gid = "GLOBAL_ID";
Range sets;
std::vector< EntityHandle > ents;
for( set_it = sets.begin(); set_it != sets.end(); ++set_it )
{
int set_id;
if( set_id == 2 )
{
double total_area = 0.0;
int gid = 0;
ents.clear();
std::cout << "Total area of meshes in surface " << gid << " = " << total_area << std::endl;
}
}
}
double compute_area( std::vector< EntityHandle >& entities )
{
double area = 0.0;
for( int i = 0; i < int( entities.size() ); i++ )
{
std::vector< EntityHandle > conn;
for( int j = 2; j <= int( conn.size() ); ++j )
{
EntityHandle vertices[3] = { conn[0], conn[j - 1], conn[j - 2] };
CartVect coords[3];
CartVect edge0 = coords[1] - coords[0];
CartVect edge1 = coords[2] - coords[0];
area += ( edge0 * edge1 ).
length() / 2.0;
}
}
entities.clear();
return area;
}