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, const char* theMeshName, bool theAutoGroups) throw(SALOME_Exception)
636 Unexpect aCatch(SalomeException);
637 DriverMED_W_SMESHDS_Mesh myWriter;
638 myWriter.SetFile ( file );
639 myWriter.SetMesh ( _myMeshDS );
641 myWriter.SetMeshId ( _idDoc );
643 myWriter.SetMeshId ( -1 );
644 myWriter.SetMeshName( theMeshName );
647 if ( theAutoGroups ) {
648 myWriter.AddGroupOfNodes();
649 myWriter.AddGroupOfEdges();
650 myWriter.AddGroupOfFaces();
651 myWriter.AddGroupOfVolumes();
654 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
655 SMESH_Group* aGroup = it->second;
656 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
658 aGroupDS->SetStoreName( aGroup->GetName() );
659 myWriter.AddGroup( aGroupDS );
666 void SMESH_Mesh::ExportDAT(const char *file) throw(SALOME_Exception)
668 Unexpect aCatch(SalomeException);
669 DriverDAT_W_SMDS_Mesh myWriter;
670 myWriter.SetFile(string(file));
671 myWriter.SetMesh(_myMeshDS);
672 myWriter.SetMeshId(_idDoc);
676 void SMESH_Mesh::ExportUNV(const char *file) throw(SALOME_Exception)
678 Unexpect aCatch(SalomeException);
679 DriverUNV_W_SMDS_Mesh myWriter;
680 myWriter.SetFile(string(file));
681 myWriter.SetMesh(_myMeshDS);
682 myWriter.SetMeshId(_idDoc);
686 void SMESH_Mesh::ExportSTL(const char *file, const bool isascii) throw(SALOME_Exception)
688 Unexpect aCatch(SalomeException);
689 DriverSTL_W_SMDS_Mesh myWriter;
690 myWriter.SetFile(string(file));
691 myWriter.SetIsAscii( isascii );
692 myWriter.SetMesh(_myMeshDS);
693 myWriter.SetMeshId(_idDoc);
697 //=============================================================================
701 //=============================================================================
702 int SMESH_Mesh::NbNodes() throw(SALOME_Exception)
704 Unexpect aCatch(SalomeException);
705 return _myMeshDS->NbNodes();
708 //=============================================================================
712 //=============================================================================
713 int SMESH_Mesh::NbEdges() throw(SALOME_Exception)
715 Unexpect aCatch(SalomeException);
716 return _myMeshDS->NbEdges();
719 //=============================================================================
723 //=============================================================================
724 int SMESH_Mesh::NbFaces() throw(SALOME_Exception)
726 Unexpect aCatch(SalomeException);
727 return _myMeshDS->NbFaces();
730 ///////////////////////////////////////////////////////////////////////////////
731 /// Return the number of 3 nodes faces in the mesh. This method run in O(n)
732 ///////////////////////////////////////////////////////////////////////////////
733 int SMESH_Mesh::NbTriangles() throw(SALOME_Exception)
735 Unexpect aCatch(SalomeException);
738 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
739 while(itFaces->more()) if(itFaces->next()->NbNodes()==3) Nb++;
743 ///////////////////////////////////////////////////////////////////////////////
744 /// Return the number of 4 nodes faces in the mesh. This method run in O(n)
745 ///////////////////////////////////////////////////////////////////////////////
746 int SMESH_Mesh::NbQuadrangles() throw(SALOME_Exception)
748 Unexpect aCatch(SalomeException);
751 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
752 while(itFaces->more()) if(itFaces->next()->NbNodes()==4) Nb++;
756 //=============================================================================
760 //=============================================================================
761 int SMESH_Mesh::NbVolumes() throw(SALOME_Exception)
763 Unexpect aCatch(SalomeException);
764 return _myMeshDS->NbVolumes();
767 int SMESH_Mesh::NbTetras() throw(SALOME_Exception)
769 Unexpect aCatch(SalomeException);
771 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
772 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==4) Nb++;
776 int SMESH_Mesh::NbHexas() throw(SALOME_Exception)
778 Unexpect aCatch(SalomeException);
780 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
781 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==8) Nb++;
785 int SMESH_Mesh::NbPyramids() throw(SALOME_Exception)
787 Unexpect aCatch(SalomeException);
789 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
790 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==5) Nb++;
794 int SMESH_Mesh::NbPrisms() throw(SALOME_Exception)
796 Unexpect aCatch(SalomeException);
798 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
799 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==6) Nb++;
803 //=============================================================================
807 //=============================================================================
808 int SMESH_Mesh::NbSubMesh() throw(SALOME_Exception)
810 Unexpect aCatch(SalomeException);
811 return _myMeshDS->NbSubMesh();
814 //=======================================================================
815 //function : IsNotConformAllowed
816 //purpose : check if a hypothesis alowing notconform mesh is present
817 //=======================================================================
819 bool SMESH_Mesh::IsNotConformAllowed() const
821 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
823 const list<const SMESHDS_Hypothesis*>& listHyp =
824 _myMeshDS->GetHypothesis( _myMeshDS->ShapeToMesh() );
825 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
826 while (it!=listHyp.end())
828 const SMESHDS_Hypothesis *aHyp = *it;
829 string hypName = aHyp->GetName();
830 if ( hypName == "NotConformAllowed" )
837 //=======================================================================
838 //function : IsMainShape
840 //=======================================================================
842 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
844 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
847 //=============================================================================
851 //=============================================================================
853 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
856 const TopoDS_Shape& theShape)
858 if (_mapGroup.find(_groupId) != _mapGroup.end())
861 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape);
862 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
863 _mapGroup[_groupId++] = aGroup;
867 //=============================================================================
871 //=============================================================================
873 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
875 if (_mapGroup.find(theGroupID) == _mapGroup.end())
877 return _mapGroup[theGroupID];
881 //=============================================================================
885 //=============================================================================
887 list<int> SMESH_Mesh::GetGroupIds()
890 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
891 anIds.push_back( it->first );
897 //=============================================================================
901 //=============================================================================
903 void SMESH_Mesh::RemoveGroup (const int theGroupID)
905 if (_mapGroup.find(theGroupID) == _mapGroup.end())
907 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
908 _mapGroup.erase (theGroupID);
909 delete _mapGroup[theGroupID];
912 //=============================================================================
914 * IsLocal1DHypothesis
915 * Check, if there is 1D hypothesis assigned directly on <theEdge>
917 //=============================================================================
918 bool SMESH_Mesh::IsLocal1DHypothesis (const TopoDS_Shape& theEdge)
920 const SMESHDS_Mesh* meshDS = GetMeshDS();
921 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis(theEdge);
922 list<const SMESHDS_Hypothesis*>::const_iterator it = listHyp.begin();
924 for (; it != listHyp.end(); it++) {
925 const SMESH_Hypothesis * aHyp = static_cast<const SMESH_Hypothesis*>(*it);
926 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO &&
927 aHyp->GetDim() == 1) { // 1D Hypothesis found
934 //=============================================================================
936 * IsPropagationHypothesis
938 //=============================================================================
939 bool SMESH_Mesh::IsPropagationHypothesis (const TopoDS_Shape& theEdge)
941 return _mapPropagationChains.Contains(theEdge);
944 //=============================================================================
946 * IsPropagatedHypothesis
948 //=============================================================================
949 bool SMESH_Mesh::IsPropagatedHypothesis (const TopoDS_Shape& theEdge,
950 TopoDS_Shape& theMainEdge)
952 int nbChains = _mapPropagationChains.Extent();
953 for (int i = 1; i <= nbChains; i++) {
954 const TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.FindFromIndex(i);
955 if (aChain.Contains(theEdge)) {
956 theMainEdge = _mapPropagationChains.FindKey(i);
964 //=============================================================================
966 * CleanMeshOnPropagationChain
968 //=============================================================================
969 void SMESH_Mesh::CleanMeshOnPropagationChain (const TopoDS_Shape& theMainEdge)
971 const TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.FindFromKey(theMainEdge);
972 int i, nbEdges = aChain.Extent();
973 for (i = 1; i <= nbEdges; i++) {
974 TopoDS_Shape anEdge = aChain.FindKey(i);
975 SMESH_subMesh *subMesh = GetSubMesh(anEdge);
976 SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
977 if (subMeshDS && subMeshDS->NbElements() > 0) {
978 subMesh->ComputeStateEngine(SMESH_subMesh::CLEANDEP);
983 //=============================================================================
985 * RebuildPropagationChains
986 * Rebuild all existing propagation chains.
987 * Have to be used, if 1D hypothesis have been assigned/removed to/from any edge
989 //=============================================================================
990 bool SMESH_Mesh::RebuildPropagationChains()
994 // Clean all chains, because they can be not up-to-date
995 int i, nbChains = _mapPropagationChains.Extent();
996 for (i = 1; i <= nbChains; i++) {
997 TopoDS_Shape aMainEdge = _mapPropagationChains.FindKey(i);
998 CleanMeshOnPropagationChain(aMainEdge);
999 _mapPropagationChains.ChangeFromIndex(i).Clear();
1003 for (i = 1; i <= nbChains; i++) {
1004 TopoDS_Shape aMainEdge = _mapPropagationChains.FindKey(i);
1005 if (!BuildPropagationChain(aMainEdge))
1007 CleanMeshOnPropagationChain(aMainEdge);
1013 //=============================================================================
1015 * RemovePropagationChain
1016 * Have to be used, if Propagation hypothesis is removed from <theMainEdge>
1018 //=============================================================================
1019 bool SMESH_Mesh::RemovePropagationChain (const TopoDS_Shape& theMainEdge)
1021 if (!_mapPropagationChains.Contains(theMainEdge))
1024 // Clean mesh elements and nodes, built on the chain
1025 CleanMeshOnPropagationChain(theMainEdge);
1028 _mapPropagationChains.ChangeFromKey(theMainEdge).Clear();
1030 // Remove the chain from the map
1031 int i = _mapPropagationChains.FindIndex(theMainEdge);
1032 TopoDS_Vertex anEmptyShape;
1034 BB.MakeVertex(anEmptyShape, gp_Pnt(0,0,0), 0.1);
1035 TopTools_IndexedMapOfShape anEmptyMap;
1036 _mapPropagationChains.Substitute(i, anEmptyShape, anEmptyMap);
1041 //=============================================================================
1043 * BuildPropagationChain
1045 //=============================================================================
1046 bool SMESH_Mesh::BuildPropagationChain (const TopoDS_Shape& theMainEdge)
1048 if (theMainEdge.ShapeType() != TopAbs_EDGE) return true;
1050 // Add new chain, if there is no
1051 if (!_mapPropagationChains.Contains(theMainEdge)) {
1052 TopTools_IndexedMapOfShape aNewChain;
1053 _mapPropagationChains.Add(theMainEdge, aNewChain);
1056 // Check presence of 1D hypothesis to be propagated
1057 if (!IsLocal1DHypothesis(theMainEdge)) {
1058 MESSAGE("Warning: There is no 1D hypothesis to propagate. Please, assign.");
1062 // Edges, on which the 1D hypothesis will be propagated from <theMainEdge>
1063 TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.ChangeFromKey(theMainEdge);
1064 if (aChain.Extent() > 0) {
1065 CleanMeshOnPropagationChain(theMainEdge);
1069 // At first put <theMainEdge> in the chain
1070 aChain.Add(theMainEdge);
1072 // List of edges, added to chain on the previous cycle pass
1073 TopTools_ListOfShape listPrevEdges;
1074 listPrevEdges.Append(theMainEdge);
1076 // 5____4____3____4____5____6
1079 // 4____3____2____3____4____5
1080 // | | | | | | Number in the each knot of
1081 // | | | | | | grid indicates cycle pass,
1082 // 3____2____1____2____3____4 on which corresponding edge
1083 // | | | | | | (perpendicular to the plane
1084 // | | | | | | of view) will be found.
1085 // 2____1____0____1____2____3
1088 // 3____2____1____2____3____4
1090 // Collect all edges pass by pass
1091 while (listPrevEdges.Extent() > 0) {
1092 // List of edges, added to chain on this cycle pass
1093 TopTools_ListOfShape listCurEdges;
1095 // Find the next portion of edges
1096 TopTools_ListIteratorOfListOfShape itE (listPrevEdges);
1097 for (; itE.More(); itE.Next()) {
1098 TopoDS_Shape anE = itE.Value();
1100 // Iterate on faces, having edge <anE>
1101 TopTools_ListIteratorOfListOfShape itA (GetAncestors(anE));
1102 for (; itA.More(); itA.Next()) {
1103 TopoDS_Shape aW = itA.Value();
1105 // There are objects of different type among the ancestors of edge
1106 if (aW.ShapeType() == TopAbs_WIRE) {
1107 TopoDS_Shape anOppE;
1109 BRepTools_WireExplorer aWE (TopoDS::Wire(aW));
1110 Standard_Integer nb = 1, found = 0;
1111 TopTools_Array1OfShape anEdges (1,4);
1112 for (; aWE.More(); aWE.Next(), nb++) {
1117 anEdges(nb) = aWE.Current();
1118 if (!_mapAncestors.Contains(anEdges(nb))) {
1119 MESSAGE("WIRE EXPLORER HAVE GIVEN AN INVALID EDGE !!!");
1122 int ind = _mapAncestors.FindIndex(anEdges(nb));
1123 anEdges(nb) = _mapAncestors.FindKey(ind);
1125 if (anEdges(nb).IsSame(anE)) found = nb;
1128 if (nb == 5 && found > 0) {
1129 // Quadrangle face found, get an opposite edge
1130 Standard_Integer opp = found + 2;
1131 if (opp > 4) opp -= 4;
1132 anOppE = anEdges(opp);
1134 if (!aChain.Contains(anOppE)) {
1135 if (!IsLocal1DHypothesis(anOppE)) {
1136 TopoDS_Shape aMainEdgeForOppEdge;
1137 if (IsPropagatedHypothesis(anOppE, aMainEdgeForOppEdge)) {
1139 MESSAGE("Error: Collision between propagated hypotheses");
1140 CleanMeshOnPropagationChain(theMainEdge);
1144 // Add found edge to the chain
1146 listCurEdges.Append(anOppE);
1150 } // if (nb == 5 && found > 0)
1151 } // if (aF.ShapeType() == TopAbs_WIRE)
1152 } // for (; itF.More(); itF.Next())
1153 } // for (; itE.More(); itE.Next())
1155 listPrevEdges = listCurEdges;
1156 } // while (listPrevEdges.Extent() > 0)
1158 CleanMeshOnPropagationChain(theMainEdge);
1162 //=======================================================================
1163 //function : GetAncestors
1164 //purpose : return list of ancestors of theSubShape in the order
1165 // that lower dimention shapes come first.
1166 //=======================================================================
1168 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS)
1170 if ( _mapAncestors.IsEmpty() )
1172 // fill _mapAncestors
1173 int desType, ancType;
1174 for ( desType = TopAbs_EDGE; desType > TopAbs_COMPOUND; desType-- )
1175 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
1176 TopExp::MapShapesAndAncestors (_myMeshDS->ShapeToMesh(),
1177 (TopAbs_ShapeEnum) desType,
1178 (TopAbs_ShapeEnum) ancType,
1182 if ( _mapAncestors.Contains( theS ) )
1183 return _mapAncestors.FindFromKey( theS );
1185 static TopTools_ListOfShape emptyList;
1189 //=======================================================================
1191 //purpose : dumps contents of mesh to stream [ debug purposes ]
1192 //=======================================================================
1193 ostream& SMESH_Mesh::Dump(ostream& save)
1195 save << "========================== Dump contents of mesh ==========================" << endl;
1196 save << "1) Total number of nodes: " << NbNodes() << endl;
1197 save << "2) Total number of edges: " << NbEdges() << endl;
1198 save << "3) Total number of faces: " << NbFaces() << endl;
1199 if ( NbFaces() > 0 ) {
1200 int nb3 = NbTriangles();
1201 int nb4 = NbQuadrangles();
1202 save << "3.1.) Number of triangles: " << nb3 << endl;
1203 save << "3.2.) Number of quadrangles: " << nb4 << endl;
1204 if ( nb3 + nb4 != NbFaces() ) {
1205 map<int,int> myFaceMap;
1206 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1207 while( itFaces->more( ) ) {
1208 int nbNodes = itFaces->next()->NbNodes();
1209 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1210 myFaceMap[ nbNodes ] = 0;
1211 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1213 save << "3.3.) Faces in detail: " << endl;
1214 map <int,int>::iterator itF;
1215 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1216 save << "--> nb nodes: " << itF->first << " - nb elemens: " << itF->second << endl;
1219 save << "4) Total number of volumes: " << NbVolumes() << endl;
1220 if ( NbVolumes() > 0 ) {
1221 int nb8 = NbHexas();
1222 int nb4 = NbTetras();
1223 int nb5 = NbPyramids();
1224 int nb6 = NbPrisms();
1225 save << "4.1.) Number of hexahedrons: " << nb8 << endl;
1226 save << "4.2.) Number of tetrahedrons: " << nb4 << endl;
1227 save << "4.3.) Number of prisms: " << nb6 << endl;
1228 save << "4.4.) Number of pyramides: " << nb5 << endl;
1229 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes() ) {
1230 map<int,int> myVolumesMap;
1231 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1232 while( itVolumes->more( ) ) {
1233 int nbNodes = itVolumes->next()->NbNodes();
1234 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1235 myVolumesMap[ nbNodes ] = 0;
1236 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1238 save << "4.5.) Volumes in detail: " << endl;
1239 map <int,int>::iterator itV;
1240 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1241 save << "--> nb nodes: " << itV->first << " - nb elemens: " << itV->second << endl;
1244 save << "===========================================================================" << endl;