00001 
00009 #include "BrepLib.h"
00010 
00011 
00012 using namespace jbxl;
00013 
00014 
00016 
00017 
00018 
00019 BrepSolidList::~BrepSolidList(void)
00020 { 
00021     DEBUG_INFO("DESTRUCTOR: BrepSolidList");
00022 }
00023 
00024 
00025 
00026 void  BrepSolidList::init(void)
00027 {
00028     solid_list.clear();
00029 }
00030 
00031 
00032 
00033 void  BrepSolidList::free(void)
00034 {
00035     BREP_SOLID_LIST::iterator isolid;
00036     for (isolid=solid_list.begin(); isolid!=solid_list.end(); isolid++) freeBrepSolid(*isolid);
00037 
00038     solid_list.clear();
00039 }
00040 
00041 
00042 
00043 void  BrepSolidList::clear(void)
00044 {
00045     BREP_SOLID_LIST::iterator isolid;
00046     for (isolid=solid_list.begin(); isolid!=solid_list.end(); isolid++) (*isolid)->FreeData();
00047 
00048     solid_list.clear();
00049 }
00050 
00051 
00052 
00053 
00055 
00056 
00057 
00058 
00059 
00060 int  BrepSolidList::addSolid(MeshObjectData* mesh)
00061 {
00062     if (mesh==NULL) return 0;
00063 
00064     Vector<double> vertex[3];
00065     Vector<double> normal[3];
00066 
00067     BREP_SOLID* solid = new BREP_SOLID();
00068     BREP_SHELL* shell = new BREP_SHELL(solid);
00069 
00070     MeshObjectNode* node = mesh->nodelist;
00071 
00072     while (node!=NULL) {
00073         
00074         for (int num=0; num<node->num_index-2; num+=3) {
00075             BREP_FACET* facet = new BREP_FACET(shell);
00076             
00077             int idx0 = node->data_index[num];
00078             int idx1 = node->data_index[num+1];
00079             int idx2 = node->data_index[num+2];
00080         
00081             vertex[0] = node->vertex_value[idx0];
00082             vertex[1] = node->vertex_value[idx1];
00083             vertex[2] = node->vertex_value[idx2];
00084             normal[0] = node->normal_value[idx0];
00085             normal[1] = node->normal_value[idx1];
00086             normal[2] = node->normal_value[idx2];
00087 
00088             BREP_CONTOUR* contour = CreateContourByVector(facet, vertex, normal, NULL, false);
00089             if (contour!=NULL) {
00090                 solid->contours.push_back(contour);
00091             }
00092             else {
00093                 deleteNull(facet);
00094             }
00095         }
00096         node = node->next;
00097     }
00098 
00099     int fno = CloseTriSolid(solid, false, NULL);
00100     solid->facetno = fno;
00101 
00102     
00103     if (mesh->affine_trans!=NULL) {
00104         long int  vnum;
00105         BREP_VERTEX** vertex_data = GetOctreeVertices(solid->octree, &vnum);
00106 
00107         for (long int i=0; i<vnum; i++) {
00108             vertex_data[i]->point  = mesh->affine_trans->execTrans (vertex_data[i]->point);
00109             vertex_data[i]->normal = mesh->affine_trans->execRotate(vertex_data[i]->normal);
00110         }
00111         ::free(vertex_data);
00112     }
00113 
00114     solid_list.push_back(solid);
00115 
00116     return fno;
00117 }
00118 
00119 
00120 
00121 void  BrepSolidList::outputFile(const char* fname, const char* path, bool binfile)
00122 {
00123     char* packname = pack_head_tail_char(get_file_name(fname), ' ');
00124     Buffer file_name = make_Buffer_bystr(packname);
00125     ::free(packname);
00126 
00127     rewrite_sBuffer_bystr(&file_name, ":", "_");
00128     rewrite_sBuffer_bystr(&file_name, "*", "_");
00129     rewrite_sBuffer_bystr(&file_name, "?", "_");
00130     rewrite_sBuffer_bystr(&file_name, "\"", "_");
00131     rewrite_sBuffer_bystr(&file_name, "<", "_");
00132     rewrite_sBuffer_bystr(&file_name, ">", "_");
00133     if (file_name.buf[0]=='.') file_name.buf[0] = '_';
00134     
00135     Buffer out_path;
00136     if (path==NULL) out_path = make_Buffer_bystr("./");
00137     else            out_path = make_Buffer_bystr(path);
00138     cat_Buffer(&file_name, &out_path);
00139     change_file_extension_Buffer(&out_path, ".stl");
00140     
00141     if (binfile) {
00142         WriteSTLFileB((char*)out_path.buf, solid_list);
00143     }
00144     else {
00145         WriteSTLFileA((char*)out_path.buf, solid_list);
00146     }
00147 
00148     free_Buffer(&file_name);
00149     free_Buffer(&out_path);
00150 
00151     return;
00152 }
00153 
00154 
00155 
00156 BREP_SOLID*  BrepSolidList::getMerge(CVCounter* counter)
00157 {
00158     BREP_SOLID* solid  = new BREP_SOLID();
00159     BREP_SHELL* shell  = new BREP_SHELL(solid);
00160 
00161     
00162     if (counter!=NULL) {
00163         int maxnum = 0;
00164         BREP_SOLID_LIST::iterator isolid;
00165         for (isolid=solid_list.begin(); isolid!=solid_list.end(); isolid++){
00166             BREP_CONTOUR_LIST::iterator icon;
00167             for (icon=(*isolid)->contours.begin(); icon!=(*isolid)->contours.end(); icon++){
00168                 maxnum++;
00169             }
00170         }
00171         counter->GetUsableCounter();
00172         counter->Start(maxnum);
00173     }
00174 
00175     BREP_SOLID_LIST::iterator isolid;
00176     for (isolid=solid_list.begin(); isolid!=solid_list.end(); isolid++){
00177         
00178         BREP_CONTOUR_LIST::iterator icon;
00179         for (icon=(*isolid)->contours.begin(); icon!=(*isolid)->contours.end(); icon++){
00180             Vector<double> vect[3];
00181             Vector<double> norm[3];
00182             
00183             BREP_WING* wing = (*icon)->wing;
00184             for (int i=0; i<3; i++) {
00185                 vect[i] = wing->vertex->point;
00186                 norm[i] = wing->vertex->normal;
00187                 wing = wing->next;
00188             }
00189             
00190             BREP_FACET* facet = new BREP_FACET(shell);
00191             BREP_CONTOUR* contour = CreateContourByVector(facet, vect, norm, NULL, false);
00192             if (contour!=NULL) solid->contours.push_back(contour);
00193             else  deleteNull(facet);
00194             
00195             if (counter!=NULL) counter->StepIt();
00196         }
00197     }
00198 
00199     int fno = CloseTriSolid(solid, false, NULL);
00200     solid->facetno = fno;
00201 
00202     return solid;
00203 }
00204