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"
69 #include <boost/thread/thread.hpp>
70 #include <boost/bind.hpp>
74 // maximum stored group name length in MED file
75 #define MAX_MED_GROUP_NAME_LENGTH 80
78 static int MYDEBUG = 0;
80 static int MYDEBUG = 0;
83 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
85 typedef SMESH_HypoFilter THypType;
87 //=============================================================================
91 //=============================================================================
93 SMESH_Mesh::SMESH_Mesh(int theLocalId,
96 bool theIsEmbeddedMode,
97 SMESHDS_Document* theDocument):
98 _groupId( 0 ), _nbSubShapes( 0 )
100 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
102 _studyId = theStudyId;
104 _myDocument = theDocument;
105 _idDoc = theDocument->NewMesh(theIsEmbeddedMode);
106 _myMeshDS = theDocument->GetMesh(_idDoc);
107 _isShapeToMesh = false;
108 _isAutoColor = false;
110 _shapeDiagonal = 0.0;
112 _myMeshDS->ShapeToMesh( PseudoShape() );
115 //================================================================================
117 * \brief Constructor of SMESH_Mesh being a base of some descendant class
119 //================================================================================
121 SMESH_Mesh::SMESH_Mesh():
127 _isShapeToMesh( false ),
131 _isAutoColor( false ),
132 _isModified( false ),
133 _shapeDiagonal( 0.0 ),
140 void deleteMeshDS(SMESHDS_Mesh* meshDS)
142 //cout << "deleteMeshDS( " << meshDS << endl;
147 //=============================================================================
151 //=============================================================================
153 SMESH_Mesh::~SMESH_Mesh()
155 MESSAGE("SMESH_Mesh::~SMESH_Mesh");
157 // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
158 // Notify event listeners at least that something happens
159 if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
160 sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
163 map < int, SMESH_Group * >::iterator itg;
164 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
165 SMESH_Group *aGroup = (*itg).second;
171 map <int, SMESH_subMesh*>::iterator sm = _mapSubMesh.begin();
172 for ( ; sm != _mapSubMesh.end(); ++sm )
176 if ( _rmGroupCallUp) delete _rmGroupCallUp;
179 // remove self from studyContext
182 StudyContextStruct * studyContext = _gen->GetStudyContext( _studyId );
183 studyContext->mapMesh.erase( _id );
186 _myDocument->RemoveMesh( _id );
189 // delete _myMeshDS, in a thread in order not to block closing a study with large meshes
190 boost::thread aThread(boost::bind( & deleteMeshDS, _myMeshDS ));
193 //=============================================================================
195 * \brief Set geometry to be meshed
197 //=============================================================================
199 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
201 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
203 if ( !aShape.IsNull() && _isShapeToMesh ) {
204 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
205 _myMeshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
206 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
208 // clear current data
209 if ( !_myMeshDS->ShapeToMesh().IsNull() )
211 // removal of a shape to mesh, delete objects referring to sub-shapes:
213 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.begin();
214 for ( ; i_sm != _mapSubMesh.end(); ++i_sm )
217 // - groups on geometry
218 map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
219 while ( i_gr != _mapGroup.end() ) {
220 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
221 _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
223 _mapGroup.erase( i_gr++ );
228 _mapAncestors.Clear();
231 TopoDS_Shape aNullShape;
232 _myMeshDS->ShapeToMesh( aNullShape );
234 _shapeDiagonal = 0.0;
237 // set a new geometry
238 if ( !aShape.IsNull() )
240 _myMeshDS->ShapeToMesh(aShape);
241 _isShapeToMesh = true;
242 _nbSubShapes = _myMeshDS->MaxShapeIndex();
244 // fill map of ancestors
245 fillAncestorsMap(aShape);
249 _isShapeToMesh = false;
250 _shapeDiagonal = 0.0;
251 _myMeshDS->ShapeToMesh( PseudoShape() );
256 //=======================================================================
258 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
260 //=======================================================================
262 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
264 return _myMeshDS->ShapeToMesh();
267 //=======================================================================
269 * \brief Return a solid which is returned by GetShapeToMesh() if
270 * a real geometry to be meshed was not set
272 //=======================================================================
274 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
276 static TopoDS_Solid aSolid;
277 if ( aSolid.IsNull() )
279 aSolid = BRepPrimAPI_MakeBox(1,1,1);
284 //=======================================================================
286 * \brief Return diagonal size of bounding box of a shape
288 //=======================================================================
290 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
292 if ( !aShape.IsNull() ) {
294 BRepBndLib::Add(aShape, Box);
295 return sqrt( Box.SquareExtent() );
300 //=======================================================================
302 * \brief Return diagonal size of bounding box of shape to mesh
304 //=======================================================================
306 double SMESH_Mesh::GetShapeDiagonalSize() const
308 if ( _shapeDiagonal == 0. && _isShapeToMesh )
309 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
311 return _shapeDiagonal;
314 //=======================================================================
316 * \brief Remove all nodes and elements
318 //=======================================================================
320 void SMESH_Mesh::Clear()
323 _myMeshDS->ClearMesh();
325 // update compute state of submeshes
326 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
328 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
329 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
330 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
331 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
336 //=======================================================================
338 * \brief Remove all nodes and elements of indicated shape
340 //=======================================================================
342 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
344 // clear sub-meshes; get ready to re-compute as a side-effect
345 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
347 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
348 /*complexShapeFirst=*/false);
349 while ( smIt->more() )
352 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
353 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
354 // all other shapes depends on vertices so they are already cleaned
355 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
356 // to recompute even if failed
357 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
362 //=======================================================================
363 //function : UNVToMesh
365 //=======================================================================
367 int SMESH_Mesh::UNVToMesh(const char* theFileName)
369 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
371 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
372 _isShapeToMesh = false;
373 DriverUNV_R_SMDS_Mesh myReader;
374 myReader.SetMesh(_myMeshDS);
375 myReader.SetFile(theFileName);
376 myReader.SetMeshId(-1);
379 MESSAGE("UNVToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
380 MESSAGE("UNVToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
381 MESSAGE("UNVToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
382 MESSAGE("UNVToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
384 SMDS_MeshGroup* aGroup = (SMDS_MeshGroup*) myReader.GetGroup();
386 TGroupNamesMap aGroupNames = myReader.GetGroupNamesMap();
387 //const TGroupIdMap& aGroupId = myReader.GetGroupIdMap();
388 aGroup->InitSubGroupsIterator();
389 while (aGroup->MoreSubGroups()) {
390 SMDS_MeshGroup* aSubGroup = (SMDS_MeshGroup*) aGroup->NextSubGroup();
391 string aName = aGroupNames[aSubGroup];
394 SMESH_Group* aSMESHGroup = AddGroup( aSubGroup->GetType(), aName.c_str(), aId );
396 if(MYDEBUG) MESSAGE("UNVToMesh - group added: "<<aName);
397 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aSMESHGroup->GetGroupDS() );
399 aGroupDS->SetStoreName(aName.c_str());
400 aSubGroup->InitIterator();
401 const SMDS_MeshElement* aElement = 0;
402 while (aSubGroup->More()) {
403 aElement = aSubGroup->Next();
405 aGroupDS->SMDSGroup().Add(aElement);
409 aGroupDS->SetType(aElement->GetType());
417 //=======================================================================
418 //function : MEDToMesh
420 //=======================================================================
422 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
424 if(MYDEBUG) MESSAGE("MEDToMesh - theFileName = "<<theFileName<<", mesh name = "<<theMeshName);
426 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
427 _isShapeToMesh = false;
428 DriverMED_R_SMESHDS_Mesh myReader;
429 myReader.SetMesh(_myMeshDS);
430 myReader.SetMeshId(-1);
431 myReader.SetFile(theFileName);
432 myReader.SetMeshName(theMeshName);
433 Driver_Mesh::Status status = myReader.Perform();
435 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
436 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
437 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
438 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
441 // Reading groups (sub-meshes are out of scope of MED import functionality)
442 list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
443 if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size());
445 list<TNameAndType>::iterator name_type = aGroupNames.begin();
446 for ( ; name_type != aGroupNames.end(); name_type++ ) {
447 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
449 if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<name_type->first.c_str());
450 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
452 aGroupDS->SetStoreName( name_type->first.c_str() );
453 myReader.GetGroup( aGroupDS );
460 //=======================================================================
461 //function : STLToMesh
463 //=======================================================================
465 int SMESH_Mesh::STLToMesh(const char* theFileName)
467 if(MYDEBUG) MESSAGE("STLToMesh - theFileName = "<<theFileName);
469 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
470 _isShapeToMesh = false;
471 DriverSTL_R_SMDS_Mesh myReader;
472 myReader.SetMesh(_myMeshDS);
473 myReader.SetFile(theFileName);
474 myReader.SetMeshId(-1);
477 MESSAGE("STLToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
478 MESSAGE("STLToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
479 MESSAGE("STLToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
480 MESSAGE("STLToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
485 //================================================================================
487 * \brief Reads the given mesh from the CGNS file
488 * \param theFileName - name of the file
489 * \retval int - Driver_Mesh::Status
491 //================================================================================
493 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
494 const int theMeshIndex,
495 std::string& theMeshName)
497 int res = Driver_Mesh::DRS_FAIL;
500 DriverCGNS_Read myReader;
501 myReader.SetMesh(_myMeshDS);
502 myReader.SetFile(theFileName);
503 myReader.SetMeshId(theMeshIndex);
504 res = myReader.Perform();
505 theMeshName = myReader.GetMeshName();
514 //=============================================================================
518 //=============================================================================
520 SMESH_Hypothesis::Hypothesis_Status
521 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
522 int anHypId ) throw(SALOME_Exception)
524 Unexpect aCatch(SalomeException);
525 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
527 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
528 if ( !subMesh || !subMesh->GetId())
529 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
531 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
532 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
534 if(MYDEBUG) MESSAGE("Hypothesis ID does not give an hypothesis");
539 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
542 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
543 MESSAGE( "SMESH_Mesh::AddHypothesis " << anHyp->GetName() );
545 bool isGlobalHyp = IsMainShape( aSubShape );
547 // NotConformAllowed can be only global
550 // NOTE: this is not a correct way to check a name of hypothesis,
551 // there should be an attribute of hypothesis saying that it can/can't
553 string hypName = anHyp->GetName();
554 if ( hypName == "NotConformAllowed" )
556 if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
557 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
563 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
564 int event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
566 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
569 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
570 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
572 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
574 SMESH_Hypothesis::Hypothesis_Status ret2 =
575 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
579 // check concurent hypotheses on ancestors
580 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
582 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
583 while ( smIt->more() ) {
584 SMESH_subMesh* sm = smIt->next();
585 if ( sm->IsApplicableHypotesis( anHyp )) {
586 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
595 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
597 GetMeshDS()->Modified();
599 if(MYDEBUG) subMesh->DumpAlgoState(true);
600 if(MYDEBUG) SCRUTE(ret);
604 //=============================================================================
608 //=============================================================================
610 SMESH_Hypothesis::Hypothesis_Status
611 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
612 int anHypId)throw(SALOME_Exception)
614 Unexpect aCatch(SalomeException);
615 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
617 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
618 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
619 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
621 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
623 int hypType = anHyp->GetType();
629 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
630 int event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
632 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
634 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
636 // there may appear concurrent hyps that were covered by the removed hyp
637 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
638 subMesh->IsApplicableHypotesis( anHyp ) &&
639 subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
640 ret = SMESH_Hypothesis::HYP_CONCURENT;
643 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
644 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
646 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
648 SMESH_Hypothesis::Hypothesis_Status ret2 =
649 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
650 if (ret2 > ret) // more severe
653 // check concurent hypotheses on ancestors
654 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
656 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
657 while ( smIt->more() ) {
658 SMESH_subMesh* sm = smIt->next();
659 if ( sm->IsApplicableHypotesis( anHyp )) {
660 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
670 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
672 GetMeshDS()->Modified();
674 if(MYDEBUG) subMesh->DumpAlgoState(true);
675 if(MYDEBUG) SCRUTE(ret);
679 //=============================================================================
683 //=============================================================================
685 const list<const SMESHDS_Hypothesis*>&
686 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
687 throw(SALOME_Exception)
689 Unexpect aCatch(SalomeException);
690 return _myMeshDS->GetHypothesis(aSubShape);
693 //=======================================================================
695 * \brief Return the hypothesis assigned to the shape
696 * \param aSubShape - the shape to check
697 * \param aFilter - the hypothesis filter
698 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
699 * \param assignedTo - to return the shape the found hypo is assigned to
700 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
702 //=======================================================================
704 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
705 const SMESH_HypoFilter& aFilter,
706 const bool andAncestors,
707 TopoDS_Shape* assignedTo) const
710 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
711 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
712 for ( ; hyp != hypList.end(); hyp++ ) {
713 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
714 if ( aFilter.IsOk( h, aSubShape)) {
715 if ( assignedTo ) *assignedTo = aSubShape;
722 // user sorted submeshes of ancestors, according to stored submesh priority
723 const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
724 list<SMESH_subMesh*>::const_iterator smIt = smList.begin();
725 for ( ; smIt != smList.end(); smIt++ )
727 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
728 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
729 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
730 for ( ; hyp != hypList.end(); hyp++ ) {
731 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
732 if (aFilter.IsOk( h, curSh )) {
733 if ( assignedTo ) *assignedTo = curSh;
742 //================================================================================
744 * \brief Return hypothesis assigned to the shape
745 * \param aSubShape - the shape to check
746 * \param aFilter - the hypothesis filter
747 * \param aHypList - the list of the found hypotheses
748 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
749 * \retval int - number of unique hypos in aHypList
751 //================================================================================
753 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
754 const SMESH_HypoFilter& aFilter,
755 list <const SMESHDS_Hypothesis * >& aHypList,
756 const bool andAncestors) const
758 set<string> hypTypes; // to exclude same type hypos from the result list
761 // only one main hypothesis is allowed
762 bool mainHypFound = false;
765 list<const SMESHDS_Hypothesis*>::const_iterator hyp;
766 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
767 if ( hypTypes.insert( (*hyp)->GetName() ).second )
769 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
773 // get hypos from aSubShape
775 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
776 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
777 if ( aFilter.IsOk (cSMESH_Hyp( *hyp ), aSubShape) &&
778 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
779 hypTypes.insert( (*hyp)->GetName() ).second )
781 aHypList.push_back( *hyp );
783 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
788 // get hypos from ancestors of aSubShape
791 TopTools_MapOfShape map;
793 // user sorted submeshes of ancestors, according to stored submesh priority
794 const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
795 list<SMESH_subMesh*>::const_iterator smIt = smList.begin();
796 for ( ; smIt != smList.end(); smIt++ )
798 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
799 if ( !map.Add( curSh ))
801 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
802 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
803 if (aFilter.IsOk( cSMESH_Hyp( *hyp ), curSh ) &&
804 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
805 hypTypes.insert( (*hyp)->GetName() ).second )
807 aHypList.push_back( *hyp );
809 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
817 //=============================================================================
821 //=============================================================================
823 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
825 Unexpect aCatch(SalomeException);
826 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
827 return _myMeshDS->GetScript()->GetCommands();
830 //=============================================================================
834 //=============================================================================
835 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
837 Unexpect aCatch(SalomeException);
838 if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
839 _myMeshDS->GetScript()->Clear();
842 //=============================================================================
844 * Get or Create the SMESH_subMesh object implementation
846 //=============================================================================
848 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
849 throw(SALOME_Exception)
851 Unexpect aCatch(SalomeException);
852 SMESH_subMesh *aSubMesh;
853 int index = _myMeshDS->ShapeToIndex(aSubShape);
855 // for submeshes on GEOM Group
856 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
857 TopoDS_Iterator it( aSubShape );
860 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
861 if ( index > _nbSubShapes ) _nbSubShapes = index; // not to create sm for this group again
863 // fill map of Ancestors
864 fillAncestorsMap(aSubShape);
868 // return NULL; // neither sub-shape nor a group
870 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.find(index);
871 if ( i_sm != _mapSubMesh.end())
873 aSubMesh = i_sm->second;
877 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
878 _mapSubMesh[index] = aSubMesh;
884 //=============================================================================
886 * Get the SMESH_subMesh object implementation. Dont create it, return null
887 * if it does not exist.
889 //=============================================================================
891 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
892 throw(SALOME_Exception)
894 Unexpect aCatch(SalomeException);
895 SMESH_subMesh *aSubMesh = NULL;
897 int index = _myMeshDS->ShapeToIndex(aSubShape);
899 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(index);
900 if ( i_sm != _mapSubMesh.end())
901 aSubMesh = i_sm->second;
905 //=============================================================================
907 * Get the SMESH_subMesh object implementation. Dont create it, return null
908 * if it does not exist.
910 //=============================================================================
912 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
913 throw(SALOME_Exception)
915 Unexpect aCatch(SalomeException);
917 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(aShapeID);
918 if (i_sm == _mapSubMesh.end())
922 //================================================================================
924 * \brief Return submeshes of groups containing the given sub-shape
926 //================================================================================
929 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
930 throw(SALOME_Exception)
932 Unexpect aCatch(SalomeException);
933 list<SMESH_subMesh*> found;
935 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
939 // submeshes of groups have max IDs, so search from the map end
940 map<int, SMESH_subMesh *>::const_reverse_iterator i_sm;
941 for ( i_sm = _mapSubMesh.rbegin(); i_sm != _mapSubMesh.rend(); ++i_sm) {
942 SMESHDS_SubMesh * ds = i_sm->second->GetSubMeshDS();
943 if ( ds && ds->IsComplexSubmesh() ) {
944 TopExp_Explorer exp( i_sm->second->GetSubShape(), aSubShape.ShapeType() );
945 for ( ; exp.More(); exp.Next() ) {
946 if ( aSubShape.IsSame( exp.Current() )) {
947 found.push_back( i_sm->second );
957 //=======================================================================
958 //function : IsUsedHypothesis
959 //purpose : Return True if anHyp is used to mesh aSubShape
960 //=======================================================================
962 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
963 const SMESH_subMesh* aSubMesh)
965 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
967 // check if anHyp can be used to mesh aSubMesh
968 if ( !aSubMesh || !aSubMesh->IsApplicableHypotesis( hyp ))
971 const TopoDS_Shape & aSubShape = const_cast<SMESH_subMesh*>( aSubMesh )->GetSubShape();
973 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape );
976 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
977 return ( anHyp == algo );
979 // algorithm parameter
982 // look trough hypotheses used by algo
983 SMESH_HypoFilter hypoKind;
984 if ( algo->InitCompatibleHypoFilter( hypoKind, !hyp->IsAuxiliary() )) {
985 list <const SMESHDS_Hypothesis * > usedHyps;
986 if ( GetHypotheses( aSubShape, hypoKind, usedHyps, true ))
987 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
991 // look through all assigned hypotheses
992 //SMESH_HypoFilter filter( SMESH_HypoFilter::Is( hyp ));
993 return false; //GetHypothesis( aSubShape, filter, true );
996 //=============================================================================
1000 //=============================================================================
1002 const list < SMESH_subMesh * >&
1003 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
1004 throw(SALOME_Exception)
1006 Unexpect aCatch(SalomeException);
1007 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
1008 map < int, SMESH_subMesh * >::iterator itsm;
1009 _subMeshesUsingHypothesisList.clear();
1010 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1012 SMESH_subMesh *aSubMesh = (*itsm).second;
1013 if ( IsUsedHypothesis ( anHyp, aSubMesh ))
1014 _subMeshesUsingHypothesisList.push_back(aSubMesh);
1016 return _subMeshesUsingHypothesisList;
1019 //=======================================================================
1020 //function : NotifySubMeshesHypothesisModification
1021 //purpose : Say all submeshes using theChangedHyp that it has been modified
1022 //=======================================================================
1024 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1026 Unexpect aCatch(SalomeException);
1028 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1031 const SMESH_Algo *foundAlgo = 0;
1032 SMESH_HypoFilter algoKind, compatibleHypoKind;
1033 list <const SMESHDS_Hypothesis * > usedHyps;
1036 map < int, SMESH_subMesh * >::iterator itsm;
1037 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1039 SMESH_subMesh *aSubMesh = (*itsm).second;
1040 if ( aSubMesh->IsApplicableHypotesis( hyp ))
1042 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1044 if ( !foundAlgo ) // init filter for algo search
1045 algoKind.Init( THypType::IsAlgo() ).And( THypType::IsApplicableTo( aSubShape ));
1047 const SMESH_Algo *algo = static_cast<const SMESH_Algo*>
1048 ( GetHypothesis( aSubShape, algoKind, true ));
1052 bool sameAlgo = ( algo == foundAlgo );
1053 if ( !sameAlgo && foundAlgo )
1054 sameAlgo = ( strcmp( algo->GetName(), foundAlgo->GetName() ) == 0);
1056 if ( !sameAlgo ) { // init filter for used hypos search
1057 if ( !algo->InitCompatibleHypoFilter( compatibleHypoKind, !hyp->IsAuxiliary() ))
1058 continue; // algo does not use any hypothesis
1062 // check if hyp is used by algo
1064 if ( GetHypotheses( aSubShape, compatibleHypoKind, usedHyps, true ) &&
1065 find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
1067 aSubMesh->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1068 const_cast< SMESH_Hypothesis*>( hyp ));
1073 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1074 GetMeshDS()->Modified();
1077 //=============================================================================
1079 * Auto color functionality
1081 //=============================================================================
1082 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1084 Unexpect aCatch(SalomeException);
1085 _isAutoColor = theAutoColor;
1088 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1090 Unexpect aCatch(SalomeException);
1091 return _isAutoColor;
1094 //=======================================================================
1095 //function : SetIsModified
1096 //purpose : Set the flag meaning that the mesh has been edited "manually"
1097 //=======================================================================
1099 void SMESH_Mesh::SetIsModified(bool isModified)
1101 _isModified = isModified;
1104 // check if mesh becomes empty as result of modification
1105 HasModificationsToDiscard();
1108 //=======================================================================
1109 //function : HasModificationsToDiscard
1110 //purpose : Return true if the mesh has been edited since a total re-compute
1111 // and those modifications may prevent successful partial re-compute.
1112 // As a side effect reset _isModified flag if mesh is empty
1114 //=======================================================================
1116 bool SMESH_Mesh::HasModificationsToDiscard() const
1118 if ( ! _isModified )
1121 // return true if the next Compute() will be partial and
1122 // existing but changed elements may prevent successful re-compute
1123 bool hasComputed = false, hasNotComputed = false;
1124 map <int, SMESH_subMesh*>::const_iterator i_sm = _mapSubMesh.begin();
1125 for ( ; i_sm != _mapSubMesh.end() ; ++i_sm )
1126 switch ( i_sm->second->GetSubShape().ShapeType() )
1131 if ( i_sm->second->IsMeshComputed() )
1134 hasNotComputed = true;
1135 if ( hasComputed && hasNotComputed)
1139 if ( NbNodes() < 1 )
1140 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1145 //================================================================================
1147 * \brief Check if any groups of the same type have equal names
1149 //================================================================================
1151 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1153 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1154 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1155 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1157 SMESH_Group* aGroup = it->second;
1158 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1159 string aGroupName = aGroup->GetName();
1160 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1161 if (!aGroupNames[aType].insert(aGroupName).second)
1168 //================================================================================
1170 * \brief Export the mesh to a med file
1172 //================================================================================
1174 void SMESH_Mesh::ExportMED(const char * file,
1175 const char* theMeshName,
1178 const SMESHDS_Mesh* meshPart)
1179 throw(SALOME_Exception)
1181 Unexpect aCatch(SalomeException);
1183 DriverMED_W_SMESHDS_Mesh myWriter;
1184 myWriter.SetFile ( file, MED::EVersion(theVersion) );
1185 myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1187 myWriter.SetMeshId ( _idDoc );
1189 myWriter.SetMeshId ( -1 );
1190 myWriter.SetMeshName( theMeshName );
1193 if ( theAutoGroups ) {
1194 myWriter.AddGroupOfNodes();
1195 myWriter.AddGroupOfEdges();
1196 myWriter.AddGroupOfFaces();
1197 myWriter.AddGroupOfVolumes();
1200 // Pass groups to writer. Provide unique group names.
1201 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1204 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1206 int maxNbIter = 10000; // to guarantee cycle finish
1207 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1208 SMESH_Group* aGroup = it->second;
1209 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1211 SMDSAbs_ElementType aType = aGroupDS->GetType();
1212 string aGroupName0 = aGroup->GetName();
1213 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1214 string aGroupName = aGroupName0;
1215 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1216 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1217 aGroupName = aString;
1218 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1220 aGroupDS->SetStoreName( aGroupName.c_str() );
1221 myWriter.AddGroup( aGroupDS );
1229 void SMESH_Mesh::ExportSAUV(const char *file,
1230 const char* theMeshName,
1232 throw(SALOME_Exception)
1234 std::string medfilename(file);
1235 medfilename += ".med";
1238 cmd = "%PYTHONBIN% ";
1243 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1245 system(cmd.c_str());
1246 ExportMED(medfilename.c_str(), theMeshName, theAutoGroups, 1);
1248 cmd = "%PYTHONBIN% ";
1253 cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
1255 system(cmd.c_str());
1257 cmd = "%PYTHONBIN% ";
1262 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1264 system(cmd.c_str());
1267 //================================================================================
1269 * \brief Export the mesh to a DAT file
1271 //================================================================================
1273 void SMESH_Mesh::ExportDAT(const char * file,
1274 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1276 Unexpect aCatch(SalomeException);
1277 DriverDAT_W_SMDS_Mesh myWriter;
1278 myWriter.SetFile( file );
1279 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1280 myWriter.SetMeshId(_idDoc);
1284 //================================================================================
1286 * \brief Export the mesh to an UNV file
1288 //================================================================================
1290 void SMESH_Mesh::ExportUNV(const char * file,
1291 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1293 Unexpect aCatch(SalomeException);
1294 DriverUNV_W_SMDS_Mesh myWriter;
1295 myWriter.SetFile( file );
1296 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1297 myWriter.SetMeshId(_idDoc);
1298 // myWriter.SetGroups(_mapGroup);
1302 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1303 SMESH_Group* aGroup = it->second;
1304 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1306 string aGroupName = aGroup->GetName();
1307 aGroupDS->SetStoreName( aGroupName.c_str() );
1308 myWriter.AddGroup( aGroupDS );
1315 //================================================================================
1317 * \brief Export the mesh to an STL file
1319 //================================================================================
1321 void SMESH_Mesh::ExportSTL(const char * file,
1323 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1325 Unexpect aCatch(SalomeException);
1326 DriverSTL_W_SMDS_Mesh myWriter;
1327 myWriter.SetFile( file );
1328 myWriter.SetIsAscii( isascii );
1329 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1330 myWriter.SetMeshId(_idDoc);
1334 //================================================================================
1336 * \brief Export the mesh to the CGNS file
1338 //================================================================================
1340 void SMESH_Mesh::ExportCGNS(const char * file,
1341 const SMESHDS_Mesh* meshDS)
1343 int res = Driver_Mesh::DRS_FAIL;
1345 DriverCGNS_Write myWriter;
1346 myWriter.SetFile( file );
1347 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1348 myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1349 res = myWriter.Perform();
1351 if ( res != Driver_Mesh::DRS_OK )
1352 throw SALOME_Exception("Export failed");
1355 //================================================================================
1357 * \brief Return number of nodes in the mesh
1359 //================================================================================
1361 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1363 Unexpect aCatch(SalomeException);
1364 return _myMeshDS->NbNodes();
1367 //================================================================================
1369 * \brief Return number of edges of given order in the mesh
1371 //================================================================================
1373 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1375 Unexpect aCatch(SalomeException);
1376 return _myMeshDS->GetMeshInfo().Nb0DElements();
1379 //================================================================================
1381 * \brief Return number of edges of given order in the mesh
1383 //================================================================================
1385 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1387 Unexpect aCatch(SalomeException);
1388 return _myMeshDS->GetMeshInfo().NbEdges(order);
1391 //================================================================================
1393 * \brief Return number of faces of given order in the mesh
1395 //================================================================================
1397 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1399 Unexpect aCatch(SalomeException);
1400 return _myMeshDS->GetMeshInfo().NbFaces(order);
1403 //================================================================================
1405 * \brief Return the number of faces in the mesh
1407 //================================================================================
1409 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1411 Unexpect aCatch(SalomeException);
1412 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1415 //================================================================================
1417 * \brief Return the number nodes faces in the mesh
1419 //================================================================================
1421 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1423 Unexpect aCatch(SalomeException);
1424 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1427 //================================================================================
1429 * \brief Return number of biquadratic quadrangles in the mesh
1431 //================================================================================
1433 int SMESH_Mesh::NbBiQuadQuadrangles() const throw(SALOME_Exception)
1435 Unexpect aCatch(SalomeException);
1436 return _myMeshDS->GetMeshInfo().NbBiQuadQuadrangles();
1439 //================================================================================
1441 * \brief Return the number of polygonal faces in the mesh
1443 //================================================================================
1445 int SMESH_Mesh::NbPolygons() const throw(SALOME_Exception)
1447 Unexpect aCatch(SalomeException);
1448 return _myMeshDS->GetMeshInfo().NbPolygons();
1451 //================================================================================
1453 * \brief Return number of volumes of given order in the mesh
1455 //================================================================================
1457 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1459 Unexpect aCatch(SalomeException);
1460 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1463 //================================================================================
1465 * \brief Return number of tetrahedrons of given order in the mesh
1467 //================================================================================
1469 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1471 Unexpect aCatch(SalomeException);
1472 return _myMeshDS->GetMeshInfo().NbTetras(order);
1475 //================================================================================
1477 * \brief Return number of hexahedrons of given order in the mesh
1479 //================================================================================
1481 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1483 Unexpect aCatch(SalomeException);
1484 return _myMeshDS->GetMeshInfo().NbHexas(order);
1487 //================================================================================
1489 * \brief Return number of triquadratic hexahedrons in the mesh
1491 //================================================================================
1493 int SMESH_Mesh::NbTriQuadraticHexas() const throw(SALOME_Exception)
1495 Unexpect aCatch(SalomeException);
1496 return _myMeshDS->GetMeshInfo().NbTriQuadHexas();
1499 //================================================================================
1501 * \brief Return number of pyramids of given order in the mesh
1503 //================================================================================
1505 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1507 Unexpect aCatch(SalomeException);
1508 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1511 //================================================================================
1513 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1515 //================================================================================
1517 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1519 Unexpect aCatch(SalomeException);
1520 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1523 //================================================================================
1525 * \brief Return number of hexagonal prisms in the mesh
1527 //================================================================================
1529 int SMESH_Mesh::NbHexagonalPrisms() const throw(SALOME_Exception)
1531 Unexpect aCatch(SalomeException);
1532 return _myMeshDS->GetMeshInfo().NbHexPrisms();
1535 //================================================================================
1537 * \brief Return number of polyhedrons in the mesh
1539 //================================================================================
1541 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1543 Unexpect aCatch(SalomeException);
1544 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1547 //================================================================================
1549 * \brief Return number of submeshes in the mesh
1551 //================================================================================
1553 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1555 Unexpect aCatch(SalomeException);
1556 return _myMeshDS->NbSubMesh();
1559 //=======================================================================
1560 //function : IsNotConformAllowed
1561 //purpose : check if a hypothesis alowing notconform mesh is present
1562 //=======================================================================
1564 bool SMESH_Mesh::IsNotConformAllowed() const
1566 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1568 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1569 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
1572 //=======================================================================
1573 //function : IsMainShape
1575 //=======================================================================
1577 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1579 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1582 //=============================================================================
1586 //=============================================================================
1588 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1589 const char* theName,
1591 const TopoDS_Shape& theShape,
1592 const SMESH_PredicatePtr& thePredicate)
1594 if (_mapGroup.count(_groupId))
1597 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape, thePredicate);
1598 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1599 _mapGroup[_groupId++] = aGroup;
1603 //================================================================================
1605 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
1606 * \retval bool - true if new SMESH_Groups have been created
1609 //================================================================================
1611 bool SMESH_Mesh::SynchronizeGroups()
1613 int nbGroups = _mapGroup.size();
1614 const set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
1615 set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
1616 for ( ; gIt != groups.end(); ++gIt )
1618 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
1619 _groupId = groupDS->GetID();
1620 if ( !_mapGroup.count( _groupId ))
1621 _mapGroup[_groupId] = new SMESH_Group( groupDS );
1623 if ( !_mapGroup.empty() )
1624 _groupId = _mapGroup.rbegin()->first + 1;
1626 return nbGroups < _mapGroup.size();
1629 //================================================================================
1631 * \brief Return iterator on all existing groups
1633 //================================================================================
1635 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
1637 typedef map <int, SMESH_Group *> TMap;
1638 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
1641 //=============================================================================
1643 * \brief Return a group by ID
1645 //=============================================================================
1647 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
1649 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1651 return _mapGroup[theGroupID];
1655 //=============================================================================
1657 * \brief Return IDs of all groups
1659 //=============================================================================
1661 list<int> SMESH_Mesh::GetGroupIds() const
1664 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1665 anIds.push_back( it->first );
1670 //================================================================================
1672 * \brief Set a caller of RemoveGroup() at level of CORBA API implementation.
1673 * The set upCaller will be deleted by SMESH_Mesh
1675 //================================================================================
1677 void SMESH_Mesh::SetRemoveGroupCallUp( TRmGroupCallUp* upCaller )
1679 if ( _rmGroupCallUp ) delete _rmGroupCallUp;
1680 _rmGroupCallUp = upCaller;
1683 //=============================================================================
1687 //=============================================================================
1689 bool SMESH_Mesh::RemoveGroup (const int theGroupID)
1691 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1693 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1694 delete _mapGroup[theGroupID];
1695 _mapGroup.erase (theGroupID);
1697 _rmGroupCallUp->RemoveGroup( theGroupID );
1701 //=======================================================================
1702 //function : GetAncestors
1703 //purpose : return list of ancestors of theSubShape in the order
1704 // that lower dimention shapes come first.
1705 //=======================================================================
1707 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1709 if ( _mapAncestors.Contains( theS ) )
1710 return _mapAncestors.FindFromKey( theS );
1712 static TopTools_ListOfShape emptyList;
1716 //=======================================================================
1718 //purpose : dumps contents of mesh to stream [ debug purposes ]
1719 //=======================================================================
1721 ostream& SMESH_Mesh::Dump(ostream& save)
1724 save << "========================== Dump contents of mesh ==========================" << endl << endl;
1725 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
1726 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
1727 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
1728 save << ++clause << ") Total number of polygons:\t" << NbPolygons() << endl;
1729 save << ++clause << ") Total number of volumes:\t" << NbVolumes() << endl;
1730 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
1731 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
1733 string orderStr = isQuadratic ? "quadratic" : "linear";
1734 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
1736 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
1737 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
1738 if ( NbFaces(order) > 0 ) {
1739 int nb3 = NbTriangles(order);
1740 int nb4 = NbQuadrangles(order);
1741 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
1742 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
1743 if ( nb3 + nb4 != NbFaces(order) ) {
1744 map<int,int> myFaceMap;
1745 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1746 while( itFaces->more( ) ) {
1747 int nbNodes = itFaces->next()->NbNodes();
1748 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1749 myFaceMap[ nbNodes ] = 0;
1750 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1752 save << clause << ".3) Faces in detail: " << endl;
1753 map <int,int>::iterator itF;
1754 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1755 save << "--> nb nodes: " << itF->first << " - nb elemens:\t" << itF->second << endl;
1758 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
1759 if ( NbVolumes(order) > 0 ) {
1760 int nb8 = NbHexas(order);
1761 int nb4 = NbTetras(order);
1762 int nb5 = NbPyramids(order);
1763 int nb6 = NbPrisms(order);
1764 save << clause << ".1) Number of " << orderStr << " hexahedrons:\t" << nb8 << endl;
1765 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
1766 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
1767 save << clause << ".4) Number of " << orderStr << " pyramids:\t" << nb5 << endl;
1768 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
1769 map<int,int> myVolumesMap;
1770 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1771 while( itVolumes->more( ) ) {
1772 int nbNodes = itVolumes->next()->NbNodes();
1773 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1774 myVolumesMap[ nbNodes ] = 0;
1775 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1777 save << clause << ".5) Volumes in detail: " << endl;
1778 map <int,int>::iterator itV;
1779 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1780 save << "--> nb nodes: " << itV->first << " - nb elemens:\t" << itV->second << endl;
1785 save << "===========================================================================" << endl;
1789 //=======================================================================
1790 //function : GetElementType
1791 //purpose : Returns type of mesh element with certain id
1792 //=======================================================================
1794 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
1796 return _myMeshDS->GetElementType( id, iselem );
1799 //=============================================================================
1801 * \brief Convert group on geometry into standalone group
1803 //=============================================================================
1805 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
1807 SMESH_Group* aGroup = 0;
1808 map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
1809 if ( itg == _mapGroup.end() )
1812 SMESH_Group* anOldGrp = (*itg).second;
1813 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
1814 if ( !anOldGrp || !anOldGrpDS )
1817 // create new standalone group
1818 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
1819 _mapGroup[theGroupID] = aGroup;
1821 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
1822 GetMeshDS()->RemoveGroup( anOldGrpDS );
1823 GetMeshDS()->AddGroup( aNewGrpDS );
1825 // add elements (or nodes) into new created group
1826 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
1827 while ( anItr->more() )
1828 aNewGrpDS->Add( (anItr->next())->GetID() );
1831 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
1839 //=============================================================================
1841 * \brief remove submesh order from Mesh
1843 //=============================================================================
1845 void SMESH_Mesh::ClearMeshOrder()
1847 _mySubMeshOrder.clear();
1850 //=============================================================================
1852 * \brief remove submesh order from Mesh
1854 //=============================================================================
1856 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
1858 _mySubMeshOrder = theOrder;
1861 //=============================================================================
1863 * \brief return submesh order if any
1865 //=============================================================================
1867 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
1869 return _mySubMeshOrder;
1872 //=============================================================================
1874 * \brief fill _mapAncestors
1876 //=============================================================================
1878 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
1881 int desType, ancType;
1882 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
1884 // a geom group is added. Insert it into lists of ancestors before
1885 // the first ancestor more complex than group members
1886 int memberType = TopoDS_Iterator( theShape ).Value().ShapeType();
1887 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
1888 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
1890 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
1891 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
1892 TopTools_ListIteratorOfListOfShape ancIt (ancList);
1893 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
1896 ancList.InsertBefore( theShape, ancIt );
1900 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
1901 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
1902 TopExp::MapShapesAndAncestors ( theShape,
1903 (TopAbs_ShapeEnum) desType,
1904 (TopAbs_ShapeEnum) ancType,
1909 //=============================================================================
1911 * \brief sort submeshes according to stored mesh order
1912 * \param theListToSort in out list to be sorted
1913 * \return FALSE if nothing sorted
1915 //=============================================================================
1917 bool SMESH_Mesh::SortByMeshOrder(list<SMESH_subMesh*>& theListToSort) const
1919 if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
1923 list<SMESH_subMesh*> onlyOrderedList;
1924 // collect all ordered submeshes in one list as pointers
1925 // and get their positions within theListToSort
1926 typedef list<SMESH_subMesh*>::iterator TPosInList;
1927 map< int, TPosInList > sortedPos;
1928 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
1929 TListOfListOfInt::const_iterator listIddIt = _mySubMeshOrder.begin();
1930 for( ; listIddIt != _mySubMeshOrder.end(); listIddIt++) {
1931 const TListOfInt& listOfId = *listIddIt;
1932 TListOfInt::const_iterator idIt = listOfId.begin();
1933 for ( ; idIt != listOfId.end(); idIt++ ) {
1934 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt )) {
1935 TPosInList smPos = find( smBeg, smEnd, sm );
1936 if ( smPos != smEnd ) {
1937 onlyOrderedList.push_back( sm );
1938 sortedPos[ distance( smBeg, smPos )] = smPos;
1943 if (onlyOrderedList.size() < 2)
1947 list<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
1948 list<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
1950 // iterate on ordered submeshes and insert them in detected positions
1951 map< int, TPosInList >::iterator i_pos = sortedPos.begin();
1952 for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
1953 *(i_pos->second) = *onlyBIt;
1958 //=============================================================================
1960 * \brief sort submeshes according to stored mesh order
1961 * \param theListToSort in out list to be sorted
1962 * \return FALSE if nothing sorted
1964 //=============================================================================
1966 list<SMESH_subMesh*> SMESH_Mesh::getAncestorsSubMeshes
1967 (const TopoDS_Shape& theSubShape) const
1969 list<SMESH_subMesh*> listOfSubMesh;
1970 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
1971 for (; it.More(); it.Next() )
1972 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
1973 listOfSubMesh.push_back(sm);
1975 // sort submeshes according to stored mesh order
1976 SortByMeshOrder( listOfSubMesh );
1978 return listOfSubMesh;