1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // File : HexoticPlugin_Hexotic.cxx
22 // Author : Lioka RAZAFINDRAZAKA (CEA)
25 #include "HexoticPlugin_Hexotic.hxx"
26 #include "HexoticPlugin_Hypothesis.hxx"
28 #include "utilities.h"
31 #include <sys/sysinfo.h>
43 #include <SMESHDS_Mesh.hxx>
44 #include <SMESHDS_GroupBase.hxx>
45 #include <SMESH_ComputeError.hxx>
46 #include <SMESH_File.hxx>
47 #include <SMESH_Gen.hxx>
48 #include <SMESH_HypoFilter.hxx>
49 #include <SMESH_MesherHelper.hxx>
50 #include <SMESH_subMesh.hxx>
56 #include <Standard_ProgramError.hxx>
58 #include <BRep_Tool.hxx>
59 #include <BRepBndLib.hxx>
60 #include <BRepClass3d_SolidClassifier.hxx>
61 #include <BRepBuilderAPI_MakeVertex.hxx>
62 #include <BRepExtrema_DistShapeShape.hxx>
63 #include <OSD_File.hxx>
64 #include <Precision.hxx>
65 #include <TopExp_Explorer.hxx>
66 #include <TopTools_MapOfShape.hxx>
69 #include <Basics_Utils.hxx>
71 static void removeFile( const TCollection_AsciiString& fileName )
74 OSD_File( fileName ).Remove();
76 catch ( Standard_ProgramError ) {
77 MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
81 //=============================================================================
85 //=============================================================================
87 HexoticPlugin_Hexotic::HexoticPlugin_Hexotic(int hypId, int studyId, SMESH_Gen* gen)
88 : SMESH_3D_Algo(hypId, studyId, gen)
90 MESSAGE("HexoticPlugin_Hexotic::HexoticPlugin_Hexotic");
92 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
93 // _onlyUnaryInput = false;
94 _requireShape = false;
97 _hexoticFilesKept=false;
98 _compatibleHypothesis.push_back("Hexotic_Parameters");
99 #ifdef WITH_BLSURFPLUGIN
102 _compute_canceled = false;
105 //=============================================================================
109 //=============================================================================
111 HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic()
113 MESSAGE("HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic");
117 #ifdef WITH_BLSURFPLUGIN
118 bool HexoticPlugin_Hexotic::CheckBLSURFHypothesis( SMESH_Mesh& aMesh,
119 const TopoDS_Shape& aShape )
121 // MESSAGE("HexoticPlugin_Hexotic::CheckBLSURFHypothesis");
124 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
125 const SMESHDS_Hypothesis* theHyp;
127 // If a BLSURF hypothesis is applied, get it
128 SMESH_HypoFilter blsurfFilter;
129 blsurfFilter.Init( blsurfFilter.HasName( "BLSURF_Parameters" ));
130 std::list<const SMESHDS_Hypothesis *> appliedHyps;
131 aMesh.GetHypotheses( aShape, blsurfFilter, appliedHyps, false );
133 if ( appliedHyps.size() > 0 ) {
134 itl = appliedHyps.begin();
135 theHyp = (*itl); // use only the first hypothesis
136 std::string hypName = theHyp->GetName();
137 if (hypName == "BLSURF_Parameters") {
138 _blsurfHypo = static_cast<const BLSURFPlugin_Hypothesis*> (theHyp);
147 //=============================================================================
151 //=============================================================================
153 bool HexoticPlugin_Hexotic::CheckHypothesis( SMESH_Mesh& aMesh,
154 const TopoDS_Shape& aShape,
155 SMESH_Hypothesis::Hypothesis_Status& aStatus )
157 // MESSAGE("HexoticPlugin_Hexotic::CheckHypothesis");
160 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
161 const SMESHDS_Hypothesis* theHyp;
163 const std::list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
164 int nbHyp = hyps.size();
166 aStatus = SMESH_Hypothesis::HYP_OK;
167 return true; // can work with no hypothesis
171 theHyp = (*itl); // use only the first hypothesis
173 std::string hypName = theHyp->GetName();
174 if (hypName == "Hexotic_Parameters") {
175 _hypothesis = static_cast<const HexoticPlugin_Hypothesis*> (theHyp);
177 aStatus = SMESH_Hypothesis::HYP_OK;
180 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
182 #ifdef WITH_BLSURFPLUGIN
183 CheckBLSURFHypothesis(aMesh, aShape);
186 return aStatus == SMESH_Hypothesis::HYP_OK;
189 //=======================================================================
190 //function : findShape
192 //=======================================================================
194 static TopoDS_Shape findShape(SMDS_MeshNode** t_Node,
196 const TopoDS_Shape* t_Shape,
201 int iShape, nbNode = 8;
203 for ( int i=0; i<3; i++ ) {
205 for ( int j=0; j<nbNode; j++ ) {
206 if ( i == 0) pntCoor[i] += t_Node[j]->X();
207 if ( i == 1) pntCoor[i] += t_Node[j]->Y();
208 if ( i == 2) pntCoor[i] += t_Node[j]->Z();
210 pntCoor[i] /= nbNode;
212 gp_Pnt aPnt(pntCoor[0], pntCoor[1], pntCoor[2]);
214 if ( aShape.IsNull() ) aShape = t_Shape[0];
215 BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
216 if ( !(SC.State() == TopAbs_IN) ) {
218 for (iShape = 0; iShape < nShape && aShape.IsNull(); iShape++) {
219 if ( !( pntCoor[0] < t_Box[iShape][0] || t_Box[iShape][1] < pntCoor[0] ||
220 pntCoor[1] < t_Box[iShape][2] || t_Box[iShape][3] < pntCoor[1] ||
221 pntCoor[2] < t_Box[iShape][4] || t_Box[iShape][5] < pntCoor[2]) ) {
222 BRepClass3d_SolidClassifier SC (t_Shape[iShape], aPnt, Precision::Confusion());
223 if (SC.State() == TopAbs_IN)
224 aShape = t_Shape[iShape];
231 //=======================================================================
232 //function : findEdge
234 //=======================================================================
236 static int findEdge(const SMDS_MeshNode* aNode,
237 const SMESHDS_Mesh* aMesh,
239 const TopoDS_Shape* t_Edge) {
241 TopoDS_Shape aPntShape, foundEdge;
242 TopoDS_Vertex aVertex;
243 gp_Pnt aPnt( aNode->X(), aNode->Y(), aNode->Z() );
246 double nearest = RealLast(), *t_Dist;
247 double epsilon = Precision::Confusion();
249 t_Dist = new double[ nEdge ];
250 aPntShape = BRepBuilderAPI_MakeVertex( aPnt ).Shape();
251 aVertex = TopoDS::Vertex( aPntShape );
253 for ( ind=0; ind < nEdge; ind++ ) {
254 BRepExtrema_DistShapeShape aDistance ( aVertex, t_Edge[ind] );
255 t_Dist[ind] = aDistance.Value();
256 if ( t_Dist[ind] < nearest ) {
257 nearest = t_Dist[ind];
258 foundEdge = t_Edge[ind];
260 if ( nearest < epsilon )
266 return aMesh->ShapeToIndex( foundEdge );
269 //=======================================================================
270 //function : getNbShape
272 //=======================================================================
274 static int getNbShape(std::string aFile, std::string aString, int defaultValue=0) {
275 int number = defaultValue;
277 std::ifstream file(aFile.c_str());
278 while ( !file.eof() ) {
279 getline( file, aLine);
280 if ( aLine == aString ) {
281 getline( file, aLine);
282 std::istringstream stringFlux( aLine );
283 stringFlux >> number;
284 number = ( number + defaultValue + std::abs(number - defaultValue) ) / 2;
292 //=======================================================================
293 //function : countShape
295 //=======================================================================
297 template < class Mesh, class Shape >
298 static int countShape( Mesh* mesh, Shape shape ) {
299 TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
300 TopTools_MapOfShape mapShape;
302 for ( ; expShape.More(); expShape.Next() ) {
303 if (mapShape.Add(expShape.Current())) {
310 //=======================================================================
311 //function : getShape
313 //=======================================================================
315 template < class Mesh, class Shape, class Tab >
316 void getShape(Mesh* mesh, Shape shape, Tab *t_Shape) {
317 TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
318 TopTools_MapOfShape mapShape;
319 for ( int i=0; expShape.More(); expShape.Next() ) {
320 if (mapShape.Add(expShape.Current())) {
321 t_Shape[i] = expShape.Current();
328 //=======================================================================
329 //function : printWarning
331 //=======================================================================
333 static void printWarning(const int nbExpected, std::string aString, const int nbFound) {
335 cout << "WARNING : " << nbExpected << " " << aString << " expected, Hexotic has found " << nbFound << std::endl;
336 cout << "=======" << std::endl;
341 //=======================================================================
342 //function : removeHexoticFiles
344 //=======================================================================
346 static void removeHexoticFiles(TCollection_AsciiString file_In, TCollection_AsciiString file_Out) {
347 removeFile( file_In );
348 removeFile( file_Out );
351 //=======================================================================
352 //function : readResult
353 //purpose : Read GMF file in case of a mesh with geometry
354 //=======================================================================
356 static bool readResult(std::string theFile,
357 #ifdef WITH_SMESH_CANCEL_COMPUTE
358 HexoticPlugin_Hexotic* theAlgo,
360 SMESHDS_Mesh* theMesh,
362 const TopoDS_Shape* tabShape,
365 // ---------------------------------
366 // Optimisation of the plugin ...
367 // Retrieve the correspondance edge --> shape
368 // (which is very costly) only when user
369 // has defined at least one group of edges
370 // which should be rare for a 3d mesh !
371 // ---------------------------------
373 bool retrieve_edges = false;
374 const std::set<SMESHDS_GroupBase*>& aGroups = theMesh->GetGroups();
375 set<SMESHDS_GroupBase*>::const_iterator GrIt = aGroups.begin();
376 for (; GrIt != aGroups.end(); GrIt++)
378 SMESHDS_GroupBase* aGrp = *GrIt;
381 if ( aGrp->GetType() == SMDSAbs_Edge )
383 retrieve_edges = true;
388 // ---------------------------------
389 // Read generated elements and nodes
390 // ---------------------------------
393 TopoDS_Vertex aVertex;
395 int EndOfFile = 0, nbElem = 0, nField = 9, nbRef = 0;
396 int aHexoticNodeID = 0, shapeID, hexoticShapeID;
397 const int IdShapeRef = 2;
398 int *tabID, *tabRef, *nodeAssigne;
399 bool *tabDummy, hasDummy = false;
400 double epsilon = Precision::Confusion();
401 std::map <std::string,int> mapField;
402 SMDS_MeshNode** HexoticNode;
403 TopoDS_Shape *tabCorner, *tabEdge;
405 const int nbDomains = countShape( theMesh, TopAbs_SHELL );
406 const int holeID = -1;
408 // tabID = new int[nbShape];
409 tabID = new int[nbDomains];
410 tabRef = new int[nField];
411 tabDummy = new bool[nField];
413 for (int i=0; i<nbDomains; i++)
415 if ( nbDomains == 1 )
416 tabID[0] = theMesh->ShapeToIndex( tabShape[0] );
418 mapField["MeshVersionFormatted"] = 0; tabRef[0] = 0; tabDummy[0] = false;
419 mapField["Dimension"] = 1; tabRef[1] = 0; tabDummy[1] = false;
420 mapField["Vertices"] = 2; tabRef[2] = 3; tabDummy[2] = true;
421 mapField["Corners"] = 3; tabRef[3] = 1; tabDummy[3] = false;
422 mapField["Edges"] = 4; tabRef[4] = 2; tabDummy[4] = true;
423 mapField["Ridges"] = 5; tabRef[5] = 1; tabDummy[5] = false;
424 mapField["Quadrilaterals"] = 6; tabRef[6] = 4; tabDummy[6] = true;
425 mapField["Hexahedra"] = 7; tabRef[7] = 8; tabDummy[7] = true;
426 mapField["End"] = 8; tabRef[8] = 0; tabDummy[0] = false;
428 SMDS_NodeIteratorPtr itOnHexoticInputNode = theMesh->nodesIterator();
429 while ( itOnHexoticInputNode->more() )
430 theMesh->RemoveNode( itOnHexoticInputNode->next() );
432 int nbVertices = getNbShape(theFile, "Vertices");
433 int nbCorners = getNbShape(theFile, "Corners", countShape( theMesh, TopAbs_VERTEX ));
434 int nbShapeEdge = countShape( theMesh, TopAbs_EDGE );
436 tabCorner = new TopoDS_Shape[ nbCorners ];
437 tabEdge = new TopoDS_Shape[ nbShapeEdge ];
438 nodeAssigne = new int[ nbVertices + 1 ];
439 HexoticNode = new SMDS_MeshNode*[ nbVertices + 1 ];
441 getShape(theMesh, TopAbs_VERTEX, tabCorner);
442 getShape(theMesh, TopAbs_EDGE, tabEdge);
444 MESSAGE("Read " << theFile << " file");
445 std::ifstream fileRes(theFile.c_str());
448 while ( EndOfFile == 0 ) {
452 if (mapField.count(token)) {
453 nField = mapField[token];
454 nbRef = tabRef[nField];
455 hasDummy = tabDummy[nField];
463 if ( nField < (mapField.size() - 1) && nField >= 0 )
467 case 0: { // "MeshVersionFormatted"
468 MESSAGE(token << " " << nbElem);
471 case 1: { // "Dimension"
472 MESSAGE("Mesh dimension " << nbElem << "D");
475 case 2: { // "Vertices"
476 MESSAGE("Read " << nbElem << " " << token);
479 SMDS_MeshNode * aHexoticNode;
481 coord = new double[nbRef];
482 for ( int iElem = 0; iElem < nbElem; iElem++ ) {
483 #ifdef WITH_SMESH_CANCEL_COMPUTE
484 if(theAlgo->computeCanceled())
489 aHexoticID = iElem + 1;
490 for ( int iCoord = 0; iCoord < 3; iCoord++ )
491 fileRes >> coord[ iCoord ];
493 aHexoticNode = theMesh->AddNode(coord[0], coord[1], coord[2]);
494 HexoticNode[ aHexoticID ] = aHexoticNode;
495 nodeAssigne[ aHexoticID ] = 0;
503 case 6: // "Quadrilaterals"
504 case 7: { // "Hexahedra"
505 MESSAGE("Read " << nbElem << " " << token);
506 SMDS_MeshNode** node;
507 int nodeDim, *nodeID;
508 SMDS_MeshElement * aHexoticElement = 0;
510 node = new SMDS_MeshNode*[ nbRef ];
511 nodeID = new int[ nbRef ];
512 for ( int iElem = 0; iElem < nbElem; iElem++ ) {
513 #ifdef WITH_SMESH_CANCEL_COMPUTE
514 if(theAlgo->computeCanceled())
519 for ( int iRef = 0; iRef < nbRef; iRef++ ) {
520 fileRes >> aHexoticNodeID; // read nbRef aHexoticNodeID
521 node[ iRef ] = HexoticNode[ aHexoticNodeID ];
522 nodeID[ iRef ] = aHexoticNodeID;
527 case 3: { // "Corners"
529 gp_Pnt HexoticPnt ( node[0]->X(), node[0]->Y(), node[0]->Z() );
530 for ( int i=0; i<nbElem; i++ ) {
531 aVertex = TopoDS::Vertex( tabCorner[i] );
532 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
533 if ( aPnt.Distance( HexoticPnt ) < epsilon )
540 aHexoticElement = theMesh->AddEdge( node[0], node[1] );
542 if ( nodeAssigne[ nodeID[0] ] == 0 || nodeAssigne[ nodeID[0] ] == 2 )
545 shapeID = findEdge( node[iNode], theMesh, nbShapeEdge, tabEdge );
550 case 5: { // "Ridges"
553 case 6: { // "Quadrilaterals"
555 aHexoticElement = theMesh->AddFace( node[0], node[1], node[2], node[3] );
559 case 7: { // "Hexahedra"
561 if ( nbDomains > 1 ) {
562 hexoticShapeID = dummy - IdShapeRef;
563 if ( tabID[ hexoticShapeID ] == 0 ) {
564 aShape = findShape(node, aShape, tabShape, tabBox, nbShape);
565 shapeID = aShape.IsNull() ? holeID : theMesh->ShapeToIndex( aShape );
566 tabID[ hexoticShapeID ] = shapeID;
569 shapeID = tabID[ hexoticShapeID ];
570 if ( iElem == (nbElem - 1) ) {
571 int shapeAssociated = 0;
572 for ( int i=0; i<nbDomains; i++ ) {
574 shapeAssociated += 1;
576 if ( shapeAssociated != nbShape )
577 printWarning(nbShape, "domains", shapeAssociated);
583 if ( shapeID != holeID )
584 aHexoticElement = theMesh->AddVolume( node[0], node[3], node[2], node[1], node[4], node[7], node[6], node[5] );
589 if ( token != "Ridges" && ( shapeID > 0 || token == "Corners")) {
590 for ( int i=0; i<nbRef; i++ ) {
591 if ( nodeAssigne[ nodeID[i] ] == 0 ) {
592 if ( token == "Corners" ) theMesh->SetNodeOnVertex( node[0], aVertex );
593 else if ( token == "Edges" ) theMesh->SetNodeOnEdge( node[i], shapeID );
594 else if ( token == "Quadrilaterals" ) theMesh->SetNodeOnFace( node[i], shapeID );
595 else if ( token == "Hexahedra" ) theMesh->SetNodeInVolume( node[i], shapeID );
596 nodeAssigne[ nodeID[i] ] = nodeDim;
599 if ( token != "Corners" && aHexoticElement )
600 theMesh->SetMeshElementOnShape( aHexoticElement, shapeID );
609 MESSAGE("End of " << theFile << " file");
613 MESSAGE("Unknown Token: " << token);
619 // remove nodes in holes
622 SMESHDS_SubMesh* subMesh;
623 for ( int i = 1; i <= nbVertices; ++i )
624 if ( HexoticNode[i]->NbInverseElements() == 0 )
626 subMesh = HexoticNode[i]->getshapeId() > 0 ? theMesh->MeshElements(HexoticNode[i]->getshapeId() ) : 0;
627 theMesh->RemoveFreeNode( HexoticNode[i], subMesh, /*fromGroups=*/false );
635 delete [] nodeAssigne;
636 delete [] HexoticNode;
641 //=======================================================================
642 //function : readResult
643 //purpose : Read GMF file in case of a mesh w/o geometry
644 //=======================================================================
646 static bool readResult(std::string theFile,
647 #ifdef WITH_SMESH_CANCEL_COMPUTE
648 HexoticPlugin_Hexotic* theAlgo,
650 SMESH_MesherHelper* theHelper)
652 SMESHDS_Mesh* theMesh = theHelper->GetMeshDS();
654 // ---------------------------------
655 // Read generated elements and nodes
656 // ---------------------------------
659 const int nbField = 9;
660 int nField, EndOfFile = 0, nbElem = 0, nbRef = 0;
661 int aHexoticNodeID = 0, shapeID;
662 int tabRef[nbField], *nodeAssigne;
663 bool tabDummy[nbField], hasDummy = false;
664 std::map <std::string,int> mapField;
665 SMDS_MeshNode** HexoticNode;
667 mapField["MeshVersionFormatted"] = 0; tabRef[0] = 0; tabDummy[0] = false;
668 mapField["Dimension"] = 1; tabRef[1] = 0; tabDummy[1] = false;
669 mapField["Vertices"] = 2; tabRef[2] = 3; tabDummy[2] = true;
670 mapField["Corners"] = 3; tabRef[3] = 1; tabDummy[3] = false;
671 mapField["Edges"] = 4; tabRef[4] = 2; tabDummy[4] = true;
672 mapField["Ridges"] = 5; tabRef[5] = 1; tabDummy[5] = false;
673 mapField["Quadrilaterals"] = 6; tabRef[6] = 4; tabDummy[6] = true;
674 mapField["Hexahedra"] = 7; tabRef[7] = 8; tabDummy[7] = true;
675 mapField["End"] = 8; tabRef[8] = 0; tabDummy[8] = false;
677 theHelper->GetMesh()->Clear();
679 int nbVertices = getNbShape(theFile, "Vertices");
680 HexoticNode = new SMDS_MeshNode*[ nbVertices + 1 ];
681 nodeAssigne = new int[ nbVertices + 1 ];
683 MESSAGE("Read " << theFile << " file");
684 std::ifstream fileRes(theFile.c_str());
692 if (mapField.count(token)) {
693 nField = mapField[token];
694 nbRef = tabRef[nField];
695 hasDummy = tabDummy[nField];
703 if ( nField < (mapField.size() - 1) && nField >= 0 )
707 case 0: { // "MeshVersionFormatted"
708 MESSAGE(token << " " << nbElem);
711 case 1: { // "Dimension"
712 MESSAGE("Mesh dimension " << nbElem << "D");
715 case 2: { // "Vertices"
716 MESSAGE("Read " << nbElem << " " << token);
719 SMDS_MeshNode * aHexoticNode;
721 for ( int iElem = 0; iElem < nbElem; iElem++ ) {
722 #ifdef WITH_SMESH_CANCEL_COMPUTE
723 if(theAlgo->computeCanceled())
728 aHexoticID = iElem + 1;
729 for ( int iCoord = 0; iCoord < 3; iCoord++ )
730 fileRes >> coord[ iCoord ];
732 aHexoticNode = theMesh->AddNode(coord[0], coord[1], coord[2]);
733 HexoticNode[ aHexoticID ] = aHexoticNode;
734 nodeAssigne[ aHexoticID ] = 0;
741 case 6: // "Quadrilaterals"
742 case 7: { // "Hexahedra"
743 MESSAGE("Read " << nbElem << " " << token);
744 std::vector< SMDS_MeshNode* > node( nbRef );
745 std::vector< int > nodeID( nbRef );
747 for ( int iElem = 0; iElem < nbElem; iElem++ )
749 #ifdef WITH_SMESH_CANCEL_COMPUTE
750 if(theAlgo->computeCanceled())
755 for ( int iRef = 0; iRef < nbRef; iRef++ )
757 fileRes >> aHexoticNodeID; // read nbRef aHexoticNodeID
758 node [ iRef ] = HexoticNode[ aHexoticNodeID ];
759 nodeID[ iRef ] = aHexoticNodeID;
766 theHelper->AddEdge( node[0], node[1] ); break;
767 case 6: // "Quadrilaterals"
768 theMesh->AddFace( node[0], node[1], node[2], node[3] ); break;
769 case 7: // "Hexahedra"
770 theHelper->AddVolume( node[0], node[3], node[2], node[1],
771 node[4], node[7], node[6], node[5] ); break;
775 for ( int iRef = 0; iRef < nbRef; iRef++ )
776 nodeAssigne[ nodeID[ iRef ]] = 1;
782 MESSAGE("End of " << theFile << " file");
786 MESSAGE("Unknown Token: " << token);
792 shapeID = theHelper->GetSubShapeID();
793 for ( int i = 0; i < nbVertices; ++i )
794 if ( !nodeAssigne[ i+1 ])
795 theMesh->SetNodeInVolume( HexoticNode[ i+1 ], shapeID );
797 delete [] HexoticNode;
798 delete [] nodeAssigne;
802 //=============================================================================
804 * Pass parameters to Hexotic
806 //=============================================================================
808 void HexoticPlugin_Hexotic::SetParameters(const HexoticPlugin_Hypothesis* hyp) {
810 MESSAGE("HexoticPlugin_Hexotic::SetParameters");
812 _hexesMinLevel = hyp->GetHexesMinLevel();
813 _hexesMaxLevel = hyp->GetHexesMaxLevel();
814 _hexesMinSize = hyp->GetMinSize();
815 _hexesMaxSize = hyp->GetMaxSize();
816 _hexoticIgnoreRidges = hyp->GetHexoticIgnoreRidges();
817 _hexoticInvalidElements = hyp->GetHexoticInvalidElements();
818 _hexoticSharpAngleThreshold = hyp->GetHexoticSharpAngleThreshold();
819 _hexoticNbProc = hyp->GetHexoticNbProc();
820 _hexoticWorkingDirectory = hyp->GetHexoticWorkingDirectory();
821 _hexoticVerbosity = hyp->GetHexoticVerbosity();
822 _hexoticMaxMemory = hyp->GetHexoticMaxMemory();
823 _hexoticSdMode = hyp->GetHexoticSdMode();
827 cout << "WARNING : The Hexotic default parameters are taken into account" << std::endl;
828 cout << "=======" << std::endl;
829 _hexesMinLevel = hyp->GetDefaultHexesMinLevel();
830 _hexesMaxLevel = hyp->GetDefaultHexesMaxLevel();
831 _hexesMinSize = hyp->GetDefaultMinSize();
832 _hexesMaxSize = hyp->GetDefaultMaxSize();
833 _hexoticIgnoreRidges = hyp->GetDefaultHexoticIgnoreRidges();
834 _hexoticInvalidElements = hyp->GetDefaultHexoticInvalidElements();
835 _hexoticSharpAngleThreshold = hyp->GetDefaultHexoticSharpAngleThreshold();
836 _hexoticNbProc = hyp->GetDefaultHexoticNbProc();
837 _hexoticWorkingDirectory = hyp->GetDefaultHexoticWorkingDirectory();
838 _hexoticVerbosity = hyp->GetDefaultHexoticVerbosity();
839 _hexoticMaxMemory = hyp->GetDefaultHexoticMaxMemory();
840 _hexoticSdMode = hyp->GetDefaultHexoticSdMode();
844 //=======================================================================
845 //function : getTmpDir
847 //=======================================================================
849 static TCollection_AsciiString getTmpDir()
851 TCollection_AsciiString aTmpDir;
853 char *Tmp_dir = getenv("SALOME_TMP_DIR");
855 if(Tmp_dir == NULL) {
856 Tmp_dir = getenv("TEMP");
858 Tmp_dir = getenv("TMP");
861 if(Tmp_dir != NULL) {
864 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
866 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
871 aTmpDir = TCollection_AsciiString("C:\\");
873 aTmpDir = TCollection_AsciiString("/tmp/");
879 //=======================================================================
880 //function : getSuffix
881 //purpose : Returns a suffix that will be unique for the current process
882 //=======================================================================
884 static TCollection_AsciiString getSuffix()
886 TCollection_AsciiString aSuffix = "";
888 aSuffix += getenv("USER");
890 aSuffix += Kernel_Utils::GetHostname().c_str();
897 //================================================================================
899 * \brief Returns a command to run Hexotic mesher
901 //================================================================================
903 std::string HexoticPlugin_Hexotic::getHexoticCommand(const TCollection_AsciiString& Hexotic_In,
904 const TCollection_AsciiString& Hexotic_Out) const
907 cout << "Hexotic execution..." << std::endl;
908 cout << _name << " parameters :" << std::endl;
909 cout << " " << _name << " Verbosity = " << _hexoticVerbosity << std::endl;
910 cout << " " << _name << " Max Memory = " << _hexoticMaxMemory << std::endl;
911 cout << " " << _name << " Segments Min Level = " << _hexesMinLevel << std::endl;
912 cout << " " << _name << " Segments Max Level = " << _hexesMaxLevel << std::endl;
913 cout << " " << _name << " Segments Min Size = " << _hexesMinSize << std::endl;
914 cout << " " << _name << " Segments Max Size = " << _hexesMaxSize << std::endl;
915 cout << " " << "Hexotic can ignore ridges : " << (_hexoticIgnoreRidges ? "yes":"no") << std::endl;
916 cout << " " << "Hexotic authorize invalide elements : " << ( _hexoticInvalidElements ? "yes":"no") << std::endl;
917 cout << " " << _name << " Sharp angle threshold = " << _hexoticSharpAngleThreshold << " degrees" << std::endl;
918 cout << " " << _name << " Number of threads = " << _hexoticNbProc << std::endl;
919 cout << " " << _name << " Working directory = \"" << _hexoticWorkingDirectory << "\"" << std::endl;
920 cout << " " << _name << " Sub. Dom mode = " << _hexoticSdMode << std::endl;
922 TCollection_AsciiString run_Hexotic( "hexotic" );
924 TCollection_AsciiString minl = " -minl ", maxl = " -maxl ", angle = " -ra ";
925 TCollection_AsciiString mins = " -mins ", maxs = " -maxs ";
926 TCollection_AsciiString in = " -in ", out = " -out ";
927 TCollection_AsciiString ignoreRidges = " -nr ", invalideElements = " -inv ";
928 TCollection_AsciiString subdom = " -sd ", sharp = " -sharp ";
929 TCollection_AsciiString proc = " -nproc ";
930 TCollection_AsciiString verb = " -v ";
931 TCollection_AsciiString maxmem = " -m ";
933 TCollection_AsciiString minLevel, maxLevel, minSize, maxSize, sharpAngle, mode, nbproc, verbosity, maxMemory;
934 minLevel = _hexesMinLevel;
935 maxLevel = _hexesMaxLevel;
936 minSize = _hexesMinSize;
937 maxSize = _hexesMaxSize;
938 sharpAngle = _hexoticSharpAngleThreshold;
939 mode = _hexoticSdMode;
940 nbproc = _hexoticNbProc;
941 verbosity = _hexoticVerbosity;
942 maxMemory = _hexoticMaxMemory;
944 if (_hexoticIgnoreRidges)
945 run_Hexotic += ignoreRidges;
947 if (_hexoticInvalidElements)
948 run_Hexotic += invalideElements;
950 if (_hexesMinSize > 0)
951 run_Hexotic += mins + minSize;
953 if (_hexesMaxSize > 0)
954 run_Hexotic += maxs + maxSize;
956 if (_hexesMinLevel > 0)
957 run_Hexotic += minl + minLevel;
959 if (_hexesMaxLevel > 0)
960 run_Hexotic += maxl + maxLevel;
962 if (_hexoticSharpAngleThreshold > 0)
963 run_Hexotic += angle + sharpAngle;
965 run_Hexotic += in + Hexotic_In + out + Hexotic_Out;
966 run_Hexotic += subdom + mode;
967 run_Hexotic += proc + nbproc;
968 run_Hexotic += verb + verbosity;
969 run_Hexotic += maxmem + maxMemory;
971 return run_Hexotic.ToCString();
974 //=============================================================================
976 * Here we are going to use the Hexotic mesher
978 //=============================================================================
980 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh& aMesh,
981 const TopoDS_Shape& aShape)
983 #ifdef WITH_SMESH_CANCEL_COMPUTE
984 _compute_canceled = false;
987 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
988 TCollection_AsciiString hexahedraMessage;
990 if (_iShape == 0 && _nbShape == 0) {
991 _nbShape = countShape( meshDS, TopAbs_SOLID ); // we count the number of shapes
994 // to prevent from displaying error message after computing,
995 // SetIsAlwaysComputed( true ) to empty sub-meshes
996 vector< SMESH_subMesh* > subMeshesAlwaysComp;
997 for ( int i = 0; i < _nbShape; ++i )
998 if ( SMESH_subMesh* sm = aMesh.GetSubMeshContaining( aShape ))
1000 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
1001 /*complexShapeFirst=*/false);
1002 while ( smIt->more() )
1005 if ( !sm->IsMeshComputed() )
1007 sm->SetIsAlwaysComputed( true );
1008 subMeshesAlwaysComp.push_back( sm );
1015 if (_iShape == _nbShape ) {
1017 // create bounding box for each shape of the compound
1020 TopoDS_Shape *tabShape;
1023 tabShape = new TopoDS_Shape[_nbShape];
1024 tabBox = new double*[_nbShape];
1025 for (int i=0; i<_nbShape; i++)
1026 tabBox[i] = new double[6];
1027 double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1029 TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID);
1030 for (; expBox.More(); expBox.Next()) {
1031 tabShape[iShape] = expBox.Current();
1032 Bnd_Box BoundingBox;
1033 BRepBndLib::Add(expBox.Current(), BoundingBox);
1034 BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
1035 tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
1036 tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
1037 tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
1041 SetParameters(_hypothesis);
1043 // TCollection_AsciiString aTmpDir = getTmpDir();
1044 TCollection_AsciiString aTmpDir = TCollection_AsciiString(_hexoticWorkingDirectory.c_str());
1046 if ( aTmpDir.Value(aTmpDir.Length()) != '\\' ) aTmpDir += '\\';
1048 if ( aTmpDir.Value(aTmpDir.Length()) != '/' ) aTmpDir += '/';
1050 TCollection_AsciiString Hexotic_In(""), Hexotic_Out;
1051 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1052 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic"+getSuffix()+".log"; // log
1054 std::map <int,int> aSmdsToHexoticIdMap;
1055 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1057 Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
1058 #ifdef WITH_BLSURFPLUGIN
1059 bool defaultInputFile = true;
1060 if (_blsurfHypo && !_blsurfHypo->GetQuadAllowed()) {
1061 Hexotic_In = TCollection_AsciiString(_blsurfHypo->GetGMFFile().c_str());
1062 if (Hexotic_In != "")
1063 defaultInputFile = false;
1065 if (defaultInputFile) {
1067 Hexotic_In = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
1068 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1070 cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1071 aMesh.ExportGMF(Hexotic_In.ToCString(), meshDS, true);
1072 #ifdef WITH_BLSURFPLUGIN
1075 removeFile( Hexotic_Out );
1080 std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1081 run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString(); // dump into file
1084 cout << "Hexotic command : " << run_Hexotic << std::endl;
1086 modeFile_In += Hexotic_In;
1087 system( modeFile_In.ToCString() );
1088 aSmdsToHexoticIdMap.clear();
1089 aHexoticIdToNodeMap.clear();
1091 MESSAGE("HexoticPlugin_Hexotic::Compute");
1093 int status = system( run_Hexotic.data() );
1099 std::ifstream fileRes( Hexotic_Out.ToCString() );
1100 modeFile_Out += Hexotic_Out;
1101 system( modeFile_Out.ToCString() );
1102 if ( ! fileRes.fail() ) {
1103 Ok = readResult( Hexotic_Out.ToCString(),
1104 #ifdef WITH_SMESH_CANCEL_COMPUTE
1107 meshDS, _nbShape, tabShape, tabBox );
1109 /*********************
1110 // TODO: Detect and remove elements in holes in case of sd mode = 4
1111 // Remove previous nodes and elements
1112 SMDS_ElemIteratorPtr itElement = meshDS->elementsIterator();
1113 SMDS_NodeIteratorPtr itNode = meshDS->nodesIterator();
1115 while ( itElement->more() )
1116 meshDS->RemoveElement( itElement->next() );
1117 while ( itNode->more() )
1118 meshDS->RemoveNode( itNode->next() );
1120 SMESH_ComputeErrorPtr myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1123 hexahedraMessage = "success";
1124 removeFile(Hexotic_Out);
1125 removeFile(Hexotic_In);
1126 removeFile(aLogFileName);
1129 hexahedraMessage = "failed";
1133 hexahedraMessage = "failed";
1134 cout << "Problem with Hexotic output file " << Hexotic_Out.ToCString() << std::endl;
1137 SMESH_File logFile( aLogFileName.ToCString() );
1138 if ( !logFile.eof() )
1140 char msgLic[] = " Dlim ";
1141 const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1142 if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1143 error("Licence problems.");
1146 if ( status > 0 && WEXITSTATUS(status) == 127 )
1147 error("hexotic: command not found");
1150 if ( status == 0 && err == ENOENT ) {
1151 error("hexotic: command not found");
1155 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1158 // restore "always computed" flag of sub-meshes (0022127)
1159 for ( size_t iSM = 0; iSM < subMeshesAlwaysComp.size(); ++iSM )
1160 subMeshesAlwaysComp[ iSM ]->SetIsAlwaysComputed( false );
1163 for (int i=0; i<_nbShape; i++)
1164 delete [] tabBox[i];
1169 #ifdef WITH_SMESH_CANCEL_COMPUTE
1170 if(_compute_canceled)
1171 return error(SMESH_Comment("interruption initiated by user"));
1176 //=============================================================================
1178 * \brief Computes mesh without geometry
1179 * \param aMesh - the mesh
1180 * \param aHelper - helper that must be used for adding elements to \aaMesh
1181 * \retval bool - is a success
1183 * The method is called if ( !aMesh->HasShapeToMesh() )
1185 //=============================================================================
1187 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
1189 #ifdef WITH_SMESH_CANCEL_COMPUTE
1190 _compute_canceled = false;
1193 SMESH_ComputeErrorPtr myError = SMESH_ComputeError::New();
1196 TCollection_AsciiString hexahedraMessage;
1198 SetParameters(_hypothesis);
1200 TCollection_AsciiString aTmpDir = getTmpDir();
1201 TCollection_AsciiString Hexotic_In, Hexotic_Out;
1202 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1203 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic"+getSuffix()+".log"; // log
1205 std::map <int,int> aSmdsToHexoticIdMap;
1206 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1208 Hexotic_In = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
1209 Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
1211 std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1212 run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString(); // dump into file
1214 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1217 cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1218 aMesh.ExportGMF(Hexotic_In.ToCString(), aHelper->GetMeshDS());
1219 modeFile_In += Hexotic_In;
1220 system( modeFile_In.ToCString() );
1221 aSmdsToHexoticIdMap.clear();
1222 aHexoticIdToNodeMap.clear();
1224 MESSAGE("HexoticPlugin_Hexotic::Compute");
1227 cout << "Hexotic command : " << run_Hexotic << std::endl;
1228 system( run_Hexotic.data() );
1234 std::ifstream fileRes( Hexotic_Out.ToCString() );
1235 modeFile_Out += Hexotic_Out;
1236 system( modeFile_Out.ToCString() );
1237 if ( ! fileRes.fail() ) {
1238 Ok = readResult( Hexotic_Out.ToCString(),
1239 #ifdef WITH_SMESH_CANCEL_COMPUTE
1245 // Remove previous nodes and elements
1246 SMDS_ElemIteratorPtr itElement = aHelper->GetMeshDS()->elementsIterator();
1247 SMDS_NodeIteratorPtr itNode = aHelper->GetMeshDS()->nodesIterator();
1249 while ( itElement->more() )
1250 aHelper->GetMeshDS()->RemoveElement( itElement->next() );
1251 while ( itNode->more() )
1252 aHelper->GetMeshDS()->RemoveNode( itNode->next() );
1255 myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1257 itElement = aHelper->GetMeshDS()->elementsIterator();
1258 itNode = aHelper->GetMeshDS()->nodesIterator();
1260 // Assign nodes and elements to the pseudo shape
1261 while ( itNode->more() )
1262 aHelper->GetMeshDS()->SetNodeInVolume(itNode->next(), 1);
1263 while ( itElement->more() )
1264 aHelper->GetMeshDS()->SetMeshElementOnShape(itElement->next(), 1);
1268 hexahedraMessage = "success";
1270 hexahedraMessage = "failed";
1274 myError->myName = COMPERR_EXCEPTION;
1276 hexahedraMessage = "failed";
1277 cout << "Problem with Hexotic output file " << Hexotic_Out << std::endl;
1279 SMESH_File logFile( aLogFileName.ToCString() );
1280 if ( !logFile.eof() )
1282 char msgLic[] = " Dlim ";
1283 const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1284 if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1285 return error("Licence problems.");
1287 return error(SMESH_Comment("Problem with Hexotic output file ")<<Hexotic_Out);
1289 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1292 #ifdef WITH_SMESH_CANCEL_COMPUTE
1293 if(_compute_canceled)
1294 return error(SMESH_Comment("interruption initiated by user"));
1296 removeFile(Hexotic_Out);
1297 removeFile(Hexotic_In);
1298 removeFile(aLogFileName);
1301 return myError->IsOK();
1305 //=============================================================================
1309 //=============================================================================
1311 bool HexoticPlugin_Hexotic::Evaluate(SMESH_Mesh& aMesh,
1312 const TopoDS_Shape& aShape,
1313 MapShapeNbElems& aResMap)
1315 std::vector<int> aResVec(SMDSEntity_Last);
1316 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1317 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1318 aResMap.insert(std::make_pair(sm,aResVec));
1320 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1321 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Evaluation is not implemented",this));
1326 #ifdef WITH_SMESH_CANCEL_COMPUTE
1327 void HexoticPlugin_Hexotic::CancelCompute()
1329 _compute_canceled = true;
1332 TCollection_AsciiString aTmpDir = getTmpDir();
1333 TCollection_AsciiString Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1334 TCollection_AsciiString cmd = TCollection_AsciiString("ps ux | grep ") + Hexotic_In;
1335 cmd += TCollection_AsciiString(" | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1");
1336 system( cmd.ToCString() );