1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SMESH_Mesh.cxx
24 // Author : Paul RASCLE, EDF
27 #include "SMESH_Mesh.hxx"
28 #include "SMESH_MesherHelper.hxx"
29 #include "SMDS_MeshVolume.hxx"
30 #include "SMDS_SetIterator.hxx"
31 #include "SMESHDS_Document.hxx"
32 #include "SMESHDS_Group.hxx"
33 #include "SMESHDS_GroupOnGeom.hxx"
34 #include "SMESHDS_Script.hxx"
35 #include "SMESHDS_TSubMeshHolder.hxx"
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_Group.hxx"
38 #include "SMESH_HypoFilter.hxx"
39 #include "SMESH_Hypothesis.hxx"
40 #include "SMESH_subMesh.hxx"
42 #include "utilities.h"
44 #include "DriverDAT_W_SMDS_Mesh.h"
45 #include "DriverGMF_Read.hxx"
46 #include "DriverGMF_Write.hxx"
47 #include "DriverMED_R_SMESHDS_Mesh.h"
48 #include "DriverMED_W_SMESHDS_Mesh.h"
49 #include "DriverSTL_R_SMDS_Mesh.h"
50 #include "DriverSTL_W_SMDS_Mesh.h"
51 #include "DriverUNV_R_SMDS_Mesh.h"
52 #include "DriverUNV_W_SMDS_Mesh.h"
54 #include "DriverCGNS_Read.hxx"
55 #include "DriverCGNS_Write.hxx"
58 #include <GEOMUtils.hxx>
60 #undef _Precision_HeaderFile
61 #include <BRepBndLib.hxx>
62 #include <BRepPrimAPI_MakeBox.hxx>
63 #include <Bnd_Box.hxx>
64 #include <TColStd_MapOfInteger.hxx>
66 #include <TopExp_Explorer.hxx>
67 #include <TopTools_ListIteratorOfListOfShape.hxx>
68 #include <TopTools_ListOfShape.hxx>
69 #include <TopTools_MapOfShape.hxx>
70 #include <TopoDS_Iterator.hxx>
72 #include "SMESH_TryCatch.hxx" // include after OCCT headers!
74 #include "Utils_ExceptHandlers.hxx"
77 #include <boost/thread/thread.hpp>
78 #include <boost/bind.hpp>
85 // maximum stored group name length in MED file
86 #define MAX_MED_GROUP_NAME_LENGTH 80
89 static int MYDEBUG = 0;
91 static int MYDEBUG = 0;
94 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
96 typedef SMESH_HypoFilter THypType;
98 class SMESH_Mesh::SubMeshHolder : public SMESHDS_TSubMeshHolder< SMESH_subMesh >
102 //=============================================================================
106 //=============================================================================
108 SMESH_Mesh::SMESH_Mesh(int theLocalId,
111 bool theIsEmbeddedMode,
112 SMESHDS_Document* theDocument):
113 _groupId( 0 ), _nbSubShapes( 0 )
115 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
117 _studyId = theStudyId;
119 _myDocument = theDocument;
120 _myMeshDS = theDocument->NewMesh(theIsEmbeddedMode,theLocalId);
121 _isShapeToMesh = false;
122 _isAutoColor = false;
124 _shapeDiagonal = 0.0;
126 _myMeshDS->ShapeToMesh( PseudoShape() );
127 _subMeshHolder = new SubMeshHolder;
130 //================================================================================
132 * \brief Constructor of SMESH_Mesh being a base of some descendant class
134 //================================================================================
136 SMESH_Mesh::SMESH_Mesh():
141 _isShapeToMesh( false ),
145 _isAutoColor( false ),
146 _isModified( false ),
147 _shapeDiagonal( 0.0 ),
150 _subMeshHolder = new SubMeshHolder;
156 void deleteMeshDS(SMESHDS_Mesh* meshDS)
158 //cout << "deleteMeshDS( " << meshDS << endl;
162 static void* deleteMeshDS(void* meshDS)
164 //cout << "deleteMeshDS( " << meshDS << endl;
165 SMESHDS_Mesh* m = (SMESHDS_Mesh*)meshDS;
174 //=============================================================================
178 //=============================================================================
180 SMESH_Mesh::~SMESH_Mesh()
182 MESSAGE("SMESH_Mesh::~SMESH_Mesh");
184 // avoid usual removal of elements while processing RemoveHypothesis( algo ) event
185 SMESHDS_SubMeshIteratorPtr smIt = _myMeshDS->SubMeshes();
186 while ( smIt->more() )
187 const_cast<SMESHDS_SubMesh*>( smIt->next() )->Clear();
189 // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
190 // Notify event listeners at least that something happens
191 if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
192 sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
195 map < int, SMESH_Group * >::iterator itg;
196 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
197 SMESH_Group *aGroup = (*itg).second;
203 delete _subMeshHolder;
205 if ( _callUp) delete _callUp;
208 // remove self from studyContext
211 StudyContextStruct * studyContext = _gen->GetStudyContext( _studyId );
212 studyContext->mapMesh.erase( _id );
215 _myDocument->RemoveMesh( _id );
219 // delete _myMeshDS, in a thread in order not to block closing a study with large meshes
221 boost::thread aThread(boost::bind( & deleteMeshDS, _myMeshDS ));
224 int result=pthread_create(&thread, NULL, deleteMeshDS, (void*)_myMeshDS);
229 //================================================================================
231 * \brief Return true if a mesh with given id exists
233 //================================================================================
235 bool SMESH_Mesh::MeshExists( int meshId ) const
237 return _myDocument ? bool( _myDocument->GetMesh( meshId )) : false;
240 //=============================================================================
242 * \brief Set geometry to be meshed
244 //=============================================================================
246 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
248 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
250 if ( !aShape.IsNull() && _isShapeToMesh ) {
251 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
252 _myMeshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
253 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
255 // clear current data
256 if ( !_myMeshDS->ShapeToMesh().IsNull() )
258 // removal of a shape to mesh, delete objects referring to sub-shapes:
260 _subMeshHolder->DeleteAll();
261 // - groups on geometry
262 map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
263 while ( i_gr != _mapGroup.end() ) {
264 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
265 _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
267 _mapGroup.erase( i_gr++ );
272 _mapAncestors.Clear();
275 TopoDS_Shape aNullShape;
276 _myMeshDS->ShapeToMesh( aNullShape );
278 _shapeDiagonal = 0.0;
281 // set a new geometry
282 if ( !aShape.IsNull() )
284 _myMeshDS->ShapeToMesh(aShape);
285 _isShapeToMesh = true;
286 _nbSubShapes = _myMeshDS->MaxShapeIndex();
288 // fill map of ancestors
289 fillAncestorsMap(aShape);
293 _isShapeToMesh = false;
294 _shapeDiagonal = 0.0;
295 _myMeshDS->ShapeToMesh( PseudoShape() );
300 //=======================================================================
302 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
304 //=======================================================================
306 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
308 return _myMeshDS->ShapeToMesh();
311 //=======================================================================
313 * \brief Return a solid which is returned by GetShapeToMesh() if
314 * a real geometry to be meshed was not set
316 //=======================================================================
318 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
320 static TopoDS_Solid aSolid;
321 if ( aSolid.IsNull() )
323 aSolid = BRepPrimAPI_MakeBox(1,1,1);
328 //=======================================================================
330 * \brief Return diagonal size of bounding box of a shape
332 //=======================================================================
334 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
336 if ( !aShape.IsNull() ) {
338 // avoid too long waiting on large shapes. PreciseBoundingBox() was added
339 // to assure same result which else depends on presence of triangulation (IPAL52557).
340 const int maxNbFaces = 4000;
342 for ( TopExp_Explorer f( aShape, TopAbs_FACE ); f.More() && nbFaces < maxNbFaces; f.Next() )
344 if ( nbFaces < maxNbFaces )
345 GEOMUtils::PreciseBoundingBox(aShape, Box);
347 BRepBndLib::Add( aShape, Box);
349 return sqrt( Box.SquareExtent() );
354 //=======================================================================
356 * \brief Return diagonal size of bounding box of shape to mesh
358 //=======================================================================
360 double SMESH_Mesh::GetShapeDiagonalSize() const
362 if ( _shapeDiagonal == 0. && _isShapeToMesh )
363 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
365 return _shapeDiagonal;
368 //================================================================================
370 * \brief Load mesh from study file
372 //================================================================================
374 void SMESH_Mesh::Load()
380 //=======================================================================
382 * \brief Remove all nodes and elements
384 //=======================================================================
386 void SMESH_Mesh::Clear()
388 if ( HasShapeToMesh() ) // remove all nodes and elements
391 _myMeshDS->ClearMesh();
393 // update compute state of submeshes
394 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
396 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
397 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
398 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
399 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
402 else // remove only nodes/elements computed by algorithms
404 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
406 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
407 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
408 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
409 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
415 //=======================================================================
417 * \brief Remove all nodes and elements of indicated shape
419 //=======================================================================
421 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
423 // clear sub-meshes; get ready to re-compute as a side-effect
424 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
426 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
427 /*complexShapeFirst=*/false);
428 while ( smIt->more() )
431 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
432 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
433 // all other shapes depends on vertices so they are already cleaned
434 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
435 // to recompute even if failed
436 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
441 //=======================================================================
442 //function : UNVToMesh
444 //=======================================================================
446 int SMESH_Mesh::UNVToMesh(const char* theFileName)
448 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
450 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
451 _isShapeToMesh = false;
452 DriverUNV_R_SMDS_Mesh myReader;
453 myReader.SetMesh(_myMeshDS);
454 myReader.SetFile(theFileName);
455 myReader.SetMeshId(-1);
458 MESSAGE("UNVToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
459 MESSAGE("UNVToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
460 MESSAGE("UNVToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
461 MESSAGE("UNVToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
463 SMDS_MeshGroup* aGroup = (SMDS_MeshGroup*) myReader.GetGroup();
465 TGroupNamesMap aGroupNames = myReader.GetGroupNamesMap();
466 //const TGroupIdMap& aGroupId = myReader.GetGroupIdMap();
467 aGroup->InitSubGroupsIterator();
468 while (aGroup->MoreSubGroups()) {
469 SMDS_MeshGroup* aSubGroup = (SMDS_MeshGroup*) aGroup->NextSubGroup();
470 string aName = aGroupNames[aSubGroup];
473 SMESH_Group* aSMESHGroup = AddGroup( aSubGroup->GetType(), aName.c_str(), aId );
475 if(MYDEBUG) MESSAGE("UNVToMesh - group added: "<<aName);
476 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aSMESHGroup->GetGroupDS() );
478 aGroupDS->SetStoreName(aName.c_str());
479 aSubGroup->InitIterator();
480 const SMDS_MeshElement* aElement = 0;
481 while (aSubGroup->More()) {
482 aElement = aSubGroup->Next();
484 aGroupDS->SMDSGroup().Add(aElement);
488 aGroupDS->SetType(aElement->GetType());
496 //=======================================================================
497 //function : MEDToMesh
499 //=======================================================================
501 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
503 if(MYDEBUG) MESSAGE("MEDToMesh - theFileName = "<<theFileName<<", mesh name = "<<theMeshName);
505 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
506 _isShapeToMesh = false;
507 DriverMED_R_SMESHDS_Mesh myReader;
508 myReader.SetMesh(_myMeshDS);
509 myReader.SetMeshId(-1);
510 myReader.SetFile(theFileName);
511 myReader.SetMeshName(theMeshName);
512 Driver_Mesh::Status status = myReader.Perform();
514 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
515 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
516 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
517 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
520 // Reading groups (sub-meshes are out of scope of MED import functionality)
521 list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
522 if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size());
524 list<TNameAndType>::iterator name_type = aGroupNames.begin();
525 for ( ; name_type != aGroupNames.end(); name_type++ ) {
526 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
528 if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<name_type->first.c_str());
529 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
531 aGroupDS->SetStoreName( name_type->first.c_str() );
532 myReader.GetGroup( aGroupDS );
539 //=======================================================================
540 //function : STLToMesh
542 //=======================================================================
544 int SMESH_Mesh::STLToMesh(const char* theFileName)
546 if(MYDEBUG) MESSAGE("STLToMesh - theFileName = "<<theFileName);
548 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
549 _isShapeToMesh = false;
550 DriverSTL_R_SMDS_Mesh myReader;
551 myReader.SetMesh(_myMeshDS);
552 myReader.SetFile(theFileName);
553 myReader.SetMeshId(-1);
556 MESSAGE("STLToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
557 MESSAGE("STLToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
558 MESSAGE("STLToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
559 MESSAGE("STLToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
564 //================================================================================
566 * \brief Reads the given mesh from the CGNS file
567 * \param theFileName - name of the file
568 * \retval int - Driver_Mesh::Status
570 //================================================================================
572 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
573 const int theMeshIndex,
574 std::string& theMeshName)
576 int res = Driver_Mesh::DRS_FAIL;
579 DriverCGNS_Read myReader;
580 myReader.SetMesh(_myMeshDS);
581 myReader.SetFile(theFileName);
582 myReader.SetMeshId(theMeshIndex);
583 res = myReader.Perform();
584 theMeshName = myReader.GetMeshName();
593 //================================================================================
595 * \brief Fill its data by reading a GMF file
597 //================================================================================
599 SMESH_ComputeErrorPtr SMESH_Mesh::GMFToMesh(const char* theFileName,
600 bool theMakeRequiredGroups)
602 DriverGMF_Read myReader;
603 myReader.SetMesh(_myMeshDS);
604 myReader.SetFile(theFileName);
605 myReader.SetMakeRequiredGroups( theMakeRequiredGroups );
607 //theMeshName = myReader.GetMeshName();
612 return myReader.GetError();
615 //=============================================================================
619 //=============================================================================
621 SMESH_Hypothesis::Hypothesis_Status
622 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
624 std::string* anError ) throw(SALOME_Exception)
626 Unexpect aCatch(SalomeException);
627 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
632 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
633 if ( !subMesh || !subMesh->GetId())
634 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
636 SMESH_Hypothesis *anHyp = GetHypothesis( anHypId );
638 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
640 bool isGlobalHyp = IsMainShape( aSubShape );
642 // NotConformAllowed can be only global
645 // NOTE: this is not a correct way to check a name of hypothesis,
646 // there should be an attribute of hypothesis saying that it can/can't
648 string hypName = anHyp->GetName();
649 if ( hypName == "NotConformAllowed" )
651 if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
652 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
658 bool isAlgo = ( anHyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
659 int event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
661 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
663 if ( anError && SMESH_Hypothesis::IsStatusFatal(ret) && subMesh->GetComputeError() )
664 *anError = subMesh->GetComputeError()->myComment;
667 if ( !SMESH_Hypothesis::IsStatusFatal(ret) &&
668 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
670 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
672 SMESH_Hypothesis::Hypothesis_Status ret2 =
673 subMesh->SubMeshesAlgoStateEngine(event, anHyp, /*exitOnFatal=*/true);
677 if ( SMESH_Hypothesis::IsStatusFatal( ret ))
679 if ( anError && subMesh->GetComputeError() )
680 *anError = subMesh->GetComputeError()->myComment;
682 event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
683 subMesh->AlgoStateEngine(event, anHyp);
687 // check concurent hypotheses on ancestors
688 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
690 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
691 while ( smIt->more() ) {
692 SMESH_subMesh* sm = smIt->next();
693 if ( sm->IsApplicableHypotesis( anHyp )) {
694 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
703 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
705 if(MYDEBUG) subMesh->DumpAlgoState(true);
706 if(MYDEBUG) SCRUTE(ret);
710 //=============================================================================
714 //=============================================================================
716 SMESH_Hypothesis::Hypothesis_Status
717 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
718 int anHypId)throw(SALOME_Exception)
720 Unexpect aCatch(SalomeException);
721 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
723 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
724 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
725 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
727 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
729 SCRUTE(anHyp->GetType());
734 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
735 int event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
737 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
739 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
741 // there may appear concurrent hyps that were covered by the removed hyp
742 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
743 subMesh->IsApplicableHypotesis( anHyp ) &&
744 subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
745 ret = SMESH_Hypothesis::HYP_CONCURENT;
748 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
749 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
751 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
753 SMESH_Hypothesis::Hypothesis_Status ret2 =
754 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
755 if (ret2 > ret) // more severe
758 // check concurent hypotheses on ancestors
759 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
761 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
762 while ( smIt->more() ) {
763 SMESH_subMesh* sm = smIt->next();
764 if ( sm->IsApplicableHypotesis( anHyp )) {
765 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
775 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
777 if(MYDEBUG) subMesh->DumpAlgoState(true);
778 if(MYDEBUG) SCRUTE(ret);
782 //=============================================================================
786 //=============================================================================
788 const list<const SMESHDS_Hypothesis*>&
789 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
790 throw(SALOME_Exception)
792 return _myMeshDS->GetHypothesis(aSubShape);
795 //=======================================================================
797 * \brief Return the hypothesis assigned to the shape
798 * \param aSubShape - the shape to check
799 * \param aFilter - the hypothesis filter
800 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
801 * \param assignedTo - to return the shape the found hypo is assigned to
802 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
804 //=======================================================================
806 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
807 const SMESH_HypoFilter& aFilter,
808 const bool andAncestors,
809 TopoDS_Shape* assignedTo) const
811 return GetHypothesis( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
812 aFilter, andAncestors, assignedTo );
815 //=======================================================================
817 * \brief Return the hypothesis assigned to the shape of a sub-mesh
818 * \param aSubMesh - the sub-mesh to check
819 * \param aFilter - the hypothesis filter
820 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
821 * \param assignedTo - to return the shape the found hypo is assigned to
822 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
824 //=======================================================================
826 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const SMESH_subMesh * aSubMesh,
827 const SMESH_HypoFilter& aFilter,
828 const bool andAncestors,
829 TopoDS_Shape* assignedTo) const
831 if ( !aSubMesh ) return 0;
834 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
835 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
836 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
837 for ( ; hyp != hypList.end(); hyp++ ) {
838 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
839 if ( aFilter.IsOk( h, aSubShape)) {
840 if ( assignedTo ) *assignedTo = aSubShape;
847 // user sorted submeshes of ancestors, according to stored submesh priority
848 std::vector< SMESH_subMesh * > & ancestors =
849 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
850 SortByMeshOrder( ancestors );
852 vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
853 for ( ; smIt != ancestors.end(); smIt++ )
855 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
856 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
857 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
858 for ( ; hyp != hypList.end(); hyp++ ) {
859 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
860 if (aFilter.IsOk( h, curSh )) {
861 if ( assignedTo ) *assignedTo = curSh;
870 //================================================================================
872 * \brief Return hypotheses assigned to the shape
873 * \param aSubShape - the shape to check
874 * \param aFilter - the hypothesis filter
875 * \param aHypList - the list of the found hypotheses
876 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
877 * \retval int - number of unique hypos in aHypList
879 //================================================================================
881 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
882 const SMESH_HypoFilter& aFilter,
883 list <const SMESHDS_Hypothesis * >& aHypList,
884 const bool andAncestors,
885 list< TopoDS_Shape > * assignedTo/*=0*/) const
887 return GetHypotheses( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
888 aFilter, aHypList, andAncestors, assignedTo );
891 //================================================================================
893 * \brief Return hypotheses assigned to the shape of a sub-mesh
894 * \param aSubShape - the sub-mesh to check
895 * \param aFilter - the hypothesis filter
896 * \param aHypList - the list of the found hypotheses
897 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
898 * \retval int - number of unique hypos in aHypList
900 //================================================================================
902 int SMESH_Mesh::GetHypotheses(const SMESH_subMesh * aSubMesh,
903 const SMESH_HypoFilter& aFilter,
904 list <const SMESHDS_Hypothesis * >& aHypList,
905 const bool andAncestors,
906 list< TopoDS_Shape > * assignedTo/*=0*/) const
908 if ( !aSubMesh ) return 0;
910 set<string> hypTypes; // to exclude same type hypos from the result list
913 // only one main hypothesis is allowed
914 bool mainHypFound = false;
917 list<const SMESHDS_Hypothesis*>::const_iterator hyp;
918 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
919 if ( hypTypes.insert( (*hyp)->GetName() ).second )
921 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
925 // get hypos from aSubShape
927 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
928 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
929 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
931 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
932 if (( aFilter.IsOk( h, aSubShape )) &&
933 ( h->IsAuxiliary() || !mainHypFound ) &&
934 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
936 aHypList.push_back( *hyp );
938 if ( !h->IsAuxiliary() )
940 if ( assignedTo ) assignedTo->push_back( aSubShape );
945 // get hypos from ancestors of aSubShape
948 // user sorted submeshes of ancestors, according to stored submesh priority
949 std::vector< SMESH_subMesh * > & ancestors =
950 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
951 SortByMeshOrder( ancestors );
953 vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
954 for ( ; smIt != ancestors.end(); smIt++ )
956 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
957 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
958 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
960 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
961 if (( aFilter.IsOk( h, curSh )) &&
962 ( h->IsAuxiliary() || !mainHypFound ) &&
963 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
965 aHypList.push_back( *hyp );
967 if ( !h->IsAuxiliary() )
969 if ( assignedTo ) assignedTo->push_back( curSh );
977 //================================================================================
979 * \brief Return a hypothesis by its ID
981 //================================================================================
983 SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const int anHypId) const
985 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
986 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
989 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
993 //=============================================================================
997 //=============================================================================
999 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
1001 Unexpect aCatch(SalomeException);
1002 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
1003 return _myMeshDS->GetScript()->GetCommands();
1006 //=============================================================================
1010 //=============================================================================
1011 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
1013 Unexpect aCatch(SalomeException);
1014 if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
1015 _myMeshDS->GetScript()->Clear();
1018 //=============================================================================
1020 * Get or Create the SMESH_subMesh object implementation
1022 //=============================================================================
1024 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
1025 throw(SALOME_Exception)
1027 int index = _myMeshDS->ShapeToIndex(aSubShape);
1028 if ( !index && aSubShape.IsNull() )
1031 // for submeshes on GEOM Group
1032 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
1033 TopoDS_Iterator it( aSubShape );
1036 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
1037 // fill map of Ancestors
1038 while ( _nbSubShapes < index )
1039 fillAncestorsMap( _myMeshDS->IndexToShape( ++_nbSubShapes ));
1043 // return NULL; // neither sub-shape nor a group
1045 SMESH_subMesh* aSubMesh = _subMeshHolder->Get( index );
1048 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
1049 _subMeshHolder->Add( index, aSubMesh );
1051 // include non-computable sub-meshes in SMESH_subMesh::_ancestors of sub-submeshes
1052 switch ( aSubShape.ShapeType() ) {
1053 case TopAbs_COMPOUND:
1056 for ( TopoDS_Iterator subIt( aSubShape ); subIt.More(); subIt.Next() )
1058 SMESH_subMesh* sm = GetSubMesh( subIt.Value() );
1059 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*inclideSelf=*/true);
1060 while ( smIt->more() )
1061 smIt->next()->ClearAncestors();
1069 //=============================================================================
1071 * Get the SMESH_subMesh object implementation. Dont create it, return null
1072 * if it does not exist.
1074 //=============================================================================
1076 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
1077 throw(SALOME_Exception)
1079 int index = _myMeshDS->ShapeToIndex(aSubShape);
1080 return GetSubMeshContaining( index );
1083 //=============================================================================
1085 * Get the SMESH_subMesh object implementation. Dont create it, return null
1086 * if it does not exist.
1088 //=============================================================================
1090 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
1091 throw(SALOME_Exception)
1093 SMESH_subMesh *aSubMesh = _subMeshHolder->Get( aShapeID );
1098 //================================================================================
1100 * \brief Return submeshes of groups containing the given sub-shape
1102 //================================================================================
1104 list<SMESH_subMesh*>
1105 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
1106 throw(SALOME_Exception)
1108 list<SMESH_subMesh*> found;
1110 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
1114 // submeshes of groups have max IDs, so search from the map end
1115 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator( /*reverse=*/true ) );
1116 while ( smIt->more() ) {
1117 SMESH_subMesh* sm = smIt->next();
1118 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
1119 if ( ds && ds->IsComplexSubmesh() ) {
1120 if ( SMESH_MesherHelper::IsSubShape( aSubShape, sm->GetSubShape() ))
1122 found.push_back( sm );
1126 break; // the rest sub-meshes are not those of groups
1130 if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
1132 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1133 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1135 TopoDS_Iterator it( mainSM->GetSubShape() );
1136 if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
1137 SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
1138 found.push_back( mainSM );
1143 //=======================================================================
1144 //function : IsUsedHypothesis
1145 //purpose : Return True if anHyp is used to mesh aSubShape
1146 //=======================================================================
1148 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
1149 const SMESH_subMesh* aSubMesh)
1151 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
1153 // check if anHyp can be used to mesh aSubMesh
1154 if ( !aSubMesh || !aSubMesh->IsApplicableHypotesis( hyp ))
1157 SMESH_Algo *algo = aSubMesh->GetAlgo();
1160 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1161 return ( anHyp == algo );
1163 // algorithm parameter
1166 // look trough hypotheses used by algo
1167 const SMESH_HypoFilter* hypoKind;
1168 if (( hypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() ))) {
1169 list <const SMESHDS_Hypothesis * > usedHyps;
1170 if ( GetHypotheses( aSubMesh, *hypoKind, usedHyps, true ))
1171 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1178 //=============================================================================
1182 //=============================================================================
1184 // const list < SMESH_subMesh * >&
1185 // SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
1186 // throw(SALOME_Exception)
1188 // _subMeshesUsingHypothesisList.clear();
1189 // SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1190 // while ( smIt->more() )
1192 // SMESH_subMesh* aSubMesh = smIt->next();
1193 // if ( IsUsedHypothesis ( anHyp, aSubMesh ))
1194 // _subMeshesUsingHypothesisList.push_back( aSubMesh );
1196 // return _subMeshesUsingHypothesisList;
1199 //=======================================================================
1200 //function : NotifySubMeshesHypothesisModification
1201 //purpose : Say all submeshes using theChangedHyp that it has been modified
1202 //=======================================================================
1204 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1206 Unexpect aCatch(SalomeException);
1208 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1212 _callUp->HypothesisModified();
1215 const SMESH_HypoFilter* compatibleHypoKind;
1216 list <const SMESHDS_Hypothesis * > usedHyps;
1218 // keep sub-meshes not to miss ones whose state can change due to notifying others
1219 vector< SMESH_subMesh* > smToNotify;
1221 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1222 while ( smIt->more() )
1224 SMESH_subMesh* aSubMesh = smIt->next();
1226 // if aSubMesh meshing depends on hyp,
1227 // we call aSubMesh->AlgoStateEngine( MODIF_HYP, hyp ) that causes either
1228 // 1) clearing of already computed aSubMesh or
1229 // 2) changing algo_state from MISSING_HYP to HYP_OK when parameters of hyp becomes valid,
1230 // other possible changes are not interesting. (IPAL0052457 - assigning hyp performance pb)
1231 if ( aSubMesh->GetComputeState() != SMESH_subMesh::COMPUTE_OK &&
1232 aSubMesh->GetComputeState() != SMESH_subMesh::FAILED_TO_COMPUTE &&
1233 aSubMesh->GetAlgoState() != SMESH_subMesh::MISSING_HYP &&
1234 !hyp->DataDependOnParams() )
1237 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1239 if (( aSubMesh->IsApplicableHypotesis( hyp )) &&
1240 ( algo = aSubMesh->GetAlgo() ) &&
1241 ( compatibleHypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() )) &&
1242 ( compatibleHypoKind->IsOk( hyp, aSubShape )))
1244 // check if hyp is used by algo
1246 if ( GetHypotheses( aSubMesh, *compatibleHypoKind, usedHyps, true ) &&
1247 find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
1249 smToNotify.push_back( aSubMesh );
1254 for ( size_t i = 0; i < smToNotify.size(); ++i )
1256 smToNotify[i]->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1257 const_cast< SMESH_Hypothesis*>( hyp ));
1260 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1261 GetMeshDS()->Modified();
1264 //=============================================================================
1266 * Auto color functionality
1268 //=============================================================================
1269 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1271 Unexpect aCatch(SalomeException);
1272 _isAutoColor = theAutoColor;
1275 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1277 Unexpect aCatch(SalomeException);
1278 return _isAutoColor;
1281 //=======================================================================
1282 //function : SetIsModified
1283 //purpose : Set the flag meaning that the mesh has been edited "manually"
1284 //=======================================================================
1286 void SMESH_Mesh::SetIsModified(bool isModified)
1288 _isModified = isModified;
1291 // check if mesh becomes empty as result of modification
1292 HasModificationsToDiscard();
1295 //=======================================================================
1296 //function : HasModificationsToDiscard
1297 //purpose : Return true if the mesh has been edited since a total re-compute
1298 // and those modifications may prevent successful partial re-compute.
1299 // As a side effect reset _isModified flag if mesh is empty
1301 //=======================================================================
1303 bool SMESH_Mesh::HasModificationsToDiscard() const
1305 if ( ! _isModified )
1308 // return true if the next Compute() will be partial and
1309 // existing but changed elements may prevent successful re-compute
1310 bool hasComputed = false, hasNotComputed = false;
1311 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1312 while ( smIt->more() )
1314 const SMESH_subMesh* aSubMesh = smIt->next();
1315 switch ( aSubMesh->GetSubShape().ShapeType() )
1320 if ( aSubMesh->IsMeshComputed() )
1323 hasNotComputed = true;
1324 if ( hasComputed && hasNotComputed)
1328 if ( NbNodes() < 1 )
1329 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1334 //================================================================================
1336 * \brief Check if any groups of the same type have equal names
1338 //================================================================================
1340 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1342 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1343 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1344 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1346 SMESH_Group* aGroup = it->second;
1347 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1348 string aGroupName = aGroup->GetName();
1349 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1350 if (!aGroupNames[aType].insert(aGroupName).second)
1357 //================================================================================
1359 * \brief Export the mesh to a med file
1360 * \param [in] file - name of the MED file
1361 * \param [in] theMeshName - name of this mesh
1362 * \param [in] theAutoGroups - boolean parameter for creating/not creating
1363 * the groups Group_On_All_Nodes, Group_On_All_Faces, ... ;
1364 * the typical use is auto_groups=false.
1365 * \param [in] theVersion - defines the version of format of MED file, that will be created
1366 * \param [in] meshPart - mesh data to export
1367 * \param [in] theAutoDimension - if \c true, a space dimension of a MED mesh can be either
1368 * - 1D if all mesh nodes lie on OX coordinate axis, or
1369 * - 2D if all mesh nodes lie on XOY coordinate plane, or
1370 * - 3D in the rest cases.
1371 * If \a theAutoDimension is \c false, the space dimension is always 3.
1372 * \return int - mesh index in the file
1374 //================================================================================
1376 void SMESH_Mesh::ExportMED(const char * file,
1377 const char* theMeshName,
1380 const SMESHDS_Mesh* meshPart,
1381 bool theAutoDimension,
1382 bool theAddODOnVertices)
1383 throw(SALOME_Exception)
1387 DriverMED_W_SMESHDS_Mesh myWriter;
1388 myWriter.SetFile ( file, MED::EVersion(theVersion) );
1389 myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1390 myWriter.SetAutoDimension( theAutoDimension );
1391 myWriter.AddODOnVertices ( theAddODOnVertices );
1393 myWriter.SetMeshId ( _id );
1395 myWriter.SetMeshId ( -1 );
1396 myWriter.SetMeshName ( theMeshName );
1399 if ( theAutoGroups ) {
1400 myWriter.AddGroupOfNodes();
1401 myWriter.AddGroupOfEdges();
1402 myWriter.AddGroupOfFaces();
1403 myWriter.AddGroupOfVolumes();
1406 // Pass groups to writer. Provide unique group names.
1407 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1410 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1412 int maxNbIter = 10000; // to guarantee cycle finish
1413 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1414 SMESH_Group* aGroup = it->second;
1415 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1417 SMDSAbs_ElementType aType = aGroupDS->GetType();
1418 string aGroupName0 = aGroup->GetName();
1419 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1420 string aGroupName = aGroupName0;
1421 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1422 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1423 aGroupName = aString;
1424 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1426 aGroupDS->SetStoreName( aGroupName.c_str() );
1427 myWriter.AddGroup( aGroupDS );
1434 SMESH_CATCH( SMESH::throwSalomeEx );
1437 //================================================================================
1439 * \brief Export the mesh to a SAUV file
1441 //================================================================================
1443 void SMESH_Mesh::ExportSAUV(const char *file,
1444 const char* theMeshName,
1446 throw(SALOME_Exception)
1448 std::string medfilename(file);
1449 medfilename += ".med";
1452 cmd = "%PYTHONBIN% ";
1457 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1459 system(cmd.c_str());
1460 ExportMED(medfilename.c_str(), theMeshName, theAutoGroups, 1);
1462 cmd = "%PYTHONBIN% ";
1467 cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
1469 system(cmd.c_str());
1471 cmd = "%PYTHONBIN% ";
1476 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1478 system(cmd.c_str());
1481 //================================================================================
1483 * \brief Export the mesh to a DAT file
1485 //================================================================================
1487 void SMESH_Mesh::ExportDAT(const char * file,
1488 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1490 Unexpect aCatch(SalomeException);
1491 DriverDAT_W_SMDS_Mesh myWriter;
1492 myWriter.SetFile( file );
1493 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1494 myWriter.SetMeshId(_id);
1498 //================================================================================
1500 * \brief Export the mesh to an UNV file
1502 //================================================================================
1504 void SMESH_Mesh::ExportUNV(const char * file,
1505 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1507 Unexpect aCatch(SalomeException);
1508 DriverUNV_W_SMDS_Mesh myWriter;
1509 myWriter.SetFile( file );
1510 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1511 myWriter.SetMeshId(_id);
1512 // myWriter.SetGroups(_mapGroup);
1516 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1517 SMESH_Group* aGroup = it->second;
1518 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1520 string aGroupName = aGroup->GetName();
1521 aGroupDS->SetStoreName( aGroupName.c_str() );
1522 myWriter.AddGroup( aGroupDS );
1529 //================================================================================
1531 * \brief Export the mesh to an STL file
1533 //================================================================================
1535 void SMESH_Mesh::ExportSTL(const char * file,
1537 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1539 Unexpect aCatch(SalomeException);
1540 DriverSTL_W_SMDS_Mesh myWriter;
1541 myWriter.SetFile( file );
1542 myWriter.SetIsAscii( isascii );
1543 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1544 myWriter.SetMeshId(_id);
1548 //================================================================================
1550 * \brief Export the mesh to the CGNS file
1552 //================================================================================
1554 void SMESH_Mesh::ExportCGNS(const char * file,
1555 const SMESHDS_Mesh* meshDS)
1557 int res = Driver_Mesh::DRS_FAIL;
1559 DriverCGNS_Write myWriter;
1560 myWriter.SetFile( file );
1561 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1562 myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1563 res = myWriter.Perform();
1565 if ( res != Driver_Mesh::DRS_OK )
1566 throw SALOME_Exception("Export failed");
1569 //================================================================================
1571 * \brief Export the mesh to a GMF file
1573 //================================================================================
1575 void SMESH_Mesh::ExportGMF(const char * file,
1576 const SMESHDS_Mesh* meshDS,
1577 bool withRequiredGroups)
1579 DriverGMF_Write myWriter;
1580 myWriter.SetFile( file );
1581 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1582 myWriter.SetExportRequiredGroups( withRequiredGroups );
1587 //================================================================================
1589 * \brief Return a ratio of "compute cost" of computed sub-meshes to the whole
1592 //================================================================================
1594 double SMESH_Mesh::GetComputeProgress() const
1596 double totalCost = 1e-100, computedCost = 0;
1597 const SMESH_subMesh* curSM = _gen->GetCurrentSubMesh();
1599 // get progress of a current algo
1600 TColStd_MapOfInteger currentSubIds;
1602 if ( SMESH_Algo* algo = curSM->GetAlgo() )
1604 int algoNotDoneCost = 0, algoDoneCost = 0;
1605 const std::vector<SMESH_subMesh*>& smToCompute = algo->SubMeshesToCompute();
1606 for ( size_t i = 0; i < smToCompute.size(); ++i )
1608 if ( smToCompute[i]->IsEmpty() )
1609 algoNotDoneCost += smToCompute[i]->GetComputeCost();
1611 algoDoneCost += smToCompute[i]->GetComputeCost();
1612 currentSubIds.Add( smToCompute[i]->GetId() );
1618 rate = algo->GetProgress();
1622 cerr << "Exception in " << algo->GetName() << "::GetProgress()" << endl;
1625 if ( 0. < rate && rate < 1.001 )
1627 computedCost += rate * ( algoDoneCost + algoNotDoneCost );
1631 rate = algo->GetProgressByTic();
1632 computedCost += algoDoneCost + rate * algoNotDoneCost;
1634 // cout << "rate: "<<rate << " algoNotDoneCost: " << algoNotDoneCost << endl;
1637 // get cost of already treated sub-meshes
1638 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1640 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1641 while ( smIt->more() )
1643 const SMESH_subMesh* sm = smIt->next();
1644 const int smCost = sm->GetComputeCost();
1645 totalCost += smCost;
1646 if ( !currentSubIds.Contains( sm->GetId() ) )
1648 if (( !sm->IsEmpty() ) ||
1649 ( sm->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
1650 !sm->DependsOn( curSM ) ))
1651 computedCost += smCost;
1655 // cout << "Total: " << totalCost
1656 // << " computed: " << computedCost << " progress: " << computedCost / totalCost
1657 // << " nbElems: " << GetMeshDS()->GetMeshInfo().NbElements() << endl;
1658 return computedCost / totalCost;
1661 //================================================================================
1663 * \brief Return number of nodes in the mesh
1665 //================================================================================
1667 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1669 Unexpect aCatch(SalomeException);
1670 return _myMeshDS->NbNodes();
1673 //================================================================================
1675 * \brief Return number of edges of given order in the mesh
1677 //================================================================================
1679 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1681 Unexpect aCatch(SalomeException);
1682 return _myMeshDS->GetMeshInfo().Nb0DElements();
1685 //================================================================================
1687 * \brief Return number of edges of given order in the mesh
1689 //================================================================================
1691 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1693 Unexpect aCatch(SalomeException);
1694 return _myMeshDS->GetMeshInfo().NbEdges(order);
1697 //================================================================================
1699 * \brief Return number of faces of given order in the mesh
1701 //================================================================================
1703 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1705 Unexpect aCatch(SalomeException);
1706 return _myMeshDS->GetMeshInfo().NbFaces(order);
1709 //================================================================================
1711 * \brief Return the number of faces in the mesh
1713 //================================================================================
1715 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1717 Unexpect aCatch(SalomeException);
1718 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1721 //================================================================================
1723 * \brief Return number of biquadratic triangles in the mesh
1725 //================================================================================
1727 int SMESH_Mesh::NbBiQuadTriangles() const throw(SALOME_Exception)
1729 Unexpect aCatch(SalomeException);
1730 return _myMeshDS->GetMeshInfo().NbBiQuadTriangles();
1733 //================================================================================
1735 * \brief Return the number nodes faces in the mesh
1737 //================================================================================
1739 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1741 Unexpect aCatch(SalomeException);
1742 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1745 //================================================================================
1747 * \brief Return number of biquadratic quadrangles in the mesh
1749 //================================================================================
1751 int SMESH_Mesh::NbBiQuadQuadrangles() const throw(SALOME_Exception)
1753 Unexpect aCatch(SalomeException);
1754 return _myMeshDS->GetMeshInfo().NbBiQuadQuadrangles();
1757 //================================================================================
1759 * \brief Return the number of polygonal faces in the mesh
1761 //================================================================================
1763 int SMESH_Mesh::NbPolygons() const throw(SALOME_Exception)
1765 Unexpect aCatch(SalomeException);
1766 return _myMeshDS->GetMeshInfo().NbPolygons();
1769 //================================================================================
1771 * \brief Return number of volumes of given order in the mesh
1773 //================================================================================
1775 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1777 Unexpect aCatch(SalomeException);
1778 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1781 //================================================================================
1783 * \brief Return number of tetrahedrons of given order in the mesh
1785 //================================================================================
1787 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1789 Unexpect aCatch(SalomeException);
1790 return _myMeshDS->GetMeshInfo().NbTetras(order);
1793 //================================================================================
1795 * \brief Return number of hexahedrons of given order in the mesh
1797 //================================================================================
1799 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1801 Unexpect aCatch(SalomeException);
1802 return _myMeshDS->GetMeshInfo().NbHexas(order);
1805 //================================================================================
1807 * \brief Return number of triquadratic hexahedrons in the mesh
1809 //================================================================================
1811 int SMESH_Mesh::NbTriQuadraticHexas() const throw(SALOME_Exception)
1813 Unexpect aCatch(SalomeException);
1814 return _myMeshDS->GetMeshInfo().NbTriQuadHexas();
1817 //================================================================================
1819 * \brief Return number of pyramids of given order in the mesh
1821 //================================================================================
1823 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1825 Unexpect aCatch(SalomeException);
1826 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1829 //================================================================================
1831 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1833 //================================================================================
1835 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1837 Unexpect aCatch(SalomeException);
1838 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1841 //================================================================================
1843 * \brief Return number of hexagonal prisms in the mesh
1845 //================================================================================
1847 int SMESH_Mesh::NbHexagonalPrisms() const throw(SALOME_Exception)
1849 Unexpect aCatch(SalomeException);
1850 return _myMeshDS->GetMeshInfo().NbHexPrisms();
1853 //================================================================================
1855 * \brief Return number of polyhedrons in the mesh
1857 //================================================================================
1859 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1861 Unexpect aCatch(SalomeException);
1862 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1865 //================================================================================
1867 * \brief Return number of ball elements in the mesh
1869 //================================================================================
1871 int SMESH_Mesh::NbBalls() const throw(SALOME_Exception)
1873 Unexpect aCatch(SalomeException);
1874 return _myMeshDS->GetMeshInfo().NbBalls();
1877 //================================================================================
1879 * \brief Return number of submeshes in the mesh
1881 //================================================================================
1883 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1885 Unexpect aCatch(SalomeException);
1886 return _myMeshDS->NbSubMesh();
1889 //================================================================================
1891 * \brief Returns number of meshes in the Study, that is supposed to be
1892 * equal to SMESHDS_Document::NbMeshes()
1894 //================================================================================
1896 int SMESH_Mesh::NbMeshes() const // nb meshes in the Study
1898 return _myDocument->NbMeshes();
1901 //=======================================================================
1902 //function : IsNotConformAllowed
1903 //purpose : check if a hypothesis alowing notconform mesh is present
1904 //=======================================================================
1906 bool SMESH_Mesh::IsNotConformAllowed() const
1908 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1910 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1911 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
1914 //=======================================================================
1915 //function : IsMainShape
1917 //=======================================================================
1919 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1921 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1924 //=============================================================================
1928 //=============================================================================
1930 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1931 const char* theName,
1933 const TopoDS_Shape& theShape,
1934 const SMESH_PredicatePtr& thePredicate)
1936 if (_mapGroup.count(_groupId))
1939 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape, thePredicate);
1940 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1941 _mapGroup[_groupId++] = aGroup;
1945 //================================================================================
1947 * \brief Creates a group based on an existing SMESHDS group. Group ID should be unique
1949 //================================================================================
1951 SMESH_Group* SMESH_Mesh::AddGroup (SMESHDS_GroupBase* groupDS) throw(SALOME_Exception)
1954 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup(): NULL SMESHDS_GroupBase"));
1956 map <int, SMESH_Group*>::iterator i_g = _mapGroup.find( groupDS->GetID() );
1957 if ( i_g != _mapGroup.end() && i_g->second )
1959 if ( i_g->second->GetGroupDS() == groupDS )
1962 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup() wrong ID of SMESHDS_GroupBase"));
1964 SMESH_Group* aGroup = new SMESH_Group (groupDS);
1965 _mapGroup[ groupDS->GetID() ] = aGroup;
1966 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1968 _groupId = 1 + _mapGroup.rbegin()->first;
1974 //================================================================================
1976 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
1977 * \retval bool - true if new SMESH_Groups have been created
1980 //================================================================================
1982 bool SMESH_Mesh::SynchronizeGroups()
1984 int nbGroups = _mapGroup.size();
1985 const set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
1986 set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
1987 for ( ; gIt != groups.end(); ++gIt )
1989 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
1990 _groupId = groupDS->GetID();
1991 if ( !_mapGroup.count( _groupId ))
1992 _mapGroup[_groupId] = new SMESH_Group( groupDS );
1994 if ( !_mapGroup.empty() )
1995 _groupId = _mapGroup.rbegin()->first + 1;
1997 return nbGroups < _mapGroup.size();
2000 //================================================================================
2002 * \brief Return iterator on all existing groups
2004 //================================================================================
2006 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
2008 typedef map <int, SMESH_Group *> TMap;
2009 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
2012 //=============================================================================
2014 * \brief Return a group by ID
2016 //=============================================================================
2018 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
2020 if (_mapGroup.find(theGroupID) == _mapGroup.end())
2022 return _mapGroup[theGroupID];
2026 //=============================================================================
2028 * \brief Return IDs of all groups
2030 //=============================================================================
2032 list<int> SMESH_Mesh::GetGroupIds() const
2035 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
2036 anIds.push_back( it->first );
2041 //================================================================================
2043 * \brief Set a caller of methods at level of CORBA API implementation.
2044 * The set upCaller will be deleted by SMESH_Mesh
2046 //================================================================================
2048 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
2050 if ( _callUp ) delete _callUp;
2054 //=============================================================================
2058 //=============================================================================
2060 bool SMESH_Mesh::RemoveGroup (const int theGroupID)
2062 if (_mapGroup.find(theGroupID) == _mapGroup.end())
2064 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
2065 delete _mapGroup[theGroupID];
2066 _mapGroup.erase (theGroupID);
2068 _callUp->RemoveGroup( theGroupID );
2072 //=======================================================================
2073 //function : GetAncestors
2074 //purpose : return list of ancestors of theSubShape in the order
2075 // that lower dimention shapes come first.
2076 //=======================================================================
2078 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
2080 if ( _mapAncestors.Contains( theS ) )
2081 return _mapAncestors.FindFromKey( theS );
2083 static TopTools_ListOfShape emptyList;
2087 //=======================================================================
2089 //purpose : dumps contents of mesh to stream [ debug purposes ]
2090 //=======================================================================
2092 ostream& SMESH_Mesh::Dump(ostream& save)
2095 save << "========================== Dump contents of mesh ==========================" << endl << endl;
2096 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
2097 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
2098 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
2099 save << ++clause << ") Total number of polygons:\t" << NbPolygons() << endl;
2100 save << ++clause << ") Total number of volumes:\t" << NbVolumes() << endl;
2101 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
2102 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
2104 string orderStr = isQuadratic ? "quadratic" : "linear";
2105 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
2107 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
2108 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
2109 if ( NbFaces(order) > 0 ) {
2110 int nb3 = NbTriangles(order);
2111 int nb4 = NbQuadrangles(order);
2112 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
2113 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
2114 if ( nb3 + nb4 != NbFaces(order) ) {
2115 map<int,int> myFaceMap;
2116 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
2117 while( itFaces->more( ) ) {
2118 int nbNodes = itFaces->next()->NbNodes();
2119 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
2120 myFaceMap[ nbNodes ] = 0;
2121 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
2123 save << clause << ".3) Faces in detail: " << endl;
2124 map <int,int>::iterator itF;
2125 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
2126 save << "--> nb nodes: " << itF->first << " - nb elemens:\t" << itF->second << endl;
2129 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
2130 if ( NbVolumes(order) > 0 ) {
2131 int nb8 = NbHexas(order);
2132 int nb4 = NbTetras(order);
2133 int nb5 = NbPyramids(order);
2134 int nb6 = NbPrisms(order);
2135 save << clause << ".1) Number of " << orderStr << " hexahedrons:\t" << nb8 << endl;
2136 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
2137 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
2138 save << clause << ".4) Number of " << orderStr << " pyramids:\t" << nb5 << endl;
2139 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
2140 map<int,int> myVolumesMap;
2141 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
2142 while( itVolumes->more( ) ) {
2143 int nbNodes = itVolumes->next()->NbNodes();
2144 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
2145 myVolumesMap[ nbNodes ] = 0;
2146 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
2148 save << clause << ".5) Volumes in detail: " << endl;
2149 map <int,int>::iterator itV;
2150 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
2151 save << "--> nb nodes: " << itV->first << " - nb elemens:\t" << itV->second << endl;
2156 save << "===========================================================================" << endl;
2160 //=======================================================================
2161 //function : GetElementType
2162 //purpose : Returns type of mesh element with certain id
2163 //=======================================================================
2165 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
2167 return _myMeshDS->GetElementType( id, iselem );
2170 //=============================================================================
2172 * \brief Convert group on geometry into standalone group
2174 //=============================================================================
2176 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
2178 SMESH_Group* aGroup = 0;
2179 map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
2180 if ( itg == _mapGroup.end() )
2183 SMESH_Group* anOldGrp = (*itg).second;
2184 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
2185 if ( !anOldGrp || !anOldGrpDS )
2188 // create new standalone group
2189 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
2190 _mapGroup[theGroupID] = aGroup;
2192 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
2193 GetMeshDS()->RemoveGroup( anOldGrpDS );
2194 GetMeshDS()->AddGroup( aNewGrpDS );
2196 // add elements (or nodes) into new created group
2197 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
2198 while ( anItr->more() )
2199 aNewGrpDS->Add( (anItr->next())->GetID() );
2202 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
2210 //=============================================================================
2212 * \brief remove submesh order from Mesh
2214 //=============================================================================
2216 void SMESH_Mesh::ClearMeshOrder()
2218 _mySubMeshOrder.clear();
2221 //=============================================================================
2223 * \brief remove submesh order from Mesh
2225 //=============================================================================
2227 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
2229 _mySubMeshOrder = theOrder;
2232 //=============================================================================
2234 * \brief return submesh order if any
2236 //=============================================================================
2238 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
2240 return _mySubMeshOrder;
2243 //=============================================================================
2245 * \brief fill _mapAncestors
2247 //=============================================================================
2249 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
2251 int desType, ancType;
2252 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
2254 // a geom group is added. Insert it into lists of ancestors before
2255 // the first ancestor more complex than group members
2256 TopoDS_Iterator subIt( theShape );
2257 if ( !subIt.More() ) return;
2258 int memberType = subIt.Value().ShapeType();
2259 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
2260 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
2262 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
2263 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
2264 TopTools_ListIteratorOfListOfShape ancIt (ancList);
2265 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
2267 if ( ancIt.More() ) ancList.InsertBefore( theShape, ancIt );
2268 else ancList.Append( theShape );
2271 else // else added for 52457: Addition of hypotheses is 8 time longer than meshing
2273 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
2274 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
2275 TopExp::MapShapesAndAncestors ( theShape,
2276 (TopAbs_ShapeEnum) desType,
2277 (TopAbs_ShapeEnum) ancType,
2280 // visit COMPOUNDs inside a COMPOUND that are not reachable by TopExp_Explorer
2281 if ( theShape.ShapeType() == TopAbs_COMPOUND )
2283 TopoDS_Iterator sIt(theShape);
2284 if ( sIt.More() && sIt.Value().ShapeType() == TopAbs_COMPOUND )
2285 for ( ; sIt.More(); sIt.Next() )
2286 if ( sIt.Value().ShapeType() == TopAbs_COMPOUND )
2287 fillAncestorsMap( sIt.Value() );
2291 //=============================================================================
2293 * \brief sort submeshes according to stored mesh order
2294 * \param theListToSort in out list to be sorted
2295 * \return FALSE if nothing sorted
2297 //=============================================================================
2299 bool SMESH_Mesh::SortByMeshOrder(std::vector<SMESH_subMesh*>& theListToSort) const
2301 if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
2305 vector<SMESH_subMesh*> onlyOrderedList;
2306 // collect all ordered submeshes in one list as pointers
2307 // and get their positions within theListToSort
2308 typedef vector<SMESH_subMesh*>::iterator TPosInList;
2309 map< int, TPosInList > sortedPos;
2310 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
2311 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2312 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++)
2314 const TListOfInt& listOfId = *listIdsIt;
2315 // convert sm ids to sm's
2316 vector<SMESH_subMesh*> smVec;
2317 TListOfInt::const_iterator idIt = listOfId.begin();
2318 for ( ; idIt != listOfId.end(); idIt++ ) {
2319 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt )) {
2320 if ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->IsComplexSubmesh() )
2322 SMESHDS_SubMeshIteratorPtr smdsIt = sm->GetSubMeshDS()->GetSubMeshIterator();
2323 while ( smdsIt->more() )
2325 const SMESHDS_SubMesh* smDS = smdsIt->next();
2326 if (( sm = GetSubMeshContaining( smDS->GetID() )))
2327 smVec.push_back( sm );
2332 smVec.push_back( sm );
2336 // find smVec items in theListToSort
2337 for ( size_t i = 0; i < smVec.size(); ++i )
2339 TPosInList smPos = find( smBeg, smEnd, smVec[i] );
2340 if ( smPos != smEnd ) {
2341 onlyOrderedList.push_back( smVec[i] );
2342 sortedPos[ distance( smBeg, smPos )] = smPos;
2346 if (onlyOrderedList.size() < 2)
2350 vector<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
2351 vector<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
2353 // iterate on ordered submeshes and insert them in detected positions
2354 map< int, TPosInList >::iterator i_pos = sortedPos.begin();
2355 for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
2356 *(i_pos->second) = *onlyBIt;
2361 //================================================================================
2363 * \brief Return true if given order of sub-meshes is OK
2365 //================================================================================
2367 bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore,
2368 const SMESH_subMesh* smAfter ) const
2370 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2371 TListOfInt::const_iterator idBef, idAft;
2372 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++)
2374 const TListOfInt& listOfId = *listIdsIt;
2375 idBef = std::find( listOfId.begin(), listOfId.end(), smBefore->GetId() );
2376 if ( idBef != listOfId.end() )
2377 idAft = std::find( listOfId.begin(), listOfId.end(), smAfter->GetId() );
2378 if ( idAft != listOfId.end () )
2379 return ( std::distance( listOfId.begin(), idBef ) <
2380 std::distance( listOfId.begin(), idAft ) );
2382 return true; // no order imposed to given submeshes
2385 //=============================================================================
2387 * \brief sort submeshes according to stored mesh order
2388 * \param theListToSort in out list to be sorted
2389 * \return FALSE if nothing sorted
2391 //=============================================================================
2393 void SMESH_Mesh::getAncestorsSubMeshes (const TopoDS_Shape& theSubShape,
2394 std::vector< SMESH_subMesh* >& theSubMeshes) const
2396 theSubMeshes.clear();
2397 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2398 for (; it.More(); it.Next() )
2399 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2400 theSubMeshes.push_back(sm);
2402 // sort submeshes according to stored mesh order
2403 SortByMeshOrder( theSubMeshes );