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 "SMESHDS_Group.hxx"
35 #include "SMESHDS_Script.hxx"
36 #include "SMDS_MeshVolume.hxx"
38 #include "utilities.h"
40 #include "DriverMED_W_SMESHDS_Mesh.h"
41 #include "DriverDAT_W_SMDS_Mesh.h"
42 #include "DriverUNV_W_SMDS_Mesh.h"
43 #include "DriverSTL_W_SMDS_Mesh.h"
45 #include "DriverMED_R_SMESHDS_Mesh.h"
46 #include "DriverUNV_R_SMDS_Mesh.h"
47 #include "DriverSTL_R_SMDS_Mesh.h"
49 #include <BRepTools_WireExplorer.hxx>
50 #include <BRep_Builder.hxx>
53 #include <TCollection_AsciiString.hxx>
55 #include <TopTools_ListOfShape.hxx>
56 #include <TopTools_Array1OfShape.hxx>
57 #include <TopTools_ListIteratorOfListOfShape.hxx>
61 #include "Utils_ExceptHandlers.hxx"
64 static int MYDEBUG = 0;
66 static int MYDEBUG = 0;
70 //=============================================================================
74 //=============================================================================
76 SMESH_Mesh::SMESH_Mesh(int localId, int studyId, SMESH_Gen * gen, SMESHDS_Document * myDocument)
79 INFOS("SMESH_Mesh::SMESH_Mesh(int localId)");
83 _myDocument = myDocument;
84 _idDoc = _myDocument->NewMesh();
85 _myMeshDS = _myDocument->GetMesh(_idDoc);
86 _isShapeToMesh = false;
89 //=============================================================================
93 //=============================================================================
95 SMESH_Mesh::~SMESH_Mesh()
97 INFOS("SMESH_Mesh::~SMESH_Mesh");
100 map < int, SMESH_Group * >::iterator itg;
101 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
102 SMESH_Group *aGroup = (*itg).second;
107 //=============================================================================
111 //=============================================================================
113 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape){
114 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
117 SALOME_Exception(LOCALIZED
118 ("a shape to mesh as already been defined"));
119 _isShapeToMesh = true;
120 _myMeshDS->ShapeToMesh(aShape);
123 //EAP: 1/9/04 TopExp::MapShapes(aShape, _subShapes); USE the same map of _myMeshDS
126 //=======================================================================
127 //function : UNVToMesh
129 //=======================================================================
131 int SMESH_Mesh::UNVToMesh(const char* theFileName)
133 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
135 throw SALOME_Exception(LOCALIZED("a shape to mesh as already been defined"));
136 _isShapeToMesh = true;
137 DriverUNV_R_SMDS_Mesh myReader;
138 myReader.SetMesh(_myMeshDS);
139 myReader.SetFile(theFileName);
140 myReader.SetMeshId(-1);
143 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
144 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
145 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
146 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
151 //=======================================================================
152 //function : MEDToMesh
154 //=======================================================================
156 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
158 if(MYDEBUG) MESSAGE("MEDToMesh - theFileName = "<<theFileName<<", mesh name = "<<theMeshName);
160 throw SALOME_Exception(LOCALIZED("a shape to mesh as already been defined"));
161 _isShapeToMesh = true;
162 DriverMED_R_SMESHDS_Mesh myReader;
163 myReader.SetMesh(_myMeshDS);
164 myReader.SetMeshId(-1);
165 myReader.SetFile(theFileName);
166 myReader.SetMeshName(theMeshName);
167 Driver_Mesh::Status status = myReader.Perform();
169 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
170 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
171 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
172 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
175 // Reading groups (sub-meshes are out of scope of MED import functionality)
176 list<string> aGroupNames = myReader.GetGroupNames();
177 if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size());
179 for ( list<string>::iterator it = aGroupNames.begin(); it != aGroupNames.end(); it++ ) {
180 SMESH_Group* aGroup = AddGroup( SMDSAbs_All, it->c_str(), anId );
182 if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<it->c_str());
183 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
185 aGroupDS->SetStoreName( it->c_str() );
186 myReader.GetGroup( aGroupDS );
193 //=======================================================================
194 //function : STLToMesh
196 //=======================================================================
198 int SMESH_Mesh::STLToMesh(const char* theFileName)
200 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
202 throw SALOME_Exception(LOCALIZED("a shape to mesh as already been defined"));
203 _isShapeToMesh = true;
204 DriverSTL_R_SMDS_Mesh myReader;
205 myReader.SetMesh(_myMeshDS);
206 myReader.SetFile(theFileName);
207 myReader.SetMeshId(-1);
210 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
211 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
212 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
213 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
218 //=============================================================================
222 //=============================================================================
224 SMESH_Hypothesis::Hypothesis_Status
225 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
226 int anHypId ) throw(SALOME_Exception)
228 Unexpect aCatch(SalomeException);
229 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
231 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
232 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
233 if ( subMeshDS && subMeshDS->IsComplexSubmesh() )
235 // return the worst but not fatal state of all group memebers
236 SMESH_Hypothesis::Hypothesis_Status aBestRet, aWorstNotFatal, ret;
237 aBestRet = SMESH_Hypothesis::HYP_BAD_DIM;
238 aWorstNotFatal = SMESH_Hypothesis::HYP_OK;
239 for ( TopoDS_Iterator itS ( aSubShape ); itS.More(); itS.Next())
241 ret = AddHypothesis( itS.Value(), anHypId );
242 if ( !SMESH_Hypothesis::IsStatusFatal( ret ) && ret > aWorstNotFatal )
243 aWorstNotFatal = ret;
244 if ( ret < aBestRet )
247 if ( SMESH_Hypothesis::IsStatusFatal( aBestRet ))
249 return aWorstNotFatal;
252 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
253 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
255 if(MYDEBUG) MESSAGE("Hypothesis ID does not give an hypothesis");
260 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
263 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
264 MESSAGE( "SMESH_Mesh::AddHypothesis " << anHyp->GetName() );
266 bool isGlobalHyp = IsMainShape( aSubShape );
268 // NotConformAllowed can be only global
271 string hypName = anHyp->GetName();
272 if ( hypName == "NotConformAllowed" )
274 if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
275 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
282 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
283 event = SMESH_subMesh::ADD_HYP;
285 event = SMESH_subMesh::ADD_ALGO;
286 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
289 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
290 !subMesh->IsApplicableHypotesis( anHyp )) // is added on father
292 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
293 event = SMESH_subMesh::ADD_FATHER_HYP;
295 event = SMESH_subMesh::ADD_FATHER_ALGO;
296 SMESH_Hypothesis::Hypothesis_Status ret2 =
297 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
301 // check concurent hypotheses on ansestors
302 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
304 const map < int, SMESH_subMesh * >& smMap = subMesh->DependsOn();
305 map < int, SMESH_subMesh * >::const_iterator smIt = smMap.begin();
306 for ( ; smIt != smMap.end(); smIt++ ) {
307 if ( smIt->second->IsApplicableHypotesis( anHyp )) {
308 ret2 = smIt->second->CheckConcurentHypothesis( anHyp->GetType() );
318 if(MYDEBUG) subMesh->DumpAlgoState(true);
323 //=============================================================================
327 //=============================================================================
329 SMESH_Hypothesis::Hypothesis_Status
330 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
331 int anHypId)throw(SALOME_Exception)
333 Unexpect aCatch(SalomeException);
334 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
336 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
337 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
338 if ( subMeshDS && subMeshDS->IsComplexSubmesh() )
340 // return the worst but not fatal state of all group memebers
341 SMESH_Hypothesis::Hypothesis_Status aBestRet, aWorstNotFatal, ret;
342 aBestRet = SMESH_Hypothesis::HYP_BAD_DIM;
343 aWorstNotFatal = SMESH_Hypothesis::HYP_OK;
344 for ( TopoDS_Iterator itS ( aSubShape ); itS.More(); itS.Next())
346 ret = RemoveHypothesis( itS.Value(), anHypId );
347 if ( !SMESH_Hypothesis::IsStatusFatal( ret ) && ret > aWorstNotFatal )
348 aWorstNotFatal = ret;
349 if ( ret < aBestRet )
352 if ( SMESH_Hypothesis::IsStatusFatal( aBestRet ))
354 return aWorstNotFatal;
357 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
358 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
359 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
361 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
362 int hypType = anHyp->GetType();
363 if(MYDEBUG) SCRUTE(hypType);
368 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
369 event = SMESH_subMesh::REMOVE_HYP;
371 event = SMESH_subMesh::REMOVE_ALGO;
372 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
374 // there may appear concurrent hyps that were covered by the removed hyp
375 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
376 subMesh->IsApplicableHypotesis( anHyp ) &&
377 subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
378 ret = SMESH_Hypothesis::HYP_CONCURENT;
381 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
382 !subMesh->IsApplicableHypotesis( anHyp )) // is removed from father
384 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
385 event = SMESH_subMesh::REMOVE_FATHER_HYP;
387 event = SMESH_subMesh::REMOVE_FATHER_ALGO;
388 SMESH_Hypothesis::Hypothesis_Status ret2 =
389 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
390 if (ret2 > ret) // more severe
393 // check concurent hypotheses on ansestors
394 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
396 const map < int, SMESH_subMesh * >& smMap = subMesh->DependsOn();
397 map < int, SMESH_subMesh * >::const_iterator smIt = smMap.begin();
398 for ( ; smIt != smMap.end(); smIt++ ) {
399 if ( smIt->second->IsApplicableHypotesis( anHyp )) {
400 ret2 = smIt->second->CheckConcurentHypothesis( anHyp->GetType() );
410 if(MYDEBUG) subMesh->DumpAlgoState(true);
411 if(MYDEBUG) SCRUTE(ret);
415 //=============================================================================
419 //=============================================================================
421 SMESHDS_Mesh * SMESH_Mesh::GetMeshDS()
426 //=============================================================================
430 //=============================================================================
432 const list<const SMESHDS_Hypothesis*>&
433 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
434 throw(SALOME_Exception)
436 Unexpect aCatch(SalomeException);
437 return _myMeshDS->GetHypothesis(aSubShape);
440 //=============================================================================
444 //=============================================================================
446 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
448 Unexpect aCatch(SalomeException);
449 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
450 return _myMeshDS->GetScript()->GetCommands();
453 //=============================================================================
457 //=============================================================================
458 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
460 Unexpect aCatch(SalomeException);
461 if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
462 _myMeshDS->GetScript()->Clear();
465 //=============================================================================
469 //=============================================================================
471 int SMESH_Mesh::GetId()
473 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetId");
477 //=============================================================================
481 //=============================================================================
483 SMESH_Gen *SMESH_Mesh::GetGen()
488 //=============================================================================
490 * Get or Create the SMESH_subMesh object implementation
492 //=============================================================================
494 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
495 throw(SALOME_Exception)
497 Unexpect aCatch(SalomeException);
498 SMESH_subMesh *aSubMesh;
499 int index = _myMeshDS->ShapeToIndex(aSubShape);
501 // for submeshes on GEOM Group
502 if ( !index && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
503 TopoDS_Iterator it( aSubShape );
505 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
508 if (_mapSubMesh.find(index) != _mapSubMesh.end())
510 aSubMesh = _mapSubMesh[index];
514 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
515 _mapSubMesh[index] = aSubMesh;
520 //=============================================================================
522 * Get the SMESH_subMesh object implementation. Dont create it, return null
523 * if it does not exist.
525 //=============================================================================
527 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape)
528 throw(SALOME_Exception)
530 Unexpect aCatch(SalomeException);
531 bool isFound = false;
532 SMESH_subMesh *aSubMesh = NULL;
534 int index = _myMeshDS->ShapeToIndex(aSubShape);
535 if (_mapSubMesh.find(index) != _mapSubMesh.end())
537 aSubMesh = _mapSubMesh[index];
545 //=======================================================================
546 //function : IsUsedHypothesis
547 //purpose : Return True if anHyp is used to mesh aSubShape
548 //=======================================================================
550 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
551 const TopoDS_Shape & aSubShape)
553 // check if anHyp is applicable to aSubShape
554 SMESH_subMesh * subMesh = GetSubMeshContaining( aSubShape );
556 !subMesh->IsApplicableHypotesis(static_cast<SMESH_Hypothesis*>(anHyp)))
559 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape);
562 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
563 return ( anHyp == algo );
565 // algorithm parameter
568 // look trough hypotheses used by algo
569 const list <const SMESHDS_Hypothesis * >&usedHyps =
570 algo->GetUsedHypothesis(*this, aSubShape);
571 list <const SMESHDS_Hypothesis * >::const_iterator itl;
572 for (itl = usedHyps.begin(); itl != usedHyps.end(); itl++)
578 // look through all assigned hypotheses
580 const list <const SMESHDS_Hypothesis * >&usedHyps =
581 _myMeshDS->GetHypothesis( aSubShape );
582 list <const SMESHDS_Hypothesis * >::const_iterator itl;
583 for (itl = usedHyps.begin(); itl != usedHyps.end(); itl++)
589 TopTools_ListIteratorOfListOfShape it( GetAncestors( aSubShape ));
590 for (; it.More(); it.Next())
592 const list <const SMESHDS_Hypothesis * >&usedHyps =
593 _myMeshDS->GetHypothesis( aSubShape );
594 list <const SMESHDS_Hypothesis * >::const_iterator itl;
595 for (itl = usedHyps.begin(); itl != usedHyps.end(); itl++)
605 //=============================================================================
609 //=============================================================================
611 const list < SMESH_subMesh * >&
612 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
613 throw(SALOME_Exception)
615 Unexpect aCatch(SalomeException);
616 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
617 map < int, SMESH_subMesh * >::iterator itsm;
618 _subMeshesUsingHypothesisList.clear();
619 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
621 SMESH_subMesh *aSubMesh = (*itsm).second;
622 if ( IsUsedHypothesis ( anHyp, aSubMesh->GetSubShape() ))
623 _subMeshesUsingHypothesisList.push_back(aSubMesh);
625 return _subMeshesUsingHypothesisList;
628 //=============================================================================
632 //=============================================================================
634 void SMESH_Mesh::ExportMED(const char *file,
635 const char* theMeshName,
638 throw(SALOME_Exception)
640 Unexpect aCatch(SalomeException);
641 DriverMED_W_SMESHDS_Mesh myWriter;
642 myWriter.SetFile ( file, MED::EVersion(theVersion) );
643 myWriter.SetMesh ( _myMeshDS );
645 myWriter.SetMeshId ( _idDoc );
647 myWriter.SetMeshId ( -1 );
648 myWriter.SetMeshName( theMeshName );
651 if ( theAutoGroups ) {
652 myWriter.AddGroupOfNodes();
653 myWriter.AddGroupOfEdges();
654 myWriter.AddGroupOfFaces();
655 myWriter.AddGroupOfVolumes();
658 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
659 SMESH_Group* aGroup = it->second;
660 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
662 aGroupDS->SetStoreName( aGroup->GetName() );
663 myWriter.AddGroup( aGroupDS );
670 void SMESH_Mesh::ExportDAT(const char *file) throw(SALOME_Exception)
672 Unexpect aCatch(SalomeException);
673 DriverDAT_W_SMDS_Mesh myWriter;
674 myWriter.SetFile(string(file));
675 myWriter.SetMesh(_myMeshDS);
676 myWriter.SetMeshId(_idDoc);
680 void SMESH_Mesh::ExportUNV(const char *file) throw(SALOME_Exception)
682 Unexpect aCatch(SalomeException);
683 DriverUNV_W_SMDS_Mesh myWriter;
684 myWriter.SetFile(string(file));
685 myWriter.SetMesh(_myMeshDS);
686 myWriter.SetMeshId(_idDoc);
690 void SMESH_Mesh::ExportSTL(const char *file, const bool isascii) throw(SALOME_Exception)
692 Unexpect aCatch(SalomeException);
693 DriverSTL_W_SMDS_Mesh myWriter;
694 myWriter.SetFile(string(file));
695 myWriter.SetIsAscii( isascii );
696 myWriter.SetMesh(_myMeshDS);
697 myWriter.SetMeshId(_idDoc);
701 //=============================================================================
705 //=============================================================================
706 int SMESH_Mesh::NbNodes() throw(SALOME_Exception)
708 Unexpect aCatch(SalomeException);
709 return _myMeshDS->NbNodes();
712 //=============================================================================
716 //=============================================================================
717 int SMESH_Mesh::NbEdges() throw(SALOME_Exception)
719 Unexpect aCatch(SalomeException);
720 return _myMeshDS->NbEdges();
723 //=============================================================================
727 //=============================================================================
728 int SMESH_Mesh::NbFaces() throw(SALOME_Exception)
730 Unexpect aCatch(SalomeException);
731 return _myMeshDS->NbFaces();
734 ///////////////////////////////////////////////////////////////////////////////
735 /// Return the number of 3 nodes faces in the mesh. This method run in O(n)
736 ///////////////////////////////////////////////////////////////////////////////
737 int SMESH_Mesh::NbTriangles() throw(SALOME_Exception)
739 Unexpect aCatch(SalomeException);
742 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
743 while(itFaces->more()) if(itFaces->next()->NbNodes()==3) Nb++;
747 ///////////////////////////////////////////////////////////////////////////////
748 /// Return the number of 4 nodes faces in the mesh. This method run in O(n)
749 ///////////////////////////////////////////////////////////////////////////////
750 int SMESH_Mesh::NbQuadrangles() throw(SALOME_Exception)
752 Unexpect aCatch(SalomeException);
755 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
756 while(itFaces->more()) if(itFaces->next()->NbNodes()==4) Nb++;
760 //=============================================================================
764 //=============================================================================
765 int SMESH_Mesh::NbVolumes() throw(SALOME_Exception)
767 Unexpect aCatch(SalomeException);
768 return _myMeshDS->NbVolumes();
771 int SMESH_Mesh::NbTetras() throw(SALOME_Exception)
773 Unexpect aCatch(SalomeException);
775 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
776 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==4) Nb++;
780 int SMESH_Mesh::NbHexas() throw(SALOME_Exception)
782 Unexpect aCatch(SalomeException);
784 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
785 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==8) Nb++;
789 int SMESH_Mesh::NbPyramids() throw(SALOME_Exception)
791 Unexpect aCatch(SalomeException);
793 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
794 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==5) Nb++;
798 int SMESH_Mesh::NbPrisms() throw(SALOME_Exception)
800 Unexpect aCatch(SalomeException);
802 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
803 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==6) Nb++;
807 //=============================================================================
811 //=============================================================================
812 int SMESH_Mesh::NbSubMesh() throw(SALOME_Exception)
814 Unexpect aCatch(SalomeException);
815 return _myMeshDS->NbSubMesh();
818 //=======================================================================
819 //function : IsNotConformAllowed
820 //purpose : check if a hypothesis alowing notconform mesh is present
821 //=======================================================================
823 bool SMESH_Mesh::IsNotConformAllowed() const
825 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
827 const list<const SMESHDS_Hypothesis*>& listHyp =
828 _myMeshDS->GetHypothesis( _myMeshDS->ShapeToMesh() );
829 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
830 while (it!=listHyp.end())
832 const SMESHDS_Hypothesis *aHyp = *it;
833 string hypName = aHyp->GetName();
834 if ( hypName == "NotConformAllowed" )
841 //=======================================================================
842 //function : IsMainShape
844 //=======================================================================
846 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
848 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
851 //=============================================================================
855 //=============================================================================
857 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
860 const TopoDS_Shape& theShape)
862 if (_mapGroup.find(_groupId) != _mapGroup.end())
865 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape);
866 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
867 _mapGroup[_groupId++] = aGroup;
871 //=============================================================================
875 //=============================================================================
877 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
879 if (_mapGroup.find(theGroupID) == _mapGroup.end())
881 return _mapGroup[theGroupID];
885 //=============================================================================
889 //=============================================================================
891 list<int> SMESH_Mesh::GetGroupIds()
894 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
895 anIds.push_back( it->first );
901 //=============================================================================
905 //=============================================================================
907 void SMESH_Mesh::RemoveGroup (const int theGroupID)
909 if (_mapGroup.find(theGroupID) == _mapGroup.end())
911 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
912 _mapGroup.erase (theGroupID);
913 delete _mapGroup[theGroupID];
916 //=============================================================================
918 * IsLocal1DHypothesis
919 * Check, if there is 1D hypothesis assigned directly on <theEdge>
921 //=============================================================================
922 bool SMESH_Mesh::IsLocal1DHypothesis (const TopoDS_Shape& theEdge)
924 const SMESHDS_Mesh* meshDS = GetMeshDS();
925 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis(theEdge);
926 list<const SMESHDS_Hypothesis*>::const_iterator it = listHyp.begin();
928 for (; it != listHyp.end(); it++) {
929 const SMESH_Hypothesis * aHyp = static_cast<const SMESH_Hypothesis*>(*it);
930 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO &&
931 aHyp->GetDim() == 1) { // 1D Hypothesis found
938 //=============================================================================
940 * IsPropagationHypothesis
942 //=============================================================================
943 bool SMESH_Mesh::IsPropagationHypothesis (const TopoDS_Shape& theEdge)
945 return _mapPropagationChains.Contains(theEdge);
948 //=============================================================================
950 * IsPropagatedHypothesis
952 //=============================================================================
953 bool SMESH_Mesh::IsPropagatedHypothesis (const TopoDS_Shape& theEdge,
954 TopoDS_Shape& theMainEdge)
956 int nbChains = _mapPropagationChains.Extent();
957 for (int i = 1; i <= nbChains; i++) {
958 const TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.FindFromIndex(i);
959 if (aChain.Contains(theEdge)) {
960 theMainEdge = _mapPropagationChains.FindKey(i);
968 //=============================================================================
970 * CleanMeshOnPropagationChain
972 //=============================================================================
973 void SMESH_Mesh::CleanMeshOnPropagationChain (const TopoDS_Shape& theMainEdge)
975 const TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.FindFromKey(theMainEdge);
976 int i, nbEdges = aChain.Extent();
977 for (i = 1; i <= nbEdges; i++) {
978 TopoDS_Shape anEdge = aChain.FindKey(i);
979 SMESH_subMesh *subMesh = GetSubMesh(anEdge);
980 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
981 if (subMeshDS && subMeshDS->NbElements() > 0) {
982 subMesh->ComputeStateEngine(SMESH_subMesh::CLEANDEP);
987 //=============================================================================
989 * RebuildPropagationChains
990 * Rebuild all existing propagation chains.
991 * Have to be used, if 1D hypothesis have been assigned/removed to/from any edge
993 //=============================================================================
994 bool SMESH_Mesh::RebuildPropagationChains()
998 // Clean all chains, because they can be not up-to-date
999 int i, nbChains = _mapPropagationChains.Extent();
1000 for (i = 1; i <= nbChains; i++) {
1001 TopoDS_Shape aMainEdge = _mapPropagationChains.FindKey(i);
1002 CleanMeshOnPropagationChain(aMainEdge);
1003 _mapPropagationChains.ChangeFromIndex(i).Clear();
1007 for (i = 1; i <= nbChains; i++) {
1008 TopoDS_Shape aMainEdge = _mapPropagationChains.FindKey(i);
1009 if (!BuildPropagationChain(aMainEdge))
1011 CleanMeshOnPropagationChain(aMainEdge);
1017 //=============================================================================
1019 * RemovePropagationChain
1020 * Have to be used, if Propagation hypothesis is removed from <theMainEdge>
1022 //=============================================================================
1023 bool SMESH_Mesh::RemovePropagationChain (const TopoDS_Shape& theMainEdge)
1025 if (!_mapPropagationChains.Contains(theMainEdge))
1028 // Clean mesh elements and nodes, built on the chain
1029 CleanMeshOnPropagationChain(theMainEdge);
1032 _mapPropagationChains.ChangeFromKey(theMainEdge).Clear();
1034 // Remove the chain from the map
1035 int i = _mapPropagationChains.FindIndex(theMainEdge);
1036 TopoDS_Vertex anEmptyShape;
1038 BB.MakeVertex(anEmptyShape, gp_Pnt(0,0,0), 0.1);
1039 TopTools_IndexedMapOfShape anEmptyMap;
1040 _mapPropagationChains.Substitute(i, anEmptyShape, anEmptyMap);
1045 //=============================================================================
1047 * BuildPropagationChain
1049 //=============================================================================
1050 bool SMESH_Mesh::BuildPropagationChain (const TopoDS_Shape& theMainEdge)
1052 if (theMainEdge.ShapeType() != TopAbs_EDGE) return true;
1054 // Add new chain, if there is no
1055 if (!_mapPropagationChains.Contains(theMainEdge)) {
1056 TopTools_IndexedMapOfShape aNewChain;
1057 _mapPropagationChains.Add(theMainEdge, aNewChain);
1060 // Check presence of 1D hypothesis to be propagated
1061 if (!IsLocal1DHypothesis(theMainEdge)) {
1062 MESSAGE("Warning: There is no 1D hypothesis to propagate. Please, assign.");
1066 // Edges, on which the 1D hypothesis will be propagated from <theMainEdge>
1067 TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.ChangeFromKey(theMainEdge);
1068 if (aChain.Extent() > 0) {
1069 CleanMeshOnPropagationChain(theMainEdge);
1073 // At first put <theMainEdge> in the chain
1074 aChain.Add(theMainEdge);
1076 // List of edges, added to chain on the previous cycle pass
1077 TopTools_ListOfShape listPrevEdges;
1078 listPrevEdges.Append(theMainEdge);
1080 // 5____4____3____4____5____6
1083 // 4____3____2____3____4____5
1084 // | | | | | | Number in the each knot of
1085 // | | | | | | grid indicates cycle pass,
1086 // 3____2____1____2____3____4 on which corresponding edge
1087 // | | | | | | (perpendicular to the plane
1088 // | | | | | | of view) will be found.
1089 // 2____1____0____1____2____3
1092 // 3____2____1____2____3____4
1094 // Collect all edges pass by pass
1095 while (listPrevEdges.Extent() > 0) {
1096 // List of edges, added to chain on this cycle pass
1097 TopTools_ListOfShape listCurEdges;
1099 // Find the next portion of edges
1100 TopTools_ListIteratorOfListOfShape itE (listPrevEdges);
1101 for (; itE.More(); itE.Next()) {
1102 TopoDS_Shape anE = itE.Value();
1104 // Iterate on faces, having edge <anE>
1105 TopTools_ListIteratorOfListOfShape itA (GetAncestors(anE));
1106 for (; itA.More(); itA.Next()) {
1107 TopoDS_Shape aW = itA.Value();
1109 // There are objects of different type among the ancestors of edge
1110 if (aW.ShapeType() == TopAbs_WIRE) {
1111 TopoDS_Shape anOppE;
1113 BRepTools_WireExplorer aWE (TopoDS::Wire(aW));
1114 Standard_Integer nb = 1, found = 0;
1115 TopTools_Array1OfShape anEdges (1,4);
1116 for (; aWE.More(); aWE.Next(), nb++) {
1121 anEdges(nb) = aWE.Current();
1122 if (!_mapAncestors.Contains(anEdges(nb))) {
1123 MESSAGE("WIRE EXPLORER HAVE GIVEN AN INVALID EDGE !!!");
1126 int ind = _mapAncestors.FindIndex(anEdges(nb));
1127 anEdges(nb) = _mapAncestors.FindKey(ind);
1129 if (anEdges(nb).IsSame(anE)) found = nb;
1132 if (nb == 5 && found > 0) {
1133 // Quadrangle face found, get an opposite edge
1134 Standard_Integer opp = found + 2;
1135 if (opp > 4) opp -= 4;
1136 anOppE = anEdges(opp);
1138 if (!aChain.Contains(anOppE)) {
1139 if (!IsLocal1DHypothesis(anOppE)) {
1140 TopoDS_Shape aMainEdgeForOppEdge;
1141 if (IsPropagatedHypothesis(anOppE, aMainEdgeForOppEdge)) {
1143 MESSAGE("Error: Collision between propagated hypotheses");
1144 CleanMeshOnPropagationChain(theMainEdge);
1148 // Add found edge to the chain
1150 listCurEdges.Append(anOppE);
1154 } // if (nb == 5 && found > 0)
1155 } // if (aF.ShapeType() == TopAbs_WIRE)
1156 } // for (; itF.More(); itF.Next())
1157 } // for (; itE.More(); itE.Next())
1159 listPrevEdges = listCurEdges;
1160 } // while (listPrevEdges.Extent() > 0)
1162 CleanMeshOnPropagationChain(theMainEdge);
1166 //=======================================================================
1167 //function : GetAncestors
1168 //purpose : return list of ancestors of theSubShape in the order
1169 // that lower dimention shapes come first.
1170 //=======================================================================
1172 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS)
1174 if ( _mapAncestors.IsEmpty() )
1176 // fill _mapAncestors
1177 int desType, ancType;
1178 for ( desType = TopAbs_EDGE; desType > TopAbs_COMPOUND; desType-- )
1179 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
1180 TopExp::MapShapesAndAncestors (_myMeshDS->ShapeToMesh(),
1181 (TopAbs_ShapeEnum) desType,
1182 (TopAbs_ShapeEnum) ancType,
1186 if ( _mapAncestors.Contains( theS ) )
1187 return _mapAncestors.FindFromKey( theS );
1189 static TopTools_ListOfShape emptyList;
1193 //=======================================================================
1195 //purpose : dumps contents of mesh to stream [ debug purposes ]
1196 //=======================================================================
1197 ostream& SMESH_Mesh::Dump(ostream& save)
1199 save << "========================== Dump contents of mesh ==========================" << endl;
1200 save << "1) Total number of nodes: " << NbNodes() << endl;
1201 save << "2) Total number of edges: " << NbEdges() << endl;
1202 save << "3) Total number of faces: " << NbFaces() << endl;
1203 if ( NbFaces() > 0 ) {
1204 int nb3 = NbTriangles();
1205 int nb4 = NbQuadrangles();
1206 save << "3.1.) Number of triangles: " << nb3 << endl;
1207 save << "3.2.) Number of quadrangles: " << nb4 << endl;
1208 if ( nb3 + nb4 != NbFaces() ) {
1209 map<int,int> myFaceMap;
1210 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1211 while( itFaces->more( ) ) {
1212 int nbNodes = itFaces->next()->NbNodes();
1213 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1214 myFaceMap[ nbNodes ] = 0;
1215 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1217 save << "3.3.) Faces in detail: " << endl;
1218 map <int,int>::iterator itF;
1219 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1220 save << "--> nb nodes: " << itF->first << " - nb elemens: " << itF->second << endl;
1223 save << "4) Total number of volumes: " << NbVolumes() << endl;
1224 if ( NbVolumes() > 0 ) {
1225 int nb8 = NbHexas();
1226 int nb4 = NbTetras();
1227 int nb5 = NbPyramids();
1228 int nb6 = NbPrisms();
1229 save << "4.1.) Number of hexahedrons: " << nb8 << endl;
1230 save << "4.2.) Number of tetrahedrons: " << nb4 << endl;
1231 save << "4.3.) Number of prisms: " << nb6 << endl;
1232 save << "4.4.) Number of pyramides: " << nb5 << endl;
1233 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes() ) {
1234 map<int,int> myVolumesMap;
1235 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1236 while( itVolumes->more( ) ) {
1237 int nbNodes = itVolumes->next()->NbNodes();
1238 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1239 myVolumesMap[ nbNodes ] = 0;
1240 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1242 save << "4.5.) Volumes in detail: " << endl;
1243 map <int,int>::iterator itV;
1244 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1245 save << "--> nb nodes: " << itV->first << " - nb elemens: " << itV->second << endl;
1248 save << "===========================================================================" << endl;