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