1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
29 #include "SMESH_subMesh.hxx"
31 #include "SMESH_Algo.hxx"
32 #include "SMESH_Gen.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMESH_Hypothesis.hxx"
35 #include "SMESH_Mesh.hxx"
36 #include "SMESH_MesherHelper.hxx"
37 #include "SMESH_subMeshEventListener.hxx"
38 #include "SMESH_Comment.hxx"
39 #include "SMDS_SetIterator.hxx"
41 #include "utilities.h"
44 #include <BRep_Builder.hxx>
45 #include <BRep_Tool.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopoDS_Compound.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopoDS_Iterator.hxx>
55 #include <Standard_OutOfMemory.hxx>
56 #include <Standard_ErrorHandler.hxx>
60 //=============================================================================
62 * \brief Allocate some memory at construction and release it at destruction.
63 * Is used to be able to continue working after mesh generation breaks due to
66 //=============================================================================
71 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
72 ~MemoryReserve() { delete [] myBuf; }
75 //=============================================================================
77 * default constructor:
79 //=============================================================================
81 SMESH_subMesh::SMESH_subMesh(int Id,
83 SMESHDS_Mesh * meshDS,
84 const TopoDS_Shape & aSubShape)
86 _subShape = aSubShape;
87 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
90 _dependenceAnalysed = _alwaysComputed = false;
92 if (_subShape.ShapeType() == TopAbs_VERTEX)
95 _computeState = READY_TO_COMPUTE;
100 _computeState = NOT_READY;
104 //=============================================================================
108 //=============================================================================
110 SMESH_subMesh::~SMESH_subMesh()
112 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
114 DeleteOwnListeners();
117 //=============================================================================
121 //=============================================================================
123 int SMESH_subMesh::GetId() const
125 //MESSAGE("SMESH_subMesh::GetId");
129 //=============================================================================
133 //=============================================================================
135 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
137 // submesh appears in DS only when a mesher set nodes and elements on a shape
138 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
141 //=============================================================================
145 //=============================================================================
147 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
149 if ( !GetSubMeshDS() ) {
150 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
151 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
153 return GetSubMeshDS();
156 //=============================================================================
160 //=============================================================================
162 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
164 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
165 while ( smIt->more() ) {
166 SMESH_subMesh *sm = smIt->next();
167 if ( sm->GetComputeState() == READY_TO_COMPUTE )
170 return 0; // nothing to compute
173 //================================================================================
175 * \brief Allow algo->Compute() if a subshape of lower dim is meshed but
176 * none mesh entity is bound to it (PAL13615, 2nd part)
178 //================================================================================
180 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
182 _alwaysComputed = isAlCo;
183 if ( _alwaysComputed )
184 _computeState = COMPUTE_OK;
186 ComputeStateEngine( CHECK_COMPUTE_STATE );
189 //=======================================================================
191 * \brief Return true if no mesh entities is bound to the submesh
193 //=======================================================================
195 bool SMESH_subMesh::IsEmpty() const
197 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
198 return (!subMeshDS->GetNodes()->more() && !subMeshDS->GetElements()->more());
202 //=======================================================================
203 //function : IsMeshComputed
204 //purpose : check if _subMeshDS contains mesh elements
205 //=======================================================================
207 bool SMESH_subMesh::IsMeshComputed() const
209 if ( _alwaysComputed )
211 // algo may bind a submesh not to _subShape, eg 3D algo
212 // sets nodes on SHELL while _subShape may be SOLID
214 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
215 int dim = SMESH_Gen::GetShapeDim( _subShape );
216 int type = _subShape.ShapeType();
217 for ( ; type <= TopAbs_VERTEX; type++) {
218 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
220 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
221 for ( ; exp.More(); exp.Next() )
223 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
225 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
238 //=============================================================================
242 //=============================================================================
244 bool SMESH_subMesh::SubMeshesComputed()
246 int myDim = SMESH_Gen::GetShapeDim( _subShape );
247 int dimToCheck = myDim - 1;
248 bool subMeshesComputed = true;
249 // check subMeshes with upper dimension => reverse iteration
250 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
251 while ( smIt->more() )
253 SMESH_subMesh *sm = smIt->next();
254 if ( sm->_alwaysComputed )
256 const TopoDS_Shape & ss = sm->GetSubShape();
257 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
258 // in checking of existence of edges if the algo needs only faces. Moreover,
259 // degenerated edges may have no submesh, as after computing NETGEN_2D.
260 int dim = SMESH_Gen::GetShapeDim( ss );
261 if (dim < dimToCheck)
262 break; // the rest subMeshes are all of less dimension
263 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
264 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
265 (ds && ( ds->NbNodes() || ds->NbElements() )));
268 int type = ss.ShapeType();
270 subMeshesComputed = false;
274 case TopAbs_COMPOUND:
276 MESSAGE("The not computed sub mesh is a COMPOUND");
279 case TopAbs_COMPSOLID:
281 MESSAGE("The not computed sub mesh is a COMPSOLID");
286 MESSAGE("The not computed sub mesh is a SHEL");
291 MESSAGE("The not computed sub mesh is a WIRE");
296 MESSAGE("The not computed sub mesh is a SOLID");
301 MESSAGE("The not computed sub mesh is a FACE");
306 MESSAGE("The not computed sub mesh is a EDGE");
311 MESSAGE("The not computed sub mesh is of unknown type");
319 return subMeshesComputed;
322 //=============================================================================
326 //=============================================================================
328 bool SMESH_subMesh::SubMeshesReady()
330 bool subMeshesReady = true;
331 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
332 while ( smIt->more() ) {
333 SMESH_subMesh *sm = smIt->next();
334 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
335 sm->GetComputeState() == READY_TO_COMPUTE);
338 subMeshesReady = false;
343 return subMeshesReady;
346 //=============================================================================
348 * Construct dependence on first level subMeshes. complex shapes (compsolid,
349 * shell, wire) are not analysed the same way as simple shapes (solid, face,
351 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
352 * with possible multiples occurences. Multiples occurences corresponds to
353 * internal frontiers within shapes of the collection and must not be keeped.
354 * See FinalizeDependence.
356 //=============================================================================
358 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
360 if (_dependenceAnalysed)
363 //MESSAGE("SMESH_subMesh::DependsOn");
365 int type = _subShape.ShapeType();
369 case TopAbs_COMPOUND:
371 //MESSAGE("compound");
372 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
375 InsertDependence(exp.Current());
377 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
380 InsertDependence(exp.Current()); //only shell not in solid
382 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
385 InsertDependence(exp.Current());
387 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
390 InsertDependence(exp.Current());
394 case TopAbs_COMPSOLID:
396 //MESSAGE("compsolid");
397 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
400 InsertDependence(exp.Current());
407 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
410 InsertDependence(exp.Current());
417 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
420 InsertDependence(exp.Current());
427 if(_father->HasShapeToMesh()) {
428 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
431 InsertDependence(exp.Current());
439 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
442 InsertDependence(exp.Current());
449 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
452 InsertDependence(exp.Current());
465 _dependenceAnalysed = true;
469 //=============================================================================
471 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
473 //=============================================================================
475 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
477 //MESSAGE("SMESH_subMesh::InsertDependence");
478 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
479 int type = aSubShape.ShapeType();
480 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
481 int cle = aSubMesh->GetId();
482 cle += 10000000 * ordType; // sort map by ordType then index
483 if ( _mapDepend.find( cle ) == _mapDepend.end())
485 _mapDepend[cle] = aSubMesh;
486 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
487 _mapDepend.insert( subMap.begin(), subMap.end() );
491 //=============================================================================
495 //=============================================================================
497 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
499 //MESSAGE("SMESH_subMesh::GetSubShape");
504 //=======================================================================
505 //function : CanAddHypothesis
506 //purpose : return true if theHypothesis can be attached to me:
507 // its dimention is checked
508 //=======================================================================
510 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
512 int aHypDim = theHypothesis->GetDim();
513 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
514 if (aHypDim == 3 && aShapeDim == 3) {
515 // check case of open shell
516 //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
517 if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
520 if ( aHypDim <= aShapeDim )
526 //=======================================================================
527 //function : IsApplicableHypotesis
529 //=======================================================================
531 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
532 const TopAbs_ShapeEnum theShapeType)
534 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
536 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
539 switch ( theShapeType ) {
544 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
547 // Special case for algorithms, building 2D mesh on a whole shell.
548 // Before this fix there was a problem after restoring from study,
549 // because in that case algorithm is assigned before hypothesis
550 // (on shell in problem case) and hypothesis is checked on faces
551 // (because it is 2D), where we have NO_ALGO state.
552 // Now 2D hypothesis is also applicable to shells.
553 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
556 // case TopAbs_COMPSOLID:
557 // case TopAbs_COMPOUND:
563 //=============================================================================
567 //=============================================================================
569 SMESH_Hypothesis::Hypothesis_Status
570 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
572 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
573 //SCRUTE(_algoState);
576 // **** les retour des evenement shape sont significatifs
577 // (add ou remove fait ou non)
578 // le retour des evenement father n'indiquent pas que add ou remove fait
580 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
582 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
583 SMESH_Gen* gen =_father->GetGen();
584 SMESH_Algo* algo = 0;
586 if (_subShape.ShapeType() == TopAbs_VERTEX )
588 if ( anHyp->GetDim() != 0) {
589 if (event == ADD_HYP || event == ADD_ALGO)
590 return SMESH_Hypothesis::HYP_BAD_DIM;
592 return SMESH_Hypothesis::HYP_OK;
595 else if ( _algoState == HYP_OK ) {
596 // update default _algoState
597 if ( event != REMOVE_FATHER_ALGO )
599 _algoState = NO_ALGO;
600 algo = gen->GetAlgo(*_father, _subShape);
602 _algoState = MISSING_HYP;
603 if ( event == REMOVE_FATHER_HYP ||
604 algo->CheckHypothesis(*_father,_subShape, aux_ret))
611 int oldAlgoState = _algoState;
612 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
614 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
616 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
618 // -------------------------------------------
619 // check if a shape needed by algo is present
620 // -------------------------------------------
621 algo = static_cast< SMESH_Algo* >( anHyp );
622 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
623 return SMESH_Hypothesis::HYP_NEED_SHAPE;
624 // ----------------------
625 // check mesh conformity
626 // ----------------------
627 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
628 return SMESH_Hypothesis::HYP_NOTCONFORM;
631 // ----------------------------------
632 // add a hypothesis to DS if possible
633 // ----------------------------------
634 if (event == ADD_HYP || event == ADD_ALGO)
636 if ( ! CanAddHypothesis( anHyp )) // check dimension
637 return SMESH_Hypothesis::HYP_BAD_DIM;
639 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
640 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
642 if ( !meshDS->AddHypothesis(_subShape, anHyp))
643 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
646 // --------------------------
647 // remove a hypothesis from DS
648 // --------------------------
649 if (event == REMOVE_HYP || event == REMOVE_ALGO)
651 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
652 return SMESH_Hypothesis::HYP_OK; // nothing changes
654 if (event == REMOVE_ALGO)
656 algo = dynamic_cast<SMESH_Algo*> (anHyp);
657 if (!algo->NeedDescretBoundary())
659 // clean all mesh in the tree of the current submesh;
660 // we must perform it now because later
661 // we will have no information about the type of the removed algo
663 ComputeStateEngine( CLEAN );
665 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
670 // ------------------
671 // analyse algo state
672 // ------------------
673 if (!isApplicableHyp)
674 return ret; // not applicable hypotheses do not change algo state
679 // ----------------------------------------------------------------------
686 algo = gen->GetAlgo((*_father), _subShape);
688 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
689 SetAlgoState(HYP_OK);
690 else if ( algo->IsStatusFatal( aux_ret )) {
691 meshDS->RemoveHypothesis(_subShape, anHyp);
695 SetAlgoState(MISSING_HYP);
702 case ADD_FATHER_ALGO: { // Algo just added in father
703 algo = gen->GetAlgo((*_father), _subShape);
705 if ( algo == anHyp ) {
706 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
707 SetAlgoState(HYP_OK);
709 SetAlgoState(MISSING_HYP);
713 case REMOVE_FATHER_HYP:
715 case REMOVE_FATHER_ALGO: {
716 algo = gen->GetAlgo((*_father), _subShape);
719 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
720 SetAlgoState(HYP_OK);
722 SetAlgoState(MISSING_HYP);
726 case MODIF_HYP: break;
733 // ----------------------------------------------------------------------
739 algo = gen->GetAlgo((*_father), _subShape);
741 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
742 SetAlgoState(HYP_OK);
743 if (SMESH_Hypothesis::IsStatusFatal( ret ))
744 meshDS->RemoveHypothesis(_subShape, anHyp);
745 else if (!_father->IsUsedHypothesis( anHyp, this ))
747 meshDS->RemoveHypothesis(_subShape, anHyp);
748 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
752 case ADD_ALGO: { //already existing algo : on father ?
753 algo = gen->GetAlgo((*_father), _subShape);
755 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
756 SetAlgoState(HYP_OK);
757 else if ( algo->IsStatusFatal( aux_ret )) {
758 meshDS->RemoveHypothesis(_subShape, anHyp);
762 SetAlgoState(MISSING_HYP);
767 case REMOVE_ALGO: { // perhaps a father algo applies ?
768 algo = gen->GetAlgo((*_father), _subShape);
769 if (algo == NULL) // no more algo applying on subShape...
771 SetAlgoState(NO_ALGO);
775 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
776 SetAlgoState(HYP_OK);
778 SetAlgoState(MISSING_HYP);
782 case MODIF_HYP: // assigned hypothesis value may become good
783 case ADD_FATHER_HYP: {
784 algo = gen->GetAlgo((*_father), _subShape);
786 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
787 SetAlgoState(HYP_OK);
789 SetAlgoState(MISSING_HYP);
792 case ADD_FATHER_ALGO: { // new father algo
793 algo = gen->GetAlgo((*_father), _subShape);
795 if ( algo == anHyp ) {
796 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
797 SetAlgoState(HYP_OK);
799 SetAlgoState(MISSING_HYP);
803 case REMOVE_FATHER_HYP: // nothing to do
805 case REMOVE_FATHER_ALGO: {
806 algo = gen->GetAlgo((*_father), _subShape);
807 if (algo == NULL) // no more applying algo on father
809 SetAlgoState(NO_ALGO);
813 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
814 SetAlgoState(HYP_OK);
816 SetAlgoState(MISSING_HYP);
826 // ----------------------------------------------------------------------
832 algo = gen->GetAlgo((*_father), _subShape);
834 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
836 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
837 // ret should be fatal: anHyp was not added
838 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
840 else if (!_father->IsUsedHypothesis( anHyp, this ))
841 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
843 if (SMESH_Hypothesis::IsStatusFatal( ret ))
845 MESSAGE("do not add extra hypothesis");
846 meshDS->RemoveHypothesis(_subShape, anHyp);
854 case ADD_ALGO: { //already existing algo : on father ?
855 algo = gen->GetAlgo((*_father), _subShape);
856 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
857 // check if algo changes
859 f.Init( SMESH_HypoFilter::IsAlgo() );
860 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
861 f.AndNot( SMESH_HypoFilter::Is( algo ));
862 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
864 string(algo->GetName()) != string(prevAlgo->GetName()) )
868 SetAlgoState(MISSING_HYP);
872 algo = gen->GetAlgo((*_father), _subShape);
874 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
875 SetAlgoState(HYP_OK);
877 SetAlgoState(MISSING_HYP);
881 case REMOVE_ALGO: { // perhaps a father algo applies ?
882 algo = gen->GetAlgo((*_father), _subShape);
883 if (algo == NULL) // no more algo applying on subShape...
885 SetAlgoState(NO_ALGO);
889 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
890 // check if algo remains
891 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
895 SetAlgoState(MISSING_HYP);
899 case MODIF_HYP: // hypothesis value may become bad
900 case ADD_FATHER_HYP: { // new father hypothesis ?
901 algo = gen->GetAlgo((*_father), _subShape);
903 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
905 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
909 SetAlgoState(MISSING_HYP);
912 case ADD_FATHER_ALGO: {
913 algo = gen->GetAlgo((*_father), _subShape);
914 if ( algo == anHyp ) { // a new algo on father
915 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
916 // check if algo changes
918 f.Init( SMESH_HypoFilter::IsAlgo() );
919 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
920 f.AndNot( SMESH_HypoFilter::Is( algo ));
921 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
923 string(algo->GetName()) != string(prevAlgo->GetName()) )
927 SetAlgoState(MISSING_HYP);
931 case REMOVE_FATHER_HYP: {
932 algo = gen->GetAlgo((*_father), _subShape);
934 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
935 // is there the same local hyp or maybe a new father algo applied?
936 if ( !GetSimilarAttached( _subShape, anHyp ) )
940 SetAlgoState(MISSING_HYP);
943 case REMOVE_FATHER_ALGO: {
944 algo = gen->GetAlgo((*_father), _subShape);
945 if (algo == NULL) // no more applying algo on father
947 SetAlgoState(NO_ALGO);
951 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
952 // check if algo changes
953 if ( string(algo->GetName()) != string( anHyp->GetName()) )
957 SetAlgoState(MISSING_HYP);
967 // ----------------------------------------------------------------------
974 // detect algorithm hiding
976 if ( ret == SMESH_Hypothesis::HYP_OK &&
977 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
978 algo->GetName() == anHyp->GetName() )
981 SMESH_Gen* gen = _father->GetGen();
982 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
983 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
984 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
985 if ( !upperAlgo->NeedDescretBoundary() && !upperAlgo->SupportSubmeshes())
986 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
989 if ( ret == SMESH_Hypothesis::HYP_OK &&
990 !algo->NeedDescretBoundary() &&
991 !algo->SupportSubmeshes()) {
992 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
993 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
994 if ( gen->GetAlgo( *_father, i_sm->second->_subShape ))
995 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
999 bool stateChange = ( _algoState != oldAlgoState );
1001 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1002 algo->SetEventListener( this );
1004 NotifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1006 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1007 DeleteOwnListeners();
1008 SetIsAlwaysComputed( false );
1009 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1010 // restore default states
1011 _algoState = HYP_OK;
1012 _computeState = READY_TO_COMPUTE;
1016 if (stateChange || modifiedHyp)
1017 ComputeStateEngine(MODIF_ALGO_STATE);
1022 //=======================================================================
1023 //function : IsConform
1024 //purpose : check if a conform mesh will be produced by the Algo
1025 //=======================================================================
1027 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1029 // MESSAGE( "SMESH_subMesh::IsConform" );
1030 if ( !theAlgo ) return false;
1032 // Suppose that theAlgo is applicable to _subShape, do not check it here
1033 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1035 // check only algo that doesn't NeedDescretBoundary(): because mesh made
1036 // on a sub-shape will be ignored by theAlgo
1037 if ( theAlgo->NeedDescretBoundary() ||
1038 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1041 SMESH_Gen* gen =_father->GetGen();
1043 // only local algo is to be checked
1044 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1045 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1048 // check algo attached to adjacent shapes
1050 // loop on one level down sub-meshes
1051 TopoDS_Iterator itsub( _subShape );
1052 for (; itsub.More(); itsub.Next())
1054 // loop on adjacent subShapes
1055 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1056 for (; it.More(); it.Next())
1058 const TopoDS_Shape& adjacent = it.Value();
1059 if ( _subShape.IsSame( adjacent )) continue;
1060 if ( adjacent.ShapeType() != _subShape.ShapeType())
1063 // check algo attached to smAdjacent
1064 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1066 !algo->NeedDescretBoundary() &&
1067 algo->OnlyUnaryInput())
1068 return false; // NOT CONFORM MESH WILL BE PRODUCED
1075 //=============================================================================
1079 //=============================================================================
1081 void SMESH_subMesh::SetAlgoState(int state)
1086 //=============================================================================
1090 //=============================================================================
1091 SMESH_Hypothesis::Hypothesis_Status
1092 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1093 SMESH_Hypothesis * anHyp)
1095 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1096 //EAP: a wire (dim==1) should notify edges (dim==1)
1097 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1098 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1100 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1101 while ( smIt->more() ) {
1102 SMESH_Hypothesis::Hypothesis_Status ret2 =
1103 smIt->next()->AlgoStateEngine(event, anHyp);
1111 //=============================================================================
1115 //=============================================================================
1117 void SMESH_subMesh::CleanDependsOn()
1119 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1120 while ( smIt->more() )
1121 smIt->next()->ComputeStateEngine(CLEAN);
1124 //=============================================================================
1128 //=============================================================================
1130 void SMESH_subMesh::DumpAlgoState(bool isMain)
1132 int dim = SMESH_Gen::GetShapeDim(_subShape);
1133 // if (dim < 1) return;
1136 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1138 map < int, SMESH_subMesh * >::const_iterator itsub;
1139 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1141 SMESH_subMesh *sm = (*itsub).second;
1142 sm->DumpAlgoState(false);
1145 int type = _subShape.ShapeType();
1146 MESSAGE("dim = " << dim << " type of shape " << type);
1150 MESSAGE(" AlgoState = NO_ALGO");
1153 MESSAGE(" AlgoState = MISSING_HYP");
1156 MESSAGE(" AlgoState = HYP_OK");
1159 switch (_computeState)
1162 MESSAGE(" ComputeState = NOT_READY");
1164 case READY_TO_COMPUTE:
1165 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1168 MESSAGE(" ComputeState = COMPUTE_OK");
1170 case FAILED_TO_COMPUTE:
1171 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1176 //================================================================================
1178 * \brief Remove nodes and elements bound to submesh
1179 * \param subMesh - submesh containing nodes and elements
1181 //================================================================================
1183 static void cleanSubMesh( SMESH_subMesh * subMesh )
1186 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1187 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1188 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1189 while (ite->more()) {
1190 const SMDS_MeshElement * elt = ite->next();
1191 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1192 //meshDS->RemoveElement(elt);
1193 meshDS->RemoveFreeElement(elt, subMeshDS);
1196 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1197 while (itn->more()) {
1198 const SMDS_MeshNode * node = itn->next();
1199 //MESSAGE( " RM node: "<<node->GetID());
1200 if ( node->NbInverseElements() == 0 )
1201 meshDS->RemoveFreeNode(node, subMeshDS);
1202 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1203 meshDS->RemoveNode(node);
1209 //=============================================================================
1213 //=============================================================================
1215 bool SMESH_subMesh::ComputeStateEngine(int event)
1217 _computeError.reset();
1219 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1220 //SCRUTE(_computeState);
1223 if (_subShape.ShapeType() == TopAbs_VERTEX)
1225 _computeState = READY_TO_COMPUTE;
1226 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1227 if ( smDS && smDS->NbNodes() ) {
1228 if ( event == CLEAN ) {
1230 cleanSubMesh( this );
1233 _computeState = COMPUTE_OK;
1235 else if ( event == COMPUTE && !_alwaysComputed ) {
1236 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1237 gp_Pnt P = BRep_Tool::Pnt(V);
1238 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1239 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1240 _computeState = COMPUTE_OK;
1243 if ( event == MODIF_ALGO_STATE )
1247 SMESH_Gen *gen = _father->GetGen();
1248 SMESH_Algo *algo = 0;
1250 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1251 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1253 switch (_computeState)
1256 // ----------------------------------------------------------------------
1261 case MODIF_ALGO_STATE:
1262 algo = gen->GetAlgo((*_father), _subShape);
1263 if (algo && !algo->NeedDescretBoundary())
1264 CleanDependsOn(); // clean sub-meshes with event CLEAN
1265 if ( _algoState == HYP_OK )
1266 _computeState = READY_TO_COMPUTE;
1268 case COMPUTE: // nothing to do
1272 RemoveSubMeshElementsAndNodes();
1274 case SUBMESH_COMPUTED: // nothing to do
1276 case SUBMESH_RESTORED:
1277 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1279 case MESH_ENTITY_REMOVED:
1281 case CHECK_COMPUTE_STATE:
1282 if ( IsMeshComputed() )
1283 _computeState = COMPUTE_OK;
1291 // ----------------------------------------------------------------------
1293 case READY_TO_COMPUTE:
1296 case MODIF_ALGO_STATE:
1297 _computeState = NOT_READY;
1298 algo = gen->GetAlgo((*_father), _subShape);
1301 if (!algo->NeedDescretBoundary())
1302 CleanDependsOn(); // clean sub-meshes with event CLEAN
1303 if ( _algoState == HYP_OK )
1304 _computeState = READY_TO_COMPUTE;
1309 algo = gen->GetAlgo((*_father), _subShape);
1311 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1314 MESSAGE("***** verify compute state *****");
1315 _computeState = NOT_READY;
1316 SetAlgoState(MISSING_HYP);
1319 TopoDS_Shape shape = _subShape;
1320 // check submeshes needed
1321 if (_father->HasShapeToMesh() ) {
1322 bool subComputed = false;
1323 if (!algo->OnlyUnaryInput())
1324 shape = GetCollection( gen, algo, subComputed );
1326 subComputed = SubMeshesComputed();
1327 ret = ( algo->NeedDescretBoundary() ? subComputed :
1328 algo->SupportSubmeshes() ? true :
1329 ( !subComputed || _father->IsNotConformAllowed() ));
1331 _computeState = FAILED_TO_COMPUTE;
1332 if ( !algo->NeedDescretBoundary() )
1334 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1335 "Unexpected computed submesh",algo);
1340 // CleanDependants(); for "UseExisting_*D" algos
1341 // RemoveSubMeshElementsAndNodes();
1343 _computeState = FAILED_TO_COMPUTE;
1344 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1346 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1349 algo->InitComputeError();
1350 MemoryReserve aMemoryReserve;
1351 SMDS_Mesh::CheckMemory();
1352 if ( !_father->HasShapeToMesh() ) // no shape
1354 SMESH_MesherHelper helper( *_father );
1355 helper.SetSubShape( shape );
1356 helper.SetElementsOnShape( true );
1357 ret = algo->Compute(*_father, &helper );
1361 ret = algo->Compute((*_father), shape);
1363 if ( !ret && _computeError->IsOK() ) // algo can set _computeError of submesh
1364 _computeError = algo->GetComputeError();
1366 catch ( std::bad_alloc& exc ) {
1367 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1368 if ( _computeError ) {
1369 _computeError->myName = COMPERR_MEMORY_PB;
1370 //_computeError->myComment = exc.what();
1372 cleanSubMesh( this );
1375 catch ( Standard_OutOfMemory& exc ) {
1376 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1377 if ( _computeError ) {
1378 _computeError->myName = COMPERR_MEMORY_PB;
1379 //_computeError->myComment = exc.what();
1381 cleanSubMesh( this );
1382 throw std::bad_alloc();
1384 catch (Standard_Failure& ex) {
1385 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1386 _computeError->myName = COMPERR_OCC_EXCEPTION;
1387 _computeError->myComment += ex.DynamicType()->Name();
1388 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1389 _computeError->myComment += ": ";
1390 _computeError->myComment += ex.GetMessageString();
1393 catch ( SALOME_Exception& S_ex ) {
1394 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1395 _computeError->myName = COMPERR_SLM_EXCEPTION;
1396 _computeError->myComment = S_ex.what();
1398 catch ( std::exception& exc ) {
1399 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1400 _computeError->myName = COMPERR_STD_EXCEPTION;
1401 _computeError->myComment = exc.what();
1404 if ( _computeError )
1405 _computeError->myName = COMPERR_EXCEPTION;
1409 if (ret && !_alwaysComputed && shape == _subShape) { // check if anything was built
1410 ret = ( GetSubMeshDS() && ( GetSubMeshDS()->NbElements() || GetSubMeshDS()->NbNodes() ));
1412 bool isComputeErrorSet = !CheckComputeError( algo, shape );
1413 if (!ret && !isComputeErrorSet)
1415 // Set _computeError
1416 if ( !_computeError )
1417 _computeError = SMESH_ComputeError::New();
1418 if ( _computeError->IsOK() )
1419 _computeError->myName = COMPERR_ALGO_FAILED;
1420 _computeState = FAILED_TO_COMPUTE;
1424 _computeError.reset();
1426 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1431 RemoveSubMeshElementsAndNodes();
1432 _computeState = NOT_READY;
1433 algo = gen->GetAlgo((*_father), _subShape);
1436 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1438 _computeState = READY_TO_COMPUTE;
1440 SetAlgoState(MISSING_HYP);
1443 case SUBMESH_COMPUTED: // nothing to do
1445 case SUBMESH_RESTORED:
1446 // check if a mesh is already computed that may
1447 // happen after retrieval from a file
1448 ComputeStateEngine( CHECK_COMPUTE_STATE );
1449 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1450 algo = gen->GetAlgo(*_father, _subShape);
1451 if (algo) algo->SubmeshRestored( this );
1453 case MESH_ENTITY_REMOVED:
1455 case CHECK_COMPUTE_STATE:
1456 if ( IsMeshComputed() )
1457 _computeState = COMPUTE_OK;
1465 // ----------------------------------------------------------------------
1470 case MODIF_ALGO_STATE:
1471 ComputeStateEngine( CLEAN );
1472 algo = gen->GetAlgo((*_father), _subShape);
1473 if (algo && !algo->NeedDescretBoundary())
1474 CleanDependsOn(); // clean sub-meshes with event CLEAN
1476 case COMPUTE: // nothing to do
1479 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1480 RemoveSubMeshElementsAndNodes();
1481 _computeState = NOT_READY;
1482 if ( _algoState == HYP_OK )
1483 _computeState = READY_TO_COMPUTE;
1485 case SUBMESH_COMPUTED: // nothing to do
1487 case SUBMESH_RESTORED:
1488 ComputeStateEngine( CHECK_COMPUTE_STATE );
1489 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1490 algo = gen->GetAlgo(*_father, _subShape);
1491 if (algo) algo->SubmeshRestored( this );
1493 case MESH_ENTITY_REMOVED:
1494 UpdateDependantsState( CHECK_COMPUTE_STATE );
1495 ComputeStateEngine( CHECK_COMPUTE_STATE );
1496 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1498 case CHECK_COMPUTE_STATE:
1499 if ( !IsMeshComputed() )
1500 if (_algoState == HYP_OK)
1501 _computeState = READY_TO_COMPUTE;
1503 _computeState = NOT_READY;
1511 // ----------------------------------------------------------------------
1513 case FAILED_TO_COMPUTE:
1516 case MODIF_ALGO_STATE:
1517 algo = gen->GetAlgo((*_father), _subShape);
1518 if (algo && !algo->NeedDescretBoundary())
1519 CleanDependsOn(); // clean sub-meshes with event CLEAN
1520 if (_algoState == HYP_OK)
1521 _computeState = READY_TO_COMPUTE;
1523 _computeState = NOT_READY;
1525 case COMPUTE: // nothing to do
1528 CleanDependants(); // submeshes dependent on me should be cleaned as well
1529 RemoveSubMeshElementsAndNodes();
1531 case SUBMESH_COMPUTED: // allow retry compute
1532 if (_algoState == HYP_OK)
1533 _computeState = READY_TO_COMPUTE;
1535 _computeState = NOT_READY;
1537 case SUBMESH_RESTORED:
1538 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1540 case MESH_ENTITY_REMOVED:
1542 case CHECK_COMPUTE_STATE:
1543 if ( IsMeshComputed() )
1544 _computeState = COMPUTE_OK;
1546 if (_algoState == HYP_OK)
1547 _computeState = READY_TO_COMPUTE;
1549 _computeState = NOT_READY;
1557 // ----------------------------------------------------------------------
1563 NotifyListenersOnEvent( event, COMPUTE_EVENT );
1568 //=======================================================================
1570 * \brief Update compute_state by _computeError and send proper events to
1571 * dependent submeshes
1572 * \retval bool - true if _computeError is NOT set
1574 //=======================================================================
1576 bool SMESH_subMesh::CheckComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1578 bool noErrors = true;
1580 if ( !theShape.IsNull() )
1582 // Check state of submeshes
1583 if ( !theAlgo->NeedDescretBoundary())
1585 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1586 while ( smIt->more() )
1587 if ( !smIt->next()->CheckComputeError( theAlgo ))
1591 // Check state of neighbours
1592 if ( !theAlgo->OnlyUnaryInput() &&
1593 theShape.ShapeType() == TopAbs_COMPOUND &&
1594 !theShape.IsSame( _subShape ))
1596 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1597 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1599 if ( !sm->CheckComputeError( theAlgo, sm->GetSubShape() ))
1601 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1608 if ( !_computeError || _computeError->IsOK() )
1610 _computeState = COMPUTE_OK;
1614 if ( !_computeError->myAlgo )
1615 _computeError->myAlgo = theAlgo;
1619 text << theAlgo->GetName() << " failed on subshape #" << _Id << " with error ";
1620 if (_computeError->IsCommon() )
1621 text << _computeError->CommonName();
1623 text << _computeError->myName;
1624 if ( _computeError->myComment.size() > 0 )
1625 text << " \"" << _computeError->myComment << "\"";
1628 MESSAGE_BEGIN ( text );
1629 // Show vertices location of a failed shape
1630 TopTools_IndexedMapOfShape vMap;
1631 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1632 MESSAGE_ADD ( "Subshape vertices " << ( vMap.Extent()>10 ? "(first 10):" : ":") );
1633 for ( int iv = 1; iv <= vMap.Extent() && iv < 11; ++iv ) {
1634 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1635 MESSAGE_ADD ( "#" << _father->GetMeshDS()->ShapeToIndex( vMap( iv )) << " "
1636 << P.X() << " " << P.Y() << " " << P.Z() << " " );
1641 _computeState = FAILED_TO_COMPUTE;
1648 //=======================================================================
1649 //function : ApplyToCollection
1650 //purpose : Apply theAlgo to all subshapes in theCollection
1651 //=======================================================================
1653 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1654 const TopoDS_Shape& theCollection)
1656 MESSAGE("SMESH_subMesh::ApplyToCollection");
1657 ASSERT ( !theAlgo->NeedDescretBoundary() );
1659 if ( _computeError )
1660 _computeError->myName = COMPERR_OK;
1662 bool ok = theAlgo->Compute( *_father, theCollection );
1664 // set _computeState of subshapes
1665 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1666 for ( ; anExplorer.More(); anExplorer.Next() )
1668 if ( SMESH_subMesh* subMesh = _father->GetSubMeshContaining( anExplorer.Current() ))
1670 bool localOK = subMesh->CheckComputeError( theAlgo );
1671 if ( !ok && localOK && !subMesh->IsMeshComputed() )
1673 subMesh->_computeError = theAlgo->GetComputeError();
1674 if ( subMesh->_computeError->IsOK() )
1675 _computeError = SMESH_ComputeError::New(COMPERR_ALGO_FAILED);
1676 localOK = CheckComputeError( theAlgo );
1679 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1680 subMesh->UpdateSubMeshState( localOK ? COMPUTE_OK : FAILED_TO_COMPUTE );
1688 //=======================================================================
1689 //function : UpdateSubMeshState
1691 //=======================================================================
1693 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1695 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1696 while ( smIt->more() )
1697 smIt->next()->_computeState = theState;
1700 //=======================================================================
1701 //function : ComputeSubMeshStateEngine
1703 //=======================================================================
1705 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1707 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1708 while ( smIt->more() )
1709 smIt->next()->ComputeStateEngine(event);
1712 //=======================================================================
1713 //function : UpdateDependantsState
1715 //=======================================================================
1717 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1719 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1720 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1721 for (; it.More(); it.Next())
1723 const TopoDS_Shape& ancestor = it.Value();
1724 SMESH_subMesh *aSubMesh =
1725 _father->GetSubMeshContaining(ancestor);
1727 aSubMesh->ComputeStateEngine( theEvent );
1731 //=============================================================================
1735 //=============================================================================
1737 void SMESH_subMesh::CleanDependants()
1739 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1741 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1742 for (; it.More(); it.Next())
1744 const TopoDS_Shape& ancestor = it.Value();
1745 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1746 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1747 // will erase mesh on other shapes in a compound
1748 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1749 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1751 aSubMesh->ComputeStateEngine(CLEAN);
1757 //=============================================================================
1761 //=============================================================================
1763 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1765 //SCRUTE(_subShape.ShapeType());
1767 cleanSubMesh( this );
1769 // algo may bind a submesh not to _subShape, eg 3D algo
1770 // sets nodes on SHELL while _subShape may be SOLID
1772 int dim = SMESH_Gen::GetShapeDim( _subShape );
1773 int type = _subShape.ShapeType() + 1;
1774 for ( ; type <= TopAbs_EDGE; type++) {
1775 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1777 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1778 for ( ; exp.More(); exp.Next() )
1779 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1786 //=======================================================================
1787 //function : GetCollection
1788 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1789 // meshed at once along with _subShape
1790 //=======================================================================
1792 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen,
1793 SMESH_Algo* theAlgo,
1794 bool & theSubComputed)
1796 MESSAGE("SMESH_subMesh::GetCollection");
1798 theSubComputed = SubMeshesComputed();
1800 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1802 if ( mainShape.IsSame( _subShape ))
1805 const bool ignoreAuxiliaryHyps = false;
1806 list<const SMESHDS_Hypothesis*> aUsedHyp =
1807 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1809 // put in a compound all shapes with the same hypothesis assigned
1810 // and a good ComputState
1812 TopoDS_Compound aCompound;
1813 BRep_Builder aBuilder;
1814 aBuilder.MakeCompound( aCompound );
1816 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1817 for ( ; anExplorer.More(); anExplorer.Next() )
1819 const TopoDS_Shape& S = anExplorer.Current();
1820 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1821 if ( subMesh == this )
1823 aBuilder.Add( aCompound, S );
1825 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1827 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1828 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1829 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1830 aBuilder.Add( aCompound, S );
1831 if ( !subMesh->SubMeshesComputed() )
1832 theSubComputed = false;
1839 //=======================================================================
1840 //function : GetSimilarAttached
1841 //purpose : return a hypothesis attached to theShape.
1842 // If theHyp is provided, similar but not same hypotheses
1843 // is returned; else only applicable ones having theHypType
1845 //=======================================================================
1847 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1848 const SMESH_Hypothesis * theHyp,
1849 const int theHypType)
1851 SMESH_HypoFilter hypoKind;
1852 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1854 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1855 hypoKind.AndNot( hypoKind.Is( theHyp ));
1856 if ( theHyp->IsAuxiliary() )
1857 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1859 hypoKind.AndNot( hypoKind.IsAuxiliary());
1862 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1865 return _father->GetHypothesis( theShape, hypoKind, false );
1868 //=======================================================================
1869 //function : CheckConcurentHypothesis
1870 //purpose : check if there are several applicable hypothesis attached to
1872 //=======================================================================
1874 SMESH_Hypothesis::Hypothesis_Status
1875 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1877 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1879 // is there local hypothesis on me?
1880 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1881 return SMESH_Hypothesis::HYP_OK;
1884 TopoDS_Shape aPrevWithHyp;
1885 const SMESH_Hypothesis* aPrevHyp = 0;
1886 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1887 for (; it.More(); it.Next())
1889 const TopoDS_Shape& ancestor = it.Value();
1890 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1893 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1895 aPrevWithHyp = ancestor;
1898 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1899 return SMESH_Hypothesis::HYP_CONCURENT;
1901 return SMESH_Hypothesis::HYP_OK;
1904 return SMESH_Hypothesis::HYP_OK;
1907 //================================================================================
1909 * \brief Sets an event listener and its data to a submesh
1910 * \param listener - the listener to store
1911 * \param data - the listener data to store
1912 * \param where - the submesh to store the listener and it's data
1913 * \param deleteListener - if true then the listener will be deleted as
1914 * it is removed from where submesh
1916 * It remembers the submesh where it puts the listener in order to delete
1917 * them when HYP_OK algo_state is lost
1918 * After being set, event listener is notified on each event of where submesh.
1920 //================================================================================
1922 void SMESH_subMesh::SetEventListener(EventListener* listener,
1923 EventListenerData* data,
1924 SMESH_subMesh* where)
1926 if ( listener && where ) {
1927 where->SetEventListener( listener, data );
1928 myOwnListeners.push_back( make_pair( where, listener ));
1932 //================================================================================
1934 * \brief Sets an event listener and its data to a submesh
1935 * \param listener - the listener to store
1936 * \param data - the listener data to store
1938 * After being set, event listener is notified on each event of a submesh.
1940 //================================================================================
1942 void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
1944 map< EventListener*, EventListenerData* >::iterator l_d =
1945 myEventListeners.find( listener );
1946 if ( l_d != myEventListeners.end() ) {
1947 EventListenerData* curData = l_d->second;
1948 if ( curData && curData != data && curData->IsDeletable() )
1953 myEventListeners.insert( make_pair( listener, data ));
1956 //================================================================================
1958 * \brief Return an event listener data
1959 * \param listener - the listener whose data is
1960 * \retval EventListenerData* - found data, maybe NULL
1962 //================================================================================
1964 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
1966 map< EventListener*, EventListenerData* >::const_iterator l_d =
1967 myEventListeners.find( listener );
1968 if ( l_d != myEventListeners.end() )
1973 //================================================================================
1975 * \brief Notify stored event listeners on the occured event
1976 * \param event - algo_event or compute_event itself
1977 * \param eventType - algo_event or compute_event
1978 * \param subMesh - the submesh where the event occures
1979 * \param data - listener data stored in the subMesh
1980 * \param hyp - hypothesis, if eventType is algo_event
1982 //================================================================================
1984 void SMESH_subMesh::NotifyListenersOnEvent( const int event,
1985 const event_type eventType,
1986 SMESH_Hypothesis* hyp)
1988 map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
1989 for ( ; l_d != myEventListeners.end(); ++l_d )
1990 l_d->first->ProcessEvent( event, eventType, this, l_d->second, hyp );
1993 //================================================================================
1995 * \brief Unregister the listener and delete listener's data
1996 * \param listener - the event listener
1998 //================================================================================
2000 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2002 map< EventListener*, EventListenerData* >::iterator l_d =
2003 myEventListeners.find( listener );
2004 if ( l_d != myEventListeners.end() ) {
2005 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2006 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2007 myEventListeners.erase( l_d );
2011 //================================================================================
2013 * \brief Delete event listeners depending on algo of this submesh
2015 //================================================================================
2017 void SMESH_subMesh::DeleteOwnListeners()
2019 list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
2020 for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
2021 sm_l->first->DeleteEventListener( sm_l->second );
2022 myOwnListeners.clear();
2025 //================================================================================
2027 * \brief Do something on a certain event
2028 * \param event - algo_event or compute_event itself
2029 * \param eventType - algo_event or compute_event
2030 * \param subMesh - the submesh where the event occures
2031 * \param data - listener data stored in the subMesh
2032 * \param hyp - hypothesis, if eventType is algo_event
2034 * The base implementation translates CLEAN event to the subMesh
2035 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2036 * successful COMPUTE event.
2038 //================================================================================
2040 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2041 const int eventType,
2042 SMESH_subMesh* subMesh,
2043 EventListenerData* data,
2044 const SMESH_Hypothesis* /*hyp*/)
2046 if ( data && !data->mySubMeshes.empty() &&
2047 eventType == SMESH_subMesh::COMPUTE_EVENT)
2049 ASSERT( data->mySubMeshes.front() != subMesh );
2050 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2051 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2053 case SMESH_subMesh::CLEAN:
2054 for ( ; smIt != smEnd; ++ smIt)
2055 (*smIt)->ComputeStateEngine( event );
2057 case SMESH_subMesh::COMPUTE:
2058 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2059 for ( ; smIt != smEnd; ++ smIt)
2060 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2069 //================================================================================
2071 * \brief Iterator over submeshes and optionally prepended or appended one
2073 //================================================================================
2075 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2077 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2078 SMESH_subMesh* prepend,
2079 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2081 myCur = prepend ? prepend : myIt->more() ? myIt->next() : 0;
2083 /// Return true if and only if there are other object in this iterator
2088 /// Return the current object and step to the next one
2089 virtual SMESH_subMesh* next()
2091 SMESH_subMesh* res = myCur;
2092 if ( myIt->more() ) { myCur = myIt->next(); }
2093 else { myCur = myAppend; myAppend = 0; }
2100 SMESH_subMesh *myAppend, *myCur;
2101 SMDS_Iterator<SMESH_subMesh*> *myIt;
2105 //================================================================================
2107 * \brief Return iterator on the submeshes this one depends on
2108 * \param includeSelf - this submesh to be returned also
2109 * \param reverse - if true, complex shape submeshes go first
2111 //================================================================================
2113 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2116 SMESH_subMesh *prepend=0, *append=0;
2117 if ( includeSelf ) {
2118 if ( reverse ) prepend = this;
2121 typedef map < int, SMESH_subMesh * > TMap;
2124 return SMESH_subMeshIteratorPtr
2125 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2128 return SMESH_subMeshIteratorPtr
2129 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));