JtReader.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Juergen Riegel 2007    <juergen.riegel@web.de>          *
00003  *   LGPL                                                                  *
00004  ***************************************************************************/
00005 
00006 #include <sys/types.h>
00007 #include <string.h>
00008 #include <strstream>
00009 #include <vector>
00010 #include <JtTk/JtkCADImporter.h>
00011 #include <JtTk/JtkCADExporter.h>
00012 #include <JtTk/JtkTraverser.h>
00013 #include <JtTk/JtkEntityFactory.h>
00014 #include <JtTk/JtkAttrib.h>
00015 #include <JtTk/JtkStandard.h>
00016 
00017 using std::string;
00018 using std::vector;
00019 using std::strstream;
00020 
00021 #include "JtReader.h"
00022 
00023 int my_level = 0;
00024 bool want_details = false;
00025 int iLod = 0;
00026 strstream InfoOut;
00027 
00028 vector<SimpleMeshFacet> result;
00029 vector<SimpleMeshFacet>::const_iterator resultIt;
00030 
00031 
00032 #define indent(i) {for(int l=0; l < i; l++) InfoOut << "   ";}
00033 
00034 void printXform(JtkTransform *partXform, int level)
00035 {
00036    float *elements= NULL;
00037 
00038    indent(level);
00039    InfoOut << "JtkTRANSFORM\n";
00040 
00041    partXform->getTElements(elements);
00042    if( elements )
00043    {
00044       indent(level+1);
00045       InfoOut << elements[0] << ", " << elements[1] << ", "
00046            << elements[2] << ", " << elements[3] << "\n";
00047       indent(level+1);
00048       InfoOut << elements[4] << ", " << elements[5] << ", "
00049            << elements[6] << ", " << elements[7] << "\n";
00050       indent(level+1);
00051       InfoOut << elements[8] << ", " << elements[9] << ", "
00052            << elements[10] << ", " << elements[11] << "\n";
00053       indent(level+1);
00054       InfoOut << elements[12] << ", " << elements[13] << ", "
00055            << elements[14] << ", " << elements[15] << "\n";
00056 #ifdef _DEBUG
00057       JtkEntityFactory::deleteMemory( elements );
00058 #else
00059       delete [] elements;
00060 #endif
00061    }
00062 }
00063 
00064 void printMaterial(JtkMaterial *partMaterial, int level)
00065 {
00066    float *ambient= NULL,
00067          *diffuse= NULL,
00068          *specular= NULL,
00069          *emission= NULL,
00070          shininess= -999.0;
00071 
00072    indent(level);
00073    InfoOut << "JtkMATERIAL\n";
00074 
00075    partMaterial->getAmbientColor(ambient);
00076    if( ambient )
00077    {
00078       indent(level+1);
00079       InfoOut << "ambient = ( " << ambient[0] << ", " << ambient[1] << ", "
00080                              << ambient[2] << ", " << ambient[3] << " )\n";
00081 #ifdef _DEBUG
00082       JtkEntityFactory::deleteMemory( ambient );
00083 #else
00084       delete [] ambient;
00085 #endif
00086    }
00087 
00088    partMaterial->getDiffuseColor(diffuse);
00089    if( diffuse )
00090    {
00091       indent(level+1);
00092       InfoOut << "diffuse = ( " << diffuse[0] << ", " << diffuse[1] << ", "
00093                              << diffuse[2] << ", " << diffuse[3] << " )\n";
00094 #ifdef _DEBUG
00095       JtkEntityFactory::deleteMemory( diffuse );
00096 #else
00097       delete [] diffuse;
00098 #endif
00099    }
00100 
00101    partMaterial->getSpecularColor(specular);
00102    if( specular )
00103    {
00104       indent(level+1);
00105       InfoOut << "specular = ( " << specular[0] << ", " << specular[1] << ", "
00106                               << specular[2] << ", " << specular[3] << " )\n";
00107 #ifdef _DEBUG
00108       JtkEntityFactory::deleteMemory( specular );
00109 #else
00110       delete [] specular;
00111 #endif
00112    }
00113 
00114    partMaterial->getEmissionColor(emission);
00115    if( emission )
00116    {
00117       indent(level+1);
00118       InfoOut << "emission = ( " << emission[0] << ", " << emission[1] << ", "
00119                               << emission[2] << ", " << emission[3] << " )\n";
00120 #ifdef _DEBUG
00121       JtkEntityFactory::deleteMemory( emission );
00122 #else
00123       delete [] emission;
00124 #endif
00125    }
00126 
00127    partMaterial->getShininess(shininess);
00128    if( shininess != -999.0 )
00129    {
00130       indent(level+1);
00131       InfoOut << "shininess = " << shininess << "\n";
00132    }
00133 }
00134 
00135 void printBrep(JtkBrep *partBrep, int level)
00136 {
00137    indent(level);
00138    InfoOut << "JtkBREP\n";
00139 }
00140 
00141 void printWrep(JtkWrep *partWrep, int level)
00142 {
00143    indent(level);
00144    InfoOut << "JtkWREP\n";
00145 }
00146 
00147 void printShape(JtkShape *partShape, int level)
00148 {
00149    indent(level);
00150    InfoOut << "JtkSHAPE\n";
00151 
00152    for(int set=0; set < partShape->numOfSets(); set++)
00153    {
00154       indent(level+1);
00155       InfoOut << "geom set #" << set << ":\n";
00156 
00157       float *vertex= NULL,
00158             *normal= NULL,
00159             *color= NULL,
00160             *texture= NULL;
00161       int   vertexCount= -1,
00162             normCount= -1,
00163             colorCount= -1,
00164             textCount= -1;
00165 
00166       partShape->getInternal(vertex, vertexCount, normal, normCount,
00167                              color, colorCount, texture, textCount, set);
00168 
00169       if( vertex && (vertexCount > 0) )
00170       {
00171          indent(level+2);
00172          InfoOut << "vertices = ( ";
00173 
00174          for(int elems=0; elems < vertexCount*3; elems++)
00175          {
00176             InfoOut << ((elems != 0) ? ", " : "") << vertex[elems];
00177          }
00178 
00179          InfoOut << " )\n";
00180 
00181 #ifdef _DEBUG
00182          JtkEntityFactory::deleteMemory( vertex );
00183 #else
00184          delete [] vertex;
00185 #endif
00186       }
00187 
00188       if( normal && (normCount > 0) )
00189       {
00190          indent(level+2);
00191          InfoOut << "normals = ( ";
00192 
00193          for(int elems=0; elems < normCount*3; elems++)
00194          {
00195             InfoOut << ((elems != 0) ? ", " : "") << normal[elems];
00196          }
00197 
00198          InfoOut << " )\n";
00199 
00200 #ifdef _DEBUG
00201          JtkEntityFactory::deleteMemory( normal );
00202 #else
00203          delete [] normal;
00204 #endif
00205       }
00206 
00207       if( color && (colorCount > 0) )
00208       {
00209          indent(level+2);
00210          InfoOut << "color = ( ";
00211 
00212          for(int elems=0; elems < colorCount*3; elems++)
00213          {
00214             InfoOut << ((elems != 0) ? ", " : "") << color[elems];
00215          }
00216 
00217          InfoOut << " )\n";
00218 
00219 #ifdef _DEBUG
00220          JtkEntityFactory::deleteMemory( color );
00221 #else
00222          delete [] color;
00223 #endif
00224       }
00225 
00226       if( texture && (textCount > 0) )
00227       {
00228          indent(level+2);
00229          InfoOut << "texture = ( ";
00230 
00231          for(int elems=0; elems < textCount; elems++)
00232          {
00233             InfoOut << ((elems != 0) ? ", " : "") << texture[elems];
00234          }
00235 
00236          InfoOut << " )\n";
00237 
00238 #ifdef _DEBUG
00239          JtkEntityFactory::deleteMemory( texture );
00240 #else
00241          delete [] texture;
00242 #endif
00243       }
00244    }
00245 }
00246 
00247 int myPreactionCB_PrintName(JtkHierarchy *CurrNode, int level, JtkClientData*)
00248 {
00249    indent(level);
00250 
00251    my_level++;
00252 
00253    switch (CurrNode->typeID())
00254    {
00255    case JtkEntity::JtkNONE:
00256       InfoOut << "JtkNONE\n";
00257       break;
00258 
00259    case JtkEntity::JtkBREP:
00260       InfoOut << "JtkBREP\n";
00261       break;
00262 
00263    case JtkEntity::JtkREGION:
00264       InfoOut << "JtkREGION\n";
00265       break;
00266 
00267    case JtkEntity::JtkSHELL:
00268       InfoOut << "JtkSHELL\n";
00269       break;
00270 
00271    case JtkEntity::JtkLOOP:
00272       InfoOut << "JtkLOOP\n";
00273       break;
00274 
00275    case JtkEntity::JtkCOEDGE:
00276       InfoOut << "JtkCOEDGE\n";
00277       break;
00278 
00279    case JtkEntity::JtkEDGE:
00280       InfoOut << "JtkEDGE\n";
00281       break;
00282 
00283    case JtkEntity::JtkVERTEX:
00284       InfoOut << "JtkVERTEX\n";
00285       break;
00286 
00287    case JtkEntity::JtkNURBSSURFACE:
00288       InfoOut << "JtkNURBSSURFACE\n";
00289       break;
00290 
00291    case JtkEntity::JtkUVCURVE:
00292       InfoOut << "JtkUVCURVE\n";
00293       break;
00294 
00295    case JtkEntity::JtkXYZCURVE:
00296       InfoOut << "JtkXYZCURVE\n";
00297       break;
00298 
00299    case JtkEntity::JtkTRISTRIPSET:
00300       InfoOut << "JtkTRISTRIPSET\n";
00301       break;
00302 
00303    case JtkEntity::JtkPOINTSET:
00304       InfoOut << "JtkPOINTSET\n";
00305       break;
00306 
00307    case JtkEntity::JtkLINESTRIPSET:
00308       InfoOut << "JtkLINESTRIPSET\n";
00309       break;
00310 
00311    case JtkEntity::JtkPOLYGONSET:
00312       InfoOut << "JtkPOLYGONSET\n";
00313       break;
00314 
00315    case JtkEntity::JtkPOINT:
00316       InfoOut << "JtkPOINT\n";
00317       break;
00318 
00319    case JtkEntity::JtkMATERIAL:
00320       InfoOut << "JtkMATERIAL\n";
00321       break;
00322 
00323    case JtkEntity::JtkTRANSFORM:
00324       InfoOut << "JtkTRANSFORM\n";
00325       break;
00326 
00327    case JtkEntity::JtkPROPERTY:
00328       InfoOut << "JtkPROPERTY\n";
00329       break;
00330 
00331    case JtkEntity::JtkPART:
00332       {
00333          InfoOut << "JtkPART: ";
00334          InfoOut << CurrNode->name() << "\n";
00335 
00336          if( want_details )
00337          {
00338             JtkTransform   *partXform= NULL;
00339             ((JtkPart*) CurrNode)->getTransform(partXform);
00340             if( partXform )
00341             {
00342                printXform(partXform, level+1);
00343             }
00344 
00345             JtkMaterial *partMaterial= NULL;
00346             ((JtkPart*) CurrNode)->getMaterial(partMaterial);
00347             if( partMaterial )
00348             {
00349                printMaterial(partMaterial, level+1);
00350             }
00351 
00352             JtkBrep  *partBrep= NULL;
00353             ((JtkPart*) CurrNode)->getBrep(partBrep);
00354             if( partBrep )
00355             {
00356                printBrep(partBrep, level+1);
00357             }
00358 
00359             JtkWrep  *partWrep= NULL;
00360             ((JtkPart*) CurrNode)->getWrep(partWrep);
00361             if( partWrep )
00362             {
00363                printWrep(partWrep, level+1);
00364             }
00365 
00366             int            partNumShapeLODs= -1;
00367             partNumShapeLODs= ((JtkPart*) CurrNode)->numPolyLODs();
00368             for(int lod=0; lod < partNumShapeLODs; lod++)
00369             {
00370                indent(level+1);
00371                InfoOut << "LOD#" << lod << ":\n";
00372 
00373                int   partNumShapes= -1;
00374                partNumShapes= ((JtkPart*) CurrNode)->numPolyShapes(lod);
00375                for(int shNum=0; shNum < partNumShapes; shNum++)
00376                {
00377                   indent(level+2);
00378                   InfoOut << "Shape#" << shNum << ":\n";
00379 
00380                   JtkShape *partShape= NULL;
00381                   ((JtkPart*) CurrNode)->getPolyShape(partShape, lod, shNum);
00382                   if( partShape )
00383                   {
00384                      printShape(partShape, level+3);
00385                   }
00386                }
00387             }
00388          }
00389       }
00390       break;
00391 
00392    case JtkEntity::JtkASSEMBLY:
00393       {
00394          InfoOut << "JtkASSEMBLY: ";
00395          InfoOut << CurrNode->name() << "("
00396               << ((JtkAssembly*) CurrNode)->numChildren()
00397               << " children)\n";
00398 
00399          if( want_details )
00400          {
00401             JtkTransform   *partXform= NULL;
00402             ((JtkPart*) CurrNode)->getTransform(partXform);
00403             if( partXform )
00404             {
00405                printXform(partXform, level+1);
00406             }
00407 
00408             JtkMaterial *partMaterial= NULL;
00409             ((JtkPart*) CurrNode)->getMaterial(partMaterial);
00410             if( partMaterial )
00411             {
00412                printMaterial(partMaterial, level+1);
00413             }
00414          }
00415       }
00416       break;
00417 
00418    case JtkEntity::JtkINSTANCE:
00419       {
00420          InfoOut << "JtkINSTANCE: ";
00421          InfoOut << CurrNode->name() << "\n";
00422 
00423          if( want_details )
00424          {
00425             JtkTransform   *partXform= NULL;
00426             ((JtkPart*) CurrNode)->getTransform(partXform);
00427             if( partXform )
00428             {
00429                printXform(partXform, level+1);
00430             }
00431 
00432             JtkMaterial *partMaterial= NULL;
00433             ((JtkPart*) CurrNode)->getMaterial(partMaterial);
00434             if( partMaterial )
00435             {
00436                printMaterial(partMaterial, level+1);
00437             }
00438          }
00439       }
00440       break;
00441 
00442 
00443    case JtkEntity::JtkCLIENTDATA:
00444       InfoOut << "JtkCLIENTDATA\n";
00445       break;
00446 
00447    case JtkEntity::JtkWIRE:
00448       InfoOut << "JtkWIRE\n";
00449       break;
00450    }
00451 
00452    return( Jtk_OK );
00453 }
00454 
00455 void insertShapeFaces(JtkShape *partShape)
00456 {
00457   for(int set=0; set < partShape->numOfSets(); set++)
00458   {
00459       float *vertex= NULL,
00460             *normal= NULL,
00461             *color= NULL,
00462             *texture= NULL;
00463       int   vertexCount= -1,
00464             normCount= -1,
00465             colorCount= -1,
00466             textCount= -1;
00467 
00468       partShape->getInternal(vertex, vertexCount, normal, normCount, color, colorCount, texture, textCount, set);
00469 
00470       if(normCount < 3)
00471         return;
00472 
00473 
00474       if( vertex && (vertexCount > 0) && normal && (normCount > 0) )
00475       {
00476         for(int i = 0 ; i< vertexCount-2; i++)
00477         { 
00478           SimpleMeshFacet temp;
00479           temp.n[0] = normal[i*3+0];
00480           temp.n[1] = normal[i*3+1];
00481           temp.n[2] = normal[i*3+2];
00482           temp.p1[0]= vertex[i*3+0];
00483           temp.p1[1]= vertex[i*3+1];
00484           temp.p1[2]= vertex[i*3+2];
00485           temp.p2[0]= vertex[i*3+3];
00486           temp.p2[1]= vertex[i*3+4];
00487           temp.p2[2]= vertex[i*3+5];
00488           temp.p3[0]= vertex[i*3+6];
00489           temp.p3[1]= vertex[i*3+7];
00490           temp.p3[2]= vertex[i*3+8];
00491 
00492           result.push_back(temp);
00493 /*
00494           file << "  facet normal "<< normal[i*3+0] << " " << normal[i*3+1] << " " << normal[i*3+2] << " " << endl;
00495           file << "    outer loop" << endl;
00496           file << "      vertex " << vertex[i*3+0] << " " << vertex[i*3+1] << " " << vertex[i*3+2] << " " << endl;
00497           file << "      vertex " << vertex[i*3+3] << " " << vertex[i*3+4] << " " << vertex[i*3+5] << " " << endl;
00498           file << "      vertex " << vertex[i*3+6] << " " << vertex[i*3+7] << " " << vertex[i*3+8] << " " << endl;
00499           file << "    endloop" << endl;
00500           file << "  endfacet" << endl;
00501 */  
00502   
00503         }
00504 
00505       }
00506 #ifdef _DEBUG
00507         if (vertex) JtkEntityFactory::deleteMemory( vertex );
00508         if (vertex) JtkEntityFactory::deleteMemory( normal );
00509         if (color)  JtkEntityFactory::deleteMemory( color );
00510         if (texture) JtkEntityFactory::deleteMemory( texture );
00511 #else
00512         if (vertex) delete [] vertex;
00513         if (vertex) delete [] normal;
00514         if (color)  delete [] color;
00515         if (texture)delete [] texture;
00516 #endif
00517      
00518    }
00519 
00520 }
00521 
00522 
00523 
00524 int myPreactionCB_CollectFacets(JtkHierarchy *CurrNode, int level, JtkClientData*)
00525 {
00526    switch (CurrNode->typeID())
00527    {
00528 
00529    case JtkEntity::JtkPART:
00530       {
00531      
00532          {
00533             JtkTransform   *partXform= NULL;
00534             ((JtkPart*) CurrNode)->getTransform(partXform);
00535             if( partXform )
00536             {
00537                printXform(partXform, level+1);
00538             }
00539 
00540             int            partNumShapeLODs= -1;
00541             partNumShapeLODs= ((JtkPart*) CurrNode)->numPolyLODs();
00542             for(int lod=0; lod < partNumShapeLODs; lod++)
00543             {
00544                indent(level+1);
00545                InfoOut << "LOD#" << lod << ":\n";
00546 
00547                if(iLod!=lod && iLod!=-1)
00548                  continue;
00549 
00550                int   partNumShapes= -1;
00551                partNumShapes= ((JtkPart*) CurrNode)->numPolyShapes(lod);
00552                for(int shNum=0; shNum < partNumShapes; shNum++)
00553                {
00554                   indent(level+2);
00555                   InfoOut << "Shape#" << shNum << ":\n";
00556 
00557                   JtkShape *partShape= NULL;
00558                   ((JtkPart*) CurrNode)->getPolyShape(partShape, lod, shNum);
00559                   if( partShape )
00560                   {
00561                     insertShapeFaces(partShape);
00562                   }
00563                }
00564             }
00565          }
00566       }
00567       break;
00568 
00569    case JtkEntity::JtkASSEMBLY:
00570       {
00571          InfoOut << "JtkASSEMBLY: ";
00572          InfoOut << CurrNode->name() << "("
00573               << ((JtkAssembly*) CurrNode)->numChildren()
00574               << " children)\n";
00575 
00576          {
00577             JtkTransform   *partXform= NULL;
00578             ((JtkPart*) CurrNode)->getTransform(partXform);
00579             if( partXform )
00580             {
00581                
00582             }
00583 
00584         }
00585       }
00586       break;
00587 
00588    case JtkEntity::JtkINSTANCE:
00589       {
00590          {
00591             JtkTransform   *partXform= NULL;
00592             ((JtkPart*) CurrNode)->getTransform(partXform);
00593             if( partXform )
00594             {
00595                
00596             }
00597 
00598          }
00599       }
00600       break;
00601 
00602    }
00603    return( Jtk_OK );
00604 }
00605 
00606 /* Interface functions */
00607 
00608 void testLicence(void)
00609 {
00610   // Try to create an JtkCADExporter to test for JT write/general licensing
00611    JtkCADExporter *jtwriter = NULL;
00612    jtwriter = JtkEntityFactory::createCADExporter();
00613    if( !jtwriter )
00614    {
00615       cerr << "No export license found.\n";
00616       exit(1);
00617    }
00618    else
00619    {
00620       jtwriter->ref();
00621       jtwriter->unref();
00622       jtwriter = NULL;
00623    }
00624 
00625    // Try to create an JtkCADImporter to test for JT read licensing
00626    JtkCADImporter *jtreader = NULL;
00627    jtreader = JtkEntityFactory::createCADImporter();
00628    if( !jtreader )
00629    {
00630       cerr << "No import license found.\n";
00631       exit(1);
00632    }
00633    else
00634    {
00635       jtreader->ref();
00636       jtreader->unref();
00637       jtreader = NULL;
00638    }
00639 }
00640 
00641 void readFile(const char* FileName, int iLods)
00642 {
00643    iLod = iLods;
00644 
00645    testLicence();
00646 
00647    JtkCADImporter* importer= NULL;
00648    importer= JtkEntityFactory::createCADImporter();
00649    if( importer )
00650    {
00651       importer->ref();
00652       importer->setShapeLoadOption(JtkCADImporter::JtkALL_LODS);
00653       importer->setBrepLoadOption(JtkCADImporter::JtkTESS_AND_BREP);
00654       JtkHierarchy *root= NULL;
00655 
00656       root= importer->import(FileName);
00657 
00658       if( root ) {
00659         root->ref();
00660         JtkTraverser* trav = JtkEntityFactory::createTraverser();
00661         trav->setupPreActionCallback(myPreactionCB_CollectFacets);
00662         if( trav ) {
00663             trav->ref();
00664             trav->traverseGraph(root);
00665             trav->unref();
00666             trav = NULL;
00667         } else {
00668             throw "Unable to create JtkTraverser.\n";
00669         }
00670 
00671         root->unref();
00672         root = NULL;
00673       }else{
00674         throw "Unable in find root node.  Check file...\n";
00675       }
00676     
00677       importer->unref();
00678       importer = NULL;
00679     }
00680     else
00681     {
00682         throw "Unable to create JtkCADImporter.  Check license...\n";
00683     }
00684     
00685    // Uninitialize JtTk
00686    //JtkEntityFactory::fini();
00687 
00688 
00689 }
00690 
00691 
00692 
00693 
00694 const SimpleMeshFacet* iterStart(void)
00695 {
00696   resultIt = result.begin();
00697   if(resultIt != result.end())
00698     return &(*(resultIt));
00699   else
00700     return 0;
00701 }
00702 
00703 const SimpleMeshFacet* iterGetNext(void)
00704 {
00705   if(++resultIt != result.end())
00706     return &(*(resultIt));
00707   else
00708     return 0;
00709 }
00710 
00711 unsigned int iterSize(void)
00712 {
00713   return (unsigned int) result.size();
00714 }
00715 
00716 
00718 void clearData(void)
00719 {
00720   result.clear();
00721   resultIt = result.begin();
00722   InfoOut.clear();
00723   my_level = 0;
00724 }
00725 

Generated on Wed Nov 23 19:00:20 2011 for FreeCAD by  doxygen 1.6.1