cgma
attribute_to_file.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <string>
00004 
00005 #include "InitCGMA.hpp"
00006 #include "GeometryModifyTool.hpp"
00007 #include "GeometryQueryTool.hpp"
00008 #include "Body.hpp"
00009 #include "CGMApp.hpp"
00010 #include "CubitAttribManager.hpp"
00011 #include "CAEntityId.hpp"
00012 #include "CubitCompat.hpp"
00013 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 
00017 #if defined (TEST_OCC)
00018 #  define ENGINE "OCC"
00019 #  define FORMAT "OCC"
00020 #elif defined (TEST_FACET)
00021 #  define ENGINE "FACET"
00022 #  define FORMAT "FACET"
00023 #else
00024 #  error "Which engine to test?"
00025 #endif
00026 
00027 #define ASSERT(A) if (!(A)) failed(#A,__FILE__,__LINE__)
00028 CubitBoolean is_files_same(std::string filename1,
00029                            std::string filename2);
00030 
00031 void failed( const char* A, const char* FILE, int LINE )
00032 {
00033   printf( "Condition failed at %s:%d : %s\n", FILE, LINE, A );
00034   abort();
00035 }
00036 
00037 int main()
00038 {
00039   CubitStatus s = InitCGMA::initialize_cgma( ENGINE );
00040   ASSERT(s);
00041   
00042   // actuate CA_BODIES attribute and turn on auto flag
00043   CGMApp::instance()->attrib_manager()->register_attrib_type(CA_ENTITY_ID, "id", "ENTITY_ID",
00044                                  CAEntityId_creator, CUBIT_TRUE,
00045                                  CUBIT_TRUE, CUBIT_TRUE, CUBIT_TRUE,
00046                                  CUBIT_TRUE, CUBIT_FALSE);
00047   CGMApp::instance()->attrib_manager()->auto_flag(CUBIT_TRUE);
00048 
00049   // make 2 bricks
00050   int n = 2;
00051   Body** bricks = new Body*[n];
00052   DLIList<RefEntity*> export_list;
00053   for (int i = 0; i < n; i++) {
00054     bricks[i] = GeometryModifyTool::instance()->brick( 1, 1, 1 );
00055     ASSERT(bricks[i]);
00056     DLIList<Body*> bods;
00057     bods.append(bricks[i]);
00058     s = GeometryQueryTool::instance()->translate( bods, CubitVector(i,0,0) );
00059     ASSERT(s);
00060     export_list.append( bricks[i] );
00061   }
00062 
00063   // export as file
00064  // int junk;
00065   const char * filename = "bricks2.occ";
00066   const CubitString cubit_version="13.1";
00067   const char * filetype = "OCC";
00068   int num_ents_exported=0;
00069   s = CubitCompat_export_solid_model( export_list, filename, filetype,
00070                                       num_ents_exported, cubit_version);
00071   ASSERT(s);
00072 
00073   //check that the two single volume bodys' attributes are exported as SINGLELUMP%
00074   std::ifstream Myfile;
00075   std::string line;
00076   std::string search = "SINGLELUMP%";
00077   Myfile.open ("bricks2.occ");
00078   int found = 0;
00079   size_t offset;
00080   if(Myfile.is_open())
00081   {
00082     while(!Myfile.eof())
00083     {
00084       getline(Myfile,line);    
00085       if ((offset = line.find(search, 0)) != std::string::npos)
00086         found ++;
00087     }
00088     Myfile.close();
00089   }
00090    
00091   assert (found == 2); 
00092 
00093   filename = "bricks22.occ";
00094   num_ents_exported = 0;
00095   s = CubitCompat_export_solid_model( export_list, filename, filetype,
00096                                       num_ents_exported, cubit_version);
00097   ASSERT(s);
00098 
00099   //check that the two single volume bodys' attributes are exported as SINGLELUMP%
00100   Myfile.open ("bricks22.occ");
00101   found = 0;
00102   if(Myfile.is_open())
00103   {
00104     while(!Myfile.eof())
00105     {
00106       getline(Myfile,line);
00107       if ((offset = line.find(search, 0)) != std::string::npos)
00108         found ++;
00109     }
00110     Myfile.close();
00111   }
00112 
00113   assert (found == 2);
00114 
00115   // delete geometry
00116   GeometryQueryTool::instance()->delete_geometry();
00117   remove(filename);
00118 
00119   // import it again
00120   DLIList<RefEntity*> import_list;
00121   filename = "bricks2.occ";
00122   s = CubitCompat_import_solid_model( filename, filetype);
00123   ASSERT(s);
00124 
00125   // check imported entity has tool data actuated by attributes
00126   DLIList<RefEntity*> body_entity_list;
00127   s = GeometryQueryTool::instance()->ref_entity_list("body", body_entity_list, CUBIT_FALSE);
00128   body_entity_list.reset();
00129   assert ((int)body_entity_list.size() == 2);
00130 
00131   export_list.clean_out();
00132   export_list.append(CAST_TO(body_entity_list.get_and_step(), Body));
00133   export_list.append(CAST_TO(body_entity_list.get_and_step(), Body));
00134   const char * filename2 = "bricks23.occ";
00135   s = CubitCompat_export_solid_model( export_list, filename2,  filetype,
00136                                       num_ents_exported, cubit_version);
00137   ASSERT(s);
00138 
00139   ASSERT(is_files_same("bricks2.occ", "bricks23.occ"));
00140   remove(filename);
00141   remove(filename2);
00142   return 0;
00143 }
00144 
00145 CubitBoolean is_files_same(std::string filename1,
00146                            std::string filename2)
00147 {
00148   FILE *fp1, *fp2;
00149   char ch1, ch2;
00150   CubitBoolean same = false;
00151   /* open first file */
00152   const char* file1 = filename1.c_str();
00153   const char* file2 = filename2.c_str();
00154   if((fp1 = fopen(file1, "rb"))==NULL) {
00155     printf("Cannot open first file.\n");
00156     exit(false);
00157   }
00158 
00159   /* open second file */
00160   if((fp2 = fopen(file2, "rb"))==NULL) {
00161     printf("Cannot open second file.\n");
00162     exit(false);
00163   } 
00164 
00165   /* compare the files */
00166   while(!feof(fp1)) {
00167     ch1 = fgetc(fp1);
00168     if(ferror(fp1)) {
00169       printf("Error reading first file.\n");
00170       exit(false);
00171     }
00172     ch2 = fgetc(fp2);
00173     if(ferror(fp2)) {
00174       printf("Error reading second file.\n");
00175       exit(false);
00176     }
00177     if(ch1 != ch2) {
00178       return same;
00179     }
00180   }
00181 
00182   if(fclose( fp1 ) == EOF) {
00183     printf("Error closing first file.\n");
00184     exit(false);
00185   }
00186 
00187   if(fclose( fp2 ) == EOF) {
00188     printf("Error closing second file.\n");
00189     exit(false);
00190   } 
00191   return true;
00192 }
00193 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines