Salome HOME
Join modifications from V3_2_0_maintainance (V3_2_6pre4 - T32x_16Aug2007_16h00m)
[plugins/ghs3dplugin.git] / src / GHS3DPlugin_GHS3D.cxx
index b779e95e346cf1a3f53fac0fed93859b5fa68127..547149056d9230829c391c4244684cefd3fbd578 100644 (file)
@@ -19,7 +19,7 @@
 //=============================================================================
 // 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$
@@ -29,6 +29,7 @@ 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"
@@ -42,6 +43,18 @@ using namespace std;
 #include <sys/sysinfo.h>
 #endif
 
+//#include <Standard_Stream.hxx>
+
+#include <BRepGProp.hxx>
+#include <BRepBndLib.hxx>
+#include <BRepClass_FaceClassifier.hxx>
+#include <BRepClass3d_SolidClassifier.hxx>
+#include <TopAbs.hxx>
+#include <Bnd_Box.hxx>
+#include <GProp_GProps.hxx>
+#include <Precision.hxx>
+
+#define castToNode(n) static_cast<const SMDS_MeshNode *>( n );
 
 #ifdef _DEBUG_
 #define DUMP(txt) \
@@ -50,6 +63,16 @@ using namespace std;
 #define DUMP(txt)
 #endif
 
+extern "C"
+{
+#ifndef WNT
+#include <unistd.h>
+#include <sys/mman.h>
+#endif
+#include <sys/stat.h>
+#include <fcntl.h>
+}
+
 //=============================================================================
 /*!
  *  
@@ -62,6 +85,9 @@ 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;
 }
 
 //=============================================================================
@@ -81,40 +107,60 @@ 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 TopoDS_Shape&   theShape,
-                        const map <int,int> & theSmdsToGhs3dIdMap)
+static bool writeFaces (ofstream &                      theFile,
+                        SMESHDS_Mesh *                  theMesh,
+                        const TopoDS_Shape&             theShape,
+                        vector <const SMDS_MeshNode*> & 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;
-  TopExp_Explorer fExp( theShape, TopAbs_FACE );
-  for ( ; fExp.More(); fExp.Next() ) {
-    SMESHDS_SubMesh* sm = theMesh->MeshElements( fExp.Current() );
+  // 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 ) {
-      SMDS_ElemIteratorPtr eIt = sm->GetElements();
+      eIt = sm->GetElements();
       while ( eIt->more() ) {
         faces.push_back( eIt->next() );
         nbFaces++;
@@ -125,30 +171,31 @@ static bool writeFaces (ofstream &            theFile,
   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
-  list< const SMDS_MeshElement* >::iterator f = faces.begin();
+
+  map<const SMDS_MeshNode*,int> aNodeToGhs3dIdMap;
+  f = faces.begin();
   for ( ; f != faces.end(); ++f )
   {
-    // NB_NODES
-    const SMDS_MeshElement* elem = *f;
-    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<int,int>::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
@@ -158,6 +205,14 @@ static bool writeFaces (ofstream &            theFile,
     theFile << endl;
   }
 
+  // put nodes to theNodeByGhs3dId vector
+  theNodeByGhs3dId.resize( aNodeToGhs3dIdMap.size() );
+  map<const SMDS_MeshNode*,int>::const_iterator n2id = aNodeToGhs3dIdMap.begin();
+  for ( ; n2id != aNodeToGhs3dIdMap.end(); ++ n2id)
+  {
+    theNodeByGhs3dId[ n2id->second - 1 ] = n2id->first; // ghs3d ids count from 1
+  }
+
   return true;
 }
 
@@ -166,10 +221,9 @@ static bool writeFaces (ofstream &            theFile,
 //purpose  : 
 //=======================================================================
 
-static bool writePoints (ofstream &                       theFile,
-                         SMESHDS_Mesh *                   theMesh,
-                         map <int,int> &                  theSmdsToGhs3dIdMap,
-                         map <int,const SMDS_MeshNode*> & theGhs3dIdToNodeMap)
+static bool writePoints (ofstream &                            theFile,
+                         SMESHDS_Mesh *                        theMesh,
+                         const vector <const SMDS_MeshNode*> & theNodeByGhs3dId)
 {
   // record structure:
   //
@@ -177,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 SMDS_MeshNode*>::const_iterator nodeIt = theNodeByGhs3dId.begin();
+  vector<const SMDS_MeshNode*>::const_iterator after  = theNodeByGhs3dId.end();
+  for ( ; nodeIt != after; ++nodeIt )
   {
-    const SMDS_MeshNode* node = it->next();
-    theSmdsToGhs3dIdMap.insert( map <int,int>::value_type( node->GetID(), aGhs3dID ));
-    theGhs3dIdToNodeMap.insert (map <int,const SMDS_MeshNode*>::value_type( aGhs3dID, node ));
-    aGhs3dID++;
+    node = *nodeIt;
 
     // X Y Z DUMMY_INT
     theFile
@@ -211,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  : 
@@ -255,120 +327,117 @@ 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 <list>
 //=======================================================================
-//function : readResult
+//function : readResultFile
 //purpose  : 
 //=======================================================================
 
-static bool readResult(FILE *                          theFile,
-                       SMESHDS_Mesh *                  theMesh,
-                       const TopoDS_Shape&             theShape,
-                       map <int,const SMDS_MeshNode*>& 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;
-  int shapeID = theMesh->ShapeToIndex( theShape );
-
-  // ----------------------------------------
-  // 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;
-
-  // -------------------------------------------
-  // 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 <int,const SMDS_MeshNode*>::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, shapeID );
-        theGhs3dIdToNodeMap.insert ( make_pair( ID, aNewNode ));
-        node[ iNode ] = aNewNode;
-      }
-      else
-      {
-        node[ iNode ] = IdNode->second;
-      }
+static bool readResultFile(const int                      fileOpen,
+                           SMESHDS_Mesh*                  theMeshDS,
+                           TopoDS_Shape                   tabShape[],
+                           double                         tabBox[][6],
+                           const int                      nShape,
+                           vector <const SMDS_MeshNode*>& 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 <int,const SMDS_MeshNode*>::iterator IdNode;
+  SMDS_MeshElement* aTet;
+
+//   for (int i=0; i<nShape; i++)
+//     tabID[i] = 0;
+
+  // 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
+  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] );
+      theNodeByGhs3dId[ iNode ] = aNewNode;
     }
-    // create a tetrahedron with orientation as for MED
-    SMDS_MeshElement* aTet = theMesh->AddVolume( node[1], node[0], node[2], node[3] );
-    theMesh->SetMeshElementOnShape( aTet, shapeID );
   }
 
-  // ------------------------
-  // record 3:
-  // read and set nodes' XYZ
-  // ------------------------
-  GHS3DPlugin_ReadLine( aPtr, aBuffer, theFile, aLineNb );
-  for (int iNode = 0; iNode < nbNodes; iNode++)
-  {
-    // 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;
-        }
-      }
+  // 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] );
+    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];
+    }
+    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 );
     }
-    // do not move old nodes
-    int ID = iNode + 1;
-    if (ID <= nbInputNodes)
-      continue;
-    // find a node
-    map <int,const SMDS_MeshNode*>::iterator IdNode = theGhs3dIdToNodeMap.find( ID );
-    ASSERT ( IdNode != theGhs3dIdToNodeMap.end());
-    SMDS_MeshNode* node = const_cast<SMDS_MeshNode*> ( (*IdNode).second );
-
-    // set XYZ
-    theMesh->MoveNode( node, coord[0], coord[1], coord[2] );
   }
-
-  return nbElems;
+  if ( nbElems )
+    cout << nbElems << " tetrahedrons have been associated to " << nbTriangle << " shapes" << endl;
+  munmap(mapPtr, length);
+  close(fileOpen);
+  return true;
 }
 
 //=======================================================================
@@ -401,49 +470,32 @@ static TCollection_AsciiString getTmpDir()
 
 //================================================================================
 /*!
- * \brief Decrease amount of memory GHS3D may use until it can allocate it
-  * \param nbMB - memory size to adjust
-  * \param aLogFileName - file for GHS3D output
-  * \retval bool - false if GHS3D can not run for any reason
+ * \brief Look for a line containing a text in a file
+  * \retval bool - true if the line is found
  */
 //================================================================================
 
-static bool adjustMemory(int & nbMB, const TCollection_AsciiString & aLogFileName)
+static bool findLineContaing(const TCollection_AsciiString& theText,
+                             const TCollection_AsciiString& theFile,
+                             TCollection_AsciiString &      theFoundLine)
 {
-  TCollection_AsciiString cmd( "ghs3d -m " );
-  cmd += nbMB;
-  cmd += " 1>";
-  cmd += aLogFileName;
-
-  system( cmd.ToCString() ); // run
-
-  // analyse log file
-
-  FILE * aLogFile = fopen( aLogFileName.ToCString(), "r" );
-  if ( aLogFile )
+  bool found = false;
+  if ( FILE * aFile = fopen( theFile.ToCString(), "r" ))
   {
-    bool memoryOK = true;
     char * aPtr;
     char aBuffer[ GHS3DPlugin_BUFLENGTH ];
     int aLineNb = 0;
-    do { 
-      GHS3DPlugin_ReadLine( aPtr, aBuffer, aLogFile, aLineNb );
+    do {
+      GHS3DPlugin_ReadLine( aPtr, aBuffer, aFile, aLineNb );
       if ( aPtr ) {
-        TCollection_AsciiString line( aPtr );
-        if ( line.Search( "UNABLE TO ALLOCATE MEMORY" ) > 0 )
-          memoryOK = false;
+        theFoundLine = aPtr;
+        found = theFoundLine.Search( theText ) >= 0;
       }
-    } while ( aPtr && memoryOK );
+    } while ( aPtr && !found );
 
-    fclose( aLogFile );
-
-    if ( !memoryOK ) {
-      nbMB *= 0.75;
-      return adjustMemory( nbMB, aLogFileName );
-    }
-    return true;
+    fclose( aFile );
   }
-  return false;
+  return found;
 }
 
 //=============================================================================
@@ -455,130 +507,210 @@ static bool adjustMemory(int & nbMB, const TCollection_AsciiString & aLogFileNam
 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();
 
-  // make a unique working file name
-  // to avoid access to the same files by eg different users
+  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++;
+  }
   
-  TCollection_AsciiString aGenericName, aTmpDir = getTmpDir();
-  aGenericName = aTmpDir + "GHS3D_";
+  //_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
-  aGenericName += GetCurrentProcessId();
+    aGenericName += GetCurrentProcessId();
 #else
-  aGenericName += getpid();
+    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 =
+    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
-    aFacesFile->is_open() && aPointsFile->is_open();
+      aFacesFile->is_open() && aPointsFile->is_open();
 #else
-    aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open();
+      aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open();
 #endif
-  if (!Ok)
-  {
-    INFOS( "Can't write into " << aTmpDir.ToCString());
-    return false;
-  }
-  map <int,int> aSmdsToGhs3dIdMap;
-  map <int,const SMDS_MeshNode*> aGhs3dIdToNodeMap;
+    if (!Ok)
+       return error(SMESH_Comment("Can't write into ") << aTmpDir);
 
-  Ok =
-    (writePoints( aPointsFile, meshDS, aSmdsToGhs3dIdMap, aGhs3dIdToNodeMap ) &&
-     writeFaces ( aFacesFile, meshDS, theShape, aSmdsToGhs3dIdMap ));
+    vector <const SMDS_MeshNode*> aNodeByGhs3dId;
 
-  aFacesFile.close();
-  aPointsFile.close();
+    Ok = ( writeFaces ( aFacesFile,  meshDS, theShape, aNodeByGhs3dId ) &&
+           writePoints( aPointsFile, meshDS, aNodeByGhs3dId ));
 
-  if ( ! Ok ) {
-    if ( !getenv("GHS3D_KEEP_FILES") ) {
-      OSD_File( aFacesFileName ).Remove();
-      OSD_File( aPointsFileName ).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);
     }
-    return false;
-  }
 
-  // -----------------
-  // run ghs3d mesher              WIN32???
-  // -----------------
+    // -----------------
+    // 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;
-#ifndef WIN32
-  struct sysinfo si;
-  int err = sysinfo( &si );
-  if ( err == 0 ) {
-    int MB = 0.9 * ( si.freeram + si.freeswap ) * si.mem_unit / 1024 / 1024;
-    adjustMemory( MB, aLogFileName );
-    memory = "-m ";
-    memory += MB;
-  }
+    // 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
+    OSD_File( aResultFileName ).Remove(); // old file prevents writing a new one
 
-  system( cmd.ToCString() ); // run
+    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
 
-  // --------------
-  // read a result
-  // --------------
+    MESSAGE("GHS3DPlugin_GHS3D::Compute() " << cmd );
+    system( cmd.ToCString() ); // run
 
-  FILE * aResultFile = fopen( aResultFileName.ToCString(), "r" );
-  if (aResultFile)
-  {
-    Ok = readResult( aResultFile, meshDS, theShape, aGhs3dIdToNodeMap );
-    fclose(aResultFile);
-  }
-  else
-    Ok = false;
+    cout << endl;
+    cout << "End of Ghs3d execution !" << endl;
+
+    // --------------
+    // read a result
+    // --------------
+
+    // Mapping the result file
+
+    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
   // ---------------------
 
-  if ( Ok ) {
-    OSD_File( aLogFileName ).Remove();
-  }
-  else if ( OSD_File( aLogFileName ).Size() > 0 ) {
-    INFOS( "GHS3D Error: see " << aLogFileName.ToCString() );
-  }
-  else {
-    OSD_File( aLogFileName ).Remove();
-    INFOS( "GHS3D Error: command '" << cmd.ToCString() << "' failed" );
-  }
+    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 ( !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;
+    }
   }
-  
+
   return Ok;
 }