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 static void removeFile( const TCollection_AsciiString& fileName )
70 OSD_File( fileName ).Remove();
72 catch ( Standard_ProgramError ) {
73 MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
77 //=============================================================================
81 //=============================================================================
83 HexoticPlugin_Hexotic::HexoticPlugin_Hexotic(int hypId, int studyId, SMESH_Gen* gen)
84 : SMESH_3D_Algo(hypId, studyId, gen)
86 MESSAGE("HexoticPlugin_Hexotic::HexoticPlugin_Hexotic");
88 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
89 // _onlyUnaryInput = false;
90 _requireShape = false;
93 _hexoticFilesKept=false;
94 _compatibleHypothesis.push_back("Hexotic_Parameters");
95 #ifdef WITH_BLSURFPLUGIN
98 _compute_canceled = false;
101 //=============================================================================
105 //=============================================================================
107 HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic()
109 MESSAGE("HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic");
113 #ifdef WITH_BLSURFPLUGIN
114 bool HexoticPlugin_Hexotic::CheckBLSURFHypothesis( SMESH_Mesh& aMesh,
115 const TopoDS_Shape& aShape )
117 // MESSAGE("HexoticPlugin_Hexotic::CheckBLSURFHypothesis");
120 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
121 const SMESHDS_Hypothesis* theHyp;
123 // If a BLSURF hypothesis is applied, get it
124 SMESH_HypoFilter blsurfFilter;
125 blsurfFilter.Init( blsurfFilter.HasName( "BLSURF_Parameters" ));
126 std::list<const SMESHDS_Hypothesis *> appliedHyps;
127 aMesh.GetHypotheses( aShape, blsurfFilter, appliedHyps, false );
129 if ( appliedHyps.size() > 0 ) {
130 itl = appliedHyps.begin();
131 theHyp = (*itl); // use only the first hypothesis
132 std::string hypName = theHyp->GetName();
133 if (hypName == "BLSURF_Parameters") {
134 _blsurfHypo = static_cast<const BLSURFPlugin_Hypothesis*> (theHyp);
143 //=============================================================================
147 //=============================================================================
149 bool HexoticPlugin_Hexotic::CheckHypothesis( SMESH_Mesh& aMesh,
150 const TopoDS_Shape& aShape,
151 SMESH_Hypothesis::Hypothesis_Status& aStatus )
153 // MESSAGE("HexoticPlugin_Hexotic::CheckHypothesis");
156 std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
157 const SMESHDS_Hypothesis* theHyp;
159 const std::list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
160 int nbHyp = hyps.size();
162 aStatus = SMESH_Hypothesis::HYP_OK;
163 return true; // can work with no hypothesis
167 theHyp = (*itl); // use only the first hypothesis
169 std::string hypName = theHyp->GetName();
170 if (hypName == "Hexotic_Parameters") {
171 _hypothesis = static_cast<const HexoticPlugin_Hypothesis*> (theHyp);
173 aStatus = SMESH_Hypothesis::HYP_OK;
176 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
178 #ifdef WITH_BLSURFPLUGIN
179 CheckBLSURFHypothesis(aMesh, aShape);
182 return aStatus == SMESH_Hypothesis::HYP_OK;
185 //=======================================================================
186 //function : findShape
188 //=======================================================================
190 static TopoDS_Shape findShape(SMDS_MeshNode** t_Node,
192 const TopoDS_Shape* t_Shape,
197 int iShape, nbNode = 8;
199 for ( int i=0; i<3; i++ ) {
201 for ( int j=0; j<nbNode; j++ ) {
202 if ( i == 0) pntCoor[i] += t_Node[j]->X();
203 if ( i == 1) pntCoor[i] += t_Node[j]->Y();
204 if ( i == 2) pntCoor[i] += t_Node[j]->Z();
206 pntCoor[i] /= nbNode;
208 gp_Pnt aPnt(pntCoor[0], pntCoor[1], pntCoor[2]);
210 if ( aShape.IsNull() ) aShape = t_Shape[0];
211 BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
212 if ( !(SC.State() == TopAbs_IN) ) {
214 for (iShape = 0; iShape < nShape && aShape.IsNull(); iShape++) {
215 if ( !( pntCoor[0] < t_Box[iShape][0] || t_Box[iShape][1] < pntCoor[0] ||
216 pntCoor[1] < t_Box[iShape][2] || t_Box[iShape][3] < pntCoor[1] ||
217 pntCoor[2] < t_Box[iShape][4] || t_Box[iShape][5] < pntCoor[2]) ) {
218 BRepClass3d_SolidClassifier SC (t_Shape[iShape], aPnt, Precision::Confusion());
219 if (SC.State() == TopAbs_IN)
220 aShape = t_Shape[iShape];
227 //=======================================================================
228 //function : findEdge
230 //=======================================================================
232 static int findEdge(const SMDS_MeshNode* aNode,
233 const SMESHDS_Mesh* aMesh,
235 const TopoDS_Shape* t_Edge) {
237 TopoDS_Shape aPntShape, foundEdge;
238 TopoDS_Vertex aVertex;
239 gp_Pnt aPnt( aNode->X(), aNode->Y(), aNode->Z() );
242 double nearest = RealLast(), *t_Dist;
243 double epsilon = Precision::Confusion();
245 t_Dist = new double[ nEdge ];
246 aPntShape = BRepBuilderAPI_MakeVertex( aPnt ).Shape();
247 aVertex = TopoDS::Vertex( aPntShape );
249 for ( ind=0; ind < nEdge; ind++ ) {
250 BRepExtrema_DistShapeShape aDistance ( aVertex, t_Edge[ind] );
251 t_Dist[ind] = aDistance.Value();
252 if ( t_Dist[ind] < nearest ) {
253 nearest = t_Dist[ind];
254 foundEdge = t_Edge[ind];
256 if ( nearest < epsilon )
262 return aMesh->ShapeToIndex( foundEdge );
265 //=======================================================================
266 //function : getNbShape
268 //=======================================================================
270 static int getNbShape(std::string aFile, std::string aString, int defaultValue=0) {
271 int number = defaultValue;
273 std::ifstream file(aFile.c_str());
274 while ( !file.eof() ) {
275 getline( file, aLine);
276 if ( aLine == aString ) {
277 getline( file, aLine);
278 std::istringstream stringFlux( aLine );
279 stringFlux >> number;
280 number = ( number + defaultValue + std::abs(number - defaultValue) ) / 2;
288 //=======================================================================
289 //function : countShape
291 //=======================================================================
293 template < class Mesh, class Shape >
294 static int countShape( Mesh* mesh, Shape shape ) {
295 TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
296 TopTools_MapOfShape mapShape;
298 for ( ; expShape.More(); expShape.Next() ) {
299 if (mapShape.Add(expShape.Current())) {
306 //=======================================================================
307 //function : getShape
309 //=======================================================================
311 template < class Mesh, class Shape, class Tab >
312 void getShape(Mesh* mesh, Shape shape, Tab *t_Shape) {
313 TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
314 TopTools_MapOfShape mapShape;
315 for ( int i=0; expShape.More(); expShape.Next() ) {
316 if (mapShape.Add(expShape.Current())) {
317 t_Shape[i] = expShape.Current();
324 //=======================================================================
325 //function : printWarning
327 //=======================================================================
329 static void printWarning(const int nbExpected, std::string aString, const int nbFound) {
331 cout << "WARNING : " << nbExpected << " " << aString << " expected, Hexotic has found " << nbFound << std::endl;
332 cout << "=======" << std::endl;
337 //=======================================================================
338 //function : removeHexoticFiles
340 //=======================================================================
342 static void removeHexoticFiles(TCollection_AsciiString file_In, TCollection_AsciiString file_Out) {
343 removeFile( file_In );
344 removeFile( file_Out );
347 //=======================================================================
348 //function : readResult
349 //purpose : Read GMF file in case of a mesh with geometry
350 //=======================================================================
352 static bool readResult(std::string theFile,
353 #ifdef WITH_SMESH_CANCEL_COMPUTE
354 HexoticPlugin_Hexotic* theAlgo,
356 SMESHDS_Mesh* theMesh,
358 const TopoDS_Shape* tabShape,
361 // ---------------------------------
362 // Optimisation of the plugin ...
363 // Retrieve the correspondance edge --> shape
364 // (which is very costly) only when user
365 // has defined at least one group of edges
366 // which should be rare for a 3d mesh !
367 // ---------------------------------
369 bool retrieve_edges = false;
370 const std::set<SMESHDS_GroupBase*>& aGroups = theMesh->GetGroups();
371 set<SMESHDS_GroupBase*>::const_iterator GrIt = aGroups.begin();
372 for (; GrIt != aGroups.end(); GrIt++)
374 SMESHDS_GroupBase* aGrp = *GrIt;
377 if ( aGrp->GetType() == SMDSAbs_Edge )
379 retrieve_edges = true;
384 // ---------------------------------
385 // Read generated elements and nodes
386 // ---------------------------------
389 TopoDS_Vertex aVertex;
391 int EndOfFile = 0, nbElem = 0, nField = 9, nbRef = 0;
392 int aHexoticNodeID = 0, shapeID, hexoticShapeID;
393 const int IdShapeRef = 2;
394 int *tabID, *tabRef, *nodeAssigne;
395 bool *tabDummy, hasDummy = false;
396 double epsilon = Precision::Confusion();
397 std::map <std::string,int> mapField;
398 SMDS_MeshNode** HexoticNode;
399 TopoDS_Shape *tabCorner, *tabEdge;
401 const int nbDomains = countShape( theMesh, TopAbs_SHELL );
402 const int holeID = -1;
404 // tabID = new int[nbShape];
405 tabID = new int[nbDomains];
406 tabRef = new int[nField];
407 tabDummy = new bool[nField];
409 for (int i=0; i<nbDomains; i++)
411 if ( nbDomains == 1 )
412 tabID[0] = theMesh->ShapeToIndex( tabShape[0] );
414 mapField["MeshVersionFormatted"] = 0; tabRef[0] = 0; tabDummy[0] = false;
415 mapField["Dimension"] = 1; tabRef[1] = 0; tabDummy[1] = false;
416 mapField["Vertices"] = 2; tabRef[2] = 3; tabDummy[2] = true;
417 mapField["Corners"] = 3; tabRef[3] = 1; tabDummy[3] = false;
418 mapField["Edges"] = 4; tabRef[4] = 2; tabDummy[4] = true;
419 mapField["Ridges"] = 5; tabRef[5] = 1; tabDummy[5] = false;
420 mapField["Quadrilaterals"] = 6; tabRef[6] = 4; tabDummy[6] = true;
421 mapField["Hexahedra"] = 7; tabRef[7] = 8; tabDummy[7] = true;
422 mapField["End"] = 8; tabRef[8] = 0; tabDummy[0] = false;
424 SMDS_NodeIteratorPtr itOnHexoticInputNode = theMesh->nodesIterator();
425 while ( itOnHexoticInputNode->more() )
426 theMesh->RemoveNode( itOnHexoticInputNode->next() );
428 int nbVertices = getNbShape(theFile, "Vertices");
429 int nbCorners = getNbShape(theFile, "Corners", countShape( theMesh, TopAbs_VERTEX ));
430 int nbShapeEdge = countShape( theMesh, TopAbs_EDGE );
432 tabCorner = new TopoDS_Shape[ nbCorners ];
433 tabEdge = new TopoDS_Shape[ nbShapeEdge ];
434 nodeAssigne = new int[ nbVertices + 1 ];
435 HexoticNode = new SMDS_MeshNode*[ nbVertices + 1 ];
437 getShape(theMesh, TopAbs_VERTEX, tabCorner);
438 getShape(theMesh, TopAbs_EDGE, tabEdge);
440 MESSAGE("Read " << theFile << " file");
441 std::ifstream fileRes(theFile.c_str());
444 while ( EndOfFile == 0 ) {
448 if (mapField.count(token)) {
449 nField = mapField[token];
450 nbRef = tabRef[nField];
451 hasDummy = tabDummy[nField];
459 if ( nField < (mapField.size() - 1) && nField >= 0 )
463 case 0: { // "MeshVersionFormatted"
464 MESSAGE(token << " " << nbElem);
467 case 1: { // "Dimension"
468 MESSAGE("Mesh dimension " << nbElem << "D");
471 case 2: { // "Vertices"
472 MESSAGE("Read " << nbElem << " " << token);
475 SMDS_MeshNode * aHexoticNode;
477 coord = new double[nbRef];
478 for ( int iElem = 0; iElem < nbElem; iElem++ ) {
479 #ifdef WITH_SMESH_CANCEL_COMPUTE
480 if(theAlgo->computeCanceled())
485 aHexoticID = iElem + 1;
486 for ( int iCoord = 0; iCoord < 3; iCoord++ )
487 fileRes >> coord[ iCoord ];
489 aHexoticNode = theMesh->AddNode(coord[0], coord[1], coord[2]);
490 HexoticNode[ aHexoticID ] = aHexoticNode;
491 nodeAssigne[ aHexoticID ] = 0;
499 case 6: // "Quadrilaterals"
500 case 7: { // "Hexahedra"
501 MESSAGE("Read " << nbElem << " " << token);
502 SMDS_MeshNode** node;
503 int nodeDim, *nodeID;
504 SMDS_MeshElement * aHexoticElement = 0;
506 node = new SMDS_MeshNode*[ nbRef ];
507 nodeID = new int[ nbRef ];
508 for ( int iElem = 0; iElem < nbElem; iElem++ ) {
509 #ifdef WITH_SMESH_CANCEL_COMPUTE
510 if(theAlgo->computeCanceled())
515 for ( int iRef = 0; iRef < nbRef; iRef++ ) {
516 fileRes >> aHexoticNodeID; // read nbRef aHexoticNodeID
517 node[ iRef ] = HexoticNode[ aHexoticNodeID ];
518 nodeID[ iRef ] = aHexoticNodeID;
523 case 3: { // "Corners"
525 gp_Pnt HexoticPnt ( node[0]->X(), node[0]->Y(), node[0]->Z() );
526 for ( int i=0; i<nbElem; i++ ) {
527 aVertex = TopoDS::Vertex( tabCorner[i] );
528 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
529 if ( aPnt.Distance( HexoticPnt ) < epsilon )
536 aHexoticElement = theMesh->AddEdge( node[0], node[1] );
538 if ( nodeAssigne[ nodeID[0] ] == 0 || nodeAssigne[ nodeID[0] ] == 2 )
541 shapeID = findEdge( node[iNode], theMesh, nbShapeEdge, tabEdge );
546 case 5: { // "Ridges"
549 case 6: { // "Quadrilaterals"
551 aHexoticElement = theMesh->AddFace( node[0], node[1], node[2], node[3] );
555 case 7: { // "Hexahedra"
557 if ( nbDomains > 1 ) {
558 hexoticShapeID = dummy - IdShapeRef;
559 if ( tabID[ hexoticShapeID ] == 0 ) {
560 aShape = findShape(node, aShape, tabShape, tabBox, nbShape);
561 shapeID = aShape.IsNull() ? holeID : theMesh->ShapeToIndex( aShape );
562 tabID[ hexoticShapeID ] = shapeID;
565 shapeID = tabID[ hexoticShapeID ];
566 if ( iElem == (nbElem - 1) ) {
567 int shapeAssociated = 0;
568 for ( int i=0; i<nbDomains; i++ ) {
570 shapeAssociated += 1;
572 if ( shapeAssociated != nbShape )
573 printWarning(nbShape, "domains", shapeAssociated);
579 if ( shapeID != holeID )
580 aHexoticElement = theMesh->AddVolume( node[0], node[3], node[2], node[1], node[4], node[7], node[6], node[5] );
585 if ( token != "Ridges" && ( shapeID > 0 || token == "Corners")) {
586 for ( int i=0; i<nbRef; i++ ) {
587 if ( nodeAssigne[ nodeID[i] ] == 0 ) {
588 if ( token == "Corners" ) theMesh->SetNodeOnVertex( node[0], aVertex );
589 else if ( token == "Edges" ) theMesh->SetNodeOnEdge( node[i], shapeID );
590 else if ( token == "Quadrilaterals" ) theMesh->SetNodeOnFace( node[i], shapeID );
591 else if ( token == "Hexahedra" ) theMesh->SetNodeInVolume( node[i], shapeID );
592 nodeAssigne[ nodeID[i] ] = nodeDim;
595 if ( token != "Corners" && aHexoticElement )
596 theMesh->SetMeshElementOnShape( aHexoticElement, shapeID );
605 MESSAGE("End of " << theFile << " file");
609 MESSAGE("Unknown Token: " << token);
615 // remove nodes in holes
618 SMESHDS_SubMesh* subMesh;
619 for ( int i = 1; i <= nbVertices; ++i )
620 if ( HexoticNode[i]->NbInverseElements() == 0 )
622 subMesh = HexoticNode[i]->getshapeId() > 0 ? theMesh->MeshElements(HexoticNode[i]->getshapeId() ) : 0;
623 theMesh->RemoveFreeNode( HexoticNode[i], subMesh, /*fromGroups=*/false );
631 delete [] nodeAssigne;
632 delete [] HexoticNode;
637 //=======================================================================
638 //function : readResult
639 //purpose : Read GMF file in case of a mesh w/o geometry
640 //=======================================================================
642 static bool readResult(std::string theFile,
643 #ifdef WITH_SMESH_CANCEL_COMPUTE
644 HexoticPlugin_Hexotic* theAlgo,
646 SMESH_MesherHelper* theHelper)
648 SMESHDS_Mesh* theMesh = theHelper->GetMeshDS();
650 // ---------------------------------
651 // Read generated elements and nodes
652 // ---------------------------------
655 const int nbField = 9;
656 int nField, EndOfFile = 0, nbElem = 0, nbRef = 0;
657 int aHexoticNodeID = 0, shapeID;
658 int tabRef[nbField], *nodeAssigne;
659 bool tabDummy[nbField], hasDummy = false;
660 std::map <std::string,int> mapField;
661 SMDS_MeshNode** HexoticNode;
663 mapField["MeshVersionFormatted"] = 0; tabRef[0] = 0; tabDummy[0] = false;
664 mapField["Dimension"] = 1; tabRef[1] = 0; tabDummy[1] = false;
665 mapField["Vertices"] = 2; tabRef[2] = 3; tabDummy[2] = true;
666 mapField["Corners"] = 3; tabRef[3] = 1; tabDummy[3] = false;
667 mapField["Edges"] = 4; tabRef[4] = 2; tabDummy[4] = true;
668 mapField["Ridges"] = 5; tabRef[5] = 1; tabDummy[5] = false;
669 mapField["Quadrilaterals"] = 6; tabRef[6] = 4; tabDummy[6] = true;
670 mapField["Hexahedra"] = 7; tabRef[7] = 8; tabDummy[7] = true;
671 mapField["End"] = 8; tabRef[8] = 0; tabDummy[8] = false;
673 theHelper->GetMesh()->Clear();
675 int nbVertices = getNbShape(theFile, "Vertices");
676 HexoticNode = new SMDS_MeshNode*[ nbVertices + 1 ];
677 nodeAssigne = new int[ nbVertices + 1 ];
679 MESSAGE("Read " << theFile << " file");
680 std::ifstream fileRes(theFile.c_str());
688 if (mapField.count(token)) {
689 nField = mapField[token];
690 nbRef = tabRef[nField];
691 hasDummy = tabDummy[nField];
699 if ( nField < (mapField.size() - 1) && nField >= 0 )
703 case 0: { // "MeshVersionFormatted"
704 MESSAGE(token << " " << nbElem);
707 case 1: { // "Dimension"
708 MESSAGE("Mesh dimension " << nbElem << "D");
711 case 2: { // "Vertices"
712 MESSAGE("Read " << nbElem << " " << token);
715 SMDS_MeshNode * aHexoticNode;
717 for ( int iElem = 0; iElem < nbElem; iElem++ ) {
718 #ifdef WITH_SMESH_CANCEL_COMPUTE
719 if(theAlgo->computeCanceled())
724 aHexoticID = iElem + 1;
725 for ( int iCoord = 0; iCoord < 3; iCoord++ )
726 fileRes >> coord[ iCoord ];
728 aHexoticNode = theMesh->AddNode(coord[0], coord[1], coord[2]);
729 HexoticNode[ aHexoticID ] = aHexoticNode;
730 nodeAssigne[ aHexoticID ] = 0;
737 case 6: // "Quadrilaterals"
738 case 7: { // "Hexahedra"
739 MESSAGE("Read " << nbElem << " " << token);
740 std::vector< SMDS_MeshNode* > node( nbRef );
741 std::vector< int > nodeID( nbRef );
743 for ( int iElem = 0; iElem < nbElem; iElem++ )
745 #ifdef WITH_SMESH_CANCEL_COMPUTE
746 if(theAlgo->computeCanceled())
751 for ( int iRef = 0; iRef < nbRef; iRef++ )
753 fileRes >> aHexoticNodeID; // read nbRef aHexoticNodeID
754 node [ iRef ] = HexoticNode[ aHexoticNodeID ];
755 nodeID[ iRef ] = aHexoticNodeID;
762 theHelper->AddEdge( node[0], node[1] ); break;
763 case 6: // "Quadrilaterals"
764 theMesh->AddFace( node[0], node[1], node[2], node[3] ); break;
765 case 7: // "Hexahedra"
766 theHelper->AddVolume( node[0], node[3], node[2], node[1],
767 node[4], node[7], node[6], node[5] ); break;
771 for ( int iRef = 0; iRef < nbRef; iRef++ )
772 nodeAssigne[ nodeID[ iRef ]] = 1;
778 MESSAGE("End of " << theFile << " file");
782 MESSAGE("Unknown Token: " << token);
788 shapeID = theHelper->GetSubShapeID();
789 for ( int i = 0; i < nbVertices; ++i )
790 if ( !nodeAssigne[ i+1 ])
791 theMesh->SetNodeInVolume( HexoticNode[ i+1 ], shapeID );
793 delete [] HexoticNode;
794 delete [] nodeAssigne;
798 //=============================================================================
800 * Pass parameters to Hexotic
802 //=============================================================================
804 void HexoticPlugin_Hexotic::SetParameters(const HexoticPlugin_Hypothesis* hyp) {
806 MESSAGE("HexoticPlugin_Hexotic::SetParameters");
808 _hexesMinLevel = hyp->GetHexesMinLevel();
809 _hexesMaxLevel = hyp->GetHexesMaxLevel();
810 _hexesMinSize = hyp->GetMinSize();
811 _hexesMaxSize = hyp->GetMaxSize();
812 _hexoticIgnoreRidges = hyp->GetHexoticIgnoreRidges();
813 _hexoticInvalidElements = hyp->GetHexoticInvalidElements();
814 _hexoticSharpAngleThreshold = hyp->GetHexoticSharpAngleThreshold();
815 _hexoticNbProc = hyp->GetHexoticNbProc();
816 _hexoticWorkingDirectory = hyp->GetHexoticWorkingDirectory();
817 _hexoticVerbosity = hyp->GetHexoticVerbosity();
818 _hexoticMaxMemory = hyp->GetHexoticMaxMemory();
819 _hexoticSdMode = hyp->GetHexoticSdMode();
823 cout << "WARNING : The Hexotic default parameters are taken into account" << std::endl;
824 cout << "=======" << std::endl;
825 _hexesMinLevel = hyp->GetDefaultHexesMinLevel();
826 _hexesMaxLevel = hyp->GetDefaultHexesMaxLevel();
827 _hexesMinSize = hyp->GetDefaultMinSize();
828 _hexesMaxSize = hyp->GetDefaultMaxSize();
829 _hexoticIgnoreRidges = hyp->GetDefaultHexoticIgnoreRidges();
830 _hexoticInvalidElements = hyp->GetDefaultHexoticInvalidElements();
831 _hexoticSharpAngleThreshold = hyp->GetDefaultHexoticSharpAngleThreshold();
832 _hexoticNbProc = hyp->GetDefaultHexoticNbProc();
833 _hexoticWorkingDirectory = hyp->GetDefaultHexoticWorkingDirectory();
834 _hexoticVerbosity = hyp->GetDefaultHexoticVerbosity();
835 _hexoticMaxMemory = hyp->GetDefaultHexoticMaxMemory();
836 _hexoticSdMode = hyp->GetDefaultHexoticSdMode();
840 //=======================================================================
841 //function : getTmpDir
843 //=======================================================================
845 static TCollection_AsciiString getTmpDir()
847 TCollection_AsciiString aTmpDir;
849 char *Tmp_dir = getenv("SALOME_TMP_DIR");
850 if(Tmp_dir != NULL) {
853 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
855 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
860 aTmpDir = TCollection_AsciiString("C:\\");
862 aTmpDir = TCollection_AsciiString("/tmp/");
868 //================================================================================
870 * \brief Returns a command to run Hexotic mesher
872 //================================================================================
874 std::string HexoticPlugin_Hexotic::getHexoticCommand(const TCollection_AsciiString& Hexotic_In,
875 const TCollection_AsciiString& Hexotic_Out) const
878 cout << "Hexotic execution..." << std::endl;
879 cout << _name << " parameters :" << std::endl;
880 cout << " " << _name << " Verbosity = " << _hexoticVerbosity << std::endl;
881 cout << " " << _name << " Max Memory = " << _hexoticMaxMemory << std::endl;
882 cout << " " << _name << " Segments Min Level = " << _hexesMinLevel << std::endl;
883 cout << " " << _name << " Segments Max Level = " << _hexesMaxLevel << std::endl;
884 cout << " " << _name << " Segments Min Size = " << _hexesMinSize << std::endl;
885 cout << " " << _name << " Segments Max Size = " << _hexesMaxSize << std::endl;
886 cout << " " << "Hexotic can ignore ridges : " << (_hexoticIgnoreRidges ? "yes":"no") << std::endl;
887 cout << " " << "Hexotic authorize invalide elements : " << ( _hexoticInvalidElements ? "yes":"no") << std::endl;
888 cout << " " << _name << " Sharp angle threshold = " << _hexoticSharpAngleThreshold << " degrees" << std::endl;
889 cout << " " << _name << " Number of threads = " << _hexoticNbProc << std::endl;
890 cout << " " << _name << " Working directory = \"" << _hexoticWorkingDirectory << "\"" << std::endl;
891 cout << " " << _name << " Sub. Dom mode = " << _hexoticSdMode << std::endl;
893 TCollection_AsciiString run_Hexotic( "hexotic" );
895 TCollection_AsciiString minl = " -minl ", maxl = " -maxl ", angle = " -ra ";
896 TCollection_AsciiString mins = " -mins ", maxs = " -maxs ";
897 TCollection_AsciiString in = " -in ", out = " -out ";
898 TCollection_AsciiString ignoreRidges = " -nr ", invalideElements = " -inv ";
899 TCollection_AsciiString subdom = " -sd ", sharp = " -sharp ";
900 TCollection_AsciiString proc = " -nproc ";
901 TCollection_AsciiString verb = " -v ";
902 TCollection_AsciiString maxmem = " -m ";
904 TCollection_AsciiString minLevel, maxLevel, minSize, maxSize, sharpAngle, mode, nbproc, verbosity, maxMemory;
905 minLevel = _hexesMinLevel;
906 maxLevel = _hexesMaxLevel;
907 minSize = _hexesMinSize;
908 maxSize = _hexesMaxSize;
909 sharpAngle = _hexoticSharpAngleThreshold;
910 mode = _hexoticSdMode;
911 nbproc = _hexoticNbProc;
912 verbosity = _hexoticVerbosity;
913 maxMemory = _hexoticMaxMemory;
915 if (_hexoticIgnoreRidges)
916 run_Hexotic += ignoreRidges;
918 if (_hexoticInvalidElements)
919 run_Hexotic += invalideElements;
921 if (_hexesMinSize > 0)
922 run_Hexotic += mins + minSize;
924 if (_hexesMaxSize > 0)
925 run_Hexotic += maxs + maxSize;
927 if (_hexesMinLevel > 0)
928 run_Hexotic += minl + minLevel;
930 if (_hexesMaxLevel > 0)
931 run_Hexotic += maxl + maxLevel;
933 if (_hexoticSharpAngleThreshold > 0)
934 run_Hexotic += angle + sharpAngle;
936 run_Hexotic += in + Hexotic_In + out + Hexotic_Out;
937 run_Hexotic += subdom + mode;
938 run_Hexotic += proc + nbproc;
939 run_Hexotic += verb + verbosity;
940 run_Hexotic += maxmem + maxMemory;
942 return run_Hexotic.ToCString();
945 //=============================================================================
947 * Here we are going to use the Hexotic mesher
949 //=============================================================================
951 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh& aMesh,
952 const TopoDS_Shape& aShape)
954 #ifdef WITH_SMESH_CANCEL_COMPUTE
955 _compute_canceled = false;
958 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
959 TCollection_AsciiString hexahedraMessage;
961 if (_iShape == 0 && _nbShape == 0) {
962 _nbShape = countShape( meshDS, TopAbs_SOLID ); // we count the number of shapes
965 // to prevent from displaying error message after computing,
966 // SetIsAlwaysComputed( true ) to empty sub-meshes
967 vector< SMESH_subMesh* > subMeshesAlwaysComp;
968 for ( int i = 0; i < _nbShape; ++i )
969 if ( SMESH_subMesh* sm = aMesh.GetSubMeshContaining( aShape ))
971 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
972 /*complexShapeFirst=*/false);
973 while ( smIt->more() )
976 if ( !sm->IsMeshComputed() )
978 sm->SetIsAlwaysComputed( true );
979 subMeshesAlwaysComp.push_back( sm );
986 if (_iShape == _nbShape ) {
988 // create bounding box for each shape of the compound
991 TopoDS_Shape *tabShape;
994 tabShape = new TopoDS_Shape[_nbShape];
995 tabBox = new double*[_nbShape];
996 for (int i=0; i<_nbShape; i++)
997 tabBox[i] = new double[6];
998 double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1000 TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID);
1001 for (; expBox.More(); expBox.Next()) {
1002 tabShape[iShape] = expBox.Current();
1003 Bnd_Box BoundingBox;
1004 BRepBndLib::Add(expBox.Current(), BoundingBox);
1005 BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
1006 tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
1007 tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
1008 tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
1012 SetParameters(_hypothesis);
1014 // TCollection_AsciiString aTmpDir = getTmpDir();
1015 TCollection_AsciiString aTmpDir = TCollection_AsciiString(_hexoticWorkingDirectory.c_str());
1017 if ( aTmpDir.Value(aTmpDir.Length()) != '\\' ) aTmpDir += '\\';
1019 if ( aTmpDir.Value(aTmpDir.Length()) != '/' ) aTmpDir += '/';
1021 TCollection_AsciiString Hexotic_In(""), Hexotic_Out;
1022 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1023 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic.log"; // log
1025 std::map <int,int> aSmdsToHexoticIdMap;
1026 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1028 Hexotic_Out = aTmpDir + "Hexotic_Out.mesh";
1029 #ifdef WITH_BLSURFPLUGIN
1030 bool defaultInputFile = true;
1031 if (_blsurfHypo && !_blsurfHypo->GetQuadAllowed()) {
1032 Hexotic_In = TCollection_AsciiString(_blsurfHypo->GetGMFFile().c_str());
1033 if (Hexotic_In != "")
1034 defaultInputFile = false;
1036 if (defaultInputFile) {
1038 Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1039 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1041 cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1042 aMesh.ExportGMF(Hexotic_In.ToCString(), meshDS, true);
1043 #ifdef WITH_BLSURFPLUGIN
1046 removeFile( Hexotic_Out );
1051 std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1052 run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString(); // dump into file
1055 cout << "Hexotic command : " << run_Hexotic << std::endl;
1057 modeFile_In += Hexotic_In;
1058 system( modeFile_In.ToCString() );
1059 aSmdsToHexoticIdMap.clear();
1060 aHexoticIdToNodeMap.clear();
1062 MESSAGE("HexoticPlugin_Hexotic::Compute");
1064 int status = system( run_Hexotic.data() );
1070 std::ifstream fileRes( Hexotic_Out.ToCString() );
1071 modeFile_Out += Hexotic_Out;
1072 system( modeFile_Out.ToCString() );
1073 if ( ! fileRes.fail() ) {
1074 Ok = readResult( Hexotic_Out.ToCString(),
1075 #ifdef WITH_SMESH_CANCEL_COMPUTE
1078 meshDS, _nbShape, tabShape, tabBox );
1080 /*********************
1081 // TODO: Detect and remove elements in holes in case of sd mode = 4
1082 // Remove previous nodes and elements
1083 SMDS_ElemIteratorPtr itElement = meshDS->elementsIterator();
1084 SMDS_NodeIteratorPtr itNode = meshDS->nodesIterator();
1086 while ( itElement->more() )
1087 meshDS->RemoveElement( itElement->next() );
1088 while ( itNode->more() )
1089 meshDS->RemoveNode( itNode->next() );
1091 SMESH_ComputeErrorPtr myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1094 hexahedraMessage = "success";
1096 hexahedraMessage = "failed";
1099 hexahedraMessage = "failed";
1100 cout << "Problem with Hexotic output file " << Hexotic_Out.ToCString() << std::endl;
1103 SMESH_File logFile( aLogFileName.ToCString() );
1104 if ( !logFile.eof() )
1106 char msgLic[] = " Dlim ";
1107 const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1108 if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1109 error("Licence problems.");
1111 if ( status > 0 && WEXITSTATUS(status) == 127 )
1112 error("hexotic: command not found");
1114 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1117 // restore "always computed" flag of sub-meshes (0022127)
1118 for ( size_t iSM = 0; iSM < subMeshesAlwaysComp.size(); ++iSM )
1119 subMeshesAlwaysComp[ iSM ]->SetIsAlwaysComputed( false );
1122 for (int i=0; i<_nbShape; i++)
1123 delete [] tabBox[i];
1128 #ifdef WITH_SMESH_CANCEL_COMPUTE
1129 if(_compute_canceled)
1130 return error(SMESH_Comment("interruption initiated by user"));
1135 //=============================================================================
1137 * \brief Computes mesh without geometry
1138 * \param aMesh - the mesh
1139 * \param aHelper - helper that must be used for adding elements to \aaMesh
1140 * \retval bool - is a success
1142 * The method is called if ( !aMesh->HasShapeToMesh() )
1144 //=============================================================================
1146 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
1148 #ifdef WITH_SMESH_CANCEL_COMPUTE
1149 _compute_canceled = false;
1152 SMESH_ComputeErrorPtr myError = SMESH_ComputeError::New();
1155 TCollection_AsciiString hexahedraMessage;
1157 SetParameters(_hypothesis);
1159 TCollection_AsciiString aTmpDir = getTmpDir();
1160 TCollection_AsciiString Hexotic_In, Hexotic_Out;
1161 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1162 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic.log"; // log
1164 std::map <int,int> aSmdsToHexoticIdMap;
1165 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1167 Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1168 Hexotic_Out = aTmpDir + "Hexotic_Out.mesh";
1170 std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1171 run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString(); // dump into file
1173 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1176 cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1177 aMesh.ExportGMF(Hexotic_In.ToCString(), aHelper->GetMeshDS());
1178 modeFile_In += Hexotic_In;
1179 system( modeFile_In.ToCString() );
1180 aSmdsToHexoticIdMap.clear();
1181 aHexoticIdToNodeMap.clear();
1183 MESSAGE("HexoticPlugin_Hexotic::Compute");
1186 cout << "Hexotic command : " << run_Hexotic << std::endl;
1187 system( run_Hexotic.data() );
1193 std::ifstream fileRes( Hexotic_Out.ToCString() );
1194 modeFile_Out += Hexotic_Out;
1195 system( modeFile_Out.ToCString() );
1196 if ( ! fileRes.fail() ) {
1197 Ok = readResult( Hexotic_Out.ToCString(),
1198 #ifdef WITH_SMESH_CANCEL_COMPUTE
1204 // Remove previous nodes and elements
1205 SMDS_ElemIteratorPtr itElement = aHelper->GetMeshDS()->elementsIterator();
1206 SMDS_NodeIteratorPtr itNode = aHelper->GetMeshDS()->nodesIterator();
1208 while ( itElement->more() )
1209 aHelper->GetMeshDS()->RemoveElement( itElement->next() );
1210 while ( itNode->more() )
1211 aHelper->GetMeshDS()->RemoveNode( itNode->next() );
1214 myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1216 itElement = aHelper->GetMeshDS()->elementsIterator();
1217 itNode = aHelper->GetMeshDS()->nodesIterator();
1219 // Assign nodes and elements to the pseudo shape
1220 while ( itNode->more() )
1221 aHelper->GetMeshDS()->SetNodeInVolume(itNode->next(), 1);
1222 while ( itElement->more() )
1223 aHelper->GetMeshDS()->SetMeshElementOnShape(itElement->next(), 1);
1227 hexahedraMessage = "success";
1229 hexahedraMessage = "failed";
1233 myError->myName = COMPERR_EXCEPTION;
1235 hexahedraMessage = "failed";
1236 cout << "Problem with Hexotic output file " << Hexotic_Out << std::endl;
1238 SMESH_File logFile( aLogFileName.ToCString() );
1239 if ( !logFile.eof() )
1241 char msgLic[] = " Dlim ";
1242 const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1243 if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1244 return error("Licence problems.");
1246 return error(SMESH_Comment("Problem with Hexotic output file ")<<Hexotic_Out);
1248 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1251 #ifdef WITH_SMESH_CANCEL_COMPUTE
1252 if(_compute_canceled)
1253 return error(SMESH_Comment("interruption initiated by user"));
1257 return myError->IsOK();
1261 //=============================================================================
1265 //=============================================================================
1267 bool HexoticPlugin_Hexotic::Evaluate(SMESH_Mesh& aMesh,
1268 const TopoDS_Shape& aShape,
1269 MapShapeNbElems& aResMap)
1271 std::vector<int> aResVec(SMDSEntity_Last);
1272 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1273 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1274 aResMap.insert(std::make_pair(sm,aResVec));
1276 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1277 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Evaluation is not implemented",this));
1282 #ifdef WITH_SMESH_CANCEL_COMPUTE
1283 void HexoticPlugin_Hexotic::CancelCompute()
1285 _compute_canceled = true;
1288 TCollection_AsciiString aTmpDir = getTmpDir();
1289 TCollection_AsciiString Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1290 TCollection_AsciiString cmd = TCollection_AsciiString("ps ux | grep ") + Hexotic_In;
1291 cmd += TCollection_AsciiString(" | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1");
1292 system( cmd.ToCString() );