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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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 "SMDS_MeshVolume.hxx"
40 #include "utilities.h"
42 #include "DriverMED_W_SMESHDS_Mesh.h"
43 #include "DriverDAT_W_SMDS_Mesh.h"
44 #include "DriverUNV_W_SMDS_Mesh.h"
45 #include "DriverSTL_W_SMDS_Mesh.h"
47 #include "DriverMED_R_SMESHDS_Mesh.h"
48 #include "DriverUNV_R_SMDS_Mesh.h"
49 #include "DriverSTL_R_SMDS_Mesh.h"
51 #include <BRepTools_WireExplorer.hxx>
52 #include <BRep_Builder.hxx>
55 #include <TCollection_AsciiString.hxx>
57 #include <TopTools_ListOfShape.hxx>
58 #include <TopTools_Array1OfShape.hxx>
59 #include <TopTools_ListIteratorOfListOfShape.hxx>
60 #include <TopTools_MapOfShape.hxx>
64 #include "Utils_ExceptHandlers.hxx"
67 static int MYDEBUG = 0;
69 static int MYDEBUG = 0;
73 //=============================================================================
77 //=============================================================================
79 SMESH_Mesh::SMESH_Mesh(int localId, int studyId, SMESH_Gen * gen, SMESHDS_Document * myDocument)
82 INFOS("SMESH_Mesh::SMESH_Mesh(int localId)");
86 _myDocument = myDocument;
87 _idDoc = _myDocument->NewMesh();
88 _myMeshDS = _myDocument->GetMesh(_idDoc);
89 _isShapeToMesh = false;
92 //=============================================================================
96 //=============================================================================
98 SMESH_Mesh::~SMESH_Mesh()
100 INFOS("SMESH_Mesh::~SMESH_Mesh");
103 map < int, SMESH_Group * >::iterator itg;
104 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
105 SMESH_Group *aGroup = (*itg).second;
110 //=============================================================================
114 //=============================================================================
116 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
118 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
120 if ( !_myMeshDS->ShapeToMesh().IsNull() && aShape.IsNull() )
122 // removal of a shape to mesh, delete objects referring to sub-shapes:
124 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.begin();
125 for ( ; i_sm != _mapSubMesh.end(); ++i_sm )
128 // - groups on geometry
129 map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
130 while ( i_gr != _mapGroup.end() ) {
131 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
132 _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
134 _mapGroup.erase( i_gr++ );
139 _mapPropagationChains.Clear();
144 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
146 _isShapeToMesh = true;
147 _myMeshDS->ShapeToMesh(aShape);
149 // fill _mapAncestors
150 _mapAncestors.Clear();
151 int desType, ancType;
152 for ( desType = TopAbs_EDGE; desType > TopAbs_COMPOUND; desType-- )
153 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
154 TopExp::MapShapesAndAncestors ( aShape,
155 (TopAbs_ShapeEnum) desType,
156 (TopAbs_ShapeEnum) ancType,
160 //EAP: 1/9/04 TopExp::MapShapes(aShape, _subShapes); USE the same map of _myMeshDS
163 //=======================================================================
164 //function : UNVToMesh
166 //=======================================================================
168 int SMESH_Mesh::UNVToMesh(const char* theFileName)
170 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
172 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
173 _isShapeToMesh = true;
174 DriverUNV_R_SMDS_Mesh myReader;
175 myReader.SetMesh(_myMeshDS);
176 myReader.SetFile(theFileName);
177 myReader.SetMeshId(-1);
180 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
181 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
182 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
183 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
188 //=======================================================================
189 //function : MEDToMesh
191 //=======================================================================
193 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
195 if(MYDEBUG) MESSAGE("MEDToMesh - theFileName = "<<theFileName<<", mesh name = "<<theMeshName);
197 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
198 _isShapeToMesh = true;
199 DriverMED_R_SMESHDS_Mesh myReader;
200 myReader.SetMesh(_myMeshDS);
201 myReader.SetMeshId(-1);
202 myReader.SetFile(theFileName);
203 myReader.SetMeshName(theMeshName);
204 Driver_Mesh::Status status = myReader.Perform();
206 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
207 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
208 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
209 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
212 // Reading groups (sub-meshes are out of scope of MED import functionality)
213 list<string> aGroupNames = myReader.GetGroupNames();
214 if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size());
216 for ( list<string>::iterator it = aGroupNames.begin(); it != aGroupNames.end(); it++ ) {
217 SMESH_Group* aGroup = AddGroup( SMDSAbs_All, it->c_str(), anId );
219 if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<it->c_str());
220 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
222 aGroupDS->SetStoreName( it->c_str() );
223 myReader.GetGroup( aGroupDS );
230 //=======================================================================
231 //function : STLToMesh
233 //=======================================================================
235 int SMESH_Mesh::STLToMesh(const char* theFileName)
237 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
239 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
240 _isShapeToMesh = true;
241 DriverSTL_R_SMDS_Mesh myReader;
242 myReader.SetMesh(_myMeshDS);
243 myReader.SetFile(theFileName);
244 myReader.SetMeshId(-1);
247 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
248 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
249 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
250 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
255 //=============================================================================
259 //=============================================================================
261 SMESH_Hypothesis::Hypothesis_Status
262 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
263 int anHypId ) throw(SALOME_Exception)
265 Unexpect aCatch(SalomeException);
266 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
268 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
269 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
270 if ( subMeshDS && subMeshDS->IsComplexSubmesh() )
272 // return the worst but not fatal state of all group memebers
273 SMESH_Hypothesis::Hypothesis_Status aBestRet, aWorstNotFatal, ret;
274 aBestRet = SMESH_Hypothesis::HYP_BAD_DIM;
275 aWorstNotFatal = SMESH_Hypothesis::HYP_OK;
276 for ( TopoDS_Iterator itS ( aSubShape ); itS.More(); itS.Next())
278 ret = AddHypothesis( itS.Value(), anHypId );
279 if ( !SMESH_Hypothesis::IsStatusFatal( ret ) && ret > aWorstNotFatal )
280 aWorstNotFatal = ret;
281 if ( ret < aBestRet )
284 if ( SMESH_Hypothesis::IsStatusFatal( aBestRet ))
286 return aWorstNotFatal;
289 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
290 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
292 if(MYDEBUG) MESSAGE("Hypothesis ID does not give an hypothesis");
297 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
300 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
301 MESSAGE( "SMESH_Mesh::AddHypothesis " << anHyp->GetName() );
303 bool isGlobalHyp = IsMainShape( aSubShape );
305 // NotConformAllowed can be only global
308 string hypName = anHyp->GetName();
309 if ( hypName == "NotConformAllowed" )
311 if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
312 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
319 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
320 event = SMESH_subMesh::ADD_HYP;
322 event = SMESH_subMesh::ADD_ALGO;
323 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
326 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
327 !subMesh->IsApplicableHypotesis( anHyp )) // is added on father
329 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
330 event = SMESH_subMesh::ADD_FATHER_HYP;
332 event = SMESH_subMesh::ADD_FATHER_ALGO;
333 SMESH_Hypothesis::Hypothesis_Status ret2 =
334 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
338 // check concurent hypotheses on ansestors
339 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
341 const map < int, SMESH_subMesh * >& smMap = subMesh->DependsOn();
342 map < int, SMESH_subMesh * >::const_iterator smIt = smMap.begin();
343 for ( ; smIt != smMap.end(); smIt++ ) {
344 if ( smIt->second->IsApplicableHypotesis( anHyp )) {
345 ret2 = smIt->second->CheckConcurentHypothesis( anHyp->GetType() );
355 if(MYDEBUG) subMesh->DumpAlgoState(true);
360 //=============================================================================
364 //=============================================================================
366 SMESH_Hypothesis::Hypothesis_Status
367 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
368 int anHypId)throw(SALOME_Exception)
370 Unexpect aCatch(SalomeException);
371 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
373 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
374 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
375 if ( subMeshDS && subMeshDS->IsComplexSubmesh() )
377 // return the worst but not fatal state of all group memebers
378 SMESH_Hypothesis::Hypothesis_Status aBestRet, aWorstNotFatal, ret;
379 aBestRet = SMESH_Hypothesis::HYP_BAD_DIM;
380 aWorstNotFatal = SMESH_Hypothesis::HYP_OK;
381 for ( TopoDS_Iterator itS ( aSubShape ); itS.More(); itS.Next())
383 ret = RemoveHypothesis( itS.Value(), anHypId );
384 if ( !SMESH_Hypothesis::IsStatusFatal( ret ) && ret > aWorstNotFatal )
385 aWorstNotFatal = ret;
386 if ( ret < aBestRet )
389 if ( SMESH_Hypothesis::IsStatusFatal( aBestRet ))
391 return aWorstNotFatal;
394 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
395 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
396 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
398 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
399 int hypType = anHyp->GetType();
400 if(MYDEBUG) SCRUTE(hypType);
405 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
406 event = SMESH_subMesh::REMOVE_HYP;
408 event = SMESH_subMesh::REMOVE_ALGO;
409 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
411 // there may appear concurrent hyps that were covered by the removed hyp
412 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
413 subMesh->IsApplicableHypotesis( anHyp ) &&
414 subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
415 ret = SMESH_Hypothesis::HYP_CONCURENT;
418 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
419 !subMesh->IsApplicableHypotesis( anHyp )) // is removed from father
421 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
422 event = SMESH_subMesh::REMOVE_FATHER_HYP;
424 event = SMESH_subMesh::REMOVE_FATHER_ALGO;
425 SMESH_Hypothesis::Hypothesis_Status ret2 =
426 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
427 if (ret2 > ret) // more severe
430 // check concurent hypotheses on ansestors
431 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
433 const map < int, SMESH_subMesh * >& smMap = subMesh->DependsOn();
434 map < int, SMESH_subMesh * >::const_iterator smIt = smMap.begin();
435 for ( ; smIt != smMap.end(); smIt++ ) {
436 if ( smIt->second->IsApplicableHypotesis( anHyp )) {
437 ret2 = smIt->second->CheckConcurentHypothesis( anHyp->GetType() );
447 if(MYDEBUG) subMesh->DumpAlgoState(true);
448 if(MYDEBUG) SCRUTE(ret);
452 //=============================================================================
456 //=============================================================================
458 SMESHDS_Mesh * SMESH_Mesh::GetMeshDS()
463 //=============================================================================
467 //=============================================================================
469 const list<const SMESHDS_Hypothesis*>&
470 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
471 throw(SALOME_Exception)
473 Unexpect aCatch(SalomeException);
474 return _myMeshDS->GetHypothesis(aSubShape);
477 //=======================================================================
478 //function : GetHypothesis
480 //=======================================================================
482 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
483 const SMESH_HypoFilter& aFilter,
484 const bool andAncestors) const
487 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
488 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
489 for ( ; hyp != hypList.end(); hyp++ ) {
490 const SMESH_Hypothesis * h = static_cast<const SMESH_Hypothesis*>( *hyp );
491 if ( aFilter.IsOk( h, aSubShape))
497 TopTools_ListIteratorOfListOfShape it( GetAncestors( aSubShape ));
498 for (; it.More(); it.Next() )
500 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(it.Value());
501 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
502 for ( ; hyp != hypList.end(); hyp++ ) {
503 const SMESH_Hypothesis * h = static_cast<const SMESH_Hypothesis*>( *hyp );
504 if (aFilter.IsOk( h, it.Value() ))
512 //=======================================================================
513 //function : GetHypotheses
515 //=======================================================================
517 bool SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
518 const SMESH_HypoFilter& aFilter,
519 list <const SMESHDS_Hypothesis * >& aHypList,
520 const bool andAncestors) const
524 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
525 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
526 for ( ; hyp != hypList.end(); hyp++ )
527 if ( aFilter.IsOk (static_cast<const SMESH_Hypothesis*>( *hyp ), aSubShape)) {
528 aHypList.push_back( *hyp );
532 // get hypos from shape of one type only: if any hypo is found on edge, do
533 // not look up on faces
534 if ( !nbHyp && andAncestors )
536 TopTools_MapOfShape map;
537 TopTools_ListIteratorOfListOfShape it( GetAncestors( aSubShape ));
538 int shapeType = it.More() ? it.Value().ShapeType() : TopAbs_SHAPE;
539 for (; it.More(); it.Next() )
541 if ( nbHyp && shapeType != it.Value().ShapeType() )
543 shapeType = it.Value().ShapeType();
544 if ( !map.Add( it.Value() ))
546 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(it.Value());
547 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
548 for ( ; hyp != hypList.end(); hyp++ )
549 if (aFilter.IsOk( static_cast<const SMESH_Hypothesis*>( *hyp ), it.Value() )) {
550 aHypList.push_back( *hyp );
558 //=============================================================================
562 //=============================================================================
564 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
566 Unexpect aCatch(SalomeException);
567 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
568 return _myMeshDS->GetScript()->GetCommands();
571 //=============================================================================
575 //=============================================================================
576 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
578 Unexpect aCatch(SalomeException);
579 if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
580 _myMeshDS->GetScript()->Clear();
583 //=============================================================================
587 //=============================================================================
589 int SMESH_Mesh::GetId()
591 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetId");
595 //=============================================================================
599 //=============================================================================
601 SMESH_Gen *SMESH_Mesh::GetGen()
606 //=============================================================================
608 * Get or Create the SMESH_subMesh object implementation
610 //=============================================================================
612 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
613 throw(SALOME_Exception)
615 Unexpect aCatch(SalomeException);
616 SMESH_subMesh *aSubMesh;
617 int index = _myMeshDS->ShapeToIndex(aSubShape);
619 // for submeshes on GEOM Group
620 if ( !index && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
621 TopoDS_Iterator it( aSubShape );
623 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
626 if (_mapSubMesh.find(index) != _mapSubMesh.end())
628 aSubMesh = _mapSubMesh[index];
632 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
633 _mapSubMesh[index] = aSubMesh;
638 //=============================================================================
640 * Get the SMESH_subMesh object implementation. Dont create it, return null
641 * if it does not exist.
643 //=============================================================================
645 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape)
646 throw(SALOME_Exception)
648 Unexpect aCatch(SalomeException);
649 bool isFound = false;
650 SMESH_subMesh *aSubMesh = NULL;
652 int index = _myMeshDS->ShapeToIndex(aSubShape);
653 if (_mapSubMesh.find(index) != _mapSubMesh.end())
655 aSubMesh = _mapSubMesh[index];
663 //=======================================================================
664 //function : IsUsedHypothesis
665 //purpose : Return True if anHyp is used to mesh aSubShape
666 //=======================================================================
668 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
669 const TopoDS_Shape & aSubShape)
671 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
672 // check if anHyp is applicable to aSubShape
673 SMESH_subMesh * subMesh = GetSubMeshContaining( aSubShape );
674 if ( !subMesh || !subMesh->IsApplicableHypotesis( hyp ))
677 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape);
680 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
681 return ( anHyp == algo );
683 // algorithm parameter
686 // look trough hypotheses used by algo
687 const list <const SMESHDS_Hypothesis * >&usedHyps =
688 algo->GetUsedHypothesis(*this, aSubShape);
689 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
692 // look through all assigned hypotheses
693 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( hyp ));
694 return GetHypothesis( aSubShape, filter, true );
698 //=============================================================================
702 //=============================================================================
704 const list < SMESH_subMesh * >&
705 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
706 throw(SALOME_Exception)
708 Unexpect aCatch(SalomeException);
709 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
710 map < int, SMESH_subMesh * >::iterator itsm;
711 _subMeshesUsingHypothesisList.clear();
712 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
714 SMESH_subMesh *aSubMesh = (*itsm).second;
715 if ( IsUsedHypothesis ( anHyp, aSubMesh->GetSubShape() ))
716 _subMeshesUsingHypothesisList.push_back(aSubMesh);
718 return _subMeshesUsingHypothesisList;
721 //=============================================================================
725 //=============================================================================
727 void SMESH_Mesh::ExportMED(const char *file,
728 const char* theMeshName,
731 throw(SALOME_Exception)
733 Unexpect aCatch(SalomeException);
734 DriverMED_W_SMESHDS_Mesh myWriter;
735 myWriter.SetFile ( file, MED::EVersion(theVersion) );
736 myWriter.SetMesh ( _myMeshDS );
738 myWriter.SetMeshId ( _idDoc );
740 myWriter.SetMeshId ( -1 );
741 myWriter.SetMeshName( theMeshName );
744 if ( theAutoGroups ) {
745 myWriter.AddGroupOfNodes();
746 myWriter.AddGroupOfEdges();
747 myWriter.AddGroupOfFaces();
748 myWriter.AddGroupOfVolumes();
751 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
752 SMESH_Group* aGroup = it->second;
753 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
755 aGroupDS->SetStoreName( aGroup->GetName() );
756 myWriter.AddGroup( aGroupDS );
763 void SMESH_Mesh::ExportDAT(const char *file) throw(SALOME_Exception)
765 Unexpect aCatch(SalomeException);
766 DriverDAT_W_SMDS_Mesh myWriter;
767 myWriter.SetFile(string(file));
768 myWriter.SetMesh(_myMeshDS);
769 myWriter.SetMeshId(_idDoc);
773 void SMESH_Mesh::ExportUNV(const char *file) throw(SALOME_Exception)
775 Unexpect aCatch(SalomeException);
776 DriverUNV_W_SMDS_Mesh myWriter;
777 myWriter.SetFile(string(file));
778 myWriter.SetMesh(_myMeshDS);
779 myWriter.SetMeshId(_idDoc);
783 void SMESH_Mesh::ExportSTL(const char *file, const bool isascii) throw(SALOME_Exception)
785 Unexpect aCatch(SalomeException);
786 DriverSTL_W_SMDS_Mesh myWriter;
787 myWriter.SetFile(string(file));
788 myWriter.SetIsAscii( isascii );
789 myWriter.SetMesh(_myMeshDS);
790 myWriter.SetMeshId(_idDoc);
794 //=============================================================================
798 //=============================================================================
799 int SMESH_Mesh::NbNodes() throw(SALOME_Exception)
801 Unexpect aCatch(SalomeException);
802 return _myMeshDS->NbNodes();
805 //=============================================================================
809 //=============================================================================
810 int SMESH_Mesh::NbEdges() throw(SALOME_Exception)
812 Unexpect aCatch(SalomeException);
813 return _myMeshDS->NbEdges();
816 //=============================================================================
820 //=============================================================================
821 int SMESH_Mesh::NbFaces() throw(SALOME_Exception)
823 Unexpect aCatch(SalomeException);
824 return _myMeshDS->NbFaces();
827 ///////////////////////////////////////////////////////////////////////////////
828 /// Return the number of 3 nodes faces in the mesh. This method run in O(n)
829 ///////////////////////////////////////////////////////////////////////////////
830 int SMESH_Mesh::NbTriangles() throw(SALOME_Exception)
832 Unexpect aCatch(SalomeException);
835 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
836 while(itFaces->more()) if(itFaces->next()->NbNodes()==3) Nb++;
840 ///////////////////////////////////////////////////////////////////////////////
841 /// Return the number of 4 nodes faces in the mesh. This method run in O(n)
842 ///////////////////////////////////////////////////////////////////////////////
843 int SMESH_Mesh::NbQuadrangles() throw(SALOME_Exception)
845 Unexpect aCatch(SalomeException);
848 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
849 while(itFaces->more()) if(itFaces->next()->NbNodes()==4) Nb++;
853 ///////////////////////////////////////////////////////////////////////////////
854 /// Return the number of polygone faces in the mesh. This method run in O(n)
855 ///////////////////////////////////////////////////////////////////////////////
856 int SMESH_Mesh::NbPolygones() throw(SALOME_Exception)
858 Unexpect aCatch(SalomeException);
861 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
862 while(itFaces->more()) if(itFaces->next()->IsPoly()) Nb++;
866 //=============================================================================
870 //=============================================================================
871 int SMESH_Mesh::NbVolumes() throw(SALOME_Exception)
873 Unexpect aCatch(SalomeException);
874 return _myMeshDS->NbVolumes();
877 int SMESH_Mesh::NbTetras() throw(SALOME_Exception)
879 Unexpect aCatch(SalomeException);
881 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
882 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==4) Nb++;
886 int SMESH_Mesh::NbHexas() throw(SALOME_Exception)
888 Unexpect aCatch(SalomeException);
890 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
891 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==8) Nb++;
895 int SMESH_Mesh::NbPyramids() throw(SALOME_Exception)
897 Unexpect aCatch(SalomeException);
899 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
900 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==5) Nb++;
904 int SMESH_Mesh::NbPrisms() throw(SALOME_Exception)
906 Unexpect aCatch(SalomeException);
908 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
909 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==6) Nb++;
913 int SMESH_Mesh::NbPolyhedrones() throw(SALOME_Exception)
915 Unexpect aCatch(SalomeException);
917 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
918 while(itVolumes->more()) if(itVolumes->next()->IsPoly()) Nb++;
922 //=============================================================================
926 //=============================================================================
927 int SMESH_Mesh::NbSubMesh() throw(SALOME_Exception)
929 Unexpect aCatch(SalomeException);
930 return _myMeshDS->NbSubMesh();
933 //=======================================================================
934 //function : IsNotConformAllowed
935 //purpose : check if a hypothesis alowing notconform mesh is present
936 //=======================================================================
938 bool SMESH_Mesh::IsNotConformAllowed() const
940 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
942 SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
943 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
946 //=======================================================================
947 //function : IsMainShape
949 //=======================================================================
951 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
953 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
956 //=============================================================================
960 //=============================================================================
962 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
965 const TopoDS_Shape& theShape)
967 if (_mapGroup.find(_groupId) != _mapGroup.end())
970 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape);
971 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
972 _mapGroup[_groupId++] = aGroup;
976 //=============================================================================
980 //=============================================================================
982 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
984 if (_mapGroup.find(theGroupID) == _mapGroup.end())
986 return _mapGroup[theGroupID];
990 //=============================================================================
994 //=============================================================================
996 list<int> SMESH_Mesh::GetGroupIds()
999 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1000 anIds.push_back( it->first );
1006 //=============================================================================
1010 //=============================================================================
1012 void SMESH_Mesh::RemoveGroup (const int theGroupID)
1014 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1016 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1017 _mapGroup.erase (theGroupID);
1018 delete _mapGroup[theGroupID];
1021 //=============================================================================
1023 * IsLocal1DHypothesis
1024 * Returns a local 1D hypothesis used for theEdge
1026 //=============================================================================
1027 const SMESH_Hypothesis* SMESH_Mesh::IsLocal1DHypothesis (const TopoDS_Shape& theEdge)
1029 SMESH_HypoFilter filter( SMESH_HypoFilter::HasDim( 1 ));
1030 filter.AndNot( SMESH_HypoFilter::IsAlgo() );
1031 filter.AndNot( SMESH_HypoFilter::IsGlobal( GetMeshDS()->ShapeToMesh() ));
1033 return GetHypothesis( theEdge, filter, true );
1036 //=============================================================================
1038 * IsPropagationHypothesis
1040 //=============================================================================
1041 bool SMESH_Mesh::IsPropagationHypothesis (const TopoDS_Shape& theEdge)
1043 return _mapPropagationChains.Contains(theEdge);
1046 //=============================================================================
1048 * IsPropagatedHypothesis
1050 //=============================================================================
1051 bool SMESH_Mesh::IsPropagatedHypothesis (const TopoDS_Shape& theEdge,
1052 TopoDS_Shape& theMainEdge)
1054 int nbChains = _mapPropagationChains.Extent();
1055 for (int i = 1; i <= nbChains; i++) {
1056 const TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.FindFromIndex(i);
1057 if (aChain.Contains(theEdge)) {
1058 theMainEdge = _mapPropagationChains.FindKey(i);
1065 //=============================================================================
1069 //=============================================================================
1071 bool SMESH_Mesh::IsReversedInChain (const TopoDS_Shape& theEdge,
1072 const TopoDS_Shape& theMainEdge)
1074 if ( !theMainEdge.IsNull() && !theEdge.IsNull() &&
1075 _mapPropagationChains.Contains( theMainEdge ))
1077 const TopTools_IndexedMapOfShape& aChain =
1078 _mapPropagationChains.FindFromKey( theMainEdge );
1079 int index = aChain.FindIndex( theEdge );
1081 return aChain(index).Orientation() == TopAbs_REVERSED;
1086 //=============================================================================
1088 * CleanMeshOnPropagationChain
1090 //=============================================================================
1091 void SMESH_Mesh::CleanMeshOnPropagationChain (const TopoDS_Shape& theMainEdge)
1093 const TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.FindFromKey(theMainEdge);
1094 int i, nbEdges = aChain.Extent();
1095 for (i = 1; i <= nbEdges; i++) {
1096 TopoDS_Shape anEdge = aChain.FindKey(i);
1097 SMESH_subMesh *subMesh = GetSubMesh(anEdge);
1098 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
1099 if (subMeshDS && subMeshDS->NbElements() > 0) {
1100 subMesh->ComputeStateEngine(SMESH_subMesh::CLEANDEP);
1105 //=============================================================================
1107 * RebuildPropagationChains
1108 * Rebuild all existing propagation chains.
1109 * Have to be used, if 1D hypothesis have been assigned/removed to/from any edge
1111 //=============================================================================
1112 bool SMESH_Mesh::RebuildPropagationChains()
1116 // Clean all chains, because they can be not up-to-date
1117 int i, nbChains = _mapPropagationChains.Extent();
1118 for (i = 1; i <= nbChains; i++) {
1119 TopoDS_Shape aMainEdge = _mapPropagationChains.FindKey(i);
1120 CleanMeshOnPropagationChain(aMainEdge);
1121 _mapPropagationChains.ChangeFromIndex(i).Clear();
1125 for (i = 1; i <= nbChains; i++) {
1126 TopoDS_Shape aMainEdge = _mapPropagationChains.FindKey(i);
1127 if (!BuildPropagationChain(aMainEdge))
1129 CleanMeshOnPropagationChain(aMainEdge);
1135 //=============================================================================
1137 * RemovePropagationChain
1138 * Have to be used, if Propagation hypothesis is removed from <theMainEdge>
1140 //=============================================================================
1141 bool SMESH_Mesh::RemovePropagationChain (const TopoDS_Shape& theMainEdge)
1143 if (!_mapPropagationChains.Contains(theMainEdge))
1146 // Clean mesh elements and nodes, built on the chain
1147 CleanMeshOnPropagationChain(theMainEdge);
1150 _mapPropagationChains.ChangeFromKey(theMainEdge).Clear();
1152 // Remove the chain from the map
1153 int i = _mapPropagationChains.FindIndex(theMainEdge);
1154 if ( i == _mapPropagationChains.Extent() )
1155 _mapPropagationChains.RemoveLast();
1157 TopoDS_Vertex anEmptyShape;
1159 BB.MakeVertex(anEmptyShape, gp_Pnt(0,0,0), 0.1);
1160 TopTools_IndexedMapOfShape anEmptyMap;
1161 _mapPropagationChains.Substitute(i, anEmptyShape, anEmptyMap);
1167 //=============================================================================
1169 * BuildPropagationChain
1171 //=============================================================================
1172 bool SMESH_Mesh::BuildPropagationChain (const TopoDS_Shape& theMainEdge)
1174 if (theMainEdge.ShapeType() != TopAbs_EDGE) return true;
1176 // Add new chain, if there is no
1177 if (!_mapPropagationChains.Contains(theMainEdge)) {
1178 TopTools_IndexedMapOfShape aNewChain;
1179 _mapPropagationChains.Add(theMainEdge, aNewChain);
1182 // Check presence of 1D hypothesis to be propagated
1183 const SMESH_Hypothesis* aMainHyp = IsLocal1DHypothesis(theMainEdge);
1185 MESSAGE("Warning: There is no 1D hypothesis to propagate. Please, assign.");
1189 // Edges, on which the 1D hypothesis will be propagated from <theMainEdge>
1190 TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.ChangeFromKey(theMainEdge);
1191 if (aChain.Extent() > 0) {
1192 CleanMeshOnPropagationChain(theMainEdge);
1196 // At first put <theMainEdge> in the chain
1197 aChain.Add(theMainEdge);
1199 // List of edges, added to chain on the previous cycle pass
1200 TopTools_ListOfShape listPrevEdges;
1201 listPrevEdges.Append(theMainEdge.Oriented( TopAbs_FORWARD ));
1203 // 5____4____3____4____5____6
1206 // 4____3____2____3____4____5
1207 // | | | | | | Number in the each knot of
1208 // | | | | | | grid indicates cycle pass,
1209 // 3____2____1____2____3____4 on which corresponding edge
1210 // | | | | | | (perpendicular to the plane
1211 // | | | | | | of view) will be found.
1212 // 2____1____0____1____2____3
1215 // 3____2____1____2____3____4
1217 // Collect all edges pass by pass
1218 while (listPrevEdges.Extent() > 0) {
1219 // List of edges, added to chain on this cycle pass
1220 TopTools_ListOfShape listCurEdges;
1222 // Find the next portion of edges
1223 TopTools_ListIteratorOfListOfShape itE (listPrevEdges);
1224 for (; itE.More(); itE.Next()) {
1225 TopoDS_Shape anE = itE.Value();
1227 // Iterate on faces, having edge <anE>
1228 TopTools_ListIteratorOfListOfShape itA (GetAncestors(anE));
1229 for (; itA.More(); itA.Next()) {
1230 TopoDS_Shape aW = itA.Value();
1232 // There are objects of different type among the ancestors of edge
1233 if (aW.ShapeType() == TopAbs_WIRE) {
1234 TopoDS_Shape anOppE;
1236 BRepTools_WireExplorer aWE (TopoDS::Wire(aW));
1237 Standard_Integer nb = 1, found = 0;
1238 TopTools_Array1OfShape anEdges (1,4);
1239 for (; aWE.More(); aWE.Next(), nb++) {
1244 anEdges(nb) = aWE.Current();
1245 if (!_mapAncestors.Contains(anEdges(nb))) {
1246 MESSAGE("WIRE EXPLORER HAVE GIVEN AN INVALID EDGE !!!");
1249 if (anEdges(nb).IsSame(anE)) found = nb;
1252 if (nb == 5 && found > 0) {
1253 // Quadrangle face found, get an opposite edge
1254 Standard_Integer opp = found + 2;
1255 if (opp > 4) opp -= 4;
1256 anOppE = anEdges(opp);
1258 // add anOppE to aChain if ...
1259 if (!aChain.Contains(anOppE)) { // ... anOppE is not in aChain
1260 if (!IsLocal1DHypothesis(anOppE)) { // ... no other 1d hyp on anOppE
1261 TopoDS_Shape aMainEdgeForOppEdge; // ... no other hyp is propagated to anOppE
1262 if (!IsPropagatedHypothesis(anOppE, aMainEdgeForOppEdge))
1264 // Add found edge to the chain oriented so that to
1265 // have it co-directed with a forward MainEdge
1266 TopAbs_Orientation ori = anE.Orientation();
1267 if ( anEdges(opp).Orientation() == anEdges(found).Orientation() )
1268 ori = TopAbs::Reverse( ori );
1269 anOppE.Orientation( ori );
1271 listCurEdges.Append(anOppE);
1275 MESSAGE("Error: Collision between propagated hypotheses");
1276 CleanMeshOnPropagationChain(theMainEdge);
1278 return ( aMainHyp == IsLocal1DHypothesis(aMainEdgeForOppEdge) );
1282 } // if (nb == 5 && found > 0)
1283 } // if (aF.ShapeType() == TopAbs_WIRE)
1284 } // for (; itF.More(); itF.Next())
1285 } // for (; itE.More(); itE.Next())
1287 listPrevEdges = listCurEdges;
1288 } // while (listPrevEdges.Extent() > 0)
1290 CleanMeshOnPropagationChain(theMainEdge);
1294 //=======================================================================
1295 //function : GetAncestors
1296 //purpose : return list of ancestors of theSubShape in the order
1297 // that lower dimention shapes come first.
1298 //=======================================================================
1300 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1302 if ( _mapAncestors.Contains( theS ) )
1303 return _mapAncestors.FindFromKey( theS );
1305 static TopTools_ListOfShape emptyList;
1309 //=======================================================================
1311 //purpose : dumps contents of mesh to stream [ debug purposes ]
1312 //=======================================================================
1313 ostream& SMESH_Mesh::Dump(ostream& save)
1315 save << "========================== Dump contents of mesh ==========================" << endl;
1316 save << "1) Total number of nodes: " << NbNodes() << endl;
1317 save << "2) Total number of edges: " << NbEdges() << endl;
1318 save << "3) Total number of faces: " << NbFaces() << endl;
1319 if ( NbFaces() > 0 ) {
1320 int nb3 = NbTriangles();
1321 int nb4 = NbQuadrangles();
1322 save << "3.1.) Number of triangles: " << nb3 << endl;
1323 save << "3.2.) Number of quadrangles: " << nb4 << endl;
1324 if ( nb3 + nb4 != NbFaces() ) {
1325 map<int,int> myFaceMap;
1326 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1327 while( itFaces->more( ) ) {
1328 int nbNodes = itFaces->next()->NbNodes();
1329 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1330 myFaceMap[ nbNodes ] = 0;
1331 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1333 save << "3.3.) Faces in detail: " << endl;
1334 map <int,int>::iterator itF;
1335 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1336 save << "--> nb nodes: " << itF->first << " - nb elemens: " << itF->second << endl;
1339 save << "4) Total number of volumes: " << NbVolumes() << endl;
1340 if ( NbVolumes() > 0 ) {
1341 int nb8 = NbHexas();
1342 int nb4 = NbTetras();
1343 int nb5 = NbPyramids();
1344 int nb6 = NbPrisms();
1345 save << "4.1.) Number of hexahedrons: " << nb8 << endl;
1346 save << "4.2.) Number of tetrahedrons: " << nb4 << endl;
1347 save << "4.3.) Number of prisms: " << nb6 << endl;
1348 save << "4.4.) Number of pyramides: " << nb5 << endl;
1349 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes() ) {
1350 map<int,int> myVolumesMap;
1351 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1352 while( itVolumes->more( ) ) {
1353 int nbNodes = itVolumes->next()->NbNodes();
1354 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1355 myVolumesMap[ nbNodes ] = 0;
1356 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1358 save << "4.5.) Volumes in detail: " << endl;
1359 map <int,int>::iterator itV;
1360 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1361 save << "--> nb nodes: " << itV->first << " - nb elemens: " << itV->second << endl;
1364 save << "===========================================================================" << endl;