[MOAB-dev] r2702 - MOAB/trunk

jiangtao_ma at yahoo.com jiangtao_ma at yahoo.com
Wed Mar 11 14:08:28 CDT 2009


Author: janehu
Date: 2009-03-11 14:08:28 -0500 (Wed, 11 Mar 2009)
New Revision: 2702

Added:
   MOAB/trunk/ReadCGM.cpp
   MOAB/trunk/ReadCGM.hpp
Modified:
   MOAB/trunk/MBReaderWriterSet.cpp
   MOAB/trunk/Makefile.am
Log:
Added ReadCGM file for load_file of sat, step or brep type of files to moab.

Modified: MOAB/trunk/MBReaderWriterSet.cpp
===================================================================
--- MOAB/trunk/MBReaderWriterSet.cpp	2009-03-11 18:58:14 UTC (rev 2701)
+++ MOAB/trunk/MBReaderWriterSet.cpp	2009-03-11 19:08:28 UTC (rev 2702)
@@ -27,6 +27,7 @@
 #include "ReadSms.hpp"
 #include "Tqdcfr.hpp"
 #include "ReadTetGen.hpp"
+#include "ReadCGM.hpp"
 
 #include "WriteAns.hpp"
 #include "WriteVtk.hpp"
@@ -78,6 +79,9 @@
   
   register_factory( Tqdcfr::factory, NULL, "Cubit", "cub", "CUBIT" );
 
+  const char* cgm_sufxs[] = {"stp", "step", "brep", "occ", "sat" };
+  register_factory( ReadCGM::factory, NULL, "CGM", cgm_sufxs, "CGM");
+ 
 #ifdef NETCDF_FILE  
   register_factory( NULL, WriteSLAC::factory, "SLAC", "slac", "SLAC" );
 #endif

Modified: MOAB/trunk/Makefile.am
===================================================================
--- MOAB/trunk/Makefile.am	2009-03-11 18:58:14 UTC (rev 2701)
+++ MOAB/trunk/Makefile.am	2009-03-11 19:08:28 UTC (rev 2702)
@@ -1,5 +1,8 @@
 # Don't require GNU-standard files (Changelog, README, etc.)
 AUTOMAKE_OPTIONS = foreign
+
+ at CGM_CONFIG_OPTIONS@
+
 # Don't loose the autoconf include path
 ACLOCAL_AMFLAGS = -I m4
 
@@ -15,7 +18,8 @@
 
 
 # Things to build
-lib_LTLIBRARIES = libMOAB.la
+lib_LTLIBRARIES = libMOAB.la 
+
 TESTS = moab_test \
         homxform_test \
         scdseq_test \
@@ -58,12 +62,13 @@
 # Conditional sources
 MOAB_EXTRA_SRCS =
 MOAB_EXTRA_HDRS =
+
 if NETCDF_FILE
   MOAB_EXTRA_SRCS +=  ReadNCDF.cpp  ReadNCDF.hpp \
                      WriteNCDF.cpp WriteNCDF.hpp \
                      WriteSLAC.cpp WriteSLAC.hpp 
 endif
-libMOAB_la_LIBADD = $(NETCDF_LIBS) $(HDF5_LIBS)
+libMOAB_la_LIBADD = $(NETCDF_LIBS) $(HDF5_LIBS) 
 if HDF5_FILE
   libMOAB_la_LIBADD += $(top_builddir)/mhdf/libmhdf.la
   INCLUDES += -I$(srcdir)/mhdf/include
@@ -75,6 +80,14 @@
   INCLUDES += -I$(srcdir)/parallel
 endif
 
+if HAVE_CGM
+  INCLUDES += $(CGM_INCLUDES)
+  CPPFLAGS += $(CGM_CPPFLAGS) -DCGM $(MOAB_CGM_DEFINES)
+  MOAB_EXTRA_SRCS +=  ReadCGM.cpp  ReadCGM.hpp
+  libMOAB_la_LIBADD += $(CGM_LTFLAGS) $(CGM_LIBS_LINK)
+endif
+
+
 # Automake doesn't seem to have a directory defined for
 # platform-dependent data (or include) files. So put 
 # in $(libdir).  Define a $(cfgdir) to get around automake's

Added: MOAB/trunk/ReadCGM.cpp
===================================================================
--- MOAB/trunk/ReadCGM.cpp	                        (rev 0)
+++ MOAB/trunk/ReadCGM.cpp	2009-03-11 19:08:28 UTC (rev 2702)
@@ -0,0 +1,636 @@
+/**
+ * MOAB, a Mesh-Oriented datABase, is a software component for creating,
+ * storing and accessing finite element mesh data.
+ *
+ * Copyright 2004 Sandia Corporation.  Under the terms of Contract
+ * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
+ * retains certain rights in this software.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ */
+#ifdef WIN32
+#pragma warning(disable:4786)
+#endif
+
+#include "GeometryQueryTool.hpp"
+#include "GeometryQueryEngine.hpp"
+#include "ModelQueryEngine.hpp"
+#include "RefEntityName.hpp"
+#include "GMem.hpp"
+
+#include "RefGroup.hpp"
+#include "RefVolume.hpp"
+#include "RefFace.hpp"
+#include "RefEdge.hpp"
+#include "RefVertex.hpp"
+
+#include "SenseEntity.hpp"
+#include "Surface.hpp"
+#include "Curve.hpp"
+#include "InitCGMA.hpp"
+
+#include "MBCore.hpp"
+#include "MBInterface.hpp"
+#include "MBRange.hpp"
+#include "MBTagConventions.hpp"
+#include "FileOptions.hpp"
+
+#include "GeomTopoTool.hpp"
+
+#include <stdio.h>
+#include <algorithm>
+#include <assert.h>
+
+#include "ReadCGM.hpp"
+
+#define GF_CUBIT_FILE_TYPE    "CUBIT"
+#define GF_STEP_FILE_TYPE     "STEP"
+#define GF_IGES_FILE_TYPE     "IGES"
+#define GF_ACIS_TXT_FILE_TYPE "ACIS_SAT"
+#define GF_ACIS_BIN_FILE_TYPE "ACIS_SAB"
+#define GF_OCC_BREP_FILE_TYPE "OCC"
+
+MBReaderIface* ReadCGM::factory( MBInterface* iface )
+{ return new ReadCGM( iface ); }
+
+ReadCGM::ReadCGM(MBInterface *impl)
+    : geom_tag(0), id_tag(0), name_tag(0), category_tag(0)
+{
+  assert(NULL != impl);
+  mdbImpl = impl;
+  myGeomTool = new GeomTopoTool(impl);
+  void* ptr = 0;
+  impl->query_interface( "MBReadUtilIface", &ptr );
+  assert(NULL != ptr);
+  readUtilIface = reinterpret_cast<MBReadUtilIface*>(ptr);
+
+  MBErrorCode rval;
+
+  // get some tag handles
+  rval = mdbImpl->tag_create( GEOM_DIMENSION_TAG_NAME, sizeof(int), 
+                            MB_TAG_SPARSE, MB_TYPE_INTEGER, geom_tag, 0, true ); 
+  assert(!rval);
+  rval = mdbImpl->tag_create( GLOBAL_ID_TAG_NAME, sizeof(int), MB_TAG_DENSE,
+                            MB_TYPE_INTEGER, id_tag, 0, true );
+  assert(!rval);
+  rval = mdbImpl->tag_create( NAME_TAG_NAME, NAME_TAG_SIZE, MB_TAG_SPARSE,
+                            MB_TYPE_OPAQUE, name_tag, 0, true );
+  assert(!rval);
+
+  rval = mdbImpl->tag_create( CATEGORY_TAG_NAME, CATEGORY_TAG_SIZE,
+                            MB_TAG_SPARSE, MB_TYPE_OPAQUE, category_tag, 0, true );
+  assert(!rval);
+}
+
+ReadCGM::~ReadCGM()
+{
+  std::string iface_name = "MBReadUtilIface";
+  mdbImpl->release_interface(iface_name, readUtilIface);
+  delete myGeomTool;
+}
+
+// copy geometry into mesh database
+MBErrorCode ReadCGM::load_file(const char *cgm_file_name,
+                      MBEntityHandle& file_set,
+                      const FileOptions& opts,
+                      const int* blocks_to_load,
+                      const int num_blocks)
+{
+  //opts, blocks_to_load and num_blocks are ignored.
+  MBErrorCode rval;
+  file_set = 0;
+
+  std::string filename( cgm_file_name );
+  cgmFile = filename;
+
+  // CGM data
+  std::map<RefEntity*,MBEntityHandle> entmap[5]; // one for each dim, and one for groups
+  std::map<RefEntity*,MBEntityHandle>::iterator ci;
+  const char geom_categories[][CATEGORY_TAG_SIZE] = 
+    {"Vertex\0", "Curve\0", "Surface\0", "Volume\0", "Group\0"};
+  const char* const names[] = { "Vertex", "Curve", "Surface", "Volume"};
+  DLIList<RefEntity*> entlist;
+  DLIList<ModelEntity*> me_list;
+
+  // Initialize CGM
+  InitCGMA::initialize_cgma();
+
+  // Get CGM file type
+  const char* file_type = 0;
+  file_type = get_geom_file_type( cgm_file_name );
+  if (!file_type) {
+    std::cerr << cgm_file_name << " : unknown file type, try '-t'" << std::endl;
+    return MB_FAILURE;
+  }
+  else if(!strcmp(file_type ,"CUBIT"))
+  {
+     std::cerr << cgm_file_name << " : cub file are not handled here." << std::endl;
+    return MB_FAILURE;
+  }
+
+  CubitStatus s;
+  s = GeometryQueryTool::instance()->import_solid_model( cgm_file_name, file_type );
+  if (CUBIT_SUCCESS != s) {
+    std::cerr << "Failed to read '" << cgm_file_name << "' of type '" << file_type << "'" << std::endl;
+    return MB_FAILURE;
+  }
+
+  // create entity sets for all geometric entities
+  for (int dim = 0; dim < 4; ++dim) {
+    entlist.clean_out();
+    GeometryQueryTool::instance()->ref_entity_list( names[dim], entlist, true );
+    
+    entlist.reset();
+    for (int i = entlist.size(); i--; ) {
+      RefEntity* ent = entlist.get_and_step();
+      MBEntityHandle handle;
+      rval = mdbImpl->create_meshset( dim == 1 ? MESHSET_ORDERED : MESHSET_SET, handle );
+      if (MB_SUCCESS != rval)
+        return MB_FAILURE;
+    
+      entmap[dim][ent] = handle;
+      
+      rval = mdbImpl->tag_set_data( geom_tag, &handle, 1, &dim );
+      if (MB_SUCCESS != rval)
+        return rval;
+      int id = ent->id();
+      rval = mdbImpl->tag_set_data( id_tag, &handle, 1, &id );
+      if (MB_SUCCESS != rval)
+        return rval;
+
+      rval = mdbImpl->tag_set_data( category_tag, &handle, 1, &geom_categories[dim] );
+      if (MB_SUCCESS != rval)
+        return rval;
+    }
+  }
+  
+    // create topology for all geometric entities
+  for (int dim = 1; dim < 4; ++dim) {
+    for (ci = entmap[dim].begin(); ci != entmap[dim].end(); ++ci) {
+      entlist.clean_out();
+      ci->first->get_child_ref_entities( entlist );
+    
+      entlist.reset();
+      for (int i = entlist.size(); i--; ) {
+        RefEntity* ent = entlist.get_and_step();
+        MBEntityHandle h = entmap[dim-1][ent];
+        rval = mdbImpl->add_parent_child( ci->second, h );
+        if (MB_SUCCESS != rval)
+          return MB_FAILURE;
+      }
+    }
+  }
+  
+    // store CoFace senses
+  for (ci = entmap[2].begin(); ci != entmap[2].end(); ++ci) {
+    RefFace* face = (RefFace*)(ci->first);
+    BasicTopologyEntity *forward = 0, *reverse = 0;
+    for (SenseEntity* cf = face->get_first_sense_entity_ptr();
+         cf; cf = cf->next_on_bte()) {
+      BasicTopologyEntity* vol = cf->get_parent_basic_topology_entity_ptr();
+      if (cf->get_sense() == CUBIT_UNKNOWN || 
+          cf->get_sense() != face->get_surface_ptr()->bridge_sense()) {
+        if (reverse) {
+          std::cout << "Surface " << face->id() << " has reverse senes " <<
+                       "with multiple volume " << reverse->id() << " and " <<
+                       "volume " << vol->id() << std::endl;
+          return MB_FAILURE;
+        }
+        reverse = vol;
+      }
+      if (cf->get_sense() == CUBIT_UNKNOWN || 
+          cf->get_sense() == face->get_surface_ptr()->bridge_sense()) {
+        if (forward) {
+          std::cout << "Surface " << face->id() << " has forward senes " <<
+                       "with multiple volume " << forward->id() << " and " <<
+                       "volume " << vol->id() << std::endl;
+          return MB_FAILURE;
+        }
+        forward = vol;
+      }
+    }
+    
+    if (forward) {
+      rval = myGeomTool->set_sense( ci->second, entmap[3][forward], true );
+      if (MB_SUCCESS != rval)
+        return rval;
+    }
+    if (reverse) {
+      rval = myGeomTool->set_sense( ci->second, entmap[3][reverse], false );
+      if (MB_SUCCESS != rval)
+        return rval;
+    }
+  }
+  
+    // create entity sets for all ref groups
+  std::vector<MBTag> extra_name_tags;
+  DLIList<CubitString*> name_list;
+  entlist.clean_out();
+  GeometryQueryTool::instance()->ref_entity_list( "group", entlist );
+  entlist.reset();
+  for (int i = entlist.size(); i--; ) {
+    RefEntity* grp = entlist.get_and_step();
+    name_list.clean_out();
+    RefEntityName::instance()->get_refentity_name( grp, name_list, true );
+    if (name_list.size() == 0)
+      continue;
+    name_list.reset();
+    CubitString name = *name_list.get();
+    
+    MBEntityHandle h;
+    rval = mdbImpl->create_meshset( MESHSET_SET, h );
+    if (MB_SUCCESS != rval)
+      return MB_FAILURE;
+    
+    char namebuf[NAME_TAG_SIZE];
+    memset( namebuf, '\0', NAME_TAG_SIZE );
+    strncpy( namebuf, name.c_str(), NAME_TAG_SIZE - 1 );
+    if (name.length() >= (unsigned)NAME_TAG_SIZE) 
+      std::cout << "WARNING: group name '" << name.c_str() 
+                << "' truncated to '" << namebuf << "'" << std::endl;
+    rval = mdbImpl->tag_set_data( name_tag, &h, 1, namebuf );
+    if (MB_SUCCESS != rval)
+      return MB_FAILURE;
+      
+    int id = grp->id();
+    rval = mdbImpl->tag_set_data( id_tag, &h, 1, &id );
+    if (MB_SUCCESS != rval)
+      return MB_FAILURE;
+      
+    rval = mdbImpl->tag_set_data( category_tag, &h, 1, &geom_categories[4] );
+    if (MB_SUCCESS != rval)
+      return MB_FAILURE;
+      
+    if (name_list.size() > 1) {
+      for (int j = extra_name_tags.size(); j < name_list.size(); ++j) {
+        sprintf( namebuf, "EXTRA_%s%d", NAME_TAG_NAME, j );
+        MBTag t;
+        rval = mdbImpl->tag_create( namebuf, NAME_TAG_SIZE, MB_TAG_SPARSE, MB_TYPE_OPAQUE, t, 0, true );
+        assert(!rval);
+        extra_name_tags.push_back(t);
+      }
+        
+      for (int j = 0; j < name_list.size(); ++j) {
+        name = *name_list.get_and_step();
+        memset( namebuf, '\0', NAME_TAG_SIZE );
+        strncpy( namebuf, name.c_str(), NAME_TAG_SIZE - 1 );
+        if (name.length() >= (unsigned)NAME_TAG_SIZE) 
+          std::cout << "WARNING: group name '" << name.c_str() 
+                    << "' truncated to '" << namebuf << "'" << std::endl;
+        rval = mdbImpl->tag_set_data( extra_name_tags[j], &h, 1, namebuf );
+        if (MB_SUCCESS != rval)
+          return MB_FAILURE;
+      }
+    }
+      
+    entmap[4][grp] = h;
+  }
+  
+    // store contents for each group
+  entlist.reset();
+  for (ci = entmap[4].begin(); ci != entmap[4].end(); ++ci) {
+    RefGroup* grp = (RefGroup*)(ci->first);
+    entlist.clean_out();
+    grp->get_child_ref_entities( entlist );
+    
+    MBRange entities;
+    while (entlist.size()) {
+      RefEntity* ent = entlist.pop();
+      int dim = ent->dimension();
+      if (dim < 0) {
+        if (entmap[4].find(ent) != entmap[4].end())
+          entities.insert( entmap[4][ent] );
+      }
+      else if (dim < 4) {
+        if (entmap[dim].find(ent) != entmap[dim].end())
+          entities.insert( entmap[dim][ent] );
+      }
+    }
+    
+    if (!entities.empty()) {
+      rval = mdbImpl->add_entities( ci->second, entities );
+      if (MB_SUCCESS != rval)
+        return MB_FAILURE;
+    }
+  }
+  
+    // done with volumes and groups
+  entmap[3].clear();
+  entmap[4].clear();
+  
+    // create geometry for all vertices and replace 
+    // vertex set handles with vertex handles in map
+  for (ci = entmap[0].begin(); ci != entmap[0].end(); ++ci) {
+    CubitVector pos = dynamic_cast<RefVertex*>(ci->first)->coordinates();
+    double coords[3] = {pos.x(), pos.y(), pos.z()};
+    MBEntityHandle vh;
+    rval = mdbImpl->create_vertex( coords, vh );
+    if (MB_SUCCESS != rval)
+      return MB_FAILURE;
+    
+    rval = mdbImpl->add_entities( ci->second, &vh, 1 );
+    if (MB_SUCCESS != rval)
+      return MB_FAILURE;
+    
+    ci->second = vh;
+  }
+  
+    // create geometry for all curves
+  GMem data;
+  for (ci = entmap[1].begin(); ci != entmap[1].end(); ++ci) {
+    RefEdge* edge = dynamic_cast<RefEdge*>(ci->first);
+    Curve* curve = edge->get_curve_ptr();
+    GeometryQueryEngine* gqe = curve->get_geometry_query_engine();
+    data.clean_out();
+    int count;
+    double faceting_tol = 0.001;
+    CubitStatus s = gqe->get_graphics( curve, count, &data, faceting_tol);
+    if (CUBIT_SUCCESS != s)
+      return MB_FAILURE;
+      
+    std::vector<CubitVector> points;
+    for (int i = 0; i < data.pointListCount; ++i)
+      points.push_back( CubitVector( data.point_list()[i].x,
+                                     data.point_list()[i].y,
+                                     data.point_list()[i].z ) );
+
+      // need to reverse data?
+    if (curve->bridge_sense() == CUBIT_REVERSED) 
+      std::reverse( points.begin(), points.end() );
+    
+       // check for closed curve
+    RefVertex *start_vtx, *end_vtx;
+    start_vtx = edge->start_vertex();
+    end_vtx = edge->end_vertex();
+    
+      // Special case for point curve
+    if (points.size() < 2) {
+      if (start_vtx != end_vtx || curve->measure() > gqe->get_sme_resabs_tolerance()) {
+        std::cerr << "Warning: No facetting for curve " << edge->id() << std::endl;
+        continue;
+      }
+      MBEntityHandle h = entmap[0][start_vtx];
+      rval = mdbImpl->add_entities( ci->second, &h, 1 );
+      if (MB_SUCCESS != rval)
+        return MB_FAILURE;
+      continue;
+    }
+    
+    const bool closed = (points.front() - points.back()).length() < gqe->get_sme_resabs_tolerance();
+    if (closed != (start_vtx == end_vtx)) {
+      std::cerr << "Warning: topology and geometry inconsistant for possibly closed curve "
+                << edge->id() << std::endl;
+    }
+    
+      // check proximity of vertices to end coordinates
+    if ((start_vtx->coordinates() - points.front()).length() > gqe->get_sme_resabs_tolerance()
+     || (  end_vtx->coordinates() - points.back() ).length() > gqe->get_sme_resabs_tolerance()) {
+      std::cerr << "Warning: vertices not at ends of curve " << edge->id() << std::endl;
+    }
+    
+      // create interior points
+    std::vector<MBEntityHandle> verts, edges;
+    verts.push_back( entmap[0][start_vtx] );
+    for (size_t i = 1; i < points.size() - 1; ++i) {
+      double coords[] = { points[i].x(), points[i].y(), points[i].z() };
+      MBEntityHandle h;
+      rval = mdbImpl->create_vertex( coords, h );
+      if (MB_SUCCESS != rval)
+        return MB_FAILURE;
+      verts.push_back( h );
+    }
+    verts.push_back( entmap[0][end_vtx] );
+    
+      // create edges
+    for (size_t i = 0; i < verts.size()-1; ++i) {
+      MBEntityHandle h;
+      rval = mdbImpl->create_element( MBEDGE, &verts[i], 2, h );
+      if (MB_SUCCESS != rval)
+        return MB_FAILURE;
+      edges.push_back( h );
+    }
+    
+      // if closed, remove duplicate
+    if (verts.front() == verts.back())
+      verts.pop_back();
+    
+    rval = mdbImpl->add_entities( ci->second, &verts[0], verts.size() );
+    if (MB_SUCCESS != rval)
+      return MB_FAILURE;
+    rval = mdbImpl->add_entities( ci->second, &edges[0], edges.size() );
+    if (MB_SUCCESS != rval)
+      return MB_FAILURE;
+  }
+  
+    // create geometry for all surfaces
+  for (ci = entmap[2].begin(); ci != entmap[2].end(); ++ci) {
+    RefFace* face = dynamic_cast<RefFace*>(ci->first);
+    Surface* surf = face->get_surface_ptr();
+    GeometryQueryEngine* gqe = surf->get_geometry_query_engine();
+    data.clean_out();
+    int nt, np, nf;
+    int norm_tol = 5;
+    double faceting_tol = 0.001, len_tol = 0.0;
+    CubitStatus s = gqe->get_graphics( surf, nt, np, nf, &data, 
+                                       norm_tol, faceting_tol, len_tol);
+    if (CUBIT_SUCCESS != s)
+      return MB_FAILURE;
+
+      // declare array of all vertex handles
+    std::vector<MBEntityHandle> verts( data.pointListCount, 0 );
+    
+      // get list of vertices in surface
+    me_list.clean_out();
+    ModelQueryEngine::instance()->query_model( *face, DagType::ref_vertex_type(), me_list );
+
+      // for each vertex, find coincident point in facets
+    for (int i = me_list.size(); i--; ) {
+      RefVertex* vtx = dynamic_cast<RefVertex*>(me_list.get_and_step());
+      CubitVector pos = vtx->coordinates();
+
+      for (int j = 0; j < data.pointListCount; ++j) {
+        CubitVector vpos( data.point_list()[j].x,
+                          data.point_list()[j].y,
+                          data.point_list()[j].z );
+        if ((pos - vpos).length_squared() < gqe->get_sme_resabs_tolerance()*gqe->get_sme_resabs_tolerance()) {
+          if (verts[j])
+            std::cerr << "Warning: Coincident vertices in surface " << face->id() << std::endl;
+          verts[j] = entmap[0][vtx];
+          break;
+        }
+      }
+    }
+    
+      // now create vertices for the remaining points in the facetting
+    for (int i = 0; i < data.pointListCount; ++i) {
+      if (verts[i]) // if a geometric vertex
+        continue;
+      double coords[] = { data.point_list()[i].x,
+                          data.point_list()[i].y,
+                          data.point_list()[i].z };
+      rval = mdbImpl->create_vertex( coords, verts[i] );
+      if (MB_SUCCESS != rval)
+        return rval;
+    }
+    
+      // now create facets
+    MBRange facets;
+    std::vector<MBEntityHandle> corners;
+    for (int i = 0; i < data.fListCount; i += data.facet_list()[i]+1) {
+      int* facet = data.facet_list() + i;
+      corners.resize( *facet );
+      for (int j = 1; j <= *facet; ++j) {
+        if (facet[j] >= (int)verts.size()) {
+          std::cerr << "ERROR: Invalid facet data for surface " << face->id() << std::endl;
+          return MB_FAILURE;
+        }
+        corners[j-1] = verts[facet[j]];
+      }
+      MBEntityType type;
+      if (*facet == 3)
+        type = MBTRI;
+      else {
+        std::cerr << "Warning: non-triangle facet in surface " << face->id() << std::endl;
+        if (*facet == 4)
+          type = MBQUAD;
+        else
+          type = MBPOLYGON;
+      }
+      
+      // if (surf->bridge_sense() == CUBIT_REVERSED)
+      //   std::reverse( corners.begin(), corners.end() );
+      
+      MBEntityHandle h;
+      rval = mdbImpl->create_element( type, &corners[0], corners.size(), h );
+      if (MB_SUCCESS != rval)
+        return MB_FAILURE;
+        
+      facets.insert( h );
+    }
+    
+      // add vertices and facets to surface set
+    rval = mdbImpl->add_entities( ci->second, &verts[0], verts.size() );
+    if (MB_SUCCESS != rval)
+      return MB_FAILURE;
+    rval = mdbImpl->add_entities( ci->second, facets );
+    if (MB_SUCCESS != rval)
+      return MB_FAILURE;
+  }
+  
+  MBRange init_range, loaded_range;
+  rval = mdbImpl->get_entities_by_handle(0, loaded_range);
+  if (MB_FAILURE == rval) return rval;
+  rval = mdbImpl->get_entities_by_handle(0, init_range);
+  loaded_range = loaded_range.subtract(init_range);
+  rval = mdbImpl->add_entities(mCurrentMeshHandle, loaded_range);
+  if (MB_FAILURE == rval) return rval;
+
+  file_set = mCurrentMeshHandle;
+  return MB_SUCCESS;
+}
+
+const char* ReadCGM::get_geom_file_type( const char* name )
+{
+  FILE* file;
+  const char* result = 0;
+
+  file = fopen( name, "r" );
+  if (file) {
+    result = get_geom_fptr_type( file );
+    fclose( file );
+  }
+ 
+  return result;
+}
+
+const char* ReadCGM::get_geom_fptr_type( FILE* file )
+{
+  static const char* CUBIT_NAME = GF_CUBIT_FILE_TYPE;
+  static const char*  STEP_NAME = GF_STEP_FILE_TYPE;
+  static const char*  IGES_NAME = GF_IGES_FILE_TYPE;
+  static const char*   SAT_NAME = GF_ACIS_TXT_FILE_TYPE;
+  static const char*   SAB_NAME = GF_ACIS_BIN_FILE_TYPE;
+  static const char*  BREP_NAME = GF_OCC_BREP_FILE_TYPE;
+ 
+  if (is_cubit_file(file))
+    return CUBIT_NAME;
+  else if (is_step_file(file))
+    return STEP_NAME;
+  else if (is_iges_file(file))
+    return IGES_NAME;
+  else if (is_acis_bin_file(file))
+    return SAB_NAME;
+  else if (is_acis_txt_file(file))
+    return SAT_NAME;
+  else if (is_occ_brep_file(file))
+    return BREP_NAME;
+  else
+    return 0;
+}    
+
+int ReadCGM::is_cubit_file( FILE* file )
+{
+  unsigned char buffer[4];
+  return !fseek(file, 0, SEEK_SET) &&
+         fread(buffer, 4, 1, file) &&
+         !memcmp(buffer, "CUBE", 4);
+}
+
+int ReadCGM::is_step_file( FILE* file )
+{
+  unsigned char buffer[9];
+  return !fseek(file, 0, SEEK_SET) &&
+         fread(buffer, 9, 1, file) &&
+         !memcmp(buffer, "ISO-10303", 9);
+}
+
+int ReadCGM::is_iges_file( FILE* file )
+{
+  unsigned char buffer[10];
+  return !fseek(file, 72, SEEK_SET) &&
+         fread(buffer, 10, 1, file) &&
+         !memcmp(buffer, "S      1", 8);
+}
+
+int ReadCGM::is_acis_bin_file( FILE* file )
+{
+  char buffer[15];
+  return !fseek(file, 0, SEEK_SET) &&
+         fread(buffer, 15, 1, file) &&
+         !memcmp(buffer, "ACIS BinaryFile", 9);
+}
+
+int ReadCGM::is_acis_txt_file( FILE* file )
+{
+  char buffer[5];
+  int version, length;
+ 
+  if (fseek(file,0,SEEK_SET) ||
+      2 != fscanf( file, "%d %*d %*d %*d %d ", &version, &length ))
+    return 0;
+   
+  if (version < 1 || version >0xFFFF)
+    return 0;
+ 
+    // Skip appliation name
+  if (fseek(file, length, SEEK_CUR))
+    return 0;
+   
+    // Read length of version string followed by first 5 characters
+  if (2 != fscanf(file, "%d %4s", &length, buffer))
+    return 0;
+
+  return !strcmp( buffer, "ACIS" );
+}
+
+int ReadCGM::is_occ_brep_file( FILE* file )
+{
+  unsigned char buffer[6];
+  return !fseek(file, 0, SEEK_SET) &&
+         fread(buffer, 6, 1, file) &&
+         !memcmp(buffer, "DBRep_", 6);
+}

Added: MOAB/trunk/ReadCGM.hpp
===================================================================
--- MOAB/trunk/ReadCGM.hpp	                        (rev 0)
+++ MOAB/trunk/ReadCGM.hpp	2009-03-11 19:08:28 UTC (rev 2702)
@@ -0,0 +1,92 @@
+/**
+ * MOAB, a Mesh-Oriented datABase, is a software component for creating,
+ * storing and accessing finite element mesh data.
+ *
+ * Copyright 2004 Sandia Corporation.  Under the terms of Contract
+ * DE-AC04-94AL85000 with Sandia Coroporation, the U.S. Government
+ * retains certain rights in this software.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ */
+
+//-------------------------------------------------------------------------
+// Filename      : ReadCGM.hpp
+//
+// Purpose       : .sat, .step and .brep file reader
+//
+// Special Notes : Lots of code taken from cgm2moab implementation
+//
+// Creator       : Jane Hu
+//
+// Date          : 3/09
+//
+//-------------------------------------------------------------------------
+
+#ifndef READCGM_HPP
+#define READCGM_HPP
+
+#ifndef CGM
+#error "ReadCGM.hpp isn't supposed to be included without building CGM"
+#endif
+
+#include <string>
+class MBReadUtilIface;
+class GeomTopoTool;
+
+#include "MBReaderIface.hpp"
+class ReadCGM : public MBReaderIface
+{
+
+public:
+
+  static MBReaderIface* factory( MBInterface* );
+
+    //! load an ExoII file
+  MBErrorCode load_file(const char *cgm_file_name,
+                         MBEntityHandle& file_set,
+                         const FileOptions& opts,
+                         const int* blocks_to_load,
+                         const int num_blocks);
+
+   //! Constructor
+   ReadCGM(MBInterface* impl = NULL);
+
+   //! Destructor
+  virtual ~ReadCGM();
+
+private:
+
+  MBReadUtilIface* readUtilIface;
+
+  GeomTopoTool* myGeomTool;
+
+  const char* get_geom_file_type( const char* filename );
+  const char* get_geom_fptr_type( FILE* file );
+
+  int is_cubit_file( FILE* file );
+  int is_step_file( FILE* file );
+  int is_iges_file( FILE* file );
+  int is_acis_txt_file( FILE* file );
+  int is_acis_bin_file( FILE* file );
+  int is_occ_brep_file( FILE* file );
+
+
+  //------------member variables ------------//
+
+    //! interface instance
+  MBInterface* mdbImpl;
+
+    //! file name
+  std::string cgmFile;
+
+    //! Meshset Handle for the mesh that is currently being read
+  MBEntityHandle mCurrentMeshHandle;
+
+  MBTag geom_tag, id_tag, name_tag, category_tag;
+};
+
+#endif



More information about the moab-dev mailing list