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_ControlPnt.hxx>
55 #include <Standard_ProgramError.hxx>
57 #include <BRepBndLib.hxx>
58 #include <BRepClass3d_SolidClassifier.hxx>
59 #include <BRep_Tool.hxx>
60 #include <Bnd_Box.hxx>
61 #include <OSD_File.hxx>
62 #include <Precision.hxx>
63 #include <TopExp_Explorer.hxx>
64 #include <TopTools_MapOfShape.hxx>
66 #include <TopoDS_Vertex.hxx>
69 #include <Basics_Utils.hxx>
70 #include <GEOMImpl_Types.hxx>
71 #include <GEOM_wrap.hxx>
73 #define GMFVERSION GmfDouble
74 #define GMFDIMENSION 3
76 using SMESHUtils::ControlPnt;
78 static void removeFile( const TCollection_AsciiString& fileName )
81 OSD_File( fileName ).Remove();
83 catch ( Standard_ProgramError ) {
84 MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
88 //=============================================================================
92 //=============================================================================
94 HexoticPlugin_Hexotic::HexoticPlugin_Hexotic(int hypId, SMESH_Gen* gen)
95 : SMESH_3D_Algo(hypId, gen)
97 MESSAGE("HexoticPlugin_Hexotic::HexoticPlugin_Hexotic");
99 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
100 _onlyUnaryInput = false;
101 _requireShape = false;
104 _hexoticFilesKept=false;
105 _compatibleHypothesis.push_back( HexoticPlugin_Hypothesis::GetHypType() );
106 #ifdef WITH_BLSURFPLUGIN
109 _computeCanceled = false;
113 //=============================================================================
117 //=============================================================================
119 HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic()
121 MESSAGE("HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic");
125 #ifdef WITH_BLSURFPLUGIN
126 bool HexoticPlugin_Hexotic::CheckBLSURFHypothesis( SMESH_Mesh& aMesh,
127 const TopoDS_Shape& aShape )
129 // MESSAGE("HexoticPlugin_Hexotic::CheckBLSURFHypothesis");
132 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
133 const SMESHDS_Hypothesis* theHyp;
135 // If a BLSURF hypothesis is applied, get it
136 SMESH_HypoFilter blsurfFilter;
137 blsurfFilter.Init( blsurfFilter.HasName( BLSURFPlugin_Hypothesis::GetHypType() ));
138 std::list<const SMESHDS_Hypothesis *> appliedHyps;
139 aMesh.GetHypotheses( aShape, blsurfFilter, appliedHyps, false );
141 if ( appliedHyps.size() > 0 ) {
142 itl = appliedHyps.begin();
143 theHyp = (*itl); // use only the first hypothesis
144 std::string hypName = theHyp->GetName();
145 if (hypName == BLSURFPlugin_Hypothesis::GetHypType()) {
146 _blsurfHypo = static_cast<const BLSURFPlugin_Hypothesis*> (theHyp);
155 //=============================================================================
159 //=============================================================================
161 bool HexoticPlugin_Hexotic::CheckHypothesis( SMESH_Mesh& aMesh,
162 const TopoDS_Shape& aShape,
163 SMESH_Hypothesis::Hypothesis_Status& aStatus )
165 // MESSAGE("HexoticPlugin_Hexotic::CheckHypothesis");
168 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
169 const SMESHDS_Hypothesis* theHyp;
171 const std::list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
172 int nbHyp = hyps.size();
174 aStatus = SMESH_Hypothesis::HYP_OK;
175 return true; // can work with no hypothesis
179 theHyp = (*itl); // use only the first hypothesis
181 std::string hypName = theHyp->GetName();
182 if (hypName == HexoticPlugin_Hypothesis::GetHypType() ) {
183 _hypothesis = static_cast<const HexoticPlugin_Hypothesis*> (theHyp);
185 aStatus = SMESH_Hypothesis::HYP_OK;
188 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
190 #ifdef WITH_BLSURFPLUGIN
191 CheckBLSURFHypothesis(aMesh, aShape);
194 return aStatus == SMESH_Hypothesis::HYP_OK;
197 //=======================================================================
198 //function : findShape
200 //=======================================================================
202 static TopoDS_Shape findShape(SMDS_MeshNode** t_Node,
204 const TopoDS_Shape* t_Shape,
209 int iShape, nbNode = 8;
211 for ( int i=0; i<3; i++ ) {
213 for ( int j=0; j<nbNode; j++ ) {
214 if ( i == 0) pntCoor[i] += t_Node[j]->X();
215 if ( i == 1) pntCoor[i] += t_Node[j]->Y();
216 if ( i == 2) pntCoor[i] += t_Node[j]->Z();
218 pntCoor[i] /= nbNode;
220 gp_Pnt aPnt(pntCoor[0], pntCoor[1], pntCoor[2]);
222 if ( aShape.IsNull() ) aShape = t_Shape[0];
223 BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
224 if ( !(SC.State() == TopAbs_IN) ) {
226 for (iShape = 0; iShape < nShape && aShape.IsNull(); iShape++) {
227 if ( !( pntCoor[0] < t_Box[iShape][0] || t_Box[iShape][1] < pntCoor[0] ||
228 pntCoor[1] < t_Box[iShape][2] || t_Box[iShape][3] < pntCoor[1] ||
229 pntCoor[2] < t_Box[iShape][4] || t_Box[iShape][5] < pntCoor[2]) ) {
230 BRepClass3d_SolidClassifier SC (t_Shape[iShape], aPnt, Precision::Confusion());
231 if (SC.State() == TopAbs_IN)
232 aShape = t_Shape[iShape];
239 //=======================================================================
240 //function : getNbShape
242 //=======================================================================
244 static int getNbShape(MG_Hexotic_API* hexaOutput, int iMesh, GmfKwdCod what, int defaultValue=0)
246 int number = hexaOutput->GmfStatKwd( iMesh, what );
249 // std::string aLine;
250 // std::ifstream file(aFile.c_str());
251 // while ( !file.eof() ) {
252 // getline( file, aLine);
253 // if ( aLine == aString ) {
254 // getline( file, aLine);
255 // std::istringstream stringFlux( aLine );
256 // stringFlux >> number;
257 number = ( number + defaultValue + std::abs(number - defaultValue) ) / 2;
263 number = defaultValue;
269 //=======================================================================
270 //function : countShape
272 //=======================================================================
274 static int countShape( SMESH_Mesh* mesh, TopAbs_ShapeEnum shape )
276 if ( !mesh->HasShapeToMesh() )
278 TopExp_Explorer expShape ( mesh->GetShapeToMesh(), shape );
279 TopTools_MapOfShape mapShape;
281 for ( ; expShape.More(); expShape.Next() ) {
282 if (mapShape.Add(expShape.Current())) {
289 //=======================================================================
290 //function : getShape
292 //=======================================================================
294 template < class Mesh, class Shape, class Tab >
295 void getShape(Mesh* mesh, Shape shape, Tab *t_Shape)
297 TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
298 TopTools_MapOfShape mapShape;
299 for ( int i=0; expShape.More(); expShape.Next() ) {
300 if (mapShape.Add(expShape.Current())) {
301 t_Shape[i] = expShape.Current();
308 //=======================================================================
309 //function : printWarning
311 //=======================================================================
313 static void printWarning(const int nbExpected, std::string aString, const int nbFound) {
315 cout << "WARNING : " << nbExpected << " " << aString << " expected, MG-Hexa has found " << nbFound << std::endl;
316 cout << "=======" << std::endl;
321 //=======================================================================
322 //function : removeHexoticFiles
324 //=======================================================================
326 static void removeHexoticFiles(TCollection_AsciiString file_In, TCollection_AsciiString file_Out) {
327 removeFile( file_In );
328 removeFile( file_Out );
331 //=======================================================================
332 //function : splitQuads
333 //purpose : splits all quadrangles into triangles
334 //=======================================================================
336 static void splitQuads(SMESH_Mesh& aMesh)
338 SMESH_MeshEditor spliter( &aMesh );
340 TIDSortedElemSet elems;
341 SMDS_ElemIteratorPtr eIt = aMesh.GetMeshDS()->elementsIterator();
343 elems.insert( elems.end(), eIt->next() );
345 spliter.QuadToTri ( elems, /*the13Diag=*/true);
348 //=======================================================================
349 //function : writeInput
350 //purpose : pass a mesh to input of MG-Hexa
351 //=======================================================================
353 static void writeInput(MG_Hexotic_API* theHexaInput,
355 const SMESHDS_Mesh* theMeshDS)
357 int meshID = theHexaInput->GmfOpenMesh( theFile, GmfWrite, GMFVERSION, GMFDIMENSION);
360 int iN = 0, nbNodes = theMeshDS->NbNodes();
361 theHexaInput->GmfSetKwd( meshID, GmfVertices, nbNodes );
362 std::map< const SMDS_MeshNode*, int, TIDCompare > node2IdMap;
363 SMDS_NodeIteratorPtr nodeIt = theMeshDS->nodesIterator();
365 while ( nodeIt->more() )
367 n.Set( nodeIt->next() );
368 theHexaInput->GmfSetLin( meshID, GmfVertices, n.X(), n.Y(), n.Z(), n._node->getshapeId() );
369 node2IdMap.insert( node2IdMap.end(), std::make_pair( n._node, ++iN ));
373 SMDS_ElemIteratorPtr elemIt = theMeshDS->elementsIterator( SMDSAbs_Edge );
374 if ( elemIt->more() )
376 int nbEdges = theMeshDS->GetMeshInfo().NbElements( SMDSAbs_Edge );
377 theHexaInput->GmfSetKwd(meshID, GmfEdges, nbEdges );
378 for ( int gmfID = 1; elemIt->more(); ++gmfID )
380 const SMDS_MeshElement* edge = elemIt->next();
381 theHexaInput->GmfSetLin(meshID, GmfEdges,
382 node2IdMap[ edge->GetNode( 0 )],
383 node2IdMap[ edge->GetNode( 1 )],
384 edge->getshapeId() );
389 elemIt = theMeshDS->elementGeomIterator( SMDSGeom_TRIANGLE );
390 if ( elemIt->more() )
392 int nbTria = theMeshDS->GetMeshInfo().NbElements( SMDSGeom_TRIANGLE );
393 theHexaInput->GmfSetKwd(meshID, GmfTriangles, nbTria );
394 for ( int gmfID = 1; elemIt->more(); ++gmfID )
396 const SMDS_MeshElement* tria = elemIt->next();
397 theHexaInput->GmfSetLin(meshID, GmfTriangles,
398 node2IdMap[ tria->GetNode( 0 )],
399 node2IdMap[ tria->GetNode( 1 )],
400 node2IdMap[ tria->GetNode( 2 )],
401 tria->getshapeId() );
404 theHexaInput->GmfCloseMesh( meshID );
407 //=======================================================================
408 //function : readResult
409 //purpose : Read GMF file in case of a mesh with geometry
410 //=======================================================================
412 static bool readResult(MG_Hexotic_API* theHexaOutput,
414 HexoticPlugin_Hexotic*theAlgo,
415 SMESH_MesherHelper* theHelper,
416 const int nbShape = 0,
417 const TopoDS_Shape* tabShape = 0,
420 SMESH_Mesh* theMesh = theHelper->GetMesh();
421 SMESHDS_Mesh* theMeshDS = theHelper->GetMeshDS();
423 // ---------------------------------
424 // Read generated elements and nodes
425 // ---------------------------------
428 TopoDS_Vertex aVertex;
430 int shapeID, hexoticShapeID;
431 const int IdShapeRef = 2;
433 double epsilon = Precision::Confusion();
434 std::map <std::string,int> mapField;
435 SMDS_MeshNode** HexoticNode;
436 TopoDS_Shape *tabCorner;
438 const int nbDomains = countShape( theMesh, TopAbs_SHELL );
439 const int holeID = -1;
443 tabID = new int[nbDomains];
445 for (int i=0; i<nbDomains; i++)
447 if ( nbDomains == 1 )
448 tabID[0] = theMeshDS->ShapeToIndex( tabShape[0] );
456 SMDS_ElemIteratorPtr eIt = theMeshDS->elementsIterator();
458 theMeshDS->RemoveFreeElement( eIt->next(), /*sm=*/0 );
459 SMDS_NodeIteratorPtr nIt = theMeshDS->nodesIterator();
460 while ( nIt->more() )
461 theMeshDS->RemoveFreeNode( nIt->next(), /*sm=*/0 );
463 theHelper->SetElementsOnShape( false );
466 int meshID = theHexaOutput->GmfOpenMesh( theFile, GmfRead, &ver, &dim );
468 int nbVertices = getNbShape(theHexaOutput, meshID, GmfVertices );
469 int nbCorners = getNbShape(theHexaOutput, meshID, GmfCorners, countShape( theMesh, TopAbs_VERTEX ));
470 if ( nbVertices == 0 )
473 tabCorner = new TopoDS_Shape[ nbCorners ];
474 HexoticNode = new SMDS_MeshNode*[ nbVertices + 1 ];
477 getShape(theMeshDS, TopAbs_VERTEX, tabCorner);
479 int nbNodes = theHexaOutput->GmfStatKwd( meshID, GmfVertices );
482 theHexaOutput->GmfGotoKwd( meshID, GmfVertices );
484 for ( int aHexoticID = 1; aHexoticID <= nbNodes; ++aHexoticID )
486 if ( theAlgo->computeCanceled() )
488 theHexaOutput->GmfGetLin( meshID, GmfVertices, &x, &y, &z, &shapeID );
489 HexoticNode[ aHexoticID ] = theHelper->AddNode( x,y,z );
494 SMDS_MeshNode* node[8];
495 SMDS_MeshElement * aHexoticElement;
497 nbCorners = theHexaOutput->GmfStatKwd( meshID, GmfCorners );
498 if ( nbCorners > 0 && nbDomains > 0 )
500 theHexaOutput->GmfGotoKwd( meshID, GmfCorners );
501 for ( int iElem = 0; iElem < nbCorners; iElem++ )
503 if ( theAlgo->computeCanceled() )
505 theHexaOutput->GmfGetLin( meshID, GmfCorners, &nodeID[0] );
506 node[0] = HexoticNode[ nodeID[0] ];
507 gp_Pnt HexoticPnt ( node[0]->X(), node[0]->Y(), node[0]->Z() );
508 for ( int i = 0; i < nbCorners; i++ )
510 aVertex = TopoDS::Vertex( tabCorner[i] );
511 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
512 if ( aPnt.Distance( HexoticPnt ) < epsilon )
514 theMeshDS->SetNodeOnVertex( node[0], aVertex );
521 int nbEdges = theHexaOutput->GmfStatKwd( meshID, GmfEdges );
524 theHexaOutput->GmfGotoKwd( meshID, GmfEdges );
525 for ( int iElem = 0; iElem < nbEdges; iElem++ )
527 if ( theAlgo->computeCanceled() )
529 theHexaOutput->GmfGetLin( meshID, GmfEdges, &nodeID[0], &nodeID[1], &shapeID );
530 for ( int i = 0; i < 2; ++i )
532 node[i] = HexoticNode[ nodeID[i]];
533 if ( shapeID > 0 && node[i]->getshapeId() < 1 )
534 theMeshDS->SetNodeOnEdge( node[i], shapeID );
536 aHexoticElement = theHelper->AddEdge( node[0], node[1] );
537 if ( shapeID > 0 && aHexoticElement->getshapeId() < 1 )
538 theMeshDS->SetMeshElementOnShape( aHexoticElement, shapeID );
542 int nbQuad = theHexaOutput->GmfStatKwd( meshID, GmfQuadrilaterals );
545 theHexaOutput->GmfGotoKwd( meshID, GmfQuadrilaterals );
546 for ( int iElem = 0; iElem < nbQuad; iElem++ )
548 if ( theAlgo->computeCanceled() )
550 theHexaOutput->GmfGetLin( meshID, GmfQuadrilaterals,
551 &nodeID[0], &nodeID[1], &nodeID[2], &nodeID[3], &shapeID );
552 for ( int i = 0; i < 4; ++i )
554 node[i] = HexoticNode[ nodeID[i]];
555 if ( shapeID > 0 && node[i]->getshapeId() < 1 )
556 theMeshDS->SetNodeOnFace( node[i], shapeID );
558 aHexoticElement = theHelper->AddFace( node[0], node[1], node[2], node[3] );
559 if ( shapeID > 0 && aHexoticElement->getshapeId() < 1 )
560 theMeshDS->SetMeshElementOnShape( aHexoticElement, shapeID );
564 int nbHexa = theHexaOutput->GmfStatKwd( meshID, GmfHexahedra );
567 theHexaOutput->GmfGotoKwd( meshID, GmfHexahedra );
568 for ( int iElem = 0; iElem < nbHexa; iElem++ )
570 if ( theAlgo->computeCanceled() )
572 theHexaOutput->GmfGetLin( meshID, GmfHexahedra,
573 &nodeID[0], &nodeID[1], &nodeID[2], &nodeID[3],
574 &nodeID[4], &nodeID[5], &nodeID[6], &nodeID[7],
576 for ( int i = 0; i < 8; ++i )
578 node[i] = HexoticNode[ nodeID[i]];
580 if ( nbDomains > 1 ) {
581 hexoticShapeID = shapeID - IdShapeRef;
582 if ( tabID[ hexoticShapeID ] == 0 ) {
583 aShape = findShape(node, aShape, tabShape, tabBox, nbShape);
584 shapeID = aShape.IsNull() ? holeID : theMeshDS->ShapeToIndex( aShape );
585 tabID[ hexoticShapeID ] = shapeID;
588 shapeID = tabID[ hexoticShapeID ];
590 if ( iElem == ( nbHexa - 1) ) {
591 int shapeAssociated = 0;
592 for ( int i=0; i<nbDomains; i++ ) {
594 shapeAssociated += 1;
596 if ( shapeAssociated != nbShape )
597 printWarning(nbShape, "domains", shapeAssociated);
604 if ( shapeID != holeID )
606 for ( int i = 0; i < 8; ++i )
608 if ( node[i]->NbInverseElements( SMDSAbs_Face ) == 0 )
609 theMeshDS->SetNodeInVolume( node[i], shapeID );
611 aHexoticElement = theHelper->AddVolume( node[0], node[3], node[2], node[1],
612 node[4], node[7], node[6], node[5]);
613 if ( aHexoticElement->getshapeId() < 1 )
614 theMeshDS->SetMeshElementOnShape( aHexoticElement, shapeID );
620 // remove nodes in holes
623 SMESHDS_SubMesh* subMesh = 0;
624 for ( int i = 1; i <= nbNodes; ++i )
625 if ( HexoticNode[i]->NbInverseElements() == 0 )
627 theMeshDS->RemoveFreeNode( HexoticNode[i], subMesh, /*fromGroups=*/false );
632 delete [] HexoticNode;
636 //=============================================================================
638 * Pass parameters to MG-Hexa
640 //=============================================================================
642 void HexoticPlugin_Hexotic::SetParameters(const HexoticPlugin_Hypothesis* hyp) {
644 MESSAGE("HexoticPlugin_Hexotic::SetParameters");
646 _hexesMinLevel = hyp->GetHexesMinLevel();
647 _hexesMaxLevel = hyp->GetHexesMaxLevel();
648 _hexesMinSize = hyp->GetMinSize();
649 _hexesMaxSize = hyp->GetMaxSize();
650 _hexoticIgnoreRidges = hyp->GetHexoticIgnoreRidges();
651 _hexoticInvalidElements = hyp->GetHexoticInvalidElements();
652 _hexoticSharpAngleThreshold = hyp->GetHexoticSharpAngleThreshold();
653 _hexoticNbProc = hyp->GetHexoticNbProc();
654 _hexoticWorkingDirectory = hyp->GetHexoticWorkingDirectory();
655 _hexoticVerbosity = hyp->GetHexoticVerbosity();
656 _hexoticMaxMemory = hyp->GetHexoticMaxMemory();
657 _hexoticSdMode = hyp->GetHexoticSdMode();
658 _textOptions = hyp->GetAdvancedOption();
659 _sizeMaps = hyp->GetSizeMaps();
660 _nbLayers = hyp->GetNbLayers();
661 _firstLayerSize = hyp->GetFirstLayerSize();
662 _direction = hyp->GetDirection();
663 _growth = hyp->GetGrowth();
664 _facesWithLayers = hyp->GetFacesWithLayers();
665 _imprintedFaces = hyp->GetImprintedFaces();
669 cout << "WARNING : The MG-Hexa default parameters are taken into account" << std::endl;
670 cout << "=======" << std::endl;
671 _hexesMinLevel = hyp->GetDefaultHexesMinLevel();
672 _hexesMaxLevel = hyp->GetDefaultHexesMaxLevel();
673 _hexesMinSize = hyp->GetDefaultMinSize();
674 _hexesMaxSize = hyp->GetDefaultMaxSize();
675 _hexoticIgnoreRidges = hyp->GetDefaultHexoticIgnoreRidges();
676 _hexoticInvalidElements = hyp->GetDefaultHexoticInvalidElements();
677 _hexoticSharpAngleThreshold = hyp->GetDefaultHexoticSharpAngleThreshold();
678 _hexoticNbProc = hyp->GetDefaultHexoticNbProc();
679 _hexoticWorkingDirectory = hyp->GetDefaultHexoticWorkingDirectory();
680 _hexoticVerbosity = hyp->GetDefaultHexoticVerbosity();
681 _hexoticMaxMemory = hyp->GetDefaultHexoticMaxMemory();
682 _hexoticSdMode = hyp->GetDefaultHexoticSdMode();
683 _textOptions = hyp->GetDefaultTextOptions();
684 _sizeMaps = hyp->GetDefaultHexoticSizeMaps();
685 _nbLayers = hyp->GetDefaultNbLayers();
686 _firstLayerSize = hyp->GetDefaultFirstLayerSize();
687 _direction = hyp->GetDefaultDirection();
688 _growth = hyp->GetDefaultGrowth();
689 _facesWithLayers = hyp->GetDefaultFacesWithLayers();
690 _imprintedFaces = hyp->GetDefaultImprintedFaces();
694 //=======================================================================
695 //function : getSuffix
696 //purpose : Returns a suffix that will be unique for the current process
697 //=======================================================================
699 static TCollection_AsciiString getSuffix()
701 TCollection_AsciiString aSuffix = "";
704 aSuffix += getenv("USER");
706 std::string uname = std::string(getenv("USERNAME"));
707 replace(uname.begin(), uname.end(), ' ', '_');
708 aSuffix += uname.c_str();
711 aSuffix += Kernel_Utils::GetHostname().c_str();
716 aSuffix += _getpid();
722 //================================================================================
724 * \brief Returns a command to run MG-Hexa mesher
726 //================================================================================
728 std::string HexoticPlugin_Hexotic::getHexoticCommand(const TCollection_AsciiString& Hexotic_In,
729 const TCollection_AsciiString& Hexotic_Out,
730 const TCollection_AsciiString& Hexotic_SizeMap_Prefix,
731 const bool forExecutable) const
734 cout << "MG-Hexa execution..." << std::endl;
735 cout << _name << " parameters :" << std::endl;
736 cout << " " << _name << " Verbosity = " << _hexoticVerbosity << std::endl;
737 cout << " " << _name << " Max Memory = " << _hexoticMaxMemory << std::endl;
738 cout << " " << _name << " Segments Min Level = " << _hexesMinLevel << std::endl;
739 cout << " " << _name << " Segments Max Level = " << _hexesMaxLevel << std::endl;
740 cout << " " << _name << " Segments Min Size = " << _hexesMinSize << std::endl;
741 cout << " " << _name << " Segments Max Size = " << _hexesMaxSize << std::endl;
742 cout << " " << "MG-Hexa can ignore ridges : " << (_hexoticIgnoreRidges ? "yes":"no") << std::endl;
743 cout << " " << "MG-Hexa authorize invalide elements : " << ( _hexoticInvalidElements ? "yes":"no") << std::endl;
744 cout << " " << _name << " Sharp angle threshold = " << _hexoticSharpAngleThreshold << " degrees" << std::endl;
745 cout << " " << _name << " Number of threads = " << _hexoticNbProc << std::endl;
746 cout << " " << _name << " Working directory = \"" << _hexoticWorkingDirectory << "\"" << std::endl;
747 cout << " " << _name << " Sub. Dom mode = " << _hexoticSdMode << std::endl;
748 cout << " " << _name << " Text options = \"" << _textOptions << "\"" << std::endl;
749 cout << " " << _name << " Number of layers = " << _nbLayers << std::endl;
750 cout << " " << _name << " Size of the first layer = " << _firstLayerSize << std::endl;
751 cout << " " << _name << " Direction of the layers = " << ( _direction ? "Inward" : "Outward" ) << std::endl;
752 cout << " " << _name << " Growth = " << _growth << std::endl;
753 if (!_facesWithLayers.empty()) {
754 cout << " " << _name << " Faces with layers = ";
755 for (size_t i = 0; i < _facesWithLayers.size(); i++)
757 cout << _facesWithLayers.at(i);
758 if ((i + 1) != _facesWithLayers.size())
763 if (!_imprintedFaces.empty()) {
764 cout << " " << _name << " Imprinted faces = ";
765 for (size_t i = 0; i < _imprintedFaces.size(); i++)
767 cout << _imprintedFaces.at(i);
768 if ((i + 1) != _imprintedFaces.size())
774 TCollection_AsciiString run_Hexotic("mg-hexa.exe");
776 TCollection_AsciiString minl = " --min_level ", maxl = " --max_level ", angle = " --ridge_angle ";
777 TCollection_AsciiString mins = " --min_size ", maxs = " --max_size ";
778 TCollection_AsciiString in = " --in ", out = " --out ";
779 TCollection_AsciiString sizeMap = " --read_sizemap ";
780 TCollection_AsciiString ignoreRidges = " --compute_ridges no ", invalideElements = " --allow_invalid_elements yes ";
781 TCollection_AsciiString subdom = " --components ";
783 TCollection_AsciiString proc = " --max_number_of_threads ";
785 TCollection_AsciiString verb = " --verbose ";
786 TCollection_AsciiString maxmem = " --max_memory ";
788 TCollection_AsciiString comNbLayers = " --number_of_boundary_layers ";
789 TCollection_AsciiString comFirstLayerSize = " --height_of_the_first_layer ";
790 TCollection_AsciiString comDirection = " --boundary_layers_subdomain_direction ";
791 TCollection_AsciiString comGrowth = " --boundary_layers_geometric_progression ";
792 TCollection_AsciiString comFacesWithLayers = " --boundary_layers_surface_ids ";
793 TCollection_AsciiString comImptintedFaces = " --imprinted_surface_ids ";
795 TCollection_AsciiString minLevel, maxLevel, minSize, maxSize, sharpAngle, mode, nbproc, verbosity, maxMemory,
796 textOptions, nbLayers, firstLayerSize, direction, growth, facesWithLayers, imprintedFaces;
797 minLevel = _hexesMinLevel;
798 maxLevel = _hexesMaxLevel;
799 minSize = _hexesMinSize;
800 maxSize = _hexesMaxSize;
801 sharpAngle = _hexoticSharpAngleThreshold;
802 // Mode translation for mg-tetra 1.1
803 switch ( _hexoticSdMode )
806 mode = "outside_skin_only";
809 mode = "outside_components";
815 mode = "all --manifold_geometry no";
818 nbproc = _hexoticNbProc;
819 verbosity = _hexoticVerbosity;
820 maxMemory = _hexoticMaxMemory;
821 textOptions = (" " + _textOptions + " ").c_str();
822 nbLayers = _nbLayers;
823 firstLayerSize = _firstLayerSize;
824 direction = _direction ? "1" : "-1";
826 for (size_t i = 0; i < _facesWithLayers.size(); i++)
828 facesWithLayers += _facesWithLayers[i];
829 if ((i + 1) != _facesWithLayers.size())
830 facesWithLayers += ",";
832 for (size_t i = 0; i < _imprintedFaces.size(); i++)
834 imprintedFaces += _imprintedFaces[i];
835 if ((i + 1) != _imprintedFaces.size())
836 imprintedFaces += ",";
839 if (_hexoticIgnoreRidges)
840 run_Hexotic += ignoreRidges;
842 if (_hexoticInvalidElements)
843 run_Hexotic += invalideElements;
845 if (_hexesMinSize > 0)
846 run_Hexotic += mins + minSize;
848 if (_hexesMaxSize > 0)
849 run_Hexotic += maxs + maxSize;
851 if (_hexesMinLevel > 0)
852 run_Hexotic += minl + minLevel;
854 if (_hexesMaxLevel > 0)
855 run_Hexotic += maxl + maxLevel;
857 if (_hexoticSharpAngleThreshold > 0)
858 run_Hexotic += angle + sharpAngle;
860 if (_sizeMaps.begin() != _sizeMaps.end() && forExecutable )
861 run_Hexotic += sizeMap + Hexotic_SizeMap_Prefix;
864 _firstLayerSize > 0 &&
866 !_facesWithLayers.empty())
868 run_Hexotic += comNbLayers + nbLayers;
869 run_Hexotic += comFirstLayerSize + firstLayerSize;
870 run_Hexotic += comDirection + direction;
871 run_Hexotic += comGrowth + growth;
872 run_Hexotic += comFacesWithLayers + facesWithLayers;
873 if (!_imprintedFaces.empty())
874 run_Hexotic += comImptintedFaces + imprintedFaces;
877 run_Hexotic += in + Hexotic_In + out + Hexotic_Out;
878 run_Hexotic += subdom + mode;
880 run_Hexotic += proc + nbproc;
882 run_Hexotic += verb + verbosity;
883 run_Hexotic += maxmem + maxMemory;
885 if (!_textOptions.empty())
886 run_Hexotic += textOptions;
888 return run_Hexotic.ToCString();
891 // TODO : this is a duplication of some code found in BLSURFPlugin_BLSURF find a proper
893 TopoDS_Shape HexoticPlugin_Hexotic::entryToShape(std::string entry)
895 MESSAGE("HexoticPlugin_Hexotic::entryToShape "<<entry );
897 GEOM::GEOM_Object_var aGeomObj;
898 TopoDS_Shape S = TopoDS_Shape();
899 SALOMEDS::SObject_var aSObj = SMESH_Gen_i::getStudyServant()->FindObjectID( entry.c_str() );
900 if (!aSObj->_is_nil()) {
901 CORBA::Object_var obj = aSObj->GetObject();
902 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
905 if ( !aGeomObj->_is_nil() )
906 S = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( aGeomObj.in() );
910 //================================================================================
912 * \brief Produces a .mesh file with the size maps informations to give to Hexotic
914 //================================================================================
915 std::vector<std::string> HexoticPlugin_Hexotic::writeSizeMapFile( MG_Hexotic_API* mgInput,
916 std::string sizeMapPrefix )
918 HexoticPlugin_Hypothesis::THexoticSizeMaps::iterator it;
920 std::vector<ControlPnt> points;
921 // Iterate on the size maps
922 for (it=_sizeMaps.begin(); it!=_sizeMaps.end(); it++)
924 // Step 1 : Get the GEOM object entry and the size
925 // from the _sizeMaps infos
926 std::string anEntry = it->first;
927 double aLocalSize = it->second;
928 TopoDS_Shape aShape = entryToShape( anEntry );
930 // Step 2 : Create the points
931 createControlPoints( aShape, aLocalSize, points );
934 // Write the .mesh size map file
936 std::string myVerticesFile = sizeMapPrefix + ".mesh";
937 std::string mySolFile = sizeMapPrefix + ".sol";
941 mgInput->GmfOpenMesh( myVerticesFile.c_str(), GmfWrite, GMFVERSION, GMFDIMENSION );
943 mgInput->GmfOpenMesh( mySolFile.c_str(), GmfWrite, GMFVERSION, GMFDIMENSION );
945 int pointsNumber = points.size();
948 mgInput->GmfSetKwd( verticesFileID, GmfVertices, pointsNumber );
949 // SolAtVertices Keyword
950 int TypTab[] = {GmfSca};
951 mgInput->GmfSetKwd(solFileID, GmfSolAtVertices, pointsNumber, 1, TypTab);
953 // Read the control points information from the vector and write it into the files
955 std::vector<ControlPnt>::const_iterator points_it;
956 for (points_it = points.begin(); points_it != points.end(); points_it++ )
958 mgInput->GmfSetLin( verticesFileID, GmfVertices, points_it->X(), points_it->Y(), points_it->Z(), 0 );
959 ValTab[0] = points_it->Size();
960 mgInput->GmfSetLin( solFileID, GmfSolAtVertices, ValTab);
964 mgInput->GmfCloseMesh( verticesFileID );
965 mgInput->GmfCloseMesh( solFileID );
967 std::vector<std::string> fileNames(2);
968 fileNames[0] = myVerticesFile;
969 fileNames[1] = mySolFile;
974 //=============================================================================
976 * Here we are going to use the MG-Hexa mesher
978 //=============================================================================
980 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh& aMesh,
981 const TopoDS_Shape& aShape)
983 _computeCanceled = false;
985 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
986 TCollection_AsciiString hexahedraMessage;
988 _nbShape = countShape( &aMesh, TopAbs_SOLID ); // we count the number of shapes
991 // create bounding box for each shape of the compound
994 TopoDS_Shape *tabShape;
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;
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;
1015 SetParameters(_hypothesis);
1017 TCollection_AsciiString aTmpDir = _hexoticWorkingDirectory.c_str();
1018 TCollection_AsciiString aQuote("");
1021 if ( aTmpDir.Value(aTmpDir.Length()) != '\\' ) aTmpDir += '\\';
1023 if ( aTmpDir.Value(aTmpDir.Length()) != '/' ) aTmpDir += '/';
1025 TCollection_AsciiString Hexotic_In(""), Hexotic_Out, Hexotic_SizeMap_Prefix;
1026 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1027 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic"+getSuffix()+".log"; // log
1029 std::map <int,int> aSmdsToHexoticIdMap;
1030 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1032 MG_Hexotic_API mgHexa( _computeCanceled, _progress );
1034 Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
1035 #ifdef WITH_BLSURFPLUGIN
1036 bool defaultInputFile = true;
1037 if (_blsurfHypo && !_blsurfHypo->GetQuadAllowed()) {
1038 Hexotic_In = _blsurfHypo->GetGMFFile().c_str();
1039 if ( !Hexotic_In.IsEmpty() &&
1040 SMESH_File( _blsurfHypo->GetGMFFile() ).exists() )
1042 mgHexa.SetUseExecutable();
1043 defaultInputFile = false;
1046 if (defaultInputFile) {
1048 Hexotic_In = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
1049 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1050 splitQuads(aMesh); // quadrangles are no longer acceptable as input
1051 if ( mgHexa.IsExecutable() )
1054 cout << "Creating MG-Hexa input mesh file : " << Hexotic_In << std::endl;
1056 writeInput( &mgHexa, Hexotic_In.ToCString(), meshDS );
1057 #ifdef WITH_BLSURFPLUGIN
1060 removeFile( Hexotic_Out );
1064 Hexotic_SizeMap_Prefix = aTmpDir + "Hexotic_SizeMap" + getSuffix();
1065 std::vector<std::string> sizeMapFiles = writeSizeMapFile( &mgHexa, Hexotic_SizeMap_Prefix.ToCString() );
1067 std::string run_Hexotic = getHexoticCommand(aQuote + Hexotic_In + aQuote, aQuote + Hexotic_Out + aQuote, Hexotic_SizeMap_Prefix, mgHexa.IsExecutable() );
1068 run_Hexotic += std::string(" 1> ") + aQuote.ToCString() + aLogFileName.ToCString() + aQuote.ToCString(); // dump into file
1069 mgHexa.SetLogFile( aLogFileName.ToCString() );
1070 cout << "Creating MG-Hexa log file : " << aLogFileName << std::endl;
1073 cout << "MG-Hexa command : " << run_Hexotic << std::endl;
1075 if ( mgHexa.IsExecutable() )
1078 modeFile_In += Hexotic_In;
1079 system( modeFile_In.ToCString() );
1082 aSmdsToHexoticIdMap.clear();
1083 aHexoticIdToNodeMap.clear();
1085 MESSAGE("HexoticPlugin_Hexotic::Compute");
1089 Ok = mgHexa.Compute( run_Hexotic, errStr ); // run
1096 if ( mgHexa.IsExecutable() )
1099 modeFile_Out += Hexotic_Out;
1100 system( modeFile_Out.ToCString() );
1103 SMESH_MesherHelper aHelper( aMesh );
1105 Ok = readResult( &mgHexa, Hexotic_Out.ToCString(),
1107 &aHelper, _nbShape, tabShape, tabBox );
1109 std::string log = mgHexa.GetLog();
1112 hexahedraMessage = "success";
1114 removeFile(Hexotic_Out);
1115 removeFile(Hexotic_In);
1116 //removeFile(aLogFileName);
1117 for( size_t i=0; i<sizeMapFiles.size(); i++)
1119 removeFile( TCollection_AsciiString( sizeMapFiles[i].c_str() ) );
1125 hexahedraMessage = "failed";
1126 if ( mgHexa.IsExecutable() )
1127 cout << "Problem with MG-Hexa output file " << Hexotic_Out.ToCString() << std::endl;
1131 char msgLic[] = " Dlim ";
1132 std::string log = mgHexa.GetLog();
1133 const char* fileBeg = &log[0], *fileEnd = fileBeg + log.size();
1134 if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1135 error("Licence problems.");
1137 if ( !errStr.empty() )
1140 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1144 for (int i=0; i<_nbShape; i++)
1145 delete [] tabBox[i];
1151 if(_computeCanceled)
1152 return error(SMESH_Comment("interruption initiated by user"));
1157 //=============================================================================
1159 * \brief Computes mesh without geometry
1160 * \param aMesh - the mesh
1161 * \param aHelper - helper that must be used for adding elements to \aaMesh
1162 * \retval bool - is a success
1164 * The method is called if ( !aMesh->HasShapeToMesh() )
1166 //=============================================================================
1168 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
1170 _computeCanceled = false;
1172 SMESH_ComputeErrorPtr myError = SMESH_ComputeError::New();
1175 TCollection_AsciiString hexahedraMessage;
1176 TCollection_AsciiString aQuote("");
1180 SetParameters(_hypothesis);
1182 TCollection_AsciiString aTmpDir = _hexoticWorkingDirectory.c_str();//getTmpDir();
1183 TCollection_AsciiString Hexotic_In, Hexotic_Out, Hexotic_SizeMap_Prefix;
1184 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1185 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic"+getSuffix()+".log"; // log
1187 std::map <int,int> aSmdsToHexoticIdMap;
1188 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1190 Hexotic_In = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
1191 Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
1192 Hexotic_SizeMap_Prefix = aTmpDir + "Hexotic_SizeMap";
1194 MG_Hexotic_API mgHexa( _computeCanceled, _progress );
1196 std::vector<std::string> sizeMapFiles = writeSizeMapFile( &mgHexa, Hexotic_SizeMap_Prefix.ToCString() );
1198 std::string run_Hexotic = getHexoticCommand(aQuote + Hexotic_In + aQuote, aQuote + Hexotic_Out + aQuote, Hexotic_SizeMap_Prefix, mgHexa.IsExecutable());
1199 run_Hexotic += std::string(" 1> ") + aQuote.ToCString() + aLogFileName.ToCString() + aQuote.ToCString(); // dump into file
1200 mgHexa.SetLogFile( aLogFileName.ToCString() );
1201 cout << "Creating MG-Hexa log file : " << aLogFileName << std::endl;
1203 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1205 splitQuads(aMesh); // quadrangles are no longer acceptable as input
1208 cout << "Creating MG-Hexa input mesh file : " << Hexotic_In << std::endl;
1209 writeInput( &mgHexa, Hexotic_In.ToCString(), aHelper->GetMeshDS() );
1210 if ( mgHexa.IsExecutable() )
1213 modeFile_In += Hexotic_In;
1214 system( modeFile_In.ToCString() );
1217 aSmdsToHexoticIdMap.clear();
1218 aHexoticIdToNodeMap.clear();
1220 MESSAGE("HexoticPlugin_Hexotic::Compute");
1223 cout << "MG-Hexa command : " << run_Hexotic << std::endl;
1226 Ok = mgHexa.Compute( run_Hexotic, errStr ); // run
1232 if ( mgHexa.IsExecutable() )
1234 modeFile_Out += Hexotic_Out;
1235 system( modeFile_Out.ToCString() );
1238 Ok = Ok && readResult( &mgHexa, Hexotic_Out.ToCString(), this, aHelper );
1240 std::string log = mgHexa.GetLog();
1243 hexahedraMessage = "success";
1247 hexahedraMessage = "failed";
1248 if ( mgHexa.IsExecutable() )
1249 cout << "Problem with MG-Hexa output file " << Hexotic_Out << std::endl;
1251 if ( log.find( " license " ) != std::string::npos ||
1252 log.find( " Dlim " ) != std::string::npos )
1253 error("License problems.");
1255 if ( !errStr.empty() )
1258 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1261 if(_computeCanceled)
1262 return error(SMESH_Comment("interruption initiated by user"));
1263 removeFile(Hexotic_Out);
1264 removeFile(Hexotic_In);
1266 removeFile(aLogFileName);
1267 for( size_t i=0; i<sizeMapFiles.size(); i++)
1269 removeFile( TCollection_AsciiString(sizeMapFiles[i].c_str()) );
1274 //=============================================================================
1278 //=============================================================================
1280 bool HexoticPlugin_Hexotic::Evaluate(SMESH_Mesh& aMesh,
1281 const TopoDS_Shape& aShape,
1282 MapShapeNbElems& aResMap)
1284 std::vector<int> aResVec(SMDSEntity_Last);
1285 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1286 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1287 aResMap.insert(std::make_pair(sm,aResVec));
1289 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1290 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Evaluation is not implemented",this));
1295 void HexoticPlugin_Hexotic::CancelCompute()
1297 _computeCanceled = true;
1300 TCollection_AsciiString aTmpDir = _hexoticWorkingDirectory.c_str(); //getTmpDir();
1301 TCollection_AsciiString Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1302 TCollection_AsciiString cmd = TCollection_AsciiString("ps ux | grep ") + Hexotic_In;
1303 cmd += TCollection_AsciiString(" | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1");
1304 system( cmd.ToCString() );