1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // File : HexoticPlugin_Hexotic.cxx
22 // Author : Lioka RAZAFINDRAZAKA (CEA)
25 #include "HexoticPlugin_Hexotic.hxx"
26 #include "HexoticPlugin_Hypothesis.hxx"
28 #include "utilities.h"
31 #include <sys/sysinfo.h>
41 #include <SMESHDS_Mesh.hxx>
42 #include <SMESHDS_GroupBase.hxx>
43 #include <SMESH_ComputeError.hxx>
44 #include <SMESH_File.hxx>
45 #include <SMESH_Gen.hxx>
46 #include <SMESH_HypoFilter.hxx>
47 #include <SMESH_MesherHelper.hxx>
48 #include <SMESH_subMesh.hxx>
54 #include <Standard_ProgramError.hxx>
56 #include <BRep_Tool.hxx>
57 #include <BRepBndLib.hxx>
58 #include <BRepClass3d_SolidClassifier.hxx>
59 #include <BRepBuilderAPI_MakeVertex.hxx>
60 #include <BRepExtrema_DistShapeShape.hxx>
61 #include <OSD_File.hxx>
62 #include <Precision.hxx>
63 #include <TopExp_Explorer.hxx>
64 #include <TopTools_MapOfShape.hxx>
67 #include <Basics_Utils.hxx>
69 static void removeFile( const TCollection_AsciiString& fileName )
72 OSD_File( fileName ).Remove();
74 catch ( Standard_ProgramError ) {
75 MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
79 //=============================================================================
83 //=============================================================================
85 HexoticPlugin_Hexotic::HexoticPlugin_Hexotic(int hypId, int studyId, SMESH_Gen* gen)
86 : SMESH_3D_Algo(hypId, studyId, gen)
88 MESSAGE("HexoticPlugin_Hexotic::HexoticPlugin_Hexotic");
90 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
91 // _onlyUnaryInput = false;
92 _requireShape = false;
95 _hexoticFilesKept=false;
96 _compatibleHypothesis.push_back("Hexotic_Parameters");
97 #ifdef WITH_BLSURFPLUGIN
100 _compute_canceled = false;
103 //=============================================================================
107 //=============================================================================
109 HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic()
111 MESSAGE("HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic");
115 #ifdef WITH_BLSURFPLUGIN
116 bool HexoticPlugin_Hexotic::CheckBLSURFHypothesis( SMESH_Mesh& aMesh,
117 const TopoDS_Shape& aShape )
119 // MESSAGE("HexoticPlugin_Hexotic::CheckBLSURFHypothesis");
122 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
123 const SMESHDS_Hypothesis* theHyp;
125 // If a BLSURF hypothesis is applied, get it
126 SMESH_HypoFilter blsurfFilter;
127 blsurfFilter.Init( blsurfFilter.HasName( "BLSURF_Parameters" ));
128 std::list<const SMESHDS_Hypothesis *> appliedHyps;
129 aMesh.GetHypotheses( aShape, blsurfFilter, appliedHyps, false );
131 if ( appliedHyps.size() > 0 ) {
132 itl = appliedHyps.begin();
133 theHyp = (*itl); // use only the first hypothesis
134 std::string hypName = theHyp->GetName();
135 if (hypName == "BLSURF_Parameters") {
136 _blsurfHypo = static_cast<const BLSURFPlugin_Hypothesis*> (theHyp);
145 //=============================================================================
149 //=============================================================================
151 bool HexoticPlugin_Hexotic::CheckHypothesis( SMESH_Mesh& aMesh,
152 const TopoDS_Shape& aShape,
153 SMESH_Hypothesis::Hypothesis_Status& aStatus )
155 // MESSAGE("HexoticPlugin_Hexotic::CheckHypothesis");
158 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
159 const SMESHDS_Hypothesis* theHyp;
161 const std::list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
162 int nbHyp = hyps.size();
164 aStatus = SMESH_Hypothesis::HYP_OK;
165 return true; // can work with no hypothesis
169 theHyp = (*itl); // use only the first hypothesis
171 std::string hypName = theHyp->GetName();
172 if (hypName == "Hexotic_Parameters") {
173 _hypothesis = static_cast<const HexoticPlugin_Hypothesis*> (theHyp);
175 aStatus = SMESH_Hypothesis::HYP_OK;
178 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
180 #ifdef WITH_BLSURFPLUGIN
181 CheckBLSURFHypothesis(aMesh, aShape);
184 return aStatus == SMESH_Hypothesis::HYP_OK;
187 //=======================================================================
188 //function : findShape
190 //=======================================================================
192 static TopoDS_Shape findShape(SMDS_MeshNode** t_Node,
194 const TopoDS_Shape* t_Shape,
199 int iShape, nbNode = 8;
201 for ( int i=0; i<3; i++ ) {
203 for ( int j=0; j<nbNode; j++ ) {
204 if ( i == 0) pntCoor[i] += t_Node[j]->X();
205 if ( i == 1) pntCoor[i] += t_Node[j]->Y();
206 if ( i == 2) pntCoor[i] += t_Node[j]->Z();
208 pntCoor[i] /= nbNode;
210 gp_Pnt aPnt(pntCoor[0], pntCoor[1], pntCoor[2]);
212 if ( aShape.IsNull() ) aShape = t_Shape[0];
213 BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
214 if ( !(SC.State() == TopAbs_IN) ) {
216 for (iShape = 0; iShape < nShape && aShape.IsNull(); iShape++) {
217 if ( !( pntCoor[0] < t_Box[iShape][0] || t_Box[iShape][1] < pntCoor[0] ||
218 pntCoor[1] < t_Box[iShape][2] || t_Box[iShape][3] < pntCoor[1] ||
219 pntCoor[2] < t_Box[iShape][4] || t_Box[iShape][5] < pntCoor[2]) ) {
220 BRepClass3d_SolidClassifier SC (t_Shape[iShape], aPnt, Precision::Confusion());
221 if (SC.State() == TopAbs_IN)
222 aShape = t_Shape[iShape];
229 //=======================================================================
230 //function : findEdge
232 //=======================================================================
234 static int findEdge(const SMDS_MeshNode* aNode,
235 const SMESHDS_Mesh* aMesh,
237 const TopoDS_Shape* t_Edge) {
239 TopoDS_Shape aPntShape, foundEdge;
240 TopoDS_Vertex aVertex;
241 gp_Pnt aPnt( aNode->X(), aNode->Y(), aNode->Z() );
244 double nearest = RealLast(), *t_Dist;
245 double epsilon = Precision::Confusion();
247 t_Dist = new double[ nEdge ];
248 aPntShape = BRepBuilderAPI_MakeVertex( aPnt ).Shape();
249 aVertex = TopoDS::Vertex( aPntShape );
251 for ( ind=0; ind < nEdge; ind++ ) {
252 BRepExtrema_DistShapeShape aDistance ( aVertex, t_Edge[ind] );
253 t_Dist[ind] = aDistance.Value();
254 if ( t_Dist[ind] < nearest ) {
255 nearest = t_Dist[ind];
256 foundEdge = t_Edge[ind];
258 if ( nearest < epsilon )
264 return aMesh->ShapeToIndex( foundEdge );
267 //=======================================================================
268 //function : getNbShape
270 //=======================================================================
272 static int getNbShape(std::string aFile, std::string aString, int defaultValue=0) {
273 int number = defaultValue;
275 std::ifstream file(aFile.c_str());
276 while ( !file.eof() ) {
277 getline( file, aLine);
278 if ( aLine == aString ) {
279 getline( file, aLine);
280 std::istringstream stringFlux( aLine );
281 stringFlux >> number;
282 number = ( number + defaultValue + std::abs(number - defaultValue) ) / 2;
290 //=======================================================================
291 //function : countShape
293 //=======================================================================
295 template < class Mesh, class Shape >
296 static int countShape( Mesh* mesh, Shape shape ) {
297 TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
298 TopTools_MapOfShape mapShape;
300 for ( ; expShape.More(); expShape.Next() ) {
301 if (mapShape.Add(expShape.Current())) {
308 //=======================================================================
309 //function : getShape
311 //=======================================================================
313 template < class Mesh, class Shape, class Tab >
314 void getShape(Mesh* mesh, Shape shape, Tab *t_Shape) {
315 TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
316 TopTools_MapOfShape mapShape;
317 for ( int i=0; expShape.More(); expShape.Next() ) {
318 if (mapShape.Add(expShape.Current())) {
319 t_Shape[i] = expShape.Current();
326 //=======================================================================
327 //function : printWarning
329 //=======================================================================
331 static void printWarning(const int nbExpected, std::string aString, const int nbFound) {
333 cout << "WARNING : " << nbExpected << " " << aString << " expected, Hexotic has found " << nbFound << std::endl;
334 cout << "=======" << std::endl;
339 //=======================================================================
340 //function : removeHexoticFiles
342 //=======================================================================
344 static void removeHexoticFiles(TCollection_AsciiString file_In, TCollection_AsciiString file_Out) {
345 removeFile( file_In );
346 removeFile( file_Out );
349 //=======================================================================
350 //function : readResult
351 //purpose : Read GMF file in case of a mesh with geometry
352 //=======================================================================
354 static bool readResult(std::string theFile,
355 #ifdef WITH_SMESH_CANCEL_COMPUTE
356 HexoticPlugin_Hexotic* theAlgo,
358 SMESHDS_Mesh* theMesh,
360 const TopoDS_Shape* tabShape,
363 // ---------------------------------
364 // Optimisation of the plugin ...
365 // Retrieve the correspondance edge --> shape
366 // (which is very costly) only when user
367 // has defined at least one group of edges
368 // which should be rare for a 3d mesh !
369 // ---------------------------------
371 bool retrieve_edges = false;
372 const std::set<SMESHDS_GroupBase*>& aGroups = theMesh->GetGroups();
373 set<SMESHDS_GroupBase*>::const_iterator GrIt = aGroups.begin();
374 for (; GrIt != aGroups.end(); GrIt++)
376 SMESHDS_GroupBase* aGrp = *GrIt;
379 if ( aGrp->GetType() == SMDSAbs_Edge )
381 retrieve_edges = true;
386 // ---------------------------------
387 // Read generated elements and nodes
388 // ---------------------------------
391 TopoDS_Vertex aVertex;
393 int EndOfFile = 0, nbElem = 0, nField = 9, nbRef = 0;
394 int aHexoticNodeID = 0, shapeID, hexoticShapeID;
395 const int IdShapeRef = 2;
396 int *tabID, *tabRef, *nodeAssigne;
397 bool *tabDummy, hasDummy = false;
398 double epsilon = Precision::Confusion();
399 std::map <std::string,int> mapField;
400 SMDS_MeshNode** HexoticNode;
401 TopoDS_Shape *tabCorner, *tabEdge;
403 const int nbDomains = countShape( theMesh, TopAbs_SHELL );
404 const int holeID = -1;
406 // tabID = new int[nbShape];
407 tabID = new int[nbDomains];
408 tabRef = new int[nField];
409 tabDummy = new bool[nField];
411 for (int i=0; i<nbDomains; i++)
413 if ( nbDomains == 1 )
414 tabID[0] = theMesh->ShapeToIndex( tabShape[0] );
416 mapField["MeshVersionFormatted"] = 0; tabRef[0] = 0; tabDummy[0] = false;
417 mapField["Dimension"] = 1; tabRef[1] = 0; tabDummy[1] = false;
418 mapField["Vertices"] = 2; tabRef[2] = 3; tabDummy[2] = true;
419 mapField["Corners"] = 3; tabRef[3] = 1; tabDummy[3] = false;
420 mapField["Edges"] = 4; tabRef[4] = 2; tabDummy[4] = true;
421 mapField["Ridges"] = 5; tabRef[5] = 1; tabDummy[5] = false;
422 mapField["Quadrilaterals"] = 6; tabRef[6] = 4; tabDummy[6] = true;
423 mapField["Hexahedra"] = 7; tabRef[7] = 8; tabDummy[7] = true;
424 mapField["End"] = 8; tabRef[8] = 0; tabDummy[0] = false;
426 SMDS_NodeIteratorPtr itOnHexoticInputNode = theMesh->nodesIterator();
427 while ( itOnHexoticInputNode->more() )
428 theMesh->RemoveNode( itOnHexoticInputNode->next() );
430 int nbVertices = getNbShape(theFile, "Vertices");
431 int nbCorners = getNbShape(theFile, "Corners", countShape( theMesh, TopAbs_VERTEX ));
432 int nbShapeEdge = countShape( theMesh, TopAbs_EDGE );
434 tabCorner = new TopoDS_Shape[ nbCorners ];
435 tabEdge = new TopoDS_Shape[ nbShapeEdge ];
436 nodeAssigne = new int[ nbVertices + 1 ];
437 HexoticNode = new SMDS_MeshNode*[ nbVertices + 1 ];
439 getShape(theMesh, TopAbs_VERTEX, tabCorner);
440 getShape(theMesh, TopAbs_EDGE, tabEdge);
442 MESSAGE("Read " << theFile << " file");
443 std::ifstream fileRes(theFile.c_str());
446 while ( EndOfFile == 0 ) {
450 if (mapField.count(token)) {
451 nField = mapField[token];
452 nbRef = tabRef[nField];
453 hasDummy = tabDummy[nField];
461 if ( nField < (mapField.size() - 1) && nField >= 0 )
465 case 0: { // "MeshVersionFormatted"
466 MESSAGE(token << " " << nbElem);
469 case 1: { // "Dimension"
470 MESSAGE("Mesh dimension " << nbElem << "D");
473 case 2: { // "Vertices"
474 MESSAGE("Read " << nbElem << " " << token);
477 SMDS_MeshNode * aHexoticNode;
479 coord = new double[nbRef];
480 for ( int iElem = 0; iElem < nbElem; iElem++ ) {
481 #ifdef WITH_SMESH_CANCEL_COMPUTE
482 if(theAlgo->computeCanceled())
487 aHexoticID = iElem + 1;
488 for ( int iCoord = 0; iCoord < 3; iCoord++ )
489 fileRes >> coord[ iCoord ];
491 aHexoticNode = theMesh->AddNode(coord[0], coord[1], coord[2]);
492 HexoticNode[ aHexoticID ] = aHexoticNode;
493 nodeAssigne[ aHexoticID ] = 0;
501 case 6: // "Quadrilaterals"
502 case 7: { // "Hexahedra"
503 MESSAGE("Read " << nbElem << " " << token);
504 SMDS_MeshNode** node;
505 int nodeDim, *nodeID;
506 SMDS_MeshElement * aHexoticElement = 0;
508 node = new SMDS_MeshNode*[ nbRef ];
509 nodeID = new int[ nbRef ];
510 for ( int iElem = 0; iElem < nbElem; iElem++ ) {
511 #ifdef WITH_SMESH_CANCEL_COMPUTE
512 if(theAlgo->computeCanceled())
517 for ( int iRef = 0; iRef < nbRef; iRef++ ) {
518 fileRes >> aHexoticNodeID; // read nbRef aHexoticNodeID
519 node[ iRef ] = HexoticNode[ aHexoticNodeID ];
520 nodeID[ iRef ] = aHexoticNodeID;
525 case 3: { // "Corners"
527 gp_Pnt HexoticPnt ( node[0]->X(), node[0]->Y(), node[0]->Z() );
528 for ( int i=0; i<nbElem; i++ ) {
529 aVertex = TopoDS::Vertex( tabCorner[i] );
530 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
531 if ( aPnt.Distance( HexoticPnt ) < epsilon )
538 aHexoticElement = theMesh->AddEdge( node[0], node[1] );
540 if ( nodeAssigne[ nodeID[0] ] == 0 || nodeAssigne[ nodeID[0] ] == 2 )
543 shapeID = findEdge( node[iNode], theMesh, nbShapeEdge, tabEdge );
548 case 5: { // "Ridges"
551 case 6: { // "Quadrilaterals"
553 aHexoticElement = theMesh->AddFace( node[0], node[1], node[2], node[3] );
557 case 7: { // "Hexahedra"
559 if ( nbDomains > 1 ) {
560 hexoticShapeID = dummy - IdShapeRef;
561 if ( tabID[ hexoticShapeID ] == 0 ) {
562 aShape = findShape(node, aShape, tabShape, tabBox, nbShape);
563 shapeID = aShape.IsNull() ? holeID : theMesh->ShapeToIndex( aShape );
564 tabID[ hexoticShapeID ] = shapeID;
567 shapeID = tabID[ hexoticShapeID ];
568 if ( iElem == (nbElem - 1) ) {
569 int shapeAssociated = 0;
570 for ( int i=0; i<nbDomains; i++ ) {
572 shapeAssociated += 1;
574 if ( shapeAssociated != nbShape )
575 printWarning(nbShape, "domains", shapeAssociated);
581 if ( shapeID != holeID )
582 aHexoticElement = theMesh->AddVolume( node[0], node[3], node[2], node[1], node[4], node[7], node[6], node[5] );
587 if ( token != "Ridges" && ( shapeID > 0 || token == "Corners")) {
588 for ( int i=0; i<nbRef; i++ ) {
589 if ( nodeAssigne[ nodeID[i] ] == 0 ) {
590 if ( token == "Corners" ) theMesh->SetNodeOnVertex( node[0], aVertex );
591 else if ( token == "Edges" ) theMesh->SetNodeOnEdge( node[i], shapeID );
592 else if ( token == "Quadrilaterals" ) theMesh->SetNodeOnFace( node[i], shapeID );
593 else if ( token == "Hexahedra" ) theMesh->SetNodeInVolume( node[i], shapeID );
594 nodeAssigne[ nodeID[i] ] = nodeDim;
597 if ( token != "Corners" && aHexoticElement )
598 theMesh->SetMeshElementOnShape( aHexoticElement, shapeID );
607 MESSAGE("End of " << theFile << " file");
611 MESSAGE("Unknown Token: " << token);
617 // remove nodes in holes
620 SMESHDS_SubMesh* subMesh;
621 for ( int i = 1; i <= nbVertices; ++i )
622 if ( HexoticNode[i]->NbInverseElements() == 0 )
624 subMesh = HexoticNode[i]->getshapeId() > 0 ? theMesh->MeshElements(HexoticNode[i]->getshapeId() ) : 0;
625 theMesh->RemoveFreeNode( HexoticNode[i], subMesh, /*fromGroups=*/false );
633 delete [] nodeAssigne;
634 delete [] HexoticNode;
639 //=======================================================================
640 //function : readResult
641 //purpose : Read GMF file in case of a mesh w/o geometry
642 //=======================================================================
644 static bool readResult(std::string theFile,
645 #ifdef WITH_SMESH_CANCEL_COMPUTE
646 HexoticPlugin_Hexotic* theAlgo,
648 SMESH_MesherHelper* theHelper)
650 SMESHDS_Mesh* theMesh = theHelper->GetMeshDS();
652 // ---------------------------------
653 // Read generated elements and nodes
654 // ---------------------------------
657 const int nbField = 9;
658 int nField, EndOfFile = 0, nbElem = 0, nbRef = 0;
659 int aHexoticNodeID = 0, shapeID;
660 int tabRef[nbField], *nodeAssigne;
661 bool tabDummy[nbField], hasDummy = false;
662 std::map <std::string,int> mapField;
663 SMDS_MeshNode** HexoticNode;
665 mapField["MeshVersionFormatted"] = 0; tabRef[0] = 0; tabDummy[0] = false;
666 mapField["Dimension"] = 1; tabRef[1] = 0; tabDummy[1] = false;
667 mapField["Vertices"] = 2; tabRef[2] = 3; tabDummy[2] = true;
668 mapField["Corners"] = 3; tabRef[3] = 1; tabDummy[3] = false;
669 mapField["Edges"] = 4; tabRef[4] = 2; tabDummy[4] = true;
670 mapField["Ridges"] = 5; tabRef[5] = 1; tabDummy[5] = false;
671 mapField["Quadrilaterals"] = 6; tabRef[6] = 4; tabDummy[6] = true;
672 mapField["Hexahedra"] = 7; tabRef[7] = 8; tabDummy[7] = true;
673 mapField["End"] = 8; tabRef[8] = 0; tabDummy[8] = false;
675 theHelper->GetMesh()->Clear();
677 int nbVertices = getNbShape(theFile, "Vertices");
678 HexoticNode = new SMDS_MeshNode*[ nbVertices + 1 ];
679 nodeAssigne = new int[ nbVertices + 1 ];
681 MESSAGE("Read " << theFile << " file");
682 std::ifstream fileRes(theFile.c_str());
690 if (mapField.count(token)) {
691 nField = mapField[token];
692 nbRef = tabRef[nField];
693 hasDummy = tabDummy[nField];
701 if ( nField < (mapField.size() - 1) && nField >= 0 )
705 case 0: { // "MeshVersionFormatted"
706 MESSAGE(token << " " << nbElem);
709 case 1: { // "Dimension"
710 MESSAGE("Mesh dimension " << nbElem << "D");
713 case 2: { // "Vertices"
714 MESSAGE("Read " << nbElem << " " << token);
717 SMDS_MeshNode * aHexoticNode;
719 for ( int iElem = 0; iElem < nbElem; iElem++ ) {
720 #ifdef WITH_SMESH_CANCEL_COMPUTE
721 if(theAlgo->computeCanceled())
726 aHexoticID = iElem + 1;
727 for ( int iCoord = 0; iCoord < 3; iCoord++ )
728 fileRes >> coord[ iCoord ];
730 aHexoticNode = theMesh->AddNode(coord[0], coord[1], coord[2]);
731 HexoticNode[ aHexoticID ] = aHexoticNode;
732 nodeAssigne[ aHexoticID ] = 0;
739 case 6: // "Quadrilaterals"
740 case 7: { // "Hexahedra"
741 MESSAGE("Read " << nbElem << " " << token);
742 std::vector< SMDS_MeshNode* > node( nbRef );
743 std::vector< int > nodeID( nbRef );
745 for ( int iElem = 0; iElem < nbElem; iElem++ )
747 #ifdef WITH_SMESH_CANCEL_COMPUTE
748 if(theAlgo->computeCanceled())
753 for ( int iRef = 0; iRef < nbRef; iRef++ )
755 fileRes >> aHexoticNodeID; // read nbRef aHexoticNodeID
756 node [ iRef ] = HexoticNode[ aHexoticNodeID ];
757 nodeID[ iRef ] = aHexoticNodeID;
764 theHelper->AddEdge( node[0], node[1] ); break;
765 case 6: // "Quadrilaterals"
766 theMesh->AddFace( node[0], node[1], node[2], node[3] ); break;
767 case 7: // "Hexahedra"
768 theHelper->AddVolume( node[0], node[3], node[2], node[1],
769 node[4], node[7], node[6], node[5] ); break;
773 for ( int iRef = 0; iRef < nbRef; iRef++ )
774 nodeAssigne[ nodeID[ iRef ]] = 1;
780 MESSAGE("End of " << theFile << " file");
784 MESSAGE("Unknown Token: " << token);
790 shapeID = theHelper->GetSubShapeID();
791 for ( int i = 0; i < nbVertices; ++i )
792 if ( !nodeAssigne[ i+1 ])
793 theMesh->SetNodeInVolume( HexoticNode[ i+1 ], shapeID );
795 delete [] HexoticNode;
796 delete [] nodeAssigne;
800 //=============================================================================
802 * Pass parameters to Hexotic
804 //=============================================================================
806 void HexoticPlugin_Hexotic::SetParameters(const HexoticPlugin_Hypothesis* hyp) {
808 MESSAGE("HexoticPlugin_Hexotic::SetParameters");
810 _hexesMinLevel = hyp->GetHexesMinLevel();
811 _hexesMaxLevel = hyp->GetHexesMaxLevel();
812 _hexesMinSize = hyp->GetMinSize();
813 _hexesMaxSize = hyp->GetMaxSize();
814 _hexoticIgnoreRidges = hyp->GetHexoticIgnoreRidges();
815 _hexoticInvalidElements = hyp->GetHexoticInvalidElements();
816 _hexoticSharpAngleThreshold = hyp->GetHexoticSharpAngleThreshold();
817 _hexoticNbProc = hyp->GetHexoticNbProc();
818 _hexoticWorkingDirectory = hyp->GetHexoticWorkingDirectory();
819 _hexoticVerbosity = hyp->GetHexoticVerbosity();
820 _hexoticMaxMemory = hyp->GetHexoticMaxMemory();
821 _hexoticSdMode = hyp->GetHexoticSdMode();
825 cout << "WARNING : The Hexotic default parameters are taken into account" << std::endl;
826 cout << "=======" << std::endl;
827 _hexesMinLevel = hyp->GetDefaultHexesMinLevel();
828 _hexesMaxLevel = hyp->GetDefaultHexesMaxLevel();
829 _hexesMinSize = hyp->GetDefaultMinSize();
830 _hexesMaxSize = hyp->GetDefaultMaxSize();
831 _hexoticIgnoreRidges = hyp->GetDefaultHexoticIgnoreRidges();
832 _hexoticInvalidElements = hyp->GetDefaultHexoticInvalidElements();
833 _hexoticSharpAngleThreshold = hyp->GetDefaultHexoticSharpAngleThreshold();
834 _hexoticNbProc = hyp->GetDefaultHexoticNbProc();
835 _hexoticWorkingDirectory = hyp->GetDefaultHexoticWorkingDirectory();
836 _hexoticVerbosity = hyp->GetDefaultHexoticVerbosity();
837 _hexoticMaxMemory = hyp->GetDefaultHexoticMaxMemory();
838 _hexoticSdMode = hyp->GetDefaultHexoticSdMode();
842 //=======================================================================
843 //function : getTmpDir
845 //=======================================================================
847 static TCollection_AsciiString getTmpDir()
849 TCollection_AsciiString aTmpDir;
851 char *Tmp_dir = getenv("SALOME_TMP_DIR");
853 if(Tmp_dir == NULL) {
854 Tmp_dir = getenv("TEMP");
856 Tmp_dir = getenv("TMP");
859 if(Tmp_dir != NULL) {
862 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
864 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
869 aTmpDir = TCollection_AsciiString("C:\\");
871 aTmpDir = TCollection_AsciiString("/tmp/");
877 //=======================================================================
878 //function : getSuffix
879 //purpose : Returns a suffix that will be unique for the current process
880 //=======================================================================
882 static TCollection_AsciiString getSuffix()
884 TCollection_AsciiString aSuffix = "";
886 aSuffix += getenv("USER");
888 aSuffix += Kernel_Utils::GetHostname().c_str();
895 //================================================================================
897 * \brief Returns a command to run Hexotic mesher
899 //================================================================================
901 std::string HexoticPlugin_Hexotic::getHexoticCommand(const TCollection_AsciiString& Hexotic_In,
902 const TCollection_AsciiString& Hexotic_Out) const
905 cout << "Hexotic execution..." << std::endl;
906 cout << _name << " parameters :" << std::endl;
907 cout << " " << _name << " Verbosity = " << _hexoticVerbosity << std::endl;
908 cout << " " << _name << " Max Memory = " << _hexoticMaxMemory << std::endl;
909 cout << " " << _name << " Segments Min Level = " << _hexesMinLevel << std::endl;
910 cout << " " << _name << " Segments Max Level = " << _hexesMaxLevel << std::endl;
911 cout << " " << _name << " Segments Min Size = " << _hexesMinSize << std::endl;
912 cout << " " << _name << " Segments Max Size = " << _hexesMaxSize << std::endl;
913 cout << " " << "Hexotic can ignore ridges : " << (_hexoticIgnoreRidges ? "yes":"no") << std::endl;
914 cout << " " << "Hexotic authorize invalide elements : " << ( _hexoticInvalidElements ? "yes":"no") << std::endl;
915 cout << " " << _name << " Sharp angle threshold = " << _hexoticSharpAngleThreshold << " degrees" << std::endl;
916 cout << " " << _name << " Number of threads = " << _hexoticNbProc << std::endl;
917 cout << " " << _name << " Working directory = \"" << _hexoticWorkingDirectory << "\"" << std::endl;
918 cout << " " << _name << " Sub. Dom mode = " << _hexoticSdMode << std::endl;
920 TCollection_AsciiString run_Hexotic( "hexotic" );
922 TCollection_AsciiString minl = " -minl ", maxl = " -maxl ", angle = " -ra ";
923 TCollection_AsciiString mins = " -mins ", maxs = " -maxs ";
924 TCollection_AsciiString in = " -in ", out = " -out ";
925 TCollection_AsciiString ignoreRidges = " -nr ", invalideElements = " -inv ";
926 TCollection_AsciiString subdom = " -sd ", sharp = " -sharp ";
927 TCollection_AsciiString proc = " -nproc ";
928 TCollection_AsciiString verb = " -v ";
929 TCollection_AsciiString maxmem = " -m ";
931 TCollection_AsciiString minLevel, maxLevel, minSize, maxSize, sharpAngle, mode, nbproc, verbosity, maxMemory;
932 minLevel = _hexesMinLevel;
933 maxLevel = _hexesMaxLevel;
934 minSize = _hexesMinSize;
935 maxSize = _hexesMaxSize;
936 sharpAngle = _hexoticSharpAngleThreshold;
937 mode = _hexoticSdMode;
938 nbproc = _hexoticNbProc;
939 verbosity = _hexoticVerbosity;
940 maxMemory = _hexoticMaxMemory;
942 if (_hexoticIgnoreRidges)
943 run_Hexotic += ignoreRidges;
945 if (_hexoticInvalidElements)
946 run_Hexotic += invalideElements;
948 if (_hexesMinSize > 0)
949 run_Hexotic += mins + minSize;
951 if (_hexesMaxSize > 0)
952 run_Hexotic += maxs + maxSize;
954 if (_hexesMinLevel > 0)
955 run_Hexotic += minl + minLevel;
957 if (_hexesMaxLevel > 0)
958 run_Hexotic += maxl + maxLevel;
960 if (_hexoticSharpAngleThreshold > 0)
961 run_Hexotic += angle + sharpAngle;
963 run_Hexotic += in + Hexotic_In + out + Hexotic_Out;
964 run_Hexotic += subdom + mode;
965 run_Hexotic += proc + nbproc;
966 run_Hexotic += verb + verbosity;
967 run_Hexotic += maxmem + maxMemory;
969 return run_Hexotic.ToCString();
972 //=============================================================================
974 * Here we are going to use the Hexotic mesher
976 //=============================================================================
978 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh& aMesh,
979 const TopoDS_Shape& aShape)
981 #ifdef WITH_SMESH_CANCEL_COMPUTE
982 _compute_canceled = false;
985 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
986 TCollection_AsciiString hexahedraMessage;
988 if (_iShape == 0 && _nbShape == 0) {
989 _nbShape = countShape( meshDS, TopAbs_SOLID ); // we count the number of shapes
992 // to prevent from displaying error message after computing,
993 // SetIsAlwaysComputed( true ) to empty sub-meshes
994 vector< SMESH_subMesh* > subMeshesAlwaysComp;
995 for ( int i = 0; i < _nbShape; ++i )
996 if ( SMESH_subMesh* sm = aMesh.GetSubMeshContaining( aShape ))
998 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
999 /*complexShapeFirst=*/false);
1000 while ( smIt->more() )
1003 if ( !sm->IsMeshComputed() )
1005 sm->SetIsAlwaysComputed( true );
1006 subMeshesAlwaysComp.push_back( sm );
1013 if (_iShape == _nbShape ) {
1015 // create bounding box for each shape of the compound
1018 TopoDS_Shape *tabShape;
1021 tabShape = new TopoDS_Shape[_nbShape];
1022 tabBox = new double*[_nbShape];
1023 for (int i=0; i<_nbShape; i++)
1024 tabBox[i] = new double[6];
1025 double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1027 TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID);
1028 for (; expBox.More(); expBox.Next()) {
1029 tabShape[iShape] = expBox.Current();
1030 Bnd_Box BoundingBox;
1031 BRepBndLib::Add(expBox.Current(), BoundingBox);
1032 BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
1033 tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
1034 tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
1035 tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
1039 SetParameters(_hypothesis);
1041 // TCollection_AsciiString aTmpDir = getTmpDir();
1042 TCollection_AsciiString aTmpDir = TCollection_AsciiString(_hexoticWorkingDirectory.c_str());
1044 if ( aTmpDir.Value(aTmpDir.Length()) != '\\' ) aTmpDir += '\\';
1046 if ( aTmpDir.Value(aTmpDir.Length()) != '/' ) aTmpDir += '/';
1048 TCollection_AsciiString Hexotic_In(""), Hexotic_Out;
1049 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1050 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic"+getSuffix()+".log"; // log
1052 std::map <int,int> aSmdsToHexoticIdMap;
1053 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1055 Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
1056 #ifdef WITH_BLSURFPLUGIN
1057 bool defaultInputFile = true;
1058 if (_blsurfHypo && !_blsurfHypo->GetQuadAllowed()) {
1059 Hexotic_In = TCollection_AsciiString(_blsurfHypo->GetGMFFile().c_str());
1060 if (Hexotic_In != "")
1061 defaultInputFile = false;
1063 if (defaultInputFile) {
1065 Hexotic_In = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
1066 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1068 cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1069 aMesh.ExportGMF(Hexotic_In.ToCString(), meshDS, true);
1070 #ifdef WITH_BLSURFPLUGIN
1073 removeFile( Hexotic_Out );
1078 std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1079 run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString(); // dump into file
1082 cout << "Hexotic command : " << run_Hexotic << std::endl;
1084 modeFile_In += Hexotic_In;
1085 system( modeFile_In.ToCString() );
1086 aSmdsToHexoticIdMap.clear();
1087 aHexoticIdToNodeMap.clear();
1089 MESSAGE("HexoticPlugin_Hexotic::Compute");
1091 int status = system( run_Hexotic.data() );
1097 std::ifstream fileRes( Hexotic_Out.ToCString() );
1098 modeFile_Out += Hexotic_Out;
1099 system( modeFile_Out.ToCString() );
1100 if ( ! fileRes.fail() ) {
1101 Ok = readResult( Hexotic_Out.ToCString(),
1102 #ifdef WITH_SMESH_CANCEL_COMPUTE
1105 meshDS, _nbShape, tabShape, tabBox );
1107 /*********************
1108 // TODO: Detect and remove elements in holes in case of sd mode = 4
1109 // Remove previous nodes and elements
1110 SMDS_ElemIteratorPtr itElement = meshDS->elementsIterator();
1111 SMDS_NodeIteratorPtr itNode = meshDS->nodesIterator();
1113 while ( itElement->more() )
1114 meshDS->RemoveElement( itElement->next() );
1115 while ( itNode->more() )
1116 meshDS->RemoveNode( itNode->next() );
1118 SMESH_ComputeErrorPtr myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1121 hexahedraMessage = "success";
1122 removeFile(Hexotic_Out);
1123 removeFile(Hexotic_In);
1124 removeFile(aLogFileName);
1127 hexahedraMessage = "failed";
1131 hexahedraMessage = "failed";
1132 cout << "Problem with Hexotic output file " << Hexotic_Out.ToCString() << std::endl;
1135 SMESH_File logFile( aLogFileName.ToCString() );
1136 if ( !logFile.eof() )
1138 char msgLic[] = " Dlim ";
1139 const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1140 if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1141 error("Licence problems.");
1143 if ( status > 0 && WEXITSTATUS(status) == 127 )
1144 error("hexotic: command not found");
1146 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1149 // restore "always computed" flag of sub-meshes (0022127)
1150 for ( size_t iSM = 0; iSM < subMeshesAlwaysComp.size(); ++iSM )
1151 subMeshesAlwaysComp[ iSM ]->SetIsAlwaysComputed( false );
1154 for (int i=0; i<_nbShape; i++)
1155 delete [] tabBox[i];
1160 #ifdef WITH_SMESH_CANCEL_COMPUTE
1161 if(_compute_canceled)
1162 return error(SMESH_Comment("interruption initiated by user"));
1167 //=============================================================================
1169 * \brief Computes mesh without geometry
1170 * \param aMesh - the mesh
1171 * \param aHelper - helper that must be used for adding elements to \aaMesh
1172 * \retval bool - is a success
1174 * The method is called if ( !aMesh->HasShapeToMesh() )
1176 //=============================================================================
1178 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
1180 #ifdef WITH_SMESH_CANCEL_COMPUTE
1181 _compute_canceled = false;
1184 SMESH_ComputeErrorPtr myError = SMESH_ComputeError::New();
1187 TCollection_AsciiString hexahedraMessage;
1189 SetParameters(_hypothesis);
1191 TCollection_AsciiString aTmpDir = getTmpDir();
1192 TCollection_AsciiString Hexotic_In, Hexotic_Out;
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";
1202 std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1203 run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString(); // dump into file
1205 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1208 cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1209 aMesh.ExportGMF(Hexotic_In.ToCString(), aHelper->GetMeshDS());
1210 modeFile_In += Hexotic_In;
1211 system( modeFile_In.ToCString() );
1212 aSmdsToHexoticIdMap.clear();
1213 aHexoticIdToNodeMap.clear();
1215 MESSAGE("HexoticPlugin_Hexotic::Compute");
1218 cout << "Hexotic command : " << run_Hexotic << std::endl;
1219 system( run_Hexotic.data() );
1225 std::ifstream fileRes( Hexotic_Out.ToCString() );
1226 modeFile_Out += Hexotic_Out;
1227 system( modeFile_Out.ToCString() );
1228 if ( ! fileRes.fail() ) {
1229 Ok = readResult( Hexotic_Out.ToCString(),
1230 #ifdef WITH_SMESH_CANCEL_COMPUTE
1236 // Remove previous nodes and elements
1237 SMDS_ElemIteratorPtr itElement = aHelper->GetMeshDS()->elementsIterator();
1238 SMDS_NodeIteratorPtr itNode = aHelper->GetMeshDS()->nodesIterator();
1240 while ( itElement->more() )
1241 aHelper->GetMeshDS()->RemoveElement( itElement->next() );
1242 while ( itNode->more() )
1243 aHelper->GetMeshDS()->RemoveNode( itNode->next() );
1246 myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1248 itElement = aHelper->GetMeshDS()->elementsIterator();
1249 itNode = aHelper->GetMeshDS()->nodesIterator();
1251 // Assign nodes and elements to the pseudo shape
1252 while ( itNode->more() )
1253 aHelper->GetMeshDS()->SetNodeInVolume(itNode->next(), 1);
1254 while ( itElement->more() )
1255 aHelper->GetMeshDS()->SetMeshElementOnShape(itElement->next(), 1);
1259 hexahedraMessage = "success";
1261 hexahedraMessage = "failed";
1265 myError->myName = COMPERR_EXCEPTION;
1267 hexahedraMessage = "failed";
1268 cout << "Problem with Hexotic output file " << Hexotic_Out << std::endl;
1270 SMESH_File logFile( aLogFileName.ToCString() );
1271 if ( !logFile.eof() )
1273 char msgLic[] = " Dlim ";
1274 const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1275 if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1276 return error("Licence problems.");
1278 return error(SMESH_Comment("Problem with Hexotic output file ")<<Hexotic_Out);
1280 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1283 #ifdef WITH_SMESH_CANCEL_COMPUTE
1284 if(_compute_canceled)
1285 return error(SMESH_Comment("interruption initiated by user"));
1287 removeFile(Hexotic_Out);
1288 removeFile(Hexotic_In);
1289 removeFile(aLogFileName);
1292 return myError->IsOK();
1296 //=============================================================================
1300 //=============================================================================
1302 bool HexoticPlugin_Hexotic::Evaluate(SMESH_Mesh& aMesh,
1303 const TopoDS_Shape& aShape,
1304 MapShapeNbElems& aResMap)
1306 std::vector<int> aResVec(SMDSEntity_Last);
1307 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1308 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1309 aResMap.insert(std::make_pair(sm,aResVec));
1311 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1312 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Evaluation is not implemented",this));
1317 #ifdef WITH_SMESH_CANCEL_COMPUTE
1318 void HexoticPlugin_Hexotic::CancelCompute()
1320 _compute_canceled = true;
1323 TCollection_AsciiString aTmpDir = getTmpDir();
1324 TCollection_AsciiString Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1325 TCollection_AsciiString cmd = TCollection_AsciiString("ps ux | grep ") + Hexotic_In;
1326 cmd += TCollection_AsciiString(" | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1");
1327 system( cmd.ToCString() );