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);
1388 //cleanDependants(); for "UseExisting_*D" algos
1389 //removeSubMeshElementsAndNodes();
1390 loadDependentMeshes();
1392 _computeState = FAILED_TO_COMPUTE;
1393 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1395 #if OCC_VERSION_LARGE > 0x06010000
1398 algo->InitComputeError();
1399 MemoryReserve aMemoryReserve;
1400 SMDS_Mesh::CheckMemory();
1401 Kernel_Utils::Localizer loc;
1402 if ( !_father->HasShapeToMesh() ) // no shape
1404 SMESH_MesherHelper helper( *_father );
1405 helper.SetSubShape( shape );
1406 helper.SetElementsOnShape( true );
1407 ret = algo->Compute(*_father, &helper );
1411 ret = algo->Compute((*_father), shape);
1413 if ( !_computeError || ( !ret && _computeError->IsOK() ) ) // algo can set _computeError of submesh
1414 _computeError = algo->GetComputeError();
1416 catch ( ::SMESH_ComputeError& comperr ) {
1417 cout << " SMESH_ComputeError caught" << endl;
1418 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1419 *_computeError = comperr;
1421 catch ( std::bad_alloc& exc ) {
1422 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1423 if ( _computeError ) {
1424 _computeError->myName = COMPERR_MEMORY_PB;
1425 //_computeError->myComment = exc.what();
1427 cleanSubMesh( this );
1430 catch ( Standard_OutOfMemory& exc ) {
1431 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1432 if ( _computeError ) {
1433 _computeError->myName = COMPERR_MEMORY_PB;
1434 //_computeError->myComment = exc.what();
1436 cleanSubMesh( this );
1437 throw std::bad_alloc();
1439 catch (Standard_Failure& ex) {
1440 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1441 _computeError->myName = COMPERR_OCC_EXCEPTION;
1442 _computeError->myComment += ex.DynamicType()->Name();
1443 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1444 _computeError->myComment += ": ";
1445 _computeError->myComment += ex.GetMessageString();
1448 catch ( SALOME_Exception& S_ex ) {
1449 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1450 _computeError->myName = COMPERR_SLM_EXCEPTION;
1451 _computeError->myComment = S_ex.what();
1453 catch ( std::exception& exc ) {
1454 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1455 _computeError->myName = COMPERR_STD_EXCEPTION;
1456 _computeError->myComment = exc.what();
1459 if ( _computeError )
1460 _computeError->myName = COMPERR_EXCEPTION;
1464 TopExp_Explorer subS(shape, _subShape.ShapeType());
1465 if (ret) // check if anything was built
1467 for (; ret && subS.More(); subS.Next())
1468 ret = _father->GetSubMesh( subS.Current() )->IsMeshComputed();
1470 bool isComputeErrorSet = !checkComputeError( algo, shape );
1471 if (!ret && !isComputeErrorSet)
1473 // Set _computeError
1474 for (subS.ReInit(); subS.More(); subS.Next())
1476 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1477 if ( !sm->IsMeshComputed() )
1479 if ( !sm->_computeError )
1480 sm->_computeError = SMESH_ComputeError::New();
1481 if ( sm->_computeError->IsOK() )
1482 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1483 sm->_computeState = FAILED_TO_COMPUTE;
1484 sm->_computeError->myAlgo = algo;
1488 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1490 _computeError.reset();
1492 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1495 #ifdef WITH_SMESH_CANCEL_COMPUTE
1496 case COMPUTE_CANCELED: // nothing to do
1501 removeSubMeshElementsAndNodes();
1502 _computeState = NOT_READY;
1506 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1508 _computeState = READY_TO_COMPUTE;
1510 setAlgoState(MISSING_HYP);
1513 case SUBMESH_COMPUTED: // nothing to do
1515 case SUBMESH_RESTORED:
1516 // check if a mesh is already computed that may
1517 // happen after retrieval from a file
1518 ComputeStateEngine( CHECK_COMPUTE_STATE );
1519 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1521 if (algo) algo->SubmeshRestored( this );
1523 case MESH_ENTITY_REMOVED:
1525 case SUBMESH_LOADED:
1526 loadDependentMeshes();
1527 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1529 case CHECK_COMPUTE_STATE:
1530 if ( IsMeshComputed() )
1531 _computeState = COMPUTE_OK;
1539 // ----------------------------------------------------------------------
1544 case MODIF_ALGO_STATE:
1545 ComputeStateEngine( CLEAN );
1547 if (algo && !algo->NeedDiscreteBoundary())
1548 cleanDependsOn(); // clean sub-meshes with event CLEAN
1550 case COMPUTE: // nothing to do
1552 #ifdef WITH_SMESH_CANCEL_COMPUTE
1553 case COMPUTE_CANCELED: // nothing to do
1557 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1558 removeSubMeshElementsAndNodes();
1559 _computeState = NOT_READY;
1560 if ( _algoState == HYP_OK )
1561 _computeState = READY_TO_COMPUTE;
1563 case SUBMESH_COMPUTED: // nothing to do
1565 case SUBMESH_RESTORED:
1566 ComputeStateEngine( CHECK_COMPUTE_STATE );
1567 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1569 if (algo) algo->SubmeshRestored( this );
1571 case MESH_ENTITY_REMOVED:
1572 updateDependantsState ( CHECK_COMPUTE_STATE );
1573 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1574 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1576 case CHECK_COMPUTE_STATE:
1577 if ( !IsMeshComputed() ) {
1578 if (_algoState == HYP_OK)
1579 _computeState = READY_TO_COMPUTE;
1581 _computeState = NOT_READY;
1584 case SUBMESH_LOADED:
1585 // already treated event, thanks to which _computeState == COMPUTE_OK
1593 // ----------------------------------------------------------------------
1595 case FAILED_TO_COMPUTE:
1598 case MODIF_ALGO_STATE:
1600 ComputeStateEngine( CLEAN );
1602 if (algo && !algo->NeedDiscreteBoundary())
1603 cleanDependsOn(); // clean sub-meshes with event CLEAN
1604 if (_algoState == HYP_OK)
1605 _computeState = READY_TO_COMPUTE;
1607 _computeState = NOT_READY;
1609 case COMPUTE: // nothing to do
1611 #ifdef WITH_SMESH_CANCEL_COMPUTE
1612 case COMPUTE_CANCELED:
1615 algo->CancelCompute();
1620 cleanDependants(); // submeshes dependent on me should be cleaned as well
1621 removeSubMeshElementsAndNodes();
1623 case SUBMESH_COMPUTED: // allow retry compute
1624 if (_algoState == HYP_OK)
1625 _computeState = READY_TO_COMPUTE;
1627 _computeState = NOT_READY;
1629 case SUBMESH_RESTORED:
1630 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1632 case MESH_ENTITY_REMOVED:
1634 case CHECK_COMPUTE_STATE:
1635 if ( IsMeshComputed() )
1636 _computeState = COMPUTE_OK;
1638 if (_algoState == HYP_OK)
1639 _computeState = READY_TO_COMPUTE;
1641 _computeState = NOT_READY;
1643 // case SUBMESH_LOADED:
1651 // ----------------------------------------------------------------------
1657 notifyListenersOnEvent( event, COMPUTE_EVENT );
1663 //=============================================================================
1667 //=============================================================================
1669 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1671 _computeError.reset();
1675 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1676 vector<int> aVec(SMDSEntity_Last,0);
1677 aVec[SMDSEntity_Node] = 1;
1678 aResMap.insert(make_pair(this,aVec));
1682 //SMESH_Gen *gen = _father->GetGen();
1683 SMESH_Algo *algo = 0;
1684 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1687 if(algo && !aResMap.count(this) )
1689 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1690 if (!ret) return false;
1692 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary())
1694 // check submeshes needed
1695 bool subMeshEvaluated = true;
1696 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1697 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1698 while ( smIt->more() && subMeshEvaluated )
1700 SMESH_subMesh* sm = smIt->next();
1701 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1702 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1703 const vector<int> & nbs = aResMap[ sm ];
1704 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1706 if ( !subMeshEvaluated )
1709 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1710 ret = algo->Evaluate((*_father), _subShape, aResMap);
1712 aResMap.insert( make_pair( this,vector<int>(0)));
1719 //=======================================================================
1721 * \brief Update compute_state by _computeError and send proper events to
1722 * dependent submeshes
1723 * \retval bool - true if _computeError is NOT set
1725 //=======================================================================
1727 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1729 bool noErrors = true;
1731 if ( !theShape.IsNull() )
1733 // Check state of submeshes
1734 if ( !theAlgo->NeedDiscreteBoundary())
1736 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1737 while ( smIt->more() )
1738 if ( !smIt->next()->checkComputeError( theAlgo ))
1742 // Check state of neighbours
1743 if ( !theAlgo->OnlyUnaryInput() &&
1744 theShape.ShapeType() == TopAbs_COMPOUND &&
1745 !theShape.IsSame( _subShape ))
1747 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1748 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1750 if ( !sm->checkComputeError( theAlgo, sm->GetSubShape() ))
1752 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1759 if ( !_computeError || _computeError->IsOK() )
1761 // no error description is set to this sub-mesh, check if any mesh is computed
1762 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1766 if ( !_computeError->myAlgo )
1767 _computeError->myAlgo = theAlgo;
1771 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1772 if (_computeError->IsCommon() )
1773 text << _computeError->CommonName();
1775 text << _computeError->myName;
1776 if ( _computeError->myComment.size() > 0 )
1777 text << " \"" << _computeError->myComment << "\"";
1781 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1789 //=======================================================================
1790 //function : updateSubMeshState
1792 //=======================================================================
1794 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1796 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1797 while ( smIt->more() )
1798 smIt->next()->_computeState = theState;
1801 //=======================================================================
1802 //function : ComputeSubMeshStateEngine
1804 //=======================================================================
1806 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1808 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1809 while ( smIt->more() )
1810 smIt->next()->ComputeStateEngine(event);
1813 //=======================================================================
1814 //function : updateDependantsState
1816 //=======================================================================
1818 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
1820 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1821 for (; it.More(); it.Next())
1823 const TopoDS_Shape& ancestor = it.Value();
1824 SMESH_subMesh *aSubMesh =
1825 _father->GetSubMeshContaining(ancestor);
1827 aSubMesh->ComputeStateEngine( theEvent );
1831 //=============================================================================
1835 //=============================================================================
1837 void SMESH_subMesh::cleanDependants()
1839 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1841 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1842 for (; it.More(); it.Next())
1844 const TopoDS_Shape& ancestor = it.Value();
1845 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1846 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1847 // will erase mesh on other shapes in a compound
1848 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1849 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1851 !aSubMesh->IsEmpty() ) // prevent infinite CLEAN via event lesteners
1852 aSubMesh->ComputeStateEngine(CLEAN);
1858 //=============================================================================
1862 //=============================================================================
1864 void SMESH_subMesh::removeSubMeshElementsAndNodes()
1866 cleanSubMesh( this );
1868 // algo may bind a submesh not to _subShape, eg 3D algo
1869 // sets nodes on SHELL while _subShape may be SOLID
1871 int dim = SMESH_Gen::GetShapeDim( _subShape );
1872 int type = _subShape.ShapeType() + 1;
1873 for ( ; type <= TopAbs_EDGE; type++) {
1874 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1876 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1877 for ( ; exp.More(); exp.Next() )
1878 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1885 //=======================================================================
1886 //function : getCollection
1887 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1888 // meshed at once along with _subShape
1889 //=======================================================================
1891 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
1892 SMESH_Algo* theAlgo,
1893 bool & theSubComputed)
1895 theSubComputed = subMeshesComputed();
1897 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1899 if ( mainShape.IsSame( _subShape ))
1902 const bool ignoreAuxiliaryHyps = false;
1903 list<const SMESHDS_Hypothesis*> aUsedHyp =
1904 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1906 // put in a compound all shapes with the same hypothesis assigned
1907 // and a good ComputState
1909 TopoDS_Compound aCompound;
1910 BRep_Builder aBuilder;
1911 aBuilder.MakeCompound( aCompound );
1913 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1914 for ( ; anExplorer.More(); anExplorer.Next() )
1916 const TopoDS_Shape& S = anExplorer.Current();
1917 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1918 if ( subMesh == this )
1920 aBuilder.Add( aCompound, S );
1922 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1924 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1925 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1926 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1927 aBuilder.Add( aCompound, S );
1928 if ( !subMesh->subMeshesComputed() )
1929 theSubComputed = false;
1936 //=======================================================================
1937 //function : getSimilarAttached
1938 //purpose : return a hypothesis attached to theShape.
1939 // If theHyp is provided, similar but not same hypotheses
1940 // is returned; else only applicable ones having theHypType
1942 //=======================================================================
1944 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
1945 const SMESH_Hypothesis * theHyp,
1946 const int theHypType)
1948 SMESH_HypoFilter hypoKind;
1949 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1951 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1952 hypoKind.AndNot( hypoKind.Is( theHyp ));
1953 if ( theHyp->IsAuxiliary() )
1954 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1956 hypoKind.AndNot( hypoKind.IsAuxiliary());
1959 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1962 return _father->GetHypothesis( theShape, hypoKind, false );
1965 //=======================================================================
1966 //function : CheckConcurentHypothesis
1967 //purpose : check if there are several applicable hypothesis attached to
1969 //=======================================================================
1971 SMESH_Hypothesis::Hypothesis_Status
1972 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1974 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1976 // is there local hypothesis on me?
1977 if ( getSimilarAttached( _subShape, 0, theHypType ) )
1978 return SMESH_Hypothesis::HYP_OK;
1981 TopoDS_Shape aPrevWithHyp;
1982 const SMESH_Hypothesis* aPrevHyp = 0;
1983 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1984 for (; it.More(); it.Next())
1986 const TopoDS_Shape& ancestor = it.Value();
1987 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
1990 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1992 aPrevWithHyp = ancestor;
1995 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1996 return SMESH_Hypothesis::HYP_CONCURENT;
1998 return SMESH_Hypothesis::HYP_OK;
2001 return SMESH_Hypothesis::HYP_OK;
2004 //================================================================================
2006 * \brief Constructor of OwnListenerData
2008 //================================================================================
2010 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2012 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2013 mySubMeshID( sm ? sm->GetId() : -1 ),
2018 //================================================================================
2020 * \brief Sets an event listener and its data to a submesh
2021 * \param listener - the listener to store
2022 * \param data - the listener data to store
2023 * \param where - the submesh to store the listener and it's data
2025 * It remembers the submesh where it puts the listener in order to delete
2026 * them when HYP_OK algo_state is lost
2027 * After being set, event listener is notified on each event of where submesh.
2029 //================================================================================
2031 void SMESH_subMesh::SetEventListener(EventListener* listener,
2032 EventListenerData* data,
2033 SMESH_subMesh* where)
2035 if ( listener && where ) {
2036 where->setEventListener( listener, data );
2037 _ownListeners.push_back( OwnListenerData( where, listener ));
2041 //================================================================================
2043 * \brief Sets an event listener and its data to a submesh
2044 * \param listener - the listener to store
2045 * \param data - the listener data to store
2047 * After being set, event listener is notified on each event of a submesh.
2049 //================================================================================
2051 void SMESH_subMesh::setEventListener(EventListener* listener, EventListenerData* data)
2053 map< EventListener*, EventListenerData* >::iterator l_d =
2054 _eventListeners.find( listener );
2055 if ( l_d != _eventListeners.end() ) {
2056 EventListenerData* curData = l_d->second;
2057 if ( curData && curData != data && curData->IsDeletable() )
2062 _eventListeners.insert( make_pair( listener, data ));
2065 //================================================================================
2067 * \brief Return an event listener data
2068 * \param listener - the listener whose data is
2069 * \retval EventListenerData* - found data, maybe NULL
2071 //================================================================================
2073 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
2075 map< EventListener*, EventListenerData* >::const_iterator l_d =
2076 _eventListeners.find( listener );
2077 if ( l_d != _eventListeners.end() )
2082 //================================================================================
2084 * \brief Notify stored event listeners on the occured event
2085 * \param event - algo_event or compute_event itself
2086 * \param eventType - algo_event or compute_event
2087 * \param hyp - hypothesis, if eventType is algo_event
2089 //================================================================================
2091 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2092 const event_type eventType,
2093 SMESH_Hypothesis* hyp)
2095 map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2096 for ( ; l_d != _eventListeners.end(); ++l_d )
2098 std::pair< EventListener*, EventListenerData* > li_da = *l_d; /* copy to enable removal
2101 its ProcessEvent() */
2102 if ( li_da.first->myBusySM.insert( this ).second )
2104 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2105 li_da.first->myBusySM.erase( this );
2110 //================================================================================
2112 * \brief Unregister the listener and delete listener's data
2113 * \param listener - the event listener
2115 //================================================================================
2117 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2119 map< EventListener*, EventListenerData* >::iterator l_d =
2120 _eventListeners.find( listener );
2121 if ( l_d != _eventListeners.end() ) {
2122 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2123 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2124 _eventListeners.erase( l_d );
2128 //================================================================================
2130 * \brief Delete event listeners depending on algo of this submesh
2132 //================================================================================
2134 void SMESH_subMesh::deleteOwnListeners()
2136 list< OwnListenerData >::iterator d;
2137 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2139 if ( !_father->MeshExists( d->myMeshID ))
2141 if ( _father->GetId() == d->myMeshID && !_father->GetSubMeshContaining( d->mySubMeshID ))
2143 d->mySubMesh->DeleteEventListener( d->myListener );
2145 _ownListeners.clear();
2148 //=======================================================================
2149 //function : loadDependentMeshes
2150 //purpose : loads dependent meshes on SUBMESH_LOADED event
2151 //=======================================================================
2153 void SMESH_subMesh::loadDependentMeshes()
2155 list< OwnListenerData >::iterator d;
2156 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2157 if ( _father != d->mySubMesh->_father )
2158 d->mySubMesh->_father->Load();
2160 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2161 // for ( ; l_d != _eventListeners.end(); ++l_d )
2162 // if ( l_d->second )
2164 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2165 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2166 // for ( ; sm != smList.end(); ++sm )
2167 // if ( _father != (*sm)->_father )
2168 // (*sm)->_father->Load();
2172 //================================================================================
2174 * \brief Do something on a certain event
2175 * \param event - algo_event or compute_event itself
2176 * \param eventType - algo_event or compute_event
2177 * \param subMesh - the submesh where the event occures
2178 * \param data - listener data stored in the subMesh
2179 * \param hyp - hypothesis, if eventType is algo_event
2181 * The base implementation translates CLEAN event to the subMesh
2182 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2183 * successful COMPUTE event.
2185 //================================================================================
2187 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2188 const int eventType,
2189 SMESH_subMesh* subMesh,
2190 EventListenerData* data,
2191 const SMESH_Hypothesis* /*hyp*/)
2193 if ( data && !data->mySubMeshes.empty() &&
2194 eventType == SMESH_subMesh::COMPUTE_EVENT)
2196 ASSERT( data->mySubMeshes.front() != subMesh );
2197 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2198 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2200 case SMESH_subMesh::CLEAN:
2201 for ( ; smIt != smEnd; ++ smIt)
2202 (*smIt)->ComputeStateEngine( event );
2204 case SMESH_subMesh::COMPUTE:
2205 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2206 for ( ; smIt != smEnd; ++ smIt)
2207 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2216 //================================================================================
2218 * \brief Iterator over submeshes and optionally prepended or appended one
2220 //================================================================================
2222 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2224 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2225 SMESH_subMesh* prepend,
2226 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2228 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2229 if ( myCur == append ) append = 0;
2231 /// Return true if and only if there are other object in this iterator
2236 /// Return the current object and step to the next one
2237 virtual SMESH_subMesh* next()
2239 SMESH_subMesh* res = myCur;
2240 if ( myIt->more() ) { myCur = myIt->next(); }
2241 else { myCur = myAppend; myAppend = 0; }
2248 SMESH_subMesh *myAppend, *myCur;
2249 SMDS_Iterator<SMESH_subMesh*> *myIt;
2253 //================================================================================
2255 * \brief Return iterator on the submeshes this one depends on
2256 * \param includeSelf - this submesh to be returned also
2257 * \param reverse - if true, complex shape submeshes go first
2259 //================================================================================
2261 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2264 SMESH_subMesh *prepend=0, *append=0;
2265 if ( includeSelf ) {
2266 if ( reverse ) prepend = this;
2269 typedef map < int, SMESH_subMesh * > TMap;
2272 return SMESH_subMeshIteratorPtr
2273 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2276 return SMESH_subMeshIteratorPtr
2277 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));
2281 //================================================================================
2283 * \brief Find common submeshes (based on shared sub-shapes with other
2284 * \param theOther submesh to check
2285 * \param theSetOfCommon set of common submesh
2287 //================================================================================
2289 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2290 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2292 int oldNb = theSetOfCommon.size();
2293 // check main submeshes
2294 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2295 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2296 theSetOfCommon.insert( this );
2297 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2298 theSetOfCommon.insert( theOther );
2299 // check common submeshes
2300 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2301 for( ; mapIt != _mapDepend.end(); mapIt++ )
2302 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2303 theSetOfCommon.insert( (*mapIt).second );
2304 return oldNb < theSetOfCommon.size();