X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2FGHS3DPlugin_GHS3D.cxx;h=547149056d9230829c391c4244684cefd3fbd578;hb=refs%2Ftags%2FV4_1_0a1;hp=bef01793f5235c5c7b5f43093950b33b835afa73;hpb=04946dddb73241bd31e0223b1d4937ece14b1ed9;p=plugins%2Fghs3dplugin.git diff --git a/src/GHS3DPlugin_GHS3D.cxx b/src/GHS3DPlugin_GHS3D.cxx index bef0179..5471490 100644 --- a/src/GHS3DPlugin_GHS3D.cxx +++ b/src/GHS3DPlugin_GHS3D.cxx @@ -1,7 +1,25 @@ +// Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// //============================================================================= // File : GHS3DPlugin_GHS3D.cxx // Created : -// Author : Edward AGAPOV +// Author : Edward AGAPOV, modified by Lioka RAZAFINDRAZAKA (CEA) 09/02/2007 // Project : SALOME // Copyright : CEA 2003 // $Header$ @@ -11,15 +29,32 @@ using namespace std; #include "GHS3DPlugin_GHS3D.hxx" #include "SMESH_Gen.hxx" #include "SMESH_Mesh.hxx" +#include "SMESH_Comment.hxx" #include "SMDS_MeshElement.hxx" #include "SMDS_MeshNode.hxx" #include +#include #include "utilities.h" -#include +#ifndef WIN32 +#include +#endif + +//#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#define castToNode(n) static_cast( n ); #ifdef _DEBUG_ #define DUMP(txt) \ @@ -28,6 +63,16 @@ using namespace std; #define DUMP(txt) #endif +extern "C" +{ +#ifndef WNT +#include +#include +#endif +#include +#include +} + //============================================================================= /*! * @@ -38,8 +83,11 @@ GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D(int hypId, int studyId, SMESH_Gen* gen) : SMESH_3D_Algo(hypId, studyId, gen) { MESSAGE("GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D"); - _name = "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; } //============================================================================= @@ -59,59 +107,95 @@ GHS3DPlugin_GHS3D::~GHS3DPlugin_GHS3D() */ //============================================================================= -bool GHS3DPlugin_GHS3D::CheckHypothesis - (SMESH_Mesh& aMesh, - const TopoDS_Shape& aShape, - SMESH_Hypothesis::Hypothesis_Status& aStatus) +bool GHS3DPlugin_GHS3D::CheckHypothesis ( SMESH_Mesh& aMesh, + const TopoDS_Shape& aShape, + SMESH_Hypothesis::Hypothesis_Status& aStatus ) { // MESSAGE("GHS3DPlugin_GHS3D::CheckHypothesis"); aStatus = SMESH_Hypothesis::HYP_OK; return true; } -//======================================================================= -//function : writeFaces -//purpose : -//======================================================================= +//================================================================================ +/*! + * \brief Write faces bounding theShape to file + */ +//================================================================================ -static bool writeFaces (ofstream & theFile, - SMESHDS_Mesh * theMesh, - const map & theSmdsToGhs3dIdMap) +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 + // 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++; + } + } + } - int nbFaces = theMesh->NbFaces(); if ( nbFaces == 0 ) return false; - const char* space = " "; - const int dummyint = 0; + 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 - SMDS_FaceIteratorPtr it = theMesh->facesIterator(); - while ( it->more() ) + + map aNodeToGhs3dIdMap; + f = faces.begin(); + for ( ; f != faces.end(); ++f ) { - // NB_NODES - const SMDS_MeshElement* elem = it->next(); - const int nbNodes = elem->NbNodes(); + // NB_NODES PER FACE + elem = *f; + nbNodes = elem->NbNodes(); theFile << space << nbNodes; // NODE_NB_1 NODE_NB_2 ... - SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator(); + nodeIt = elem->nodesIterator(); while ( nodeIt->more() ) { // find GHS3D ID - int aSmdsID = nodeIt->next()->GetID(); - map::const_iterator it = theSmdsToGhs3dIdMap.find( aSmdsID ); - ASSERT( it != theSmdsToGhs3dIdMap.end() ); - theFile << space << (*it).second; + 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 @@ -121,6 +205,14 @@ static bool writeFaces (ofstream & theFile, 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; } @@ -129,10 +221,9 @@ static bool writeFaces (ofstream & theFile, //purpose : //======================================================================= -static bool writePoints (ofstream & theFile, - SMESHDS_Mesh * theMesh, - map & theSmdsToGhs3dIdMap, - map & theGhs3dIdToNodeMap) +static bool writePoints (ofstream & theFile, + SMESHDS_Mesh * theMesh, + const vector & theNodeByGhs3dId) { // record structure: // @@ -140,25 +231,27 @@ static bool writePoints (ofstream & theFile, // Loop from 1 to NB_NODES // X Y Z DUMMY_INT - int nbNodes = theMesh->NbNodes(); + //int nbNodes = theMesh->NbNodes(); + int nbNodes = theNodeByGhs3dId.size(); if ( nbNodes == 0 ) return false; const char* space = " "; const int dummyint = 0; + const SMDS_MeshNode* node; + // NB_NODES theFile << space << nbNodes << endl; + cout << nbNodes << " nodes" << endl; // Loop from 1 to NB_NODES - int aGhs3dID = 1; - SMDS_NodeIteratorPtr it = theMesh->nodesIterator(); - while ( it->more() ) + + vector::const_iterator nodeIt = theNodeByGhs3dId.begin(); + vector::const_iterator after = theNodeByGhs3dId.end(); + for ( ; nodeIt != after; ++nodeIt ) { - const SMDS_MeshNode* node = it->next(); - theSmdsToGhs3dIdMap.insert( map ::value_type( node->GetID(), aGhs3dID )); - theGhs3dIdToNodeMap.insert (map ::value_type( aGhs3dID, node )); - aGhs3dID++; + node = *nodeIt; // X Y Z DUMMY_INT theFile @@ -174,41 +267,57 @@ static bool writePoints (ofstream & theFile, } //======================================================================= -//function : getInt +//function : findSolid //purpose : //======================================================================= -static bool getInt( int & theValue, char * & theLine ) -{ - char *ptr; - theValue = strtol( theLine, &ptr, 10 ); - if ( ptr == theLine || - // there must not be neither '.' nor ',' nor 'E' ... - (*ptr != ' ' && *ptr != '\n' && *ptr != '\0')) - return false; - - DUMP( " " << theValue ); - theLine = ptr; - return true; +static TopoDS_Shape findSolid(const SMDS_MeshNode *aNode[], + TopoDS_Shape aSolid, + const TopoDS_Shape shape[], + const double box[][6], + const int nShape) { + + Standard_Real PX, PY, PZ; + int iShape; + + PX = ( aNode[0]->X() + aNode[1]->X() + aNode[2]->X() + aNode[3]->X() ) / 4.0; + PY = ( aNode[0]->Y() + aNode[1]->Y() + aNode[2]->Y() + aNode[3]->Y() ) / 4.0; + 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()); + if ( not(SC.State() == TopAbs_IN) ) { + for (iShape = 0; iShape < nShape; iShape++) { + aSolid = 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()); + if (SC.State() == TopAbs_IN) + break; + } + } + } + return aSolid; } //======================================================================= -//function : getDouble +//function : readMapIntLine //purpose : //======================================================================= -static bool getDouble( double & theValue, char * & theLine ) -{ - char *ptr; - theValue = strtod( theLine, &ptr ); - if ( ptr == theLine ) - return false; +static char* readMapIntLine(char* ptr, int tab[]) { + long int intVal; + cout << endl; - DUMP( " " << theValue ); - theLine = ptr; - return true; + for ( int i=0; i<17; i++ ) { + intVal = strtol(ptr, &ptr, 10); + if ( i < 3 ) + tab[i] = intVal; + } + return ptr; } - + //======================================================================= //function : readLine //purpose : @@ -218,126 +327,175 @@ static bool getDouble( double & theValue, char * & theLine ) #define GHS3DPlugin_ReadLine(aPtr,aBuf,aFile,aLineNb) \ { aPtr = fgets( aBuf, GHS3DPlugin_BUFLENGTH - 2, aFile ); aLineNb++; DUMP(endl); } +#include //======================================================================= -//function : readResult +//function : readResultFile //purpose : //======================================================================= -static bool readResult(FILE * theFile, - SMESHDS_Mesh * theMesh, - const TopoDS_Shape& theShape, - map & theGhs3dIdToNodeMap) -{ - // structure: - - // record 1: - // NB_ELEMENTS NB_NODES NB_INPUT_NODES (14 DUMMY_INT) - // record 2: - // (NB_ELEMENTS * 4) node nbs - // record 3: - // (NB_NODES) node XYZ - - char aBuffer[ GHS3DPlugin_BUFLENGTH ]; - char * aPtr; - int aLineNb = 0; - - // get shell to set nodes in - TopExp_Explorer exp( theShape, TopAbs_SHELL ); - TopoDS_Shell aShell = TopoDS::Shell( exp.Current() ); - if ( aShell.IsNull() ) - return false; +static bool readResultFile(const int fileOpen, + SMESHDS_Mesh* theMeshDS, + TopoDS_Shape tabShape[], + double tabBox[][6], + const int nShape, + 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]/*, tabID[nShape]*/; + int nbTriangle; + int ID, shapeID, ghs3dShapeID; + + double coord [3]; + vector< int > tabID (nShape, 0); + + TopoDS_Shape aSolid; + SMDS_MeshNode * aNewNode; + const SMDS_MeshNode * node[4]; + map ::iterator IdNode; + SMDS_MeshElement* aTet; + +// for (int i=0; i nbInputNodes) { + aNewNode = theMeshDS->AddNode( coord[0],coord[1],coord[2] ); + theNodeByGhs3dId[ iNode ] = aNewNode; + } + } - // ---------------------------------------- - // record 1: - // read nb of generated elements and nodes - // ---------------------------------------- - int nbElems = 0 , nbNodes = 0, nbInputNodes = 0; - GHS3DPlugin_ReadLine( aPtr, aBuffer, theFile, aLineNb ); - if (!aPtr || - !getInt( nbElems, aPtr ) || - !getInt( nbNodes, aPtr ) || - !getInt( nbInputNodes, aPtr)) - return false; + // Reading the triangles + nbTriangle = strtol(ptr, &ptr, 10); - // ------------------------------------------- - // record 2: - // read element nodes and create tetrahedrons - // ------------------------------------------- - GHS3DPlugin_ReadLine( aPtr, aBuffer, theFile, aLineNb ); - for (int iElem = 0; iElem < nbElems; iElem++) - { - // read 4 nodes - const SMDS_MeshNode * node[4]; - for (int iNode = 0; iNode < 4; iNode++) - { - // read Ghs3d node ID - int ID = 0; - if (!aPtr || ! getInt ( ID, aPtr )) - { - GHS3DPlugin_ReadLine( aPtr, aBuffer, theFile, aLineNb ); - if (!aPtr || ! getInt ( ID, aPtr )) - { - MESSAGE( "Cant read " << (iNode+1) << "-th node on line " << aLineNb ); - return false; - } - } - // find/create a node with ID - map ::iterator IdNode = theGhs3dIdToNodeMap.find( ID ); - if ( IdNode == theGhs3dIdToNodeMap.end()) - { - // ID is not yet in theGhs3dIdToNodeMap - ASSERT ( ID > nbInputNodes ); // it should be a new one - SMDS_MeshNode * aNewNode = theMesh->AddNode( 0.,0.,0. ); // read XYZ later - theMesh->SetNodeInVolume( aNewNode, aShell ); - theGhs3dIdToNodeMap.insert - ( map ::value_type( ID, aNewNode )); - node[ iNode ] = aNewNode; - } - else - { - node[ iNode ] = (*IdNode).second; - } + 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] ); + 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]; } - // create a tetrahedron - SMDS_MeshElement* aTet = theMesh->AddVolume( node[0], node[1], node[2], node[3] ); - theMesh->SetMeshElementOnShape( aTet, theShape ); + 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 ( nbElems ) + cout << nbElems << " tetrahedrons have been associated to " << nbTriangle << " shapes" << endl; + munmap(mapPtr, length); + close(fileOpen); + return true; +} + +//======================================================================= +//function : getTmpDir +//purpose : +//======================================================================= + +static TCollection_AsciiString getTmpDir() +{ + TCollection_AsciiString aTmpDir; + + char *Tmp_dir = getenv("SALOME_TMP_DIR"); + if(Tmp_dir != NULL) { + aTmpDir = Tmp_dir; +#ifdef WIN32 + if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\'; +#else + if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/'; +#endif } + else { +#ifdef WIN32 + aTmpDir = TCollection_AsciiString("C:\\"); +#else + aTmpDir = TCollection_AsciiString("/tmp/"); +#endif + } + return aTmpDir; +} + +//================================================================================ +/*! + * \brief Look for a line containing a text in a file + * \retval bool - true if the line is found + */ +//================================================================================ - // ------------------------ - // record 3: - // read and set nodes' XYZ - // ------------------------ - GHS3DPlugin_ReadLine( aPtr, aBuffer, theFile, aLineNb ); - for (int iNode = 0; iNode < nbNodes; iNode++) +static bool findLineContaing(const TCollection_AsciiString& theText, + const TCollection_AsciiString& theFile, + TCollection_AsciiString & theFoundLine) +{ + bool found = false; + if ( FILE * aFile = fopen( theFile.ToCString(), "r" )) { - // read 3 coordinates - double coord [3]; - for (int iCoord = 0; iCoord < 3; iCoord++) - { - if (!aPtr || ! getDouble ( coord[ iCoord ], aPtr )) - { - GHS3DPlugin_ReadLine( aPtr, aBuffer, theFile, aLineNb ); - if (!aPtr || ! getDouble ( coord[ iCoord ], aPtr )) - { - MESSAGE( "Cant read " << (iCoord+1) << "-th node coord on line " << aLineNb ); - return false; - } + 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; } - } - // do not move old nodes - int ID = iNode + 1; - if (ID <= nbInputNodes) - continue; - // find a node - map ::iterator IdNode = theGhs3dIdToNodeMap.find( ID ); - ASSERT ( IdNode != theGhs3dIdToNodeMap.end()); - SMDS_MeshNode* node = const_cast ( (*IdNode).second ); - - // set XYZ - theMesh->MoveNode( node, coord[0], coord[1], coord[2] ); - } + } while ( aPtr && !found ); - return nbElems; + fclose( aFile ); + } + return found; } //============================================================================= @@ -349,154 +507,210 @@ static bool readResult(FILE * theFile, bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape) { - MESSAGE("GHS3DPlugin_GHS3D::Compute"); + // theShape is a compound of solids as _onlyUnaryInput = false + bool Ok(false); + SMESHDS_Mesh* meshDS = theMesh.GetMeshDS(); - // working dir - QString aTmpDir ( getenv("SALOME_TMP_DIR") ); - if ( !aTmpDir.isEmpty() ) { -#ifdef WIN32 - if(aTmpDir.at(aTmpDir.length()-1) != '\\') aTmpDir+='\\'; -#else - if(aTmpDir.at(aTmpDir.length()-1) != '/') aTmpDir+='/'; -#endif + 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); + for (; exp.More(); exp.Next()) + _nbShape++; } - else { + + //_iShape++; + + /*if ( _iShape == _nbShape )*/ { + + // create bounding box for every shape + + int iShape = 0; + TopoDS_Shape tabShape[_nbShape]; + double tabBox[_nbShape][6]; + Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax; + + TopExp_Explorer expBox (theShape, TopAbs_SOLID); + for (; expBox.More(); expBox.Next()) { + tabShape[iShape] = expBox.Current(); + Bnd_Box BoundingBox; + BRepBndLib::Add(expBox.Current(), BoundingBox); + BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax); + tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax; + tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax; + tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax; + iShape++; + } + + // 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 - aTmpDir = "C:\\"; + aGenericName += GetCurrentProcessId(); #else - aTmpDir = "/tmp/"; + aGenericName += getpid(); #endif - } - // a unique name helps to avoid access to the same files by eg different users - int aUniqueNb; + 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 = + Ok = #ifdef WIN32 - aUniqueNb = GetCurrentProcessId(); + aFacesFile->is_open() && aPointsFile->is_open(); #else - aUniqueNb = getpid(); + aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open(); #endif + if (!Ok) + return error(SMESH_Comment("Can't write into ") << aTmpDir); + + vector aNodeByGhs3dId; - const QString aGenericName = (aTmpDir + ( "GHS3D_%1" )).arg( aUniqueNb ); - const QString aFacesFileName = aGenericName + ".faces"; - const QString aPointsFileName = aGenericName + ".points"; - const QString aResultFileName = aGenericName + ".noboite"; - const QString aErrorFileName = aGenericName + ".log"; + Ok = ( writeFaces ( aFacesFile, meshDS, theShape, aNodeByGhs3dId ) && + writePoints( aPointsFile, meshDS, aNodeByGhs3dId )); - // remove old files - QFile( aFacesFileName ).remove(); - QFile( aPointsFileName ).remove(); - QFile( aResultFileName ).remove(); - QFile( aErrorFileName ).remove(); + 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??? + // ----------------- - // ----------------- - // make input files - // ----------------- + // 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 - ofstream aFacesFile ( aFacesFileName.latin1() , ios::out); - ofstream aPointsFile ( aPointsFileName.latin1() , ios::out); - bool Ok = + TCollection_AsciiString memory; #ifdef WIN32 - aFacesFile->is_open() && aPointsFile->is_open(); + // ???? #else - aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open(); + 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 - if (!Ok) - { - MESSAGE( "Can't write into " << aTmpDir << " directory"); - return false; - } - SMESHDS_Mesh* meshDS = theMesh.GetMeshDS(); - map aSmdsToGhs3dIdMap; - map aGhs3dIdToNodeMap; - - Ok = - (writePoints( aPointsFile, meshDS, aSmdsToGhs3dIdMap, aGhs3dIdToNodeMap ) && - writeFaces ( aFacesFile, meshDS, aSmdsToGhs3dIdMap )); - aFacesFile.close(); - aPointsFile.close(); + OSD_File( aResultFileName ).Remove(); // old file prevents writing a new one - if ( ! Ok ) - return false; + 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 - // ----------------- - // run ghs3d mesher WIN32??? - // ----------------- + MESSAGE("GHS3DPlugin_GHS3D::Compute() " << cmd ); + system( cmd.ToCString() ); // run - QString cmd = "ghs3d " - "-m 1000 " // memory: 1000 M - "-f " + aGenericName + // file to read - " 1>" + aErrorFileName; // dump into file - if (system(cmd.latin1())) - { - MESSAGE ("command failed: " << cmd.latin1() ); - return false; - } - - // -------------- - // read a result - // -------------- - - FILE * aResultFile = fopen( aResultFileName.latin1(), "r" ); - if (!aResultFile) - { - MESSAGE( "GHS3D ERROR: see " << aErrorFileName.latin1() ); - return false; - } - - Ok = readResult( aResultFile, meshDS, theShape, aGhs3dIdToNodeMap ); - fclose(aResultFile); - - if ( Ok ) { - QFile( aFacesFileName ).remove(); - QFile( aPointsFileName ).remove(); - QFile( aResultFileName ).remove(); - QFile( aErrorFileName ).remove(); - } - return Ok; -} - - -//============================================================================= -/*! - * - */ -//============================================================================= + cout << endl; + cout << "End of Ghs3d execution !" << endl; -ostream & GHS3DPlugin_GHS3D::SaveTo(ostream & save) -{ - return save; -} + // -------------- + // read a result + // -------------- -//============================================================================= -/*! - * - */ -//============================================================================= + // Mapping the result file -istream & GHS3DPlugin_GHS3D::LoadFrom(istream & load) -{ - return load; -} + 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, tabShape, tabBox, _nbShape, aNodeByGhs3dId ); + } -//============================================================================= -/*! - * - */ -//============================================================================= + // --------------------- + // remove working files + // --------------------- -ostream & operator << (ostream & save, GHS3DPlugin_GHS3D & hyp) -{ - return hyp.SaveTo( save ); -} + if ( Ok ) + { + OSD_File( aLogFileName ).Remove(); + } + 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 + OSD_File( aLogFileName ).Remove(); + error(COMPERR_ALGO_FAILED, "ghs3d: command not found" ); + } -//============================================================================= -/*! - * - */ -//============================================================================= + 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 ( !Ok ) + cout << "not "; + cout << "treated !" << endl; + cout << endl; + } + } -istream & operator >> (istream & load, GHS3DPlugin_GHS3D & hyp) -{ - return hyp.LoadFrom( load ); + return Ok; }