cgma
copy_attrib.cpp
Go to the documentation of this file.
00001 #include "iGeom.h"
00002 #include <vector>
00003 #include <iostream>
00004 #include <stdio.h>
00005 #include <fstream>
00006 #include <assert.h>
00007 
00008 // helper macro for igeom
00009 #define ASSERT(A) if (!(A)) failed(#A,__FILE__,__LINE__)
00010 #define CHECK( STR ) if (err != iBase_SUCCESS) return Print_Error( STR, err, geom, __FILE__, __LINE__ )
00011 int Tag_Body( iGeom_Instance geom, const iBase_TagHandle tag, const std::string id, const iBase_EntityHandle body);
00012 int Tag_Get( iGeom_Instance geom, const iBase_TagHandle tag, const iBase_EntityHandle body);
00013 bool Print_Error( const char* desc, 
00014           int err,
00015           iGeom_Instance geom,
00016           const char* file,
00017           int line );
00018 int main(){
00019   int err;
00020   iGeom_Instance geom;
00021   double dSide = 10.0, dHeight = 10.0, dRad = 3.0;
00022   iBase_EntityHandle assm = NULL, cyl = NULL, tmp_cyl = NULL, tmp_new = NULL;
00023   std::string geomFile = "test.occ";
00024 
00025 
00026   iGeom_newGeom( 0, &geom, &err, 0 );
00027 
00028   iBase_TagHandle pin_tag = NULL, name_tag = NULL;
00029   char* tag_name_pin = (char*)"PIN";
00030   char* tag_name = (char*)"NAME";
00031   iGeom_getTagHandle(geom, tag_name_pin, &pin_tag, &err, 3);
00032   if(err == iBase_TAG_NOT_FOUND){
00033       
00034     iGeom_createTag(geom, tag_name_pin, 1, iBase_INTEGER, &pin_tag, &err,  3);
00035 
00036   }
00037 
00038   iGeom_getTagHandle(geom, tag_name, &name_tag, &err, 4);
00039   if(err == iBase_TAG_NOT_FOUND){
00040       
00041     iGeom_createTag(geom, tag_name, 1, iBase_INTEGER, &name_tag, &err,  4);
00042 
00043   }
00044 
00045   // creating prism
00046   std::cout << "\n\n\nCreating Prism\n" << std::endl;
00047   iGeom_createPrism(geom, dHeight, 6, 
00048             dSide, dSide,
00049             &assm, &err); 
00050   std::string one = "1";
00051   Tag_Body(geom, pin_tag, one, assm);
00052   Tag_Get (geom, pin_tag, assm);
00053   std::cout << "name tag: ";
00054   std::string a = "a";
00055   Tag_Body(geom, name_tag, a, assm);
00056   Tag_Get (geom, name_tag, assm);  
00057 
00058   std::cout << "\n\n\nCreating Cylinder\n" << std::endl;
00059   // create cylinder
00060   iGeom_createCylinder(geom, dHeight, dRad, dRad, &cyl, &err);
00061   std::string two = "2";
00062   Tag_Body(geom, pin_tag, two, cyl);
00063   Tag_Get(geom, pin_tag, cyl);
00064 
00065   std::cout << "name tag: ";
00066   std::string b = "b";
00067   Tag_Body(geom, name_tag, b, cyl);
00068   Tag_Get (geom, name_tag, cyl); 
00069 
00070   // Copy
00071   iGeom_copyEnt(geom, cyl, &tmp_cyl, &err);
00072   std::cout << "\n\n After copy operation\n" << std::endl;
00073   Tag_Get(geom, pin_tag, tmp_cyl);
00074   std::cout << "\ngetting name tag " << std::endl;
00075   Tag_Get(geom, name_tag, tmp_cyl);
00076 
00077   // Substract
00078   iGeom_subtractEnts(geom, assm, tmp_cyl, &tmp_new, &err);
00079   std::cout << "\n\n After subtract operation\n" << std::endl;
00080   Tag_Get(geom, pin_tag, tmp_new);
00081   std::cout << "\n getting name tag " << std::endl;
00082   Tag_Get(geom, name_tag, tmp_new);
00083 
00084   // save
00085   iGeom_save(geom, geomFile.c_str(), NULL, &err, geomFile.length() , 0);
00086   
00087   //check that the two single volume bodys' attributes are exported as SINGLELUMP%
00088   std::string search = "SINGLELUMP%";
00089   std::ifstream Myfile;
00090   Myfile.open (geomFile.c_str());
00091   int found = 0;
00092   std::string line;
00093   size_t offset;
00094   if(Myfile.is_open())
00095   {
00096     while(!Myfile.eof())
00097     {
00098       getline(Myfile,line);
00099       if ((offset = line.find(search, 0)) != std::string::npos)
00100         found ++;
00101     }
00102     Myfile.close();
00103   }
00104 
00105   assert (found == 4);
00106 
00107   return 0;
00108 }
00109   
00110 
00111 int Tag_Body( iGeom_Instance geom, const iBase_TagHandle tag, const std::string id, const iBase_EntityHandle body)
00112 //---------------------------------------------------------------------------
00113 //Function: Tag body with pin number
00114 //Input:    none
00115 //Output:   none
00116 //---------------------------------------------------------------------------
00117 {
00118   int err = 0;
00119 
00120   iGeom_setData(geom, body, tag, id.c_str(), id.size(), &err);
00121   std::cout<<"\nset pin tag - " << id<< " on " << body << std::endl;
00122   return 0;
00123 }
00124 
00125 
00126 
00127 int Tag_Get(iGeom_Instance geom, const iBase_TagHandle tag, const iBase_EntityHandle body)
00128 //---------------------------------------------------------------------------
00129 //Function: Tag get
00130 //Input:    none
00131 //Output:   none
00132 //---------------------------------------------------------------------------
00133 {
00134   int err = 0, bytes;
00135   iGeom_getTagSizeBytes( geom, tag, &bytes, &err );
00136   CHECK( "Can't get size" );
00137 
00138   std::vector<char> data(bytes);
00139 
00140   //just check if pin tag exist, IT'S NOT USED IN THIS FUNCTION
00141   iBase_TagHandle pin1 = NULL;
00142   char* p = (char*)"PIN";
00143 
00144   iGeom_getTagHandle(geom, p, &pin1, &err, 3);
00145   CHECK( "PIN tag doesn't exist" );
00146 
00147   void* ptr = &data[0];
00148   int junk1 = bytes, junk2;
00149   iGeom_getData( geom, body, tag, (void**)&ptr, &junk1, &junk2, &err );
00150   CHECK( "failed to getData for tag" );
00151   std::cout << "Able to get this tag: "<<*(void**)ptr << " on " << body <<std::endl;
00152   return 0;
00153 }
00154 
00155 
00156 
00157 // print error function definition (iGeom)
00158 bool Print_Error( const char* desc, 
00159           int err,
00160           iGeom_Instance geom,
00161           const char* file,
00162           int line )
00163 {
00164   char buffer[1024];
00165   iGeom_getDescription( geom, buffer, sizeof(buffer) );
00166   buffer[sizeof(buffer)-1] = '\0';
00167   
00168   std::cerr << "ERROR: " << desc << std::endl
00169         << "  Error code: " << err << std::endl
00170         << "  Error desc: " << buffer << std::endl
00171         << "  At        : " << file << ':' << line << std::endl
00172     ;
00173   
00174   return false; // must always return false or CHECK macro will break
00175 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines