1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 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
24 // File : SMESH_Mesh.cxx
25 // Author : Paul RASCLE, EDF
29 #include "SMESH_Mesh.hxx"
30 #include "SMESH_subMesh.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_Hypothesis.hxx"
33 #include "SMESH_Group.hxx"
34 #include "SMESH_HypoFilter.hxx"
35 #include "SMESHDS_Group.hxx"
36 #include "SMESHDS_Script.hxx"
37 #include "SMESHDS_GroupOnGeom.hxx"
38 #include "SMESHDS_Document.hxx"
39 #include "SMDS_MeshVolume.hxx"
40 #include "SMDS_SetIterator.hxx"
42 #include "utilities.h"
44 #include "DriverMED_W_SMESHDS_Mesh.h"
45 #include "DriverDAT_W_SMDS_Mesh.h"
46 #include "DriverUNV_W_SMDS_Mesh.h"
47 #include "DriverSTL_W_SMDS_Mesh.h"
49 #include "DriverMED_R_SMESHDS_Mesh.h"
50 #include "DriverUNV_R_SMDS_Mesh.h"
51 #include "DriverSTL_R_SMDS_Mesh.h"
53 #include <BRepPrimAPI_MakeBox.hxx>
55 #include <TopExp_Explorer.hxx>
56 #include <TopTools_ListIteratorOfListOfShape.hxx>
57 #include <TopTools_ListOfShape.hxx>
58 #include <TopTools_MapOfShape.hxx>
59 #include <TopoDS_Iterator.hxx>
61 #include "Utils_ExceptHandlers.hxx"
63 // maximum stored group name length in MED file
64 #define MAX_MED_GROUP_NAME_LENGTH 80
67 static int MYDEBUG = 0;
69 static int MYDEBUG = 0;
72 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
74 typedef SMESH_HypoFilter THypType;
76 //=============================================================================
80 //=============================================================================
82 SMESH_Mesh::SMESH_Mesh(int theLocalId,
85 bool theIsEmbeddedMode,
86 SMESHDS_Document* theDocument):
89 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
91 _studyId = theStudyId;
93 _myDocument = theDocument;
94 _idDoc = theDocument->NewMesh(theIsEmbeddedMode);
95 _myMeshDS = theDocument->GetMesh(_idDoc);
96 _isShapeToMesh = false;
98 _myMeshDS->ShapeToMesh( PseudoShape() );
101 //=============================================================================
105 //=============================================================================
107 SMESH_Mesh::~SMESH_Mesh()
109 INFOS("SMESH_Mesh::~SMESH_Mesh");
112 map < int, SMESH_Group * >::iterator itg;
113 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
114 SMESH_Group *aGroup = (*itg).second;
119 //=============================================================================
121 * \brief Set geometry to be meshed
123 //=============================================================================
125 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
127 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
129 if ( !aShape.IsNull() && _isShapeToMesh )
130 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
132 // clear current data
133 if ( !_myMeshDS->ShapeToMesh().IsNull() )
135 // removal of a shape to mesh, delete objects referring to sub-shapes:
137 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.begin();
138 for ( ; i_sm != _mapSubMesh.end(); ++i_sm )
141 // - groups on geometry
142 map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
143 while ( i_gr != _mapGroup.end() ) {
144 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
145 _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
147 _mapGroup.erase( i_gr++ );
152 _mapAncestors.Clear();
155 TopoDS_Shape aNullShape;
156 _myMeshDS->ShapeToMesh( aNullShape );
159 // set a new geometry
160 if ( !aShape.IsNull() )
162 _myMeshDS->ShapeToMesh(aShape);
163 _isShapeToMesh = true;
165 // fill _mapAncestors
166 int desType, ancType;
167 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
168 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
169 TopExp::MapShapesAndAncestors ( aShape,
170 (TopAbs_ShapeEnum) desType,
171 (TopAbs_ShapeEnum) ancType,
176 //=======================================================================
178 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
180 //=======================================================================
182 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
184 return _myMeshDS->ShapeToMesh();
187 //=======================================================================
189 * \brief Return a solid which is returned by GetShapeToMesh() if
190 * a real geometry to be meshed was not set
192 //=======================================================================
194 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
196 static TopoDS_Solid aSolid;
197 if ( aSolid.IsNull() )
199 aSolid = BRepPrimAPI_MakeBox(1,1,1);
204 //=======================================================================
205 //function : UNVToMesh
207 //=======================================================================
209 int SMESH_Mesh::UNVToMesh(const char* theFileName)
211 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
213 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
214 _isShapeToMesh = false;
215 DriverUNV_R_SMDS_Mesh myReader;
216 myReader.SetMesh(_myMeshDS);
217 myReader.SetFile(theFileName);
218 myReader.SetMeshId(-1);
221 MESSAGE("UNVToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
222 MESSAGE("UNVToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
223 MESSAGE("UNVToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
224 MESSAGE("UNVToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
226 SMDS_MeshGroup* aGroup = (SMDS_MeshGroup*) myReader.GetGroup();
228 TGroupNamesMap aGroupNames = myReader.GetGroupNamesMap();
229 //const TGroupIdMap& aGroupId = myReader.GetGroupIdMap();
230 aGroup->InitSubGroupsIterator();
231 while (aGroup->MoreSubGroups()) {
232 SMDS_MeshGroup* aSubGroup = (SMDS_MeshGroup*) aGroup->NextSubGroup();
233 std::string aName = aGroupNames[aSubGroup];
236 SMESH_Group* aSMESHGroup = AddGroup( aSubGroup->GetType(), aName.c_str(), aId );
238 if(MYDEBUG) MESSAGE("UNVToMesh - group added: "<<aName);
239 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aSMESHGroup->GetGroupDS() );
241 aGroupDS->SetStoreName(aName.c_str());
242 aSubGroup->InitIterator();
243 const SMDS_MeshElement* aElement = 0;
244 while (aSubGroup->More()) {
245 aElement = aSubGroup->Next();
247 aGroupDS->SMDSGroup().Add(aElement);
251 aGroupDS->SetType(aElement->GetType());
259 //=======================================================================
260 //function : MEDToMesh
262 //=======================================================================
264 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
266 if(MYDEBUG) MESSAGE("MEDToMesh - theFileName = "<<theFileName<<", mesh name = "<<theMeshName);
268 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
269 _isShapeToMesh = false;
270 DriverMED_R_SMESHDS_Mesh myReader;
271 myReader.SetMesh(_myMeshDS);
272 myReader.SetMeshId(-1);
273 myReader.SetFile(theFileName);
274 myReader.SetMeshName(theMeshName);
275 Driver_Mesh::Status status = myReader.Perform();
277 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
278 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
279 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
280 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
283 // Reading groups (sub-meshes are out of scope of MED import functionality)
284 list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
285 if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size());
287 list<TNameAndType>::iterator name_type = aGroupNames.begin();
288 for ( ; name_type != aGroupNames.end(); name_type++ ) {
289 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
291 if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<name_type->first.c_str());
292 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
294 aGroupDS->SetStoreName( name_type->first.c_str() );
295 myReader.GetGroup( aGroupDS );
302 //=======================================================================
303 //function : STLToMesh
305 //=======================================================================
307 int SMESH_Mesh::STLToMesh(const char* theFileName)
309 if(MYDEBUG) MESSAGE("STLToMesh - theFileName = "<<theFileName);
311 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
312 _isShapeToMesh = false;
313 DriverSTL_R_SMDS_Mesh myReader;
314 myReader.SetMesh(_myMeshDS);
315 myReader.SetFile(theFileName);
316 myReader.SetMeshId(-1);
319 MESSAGE("STLToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
320 MESSAGE("STLToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
321 MESSAGE("STLToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
322 MESSAGE("STLToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
327 //=============================================================================
331 //=============================================================================
333 SMESH_Hypothesis::Hypothesis_Status
334 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
335 int anHypId ) throw(SALOME_Exception)
337 Unexpect aCatch(SalomeException);
338 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
340 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
341 if ( !subMesh || !subMesh->GetId())
342 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
344 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
345 if ( subMeshDS && subMeshDS->IsComplexSubmesh() ) // group of sub-shapes and maybe of not sub-
347 MESSAGE("AddHypothesis() to complex submesh");
348 // return the worst but not fatal state of all group memebers
349 SMESH_Hypothesis::Hypothesis_Status aBestRet, aWorstNotFatal, ret;
350 aBestRet = SMESH_Hypothesis::HYP_BAD_DIM;
351 aWorstNotFatal = SMESH_Hypothesis::HYP_OK;
352 for ( TopoDS_Iterator itS ( aSubShape ); itS.More(); itS.Next())
354 if ( !GetMeshDS()->ShapeToIndex( itS.Value() ))
355 continue; // not sub-shape
356 ret = AddHypothesis( itS.Value(), anHypId );
357 if ( !SMESH_Hypothesis::IsStatusFatal( ret ) && ret > aWorstNotFatal )
358 aWorstNotFatal = ret;
359 if ( ret < aBestRet )
362 // bind hypotheses to a group just to know
363 SMESH_Hypothesis *anHyp = _gen->GetStudyContext(_studyId)->mapHypothesis[anHypId];
364 GetMeshDS()->AddHypothesis( aSubShape, anHyp );
366 if ( SMESH_Hypothesis::IsStatusFatal( aBestRet ))
368 return aWorstNotFatal;
371 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
372 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
374 if(MYDEBUG) MESSAGE("Hypothesis ID does not give an hypothesis");
379 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
382 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
383 MESSAGE( "SMESH_Mesh::AddHypothesis " << anHyp->GetName() );
385 bool isGlobalHyp = IsMainShape( aSubShape );
387 // NotConformAllowed can be only global
390 string hypName = anHyp->GetName();
391 if ( hypName == "NotConformAllowed" )
393 if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
394 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
400 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
401 int event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
403 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
406 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
407 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
409 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
411 SMESH_Hypothesis::Hypothesis_Status ret2 =
412 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
416 // check concurent hypotheses on ansestors
417 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
419 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
420 while ( smIt->more() ) {
421 SMESH_subMesh* sm = smIt->next();
422 if ( sm->IsApplicableHypotesis( anHyp )) {
423 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
433 if(MYDEBUG) subMesh->DumpAlgoState(true);
438 //=============================================================================
442 //=============================================================================
444 SMESH_Hypothesis::Hypothesis_Status
445 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
446 int anHypId)throw(SALOME_Exception)
448 Unexpect aCatch(SalomeException);
449 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
451 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
452 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
453 if ( subMeshDS && subMeshDS->IsComplexSubmesh() )
455 // return the worst but not fatal state of all group memebers
456 SMESH_Hypothesis::Hypothesis_Status aBestRet, aWorstNotFatal, ret;
457 aBestRet = SMESH_Hypothesis::HYP_BAD_DIM;
458 aWorstNotFatal = SMESH_Hypothesis::HYP_OK;
459 for ( TopoDS_Iterator itS ( aSubShape ); itS.More(); itS.Next())
461 if ( !GetMeshDS()->ShapeToIndex( itS.Value() ))
462 continue; // not sub-shape
463 ret = RemoveHypothesis( itS.Value(), anHypId );
464 if ( !SMESH_Hypothesis::IsStatusFatal( ret ) && ret > aWorstNotFatal )
465 aWorstNotFatal = ret;
466 if ( ret < aBestRet )
469 SMESH_Hypothesis *anHyp = _gen->GetStudyContext(_studyId)->mapHypothesis[anHypId];
470 GetMeshDS()->RemoveHypothesis( aSubShape, anHyp );
472 if ( SMESH_Hypothesis::IsStatusFatal( aBestRet ))
474 return aWorstNotFatal;
477 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
478 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
479 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
481 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
482 int hypType = anHyp->GetType();
483 if(MYDEBUG) SCRUTE(hypType);
487 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
488 int event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
490 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
492 // there may appear concurrent hyps that were covered by the removed hyp
493 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
494 subMesh->IsApplicableHypotesis( anHyp ) &&
495 subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
496 ret = SMESH_Hypothesis::HYP_CONCURENT;
499 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
500 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
502 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
504 SMESH_Hypothesis::Hypothesis_Status ret2 =
505 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
506 if (ret2 > ret) // more severe
509 // check concurent hypotheses on ansestors
510 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
512 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
513 while ( smIt->more() ) {
514 SMESH_subMesh* sm = smIt->next();
515 if ( sm->IsApplicableHypotesis( anHyp )) {
516 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
526 if(MYDEBUG) subMesh->DumpAlgoState(true);
527 if(MYDEBUG) SCRUTE(ret);
531 //=============================================================================
535 //=============================================================================
537 const list<const SMESHDS_Hypothesis*>&
538 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
539 throw(SALOME_Exception)
541 Unexpect aCatch(SalomeException);
542 return _myMeshDS->GetHypothesis(aSubShape);
545 //=======================================================================
547 * \brief Return the hypothesis assigned to the shape
548 * \param aSubShape - the shape to check
549 * \param aFilter - the hypothesis filter
550 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
551 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
553 //=======================================================================
555 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
556 const SMESH_HypoFilter& aFilter,
557 const bool andAncestors) const
560 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
561 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
562 for ( ; hyp != hypList.end(); hyp++ ) {
563 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
564 if ( aFilter.IsOk( h, aSubShape))
570 TopTools_ListIteratorOfListOfShape it( GetAncestors( aSubShape ));
571 for (; it.More(); it.Next() )
573 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(it.Value());
574 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
575 for ( ; hyp != hypList.end(); hyp++ ) {
576 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
577 if (aFilter.IsOk( h, it.Value() ))
585 //================================================================================
587 * \brief Return hypothesis assigned to the shape
588 * \param aSubShape - the shape to check
589 * \param aFilter - the hypothesis filter
590 * \param aHypList - the list of the found hypotheses
591 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
592 * \retval int - number of unique hypos in aHypList
594 //================================================================================
596 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
597 const SMESH_HypoFilter& aFilter,
598 list <const SMESHDS_Hypothesis * >& aHypList,
599 const bool andAncestors) const
601 set<string> hypTypes; // to exclude same type hypos from the result list
604 // only one main hypothesis is allowed
605 bool mainHypFound = false;
608 list<const SMESHDS_Hypothesis*>::const_iterator hyp;
609 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
610 if ( hypTypes.insert( (*hyp)->GetName() ).second )
612 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
616 // get hypos from aSubShape
618 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
619 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
620 if ( aFilter.IsOk (cSMESH_Hyp( *hyp ), aSubShape) &&
621 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
622 hypTypes.insert( (*hyp)->GetName() ).second )
624 aHypList.push_back( *hyp );
626 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
631 // get hypos from ancestors of aSubShape
634 TopTools_MapOfShape map;
635 TopTools_ListIteratorOfListOfShape it( GetAncestors( aSubShape ));
636 for (; it.More(); it.Next() )
638 if ( !map.Add( it.Value() ))
640 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(it.Value());
641 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
642 if (aFilter.IsOk( cSMESH_Hyp( *hyp ), it.Value() ) &&
643 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
644 hypTypes.insert( (*hyp)->GetName() ).second )
646 aHypList.push_back( *hyp );
648 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
656 //=============================================================================
660 //=============================================================================
662 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
664 Unexpect aCatch(SalomeException);
665 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
666 return _myMeshDS->GetScript()->GetCommands();
669 //=============================================================================
673 //=============================================================================
674 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
676 Unexpect aCatch(SalomeException);
677 if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
678 _myMeshDS->GetScript()->Clear();
681 //=============================================================================
683 * Get or Create the SMESH_subMesh object implementation
685 //=============================================================================
687 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
688 throw(SALOME_Exception)
690 Unexpect aCatch(SalomeException);
691 SMESH_subMesh *aSubMesh;
692 int index = _myMeshDS->ShapeToIndex(aSubShape);
694 // for submeshes on GEOM Group
695 if ( !index && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
696 TopoDS_Iterator it( aSubShape );
698 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
701 // return NULL; // neither sub-shape nor a group
703 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.find(index);
704 if ( i_sm != _mapSubMesh.end())
706 aSubMesh = i_sm->second;
710 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
711 _mapSubMesh[index] = aSubMesh;
716 //=============================================================================
718 * Get the SMESH_subMesh object implementation. Dont create it, return null
719 * if it does not exist.
721 //=============================================================================
723 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
724 throw(SALOME_Exception)
726 Unexpect aCatch(SalomeException);
727 SMESH_subMesh *aSubMesh = NULL;
729 int index = _myMeshDS->ShapeToIndex(aSubShape);
731 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(index);
732 if ( i_sm != _mapSubMesh.end())
733 aSubMesh = i_sm->second;
737 //=============================================================================
739 * Get the SMESH_subMesh object implementation. Dont create it, return null
740 * if it does not exist.
742 //=============================================================================
744 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
745 throw(SALOME_Exception)
747 Unexpect aCatch(SalomeException);
749 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(aShapeID);
750 if (i_sm == _mapSubMesh.end())
754 //================================================================================
756 * \brief Return submeshes of groups containing the given subshape
758 //================================================================================
761 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
762 throw(SALOME_Exception)
764 Unexpect aCatch(SalomeException);
765 list<SMESH_subMesh*> found;
767 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
771 // submeshes of groups have max IDs, so search from the map end
772 map<int, SMESH_subMesh *>::const_reverse_iterator i_sm;
773 for ( i_sm = _mapSubMesh.rbegin(); i_sm != _mapSubMesh.rend(); ++i_sm) {
774 SMESHDS_SubMesh * ds = i_sm->second->GetSubMeshDS();
775 if ( ds && ds->IsComplexSubmesh() ) {
776 TopExp_Explorer exp( i_sm->second->GetSubShape(), aSubShape.ShapeType() );
777 for ( ; exp.More(); exp.Next() ) {
778 if ( aSubShape.IsSame( exp.Current() )) {
779 found.push_back( i_sm->second );
789 //=======================================================================
790 //function : IsUsedHypothesis
791 //purpose : Return True if anHyp is used to mesh aSubShape
792 //=======================================================================
794 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
795 const SMESH_subMesh* aSubMesh)
797 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
799 // check if anHyp can be used to mesh aSubMesh
800 if ( !aSubMesh || !aSubMesh->IsApplicableHypotesis( hyp ))
803 const TopoDS_Shape & aSubShape = const_cast<SMESH_subMesh*>( aSubMesh )->GetSubShape();
805 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape );
808 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
809 return ( anHyp == algo );
811 // algorithm parameter
814 // look trough hypotheses used by algo
815 SMESH_HypoFilter hypoKind;
816 if ( algo->InitCompatibleHypoFilter( hypoKind, !hyp->IsAuxiliary() )) {
817 list <const SMESHDS_Hypothesis * > usedHyps;
818 if ( GetHypotheses( aSubShape, hypoKind, usedHyps, true ))
819 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
823 // look through all assigned hypotheses
824 //SMESH_HypoFilter filter( SMESH_HypoFilter::Is( hyp ));
825 return false; //GetHypothesis( aSubShape, filter, true );
828 //=============================================================================
832 //=============================================================================
834 const list < SMESH_subMesh * >&
835 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
836 throw(SALOME_Exception)
838 Unexpect aCatch(SalomeException);
839 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
840 map < int, SMESH_subMesh * >::iterator itsm;
841 _subMeshesUsingHypothesisList.clear();
842 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
844 SMESH_subMesh *aSubMesh = (*itsm).second;
845 if ( IsUsedHypothesis ( anHyp, aSubMesh ))
846 _subMeshesUsingHypothesisList.push_back(aSubMesh);
848 return _subMeshesUsingHypothesisList;
851 //=======================================================================
852 //function : NotifySubMeshesHypothesisModification
853 //purpose : Say all submeshes using theChangedHyp that it has been modified
854 //=======================================================================
856 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
858 Unexpect aCatch(SalomeException);
860 const SMESH_Algo *foundAlgo = 0;
861 SMESH_HypoFilter algoKind, compatibleHypoKind;
862 list <const SMESHDS_Hypothesis * > usedHyps;
865 map < int, SMESH_subMesh * >::iterator itsm;
866 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
868 SMESH_subMesh *aSubMesh = (*itsm).second;
869 if ( aSubMesh->IsApplicableHypotesis( hyp ))
871 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
873 if ( !foundAlgo ) // init filter for algo search
874 algoKind.Init( THypType::IsAlgo() ).And( THypType::IsApplicableTo( aSubShape ));
876 const SMESH_Algo *algo = static_cast<const SMESH_Algo*>
877 ( GetHypothesis( aSubShape, algoKind, true ));
881 bool sameAlgo = ( algo == foundAlgo );
882 if ( !sameAlgo && foundAlgo )
883 sameAlgo = ( strcmp( algo->GetName(), foundAlgo->GetName() ) == 0);
885 if ( !sameAlgo ) { // init filter for used hypos search
886 if ( !algo->InitCompatibleHypoFilter( compatibleHypoKind, !hyp->IsAuxiliary() ))
887 continue; // algo does not use any hypothesis
891 // check if hyp is used by algo
893 if ( GetHypotheses( aSubShape, compatibleHypoKind, usedHyps, true ) &&
894 find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
896 aSubMesh->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
897 const_cast< SMESH_Hypothesis*>( hyp ));
904 //=============================================================================
906 * Auto color functionality
908 //=============================================================================
909 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
911 Unexpect aCatch(SalomeException);
912 _isAutoColor = theAutoColor;
915 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
917 Unexpect aCatch(SalomeException);
921 //=============================================================================
923 * To store mesh contents on disk in different formats.
925 //=============================================================================
927 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
929 set<string> aGroupNames;
930 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
931 SMESH_Group* aGroup = it->second;
932 string aGroupName = aGroup->GetName();
933 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
934 if (!aGroupNames.insert(aGroupName).second)
941 void SMESH_Mesh::ExportMED(const char *file,
942 const char* theMeshName,
945 throw(SALOME_Exception)
947 Unexpect aCatch(SalomeException);
949 DriverMED_W_SMESHDS_Mesh myWriter;
950 myWriter.SetFile ( file, MED::EVersion(theVersion) );
951 myWriter.SetMesh ( _myMeshDS );
953 myWriter.SetMeshId ( _idDoc );
955 myWriter.SetMeshId ( -1 );
956 myWriter.SetMeshName( theMeshName );
959 if ( theAutoGroups ) {
960 myWriter.AddGroupOfNodes();
961 myWriter.AddGroupOfEdges();
962 myWriter.AddGroupOfFaces();
963 myWriter.AddGroupOfVolumes();
966 // Pass groups to writer. Provide unique group names.
967 set<string> aGroupNames;
969 int maxNbIter = 10000; // to guarantee cycle finish
970 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
971 SMESH_Group* aGroup = it->second;
972 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
974 string aGroupName0 = aGroup->GetName();
975 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
976 string aGroupName = aGroupName0;
977 for (int i = 1; !aGroupNames.insert(aGroupName).second && i < maxNbIter; i++) {
978 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
979 aGroupName = aString;
980 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
982 aGroupDS->SetStoreName( aGroupName.c_str() );
983 myWriter.AddGroup( aGroupDS );
991 void SMESH_Mesh::ExportDAT(const char *file) throw(SALOME_Exception)
993 Unexpect aCatch(SalomeException);
994 DriverDAT_W_SMDS_Mesh myWriter;
995 myWriter.SetFile(string(file));
996 myWriter.SetMesh(_myMeshDS);
997 myWriter.SetMeshId(_idDoc);
1001 void SMESH_Mesh::ExportUNV(const char *file) throw(SALOME_Exception)
1003 Unexpect aCatch(SalomeException);
1004 DriverUNV_W_SMDS_Mesh myWriter;
1005 myWriter.SetFile(string(file));
1006 myWriter.SetMesh(_myMeshDS);
1007 myWriter.SetMeshId(_idDoc);
1008 // myWriter.SetGroups(_mapGroup);
1010 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1011 SMESH_Group* aGroup = it->second;
1012 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1014 string aGroupName = aGroup->GetName();
1015 aGroupDS->SetStoreName( aGroupName.c_str() );
1016 myWriter.AddGroup( aGroupDS );
1022 void SMESH_Mesh::ExportSTL(const char *file, const bool isascii) throw(SALOME_Exception)
1024 Unexpect aCatch(SalomeException);
1025 DriverSTL_W_SMDS_Mesh myWriter;
1026 myWriter.SetFile(string(file));
1027 myWriter.SetIsAscii( isascii );
1028 myWriter.SetMesh(_myMeshDS);
1029 myWriter.SetMeshId(_idDoc);
1033 //================================================================================
1035 * \brief Return number of nodes in the mesh
1037 //================================================================================
1039 int SMESH_Mesh::NbNodes() throw(SALOME_Exception)
1041 Unexpect aCatch(SalomeException);
1042 return _myMeshDS->NbNodes();
1045 //================================================================================
1047 * \brief Return number of edges of given order in the mesh
1049 //================================================================================
1051 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) throw(SALOME_Exception)
1053 Unexpect aCatch(SalomeException);
1054 return _myMeshDS->GetMeshInfo().NbEdges(order);
1057 //================================================================================
1059 * \brief Return number of faces of given order in the mesh
1061 //================================================================================
1063 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) throw(SALOME_Exception)
1065 Unexpect aCatch(SalomeException);
1066 return _myMeshDS->GetMeshInfo().NbFaces(order);
1069 //================================================================================
1071 * \brief Return the number of faces in the mesh
1073 //================================================================================
1075 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) throw(SALOME_Exception)
1077 Unexpect aCatch(SalomeException);
1078 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1081 //================================================================================
1083 * \brief Return the number nodes faces in the mesh
1085 //================================================================================
1087 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) throw(SALOME_Exception)
1089 Unexpect aCatch(SalomeException);
1090 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1093 //================================================================================
1095 * \brief Return the number of polygonal faces in the mesh
1097 //================================================================================
1099 int SMESH_Mesh::NbPolygons() throw(SALOME_Exception)
1101 Unexpect aCatch(SalomeException);
1102 return _myMeshDS->GetMeshInfo().NbPolygons();
1105 //================================================================================
1107 * \brief Return number of volumes of given order in the mesh
1109 //================================================================================
1111 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) throw(SALOME_Exception)
1113 Unexpect aCatch(SalomeException);
1114 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1117 //================================================================================
1119 * \brief Return number of tetrahedrons of given order in the mesh
1121 //================================================================================
1123 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) throw(SALOME_Exception)
1125 Unexpect aCatch(SalomeException);
1126 return _myMeshDS->GetMeshInfo().NbTetras(order);
1129 //================================================================================
1131 * \brief Return number of hexahedrons of given order in the mesh
1133 //================================================================================
1135 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) throw(SALOME_Exception)
1137 Unexpect aCatch(SalomeException);
1138 return _myMeshDS->GetMeshInfo().NbHexas(order);
1141 //================================================================================
1143 * \brief Return number of pyramids of given order in the mesh
1145 //================================================================================
1147 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) throw(SALOME_Exception)
1149 Unexpect aCatch(SalomeException);
1150 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1153 //================================================================================
1155 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1157 //================================================================================
1159 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) throw(SALOME_Exception)
1161 Unexpect aCatch(SalomeException);
1162 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1165 //================================================================================
1167 * \brief Return number of polyhedrons in the mesh
1169 //================================================================================
1171 int SMESH_Mesh::NbPolyhedrons() throw(SALOME_Exception)
1173 Unexpect aCatch(SalomeException);
1174 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1177 //================================================================================
1179 * \brief Return number of submeshes in the mesh
1181 //================================================================================
1183 int SMESH_Mesh::NbSubMesh() throw(SALOME_Exception)
1185 Unexpect aCatch(SalomeException);
1186 return _myMeshDS->NbSubMesh();
1189 //=======================================================================
1190 //function : IsNotConformAllowed
1191 //purpose : check if a hypothesis alowing notconform mesh is present
1192 //=======================================================================
1194 bool SMESH_Mesh::IsNotConformAllowed() const
1196 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1198 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1199 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
1202 //=======================================================================
1203 //function : IsMainShape
1205 //=======================================================================
1207 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1209 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1212 //=============================================================================
1216 //=============================================================================
1218 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1219 const char* theName,
1221 const TopoDS_Shape& theShape)
1223 if (_mapGroup.find(_groupId) != _mapGroup.end())
1226 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape);
1227 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1228 _mapGroup[_groupId++] = aGroup;
1232 //================================================================================
1234 * \brief Return iterator on all existing groups
1236 //================================================================================
1238 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
1240 typedef map <int, SMESH_Group *> TMap;
1241 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
1244 //=============================================================================
1246 * \brief Return a group by ID
1248 //=============================================================================
1250 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
1252 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1254 return _mapGroup[theGroupID];
1258 //=============================================================================
1260 * \brief Return IDs of all groups
1262 //=============================================================================
1264 list<int> SMESH_Mesh::GetGroupIds() const
1267 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1268 anIds.push_back( it->first );
1274 //=============================================================================
1278 //=============================================================================
1280 void SMESH_Mesh::RemoveGroup (const int theGroupID)
1282 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1284 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1285 delete _mapGroup[theGroupID];
1286 _mapGroup.erase (theGroupID);
1289 //=======================================================================
1290 //function : GetAncestors
1291 //purpose : return list of ancestors of theSubShape in the order
1292 // that lower dimention shapes come first.
1293 //=======================================================================
1295 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1297 if ( _mapAncestors.Contains( theS ) )
1298 return _mapAncestors.FindFromKey( theS );
1300 static TopTools_ListOfShape emptyList;
1304 //=======================================================================
1306 //purpose : dumps contents of mesh to stream [ debug purposes ]
1307 //=======================================================================
1309 ostream& SMESH_Mesh::Dump(ostream& save)
1312 save << "========================== Dump contents of mesh ==========================" << endl << endl;
1313 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
1314 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
1315 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
1316 save << ++clause << ") Total number of polygons:\t" << NbPolygons() << endl;
1317 save << ++clause << ") Total number of volumes:\t" << NbVolumes() << endl;
1318 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
1319 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
1321 string orderStr = isQuadratic ? "quadratic" : "linear";
1322 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
1324 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
1325 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
1326 if ( NbFaces(order) > 0 ) {
1327 int nb3 = NbTriangles(order);
1328 int nb4 = NbQuadrangles(order);
1329 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
1330 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
1331 if ( nb3 + nb4 != NbFaces(order) ) {
1332 map<int,int> myFaceMap;
1333 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1334 while( itFaces->more( ) ) {
1335 int nbNodes = itFaces->next()->NbNodes();
1336 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1337 myFaceMap[ nbNodes ] = 0;
1338 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1340 save << clause << ".3) Faces in detail: " << endl;
1341 map <int,int>::iterator itF;
1342 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1343 save << "--> nb nodes: " << itF->first << " - nb elemens:\t" << itF->second << endl;
1346 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
1347 if ( NbVolumes(order) > 0 ) {
1348 int nb8 = NbHexas(order);
1349 int nb4 = NbTetras(order);
1350 int nb5 = NbPyramids(order);
1351 int nb6 = NbPrisms(order);
1352 save << clause << ".1) Number of " << orderStr << " hexahedrons:\t" << nb8 << endl;
1353 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
1354 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
1355 save << clause << ".4) Number of " << orderStr << " pyramids:\t" << nb5 << endl;
1356 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
1357 map<int,int> myVolumesMap;
1358 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1359 while( itVolumes->more( ) ) {
1360 int nbNodes = itVolumes->next()->NbNodes();
1361 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1362 myVolumesMap[ nbNodes ] = 0;
1363 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1365 save << clause << ".5) Volumes in detail: " << endl;
1366 map <int,int>::iterator itV;
1367 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1368 save << "--> nb nodes: " << itV->first << " - nb elemens:\t" << itV->second << endl;
1373 save << "===========================================================================" << endl;
1377 //=======================================================================
1378 //function : GetElementType
1379 //purpose : Returns type of mesh element with certain id
1380 //=======================================================================
1382 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
1384 return _myMeshDS->GetElementType( id, iselem );