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"
38 #include <SMESHDS_GroupBase.hxx>
39 #include <SMESHDS_Mesh.hxx>
40 #include <SMESH_ComputeError.hxx>
41 #include <SMESH_File.hxx>
42 #include <SMESH_Gen.hxx>
43 #include <SMESH_HypoFilter.hxx>
44 #include <SMESH_MesherHelper.hxx>
45 #include <SMESH_subMesh.hxx>
46 #include <SMESH_ControlPnt.hxx>
51 #include <Standard_ProgramError.hxx>
53 #include <BRepBndLib.hxx>
54 #include <BRepClass3d_SolidClassifier.hxx>
55 #include <BRep_Tool.hxx>
56 #include <Bnd_Box.hxx>
57 #include <OSD_File.hxx>
58 #include <Precision.hxx>
59 #include <TopExp_Explorer.hxx>
60 #include <TopTools_MapOfShape.hxx>
62 #include <TopoDS_Vertex.hxx>
65 #include <Basics_Utils.hxx>
66 #include <GEOMImpl_Types.hxx>
67 #include <GEOM_wrap.hxx>
69 #define GMFVERSION GmfDouble
70 #define GMFDIMENSION 3
72 using SMESHUtils::ControlPnt;
74 static void removeFile( const TCollection_AsciiString& fileName )
77 OSD_File( fileName ).Remove();
79 catch ( Standard_ProgramError ) {
80 MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
84 //=============================================================================
88 //=============================================================================
90 HexoticPlugin_Hexotic::HexoticPlugin_Hexotic(int hypId, int studyId, SMESH_Gen* gen)
91 : SMESH_3D_Algo(hypId, studyId, gen)
93 MESSAGE("HexoticPlugin_Hexotic::HexoticPlugin_Hexotic");
95 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
96 _onlyUnaryInput = false;
97 _requireShape = false;
100 _hexoticFilesKept=false;
101 _compatibleHypothesis.push_back( HexoticPlugin_Hypothesis::GetHypType() );
102 #ifdef WITH_BLSURFPLUGIN
105 _computeCanceled = false;
107 // Copy of what is done in BLSURFPLugin TODO : share the code
108 smeshGen_i = SMESH_Gen_i::GetSMESHGen();
109 CORBA::Object_var anObject = smeshGen_i->GetNS()->Resolve("/myStudyManager");
110 SALOMEDS::StudyManager_var aStudyMgr = SALOMEDS::StudyManager::_narrow(anObject);
113 myStudy = aStudyMgr->GetStudyByID(_studyId);
114 if ( !myStudy->_is_nil() )
115 MESSAGE("myStudy->StudyId() = " << myStudy->StudyId());
118 //=============================================================================
122 //=============================================================================
124 HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic()
126 MESSAGE("HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic");
130 #ifdef WITH_BLSURFPLUGIN
131 bool HexoticPlugin_Hexotic::CheckBLSURFHypothesis( SMESH_Mesh& aMesh,
132 const TopoDS_Shape& aShape )
134 // MESSAGE("HexoticPlugin_Hexotic::CheckBLSURFHypothesis");
137 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
138 const SMESHDS_Hypothesis* theHyp;
140 // If a BLSURF hypothesis is applied, get it
141 SMESH_HypoFilter blsurfFilter;
142 blsurfFilter.Init( blsurfFilter.HasName( BLSURFPlugin_Hypothesis::GetHypType() ));
143 std::list<const SMESHDS_Hypothesis *> appliedHyps;
144 aMesh.GetHypotheses( aShape, blsurfFilter, appliedHyps, false );
146 if ( appliedHyps.size() > 0 ) {
147 itl = appliedHyps.begin();
148 theHyp = (*itl); // use only the first hypothesis
149 std::string hypName = theHyp->GetName();
150 if (hypName == BLSURFPlugin_Hypothesis::GetHypType()) {
151 _blsurfHypo = static_cast<const BLSURFPlugin_Hypothesis*> (theHyp);
160 //=============================================================================
164 //=============================================================================
166 bool HexoticPlugin_Hexotic::CheckHypothesis( SMESH_Mesh& aMesh,
167 const TopoDS_Shape& aShape,
168 SMESH_Hypothesis::Hypothesis_Status& aStatus )
170 // MESSAGE("HexoticPlugin_Hexotic::CheckHypothesis");
173 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
174 const SMESHDS_Hypothesis* theHyp;
176 const std::list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
177 int nbHyp = hyps.size();
179 aStatus = SMESH_Hypothesis::HYP_OK;
180 return true; // can work with no hypothesis
184 theHyp = (*itl); // use only the first hypothesis
186 std::string hypName = theHyp->GetName();
187 if (hypName == HexoticPlugin_Hypothesis::GetHypType() ) {
188 _hypothesis = static_cast<const HexoticPlugin_Hypothesis*> (theHyp);
190 aStatus = SMESH_Hypothesis::HYP_OK;
193 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
195 #ifdef WITH_BLSURFPLUGIN
196 CheckBLSURFHypothesis(aMesh, aShape);
199 return aStatus == SMESH_Hypothesis::HYP_OK;
202 //=======================================================================
203 //function : findShape
205 //=======================================================================
207 static TopoDS_Shape findShape(SMDS_MeshNode** t_Node,
209 const TopoDS_Shape* t_Shape,
214 int iShape, nbNode = 8;
216 for ( int i=0; i<3; i++ ) {
218 for ( int j=0; j<nbNode; j++ ) {
219 if ( i == 0) pntCoor[i] += t_Node[j]->X();
220 if ( i == 1) pntCoor[i] += t_Node[j]->Y();
221 if ( i == 2) pntCoor[i] += t_Node[j]->Z();
223 pntCoor[i] /= nbNode;
225 gp_Pnt aPnt(pntCoor[0], pntCoor[1], pntCoor[2]);
227 if ( aShape.IsNull() ) aShape = t_Shape[0];
228 BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
229 if ( !(SC.State() == TopAbs_IN) ) {
231 for (iShape = 0; iShape < nShape && aShape.IsNull(); iShape++) {
232 if ( !( pntCoor[0] < t_Box[iShape][0] || t_Box[iShape][1] < pntCoor[0] ||
233 pntCoor[1] < t_Box[iShape][2] || t_Box[iShape][3] < pntCoor[1] ||
234 pntCoor[2] < t_Box[iShape][4] || t_Box[iShape][5] < pntCoor[2]) ) {
235 BRepClass3d_SolidClassifier SC (t_Shape[iShape], aPnt, Precision::Confusion());
236 if (SC.State() == TopAbs_IN)
237 aShape = t_Shape[iShape];
244 //=======================================================================
245 //function : getNbShape
247 //=======================================================================
249 static int getNbShape(MG_Hexotic_API* hexaOutput, int iMesh, GmfKwdCod what, int defaultValue=0)
251 int number = hexaOutput->GmfStatKwd( iMesh, what );
254 // std::string aLine;
255 // std::ifstream file(aFile.c_str());
256 // while ( !file.eof() ) {
257 // getline( file, aLine);
258 // if ( aLine == aString ) {
259 // getline( file, aLine);
260 // std::istringstream stringFlux( aLine );
261 // stringFlux >> number;
262 number = ( number + defaultValue + std::abs(number - defaultValue) ) / 2;
268 number = defaultValue;
274 //=======================================================================
275 //function : countShape
277 //=======================================================================
279 static int countShape( SMESH_Mesh* mesh, TopAbs_ShapeEnum shape )
281 if ( !mesh->HasShapeToMesh() )
283 TopExp_Explorer expShape ( mesh->GetShapeToMesh(), shape );
284 TopTools_MapOfShape mapShape;
286 for ( ; expShape.More(); expShape.Next() ) {
287 if (mapShape.Add(expShape.Current())) {
294 //=======================================================================
295 //function : getShape
297 //=======================================================================
299 template < class Mesh, class Shape, class Tab >
300 void getShape(Mesh* mesh, Shape shape, Tab *t_Shape)
302 TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
303 TopTools_MapOfShape mapShape;
304 for ( int i=0; expShape.More(); expShape.Next() ) {
305 if (mapShape.Add(expShape.Current())) {
306 t_Shape[i] = expShape.Current();
313 //=======================================================================
314 //function : printWarning
316 //=======================================================================
318 static void printWarning(const int nbExpected, std::string aString, const int nbFound) {
320 cout << "WARNING : " << nbExpected << " " << aString << " expected, MG-Hexa has found " << nbFound << std::endl;
321 cout << "=======" << std::endl;
326 //=======================================================================
327 //function : removeHexoticFiles
329 //=======================================================================
331 static void removeHexoticFiles(TCollection_AsciiString file_In, TCollection_AsciiString file_Out) {
332 removeFile( file_In );
333 removeFile( file_Out );
336 //=======================================================================
337 //function : splitQuads
338 //purpose : splits all quadrangles into triangles
339 //=======================================================================
341 static void splitQuads(SMESH_Mesh& aMesh)
343 SMESH_MeshEditor spliter( &aMesh );
345 TIDSortedElemSet elems;
346 SMDS_ElemIteratorPtr eIt = aMesh.GetMeshDS()->elementsIterator();
348 elems.insert( elems.end(), eIt->next() );
350 spliter.QuadToTri ( elems, /*the13Diag=*/true);
353 //=======================================================================
354 //function : writeInput
355 //purpose : pass a mesh to input of MG-Hexa
356 //=======================================================================
358 static void writeInput(MG_Hexotic_API* theHexaInput,
360 const SMESHDS_Mesh* theMeshDS)
362 int meshID = theHexaInput->GmfOpenMesh( theFile, GmfWrite, GMFVERSION, GMFDIMENSION);
365 int iN = 0, nbNodes = theMeshDS->NbNodes();
366 theHexaInput->GmfSetKwd( meshID, GmfVertices, nbNodes );
367 std::map< const SMDS_MeshNode*, int, TIDCompare > node2IdMap;
368 SMDS_NodeIteratorPtr nodeIt = theMeshDS->nodesIterator();
370 while ( nodeIt->more() )
372 n.Set( nodeIt->next() );
373 theHexaInput->GmfSetLin( meshID, GmfVertices, n.X(), n.Y(), n.Z(), n._node->getshapeId() );
374 node2IdMap.insert( node2IdMap.end(), std::make_pair( n._node, ++iN ));
378 SMDS_ElemIteratorPtr elemIt = theMeshDS->elementsIterator( SMDSAbs_Edge );
379 if ( elemIt->more() )
381 int nbEdges = theMeshDS->GetMeshInfo().NbElements( SMDSAbs_Edge );
382 theHexaInput->GmfSetKwd(meshID, GmfEdges, nbEdges );
383 for ( int gmfID = 1; elemIt->more(); ++gmfID )
385 const SMDS_MeshElement* edge = elemIt->next();
386 theHexaInput->GmfSetLin(meshID, GmfEdges,
387 node2IdMap[ edge->GetNode( 0 )],
388 node2IdMap[ edge->GetNode( 1 )],
389 edge->getshapeId() );
394 elemIt = theMeshDS->elementGeomIterator( SMDSGeom_TRIANGLE );
395 if ( elemIt->more() )
397 int nbTria = theMeshDS->GetMeshInfo().NbElements( SMDSGeom_TRIANGLE );
398 theHexaInput->GmfSetKwd(meshID, GmfTriangles, nbTria );
399 for ( int gmfID = 1; elemIt->more(); ++gmfID )
401 const SMDS_MeshElement* tria = elemIt->next();
402 theHexaInput->GmfSetLin(meshID, GmfTriangles,
403 node2IdMap[ tria->GetNode( 0 )],
404 node2IdMap[ tria->GetNode( 1 )],
405 node2IdMap[ tria->GetNode( 2 )],
406 tria->getshapeId() );
409 theHexaInput->GmfCloseMesh( meshID );
412 //=======================================================================
413 //function : readResult
414 //purpose : Read GMF file in case of a mesh with geometry
415 //=======================================================================
417 static bool readResult(MG_Hexotic_API* theHexaOutput,
419 HexoticPlugin_Hexotic*theAlgo,
420 SMESH_MesherHelper* theHelper,
421 const int nbShape = 0,
422 const TopoDS_Shape* tabShape = 0,
425 SMESH_Mesh* theMesh = theHelper->GetMesh();
426 SMESHDS_Mesh* theMeshDS = theHelper->GetMeshDS();
428 // ---------------------------------
429 // Read generated elements and nodes
430 // ---------------------------------
433 TopoDS_Vertex aVertex;
435 int shapeID, hexoticShapeID;
436 const int IdShapeRef = 2;
438 double epsilon = Precision::Confusion();
439 std::map <std::string,int> mapField;
440 SMDS_MeshNode** HexoticNode;
441 TopoDS_Shape *tabCorner;
443 const int nbDomains = countShape( theMesh, TopAbs_SHELL );
444 const int holeID = -1;
448 tabID = new int[nbDomains];
450 for (int i=0; i<nbDomains; i++)
452 if ( nbDomains == 1 )
453 tabID[0] = theMeshDS->ShapeToIndex( tabShape[0] );
461 SMDS_ElemIteratorPtr eIt = theMeshDS->elementsIterator();
463 theMeshDS->RemoveFreeElement( eIt->next(), /*sm=*/0 );
464 SMDS_NodeIteratorPtr nIt = theMeshDS->nodesIterator();
465 while ( nIt->more() )
466 theMeshDS->RemoveFreeNode( nIt->next(), /*sm=*/0 );
468 theHelper->SetElementsOnShape( false );
471 int meshID = theHexaOutput->GmfOpenMesh( theFile, GmfRead, &ver, &dim );
473 int nbVertices = getNbShape(theHexaOutput, meshID, GmfVertices );
474 int nbCorners = getNbShape(theHexaOutput, meshID, GmfCorners, countShape( theMesh, TopAbs_VERTEX ));
475 if ( nbVertices == 0 )
478 tabCorner = new TopoDS_Shape[ nbCorners ];
479 HexoticNode = new SMDS_MeshNode*[ nbVertices + 1 ];
482 getShape(theMeshDS, TopAbs_VERTEX, tabCorner);
484 int nbNodes = theHexaOutput->GmfStatKwd( meshID, GmfVertices );
487 theHexaOutput->GmfGotoKwd( meshID, GmfVertices );
489 for ( int aHexoticID = 1; aHexoticID <= nbNodes; ++aHexoticID )
491 if ( theAlgo->computeCanceled() )
493 theHexaOutput->GmfGetLin( meshID, GmfVertices, &x, &y, &z, &shapeID );
494 HexoticNode[ aHexoticID ] = theHelper->AddNode( x,y,z );
499 SMDS_MeshNode* node[8];
500 SMDS_MeshElement * aHexoticElement;
502 nbCorners = theHexaOutput->GmfStatKwd( meshID, GmfCorners );
503 if ( nbCorners > 0 && nbDomains > 0 )
505 theHexaOutput->GmfGotoKwd( meshID, GmfCorners );
506 for ( int iElem = 0; iElem < nbCorners; iElem++ )
508 if ( theAlgo->computeCanceled() )
510 theHexaOutput->GmfGetLin( meshID, GmfCorners, &nodeID[0] );
511 node[0] = HexoticNode[ nodeID[0] ];
512 gp_Pnt HexoticPnt ( node[0]->X(), node[0]->Y(), node[0]->Z() );
513 for ( int i = 0; i < nbCorners; i++ )
515 aVertex = TopoDS::Vertex( tabCorner[i] );
516 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
517 if ( aPnt.Distance( HexoticPnt ) < epsilon )
519 theMeshDS->SetNodeOnVertex( node[0], aVertex );
526 int nbEdges = theHexaOutput->GmfStatKwd( meshID, GmfEdges );
529 theHexaOutput->GmfGotoKwd( meshID, GmfEdges );
530 for ( int iElem = 0; iElem < nbEdges; iElem++ )
532 if ( theAlgo->computeCanceled() )
534 theHexaOutput->GmfGetLin( meshID, GmfEdges, &nodeID[0], &nodeID[1], &shapeID );
535 for ( int i = 0; i < 2; ++i )
537 node[i] = HexoticNode[ nodeID[i]];
538 if ( shapeID > 0 && node[i]->getshapeId() < 1 )
539 theMeshDS->SetNodeOnEdge( node[i], shapeID );
541 aHexoticElement = theHelper->AddEdge( node[0], node[1] );
542 if ( shapeID > 0 && aHexoticElement->getshapeId() < 1 )
543 theMeshDS->SetMeshElementOnShape( aHexoticElement, shapeID );
547 int nbQuad = theHexaOutput->GmfStatKwd( meshID, GmfQuadrilaterals );
550 theHexaOutput->GmfGotoKwd( meshID, GmfQuadrilaterals );
551 for ( int iElem = 0; iElem < nbQuad; iElem++ )
553 if ( theAlgo->computeCanceled() )
555 theHexaOutput->GmfGetLin( meshID, GmfQuadrilaterals,
556 &nodeID[0], &nodeID[1], &nodeID[2], &nodeID[3], &shapeID );
557 for ( int i = 0; i < 4; ++i )
559 node[i] = HexoticNode[ nodeID[i]];
560 if ( shapeID > 0 && node[i]->getshapeId() < 1 )
561 theMeshDS->SetNodeOnFace( node[i], shapeID );
563 aHexoticElement = theHelper->AddFace( node[0], node[1], node[2], node[3] );
564 if ( shapeID > 0 && aHexoticElement->getshapeId() < 1 )
565 theMeshDS->SetMeshElementOnShape( aHexoticElement, shapeID );
569 int nbHexa = theHexaOutput->GmfStatKwd( meshID, GmfHexahedra );
572 theHexaOutput->GmfGotoKwd( meshID, GmfHexahedra );
573 for ( int iElem = 0; iElem < nbHexa; iElem++ )
575 if ( theAlgo->computeCanceled() )
577 theHexaOutput->GmfGetLin( meshID, GmfHexahedra,
578 &nodeID[0], &nodeID[1], &nodeID[2], &nodeID[3],
579 &nodeID[4], &nodeID[5], &nodeID[6], &nodeID[7],
581 for ( int i = 0; i < 8; ++i )
583 node[i] = HexoticNode[ nodeID[i]];
585 if ( nbDomains > 1 ) {
586 hexoticShapeID = shapeID - IdShapeRef;
587 if ( tabID[ hexoticShapeID ] == 0 ) {
588 aShape = findShape(node, aShape, tabShape, tabBox, nbShape);
589 shapeID = aShape.IsNull() ? holeID : theMeshDS->ShapeToIndex( aShape );
590 tabID[ hexoticShapeID ] = shapeID;
593 shapeID = tabID[ hexoticShapeID ];
595 if ( iElem == ( nbHexa - 1) ) {
596 int shapeAssociated = 0;
597 for ( int i=0; i<nbDomains; i++ ) {
599 shapeAssociated += 1;
601 if ( shapeAssociated != nbShape )
602 printWarning(nbShape, "domains", shapeAssociated);
609 if ( shapeID != holeID )
611 for ( int i = 0; i < 8; ++i )
613 if ( node[i]->NbInverseElements( SMDSAbs_Face ) == 0 )
614 theMeshDS->SetNodeInVolume( node[i], shapeID );
616 aHexoticElement = theHelper->AddVolume( node[0], node[3], node[2], node[1],
617 node[4], node[7], node[6], node[5]);
618 if ( aHexoticElement->getshapeId() < 1 )
619 theMeshDS->SetMeshElementOnShape( aHexoticElement, shapeID );
625 // remove nodes in holes
628 SMESHDS_SubMesh* subMesh = 0;
629 for ( int i = 1; i <= nbNodes; ++i )
630 if ( HexoticNode[i]->NbInverseElements() == 0 )
632 theMeshDS->RemoveFreeNode( HexoticNode[i], subMesh, /*fromGroups=*/false );
637 delete [] HexoticNode;
641 //=============================================================================
643 * Pass parameters to MG-Hexa
645 //=============================================================================
647 void HexoticPlugin_Hexotic::SetParameters(const HexoticPlugin_Hypothesis* hyp) {
649 MESSAGE("HexoticPlugin_Hexotic::SetParameters");
651 _hexesMinLevel = hyp->GetHexesMinLevel();
652 _hexesMaxLevel = hyp->GetHexesMaxLevel();
653 _hexesMinSize = hyp->GetMinSize();
654 _hexesMaxSize = hyp->GetMaxSize();
655 _hexoticIgnoreRidges = hyp->GetHexoticIgnoreRidges();
656 _hexoticInvalidElements = hyp->GetHexoticInvalidElements();
657 _hexoticSharpAngleThreshold = hyp->GetHexoticSharpAngleThreshold();
658 _hexoticNbProc = hyp->GetHexoticNbProc();
659 _hexoticWorkingDirectory = hyp->GetHexoticWorkingDirectory();
660 _hexoticVerbosity = hyp->GetHexoticVerbosity();
661 _hexoticMaxMemory = hyp->GetHexoticMaxMemory();
662 _hexoticSdMode = hyp->GetHexoticSdMode();
663 _textOptions = hyp->GetAdvancedOption();
664 _sizeMaps = hyp->GetSizeMaps();
665 _nbLayers = hyp->GetNbLayers();
666 _firstLayerSize = hyp->GetFirstLayerSize();
667 _direction = hyp->GetDirection();
668 _growth = hyp->GetGrowth();
669 _facesWithLayers = hyp->GetFacesWithLayers();
670 _imprintedFaces = hyp->GetImprintedFaces();
674 cout << "WARNING : The MG-Hexa default parameters are taken into account" << std::endl;
675 cout << "=======" << std::endl;
676 _hexesMinLevel = hyp->GetDefaultHexesMinLevel();
677 _hexesMaxLevel = hyp->GetDefaultHexesMaxLevel();
678 _hexesMinSize = hyp->GetDefaultMinSize();
679 _hexesMaxSize = hyp->GetDefaultMaxSize();
680 _hexoticIgnoreRidges = hyp->GetDefaultHexoticIgnoreRidges();
681 _hexoticInvalidElements = hyp->GetDefaultHexoticInvalidElements();
682 _hexoticSharpAngleThreshold = hyp->GetDefaultHexoticSharpAngleThreshold();
683 _hexoticNbProc = hyp->GetDefaultHexoticNbProc();
684 _hexoticWorkingDirectory = hyp->GetDefaultHexoticWorkingDirectory();
685 _hexoticVerbosity = hyp->GetDefaultHexoticVerbosity();
686 _hexoticMaxMemory = hyp->GetDefaultHexoticMaxMemory();
687 _hexoticSdMode = hyp->GetDefaultHexoticSdMode();
688 _textOptions = hyp->GetDefaultTextOptions();
689 _sizeMaps = hyp->GetDefaultHexoticSizeMaps();
690 _nbLayers = hyp->GetDefaultNbLayers();
691 _firstLayerSize = hyp->GetDefaultFirstLayerSize();
692 _direction = hyp->GetDefaultDirection();
693 _growth = hyp->GetDefaultGrowth();
694 _facesWithLayers = hyp->GetDefaultFacesWithLayers();
695 _imprintedFaces = hyp->GetDefaultImprintedFaces();
699 //=======================================================================
700 //function : getSuffix
701 //purpose : Returns a suffix that will be unique for the current process
702 //=======================================================================
704 static TCollection_AsciiString getSuffix()
706 TCollection_AsciiString aSuffix = "";
709 aSuffix += getenv("USER");
711 std::string uname = std::string(getenv("USERNAME"));
712 replace(uname.begin(), uname.end(), ' ', '_');
713 aSuffix += uname.c_str();
716 aSuffix += Kernel_Utils::GetHostname().c_str();
721 aSuffix += _getpid();
727 //================================================================================
729 * \brief Returns a command to run MG-Hexa mesher
731 //================================================================================
733 std::string HexoticPlugin_Hexotic::getHexoticCommand(const TCollection_AsciiString& Hexotic_In,
734 const TCollection_AsciiString& Hexotic_Out,
735 const TCollection_AsciiString& Hexotic_SizeMap_Prefix,
736 const bool forExecutable) const
739 cout << "MG-Hexa execution..." << std::endl;
740 cout << _name << " parameters :" << std::endl;
741 cout << " " << _name << " Verbosity = " << _hexoticVerbosity << std::endl;
742 cout << " " << _name << " Max Memory = " << _hexoticMaxMemory << std::endl;
743 cout << " " << _name << " Segments Min Level = " << _hexesMinLevel << std::endl;
744 cout << " " << _name << " Segments Max Level = " << _hexesMaxLevel << std::endl;
745 cout << " " << _name << " Segments Min Size = " << _hexesMinSize << std::endl;
746 cout << " " << _name << " Segments Max Size = " << _hexesMaxSize << std::endl;
747 cout << " " << "MG-Hexa can ignore ridges : " << (_hexoticIgnoreRidges ? "yes":"no") << std::endl;
748 cout << " " << "MG-Hexa authorize invalide elements : " << ( _hexoticInvalidElements ? "yes":"no") << std::endl;
749 cout << " " << _name << " Sharp angle threshold = " << _hexoticSharpAngleThreshold << " degrees" << std::endl;
750 cout << " " << _name << " Number of threads = " << _hexoticNbProc << std::endl;
751 cout << " " << _name << " Working directory = \"" << _hexoticWorkingDirectory << "\"" << std::endl;
752 cout << " " << _name << " Sub. Dom mode = " << _hexoticSdMode << std::endl;
753 cout << " " << _name << " Text options = \"" << _textOptions << "\"" << std::endl;
754 cout << " " << _name << " Number of layers = " << _nbLayers << std::endl;
755 cout << " " << _name << " Size of the first layer = " << _firstLayerSize << std::endl;
756 cout << " " << _name << " Direction of the layers = " << ( _direction ? "Inward" : "Outward" ) << std::endl;
757 cout << " " << _name << " Growth = " << _growth << std::endl;
758 if (!_facesWithLayers.empty()) {
759 cout << " " << _name << " Faces with layers = ";
760 for (size_t i = 0; i < _facesWithLayers.size(); i++)
762 cout << _facesWithLayers.at(i);
763 if ((i + 1) != _facesWithLayers.size())
768 if (!_imprintedFaces.empty()) {
769 cout << " " << _name << " Imprinted faces = ";
770 for (size_t i = 0; i < _imprintedFaces.size(); i++)
772 cout << _imprintedFaces.at(i);
773 if ((i + 1) != _imprintedFaces.size())
779 TCollection_AsciiString run_Hexotic("mg-hexa.exe");
781 TCollection_AsciiString minl = " --min_level ", maxl = " --max_level ", angle = " --ridge_angle ";
782 TCollection_AsciiString mins = " --min_size ", maxs = " --max_size ";
783 TCollection_AsciiString in = " --in ", out = " --out ";
784 TCollection_AsciiString sizeMap = " --read_sizemap ";
785 TCollection_AsciiString ignoreRidges = " --compute_ridges no ", invalideElements = " --allow_invalid_elements yes ";
786 TCollection_AsciiString subdom = " --components ";
788 TCollection_AsciiString proc = " --max_number_of_threads ";
790 TCollection_AsciiString verb = " --verbose ";
791 TCollection_AsciiString maxmem = " --max_memory ";
793 TCollection_AsciiString comNbLayers = " --number_of_boundary_layers ";
794 TCollection_AsciiString comFirstLayerSize = " --height_of_the_first_layer ";
795 TCollection_AsciiString comDirection = " --boundary_layers_subdomain_direction ";
796 TCollection_AsciiString comGrowth = " --boundary_layers_geometric_progression ";
797 TCollection_AsciiString comFacesWithLayers = " --boundary_layers_surface_ids ";
798 TCollection_AsciiString comImptintedFaces = " --imprinted_surface_ids ";
800 TCollection_AsciiString minLevel, maxLevel, minSize, maxSize, sharpAngle, mode, nbproc, verbosity, maxMemory,
801 textOptions, nbLayers, firstLayerSize, direction, growth, facesWithLayers, imprintedFaces;
802 minLevel = _hexesMinLevel;
803 maxLevel = _hexesMaxLevel;
804 minSize = _hexesMinSize;
805 maxSize = _hexesMaxSize;
806 sharpAngle = _hexoticSharpAngleThreshold;
807 // Mode translation for mg-tetra 1.1
808 switch ( _hexoticSdMode )
811 mode = "outside_skin_only";
814 mode = "outside_components";
820 mode = "all --manifold_geometry no";
823 nbproc = _hexoticNbProc;
824 verbosity = _hexoticVerbosity;
825 maxMemory = _hexoticMaxMemory;
826 textOptions = (" " + _textOptions + " ").c_str();
827 nbLayers = _nbLayers;
828 firstLayerSize = _firstLayerSize;
829 direction = _direction ? "1" : "-1";
831 for (size_t i = 0; i < _facesWithLayers.size(); i++)
833 facesWithLayers += _facesWithLayers[i];
834 if ((i + 1) != _facesWithLayers.size())
835 facesWithLayers += ",";
837 for (size_t i = 0; i < _imprintedFaces.size(); i++)
839 imprintedFaces += _imprintedFaces[i];
840 if ((i + 1) != _imprintedFaces.size())
841 imprintedFaces += ",";
844 if (_hexoticIgnoreRidges)
845 run_Hexotic += ignoreRidges;
847 if (_hexoticInvalidElements)
848 run_Hexotic += invalideElements;
850 if (_hexesMinSize > 0)
851 run_Hexotic += mins + minSize;
853 if (_hexesMaxSize > 0)
854 run_Hexotic += maxs + maxSize;
856 if (_hexesMinLevel > 0)
857 run_Hexotic += minl + minLevel;
859 if (_hexesMaxLevel > 0)
860 run_Hexotic += maxl + maxLevel;
862 if (_hexoticSharpAngleThreshold > 0)
863 run_Hexotic += angle + sharpAngle;
865 if (_sizeMaps.begin() != _sizeMaps.end() && forExecutable )
866 run_Hexotic += sizeMap + Hexotic_SizeMap_Prefix;
869 _firstLayerSize > 0 &&
871 !_facesWithLayers.empty())
873 run_Hexotic += comNbLayers + nbLayers;
874 run_Hexotic += comFirstLayerSize + firstLayerSize;
875 run_Hexotic += comDirection + direction;
876 run_Hexotic += comGrowth + growth;
877 run_Hexotic += comFacesWithLayers + facesWithLayers;
878 if (!_imprintedFaces.empty())
879 run_Hexotic += comImptintedFaces + imprintedFaces;
882 run_Hexotic += in + Hexotic_In + out + Hexotic_Out;
883 run_Hexotic += subdom + mode;
885 run_Hexotic += proc + nbproc;
887 run_Hexotic += verb + verbosity;
888 run_Hexotic += maxmem + maxMemory;
890 if (!_textOptions.empty())
891 run_Hexotic += textOptions;
893 return run_Hexotic.ToCString();
896 // TODO : this is a duplication of some code found in BLSURFPlugin_BLSURF find a proper
898 TopoDS_Shape HexoticPlugin_Hexotic::entryToShape(std::string entry)
900 MESSAGE("HexoticPlugin_Hexotic::entryToShape "<<entry );
901 if ( myStudy->_is_nil() )
902 throw SALOME_Exception("MG-Hexa plugin can't work w/o publishing in the study");
903 GEOM::GEOM_Object_var aGeomObj;
904 TopoDS_Shape S = TopoDS_Shape();
905 SALOMEDS::SObject_var aSObj = myStudy->FindObjectID( entry.c_str() );
906 if (!aSObj->_is_nil()) {
907 CORBA::Object_var obj = aSObj->GetObject();
908 aGeomObj = GEOM::GEOM_Object::_narrow(obj);
911 if ( !aGeomObj->_is_nil() )
912 S = smeshGen_i->GeomObjectToShape( aGeomObj.in() );
916 //================================================================================
918 * \brief Produces a .mesh file with the size maps informations to give to Hexotic
920 //================================================================================
921 std::vector<std::string> HexoticPlugin_Hexotic::writeSizeMapFile( MG_Hexotic_API* mgInput,
922 std::string sizeMapPrefix )
924 HexoticPlugin_Hypothesis::THexoticSizeMaps::iterator it;
926 std::vector<ControlPnt> points;
927 // Iterate on the size maps
928 for (it=_sizeMaps.begin(); it!=_sizeMaps.end(); it++)
930 // Step 1 : Get the GEOM object entry and the size
931 // from the _sizeMaps infos
932 std::string anEntry = it->first;
933 double aLocalSize = it->second;
934 TopoDS_Shape aShape = entryToShape( anEntry );
936 // Step 2 : Create the points
937 createControlPoints( aShape, aLocalSize, points );
940 // Write the .mesh size map file
942 std::string myVerticesFile = sizeMapPrefix + ".mesh";
943 std::string mySolFile = sizeMapPrefix + ".sol";
947 mgInput->GmfOpenMesh( myVerticesFile.c_str(), GmfWrite, GMFVERSION, GMFDIMENSION );
949 mgInput->GmfOpenMesh( mySolFile.c_str(), GmfWrite, GMFVERSION, GMFDIMENSION );
951 int pointsNumber = points.size();
954 mgInput->GmfSetKwd( verticesFileID, GmfVertices, pointsNumber );
955 // SolAtVertices Keyword
956 int TypTab[] = {GmfSca};
957 mgInput->GmfSetKwd(solFileID, GmfSolAtVertices, pointsNumber, 1, TypTab);
959 // Read the control points information from the vector and write it into the files
961 std::vector<ControlPnt>::const_iterator points_it;
962 for (points_it = points.begin(); points_it != points.end(); points_it++ )
964 mgInput->GmfSetLin( verticesFileID, GmfVertices, points_it->X(), points_it->Y(), points_it->Z(), 0 );
965 ValTab[0] = points_it->Size();
966 mgInput->GmfSetLin( solFileID, GmfSolAtVertices, ValTab);
970 mgInput->GmfCloseMesh( verticesFileID );
971 mgInput->GmfCloseMesh( solFileID );
973 std::vector<std::string> fileNames(2);
974 fileNames[0] = myVerticesFile;
975 fileNames[1] = mySolFile;
980 //=============================================================================
982 * Here we are going to use the MG-Hexa mesher
984 //=============================================================================
986 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh& aMesh,
987 const TopoDS_Shape& aShape)
989 _computeCanceled = false;
991 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
992 TCollection_AsciiString hexahedraMessage;
994 _nbShape = countShape( &aMesh, TopAbs_SOLID ); // we count the number of shapes
997 // create bounding box for each shape of the compound
1000 TopoDS_Shape *tabShape;
1003 tabShape = new TopoDS_Shape[_nbShape];
1004 tabBox = new double*[_nbShape];
1005 for (int i=0; i<_nbShape; i++)
1006 tabBox[i] = new double[6];
1007 double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1009 TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID);
1010 for (; expBox.More(); expBox.Next()) {
1011 tabShape[iShape] = expBox.Current();
1012 Bnd_Box BoundingBox;
1013 BRepBndLib::Add(expBox.Current(), BoundingBox);
1014 BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
1015 tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
1016 tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
1017 tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
1021 SetParameters(_hypothesis);
1023 TCollection_AsciiString aTmpDir = _hexoticWorkingDirectory.c_str();
1024 TCollection_AsciiString aQuote("");
1027 if ( aTmpDir.Value(aTmpDir.Length()) != '\\' ) aTmpDir += '\\';
1029 if ( aTmpDir.Value(aTmpDir.Length()) != '/' ) aTmpDir += '/';
1031 TCollection_AsciiString Hexotic_In(""), Hexotic_Out, Hexotic_SizeMap_Prefix;
1032 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1033 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic"+getSuffix()+".log"; // log
1035 std::map <int,int> aSmdsToHexoticIdMap;
1036 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1038 MG_Hexotic_API mgHexa( _computeCanceled, _progress );
1040 Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
1041 #ifdef WITH_BLSURFPLUGIN
1042 bool defaultInputFile = true;
1043 if (_blsurfHypo && !_blsurfHypo->GetQuadAllowed()) {
1044 Hexotic_In = _blsurfHypo->GetGMFFile().c_str();
1045 if ( !Hexotic_In.IsEmpty() &&
1046 SMESH_File( _blsurfHypo->GetGMFFile() ).exists() )
1048 mgHexa.SetUseExecutable();
1049 defaultInputFile = false;
1052 if (defaultInputFile) {
1054 Hexotic_In = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
1055 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1056 splitQuads(aMesh); // quadrangles are no longer acceptable as input
1057 if ( mgHexa.IsExecutable() )
1060 cout << "Creating MG-Hexa input mesh file : " << Hexotic_In << std::endl;
1062 writeInput( &mgHexa, Hexotic_In.ToCString(), meshDS );
1063 #ifdef WITH_BLSURFPLUGIN
1066 removeFile( Hexotic_Out );
1070 Hexotic_SizeMap_Prefix = aTmpDir + "Hexotic_SizeMap" + getSuffix();
1071 std::vector<std::string> sizeMapFiles = writeSizeMapFile( &mgHexa, Hexotic_SizeMap_Prefix.ToCString() );
1073 std::string run_Hexotic = getHexoticCommand(aQuote + Hexotic_In + aQuote, aQuote + Hexotic_Out + aQuote, Hexotic_SizeMap_Prefix, mgHexa.IsExecutable() );
1074 run_Hexotic += std::string(" 1> ") + aQuote.ToCString() + aLogFileName.ToCString() + aQuote.ToCString(); // dump into file
1075 mgHexa.SetLogFile( aLogFileName.ToCString() );
1076 cout << "Creating MG-Hexa log file : " << aLogFileName << std::endl;
1079 cout << "MG-Hexa command : " << run_Hexotic << std::endl;
1081 if ( mgHexa.IsExecutable() )
1084 modeFile_In += Hexotic_In;
1085 system( modeFile_In.ToCString() );
1088 aSmdsToHexoticIdMap.clear();
1089 aHexoticIdToNodeMap.clear();
1091 MESSAGE("HexoticPlugin_Hexotic::Compute");
1095 Ok = mgHexa.Compute( run_Hexotic, errStr ); // run
1102 if ( mgHexa.IsExecutable() )
1105 modeFile_Out += Hexotic_Out;
1106 system( modeFile_Out.ToCString() );
1109 SMESH_MesherHelper aHelper( aMesh );
1111 Ok = readResult( &mgHexa, Hexotic_Out.ToCString(),
1113 &aHelper, _nbShape, tabShape, tabBox );
1115 std::string log = mgHexa.GetLog();
1118 hexahedraMessage = "success";
1120 removeFile(Hexotic_Out);
1121 removeFile(Hexotic_In);
1122 //removeFile(aLogFileName);
1123 for( size_t i=0; i<sizeMapFiles.size(); i++)
1125 removeFile( TCollection_AsciiString( sizeMapFiles[i].c_str() ) );
1131 hexahedraMessage = "failed";
1132 if ( mgHexa.IsExecutable() )
1133 cout << "Problem with MG-Hexa output file " << Hexotic_Out.ToCString() << std::endl;
1137 char msgLic[] = " Dlim ";
1138 std::string log = mgHexa.GetLog();
1139 const char* fileBeg = &log[0], *fileEnd = fileBeg + log.size();
1140 if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1141 error("Licence problems.");
1143 if ( !errStr.empty() )
1146 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1150 for (int i=0; i<_nbShape; i++)
1151 delete [] tabBox[i];
1157 if(_computeCanceled)
1158 return error(SMESH_Comment("interruption initiated by user"));
1163 //=============================================================================
1165 * \brief Computes mesh without geometry
1166 * \param aMesh - the mesh
1167 * \param aHelper - helper that must be used for adding elements to \aaMesh
1168 * \retval bool - is a success
1170 * The method is called if ( !aMesh->HasShapeToMesh() )
1172 //=============================================================================
1174 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
1176 _computeCanceled = false;
1178 SMESH_ComputeErrorPtr myError = SMESH_ComputeError::New();
1181 TCollection_AsciiString hexahedraMessage;
1182 TCollection_AsciiString aQuote("");
1186 SetParameters(_hypothesis);
1188 TCollection_AsciiString aTmpDir = _hexoticWorkingDirectory.c_str();//getTmpDir();
1189 TCollection_AsciiString Hexotic_In, Hexotic_Out, Hexotic_SizeMap_Prefix;
1190 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1191 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic"+getSuffix()+".log"; // log
1193 std::map <int,int> aSmdsToHexoticIdMap;
1194 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1196 Hexotic_In = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
1197 Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
1198 Hexotic_SizeMap_Prefix = aTmpDir + "Hexotic_SizeMap";
1200 MG_Hexotic_API mgHexa( _computeCanceled, _progress );
1202 std::vector<std::string> sizeMapFiles = writeSizeMapFile( &mgHexa, Hexotic_SizeMap_Prefix.ToCString() );
1204 std::string run_Hexotic = getHexoticCommand(aQuote + Hexotic_In + aQuote, aQuote + Hexotic_Out + aQuote, Hexotic_SizeMap_Prefix, mgHexa.IsExecutable());
1205 run_Hexotic += std::string(" 1> ") + aQuote.ToCString() + aLogFileName.ToCString() + aQuote.ToCString(); // dump into file
1206 mgHexa.SetLogFile( aLogFileName.ToCString() );
1207 cout << "Creating MG-Hexa log file : " << aLogFileName << std::endl;
1209 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1211 splitQuads(aMesh); // quadrangles are no longer acceptable as input
1214 cout << "Creating MG-Hexa input mesh file : " << Hexotic_In << std::endl;
1215 writeInput( &mgHexa, Hexotic_In.ToCString(), aHelper->GetMeshDS() );
1216 if ( mgHexa.IsExecutable() )
1219 modeFile_In += Hexotic_In;
1220 system( modeFile_In.ToCString() );
1223 aSmdsToHexoticIdMap.clear();
1224 aHexoticIdToNodeMap.clear();
1226 MESSAGE("HexoticPlugin_Hexotic::Compute");
1229 cout << "MG-Hexa command : " << run_Hexotic << std::endl;
1232 Ok = mgHexa.Compute( run_Hexotic, errStr ); // run
1238 if ( mgHexa.IsExecutable() )
1240 modeFile_Out += Hexotic_Out;
1241 system( modeFile_Out.ToCString() );
1244 Ok = Ok && readResult( &mgHexa, Hexotic_Out.ToCString(), this, aHelper );
1246 std::string log = mgHexa.GetLog();
1249 hexahedraMessage = "success";
1253 hexahedraMessage = "failed";
1254 if ( mgHexa.IsExecutable() )
1255 cout << "Problem with MG-Hexa output file " << Hexotic_Out << std::endl;
1257 if ( log.find( " license " ) != std::string::npos ||
1258 log.find( " Dlim " ) != std::string::npos )
1259 error("License problems.");
1261 if ( !errStr.empty() )
1264 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1267 if(_computeCanceled)
1268 return error(SMESH_Comment("interruption initiated by user"));
1269 removeFile(Hexotic_Out);
1270 removeFile(Hexotic_In);
1272 removeFile(aLogFileName);
1273 for( size_t i=0; i<sizeMapFiles.size(); i++)
1275 removeFile( TCollection_AsciiString(sizeMapFiles[i].c_str()) );
1280 //=============================================================================
1284 //=============================================================================
1286 bool HexoticPlugin_Hexotic::Evaluate(SMESH_Mesh& aMesh,
1287 const TopoDS_Shape& aShape,
1288 MapShapeNbElems& aResMap)
1290 std::vector<int> aResVec(SMDSEntity_Last);
1291 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1292 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1293 aResMap.insert(std::make_pair(sm,aResVec));
1295 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1296 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Evaluation is not implemented",this));
1301 void HexoticPlugin_Hexotic::CancelCompute()
1303 _computeCanceled = true;
1306 TCollection_AsciiString aTmpDir = _hexoticWorkingDirectory.c_str(); //getTmpDir();
1307 TCollection_AsciiString Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1308 TCollection_AsciiString cmd = TCollection_AsciiString("ps ux | grep ") + Hexotic_In;
1309 cmd += TCollection_AsciiString(" | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1");
1310 system( cmd.ToCString() );