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<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
214 if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size());
216 list<TNameAndType>::iterator name_type = aGroupNames.begin();
217 for ( ; name_type != aGroupNames.end(); name_type++ ) {
218 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
220 if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<name_type->first.c_str());
221 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
223 aGroupDS->SetStoreName( name_type->first.c_str() );
224 myReader.GetGroup( aGroupDS );
231 //=======================================================================
232 //function : STLToMesh
234 //=======================================================================
236 int SMESH_Mesh::STLToMesh(const char* theFileName)
238 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
240 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
241 _isShapeToMesh = true;
242 DriverSTL_R_SMDS_Mesh myReader;
243 myReader.SetMesh(_myMeshDS);
244 myReader.SetFile(theFileName);
245 myReader.SetMeshId(-1);
248 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
249 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
250 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
251 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
256 //=============================================================================
260 //=============================================================================
262 SMESH_Hypothesis::Hypothesis_Status
263 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
264 int anHypId ) throw(SALOME_Exception)
266 Unexpect aCatch(SalomeException);
267 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
269 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
270 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
271 if ( subMeshDS && subMeshDS->IsComplexSubmesh() ) // group of sub-shapes and maybe of not sub-
273 MESSAGE("AddHypothesis() to complex submesh");
274 // return the worst but not fatal state of all group memebers
275 SMESH_Hypothesis::Hypothesis_Status aBestRet, aWorstNotFatal, ret;
276 aBestRet = SMESH_Hypothesis::HYP_BAD_DIM;
277 aWorstNotFatal = SMESH_Hypothesis::HYP_OK;
278 for ( TopoDS_Iterator itS ( aSubShape ); itS.More(); itS.Next())
280 if ( !GetMeshDS()->ShapeToIndex( itS.Value() ))
281 continue; // not sub-shape
282 ret = AddHypothesis( itS.Value(), anHypId );
283 if ( !SMESH_Hypothesis::IsStatusFatal( ret ) && ret > aWorstNotFatal )
284 aWorstNotFatal = ret;
285 if ( ret < aBestRet )
288 if ( SMESH_Hypothesis::IsStatusFatal( aBestRet ))
290 return aWorstNotFatal;
293 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
294 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
296 if(MYDEBUG) MESSAGE("Hypothesis ID does not give an hypothesis");
301 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
304 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
305 MESSAGE( "SMESH_Mesh::AddHypothesis " << anHyp->GetName() );
307 bool isGlobalHyp = IsMainShape( aSubShape );
309 // NotConformAllowed can be only global
312 string hypName = anHyp->GetName();
313 if ( hypName == "NotConformAllowed" )
315 if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
316 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
323 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
324 event = SMESH_subMesh::ADD_HYP;
326 event = SMESH_subMesh::ADD_ALGO;
327 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
330 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
331 !subMesh->IsApplicableHypotesis( anHyp )) // is added on father
333 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
334 event = SMESH_subMesh::ADD_FATHER_HYP;
336 event = SMESH_subMesh::ADD_FATHER_ALGO;
337 SMESH_Hypothesis::Hypothesis_Status ret2 =
338 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
342 // check concurent hypotheses on ansestors
343 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
345 const map < int, SMESH_subMesh * >& smMap = subMesh->DependsOn();
346 map < int, SMESH_subMesh * >::const_iterator smIt = smMap.begin();
347 for ( ; smIt != smMap.end(); smIt++ ) {
348 if ( smIt->second->IsApplicableHypotesis( anHyp )) {
349 ret2 = smIt->second->CheckConcurentHypothesis( anHyp->GetType() );
359 if(MYDEBUG) subMesh->DumpAlgoState(true);
364 //=============================================================================
368 //=============================================================================
370 SMESH_Hypothesis::Hypothesis_Status
371 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
372 int anHypId)throw(SALOME_Exception)
374 Unexpect aCatch(SalomeException);
375 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
377 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
378 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
379 if ( subMeshDS && subMeshDS->IsComplexSubmesh() )
381 // return the worst but not fatal state of all group memebers
382 SMESH_Hypothesis::Hypothesis_Status aBestRet, aWorstNotFatal, ret;
383 aBestRet = SMESH_Hypothesis::HYP_BAD_DIM;
384 aWorstNotFatal = SMESH_Hypothesis::HYP_OK;
385 for ( TopoDS_Iterator itS ( aSubShape ); itS.More(); itS.Next())
387 if ( !GetMeshDS()->ShapeToIndex( itS.Value() ))
388 continue; // not sub-shape
389 ret = RemoveHypothesis( itS.Value(), anHypId );
390 if ( !SMESH_Hypothesis::IsStatusFatal( ret ) && ret > aWorstNotFatal )
391 aWorstNotFatal = ret;
392 if ( ret < aBestRet )
395 if ( SMESH_Hypothesis::IsStatusFatal( aBestRet ))
397 return aWorstNotFatal;
400 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
401 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
402 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
404 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
405 int hypType = anHyp->GetType();
406 if(MYDEBUG) SCRUTE(hypType);
411 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
412 event = SMESH_subMesh::REMOVE_HYP;
414 event = SMESH_subMesh::REMOVE_ALGO;
415 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
417 // there may appear concurrent hyps that were covered by the removed hyp
418 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
419 subMesh->IsApplicableHypotesis( anHyp ) &&
420 subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
421 ret = SMESH_Hypothesis::HYP_CONCURENT;
424 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
425 !subMesh->IsApplicableHypotesis( anHyp )) // is removed from father
427 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
428 event = SMESH_subMesh::REMOVE_FATHER_HYP;
430 event = SMESH_subMesh::REMOVE_FATHER_ALGO;
431 SMESH_Hypothesis::Hypothesis_Status ret2 =
432 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
433 if (ret2 > ret) // more severe
436 // check concurent hypotheses on ansestors
437 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
439 const map < int, SMESH_subMesh * >& smMap = subMesh->DependsOn();
440 map < int, SMESH_subMesh * >::const_iterator smIt = smMap.begin();
441 for ( ; smIt != smMap.end(); smIt++ ) {
442 if ( smIt->second->IsApplicableHypotesis( anHyp )) {
443 ret2 = smIt->second->CheckConcurentHypothesis( anHyp->GetType() );
453 if(MYDEBUG) subMesh->DumpAlgoState(true);
454 if(MYDEBUG) SCRUTE(ret);
458 //=============================================================================
462 //=============================================================================
464 SMESHDS_Mesh * SMESH_Mesh::GetMeshDS()
469 //=============================================================================
473 //=============================================================================
475 const list<const SMESHDS_Hypothesis*>&
476 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
477 throw(SALOME_Exception)
479 Unexpect aCatch(SalomeException);
480 return _myMeshDS->GetHypothesis(aSubShape);
483 //=======================================================================
484 //function : GetHypothesis
486 //=======================================================================
488 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
489 const SMESH_HypoFilter& aFilter,
490 const bool andAncestors) const
493 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
494 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
495 for ( ; hyp != hypList.end(); hyp++ ) {
496 const SMESH_Hypothesis * h = static_cast<const SMESH_Hypothesis*>( *hyp );
497 if ( aFilter.IsOk( h, aSubShape))
503 TopTools_ListIteratorOfListOfShape it( GetAncestors( aSubShape ));
504 for (; it.More(); it.Next() )
506 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(it.Value());
507 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
508 for ( ; hyp != hypList.end(); hyp++ ) {
509 const SMESH_Hypothesis * h = static_cast<const SMESH_Hypothesis*>( *hyp );
510 if (aFilter.IsOk( h, it.Value() ))
518 //=======================================================================
519 //function : GetHypotheses
521 //=======================================================================
523 bool SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
524 const SMESH_HypoFilter& aFilter,
525 list <const SMESHDS_Hypothesis * >& aHypList,
526 const bool andAncestors) const
530 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
531 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
532 for ( ; hyp != hypList.end(); hyp++ )
533 if ( aFilter.IsOk (static_cast<const SMESH_Hypothesis*>( *hyp ), aSubShape)) {
534 aHypList.push_back( *hyp );
538 // get hypos from shape of one type only: if any hypo is found on edge, do
539 // not look up on faces
540 if ( !nbHyp && andAncestors )
542 TopTools_MapOfShape map;
543 TopTools_ListIteratorOfListOfShape it( GetAncestors( aSubShape ));
544 int shapeType = it.More() ? it.Value().ShapeType() : TopAbs_SHAPE;
545 for (; it.More(); it.Next() )
547 if ( nbHyp && shapeType != it.Value().ShapeType() )
549 shapeType = it.Value().ShapeType();
550 if ( !map.Add( it.Value() ))
552 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(it.Value());
553 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
554 for ( ; hyp != hypList.end(); hyp++ )
555 if (aFilter.IsOk( static_cast<const SMESH_Hypothesis*>( *hyp ), it.Value() )) {
556 aHypList.push_back( *hyp );
564 //=============================================================================
568 //=============================================================================
570 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
572 Unexpect aCatch(SalomeException);
573 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
574 return _myMeshDS->GetScript()->GetCommands();
577 //=============================================================================
581 //=============================================================================
582 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
584 Unexpect aCatch(SalomeException);
585 if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
586 _myMeshDS->GetScript()->Clear();
589 //=============================================================================
593 //=============================================================================
595 int SMESH_Mesh::GetId()
597 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetId");
601 //=============================================================================
605 //=============================================================================
607 SMESH_Gen *SMESH_Mesh::GetGen()
612 //=============================================================================
614 * Get or Create the SMESH_subMesh object implementation
616 //=============================================================================
618 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
619 throw(SALOME_Exception)
621 Unexpect aCatch(SalomeException);
622 SMESH_subMesh *aSubMesh;
623 int index = _myMeshDS->ShapeToIndex(aSubShape);
625 // for submeshes on GEOM Group
626 if ( !index && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
627 TopoDS_Iterator it( aSubShape );
629 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
632 if (_mapSubMesh.find(index) != _mapSubMesh.end())
634 aSubMesh = _mapSubMesh[index];
638 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
639 _mapSubMesh[index] = aSubMesh;
644 //=============================================================================
646 * Get the SMESH_subMesh object implementation. Dont create it, return null
647 * if it does not exist.
649 //=============================================================================
651 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape)
652 throw(SALOME_Exception)
654 Unexpect aCatch(SalomeException);
655 bool isFound = false;
656 SMESH_subMesh *aSubMesh = NULL;
658 int index = _myMeshDS->ShapeToIndex(aSubShape);
659 if (_mapSubMesh.find(index) != _mapSubMesh.end())
661 aSubMesh = _mapSubMesh[index];
669 //=======================================================================
670 //function : IsUsedHypothesis
671 //purpose : Return True if anHyp is used to mesh aSubShape
672 //=======================================================================
674 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
675 const TopoDS_Shape & aSubShape)
677 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
678 // check if anHyp is applicable to aSubShape
679 SMESH_subMesh * subMesh = GetSubMeshContaining( aSubShape );
680 if ( !subMesh || !subMesh->IsApplicableHypotesis( hyp ))
683 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape);
686 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
687 return ( anHyp == algo );
689 // algorithm parameter
692 // look trough hypotheses used by algo
693 const list <const SMESHDS_Hypothesis * >&usedHyps =
694 algo->GetUsedHypothesis(*this, aSubShape);
695 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
698 // look through all assigned hypotheses
699 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( hyp ));
700 return GetHypothesis( aSubShape, filter, true );
704 //=============================================================================
708 //=============================================================================
710 const list < SMESH_subMesh * >&
711 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
712 throw(SALOME_Exception)
714 Unexpect aCatch(SalomeException);
715 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
716 map < int, SMESH_subMesh * >::iterator itsm;
717 _subMeshesUsingHypothesisList.clear();
718 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
720 SMESH_subMesh *aSubMesh = (*itsm).second;
721 if ( IsUsedHypothesis ( anHyp, aSubMesh->GetSubShape() ))
722 _subMeshesUsingHypothesisList.push_back(aSubMesh);
724 return _subMeshesUsingHypothesisList;
727 //=============================================================================
731 //=============================================================================
733 void SMESH_Mesh::ExportMED(const char *file,
734 const char* theMeshName,
737 throw(SALOME_Exception)
739 Unexpect aCatch(SalomeException);
740 DriverMED_W_SMESHDS_Mesh myWriter;
741 myWriter.SetFile ( file, MED::EVersion(theVersion) );
742 myWriter.SetMesh ( _myMeshDS );
744 myWriter.SetMeshId ( _idDoc );
746 myWriter.SetMeshId ( -1 );
747 myWriter.SetMeshName( theMeshName );
750 if ( theAutoGroups ) {
751 myWriter.AddGroupOfNodes();
752 myWriter.AddGroupOfEdges();
753 myWriter.AddGroupOfFaces();
754 myWriter.AddGroupOfVolumes();
757 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
758 SMESH_Group* aGroup = it->second;
759 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
761 aGroupDS->SetStoreName( aGroup->GetName() );
762 myWriter.AddGroup( aGroupDS );
769 void SMESH_Mesh::ExportDAT(const char *file) throw(SALOME_Exception)
771 Unexpect aCatch(SalomeException);
772 DriverDAT_W_SMDS_Mesh myWriter;
773 myWriter.SetFile(string(file));
774 myWriter.SetMesh(_myMeshDS);
775 myWriter.SetMeshId(_idDoc);
779 void SMESH_Mesh::ExportUNV(const char *file) throw(SALOME_Exception)
781 Unexpect aCatch(SalomeException);
782 DriverUNV_W_SMDS_Mesh myWriter;
783 myWriter.SetFile(string(file));
784 myWriter.SetMesh(_myMeshDS);
785 myWriter.SetMeshId(_idDoc);
789 void SMESH_Mesh::ExportSTL(const char *file, const bool isascii) throw(SALOME_Exception)
791 Unexpect aCatch(SalomeException);
792 DriverSTL_W_SMDS_Mesh myWriter;
793 myWriter.SetFile(string(file));
794 myWriter.SetIsAscii( isascii );
795 myWriter.SetMesh(_myMeshDS);
796 myWriter.SetMeshId(_idDoc);
800 //=============================================================================
804 //=============================================================================
805 int SMESH_Mesh::NbNodes() throw(SALOME_Exception)
807 Unexpect aCatch(SalomeException);
808 return _myMeshDS->NbNodes();
811 //=============================================================================
815 //=============================================================================
816 int SMESH_Mesh::NbEdges() throw(SALOME_Exception)
818 Unexpect aCatch(SalomeException);
819 return _myMeshDS->NbEdges();
822 //=============================================================================
826 //=============================================================================
827 int SMESH_Mesh::NbFaces() throw(SALOME_Exception)
829 Unexpect aCatch(SalomeException);
830 return _myMeshDS->NbFaces();
833 ///////////////////////////////////////////////////////////////////////////////
834 /// Return the number of 3 nodes faces in the mesh. This method run in O(n)
835 ///////////////////////////////////////////////////////////////////////////////
836 int SMESH_Mesh::NbTriangles() throw(SALOME_Exception)
838 Unexpect aCatch(SalomeException);
841 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
842 while(itFaces->more()) if(itFaces->next()->NbNodes()==3) Nb++;
846 ///////////////////////////////////////////////////////////////////////////////
847 /// Return the number of 4 nodes faces in the mesh. This method run in O(n)
848 ///////////////////////////////////////////////////////////////////////////////
849 int SMESH_Mesh::NbQuadrangles() throw(SALOME_Exception)
851 Unexpect aCatch(SalomeException);
854 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
855 while(itFaces->more()) if(itFaces->next()->NbNodes()==4) Nb++;
859 //=============================================================================
863 //=============================================================================
864 int SMESH_Mesh::NbVolumes() throw(SALOME_Exception)
866 Unexpect aCatch(SalomeException);
867 return _myMeshDS->NbVolumes();
870 int SMESH_Mesh::NbTetras() throw(SALOME_Exception)
872 Unexpect aCatch(SalomeException);
874 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
875 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==4) Nb++;
879 int SMESH_Mesh::NbHexas() throw(SALOME_Exception)
881 Unexpect aCatch(SalomeException);
883 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
884 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==8) Nb++;
888 int SMESH_Mesh::NbPyramids() throw(SALOME_Exception)
890 Unexpect aCatch(SalomeException);
892 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
893 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==5) Nb++;
897 int SMESH_Mesh::NbPrisms() throw(SALOME_Exception)
899 Unexpect aCatch(SalomeException);
901 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
902 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==6) Nb++;
906 //=============================================================================
910 //=============================================================================
911 int SMESH_Mesh::NbSubMesh() throw(SALOME_Exception)
913 Unexpect aCatch(SalomeException);
914 return _myMeshDS->NbSubMesh();
917 //=======================================================================
918 //function : IsNotConformAllowed
919 //purpose : check if a hypothesis alowing notconform mesh is present
920 //=======================================================================
922 bool SMESH_Mesh::IsNotConformAllowed() const
924 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
926 SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
927 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
930 //=======================================================================
931 //function : IsMainShape
933 //=======================================================================
935 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
937 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
940 //=============================================================================
944 //=============================================================================
946 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
949 const TopoDS_Shape& theShape)
951 if (_mapGroup.find(_groupId) != _mapGroup.end())
954 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape);
955 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
956 _mapGroup[_groupId++] = aGroup;
960 //=============================================================================
964 //=============================================================================
966 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
968 if (_mapGroup.find(theGroupID) == _mapGroup.end())
970 return _mapGroup[theGroupID];
974 //=============================================================================
978 //=============================================================================
980 list<int> SMESH_Mesh::GetGroupIds()
983 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
984 anIds.push_back( it->first );
990 //=============================================================================
994 //=============================================================================
996 void SMESH_Mesh::RemoveGroup (const int theGroupID)
998 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1000 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1001 delete _mapGroup[theGroupID];
1002 _mapGroup.erase (theGroupID);
1005 //=============================================================================
1007 * IsLocal1DHypothesis
1008 * Returns a local 1D hypothesis used for theEdge
1010 //=============================================================================
1011 const SMESH_Hypothesis* SMESH_Mesh::IsLocal1DHypothesis (const TopoDS_Shape& theEdge)
1013 SMESH_HypoFilter filter( SMESH_HypoFilter::HasDim( 1 ));
1014 filter.AndNot( SMESH_HypoFilter::IsAlgo() );
1015 filter.AndNot( SMESH_HypoFilter::IsGlobal( GetMeshDS()->ShapeToMesh() ));
1017 return GetHypothesis( theEdge, filter, true );
1020 //=============================================================================
1022 * IsPropagationHypothesis
1024 //=============================================================================
1025 bool SMESH_Mesh::IsPropagationHypothesis (const TopoDS_Shape& theEdge)
1027 return _mapPropagationChains.Contains(theEdge);
1030 //=============================================================================
1032 * IsPropagatedHypothesis
1034 //=============================================================================
1035 bool SMESH_Mesh::IsPropagatedHypothesis (const TopoDS_Shape& theEdge,
1036 TopoDS_Shape& theMainEdge)
1038 int nbChains = _mapPropagationChains.Extent();
1039 for (int i = 1; i <= nbChains; i++) {
1040 const TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.FindFromIndex(i);
1041 if (aChain.Contains(theEdge)) {
1042 theMainEdge = _mapPropagationChains.FindKey(i);
1049 //=============================================================================
1053 //=============================================================================
1055 bool SMESH_Mesh::IsReversedInChain (const TopoDS_Shape& theEdge,
1056 const TopoDS_Shape& theMainEdge)
1058 if ( !theMainEdge.IsNull() && !theEdge.IsNull() &&
1059 _mapPropagationChains.Contains( theMainEdge ))
1061 const TopTools_IndexedMapOfShape& aChain =
1062 _mapPropagationChains.FindFromKey( theMainEdge );
1063 int index = aChain.FindIndex( theEdge );
1065 return aChain(index).Orientation() == TopAbs_REVERSED;
1070 //=============================================================================
1072 * CleanMeshOnPropagationChain
1074 //=============================================================================
1075 void SMESH_Mesh::CleanMeshOnPropagationChain (const TopoDS_Shape& theMainEdge)
1077 const TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.FindFromKey(theMainEdge);
1078 int i, nbEdges = aChain.Extent();
1079 for (i = 1; i <= nbEdges; i++) {
1080 TopoDS_Shape anEdge = aChain.FindKey(i);
1081 SMESH_subMesh *subMesh = GetSubMesh(anEdge);
1082 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
1083 if (subMeshDS && subMeshDS->NbElements() > 0) {
1084 subMesh->ComputeStateEngine(SMESH_subMesh::CLEANDEP);
1089 //=============================================================================
1091 * RebuildPropagationChains
1092 * Rebuild all existing propagation chains.
1093 * Have to be used, if 1D hypothesis have been assigned/removed to/from any edge
1095 //=============================================================================
1096 bool SMESH_Mesh::RebuildPropagationChains()
1100 // Clean all chains, because they can be not up-to-date
1101 int i, nbChains = _mapPropagationChains.Extent();
1102 for (i = 1; i <= nbChains; i++) {
1103 TopoDS_Shape aMainEdge = _mapPropagationChains.FindKey(i);
1104 CleanMeshOnPropagationChain(aMainEdge);
1105 _mapPropagationChains.ChangeFromIndex(i).Clear();
1109 for (i = 1; i <= nbChains; i++) {
1110 TopoDS_Shape aMainEdge = _mapPropagationChains.FindKey(i);
1111 if (!BuildPropagationChain(aMainEdge))
1113 CleanMeshOnPropagationChain(aMainEdge);
1119 //=============================================================================
1121 * RemovePropagationChain
1122 * Have to be used, if Propagation hypothesis is removed from <theMainEdge>
1124 //=============================================================================
1125 bool SMESH_Mesh::RemovePropagationChain (const TopoDS_Shape& theMainEdge)
1127 if (!_mapPropagationChains.Contains(theMainEdge))
1130 // Clean mesh elements and nodes, built on the chain
1131 CleanMeshOnPropagationChain(theMainEdge);
1134 _mapPropagationChains.ChangeFromKey(theMainEdge).Clear();
1136 // Remove the chain from the map
1137 int i = _mapPropagationChains.FindIndex(theMainEdge);
1138 if ( i == _mapPropagationChains.Extent() )
1139 _mapPropagationChains.RemoveLast();
1141 TopoDS_Vertex anEmptyShape;
1143 BB.MakeVertex(anEmptyShape, gp_Pnt(0,0,0), 0.1);
1144 TopTools_IndexedMapOfShape anEmptyMap;
1145 _mapPropagationChains.Substitute(i, anEmptyShape, anEmptyMap);
1151 //=============================================================================
1153 * BuildPropagationChain
1155 //=============================================================================
1156 bool SMESH_Mesh::BuildPropagationChain (const TopoDS_Shape& theMainEdge)
1158 if (theMainEdge.ShapeType() != TopAbs_EDGE) return true;
1160 // Add new chain, if there is no
1161 if (!_mapPropagationChains.Contains(theMainEdge)) {
1162 TopTools_IndexedMapOfShape aNewChain;
1163 _mapPropagationChains.Add(theMainEdge, aNewChain);
1166 // Check presence of 1D hypothesis to be propagated
1167 const SMESH_Hypothesis* aMainHyp = IsLocal1DHypothesis(theMainEdge);
1169 MESSAGE("Warning: There is no 1D hypothesis to propagate. Please, assign.");
1173 // Edges, on which the 1D hypothesis will be propagated from <theMainEdge>
1174 TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.ChangeFromKey(theMainEdge);
1175 if (aChain.Extent() > 0) {
1176 CleanMeshOnPropagationChain(theMainEdge);
1180 // At first put <theMainEdge> in the chain
1181 aChain.Add(theMainEdge);
1183 // List of edges, added to chain on the previous cycle pass
1184 TopTools_ListOfShape listPrevEdges;
1185 listPrevEdges.Append(theMainEdge.Oriented( TopAbs_FORWARD ));
1187 // 5____4____3____4____5____6
1190 // 4____3____2____3____4____5
1191 // | | | | | | Number in the each knot of
1192 // | | | | | | grid indicates cycle pass,
1193 // 3____2____1____2____3____4 on which corresponding edge
1194 // | | | | | | (perpendicular to the plane
1195 // | | | | | | of view) will be found.
1196 // 2____1____0____1____2____3
1199 // 3____2____1____2____3____4
1201 // Collect all edges pass by pass
1202 while (listPrevEdges.Extent() > 0) {
1203 // List of edges, added to chain on this cycle pass
1204 TopTools_ListOfShape listCurEdges;
1206 // Find the next portion of edges
1207 TopTools_ListIteratorOfListOfShape itE (listPrevEdges);
1208 for (; itE.More(); itE.Next()) {
1209 TopoDS_Shape anE = itE.Value();
1211 // Iterate on faces, having edge <anE>
1212 TopTools_ListIteratorOfListOfShape itA (GetAncestors(anE));
1213 for (; itA.More(); itA.Next()) {
1214 TopoDS_Shape aW = itA.Value();
1216 // There are objects of different type among the ancestors of edge
1217 if (aW.ShapeType() == TopAbs_WIRE) {
1218 TopoDS_Shape anOppE;
1220 BRepTools_WireExplorer aWE (TopoDS::Wire(aW));
1221 Standard_Integer nb = 1, found = 0;
1222 TopTools_Array1OfShape anEdges (1,4);
1223 for (; aWE.More(); aWE.Next(), nb++) {
1228 anEdges(nb) = aWE.Current();
1229 if (!_mapAncestors.Contains(anEdges(nb))) {
1230 MESSAGE("WIRE EXPLORER HAVE GIVEN AN INVALID EDGE !!!");
1233 if (anEdges(nb).IsSame(anE)) found = nb;
1236 if (nb == 5 && found > 0) {
1237 // Quadrangle face found, get an opposite edge
1238 Standard_Integer opp = found + 2;
1239 if (opp > 4) opp -= 4;
1240 anOppE = anEdges(opp);
1242 // add anOppE to aChain if ...
1243 if (!aChain.Contains(anOppE)) { // ... anOppE is not in aChain
1244 if (!IsLocal1DHypothesis(anOppE)) { // ... no other 1d hyp on anOppE
1245 TopoDS_Shape aMainEdgeForOppEdge; // ... no other hyp is propagated to anOppE
1246 if (!IsPropagatedHypothesis(anOppE, aMainEdgeForOppEdge))
1248 // Add found edge to the chain oriented so that to
1249 // have it co-directed with a forward MainEdge
1250 TopAbs_Orientation ori = anE.Orientation();
1251 if ( anEdges(opp).Orientation() == anEdges(found).Orientation() )
1252 ori = TopAbs::Reverse( ori );
1253 anOppE.Orientation( ori );
1255 listCurEdges.Append(anOppE);
1259 MESSAGE("Error: Collision between propagated hypotheses");
1260 CleanMeshOnPropagationChain(theMainEdge);
1262 return ( aMainHyp == IsLocal1DHypothesis(aMainEdgeForOppEdge) );
1266 } // if (nb == 5 && found > 0)
1267 } // if (aF.ShapeType() == TopAbs_WIRE)
1268 } // for (; itF.More(); itF.Next())
1269 } // for (; itE.More(); itE.Next())
1271 listPrevEdges = listCurEdges;
1272 } // while (listPrevEdges.Extent() > 0)
1274 CleanMeshOnPropagationChain(theMainEdge);
1278 //=======================================================================
1279 //function : GetAncestors
1280 //purpose : return list of ancestors of theSubShape in the order
1281 // that lower dimention shapes come first.
1282 //=======================================================================
1284 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1286 if ( _mapAncestors.Contains( theS ) )
1287 return _mapAncestors.FindFromKey( theS );
1289 static TopTools_ListOfShape emptyList;
1293 //=======================================================================
1295 //purpose : dumps contents of mesh to stream [ debug purposes ]
1296 //=======================================================================
1297 ostream& SMESH_Mesh::Dump(ostream& save)
1299 save << "========================== Dump contents of mesh ==========================" << endl;
1300 save << "1) Total number of nodes: " << NbNodes() << endl;
1301 save << "2) Total number of edges: " << NbEdges() << endl;
1302 save << "3) Total number of faces: " << NbFaces() << endl;
1303 if ( NbFaces() > 0 ) {
1304 int nb3 = NbTriangles();
1305 int nb4 = NbQuadrangles();
1306 save << "3.1.) Number of triangles: " << nb3 << endl;
1307 save << "3.2.) Number of quadrangles: " << nb4 << endl;
1308 if ( nb3 + nb4 != NbFaces() ) {
1309 map<int,int> myFaceMap;
1310 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1311 while( itFaces->more( ) ) {
1312 int nbNodes = itFaces->next()->NbNodes();
1313 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1314 myFaceMap[ nbNodes ] = 0;
1315 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1317 save << "3.3.) Faces in detail: " << endl;
1318 map <int,int>::iterator itF;
1319 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1320 save << "--> nb nodes: " << itF->first << " - nb elemens: " << itF->second << endl;
1323 save << "4) Total number of volumes: " << NbVolumes() << endl;
1324 if ( NbVolumes() > 0 ) {
1325 int nb8 = NbHexas();
1326 int nb4 = NbTetras();
1327 int nb5 = NbPyramids();
1328 int nb6 = NbPrisms();
1329 save << "4.1.) Number of hexahedrons: " << nb8 << endl;
1330 save << "4.2.) Number of tetrahedrons: " << nb4 << endl;
1331 save << "4.3.) Number of prisms: " << nb6 << endl;
1332 save << "4.4.) Number of pyramides: " << nb5 << endl;
1333 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes() ) {
1334 map<int,int> myVolumesMap;
1335 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1336 while( itVolumes->more( ) ) {
1337 int nbNodes = itVolumes->next()->NbNodes();
1338 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1339 myVolumesMap[ nbNodes ] = 0;
1340 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1342 save << "4.5.) Volumes in detail: " << endl;
1343 map <int,int>::iterator itV;
1344 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1345 save << "--> nb nodes: " << itV->first << " - nb elemens: " << itV->second << endl;
1348 save << "===========================================================================" << endl;