Salome HOME
Merge from V7_2_BR 09/08/2013
[plugins/hexoticplugin.git] / src / HexoticPlugin / HexoticPlugin_Hexotic.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // ---
21 // File   : HexoticPlugin_Hexotic.cxx
22 // Author : Lioka RAZAFINDRAZAKA (CEA)
23 // ---
24 //
25 #include "HexoticPlugin_Hexotic.hxx"
26 #include "HexoticPlugin_Hypothesis.hxx"
27
28 #include "utilities.h"
29
30 #ifndef WIN32
31 #include <sys/sysinfo.h>
32 #else
33 #include <errno.h>
34 #endif
35
36 #ifdef _DEBUG_
37 #define DUMP(txt) \
38 //  cout << txt
39 #else
40 #define DUMP(txt)
41 #endif
42
43 #include <SMESHDS_Mesh.hxx>
44 #include <SMESHDS_GroupBase.hxx>
45 #include <SMESH_ComputeError.hxx>
46 #include <SMESH_File.hxx>
47 #include <SMESH_Gen.hxx>
48 #include <SMESH_HypoFilter.hxx>
49 #include <SMESH_MesherHelper.hxx>
50 #include <SMESH_subMesh.hxx>
51
52 #include <list>
53 #include <cstdlib>
54 #include <iostream>
55
56 #include <Standard_ProgramError.hxx>
57
58 #include <BRep_Tool.hxx>
59 #include <BRepBndLib.hxx>
60 #include <BRepClass3d_SolidClassifier.hxx>
61 #include <BRepBuilderAPI_MakeVertex.hxx>
62 #include <BRepExtrema_DistShapeShape.hxx>
63 #include <OSD_File.hxx>
64 #include <Precision.hxx>
65 #include <TopExp_Explorer.hxx>
66 #include <TopTools_MapOfShape.hxx>
67 #include <TopoDS.hxx>
68
69 #include <Basics_Utils.hxx>
70
71 static void removeFile( const TCollection_AsciiString& fileName )
72 {
73   try {
74     OSD_File( fileName ).Remove();
75   }
76   catch ( Standard_ProgramError ) {
77     MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
78   }
79 }
80
81 //=============================================================================
82 /*!
83  *  
84  */
85 //=============================================================================
86
87 HexoticPlugin_Hexotic::HexoticPlugin_Hexotic(int hypId, int studyId, SMESH_Gen* gen)
88   : SMESH_3D_Algo(hypId, studyId, gen)
89 {
90   MESSAGE("HexoticPlugin_Hexotic::HexoticPlugin_Hexotic");
91   _name = "Hexotic_3D";
92   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
93 //   _onlyUnaryInput = false;
94   _requireShape = false;
95   _iShape=0;
96   _nbShape=0;
97   _hexoticFilesKept=false;
98   _compatibleHypothesis.push_back("Hexotic_Parameters");
99 #ifdef WITH_BLSURFPLUGIN
100   _blsurfHypo = NULL;
101 #endif
102   _compute_canceled = false;
103 }
104
105 //=============================================================================
106 /*!
107  *  
108  */
109 //=============================================================================
110
111 HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic()
112 {
113   MESSAGE("HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic");
114 }
115
116
117 #ifdef WITH_BLSURFPLUGIN
118 bool HexoticPlugin_Hexotic::CheckBLSURFHypothesis( SMESH_Mesh&         aMesh,
119                                                    const TopoDS_Shape& aShape )
120 {
121   // MESSAGE("HexoticPlugin_Hexotic::CheckBLSURFHypothesis");
122   _blsurfHypo = NULL;
123
124   std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
125   const SMESHDS_Hypothesis* theHyp;
126
127   // If a BLSURF hypothesis is applied, get it
128   SMESH_HypoFilter blsurfFilter;
129   blsurfFilter.Init( blsurfFilter.HasName( "BLSURF_Parameters" ));
130   std::list<const SMESHDS_Hypothesis *> appliedHyps;
131   aMesh.GetHypotheses( aShape, blsurfFilter, appliedHyps, false );
132
133   if ( appliedHyps.size() > 0 ) {
134     itl = appliedHyps.begin();
135     theHyp = (*itl); // use only the first hypothesis
136     std::string hypName = theHyp->GetName();
137     if (hypName == "BLSURF_Parameters") {
138       _blsurfHypo = static_cast<const BLSURFPlugin_Hypothesis*> (theHyp);
139       ASSERT(_blsurfHypo);
140       return true;
141     }
142   }
143   return false;
144 }
145 #endif
146
147 //=============================================================================
148 /*!
149  *  
150  */
151 //=============================================================================
152
153 bool HexoticPlugin_Hexotic::CheckHypothesis( SMESH_Mesh&                          aMesh,
154                                              const TopoDS_Shape&                  aShape,
155                                              SMESH_Hypothesis::Hypothesis_Status& aStatus )
156 {
157   // MESSAGE("HexoticPlugin_Hexotic::CheckHypothesis");
158   _hypothesis = NULL;
159
160   std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
161   const SMESHDS_Hypothesis* theHyp;
162
163   const std::list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
164   int nbHyp = hyps.size();
165   if (!nbHyp) {
166     aStatus = SMESH_Hypothesis::HYP_OK;
167     return true;  // can work with no hypothesis
168   }
169
170   itl = hyps.begin();
171   theHyp = (*itl); // use only the first hypothesis
172
173   std::string hypName = theHyp->GetName();
174   if (hypName == "Hexotic_Parameters") {
175     _hypothesis = static_cast<const HexoticPlugin_Hypothesis*> (theHyp);
176     ASSERT(_hypothesis);
177     aStatus = SMESH_Hypothesis::HYP_OK;
178   }
179   else
180     aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
181   
182 #ifdef WITH_BLSURFPLUGIN
183   CheckBLSURFHypothesis(aMesh, aShape);
184 #endif
185   
186   return aStatus == SMESH_Hypothesis::HYP_OK;
187 }
188
189 //=======================================================================
190 //function : findShape
191 //purpose  :
192 //=======================================================================
193
194 static TopoDS_Shape findShape(SMDS_MeshNode**     t_Node,
195                               TopoDS_Shape        aShape,
196                               const TopoDS_Shape* t_Shape,
197                               double**            t_Box,
198                               const int           nShape)
199 {
200   double pntCoor[3];
201   int iShape, nbNode = 8;
202
203   for ( int i=0; i<3; i++ ) {
204     pntCoor[i] = 0;
205     for ( int j=0; j<nbNode; j++ ) {
206       if ( i == 0) pntCoor[i] += t_Node[j]->X();
207       if ( i == 1) pntCoor[i] += t_Node[j]->Y();
208       if ( i == 2) pntCoor[i] += t_Node[j]->Z();
209     }
210     pntCoor[i] /= nbNode;
211   }
212   gp_Pnt aPnt(pntCoor[0], pntCoor[1], pntCoor[2]);
213
214   if ( aShape.IsNull() ) aShape = t_Shape[0];
215   BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
216   if ( !(SC.State() == TopAbs_IN) ) {
217     aShape.Nullify();
218     for (iShape = 0; iShape < nShape && aShape.IsNull(); iShape++) {
219       if ( !( pntCoor[0] < t_Box[iShape][0] || t_Box[iShape][1] < pntCoor[0] ||
220               pntCoor[1] < t_Box[iShape][2] || t_Box[iShape][3] < pntCoor[1] ||
221               pntCoor[2] < t_Box[iShape][4] || t_Box[iShape][5] < pntCoor[2]) ) {
222         BRepClass3d_SolidClassifier SC (t_Shape[iShape], aPnt, Precision::Confusion());
223         if (SC.State() == TopAbs_IN)
224           aShape = t_Shape[iShape];
225       }
226     }
227   }
228   return aShape;
229 }
230
231 //=======================================================================
232 //function : findEdge
233 //purpose  :
234 //=======================================================================
235
236 static int findEdge(const SMDS_MeshNode* aNode,
237                     const SMESHDS_Mesh*  aMesh,
238                     const int            nEdge,
239                     const TopoDS_Shape*  t_Edge) {
240
241   TopoDS_Shape aPntShape, foundEdge;
242   TopoDS_Vertex aVertex;
243   gp_Pnt aPnt( aNode->X(), aNode->Y(), aNode->Z() );
244
245   int foundInd, ind;
246   double nearest = RealLast(), *t_Dist;
247   double epsilon = Precision::Confusion();
248
249   t_Dist = new double[ nEdge ];
250   aPntShape = BRepBuilderAPI_MakeVertex( aPnt ).Shape();
251   aVertex   = TopoDS::Vertex( aPntShape );
252
253   for ( ind=0; ind < nEdge; ind++ ) {
254     BRepExtrema_DistShapeShape aDistance ( aVertex, t_Edge[ind] );
255     t_Dist[ind] = aDistance.Value();
256     if ( t_Dist[ind] < nearest ) {
257       nearest   = t_Dist[ind];
258       foundEdge = t_Edge[ind];
259       foundInd  = ind;
260       if ( nearest < epsilon )
261         ind = nEdge;
262     }
263   }
264
265   delete [] t_Dist;
266   return aMesh->ShapeToIndex( foundEdge );
267 }
268
269 //=======================================================================
270 //function : getNbShape
271 //purpose  :
272 //=======================================================================
273
274 static int getNbShape(std::string aFile, std::string aString, int defaultValue=0) {
275   int number = defaultValue;
276   std::string aLine;
277   std::ifstream file(aFile.c_str());
278   while ( !file.eof() ) {
279     getline( file, aLine);
280     if ( aLine == aString ) {
281       getline( file, aLine);
282       std::istringstream stringFlux( aLine );
283       stringFlux >> number;
284       number = ( number + defaultValue + std::abs(number - defaultValue) ) / 2;
285       break;
286     }
287   }
288   file.close();
289   return number;
290 }
291
292 //=======================================================================
293 //function : countShape
294 //purpose  :
295 //=======================================================================
296
297 template < class Mesh, class Shape >
298 static int countShape( Mesh* mesh, Shape shape ) {
299   TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
300   TopTools_MapOfShape mapShape;
301   int nbShape = 0;
302   for ( ; expShape.More(); expShape.Next() ) {
303     if (mapShape.Add(expShape.Current())) {
304       nbShape++;
305     }
306   }
307   return nbShape;
308 }
309
310 //=======================================================================
311 //function : getShape
312 //purpose  :
313 //=======================================================================
314
315 template < class Mesh, class Shape, class Tab >
316 void getShape(Mesh* mesh, Shape shape, Tab *t_Shape) {
317   TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
318   TopTools_MapOfShape mapShape;
319   for ( int i=0; expShape.More(); expShape.Next() ) {
320     if (mapShape.Add(expShape.Current())) {
321       t_Shape[i] = expShape.Current();
322       i++;
323     }
324   }
325   return;
326 }
327
328 //=======================================================================
329 //function : printWarning
330 //purpose  :
331 //=======================================================================
332
333 static void printWarning(const int nbExpected, std::string aString, const int nbFound) {
334   cout << std::endl;
335   cout << "WARNING : " << nbExpected << " " << aString << " expected, Hexotic has found " << nbFound << std::endl;
336   cout << "=======" << std::endl;
337   cout << std::endl;
338   return;
339 }
340
341 //=======================================================================
342 //function : removeHexoticFiles
343 //purpose  :
344 //=======================================================================
345
346 static void removeHexoticFiles(TCollection_AsciiString file_In, TCollection_AsciiString file_Out) {
347   removeFile( file_In );
348   removeFile( file_Out );
349 }
350
351 //=======================================================================
352 //function : readResult
353 //purpose  : Read GMF file in case of a mesh with geometry
354 //=======================================================================
355
356 static bool readResult(std::string         theFile,
357 #ifdef WITH_SMESH_CANCEL_COMPUTE
358                        HexoticPlugin_Hexotic*  theAlgo,
359 #endif
360                        SMESHDS_Mesh*       theMesh,
361                        const int           nbShape,
362                        const TopoDS_Shape* tabShape,
363                        double**            tabBox)
364 {
365   // ---------------------------------
366   // Optimisation of the plugin ...
367   // Retrieve the correspondance edge --> shape
368   // (which is very costly) only when user
369   // has defined at least one group of edges
370   // which should be rare for a 3d mesh !
371   // ---------------------------------
372   
373   bool retrieve_edges = false;
374   const std::set<SMESHDS_GroupBase*>& aGroups = theMesh->GetGroups();
375   set<SMESHDS_GroupBase*>::const_iterator GrIt = aGroups.begin();
376   for (; GrIt != aGroups.end(); GrIt++)
377     {
378       SMESHDS_GroupBase* aGrp = *GrIt;
379       if ( !aGrp )
380         continue;
381       if ( aGrp->GetType() == SMDSAbs_Edge )
382         {
383           retrieve_edges = true;
384           break;
385         }
386     }
387   
388   // ---------------------------------
389   // Read generated elements and nodes
390   // ---------------------------------
391
392   TopoDS_Shape aShape;
393   TopoDS_Vertex aVertex;
394   std::string token;
395   int EndOfFile = 0, nbElem = 0, nField = 9, nbRef = 0;
396   int aHexoticNodeID = 0, shapeID, hexoticShapeID;
397   const int IdShapeRef = 2;
398   int *tabID, *tabRef, *nodeAssigne;
399   bool *tabDummy, hasDummy = false;
400   double epsilon = Precision::Confusion();
401   std::map <std::string,int> mapField;
402   SMDS_MeshNode** HexoticNode;
403   TopoDS_Shape *tabCorner, *tabEdge;
404
405   const int nbDomains = countShape( theMesh, TopAbs_SHELL );
406   const int holeID = -1;
407
408   // tabID    = new int[nbShape];
409   tabID    = new int[nbDomains];
410   tabRef   = new int[nField];
411   tabDummy = new bool[nField];
412
413   for (int i=0; i<nbDomains; i++)
414     tabID[i] = 0;
415   if ( nbDomains == 1 )
416     tabID[0] = theMesh->ShapeToIndex( tabShape[0] );
417
418   mapField["MeshVersionFormatted"] = 0; tabRef[0] = 0; tabDummy[0] = false;
419   mapField["Dimension"]            = 1; tabRef[1] = 0; tabDummy[1] = false;
420   mapField["Vertices"]             = 2; tabRef[2] = 3; tabDummy[2] = true;
421   mapField["Corners"]              = 3; tabRef[3] = 1; tabDummy[3] = false;
422   mapField["Edges"]                = 4; tabRef[4] = 2; tabDummy[4] = true;
423   mapField["Ridges"]               = 5; tabRef[5] = 1; tabDummy[5] = false;
424   mapField["Quadrilaterals"]       = 6; tabRef[6] = 4; tabDummy[6] = true;
425   mapField["Hexahedra"]            = 7; tabRef[7] = 8; tabDummy[7] = true;
426   mapField["End"]                  = 8; tabRef[8] = 0; tabDummy[0] = false;
427
428   SMDS_NodeIteratorPtr itOnHexoticInputNode = theMesh->nodesIterator();
429   while ( itOnHexoticInputNode->more() )
430     theMesh->RemoveNode( itOnHexoticInputNode->next() );
431
432   int nbVertices   = getNbShape(theFile, "Vertices");
433   int nbCorners    = getNbShape(theFile, "Corners", countShape( theMesh, TopAbs_VERTEX ));
434   int nbShapeEdge  = countShape( theMesh, TopAbs_EDGE );
435
436   tabCorner   = new TopoDS_Shape[ nbCorners ];
437   tabEdge     = new TopoDS_Shape[ nbShapeEdge ];
438   nodeAssigne = new int[ nbVertices + 1 ];
439   HexoticNode = new SMDS_MeshNode*[ nbVertices + 1 ];
440
441   getShape(theMesh, TopAbs_VERTEX, tabCorner);
442   getShape(theMesh, TopAbs_EDGE,   tabEdge);
443
444   MESSAGE("Read " << theFile << " file");
445   std::ifstream fileRes(theFile.c_str());
446   ASSERT(fileRes);
447
448   while ( EndOfFile == 0  ) {
449     int dummy;
450     fileRes >> token;
451
452     if (mapField.count(token)) {
453       nField   = mapField[token];
454       nbRef    = tabRef[nField];
455       hasDummy = tabDummy[nField];
456     }
457     else {
458       nField = -1;
459       nbRef = 0;
460     }
461
462     nbElem = 0;
463     if ( nField < (mapField.size() - 1) && nField >= 0 )
464       fileRes >> nbElem;
465
466     switch (nField) {
467       case 0: { // "MeshVersionFormatted"
468         MESSAGE(token << " " << nbElem);
469         break;
470       }
471       case 1: { // "Dimension"
472         MESSAGE("Mesh dimension " << nbElem << "D");
473         break;
474       }
475       case 2: { // "Vertices"
476         MESSAGE("Read " << nbElem << " " << token);
477         int aHexoticID;
478         double *coord;
479         SMDS_MeshNode * aHexoticNode;
480
481         coord = new double[nbRef];
482         for ( int iElem = 0; iElem < nbElem; iElem++ ) {
483 #ifdef WITH_SMESH_CANCEL_COMPUTE
484           if(theAlgo->computeCanceled())
485             {
486               return false;
487             }
488 #endif
489           aHexoticID = iElem + 1;
490           for ( int iCoord = 0; iCoord < 3; iCoord++ )
491             fileRes >> coord[ iCoord ];
492           fileRes >> dummy;
493           aHexoticNode = theMesh->AddNode(coord[0], coord[1], coord[2]);
494           HexoticNode[ aHexoticID ] = aHexoticNode;
495           nodeAssigne[ aHexoticID ] = 0;
496         }
497         delete [] coord;
498         break;
499       }
500       case 3: // "Corners"
501       case 4: // "Edges"
502       case 5: // "Ridges"
503       case 6: // "Quadrilaterals"
504       case 7: { // "Hexahedra"
505         MESSAGE("Read " << nbElem << " " << token);
506         SMDS_MeshNode** node;
507         int nodeDim, *nodeID;
508         SMDS_MeshElement * aHexoticElement = 0;
509
510         node   = new SMDS_MeshNode*[ nbRef ];
511         nodeID = new int[ nbRef ];
512         for ( int iElem = 0; iElem < nbElem; iElem++ ) {
513 #ifdef WITH_SMESH_CANCEL_COMPUTE
514           if(theAlgo->computeCanceled())
515             {
516               return false;
517             }
518 #endif
519           for ( int iRef = 0; iRef < nbRef; iRef++ ) {
520             fileRes >> aHexoticNodeID;                          // read nbRef aHexoticNodeID
521             node[ iRef ]   = HexoticNode[ aHexoticNodeID ];
522             nodeID[ iRef ] = aHexoticNodeID;
523           }
524           if ( hasDummy )
525             fileRes >> dummy;
526           switch (nField) {
527             case 3: { // "Corners"
528               nodeDim = 1;
529               gp_Pnt HexoticPnt ( node[0]->X(), node[0]->Y(), node[0]->Z() );
530               for ( int i=0; i<nbElem; i++ ) {
531                 aVertex = TopoDS::Vertex( tabCorner[i] );
532                 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
533                 if ( aPnt.Distance( HexoticPnt ) < epsilon )
534                   break;
535               }
536               break;
537             }
538             case 4: { // "Edges"
539               nodeDim = 2;
540               aHexoticElement = theMesh->AddEdge( node[0], node[1] );
541               int iNode = 1;
542               if ( nodeAssigne[ nodeID[0] ] == 0 || nodeAssigne[ nodeID[0] ] == 2 )
543                 iNode = 0;
544               if(retrieve_edges)
545                 shapeID = findEdge( node[iNode], theMesh, nbShapeEdge, tabEdge );
546               else
547                 shapeID = 0;
548               break;
549             }
550             case 5: { // "Ridges"
551               break;
552             }
553             case 6: { // "Quadrilaterals"
554               nodeDim = 3;
555               aHexoticElement = theMesh->AddFace( node[0], node[1], node[2], node[3] );
556               shapeID = dummy;
557               break;
558             }
559             case 7: { // "Hexahedra"
560               nodeDim = 4;
561               if ( nbDomains > 1 ) {
562                 hexoticShapeID = dummy - IdShapeRef;
563                 if ( tabID[ hexoticShapeID ] == 0 ) {
564                   aShape = findShape(node, aShape, tabShape, tabBox, nbShape);
565                   shapeID = aShape.IsNull() ? holeID : theMesh->ShapeToIndex( aShape );
566                   tabID[ hexoticShapeID ] = shapeID;
567                 }
568                 else
569                   shapeID = tabID[ hexoticShapeID ];
570                 if ( iElem == (nbElem - 1) ) {
571                   int shapeAssociated = 0;
572                   for ( int i=0; i<nbDomains; i++ ) {
573                     if (tabID[i] > 0 )
574                       shapeAssociated += 1;
575                   }
576                   if ( shapeAssociated != nbShape )
577                     printWarning(nbShape, "domains", shapeAssociated);
578                 }
579               }
580               else {
581                 shapeID = tabID[0];
582               }
583               if ( shapeID != holeID )
584                 aHexoticElement = theMesh->AddVolume( node[0], node[3], node[2], node[1], node[4], node[7], node[6], node[5] );
585               break;
586             }
587           } // switch (nField)
588
589           if ( token != "Ridges" && ( shapeID > 0 || token == "Corners")) {
590             for ( int i=0; i<nbRef; i++ ) {
591               if ( nodeAssigne[ nodeID[i] ] == 0 ) {
592                 if      ( token == "Corners" )        theMesh->SetNodeOnVertex( node[0], aVertex );
593                 else if ( token == "Edges" )          theMesh->SetNodeOnEdge( node[i], shapeID );
594                 else if ( token == "Quadrilaterals" ) theMesh->SetNodeOnFace( node[i], shapeID );
595                 else if ( token == "Hexahedra" )      theMesh->SetNodeInVolume( node[i], shapeID );
596                 nodeAssigne[ nodeID[i] ] = nodeDim;
597               }
598             }
599             if ( token != "Corners" && aHexoticElement )
600               theMesh->SetMeshElementOnShape( aHexoticElement, shapeID );
601           }
602         }
603         delete [] node;
604         delete [] nodeID;
605         break;
606       }
607       case 8: { // "End"
608         EndOfFile = 1;
609         MESSAGE("End of " << theFile << " file");
610         break;
611       }
612       default: {
613         MESSAGE("Unknown Token: " << token);
614       }
615     }
616   }
617   cout << std::endl;
618
619   // remove nodes in holes
620   if ( nbDomains > 1 )
621   {
622     SMESHDS_SubMesh* subMesh;
623     for ( int i = 1; i <= nbVertices; ++i )
624       if ( HexoticNode[i]->NbInverseElements() == 0 )
625       {
626         subMesh =  HexoticNode[i]->getshapeId() > 0 ? theMesh->MeshElements(HexoticNode[i]->getshapeId() ) : 0;
627         theMesh->RemoveFreeNode( HexoticNode[i], subMesh, /*fromGroups=*/false );
628       }
629   }
630   delete [] tabID;
631   delete [] tabRef;
632   delete [] tabDummy;
633   delete [] tabCorner;
634   delete [] tabEdge;
635   delete [] nodeAssigne;
636   delete [] HexoticNode;
637   return true;
638 }
639
640
641 //=======================================================================
642 //function : readResult
643 //purpose  : Read GMF file in case of a mesh w/o geometry
644 //=======================================================================
645
646 static bool readResult(std::string theFile,
647 #ifdef WITH_SMESH_CANCEL_COMPUTE
648                        HexoticPlugin_Hexotic*  theAlgo,
649 #endif
650                        SMESH_MesherHelper* theHelper)
651 {
652   SMESHDS_Mesh* theMesh = theHelper->GetMeshDS();
653
654   // ---------------------------------
655   // Read generated elements and nodes
656   // ---------------------------------
657
658   std::string token;
659   const int nbField = 9;
660   int nField, EndOfFile = 0, nbElem = 0, nbRef = 0;
661   int aHexoticNodeID = 0, shapeID;
662   int tabRef[nbField], *nodeAssigne;
663   bool tabDummy[nbField], hasDummy = false;
664   std::map <std::string,int> mapField;
665   SMDS_MeshNode** HexoticNode;
666
667   mapField["MeshVersionFormatted"] = 0; tabRef[0] = 0; tabDummy[0] = false;
668   mapField["Dimension"]            = 1; tabRef[1] = 0; tabDummy[1] = false;
669   mapField["Vertices"]             = 2; tabRef[2] = 3; tabDummy[2] = true;
670   mapField["Corners"]              = 3; tabRef[3] = 1; tabDummy[3] = false;
671   mapField["Edges"]                = 4; tabRef[4] = 2; tabDummy[4] = true;
672   mapField["Ridges"]               = 5; tabRef[5] = 1; tabDummy[5] = false;
673   mapField["Quadrilaterals"]       = 6; tabRef[6] = 4; tabDummy[6] = true;
674   mapField["Hexahedra"]            = 7; tabRef[7] = 8; tabDummy[7] = true;
675   mapField["End"]                  = 8; tabRef[8] = 0; tabDummy[8] = false;
676
677   theHelper->GetMesh()->Clear();
678
679   int nbVertices = getNbShape(theFile, "Vertices");
680   HexoticNode = new SMDS_MeshNode*[ nbVertices + 1 ];
681   nodeAssigne = new int[ nbVertices + 1 ];
682
683   MESSAGE("Read " << theFile << " file");
684   std::ifstream fileRes(theFile.c_str());
685   ASSERT(fileRes);
686
687   while ( !EndOfFile  )
688   {
689     int dummy;
690     fileRes >> token;
691
692     if (mapField.count(token)) {
693       nField   = mapField[token];
694       nbRef    = tabRef[nField];
695       hasDummy = tabDummy[nField];
696     }
697     else {
698       nField = -1;
699       nbRef = 0;
700     }
701
702     nbElem = 0;
703     if ( nField < (mapField.size() - 1) && nField >= 0 )
704       fileRes >> nbElem;
705
706     switch (nField) {
707     case 0: { // "MeshVersionFormatted"
708       MESSAGE(token << " " << nbElem);
709       break;
710     }
711     case 1: { // "Dimension"
712       MESSAGE("Mesh dimension " << nbElem << "D");
713       break;
714     }
715     case 2: { // "Vertices"
716       MESSAGE("Read " << nbElem << " " << token);
717       int aHexoticID;
718       double coord[3];
719       SMDS_MeshNode * aHexoticNode;
720
721       for ( int iElem = 0; iElem < nbElem; iElem++ ) {
722 #ifdef WITH_SMESH_CANCEL_COMPUTE
723         if(theAlgo->computeCanceled())
724           {
725             return false;
726           }
727 #endif
728         aHexoticID = iElem + 1;
729         for ( int iCoord = 0; iCoord < 3; iCoord++ )
730           fileRes >> coord[ iCoord ];
731         fileRes >> dummy;
732         aHexoticNode = theMesh->AddNode(coord[0], coord[1], coord[2]);
733         HexoticNode[ aHexoticID ] = aHexoticNode;
734         nodeAssigne[ aHexoticID ] = 0;
735       }
736       break;
737     }
738     case 3: // "Corners"
739     case 4: // "Edges"
740     case 5: // "Ridges"
741     case 6: // "Quadrilaterals"
742     case 7: { // "Hexahedra"
743       MESSAGE("Read " << nbElem << " " << token);
744       std::vector< SMDS_MeshNode* > node( nbRef );
745       std::vector< int >          nodeID( nbRef );
746
747       for ( int iElem = 0; iElem < nbElem; iElem++ )
748       {
749 #ifdef WITH_SMESH_CANCEL_COMPUTE
750         if(theAlgo->computeCanceled())
751           {
752             return false;
753           }
754 #endif
755         for ( int iRef = 0; iRef < nbRef; iRef++ )
756         {
757           fileRes >> aHexoticNodeID;                          // read nbRef aHexoticNodeID
758           node  [ iRef ] = HexoticNode[ aHexoticNodeID ];
759           nodeID[ iRef ] = aHexoticNodeID;
760         }
761         if ( hasDummy )
762           fileRes >> dummy;
763         switch (nField)
764         {
765         case 4: // "Edges"
766           theHelper->AddEdge( node[0], node[1] ); break;
767         case 6:  // "Quadrilaterals"
768           theMesh->AddFace( node[0], node[1], node[2], node[3] ); break;
769         case 7: // "Hexahedra"
770           theHelper->AddVolume( node[0], node[3], node[2], node[1],
771                                 node[4], node[7], node[6], node[5] ); break;
772         default: continue;
773         }
774         if ( nField == 6 )
775           for ( int iRef = 0; iRef < nbRef; iRef++ )
776             nodeAssigne[ nodeID[ iRef ]] = 1;
777       }
778       break;
779     }
780     case 8: { // "End"
781       EndOfFile = 1;
782       MESSAGE("End of " << theFile << " file");
783       break;
784     }
785     default: {
786       MESSAGE("Unknown Token: " << token);
787     }
788     }
789   }
790   cout << std::endl;
791
792   shapeID = theHelper->GetSubShapeID();
793   for ( int i = 0; i < nbVertices; ++i )
794     if ( !nodeAssigne[ i+1 ])
795       theMesh->SetNodeInVolume( HexoticNode[ i+1 ], shapeID );
796
797   delete [] HexoticNode;
798   delete [] nodeAssigne;
799   return true;
800 }
801
802 //=============================================================================
803 /*!
804  * Pass parameters to Hexotic
805  */
806 //=============================================================================
807
808 void HexoticPlugin_Hexotic::SetParameters(const HexoticPlugin_Hypothesis* hyp) {
809
810   MESSAGE("HexoticPlugin_Hexotic::SetParameters");
811   if (hyp) {
812     _hexesMinLevel = hyp->GetHexesMinLevel();
813     _hexesMaxLevel = hyp->GetHexesMaxLevel();
814     _hexesMinSize = hyp->GetMinSize();
815     _hexesMaxSize = hyp->GetMaxSize();
816     _hexoticIgnoreRidges = hyp->GetHexoticIgnoreRidges();
817     _hexoticInvalidElements = hyp->GetHexoticInvalidElements();
818     _hexoticSharpAngleThreshold = hyp->GetHexoticSharpAngleThreshold();
819     _hexoticNbProc = hyp->GetHexoticNbProc();
820     _hexoticWorkingDirectory = hyp->GetHexoticWorkingDirectory();
821     _hexoticVerbosity = hyp->GetHexoticVerbosity();
822     _hexoticMaxMemory = hyp->GetHexoticMaxMemory();
823     _hexoticSdMode = hyp->GetHexoticSdMode();
824   }
825   else {
826     cout << std::endl;
827     cout << "WARNING : The Hexotic default parameters are taken into account" << std::endl;
828     cout << "=======" << std::endl;
829     _hexesMinLevel = hyp->GetDefaultHexesMinLevel();
830     _hexesMaxLevel = hyp->GetDefaultHexesMaxLevel();
831     _hexesMinSize = hyp->GetDefaultMinSize();
832     _hexesMaxSize = hyp->GetDefaultMaxSize();
833     _hexoticIgnoreRidges = hyp->GetDefaultHexoticIgnoreRidges();
834     _hexoticInvalidElements = hyp->GetDefaultHexoticInvalidElements();
835     _hexoticSharpAngleThreshold = hyp->GetDefaultHexoticSharpAngleThreshold();
836     _hexoticNbProc = hyp->GetDefaultHexoticNbProc();
837     _hexoticWorkingDirectory = hyp->GetDefaultHexoticWorkingDirectory();
838     _hexoticVerbosity = hyp->GetDefaultHexoticVerbosity();
839     _hexoticMaxMemory = hyp->GetDefaultHexoticMaxMemory();
840     _hexoticSdMode = hyp->GetDefaultHexoticSdMode();
841   }
842 }
843
844 //=======================================================================
845 //function : getTmpDir
846 //purpose  :
847 //=======================================================================
848
849 static TCollection_AsciiString getTmpDir()
850 {
851   TCollection_AsciiString aTmpDir;
852
853   char *Tmp_dir = getenv("SALOME_TMP_DIR");
854 #ifdef WIN32
855   if(Tmp_dir == NULL) {
856     Tmp_dir = getenv("TEMP");
857     if( Tmp_dir== NULL )
858       Tmp_dir = getenv("TMP");
859 #endif
860
861   if(Tmp_dir != NULL) {
862     aTmpDir = Tmp_dir;
863 #ifdef WIN32
864     if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
865 #else
866     if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
867 #endif
868   }
869   else {
870 #ifdef WIN32
871     aTmpDir = TCollection_AsciiString("C:\\");
872 #else
873     aTmpDir = TCollection_AsciiString("/tmp/");
874 #endif
875   }
876   return aTmpDir;
877 }
878
879 //=======================================================================
880 //function : getSuffix
881 //purpose  : Returns a suffix that will be unique for the current process
882 //=======================================================================
883
884 static TCollection_AsciiString getSuffix()
885 {
886   TCollection_AsciiString aSuffix = "";
887   aSuffix += "_";
888   aSuffix += getenv("USER");
889   aSuffix += "_";
890   aSuffix += Kernel_Utils::GetHostname().c_str();
891   aSuffix += "_";
892   aSuffix += getpid();
893
894   return aSuffix;
895 }
896
897 //================================================================================
898 /*!
899  * \brief Returns a command to run Hexotic mesher
900  */
901 //================================================================================
902
903 std::string HexoticPlugin_Hexotic::getHexoticCommand(const TCollection_AsciiString& Hexotic_In,
904                                                      const TCollection_AsciiString& Hexotic_Out) const
905 {
906   cout << std::endl;
907   cout << "Hexotic execution..." << std::endl;
908   cout << _name << " parameters :" << std::endl;
909   cout << "    " << _name << " Verbosity = " << _hexoticVerbosity << std::endl;
910   cout << "    " << _name << " Max Memory = " << _hexoticMaxMemory << std::endl;
911   cout << "    " << _name << " Segments Min Level = " << _hexesMinLevel << std::endl;
912   cout << "    " << _name << " Segments Max Level = " << _hexesMaxLevel << std::endl;
913   cout << "    " << _name << " Segments Min Size = " << _hexesMinSize << std::endl;
914   cout << "    " << _name << " Segments Max Size = " << _hexesMaxSize << std::endl;
915   cout << "    " << "Hexotic can ignore ridges : " << (_hexoticIgnoreRidges ? "yes":"no") << std::endl;
916   cout << "    " << "Hexotic authorize invalide elements : " << ( _hexoticInvalidElements ? "yes":"no") << std::endl;
917   cout << "    " << _name << " Sharp angle threshold = " << _hexoticSharpAngleThreshold << " degrees" << std::endl;
918   cout << "    " << _name << " Number of threads = " << _hexoticNbProc << std::endl;
919   cout << "    " << _name << " Working directory = \"" << _hexoticWorkingDirectory << "\"" << std::endl;
920   cout << "    " << _name << " Sub. Dom mode = " << _hexoticSdMode << std::endl;
921
922   TCollection_AsciiString run_Hexotic( "hexotic" );
923
924   TCollection_AsciiString minl = " -minl ", maxl = " -maxl ", angle = " -ra ";
925   TCollection_AsciiString mins = " -mins ", maxs = " -maxs ";
926   TCollection_AsciiString in   = " -in ",   out  = " -out ";
927   TCollection_AsciiString ignoreRidges = " -nr ", invalideElements = " -inv ";
928   TCollection_AsciiString subdom = " -sd ", sharp = " -sharp ";
929   TCollection_AsciiString proc = " -nproc ";
930   TCollection_AsciiString verb = " -v ";
931   TCollection_AsciiString maxmem = " -m ";
932
933   TCollection_AsciiString minLevel, maxLevel, minSize, maxSize, sharpAngle, mode, nbproc, verbosity, maxMemory;
934   minLevel = _hexesMinLevel;
935   maxLevel = _hexesMaxLevel;
936   minSize = _hexesMinSize;
937   maxSize = _hexesMaxSize;
938   sharpAngle = _hexoticSharpAngleThreshold;
939   mode = _hexoticSdMode;
940   nbproc = _hexoticNbProc;
941   verbosity = _hexoticVerbosity;
942   maxMemory = _hexoticMaxMemory;
943
944   if (_hexoticIgnoreRidges)
945     run_Hexotic +=  ignoreRidges;
946
947   if (_hexoticInvalidElements)
948     run_Hexotic +=  invalideElements;
949
950   if (_hexesMinSize > 0)
951     run_Hexotic +=  mins + minSize;
952
953   if (_hexesMaxSize > 0)
954     run_Hexotic +=  maxs + maxSize;
955
956   if (_hexesMinLevel > 0)
957     run_Hexotic +=  minl + minLevel;
958
959   if (_hexesMaxLevel > 0)
960     run_Hexotic +=  maxl + maxLevel;
961
962   if (_hexoticSharpAngleThreshold > 0)
963     run_Hexotic +=  angle + sharpAngle;
964
965   run_Hexotic += in + Hexotic_In + out + Hexotic_Out;
966   run_Hexotic += subdom + mode;
967   run_Hexotic += proc + nbproc;
968   run_Hexotic += verb + verbosity;
969   run_Hexotic += maxmem + maxMemory;
970
971   return run_Hexotic.ToCString();
972 }
973
974 //=============================================================================
975 /*!
976  * Here we are going to use the Hexotic mesher
977  */
978 //=============================================================================
979
980 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh&          aMesh,
981                                      const TopoDS_Shape& aShape)
982 {
983 #ifdef WITH_SMESH_CANCEL_COMPUTE
984   _compute_canceled = false;
985 #endif
986   bool Ok = true;
987   SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
988   TCollection_AsciiString hexahedraMessage;
989
990   if (_iShape == 0 && _nbShape == 0) {
991     _nbShape = countShape( meshDS, TopAbs_SOLID );  // we count the number of shapes
992   }
993
994   // to prevent from displaying error message after computing,
995   // SetIsAlwaysComputed( true ) to empty sub-meshes
996   vector< SMESH_subMesh* > subMeshesAlwaysComp;
997   for ( int i = 0; i < _nbShape; ++i )
998     if ( SMESH_subMesh* sm = aMesh.GetSubMeshContaining( aShape ))
999     {
1000       SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
1001                                                                /*complexShapeFirst=*/false);
1002       while ( smIt->more() )
1003       {
1004         sm = smIt->next();
1005         if ( !sm->IsMeshComputed() )
1006         {
1007           sm->SetIsAlwaysComputed( true );
1008           subMeshesAlwaysComp.push_back( sm );
1009         }
1010       }
1011     }
1012
1013   _iShape++;
1014
1015   if (_iShape == _nbShape ) {
1016
1017     // create bounding box for each shape of the compound
1018
1019     int iShape = 0;
1020     TopoDS_Shape *tabShape;
1021     double **tabBox;
1022
1023     tabShape = new TopoDS_Shape[_nbShape];
1024     tabBox   = new double*[_nbShape];
1025     for (int i=0; i<_nbShape; i++)
1026       tabBox[i] = new double[6];
1027     double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1028
1029     TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID);
1030     for (; expBox.More(); expBox.Next()) {
1031       tabShape[iShape] = expBox.Current();
1032       Bnd_Box BoundingBox;
1033       BRepBndLib::Add(expBox.Current(), BoundingBox);
1034       BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
1035       tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
1036       tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
1037       tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
1038       iShape++;
1039     }
1040
1041     SetParameters(_hypothesis);
1042
1043 //     TCollection_AsciiString aTmpDir = getTmpDir();
1044     TCollection_AsciiString aTmpDir = TCollection_AsciiString(_hexoticWorkingDirectory.c_str());
1045 #ifdef WIN32
1046     if ( aTmpDir.Value(aTmpDir.Length()) != '\\' ) aTmpDir += '\\';
1047 #else
1048     if ( aTmpDir.Value(aTmpDir.Length()) != '/' ) aTmpDir += '/';
1049 #endif
1050     TCollection_AsciiString Hexotic_In(""), Hexotic_Out;
1051     TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1052     TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic"+getSuffix()+".log";    // log
1053
1054     std::map <int,int> aSmdsToHexoticIdMap;
1055     std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1056
1057     Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
1058 #ifdef WITH_BLSURFPLUGIN
1059     bool defaultInputFile = true;
1060     if (_blsurfHypo && !_blsurfHypo->GetQuadAllowed()) {
1061       Hexotic_In = TCollection_AsciiString(_blsurfHypo->GetGMFFile().c_str());
1062       if (Hexotic_In != "")
1063         defaultInputFile = false;
1064     }
1065     if (defaultInputFile) {
1066 #endif
1067       Hexotic_In  = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
1068       removeHexoticFiles(Hexotic_In, Hexotic_Out);
1069       cout << std::endl;
1070       cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1071       aMesh.ExportGMF(Hexotic_In.ToCString(), meshDS, true);
1072 #ifdef WITH_BLSURFPLUGIN
1073     }
1074     else {
1075       removeFile( Hexotic_Out );
1076     }
1077 #endif
1078     
1079
1080     std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1081     run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString();  // dump into file
1082
1083     cout << std::endl;
1084     cout << "Hexotic command : " << run_Hexotic << std::endl;
1085
1086     modeFile_In += Hexotic_In;
1087     system( modeFile_In.ToCString() );
1088     aSmdsToHexoticIdMap.clear();
1089     aHexoticIdToNodeMap.clear();
1090
1091     MESSAGE("HexoticPlugin_Hexotic::Compute");
1092
1093     int status = system( run_Hexotic.data() );
1094
1095     // --------------
1096     // read a result
1097     // --------------
1098
1099     std::ifstream fileRes( Hexotic_Out.ToCString() );
1100     modeFile_Out += Hexotic_Out;
1101     system( modeFile_Out.ToCString() );
1102     if ( ! fileRes.fail() ) {
1103       Ok = readResult( Hexotic_Out.ToCString(),
1104 #ifdef WITH_SMESH_CANCEL_COMPUTE
1105                        this,
1106 #endif
1107                        meshDS, _nbShape, tabShape, tabBox );
1108       if(Ok) {
1109 /*********************
1110 // TODO: Detect and remove elements in holes in case of sd mode = 4
1111       // Remove previous nodes and elements
1112       SMDS_ElemIteratorPtr itElement = meshDS->elementsIterator();
1113       SMDS_NodeIteratorPtr itNode = meshDS->nodesIterator();
1114     
1115       while ( itElement->more() )
1116         meshDS->RemoveElement( itElement->next() );
1117       while ( itNode->more() )
1118         meshDS->RemoveNode( itNode->next() );
1119   
1120       SMESH_ComputeErrorPtr myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1121       if (myError)
1122 */
1123         hexahedraMessage = "success";
1124         removeFile(Hexotic_Out);
1125         removeFile(Hexotic_In);
1126         removeFile(aLogFileName);
1127       }
1128       else {
1129         hexahedraMessage = "failed";
1130     }
1131     }
1132     else {
1133       hexahedraMessage = "failed";
1134       cout << "Problem with Hexotic output file " << Hexotic_Out.ToCString() << std::endl;
1135       Ok = false;
1136       // analyse log file
1137       SMESH_File logFile( aLogFileName.ToCString() );
1138       if ( !logFile.eof() )
1139       {
1140         char msgLic[] = " Dlim ";
1141         const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1142         if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1143           error("Licence problems.");
1144       }
1145 #ifndef WIN32
1146       if ( status > 0 && WEXITSTATUS(status) == 127 )
1147         error("hexotic: command not found");
1148 #else
1149       int err = errno;
1150       if ( status == 0 && err == ENOENT ) {
1151         error("hexotic: command not found");
1152       }
1153 #endif
1154     }
1155     cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1156     cout << std::endl;
1157
1158     // restore "always computed" flag of sub-meshes (0022127)
1159     for  ( size_t iSM = 0; iSM < subMeshesAlwaysComp.size(); ++iSM )
1160       subMeshesAlwaysComp[ iSM ]->SetIsAlwaysComputed( false );
1161
1162     delete [] tabShape;
1163     for (int i=0; i<_nbShape; i++)
1164       delete [] tabBox[i];
1165     delete [] tabBox;
1166     _nbShape = 0;
1167     _iShape  = 0;
1168   }
1169 #ifdef WITH_SMESH_CANCEL_COMPUTE
1170   if(_compute_canceled)
1171     return error(SMESH_Comment("interruption initiated by user"));
1172 #endif
1173   return Ok;
1174 }
1175
1176 //=============================================================================
1177 /*!
1178  * \brief Computes mesh without geometry
1179  *  \param aMesh - the mesh
1180  *  \param aHelper - helper that must be used for adding elements to \aaMesh
1181  *  \retval bool - is a success
1182  *
1183  * The method is called if ( !aMesh->HasShapeToMesh() )
1184  */
1185 //=============================================================================
1186
1187 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
1188 {
1189 #ifdef WITH_SMESH_CANCEL_COMPUTE
1190   _compute_canceled = false;
1191 #endif
1192 /*
1193   SMESH_ComputeErrorPtr myError = SMESH_ComputeError::New();
1194 */
1195   bool Ok = true;
1196   TCollection_AsciiString hexahedraMessage;
1197
1198   SetParameters(_hypothesis);
1199
1200   TCollection_AsciiString aTmpDir = getTmpDir();
1201   TCollection_AsciiString Hexotic_In, Hexotic_Out;
1202   TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1203   TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic"+getSuffix()+".log";    // log
1204
1205   std::map <int,int> aSmdsToHexoticIdMap;
1206   std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1207
1208   Hexotic_In  = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
1209   Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
1210
1211   std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1212   run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString();  // dump into file
1213
1214   removeHexoticFiles(Hexotic_In, Hexotic_Out);
1215
1216   cout << std::endl;
1217   cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1218   aMesh.ExportGMF(Hexotic_In.ToCString(), aHelper->GetMeshDS());
1219   modeFile_In += Hexotic_In;
1220   system( modeFile_In.ToCString() );
1221   aSmdsToHexoticIdMap.clear();
1222   aHexoticIdToNodeMap.clear();
1223
1224   MESSAGE("HexoticPlugin_Hexotic::Compute");
1225
1226   cout << std::endl;
1227   cout << "Hexotic command : " << run_Hexotic << std::endl;
1228   system( run_Hexotic.data() );
1229
1230   // --------------
1231   // read a result
1232   // --------------
1233
1234   std::ifstream fileRes( Hexotic_Out.ToCString() );
1235   modeFile_Out += Hexotic_Out;
1236   system( modeFile_Out.ToCString() );
1237   if ( ! fileRes.fail() ) {
1238     Ok = readResult( Hexotic_Out.ToCString(),
1239 #ifdef WITH_SMESH_CANCEL_COMPUTE
1240                      this,
1241 #endif
1242                      aHelper );
1243     if(Ok)
1244 /*
1245     // Remove previous nodes and elements
1246     SMDS_ElemIteratorPtr itElement = aHelper->GetMeshDS()->elementsIterator();
1247     SMDS_NodeIteratorPtr itNode = aHelper->GetMeshDS()->nodesIterator();
1248     
1249     while ( itElement->more() )
1250       aHelper->GetMeshDS()->RemoveElement( itElement->next() );
1251     while ( itNode->more() )
1252       aHelper->GetMeshDS()->RemoveNode( itNode->next() );
1253
1254     // Import GMF mesh
1255     myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1256     
1257     itElement = aHelper->GetMeshDS()->elementsIterator();
1258     itNode = aHelper->GetMeshDS()->nodesIterator();
1259
1260     // Assign nodes and elements to the pseudo shape
1261     while ( itNode->more() )
1262       aHelper->GetMeshDS()->SetNodeInVolume(itNode->next(), 1);
1263     while ( itElement->more() )
1264       aHelper->GetMeshDS()->SetMeshElementOnShape(itElement->next(), 1);
1265
1266     if(myError->IsOK())
1267 */
1268       hexahedraMessage = "success";
1269     else
1270       hexahedraMessage = "failed";
1271   }
1272   else {
1273 /*
1274     myError->myName = COMPERR_EXCEPTION;
1275 */
1276     hexahedraMessage = "failed";
1277     cout << "Problem with Hexotic output file " << Hexotic_Out << std::endl;
1278     // analyse log file
1279     SMESH_File logFile( aLogFileName.ToCString() );
1280     if ( !logFile.eof() )
1281     {
1282       char msgLic[] = " Dlim ";
1283       const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1284       if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1285         return error("Licence problems.");
1286     }
1287     return error(SMESH_Comment("Problem with Hexotic output file ")<<Hexotic_Out);
1288   }
1289   cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1290   cout << std::endl;
1291
1292 #ifdef WITH_SMESH_CANCEL_COMPUTE
1293   if(_compute_canceled)
1294     return error(SMESH_Comment("interruption initiated by user"));
1295 #endif
1296   removeFile(Hexotic_Out);
1297   removeFile(Hexotic_In);
1298   removeFile(aLogFileName);
1299   return Ok;
1300 /*
1301   return myError->IsOK();
1302 */
1303 }
1304
1305 //=============================================================================
1306 /*!
1307  *
1308  */
1309 //=============================================================================
1310
1311 bool HexoticPlugin_Hexotic::Evaluate(SMESH_Mesh& aMesh,
1312                                      const TopoDS_Shape& aShape,
1313                                      MapShapeNbElems& aResMap)
1314 {
1315   std::vector<int> aResVec(SMDSEntity_Last);
1316   for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1317   SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1318   aResMap.insert(std::make_pair(sm,aResVec));
1319
1320   SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1321   smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Evaluation is not implemented",this));
1322
1323   return true;
1324 }
1325
1326 #ifdef WITH_SMESH_CANCEL_COMPUTE
1327 void HexoticPlugin_Hexotic::CancelCompute()
1328 {
1329   _compute_canceled = true;
1330 #ifdef WNT
1331 #else
1332   TCollection_AsciiString aTmpDir = getTmpDir();
1333   TCollection_AsciiString Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1334   TCollection_AsciiString cmd = TCollection_AsciiString("ps ux | grep ") + Hexotic_In;
1335   cmd += TCollection_AsciiString(" | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1");
1336   system( cmd.ToCString() );
1337 #endif
1338 }
1339 #endif