1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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"
27 #include "MG_Hexotic_API.hxx"
29 #include "utilities.h"
42 #include <SMESHDS_GroupBase.hxx>
43 #include <SMESHDS_Mesh.hxx>
44 #include <SMESH_ComputeError.hxx>
45 #include <SMESH_File.hxx>
46 #include <SMESH_Gen.hxx>
47 #include <SMESH_HypoFilter.hxx>
48 #include <SMESH_MesherHelper.hxx>
49 #include <SMESH_subMesh.hxx>
50 #include <SMESH_MeshEditor.hxx>
51 #include <SMESH_ControlPnt.hxx>
56 #include <Standard_ProgramError.hxx>
58 #include <BRepBndLib.hxx>
59 #include <BRepClass3d_SolidClassifier.hxx>
60 #include <BRep_Tool.hxx>
61 #include <Bnd_Box.hxx>
62 #include <OSD_File.hxx>
63 #include <Precision.hxx>
64 #include <TopExp_Explorer.hxx>
65 #include <TopTools_MapOfShape.hxx>
67 #include <TopoDS_Vertex.hxx>
70 #include <Basics_Utils.hxx>
71 #include <GEOMImpl_Types.hxx>
72 #include <GEOM_wrap.hxx>
74 #define GMFVERSION GmfDouble
75 #define GMFDIMENSION 3
77 using SMESHUtils::ControlPnt;
79 static void removeFile( const TCollection_AsciiString& fileName )
82 OSD_File( fileName ).Remove();
84 catch ( Standard_ProgramError ) {
85 MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
89 //=============================================================================
93 //=============================================================================
95 HexoticPlugin_Hexotic::HexoticPlugin_Hexotic(int hypId, SMESH_Gen* gen)
96 : SMESH_3D_Algo(hypId, gen)
98 MESSAGE("HexoticPlugin_Hexotic::HexoticPlugin_Hexotic");
100 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
101 _onlyUnaryInput = false;
102 _requireShape = false;
105 _hexoticFilesKept=false;
106 _compatibleHypothesis.push_back( HexoticPlugin_Hypothesis::GetHypType() );
107 #ifdef WITH_BLSURFPLUGIN
110 _computeCanceled = false;
114 //=============================================================================
118 //=============================================================================
120 HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic()
122 MESSAGE("HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic");
126 #ifdef WITH_BLSURFPLUGIN
127 bool HexoticPlugin_Hexotic::CheckBLSURFHypothesis( SMESH_Mesh& aMesh,
128 const TopoDS_Shape& aShape )
130 MESSAGE("HexoticPlugin_Hexotic::CheckBLSURFHypothesis");
133 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
134 const SMESHDS_Hypothesis* theHyp;
136 // If a BLSURF hypothesis is applied, get it
137 SMESH_HypoFilter blsurfFilter;
138 blsurfFilter.Init( blsurfFilter.HasName( BLSURFPlugin_Hypothesis::GetHypType(true) ));
139 blsurfFilter.Or ( blsurfFilter.HasName( BLSURFPlugin_Hypothesis::GetHypType(false)));
140 std::list<const SMESHDS_Hypothesis *> appliedHyps;
141 aMesh.GetHypotheses( aShape, blsurfFilter, appliedHyps, false );
143 if ( appliedHyps.size() > 0 ) {
144 itl = appliedHyps.begin();
145 theHyp = (*itl); // use only the first hypothesis
146 std::string hypName = theHyp->GetName();
147 if (hypName == BLSURFPlugin_Hypothesis::GetHypType(true) ||
148 hypName == BLSURFPlugin_Hypothesis::GetHypType(false) )
150 _blsurfHypo = static_cast<const BLSURFPlugin_Hypothesis*> (theHyp);
159 //=============================================================================
163 //=============================================================================
165 bool HexoticPlugin_Hexotic::CheckHypothesis( SMESH_Mesh& aMesh,
166 const TopoDS_Shape& aShape,
167 SMESH_Hypothesis::Hypothesis_Status& aStatus )
169 MESSAGE("HexoticPlugin_Hexotic::CheckHypothesis");
172 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
173 const SMESHDS_Hypothesis* theHyp;
175 const std::list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
176 int nbHyp = hyps.size();
178 // retrieve BLSURF hypothesis if no hexotic hypothesis has been set
179 #ifdef WITH_BLSURFPLUGIN
180 CheckBLSURFHypothesis(aMesh, aShape);
182 aStatus = SMESH_Hypothesis::HYP_OK;
183 return true; // can work with no hypothesis
187 theHyp = (*itl); // use only the first hypothesis
189 std::string hypName = theHyp->GetName();
190 if (hypName == HexoticPlugin_Hypothesis::GetHypType() ) {
191 _hypothesis = static_cast<const HexoticPlugin_Hypothesis*> (theHyp);
193 aStatus = SMESH_Hypothesis::HYP_OK;
196 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
198 #ifdef WITH_BLSURFPLUGIN
199 CheckBLSURFHypothesis(aMesh, aShape);
202 return aStatus == SMESH_Hypothesis::HYP_OK;
205 //=======================================================================
206 //function : findShape
208 //=======================================================================
210 static TopoDS_Shape findShape(SMDS_MeshNode** t_Node,
212 const TopoDS_Shape* t_Shape,
217 int iShape, nbNode = 8;
219 for ( int i=0; i<3; i++ ) {
221 for ( int j=0; j<nbNode; j++ ) {
222 if ( i == 0) pntCoor[i] += t_Node[j]->X();
223 if ( i == 1) pntCoor[i] += t_Node[j]->Y();
224 if ( i == 2) pntCoor[i] += t_Node[j]->Z();
226 pntCoor[i] /= nbNode;
228 gp_Pnt aPnt(pntCoor[0], pntCoor[1], pntCoor[2]);
230 if ( aShape.IsNull() ) aShape = t_Shape[0];
231 BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
232 if ( !(SC.State() == TopAbs_IN) ) {
234 for (iShape = 0; iShape < nShape && aShape.IsNull(); iShape++) {
235 if ( !( pntCoor[0] < t_Box[iShape][0] || t_Box[iShape][1] < pntCoor[0] ||
236 pntCoor[1] < t_Box[iShape][2] || t_Box[iShape][3] < pntCoor[1] ||
237 pntCoor[2] < t_Box[iShape][4] || t_Box[iShape][5] < pntCoor[2]) ) {
238 BRepClass3d_SolidClassifier SC (t_Shape[iShape], aPnt, Precision::Confusion());
239 if (SC.State() == TopAbs_IN)
240 aShape = t_Shape[iShape];
247 //=======================================================================
248 //function : getNbShape
250 //=======================================================================
252 static int getNbShape(MG_Hexotic_API* hexaOutput, int iMesh, GmfKwdCod what, int defaultValue=0)
254 int number = hexaOutput->GmfStatKwd( iMesh, what );
257 // std::string aLine;
258 // std::ifstream file(aFile.c_str());
259 // while ( !file.eof() ) {
260 // getline( file, aLine);
261 // if ( aLine == aString ) {
262 // getline( file, aLine);
263 // std::istringstream stringFlux( aLine );
264 // stringFlux >> number;
265 number = ( number + defaultValue + std::abs(number - defaultValue) ) / 2;
271 number = defaultValue;
277 //=======================================================================
278 //function : countShape
280 //=======================================================================
282 static int countShape( SMESH_Mesh* mesh, TopAbs_ShapeEnum shape )
284 if ( !mesh->HasShapeToMesh() )
286 TopExp_Explorer expShape ( mesh->GetShapeToMesh(), shape );
287 TopTools_MapOfShape mapShape;
289 for ( ; expShape.More(); expShape.Next() ) {
290 if (mapShape.Add(expShape.Current())) {
297 //=======================================================================
298 //function : getShape
300 //=======================================================================
302 template < class Mesh, class Shape, class Tab >
303 void getShape(Mesh* mesh, Shape shape, Tab *t_Shape)
305 TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
306 TopTools_MapOfShape mapShape;
307 for ( int i=0; expShape.More(); expShape.Next() ) {
308 if (mapShape.Add(expShape.Current())) {
309 t_Shape[i] = expShape.Current();
316 //=======================================================================
317 //function : printWarning
319 //=======================================================================
321 static void printWarning(const int nbExpected, std::string aString, const int nbFound) {
323 cout << "WARNING : " << nbExpected << " " << aString << " expected, MG-Hexa has found " << nbFound << std::endl;
324 cout << "=======" << std::endl;
329 //=======================================================================
330 //function : removeHexoticFiles
332 //=======================================================================
334 static void removeHexoticFiles(TCollection_AsciiString file_In, TCollection_AsciiString file_Out) {
335 removeFile( file_In );
336 removeFile( file_Out );
339 //=======================================================================
340 //function : splitQuads
341 //purpose : splits all quadrangles into triangles
342 //=======================================================================
344 static void splitQuads(SMESH_Mesh& aMesh)
346 SMESH_MeshEditor spliter( &aMesh );
348 TIDSortedElemSet elems;
349 SMDS_ElemIteratorPtr eIt = aMesh.GetMeshDS()->elementsIterator();
351 elems.insert( elems.end(), eIt->next() );
353 spliter.QuadToTri ( elems, /*the13Diag=*/true);
356 //=======================================================================
357 //function : writeInput
358 //purpose : pass a mesh to input of MG-Hexa
359 //=======================================================================
361 static void writeInput(MG_Hexotic_API* theHexaInput,
363 const SMESHDS_Mesh* theMeshDS)
365 int meshID = theHexaInput->GmfOpenMesh( theFile, GmfWrite, GMFVERSION, GMFDIMENSION);
368 int iN = 0, nbNodes = theMeshDS->NbNodes();
369 theHexaInput->GmfSetKwd( meshID, GmfVertices, nbNodes );
370 std::map< const SMDS_MeshNode*, int, TIDCompare > node2IdMap;
371 SMDS_NodeIteratorPtr nodeIt = theMeshDS->nodesIterator();
373 while ( nodeIt->more() )
375 n.Set( nodeIt->next() );
376 theHexaInput->GmfSetLin( meshID, GmfVertices, n.X(), n.Y(), n.Z(), n._node->getshapeId() );
377 node2IdMap.insert( node2IdMap.end(), std::make_pair( n._node, ++iN ));
381 SMDS_ElemIteratorPtr elemIt = theMeshDS->elementsIterator( SMDSAbs_Edge );
382 if ( elemIt->more() )
384 int nbEdges = theMeshDS->GetMeshInfo().NbElements( SMDSAbs_Edge );
385 theHexaInput->GmfSetKwd(meshID, GmfEdges, nbEdges );
386 for ( int gmfID = 1; elemIt->more(); ++gmfID )
388 const SMDS_MeshElement* edge = elemIt->next();
389 theHexaInput->GmfSetLin(meshID, GmfEdges,
390 node2IdMap[ edge->GetNode( 0 )],
391 node2IdMap[ edge->GetNode( 1 )],
392 edge->getshapeId() );
397 elemIt = theMeshDS->elementGeomIterator( SMDSGeom_TRIANGLE );
398 if ( elemIt->more() )
400 int nbTria = theMeshDS->GetMeshInfo().NbElements( SMDSGeom_TRIANGLE );
401 theHexaInput->GmfSetKwd(meshID, GmfTriangles, nbTria );
402 for ( int gmfID = 1; elemIt->more(); ++gmfID )
404 const SMDS_MeshElement* tria = elemIt->next();
405 theHexaInput->GmfSetLin(meshID, GmfTriangles,
406 node2IdMap[ tria->GetNode( 0 )],
407 node2IdMap[ tria->GetNode( 1 )],
408 node2IdMap[ tria->GetNode( 2 )],
409 tria->getshapeId() );
412 theHexaInput->GmfCloseMesh( meshID );
415 //=======================================================================
416 //function : readResult
417 //purpose : Read GMF file in case of a mesh with geometry
418 //=======================================================================
420 static bool readResult(MG_Hexotic_API* theHexaOutput,
422 HexoticPlugin_Hexotic*theAlgo,
423 SMESH_MesherHelper* theHelper,
424 const int nbShape = 0,
425 const TopoDS_Shape* tabShape = 0,
428 SMESH_Mesh* theMesh = theHelper->GetMesh();
429 SMESHDS_Mesh* theMeshDS = theHelper->GetMeshDS();
431 // ---------------------------------
432 // Read generated elements and nodes
433 // ---------------------------------
436 TopoDS_Vertex aVertex;
438 int shapeID, hexoticShapeID;
439 const int IdShapeRef = 2;
441 double epsilon = Precision::Confusion();
442 std::map <std::string,int> mapField;
443 SMDS_MeshNode** HexoticNode;
444 TopoDS_Shape *tabCorner;
446 const int nbDomains = countShape( theMesh, TopAbs_SHELL );
447 const int holeID = -1;
451 tabID = new int[nbDomains];
453 for (int i=0; i<nbDomains; i++)
455 if ( nbDomains == 1 )
456 tabID[0] = theMeshDS->ShapeToIndex( tabShape[0] );
464 SMDS_ElemIteratorPtr eIt = theMeshDS->elementsIterator();
466 theMeshDS->RemoveFreeElement( eIt->next(), /*sm=*/0 );
467 SMDS_NodeIteratorPtr nIt = theMeshDS->nodesIterator();
468 while ( nIt->more() )
469 theMeshDS->RemoveFreeNode( nIt->next(), /*sm=*/0 );
471 theHelper->SetElementsOnShape( false );
474 int meshID = theHexaOutput->GmfOpenMesh( theFile, GmfRead, &ver, &dim );
476 int nbVertices = getNbShape(theHexaOutput, meshID, GmfVertices );
477 int nbCorners = getNbShape(theHexaOutput, meshID, GmfCorners, countShape( theMesh, TopAbs_VERTEX ));
478 if ( nbVertices == 0 )
481 tabCorner = new TopoDS_Shape[ nbCorners ];
482 HexoticNode = new SMDS_MeshNode*[ nbVertices + 1 ];
485 getShape(theMeshDS, TopAbs_VERTEX, tabCorner);
487 int nbNodes = theHexaOutput->GmfStatKwd( meshID, GmfVertices );
490 theHexaOutput->GmfGotoKwd( meshID, GmfVertices );
492 for ( int aHexoticID = 1; aHexoticID <= nbNodes; ++aHexoticID )
494 if ( theAlgo->computeCanceled() )
496 theHexaOutput->GmfGetLin( meshID, GmfVertices, &x, &y, &z, &shapeID );
497 HexoticNode[ aHexoticID ] = theHelper->AddNode( x,y,z );
502 SMDS_MeshNode* node[8];
503 SMDS_MeshElement * aHexoticElement;
505 nbCorners = theHexaOutput->GmfStatKwd( meshID, GmfCorners );
506 if ( nbCorners > 0 && nbDomains > 0 )
508 theHexaOutput->GmfGotoKwd( meshID, GmfCorners );
509 for ( int iElem = 0; iElem < nbCorners; iElem++ )
511 if ( theAlgo->computeCanceled() )
513 theHexaOutput->GmfGetLin( meshID, GmfCorners, &nodeID[0] );
514 node[0] = HexoticNode[ nodeID[0] ];
515 gp_Pnt HexoticPnt ( node[0]->X(), node[0]->Y(), node[0]->Z() );
516 for ( int i = 0; i < nbCorners; i++ )
518 aVertex = TopoDS::Vertex( tabCorner[i] );
519 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
520 if ( aPnt.Distance( HexoticPnt ) < epsilon )
522 theMeshDS->SetNodeOnVertex( node[0], aVertex );
529 int nbEdges = theHexaOutput->GmfStatKwd( meshID, GmfEdges );
532 theHexaOutput->GmfGotoKwd( meshID, GmfEdges );
533 for ( int iElem = 0; iElem < nbEdges; iElem++ )
535 if ( theAlgo->computeCanceled() )
537 theHexaOutput->GmfGetLin( meshID, GmfEdges, &nodeID[0], &nodeID[1], &shapeID );
538 for ( int i = 0; i < 2; ++i )
540 node[i] = HexoticNode[ nodeID[i]];
541 if ( shapeID > 0 && node[i]->getshapeId() < 1 )
542 theMeshDS->SetNodeOnEdge( node[i], shapeID );
544 aHexoticElement = theHelper->AddEdge( node[0], node[1] );
545 if ( shapeID > 0 && aHexoticElement->getshapeId() < 1 )
546 theMeshDS->SetMeshElementOnShape( aHexoticElement, shapeID );
550 int nbQuad = theHexaOutput->GmfStatKwd( meshID, GmfQuadrilaterals );
553 theHexaOutput->GmfGotoKwd( meshID, GmfQuadrilaterals );
554 for ( int iElem = 0; iElem < nbQuad; iElem++ )
556 if ( theAlgo->computeCanceled() )
558 theHexaOutput->GmfGetLin( meshID, GmfQuadrilaterals,
559 &nodeID[0], &nodeID[1], &nodeID[2], &nodeID[3], &shapeID );
560 for ( int i = 0; i < 4; ++i )
562 node[i] = HexoticNode[ nodeID[i]];
563 if ( shapeID > 0 && node[i]->getshapeId() < 1 )
564 theMeshDS->SetNodeOnFace( node[i], shapeID );
566 aHexoticElement = theHelper->AddFace( node[0], node[1], node[2], node[3] );
567 if ( shapeID > 0 && aHexoticElement->getshapeId() < 1 )
568 theMeshDS->SetMeshElementOnShape( aHexoticElement, shapeID );
572 int nbHexa = theHexaOutput->GmfStatKwd( meshID, GmfHexahedra );
575 theHexaOutput->GmfGotoKwd( meshID, GmfHexahedra );
576 for ( int iElem = 0; iElem < nbHexa; iElem++ )
578 if ( theAlgo->computeCanceled() )
580 theHexaOutput->GmfGetLin( meshID, GmfHexahedra,
581 &nodeID[0], &nodeID[1], &nodeID[2], &nodeID[3],
582 &nodeID[4], &nodeID[5], &nodeID[6], &nodeID[7],
584 for ( int i = 0; i < 8; ++i )
586 node[i] = HexoticNode[ nodeID[i]];
588 if ( nbDomains > 1 ) {
589 hexoticShapeID = shapeID - IdShapeRef;
590 if ( tabID[ hexoticShapeID ] == 0 ) {
591 aShape = findShape(node, aShape, tabShape, tabBox, nbShape);
592 shapeID = aShape.IsNull() ? holeID : theMeshDS->ShapeToIndex( aShape );
593 tabID[ hexoticShapeID ] = shapeID;
596 shapeID = tabID[ hexoticShapeID ];
598 if ( iElem == ( nbHexa - 1) ) {
599 int shapeAssociated = 0;
600 for ( int i=0; i<nbDomains; i++ ) {
602 shapeAssociated += 1;
604 if ( shapeAssociated != nbShape )
605 printWarning(nbShape, "domains", shapeAssociated);
612 if ( shapeID != holeID )
614 for ( int i = 0; i < 8; ++i )
616 if ( node[i]->NbInverseElements( SMDSAbs_Face ) == 0 )
617 theMeshDS->SetNodeInVolume( node[i], shapeID );
619 aHexoticElement = theHelper->AddVolume( node[0], node[3], node[2], node[1],
620 node[4], node[7], node[6], node[5]);
621 if ( aHexoticElement->getshapeId() < 1 )
622 theMeshDS->SetMeshElementOnShape( aHexoticElement, shapeID );
628 // remove nodes in holes
631 SMESHDS_SubMesh* subMesh = 0;
632 for ( int i = 1; i <= nbNodes; ++i )
633 if ( HexoticNode[i]->NbInverseElements() == 0 )
635 theMeshDS->RemoveFreeNode( HexoticNode[i], subMesh, /*fromGroups=*/false );
640 delete [] HexoticNode;
644 //=============================================================================
646 * Pass parameters to MG-Hexa
648 //=============================================================================
650 void HexoticPlugin_Hexotic::SetParameters(const HexoticPlugin_Hypothesis* hyp) {
652 MESSAGE("HexoticPlugin_Hexotic::SetParameters");
654 _hexesMinLevel = hyp->GetHexesMinLevel();
655 _hexesMaxLevel = hyp->GetHexesMaxLevel();
656 _hexesMinSize = hyp->GetMinSize();
657 _hexesMaxSize = hyp->GetMaxSize();
658 _hexoticIgnoreRidges = hyp->GetHexoticIgnoreRidges();
659 _hexoticInvalidElements = hyp->GetHexoticInvalidElements();
660 _hexoticSharpAngleThreshold = hyp->GetHexoticSharpAngleThreshold();
661 _hexoticNbProc = hyp->GetHexoticNbProc();
662 _hexoticWorkingDirectory = hyp->GetHexoticWorkingDirectory();
663 _hexoticVerbosity = hyp->GetHexoticVerbosity();
664 _hexoticMaxMemory = hyp->GetHexoticMaxMemory();
665 _hexoticSdMode = hyp->GetHexoticSdMode();
666 _textOptions = hyp->GetAdvancedOption();
667 _sizeMaps = hyp->GetSizeMaps();
668 _nbLayers = hyp->GetNbLayers();
669 _firstLayerSize = hyp->GetFirstLayerSize();
670 _direction = hyp->GetDirection();
671 _growth = hyp->GetGrowth();
672 _facesWithLayers = hyp->GetFacesWithLayers();
673 _imprintedFaces = hyp->GetImprintedFaces();
677 cout << "WARNING : The MG-Hexa default parameters are taken into account" << std::endl;
678 cout << "=======" << std::endl;
679 _hexesMinLevel = hyp->GetDefaultHexesMinLevel();
680 _hexesMaxLevel = hyp->GetDefaultHexesMaxLevel();
681 _hexesMinSize = hyp->GetDefaultMinSize();
682 _hexesMaxSize = hyp->GetDefaultMaxSize();
683 _hexoticIgnoreRidges = hyp->GetDefaultHexoticIgnoreRidges();
684 _hexoticInvalidElements = hyp->GetDefaultHexoticInvalidElements();
685 _hexoticSharpAngleThreshold = hyp->GetDefaultHexoticSharpAngleThreshold();
686 _hexoticNbProc = hyp->GetDefaultHexoticNbProc();
687 _hexoticWorkingDirectory = hyp->GetDefaultHexoticWorkingDirectory();
688 _hexoticVerbosity = hyp->GetDefaultHexoticVerbosity();
689 _hexoticMaxMemory = hyp->GetDefaultHexoticMaxMemory();
690 _hexoticSdMode = hyp->GetDefaultHexoticSdMode();
691 _textOptions = hyp->GetDefaultTextOptions();
692 _sizeMaps = hyp->GetDefaultHexoticSizeMaps();
693 _nbLayers = hyp->GetDefaultNbLayers();
694 _firstLayerSize = hyp->GetDefaultFirstLayerSize();
695 _direction = hyp->GetDefaultDirection();
696 _growth = hyp->GetDefaultGrowth();
697 _facesWithLayers = hyp->GetDefaultFacesWithLayers();
698 _imprintedFaces = hyp->GetDefaultImprintedFaces();
702 //=======================================================================
703 //function : getSuffix
704 //purpose : Returns a suffix that will be unique for the current process
705 //=======================================================================
707 static TCollection_AsciiString getSuffix()
709 TCollection_AsciiString aSuffix = "";
712 aSuffix += getenv("USER");
714 std::string uname = std::string(getenv("USERNAME"));
715 replace(uname.begin(), uname.end(), ' ', '_');
716 aSuffix += uname.c_str();
719 aSuffix += Kernel_Utils::GetHostname().c_str();
724 aSuffix += _getpid();
730 //================================================================================
732 * \brief Returns a command to run MG-Hexa mesher
734 //================================================================================
736 std::string HexoticPlugin_Hexotic::getHexoticCommand(const TCollection_AsciiString& Hexotic_In,
737 const TCollection_AsciiString& Hexotic_Out,
738 const TCollection_AsciiString& Hexotic_SizeMap_Prefix,
739 const bool forExecutable) const
742 cout << "MG-Hexa execution..." << std::endl;
743 cout << _name << " parameters :" << std::endl;
744 cout << " " << _name << " Verbosity = " << _hexoticVerbosity << std::endl;
745 cout << " " << _name << " Max Memory = " << _hexoticMaxMemory << std::endl;
746 cout << " " << _name << " Segments Min Level = " << _hexesMinLevel << std::endl;
747 cout << " " << _name << " Segments Max Level = " << _hexesMaxLevel << std::endl;
748 cout << " " << _name << " Segments Min Size = " << _hexesMinSize << std::endl;
749 cout << " " << _name << " Segments Max Size = " << _hexesMaxSize << std::endl;
750 cout << " " << "MG-Hexa can ignore ridges : " << (_hexoticIgnoreRidges ? "yes":"no") << std::endl;
751 cout << " " << "MG-Hexa authorize invalide elements : " << ( _hexoticInvalidElements ? "yes":"no") << std::endl;
752 cout << " " << _name << " Sharp angle threshold = " << _hexoticSharpAngleThreshold << " degrees" << std::endl;
753 cout << " " << _name << " Number of threads = " << _hexoticNbProc << std::endl;
754 cout << " " << _name << " Working directory = \"" << _hexoticWorkingDirectory << "\"" << std::endl;
755 cout << " " << _name << " Sub. Dom mode = " << _hexoticSdMode << std::endl;
756 cout << " " << _name << " Text options = \"" << _textOptions << "\"" << std::endl;
757 cout << " " << _name << " Number of layers = " << _nbLayers << std::endl;
758 cout << " " << _name << " Size of the first layer = " << _firstLayerSize << std::endl;
759 cout << " " << _name << " Direction of the layers = " << ( _direction ? "Inward" : "Outward" ) << std::endl;
760 cout << " " << _name << " Growth = " << _growth << std::endl;
761 if (!_facesWithLayers.empty()) {
762 cout << " " << _name << " Faces with layers = ";
763 for (size_t i = 0; i < _facesWithLayers.size(); i++)
765 cout << _facesWithLayers.at(i);
766 if ((i + 1) != _facesWithLayers.size())
771 if (!_imprintedFaces.empty()) {
772 cout << " " << _name << " Imprinted faces = ";
773 for (size_t i = 0; i < _imprintedFaces.size(); i++)
775 cout << _imprintedFaces.at(i);
776 if ((i + 1) != _imprintedFaces.size())
782 TCollection_AsciiString run_Hexotic("mg-hexa.exe");
784 TCollection_AsciiString minl = " --min_level ", maxl = " --max_level ", angle = " --ridge_angle ";
785 TCollection_AsciiString mins = " --min_size ", maxs = " --max_size ";
786 TCollection_AsciiString in = " --in ", out = " --out ";
787 TCollection_AsciiString sizeMap = " --read_sizemap ";
788 TCollection_AsciiString ignoreRidges = " --compute_ridges no ", invalideElements = " --allow_invalid_elements yes ";
789 TCollection_AsciiString subdom = " --components ";
791 TCollection_AsciiString proc = " --max_number_of_threads ";
793 TCollection_AsciiString verb = " --verbose ";
794 TCollection_AsciiString maxmem = " --max_memory ";
796 TCollection_AsciiString comNbLayers = " --number_of_boundary_layers ";
797 TCollection_AsciiString comFirstLayerSize = " --height_of_the_first_layer ";
798 TCollection_AsciiString comDirection = " --boundary_layers_subdomain_direction ";
799 TCollection_AsciiString comGrowth = " --boundary_layers_geometric_progression ";
800 TCollection_AsciiString comFacesWithLayers = " --boundary_layers_surface_ids ";
801 TCollection_AsciiString comImptintedFaces = " --imprinted_surface_ids ";
803 TCollection_AsciiString minLevel, maxLevel, minSize, maxSize, sharpAngle, mode, nbproc, verbosity, maxMemory,
804 textOptions, nbLayers, firstLayerSize, direction, growth, facesWithLayers, imprintedFaces;
805 minLevel = _hexesMinLevel;
806 maxLevel = _hexesMaxLevel;
807 minSize = _hexesMinSize;
808 maxSize = _hexesMaxSize;
809 sharpAngle = _hexoticSharpAngleThreshold;
810 // Mode translation for mg-tetra 1.1
811 switch ( _hexoticSdMode )
814 mode = "outside_skin_only";
817 mode = "outside_components";
823 mode = "all --manifold_geometry no";
826 nbproc = _hexoticNbProc;
827 verbosity = _hexoticVerbosity;
828 maxMemory = _hexoticMaxMemory;
829 textOptions = (" " + _textOptions + " ").c_str();
830 nbLayers = _nbLayers;
831 firstLayerSize = _firstLayerSize;
832 direction = _direction ? "1" : "-1";
834 for (size_t i = 0; i < _facesWithLayers.size(); i++)
836 facesWithLayers += _facesWithLayers[i];
837 if ((i + 1) != _facesWithLayers.size())
838 facesWithLayers += ",";
840 for (size_t i = 0; i < _imprintedFaces.size(); i++)
842 imprintedFaces += _imprintedFaces[i];
843 if ((i + 1) != _imprintedFaces.size())
844 imprintedFaces += ",";
847 if (_hexoticIgnoreRidges)
848 run_Hexotic += ignoreRidges;
850 if (_hexoticInvalidElements)
851 run_Hexotic += invalideElements;
853 if (_hexesMinSize > 0)
854 run_Hexotic += mins + minSize;
856 if (_hexesMaxSize > 0)
857 run_Hexotic += maxs + maxSize;
859 if (_hexesMinLevel > 0)
860 run_Hexotic += minl + minLevel;
862 if (_hexesMaxLevel > 0)
863 run_Hexotic += maxl + maxLevel;
865 if (_hexoticSharpAngleThreshold > 0)
866 run_Hexotic += angle + sharpAngle;
868 if (_sizeMaps.begin() != _sizeMaps.end() && forExecutable )
869 run_Hexotic += sizeMap + Hexotic_SizeMap_Prefix;
872 _firstLayerSize > 0 &&
874 !_facesWithLayers.empty())
876 run_Hexotic += comNbLayers + nbLayers;
877 run_Hexotic += comFirstLayerSize + firstLayerSize;
878 run_Hexotic += comDirection + direction;
879 run_Hexotic += comGrowth + growth;
880 run_Hexotic += comFacesWithLayers + facesWithLayers;
881 if (!_imprintedFaces.empty())
882 run_Hexotic += comImptintedFaces + imprintedFaces;
885 run_Hexotic += in + Hexotic_In + out + Hexotic_Out;
886 run_Hexotic += subdom + mode;
888 run_Hexotic += proc + nbproc;
890 run_Hexotic += verb + verbosity;
891 run_Hexotic += maxmem + maxMemory;
893 if (!_textOptions.empty())
894 run_Hexotic += textOptions;
896 return run_Hexotic.ToCString();
899 // TODO : this is a duplication of some code found in BLSURFPlugin_BLSURF find a proper
901 TopoDS_Shape HexoticPlugin_Hexotic::entryToShape(std::string entry)
903 MESSAGE("HexoticPlugin_Hexotic::entryToShape "<<entry );
905 GEOM::GEOM_Object_var aGeomObj;
906 TopoDS_Shape S = TopoDS_Shape();
907 SALOMEDS::SObject_var aSObj = SMESH_Gen_i::getStudyServant()->FindObjectID( entry.c_str() );
908 if (!aSObj->_is_nil()) {
909 CORBA::Object_var obj = aSObj->GetObject();
910 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
913 if ( !aGeomObj->_is_nil() )
914 S = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( aGeomObj.in() );
918 //================================================================================
920 * \brief Produces a .mesh file with the size maps informations to give to Hexotic
922 //================================================================================
923 std::vector<std::string> HexoticPlugin_Hexotic::writeSizeMapFile( MG_Hexotic_API* mgInput,
924 std::string sizeMapPrefix )
926 HexoticPlugin_Hypothesis::THexoticSizeMaps::iterator it;
928 std::vector<ControlPnt> points;
929 // Iterate on the size maps
930 for (it=_sizeMaps.begin(); it!=_sizeMaps.end(); it++)
932 // Step 1 : Get the GEOM object entry and the size
933 // from the _sizeMaps infos
934 std::string anEntry = it->first;
935 double aLocalSize = it->second;
936 TopoDS_Shape aShape = entryToShape( anEntry );
938 // Step 2 : Create the points
939 createControlPoints( aShape, aLocalSize, points );
942 // Write the .mesh size map file
944 std::string myVerticesFile = sizeMapPrefix + ".mesh";
945 std::string mySolFile = sizeMapPrefix + ".sol";
949 mgInput->GmfOpenMesh( myVerticesFile.c_str(), GmfWrite, GMFVERSION, GMFDIMENSION );
951 mgInput->GmfOpenMesh( mySolFile.c_str(), GmfWrite, GMFVERSION, GMFDIMENSION );
953 int pointsNumber = points.size();
956 mgInput->GmfSetKwd( verticesFileID, GmfVertices, pointsNumber );
957 // SolAtVertices Keyword
958 int TypTab[] = {GmfSca};
959 mgInput->GmfSetKwd(solFileID, GmfSolAtVertices, pointsNumber, 1, TypTab);
961 // Read the control points information from the vector and write it into the files
963 std::vector<ControlPnt>::const_iterator points_it;
964 for (points_it = points.begin(); points_it != points.end(); points_it++ )
966 mgInput->GmfSetLin( verticesFileID, GmfVertices, points_it->X(), points_it->Y(), points_it->Z(), 0 );
967 ValTab[0] = points_it->Size();
968 mgInput->GmfSetLin( solFileID, GmfSolAtVertices, ValTab);
972 mgInput->GmfCloseMesh( verticesFileID );
973 mgInput->GmfCloseMesh( solFileID );
975 std::vector<std::string> fileNames(2);
976 fileNames[0] = myVerticesFile;
977 fileNames[1] = mySolFile;
982 //=============================================================================
984 * Here we are going to use the MG-Hexa mesher
986 //=============================================================================
988 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh& aMesh,
989 const TopoDS_Shape& aShape)
991 _computeCanceled = false;
993 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
994 TCollection_AsciiString hexahedraMessage;
996 _nbShape = countShape( &aMesh, TopAbs_SOLID ); // we count the number of shapes
999 // create bounding box for each shape of the compound
1002 TopoDS_Shape *tabShape;
1005 tabShape = new TopoDS_Shape[_nbShape];
1006 tabBox = new double*[_nbShape];
1007 for (int i=0; i<_nbShape; i++)
1008 tabBox[i] = new double[6];
1009 double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1011 TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID);
1012 for (; expBox.More(); expBox.Next()) {
1013 tabShape[iShape] = expBox.Current();
1014 Bnd_Box BoundingBox;
1015 BRepBndLib::Add(expBox.Current(), BoundingBox);
1016 BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
1017 tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
1018 tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
1019 tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
1023 SetParameters(_hypothesis);
1025 TCollection_AsciiString aTmpDir = _hexoticWorkingDirectory.c_str();
1026 TCollection_AsciiString aQuote("");
1029 if ( aTmpDir.Value(aTmpDir.Length()) != '\\' ) aTmpDir += '\\';
1031 if ( aTmpDir.Value(aTmpDir.Length()) != '/' ) aTmpDir += '/';
1033 TCollection_AsciiString Hexotic_In(""), Hexotic_Out, Hexotic_SizeMap_Prefix;
1034 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1035 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic"+getSuffix()+".log"; // log
1037 std::map <int,int> aSmdsToHexoticIdMap;
1038 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1040 MG_Hexotic_API mgHexa( _computeCanceled, _progress );
1042 Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
1043 #ifdef WITH_BLSURFPLUGIN
1044 bool defaultInputFile = true;
1045 if (_blsurfHypo && _blsurfHypo->GetElementType() == BLSURFPlugin_Hypothesis::Triangles ) {
1046 Hexotic_In = _blsurfHypo->GetGMFFile().c_str();
1047 if ( !Hexotic_In.IsEmpty() &&
1048 SMESH_File( _blsurfHypo->GetGMFFile() ).exists() )
1050 MESSAGE("Use output file from blsurf as input file from hexotic: " << Hexotic_In);
1051 mgHexa.SetUseExecutable();
1052 defaultInputFile = false;
1055 if (defaultInputFile) {
1057 Hexotic_In = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
1058 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1059 splitQuads(aMesh); // quadrangles are no longer acceptable as input
1060 if ( mgHexa.IsExecutable() )
1063 cout << "Creating MG-Hexa input mesh file : " << Hexotic_In << std::endl;
1065 writeInput( &mgHexa, Hexotic_In.ToCString(), meshDS );
1066 #ifdef WITH_BLSURFPLUGIN
1069 removeFile( Hexotic_Out );
1073 Hexotic_SizeMap_Prefix = aTmpDir + "Hexotic_SizeMap" + getSuffix();
1074 std::vector<std::string> sizeMapFiles = writeSizeMapFile( &mgHexa, Hexotic_SizeMap_Prefix.ToCString() );
1076 std::string run_Hexotic = getHexoticCommand(aQuote + Hexotic_In + aQuote, aQuote + Hexotic_Out + aQuote, Hexotic_SizeMap_Prefix, mgHexa.IsExecutable() );
1077 run_Hexotic += std::string(" 1> ") + aQuote.ToCString() + aLogFileName.ToCString() + aQuote.ToCString(); // dump into file
1078 mgHexa.SetLogFile( aLogFileName.ToCString() );
1079 cout << "Creating MG-Hexa log file : " << aLogFileName << std::endl;
1082 cout << "MG-Hexa command : " << run_Hexotic << std::endl;
1084 if ( mgHexa.IsExecutable() )
1087 modeFile_In += Hexotic_In;
1088 system( modeFile_In.ToCString() );
1091 aSmdsToHexoticIdMap.clear();
1092 aHexoticIdToNodeMap.clear();
1094 MESSAGE("HexoticPlugin_Hexotic::Compute");
1098 Ok = mgHexa.Compute( run_Hexotic, errStr ); // run
1105 if ( mgHexa.IsExecutable() )
1108 modeFile_Out += Hexotic_Out;
1109 system( modeFile_Out.ToCString() );
1112 SMESH_MesherHelper aHelper( aMesh );
1114 Ok = readResult( &mgHexa, Hexotic_Out.ToCString(),
1116 &aHelper, _nbShape, tabShape, tabBox );
1118 std::string log = mgHexa.GetLog();
1121 hexahedraMessage = "success";
1123 removeFile(Hexotic_Out);
1124 removeFile(Hexotic_In);
1125 //removeFile(aLogFileName);
1126 for( size_t i=0; i<sizeMapFiles.size(); i++)
1128 removeFile( TCollection_AsciiString( sizeMapFiles[i].c_str() ) );
1134 hexahedraMessage = "failed";
1135 if ( mgHexa.IsExecutable() )
1136 cout << "Problem with MG-Hexa output file " << Hexotic_Out.ToCString() << std::endl;
1140 char msgLic[] = " Dlim ";
1141 std::string log = mgHexa.GetLog();
1142 const char* fileBeg = &log[0], *fileEnd = fileBeg + log.size();
1143 if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1144 error("Licence problems.");
1146 if ( !errStr.empty() )
1149 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1153 for (int i=0; i<_nbShape; i++)
1154 delete [] tabBox[i];
1160 if(_computeCanceled)
1161 return error(SMESH_Comment("interruption initiated by user"));
1166 //=============================================================================
1168 * \brief Computes mesh without geometry
1169 * \param aMesh - the mesh
1170 * \param aHelper - helper that must be used for adding elements to \aaMesh
1171 * \retval bool - is a success
1173 * The method is called if ( !aMesh->HasShapeToMesh() )
1175 //=============================================================================
1177 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
1179 _computeCanceled = false;
1181 SMESH_ComputeErrorPtr myError = SMESH_ComputeError::New();
1184 TCollection_AsciiString hexahedraMessage;
1185 TCollection_AsciiString aQuote("");
1189 SetParameters(_hypothesis);
1191 TCollection_AsciiString aTmpDir = _hexoticWorkingDirectory.c_str();//getTmpDir();
1192 TCollection_AsciiString Hexotic_In, Hexotic_Out, Hexotic_SizeMap_Prefix;
1193 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1194 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic"+getSuffix()+".log"; // log
1196 std::map <int,int> aSmdsToHexoticIdMap;
1197 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1199 Hexotic_In = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
1200 Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
1201 Hexotic_SizeMap_Prefix = aTmpDir + "Hexotic_SizeMap";
1203 MG_Hexotic_API mgHexa( _computeCanceled, _progress );
1205 std::vector<std::string> sizeMapFiles = writeSizeMapFile( &mgHexa, Hexotic_SizeMap_Prefix.ToCString() );
1207 std::string run_Hexotic = getHexoticCommand(aQuote + Hexotic_In + aQuote, aQuote + Hexotic_Out + aQuote, Hexotic_SizeMap_Prefix, mgHexa.IsExecutable());
1208 run_Hexotic += std::string(" 1> ") + aQuote.ToCString() + aLogFileName.ToCString() + aQuote.ToCString(); // dump into file
1209 mgHexa.SetLogFile( aLogFileName.ToCString() );
1210 cout << "Creating MG-Hexa log file : " << aLogFileName << std::endl;
1212 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1214 splitQuads(aMesh); // quadrangles are no longer acceptable as input
1217 cout << "Creating MG-Hexa input mesh file : " << Hexotic_In << std::endl;
1218 writeInput( &mgHexa, Hexotic_In.ToCString(), aHelper->GetMeshDS() );
1219 if ( mgHexa.IsExecutable() )
1222 modeFile_In += Hexotic_In;
1223 system( modeFile_In.ToCString() );
1226 aSmdsToHexoticIdMap.clear();
1227 aHexoticIdToNodeMap.clear();
1229 MESSAGE("HexoticPlugin_Hexotic::Compute");
1232 cout << "MG-Hexa command : " << run_Hexotic << std::endl;
1235 Ok = mgHexa.Compute( run_Hexotic, errStr ); // run
1241 if ( mgHexa.IsExecutable() )
1243 modeFile_Out += Hexotic_Out;
1244 system( modeFile_Out.ToCString() );
1247 Ok = Ok && readResult( &mgHexa, Hexotic_Out.ToCString(), this, aHelper );
1249 std::string log = mgHexa.GetLog();
1252 hexahedraMessage = "success";
1256 hexahedraMessage = "failed";
1257 if ( mgHexa.IsExecutable() )
1258 cout << "Problem with MG-Hexa output file " << Hexotic_Out << std::endl;
1260 if ( log.find( " license " ) != std::string::npos ||
1261 log.find( " Dlim " ) != std::string::npos )
1262 error("License problems.");
1264 if ( !errStr.empty() )
1267 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1270 if(_computeCanceled)
1271 return error(SMESH_Comment("interruption initiated by user"));
1272 removeFile(Hexotic_Out);
1273 removeFile(Hexotic_In);
1275 removeFile(aLogFileName);
1276 for( size_t i=0; i<sizeMapFiles.size(); i++)
1278 removeFile( TCollection_AsciiString(sizeMapFiles[i].c_str()) );
1283 //=============================================================================
1287 //=============================================================================
1289 bool HexoticPlugin_Hexotic::Evaluate(SMESH_Mesh& aMesh,
1290 const TopoDS_Shape& aShape,
1291 MapShapeNbElems& aResMap)
1293 std::vector<int> aResVec(SMDSEntity_Last);
1294 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1295 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1296 aResMap.insert(std::make_pair(sm,aResVec));
1298 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1299 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Evaluation is not implemented",this));
1304 void HexoticPlugin_Hexotic::CancelCompute()
1306 _computeCanceled = true;
1309 TCollection_AsciiString aTmpDir = _hexoticWorkingDirectory.c_str(); //getTmpDir();
1310 TCollection_AsciiString Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1311 TCollection_AsciiString cmd = TCollection_AsciiString("ps ux | grep ") + Hexotic_In;
1312 cmd += TCollection_AsciiString(" | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1");
1313 system( cmd.ToCString() );