cgma
mergetest.cc
Go to the documentation of this file.
00001 
00007 #include "iGeom.h"
00008 #include <iostream>
00009 #include <set>
00010 #include <algorithm>
00011 #include <vector>
00012 #include <iomanip>
00013 #include <assert.h>
00014 #include <string.h>
00015 #include <math.h>
00016 
00017 
00018 #define CHECK( STR ) if (err != iBase_SUCCESS) return print_error( STR, err, geom, __FILE__, __LINE__ )
00019 
00020 #define STRINGIFY(S) XSTRINGIFY(S)
00021 #define XSTRINGIFY(S) #S
00022 
00023 static bool print_error( const char* desc,
00024                          int err,
00025                          iGeom_Instance geom,
00026                          const char* file,
00027                          int line )
00028 {
00029   char buffer[1024];
00030   iGeom_getDescription( geom, buffer, sizeof(buffer) );
00031   buffer[sizeof(buffer)-1] = '\0';
00032 
00033   std::cerr << "ERROR: " << desc << std::endl
00034             << "  Error code: " << err << std::endl
00035             << "  Error desc: " << buffer << std::endl
00036             << "  At        : " << file << ':' << line << std::endl
00037             ;
00038 
00039   return false; // must always return false or CHECK macro will break
00040 }
00041 
00042 typedef iBase_TagHandle TagHandle;
00043 typedef iBase_EntityHandle GentityHandle;
00044 typedef iBase_EntitySetHandle GentitysetHandle;
00045 
00046 /* Frees allocated arrays for us */
00047 template <typename T> class SimpleArray
00048 {
00049   private:
00050     T* arr;
00051     int arrSize;
00052     int arrAllocated;
00053 
00054   public:
00055     SimpleArray() : arr(0) , arrSize(0), arrAllocated(0) {}
00056     SimpleArray( unsigned s ) :arrSize(s), arrAllocated(s) {
00057       arr = (T*)malloc(s*sizeof(T));
00058       for (unsigned i = 0; i < s; ++i)
00059         new (arr+i) T();
00060     }
00061 
00062     ~SimpleArray() {
00063       for (int i = 0; i < size(); ++i)
00064         arr[i].~T();
00065       free(arr);
00066     }
00067 
00068     T**  ptr()            { return &arr; }
00069     int& size()           { return arrSize; }
00070     int  size()     const { return arrSize; }
00071     int& capacity()       { return arrAllocated; }
00072     int  capacity() const { return arrAllocated; }
00073 
00074     typedef T* iterator;
00075     typedef const T* const_iterator;
00076     iterator       begin()       { return arr; }
00077     const_iterator begin() const { return arr; }
00078     iterator         end()       { return arr + arrSize; }
00079     const_iterator   end() const { return arr + arrSize; }
00080 
00081 
00082     T& operator[]( unsigned idx )       { return arr[idx]; }
00083     T  operator[]( unsigned idx ) const { return arr[idx]; }
00084 };
00085 
00086 #define ARRAY_INOUT( A ) A.ptr(), &A.capacity(), &A.size()
00087 #define ARRAY_IN( A ) &A[0], A.size()
00088 
00089 int main( int argc, char *argv[] )
00090 {
00091     // Check command line arg
00092   #ifdef FORCE_OCC
00093     #ifndef HAVE_OCC
00094       #error "Cannot force use of OCC w/out OCC support"
00095     #endif
00096   std::string filename = STRINGIFY(SRCDIR) "/voltest.stp";
00097   std::string outfile = "merged.occ";
00098     std::string engine_opt = ";engine=OCC";
00099   #elif defined(HAVE_OCC)
00100     std::string filename = STRINGIFY(SRCDIR) "/voltest.stp";
00101     std::string outfile = "merged.occ";
00102     std::string engine_opt = ";engine=OCC";
00103   #else
00104     std::cerr << "for facet engine merge is not supported\n";
00105     return 1;
00106   #endif
00107   if (argc == 1) {
00108     std::cout << "Using default input file: " << filename << std::endl;
00109     std::cout << "Using default output file: " << outfile << std::endl;
00110   }
00111   else if (argc >= 3) {
00112     filename = argv[1];
00113     outfile = argv[2];
00114   }
00115   else {
00116     std::cerr << "Usage: " << argv[0] << " [geom_filename] [out file]" << std::endl;
00117     return 1;
00118   }
00119 
00120   // initialize  geom
00121   int err;
00122   iGeom_Instance geom;
00123   iGeom_newGeom( engine_opt.c_str(), &geom, &err, engine_opt.length() );
00124 
00125     // Print out Header information
00126   std::cout << "\n merge utility :\n\n";
00127 
00128   iGeom_load( geom, &filename[0], 0, &err, filename.length(), 0 );
00129   CHECK( "ERROR : can not load a geometry" );
00130 
00131   iBase_EntitySetHandle root_set;
00132   iGeom_getRootSet( geom, &root_set, &err );
00133   CHECK( "ERROR : getRootSet failed!" );
00134 
00135     // print out the number of entities
00136   std::cout << "Model contents: " << std::endl;
00137   const char *gtype[] = {"vertices: ", "edges: ", "faces: ", "regions: "};
00138   for (int i = 0; i <= 3; ++i) {
00139     int count;
00140     iGeom_getNumOfType( geom, root_set, i, &count, &err );
00141     CHECK( "Error: problem getting entities after gLoad." );
00142     std::cout << gtype[i] << count << std::endl;
00143   }
00144 
00145   // get volumes, and call merge
00146   int count;
00147   iGeom_getNumOfType( geom, root_set, 3, &count, &err );
00148   CHECK( "Error: problem getting volume numbers after gLoad." );
00149 
00150   SimpleArray<iBase_EntityHandle> vols;
00151 
00152   iGeom_getEntities( geom, root_set, 3, ARRAY_INOUT(vols), &err );
00153   CHECK( "Error: problem getting volumes." );
00154 
00155   //  now imprint
00156   std::cout << "\n\nImprinting...." << std::endl;
00157   iGeom_imprintEnts(geom, ARRAY_IN(vols),&err);
00158   CHECK( "Error: problem imprinting volumes." );
00160 
00161   std::cout << "\n\nMerging...." << std::endl;
00162   double dTol = 1e-4;
00163   iGeom_mergeEnts(geom, ARRAY_IN(vols), dTol, &err);
00165   CHECK( "Error: problem merging volumes." );
00166 
00167   iGeom_save(geom, outfile.c_str(), NULL, &err,
00168                 strlen(outfile.c_str()), 0);
00169   CHECK( "Error: problem saving." );
00170 
00171   return 0;
00172 }
00173 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines