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 //=======================================================================
190 //function : IsMeshComputed
191 //purpose : check if _subMeshDS contains mesh elements
192 //=======================================================================
194 bool SMESH_subMesh::IsMeshComputed() const
196 if ( _alwaysComputed )
198 // algo may bind a submesh not to _subShape, eg 3D algo
199 // sets nodes on SHELL while _subShape may be SOLID
201 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
202 int dim = SMESH_Gen::GetShapeDim( _subShape );
203 int type = _subShape.ShapeType();
204 for ( ; type <= TopAbs_VERTEX; type++) {
205 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
207 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
208 for ( ; exp.More(); exp.Next() )
210 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
212 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
225 //=============================================================================
229 //=============================================================================
231 bool SMESH_subMesh::SubMeshesComputed()
233 int myDim = SMESH_Gen::GetShapeDim( _subShape );
234 int dimToCheck = myDim - 1;
235 bool subMeshesComputed = true;
236 // check subMeshes with upper dimension => reverse iteration
237 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
238 while ( smIt->more() )
240 SMESH_subMesh *sm = smIt->next();
241 if ( sm->_alwaysComputed )
243 const TopoDS_Shape & ss = sm->GetSubShape();
244 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
245 // in checking of existence of edges if the algo needs only faces. Moreover,
246 // degenerated edges may have no submesh, as after computing NETGEN_2D.
247 int dim = SMESH_Gen::GetShapeDim( ss );
248 if (dim < dimToCheck)
249 break; // the rest subMeshes are all of less dimension
250 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
251 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
252 (ds && ( ds->NbNodes() || ds->NbElements() )));
255 int type = ss.ShapeType();
257 subMeshesComputed = false;
261 case TopAbs_COMPOUND:
263 MESSAGE("The not computed sub mesh is a COMPOUND");
266 case TopAbs_COMPSOLID:
268 MESSAGE("The not computed sub mesh is a COMPSOLID");
273 MESSAGE("The not computed sub mesh is a SHEL");
278 MESSAGE("The not computed sub mesh is a WIRE");
283 MESSAGE("The not computed sub mesh is a SOLID");
288 MESSAGE("The not computed sub mesh is a FACE");
293 MESSAGE("The not computed sub mesh is a EDGE");
298 MESSAGE("The not computed sub mesh is of unknown type");
306 return subMeshesComputed;
309 //=============================================================================
313 //=============================================================================
315 bool SMESH_subMesh::SubMeshesReady()
317 bool subMeshesReady = true;
318 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
319 while ( smIt->more() ) {
320 SMESH_subMesh *sm = smIt->next();
321 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
322 sm->GetComputeState() == READY_TO_COMPUTE);
325 subMeshesReady = false;
330 return subMeshesReady;
333 //=============================================================================
335 * Construct dependence on first level subMeshes. complex shapes (compsolid,
336 * shell, wire) are not analysed the same way as simple shapes (solid, face,
338 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
339 * with possible multiples occurences. Multiples occurences corresponds to
340 * internal frontiers within shapes of the collection and must not be keeped.
341 * See FinalizeDependence.
343 //=============================================================================
345 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
347 if (_dependenceAnalysed)
350 //MESSAGE("SMESH_subMesh::DependsOn");
352 int type = _subShape.ShapeType();
356 case TopAbs_COMPOUND:
358 //MESSAGE("compound");
359 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
362 InsertDependence(exp.Current());
364 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
367 InsertDependence(exp.Current()); //only shell not in solid
369 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
372 InsertDependence(exp.Current());
374 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
377 InsertDependence(exp.Current());
381 case TopAbs_COMPSOLID:
383 //MESSAGE("compsolid");
384 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
387 InsertDependence(exp.Current());
394 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
397 InsertDependence(exp.Current());
404 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
407 InsertDependence(exp.Current());
414 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
417 InsertDependence(exp.Current());
424 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
427 InsertDependence(exp.Current());
434 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
437 InsertDependence(exp.Current());
450 _dependenceAnalysed = true;
454 //=============================================================================
456 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
458 //=============================================================================
460 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
462 //MESSAGE("SMESH_subMesh::InsertDependence");
463 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
464 int type = aSubShape.ShapeType();
465 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
466 int cle = aSubMesh->GetId();
467 cle += 10000000 * ordType; // sort map by ordType then index
468 if ( _mapDepend.find( cle ) == _mapDepend.end())
470 _mapDepend[cle] = aSubMesh;
471 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
472 _mapDepend.insert( subMap.begin(), subMap.end() );
476 //=============================================================================
480 //=============================================================================
482 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
484 //MESSAGE("SMESH_subMesh::GetSubShape");
489 //=======================================================================
490 //function : CanAddHypothesis
491 //purpose : return true if theHypothesis can be attached to me:
492 // its dimention is checked
493 //=======================================================================
495 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
497 int aHypDim = theHypothesis->GetDim();
498 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
499 if ( aHypDim <= aShapeDim )
501 // if ( aHypDim < aShapeDim )
502 // return ( _father->IsMainShape( _subShape ));
507 //=======================================================================
508 //function : IsApplicableHypotesis
510 //=======================================================================
512 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
513 const TopAbs_ShapeEnum theShapeType)
515 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
517 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
520 switch ( theShapeType ) {
525 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
528 // Special case for algorithms, building 2D mesh on a whole shell.
529 // Before this fix there was a problem after restoring from study,
530 // because in that case algorithm is assigned before hypothesis
531 // (on shell in problem case) and hypothesis is checked on faces
532 // (because it is 2D), where we have NO_ALGO state.
533 // Now 2D hypothesis is also applicable to shells.
534 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
537 // case TopAbs_COMPSOLID:
538 // case TopAbs_COMPOUND:
544 //=============================================================================
548 //=============================================================================
550 SMESH_Hypothesis::Hypothesis_Status
551 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
553 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
554 //SCRUTE(_algoState);
557 // **** les retour des evenement shape sont significatifs
558 // (add ou remove fait ou non)
559 // le retour des evenement father n'indiquent pas que add ou remove fait
561 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
563 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
564 SMESH_Gen* gen =_father->GetGen();
565 SMESH_Algo* algo = 0;
567 if (_subShape.ShapeType() == TopAbs_VERTEX )
569 if ( anHyp->GetDim() != 0) {
570 if (event == ADD_HYP || event == ADD_ALGO)
571 return SMESH_Hypothesis::HYP_BAD_DIM;
573 return SMESH_Hypothesis::HYP_OK;
576 else if ( _algoState == HYP_OK ) {
577 // update default _algoState
578 if ( event != REMOVE_FATHER_ALGO )
580 _algoState = NO_ALGO;
581 algo = gen->GetAlgo(*_father, _subShape);
583 _algoState = MISSING_HYP;
584 if ( event == REMOVE_FATHER_HYP ||
585 algo->CheckHypothesis(*_father,_subShape, aux_ret))
592 int oldAlgoState = _algoState;
593 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
595 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
597 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
599 // -------------------------------------------
600 // check if a shape needed by algo is present
601 // -------------------------------------------
602 algo = static_cast< SMESH_Algo* >( anHyp );
603 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
604 return SMESH_Hypothesis::HYP_BAD_GEOMETRY;
605 // ----------------------
606 // check mesh conformity
607 // ----------------------
608 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
609 return SMESH_Hypothesis::HYP_NOTCONFORM;
612 // ----------------------------------
613 // add a hypothesis to DS if possible
614 // ----------------------------------
615 if (event == ADD_HYP || event == ADD_ALGO)
617 if ( ! CanAddHypothesis( anHyp )) // check dimension
618 return SMESH_Hypothesis::HYP_BAD_DIM;
620 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
621 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
623 if ( !meshDS->AddHypothesis(_subShape, anHyp))
624 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
627 // --------------------------
628 // remove a hypothesis from DS
629 // --------------------------
630 if (event == REMOVE_HYP || event == REMOVE_ALGO)
632 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
633 return SMESH_Hypothesis::HYP_OK; // nothing changes
635 if (event == REMOVE_ALGO)
637 algo = dynamic_cast<SMESH_Algo*> (anHyp);
638 if (!algo->NeedDescretBoundary())
640 // clean all mesh in the tree of the current submesh;
641 // we must perform it now because later
642 // we will have no information about the type of the removed algo
644 ComputeStateEngine( CLEAN );
646 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
651 // ------------------
652 // analyse algo state
653 // ------------------
654 if (!isApplicableHyp)
655 return ret; // not applicable hypotheses do not change algo state
660 // ----------------------------------------------------------------------
667 algo = gen->GetAlgo((*_father), _subShape);
669 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
670 SetAlgoState(HYP_OK);
671 else if ( algo->IsStatusFatal( aux_ret )) {
672 meshDS->RemoveHypothesis(_subShape, anHyp);
676 SetAlgoState(MISSING_HYP);
683 case ADD_FATHER_ALGO: { // Algo just added in father
684 algo = gen->GetAlgo((*_father), _subShape);
686 if ( algo == anHyp ) {
687 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
688 SetAlgoState(HYP_OK);
690 SetAlgoState(MISSING_HYP);
694 case REMOVE_FATHER_HYP:
696 case REMOVE_FATHER_ALGO: {
697 algo = gen->GetAlgo((*_father), _subShape);
700 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
701 SetAlgoState(HYP_OK);
703 SetAlgoState(MISSING_HYP);
707 case MODIF_HYP: break;
714 // ----------------------------------------------------------------------
720 algo = gen->GetAlgo((*_father), _subShape);
722 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
723 SetAlgoState(HYP_OK);
724 if (SMESH_Hypothesis::IsStatusFatal( ret ))
725 meshDS->RemoveHypothesis(_subShape, anHyp);
726 else if (!_father->IsUsedHypothesis( anHyp, this ))
728 meshDS->RemoveHypothesis(_subShape, anHyp);
729 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
733 case ADD_ALGO: { //already existing algo : on father ?
734 algo = gen->GetAlgo((*_father), _subShape);
736 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
737 SetAlgoState(HYP_OK);
738 else if ( algo->IsStatusFatal( aux_ret )) {
739 meshDS->RemoveHypothesis(_subShape, anHyp);
743 SetAlgoState(MISSING_HYP);
748 case REMOVE_ALGO: { // perhaps a father algo applies ?
749 algo = gen->GetAlgo((*_father), _subShape);
750 if (algo == NULL) // no more algo applying on subShape...
752 SetAlgoState(NO_ALGO);
756 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
757 SetAlgoState(HYP_OK);
759 SetAlgoState(MISSING_HYP);
763 case MODIF_HYP: // assigned hypothesis value may become good
764 case ADD_FATHER_HYP: {
765 algo = gen->GetAlgo((*_father), _subShape);
767 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
768 SetAlgoState(HYP_OK);
770 SetAlgoState(MISSING_HYP);
773 case ADD_FATHER_ALGO: { // new father algo
774 algo = gen->GetAlgo((*_father), _subShape);
776 if ( algo == anHyp ) {
777 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
778 SetAlgoState(HYP_OK);
780 SetAlgoState(MISSING_HYP);
784 case REMOVE_FATHER_HYP: // nothing to do
786 case REMOVE_FATHER_ALGO: {
787 algo = gen->GetAlgo((*_father), _subShape);
788 if (algo == NULL) // no more applying algo on father
790 SetAlgoState(NO_ALGO);
794 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
795 SetAlgoState(HYP_OK);
797 SetAlgoState(MISSING_HYP);
807 // ----------------------------------------------------------------------
813 algo = gen->GetAlgo((*_father), _subShape);
815 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
817 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
818 // ret should be fatal: anHyp was not added
819 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
821 else if (!_father->IsUsedHypothesis( anHyp, this ))
822 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
824 if (SMESH_Hypothesis::IsStatusFatal( ret ))
826 MESSAGE("do not add extra hypothesis");
827 meshDS->RemoveHypothesis(_subShape, anHyp);
835 case ADD_ALGO: { //already existing algo : on father ?
836 algo = gen->GetAlgo((*_father), _subShape);
837 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
838 // check if algo changes
840 f.Init( SMESH_HypoFilter::IsAlgo() );
841 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
842 f.AndNot( SMESH_HypoFilter::Is( algo ));
843 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
845 string(algo->GetName()) != string(prevAlgo->GetName()) )
849 SetAlgoState(MISSING_HYP);
853 algo = gen->GetAlgo((*_father), _subShape);
855 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
856 SetAlgoState(HYP_OK);
858 SetAlgoState(MISSING_HYP);
862 case REMOVE_ALGO: { // perhaps a father algo applies ?
863 algo = gen->GetAlgo((*_father), _subShape);
864 if (algo == NULL) // no more algo applying on subShape...
866 SetAlgoState(NO_ALGO);
870 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
871 // check if algo remains
872 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
876 SetAlgoState(MISSING_HYP);
880 case MODIF_HYP: // hypothesis value may become bad
881 case ADD_FATHER_HYP: { // new father hypothesis ?
882 algo = gen->GetAlgo((*_father), _subShape);
884 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
886 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
890 SetAlgoState(MISSING_HYP);
893 case ADD_FATHER_ALGO: {
894 algo = gen->GetAlgo((*_father), _subShape);
895 if ( algo == anHyp ) { // a new algo on father
896 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
897 // check if algo changes
899 f.Init( SMESH_HypoFilter::IsAlgo() );
900 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
901 f.AndNot( SMESH_HypoFilter::Is( algo ));
902 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
904 string(algo->GetName()) != string(prevAlgo->GetName()) )
908 SetAlgoState(MISSING_HYP);
912 case REMOVE_FATHER_HYP: {
913 algo = gen->GetAlgo((*_father), _subShape);
915 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
916 // is there the same local hyp or maybe a new father algo applied?
917 if ( !GetSimilarAttached( _subShape, anHyp ) )
921 SetAlgoState(MISSING_HYP);
924 case REMOVE_FATHER_ALGO: {
925 algo = gen->GetAlgo((*_father), _subShape);
926 if (algo == NULL) // no more applying algo on father
928 SetAlgoState(NO_ALGO);
932 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
933 // check if algo changes
934 if ( string(algo->GetName()) != string( anHyp->GetName()) )
938 SetAlgoState(MISSING_HYP);
948 // ----------------------------------------------------------------------
955 // detect algorithm hiding
957 if ( ret == SMESH_Hypothesis::HYP_OK &&
958 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
959 algo->GetName() == anHyp->GetName() )
962 SMESH_Gen* gen = _father->GetGen();
963 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
964 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
965 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
966 if ( !upperAlgo->NeedDescretBoundary() )
967 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
970 if ( ret == SMESH_Hypothesis::HYP_OK && !algo->NeedDescretBoundary() ) {
971 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
972 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
973 if ( gen->GetAlgo( *_father, i_sm->second->_subShape ))
974 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
978 bool stateChange = ( _algoState != oldAlgoState );
980 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
981 algo->SetEventListener( this );
983 NotifyListenersOnEvent( event, ALGO_EVENT, anHyp );
985 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
986 DeleteOwnListeners();
987 SetIsAlwaysComputed( false );
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->NbInverseElements() == 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 if ( event == CLEAN ) // this occures for algo which !NeedDescretBoundary() (PAL19272)
1208 cleanSubMesh( this );
1210 _computeState = COMPUTE_OK;
1212 else if ( event == COMPUTE && !_alwaysComputed ) {
1213 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1214 gp_Pnt P = BRep_Tool::Pnt(V);
1215 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1216 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1217 _computeState = COMPUTE_OK;
1220 if ( event == MODIF_ALGO_STATE )
1224 SMESH_Gen *gen = _father->GetGen();
1225 SMESH_Algo *algo = 0;
1227 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1228 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1230 switch (_computeState)
1233 // ----------------------------------------------------------------------
1238 case MODIF_ALGO_STATE:
1239 algo = gen->GetAlgo((*_father), _subShape);
1240 if (algo && !algo->NeedDescretBoundary())
1241 CleanDependsOn(); // clean sub-meshes with event CLEAN
1242 if ( _algoState == HYP_OK )
1243 _computeState = READY_TO_COMPUTE;
1245 case COMPUTE: // nothing to do
1249 RemoveSubMeshElementsAndNodes();
1251 case SUBMESH_COMPUTED: // nothing to do
1253 case SUBMESH_RESTORED:
1254 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1256 case MESH_ENTITY_REMOVED:
1258 case CHECK_COMPUTE_STATE:
1259 if ( IsMeshComputed() )
1260 _computeState = COMPUTE_OK;
1268 // ----------------------------------------------------------------------
1270 case READY_TO_COMPUTE:
1273 case MODIF_ALGO_STATE:
1274 _computeState = NOT_READY;
1275 algo = gen->GetAlgo((*_father), _subShape);
1278 if (!algo->NeedDescretBoundary())
1279 CleanDependsOn(); // clean sub-meshes with event CLEAN
1280 if ( _algoState == HYP_OK )
1281 _computeState = READY_TO_COMPUTE;
1286 algo = gen->GetAlgo((*_father), _subShape);
1288 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1291 MESSAGE("***** verify compute state *****");
1292 _computeState = NOT_READY;
1293 SetAlgoState(MISSING_HYP);
1296 // check submeshes needed
1297 if (_father->HasShapeToMesh() ) {
1298 bool subComputed = SubMeshesComputed();
1299 ret = ( algo->NeedDescretBoundary() ? subComputed :
1300 ( !subComputed || _father->IsNotConformAllowed() ));
1302 _computeState = FAILED_TO_COMPUTE;
1303 if ( !algo->NeedDescretBoundary() )
1305 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1306 "Unexpected computed submesh",algo);
1311 // CleanDependants(); for "UseExisting_*D" algos
1312 // RemoveSubMeshElementsAndNodes();
1314 _computeState = FAILED_TO_COMPUTE;
1315 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1316 TopoDS_Shape shape = _subShape;
1318 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1321 algo->InitComputeError();
1322 MemoryReserve aMemoryReserve;
1323 SMDS_Mesh::CheckMemory();
1324 if ( !_father->HasShapeToMesh() ) // no shape
1326 SMESH_MesherHelper helper( *_father );
1327 helper.SetSubShape( shape );
1328 helper.SetElementsOnShape( true );
1329 ret = algo->Compute(*_father, &helper );
1333 if (!algo->OnlyUnaryInput()) {
1334 shape = GetCollection( gen, algo );
1336 ret = algo->Compute((*_father), shape);
1339 _computeError = algo->GetComputeError();
1341 catch ( std::bad_alloc& exc ) {
1342 printf("std::bad_alloc thrown inside algo->Compute()\n");
1343 if ( _computeError ) {
1344 _computeError->myName = COMPERR_MEMORY_PB;
1345 //_computeError->myComment = exc.what();
1347 cleanSubMesh( this );
1350 catch ( Standard_OutOfMemory& exc ) {
1351 printf("Standard_OutOfMemory thrown inside algo->Compute()\n");
1352 if ( _computeError ) {
1353 _computeError->myName = COMPERR_MEMORY_PB;
1354 //_computeError->myComment = exc.what();
1356 cleanSubMesh( this );
1357 throw std::bad_alloc();
1359 catch (Standard_Failure& ex) {
1360 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1361 _computeError->myName = COMPERR_OCC_EXCEPTION;
1362 _computeError->myComment += ex.DynamicType()->Name();
1363 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1364 _computeError->myComment += ": ";
1365 _computeError->myComment += ex.GetMessageString();
1368 catch ( SALOME_Exception& S_ex ) {
1369 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1370 _computeError->myName = COMPERR_SLM_EXCEPTION;
1371 _computeError->myComment = S_ex.what();
1373 catch ( std::exception& exc ) {
1374 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1375 _computeError->myName = COMPERR_STD_EXCEPTION;
1376 _computeError->myComment = exc.what();
1379 if ( _computeError )
1380 _computeError->myName = COMPERR_EXCEPTION;
1384 if (ret && !_alwaysComputed) { // check if anything was built
1385 ret = ( GetSubMeshDS() && ( GetSubMeshDS()->NbElements() || GetSubMeshDS()->NbNodes() ));
1387 bool isComputeErrorSet = !CheckComputeError( algo, shape );
1388 if (!ret && !isComputeErrorSet)
1390 // Set _computeError
1391 if ( !_computeError )
1392 _computeError = SMESH_ComputeError::New();
1393 if ( _computeError->IsOK() )
1394 _computeError->myName = COMPERR_ALGO_FAILED;
1395 _computeState = FAILED_TO_COMPUTE;
1399 _computeError.reset();
1401 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1406 RemoveSubMeshElementsAndNodes();
1407 _computeState = NOT_READY;
1408 algo = gen->GetAlgo((*_father), _subShape);
1411 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1413 _computeState = READY_TO_COMPUTE;
1415 SetAlgoState(MISSING_HYP);
1418 case SUBMESH_COMPUTED: // nothing to do
1420 case SUBMESH_RESTORED:
1421 // check if a mesh is already computed that may
1422 // happen after retrieval from a file
1423 ComputeStateEngine( CHECK_COMPUTE_STATE );
1424 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1425 algo = gen->GetAlgo(*_father, _subShape);
1426 if (algo) algo->SubmeshRestored( this );
1428 case MESH_ENTITY_REMOVED:
1430 case CHECK_COMPUTE_STATE:
1431 if ( IsMeshComputed() )
1432 _computeState = COMPUTE_OK;
1440 // ----------------------------------------------------------------------
1445 case MODIF_ALGO_STATE:
1446 ComputeStateEngine( CLEAN );
1447 algo = gen->GetAlgo((*_father), _subShape);
1448 if (algo && !algo->NeedDescretBoundary())
1449 CleanDependsOn(); // clean sub-meshes with event CLEAN
1451 case COMPUTE: // nothing to do
1454 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1455 RemoveSubMeshElementsAndNodes();
1456 _computeState = NOT_READY;
1457 if ( _algoState == HYP_OK )
1458 _computeState = READY_TO_COMPUTE;
1460 case SUBMESH_COMPUTED: // nothing to do
1462 case SUBMESH_RESTORED:
1463 ComputeStateEngine( CHECK_COMPUTE_STATE );
1464 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1465 algo = gen->GetAlgo(*_father, _subShape);
1466 if (algo) algo->SubmeshRestored( this );
1468 case MESH_ENTITY_REMOVED:
1469 UpdateDependantsState( CHECK_COMPUTE_STATE );
1470 ComputeStateEngine( CHECK_COMPUTE_STATE );
1471 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1473 case CHECK_COMPUTE_STATE:
1474 if ( !IsMeshComputed() )
1475 if (_algoState == HYP_OK)
1476 _computeState = READY_TO_COMPUTE;
1478 _computeState = NOT_READY;
1486 // ----------------------------------------------------------------------
1488 case FAILED_TO_COMPUTE:
1491 case MODIF_ALGO_STATE:
1492 if (_algoState == HYP_OK)
1493 _computeState = READY_TO_COMPUTE;
1495 _computeState = NOT_READY;
1497 case COMPUTE: // nothing to do
1500 CleanDependants(); // submeshes dependent on me should be cleaned as well
1501 RemoveSubMeshElementsAndNodes();
1503 case SUBMESH_COMPUTED: // allow retry compute
1504 if (_algoState == HYP_OK)
1505 _computeState = READY_TO_COMPUTE;
1507 _computeState = NOT_READY;
1509 case SUBMESH_RESTORED:
1510 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1512 case MESH_ENTITY_REMOVED:
1514 case CHECK_COMPUTE_STATE:
1515 if ( IsMeshComputed() )
1516 _computeState = COMPUTE_OK;
1518 if (_algoState == HYP_OK)
1519 _computeState = READY_TO_COMPUTE;
1521 _computeState = NOT_READY;
1529 // ----------------------------------------------------------------------
1535 NotifyListenersOnEvent( event, COMPUTE_EVENT );
1540 //=======================================================================
1542 * \brief Update compute_state by _computeError and send proper events to
1543 * dependent submeshes
1544 * \retval bool - true if _computeError is NOT set
1546 //=======================================================================
1548 bool SMESH_subMesh::CheckComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1550 bool noErrors = true;
1552 if ( !theShape.IsNull() )
1554 // Check state of submeshes
1555 if ( !theAlgo->NeedDescretBoundary())
1557 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1558 while ( smIt->more() )
1559 if ( !smIt->next()->CheckComputeError( theAlgo ))
1563 // Check state of neighbours
1564 if ( !theAlgo->OnlyUnaryInput() &&
1565 theShape.ShapeType() == TopAbs_COMPOUND &&
1566 !theShape.IsSame( _subShape ))
1568 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1569 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1571 if ( !sm->CheckComputeError( theAlgo ))
1573 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1580 if ( !_computeError || _computeError->IsOK() )
1582 _computeState = COMPUTE_OK;
1586 if ( !_computeError->myAlgo )
1587 _computeError->myAlgo = theAlgo;
1591 text << theAlgo->GetName() << " failed on subshape #" << _Id << " with error ";
1592 if (_computeError->IsCommon() )
1593 text << _computeError->CommonName();
1595 text << _computeError->myName;
1596 if ( _computeError->myComment.size() > 0 )
1597 text << " \"" << _computeError->myComment << "\"";
1600 cout << text << endl;
1601 // Show vertices location of a failed shape
1602 TopTools_IndexedMapOfShape vMap;
1603 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1604 cout << "Subshape vertices " << ( vMap.Extent()>10 ? "(first 10):" : ":") << endl;
1605 for ( int iv = 1; iv <= vMap.Extent() && iv < 11; ++iv ) {
1606 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1607 cout << "#" << _father->GetMeshDS()->ShapeToIndex( vMap( iv )) << " ";
1608 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1613 _computeState = FAILED_TO_COMPUTE;
1620 //=======================================================================
1621 //function : ApplyToCollection
1622 //purpose : Apply theAlgo to all subshapes in theCollection
1623 //=======================================================================
1625 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1626 const TopoDS_Shape& theCollection)
1628 MESSAGE("SMESH_subMesh::ApplyToCollection");
1629 ASSERT ( !theAlgo->NeedDescretBoundary() );
1631 if ( _computeError )
1632 _computeError->myName = COMPERR_OK;
1634 bool ok = theAlgo->Compute( *_father, theCollection );
1636 // set _computeState of subshapes
1637 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1638 for ( ; anExplorer.More(); anExplorer.Next() )
1640 if ( SMESH_subMesh* subMesh = _father->GetSubMeshContaining( anExplorer.Current() ))
1642 bool localOK = subMesh->CheckComputeError( theAlgo );
1643 if ( !ok && localOK && !subMesh->IsMeshComputed() )
1645 subMesh->_computeError = theAlgo->GetComputeError();
1646 if ( subMesh->_computeError->IsOK() )
1647 _computeError = SMESH_ComputeError::New(COMPERR_ALGO_FAILED);
1648 localOK = CheckComputeError( theAlgo );
1651 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1652 subMesh->UpdateSubMeshState( localOK ? COMPUTE_OK : FAILED_TO_COMPUTE );
1660 //=======================================================================
1661 //function : UpdateSubMeshState
1663 //=======================================================================
1665 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1667 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1668 while ( smIt->more() )
1669 smIt->next()->_computeState = theState;
1672 //=======================================================================
1673 //function : ComputeSubMeshStateEngine
1675 //=======================================================================
1677 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1679 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1680 while ( smIt->more() )
1681 smIt->next()->ComputeStateEngine(event);
1684 //=======================================================================
1685 //function : UpdateDependantsState
1687 //=======================================================================
1689 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1691 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1692 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1693 for (; it.More(); it.Next())
1695 const TopoDS_Shape& ancestor = it.Value();
1696 SMESH_subMesh *aSubMesh =
1697 _father->GetSubMeshContaining(ancestor);
1699 aSubMesh->ComputeStateEngine( theEvent );
1703 //=============================================================================
1707 //=============================================================================
1709 void SMESH_subMesh::CleanDependants()
1711 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1713 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1714 for (; it.More(); it.Next())
1716 const TopoDS_Shape& ancestor = it.Value();
1717 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1718 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1719 // will erase mesh on other shapes in a compound
1720 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1721 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1723 aSubMesh->ComputeStateEngine(CLEAN);
1729 //=============================================================================
1733 //=============================================================================
1735 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1737 //SCRUTE(_subShape.ShapeType());
1739 cleanSubMesh( this );
1741 // algo may bind a submesh not to _subShape, eg 3D algo
1742 // sets nodes on SHELL while _subShape may be SOLID
1744 int dim = SMESH_Gen::GetShapeDim( _subShape );
1745 int type = _subShape.ShapeType() + 1;
1746 for ( ; type <= TopAbs_EDGE; type++) {
1747 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1749 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1750 for ( ; exp.More(); exp.Next() )
1751 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1758 //=======================================================================
1759 //function : GetCollection
1760 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1761 // meshed at once along with _subShape
1762 //=======================================================================
1764 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1766 MESSAGE("SMESH_subMesh::GetCollection");
1768 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1770 if ( mainShape.IsSame( _subShape ))
1773 const bool ignoreAuxiliaryHyps = false;
1774 list<const SMESHDS_Hypothesis*> aUsedHyp =
1775 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1777 // put in a compound all shapes with the same hypothesis assigned
1778 // and a good ComputState
1780 TopoDS_Compound aCompound;
1781 BRep_Builder aBuilder;
1782 aBuilder.MakeCompound( aCompound );
1784 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1785 for ( ; anExplorer.More(); anExplorer.Next() )
1787 const TopoDS_Shape& S = anExplorer.Current();
1788 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1789 if ( subMesh == this )
1791 aBuilder.Add( aCompound, S );
1793 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1795 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1796 if (anAlgo == theAlgo &&
1797 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp)
1798 aBuilder.Add( aCompound, S );
1805 //=======================================================================
1806 //function : GetSimilarAttached
1807 //purpose : return a hypothesis attached to theShape.
1808 // If theHyp is provided, similar but not same hypotheses
1809 // is returned; else only applicable ones having theHypType
1811 //=======================================================================
1813 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1814 const SMESH_Hypothesis * theHyp,
1815 const int theHypType)
1817 SMESH_HypoFilter hypoKind;
1818 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1820 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1821 hypoKind.AndNot( hypoKind.Is( theHyp ));
1822 if ( theHyp->IsAuxiliary() )
1823 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1825 hypoKind.AndNot( hypoKind.IsAuxiliary());
1828 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1831 return _father->GetHypothesis( theShape, hypoKind, false );
1834 //=======================================================================
1835 //function : CheckConcurentHypothesis
1836 //purpose : check if there are several applicable hypothesis attached to
1838 //=======================================================================
1840 SMESH_Hypothesis::Hypothesis_Status
1841 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1843 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1845 // is there local hypothesis on me?
1846 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1847 return SMESH_Hypothesis::HYP_OK;
1850 TopoDS_Shape aPrevWithHyp;
1851 const SMESH_Hypothesis* aPrevHyp = 0;
1852 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1853 for (; it.More(); it.Next())
1855 const TopoDS_Shape& ancestor = it.Value();
1856 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1859 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1861 aPrevWithHyp = ancestor;
1864 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1865 return SMESH_Hypothesis::HYP_CONCURENT;
1867 return SMESH_Hypothesis::HYP_OK;
1870 return SMESH_Hypothesis::HYP_OK;
1873 //================================================================================
1875 * \brief Sets an event listener and its data to a submesh
1876 * \param listener - the listener to store
1877 * \param data - the listener data to store
1878 * \param where - the submesh to store the listener and it's data
1879 * \param deleteListener - if true then the listener will be deleted as
1880 * it is removed from where submesh
1882 * It remembers the submesh where it puts the listener in order to delete
1883 * them when HYP_OK algo_state is lost
1884 * After being set, event listener is notified on each event of where submesh.
1886 //================================================================================
1888 void SMESH_subMesh::SetEventListener(EventListener* listener,
1889 EventListenerData* data,
1890 SMESH_subMesh* where)
1892 if ( listener && where ) {
1893 where->SetEventListener( listener, data );
1894 myOwnListeners.push_back( make_pair( where, listener ));
1898 //================================================================================
1900 * \brief Sets an event listener and its data to a submesh
1901 * \param listener - the listener to store
1902 * \param data - the listener data to store
1904 * After being set, event listener is notified on each event of a submesh.
1906 //================================================================================
1908 void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
1910 map< EventListener*, EventListenerData* >::iterator l_d =
1911 myEventListeners.find( listener );
1912 if ( l_d != myEventListeners.end() ) {
1913 EventListenerData* curData = l_d->second;
1914 if ( curData && curData != data && curData->IsDeletable() )
1919 myEventListeners.insert( make_pair( listener, data ));
1922 //================================================================================
1924 * \brief Return an event listener data
1925 * \param listener - the listener whose data is
1926 * \retval EventListenerData* - found data, maybe NULL
1928 //================================================================================
1930 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
1932 map< EventListener*, EventListenerData* >::const_iterator l_d =
1933 myEventListeners.find( listener );
1934 if ( l_d != myEventListeners.end() )
1939 //================================================================================
1941 * \brief Notify stored event listeners on the occured event
1942 * \param event - algo_event or compute_event itself
1943 * \param eventType - algo_event or compute_event
1944 * \param subMesh - the submesh where the event occures
1945 * \param data - listener data stored in the subMesh
1946 * \param hyp - hypothesis, if eventType is algo_event
1948 //================================================================================
1950 void SMESH_subMesh::NotifyListenersOnEvent( const int event,
1951 const event_type eventType,
1952 SMESH_Hypothesis* hyp)
1954 map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
1955 for ( ; l_d != myEventListeners.end(); ++l_d )
1956 l_d->first->ProcessEvent( event, eventType, this, l_d->second, hyp );
1959 //================================================================================
1961 * \brief Unregister the listener and delete listener's data
1962 * \param listener - the event listener
1964 //================================================================================
1966 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
1968 map< EventListener*, EventListenerData* >::iterator l_d =
1969 myEventListeners.find( listener );
1970 if ( l_d != myEventListeners.end() ) {
1971 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
1972 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
1973 myEventListeners.erase( l_d );
1977 //================================================================================
1979 * \brief Delete event listeners depending on algo of this submesh
1981 //================================================================================
1983 void SMESH_subMesh::DeleteOwnListeners()
1985 list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
1986 for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
1987 sm_l->first->DeleteEventListener( sm_l->second );
1988 myOwnListeners.clear();
1991 //================================================================================
1993 * \brief Do something on a certain event
1994 * \param event - algo_event or compute_event itself
1995 * \param eventType - algo_event or compute_event
1996 * \param subMesh - the submesh where the event occures
1997 * \param data - listener data stored in the subMesh
1998 * \param hyp - hypothesis, if eventType is algo_event
2000 * The base implementation translates CLEAN event to the subMesh
2001 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2002 * successful COMPUTE event.
2004 //================================================================================
2006 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2007 const int eventType,
2008 SMESH_subMesh* subMesh,
2009 EventListenerData* data,
2010 const SMESH_Hypothesis* /*hyp*/)
2012 if ( data && !data->mySubMeshes.empty() &&
2013 eventType == SMESH_subMesh::COMPUTE_EVENT)
2015 ASSERT( data->mySubMeshes.front() != subMesh );
2016 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2017 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2019 case SMESH_subMesh::CLEAN:
2020 for ( ; smIt != smEnd; ++ smIt)
2021 (*smIt)->ComputeStateEngine( event );
2023 case SMESH_subMesh::COMPUTE:
2024 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2025 for ( ; smIt != smEnd; ++ smIt)
2026 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2035 //================================================================================
2037 * \brief Iterator over submeshes and optionally prepended or appended one
2039 //================================================================================
2041 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2043 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2044 SMESH_subMesh* prepend,
2045 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2047 myCur = prepend ? prepend : myIt->more() ? myIt->next() : 0;
2049 /// Return true if and only if there are other object in this iterator
2054 /// Return the current object and step to the next one
2055 virtual SMESH_subMesh* next()
2057 SMESH_subMesh* res = myCur;
2058 if ( myIt->more() ) { myCur = myIt->next(); }
2059 else { myCur = myAppend; myAppend = 0; }
2066 SMESH_subMesh *myAppend, *myCur;
2067 SMDS_Iterator<SMESH_subMesh*> *myIt;
2071 //================================================================================
2073 * \brief Return iterator on the submeshes this one depends on
2074 * \param includeSelf - this submesh to be returned also
2075 * \param reverse - if true, complex shape submeshes go first
2077 //================================================================================
2079 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2082 SMESH_subMesh *prepend=0, *append=0;
2083 if ( includeSelf ) {
2084 if ( reverse ) prepend = this;
2087 typedef map < int, SMESH_subMesh * > TMap;
2090 return SMESH_subMeshIteratorPtr
2091 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2094 return SMESH_subMeshIteratorPtr
2095 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));