1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // SMESH SMESH : implementaion of SMESH idl descriptions
23 // File : SMESH_subMesh.cxx
24 // Author : Paul RASCLE, EDF
27 #include "SMESH_subMesh.hxx"
29 #include "SMESH_Algo.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_HypoFilter.hxx"
32 #include "SMESH_Hypothesis.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_MesherHelper.hxx"
35 #include "SMESH_subMeshEventListener.hxx"
36 #include "SMESH_Comment.hxx"
37 #include "SMDS_SetIterator.hxx"
38 #include "SMDSAbs_ElementType.hxx"
40 #include <Basics_OCCTVersion.hxx>
42 #include "utilities.h"
44 #include "Basics_Utils.hxx"
46 #include <BRep_Builder.hxx>
47 #include <BRep_Tool.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
52 #include <TopoDS_Compound.hxx>
54 #include <TopExp_Explorer.hxx>
55 #include <TopoDS_Iterator.hxx>
57 #include <Standard_OutOfMemory.hxx>
58 #include <Standard_ErrorHandler.hxx>
64 //=============================================================================
66 * \brief Allocate some memory at construction and release it at destruction.
67 * Is used to be able to continue working after mesh generation breaks due to
70 //=============================================================================
75 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
76 ~MemoryReserve() { delete [] myBuf; }
79 //=============================================================================
81 * default constructor:
83 //=============================================================================
85 SMESH_subMesh::SMESH_subMesh(int Id,
87 SMESHDS_Mesh * meshDS,
88 const TopoDS_Shape & aSubShape)
90 _subShape = aSubShape;
91 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
94 _dependenceAnalysed = _alwaysComputed = false;
96 if (_subShape.ShapeType() == TopAbs_VERTEX)
99 _computeState = READY_TO_COMPUTE;
103 _algoState = NO_ALGO;
104 _computeState = NOT_READY;
108 //=============================================================================
112 //=============================================================================
114 SMESH_subMesh::~SMESH_subMesh()
116 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
118 deleteOwnListeners();
121 //=============================================================================
125 //=============================================================================
127 int SMESH_subMesh::GetId() const
129 //MESSAGE("SMESH_subMesh::GetId");
133 //=============================================================================
137 //=============================================================================
139 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
141 // submesh appears in DS only when a mesher set nodes and elements on a shape
142 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
145 //=============================================================================
149 //=============================================================================
151 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
153 if ( !GetSubMeshDS() ) {
154 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
155 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
157 return GetSubMeshDS();
160 //=============================================================================
164 //=============================================================================
166 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
168 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
169 while ( smIt->more() ) {
170 SMESH_subMesh *sm = smIt->next();
171 if ( sm->GetComputeState() == READY_TO_COMPUTE )
174 return 0; // nothing to compute
177 //================================================================================
179 * \brief Returns a current algorithm
181 //================================================================================
183 SMESH_Algo* SMESH_subMesh::GetAlgo() const
185 return _father->GetGen()->GetAlgo(*_father, _subShape);
188 //================================================================================
190 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
191 * none mesh entity is bound to it (PAL13615, 2nd part)
193 //================================================================================
195 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
197 _alwaysComputed = isAlCo;
198 if ( _alwaysComputed )
199 _computeState = COMPUTE_OK;
201 ComputeStateEngine( CHECK_COMPUTE_STATE );
204 //=======================================================================
206 * \brief Return true if no mesh entities is bound to the submesh
208 //=======================================================================
210 bool SMESH_subMesh::IsEmpty() const
212 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
213 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
217 //=======================================================================
218 //function : IsMeshComputed
219 //purpose : check if _subMeshDS contains mesh elements
220 //=======================================================================
222 bool SMESH_subMesh::IsMeshComputed() const
224 if ( _alwaysComputed )
226 // algo may bind a submesh not to _subShape, eg 3D algo
227 // sets nodes on SHELL while _subShape may be SOLID
229 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
230 int dim = SMESH_Gen::GetShapeDim( _subShape );
231 int type = _subShape.ShapeType();
232 for ( ; type <= TopAbs_VERTEX; type++) {
233 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
235 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
236 for ( ; exp.More(); exp.Next() )
238 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
240 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
253 //=============================================================================
257 //=============================================================================
259 bool SMESH_subMesh::subMeshesComputed()
261 int myDim = SMESH_Gen::GetShapeDim( _subShape );
262 int dimToCheck = myDim - 1;
263 bool subMeshesComputed = true;
264 // check subMeshes with upper dimension => reverse iteration
265 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
266 while ( smIt->more() )
268 SMESH_subMesh *sm = smIt->next();
269 if ( sm->_alwaysComputed )
271 const TopoDS_Shape & ss = sm->GetSubShape();
272 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
273 // in checking of existence of edges if the algo needs only faces. Moreover,
274 // degenerated edges may have no submesh, as after computing NETGEN_2D.
275 int dim = SMESH_Gen::GetShapeDim( ss );
276 if (dim < dimToCheck)
277 break; // the rest subMeshes are all of less dimension
278 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
279 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
280 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
283 int type = ss.ShapeType();
285 subMeshesComputed = false;
289 case TopAbs_COMPOUND:
291 MESSAGE("The not computed sub mesh is a COMPOUND");
294 case TopAbs_COMPSOLID:
296 MESSAGE("The not computed sub mesh is a COMPSOLID");
301 MESSAGE("The not computed sub mesh is a SHEL");
306 MESSAGE("The not computed sub mesh is a WIRE");
311 MESSAGE("The not computed sub mesh is a SOLID");
316 MESSAGE("The not computed sub mesh is a FACE");
321 MESSAGE("The not computed sub mesh is a EDGE");
326 MESSAGE("The not computed sub mesh is of unknown type");
334 return subMeshesComputed;
337 //=============================================================================
341 //=============================================================================
343 // bool SMESH_subMesh::SubMeshesReady()
345 // bool subMeshesReady = true;
346 // SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
347 // while ( smIt->more() ) {
348 // SMESH_subMesh *sm = smIt->next();
349 // bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
350 // sm->GetComputeState() == READY_TO_COMPUTE);
353 // subMeshesReady = false;
354 // SCRUTE(sm->GetId());
358 // return subMeshesReady;
361 //=============================================================================
363 * Construct dependence on first level subMeshes. complex shapes (compsolid,
364 * shell, wire) are not analysed the same way as simple shapes (solid, face,
366 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
367 * with possible multiples occurences. Multiples occurences corresponds to
368 * internal frontiers within shapes of the collection and must not be keeped.
369 * See FinalizeDependence.
371 //=============================================================================
373 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
375 if (_dependenceAnalysed)
378 //MESSAGE("SMESH_subMesh::DependsOn");
380 int type = _subShape.ShapeType();
384 case TopAbs_COMPOUND:
386 //MESSAGE("compound");
387 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();exp.Next())
389 insertDependence(exp.Current());
391 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More(); exp.Next())
393 if ( BRep_Tool::IsClosed(exp.Current() ))
394 insertDependence(exp.Current()); //only shell not in solid
396 for (TopExp_Explorer expF(exp.Current(), TopAbs_FACE); expF.More();expF.Next())
397 insertDependence(expF.Current()); // issue 0020959: HEXA_3D fails on shell
400 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();exp.Next())
402 insertDependence(exp.Current());
404 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();exp.Next())
406 insertDependence(exp.Current());
410 case TopAbs_COMPSOLID:
412 //MESSAGE("compsolid");
413 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More(); exp.Next())
415 insertDependence(exp.Current());
422 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More(); exp.Next())
424 insertDependence(exp.Current());
431 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More(); exp.Next())
433 insertDependence(exp.Current());
440 if(_father->HasShapeToMesh()) {
441 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();exp.Next())
443 insertDependence(exp.Current());
451 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();exp.Next())
453 insertDependence(exp.Current());
460 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More(); exp.Next())
462 insertDependence(exp.Current());
475 _dependenceAnalysed = true;
479 //=============================================================================
481 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
483 //=============================================================================
485 void SMESH_subMesh::insertDependence(const TopoDS_Shape aSubShape)
487 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
488 int type = aSubShape.ShapeType();
489 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
490 int cle = aSubMesh->GetId();
491 cle += 10000000 * ordType; // sort map by ordType then index
492 if ( _mapDepend.find( cle ) == _mapDepend.end())
494 _mapDepend[cle] = aSubMesh;
495 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
496 _mapDepend.insert( subMap.begin(), subMap.end() );
500 //=============================================================================
504 //=============================================================================
506 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
508 //MESSAGE("SMESH_subMesh::GetSubShape");
513 //=======================================================================
514 //function : CanAddHypothesis
515 //purpose : return true if theHypothesis can be attached to me:
516 // its dimention is checked
517 //=======================================================================
519 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
521 int aHypDim = theHypothesis->GetDim();
522 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
523 // issue 21106. Forbid 3D mesh on the SHELL
524 // if (aHypDim == 3 && aShapeDim == 3) {
525 // // check case of open shell
526 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
527 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
530 if ( aHypDim <= aShapeDim )
536 //=======================================================================
537 //function : IsApplicableHypotesis
539 //=======================================================================
541 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
542 const TopAbs_ShapeEnum theShapeType)
544 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
547 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
548 // issue 21106. Forbid 3D mesh on the SHELL
549 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
555 switch ( theShapeType ) {
560 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
563 // Special case for algorithms, building 2D mesh on a whole shell.
564 // Before this fix there was a problem after restoring from study,
565 // because in that case algorithm is assigned before hypothesis
566 // (on shell in problem case) and hypothesis is checked on faces
567 // (because it is 2D), where we have NO_ALGO state.
568 // Now 2D hypothesis is also applicable to shells.
569 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
572 // case TopAbs_COMPSOLID:
573 // case TopAbs_COMPOUND:
579 //=============================================================================
583 //=============================================================================
585 SMESH_Hypothesis::Hypothesis_Status
586 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
588 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
589 //SCRUTE(_algoState);
592 // **** les retour des evenement shape sont significatifs
593 // (add ou remove fait ou non)
594 // le retour des evenement father n'indiquent pas que add ou remove fait
596 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
598 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
599 //SMESH_Gen* gen =_father->GetGen();
600 SMESH_Algo* algo = 0;
602 if (_subShape.ShapeType() == TopAbs_VERTEX )
604 if ( anHyp->GetDim() != 0) {
605 if (event == ADD_HYP || event == ADD_ALGO)
606 return SMESH_Hypothesis::HYP_BAD_DIM;
608 return SMESH_Hypothesis::HYP_OK;
611 else if ( _algoState == HYP_OK ) {
612 // update default _algoState
613 if ( event != REMOVE_FATHER_ALGO )
615 _algoState = NO_ALGO;
618 _algoState = MISSING_HYP;
619 if ( event == REMOVE_FATHER_HYP ||
620 algo->CheckHypothesis(*_father,_subShape, aux_ret))
627 int oldAlgoState = _algoState;
628 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
629 bool needFullClean = false;
631 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
633 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
635 // -------------------------------------------
636 // check if a shape needed by algo is present
637 // -------------------------------------------
638 algo = static_cast< SMESH_Algo* >( anHyp );
639 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
640 return SMESH_Hypothesis::HYP_NEED_SHAPE;
641 // ----------------------
642 // check mesh conformity
643 // ----------------------
644 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
645 return SMESH_Hypothesis::HYP_NOTCONFORM;
647 // check if all-dimensional algo is hidden by other local one
648 if ( event == ADD_ALGO ) {
649 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
650 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
651 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
652 if ( SMESH_Algo * curAlgo = (SMESH_Algo*) _father->GetHypothesis( _subShape, filter, true ))
653 needFullClean = ( !curAlgo->NeedDiscreteBoundary() );
657 // ----------------------------------
658 // add a hypothesis to DS if possible
659 // ----------------------------------
660 if (event == ADD_HYP || event == ADD_ALGO)
662 if ( ! CanAddHypothesis( anHyp )) // check dimension
663 return SMESH_Hypothesis::HYP_BAD_DIM;
665 if ( /*!anHyp->IsAuxiliary() &&*/ getSimilarAttached( _subShape, anHyp ) )
666 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
668 if ( !meshDS->AddHypothesis(_subShape, anHyp))
669 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
672 // --------------------------
673 // remove a hypothesis from DS
674 // --------------------------
675 if (event == REMOVE_HYP || event == REMOVE_ALGO)
677 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
678 return SMESH_Hypothesis::HYP_OK; // nothing changes
680 if (event == REMOVE_ALGO)
682 algo = dynamic_cast<SMESH_Algo*> (anHyp);
683 if (!algo->NeedDiscreteBoundary())
685 // clean all mesh in the tree of the current submesh;
686 // we must perform it now because later
687 // we will have no information about the type of the removed algo
688 needFullClean = true;
693 // ------------------
694 // analyse algo state
695 // ------------------
696 if (!isApplicableHyp)
697 return ret; // not applicable hypotheses do not change algo state
702 // ----------------------------------------------------------------------
711 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
712 setAlgoState(HYP_OK);
713 else if ( algo->IsStatusFatal( aux_ret )) {
714 meshDS->RemoveHypothesis(_subShape, anHyp);
718 setAlgoState(MISSING_HYP);
725 case ADD_FATHER_ALGO: { // Algo just added in father
728 if ( algo == anHyp ) {
729 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
730 setAlgoState(HYP_OK);
732 setAlgoState(MISSING_HYP);
736 case REMOVE_FATHER_HYP:
738 case REMOVE_FATHER_ALGO: {
742 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
743 setAlgoState(HYP_OK);
745 setAlgoState(MISSING_HYP);
749 case MODIF_HYP: break;
756 // ----------------------------------------------------------------------
764 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
765 setAlgoState(HYP_OK);
766 if (SMESH_Hypothesis::IsStatusFatal( ret ))
767 meshDS->RemoveHypothesis(_subShape, anHyp);
768 else if (!_father->IsUsedHypothesis( anHyp, this ))
770 meshDS->RemoveHypothesis(_subShape, anHyp);
771 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
775 case ADD_ALGO: { //already existing algo : on father ?
778 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
779 setAlgoState(HYP_OK);
780 else if ( algo->IsStatusFatal( aux_ret )) {
781 meshDS->RemoveHypothesis(_subShape, anHyp);
785 setAlgoState(MISSING_HYP);
790 case REMOVE_ALGO: { // perhaps a father algo applies ?
792 if (algo == NULL) // no more algo applying on sub-shape...
794 setAlgoState(NO_ALGO);
798 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
799 setAlgoState(HYP_OK);
801 setAlgoState(MISSING_HYP);
805 case MODIF_HYP: // assigned hypothesis value may become good
806 case ADD_FATHER_HYP: {
809 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
810 setAlgoState(HYP_OK);
812 setAlgoState(MISSING_HYP);
815 case ADD_FATHER_ALGO: { // new father algo
818 if ( algo == anHyp ) {
819 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
820 setAlgoState(HYP_OK);
822 setAlgoState(MISSING_HYP);
826 case REMOVE_FATHER_HYP: // nothing to do
828 case REMOVE_FATHER_ALGO: {
830 if (algo == NULL) // no more applying algo on father
832 setAlgoState(NO_ALGO);
836 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
837 setAlgoState(HYP_OK);
839 setAlgoState(MISSING_HYP);
849 // ----------------------------------------------------------------------
857 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
859 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
860 // ret should be fatal: anHyp was not added
861 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
863 else if (!_father->IsUsedHypothesis( anHyp, this ))
864 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
866 if (SMESH_Hypothesis::IsStatusFatal( ret ))
868 MESSAGE("do not add extra hypothesis");
869 meshDS->RemoveHypothesis(_subShape, anHyp);
877 case ADD_ALGO: { //already existing algo : on father ?
879 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
880 // check if algo changes
882 f.Init( SMESH_HypoFilter::IsAlgo() );
883 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
884 f.AndNot( SMESH_HypoFilter::Is( algo ));
885 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
887 string(algo->GetName()) != string(prevAlgo->GetName()) )
891 setAlgoState(MISSING_HYP);
897 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
898 setAlgoState(HYP_OK);
900 setAlgoState(MISSING_HYP);
904 case REMOVE_ALGO: { // perhaps a father algo applies ?
906 if (algo == NULL) // no more algo applying on sub-shape...
908 setAlgoState(NO_ALGO);
912 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
913 // check if algo remains
914 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
918 setAlgoState(MISSING_HYP);
922 case MODIF_HYP: // hypothesis value may become bad
923 case ADD_FATHER_HYP: { // new father hypothesis ?
926 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
928 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
932 setAlgoState(MISSING_HYP);
935 case ADD_FATHER_ALGO: {
937 if ( algo == anHyp ) { // a new algo on father
938 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
939 // check if algo changes
941 f.Init( SMESH_HypoFilter::IsAlgo() );
942 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
943 f.AndNot( SMESH_HypoFilter::Is( algo ));
944 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
946 string(algo->GetName()) != string(prevAlgo->GetName()) )
950 setAlgoState(MISSING_HYP);
954 case REMOVE_FATHER_HYP: {
957 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
958 // is there the same local hyp or maybe a new father algo applied?
959 if ( !getSimilarAttached( _subShape, anHyp ) )
963 setAlgoState(MISSING_HYP);
966 case REMOVE_FATHER_ALGO: {
967 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
968 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
969 algo = dynamic_cast<SMESH_Algo*> (anHyp);
970 if (!algo->NeedDiscreteBoundary())
971 needFullClean = true;
974 if (algo == NULL) // no more applying algo on father
976 setAlgoState(NO_ALGO);
980 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
981 // check if algo changes
982 if ( string(algo->GetName()) != string( anHyp->GetName()) )
986 setAlgoState(MISSING_HYP);
996 // ----------------------------------------------------------------------
1003 // detect algorithm hiding
1005 if ( ret == SMESH_Hypothesis::HYP_OK &&
1006 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
1007 algo->GetName() == anHyp->GetName() )
1010 SMESH_Gen* gen = _father->GetGen();
1011 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1012 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
1013 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
1014 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1015 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1018 if ( ret == SMESH_Hypothesis::HYP_OK &&
1019 !algo->NeedDiscreteBoundary() &&
1020 !algo->SupportSubmeshes()) {
1021 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1022 gen->GetAlgo( *_father, _subShape, &algoAssignedTo );
1023 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1024 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1025 if ( gen->GetAlgo( *_father, i_sm->second->_subShape, &otherAssignedTo ) &&
1026 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1027 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1031 bool stateChange = ( _algoState != oldAlgoState );
1033 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1034 algo->SetEventListener( this );
1036 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1038 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1039 deleteOwnListeners();
1040 SetIsAlwaysComputed( false );
1041 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1042 // restore default states
1043 _algoState = HYP_OK;
1044 _computeState = READY_TO_COMPUTE;
1048 if ( needFullClean ) {
1049 // added or removed algo is all-dimensional
1050 ComputeStateEngine( CLEAN );
1052 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1055 if (stateChange || modifiedHyp)
1056 ComputeStateEngine(MODIF_ALGO_STATE);
1061 //=======================================================================
1062 //function : IsConform
1063 //purpose : check if a conform mesh will be produced by the Algo
1064 //=======================================================================
1066 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1068 // MESSAGE( "SMESH_subMesh::IsConform" );
1069 if ( !theAlgo ) return false;
1071 // Suppose that theAlgo is applicable to _subShape, do not check it here
1072 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1074 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1075 // on a sub-shape will be ignored by theAlgo
1076 if ( theAlgo->NeedDiscreteBoundary() ||
1077 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1080 SMESH_Gen* gen =_father->GetGen();
1082 // only local algo is to be checked
1083 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1084 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1087 // check algo attached to adjacent shapes
1089 // loop on one level down sub-meshes
1090 TopoDS_Iterator itsub( _subShape );
1091 for (; itsub.More(); itsub.Next())
1093 // loop on adjacent subShapes
1094 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1095 for (; it.More(); it.Next())
1097 const TopoDS_Shape& adjacent = it.Value();
1098 if ( _subShape.IsSame( adjacent )) continue;
1099 if ( adjacent.ShapeType() != _subShape.ShapeType())
1102 // check algo attached to smAdjacent
1103 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1105 !algo->NeedDiscreteBoundary() &&
1106 algo->OnlyUnaryInput())
1107 return false; // NOT CONFORM MESH WILL BE PRODUCED
1114 //=============================================================================
1118 //=============================================================================
1120 void SMESH_subMesh::setAlgoState(int state)
1125 //=============================================================================
1129 //=============================================================================
1130 SMESH_Hypothesis::Hypothesis_Status
1131 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1132 SMESH_Hypothesis * anHyp)
1134 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1135 //EAP: a wire (dim==1) should notify edges (dim==1)
1136 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1137 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1139 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1140 while ( smIt->more() ) {
1141 SMESH_Hypothesis::Hypothesis_Status ret2 =
1142 smIt->next()->AlgoStateEngine(event, anHyp);
1150 //=============================================================================
1154 //=============================================================================
1156 void SMESH_subMesh::cleanDependsOn()
1158 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1159 while ( smIt->more() )
1160 smIt->next()->ComputeStateEngine(CLEAN);
1163 //=============================================================================
1167 //=============================================================================
1169 void SMESH_subMesh::DumpAlgoState(bool isMain)
1171 int dim = SMESH_Gen::GetShapeDim(_subShape);
1172 // if (dim < 1) return;
1175 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1177 map < int, SMESH_subMesh * >::const_iterator itsub;
1178 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1180 SMESH_subMesh *sm = (*itsub).second;
1181 sm->DumpAlgoState(false);
1184 int type = _subShape.ShapeType();
1185 MESSAGE("dim = " << dim << " type of shape " << type);
1189 MESSAGE(" AlgoState = NO_ALGO");
1192 MESSAGE(" AlgoState = MISSING_HYP");
1195 MESSAGE(" AlgoState = HYP_OK");
1198 switch (_computeState)
1201 MESSAGE(" ComputeState = NOT_READY");
1203 case READY_TO_COMPUTE:
1204 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1207 MESSAGE(" ComputeState = COMPUTE_OK");
1209 case FAILED_TO_COMPUTE:
1210 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1215 //================================================================================
1217 * \brief Remove nodes and elements bound to submesh
1218 * \param subMesh - submesh containing nodes and elements
1220 //================================================================================
1222 static void cleanSubMesh( SMESH_subMesh * subMesh )
1225 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1226 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1227 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1228 while (ite->more()) {
1229 const SMDS_MeshElement * elt = ite->next();
1230 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1231 //meshDS->RemoveElement(elt);
1232 meshDS->RemoveFreeElement(elt, subMeshDS);
1235 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1236 while (itn->more()) {
1237 const SMDS_MeshNode * node = itn->next();
1238 //MESSAGE( " RM node: "<<node->GetID());
1239 if ( node->NbInverseElements() == 0 )
1240 meshDS->RemoveFreeNode(node, subMeshDS);
1241 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1242 meshDS->RemoveNode(node);
1248 //=============================================================================
1252 //=============================================================================
1254 bool SMESH_subMesh::ComputeStateEngine(int event)
1256 _computeError.reset();
1258 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1259 //SCRUTE(_computeState);
1262 if (_subShape.ShapeType() == TopAbs_VERTEX)
1264 _computeState = READY_TO_COMPUTE;
1265 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1266 if ( smDS && smDS->NbNodes() ) {
1267 if ( event == CLEAN ) {
1269 cleanSubMesh( this );
1272 _computeState = COMPUTE_OK;
1274 else if ( event == COMPUTE && !_alwaysComputed ) {
1275 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1276 gp_Pnt P = BRep_Tool::Pnt(V);
1277 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1278 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1279 _computeState = COMPUTE_OK;
1282 if ( event == MODIF_ALGO_STATE )
1286 SMESH_Gen *gen = _father->GetGen();
1287 SMESH_Algo *algo = 0;
1289 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1290 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1292 switch (_computeState)
1295 // ----------------------------------------------------------------------
1300 case MODIF_ALGO_STATE:
1302 if (algo && !algo->NeedDiscreteBoundary())
1303 cleanDependsOn(); // clean sub-meshes with event CLEAN
1304 if ( _algoState == HYP_OK )
1305 _computeState = READY_TO_COMPUTE;
1307 case COMPUTE: // nothing to do
1309 #ifdef WITH_SMESH_CANCEL_COMPUTE
1310 case COMPUTE_CANCELED: // nothing to do
1315 removeSubMeshElementsAndNodes();
1317 case SUBMESH_COMPUTED: // nothing to do
1319 case SUBMESH_RESTORED:
1320 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1322 case MESH_ENTITY_REMOVED:
1324 case SUBMESH_LOADED:
1325 loadDependentMeshes();
1326 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1328 case CHECK_COMPUTE_STATE:
1329 if ( IsMeshComputed() )
1330 _computeState = COMPUTE_OK;
1338 // ----------------------------------------------------------------------
1340 case READY_TO_COMPUTE:
1343 case MODIF_ALGO_STATE:
1344 _computeState = NOT_READY;
1348 if (!algo->NeedDiscreteBoundary())
1349 cleanDependsOn(); // clean sub-meshes with event CLEAN
1350 if ( _algoState == HYP_OK )
1351 _computeState = READY_TO_COMPUTE;
1358 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1361 MESSAGE("***** verify compute state *****");
1362 _computeState = NOT_READY;
1363 setAlgoState(MISSING_HYP);
1366 TopoDS_Shape shape = _subShape;
1367 // check submeshes needed
1368 if (_father->HasShapeToMesh() ) {
1369 bool subComputed = false;
1370 if (!algo->OnlyUnaryInput())
1371 shape = getCollection( gen, algo, subComputed );
1373 subComputed = subMeshesComputed();
1374 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1375 algo->SupportSubmeshes() ? true :
1376 ( !subComputed || _father->IsNotConformAllowed() ));
1378 _computeState = FAILED_TO_COMPUTE;
1379 if ( !algo->NeedDiscreteBoundary() )
1381 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1382 "Unexpected computed submesh",algo);
1387 // cleanDependants(); for "UseExisting_*D" algos
1388 // removeSubMeshElementsAndNodes();
1390 _computeState = FAILED_TO_COMPUTE;
1391 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1393 #if OCC_VERSION_LARGE > 0x06010000
1396 algo->InitComputeError();
1397 MemoryReserve aMemoryReserve;
1398 SMDS_Mesh::CheckMemory();
1399 Kernel_Utils::Localizer loc;
1400 if ( !_father->HasShapeToMesh() ) // no shape
1402 SMESH_MesherHelper helper( *_father );
1403 helper.SetSubShape( shape );
1404 helper.SetElementsOnShape( true );
1405 ret = algo->Compute(*_father, &helper );
1409 ret = algo->Compute((*_father), shape);
1411 if ( !_computeError || ( !ret && _computeError->IsOK() ) ) // algo can set _computeError of submesh
1412 _computeError = algo->GetComputeError();
1414 catch ( ::SMESH_ComputeError& comperr ) {
1415 cout << " SMESH_ComputeError caught" << endl;
1416 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1417 *_computeError = comperr;
1419 catch ( std::bad_alloc& exc ) {
1420 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1421 if ( _computeError ) {
1422 _computeError->myName = COMPERR_MEMORY_PB;
1423 //_computeError->myComment = exc.what();
1425 cleanSubMesh( this );
1428 catch ( Standard_OutOfMemory& exc ) {
1429 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1430 if ( _computeError ) {
1431 _computeError->myName = COMPERR_MEMORY_PB;
1432 //_computeError->myComment = exc.what();
1434 cleanSubMesh( this );
1435 throw std::bad_alloc();
1437 catch (Standard_Failure& ex) {
1438 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1439 _computeError->myName = COMPERR_OCC_EXCEPTION;
1440 _computeError->myComment += ex.DynamicType()->Name();
1441 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1442 _computeError->myComment += ": ";
1443 _computeError->myComment += ex.GetMessageString();
1446 catch ( SALOME_Exception& S_ex ) {
1447 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1448 _computeError->myName = COMPERR_SLM_EXCEPTION;
1449 _computeError->myComment = S_ex.what();
1451 catch ( std::exception& exc ) {
1452 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1453 _computeError->myName = COMPERR_STD_EXCEPTION;
1454 _computeError->myComment = exc.what();
1457 if ( _computeError )
1458 _computeError->myName = COMPERR_EXCEPTION;
1462 TopExp_Explorer subS(shape, _subShape.ShapeType());
1463 if (ret) // check if anything was built
1465 for (; ret && subS.More(); subS.Next())
1466 ret = _father->GetSubMesh( subS.Current() )->IsMeshComputed();
1468 bool isComputeErrorSet = !checkComputeError( algo, shape );
1469 if (!ret && !isComputeErrorSet)
1471 // Set _computeError
1472 for (subS.ReInit(); subS.More(); subS.Next())
1474 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1475 if ( !sm->IsMeshComputed() )
1477 if ( !sm->_computeError )
1478 sm->_computeError = SMESH_ComputeError::New();
1479 if ( sm->_computeError->IsOK() )
1480 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1481 sm->_computeState = FAILED_TO_COMPUTE;
1482 sm->_computeError->myAlgo = algo;
1486 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1488 _computeError.reset();
1490 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1493 #ifdef WITH_SMESH_CANCEL_COMPUTE
1494 case COMPUTE_CANCELED: // nothing to do
1499 removeSubMeshElementsAndNodes();
1500 _computeState = NOT_READY;
1504 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1506 _computeState = READY_TO_COMPUTE;
1508 setAlgoState(MISSING_HYP);
1511 case SUBMESH_COMPUTED: // nothing to do
1513 case SUBMESH_RESTORED:
1514 // check if a mesh is already computed that may
1515 // happen after retrieval from a file
1516 ComputeStateEngine( CHECK_COMPUTE_STATE );
1517 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1519 if (algo) algo->SubmeshRestored( this );
1521 case MESH_ENTITY_REMOVED:
1523 case SUBMESH_LOADED:
1524 loadDependentMeshes();
1525 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1527 case CHECK_COMPUTE_STATE:
1528 if ( IsMeshComputed() )
1529 _computeState = COMPUTE_OK;
1537 // ----------------------------------------------------------------------
1542 case MODIF_ALGO_STATE:
1543 ComputeStateEngine( CLEAN );
1545 if (algo && !algo->NeedDiscreteBoundary())
1546 cleanDependsOn(); // clean sub-meshes with event CLEAN
1548 case COMPUTE: // nothing to do
1550 #ifdef WITH_SMESH_CANCEL_COMPUTE
1551 case COMPUTE_CANCELED: // nothing to do
1555 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1556 removeSubMeshElementsAndNodes();
1557 _computeState = NOT_READY;
1558 if ( _algoState == HYP_OK )
1559 _computeState = READY_TO_COMPUTE;
1561 case SUBMESH_COMPUTED: // nothing to do
1563 case SUBMESH_RESTORED:
1564 ComputeStateEngine( CHECK_COMPUTE_STATE );
1565 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1567 if (algo) algo->SubmeshRestored( this );
1569 case MESH_ENTITY_REMOVED:
1570 updateDependantsState ( CHECK_COMPUTE_STATE );
1571 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1572 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1574 case CHECK_COMPUTE_STATE:
1575 if ( !IsMeshComputed() ) {
1576 if (_algoState == HYP_OK)
1577 _computeState = READY_TO_COMPUTE;
1579 _computeState = NOT_READY;
1582 case SUBMESH_LOADED:
1583 // already treated event, thanks to which _computeState == COMPUTE_OK
1591 // ----------------------------------------------------------------------
1593 case FAILED_TO_COMPUTE:
1596 case MODIF_ALGO_STATE:
1598 ComputeStateEngine( CLEAN );
1600 if (algo && !algo->NeedDiscreteBoundary())
1601 cleanDependsOn(); // clean sub-meshes with event CLEAN
1602 if (_algoState == HYP_OK)
1603 _computeState = READY_TO_COMPUTE;
1605 _computeState = NOT_READY;
1607 case COMPUTE: // nothing to do
1609 #ifdef WITH_SMESH_CANCEL_COMPUTE
1610 case COMPUTE_CANCELED:
1613 algo->CancelCompute();
1618 cleanDependants(); // submeshes dependent on me should be cleaned as well
1619 removeSubMeshElementsAndNodes();
1621 case SUBMESH_COMPUTED: // allow retry compute
1622 if (_algoState == HYP_OK)
1623 _computeState = READY_TO_COMPUTE;
1625 _computeState = NOT_READY;
1627 case SUBMESH_RESTORED:
1628 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1630 case MESH_ENTITY_REMOVED:
1632 case CHECK_COMPUTE_STATE:
1633 if ( IsMeshComputed() )
1634 _computeState = COMPUTE_OK;
1636 if (_algoState == HYP_OK)
1637 _computeState = READY_TO_COMPUTE;
1639 _computeState = NOT_READY;
1647 // ----------------------------------------------------------------------
1653 notifyListenersOnEvent( event, COMPUTE_EVENT );
1659 //=============================================================================
1663 //=============================================================================
1665 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1667 _computeError.reset();
1671 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1672 vector<int> aVec(SMDSEntity_Last,0);
1673 aVec[SMDSEntity_Node] = 1;
1674 aResMap.insert(make_pair(this,aVec));
1678 //SMESH_Gen *gen = _father->GetGen();
1679 SMESH_Algo *algo = 0;
1680 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1683 if(algo && !aResMap.count(this) )
1685 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1686 if (!ret) return false;
1688 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary())
1690 // check submeshes needed
1691 bool subMeshEvaluated = true;
1692 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1693 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1694 while ( smIt->more() && subMeshEvaluated )
1696 SMESH_subMesh* sm = smIt->next();
1697 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1698 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1699 const vector<int> & nbs = aResMap[ sm ];
1700 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1702 if ( !subMeshEvaluated )
1705 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1706 ret = algo->Evaluate((*_father), _subShape, aResMap);
1708 aResMap.insert( make_pair( this,vector<int>(0)));
1715 //=======================================================================
1717 * \brief Update compute_state by _computeError and send proper events to
1718 * dependent submeshes
1719 * \retval bool - true if _computeError is NOT set
1721 //=======================================================================
1723 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1725 bool noErrors = true;
1727 if ( !theShape.IsNull() )
1729 // Check state of submeshes
1730 if ( !theAlgo->NeedDiscreteBoundary())
1732 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1733 while ( smIt->more() )
1734 if ( !smIt->next()->checkComputeError( theAlgo ))
1738 // Check state of neighbours
1739 if ( !theAlgo->OnlyUnaryInput() &&
1740 theShape.ShapeType() == TopAbs_COMPOUND &&
1741 !theShape.IsSame( _subShape ))
1743 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1744 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1746 if ( !sm->checkComputeError( theAlgo, sm->GetSubShape() ))
1748 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1755 if ( !_computeError || _computeError->IsOK() )
1757 // no error description is set to this sub-mesh, check if any mesh is computed
1758 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1762 if ( !_computeError->myAlgo )
1763 _computeError->myAlgo = theAlgo;
1767 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1768 if (_computeError->IsCommon() )
1769 text << _computeError->CommonName();
1771 text << _computeError->myName;
1772 if ( _computeError->myComment.size() > 0 )
1773 text << " \"" << _computeError->myComment << "\"";
1777 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1785 //=======================================================================
1786 //function : updateSubMeshState
1788 //=======================================================================
1790 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1792 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1793 while ( smIt->more() )
1794 smIt->next()->_computeState = theState;
1797 //=======================================================================
1798 //function : ComputeSubMeshStateEngine
1800 //=======================================================================
1802 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1804 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1805 while ( smIt->more() )
1806 smIt->next()->ComputeStateEngine(event);
1809 //=======================================================================
1810 //function : updateDependantsState
1812 //=======================================================================
1814 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
1816 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1817 for (; it.More(); it.Next())
1819 const TopoDS_Shape& ancestor = it.Value();
1820 SMESH_subMesh *aSubMesh =
1821 _father->GetSubMeshContaining(ancestor);
1823 aSubMesh->ComputeStateEngine( theEvent );
1827 //=============================================================================
1831 //=============================================================================
1833 void SMESH_subMesh::cleanDependants()
1835 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1837 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1838 for (; it.More(); it.Next())
1840 const TopoDS_Shape& ancestor = it.Value();
1841 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1842 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1843 // will erase mesh on other shapes in a compound
1844 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1845 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1847 !aSubMesh->IsEmpty() ) // prevent infinite CLEAN via event lesteners
1848 aSubMesh->ComputeStateEngine(CLEAN);
1854 //=============================================================================
1858 //=============================================================================
1860 void SMESH_subMesh::removeSubMeshElementsAndNodes()
1862 cleanSubMesh( this );
1864 // algo may bind a submesh not to _subShape, eg 3D algo
1865 // sets nodes on SHELL while _subShape may be SOLID
1867 int dim = SMESH_Gen::GetShapeDim( _subShape );
1868 int type = _subShape.ShapeType() + 1;
1869 for ( ; type <= TopAbs_EDGE; type++) {
1870 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1872 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1873 for ( ; exp.More(); exp.Next() )
1874 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1881 //=======================================================================
1882 //function : getCollection
1883 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1884 // meshed at once along with _subShape
1885 //=======================================================================
1887 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
1888 SMESH_Algo* theAlgo,
1889 bool & theSubComputed)
1891 theSubComputed = subMeshesComputed();
1893 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1895 if ( mainShape.IsSame( _subShape ))
1898 const bool ignoreAuxiliaryHyps = false;
1899 list<const SMESHDS_Hypothesis*> aUsedHyp =
1900 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1902 // put in a compound all shapes with the same hypothesis assigned
1903 // and a good ComputState
1905 TopoDS_Compound aCompound;
1906 BRep_Builder aBuilder;
1907 aBuilder.MakeCompound( aCompound );
1909 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1910 for ( ; anExplorer.More(); anExplorer.Next() )
1912 const TopoDS_Shape& S = anExplorer.Current();
1913 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1914 if ( subMesh == this )
1916 aBuilder.Add( aCompound, S );
1918 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1920 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1921 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1922 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1923 aBuilder.Add( aCompound, S );
1924 if ( !subMesh->subMeshesComputed() )
1925 theSubComputed = false;
1932 //=======================================================================
1933 //function : getSimilarAttached
1934 //purpose : return a hypothesis attached to theShape.
1935 // If theHyp is provided, similar but not same hypotheses
1936 // is returned; else only applicable ones having theHypType
1938 //=======================================================================
1940 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
1941 const SMESH_Hypothesis * theHyp,
1942 const int theHypType)
1944 SMESH_HypoFilter hypoKind;
1945 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1947 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1948 hypoKind.AndNot( hypoKind.Is( theHyp ));
1949 if ( theHyp->IsAuxiliary() )
1950 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1952 hypoKind.AndNot( hypoKind.IsAuxiliary());
1955 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1958 return _father->GetHypothesis( theShape, hypoKind, false );
1961 //=======================================================================
1962 //function : CheckConcurentHypothesis
1963 //purpose : check if there are several applicable hypothesis attached to
1965 //=======================================================================
1967 SMESH_Hypothesis::Hypothesis_Status
1968 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1970 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1972 // is there local hypothesis on me?
1973 if ( getSimilarAttached( _subShape, 0, theHypType ) )
1974 return SMESH_Hypothesis::HYP_OK;
1977 TopoDS_Shape aPrevWithHyp;
1978 const SMESH_Hypothesis* aPrevHyp = 0;
1979 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1980 for (; it.More(); it.Next())
1982 const TopoDS_Shape& ancestor = it.Value();
1983 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
1986 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1988 aPrevWithHyp = ancestor;
1991 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1992 return SMESH_Hypothesis::HYP_CONCURENT;
1994 return SMESH_Hypothesis::HYP_OK;
1997 return SMESH_Hypothesis::HYP_OK;
2000 //================================================================================
2002 * \brief Constructor of OwnListenerData
2004 //================================================================================
2006 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2008 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2009 mySubMeshID( sm ? sm->GetId() : -1 ),
2014 //================================================================================
2016 * \brief Sets an event listener and its data to a submesh
2017 * \param listener - the listener to store
2018 * \param data - the listener data to store
2019 * \param where - the submesh to store the listener and it's data
2021 * It remembers the submesh where it puts the listener in order to delete
2022 * them when HYP_OK algo_state is lost
2023 * After being set, event listener is notified on each event of where submesh.
2025 //================================================================================
2027 void SMESH_subMesh::SetEventListener(EventListener* listener,
2028 EventListenerData* data,
2029 SMESH_subMesh* where)
2031 if ( listener && where ) {
2032 where->setEventListener( listener, data );
2033 _ownListeners.push_back( OwnListenerData( where, listener ));
2037 //================================================================================
2039 * \brief Sets an event listener and its data to a submesh
2040 * \param listener - the listener to store
2041 * \param data - the listener data to store
2043 * After being set, event listener is notified on each event of a submesh.
2045 //================================================================================
2047 void SMESH_subMesh::setEventListener(EventListener* listener, EventListenerData* data)
2049 map< EventListener*, EventListenerData* >::iterator l_d =
2050 _eventListeners.find( listener );
2051 if ( l_d != _eventListeners.end() ) {
2052 EventListenerData* curData = l_d->second;
2053 if ( curData && curData != data && curData->IsDeletable() )
2058 _eventListeners.insert( make_pair( listener, data ));
2061 //================================================================================
2063 * \brief Return an event listener data
2064 * \param listener - the listener whose data is
2065 * \retval EventListenerData* - found data, maybe NULL
2067 //================================================================================
2069 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
2071 map< EventListener*, EventListenerData* >::const_iterator l_d =
2072 _eventListeners.find( listener );
2073 if ( l_d != _eventListeners.end() )
2078 //================================================================================
2080 * \brief Notify stored event listeners on the occured event
2081 * \param event - algo_event or compute_event itself
2082 * \param eventType - algo_event or compute_event
2083 * \param hyp - hypothesis, if eventType is algo_event
2085 //================================================================================
2087 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2088 const event_type eventType,
2089 SMESH_Hypothesis* hyp)
2091 map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2092 for ( ; l_d != _eventListeners.end(); ++l_d )
2094 std::pair< EventListener*, EventListenerData* > li_da = *l_d; /* copy to enable removal
2097 its ProcessEvent() */
2098 if ( li_da.first->myBusySM.insert( this ).second )
2100 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2101 li_da.first->myBusySM.erase( this );
2106 //================================================================================
2108 * \brief Unregister the listener and delete listener's data
2109 * \param listener - the event listener
2111 //================================================================================
2113 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2115 map< EventListener*, EventListenerData* >::iterator l_d =
2116 _eventListeners.find( listener );
2117 if ( l_d != _eventListeners.end() ) {
2118 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2119 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2120 _eventListeners.erase( l_d );
2124 //================================================================================
2126 * \brief Delete event listeners depending on algo of this submesh
2128 //================================================================================
2130 void SMESH_subMesh::deleteOwnListeners()
2132 list< OwnListenerData >::iterator d;
2133 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2135 if ( !_father->MeshExists( d->myMeshID ))
2137 if ( _father->GetId() == d->myMeshID && !_father->GetSubMeshContaining( d->mySubMeshID ))
2139 d->mySubMesh->DeleteEventListener( d->myListener );
2141 _ownListeners.clear();
2144 //=======================================================================
2145 //function : loadDependentMeshes
2146 //purpose : loads dependent meshes on SUBMESH_LOADED event
2147 //=======================================================================
2149 void SMESH_subMesh::loadDependentMeshes()
2151 list< OwnListenerData >::iterator d;
2152 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2153 if ( _father != d->mySubMesh->_father )
2154 d->mySubMesh->_father->Load();
2156 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2157 // for ( ; l_d != _eventListeners.end(); ++l_d )
2158 // if ( l_d->second )
2160 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2161 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2162 // for ( ; sm != smList.end(); ++sm )
2163 // if ( _father != (*sm)->_father )
2164 // (*sm)->_father->Load();
2168 //================================================================================
2170 * \brief Do something on a certain event
2171 * \param event - algo_event or compute_event itself
2172 * \param eventType - algo_event or compute_event
2173 * \param subMesh - the submesh where the event occures
2174 * \param data - listener data stored in the subMesh
2175 * \param hyp - hypothesis, if eventType is algo_event
2177 * The base implementation translates CLEAN event to the subMesh
2178 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2179 * successful COMPUTE event.
2181 //================================================================================
2183 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2184 const int eventType,
2185 SMESH_subMesh* subMesh,
2186 EventListenerData* data,
2187 const SMESH_Hypothesis* /*hyp*/)
2189 if ( data && !data->mySubMeshes.empty() &&
2190 eventType == SMESH_subMesh::COMPUTE_EVENT)
2192 ASSERT( data->mySubMeshes.front() != subMesh );
2193 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2194 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2196 case SMESH_subMesh::CLEAN:
2197 for ( ; smIt != smEnd; ++ smIt)
2198 (*smIt)->ComputeStateEngine( event );
2200 case SMESH_subMesh::COMPUTE:
2201 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2202 for ( ; smIt != smEnd; ++ smIt)
2203 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2212 //================================================================================
2214 * \brief Iterator over submeshes and optionally prepended or appended one
2216 //================================================================================
2218 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2220 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2221 SMESH_subMesh* prepend,
2222 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2224 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2225 if ( myCur == append ) append = 0;
2227 /// Return true if and only if there are other object in this iterator
2232 /// Return the current object and step to the next one
2233 virtual SMESH_subMesh* next()
2235 SMESH_subMesh* res = myCur;
2236 if ( myIt->more() ) { myCur = myIt->next(); }
2237 else { myCur = myAppend; myAppend = 0; }
2244 SMESH_subMesh *myAppend, *myCur;
2245 SMDS_Iterator<SMESH_subMesh*> *myIt;
2249 //================================================================================
2251 * \brief Return iterator on the submeshes this one depends on
2252 * \param includeSelf - this submesh to be returned also
2253 * \param reverse - if true, complex shape submeshes go first
2255 //================================================================================
2257 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2260 SMESH_subMesh *prepend=0, *append=0;
2261 if ( includeSelf ) {
2262 if ( reverse ) prepend = this;
2265 typedef map < int, SMESH_subMesh * > TMap;
2268 return SMESH_subMeshIteratorPtr
2269 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2272 return SMESH_subMeshIteratorPtr
2273 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));
2277 //================================================================================
2279 * \brief Find common submeshes (based on shared sub-shapes with other
2280 * \param theOther submesh to check
2281 * \param theSetOfCommon set of common submesh
2283 //================================================================================
2285 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2286 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2288 int oldNb = theSetOfCommon.size();
2289 // check main submeshes
2290 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2291 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2292 theSetOfCommon.insert( this );
2293 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2294 theSetOfCommon.insert( theOther );
2295 // check common submeshes
2296 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2297 for( ; mapIt != _mapDepend.end(); mapIt++ )
2298 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2299 theSetOfCommon.insert( (*mapIt).second );
2300 return oldNb < theSetOfCommon.size();