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_IndexedDataMapOfShapeListOfShape.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_ListOfShape.hxx>
51 #include <TopTools_MapOfShape.hxx>
53 #include <TopoDS_Compound.hxx>
56 #include <Standard_OutOfMemory.hxx>
57 #include <Standard_ErrorHandler.hxx>
61 //=============================================================================
63 * \brief Allocate some memory at construction and release it at destruction.
64 * Is used to be able to continue working after mesh generation breaks due to
67 //=============================================================================
72 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
73 ~MemoryReserve() { delete [] myBuf; }
76 //=============================================================================
78 * default constructor:
80 //=============================================================================
82 SMESH_subMesh::SMESH_subMesh(int Id,
84 SMESHDS_Mesh * meshDS,
85 const TopoDS_Shape & aSubShape)
87 _subShape = aSubShape;
88 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
91 _dependenceAnalysed = _alwaysComputed = false;
93 if (_subShape.ShapeType() == TopAbs_VERTEX)
96 _computeState = READY_TO_COMPUTE;
100 _algoState = NO_ALGO;
101 _computeState = NOT_READY;
105 //=============================================================================
109 //=============================================================================
111 SMESH_subMesh::~SMESH_subMesh()
113 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
115 DeleteOwnListeners();
118 //=============================================================================
122 //=============================================================================
124 int SMESH_subMesh::GetId() const
126 //MESSAGE("SMESH_subMesh::GetId");
130 //=============================================================================
134 //=============================================================================
136 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
138 // submesh appears in DS only when a mesher set nodes and elements on a shape
139 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
142 //=============================================================================
146 //=============================================================================
148 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
150 if ( !GetSubMeshDS() ) {
151 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
152 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
154 return GetSubMeshDS();
157 //=============================================================================
161 //=============================================================================
163 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
165 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
166 while ( smIt->more() ) {
167 SMESH_subMesh *sm = smIt->next();
168 if ( sm->GetComputeState() == READY_TO_COMPUTE )
171 return 0; // nothing to compute
174 //================================================================================
176 * \brief Allow algo->Compute() if a subshape of lower dim is meshed but
177 * none mesh entity is bound to it (PAL13615, 2nd part)
179 //================================================================================
181 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
183 _alwaysComputed = isAlCo;
184 if ( _alwaysComputed )
185 _computeState = COMPUTE_OK;
187 ComputeStateEngine( CHECK_COMPUTE_STATE );
190 //=======================================================================
191 //function : IsMeshComputed
192 //purpose : check if _subMeshDS contains mesh elements
193 //=======================================================================
195 bool SMESH_subMesh::IsMeshComputed() const
197 if ( _alwaysComputed )
199 // algo may bind a submesh not to _subShape, eg 3D algo
200 // sets nodes on SHELL while _subShape may be SOLID
202 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
203 int dim = SMESH_Gen::GetShapeDim( _subShape );
204 int type = _subShape.ShapeType();
205 for ( ; type <= TopAbs_VERTEX; type++) {
206 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
208 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
209 for ( ; exp.More(); exp.Next() )
211 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
213 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
226 //=============================================================================
230 //=============================================================================
232 bool SMESH_subMesh::SubMeshesComputed()
234 int myDim = SMESH_Gen::GetShapeDim( _subShape );
235 int dimToCheck = myDim - 1;
236 bool subMeshesComputed = true;
237 // check subMeshes with upper dimension => reverse iteration
238 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
239 while ( smIt->more() )
241 SMESH_subMesh *sm = smIt->next();
242 if ( sm->_alwaysComputed )
244 const TopoDS_Shape & ss = sm->GetSubShape();
245 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
246 // in checking of existence of edges if the algo needs only faces. Moreover,
247 // degenerated edges may have no submesh, as after computing NETGEN_2D.
248 int dim = SMESH_Gen::GetShapeDim( ss );
249 if (dim < dimToCheck)
250 break; // the rest subMeshes are all of less dimension
251 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
252 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
253 (ds && ( ds->NbNodes() || ds->NbElements() )));
256 int type = ss.ShapeType();
258 subMeshesComputed = false;
262 case TopAbs_COMPOUND:
264 MESSAGE("The not computed sub mesh is a COMPOUND");
267 case TopAbs_COMPSOLID:
269 MESSAGE("The not computed sub mesh is a COMPSOLID");
274 MESSAGE("The not computed sub mesh is a SHEL");
279 MESSAGE("The not computed sub mesh is a WIRE");
284 MESSAGE("The not computed sub mesh is a SOLID");
289 MESSAGE("The not computed sub mesh is a FACE");
294 MESSAGE("The not computed sub mesh is a EDGE");
299 MESSAGE("The not computed sub mesh is of unknown type");
307 return subMeshesComputed;
310 //=============================================================================
314 //=============================================================================
316 bool SMESH_subMesh::SubMeshesReady()
318 bool subMeshesReady = true;
319 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
320 while ( smIt->more() ) {
321 SMESH_subMesh *sm = smIt->next();
322 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
323 sm->GetComputeState() == READY_TO_COMPUTE);
326 subMeshesReady = false;
331 return subMeshesReady;
334 //=============================================================================
336 * Construct dependence on first level subMeshes. complex shapes (compsolid,
337 * shell, wire) are not analysed the same way as simple shapes (solid, face,
339 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
340 * with possible multiples occurences. Multiples occurences corresponds to
341 * internal frontiers within shapes of the collection and must not be keeped.
342 * See FinalizeDependence.
344 //=============================================================================
346 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
348 if (_dependenceAnalysed)
351 //MESSAGE("SMESH_subMesh::DependsOn");
353 int type = _subShape.ShapeType();
357 case TopAbs_COMPOUND:
359 //MESSAGE("compound");
360 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
363 InsertDependence(exp.Current());
365 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
368 InsertDependence(exp.Current()); //only shell not in solid
370 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
373 InsertDependence(exp.Current());
375 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
378 InsertDependence(exp.Current());
382 case TopAbs_COMPSOLID:
384 //MESSAGE("compsolid");
385 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
388 InsertDependence(exp.Current());
395 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
398 InsertDependence(exp.Current());
405 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
408 InsertDependence(exp.Current());
415 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
418 InsertDependence(exp.Current());
425 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
428 InsertDependence(exp.Current());
435 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
438 InsertDependence(exp.Current());
451 _dependenceAnalysed = true;
455 //=============================================================================
457 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
459 //=============================================================================
461 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
463 //MESSAGE("SMESH_subMesh::InsertDependence");
464 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
465 int type = aSubShape.ShapeType();
466 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
467 int cle = aSubMesh->GetId();
468 cle += 10000000 * ordType; // sort map by ordType then index
469 if ( _mapDepend.find( cle ) == _mapDepend.end())
471 _mapDepend[cle] = aSubMesh;
472 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
473 _mapDepend.insert( subMap.begin(), subMap.end() );
477 //=============================================================================
481 //=============================================================================
483 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
485 //MESSAGE("SMESH_subMesh::GetSubShape");
490 //=======================================================================
491 //function : CanAddHypothesis
492 //purpose : return true if theHypothesis can be attached to me:
493 // its dimention is checked
494 //=======================================================================
496 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
498 int aHypDim = theHypothesis->GetDim();
499 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
500 if ( aHypDim <= aShapeDim )
502 // if ( aHypDim < aShapeDim )
503 // return ( _father->IsMainShape( _subShape ));
508 //=======================================================================
509 //function : IsApplicableHypotesis
511 //=======================================================================
513 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
514 const TopAbs_ShapeEnum theShapeType)
516 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
518 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
521 switch ( theShapeType ) {
526 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
529 // Special case for algorithms, building 2D mesh on a whole shell.
530 // Before this fix there was a problem after restoring from study,
531 // because in that case algorithm is assigned before hypothesis
532 // (on shell in problem case) and hypothesis is checked on faces
533 // (because it is 2D), where we have NO_ALGO state.
534 // Now 2D hypothesis is also applicable to shells.
535 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
538 // case TopAbs_COMPSOLID:
539 // case TopAbs_COMPOUND:
545 //=============================================================================
549 //=============================================================================
551 SMESH_Hypothesis::Hypothesis_Status
552 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
554 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
555 //SCRUTE(_algoState);
558 // **** les retour des evenement shape sont significatifs
559 // (add ou remove fait ou non)
560 // le retour des evenement father n'indiquent pas que add ou remove fait
562 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
564 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
565 SMESH_Gen* gen =_father->GetGen();
566 SMESH_Algo* algo = 0;
568 if (_subShape.ShapeType() == TopAbs_VERTEX )
570 if ( anHyp->GetDim() != 0) {
571 if (event == ADD_HYP || event == ADD_ALGO)
572 return SMESH_Hypothesis::HYP_BAD_DIM;
574 return SMESH_Hypothesis::HYP_OK;
577 else if ( _algoState == HYP_OK ) {
578 // update default _algoState
579 if ( event != REMOVE_FATHER_ALGO )
581 _algoState = NO_ALGO;
582 algo = gen->GetAlgo(*_father, _subShape);
584 _algoState = MISSING_HYP;
585 if ( event == REMOVE_FATHER_HYP ||
586 algo->CheckHypothesis(*_father,_subShape, aux_ret))
593 int oldAlgoState = _algoState;
594 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
596 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
598 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
600 // -------------------------------------------
601 // check if a shape needed by algo is present
602 // -------------------------------------------
603 algo = static_cast< SMESH_Algo* >( anHyp );
604 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
605 return SMESH_Hypothesis::HYP_BAD_GEOMETRY;
606 // ----------------------
607 // check mesh conformity
608 // ----------------------
609 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
610 return SMESH_Hypothesis::HYP_NOTCONFORM;
613 // ----------------------------------
614 // add a hypothesis to DS if possible
615 // ----------------------------------
616 if (event == ADD_HYP || event == ADD_ALGO)
618 if ( ! CanAddHypothesis( anHyp )) // check dimension
619 return SMESH_Hypothesis::HYP_BAD_DIM;
621 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
622 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
624 if ( !meshDS->AddHypothesis(_subShape, anHyp))
625 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
628 // --------------------------
629 // remove a hypothesis from DS
630 // --------------------------
631 if (event == REMOVE_HYP || event == REMOVE_ALGO)
633 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
634 return SMESH_Hypothesis::HYP_OK; // nothing changes
636 if (event == REMOVE_ALGO)
638 algo = dynamic_cast<SMESH_Algo*> (anHyp);
639 if (!algo->NeedDescretBoundary())
641 // clean all mesh in the tree of the current submesh;
642 // we must perform it now because later
643 // we will have no information about the type of the removed algo
645 ComputeStateEngine( CLEAN );
647 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
652 // ------------------
653 // analyse algo state
654 // ------------------
655 if (!isApplicableHyp)
656 return ret; // not applicable hypotheses do not change algo state
661 // ----------------------------------------------------------------------
668 algo = gen->GetAlgo((*_father), _subShape);
670 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
671 SetAlgoState(HYP_OK);
672 else if ( algo->IsStatusFatal( aux_ret )) {
673 meshDS->RemoveHypothesis(_subShape, anHyp);
677 SetAlgoState(MISSING_HYP);
684 case ADD_FATHER_ALGO: { // Algo just added in father
685 algo = gen->GetAlgo((*_father), _subShape);
687 if ( algo == anHyp ) {
688 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
689 SetAlgoState(HYP_OK);
691 SetAlgoState(MISSING_HYP);
695 case REMOVE_FATHER_HYP:
697 case REMOVE_FATHER_ALGO: {
698 algo = gen->GetAlgo((*_father), _subShape);
701 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
702 SetAlgoState(HYP_OK);
704 SetAlgoState(MISSING_HYP);
708 case MODIF_HYP: break;
715 // ----------------------------------------------------------------------
721 algo = gen->GetAlgo((*_father), _subShape);
723 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
724 SetAlgoState(HYP_OK);
725 if (SMESH_Hypothesis::IsStatusFatal( ret ))
726 meshDS->RemoveHypothesis(_subShape, anHyp);
727 else if (!_father->IsUsedHypothesis( anHyp, this ))
729 meshDS->RemoveHypothesis(_subShape, anHyp);
730 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
734 case ADD_ALGO: { //already existing algo : on father ?
735 algo = gen->GetAlgo((*_father), _subShape);
737 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
738 SetAlgoState(HYP_OK);
739 else if ( algo->IsStatusFatal( aux_ret )) {
740 meshDS->RemoveHypothesis(_subShape, anHyp);
744 SetAlgoState(MISSING_HYP);
749 case REMOVE_ALGO: { // perhaps a father algo applies ?
750 algo = gen->GetAlgo((*_father), _subShape);
751 if (algo == NULL) // no more algo applying on subShape...
753 SetAlgoState(NO_ALGO);
757 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
758 SetAlgoState(HYP_OK);
760 SetAlgoState(MISSING_HYP);
764 case MODIF_HYP: // assigned hypothesis value may become good
765 case ADD_FATHER_HYP: {
766 algo = gen->GetAlgo((*_father), _subShape);
768 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
769 SetAlgoState(HYP_OK);
771 SetAlgoState(MISSING_HYP);
774 case ADD_FATHER_ALGO: { // new father algo
775 algo = gen->GetAlgo((*_father), _subShape);
777 if ( algo == anHyp ) {
778 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
779 SetAlgoState(HYP_OK);
781 SetAlgoState(MISSING_HYP);
785 case REMOVE_FATHER_HYP: // nothing to do
787 case REMOVE_FATHER_ALGO: {
788 algo = gen->GetAlgo((*_father), _subShape);
789 if (algo == NULL) // no more applying algo on father
791 SetAlgoState(NO_ALGO);
795 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
796 SetAlgoState(HYP_OK);
798 SetAlgoState(MISSING_HYP);
808 // ----------------------------------------------------------------------
814 algo = gen->GetAlgo((*_father), _subShape);
816 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
818 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
819 // ret should be fatal: anHyp was not added
820 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
822 else if (!_father->IsUsedHypothesis( anHyp, this ))
823 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
825 if (SMESH_Hypothesis::IsStatusFatal( ret ))
827 MESSAGE("do not add extra hypothesis");
828 meshDS->RemoveHypothesis(_subShape, anHyp);
836 case ADD_ALGO: { //already existing algo : on father ?
837 algo = gen->GetAlgo((*_father), _subShape);
838 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
839 // check if algo changes
841 f.Init( SMESH_HypoFilter::IsAlgo() );
842 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
843 f.AndNot( SMESH_HypoFilter::Is( algo ));
844 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
846 string(algo->GetName()) != string(prevAlgo->GetName()) )
850 SetAlgoState(MISSING_HYP);
854 algo = gen->GetAlgo((*_father), _subShape);
856 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
857 SetAlgoState(HYP_OK);
859 SetAlgoState(MISSING_HYP);
863 case REMOVE_ALGO: { // perhaps a father algo applies ?
864 algo = gen->GetAlgo((*_father), _subShape);
865 if (algo == NULL) // no more algo applying on subShape...
867 SetAlgoState(NO_ALGO);
871 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
872 // check if algo remains
873 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
877 SetAlgoState(MISSING_HYP);
881 case MODIF_HYP: // hypothesis value may become bad
882 case ADD_FATHER_HYP: { // new father hypothesis ?
883 algo = gen->GetAlgo((*_father), _subShape);
885 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
887 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
891 SetAlgoState(MISSING_HYP);
894 case ADD_FATHER_ALGO: {
895 algo = gen->GetAlgo((*_father), _subShape);
896 if ( algo == anHyp ) { // a new algo on father
897 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
898 // check if algo changes
900 f.Init( SMESH_HypoFilter::IsAlgo() );
901 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
902 f.AndNot( SMESH_HypoFilter::Is( algo ));
903 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
905 string(algo->GetName()) != string(prevAlgo->GetName()) )
909 SetAlgoState(MISSING_HYP);
913 case REMOVE_FATHER_HYP: {
914 algo = gen->GetAlgo((*_father), _subShape);
916 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
917 // is there the same local hyp or maybe a new father algo applied?
918 if ( !GetSimilarAttached( _subShape, anHyp ) )
922 SetAlgoState(MISSING_HYP);
925 case REMOVE_FATHER_ALGO: {
926 algo = gen->GetAlgo((*_father), _subShape);
927 if (algo == NULL) // no more applying algo on father
929 SetAlgoState(NO_ALGO);
933 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
934 // check if algo changes
935 if ( string(algo->GetName()) != string( anHyp->GetName()) )
939 SetAlgoState(MISSING_HYP);
949 // ----------------------------------------------------------------------
956 // detect algorithm hiding
958 if ( ret == SMESH_Hypothesis::HYP_OK &&
959 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
960 algo->GetName() == anHyp->GetName() )
963 SMESH_Gen* gen = _father->GetGen();
964 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
965 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
966 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
967 if ( !upperAlgo->NeedDescretBoundary() )
968 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
971 if ( ret == SMESH_Hypothesis::HYP_OK && !algo->NeedDescretBoundary() ) {
972 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
973 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
974 if ( gen->GetAlgo( *_father, i_sm->second->_subShape ))
975 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
979 bool stateChange = ( _algoState != oldAlgoState );
981 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
982 algo->SetEventListener( this );
984 NotifyListenersOnEvent( event, ALGO_EVENT, anHyp );
986 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
987 DeleteOwnListeners();
988 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
989 // restore default states
991 _computeState = READY_TO_COMPUTE;
995 if (stateChange || modifiedHyp)
996 ComputeStateEngine(MODIF_ALGO_STATE);
1001 //=======================================================================
1002 //function : IsConform
1003 //purpose : check if a conform mesh will be produced by the Algo
1004 //=======================================================================
1006 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1008 // MESSAGE( "SMESH_subMesh::IsConform" );
1009 if ( !theAlgo ) return false;
1011 // Suppose that theAlgo is applicable to _subShape, do not check it here
1012 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1014 // check only algo that doesn't NeedDescretBoundary(): because mesh made
1015 // on a sub-shape will be ignored by theAlgo
1016 if ( theAlgo->NeedDescretBoundary() ||
1017 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1020 SMESH_Gen* gen =_father->GetGen();
1022 // only local algo is to be checked
1023 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1024 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1027 // check algo attached to adjacent shapes
1029 // loop on one level down sub-meshes
1030 TopoDS_Iterator itsub( _subShape );
1031 for (; itsub.More(); itsub.Next())
1033 // loop on adjacent subShapes
1034 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1035 for (; it.More(); it.Next())
1037 const TopoDS_Shape& adjacent = it.Value();
1038 if ( _subShape.IsSame( adjacent )) continue;
1039 if ( adjacent.ShapeType() != _subShape.ShapeType())
1042 // check algo attached to smAdjacent
1043 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1045 !algo->NeedDescretBoundary() &&
1046 algo->OnlyUnaryInput())
1047 return false; // NOT CONFORM MESH WILL BE PRODUCED
1054 //=============================================================================
1058 //=============================================================================
1060 void SMESH_subMesh::SetAlgoState(int state)
1065 //=============================================================================
1069 //=============================================================================
1070 SMESH_Hypothesis::Hypothesis_Status
1071 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1072 SMESH_Hypothesis * anHyp)
1074 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1075 //EAP: a wire (dim==1) should notify edges (dim==1)
1076 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1077 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1079 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1080 while ( smIt->more() ) {
1081 SMESH_Hypothesis::Hypothesis_Status ret2 =
1082 smIt->next()->AlgoStateEngine(event, anHyp);
1090 //=============================================================================
1094 //=============================================================================
1096 void SMESH_subMesh::CleanDependsOn()
1098 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1099 while ( smIt->more() )
1100 smIt->next()->ComputeStateEngine(CLEAN);
1103 //=============================================================================
1107 //=============================================================================
1109 void SMESH_subMesh::DumpAlgoState(bool isMain)
1111 int dim = SMESH_Gen::GetShapeDim(_subShape);
1112 // if (dim < 1) return;
1115 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1117 map < int, SMESH_subMesh * >::const_iterator itsub;
1118 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1120 SMESH_subMesh *sm = (*itsub).second;
1121 sm->DumpAlgoState(false);
1124 int type = _subShape.ShapeType();
1125 MESSAGE("dim = " << dim << " type of shape " << type);
1129 MESSAGE(" AlgoState = NO_ALGO");
1132 MESSAGE(" AlgoState = MISSING_HYP");
1135 MESSAGE(" AlgoState = HYP_OK");
1138 switch (_computeState)
1141 MESSAGE(" ComputeState = NOT_READY");
1143 case READY_TO_COMPUTE:
1144 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1147 MESSAGE(" ComputeState = COMPUTE_OK");
1149 case FAILED_TO_COMPUTE:
1150 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1155 //================================================================================
1157 * \brief Remove nodes and elements bound to submesh
1158 * \param subMesh - submesh containing nodes and elements
1160 //================================================================================
1162 static void cleanSubMesh( SMESH_subMesh * subMesh )
1165 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1166 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1167 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1168 while (ite->more()) {
1169 const SMDS_MeshElement * elt = ite->next();
1170 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1171 //meshDS->RemoveElement(elt);
1172 meshDS->RemoveFreeElement(elt, subMeshDS);
1175 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1176 while (itn->more()) {
1177 const SMDS_MeshNode * node = itn->next();
1178 //MESSAGE( " RM node: "<<node->GetID());
1179 if ( node->NbInverseNodes() == 0 )
1180 meshDS->RemoveFreeNode(node, subMeshDS);
1181 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1182 meshDS->RemoveNode(node);
1188 //=============================================================================
1192 //=============================================================================
1194 bool SMESH_subMesh::ComputeStateEngine(int event)
1196 _computeError.reset();
1198 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1199 //SCRUTE(_computeState);
1202 if (_subShape.ShapeType() == TopAbs_VERTEX)
1204 _computeState = READY_TO_COMPUTE;
1205 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1206 if ( smDS && smDS->NbNodes() ) {
1207 _computeState = COMPUTE_OK;
1209 else if ( event == COMPUTE && !_alwaysComputed ) {
1210 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1211 gp_Pnt P = BRep_Tool::Pnt(V);
1212 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1213 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1214 _computeState = COMPUTE_OK;
1217 if ( event == MODIF_ALGO_STATE )
1221 SMESH_Gen *gen = _father->GetGen();
1222 SMESH_Algo *algo = 0;
1224 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1225 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1227 switch (_computeState)
1230 // ----------------------------------------------------------------------
1235 case MODIF_ALGO_STATE:
1236 algo = gen->GetAlgo((*_father), _subShape);
1237 if (algo && !algo->NeedDescretBoundary())
1238 CleanDependsOn(); // clean sub-meshes with event CLEAN
1239 if ( _algoState == HYP_OK )
1240 _computeState = READY_TO_COMPUTE;
1242 case COMPUTE: // nothing to do
1246 RemoveSubMeshElementsAndNodes();
1248 case SUBMESH_COMPUTED: // nothing to do
1250 case SUBMESH_RESTORED:
1251 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1253 case MESH_ENTITY_REMOVED:
1255 case CHECK_COMPUTE_STATE:
1256 if ( IsMeshComputed() )
1257 _computeState = COMPUTE_OK;
1265 // ----------------------------------------------------------------------
1267 case READY_TO_COMPUTE:
1270 case MODIF_ALGO_STATE:
1271 _computeState = NOT_READY;
1272 algo = gen->GetAlgo((*_father), _subShape);
1275 if (!algo->NeedDescretBoundary())
1276 CleanDependsOn(); // clean sub-meshes with event CLEAN
1277 if ( _algoState == HYP_OK )
1278 _computeState = READY_TO_COMPUTE;
1283 algo = gen->GetAlgo((*_father), _subShape);
1285 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1288 MESSAGE("***** verify compute state *****");
1289 _computeState = NOT_READY;
1290 SetAlgoState(MISSING_HYP);
1293 // check submeshes needed
1294 if (_father->HasShapeToMesh() ) {
1295 bool subComputed = SubMeshesComputed();
1296 ret = ( algo->NeedDescretBoundary() ? subComputed :
1297 ( !subComputed || _father->IsNotConformAllowed() ));
1299 _computeState = FAILED_TO_COMPUTE;
1300 if ( !algo->NeedDescretBoundary() )
1302 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1303 "Unexpected computed submesh",algo);
1309 RemoveSubMeshElementsAndNodes();
1311 _computeState = FAILED_TO_COMPUTE;
1312 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1313 TopoDS_Shape shape = _subShape;
1315 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1318 MemoryReserve aMemoryReserve;
1319 algo->InitComputeError();
1320 if ( !_father->HasShapeToMesh() ) // no shape
1322 SMESH_MesherHelper helper( *_father );
1323 helper.SetSubShape( shape );
1324 helper.SetElementsOnShape( true );
1325 ret = algo->Compute(*_father, &helper );
1329 if (!algo->OnlyUnaryInput()) {
1330 shape = GetCollection( gen, algo );
1332 ret = algo->Compute((*_father), shape);
1335 _computeError = algo->GetComputeError();
1337 catch ( std::bad_alloc& exc ) {
1338 printf("std::bad_alloc\n");
1339 if ( _computeError ) {
1340 _computeError->myName = COMPERR_MEMORY_PB;
1341 //_computeError->myComment = exc.what();
1343 cleanSubMesh( this );
1346 catch ( Standard_OutOfMemory& exc ) {
1347 printf("Standard_OutOfMemory\n");
1348 if ( _computeError ) {
1349 _computeError->myName = COMPERR_MEMORY_PB;
1350 //_computeError->myComment = exc.what();
1352 cleanSubMesh( this );
1353 throw std::bad_alloc();
1355 catch (Standard_Failure& ex) {
1356 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1357 _computeError->myName = COMPERR_OCC_EXCEPTION;
1358 _computeError->myComment += ex.DynamicType()->Name();
1359 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1360 _computeError->myComment += ": ";
1361 _computeError->myComment += ex.GetMessageString();
1364 catch ( SALOME_Exception& S_ex ) {
1365 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1366 _computeError->myName = COMPERR_SLM_EXCEPTION;
1367 _computeError->myComment = S_ex.what();
1369 catch ( std::exception& exc ) {
1370 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1371 _computeError->myName = COMPERR_STD_EXCEPTION;
1372 _computeError->myComment = exc.what();
1375 if ( _computeError )
1376 _computeError->myName = COMPERR_EXCEPTION;
1380 if (ret && !_alwaysComputed) { // check if anything was built
1381 ret = ( GetSubMeshDS() && ( GetSubMeshDS()->NbElements() || GetSubMeshDS()->NbNodes() ));
1383 bool isComputeErrorSet = !CheckComputeError( algo, shape );
1384 if (!ret && !isComputeErrorSet)
1386 // Set _computeError
1387 if ( !_computeError )
1388 _computeError = SMESH_ComputeError::New();
1389 if ( _computeError->IsOK() )
1390 _computeError->myName = COMPERR_ALGO_FAILED;
1391 _computeState = FAILED_TO_COMPUTE;
1395 _computeError.reset();
1396 //UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1398 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1403 RemoveSubMeshElementsAndNodes();
1404 _computeState = NOT_READY;
1405 algo = gen->GetAlgo((*_father), _subShape);
1408 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1410 _computeState = READY_TO_COMPUTE;
1412 SetAlgoState(MISSING_HYP);
1415 case SUBMESH_COMPUTED: // nothing to do
1417 case SUBMESH_RESTORED:
1418 // check if a mesh is already computed that may
1419 // happen after retrieval from a file
1420 ComputeStateEngine( CHECK_COMPUTE_STATE );
1421 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1422 algo = gen->GetAlgo(*_father, _subShape);
1423 if (algo) algo->SubmeshRestored( this );
1425 case MESH_ENTITY_REMOVED:
1427 case CHECK_COMPUTE_STATE:
1428 if ( IsMeshComputed() )
1429 _computeState = COMPUTE_OK;
1437 // ----------------------------------------------------------------------
1442 case MODIF_ALGO_STATE:
1443 ComputeStateEngine( CLEAN );
1444 algo = gen->GetAlgo((*_father), _subShape);
1445 if (algo && !algo->NeedDescretBoundary())
1446 CleanDependsOn(); // clean sub-meshes with event CLEAN
1448 case COMPUTE: // nothing to do
1451 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1452 RemoveSubMeshElementsAndNodes();
1453 _computeState = NOT_READY;
1454 if ( _algoState == HYP_OK )
1455 _computeState = READY_TO_COMPUTE;
1457 case SUBMESH_COMPUTED: // nothing to do
1459 case SUBMESH_RESTORED:
1460 ComputeStateEngine( CHECK_COMPUTE_STATE );
1461 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1462 algo = gen->GetAlgo(*_father, _subShape);
1463 if (algo) algo->SubmeshRestored( this );
1465 case MESH_ENTITY_REMOVED:
1466 UpdateDependantsState( CHECK_COMPUTE_STATE );
1467 ComputeStateEngine( CHECK_COMPUTE_STATE );
1468 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1470 case CHECK_COMPUTE_STATE:
1471 if ( !IsMeshComputed() )
1472 if (_algoState == HYP_OK)
1473 _computeState = READY_TO_COMPUTE;
1475 _computeState = NOT_READY;
1483 // ----------------------------------------------------------------------
1485 case FAILED_TO_COMPUTE:
1488 case MODIF_ALGO_STATE:
1489 if (_algoState == HYP_OK)
1490 _computeState = READY_TO_COMPUTE;
1492 _computeState = NOT_READY;
1494 case COMPUTE: // nothing to do
1497 CleanDependants(); // submeshes dependent on me should be cleaned as well
1498 RemoveSubMeshElementsAndNodes();
1500 case SUBMESH_COMPUTED: // allow retry compute
1501 if (_algoState == HYP_OK)
1502 _computeState = READY_TO_COMPUTE;
1504 _computeState = NOT_READY;
1506 case SUBMESH_RESTORED:
1507 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1509 case MESH_ENTITY_REMOVED:
1511 case CHECK_COMPUTE_STATE:
1512 if ( IsMeshComputed() )
1513 _computeState = COMPUTE_OK;
1515 if (_algoState == HYP_OK)
1516 _computeState = READY_TO_COMPUTE;
1518 _computeState = NOT_READY;
1526 // ----------------------------------------------------------------------
1532 NotifyListenersOnEvent( event, COMPUTE_EVENT );
1537 //=======================================================================
1539 * \brief Update compute_state by _computeError and send proper events to
1540 * dependent submeshes
1541 * \retval bool - true if _computeError is NOT set
1543 //=======================================================================
1545 bool SMESH_subMesh::CheckComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1547 bool noErrors = true;
1549 if ( !theShape.IsNull() )
1551 // Check state of submeshes
1552 if ( !theAlgo->NeedDescretBoundary())
1554 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1555 while ( smIt->more() )
1556 if ( !smIt->next()->CheckComputeError( theAlgo ))
1560 // Check state of neighbours
1561 if ( !theAlgo->OnlyUnaryInput() &&
1562 theShape.ShapeType() == TopAbs_COMPOUND &&
1563 !theShape.IsSame( _subShape ))
1565 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1566 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1568 if ( !sm->CheckComputeError( theAlgo ))
1570 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1577 if ( !_computeError || _computeError->IsOK() )
1579 _computeState = COMPUTE_OK;
1583 if ( !_computeError->myAlgo )
1584 _computeError->myAlgo = theAlgo;
1588 text << theAlgo->GetName() << " failed on subshape #" << _Id << " with error ";
1589 if (_computeError->IsCommon() )
1590 text << _computeError->CommonName();
1592 text << _computeError->myName;
1593 if ( _computeError->myComment.size() > 0 )
1594 text << " \"" << _computeError->myComment << "\"";
1597 cout << text << endl;
1598 // Show vertices location of a failed shape
1599 TopTools_IndexedMapOfShape vMap;
1600 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1601 cout << "Subshape vertices " << ( vMap.Extent()>10 ? "(first 10):" : ":") << endl;
1602 for ( int iv = 1; iv <= vMap.Extent() && iv < 11; ++iv ) {
1603 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1604 cout << "#" << _father->GetMeshDS()->ShapeToIndex( vMap( iv )) << " ";
1605 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1610 _computeState = FAILED_TO_COMPUTE;
1614 if ( !theAlgo->OnlyUnaryInput() && !theShape.IsNull() &&
1615 theShape.ShapeType() == TopAbs_COMPOUND )
1617 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1618 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1619 if ( sm != this && !sm->CheckComputeError( theAlgo ))
1626 //=======================================================================
1627 //function : ApplyToCollection
1628 //purpose : Apply theAlgo to all subshapes in theCollection
1629 //=======================================================================
1631 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1632 const TopoDS_Shape& theCollection)
1634 MESSAGE("SMESH_subMesh::ApplyToCollection");
1635 ASSERT ( !theAlgo->NeedDescretBoundary() );
1637 if ( _computeError )
1638 _computeError->myName = COMPERR_OK;
1640 bool ok = theAlgo->Compute( *_father, theCollection );
1642 // set _computeState of subshapes
1643 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1644 for ( ; anExplorer.More(); anExplorer.Next() )
1646 if ( SMESH_subMesh* subMesh = _father->GetSubMeshContaining( anExplorer.Current() ))
1648 bool localOK = subMesh->CheckComputeError( theAlgo );
1649 if ( !ok && localOK && !subMesh->IsMeshComputed() )
1651 subMesh->_computeError = theAlgo->GetComputeError();
1652 if ( subMesh->_computeError->IsOK() )
1653 _computeError = SMESH_ComputeError::New(COMPERR_ALGO_FAILED);
1654 localOK = CheckComputeError( theAlgo );
1657 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1658 subMesh->UpdateSubMeshState( localOK ? COMPUTE_OK : FAILED_TO_COMPUTE );
1666 //=======================================================================
1667 //function : UpdateSubMeshState
1669 //=======================================================================
1671 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1673 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1674 while ( smIt->more() )
1675 smIt->next()->_computeState = theState;
1678 //=======================================================================
1679 //function : ComputeSubMeshStateEngine
1681 //=======================================================================
1683 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1685 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1686 while ( smIt->more() )
1687 smIt->next()->ComputeStateEngine(event);
1690 //=======================================================================
1691 //function : UpdateDependantsState
1693 //=======================================================================
1695 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1697 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1698 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1699 for (; it.More(); it.Next())
1701 const TopoDS_Shape& ancestor = it.Value();
1702 SMESH_subMesh *aSubMesh =
1703 _father->GetSubMeshContaining(ancestor);
1705 aSubMesh->ComputeStateEngine( theEvent );
1709 //=============================================================================
1713 //=============================================================================
1715 void SMESH_subMesh::CleanDependants()
1717 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1719 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1720 for (; it.More(); it.Next())
1722 const TopoDS_Shape& ancestor = it.Value();
1723 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1724 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1725 // will erase mesh on other shapes in a compound
1726 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1727 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1729 aSubMesh->ComputeStateEngine(CLEAN);
1735 //=============================================================================
1739 //=============================================================================
1741 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1743 //SCRUTE(_subShape.ShapeType());
1745 cleanSubMesh( this );
1747 // algo may bind a submesh not to _subShape, eg 3D algo
1748 // sets nodes on SHELL while _subShape may be SOLID
1750 int dim = SMESH_Gen::GetShapeDim( _subShape );
1751 int type = _subShape.ShapeType() + 1;
1752 for ( ; type <= TopAbs_EDGE; type++) {
1753 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1755 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1756 for ( ; exp.More(); exp.Next() )
1757 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1764 //=======================================================================
1765 //function : GetCollection
1766 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1767 // meshed at once along with _subShape
1768 //=======================================================================
1770 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1772 MESSAGE("SMESH_subMesh::GetCollection");
1774 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1776 if ( mainShape.IsSame( _subShape ))
1779 const bool ignoreAuxiliaryHyps = false;
1780 list<const SMESHDS_Hypothesis*> aUsedHyp =
1781 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1783 // put in a compound all shapes with the same hypothesis assigned
1784 // and a good ComputState
1786 TopoDS_Compound aCompound;
1787 BRep_Builder aBuilder;
1788 aBuilder.MakeCompound( aCompound );
1790 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1791 for ( ; anExplorer.More(); anExplorer.Next() )
1793 const TopoDS_Shape& S = anExplorer.Current();
1794 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1795 if ( subMesh == this )
1797 aBuilder.Add( aCompound, S );
1799 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1801 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1802 if (anAlgo == theAlgo &&
1803 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp)
1804 aBuilder.Add( aCompound, S );
1811 //=======================================================================
1812 //function : GetSimilarAttached
1813 //purpose : return a hypothesis attached to theShape.
1814 // If theHyp is provided, similar but not same hypotheses
1815 // is returned; else only applicable ones having theHypType
1817 //=======================================================================
1819 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1820 const SMESH_Hypothesis * theHyp,
1821 const int theHypType)
1823 SMESH_HypoFilter hypoKind;
1824 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1826 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1827 hypoKind.AndNot( hypoKind.Is( theHyp ));
1828 if ( theHyp->IsAuxiliary() )
1829 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1831 hypoKind.AndNot( hypoKind.IsAuxiliary());
1834 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1837 return _father->GetHypothesis( theShape, hypoKind, false );
1840 //=======================================================================
1841 //function : CheckConcurentHypothesis
1842 //purpose : check if there are several applicable hypothesis attached to
1844 //=======================================================================
1846 SMESH_Hypothesis::Hypothesis_Status
1847 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1849 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1851 // is there local hypothesis on me?
1852 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1853 return SMESH_Hypothesis::HYP_OK;
1856 TopoDS_Shape aPrevWithHyp;
1857 const SMESH_Hypothesis* aPrevHyp = 0;
1858 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1859 for (; it.More(); it.Next())
1861 const TopoDS_Shape& ancestor = it.Value();
1862 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1865 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1867 aPrevWithHyp = ancestor;
1870 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1871 return SMESH_Hypothesis::HYP_CONCURENT;
1873 return SMESH_Hypothesis::HYP_OK;
1876 return SMESH_Hypothesis::HYP_OK;
1879 //================================================================================
1881 * \brief Sets an event listener and its data to a submesh
1882 * \param listener - the listener to store
1883 * \param data - the listener data to store
1884 * \param where - the submesh to store the listener and it's data
1885 * \param deleteListener - if true then the listener will be deleted as
1886 * it is removed from where submesh
1888 * It remembers the submesh where it puts the listener in order to delete
1889 * them when HYP_OK algo_state is lost
1890 * After being set, event listener is notified on each event of where submesh.
1892 //================================================================================
1894 void SMESH_subMesh::SetEventListener(EventListener* listener,
1895 EventListenerData* data,
1896 SMESH_subMesh* where)
1898 if ( listener && where ) {
1899 where->SetEventListener( listener, data );
1900 myOwnListeners.push_back( make_pair( where, listener ));
1904 //================================================================================
1906 * \brief Sets an event listener and its data to a submesh
1907 * \param listener - the listener to store
1908 * \param data - the listener data to store
1910 * After being set, event listener is notified on each event of a submesh.
1912 //================================================================================
1914 void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
1916 map< EventListener*, EventListenerData* >::iterator l_d =
1917 myEventListeners.find( listener );
1918 if ( l_d != myEventListeners.end() ) {
1919 if ( l_d->second && l_d->second->IsDeletable() )
1924 myEventListeners.insert( make_pair( listener, data ));
1927 //================================================================================
1929 * \brief Return an event listener data
1930 * \param listener - the listener whose data is
1931 * \retval EventListenerData* - found data, maybe NULL
1933 //================================================================================
1935 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
1937 map< EventListener*, EventListenerData* >::const_iterator l_d =
1938 myEventListeners.find( listener );
1939 if ( l_d != myEventListeners.end() )
1944 //================================================================================
1946 * \brief Notify stored event listeners on the occured event
1947 * \param event - algo_event or compute_event itself
1948 * \param eventType - algo_event or compute_event
1949 * \param subMesh - the submesh where the event occures
1950 * \param data - listener data stored in the subMesh
1951 * \param hyp - hypothesis, if eventType is algo_event
1953 //================================================================================
1955 void SMESH_subMesh::NotifyListenersOnEvent( const int event,
1956 const event_type eventType,
1957 SMESH_Hypothesis* hyp)
1959 map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
1960 for ( ; l_d != myEventListeners.end(); ++l_d )
1961 l_d->first->ProcessEvent( event, eventType, this, l_d->second, hyp );
1964 //================================================================================
1966 * \brief Unregister the listener and delete listener's data
1967 * \param listener - the event listener
1969 //================================================================================
1971 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
1973 map< EventListener*, EventListenerData* >::iterator l_d =
1974 myEventListeners.find( listener );
1975 if ( l_d != myEventListeners.end() ) {
1976 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
1977 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
1978 myEventListeners.erase( l_d );
1982 //================================================================================
1984 * \brief Delete event listeners depending on algo of this submesh
1986 //================================================================================
1988 void SMESH_subMesh::DeleteOwnListeners()
1990 list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
1991 for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
1992 sm_l->first->DeleteEventListener( sm_l->second );
1993 myOwnListeners.clear();
1996 //================================================================================
1998 * \brief Do something on a certain event
1999 * \param event - algo_event or compute_event itself
2000 * \param eventType - algo_event or compute_event
2001 * \param subMesh - the submesh where the event occures
2002 * \param data - listener data stored in the subMesh
2003 * \param hyp - hypothesis, if eventType is algo_event
2005 * The base implementation translates CLEAN event to the subMesh
2006 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2007 * successful COMPUTE event.
2009 //================================================================================
2011 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2012 const int eventType,
2013 SMESH_subMesh* subMesh,
2014 EventListenerData* data,
2015 const SMESH_Hypothesis* /*hyp*/)
2017 if ( data && !data->mySubMeshes.empty() &&
2018 eventType == SMESH_subMesh::COMPUTE_EVENT)
2020 ASSERT( data->mySubMeshes.front() != subMesh );
2022 case SMESH_subMesh::CLEAN:
2023 data->mySubMeshes.front()->ComputeStateEngine( event );
2025 case SMESH_subMesh::COMPUTE:
2026 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2027 data->mySubMeshes.front()->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2036 //================================================================================
2038 * \brief Iterator over submeshes and optionally prepended or appended one
2040 //================================================================================
2042 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2044 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2045 SMESH_subMesh* prepend,
2046 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2048 myCur = prepend ? prepend : myIt->more() ? myIt->next() : 0;
2050 /// Return true if and only if there are other object in this iterator
2055 /// Return the current object and step to the next one
2056 virtual SMESH_subMesh* next()
2058 SMESH_subMesh* res = myCur;
2059 if ( myIt->more() ) { myCur = myIt->next(); }
2060 else { myCur = myAppend; myAppend = 0; }
2067 SMESH_subMesh *myAppend, *myCur;
2068 SMDS_Iterator<SMESH_subMesh*> *myIt;
2072 //================================================================================
2074 * \brief Return iterator on the submeshes this one depends on
2075 * \param includeSelf - this submesh to be returned also
2076 * \param reverse - if true, complex shape submeshes go first
2078 //================================================================================
2080 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2083 SMESH_subMesh *prepend=0, *append=0;
2084 if ( includeSelf ) {
2085 if ( reverse ) prepend = this;
2088 typedef map < int, SMESH_subMesh * > TMap;
2091 return SMESH_subMeshIteratorPtr
2092 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2095 return SMESH_subMeshIteratorPtr
2096 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));