1 // Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //=============================================================================
20 // File : GHS3DPlugin_GHS3D.cxx
22 // Author : Edward AGAPOV, modified by Lioka RAZAFINDRAZAKA (CEA) 09/02/2007
24 // Copyright : CEA 2003
26 //=============================================================================
29 #include "GHS3DPlugin_GHS3D.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_Mesh.hxx"
32 #include "SMESH_Comment.hxx"
33 #include "SMESH_MesherHelper.hxx"
35 #include "SMDS_MeshElement.hxx"
36 #include "SMDS_MeshNode.hxx"
39 #include <TopExp_Explorer.hxx>
40 #include <OSD_File.hxx>
42 #include "utilities.h"
45 #include <sys/sysinfo.h>
48 //#include <Standard_Stream.hxx>
50 #include <BRepGProp.hxx>
51 #include <BRepBndLib.hxx>
52 #include <BRepClass_FaceClassifier.hxx>
53 #include <BRepClass3d_SolidClassifier.hxx>
55 #include <Bnd_Box.hxx>
56 #include <GProp_GProps.hxx>
57 #include <Precision.hxx>
59 #define castToNode(n) static_cast<const SMDS_MeshNode *>( n );
78 //=============================================================================
82 //=============================================================================
84 GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D(int hypId, int studyId, SMESH_Gen* gen)
85 : SMESH_3D_Algo(hypId, studyId, gen)
87 MESSAGE("GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D");
89 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
90 _onlyUnaryInput = false; // Compute() will be called on a compound of solids
95 //=============================================================================
99 //=============================================================================
101 GHS3DPlugin_GHS3D::~GHS3DPlugin_GHS3D()
103 MESSAGE("GHS3DPlugin_GHS3D::~GHS3DPlugin_GHS3D");
106 //=============================================================================
110 //=============================================================================
112 bool GHS3DPlugin_GHS3D::CheckHypothesis ( SMESH_Mesh& aMesh,
113 const TopoDS_Shape& aShape,
114 SMESH_Hypothesis::Hypothesis_Status& aStatus )
116 // MESSAGE("GHS3DPlugin_GHS3D::CheckHypothesis");
117 aStatus = SMESH_Hypothesis::HYP_OK;
121 //=======================================================================
122 //function : findShape
124 //=======================================================================
126 static TopoDS_Shape findShape(const SMDS_MeshNode *aNode[],
128 const TopoDS_Shape shape[],
132 int iShape, nbNode = 4;
134 pntCoor = new double[3];
135 for ( int i=0; i<3; i++ ) {
137 for ( int j=0; j<nbNode; j++ ) {
138 if ( i == 0) pntCoor[i] += aNode[j]->X();
139 if ( i == 1) pntCoor[i] += aNode[j]->Y();
140 if ( i == 2) pntCoor[i] += aNode[j]->Z();
142 pntCoor[i] /= nbNode;
145 gp_Pnt aPnt(pntCoor[0], pntCoor[1], pntCoor[2]);
146 BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
147 if ( not(SC.State() == TopAbs_IN) ) {
148 for (iShape = 0; iShape < nShape; iShape++) {
149 aShape = shape[iShape];
150 if ( not( pntCoor[0] < box[iShape][0] || box[iShape][1] < pntCoor[0] ||
151 pntCoor[1] < box[iShape][2] || box[iShape][3] < pntCoor[1] ||
152 pntCoor[2] < box[iShape][4] || box[iShape][5] < pntCoor[2]) ) {
153 BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
154 if (SC.State() == TopAbs_IN)
162 //=======================================================================
163 //function : readMapIntLine
165 //=======================================================================
167 static char* readMapIntLine(char* ptr, int tab[]) {
171 for ( int i=0; i<17; i++ ) {
172 intVal = strtol(ptr, &ptr, 10);
179 //=======================================================================
180 //function : readLine
182 //=======================================================================
184 #define GHS3DPlugin_BUFLENGTH 256
185 #define GHS3DPlugin_ReadLine(aPtr,aBuf,aFile,aLineNb) \
186 { aPtr = fgets( aBuf, GHS3DPlugin_BUFLENGTH - 2, aFile ); aLineNb++; DUMP(endl); }
188 //=======================================================================
189 //function : countShape
191 //=======================================================================
193 template < class Mesh, class Shape >
194 static int countShape( Mesh* mesh, Shape shape ) {
195 TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
197 for ( ; expShape.More(); expShape.Next() ) {
203 //=======================================================================
204 //function : writeFaces
206 //=======================================================================
208 static bool writeFaces (ofstream & theFile,
209 SMESHDS_Mesh * theMesh,
210 const map <int,int> & theSmdsToGhs3dIdMap)
214 // NB_ELEMS DUMMY_INT
215 // Loop from 1 to NB_ELEMS
216 // NB_NODES NODE_NB_1 NODE_NB_2 ... (NB_NODES + 1) times: DUMMY_INT
218 int nbShape = countShape( theMesh, TopAbs_FACE );
220 int *tabID; tabID = new int[nbShape];
221 TopoDS_Shape *tabShape; tabShape = new TopoDS_Shape[nbShape];
223 SMESHDS_SubMesh* theSubMesh;
224 const SMDS_MeshElement* aFace;
225 const char* space = " ";
226 const int dummyint = 0;
227 map<int,int>::const_iterator itOnMap;
228 SMDS_ElemIteratorPtr itOnSubMesh, itOnSubFace;
229 int shapeID, nbNodes, aSmdsID;
232 cout << " " << theMesh->NbFaces() << " shapes of 2D dimension" << endl;
235 theFile << space << theMesh->NbFaces() << space << dummyint << endl;
237 TopExp_Explorer expface( theMesh->ShapeToMesh(), TopAbs_FACE );
238 for ( int i = 0; expface.More(); expface.Next(), i++ ) {
240 aShape = expface.Current();
241 shapeID = theMesh->ShapeToIndex( aShape );
243 for ( int j=0; j<=i; j++) {
244 if ( shapeID == tabID[j] ) {
251 tabShape[i] = aShape;
254 for ( int i =0; i < nbShape; i++ ) {
255 if ( not (tabID[i] == 0) ) {
256 aShape = tabShape[i];
258 theSubMesh = theMesh->MeshElements( aShape );
259 itOnSubMesh = theSubMesh->GetElements();
260 while ( itOnSubMesh->more() ) {
261 aFace = itOnSubMesh->next();
262 nbNodes = aFace->NbNodes();
264 theFile << space << nbNodes;
266 itOnSubFace = aFace->nodesIterator();
267 while ( itOnSubFace->more() ) {
269 aSmdsID = itOnSubFace->next()->GetID();
270 itOnMap = theSmdsToGhs3dIdMap.find( aSmdsID );
271 ASSERT( itOnMap != theSmdsToGhs3dIdMap.end() );
273 theFile << space << (*itOnMap).second;
276 // (NB_NODES + 1) times: DUMMY_INT
277 for ( int j=0; j<=nbNodes; j++)
278 theFile << space << dummyint;
291 //=======================================================================
292 //function : writeFaces
293 //purpose : Write Faces in case if generate 3D mesh w/o geometry
294 //=======================================================================
296 static bool writeFaces (ofstream & theFile,
297 SMESHDS_Mesh * theMesh,
298 vector <const SMDS_MeshNode*> & theNodeByGhs3dId)
302 // NB_ELEMS DUMMY_INT
303 // Loop from 1 to NB_ELEMS
304 // NB_NODES NODE_NB_1 NODE_NB_2 ... (NB_NODES + 1) times: DUMMY_INT
308 list< const SMDS_MeshElement* > faces;
309 list< const SMDS_MeshElement* >::iterator f;
310 map< const SMDS_MeshNode*,int >::iterator it;
311 SMDS_ElemIteratorPtr nodeIt;
312 const SMDS_MeshElement* elem;
315 const char* space = " ";
316 const int dummyint = 0;
318 //get all faces from mesh
319 SMDS_FaceIteratorPtr eIt = theMesh->facesIterator();
320 while ( eIt->more() ) {
321 const SMDS_MeshElement* elem = eIt->next();
324 faces.push_back( elem );
331 cout << "The initial 2D mesh contains " << nbFaces << " faces and ";
333 // NB_ELEMS DUMMY_INT
334 theFile << space << nbFaces << space << dummyint << endl;
336 // Loop from 1 to NB_ELEMS
338 map<const SMDS_MeshNode*,int> aNodeToGhs3dIdMap;
340 for ( ; f != faces.end(); ++f )
344 nbNodes = elem->NbNodes();
345 theFile << space << nbNodes;
347 // NODE_NB_1 NODE_NB_2 ...
348 nodeIt = elem->nodesIterator();
349 while ( nodeIt->more() )
352 const SMDS_MeshNode* node = castToNode( nodeIt->next() );
353 int newId = aNodeToGhs3dIdMap.size() + 1; // ghs3d ids count from 1
354 it = aNodeToGhs3dIdMap.insert( make_pair( node, newId )).first;
355 theFile << space << it->second;
358 // (NB_NODES + 1) times: DUMMY_INT
359 for ( int i=0; i<=nbNodes; i++)
360 theFile << space << dummyint;
365 // put nodes to theNodeByGhs3dId vector
366 theNodeByGhs3dId.resize( aNodeToGhs3dIdMap.size() );
367 map<const SMDS_MeshNode*,int>::const_iterator n2id = aNodeToGhs3dIdMap.begin();
368 for ( ; n2id != aNodeToGhs3dIdMap.end(); ++ n2id)
370 theNodeByGhs3dId[ n2id->second - 1 ] = n2id->first; // ghs3d ids count from 1
376 //=======================================================================
377 //function : writePoints
379 //=======================================================================
381 static bool writePoints (ofstream & theFile,
382 SMESHDS_Mesh * theMesh,
383 map <int,int> & theSmdsToGhs3dIdMap,
384 map <int,const SMDS_MeshNode*> & theGhs3dIdToNodeMap)
389 // Loop from 1 to NB_NODES
392 int nbNodes = theMesh->NbNodes();
396 const char* space = " ";
397 const int dummyint = 0;
400 SMDS_NodeIteratorPtr it = theMesh->nodesIterator();
401 const SMDS_MeshNode* node;
404 theFile << space << nbNodes << endl;
406 cout << "The initial 2D mesh contains :" << endl;
407 cout << " " << nbNodes << " vertices" << endl;
409 // Loop from 1 to NB_NODES
414 theSmdsToGhs3dIdMap.insert( map <int,int>::value_type( node->GetID(), aGhs3dID ));
415 theGhs3dIdToNodeMap.insert (map <int,const SMDS_MeshNode*>::value_type( aGhs3dID, node ));
420 << space << node->X()
421 << space << node->Y()
422 << space << node->Z()
423 << space << dummyint;
431 //=======================================================================
432 //function : writePoints
434 //=======================================================================
436 static bool writePoints (ofstream & theFile,
437 SMESHDS_Mesh * theMesh,
438 const vector <const SMDS_MeshNode*> & theNodeByGhs3dId)
443 // Loop from 1 to NB_NODES
446 //int nbNodes = theMesh->NbNodes();
447 int nbNodes = theNodeByGhs3dId.size();
451 const char* space = " ";
452 const int dummyint = 0;
454 const SMDS_MeshNode* node;
457 theFile << space << nbNodes << endl;
458 cout << nbNodes << " nodes" << endl;
460 // Loop from 1 to NB_NODES
462 vector<const SMDS_MeshNode*>::const_iterator nodeIt = theNodeByGhs3dId.begin();
463 vector<const SMDS_MeshNode*>::const_iterator after = theNodeByGhs3dId.end();
464 for ( ; nodeIt != after; ++nodeIt )
470 << space << node->X()
471 << space << node->Y()
472 << space << node->Z()
473 << space << dummyint;
481 //=======================================================================
482 //function : readResultFile
484 //=======================================================================
486 static bool readResultFile(const int fileOpen,
487 SMESHDS_Mesh* theMeshDS,
488 TopoDS_Shape tabShape[],
491 map <int,const SMDS_MeshNode*>& theGhs3dIdToNodeMap) {
501 int nbElems, nbNodes, nbInputNodes;
502 int nodeId, triangleId;
504 int ID, shapeID, ghs3dShapeID;
506 int compoundID = theMeshDS->ShapeToIndex( theMeshDS->ShapeToMesh() );
508 int *tab, *tabID, *nodeID, *nodeAssigne;
510 const SMDS_MeshNode **node;
513 tabID = new int[nbShape];
515 coord = new double[3];
516 node = new const SMDS_MeshNode*[4];
519 SMDS_MeshNode * aNewNode;
520 map <int,const SMDS_MeshNode*>::iterator itOnNode;
521 SMDS_MeshElement* aTet;
523 for (int i=0; i<nbShape; i++)
526 // Read the file state
527 fileStat = fstat(fileOpen, &status);
528 length = status.st_size;
530 // Mapping the result file into memory
531 ptr = (char *) mmap(0,length,PROT_READ,MAP_PRIVATE,fileOpen,0);
534 ptr = readMapIntLine(ptr, tab);
539 nbInputNodes = tab[2];
541 nodeAssigne = new int[ nbNodes+1 ];
543 // Reading the nodeId
544 for (int i=0; i < 4*nbElems; i++)
545 nodeId = strtol(ptr, &ptr, 10);
547 // Reading the nodeCoor and update the nodeMap
548 for (int iNode=0; iNode < nbNodes; iNode++) {
549 for (int iCoor=0; iCoor < 3; iCoor++)
550 coord[ iCoor ] = strtod(ptr, &ptr);
551 nodeAssigne[ iNode+1 ] = 1;
552 if ( (iNode+1) > nbInputNodes ) {
553 nodeAssigne[ iNode+1 ] = 0;
554 aNewNode = theMeshDS->AddNode( coord[0],coord[1],coord[2] );
555 theGhs3dIdToNodeMap.insert(make_pair( (iNode+1), aNewNode ));
559 // Reading the number of triangles which corresponds to the number of shapes
560 nbTriangle = strtol(ptr, &ptr, 10);
562 for (int i=0; i < 3*nbShape; i++)
563 triangleId = strtol(ptr, &ptr, 10);
567 // Associating the tetrahedrons to the shapes
568 shapeID = compoundID;
569 for (int iElem = 0; iElem < nbElems; iElem++) {
570 for (int iNode = 0; iNode < 4; iNode++) {
571 ID = strtol(tetraPtr, &tetraPtr, 10);
572 itOnNode = theGhs3dIdToNodeMap.find(ID);
573 node[ iNode ] = itOnNode->second;
574 nodeID[ iNode ] = ID;
576 aTet = theMeshDS->AddVolume( node[1], node[0], node[2], node[3] );
578 ghs3dShapeID = strtol(shapePtr, &shapePtr, 10) - IdShapeRef;
579 if ( tabID[ ghs3dShapeID ] == 0 ) {
581 aSolid = tabShape[0];
582 aSolid = findShape(node, aSolid, tabShape, tabBox, nbShape);
583 shapeID = theMeshDS->ShapeToIndex( aSolid );
584 tabID[ ghs3dShapeID ] = shapeID;
587 shapeID = tabID[ ghs3dShapeID ];
589 // set new nodes and tetrahedron on to the shape
590 for ( int i=0; i<4; i++ ) {
591 if ( nodeAssigne[ nodeID[i] ] == 0 )
592 theMeshDS->SetNodeInVolume( node[i], shapeID );
594 theMeshDS->SetMeshElementOnShape( aTet, shapeID );
595 if ( (iElem + 1) == nbElems )
596 cout << nbElems << " tetrahedrons have been associated to " << nbShape << " shapes" << endl;
598 munmap(mapPtr, length);
610 //=======================================================================
611 //function : readResultFile
613 //=======================================================================
615 static bool readResultFile(const int fileOpen,
616 SMESHDS_Mesh* theMeshDS,
618 vector <const SMDS_MeshNode*>& theNodeByGhs3dId) {
628 int nbElems, nbNodes, nbInputNodes;
629 int nodeId, triangleId;
635 const SMDS_MeshNode **node;
638 coord = new double[3];
639 node = new const SMDS_MeshNode*[4];
641 SMDS_MeshNode * aNewNode;
642 map <int,const SMDS_MeshNode*>::iterator IdNode;
643 SMDS_MeshElement* aTet;
645 // Read the file state
646 fileStat = fstat(fileOpen, &status);
647 length = status.st_size;
649 // Mapping the result file into memory
650 ptr = (char *) mmap(0,length,PROT_READ,MAP_PRIVATE,fileOpen,0);
653 ptr = readMapIntLine(ptr, tab);
658 nbInputNodes = tab[2];
660 theNodeByGhs3dId.resize( nbNodes );
662 // Reading the nodeId
663 for (int i=0; i < 4*nbElems; i++)
664 nodeId = strtol(ptr, &ptr, 10);
666 // Reading the nodeCoor and update the nodeMap
667 shapeID = theMeshDS->ShapeToIndex( aSolid );
668 for (int iNode=0; iNode < nbNodes; iNode++) {
669 for (int iCoor=0; iCoor < 3; iCoor++)
670 coord[ iCoor ] = strtod(ptr, &ptr);
671 if ((iNode+1) > nbInputNodes) {
672 aNewNode = theMeshDS->AddNode( coord[0],coord[1],coord[2] );
673 theMeshDS->SetNodeInVolume( aNewNode, shapeID );
674 theNodeByGhs3dId[ iNode ] = aNewNode;
678 // Reading the triangles
679 nbTriangle = strtol(ptr, &ptr, 10);
681 for (int i=0; i < 3*nbTriangle; i++)
682 triangleId = strtol(ptr, &ptr, 10);
686 // Associating the tetrahedrons to the shapes
687 for (int iElem = 0; iElem < nbElems; iElem++) {
688 for (int iNode = 0; iNode < 4; iNode++) {
689 ID = strtol(tetraPtr, &tetraPtr, 10);
690 node[ iNode ] = theNodeByGhs3dId[ ID-1 ];
692 aTet = theMeshDS->AddVolume( node[1], node[0], node[2], node[3] );
693 shapeID = theMeshDS->ShapeToIndex( aSolid );
694 theMeshDS->SetMeshElementOnShape( aTet, shapeID );
697 cout << nbElems << " tetrahedrons have been associated to " << nbTriangle << " shapes" << endl;
698 munmap(mapPtr, length);
708 //=======================================================================
709 //function : getTmpDir
711 //=======================================================================
713 static TCollection_AsciiString getTmpDir()
715 TCollection_AsciiString aTmpDir;
717 char *Tmp_dir = getenv("SALOME_TMP_DIR");
718 if(Tmp_dir != NULL) {
721 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
723 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
728 aTmpDir = TCollection_AsciiString("C:\\");
730 aTmpDir = TCollection_AsciiString("/tmp/");
736 //================================================================================
738 * \brief Look for a line containing a text in a file
739 * \retval bool - true if the line is found
741 //================================================================================
743 static bool findLineContaing(const TCollection_AsciiString& theText,
744 const TCollection_AsciiString& theFile,
745 TCollection_AsciiString & theFoundLine)
748 if ( FILE * aFile = fopen( theFile.ToCString(), "r" ))
751 char aBuffer[ GHS3DPlugin_BUFLENGTH ];
754 GHS3DPlugin_ReadLine( aPtr, aBuffer, aFile, aLineNb );
757 found = theFoundLine.Search( theText ) >= 0;
759 } while ( aPtr && !found );
766 //=============================================================================
768 *Here we are going to use the GHS3D mesher
770 //=============================================================================
772 bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh,
773 const TopoDS_Shape& theShape)
776 SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
778 _nbShape = countShape( meshDS, TopAbs_SOLID ); // we count the number of shapes
780 // create bounding box for every shape inside the compound
783 TopoDS_Shape* tabShape;
785 tabShape = new TopoDS_Shape[_nbShape];
786 tabBox = new double*[_nbShape];
787 for (int i=0; i<_nbShape; i++)
788 tabBox[i] = new double[6];
789 Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
791 TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID);
792 for (; expBox.More(); expBox.Next()) {
793 tabShape[iShape] = expBox.Current();
795 BRepBndLib::Add(expBox.Current(), BoundingBox);
796 BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
797 tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
798 tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
799 tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
804 cout << "Ghs3d execution..." << endl;
806 // make a unique working file name
807 // to avoid access to the same files by eg different users
809 TCollection_AsciiString aGenericName, aTmpDir = getTmpDir();
810 aGenericName = aTmpDir + "GHS3D_";
812 aGenericName += GetCurrentProcessId();
814 aGenericName += getpid();
817 aGenericName += meshDS->ShapeToIndex( theShape );
819 TCollection_AsciiString aFacesFileName, aPointsFileName, aResultFileName;
820 TCollection_AsciiString aBadResFileName, aBbResFileName, aLogFileName;
821 aFacesFileName = aGenericName + ".faces"; // in faces
822 aPointsFileName = aGenericName + ".points"; // in points
823 aResultFileName = aGenericName + ".noboite";// out points and volumes
824 aBadResFileName = aGenericName + ".boite"; // out bad result
825 aBbResFileName = aGenericName + ".bb"; // out vertex stepsize
826 aLogFileName = aGenericName + ".log"; // log
832 ofstream aFacesFile ( aFacesFileName.ToCString() , ios::out);
833 ofstream aPointsFile ( aPointsFileName.ToCString() , ios::out);
837 aFacesFile->is_open() && aPointsFile->is_open();
839 aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open();
842 INFOS( "Can't write into " << aTmpDir.ToCString());
843 return error(SMESH_Comment("Can't write into ") << aTmpDir);
845 map <int,int> aSmdsToGhs3dIdMap;
846 map <int,const SMDS_MeshNode*> aGhs3dIdToNodeMap;
848 Ok = writePoints( aPointsFile, meshDS, aSmdsToGhs3dIdMap, aGhs3dIdToNodeMap ) &&
849 writeFaces ( aFacesFile, meshDS, aSmdsToGhs3dIdMap );
855 if ( !getenv("GHS3D_KEEP_FILES") ) {
856 OSD_File( aFacesFileName ).Remove();
857 OSD_File( aPointsFileName ).Remove();
859 return error(COMPERR_BAD_INPUT_MESH);
863 // run ghs3d mesher WIN32???
866 // ghs3d need to know amount of memory it may use (MB).
867 // Default memory is defined at ghs3d installation but it may be not enough,
868 // so allow to use about all available memory
870 TCollection_AsciiString memory;
873 int err = sysinfo( &si );
875 int freeMem = si.totalram * si.mem_unit / 1024 / 1024;
877 memory += int( 0.7 * freeMem );
881 MESSAGE("GHS3DPlugin_GHS3D::Compute");
882 TCollection_AsciiString cmd( "ghs3d " ); // command to run
885 " -c0 -f " + aGenericName + // file to read
886 " 1>" + aLogFileName; // dump into file
888 system( cmd.ToCString() ); // run
891 cout << "End of Ghs3d execution !" << endl;
897 // Mapping the result file
900 fileOpen = open( aResultFileName.ToCString(), O_RDONLY);
901 if ( fileOpen < 0 ) {
903 cout << "Can't open the " << aResultFileName.ToCString() << " GHS3D output file" << endl;
907 Ok = readResultFile( fileOpen, meshDS, tabShape, tabBox, _nbShape, aGhs3dIdToNodeMap );
909 // ---------------------
910 // remove working files
911 // ---------------------
915 OSD_File( aLogFileName ).Remove();
917 else if ( OSD_File( aLogFileName ).Size() > 0 )
919 INFOS( "GHS3D Error, see the " << aLogFileName.ToCString() << " file" );
921 // get problem description from the log file
922 SMESH_Comment comment;
923 TCollection_AsciiString foundLine;
924 if ( findLineContaing( "has expired",aLogFileName,foundLine) &&
925 foundLine.Search("Licence") >= 0)
927 foundLine.LeftAdjust();
928 comment << foundLine;
930 if ( findLineContaing( "%% ERROR",aLogFileName,foundLine))
932 foundLine.LeftAdjust();
933 comment << foundLine;
935 if ( findLineContaing( "%% NO SAVING OPERATION",aLogFileName,foundLine))
937 comment << "Too many elements generated for a trial version.\n";
939 if ( comment.empty() )
940 comment << "See " << aLogFileName << " for problem description";
942 comment << "See " << aLogFileName << " for more information";
943 error(COMPERR_ALGO_FAILED, comment);
947 // the log file is empty
948 OSD_File( aLogFileName ).Remove();
949 INFOS( "GHS3D Error, command '" << cmd.ToCString() << "' failed" );
950 error(COMPERR_ALGO_FAILED, "ghs3d: command not found" );
953 if ( !getenv("GHS3D_KEEP_FILES") ) {
954 OSD_File( aFacesFileName ).Remove();
955 OSD_File( aPointsFileName ).Remove();
956 OSD_File( aResultFileName ).Remove();
957 OSD_File( aBadResFileName ).Remove();
958 OSD_File( aBbResFileName ).Remove();
960 cout << "<" << aResultFileName.ToCString() << "> GHS3D output file ";
963 cout << "treated !" << endl;
966 _nbShape = 0; // re-initializing _nbShape for the next Compute() method call
973 //=============================================================================
975 *Here we are going to use the GHS3D mesher w/o geometry
977 //=============================================================================
978 bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh,
979 SMESH_MesherHelper* aHelper)
981 MESSAGE("GHS3DPlugin_GHS3D::Compute()");
983 SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
984 TopoDS_Shape theShape = aHelper->GetSubShape();
986 // make a unique working file name
987 // to avoid access to the same files by eg different users
989 TCollection_AsciiString aGenericName, aTmpDir = getTmpDir();
990 aGenericName = aTmpDir + "GHS3D_";
992 aGenericName += GetCurrentProcessId();
994 aGenericName += getpid();
997 aGenericName += meshDS->ShapeToIndex( theShape );
999 TCollection_AsciiString aFacesFileName, aPointsFileName, aResultFileName;
1000 TCollection_AsciiString aBadResFileName, aBbResFileName, aLogFileName;
1001 aFacesFileName = aGenericName + ".faces"; // in faces
1002 aPointsFileName = aGenericName + ".points"; // in points
1003 aResultFileName = aGenericName + ".noboite";// out points and volumes
1004 aBadResFileName = aGenericName + ".boite"; // out bad result
1005 aBbResFileName = aGenericName + ".bb"; // out vertex stepsize
1006 aLogFileName = aGenericName + ".log"; // log
1008 // -----------------
1010 // -----------------
1012 ofstream aFacesFile ( aFacesFileName.ToCString() , ios::out);
1013 ofstream aPointsFile ( aPointsFileName.ToCString() , ios::out);
1016 aFacesFile->is_open() && aPointsFile->is_open();
1018 aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open();
1022 return error( SMESH_Comment("Can't write into ") << aTmpDir.ToCString());
1024 vector <const SMDS_MeshNode*> aNodeByGhs3dId;
1026 Ok = (writeFaces ( aFacesFile, meshDS, aNodeByGhs3dId ) &&
1027 writePoints( aPointsFile, meshDS, aNodeByGhs3dId));
1030 aPointsFile.close();
1033 if ( !getenv("GHS3D_KEEP_FILES") ) {
1034 OSD_File( aFacesFileName ).Remove();
1035 OSD_File( aPointsFileName ).Remove();
1037 return error(COMPERR_BAD_INPUT_MESH);
1040 // -----------------
1041 // run ghs3d mesher WIN32???
1042 // -----------------
1044 // ghs3d need to know amount of memory it may use (MB).
1045 // Default memory is defined at ghs3d installation but it may be not enough,
1046 // so allow to use about all available memory
1047 TCollection_AsciiString memory;
1052 int err = sysinfo( &si );
1054 int freeMem = si.totalram * si.mem_unit / 1024 / 1024;
1056 memory += int( 0.7 * freeMem );
1060 TCollection_AsciiString cmd( "ghs3d " ); // command to run
1063 " -f " + aGenericName + // file to read
1064 " 1>" + aLogFileName; // dump into file
1068 system( cmd.ToCString() ); // run
1074 fileOpen = open( aResultFileName.ToCString(), O_RDONLY);
1075 if ( fileOpen < 0 ) {
1077 cout << "Error when opening the " << aResultFileName.ToCString() << " file" << endl;
1082 Ok = readResultFile( fileOpen, meshDS, theShape ,aNodeByGhs3dId );
1085 // ---------------------
1086 // remove working files
1087 // ---------------------
1091 OSD_File( aLogFileName ).Remove();
1093 else if ( OSD_File( aLogFileName ).Size() > 0 )
1095 INFOS( "GHS3D Error, see the " << aLogFileName.ToCString() << " file" );
1097 // get problem description from the log file
1098 SMESH_Comment comment;
1099 TCollection_AsciiString foundLine;
1100 if ( findLineContaing( "has expired",aLogFileName,foundLine) &&
1101 foundLine.Search("Licence") >= 0)
1103 foundLine.LeftAdjust();
1104 comment << foundLine;
1106 if ( findLineContaing( "%% ERROR",aLogFileName,foundLine))
1108 foundLine.LeftAdjust();
1109 comment << foundLine;
1111 if ( findLineContaing( "%% NO SAVING OPERATION",aLogFileName,foundLine))
1113 comment << "Too many elements generated for a trial version.\n";
1115 if ( comment.empty() )
1116 comment << "See " << aLogFileName << " for problem description";
1118 comment << "See " << aLogFileName << " for more information";
1119 error(COMPERR_ALGO_FAILED, comment);
1122 // the log file is empty
1123 OSD_File( aLogFileName ).Remove();
1124 INFOS( "GHS3D Error, command '" << cmd.ToCString() << "' failed" );
1125 error(COMPERR_ALGO_FAILED, "ghs3d: command not found" );
1128 if ( !getenv("GHS3D_KEEP_FILES") )
1130 OSD_File( aFacesFileName ).Remove();
1131 OSD_File( aPointsFileName ).Remove();
1132 OSD_File( aResultFileName ).Remove();
1133 OSD_File( aBadResFileName ).Remove();
1134 OSD_File( aBbResFileName ).Remove();
1140 //=============================================================================
1144 //=============================================================================
1146 ostream & GHS3DPlugin_GHS3D::SaveTo(ostream & save)
1151 //=============================================================================
1155 //=============================================================================
1157 istream & GHS3DPlugin_GHS3D::LoadFrom(istream & load)
1162 //=============================================================================
1166 //=============================================================================
1168 ostream & operator << (ostream & save, GHS3DPlugin_GHS3D & hyp)
1170 return hyp.SaveTo( save );
1173 //=============================================================================
1177 //=============================================================================
1179 istream & operator >> (istream & load, GHS3DPlugin_GHS3D & hyp)
1181 return hyp.LoadFrom( load );