Salome HOME
Merge from V6_main 13/12/2012
[plugins/hexoticplugin.git] / src / HexoticPlugin / HexoticPlugin_Hexotic.cxx
1 // Copyright (C) 2007-2012  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 static void removeFile( const TCollection_AsciiString& fileName )
68 {
69   try {
70     OSD_File( fileName ).Remove();
71   }
72   catch ( Standard_ProgramError ) {
73     MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
74   }
75 }
76
77 //=============================================================================
78 /*!
79  *  
80  */
81 //=============================================================================
82
83 HexoticPlugin_Hexotic::HexoticPlugin_Hexotic(int hypId, int studyId, SMESH_Gen* gen)
84   : SMESH_3D_Algo(hypId, studyId, gen)
85 {
86   MESSAGE("HexoticPlugin_Hexotic::HexoticPlugin_Hexotic");
87   _name = "Hexotic_3D";
88   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
89 //   _onlyUnaryInput = false;
90   _requireShape = false;
91   _iShape=0;
92   _nbShape=0;
93   _hexoticFilesKept=false;
94   _compatibleHypothesis.push_back("Hexotic_Parameters");
95 #ifdef WITH_BLSURFPLUGIN
96   _blsurfHypo = NULL;
97 #endif
98 #ifdef WITH_SMESH_CANCEL_COMPUTE
99   _compute_canceled = false;
100 #endif
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   if(Tmp_dir != NULL) {
853     aTmpDir = Tmp_dir;
854     #ifdef WIN32
855     if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
856 #else
857     if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
858 #endif
859   }
860   else {
861 #ifdef WIN32
862     aTmpDir = TCollection_AsciiString("C:\\");
863 #else
864     aTmpDir = TCollection_AsciiString("/tmp/");
865 #endif
866   }
867   return aTmpDir;
868 }
869
870 //================================================================================
871 /*!
872  * \brief Returns a command to run Hexotic mesher
873  */
874 //================================================================================
875
876 std::string HexoticPlugin_Hexotic::getHexoticCommand(const TCollection_AsciiString& Hexotic_In,
877                                                      const TCollection_AsciiString& Hexotic_Out) const
878 {
879   cout << std::endl;
880   cout << "Hexotic execution..." << std::endl;
881   cout << _name << " parameters :" << std::endl;
882   cout << "    " << _name << " Verbosity = " << _hexoticVerbosity << std::endl;
883   cout << "    " << _name << " Max Memory = " << _hexoticMaxMemory << std::endl;
884   cout << "    " << _name << " Segments Min Level = " << _hexesMinLevel << std::endl;
885   cout << "    " << _name << " Segments Max Level = " << _hexesMaxLevel << std::endl;
886   cout << "    " << _name << " Segments Min Size = " << _hexesMinSize << std::endl;
887   cout << "    " << _name << " Segments Max Size = " << _hexesMaxSize << std::endl;
888   cout << "    " << "Hexotic can ignore ridges : " << (_hexoticIgnoreRidges ? "yes":"no") << std::endl;
889   cout << "    " << "Hexotic authorize invalide elements : " << ( _hexoticInvalidElements ? "yes":"no") << std::endl;
890   cout << "    " << _name << " Sharp angle threshold = " << _hexoticSharpAngleThreshold << " degrees" << std::endl;
891   cout << "    " << _name << " Number of threads = " << _hexoticNbProc << std::endl;
892   cout << "    " << _name << " Working directory = \"" << _hexoticWorkingDirectory << "\"" << std::endl;
893   cout << "    " << _name << " Sub. Dom mode = " << _hexoticSdMode << std::endl;
894
895   TCollection_AsciiString run_Hexotic( "hexotic" );
896
897   TCollection_AsciiString minl = " -minl ", maxl = " -maxl ", angle = " -ra ";
898   TCollection_AsciiString mins = " -mins ", maxs = " -maxs ";
899   TCollection_AsciiString in   = " -in ",   out  = " -out ";
900   TCollection_AsciiString ignoreRidges = " -nr ", invalideElements = " -inv ";
901   TCollection_AsciiString subdom = " -sd ", sharp = " -sharp ";
902   TCollection_AsciiString proc = " -nproc ";
903   TCollection_AsciiString verb = " -v ";
904   TCollection_AsciiString maxmem = " -m ";
905
906   TCollection_AsciiString minLevel, maxLevel, minSize, maxSize, sharpAngle, mode, nbproc, verbosity, maxMemory;
907   minLevel = _hexesMinLevel;
908   maxLevel = _hexesMaxLevel;
909   minSize = _hexesMinSize;
910   maxSize = _hexesMaxSize;
911   sharpAngle = _hexoticSharpAngleThreshold;
912   mode = _hexoticSdMode;
913   nbproc = _hexoticNbProc;
914   verbosity = _hexoticVerbosity;
915   maxMemory = _hexoticMaxMemory;
916
917   if (_hexoticIgnoreRidges)
918     run_Hexotic +=  ignoreRidges;
919
920   if (_hexoticInvalidElements)
921     run_Hexotic +=  invalideElements;
922
923   if (_hexesMinSize > 0)
924     run_Hexotic +=  mins + minSize;
925
926   if (_hexesMaxSize > 0)
927     run_Hexotic +=  maxs + maxSize;
928
929   if (_hexesMinLevel > 0)
930     run_Hexotic +=  minl + minLevel;
931
932   if (_hexesMaxLevel > 0)
933     run_Hexotic +=  maxl + maxLevel;
934
935   if (_hexoticSharpAngleThreshold > 0)
936     run_Hexotic +=  angle + sharpAngle;
937
938   run_Hexotic += in + Hexotic_In + out + Hexotic_Out;
939   run_Hexotic += subdom + mode;
940   run_Hexotic += proc + nbproc;
941   run_Hexotic += verb + verbosity;
942   run_Hexotic += maxmem + maxMemory;
943
944   return run_Hexotic.ToCString();
945 }
946
947 //=============================================================================
948 /*!
949  * Here we are going to use the Hexotic mesher
950  */
951 //=============================================================================
952
953 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh&          aMesh,
954                                      const TopoDS_Shape& aShape)
955 {
956 #ifdef WITH_SMESH_CANCEL_COMPUTE
957   _compute_canceled = false;
958 #endif
959   bool Ok = true;
960   SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
961   TCollection_AsciiString hexahedraMessage;
962
963   if (_iShape == 0 && _nbShape == 0) {
964     _nbShape = countShape( meshDS, TopAbs_SOLID );  // we count the number of shapes
965     //_tabNode = new SMDS_MeshNode*[_nbShape];        // we declare the size of the node array
966   }
967
968   // to prevent from displaying error message after computing,
969   // SetIsAlwaysComputed( true ) to empty sub-meshes
970   for ( int i = 0; i < _nbShape; ++i )
971     if ( SMESH_subMesh* sm = aMesh.GetSubMeshContaining( aShape ))
972     {
973       SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
974                                                                /*complexShapeFirst=*/false);
975       while ( smIt->more() )
976       {
977         sm = smIt->next();
978         if ( !sm->IsMeshComputed() )
979           sm->SetIsAlwaysComputed( true );
980       }
981     }
982
983   _iShape++;
984
985   if (_iShape == _nbShape ) {
986
987     // for (int i=0; i<_nbShape; i++)        // we destroy the (_nbShape - 1) nodes created and used
988     //   meshDS->RemoveNode( _tabNode[i] );  // to simulate successful mesh computing.
989     // delete [] _tabNode;
990
991     // create bounding box for each shape of the compound
992
993     int iShape = 0;
994     TopoDS_Shape *tabShape;
995     double **tabBox;
996
997     tabShape = new TopoDS_Shape[_nbShape];
998     tabBox   = new double*[_nbShape];
999     for (int i=0; i<_nbShape; i++)
1000       tabBox[i] = new double[6];
1001     double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1002
1003     TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID);
1004     for (; expBox.More(); expBox.Next()) {
1005       tabShape[iShape] = expBox.Current();
1006       Bnd_Box BoundingBox;
1007       BRepBndLib::Add(expBox.Current(), BoundingBox);
1008       BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
1009       tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
1010       tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
1011       tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
1012       iShape++;
1013     }
1014
1015     SetParameters(_hypothesis);
1016
1017 //     TCollection_AsciiString aTmpDir = getTmpDir();
1018     TCollection_AsciiString aTmpDir = TCollection_AsciiString(_hexoticWorkingDirectory.c_str());
1019 #ifdef WIN32
1020     if ( aTmpDir.Value(aTmpDir.Length()) != '\\' ) aTmpDir += '\\';
1021 #else
1022     if ( aTmpDir.Value(aTmpDir.Length()) != '/' ) aTmpDir += '/';
1023 #endif
1024     TCollection_AsciiString Hexotic_In(""), Hexotic_Out;
1025     TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1026     TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic.log";    // log
1027
1028     std::map <int,int> aSmdsToHexoticIdMap;
1029     std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1030
1031     Hexotic_Out = aTmpDir + "Hexotic_Out.mesh";
1032 #ifdef WITH_BLSURFPLUGIN
1033     bool defaultInputFile = true;
1034     if (_blsurfHypo && !_blsurfHypo->GetQuadAllowed()) {
1035       Hexotic_In = TCollection_AsciiString(_blsurfHypo->GetGMFFile().c_str());
1036       if (Hexotic_In != "")
1037         defaultInputFile = false;
1038     }
1039     if (defaultInputFile) {
1040 #endif
1041       Hexotic_In  = aTmpDir + "Hexotic_In.mesh";
1042       removeHexoticFiles(Hexotic_In, Hexotic_Out);
1043       cout << std::endl;
1044       cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1045       aMesh.ExportGMF(Hexotic_In.ToCString(), meshDS, true);
1046 #ifdef WITH_BLSURFPLUGIN
1047     }
1048     else {
1049       removeFile( Hexotic_Out );
1050     }
1051 #endif
1052     
1053
1054     std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1055     run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString();  // dump into file
1056
1057     cout << std::endl;
1058     cout << "Hexotic command : " << run_Hexotic << std::endl;
1059
1060     modeFile_In += Hexotic_In;
1061     system( modeFile_In.ToCString() );
1062     aSmdsToHexoticIdMap.clear();
1063     aHexoticIdToNodeMap.clear();
1064
1065     MESSAGE("HexoticPlugin_Hexotic::Compute");
1066
1067     system( run_Hexotic.data() );
1068
1069     // --------------
1070     // read a result
1071     // --------------
1072
1073     std::ifstream fileRes( Hexotic_Out.ToCString() );
1074     modeFile_Out += Hexotic_Out;
1075     system( modeFile_Out.ToCString() );
1076     if ( ! fileRes.fail() ) {
1077       Ok = readResult( Hexotic_Out.ToCString(),
1078 #ifdef WITH_SMESH_CANCEL_COMPUTE
1079                        this,
1080 #endif
1081                        meshDS, _nbShape, tabShape, tabBox );
1082       if(Ok)
1083 /*********************
1084 // TODO: Detect and remove elements in holes in case of sd mode = 4
1085       // Remove previous nodes and elements
1086       SMDS_ElemIteratorPtr itElement = meshDS->elementsIterator();
1087       SMDS_NodeIteratorPtr itNode = meshDS->nodesIterator();
1088     
1089       while ( itElement->more() )
1090         meshDS->RemoveElement( itElement->next() );
1091       while ( itNode->more() )
1092         meshDS->RemoveNode( itNode->next() );
1093   
1094       SMESH_ComputeErrorPtr myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1095       if (myError)
1096 */
1097         hexahedraMessage = "success";
1098       else
1099         hexahedraMessage = "failed";
1100     }
1101     else {
1102       hexahedraMessage = "failed";
1103       cout << "Problem with Hexotic output file " << Hexotic_Out.ToCString() << std::endl;
1104       Ok = false;
1105       // analyse log file
1106       SMESH_File logFile( aLogFileName.ToCString() );
1107       if ( !logFile.eof() )
1108       {
1109         char msgLic[] = " Dlim ";
1110         const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1111         if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1112           error("Licence problems.");
1113       }
1114     }
1115     cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1116     cout << std::endl;
1117
1118     delete [] tabShape;
1119     for (int i=0; i<_nbShape; i++)
1120       delete [] tabBox[i];
1121     delete [] tabBox;
1122     _nbShape = 0;
1123     _iShape  = 0;
1124   }
1125 #ifdef WITH_SMESH_CANCEL_COMPUTE
1126   if(_compute_canceled)
1127     return error(SMESH_Comment("interruption initiated by user"));
1128 #endif
1129   return Ok;
1130 }
1131
1132 //=============================================================================
1133 /*!
1134  * \brief Computes mesh without geometry
1135  *  \param aMesh - the mesh
1136  *  \param aHelper - helper that must be used for adding elements to \aaMesh
1137  *  \retval bool - is a success
1138  *
1139  * The method is called if ( !aMesh->HasShapeToMesh() )
1140  */
1141 //=============================================================================
1142
1143 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
1144 {
1145 #ifdef WITH_SMESH_CANCEL_COMPUTE
1146   _compute_canceled = false;
1147 #endif
1148 /*
1149   SMESH_ComputeErrorPtr myError = SMESH_ComputeError::New();
1150 */
1151   bool Ok = true;
1152   TCollection_AsciiString hexahedraMessage;
1153
1154   SetParameters(_hypothesis);
1155
1156   TCollection_AsciiString aTmpDir = getTmpDir();
1157   TCollection_AsciiString Hexotic_In, Hexotic_Out;
1158   TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1159   TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic.log";    // log
1160
1161   std::map <int,int> aSmdsToHexoticIdMap;
1162   std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1163
1164   Hexotic_In  = aTmpDir + "Hexotic_In.mesh";
1165   Hexotic_Out = aTmpDir + "Hexotic_Out.mesh";
1166
1167   std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1168   run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString();  // dump into file
1169
1170   removeHexoticFiles(Hexotic_In, Hexotic_Out);
1171
1172   cout << std::endl;
1173   cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1174   aMesh.ExportGMF(Hexotic_In.ToCString(), aHelper->GetMeshDS());
1175   modeFile_In += Hexotic_In;
1176   system( modeFile_In.ToCString() );
1177   aSmdsToHexoticIdMap.clear();
1178   aHexoticIdToNodeMap.clear();
1179
1180   MESSAGE("HexoticPlugin_Hexotic::Compute");
1181
1182   cout << std::endl;
1183   cout << "Hexotic command : " << run_Hexotic << std::endl;
1184   system( run_Hexotic.data() );
1185
1186   // --------------
1187   // read a result
1188   // --------------
1189
1190   std::ifstream fileRes( Hexotic_Out.ToCString() );
1191   modeFile_Out += Hexotic_Out;
1192   system( modeFile_Out.ToCString() );
1193   if ( ! fileRes.fail() ) {
1194     Ok = readResult( Hexotic_Out.ToCString(),
1195 #ifdef WITH_SMESH_CANCEL_COMPUTE
1196                      this,
1197 #endif
1198                      aHelper );
1199     if(Ok)
1200 /*
1201     // Remove previous nodes and elements
1202     SMDS_ElemIteratorPtr itElement = aHelper->GetMeshDS()->elementsIterator();
1203     SMDS_NodeIteratorPtr itNode = aHelper->GetMeshDS()->nodesIterator();
1204     
1205     while ( itElement->more() )
1206       aHelper->GetMeshDS()->RemoveElement( itElement->next() );
1207     while ( itNode->more() )
1208       aHelper->GetMeshDS()->RemoveNode( itNode->next() );
1209
1210     // Import GMF mesh
1211     myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1212     
1213     itElement = aHelper->GetMeshDS()->elementsIterator();
1214     itNode = aHelper->GetMeshDS()->nodesIterator();
1215
1216     // Assign nodes and elements to the pseudo shape
1217     while ( itNode->more() )
1218       aHelper->GetMeshDS()->SetNodeInVolume(itNode->next(), 1);
1219     while ( itElement->more() )
1220       aHelper->GetMeshDS()->SetMeshElementOnShape(itElement->next(), 1);
1221
1222     if(myError->IsOK())
1223 */
1224       hexahedraMessage = "success";
1225     else
1226       hexahedraMessage = "failed";
1227   }
1228   else {
1229 /*
1230     myError->myName = COMPERR_EXCEPTION;
1231 */
1232     hexahedraMessage = "failed";
1233     cout << "Problem with Hexotic output file " << Hexotic_Out << std::endl;
1234     // analyse log file
1235     SMESH_File logFile( aLogFileName.ToCString() );
1236     if ( !logFile.eof() )
1237     {
1238       char msgLic[] = " Dlim ";
1239       const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1240       if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1241         return error("Licence problems.");
1242     }
1243     return error(SMESH_Comment("Problem with Hexotic output file ")<<Hexotic_Out);
1244   }
1245   cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1246   cout << std::endl;
1247
1248 #ifdef WITH_SMESH_CANCEL_COMPUTE
1249   if(_compute_canceled)
1250     return error(SMESH_Comment("interruption initiated by user"));
1251 #endif
1252   return Ok;
1253 /*
1254   return myError->IsOK();
1255 */
1256 }
1257
1258 //=============================================================================
1259 /*!
1260  *
1261  */
1262 //=============================================================================
1263
1264 bool HexoticPlugin_Hexotic::Evaluate(SMESH_Mesh& aMesh,
1265                                      const TopoDS_Shape& aShape,
1266                                      MapShapeNbElems& aResMap)
1267 {
1268   std::vector<int> aResVec(SMDSEntity_Last);
1269   for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1270   SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1271   aResMap.insert(std::make_pair(sm,aResVec));
1272
1273   SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1274   smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Evaluation is not implemented",this));
1275
1276   return true;
1277 }
1278
1279 #ifdef WITH_SMESH_CANCEL_COMPUTE
1280 void HexoticPlugin_Hexotic::CancelCompute()
1281 {
1282   _compute_canceled = true;
1283 #ifdef WNT
1284 #else
1285   TCollection_AsciiString aTmpDir = getTmpDir();
1286   TCollection_AsciiString Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1287   TCollection_AsciiString cmd = TCollection_AsciiString("ps ux | grep ") + Hexotic_In;
1288   cmd += TCollection_AsciiString(" | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1");
1289   system( cmd.ToCString() );
1290 #endif
1291 }
1292 #endif