From: raza Date: Tue, 13 Nov 2007 15:16:43 +0000 (+0000) Subject: This file replaces the current one, whose version is 1.9.6.12, into the repository... X-Git-Tag: V4_1_0rc2~3 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=78394673cf4845da2753a7bf3211dca6cd7d546e;p=plugins%2Fghs3dplugin.git This file replaces the current one, whose version is 1.9.6.12, into the repository and the merge isn't necessary. --- diff --git a/src/GHS3DPlugin_GHS3D.cxx b/src/GHS3DPlugin_GHS3D.cxx index 6de3681..02437ef 100644 --- a/src/GHS3DPlugin_GHS3D.cxx +++ b/src/GHS3DPlugin_GHS3D.cxx @@ -29,13 +29,10 @@ using namespace std; #include "GHS3DPlugin_GHS3D.hxx" #include "SMESH_Gen.hxx" #include "SMESH_Mesh.hxx" -#include "SMESH_Comment.hxx" -#include "SMESH_MesherHelper.hxx" #include "SMDS_MeshElement.hxx" #include "SMDS_MeshNode.hxx" -#include #include #include @@ -56,8 +53,6 @@ using namespace std; #include #include -#define castToNode(n) static_cast( n ); - #ifdef _DEBUG_ #define DUMP(txt) \ // cout << txt @@ -87,9 +82,8 @@ GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D(int hypId, int studyId, SMESH_Gen* gen) MESSAGE("GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D"); _name = "GHS3D_3D"; _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type - _onlyUnaryInput = false; // Compute() will be called on a compound of solids -// _iShape=0; -// _nbShape=0; + _iShape=0; + _nbShape=0; } //============================================================================= @@ -118,190 +112,91 @@ bool GHS3DPlugin_GHS3D::CheckHypothesis ( SMESH_Mesh& a return true; } -//================================================================================ -/*! - * \brief Write faces bounding theShape to file - */ -//================================================================================ - -static bool writeFaces (ofstream & theFile, - SMESHDS_Mesh * theMesh, - const TopoDS_Shape& theShape, - vector & theNodeByGhs3dId) -{ - // record structure: - // - // NB_ELEMS DUMMY_INT - // Loop from 1 to NB_ELEMS - // NB_NODES NODE_NB_1 NODE_NB_2 ... (NB_NODES + 1) times: DUMMY_INT - - // get all faces bound to theShape - - // Solids in the ShapeToMesh() can be meshed by different meshers, - // so we take faces only from the given shape - //TopoDS_Shape theShape = theMesh->ShapeToMesh(); - int nbFaces = 0; - list< const SMDS_MeshElement* > faces; - // Use TopTools_IndexedMapOfShape in order not to take twice mesh faces from - // a geom face shared by two solids - TopTools_IndexedMapOfShape faceMap; - TopExp::MapShapes( theShape, TopAbs_FACE, faceMap ); - SMESHDS_SubMesh* sm; - SMDS_ElemIteratorPtr eIt; - - const char* space = " "; - const int dummyint = 0; - - list< const SMDS_MeshElement* >::iterator f; - map< const SMDS_MeshNode*,int >::iterator it; - SMDS_ElemIteratorPtr nodeIt; - const SMDS_MeshElement* elem; - int nbNodes; - //int aSmdsID; - - for ( int i = 0; i < faceMap.Extent(); ++i ) { - sm = theMesh->MeshElements( faceMap( i+1 ) ); - if ( sm ) { - eIt = sm->GetElements(); - while ( eIt->more() ) { - faces.push_back( eIt->next() ); - nbFaces++; - } - } - } - - if ( nbFaces == 0 ) - return false; - - cout << "The initial 2D mesh contains " << nbFaces << " faces and "; - - // NB_ELEMS DUMMY_INT - theFile << space << nbFaces << space << dummyint << endl; - - // Loop from 1 to NB_ELEMS - - map aNodeToGhs3dIdMap; - f = faces.begin(); - for ( ; f != faces.end(); ++f ) - { - // NB_NODES PER FACE - elem = *f; - nbNodes = elem->NbNodes(); - theFile << space << nbNodes; - - // NODE_NB_1 NODE_NB_2 ... - nodeIt = elem->nodesIterator(); - while ( nodeIt->more() ) - { - // find GHS3D ID - const SMDS_MeshNode* node = castToNode( nodeIt->next() ); - int newId = aNodeToGhs3dIdMap.size() + 1; // ghs3d ids count from 1 - it = aNodeToGhs3dIdMap.insert( make_pair( node, newId )).first; - theFile << space << it->second; - } - - // (NB_NODES + 1) times: DUMMY_INT - for ( int i=0; i<=nbNodes; i++) - theFile << space << dummyint; - - theFile << endl; - } - - // put nodes to theNodeByGhs3dId vector - theNodeByGhs3dId.resize( aNodeToGhs3dIdMap.size() ); - map::const_iterator n2id = aNodeToGhs3dIdMap.begin(); - for ( ; n2id != aNodeToGhs3dIdMap.end(); ++ n2id) - { - theNodeByGhs3dId[ n2id->second - 1 ] = n2id->first; // ghs3d ids count from 1 - } - - return true; -} - //======================================================================= //function : writeFaces -//purpose : Write Faces in case if generate 3D mesh w/o geometry +//purpose : //======================================================================= static bool writeFaces (ofstream & theFile, SMESHDS_Mesh * theMesh, - vector & theNodeByGhs3dId) + const map & theSmdsToGhs3dIdMap) { // record structure: // // NB_ELEMS DUMMY_INT // Loop from 1 to NB_ELEMS - // NB_NODES NODE_NB_1 NODE_NB_2 ... (NB_NODES + 1) times: DUMMY_INT + // NB_NODES NODE_NB_1 NODE_NB_2 ... (NB_NODES + 1) times: DUMMY_INT + int nbShape = 0; - int nbFaces = 0; - list< const SMDS_MeshElement* > faces; - list< const SMDS_MeshElement* >::iterator f; - map< const SMDS_MeshNode*,int >::iterator it; - SMDS_ElemIteratorPtr nodeIt; - const SMDS_MeshElement* elem; - int nbNodes; + TopExp_Explorer expface( theMesh->ShapeToMesh(), TopAbs_FACE ); + for ( ; expface.More(); expface.Next() ) + nbShape++; + int tabID[nbShape]; + TopoDS_Shape tabShape[nbShape], aShape; + SMESHDS_SubMesh* theSubMesh; + const SMDS_MeshElement* aFace; const char* space = " "; const int dummyint = 0; + map::const_iterator itOnMap; + SMDS_ElemIteratorPtr itOnSubMesh, itOnSubFace; + int shapeID, nbNodes, aSmdsID; + bool idFound; - //get all faces from mesh - SMDS_FaceIteratorPtr eIt = theMesh->facesIterator(); - while ( eIt->more() ) { - const SMDS_MeshElement* elem = eIt->next(); - if ( !elem ) - return false; - faces.push_back( elem ); - nbFaces++; - } - - if ( nbFaces == 0 ) - return false; - - cout << "The initial 2D mesh contains " << nbFaces << " faces and "; - - // NB_ELEMS DUMMY_INT - theFile << space << nbFaces << space << dummyint << endl; - - // Loop from 1 to NB_ELEMS + cout << " " << theMesh->NbFaces() << " triangles" << endl; + cout << endl; - map aNodeToGhs3dIdMap; - f = faces.begin(); - for ( ; f != faces.end(); ++f ) - { - // NB_NODES PER FACE - elem = *f; - nbNodes = elem->NbNodes(); - theFile << space << nbNodes; - - // NODE_NB_1 NODE_NB_2 ... - nodeIt = elem->nodesIterator(); - while ( nodeIt->more() ) - { - // find GHS3D ID - const SMDS_MeshNode* node = castToNode( nodeIt->next() ); - int newId = aNodeToGhs3dIdMap.size() + 1; // ghs3d ids count from 1 - it = aNodeToGhs3dIdMap.insert( make_pair( node, newId )).first; - theFile << space << it->second; + theFile << space << theMesh->NbFaces() << space << dummyint << endl; + + expface.ReInit(); + for ( int i = 0; expface.More(); expface.Next(), i++ ) { + tabID[i] = 0; + aShape = expface.Current(); + shapeID = theMesh->ShapeToIndex( aShape ); + idFound = false; + for ( int j=0; j<=i; j++) { + if ( shapeID == tabID[j] ) { + idFound = true; + break; + } + } + if ( not idFound ) { + tabID[i] = shapeID; + tabShape[i] = aShape; } - - // (NB_NODES + 1) times: DUMMY_INT - for ( int i=0; i<=nbNodes; i++) - theFile << space << dummyint; - - theFile << endl; } - - // put nodes to theNodeByGhs3dId vector - theNodeByGhs3dId.resize( aNodeToGhs3dIdMap.size() ); - map::const_iterator n2id = aNodeToGhs3dIdMap.begin(); - for ( ; n2id != aNodeToGhs3dIdMap.end(); ++ n2id) - { - theNodeByGhs3dId[ n2id->second - 1 ] = n2id->first; // ghs3d ids count from 1 + for ( int i =0; i < nbShape; i++ ) { + if ( not (tabID[i] == 0) ) { + aShape = tabShape[i]; + shapeID = tabID[i]; + theSubMesh = theMesh->MeshElements( aShape ); + itOnSubMesh = theSubMesh->GetElements(); + while ( itOnSubMesh->more() ) { + aFace = itOnSubMesh->next(); + nbNodes = aFace->NbNodes(); + + theFile << space << nbNodes; + + itOnSubFace = aFace->nodesIterator(); + while ( itOnSubFace->more() ) { + // find GHS3D ID + aSmdsID = itOnSubFace->next()->GetID(); + itOnMap = theSmdsToGhs3dIdMap.find( aSmdsID ); + ASSERT( itOnMap != theSmdsToGhs3dIdMap.end() ); + + theFile << space << (*itOnMap).second; + } + + // (NB_NODES + 1) times: DUMMY_INT + for ( int j=0; j<=nbNodes; j++) + theFile << space << dummyint; + + theFile << endl; + } + } } - return true; - } //======================================================================= @@ -309,9 +204,10 @@ static bool writeFaces (ofstream & theFile, //purpose : //======================================================================= -static bool writePoints (ofstream & theFile, - SMESHDS_Mesh * theMesh, - const vector & theNodeByGhs3dId) +static bool writePoints (ofstream & theFile, + SMESHDS_Mesh * theMesh, + map & theSmdsToGhs3dIdMap, + map & theGhs3dIdToNodeMap) { // record structure: // @@ -319,27 +215,31 @@ static bool writePoints (ofstream & theFile, // Loop from 1 to NB_NODES // X Y Z DUMMY_INT - //int nbNodes = theMesh->NbNodes(); - int nbNodes = theNodeByGhs3dId.size(); + int nbNodes = theMesh->NbNodes(); if ( nbNodes == 0 ) return false; const char* space = " "; const int dummyint = 0; + int aGhs3dID = 1; + SMDS_NodeIteratorPtr it = theMesh->nodesIterator(); const SMDS_MeshNode* node; // NB_NODES theFile << space << nbNodes << endl; - cout << nbNodes << " nodes" << endl; + cout << endl; + cout << "The initial 2D mesh contains :" << endl; + cout << " " << nbNodes << " vertices" << endl; // Loop from 1 to NB_NODES - vector::const_iterator nodeIt = theNodeByGhs3dId.begin(); - vector::const_iterator after = theNodeByGhs3dId.end(); - for ( ; nodeIt != after; ++nodeIt ) + while ( it->more() ) { - node = *nodeIt; + node = it->next(); + theSmdsToGhs3dIdMap.insert( map ::value_type( node->GetID(), aGhs3dID )); + theGhs3dIdToNodeMap.insert (map ::value_type( aGhs3dID, node )); + aGhs3dID++; // X Y Z DUMMY_INT theFile @@ -355,12 +255,12 @@ static bool writePoints (ofstream & theFile, } //======================================================================= -//function : findSolid +//function : findShape //purpose : //======================================================================= -static TopoDS_Shape findSolid(const SMDS_MeshNode *aNode[], - TopoDS_Shape aSolid, +static TopoDS_Shape findShape(const SMDS_MeshNode *aNode[], + TopoDS_Shape aShape, const TopoDS_Shape shape[], const double box[][6], const int nShape) { @@ -373,20 +273,20 @@ static TopoDS_Shape findSolid(const SMDS_MeshNode *aNode[], PZ = ( aNode[0]->Z() + aNode[1]->Z() + aNode[2]->Z() + aNode[3]->Z() ) / 4.0; gp_Pnt aPnt(PX, PY, PZ); - BRepClass3d_SolidClassifier SC (aSolid, aPnt, Precision::Confusion()); + BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion()); if ( not(SC.State() == TopAbs_IN) ) { for (iShape = 0; iShape < nShape; iShape++) { - aSolid = shape[iShape]; + aShape = shape[iShape]; if ( not( PX < box[iShape][0] || box[iShape][1] < PX || PY < box[iShape][2] || box[iShape][3] < PY || PZ < box[iShape][4] || box[iShape][5] < PZ) ) { - BRepClass3d_SolidClassifier SC (aSolid, aPnt, Precision::Confusion()); + BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion()); if (SC.State() == TopAbs_IN) break; } } } - return aSolid; + return aShape; } //======================================================================= @@ -406,114 +306,20 @@ static char* readMapIntLine(char* ptr, int tab[]) { return ptr; } -//======================================================================= -//function : readLine -//purpose : -//======================================================================= - -#define GHS3DPlugin_BUFLENGTH 256 -#define GHS3DPlugin_ReadLine(aPtr,aBuf,aFile,aLineNb) \ -{ aPtr = fgets( aBuf, GHS3DPlugin_BUFLENGTH - 2, aFile ); aLineNb++; DUMP(endl); } - -#include //======================================================================= //function : readResultFile //purpose : //======================================================================= -static bool readResultFile(const int fileOpen, - SMESHDS_Mesh* theMeshDS, - TopoDS_Shape aSolid, - vector & theNodeByGhs3dId) { - - struct stat status; - size_t length; - - char *ptr, *mapPtr; - char *tetraPtr; - char *shapePtr; - - int fileStat; - int nbElems, nbNodes, nbInputNodes; - int nodeId, triangleId; - int tab[3]; - int nbTriangle; - int ID, shapeID; - - double coord [3]; - - SMDS_MeshNode * aNewNode; - const SMDS_MeshNode * node[4]; - map ::iterator IdNode; - SMDS_MeshElement* aTet; - - // Read the file state - fileStat = fstat(fileOpen, &status); - length = status.st_size; - - // Mapping the result file into memory - ptr = (char *) mmap(0,length,PROT_READ,MAP_PRIVATE,fileOpen,0); - mapPtr = ptr; - - ptr = readMapIntLine(ptr, tab); - tetraPtr = ptr; - - nbElems = tab[0]; - nbNodes = tab[1]; - nbInputNodes = tab[2]; - - theNodeByGhs3dId.resize( nbNodes ); - - // Reading the nodeId - for (int i=0; i < 4*nbElems; i++) - nodeId = strtol(ptr, &ptr, 10); - - // Reading the nodeCoor and update the nodeMap - shapeID = theMeshDS->ShapeToIndex( aSolid ); - for (int iNode=0; iNode < nbNodes; iNode++) { - for (int iCoor=0; iCoor < 3; iCoor++) - coord[ iCoor ] = strtod(ptr, &ptr); - if ((iNode+1) > nbInputNodes) { - aNewNode = theMeshDS->AddNode( coord[0],coord[1],coord[2] ); - theMeshDS->SetNodeInVolume( aNewNode, shapeID ); - theNodeByGhs3dId[ iNode ] = aNewNode; - } - } - - // Reading the triangles - nbTriangle = strtol(ptr, &ptr, 10); - - for (int i=0; i < 3*nbTriangle; i++) - triangleId = strtol(ptr, &ptr, 10); - - shapePtr = ptr; - - // Associating the tetrahedrons to the shapes - for (int iElem = 0; iElem < nbElems; iElem++) { - for (int iNode = 0; iNode < 4; iNode++) { - ID = strtol(tetraPtr, &tetraPtr, 10); - node[ iNode ] = theNodeByGhs3dId[ ID-1 ]; - } - aTet = theMeshDS->AddVolume( node[1], node[0], node[2], node[3] ); - shapeID = theMeshDS->ShapeToIndex( aSolid ); - theMeshDS->SetMeshElementOnShape( aTet, shapeID ); - } - if ( nbElems ) - cout << nbElems << " tetrahedrons have been associated to " << nbTriangle << " shapes" << endl; - munmap(mapPtr, length); - close(fileOpen); - return true; -} - -static bool readResultFile(const int fileOpen, - SMESHDS_Mesh* theMeshDS, - TopoDS_Shape tabShape[], - double tabBox[][6], - const int nShape, - vector & theNodeByGhs3dId) { +static bool readResultFile(const int fileOpen, + SMESHDS_Mesh* theMeshDS, + TopoDS_Shape tabShape[], + double tabBox[][6], + const int nbShape, + map & theGhs3dIdToNodeMap) { - struct stat status; - size_t length; + struct stat status; + size_t length; char *ptr, *mapPtr; char *tetraPtr; @@ -522,12 +328,13 @@ static bool readResultFile(const int fileOpen, int fileStat; int nbElems, nbNodes, nbInputNodes; int nodeId, triangleId; - int tab[3]/*, tabID[nShape]*/; + int tab[3], tabID[nbShape]; int nbTriangle; int ID, shapeID, ghs3dShapeID; + int IdShapeRef = 1; + int compoundID = theMeshDS->ShapeToIndex( theMeshDS->ShapeToMesh() ); double coord [3]; - vector< int > tabID (nShape, 0); TopoDS_Shape aSolid; SMDS_MeshNode * aNewNode; @@ -535,8 +342,8 @@ static bool readResultFile(const int fileOpen, map ::iterator IdNode; SMDS_MeshElement* aTet; -// for (int i=0; i nbInputNodes) { aNewNode = theMeshDS->AddNode( coord[0],coord[1],coord[2] ); - theNodeByGhs3dId[ iNode ] = aNewNode; + theMeshDS->SetNodeInVolume( aNewNode, compoundID ); + theGhs3dIdToNodeMap.insert(make_pair( (iNode+1), aNewNode )); } } - // Reading the triangles + cout << endl; + + // Reading the number of triangles which corresponds to the number of shapes nbTriangle = strtol(ptr, &ptr, 10); - for (int i=0; i < 3*nbTriangle; i++) + for (int i=0; i < 3*nbShape; i++) triangleId = strtol(ptr, &ptr, 10); shapePtr = ptr; // Associating the tetrahedrons to the shapes + shapeID = compoundID; for (int iElem = 0; iElem < nbElems; iElem++) { for (int iNode = 0; iNode < 4; iNode++) { ID = strtol(tetraPtr, &tetraPtr, 10); - node[ iNode ] = theNodeByGhs3dId[ ID-1 ]; + IdNode = theGhs3dIdToNodeMap.find(ID); + node[ iNode ] = IdNode->second; } aTet = theMeshDS->AddVolume( node[1], node[0], node[2], node[3] ); - ghs3dShapeID = strtol(shapePtr, &shapePtr, 10); - if ( !ghs3dShapeID ) ghs3dShapeID = 1; - if ( tabID[ ghs3dShapeID - 1 ] == 0 ) { - if (iElem == 0) - aSolid = tabShape[0]; - aSolid = findSolid(node, aSolid, tabShape, tabBox, nShape /*nbTriangle*/); - shapeID = theMeshDS->ShapeToIndex( aSolid ); - tabID[ ghs3dShapeID - 1] = shapeID; - } - else { - shapeID = tabID[ ghs3dShapeID - 1]; + if ( nbShape > 1 ) { + ghs3dShapeID = strtol(shapePtr, &shapePtr, 10) - IdShapeRef; + if ( tabID[ ghs3dShapeID ] == 0 ) { + if (iElem == 0) + aSolid = tabShape[0]; + aSolid = findShape(node, aSolid, tabShape, tabBox, nbShape); + shapeID = theMeshDS->ShapeToIndex( aSolid ); + tabID[ ghs3dShapeID ] = shapeID; + } + else + shapeID = tabID[ ghs3dShapeID ]; } theMeshDS->SetMeshElementOnShape( aTet, shapeID ); - // set new nodes on to the shape - SMDS_ElemIteratorPtr nodeIt = aTet->nodesIterator(); - while ( nodeIt->more() ) { - const SMDS_MeshNode * n = castToNode( nodeIt->next() ); - if ( !n->GetPosition()->GetShapeId() ) - theMeshDS->SetNodeInVolume( n, shapeID ); - } + if ( (iElem + 1) == nbElems ) + cout << nbElems << " tetrahedrons have been associated to " << nbShape << " shapes" << endl; } - if ( nbElems ) - cout << nbElems << " tetrahedrons have been associated to " << nbTriangle << " shapes" << endl; munmap(mapPtr, length); close(fileOpen); return true; } - - //======================================================================= //function : getTmpDir //purpose : @@ -642,36 +444,6 @@ static TCollection_AsciiString getTmpDir() return aTmpDir; } -//================================================================================ -/*! - * \brief Look for a line containing a text in a file - * \retval bool - true if the line is found - */ -//================================================================================ - -static bool findLineContaing(const TCollection_AsciiString& theText, - const TCollection_AsciiString& theFile, - TCollection_AsciiString & theFoundLine) -{ - bool found = false; - if ( FILE * aFile = fopen( theFile.ToCString(), "r" )) - { - char * aPtr; - char aBuffer[ GHS3DPlugin_BUFLENGTH ]; - int aLineNb = 0; - do { - GHS3DPlugin_ReadLine( aPtr, aBuffer, aFile, aLineNb ); - if ( aPtr ) { - theFoundLine = aPtr; - found = theFoundLine.Search( theText ) >= 0; - } - } while ( aPtr && !found ); - - fclose( aFile ); - } - return found; -} - //============================================================================= /*! *Here we are going to use the GHS3D mesher @@ -681,25 +453,29 @@ static bool findLineContaing(const TCollection_AsciiString& theText, bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape) { - // theShape is a compound of solids as _onlyUnaryInput = false - bool Ok(false); + bool Ok = true; SMESHDS_Mesh* meshDS = theMesh.GetMeshDS(); - int _nbShape = 0; - /*if (_iShape == 0 && _nbShape == 0)*/ { - cout << endl; - cout << "Ghs3d execution..." << endl; - cout << endl; - - //TopExp_Explorer exp (meshDS->ShapeToMesh(), TopAbs_SOLID); - TopExp_Explorer exp (theShape, TopAbs_SOLID); + if (_iShape == 0 && _nbShape == 0) { + TopExp_Explorer exp (meshDS->ShapeToMesh(), TopAbs_SOLID); for (; exp.More(); exp.Next()) - _nbShape++; + _nbShape++; // we count the number of shapes + _tabNode = new SMDS_MeshNode*[_nbShape]; // we declare the size of the node array } - - //_iShape++; - /*if ( _iShape == _nbShape )*/ { + // to prevent from displaying error message after successful mesh computing, + // we create one node for each shape theShape. + + _tabNode[_iShape] = meshDS->AddNode(0, 0, 0); + meshDS->SetNodeInVolume( _tabNode[_iShape], meshDS->ShapeToIndex(theShape) ); + + _iShape++; + + if ( _iShape == _nbShape ) { + + for (int i=0; i<_nbShape; i++) // we destroy the (_nbShape - 1) nodes created and used + meshDS->RemoveNode( _tabNode[i] ); // to simulate successful mesh computing. + delete _tabNode; // create bounding box for every shape @@ -708,7 +484,7 @@ bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh, double tabBox[_nbShape][6]; Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax; - TopExp_Explorer expBox (theShape, TopAbs_SOLID); + TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID); for (; expBox.More(); expBox.Next()) { tabShape[iShape] = expBox.Current(); Bnd_Box BoundingBox; @@ -720,6 +496,9 @@ bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh, iShape++; } + cout << endl; + cout << "Ghs3d execution..." << endl; + // make a unique working file name // to avoid access to the same files by eg different users @@ -748,20 +527,22 @@ bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh, ofstream aFacesFile ( aFacesFileName.ToCString() , ios::out); ofstream aPointsFile ( aPointsFileName.ToCString() , ios::out); - // bool Ok = + Ok = #ifdef WIN32 aFacesFile->is_open() && aPointsFile->is_open(); #else aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open(); #endif - if (!Ok) - return error(SMESH_Comment("Can't write into ") << aTmpDir); - - vector aNodeByGhs3dId; + if (!Ok) { + INFOS( "Can't write into " << aTmpDir.ToCString()); + return false; + } + map aSmdsToGhs3dIdMap; + map aGhs3dIdToNodeMap; - Ok = ( writeFaces ( aFacesFile, meshDS, theShape, aNodeByGhs3dId ) && - writePoints( aPointsFile, meshDS, aNodeByGhs3dId )); + Ok = writePoints( aPointsFile, meshDS, aSmdsToGhs3dIdMap, aGhs3dIdToNodeMap ) && + writeFaces ( aFacesFile, meshDS, aSmdsToGhs3dIdMap ); aFacesFile.close(); aPointsFile.close(); @@ -771,7 +552,7 @@ bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh, OSD_File( aFacesFileName ).Remove(); OSD_File( aPointsFileName ).Remove(); } - return error(COMPERR_BAD_INPUT_MESH); + return false; } // ----------------- @@ -783,28 +564,23 @@ bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh, // so allow to use about all available memory TCollection_AsciiString memory; -#ifdef WIN32 - // ???? -#else +#ifndef WIN32 struct sysinfo si; int err = sysinfo( &si ); - if ( !err ) { + if ( err == 0 ) { int freeMem = si.totalram * si.mem_unit / 1024 / 1024; memory = "-m "; memory += int( 0.7 * freeMem ); } #endif - OSD_File( aResultFileName ).Remove(); // old file prevents writing a new one - + MESSAGE("GHS3DPlugin_GHS3D::Compute"); TCollection_AsciiString cmd( "ghs3d " ); // command to run cmd += - memory + // memory - " -c 0" // 0 - mesh all components, 1 - keep only the main component - " -f " + aGenericName + // file to read - " 1>" + aLogFileName; // dump into file + memory + // memory + " -c0 -f " + aGenericName + // file to read + " 1>" + aLogFileName; // dump into file - MESSAGE("GHS3DPlugin_GHS3D::Compute() " << cmd ); system( cmd.ToCString() ); // run cout << endl; @@ -820,216 +596,85 @@ bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh, fileOpen = open( aResultFileName.ToCString(), O_RDONLY); if ( fileOpen < 0 ) { cout << endl; - cout << "Error when opening the " << aResultFileName.ToCString() << " file" << endl; - cout << endl; + cout << "Can't open the " << aResultFileName.ToCString() << " GHS3D output file" << endl; Ok = false; } - else { - Ok = readResultFile( fileOpen, meshDS, tabShape, tabBox, _nbShape, aNodeByGhs3dId ); - } + else + Ok = readResultFile( fileOpen, meshDS, tabShape, tabBox, _nbShape, aGhs3dIdToNodeMap ); - // --------------------- - // remove working files - // --------------------- + // --------------------- + // remove working files + // --------------------- if ( Ok ) - { OSD_File( aLogFileName ).Remove(); + else if ( OSD_File( aLogFileName ).Size() > 0 ) { + INFOS( "GHS3D Error, see the " << aLogFileName.ToCString() << " file" ); } - else if ( OSD_File( aLogFileName ).Size() > 0 ) - { - // get problem description from the log file - SMESH_Comment comment; - TCollection_AsciiString foundLine; - if ( findLineContaing( "has expired",aLogFileName,foundLine) && - foundLine.Search("Licence") >= 0) - { - foundLine.LeftAdjust(); - comment << foundLine; - } - if ( findLineContaing( "%% ERROR",aLogFileName,foundLine)) - { - foundLine.LeftAdjust(); - comment << foundLine; - } - if ( findLineContaing( "%% NO SAVING OPERATION",aLogFileName,foundLine)) - { - comment << "Too many elements generated for a trial version.\n"; - } - if ( comment.empty() ) - comment << "See " << aLogFileName << " for problem description"; - else - comment << "See " << aLogFileName << " for more information"; - error(COMPERR_ALGO_FAILED, comment); - } - else - { - // the log file is empty + else { OSD_File( aLogFileName ).Remove(); - error(COMPERR_ALGO_FAILED, "ghs3d: command not found" ); + INFOS( "GHS3D Error, command '" << cmd.ToCString() << "' failed" ); } - if ( !getenv("GHS3D_KEEP_FILES") ) - { + if ( !getenv("GHS3D_KEEP_FILES") ) { OSD_File( aFacesFileName ).Remove(); OSD_File( aPointsFileName ).Remove(); OSD_File( aResultFileName ).Remove(); OSD_File( aBadResFileName ).Remove(); OSD_File( aBbResFileName ).Remove(); } - /*if ( _iShape == _nbShape )*/ { - cout << aResultFileName.ToCString() << " Output file "; + if ( _iShape == _nbShape ) { + cout << aResultFileName.ToCString() << " GHS3D output file "; if ( !Ok ) cout << "not "; cout << "treated !" << endl; cout << endl; } } - return Ok; } //============================================================================= /*! - *Here we are going to use the GHS3D mesher w/o geometry + * */ //============================================================================= -bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh, - SMESH_MesherHelper* aHelper) -{ - MESSAGE("GHS3DPlugin_GHS3D::Compute()"); - - SMESHDS_Mesh* meshDS = theMesh.GetMeshDS(); - TopoDS_Shape theShape = aHelper->GetSubShape(); - // make a unique working file name - // to avoid access to the same files by eg different users - - TCollection_AsciiString aGenericName, aTmpDir = getTmpDir(); - aGenericName = aTmpDir + "GHS3D_"; -#ifdef WIN32 - aGenericName += GetCurrentProcessId(); -#else - aGenericName += getpid(); -#endif - aGenericName += "_"; - aGenericName += meshDS->ShapeToIndex( theShape ); - - TCollection_AsciiString aFacesFileName, aPointsFileName, aResultFileName; - TCollection_AsciiString aBadResFileName, aBbResFileName, aLogFileName; - aFacesFileName = aGenericName + ".faces"; // in faces - aPointsFileName = aGenericName + ".points"; // in points - aResultFileName = aGenericName + ".noboite";// out points and volumes - aBadResFileName = aGenericName + ".boite"; // out bad result - aBbResFileName = aGenericName + ".bb"; // out vertex stepsize - aLogFileName = aGenericName + ".log"; // log - - // ----------------- - // make input files - // ----------------- - - ofstream aFacesFile ( aFacesFileName.ToCString() , ios::out); - ofstream aPointsFile ( aPointsFileName.ToCString() , ios::out); - bool Ok = -#ifdef WIN32 - aFacesFile->is_open() && aPointsFile->is_open(); -#else - aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open(); -#endif - - if (!Ok) - return error( SMESH_Comment("Can't write into ") << aTmpDir.ToCString()); - - vector aNodeByGhs3dId; - - Ok = (writeFaces ( aFacesFile, meshDS, aNodeByGhs3dId ) && - writePoints( aPointsFile, meshDS, aNodeByGhs3dId)); - - aFacesFile.close(); - aPointsFile.close(); - - if ( ! Ok ) { - if ( !getenv("GHS3D_KEEP_FILES") ) { - OSD_File( aFacesFileName ).Remove(); - OSD_File( aPointsFileName ).Remove(); - } - return error(COMPERR_BAD_INPUT_MESH); - } - - // ----------------- - // run ghs3d mesher WIN32??? - // ----------------- - - // ghs3d need to know amount of memory it may use (MB). - // Default memory is defined at ghs3d installation but it may be not enough, - // so allow to use about all available memory - TCollection_AsciiString memory; -#ifdef WIN32 - // ???? -#else - struct sysinfo si; - int err = sysinfo( &si ); - if ( !err ) { - int freeMem = si.totalram * si.mem_unit / 1024 / 1024; - memory = "-m "; - memory += int( 0.7 * freeMem ); - } -#endif - - TCollection_AsciiString cmd( "ghs3d " ); // command to run - cmd += - memory + // memory - " -f " + aGenericName + // file to read - " 1>" + aLogFileName; // dump into file - - - - system( cmd.ToCString() ); // run - - // -------------- - // read a result - // -------------- - int fileOpen; - fileOpen = open( aResultFileName.ToCString(), O_RDONLY); - if ( fileOpen < 0 ) { - cout << endl; - cout << "Error when opening the " << aResultFileName.ToCString() << " file" << endl; - cout << endl; - Ok = false; - } - else { - Ok = readResultFile( fileOpen, meshDS, theShape ,aNodeByGhs3dId ); - } - - // --------------------- - // remove working files - // --------------------- - - if ( Ok ) { - OSD_File( aLogFileName ).Remove(); - } - else if ( OSD_File( aLogFileName ).Size() > 0 ) { - Ok = error( SMESH_Comment("See ")<< aLogFileName.ToCString() ); - } - else { - OSD_File( aLogFileName ).Remove(); - } - - if ( !getenv("GHS3D_KEEP_FILES") ) - { - OSD_File( aFacesFileName ).Remove(); - OSD_File( aPointsFileName ).Remove(); - OSD_File( aResultFileName ).Remove(); - OSD_File( aBadResFileName ).Remove(); - OSD_File( aBbResFileName ).Remove(); - } - - return Ok; +ostream & GHS3DPlugin_GHS3D::SaveTo(ostream & save) +{ + return save; } +//============================================================================= +/*! + * + */ +//============================================================================= +istream & GHS3DPlugin_GHS3D::LoadFrom(istream & load) +{ + return load; +} +//============================================================================= +/*! + * + */ +//============================================================================= +ostream & operator << (ostream & save, GHS3DPlugin_GHS3D & hyp) +{ + return hyp.SaveTo( save ); +} +//============================================================================= +/*! + * + */ +//============================================================================= +istream & operator >> (istream & load, GHS3DPlugin_GHS3D & hyp) +{ + return hyp.LoadFrom( load ); +}