1 // Copyright (C) 2007-2011 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.
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 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : SMESH_Mesh.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_Mesh.hxx"
29 #include "SMESH_subMesh.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_Hypothesis.hxx"
32 #include "SMESH_Group.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMESHDS_Group.hxx"
35 #include "SMESHDS_Script.hxx"
36 #include "SMESHDS_GroupOnGeom.hxx"
37 #include "SMESHDS_Document.hxx"
38 #include "SMDS_MeshVolume.hxx"
39 #include "SMDS_SetIterator.hxx"
41 #include "utilities.h"
43 #include "DriverMED_W_SMESHDS_Mesh.h"
44 #include "DriverDAT_W_SMDS_Mesh.h"
45 #include "DriverUNV_W_SMDS_Mesh.h"
46 #include "DriverSTL_W_SMDS_Mesh.h"
48 #include "DriverMED_R_SMESHDS_Mesh.h"
49 #include "DriverUNV_R_SMDS_Mesh.h"
50 #include "DriverSTL_R_SMDS_Mesh.h"
52 #include "DriverCGNS_Read.hxx"
53 #include "DriverCGNS_Write.hxx"
56 #undef _Precision_HeaderFile
57 #include <BRepBndLib.hxx>
58 #include <BRepPrimAPI_MakeBox.hxx>
59 #include <Bnd_Box.hxx>
61 #include <TopExp_Explorer.hxx>
62 #include <TopTools_ListIteratorOfListOfShape.hxx>
63 #include <TopTools_ListOfShape.hxx>
64 #include <TopTools_MapOfShape.hxx>
65 #include <TopoDS_Iterator.hxx>
67 #include "Utils_ExceptHandlers.hxx"
71 // maximum stored group name length in MED file
72 #define MAX_MED_GROUP_NAME_LENGTH 80
75 static int MYDEBUG = 0;
77 static int MYDEBUG = 0;
80 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
82 typedef SMESH_HypoFilter THypType;
84 //=============================================================================
88 //=============================================================================
90 SMESH_Mesh::SMESH_Mesh(int theLocalId,
93 bool theIsEmbeddedMode,
94 SMESHDS_Document* theDocument):
95 _groupId( 0 ), _nbSubShapes( 0 )
97 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
99 _studyId = theStudyId;
101 _myDocument = theDocument;
102 _idDoc = theDocument->NewMesh(theIsEmbeddedMode);
103 _myMeshDS = theDocument->GetMesh(_idDoc);
104 _isShapeToMesh = false;
105 _isAutoColor = false;
107 _shapeDiagonal = 0.0;
109 _myMeshDS->ShapeToMesh( PseudoShape() );
112 //================================================================================
114 * \brief Constructor of SMESH_Mesh being a base of some descendant class
116 //================================================================================
118 SMESH_Mesh::SMESH_Mesh():
119 _groupId( 0 ), _nbSubShapes( 0 )
122 _isShapeToMesh = false;
123 _isAutoColor = false;
125 _shapeDiagonal = 0.0;
129 //=============================================================================
133 //=============================================================================
135 SMESH_Mesh::~SMESH_Mesh()
137 MESSAGE("SMESH_Mesh::~SMESH_Mesh");
139 // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
140 // Notify event listeners at least that something happens
141 if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
142 sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
145 map < int, SMESH_Group * >::iterator itg;
146 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
147 SMESH_Group *aGroup = (*itg).second;
152 if ( _rmGroupCallUp) delete _rmGroupCallUp;
156 //=============================================================================
158 * \brief Set geometry to be meshed
160 //=============================================================================
162 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
164 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
166 if ( !aShape.IsNull() && _isShapeToMesh ) {
167 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
168 _myMeshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
169 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
171 // clear current data
172 if ( !_myMeshDS->ShapeToMesh().IsNull() )
174 // removal of a shape to mesh, delete objects referring to sub-shapes:
176 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.begin();
177 for ( ; i_sm != _mapSubMesh.end(); ++i_sm )
180 // - groups on geometry
181 map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
182 while ( i_gr != _mapGroup.end() ) {
183 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
184 _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
186 _mapGroup.erase( i_gr++ );
191 _mapAncestors.Clear();
194 TopoDS_Shape aNullShape;
195 _myMeshDS->ShapeToMesh( aNullShape );
197 _shapeDiagonal = 0.0;
200 // set a new geometry
201 if ( !aShape.IsNull() )
203 _myMeshDS->ShapeToMesh(aShape);
204 _isShapeToMesh = true;
205 _nbSubShapes = _myMeshDS->MaxShapeIndex();
207 // fill map of ancestors
208 fillAncestorsMap(aShape);
212 _isShapeToMesh = false;
213 _shapeDiagonal = 0.0;
214 _myMeshDS->ShapeToMesh( PseudoShape() );
219 //=======================================================================
221 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
223 //=======================================================================
225 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
227 return _myMeshDS->ShapeToMesh();
230 //=======================================================================
232 * \brief Return a solid which is returned by GetShapeToMesh() if
233 * a real geometry to be meshed was not set
235 //=======================================================================
237 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
239 static TopoDS_Solid aSolid;
240 if ( aSolid.IsNull() )
242 aSolid = BRepPrimAPI_MakeBox(1,1,1);
247 //=======================================================================
249 * \brief Return diagonal size of bounding box of a shape
251 //=======================================================================
253 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
255 if ( !aShape.IsNull() ) {
257 BRepBndLib::Add(aShape, Box);
258 return sqrt( Box.SquareExtent() );
263 //=======================================================================
265 * \brief Return diagonal size of bounding box of shape to mesh
267 //=======================================================================
269 double SMESH_Mesh::GetShapeDiagonalSize() const
271 if ( _shapeDiagonal == 0. && _isShapeToMesh )
272 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
274 return _shapeDiagonal;
277 //=======================================================================
279 * \brief Remove all nodes and elements
281 //=======================================================================
283 void SMESH_Mesh::Clear()
286 _myMeshDS->ClearMesh();
288 // update compute state of submeshes
289 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
291 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
292 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
293 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
294 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
299 //=======================================================================
301 * \brief Remove all nodes and elements of indicated shape
303 //=======================================================================
305 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
307 // clear sub-meshes; get ready to re-compute as a side-effect
308 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
310 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
311 /*complexShapeFirst=*/false);
312 while ( smIt->more() )
315 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
316 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
317 // all other shapes depends on vertices so they are already cleaned
318 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
319 // to recompute even if failed
320 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
325 //=======================================================================
326 //function : UNVToMesh
328 //=======================================================================
330 int SMESH_Mesh::UNVToMesh(const char* theFileName)
332 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
334 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
335 _isShapeToMesh = false;
336 DriverUNV_R_SMDS_Mesh myReader;
337 myReader.SetMesh(_myMeshDS);
338 myReader.SetFile(theFileName);
339 myReader.SetMeshId(-1);
342 MESSAGE("UNVToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
343 MESSAGE("UNVToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
344 MESSAGE("UNVToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
345 MESSAGE("UNVToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
347 SMDS_MeshGroup* aGroup = (SMDS_MeshGroup*) myReader.GetGroup();
349 TGroupNamesMap aGroupNames = myReader.GetGroupNamesMap();
350 //const TGroupIdMap& aGroupId = myReader.GetGroupIdMap();
351 aGroup->InitSubGroupsIterator();
352 while (aGroup->MoreSubGroups()) {
353 SMDS_MeshGroup* aSubGroup = (SMDS_MeshGroup*) aGroup->NextSubGroup();
354 string aName = aGroupNames[aSubGroup];
357 SMESH_Group* aSMESHGroup = AddGroup( aSubGroup->GetType(), aName.c_str(), aId );
359 if(MYDEBUG) MESSAGE("UNVToMesh - group added: "<<aName);
360 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aSMESHGroup->GetGroupDS() );
362 aGroupDS->SetStoreName(aName.c_str());
363 aSubGroup->InitIterator();
364 const SMDS_MeshElement* aElement = 0;
365 while (aSubGroup->More()) {
366 aElement = aSubGroup->Next();
368 aGroupDS->SMDSGroup().Add(aElement);
372 aGroupDS->SetType(aElement->GetType());
380 //=======================================================================
381 //function : MEDToMesh
383 //=======================================================================
385 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
387 if(MYDEBUG) MESSAGE("MEDToMesh - theFileName = "<<theFileName<<", mesh name = "<<theMeshName);
389 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
390 _isShapeToMesh = false;
391 DriverMED_R_SMESHDS_Mesh myReader;
392 myReader.SetMesh(_myMeshDS);
393 myReader.SetMeshId(-1);
394 myReader.SetFile(theFileName);
395 myReader.SetMeshName(theMeshName);
396 Driver_Mesh::Status status = myReader.Perform();
398 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
399 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
400 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
401 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
404 // Reading groups (sub-meshes are out of scope of MED import functionality)
405 list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
406 if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size());
408 list<TNameAndType>::iterator name_type = aGroupNames.begin();
409 for ( ; name_type != aGroupNames.end(); name_type++ ) {
410 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
412 if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<name_type->first.c_str());
413 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
415 aGroupDS->SetStoreName( name_type->first.c_str() );
416 myReader.GetGroup( aGroupDS );
423 //=======================================================================
424 //function : STLToMesh
426 //=======================================================================
428 int SMESH_Mesh::STLToMesh(const char* theFileName)
430 if(MYDEBUG) MESSAGE("STLToMesh - theFileName = "<<theFileName);
432 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
433 _isShapeToMesh = false;
434 DriverSTL_R_SMDS_Mesh myReader;
435 myReader.SetMesh(_myMeshDS);
436 myReader.SetFile(theFileName);
437 myReader.SetMeshId(-1);
440 MESSAGE("STLToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
441 MESSAGE("STLToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
442 MESSAGE("STLToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
443 MESSAGE("STLToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
448 //================================================================================
450 * \brief Reads the given mesh from the CGNS file
451 * \param theFileName - name of the file
452 * \retval int - Driver_Mesh::Status
454 //================================================================================
456 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
457 const int theMeshIndex,
458 std::string& theMeshName)
460 int res = Driver_Mesh::DRS_FAIL;
463 DriverCGNS_Read myReader;
464 myReader.SetMesh(_myMeshDS);
465 myReader.SetFile(theFileName);
466 myReader.SetMeshId(theMeshIndex);
467 res = myReader.Perform();
468 theMeshName = myReader.GetMeshName();
477 //=============================================================================
481 //=============================================================================
483 SMESH_Hypothesis::Hypothesis_Status
484 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
485 int anHypId ) throw(SALOME_Exception)
487 Unexpect aCatch(SalomeException);
488 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
490 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
491 if ( !subMesh || !subMesh->GetId())
492 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
494 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
495 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
497 if(MYDEBUG) MESSAGE("Hypothesis ID does not give an hypothesis");
502 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
505 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
506 MESSAGE( "SMESH_Mesh::AddHypothesis " << anHyp->GetName() );
508 bool isGlobalHyp = IsMainShape( aSubShape );
510 // NotConformAllowed can be only global
513 // NOTE: this is not a correct way to check a name of hypothesis,
514 // there should be an attribute of hypothesis saying that it can/can't
516 string hypName = anHyp->GetName();
517 if ( hypName == "NotConformAllowed" )
519 if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
520 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
526 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
527 int event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
529 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
532 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
533 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
535 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
537 SMESH_Hypothesis::Hypothesis_Status ret2 =
538 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
542 // check concurent hypotheses on ancestors
543 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
545 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
546 while ( smIt->more() ) {
547 SMESH_subMesh* sm = smIt->next();
548 if ( sm->IsApplicableHypotesis( anHyp )) {
549 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
558 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
560 GetMeshDS()->Modified();
562 if(MYDEBUG) subMesh->DumpAlgoState(true);
563 if(MYDEBUG) SCRUTE(ret);
567 //=============================================================================
571 //=============================================================================
573 SMESH_Hypothesis::Hypothesis_Status
574 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
575 int anHypId)throw(SALOME_Exception)
577 Unexpect aCatch(SalomeException);
578 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
580 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
581 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
582 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
584 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
586 int hypType = anHyp->GetType();
592 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
593 int event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
595 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
597 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
599 // there may appear concurrent hyps that were covered by the removed hyp
600 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
601 subMesh->IsApplicableHypotesis( anHyp ) &&
602 subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
603 ret = SMESH_Hypothesis::HYP_CONCURENT;
606 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
607 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
609 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
611 SMESH_Hypothesis::Hypothesis_Status ret2 =
612 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
613 if (ret2 > ret) // more severe
616 // check concurent hypotheses on ancestors
617 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
619 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
620 while ( smIt->more() ) {
621 SMESH_subMesh* sm = smIt->next();
622 if ( sm->IsApplicableHypotesis( anHyp )) {
623 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
633 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
635 GetMeshDS()->Modified();
637 if(MYDEBUG) subMesh->DumpAlgoState(true);
638 if(MYDEBUG) SCRUTE(ret);
642 //=============================================================================
646 //=============================================================================
648 const list<const SMESHDS_Hypothesis*>&
649 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
650 throw(SALOME_Exception)
652 Unexpect aCatch(SalomeException);
653 return _myMeshDS->GetHypothesis(aSubShape);
656 //=======================================================================
658 * \brief Return the hypothesis assigned to the shape
659 * \param aSubShape - the shape to check
660 * \param aFilter - the hypothesis filter
661 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
662 * \param assignedTo - to return the shape the found hypo is assigned to
663 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
665 //=======================================================================
667 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
668 const SMESH_HypoFilter& aFilter,
669 const bool andAncestors,
670 TopoDS_Shape* assignedTo) const
673 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
674 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
675 for ( ; hyp != hypList.end(); hyp++ ) {
676 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
677 if ( aFilter.IsOk( h, aSubShape)) {
678 if ( assignedTo ) *assignedTo = aSubShape;
685 // user sorted submeshes of ancestors, according to stored submesh priority
686 const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
687 list<SMESH_subMesh*>::const_iterator smIt = smList.begin();
688 for ( ; smIt != smList.end(); smIt++ )
690 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
691 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
692 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
693 for ( ; hyp != hypList.end(); hyp++ ) {
694 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
695 if (aFilter.IsOk( h, curSh )) {
696 if ( assignedTo ) *assignedTo = curSh;
705 //================================================================================
707 * \brief Return hypothesis assigned to the shape
708 * \param aSubShape - the shape to check
709 * \param aFilter - the hypothesis filter
710 * \param aHypList - the list of the found hypotheses
711 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
712 * \retval int - number of unique hypos in aHypList
714 //================================================================================
716 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
717 const SMESH_HypoFilter& aFilter,
718 list <const SMESHDS_Hypothesis * >& aHypList,
719 const bool andAncestors) const
721 set<string> hypTypes; // to exclude same type hypos from the result list
724 // only one main hypothesis is allowed
725 bool mainHypFound = false;
728 list<const SMESHDS_Hypothesis*>::const_iterator hyp;
729 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
730 if ( hypTypes.insert( (*hyp)->GetName() ).second )
732 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
736 // get hypos from aSubShape
738 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
739 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
740 if ( aFilter.IsOk (cSMESH_Hyp( *hyp ), aSubShape) &&
741 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
742 hypTypes.insert( (*hyp)->GetName() ).second )
744 aHypList.push_back( *hyp );
746 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
751 // get hypos from ancestors of aSubShape
754 TopTools_MapOfShape map;
756 // user sorted submeshes of ancestors, according to stored submesh priority
757 const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
758 list<SMESH_subMesh*>::const_iterator smIt = smList.begin();
759 for ( ; smIt != smList.end(); smIt++ )
761 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
762 if ( !map.Add( curSh ))
764 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
765 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
766 if (aFilter.IsOk( cSMESH_Hyp( *hyp ), curSh ) &&
767 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
768 hypTypes.insert( (*hyp)->GetName() ).second )
770 aHypList.push_back( *hyp );
772 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
780 //=============================================================================
784 //=============================================================================
786 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
788 Unexpect aCatch(SalomeException);
789 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
790 return _myMeshDS->GetScript()->GetCommands();
793 //=============================================================================
797 //=============================================================================
798 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
800 Unexpect aCatch(SalomeException);
801 if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
802 _myMeshDS->GetScript()->Clear();
805 //=============================================================================
807 * Get or Create the SMESH_subMesh object implementation
809 //=============================================================================
811 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
812 throw(SALOME_Exception)
814 Unexpect aCatch(SalomeException);
815 SMESH_subMesh *aSubMesh;
816 int index = _myMeshDS->ShapeToIndex(aSubShape);
818 // for submeshes on GEOM Group
819 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
820 TopoDS_Iterator it( aSubShape );
823 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
824 if ( index > _nbSubShapes ) _nbSubShapes = index; // not to create sm for this group again
826 // fill map of Ancestors
827 fillAncestorsMap(aSubShape);
831 // return NULL; // neither sub-shape nor a group
833 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.find(index);
834 if ( i_sm != _mapSubMesh.end())
836 aSubMesh = i_sm->second;
840 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
841 _mapSubMesh[index] = aSubMesh;
847 //=============================================================================
849 * Get the SMESH_subMesh object implementation. Dont create it, return null
850 * if it does not exist.
852 //=============================================================================
854 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
855 throw(SALOME_Exception)
857 Unexpect aCatch(SalomeException);
858 SMESH_subMesh *aSubMesh = NULL;
860 int index = _myMeshDS->ShapeToIndex(aSubShape);
862 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(index);
863 if ( i_sm != _mapSubMesh.end())
864 aSubMesh = i_sm->second;
868 //=============================================================================
870 * Get the SMESH_subMesh object implementation. Dont create it, return null
871 * if it does not exist.
873 //=============================================================================
875 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
876 throw(SALOME_Exception)
878 Unexpect aCatch(SalomeException);
880 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(aShapeID);
881 if (i_sm == _mapSubMesh.end())
885 //================================================================================
887 * \brief Return submeshes of groups containing the given subshape
889 //================================================================================
892 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
893 throw(SALOME_Exception)
895 Unexpect aCatch(SalomeException);
896 list<SMESH_subMesh*> found;
898 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
902 // submeshes of groups have max IDs, so search from the map end
903 map<int, SMESH_subMesh *>::const_reverse_iterator i_sm;
904 for ( i_sm = _mapSubMesh.rbegin(); i_sm != _mapSubMesh.rend(); ++i_sm) {
905 SMESHDS_SubMesh * ds = i_sm->second->GetSubMeshDS();
906 if ( ds && ds->IsComplexSubmesh() ) {
907 TopExp_Explorer exp( i_sm->second->GetSubShape(), aSubShape.ShapeType() );
908 for ( ; exp.More(); exp.Next() ) {
909 if ( aSubShape.IsSame( exp.Current() )) {
910 found.push_back( i_sm->second );
920 //=======================================================================
921 //function : IsUsedHypothesis
922 //purpose : Return True if anHyp is used to mesh aSubShape
923 //=======================================================================
925 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
926 const SMESH_subMesh* aSubMesh)
928 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
930 // check if anHyp can be used to mesh aSubMesh
931 if ( !aSubMesh || !aSubMesh->IsApplicableHypotesis( hyp ))
934 const TopoDS_Shape & aSubShape = const_cast<SMESH_subMesh*>( aSubMesh )->GetSubShape();
936 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape );
939 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
940 return ( anHyp == algo );
942 // algorithm parameter
945 // look trough hypotheses used by algo
946 SMESH_HypoFilter hypoKind;
947 if ( algo->InitCompatibleHypoFilter( hypoKind, !hyp->IsAuxiliary() )) {
948 list <const SMESHDS_Hypothesis * > usedHyps;
949 if ( GetHypotheses( aSubShape, hypoKind, usedHyps, true ))
950 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
954 // look through all assigned hypotheses
955 //SMESH_HypoFilter filter( SMESH_HypoFilter::Is( hyp ));
956 return false; //GetHypothesis( aSubShape, filter, true );
959 //=============================================================================
963 //=============================================================================
965 const list < SMESH_subMesh * >&
966 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
967 throw(SALOME_Exception)
969 Unexpect aCatch(SalomeException);
970 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
971 map < int, SMESH_subMesh * >::iterator itsm;
972 _subMeshesUsingHypothesisList.clear();
973 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
975 SMESH_subMesh *aSubMesh = (*itsm).second;
976 if ( IsUsedHypothesis ( anHyp, aSubMesh ))
977 _subMeshesUsingHypothesisList.push_back(aSubMesh);
979 return _subMeshesUsingHypothesisList;
982 //=======================================================================
983 //function : NotifySubMeshesHypothesisModification
984 //purpose : Say all submeshes using theChangedHyp that it has been modified
985 //=======================================================================
987 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
989 Unexpect aCatch(SalomeException);
991 const SMESH_Algo *foundAlgo = 0;
992 SMESH_HypoFilter algoKind, compatibleHypoKind;
993 list <const SMESHDS_Hypothesis * > usedHyps;
996 map < int, SMESH_subMesh * >::iterator itsm;
997 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
999 SMESH_subMesh *aSubMesh = (*itsm).second;
1000 if ( aSubMesh->IsApplicableHypotesis( hyp ))
1002 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1004 if ( !foundAlgo ) // init filter for algo search
1005 algoKind.Init( THypType::IsAlgo() ).And( THypType::IsApplicableTo( aSubShape ));
1007 const SMESH_Algo *algo = static_cast<const SMESH_Algo*>
1008 ( GetHypothesis( aSubShape, algoKind, true ));
1012 bool sameAlgo = ( algo == foundAlgo );
1013 if ( !sameAlgo && foundAlgo )
1014 sameAlgo = ( strcmp( algo->GetName(), foundAlgo->GetName() ) == 0);
1016 if ( !sameAlgo ) { // init filter for used hypos search
1017 if ( !algo->InitCompatibleHypoFilter( compatibleHypoKind, !hyp->IsAuxiliary() ))
1018 continue; // algo does not use any hypothesis
1022 // check if hyp is used by algo
1024 if ( GetHypotheses( aSubShape, compatibleHypoKind, usedHyps, true ) &&
1025 find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
1027 aSubMesh->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1028 const_cast< SMESH_Hypothesis*>( hyp ));
1033 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1034 GetMeshDS()->Modified();
1037 //=============================================================================
1039 * Auto color functionality
1041 //=============================================================================
1042 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1044 Unexpect aCatch(SalomeException);
1045 _isAutoColor = theAutoColor;
1048 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1050 Unexpect aCatch(SalomeException);
1051 return _isAutoColor;
1054 //=======================================================================
1055 //function : SetIsModified
1056 //purpose : Set the flag meaning that the mesh has been edited "manually"
1057 //=======================================================================
1059 void SMESH_Mesh::SetIsModified(bool isModified)
1061 _isModified = isModified;
1064 // check if mesh becomes empty as result of modification
1065 HasModificationsToDiscard();
1068 //=======================================================================
1069 //function : HasModificationsToDiscard
1070 //purpose : Return true if the mesh has been edited since a total re-compute
1071 // and those modifications may prevent successful partial re-compute.
1072 // As a side effect reset _isModified flag if mesh is empty
1074 //=======================================================================
1076 bool SMESH_Mesh::HasModificationsToDiscard() const
1078 if ( ! _isModified )
1081 // return true if the next Compute() will be partial and
1082 // existing but changed elements may prevent successful re-compute
1083 bool hasComputed = false, hasNotComputed = false;
1084 map <int, SMESH_subMesh*>::const_iterator i_sm = _mapSubMesh.begin();
1085 for ( ; i_sm != _mapSubMesh.end() ; ++i_sm )
1086 switch ( i_sm->second->GetSubShape().ShapeType() )
1091 if ( i_sm->second->IsMeshComputed() )
1094 hasNotComputed = true;
1095 if ( hasComputed && hasNotComputed)
1099 if ( NbNodes() < 1 )
1100 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1105 //================================================================================
1107 * \brief Check if any groups of the same type have equal names
1109 //================================================================================
1111 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1113 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1114 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1115 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1117 SMESH_Group* aGroup = it->second;
1118 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1119 string aGroupName = aGroup->GetName();
1120 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1121 if (!aGroupNames[aType].insert(aGroupName).second)
1128 //================================================================================
1130 * \brief Export the mesh to a med file
1132 //================================================================================
1134 void SMESH_Mesh::ExportMED(const char * file,
1135 const char* theMeshName,
1138 const SMESHDS_Mesh* meshPart)
1139 throw(SALOME_Exception)
1141 Unexpect aCatch(SalomeException);
1143 DriverMED_W_SMESHDS_Mesh myWriter;
1144 myWriter.SetFile ( file, MED::EVersion(theVersion) );
1145 myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1147 myWriter.SetMeshId ( _idDoc );
1149 myWriter.SetMeshId ( -1 );
1150 myWriter.SetMeshName( theMeshName );
1153 if ( theAutoGroups ) {
1154 myWriter.AddGroupOfNodes();
1155 myWriter.AddGroupOfEdges();
1156 myWriter.AddGroupOfFaces();
1157 myWriter.AddGroupOfVolumes();
1160 // Pass groups to writer. Provide unique group names.
1161 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1164 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1166 int maxNbIter = 10000; // to guarantee cycle finish
1167 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1168 SMESH_Group* aGroup = it->second;
1169 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1171 SMDSAbs_ElementType aType = aGroupDS->GetType();
1172 string aGroupName0 = aGroup->GetName();
1173 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1174 string aGroupName = aGroupName0;
1175 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1176 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1177 aGroupName = aString;
1178 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1180 aGroupDS->SetStoreName( aGroupName.c_str() );
1181 myWriter.AddGroup( aGroupDS );
1189 //================================================================================
1191 * \brief Export the mesh to a DAT file
1193 //================================================================================
1195 void SMESH_Mesh::ExportDAT(const char * file,
1196 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1198 Unexpect aCatch(SalomeException);
1199 DriverDAT_W_SMDS_Mesh myWriter;
1200 myWriter.SetFile( file );
1201 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1202 myWriter.SetMeshId(_idDoc);
1206 //================================================================================
1208 * \brief Export the mesh to an UNV file
1210 //================================================================================
1212 void SMESH_Mesh::ExportUNV(const char * file,
1213 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1215 Unexpect aCatch(SalomeException);
1216 DriverUNV_W_SMDS_Mesh myWriter;
1217 myWriter.SetFile( file );
1218 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1219 myWriter.SetMeshId(_idDoc);
1220 // myWriter.SetGroups(_mapGroup);
1224 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1225 SMESH_Group* aGroup = it->second;
1226 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1228 string aGroupName = aGroup->GetName();
1229 aGroupDS->SetStoreName( aGroupName.c_str() );
1230 myWriter.AddGroup( aGroupDS );
1237 //================================================================================
1239 * \brief Export the mesh to an STL file
1241 //================================================================================
1243 void SMESH_Mesh::ExportSTL(const char * file,
1245 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1247 Unexpect aCatch(SalomeException);
1248 DriverSTL_W_SMDS_Mesh myWriter;
1249 myWriter.SetFile( file );
1250 myWriter.SetIsAscii( isascii );
1251 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1252 myWriter.SetMeshId(_idDoc);
1256 //================================================================================
1258 * \brief Export the mesh to the CGNS file
1260 //================================================================================
1262 void SMESH_Mesh::ExportCGNS(const char * file,
1263 const SMESHDS_Mesh* meshDS)
1265 int res = Driver_Mesh::DRS_FAIL;
1267 DriverCGNS_Write myWriter;
1268 myWriter.SetFile( file );
1269 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1270 myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1271 res = myWriter.Perform();
1273 if ( res != Driver_Mesh::DRS_OK )
1274 throw SALOME_Exception("Export failed");
1277 //================================================================================
1279 * \brief Return number of nodes in the mesh
1281 //================================================================================
1283 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1285 Unexpect aCatch(SalomeException);
1286 return _myMeshDS->NbNodes();
1289 //================================================================================
1291 * \brief Return number of edges of given order in the mesh
1293 //================================================================================
1295 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1297 Unexpect aCatch(SalomeException);
1298 return _myMeshDS->GetMeshInfo().Nb0DElements();
1301 //================================================================================
1303 * \brief Return number of edges of given order in the mesh
1305 //================================================================================
1307 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1309 Unexpect aCatch(SalomeException);
1310 return _myMeshDS->GetMeshInfo().NbEdges(order);
1313 //================================================================================
1315 * \brief Return number of faces of given order in the mesh
1317 //================================================================================
1319 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1321 Unexpect aCatch(SalomeException);
1322 return _myMeshDS->GetMeshInfo().NbFaces(order);
1325 //================================================================================
1327 * \brief Return the number of faces in the mesh
1329 //================================================================================
1331 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1333 Unexpect aCatch(SalomeException);
1334 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1337 //================================================================================
1339 * \brief Return the number nodes faces in the mesh
1341 //================================================================================
1343 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1345 Unexpect aCatch(SalomeException);
1346 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1349 //================================================================================
1351 * \brief Return the number of polygonal faces in the mesh
1353 //================================================================================
1355 int SMESH_Mesh::NbPolygons() const throw(SALOME_Exception)
1357 Unexpect aCatch(SalomeException);
1358 return _myMeshDS->GetMeshInfo().NbPolygons();
1361 //================================================================================
1363 * \brief Return number of volumes of given order in the mesh
1365 //================================================================================
1367 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1369 Unexpect aCatch(SalomeException);
1370 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1373 //================================================================================
1375 * \brief Return number of tetrahedrons of given order in the mesh
1377 //================================================================================
1379 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1381 Unexpect aCatch(SalomeException);
1382 return _myMeshDS->GetMeshInfo().NbTetras(order);
1385 //================================================================================
1387 * \brief Return number of hexahedrons of given order in the mesh
1389 //================================================================================
1391 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1393 Unexpect aCatch(SalomeException);
1394 return _myMeshDS->GetMeshInfo().NbHexas(order);
1397 //================================================================================
1399 * \brief Return number of pyramids of given order in the mesh
1401 //================================================================================
1403 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1405 Unexpect aCatch(SalomeException);
1406 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1409 //================================================================================
1411 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1413 //================================================================================
1415 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1417 Unexpect aCatch(SalomeException);
1418 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1421 //================================================================================
1423 * \brief Return number of polyhedrons in the mesh
1425 //================================================================================
1427 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1429 Unexpect aCatch(SalomeException);
1430 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1433 //================================================================================
1435 * \brief Return number of submeshes in the mesh
1437 //================================================================================
1439 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1441 Unexpect aCatch(SalomeException);
1442 return _myMeshDS->NbSubMesh();
1445 //=======================================================================
1446 //function : IsNotConformAllowed
1447 //purpose : check if a hypothesis alowing notconform mesh is present
1448 //=======================================================================
1450 bool SMESH_Mesh::IsNotConformAllowed() const
1452 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1454 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1455 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
1458 //=======================================================================
1459 //function : IsMainShape
1461 //=======================================================================
1463 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1465 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1468 //=============================================================================
1472 //=============================================================================
1474 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1475 const char* theName,
1477 const TopoDS_Shape& theShape,
1478 const SMESH_PredicatePtr& thePredicate)
1480 if (_mapGroup.count(_groupId))
1483 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape, thePredicate);
1484 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1485 _mapGroup[_groupId++] = aGroup;
1489 //================================================================================
1491 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
1492 * \retval bool - true if new SMESH_Groups have been created
1495 //================================================================================
1497 bool SMESH_Mesh::SynchronizeGroups()
1499 int nbGroups = _mapGroup.size();
1500 const set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
1501 set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
1502 for ( ; gIt != groups.end(); ++gIt )
1504 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
1505 _groupId = groupDS->GetID();
1506 if ( !_mapGroup.count( _groupId ))
1507 _mapGroup[_groupId] = new SMESH_Group( groupDS );
1509 if ( !_mapGroup.empty() )
1510 _groupId = _mapGroup.rbegin()->first + 1;
1512 return nbGroups < _mapGroup.size();
1515 //================================================================================
1517 * \brief Return iterator on all existing groups
1519 //================================================================================
1521 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
1523 typedef map <int, SMESH_Group *> TMap;
1524 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
1527 //=============================================================================
1529 * \brief Return a group by ID
1531 //=============================================================================
1533 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
1535 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1537 return _mapGroup[theGroupID];
1541 //=============================================================================
1543 * \brief Return IDs of all groups
1545 //=============================================================================
1547 list<int> SMESH_Mesh::GetGroupIds() const
1550 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1551 anIds.push_back( it->first );
1556 //================================================================================
1558 * \brief Set a caller of RemoveGroup() at level of CORBA API implementation.
1559 * The set upCaller will be deleted by SMESH_Mesh
1561 //================================================================================
1563 void SMESH_Mesh::SetRemoveGroupCallUp( TRmGroupCallUp* upCaller )
1565 if ( _rmGroupCallUp ) delete _rmGroupCallUp;
1566 _rmGroupCallUp = upCaller;
1569 //=============================================================================
1573 //=============================================================================
1575 bool SMESH_Mesh::RemoveGroup (const int theGroupID)
1577 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1579 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1580 delete _mapGroup[theGroupID];
1581 _mapGroup.erase (theGroupID);
1583 _rmGroupCallUp->RemoveGroup( theGroupID );
1587 //=======================================================================
1588 //function : GetAncestors
1589 //purpose : return list of ancestors of theSubShape in the order
1590 // that lower dimention shapes come first.
1591 //=======================================================================
1593 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1595 if ( _mapAncestors.Contains( theS ) )
1596 return _mapAncestors.FindFromKey( theS );
1598 static TopTools_ListOfShape emptyList;
1602 //=======================================================================
1604 //purpose : dumps contents of mesh to stream [ debug purposes ]
1605 //=======================================================================
1607 ostream& SMESH_Mesh::Dump(ostream& save)
1610 save << "========================== Dump contents of mesh ==========================" << endl << endl;
1611 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
1612 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
1613 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
1614 save << ++clause << ") Total number of polygons:\t" << NbPolygons() << endl;
1615 save << ++clause << ") Total number of volumes:\t" << NbVolumes() << endl;
1616 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
1617 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
1619 string orderStr = isQuadratic ? "quadratic" : "linear";
1620 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
1622 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
1623 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
1624 if ( NbFaces(order) > 0 ) {
1625 int nb3 = NbTriangles(order);
1626 int nb4 = NbQuadrangles(order);
1627 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
1628 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
1629 if ( nb3 + nb4 != NbFaces(order) ) {
1630 map<int,int> myFaceMap;
1631 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1632 while( itFaces->more( ) ) {
1633 int nbNodes = itFaces->next()->NbNodes();
1634 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1635 myFaceMap[ nbNodes ] = 0;
1636 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1638 save << clause << ".3) Faces in detail: " << endl;
1639 map <int,int>::iterator itF;
1640 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1641 save << "--> nb nodes: " << itF->first << " - nb elemens:\t" << itF->second << endl;
1644 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
1645 if ( NbVolumes(order) > 0 ) {
1646 int nb8 = NbHexas(order);
1647 int nb4 = NbTetras(order);
1648 int nb5 = NbPyramids(order);
1649 int nb6 = NbPrisms(order);
1650 save << clause << ".1) Number of " << orderStr << " hexahedrons:\t" << nb8 << endl;
1651 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
1652 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
1653 save << clause << ".4) Number of " << orderStr << " pyramids:\t" << nb5 << endl;
1654 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
1655 map<int,int> myVolumesMap;
1656 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1657 while( itVolumes->more( ) ) {
1658 int nbNodes = itVolumes->next()->NbNodes();
1659 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1660 myVolumesMap[ nbNodes ] = 0;
1661 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1663 save << clause << ".5) Volumes in detail: " << endl;
1664 map <int,int>::iterator itV;
1665 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1666 save << "--> nb nodes: " << itV->first << " - nb elemens:\t" << itV->second << endl;
1671 save << "===========================================================================" << endl;
1675 //=======================================================================
1676 //function : GetElementType
1677 //purpose : Returns type of mesh element with certain id
1678 //=======================================================================
1680 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
1682 return _myMeshDS->GetElementType( id, iselem );
1685 //=============================================================================
1687 * \brief Convert group on geometry into standalone group
1689 //=============================================================================
1691 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
1693 SMESH_Group* aGroup = 0;
1694 map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
1695 if ( itg == _mapGroup.end() )
1698 SMESH_Group* anOldGrp = (*itg).second;
1699 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
1700 if ( !anOldGrp || !anOldGrpDS )
1703 // create new standalone group
1704 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
1705 _mapGroup[theGroupID] = aGroup;
1707 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
1708 GetMeshDS()->RemoveGroup( anOldGrpDS );
1709 GetMeshDS()->AddGroup( aNewGrpDS );
1711 // add elements (or nodes) into new created group
1712 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
1713 while ( anItr->more() )
1714 aNewGrpDS->Add( (anItr->next())->GetID() );
1722 //=============================================================================
1724 * \brief remove submesh order from Mesh
1726 //=============================================================================
1728 void SMESH_Mesh::ClearMeshOrder()
1730 _mySubMeshOrder.clear();
1733 //=============================================================================
1735 * \brief remove submesh order from Mesh
1737 //=============================================================================
1739 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
1741 _mySubMeshOrder = theOrder;
1744 //=============================================================================
1746 * \brief return submesh order if any
1748 //=============================================================================
1750 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
1752 return _mySubMeshOrder;
1755 //=============================================================================
1757 * \brief fill _mapAncestors
1759 //=============================================================================
1761 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
1764 int desType, ancType;
1765 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
1767 // a geom group is added. Insert it into lists of ancestors before
1768 // the first ancestor more complex than group members
1769 int memberType = TopoDS_Iterator( theShape ).Value().ShapeType();
1770 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
1771 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
1773 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
1774 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
1775 TopTools_ListIteratorOfListOfShape ancIt (ancList);
1776 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
1779 ancList.InsertBefore( theShape, ancIt );
1783 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
1784 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
1785 TopExp::MapShapesAndAncestors ( theShape,
1786 (TopAbs_ShapeEnum) desType,
1787 (TopAbs_ShapeEnum) ancType,
1792 //=============================================================================
1794 * \brief sort submeshes according to stored mesh order
1795 * \param theListToSort in out list to be sorted
1796 * \return FALSE if nothing sorted
1798 //=============================================================================
1800 bool SMESH_Mesh::SortByMeshOrder(list<SMESH_subMesh*>& theListToSort) const
1802 if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
1806 list<SMESH_subMesh*> onlyOrderedList;
1807 // collect all ordered submeshes in one list as pointers
1808 // and get their positions within theListToSort
1809 typedef list<SMESH_subMesh*>::iterator TPosInList;
1810 map< int, TPosInList > sortedPos;
1811 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
1812 TListOfListOfInt::const_iterator listIddIt = _mySubMeshOrder.begin();
1813 for( ; listIddIt != _mySubMeshOrder.end(); listIddIt++) {
1814 const TListOfInt& listOfId = *listIddIt;
1815 TListOfInt::const_iterator idIt = listOfId.begin();
1816 for ( ; idIt != listOfId.end(); idIt++ ) {
1817 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt )) {
1818 TPosInList smPos = find( smBeg, smEnd, sm );
1819 if ( smPos != smEnd ) {
1820 onlyOrderedList.push_back( sm );
1821 sortedPos[ distance( smBeg, smPos )] = smPos;
1826 if (onlyOrderedList.size() < 2)
1830 list<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
1831 list<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
1833 // iterates on ordered submeshes and insert them in detected positions
1834 map< int, TPosInList >::iterator i_pos = sortedPos.begin();
1835 for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
1836 *(i_pos->second) = *onlyBIt;
1841 //=============================================================================
1843 * \brief sort submeshes according to stored mesh order
1844 * \param theListToSort in out list to be sorted
1845 * \return FALSE if nothing sorted
1847 //=============================================================================
1849 list<SMESH_subMesh*> SMESH_Mesh::getAncestorsSubMeshes
1850 (const TopoDS_Shape& theSubShape) const
1852 list<SMESH_subMesh*> listOfSubMesh;
1853 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
1854 for (; it.More(); it.Next() )
1855 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
1856 listOfSubMesh.push_back(sm);
1858 // sort submeshes according to stored mesh order
1859 SortByMeshOrder( listOfSubMesh );
1861 return listOfSubMesh;