1 // Copyright (C) 2007-2012 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 #ifdef WITH_SMESH_CANCEL_COMPUTE
99 _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");
852 if(Tmp_dir != NULL) {
855 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
857 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
862 aTmpDir = TCollection_AsciiString("C:\\");
864 aTmpDir = TCollection_AsciiString("/tmp/");
870 //================================================================================
872 * \brief Returns a command to run Hexotic mesher
874 //================================================================================
876 std::string HexoticPlugin_Hexotic::getHexoticCommand(const TCollection_AsciiString& Hexotic_In,
877 const TCollection_AsciiString& Hexotic_Out) const
880 cout << "Hexotic execution..." << std::endl;
881 cout << _name << " parameters :" << std::endl;
882 cout << " " << _name << " Verbosity = " << _hexoticVerbosity << std::endl;
883 cout << " " << _name << " Max Memory = " << _hexoticMaxMemory << std::endl;
884 cout << " " << _name << " Segments Min Level = " << _hexesMinLevel << std::endl;
885 cout << " " << _name << " Segments Max Level = " << _hexesMaxLevel << std::endl;
886 cout << " " << _name << " Segments Min Size = " << _hexesMinSize << std::endl;
887 cout << " " << _name << " Segments Max Size = " << _hexesMaxSize << std::endl;
888 cout << " " << "Hexotic can ignore ridges : " << (_hexoticIgnoreRidges ? "yes":"no") << std::endl;
889 cout << " " << "Hexotic authorize invalide elements : " << ( _hexoticInvalidElements ? "yes":"no") << std::endl;
890 cout << " " << _name << " Sharp angle threshold = " << _hexoticSharpAngleThreshold << " degrees" << std::endl;
891 cout << " " << _name << " Number of threads = " << _hexoticNbProc << std::endl;
892 cout << " " << _name << " Working directory = \"" << _hexoticWorkingDirectory << "\"" << std::endl;
893 cout << " " << _name << " Sub. Dom mode = " << _hexoticSdMode << std::endl;
895 TCollection_AsciiString run_Hexotic( "hexotic" );
897 TCollection_AsciiString minl = " -minl ", maxl = " -maxl ", angle = " -ra ";
898 TCollection_AsciiString mins = " -mins ", maxs = " -maxs ";
899 TCollection_AsciiString in = " -in ", out = " -out ";
900 TCollection_AsciiString ignoreRidges = " -nr ", invalideElements = " -inv ";
901 TCollection_AsciiString subdom = " -sd ", sharp = " -sharp ";
902 TCollection_AsciiString proc = " -nproc ";
903 TCollection_AsciiString verb = " -v ";
904 TCollection_AsciiString maxmem = " -m ";
906 TCollection_AsciiString minLevel, maxLevel, minSize, maxSize, sharpAngle, mode, nbproc, verbosity, maxMemory;
907 minLevel = _hexesMinLevel;
908 maxLevel = _hexesMaxLevel;
909 minSize = _hexesMinSize;
910 maxSize = _hexesMaxSize;
911 sharpAngle = _hexoticSharpAngleThreshold;
912 mode = _hexoticSdMode;
913 nbproc = _hexoticNbProc;
914 verbosity = _hexoticVerbosity;
915 maxMemory = _hexoticMaxMemory;
917 if (_hexoticIgnoreRidges)
918 run_Hexotic += ignoreRidges;
920 if (_hexoticInvalidElements)
921 run_Hexotic += invalideElements;
923 if (_hexesMinSize > 0)
924 run_Hexotic += mins + minSize;
926 if (_hexesMaxSize > 0)
927 run_Hexotic += maxs + maxSize;
929 if (_hexesMinLevel > 0)
930 run_Hexotic += minl + minLevel;
932 if (_hexesMaxLevel > 0)
933 run_Hexotic += maxl + maxLevel;
935 if (_hexoticSharpAngleThreshold > 0)
936 run_Hexotic += angle + sharpAngle;
938 run_Hexotic += in + Hexotic_In + out + Hexotic_Out;
939 run_Hexotic += subdom + mode;
940 run_Hexotic += proc + nbproc;
941 run_Hexotic += verb + verbosity;
942 run_Hexotic += maxmem + maxMemory;
944 return run_Hexotic.ToCString();
947 //=============================================================================
949 * Here we are going to use the Hexotic mesher
951 //=============================================================================
953 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh& aMesh,
954 const TopoDS_Shape& aShape)
956 #ifdef WITH_SMESH_CANCEL_COMPUTE
957 _compute_canceled = false;
960 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
961 TCollection_AsciiString hexahedraMessage;
963 if (_iShape == 0 && _nbShape == 0) {
964 _nbShape = countShape( meshDS, TopAbs_SOLID ); // we count the number of shapes
965 //_tabNode = new SMDS_MeshNode*[_nbShape]; // we declare the size of the node array
968 // to prevent from displaying error message after computing,
969 // SetIsAlwaysComputed( true ) to empty sub-meshes
970 for ( int i = 0; i < _nbShape; ++i )
971 if ( SMESH_subMesh* sm = aMesh.GetSubMeshContaining( aShape ))
973 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
974 /*complexShapeFirst=*/false);
975 while ( smIt->more() )
978 if ( !sm->IsMeshComputed() )
979 sm->SetIsAlwaysComputed( true );
985 if (_iShape == _nbShape ) {
987 // for (int i=0; i<_nbShape; i++) // we destroy the (_nbShape - 1) nodes created and used
988 // meshDS->RemoveNode( _tabNode[i] ); // to simulate successful mesh computing.
989 // delete [] _tabNode;
991 // create bounding box for each shape of the compound
994 TopoDS_Shape *tabShape;
997 tabShape = new TopoDS_Shape[_nbShape];
998 tabBox = new double*[_nbShape];
999 for (int i=0; i<_nbShape; i++)
1000 tabBox[i] = new double[6];
1001 double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1003 TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID);
1004 for (; expBox.More(); expBox.Next()) {
1005 tabShape[iShape] = expBox.Current();
1006 Bnd_Box BoundingBox;
1007 BRepBndLib::Add(expBox.Current(), BoundingBox);
1008 BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
1009 tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
1010 tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
1011 tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
1015 SetParameters(_hypothesis);
1017 // TCollection_AsciiString aTmpDir = getTmpDir();
1018 TCollection_AsciiString aTmpDir = TCollection_AsciiString(_hexoticWorkingDirectory.c_str());
1020 if ( aTmpDir.Value(aTmpDir.Length()) != '\\' ) aTmpDir += '\\';
1022 if ( aTmpDir.Value(aTmpDir.Length()) != '/' ) aTmpDir += '/';
1024 TCollection_AsciiString Hexotic_In(""), Hexotic_Out;
1025 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1026 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic.log"; // log
1028 std::map <int,int> aSmdsToHexoticIdMap;
1029 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1031 Hexotic_Out = aTmpDir + "Hexotic_Out.mesh";
1032 #ifdef WITH_BLSURFPLUGIN
1033 bool defaultInputFile = true;
1034 if (_blsurfHypo && !_blsurfHypo->GetQuadAllowed()) {
1035 Hexotic_In = TCollection_AsciiString(_blsurfHypo->GetGMFFile().c_str());
1036 if (Hexotic_In != "")
1037 defaultInputFile = false;
1039 if (defaultInputFile) {
1041 Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1042 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1044 cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1045 aMesh.ExportGMF(Hexotic_In.ToCString(), meshDS, true);
1046 #ifdef WITH_BLSURFPLUGIN
1049 removeFile( Hexotic_Out );
1054 std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1055 run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString(); // dump into file
1058 cout << "Hexotic command : " << run_Hexotic << std::endl;
1060 modeFile_In += Hexotic_In;
1061 system( modeFile_In.ToCString() );
1062 aSmdsToHexoticIdMap.clear();
1063 aHexoticIdToNodeMap.clear();
1065 MESSAGE("HexoticPlugin_Hexotic::Compute");
1067 system( run_Hexotic.data() );
1073 std::ifstream fileRes( Hexotic_Out.ToCString() );
1074 modeFile_Out += Hexotic_Out;
1075 system( modeFile_Out.ToCString() );
1076 if ( ! fileRes.fail() ) {
1077 Ok = readResult( Hexotic_Out.ToCString(),
1078 #ifdef WITH_SMESH_CANCEL_COMPUTE
1081 meshDS, _nbShape, tabShape, tabBox );
1083 /*********************
1084 // TODO: Detect and remove elements in holes in case of sd mode = 4
1085 // Remove previous nodes and elements
1086 SMDS_ElemIteratorPtr itElement = meshDS->elementsIterator();
1087 SMDS_NodeIteratorPtr itNode = meshDS->nodesIterator();
1089 while ( itElement->more() )
1090 meshDS->RemoveElement( itElement->next() );
1091 while ( itNode->more() )
1092 meshDS->RemoveNode( itNode->next() );
1094 SMESH_ComputeErrorPtr myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1097 hexahedraMessage = "success";
1099 hexahedraMessage = "failed";
1102 hexahedraMessage = "failed";
1103 cout << "Problem with Hexotic output file " << Hexotic_Out.ToCString() << std::endl;
1106 SMESH_File logFile( aLogFileName.ToCString() );
1107 if ( !logFile.eof() )
1109 char msgLic[] = " Dlim ";
1110 const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1111 if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1112 error("Licence problems.");
1115 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1119 for (int i=0; i<_nbShape; i++)
1120 delete [] tabBox[i];
1125 #ifdef WITH_SMESH_CANCEL_COMPUTE
1126 if(_compute_canceled)
1127 return error(SMESH_Comment("interruption initiated by user"));
1132 //=============================================================================
1134 * \brief Computes mesh without geometry
1135 * \param aMesh - the mesh
1136 * \param aHelper - helper that must be used for adding elements to \aaMesh
1137 * \retval bool - is a success
1139 * The method is called if ( !aMesh->HasShapeToMesh() )
1141 //=============================================================================
1143 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
1145 #ifdef WITH_SMESH_CANCEL_COMPUTE
1146 _compute_canceled = false;
1149 SMESH_ComputeErrorPtr myError = SMESH_ComputeError::New();
1152 TCollection_AsciiString hexahedraMessage;
1154 SetParameters(_hypothesis);
1156 TCollection_AsciiString aTmpDir = getTmpDir();
1157 TCollection_AsciiString Hexotic_In, Hexotic_Out;
1158 TCollection_AsciiString modeFile_In( "chmod 666 " ), modeFile_Out( "chmod 666 " );
1159 TCollection_AsciiString aLogFileName = aTmpDir + "Hexotic.log"; // log
1161 std::map <int,int> aSmdsToHexoticIdMap;
1162 std::map <int,const SMDS_MeshNode*> aHexoticIdToNodeMap;
1164 Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1165 Hexotic_Out = aTmpDir + "Hexotic_Out.mesh";
1167 std::string run_Hexotic = getHexoticCommand(Hexotic_In, Hexotic_Out);
1168 run_Hexotic += std::string(" 1 > ") + aLogFileName.ToCString(); // dump into file
1170 removeHexoticFiles(Hexotic_In, Hexotic_Out);
1173 cout << "Creating Hexotic input mesh file : " << Hexotic_In << std::endl;
1174 aMesh.ExportGMF(Hexotic_In.ToCString(), aHelper->GetMeshDS());
1175 modeFile_In += Hexotic_In;
1176 system( modeFile_In.ToCString() );
1177 aSmdsToHexoticIdMap.clear();
1178 aHexoticIdToNodeMap.clear();
1180 MESSAGE("HexoticPlugin_Hexotic::Compute");
1183 cout << "Hexotic command : " << run_Hexotic << std::endl;
1184 system( run_Hexotic.data() );
1190 std::ifstream fileRes( Hexotic_Out.ToCString() );
1191 modeFile_Out += Hexotic_Out;
1192 system( modeFile_Out.ToCString() );
1193 if ( ! fileRes.fail() ) {
1194 Ok = readResult( Hexotic_Out.ToCString(),
1195 #ifdef WITH_SMESH_CANCEL_COMPUTE
1201 // Remove previous nodes and elements
1202 SMDS_ElemIteratorPtr itElement = aHelper->GetMeshDS()->elementsIterator();
1203 SMDS_NodeIteratorPtr itNode = aHelper->GetMeshDS()->nodesIterator();
1205 while ( itElement->more() )
1206 aHelper->GetMeshDS()->RemoveElement( itElement->next() );
1207 while ( itNode->more() )
1208 aHelper->GetMeshDS()->RemoveNode( itNode->next() );
1211 myError = aMesh.GMFToMesh(Hexotic_Out.ToCString());
1213 itElement = aHelper->GetMeshDS()->elementsIterator();
1214 itNode = aHelper->GetMeshDS()->nodesIterator();
1216 // Assign nodes and elements to the pseudo shape
1217 while ( itNode->more() )
1218 aHelper->GetMeshDS()->SetNodeInVolume(itNode->next(), 1);
1219 while ( itElement->more() )
1220 aHelper->GetMeshDS()->SetMeshElementOnShape(itElement->next(), 1);
1224 hexahedraMessage = "success";
1226 hexahedraMessage = "failed";
1230 myError->myName = COMPERR_EXCEPTION;
1232 hexahedraMessage = "failed";
1233 cout << "Problem with Hexotic output file " << Hexotic_Out << std::endl;
1235 SMESH_File logFile( aLogFileName.ToCString() );
1236 if ( !logFile.eof() )
1238 char msgLic[] = " Dlim ";
1239 const char* fileBeg = logFile.getPos(), *fileEnd = fileBeg + logFile.size();
1240 if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
1241 return error("Licence problems.");
1243 return error(SMESH_Comment("Problem with Hexotic output file ")<<Hexotic_Out);
1245 cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
1248 #ifdef WITH_SMESH_CANCEL_COMPUTE
1249 if(_compute_canceled)
1250 return error(SMESH_Comment("interruption initiated by user"));
1254 return myError->IsOK();
1258 //=============================================================================
1262 //=============================================================================
1264 bool HexoticPlugin_Hexotic::Evaluate(SMESH_Mesh& aMesh,
1265 const TopoDS_Shape& aShape,
1266 MapShapeNbElems& aResMap)
1268 std::vector<int> aResVec(SMDSEntity_Last);
1269 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1270 SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1271 aResMap.insert(std::make_pair(sm,aResVec));
1273 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1274 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Evaluation is not implemented",this));
1279 #ifdef WITH_SMESH_CANCEL_COMPUTE
1280 void HexoticPlugin_Hexotic::CancelCompute()
1282 _compute_canceled = true;
1285 TCollection_AsciiString aTmpDir = getTmpDir();
1286 TCollection_AsciiString Hexotic_In = aTmpDir + "Hexotic_In.mesh";
1287 TCollection_AsciiString cmd = TCollection_AsciiString("ps ux | grep ") + Hexotic_In;
1288 cmd += TCollection_AsciiString(" | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1");
1289 system( cmd.ToCString() );