1 // Copyright (C) 2007-2016 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 if(MYDEBUG) 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 if(MYDEBUG) 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 Return a mesh by id
244 //================================================================================
246 SMESH_Mesh* SMESH_Mesh::FindMesh( int meshId ) const
249 return (SMESH_Mesh*) this;
251 if ( StudyContextStruct *aStudyContext = _gen->GetStudyContext( _studyId ))
253 std::map < int, SMESH_Mesh * >::iterator i_m = aStudyContext->mapMesh.find( meshId );
254 if ( i_m != aStudyContext->mapMesh.end() )
260 //=============================================================================
262 * \brief Set geometry to be meshed
264 //=============================================================================
266 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
268 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
270 if ( !aShape.IsNull() && _isShapeToMesh ) {
271 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
272 _myMeshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
273 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
275 // clear current data
276 if ( !_myMeshDS->ShapeToMesh().IsNull() )
278 // removal of a shape to mesh, delete objects referring to sub-shapes:
280 _subMeshHolder->DeleteAll();
281 // - groups on geometry
282 map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
283 while ( i_gr != _mapGroup.end() ) {
284 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
285 _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
287 _mapGroup.erase( i_gr++ );
292 _mapAncestors.Clear();
295 TopoDS_Shape aNullShape;
296 _myMeshDS->ShapeToMesh( aNullShape );
298 _shapeDiagonal = 0.0;
301 // set a new geometry
302 if ( !aShape.IsNull() )
304 _myMeshDS->ShapeToMesh(aShape);
305 _isShapeToMesh = true;
306 _nbSubShapes = _myMeshDS->MaxShapeIndex();
308 // fill map of ancestors
309 fillAncestorsMap(aShape);
313 _isShapeToMesh = false;
314 _shapeDiagonal = 0.0;
315 _myMeshDS->ShapeToMesh( PseudoShape() );
320 //=======================================================================
322 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
324 //=======================================================================
326 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
328 return _myMeshDS->ShapeToMesh();
331 //=======================================================================
333 * \brief Return a solid which is returned by GetShapeToMesh() if
334 * a real geometry to be meshed was not set
336 //=======================================================================
338 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
340 static TopoDS_Solid aSolid;
341 if ( aSolid.IsNull() )
343 aSolid = BRepPrimAPI_MakeBox(1,1,1);
348 //=======================================================================
350 * \brief Return diagonal size of bounding box of a shape
352 //=======================================================================
354 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
356 if ( !aShape.IsNull() ) {
358 // avoid too long waiting on large shapes. PreciseBoundingBox() was added
359 // to assure same result which else depends on presence of triangulation (IPAL52557).
360 const int maxNbFaces = 4000;
362 for ( TopExp_Explorer f( aShape, TopAbs_FACE ); f.More() && nbFaces < maxNbFaces; f.Next() )
364 bool isPrecise = false;
365 if ( nbFaces < maxNbFaces )
367 GEOMUtils::PreciseBoundingBox( aShape, Box );
375 BRepBndLib::Add( aShape, Box );
378 return sqrt( Box.SquareExtent() );
383 //=======================================================================
385 * \brief Return diagonal size of bounding box of shape to mesh
387 //=======================================================================
389 double SMESH_Mesh::GetShapeDiagonalSize() const
391 if ( _shapeDiagonal == 0. && _isShapeToMesh )
392 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
394 return _shapeDiagonal;
397 //================================================================================
399 * \brief Load mesh from study file
401 //================================================================================
403 void SMESH_Mesh::Load()
409 //=======================================================================
411 * \brief Remove all nodes and elements
413 //=======================================================================
415 void SMESH_Mesh::Clear()
417 if ( HasShapeToMesh() ) // remove all nodes and elements
420 _myMeshDS->ClearMesh();
422 // update compute state of submeshes
423 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
425 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
426 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
427 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
428 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
431 else // remove only nodes/elements computed by algorithms
433 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
435 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
436 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
437 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
438 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
441 GetMeshDS()->Modified();
445 //=======================================================================
447 * \brief Remove all nodes and elements of indicated shape
449 //=======================================================================
451 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
453 // clear sub-meshes; get ready to re-compute as a side-effect
454 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
456 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
457 /*complexShapeFirst=*/false);
458 while ( smIt->more() )
461 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
462 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
463 // all other shapes depends on vertices so they are already cleaned
464 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
465 // to recompute even if failed
466 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
471 //=======================================================================
472 //function : UNVToMesh
474 //=======================================================================
476 int SMESH_Mesh::UNVToMesh(const char* theFileName)
478 if ( _isShapeToMesh )
479 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
480 _isShapeToMesh = false;
482 DriverUNV_R_SMDS_Mesh myReader;
483 myReader.SetMesh(_myMeshDS);
484 myReader.SetFile(theFileName);
485 myReader.SetMeshId(-1);
488 if ( SMDS_MeshGroup* aGroup = (SMDS_MeshGroup*) myReader.GetGroup() )
490 TGroupNamesMap aGroupNames = myReader.GetGroupNamesMap();
491 aGroup->InitSubGroupsIterator();
492 while (aGroup->MoreSubGroups())
494 SMDS_MeshGroup* aSubGroup = (SMDS_MeshGroup*) aGroup->NextSubGroup();
495 string aName = aGroupNames[aSubGroup];
497 if ( SMESH_Group* aSMESHGroup = AddGroup( aSubGroup->GetType(), aName.c_str(), aId ))
499 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aSMESHGroup->GetGroupDS() );
501 aGroupDS->SetStoreName(aName.c_str());
502 aSubGroup->InitIterator();
503 const SMDS_MeshElement* aElement = 0;
504 while ( aSubGroup->More() )
505 if (( aElement = aSubGroup->Next() ))
506 aGroupDS->SMDSGroup().Add( aElement );
509 aGroupDS->SetType( aElement->GetType() );
517 //=======================================================================
518 //function : MEDToMesh
520 //=======================================================================
522 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
524 if ( _isShapeToMesh )
525 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
526 _isShapeToMesh = false;
528 DriverMED_R_SMESHDS_Mesh myReader;
529 myReader.SetMesh(_myMeshDS);
530 myReader.SetMeshId(-1);
531 myReader.SetFile(theFileName);
532 myReader.SetMeshName(theMeshName);
533 Driver_Mesh::Status status = myReader.Perform();
535 SMESH_ComputeErrorPtr er = myReader.GetError();
536 if ( er && !er->IsOK() ) cout << er->myComment << endl;
539 // Reading groups (sub-meshes are out of scope of MED import functionality)
540 list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
542 list<TNameAndType>::iterator name_type = aGroupNames.begin();
543 for ( ; name_type != aGroupNames.end(); name_type++ )
545 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
547 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
549 aGroupDS->SetStoreName( name_type->first.c_str() );
550 myReader.GetGroup( aGroupDS );
557 //=======================================================================
558 //function : STLToMesh
560 //=======================================================================
562 int SMESH_Mesh::STLToMesh(const char* theFileName)
565 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
566 _isShapeToMesh = false;
568 DriverSTL_R_SMDS_Mesh myReader;
569 myReader.SetMesh(_myMeshDS);
570 myReader.SetFile(theFileName);
571 myReader.SetMeshId(-1);
577 //================================================================================
579 * \brief Reads the given mesh from the CGNS file
580 * \param theFileName - name of the file
581 * \retval int - Driver_Mesh::Status
583 //================================================================================
585 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
586 const int theMeshIndex,
587 std::string& theMeshName)
589 int res = Driver_Mesh::DRS_FAIL;
592 DriverCGNS_Read myReader;
593 myReader.SetMesh(_myMeshDS);
594 myReader.SetFile(theFileName);
595 myReader.SetMeshId(theMeshIndex);
596 res = myReader.Perform();
597 theMeshName = myReader.GetMeshName();
606 //================================================================================
608 * \brief Fill its data by reading a GMF file
610 //================================================================================
612 SMESH_ComputeErrorPtr SMESH_Mesh::GMFToMesh(const char* theFileName,
613 bool theMakeRequiredGroups)
615 DriverGMF_Read myReader;
616 myReader.SetMesh(_myMeshDS);
617 myReader.SetFile(theFileName);
618 myReader.SetMakeRequiredGroups( theMakeRequiredGroups );
620 //theMeshName = myReader.GetMeshName();
625 return myReader.GetError();
628 //=============================================================================
632 //=============================================================================
634 SMESH_Hypothesis::Hypothesis_Status
635 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
637 std::string* anError ) throw(SALOME_Exception)
639 Unexpect aCatch(SalomeException);
640 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
645 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
646 if ( !subMesh || !subMesh->GetId())
647 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
649 SMESH_Hypothesis *anHyp = GetHypothesis( anHypId );
651 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
653 bool isGlobalHyp = IsMainShape( aSubShape );
655 // NotConformAllowed can be only global
658 // NOTE: this is not a correct way to check a name of hypothesis,
659 // there should be an attribute of hypothesis saying that it can/can't
661 string hypName = anHyp->GetName();
662 if ( hypName == "NotConformAllowed" )
664 if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
665 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
671 bool isAlgo = ( anHyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
672 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
674 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
676 if ( anError && SMESH_Hypothesis::IsStatusFatal(ret) && subMesh->GetComputeError() )
677 *anError = subMesh->GetComputeError()->myComment;
680 if ( !SMESH_Hypothesis::IsStatusFatal(ret) &&
681 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
683 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
685 SMESH_Hypothesis::Hypothesis_Status ret2 =
686 subMesh->SubMeshesAlgoStateEngine(event, anHyp, /*exitOnFatal=*/true);
690 if ( SMESH_Hypothesis::IsStatusFatal( ret ))
692 if ( anError && subMesh->GetComputeError() )
693 *anError = subMesh->GetComputeError()->myComment;
695 event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
696 subMesh->AlgoStateEngine(event, anHyp);
700 // check concurent hypotheses on ancestors
701 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
703 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
704 while ( smIt->more() ) {
705 SMESH_subMesh* sm = smIt->next();
706 if ( sm->IsApplicableHypotesis( anHyp )) {
707 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
716 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
717 GetMeshDS()->Modified();
719 if(MYDEBUG) subMesh->DumpAlgoState(true);
720 if(MYDEBUG) SCRUTE(ret);
724 //=============================================================================
728 //=============================================================================
730 SMESH_Hypothesis::Hypothesis_Status
731 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
732 int anHypId) throw( SALOME_Exception )
734 Unexpect aCatch(SalomeException);
735 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
737 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
738 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
739 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
741 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
742 if(MYDEBUG) { SCRUTE(anHyp->GetType()); }
746 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
747 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
749 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
751 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
753 // there may appear concurrent hyps that were covered by the removed hyp
754 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
755 subMesh->IsApplicableHypotesis( anHyp ) &&
756 subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
757 ret = SMESH_Hypothesis::HYP_CONCURENT;
760 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
761 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
763 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
765 SMESH_Hypothesis::Hypothesis_Status ret2 =
766 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
767 if (ret2 > ret) // more severe
770 // check concurent hypotheses on ancestors
771 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
773 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
774 while ( smIt->more() ) {
775 SMESH_subMesh* sm = smIt->next();
776 if ( sm->IsApplicableHypotesis( anHyp )) {
777 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
787 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
788 GetMeshDS()->Modified();
790 if(MYDEBUG) subMesh->DumpAlgoState(true);
791 if(MYDEBUG) SCRUTE(ret);
795 //=============================================================================
799 //=============================================================================
801 const list<const SMESHDS_Hypothesis*>&
802 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
803 throw(SALOME_Exception)
805 return _myMeshDS->GetHypothesis(aSubShape);
808 //=======================================================================
810 * \brief Return the hypothesis assigned to the shape
811 * \param aSubShape - the shape to check
812 * \param aFilter - the hypothesis filter
813 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
814 * \param assignedTo - to return the shape the found hypo is assigned to
815 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
817 //=======================================================================
819 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
820 const SMESH_HypoFilter& aFilter,
821 const bool andAncestors,
822 TopoDS_Shape* assignedTo) const
824 return GetHypothesis( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
825 aFilter, andAncestors, assignedTo );
828 //=======================================================================
830 * \brief Return the hypothesis assigned to the shape of a sub-mesh
831 * \param aSubMesh - the sub-mesh to check
832 * \param aFilter - the hypothesis filter
833 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
834 * \param assignedTo - to return the shape the found hypo is assigned to
835 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
837 //=======================================================================
839 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const SMESH_subMesh * aSubMesh,
840 const SMESH_HypoFilter& aFilter,
841 const bool andAncestors,
842 TopoDS_Shape* assignedTo) const
844 if ( !aSubMesh ) return 0;
847 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
848 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
849 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
850 for ( ; hyp != hypList.end(); hyp++ ) {
851 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
852 if ( aFilter.IsOk( h, aSubShape)) {
853 if ( assignedTo ) *assignedTo = aSubShape;
860 // user sorted submeshes of ancestors, according to stored submesh priority
861 std::vector< SMESH_subMesh * > & ancestors =
862 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
863 SortByMeshOrder( ancestors );
865 vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
866 for ( ; smIt != ancestors.end(); smIt++ )
868 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
869 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
870 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
871 for ( ; hyp != hypList.end(); hyp++ ) {
872 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
873 if (aFilter.IsOk( h, curSh )) {
874 if ( assignedTo ) *assignedTo = curSh;
883 //================================================================================
885 * \brief Return hypotheses assigned to the shape
886 * \param aSubShape - the shape to check
887 * \param aFilter - the hypothesis filter
888 * \param aHypList - the list of the found hypotheses
889 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
890 * \retval int - number of unique hypos in aHypList
892 //================================================================================
894 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
895 const SMESH_HypoFilter& aFilter,
896 list <const SMESHDS_Hypothesis * >& aHypList,
897 const bool andAncestors,
898 list< TopoDS_Shape > * assignedTo/*=0*/) const
900 return GetHypotheses( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
901 aFilter, aHypList, andAncestors, assignedTo );
904 //================================================================================
906 * \brief Return hypotheses assigned to the shape of a sub-mesh
907 * \param aSubShape - the sub-mesh to check
908 * \param aFilter - the hypothesis filter
909 * \param aHypList - the list of the found hypotheses
910 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
911 * \retval int - number of unique hypos in aHypList
913 //================================================================================
915 int SMESH_Mesh::GetHypotheses(const SMESH_subMesh * aSubMesh,
916 const SMESH_HypoFilter& aFilter,
917 list <const SMESHDS_Hypothesis * >& aHypList,
918 const bool andAncestors,
919 list< TopoDS_Shape > * assignedTo/*=0*/) const
921 if ( !aSubMesh ) return 0;
923 set<string> hypTypes; // to exclude same type hypos from the result list
926 // only one main hypothesis is allowed
927 bool mainHypFound = false;
930 list<const SMESHDS_Hypothesis*>::const_iterator hyp;
931 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
932 if ( hypTypes.insert( (*hyp)->GetName() ).second )
934 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
938 // get hypos from aSubShape
940 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
941 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
942 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
944 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
945 if (( aFilter.IsOk( h, aSubShape )) &&
946 ( h->IsAuxiliary() || !mainHypFound ) &&
947 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
949 aHypList.push_back( *hyp );
951 if ( !h->IsAuxiliary() )
953 if ( assignedTo ) assignedTo->push_back( aSubShape );
958 // get hypos from ancestors of aSubShape
961 // user sorted submeshes of ancestors, according to stored submesh priority
962 std::vector< SMESH_subMesh * > & ancestors =
963 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
964 SortByMeshOrder( ancestors );
966 vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
967 for ( ; smIt != ancestors.end(); smIt++ )
969 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
970 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
971 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
973 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
974 if (( aFilter.IsOk( h, curSh )) &&
975 ( h->IsAuxiliary() || !mainHypFound ) &&
976 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
978 aHypList.push_back( *hyp );
980 if ( !h->IsAuxiliary() )
982 if ( assignedTo ) assignedTo->push_back( curSh );
990 //================================================================================
992 * \brief Return a hypothesis by its ID
994 //================================================================================
996 SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const int anHypId) const
998 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
999 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
1002 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
1006 //=============================================================================
1010 //=============================================================================
1012 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
1014 Unexpect aCatch(SalomeException);
1015 return _myMeshDS->GetScript()->GetCommands();
1018 //=============================================================================
1022 //=============================================================================
1023 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
1025 Unexpect aCatch(SalomeException);
1026 _myMeshDS->GetScript()->Clear();
1029 //=============================================================================
1031 * Get or Create the SMESH_subMesh object implementation
1033 //=============================================================================
1035 SMESH_subMesh * SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
1036 throw(SALOME_Exception)
1038 int index = _myMeshDS->ShapeToIndex(aSubShape);
1039 if ( !index && aSubShape.IsNull() )
1042 // for submeshes on GEOM Group
1043 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND )
1045 TopoDS_Iterator it( aSubShape );
1048 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
1049 // fill map of Ancestors
1050 while ( _nbSubShapes < index )
1051 fillAncestorsMap( _myMeshDS->IndexToShape( ++_nbSubShapes ));
1055 // return NULL; // neither sub-shape nor a group
1057 SMESH_subMesh* aSubMesh = _subMeshHolder->Get( index );
1060 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
1061 _subMeshHolder->Add( index, aSubMesh );
1063 // include non-computable sub-meshes in SMESH_subMesh::_ancestors of sub-submeshes
1064 switch ( aSubShape.ShapeType() ) {
1065 case TopAbs_COMPOUND:
1068 for ( TopoDS_Iterator subIt( aSubShape ); subIt.More(); subIt.Next() )
1070 SMESH_subMesh* sm = GetSubMesh( subIt.Value() );
1071 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*inclideSelf=*/true);
1072 while ( smIt->more() )
1073 smIt->next()->ClearAncestors();
1081 //=============================================================================
1083 * Get the SMESH_subMesh object implementation. Dont create it, return null
1084 * if it does not exist.
1086 //=============================================================================
1088 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
1089 throw(SALOME_Exception)
1091 int index = _myMeshDS->ShapeToIndex(aSubShape);
1092 return GetSubMeshContaining( index );
1095 //=============================================================================
1097 * Get the SMESH_subMesh object implementation. Dont create it, return null
1098 * if it does not exist.
1100 //=============================================================================
1102 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
1103 throw(SALOME_Exception)
1105 SMESH_subMesh *aSubMesh = _subMeshHolder->Get( aShapeID );
1110 //================================================================================
1112 * \brief Return sub-meshes of groups containing the given sub-shape
1114 //================================================================================
1116 list<SMESH_subMesh*>
1117 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
1118 throw(SALOME_Exception)
1120 list<SMESH_subMesh*> found;
1122 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
1126 // sub-meshes of groups have max IDs, so search from the map end
1127 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator( /*reverse=*/true ) );
1128 while ( smIt->more() ) {
1129 SMESH_subMesh* sm = smIt->next();
1130 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
1131 if ( ds && ds->IsComplexSubmesh() ) {
1132 if ( SMESH_MesherHelper::IsSubShape( aSubShape, sm->GetSubShape() ))
1134 found.push_back( sm );
1138 break; // the rest sub-meshes are not those of groups
1142 if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
1144 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1145 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1147 TopoDS_Iterator it( mainSM->GetSubShape() );
1148 if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
1149 SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
1150 found.push_back( mainSM );
1153 else // issue 0023068
1155 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1156 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1157 found.push_back( mainSM );
1161 //=======================================================================
1162 //function : IsUsedHypothesis
1163 //purpose : Return True if anHyp is used to mesh aSubShape
1164 //=======================================================================
1166 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
1167 const SMESH_subMesh* aSubMesh)
1169 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
1171 // check if anHyp can be used to mesh aSubMesh
1172 if ( !aSubMesh || !aSubMesh->IsApplicableHypotesis( hyp ))
1175 SMESH_Algo *algo = aSubMesh->GetAlgo();
1178 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1179 return ( anHyp == algo );
1181 // algorithm parameter
1184 // look trough hypotheses used by algo
1185 const SMESH_HypoFilter* hypoKind;
1186 if (( hypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() ))) {
1187 list <const SMESHDS_Hypothesis * > usedHyps;
1188 if ( GetHypotheses( aSubMesh, *hypoKind, usedHyps, true ))
1189 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1196 //=======================================================================
1197 //function : NotifySubMeshesHypothesisModification
1198 //purpose : Say all submeshes using theChangedHyp that it has been modified
1199 //=======================================================================
1201 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1203 Unexpect aCatch(SalomeException);
1205 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1209 _callUp->HypothesisModified();
1212 const SMESH_HypoFilter* compatibleHypoKind;
1213 list <const SMESHDS_Hypothesis * > usedHyps;
1214 vector< SMESH_subMesh* > smToNotify;
1215 bool allMeshedEdgesNotified = true;
1217 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1218 while ( smIt->more() )
1220 SMESH_subMesh* aSubMesh = smIt->next();
1221 bool toNotify = false;
1223 // if aSubMesh meshing depends on hyp,
1224 // we call aSubMesh->AlgoStateEngine( MODIF_HYP, hyp ) that causes either
1225 // 1) clearing already computed aSubMesh or
1226 // 2) changing algo_state from MISSING_HYP to HYP_OK when parameters of hyp becomes valid,
1227 // other possible changes are not interesting. (IPAL0052457 - assigning hyp performance pb)
1228 if ( aSubMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK ||
1229 aSubMesh->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE ||
1230 aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP ||
1231 hyp->DataDependOnParams() )
1233 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1235 if (( aSubMesh->IsApplicableHypotesis( hyp )) &&
1236 ( algo = aSubMesh->GetAlgo() ) &&
1237 ( compatibleHypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() )) &&
1238 ( compatibleHypoKind->IsOk( hyp, aSubShape )))
1240 // check if hyp is used by algo
1242 toNotify = ( GetHypotheses( aSubMesh, *compatibleHypoKind, usedHyps, true ) &&
1243 std::find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() );
1248 smToNotify.push_back( aSubMesh );
1249 if ( aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP )
1250 allMeshedEdgesNotified = false; // update of algo state needed, not mesh clearing
1254 if ( !aSubMesh->IsEmpty() &&
1255 aSubMesh->GetSubShape().ShapeType() == TopAbs_EDGE )
1256 allMeshedEdgesNotified = false;
1259 if ( smToNotify.empty() )
1262 // if all meshed EDGEs will be notified then the notification is equivalent
1263 // to the whole mesh clearing, which is usually faster
1264 if ( allMeshedEdgesNotified && NbNodes() > 0 )
1270 // notify in reverse order to avoid filling the pool of IDs
1271 for ( int i = smToNotify.size()-1; i >= 0; --i )
1272 smToNotify[i]->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1273 const_cast< SMESH_Hypothesis*>( hyp ));
1275 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1276 GetMeshDS()->Modified();
1279 //=============================================================================
1281 * Auto color functionality
1283 //=============================================================================
1284 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1286 Unexpect aCatch(SalomeException);
1287 _isAutoColor = theAutoColor;
1290 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1292 Unexpect aCatch(SalomeException);
1293 return _isAutoColor;
1296 //=======================================================================
1297 //function : SetIsModified
1298 //purpose : Set the flag meaning that the mesh has been edited "manually"
1299 //=======================================================================
1301 void SMESH_Mesh::SetIsModified(bool isModified)
1303 _isModified = isModified;
1306 // check if mesh becomes empty as result of modification
1307 HasModificationsToDiscard();
1310 //=======================================================================
1311 //function : HasModificationsToDiscard
1312 //purpose : Return true if the mesh has been edited since a total re-compute
1313 // and those modifications may prevent successful partial re-compute.
1314 // As a side effect reset _isModified flag if mesh is empty
1316 //=======================================================================
1318 bool SMESH_Mesh::HasModificationsToDiscard() const
1320 if ( ! _isModified )
1323 // return true if the next Compute() will be partial and
1324 // existing but changed elements may prevent successful re-compute
1325 bool hasComputed = false, hasNotComputed = false;
1326 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1327 while ( smIt->more() )
1329 const SMESH_subMesh* aSubMesh = smIt->next();
1330 switch ( aSubMesh->GetSubShape().ShapeType() )
1335 if ( aSubMesh->IsMeshComputed() )
1338 hasNotComputed = true;
1339 if ( hasComputed && hasNotComputed)
1345 if ( NbNodes() < 1 )
1346 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1351 //================================================================================
1353 * \brief Check if any groups of the same type have equal names
1355 //================================================================================
1357 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1359 // Corrected for Mantis issue 0020028
1360 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1361 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1363 SMESH_Group* aGroup = it->second;
1364 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1365 string aGroupName = aGroup->GetName();
1366 aGroupName.resize( MAX_MED_GROUP_NAME_LENGTH );
1367 if ( !aGroupNames[aType].insert(aGroupName).second )
1374 //================================================================================
1376 * \brief Export the mesh to a med file
1377 * \param [in] file - name of the MED file
1378 * \param [in] theMeshName - name of this mesh
1379 * \param [in] theAutoGroups - boolean parameter for creating/not creating
1380 * the groups Group_On_All_Nodes, Group_On_All_Faces, ... ;
1381 * the typical use is auto_groups=false.
1382 * \param [in] theVersion - defines the version of format of MED file, that will be created
1383 * \param [in] meshPart - mesh data to export
1384 * \param [in] theAutoDimension - if \c true, a space dimension of a MED mesh can be either
1385 * - 1D if all mesh nodes lie on OX coordinate axis, or
1386 * - 2D if all mesh nodes lie on XOY coordinate plane, or
1387 * - 3D in the rest cases.
1388 * If \a theAutoDimension is \c false, the space dimension is always 3.
1389 * \param [in] theAddODOnVertices - to create 0D elements on all vertices
1390 * \param [in] theAllElemsToGroup - to make every element to belong to any group (PAL23413)
1391 * \return int - mesh index in the file
1393 //================================================================================
1395 void SMESH_Mesh::ExportMED(const char * file,
1396 const char* theMeshName,
1399 const SMESHDS_Mesh* meshPart,
1400 bool theAutoDimension,
1401 bool theAddODOnVertices,
1402 bool theAllElemsToGroup)
1403 throw(SALOME_Exception)
1407 DriverMED_W_SMESHDS_Mesh myWriter;
1408 myWriter.SetFile ( file, MED::EVersion(theVersion) );
1409 myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1410 myWriter.SetAutoDimension( theAutoDimension );
1411 myWriter.AddODOnVertices ( theAddODOnVertices );
1413 myWriter.SetMeshId ( _id );
1415 myWriter.SetMeshId ( -1 );
1416 myWriter.SetMeshName ( theMeshName );
1419 if ( theAutoGroups ) {
1420 myWriter.AddGroupOfNodes();
1421 myWriter.AddGroupOfEdges();
1422 myWriter.AddGroupOfFaces();
1423 myWriter.AddGroupOfVolumes();
1425 if ( theAllElemsToGroup )
1426 myWriter.AddAllToGroup();
1428 // Pass groups to writer. Provide unique group names.
1429 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1432 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1434 int maxNbIter = 10000; // to guarantee cycle finish
1435 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1436 SMESH_Group* aGroup = it->second;
1437 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1439 SMDSAbs_ElementType aType = aGroupDS->GetType();
1440 string aGroupName0 = aGroup->GetName();
1441 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1442 string aGroupName = aGroupName0;
1443 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1444 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1445 aGroupName = aString;
1446 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1448 aGroupDS->SetStoreName( aGroupName.c_str() );
1449 myWriter.AddGroup( aGroupDS );
1456 SMESH_CATCH( SMESH::throwSalomeEx );
1459 //================================================================================
1461 * \brief Export the mesh to a SAUV file
1463 //================================================================================
1465 void SMESH_Mesh::ExportSAUV(const char *file,
1466 const char* theMeshName,
1468 throw(SALOME_Exception)
1470 std::string medfilename(file);
1471 medfilename += ".med";
1474 cmd = "%PYTHONBIN% ";
1479 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1481 system(cmd.c_str());
1482 ExportMED(medfilename.c_str(), theMeshName, theAutoGroups, /*theVersion=*/1,
1483 /*meshPart=*/NULL, /*theAutoDimension=*/false, /*theAddODOnVertices=*/false,
1484 /*theAllElemsToGroup=*/true ); // theAllElemsToGroup is for PAL0023413
1486 cmd = "%PYTHONBIN% ";
1491 cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
1493 system(cmd.c_str());
1495 cmd = "%PYTHONBIN% ";
1500 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1502 system(cmd.c_str());
1505 //================================================================================
1507 * \brief Export the mesh to a DAT file
1509 //================================================================================
1511 void SMESH_Mesh::ExportDAT(const char * file,
1512 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1514 Unexpect aCatch(SalomeException);
1515 DriverDAT_W_SMDS_Mesh myWriter;
1516 myWriter.SetFile( file );
1517 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1518 myWriter.SetMeshId(_id);
1522 //================================================================================
1524 * \brief Export the mesh to an UNV file
1526 //================================================================================
1528 void SMESH_Mesh::ExportUNV(const char * file,
1529 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1531 Unexpect aCatch(SalomeException);
1532 DriverUNV_W_SMDS_Mesh myWriter;
1533 myWriter.SetFile( file );
1534 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1535 myWriter.SetMeshId(_id);
1536 // myWriter.SetGroups(_mapGroup);
1540 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1541 SMESH_Group* aGroup = it->second;
1542 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1544 string aGroupName = aGroup->GetName();
1545 aGroupDS->SetStoreName( aGroupName.c_str() );
1546 myWriter.AddGroup( aGroupDS );
1553 //================================================================================
1555 * \brief Export the mesh to an STL file
1557 //================================================================================
1559 void SMESH_Mesh::ExportSTL(const char * file,
1561 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1563 Unexpect aCatch(SalomeException);
1564 DriverSTL_W_SMDS_Mesh myWriter;
1565 myWriter.SetFile( file );
1566 myWriter.SetIsAscii( isascii );
1567 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1568 myWriter.SetMeshId(_id);
1572 //================================================================================
1574 * \brief Export the mesh to the CGNS file
1576 //================================================================================
1578 void SMESH_Mesh::ExportCGNS(const char * file,
1579 const SMESHDS_Mesh* meshDS,
1580 const char * meshName)
1582 int res = Driver_Mesh::DRS_FAIL;
1584 DriverCGNS_Write myWriter;
1585 myWriter.SetFile( file );
1586 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1587 myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1588 if ( meshName && meshName[0] )
1589 myWriter.SetMeshName( meshName );
1590 res = myWriter.Perform();
1592 if ( res != Driver_Mesh::DRS_OK )
1593 throw SALOME_Exception("Export failed");
1596 //================================================================================
1598 * \brief Export the mesh to a GMF file
1600 //================================================================================
1602 void SMESH_Mesh::ExportGMF(const char * file,
1603 const SMESHDS_Mesh* meshDS,
1604 bool withRequiredGroups)
1606 DriverGMF_Write myWriter;
1607 myWriter.SetFile( file );
1608 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1609 myWriter.SetExportRequiredGroups( withRequiredGroups );
1614 //================================================================================
1616 * \brief Return a ratio of "compute cost" of computed sub-meshes to the whole
1619 //================================================================================
1621 double SMESH_Mesh::GetComputeProgress() const
1623 double totalCost = 1e-100, computedCost = 0;
1624 const SMESH_subMesh* curSM = _gen->GetCurrentSubMesh();
1626 // get progress of a current algo
1627 TColStd_MapOfInteger currentSubIds;
1629 if ( SMESH_Algo* algo = curSM->GetAlgo() )
1631 int algoNotDoneCost = 0, algoDoneCost = 0;
1632 const std::vector<SMESH_subMesh*>& smToCompute = algo->SubMeshesToCompute();
1633 for ( size_t i = 0; i < smToCompute.size(); ++i )
1635 if ( smToCompute[i]->IsEmpty() || smToCompute.size() == 1 )
1636 algoNotDoneCost += smToCompute[i]->GetComputeCost();
1638 algoDoneCost += smToCompute[i]->GetComputeCost();
1639 currentSubIds.Add( smToCompute[i]->GetId() );
1645 rate = algo->GetProgress();
1649 cerr << "Exception in " << algo->GetName() << "::GetProgress()" << endl;
1652 if ( 0. < rate && rate < 1.001 )
1654 computedCost += rate * ( algoDoneCost + algoNotDoneCost );
1658 rate = algo->GetProgressByTic();
1659 computedCost += algoDoneCost + rate * algoNotDoneCost;
1661 // cout << "rate: "<<rate << " algoNotDoneCost: " << algoNotDoneCost << endl;
1664 // get cost of already treated sub-meshes
1665 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1667 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1668 while ( smIt->more() )
1670 const SMESH_subMesh* sm = smIt->next();
1671 const int smCost = sm->GetComputeCost();
1672 totalCost += smCost;
1673 if ( !currentSubIds.Contains( sm->GetId() ) )
1675 if (( !sm->IsEmpty() ) ||
1676 ( sm->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
1677 !sm->DependsOn( curSM ) ))
1678 computedCost += smCost;
1682 // cout << "Total: " << totalCost
1683 // << " computed: " << computedCost << " progress: " << computedCost / totalCost
1684 // << " nbElems: " << GetMeshDS()->GetMeshInfo().NbElements() << endl;
1685 return computedCost / totalCost;
1688 //================================================================================
1690 * \brief Return number of nodes in the mesh
1692 //================================================================================
1694 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1696 Unexpect aCatch(SalomeException);
1697 return _myMeshDS->NbNodes();
1700 //================================================================================
1702 * \brief Return number of edges of given order in the mesh
1704 //================================================================================
1706 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1708 Unexpect aCatch(SalomeException);
1709 return _myMeshDS->GetMeshInfo().Nb0DElements();
1712 //================================================================================
1714 * \brief Return number of edges of given order in the mesh
1716 //================================================================================
1718 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1720 Unexpect aCatch(SalomeException);
1721 return _myMeshDS->GetMeshInfo().NbEdges(order);
1724 //================================================================================
1726 * \brief Return number of faces of given order in the mesh
1728 //================================================================================
1730 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1732 Unexpect aCatch(SalomeException);
1733 return _myMeshDS->GetMeshInfo().NbFaces(order);
1736 //================================================================================
1738 * \brief Return the number of faces in the mesh
1740 //================================================================================
1742 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1744 Unexpect aCatch(SalomeException);
1745 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1748 //================================================================================
1750 * \brief Return number of biquadratic triangles in the mesh
1752 //================================================================================
1754 int SMESH_Mesh::NbBiQuadTriangles() const throw(SALOME_Exception)
1756 Unexpect aCatch(SalomeException);
1757 return _myMeshDS->GetMeshInfo().NbBiQuadTriangles();
1760 //================================================================================
1762 * \brief Return the number nodes faces in the mesh
1764 //================================================================================
1766 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1768 Unexpect aCatch(SalomeException);
1769 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1772 //================================================================================
1774 * \brief Return number of biquadratic quadrangles in the mesh
1776 //================================================================================
1778 int SMESH_Mesh::NbBiQuadQuadrangles() const throw(SALOME_Exception)
1780 Unexpect aCatch(SalomeException);
1781 return _myMeshDS->GetMeshInfo().NbBiQuadQuadrangles();
1784 //================================================================================
1786 * \brief Return the number of polygonal faces in the mesh
1788 //================================================================================
1790 int SMESH_Mesh::NbPolygons(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1792 Unexpect aCatch(SalomeException);
1793 return _myMeshDS->GetMeshInfo().NbPolygons(order);
1796 //================================================================================
1798 * \brief Return number of volumes of given order in the mesh
1800 //================================================================================
1802 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1804 Unexpect aCatch(SalomeException);
1805 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1808 //================================================================================
1810 * \brief Return number of tetrahedrons of given order in the mesh
1812 //================================================================================
1814 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1816 Unexpect aCatch(SalomeException);
1817 return _myMeshDS->GetMeshInfo().NbTetras(order);
1820 //================================================================================
1822 * \brief Return number of hexahedrons of given order in the mesh
1824 //================================================================================
1826 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1828 Unexpect aCatch(SalomeException);
1829 return _myMeshDS->GetMeshInfo().NbHexas(order);
1832 //================================================================================
1834 * \brief Return number of triquadratic hexahedrons in the mesh
1836 //================================================================================
1838 int SMESH_Mesh::NbTriQuadraticHexas() const throw(SALOME_Exception)
1840 Unexpect aCatch(SalomeException);
1841 return _myMeshDS->GetMeshInfo().NbTriQuadHexas();
1844 //================================================================================
1846 * \brief Return number of pyramids of given order in the mesh
1848 //================================================================================
1850 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1852 Unexpect aCatch(SalomeException);
1853 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1856 //================================================================================
1858 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1860 //================================================================================
1862 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1864 Unexpect aCatch(SalomeException);
1865 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1868 //================================================================================
1870 * \brief Return number of hexagonal prisms in the mesh
1872 //================================================================================
1874 int SMESH_Mesh::NbHexagonalPrisms() const throw(SALOME_Exception)
1876 Unexpect aCatch(SalomeException);
1877 return _myMeshDS->GetMeshInfo().NbHexPrisms();
1880 //================================================================================
1882 * \brief Return number of polyhedrons in the mesh
1884 //================================================================================
1886 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1888 Unexpect aCatch(SalomeException);
1889 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1892 //================================================================================
1894 * \brief Return number of ball elements in the mesh
1896 //================================================================================
1898 int SMESH_Mesh::NbBalls() const throw(SALOME_Exception)
1900 Unexpect aCatch(SalomeException);
1901 return _myMeshDS->GetMeshInfo().NbBalls();
1904 //================================================================================
1906 * \brief Return number of submeshes in the mesh
1908 //================================================================================
1910 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1912 Unexpect aCatch(SalomeException);
1913 return _myMeshDS->NbSubMesh();
1916 //================================================================================
1918 * \brief Returns number of meshes in the Study, that is supposed to be
1919 * equal to SMESHDS_Document::NbMeshes()
1921 //================================================================================
1923 int SMESH_Mesh::NbMeshes() const // nb meshes in the Study
1925 return _myDocument->NbMeshes();
1928 //=======================================================================
1929 //function : IsNotConformAllowed
1930 //purpose : check if a hypothesis alowing notconform mesh is present
1931 //=======================================================================
1933 bool SMESH_Mesh::IsNotConformAllowed() const
1935 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1937 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1938 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
1941 //=======================================================================
1942 //function : IsMainShape
1944 //=======================================================================
1946 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1948 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1951 //=============================================================================
1955 //=============================================================================
1957 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1958 const char* theName,
1960 const TopoDS_Shape& theShape,
1961 const SMESH_PredicatePtr& thePredicate)
1963 if (_mapGroup.count(_groupId))
1966 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape, thePredicate);
1967 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1968 _mapGroup[_groupId++] = aGroup;
1972 //================================================================================
1974 * \brief Creates a group based on an existing SMESHDS group. Group ID should be unique
1976 //================================================================================
1978 SMESH_Group* SMESH_Mesh::AddGroup (SMESHDS_GroupBase* groupDS) throw(SALOME_Exception)
1981 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup(): NULL SMESHDS_GroupBase"));
1983 map <int, SMESH_Group*>::iterator i_g = _mapGroup.find( groupDS->GetID() );
1984 if ( i_g != _mapGroup.end() && i_g->second )
1986 if ( i_g->second->GetGroupDS() == groupDS )
1989 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup() wrong ID of SMESHDS_GroupBase"));
1991 SMESH_Group* aGroup = new SMESH_Group (groupDS);
1992 _mapGroup[ groupDS->GetID() ] = aGroup;
1993 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1995 _groupId = 1 + _mapGroup.rbegin()->first;
2001 //================================================================================
2003 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
2004 * \retval bool - true if new SMESH_Groups have been created
2007 //================================================================================
2009 bool SMESH_Mesh::SynchronizeGroups()
2011 const size_t nbGroups = _mapGroup.size();
2012 const set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
2013 set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
2014 for ( ; gIt != groups.end(); ++gIt )
2016 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
2017 _groupId = groupDS->GetID();
2018 if ( !_mapGroup.count( _groupId ))
2019 _mapGroup[_groupId] = new SMESH_Group( groupDS );
2021 if ( !_mapGroup.empty() )
2022 _groupId = _mapGroup.rbegin()->first + 1;
2024 return nbGroups < _mapGroup.size();
2027 //================================================================================
2029 * \brief Return iterator on all existing groups
2031 //================================================================================
2033 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
2035 typedef map <int, SMESH_Group *> TMap;
2036 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
2039 //=============================================================================
2041 * \brief Return a group by ID
2043 //=============================================================================
2045 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
2047 if (_mapGroup.find(theGroupID) == _mapGroup.end())
2049 return _mapGroup[theGroupID];
2053 //=============================================================================
2055 * \brief Return IDs of all groups
2057 //=============================================================================
2059 list<int> SMESH_Mesh::GetGroupIds() const
2062 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
2063 anIds.push_back( it->first );
2068 //================================================================================
2070 * \brief Set a caller of methods at level of CORBA API implementation.
2071 * The set upCaller will be deleted by SMESH_Mesh
2073 //================================================================================
2075 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
2077 if ( _callUp ) delete _callUp;
2081 //=============================================================================
2085 //=============================================================================
2087 bool SMESH_Mesh::RemoveGroup( const int theGroupID )
2089 if (_mapGroup.find(theGroupID) == _mapGroup.end())
2091 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
2092 delete _mapGroup[theGroupID];
2093 _mapGroup.erase (theGroupID);
2095 _callUp->RemoveGroup( theGroupID );
2099 //=======================================================================
2100 //function : GetAncestors
2101 //purpose : return list of ancestors of theSubShape in the order
2102 // that lower dimention shapes come first.
2103 //=======================================================================
2105 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
2107 if ( _mapAncestors.Contains( theS ) )
2108 return _mapAncestors.FindFromKey( theS );
2110 static TopTools_ListOfShape emptyList;
2114 //=======================================================================
2116 //purpose : dumps contents of mesh to stream [ debug purposes ]
2117 //=======================================================================
2119 ostream& SMESH_Mesh::Dump(ostream& save)
2122 save << "========================== Dump contents of mesh ==========================" << endl << endl;
2123 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
2124 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
2125 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
2126 save << ++clause << ") Total number of polygons: \t" << NbPolygons() << endl;
2127 save << ++clause << ") Total number of volumes: \t" << NbVolumes() << endl;
2128 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
2129 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
2131 string orderStr = isQuadratic ? "quadratic" : "linear";
2132 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
2134 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
2135 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
2136 if ( NbFaces(order) > 0 ) {
2137 int nb3 = NbTriangles(order);
2138 int nb4 = NbQuadrangles(order);
2139 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
2140 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
2141 if ( nb3 + nb4 != NbFaces(order) ) {
2142 map<int,int> myFaceMap;
2143 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
2144 while( itFaces->more( ) ) {
2145 int nbNodes = itFaces->next()->NbNodes();
2146 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
2147 myFaceMap[ nbNodes ] = 0;
2148 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
2150 save << clause << ".3) Faces in detail: " << endl;
2151 map <int,int>::iterator itF;
2152 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
2153 save << "--> nb nodes: " << itF->first << " - nb elemens:\t" << itF->second << endl;
2156 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
2157 if ( NbVolumes(order) > 0 ) {
2158 int nb8 = NbHexas(order);
2159 int nb4 = NbTetras(order);
2160 int nb5 = NbPyramids(order);
2161 int nb6 = NbPrisms(order);
2162 save << clause << ".1) Number of " << orderStr << " hexahedrons: \t" << nb8 << endl;
2163 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
2164 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
2165 save << clause << ".4) Number of " << orderStr << " pyramids: \t" << nb5 << endl;
2166 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
2167 map<int,int> myVolumesMap;
2168 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
2169 while( itVolumes->more( ) ) {
2170 int nbNodes = itVolumes->next()->NbNodes();
2171 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
2172 myVolumesMap[ nbNodes ] = 0;
2173 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
2175 save << clause << ".5) Volumes in detail: " << endl;
2176 map <int,int>::iterator itV;
2177 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
2178 save << "--> nb nodes: " << itV->first << " - nb elemens:\t" << itV->second << endl;
2183 save << "===========================================================================" << endl;
2187 //=======================================================================
2188 //function : GetElementType
2189 //purpose : Returns type of mesh element with certain id
2190 //=======================================================================
2192 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
2194 return _myMeshDS->GetElementType( id, iselem );
2197 //=============================================================================
2199 * \brief Convert group on geometry into standalone group
2201 //=============================================================================
2203 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
2205 SMESH_Group* aGroup = 0;
2206 map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
2207 if ( itg == _mapGroup.end() )
2210 SMESH_Group* anOldGrp = (*itg).second;
2211 if ( !anOldGrp || !anOldGrp->GetGroupDS() )
2213 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
2215 // create new standalone group
2216 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
2217 _mapGroup[theGroupID] = aGroup;
2219 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
2220 GetMeshDS()->RemoveGroup( anOldGrpDS );
2221 GetMeshDS()->AddGroup( aNewGrpDS );
2223 // add elements (or nodes) into new created group
2224 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
2225 while ( anItr->more() )
2226 aNewGrpDS->Add( (anItr->next())->GetID() );
2229 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
2237 //=============================================================================
2239 * \brief remove submesh order from Mesh
2241 //=============================================================================
2243 void SMESH_Mesh::ClearMeshOrder()
2245 _mySubMeshOrder.clear();
2248 //=============================================================================
2250 * \brief remove submesh order from Mesh
2252 //=============================================================================
2254 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
2256 _mySubMeshOrder = theOrder;
2259 //=============================================================================
2261 * \brief return submesh order if any
2263 //=============================================================================
2265 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
2267 return _mySubMeshOrder;
2270 //=============================================================================
2272 * \brief fill _mapAncestors
2274 //=============================================================================
2276 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
2278 int desType, ancType;
2279 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
2281 // a geom group is added. Insert it into lists of ancestors before
2282 // the first ancestor more complex than group members
2283 TopoDS_Iterator subIt( theShape );
2284 if ( !subIt.More() ) return;
2285 int memberType = subIt.Value().ShapeType();
2286 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
2287 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
2289 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
2290 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
2291 TopTools_ListIteratorOfListOfShape ancIt (ancList);
2292 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
2294 if ( ancIt.More() ) ancList.InsertBefore( theShape, ancIt );
2295 else ancList.Append( theShape );
2298 else // else added for 52457: Addition of hypotheses is 8 time longer than meshing
2300 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
2301 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
2302 TopExp::MapShapesAndAncestors ( theShape,
2303 (TopAbs_ShapeEnum) desType,
2304 (TopAbs_ShapeEnum) ancType,
2307 // visit COMPOUNDs inside a COMPOUND that are not reachable by TopExp_Explorer
2308 if ( theShape.ShapeType() == TopAbs_COMPOUND )
2310 TopoDS_Iterator sIt(theShape);
2311 if ( sIt.More() && sIt.Value().ShapeType() == TopAbs_COMPOUND )
2312 for ( ; sIt.More(); sIt.Next() )
2313 if ( sIt.Value().ShapeType() == TopAbs_COMPOUND )
2314 fillAncestorsMap( sIt.Value() );
2318 //=============================================================================
2320 * \brief sort submeshes according to stored mesh order
2321 * \param theListToSort in out list to be sorted
2322 * \return FALSE if nothing sorted
2324 //=============================================================================
2326 bool SMESH_Mesh::SortByMeshOrder(std::vector<SMESH_subMesh*>& theListToSort) const
2328 if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
2332 vector<SMESH_subMesh*> onlyOrderedList, smVec;
2334 // collect all ordered submeshes in one list as pointers
2335 // and get their positions within theListToSort
2336 typedef vector<SMESH_subMesh*>::iterator TPosInList;
2337 map< int, TPosInList > sortedPos;
2338 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
2339 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2340 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++)
2342 const TListOfInt& listOfId = *listIdsIt;
2343 // convert sm ids to sm's
2345 TListOfInt::const_iterator idIt = listOfId.begin();
2346 for ( ; idIt != listOfId.end(); idIt++ )
2348 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt ))
2350 smVec.push_back( sm );
2351 if ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->IsComplexSubmesh() )
2353 SMESHDS_SubMeshIteratorPtr smdsIt = sm->GetSubMeshDS()->GetSubMeshIterator();
2354 while ( smdsIt->more() )
2356 const SMESHDS_SubMesh* smDS = smdsIt->next();
2357 if (( sm = GetSubMeshContaining( smDS->GetID() )))
2358 smVec.push_back( sm );
2363 // find smVec items in theListToSort
2364 for ( size_t i = 0; i < smVec.size(); ++i )
2366 TPosInList smPos = find( smBeg, smEnd, smVec[i] );
2367 if ( smPos != smEnd ) {
2368 onlyOrderedList.push_back( smVec[i] );
2369 sortedPos[ distance( smBeg, smPos )] = smPos;
2373 if (onlyOrderedList.size() < 2)
2377 vector<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
2378 vector<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
2380 // iterate on ordered sub-meshes and insert them in detected positions
2381 map< int, TPosInList >::iterator i_pos = sortedPos.begin();
2382 for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
2383 *(i_pos->second) = *onlyBIt;
2388 //================================================================================
2390 * \brief Return true if given order of sub-meshes is OK
2392 //================================================================================
2394 bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore,
2395 const SMESH_subMesh* smAfter ) const
2397 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2398 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++)
2400 const TListOfInt& listOfId = *listIdsIt;
2401 int iB = -1, iA = -1, i = 0;
2402 for ( TListOfInt::const_iterator id = listOfId.begin(); id != listOfId.end(); ++id, ++i )
2404 if ( *id == smBefore->GetId() )
2410 else if ( *id == smAfter->GetId() )
2418 return true; // no order imposed to given sub-meshes
2421 //=============================================================================
2423 * \brief sort submeshes according to stored mesh order
2424 * \param theListToSort in out list to be sorted
2425 * \return FALSE if nothing sorted
2427 //=============================================================================
2429 void SMESH_Mesh::getAncestorsSubMeshes (const TopoDS_Shape& theSubShape,
2430 std::vector< SMESH_subMesh* >& theSubMeshes) const
2432 theSubMeshes.clear();
2433 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2434 for (; it.More(); it.Next() )
2435 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2436 theSubMeshes.push_back(sm);
2438 // sort submeshes according to stored mesh order
2439 SortByMeshOrder( theSubMeshes );