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 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
994 const SMESH_Algo *foundAlgo = 0;
995 SMESH_HypoFilter algoKind, compatibleHypoKind;
996 list <const SMESHDS_Hypothesis * > usedHyps;
999 map < int, SMESH_subMesh * >::iterator itsm;
1000 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1002 SMESH_subMesh *aSubMesh = (*itsm).second;
1003 if ( aSubMesh->IsApplicableHypotesis( hyp ))
1005 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1007 if ( !foundAlgo ) // init filter for algo search
1008 algoKind.Init( THypType::IsAlgo() ).And( THypType::IsApplicableTo( aSubShape ));
1010 const SMESH_Algo *algo = static_cast<const SMESH_Algo*>
1011 ( GetHypothesis( aSubShape, algoKind, true ));
1015 bool sameAlgo = ( algo == foundAlgo );
1016 if ( !sameAlgo && foundAlgo )
1017 sameAlgo = ( strcmp( algo->GetName(), foundAlgo->GetName() ) == 0);
1019 if ( !sameAlgo ) { // init filter for used hypos search
1020 if ( !algo->InitCompatibleHypoFilter( compatibleHypoKind, !hyp->IsAuxiliary() ))
1021 continue; // algo does not use any hypothesis
1025 // check if hyp is used by algo
1027 if ( GetHypotheses( aSubShape, compatibleHypoKind, usedHyps, true ) &&
1028 find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
1030 aSubMesh->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1031 const_cast< SMESH_Hypothesis*>( hyp ));
1036 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1037 GetMeshDS()->Modified();
1040 //=============================================================================
1042 * Auto color functionality
1044 //=============================================================================
1045 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1047 Unexpect aCatch(SalomeException);
1048 _isAutoColor = theAutoColor;
1051 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1053 Unexpect aCatch(SalomeException);
1054 return _isAutoColor;
1057 //=======================================================================
1058 //function : SetIsModified
1059 //purpose : Set the flag meaning that the mesh has been edited "manually"
1060 //=======================================================================
1062 void SMESH_Mesh::SetIsModified(bool isModified)
1064 _isModified = isModified;
1067 // check if mesh becomes empty as result of modification
1068 HasModificationsToDiscard();
1071 //=======================================================================
1072 //function : HasModificationsToDiscard
1073 //purpose : Return true if the mesh has been edited since a total re-compute
1074 // and those modifications may prevent successful partial re-compute.
1075 // As a side effect reset _isModified flag if mesh is empty
1077 //=======================================================================
1079 bool SMESH_Mesh::HasModificationsToDiscard() const
1081 if ( ! _isModified )
1084 // return true if the next Compute() will be partial and
1085 // existing but changed elements may prevent successful re-compute
1086 bool hasComputed = false, hasNotComputed = false;
1087 map <int, SMESH_subMesh*>::const_iterator i_sm = _mapSubMesh.begin();
1088 for ( ; i_sm != _mapSubMesh.end() ; ++i_sm )
1089 switch ( i_sm->second->GetSubShape().ShapeType() )
1094 if ( i_sm->second->IsMeshComputed() )
1097 hasNotComputed = true;
1098 if ( hasComputed && hasNotComputed)
1102 if ( NbNodes() < 1 )
1103 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1108 //================================================================================
1110 * \brief Check if any groups of the same type have equal names
1112 //================================================================================
1114 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1116 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1117 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1118 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1120 SMESH_Group* aGroup = it->second;
1121 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1122 string aGroupName = aGroup->GetName();
1123 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1124 if (!aGroupNames[aType].insert(aGroupName).second)
1131 //================================================================================
1133 * \brief Export the mesh to a med file
1135 //================================================================================
1137 void SMESH_Mesh::ExportMED(const char * file,
1138 const char* theMeshName,
1141 const SMESHDS_Mesh* meshPart)
1142 throw(SALOME_Exception)
1144 Unexpect aCatch(SalomeException);
1146 DriverMED_W_SMESHDS_Mesh myWriter;
1147 myWriter.SetFile ( file, MED::EVersion(theVersion) );
1148 myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1150 myWriter.SetMeshId ( _idDoc );
1152 myWriter.SetMeshId ( -1 );
1153 myWriter.SetMeshName( theMeshName );
1156 if ( theAutoGroups ) {
1157 myWriter.AddGroupOfNodes();
1158 myWriter.AddGroupOfEdges();
1159 myWriter.AddGroupOfFaces();
1160 myWriter.AddGroupOfVolumes();
1163 // Pass groups to writer. Provide unique group names.
1164 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1167 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1169 int maxNbIter = 10000; // to guarantee cycle finish
1170 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1171 SMESH_Group* aGroup = it->second;
1172 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1174 SMDSAbs_ElementType aType = aGroupDS->GetType();
1175 string aGroupName0 = aGroup->GetName();
1176 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1177 string aGroupName = aGroupName0;
1178 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1179 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1180 aGroupName = aString;
1181 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1183 aGroupDS->SetStoreName( aGroupName.c_str() );
1184 myWriter.AddGroup( aGroupDS );
1192 void SMESH_Mesh::ExportSAUV(const char *file,
1193 const char* theMeshName,
1195 throw(SALOME_Exception)
1197 std::string medfilename(file);
1198 medfilename += ".med";
1201 cmd = "%PYTHONBIN% ";
1206 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1208 system(cmd.c_str());
1209 ExportMED(medfilename.c_str(), theMeshName, theAutoGroups, 1);
1211 cmd = "%PYTHONBIN% ";
1216 cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
1218 system(cmd.c_str());
1220 cmd = "%PYTHONBIN% ";
1225 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1227 system(cmd.c_str());
1230 //================================================================================
1232 * \brief Export the mesh to a DAT file
1234 //================================================================================
1236 void SMESH_Mesh::ExportDAT(const char * file,
1237 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1239 Unexpect aCatch(SalomeException);
1240 DriverDAT_W_SMDS_Mesh myWriter;
1241 myWriter.SetFile( file );
1242 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1243 myWriter.SetMeshId(_idDoc);
1247 //================================================================================
1249 * \brief Export the mesh to an UNV file
1251 //================================================================================
1253 void SMESH_Mesh::ExportUNV(const char * file,
1254 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1256 Unexpect aCatch(SalomeException);
1257 DriverUNV_W_SMDS_Mesh myWriter;
1258 myWriter.SetFile( file );
1259 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1260 myWriter.SetMeshId(_idDoc);
1261 // myWriter.SetGroups(_mapGroup);
1265 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1266 SMESH_Group* aGroup = it->second;
1267 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1269 string aGroupName = aGroup->GetName();
1270 aGroupDS->SetStoreName( aGroupName.c_str() );
1271 myWriter.AddGroup( aGroupDS );
1278 //================================================================================
1280 * \brief Export the mesh to an STL file
1282 //================================================================================
1284 void SMESH_Mesh::ExportSTL(const char * file,
1286 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1288 Unexpect aCatch(SalomeException);
1289 DriverSTL_W_SMDS_Mesh myWriter;
1290 myWriter.SetFile( file );
1291 myWriter.SetIsAscii( isascii );
1292 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1293 myWriter.SetMeshId(_idDoc);
1297 //================================================================================
1299 * \brief Export the mesh to the CGNS file
1301 //================================================================================
1303 void SMESH_Mesh::ExportCGNS(const char * file,
1304 const SMESHDS_Mesh* meshDS)
1306 int res = Driver_Mesh::DRS_FAIL;
1308 DriverCGNS_Write myWriter;
1309 myWriter.SetFile( file );
1310 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1311 myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1312 res = myWriter.Perform();
1314 if ( res != Driver_Mesh::DRS_OK )
1315 throw SALOME_Exception("Export failed");
1318 //================================================================================
1320 * \brief Return number of nodes in the mesh
1322 //================================================================================
1324 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1326 Unexpect aCatch(SalomeException);
1327 return _myMeshDS->NbNodes();
1330 //================================================================================
1332 * \brief Return number of edges of given order in the mesh
1334 //================================================================================
1336 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1338 Unexpect aCatch(SalomeException);
1339 return _myMeshDS->GetMeshInfo().Nb0DElements();
1342 //================================================================================
1344 * \brief Return number of edges of given order in the mesh
1346 //================================================================================
1348 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1350 Unexpect aCatch(SalomeException);
1351 return _myMeshDS->GetMeshInfo().NbEdges(order);
1354 //================================================================================
1356 * \brief Return number of faces of given order in the mesh
1358 //================================================================================
1360 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1362 Unexpect aCatch(SalomeException);
1363 return _myMeshDS->GetMeshInfo().NbFaces(order);
1366 //================================================================================
1368 * \brief Return the number of faces in the mesh
1370 //================================================================================
1372 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1374 Unexpect aCatch(SalomeException);
1375 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1378 //================================================================================
1380 * \brief Return the number nodes faces in the mesh
1382 //================================================================================
1384 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1386 Unexpect aCatch(SalomeException);
1387 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1390 //================================================================================
1392 * \brief Return the number of polygonal faces in the mesh
1394 //================================================================================
1396 int SMESH_Mesh::NbPolygons() const throw(SALOME_Exception)
1398 Unexpect aCatch(SalomeException);
1399 return _myMeshDS->GetMeshInfo().NbPolygons();
1402 //================================================================================
1404 * \brief Return number of volumes of given order in the mesh
1406 //================================================================================
1408 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1410 Unexpect aCatch(SalomeException);
1411 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1414 //================================================================================
1416 * \brief Return number of tetrahedrons of given order in the mesh
1418 //================================================================================
1420 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1422 Unexpect aCatch(SalomeException);
1423 return _myMeshDS->GetMeshInfo().NbTetras(order);
1426 //================================================================================
1428 * \brief Return number of hexahedrons of given order in the mesh
1430 //================================================================================
1432 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1434 Unexpect aCatch(SalomeException);
1435 return _myMeshDS->GetMeshInfo().NbHexas(order);
1438 //================================================================================
1440 * \brief Return number of pyramids of given order in the mesh
1442 //================================================================================
1444 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1446 Unexpect aCatch(SalomeException);
1447 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1450 //================================================================================
1452 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1454 //================================================================================
1456 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1458 Unexpect aCatch(SalomeException);
1459 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1462 //================================================================================
1464 * \brief Return number of polyhedrons in the mesh
1466 //================================================================================
1468 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1470 Unexpect aCatch(SalomeException);
1471 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1474 //================================================================================
1476 * \brief Return number of submeshes in the mesh
1478 //================================================================================
1480 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1482 Unexpect aCatch(SalomeException);
1483 return _myMeshDS->NbSubMesh();
1486 //=======================================================================
1487 //function : IsNotConformAllowed
1488 //purpose : check if a hypothesis alowing notconform mesh is present
1489 //=======================================================================
1491 bool SMESH_Mesh::IsNotConformAllowed() const
1493 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1495 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1496 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
1499 //=======================================================================
1500 //function : IsMainShape
1502 //=======================================================================
1504 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1506 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1509 //=============================================================================
1513 //=============================================================================
1515 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1516 const char* theName,
1518 const TopoDS_Shape& theShape,
1519 const SMESH_PredicatePtr& thePredicate)
1521 if (_mapGroup.count(_groupId))
1524 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape, thePredicate);
1525 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1526 _mapGroup[_groupId++] = aGroup;
1530 //================================================================================
1532 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
1533 * \retval bool - true if new SMESH_Groups have been created
1536 //================================================================================
1538 bool SMESH_Mesh::SynchronizeGroups()
1540 int nbGroups = _mapGroup.size();
1541 const set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
1542 set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
1543 for ( ; gIt != groups.end(); ++gIt )
1545 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
1546 _groupId = groupDS->GetID();
1547 if ( !_mapGroup.count( _groupId ))
1548 _mapGroup[_groupId] = new SMESH_Group( groupDS );
1550 if ( !_mapGroup.empty() )
1551 _groupId = _mapGroup.rbegin()->first + 1;
1553 return nbGroups < _mapGroup.size();
1556 //================================================================================
1558 * \brief Return iterator on all existing groups
1560 //================================================================================
1562 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
1564 typedef map <int, SMESH_Group *> TMap;
1565 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
1568 //=============================================================================
1570 * \brief Return a group by ID
1572 //=============================================================================
1574 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
1576 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1578 return _mapGroup[theGroupID];
1582 //=============================================================================
1584 * \brief Return IDs of all groups
1586 //=============================================================================
1588 list<int> SMESH_Mesh::GetGroupIds() const
1591 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1592 anIds.push_back( it->first );
1597 //================================================================================
1599 * \brief Set a caller of RemoveGroup() at level of CORBA API implementation.
1600 * The set upCaller will be deleted by SMESH_Mesh
1602 //================================================================================
1604 void SMESH_Mesh::SetRemoveGroupCallUp( TRmGroupCallUp* upCaller )
1606 if ( _rmGroupCallUp ) delete _rmGroupCallUp;
1607 _rmGroupCallUp = upCaller;
1610 //=============================================================================
1614 //=============================================================================
1616 bool SMESH_Mesh::RemoveGroup (const int theGroupID)
1618 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1620 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1621 delete _mapGroup[theGroupID];
1622 _mapGroup.erase (theGroupID);
1624 _rmGroupCallUp->RemoveGroup( theGroupID );
1628 //=======================================================================
1629 //function : GetAncestors
1630 //purpose : return list of ancestors of theSubShape in the order
1631 // that lower dimention shapes come first.
1632 //=======================================================================
1634 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1636 if ( _mapAncestors.Contains( theS ) )
1637 return _mapAncestors.FindFromKey( theS );
1639 static TopTools_ListOfShape emptyList;
1643 //=======================================================================
1645 //purpose : dumps contents of mesh to stream [ debug purposes ]
1646 //=======================================================================
1648 ostream& SMESH_Mesh::Dump(ostream& save)
1651 save << "========================== Dump contents of mesh ==========================" << endl << endl;
1652 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
1653 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
1654 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
1655 save << ++clause << ") Total number of polygons:\t" << NbPolygons() << endl;
1656 save << ++clause << ") Total number of volumes:\t" << NbVolumes() << endl;
1657 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
1658 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
1660 string orderStr = isQuadratic ? "quadratic" : "linear";
1661 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
1663 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
1664 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
1665 if ( NbFaces(order) > 0 ) {
1666 int nb3 = NbTriangles(order);
1667 int nb4 = NbQuadrangles(order);
1668 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
1669 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
1670 if ( nb3 + nb4 != NbFaces(order) ) {
1671 map<int,int> myFaceMap;
1672 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1673 while( itFaces->more( ) ) {
1674 int nbNodes = itFaces->next()->NbNodes();
1675 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1676 myFaceMap[ nbNodes ] = 0;
1677 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1679 save << clause << ".3) Faces in detail: " << endl;
1680 map <int,int>::iterator itF;
1681 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1682 save << "--> nb nodes: " << itF->first << " - nb elemens:\t" << itF->second << endl;
1685 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
1686 if ( NbVolumes(order) > 0 ) {
1687 int nb8 = NbHexas(order);
1688 int nb4 = NbTetras(order);
1689 int nb5 = NbPyramids(order);
1690 int nb6 = NbPrisms(order);
1691 save << clause << ".1) Number of " << orderStr << " hexahedrons:\t" << nb8 << endl;
1692 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
1693 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
1694 save << clause << ".4) Number of " << orderStr << " pyramids:\t" << nb5 << endl;
1695 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
1696 map<int,int> myVolumesMap;
1697 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1698 while( itVolumes->more( ) ) {
1699 int nbNodes = itVolumes->next()->NbNodes();
1700 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1701 myVolumesMap[ nbNodes ] = 0;
1702 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1704 save << clause << ".5) Volumes in detail: " << endl;
1705 map <int,int>::iterator itV;
1706 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1707 save << "--> nb nodes: " << itV->first << " - nb elemens:\t" << itV->second << endl;
1712 save << "===========================================================================" << endl;
1716 //=======================================================================
1717 //function : GetElementType
1718 //purpose : Returns type of mesh element with certain id
1719 //=======================================================================
1721 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
1723 return _myMeshDS->GetElementType( id, iselem );
1726 //=============================================================================
1728 * \brief Convert group on geometry into standalone group
1730 //=============================================================================
1732 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
1734 SMESH_Group* aGroup = 0;
1735 map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
1736 if ( itg == _mapGroup.end() )
1739 SMESH_Group* anOldGrp = (*itg).second;
1740 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
1741 if ( !anOldGrp || !anOldGrpDS )
1744 // create new standalone group
1745 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
1746 _mapGroup[theGroupID] = aGroup;
1748 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
1749 GetMeshDS()->RemoveGroup( anOldGrpDS );
1750 GetMeshDS()->AddGroup( aNewGrpDS );
1752 // add elements (or nodes) into new created group
1753 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
1754 while ( anItr->more() )
1755 aNewGrpDS->Add( (anItr->next())->GetID() );
1758 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
1766 //=============================================================================
1768 * \brief remove submesh order from Mesh
1770 //=============================================================================
1772 void SMESH_Mesh::ClearMeshOrder()
1774 _mySubMeshOrder.clear();
1777 //=============================================================================
1779 * \brief remove submesh order from Mesh
1781 //=============================================================================
1783 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
1785 _mySubMeshOrder = theOrder;
1788 //=============================================================================
1790 * \brief return submesh order if any
1792 //=============================================================================
1794 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
1796 return _mySubMeshOrder;
1799 //=============================================================================
1801 * \brief fill _mapAncestors
1803 //=============================================================================
1805 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
1808 int desType, ancType;
1809 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
1811 // a geom group is added. Insert it into lists of ancestors before
1812 // the first ancestor more complex than group members
1813 int memberType = TopoDS_Iterator( theShape ).Value().ShapeType();
1814 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
1815 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
1817 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
1818 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
1819 TopTools_ListIteratorOfListOfShape ancIt (ancList);
1820 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
1823 ancList.InsertBefore( theShape, ancIt );
1827 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
1828 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
1829 TopExp::MapShapesAndAncestors ( theShape,
1830 (TopAbs_ShapeEnum) desType,
1831 (TopAbs_ShapeEnum) ancType,
1836 //=============================================================================
1838 * \brief sort submeshes according to stored mesh order
1839 * \param theListToSort in out list to be sorted
1840 * \return FALSE if nothing sorted
1842 //=============================================================================
1844 bool SMESH_Mesh::SortByMeshOrder(list<SMESH_subMesh*>& theListToSort) const
1846 if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
1850 list<SMESH_subMesh*> onlyOrderedList;
1851 // collect all ordered submeshes in one list as pointers
1852 // and get their positions within theListToSort
1853 typedef list<SMESH_subMesh*>::iterator TPosInList;
1854 map< int, TPosInList > sortedPos;
1855 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
1856 TListOfListOfInt::const_iterator listIddIt = _mySubMeshOrder.begin();
1857 for( ; listIddIt != _mySubMeshOrder.end(); listIddIt++) {
1858 const TListOfInt& listOfId = *listIddIt;
1859 TListOfInt::const_iterator idIt = listOfId.begin();
1860 for ( ; idIt != listOfId.end(); idIt++ ) {
1861 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt )) {
1862 TPosInList smPos = find( smBeg, smEnd, sm );
1863 if ( smPos != smEnd ) {
1864 onlyOrderedList.push_back( sm );
1865 sortedPos[ distance( smBeg, smPos )] = smPos;
1870 if (onlyOrderedList.size() < 2)
1874 list<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
1875 list<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
1877 // iterate on ordered submeshes and insert them in detected positions
1878 map< int, TPosInList >::iterator i_pos = sortedPos.begin();
1879 for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
1880 *(i_pos->second) = *onlyBIt;
1885 //=============================================================================
1887 * \brief sort submeshes according to stored mesh order
1888 * \param theListToSort in out list to be sorted
1889 * \return FALSE if nothing sorted
1891 //=============================================================================
1893 list<SMESH_subMesh*> SMESH_Mesh::getAncestorsSubMeshes
1894 (const TopoDS_Shape& theSubShape) const
1896 list<SMESH_subMesh*> listOfSubMesh;
1897 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
1898 for (; it.More(); it.Next() )
1899 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
1900 listOfSubMesh.push_back(sm);
1902 // sort submeshes according to stored mesh order
1903 SortByMeshOrder( listOfSubMesh );
1905 return listOfSubMesh;