1 // Copyright (C) 2007-2012 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
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_subMesh.hxx"
30 #include "SMESH_Algo.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_HypoFilter.hxx"
33 #include "SMESH_Hypothesis.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "SMESH_MesherHelper.hxx"
36 #include "SMESH_subMeshEventListener.hxx"
37 #include "SMESH_Comment.hxx"
38 #include "SMDS_SetIterator.hxx"
39 #include "SMDSAbs_ElementType.hxx"
41 #include <Basics_OCCTVersion.hxx>
43 #include "utilities.h"
45 #include "Basics_Utils.hxx"
47 #include <BRep_Builder.hxx>
48 #include <BRep_Tool.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <TopoDS_Compound.hxx>
55 #include <TopExp_Explorer.hxx>
56 #include <TopoDS_Iterator.hxx>
58 #include <Standard_OutOfMemory.hxx>
59 #include <Standard_ErrorHandler.hxx>
65 //=============================================================================
67 * \brief Allocate some memory at construction and release it at destruction.
68 * Is used to be able to continue working after mesh generation breaks due to
71 //=============================================================================
76 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
77 ~MemoryReserve() { delete [] myBuf; }
80 //=============================================================================
82 * default constructor:
84 //=============================================================================
86 SMESH_subMesh::SMESH_subMesh(int Id,
88 SMESHDS_Mesh * meshDS,
89 const TopoDS_Shape & aSubShape)
91 _subShape = aSubShape;
92 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
95 _dependenceAnalysed = _alwaysComputed = false;
97 if (_subShape.ShapeType() == TopAbs_VERTEX)
100 _computeState = READY_TO_COMPUTE;
104 _algoState = NO_ALGO;
105 _computeState = NOT_READY;
109 //=============================================================================
113 //=============================================================================
115 SMESH_subMesh::~SMESH_subMesh()
117 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
119 deleteOwnListeners();
122 //=============================================================================
126 //=============================================================================
128 int SMESH_subMesh::GetId() const
130 //MESSAGE("SMESH_subMesh::GetId");
134 //=============================================================================
138 //=============================================================================
140 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
142 // submesh appears in DS only when a mesher set nodes and elements on a shape
143 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
146 //=============================================================================
150 //=============================================================================
152 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
154 if ( !GetSubMeshDS() ) {
155 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
156 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
158 return GetSubMeshDS();
161 //=============================================================================
165 //=============================================================================
167 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
169 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
170 while ( smIt->more() ) {
171 SMESH_subMesh *sm = smIt->next();
172 if ( sm->GetComputeState() == READY_TO_COMPUTE )
175 return 0; // nothing to compute
178 //================================================================================
180 * \brief Returns a current algorithm
182 //================================================================================
184 SMESH_Algo* SMESH_subMesh::GetAlgo() const
186 return _father->GetGen()->GetAlgo(*_father, _subShape);
189 //================================================================================
191 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
192 * none mesh entity is bound to it (PAL13615, 2nd part)
194 //================================================================================
196 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
198 _alwaysComputed = isAlCo;
199 if ( _alwaysComputed )
200 _computeState = COMPUTE_OK;
202 ComputeStateEngine( CHECK_COMPUTE_STATE );
205 //=======================================================================
207 * \brief Return true if no mesh entities is bound to the submesh
209 //=======================================================================
211 bool SMESH_subMesh::IsEmpty() const
213 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
214 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
218 //=======================================================================
219 //function : IsMeshComputed
220 //purpose : check if _subMeshDS contains mesh elements
221 //=======================================================================
223 bool SMESH_subMesh::IsMeshComputed() const
225 if ( _alwaysComputed )
227 // algo may bind a submesh not to _subShape, eg 3D algo
228 // sets nodes on SHELL while _subShape may be SOLID
230 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
231 int dim = SMESH_Gen::GetShapeDim( _subShape );
232 int type = _subShape.ShapeType();
233 for ( ; type <= TopAbs_VERTEX; type++) {
234 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
236 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
237 for ( ; exp.More(); exp.Next() )
239 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
241 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
254 //=============================================================================
258 //=============================================================================
260 bool SMESH_subMesh::subMeshesComputed()
262 int myDim = SMESH_Gen::GetShapeDim( _subShape );
263 int dimToCheck = myDim - 1;
264 bool subMeshesComputed = true;
265 // check subMeshes with upper dimension => reverse iteration
266 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
267 while ( smIt->more() )
269 SMESH_subMesh *sm = smIt->next();
270 if ( sm->_alwaysComputed )
272 const TopoDS_Shape & ss = sm->GetSubShape();
273 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
274 // in checking of existence of edges if the algo needs only faces. Moreover,
275 // degenerated edges may have no submesh, as after computing NETGEN_2D.
276 int dim = SMESH_Gen::GetShapeDim( ss );
277 if (dim < dimToCheck)
278 break; // the rest subMeshes are all of less dimension
279 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
280 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
281 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
284 int type = ss.ShapeType();
286 subMeshesComputed = false;
290 case TopAbs_COMPOUND:
292 MESSAGE("The not computed sub mesh is a COMPOUND");
295 case TopAbs_COMPSOLID:
297 MESSAGE("The not computed sub mesh is a COMPSOLID");
302 MESSAGE("The not computed sub mesh is a SHEL");
307 MESSAGE("The not computed sub mesh is a WIRE");
312 MESSAGE("The not computed sub mesh is a SOLID");
317 MESSAGE("The not computed sub mesh is a FACE");
322 MESSAGE("The not computed sub mesh is a EDGE");
327 MESSAGE("The not computed sub mesh is of unknown type");
335 return subMeshesComputed;
338 //=============================================================================
342 //=============================================================================
344 // bool SMESH_subMesh::SubMeshesReady()
346 // bool subMeshesReady = true;
347 // SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
348 // while ( smIt->more() ) {
349 // SMESH_subMesh *sm = smIt->next();
350 // bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
351 // sm->GetComputeState() == READY_TO_COMPUTE);
354 // subMeshesReady = false;
355 // SCRUTE(sm->GetId());
359 // return subMeshesReady;
362 //=============================================================================
364 * Construct dependence on first level subMeshes. complex shapes (compsolid,
365 * shell, wire) are not analysed the same way as simple shapes (solid, face,
367 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
368 * with possible multiples occurences. Multiples occurences corresponds to
369 * internal frontiers within shapes of the collection and must not be keeped.
370 * See FinalizeDependence.
372 //=============================================================================
374 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
376 if (_dependenceAnalysed)
379 //MESSAGE("SMESH_subMesh::DependsOn");
381 int type = _subShape.ShapeType();
385 case TopAbs_COMPOUND:
387 //MESSAGE("compound");
388 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();exp.Next())
390 insertDependence(exp.Current());
392 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More(); exp.Next())
394 if ( BRep_Tool::IsClosed(exp.Current() ))
395 insertDependence(exp.Current()); //only shell not in solid
397 for (TopExp_Explorer expF(exp.Current(), TopAbs_FACE); expF.More();expF.Next())
398 insertDependence(expF.Current()); // issue 0020959: HEXA_3D fails on shell
401 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();exp.Next())
403 insertDependence(exp.Current());
405 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();exp.Next())
407 insertDependence(exp.Current());
411 case TopAbs_COMPSOLID:
413 //MESSAGE("compsolid");
414 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More(); exp.Next())
416 insertDependence(exp.Current());
423 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More(); exp.Next())
425 insertDependence(exp.Current());
432 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More(); exp.Next())
434 insertDependence(exp.Current());
441 if(_father->HasShapeToMesh()) {
442 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();exp.Next())
444 insertDependence(exp.Current());
452 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();exp.Next())
454 insertDependence(exp.Current());
461 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More(); exp.Next())
463 insertDependence(exp.Current());
476 _dependenceAnalysed = true;
480 //=============================================================================
482 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
484 //=============================================================================
486 void SMESH_subMesh::insertDependence(const TopoDS_Shape aSubShape)
488 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
489 int type = aSubShape.ShapeType();
490 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
491 int cle = aSubMesh->GetId();
492 cle += 10000000 * ordType; // sort map by ordType then index
493 if ( _mapDepend.find( cle ) == _mapDepend.end())
495 _mapDepend[cle] = aSubMesh;
496 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
497 _mapDepend.insert( subMap.begin(), subMap.end() );
501 //=============================================================================
505 //=============================================================================
507 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
509 //MESSAGE("SMESH_subMesh::GetSubShape");
514 //=======================================================================
515 //function : CanAddHypothesis
516 //purpose : return true if theHypothesis can be attached to me:
517 // its dimention is checked
518 //=======================================================================
520 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
522 int aHypDim = theHypothesis->GetDim();
523 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
524 // issue 21106. Forbid 3D mesh on the SHELL
525 // if (aHypDim == 3 && aShapeDim == 3) {
526 // // check case of open shell
527 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
528 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
531 if ( aHypDim <= aShapeDim )
537 //=======================================================================
538 //function : IsApplicableHypotesis
540 //=======================================================================
542 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
543 const TopAbs_ShapeEnum theShapeType)
545 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
548 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
549 // issue 21106. Forbid 3D mesh on the SHELL
550 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
556 switch ( theShapeType ) {
561 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
564 // Special case for algorithms, building 2D mesh on a whole shell.
565 // Before this fix there was a problem after restoring from study,
566 // because in that case algorithm is assigned before hypothesis
567 // (on shell in problem case) and hypothesis is checked on faces
568 // (because it is 2D), where we have NO_ALGO state.
569 // Now 2D hypothesis is also applicable to shells.
570 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
573 // case TopAbs_COMPSOLID:
574 // case TopAbs_COMPOUND:
580 //=============================================================================
584 //=============================================================================
586 SMESH_Hypothesis::Hypothesis_Status
587 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
589 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
590 //SCRUTE(_algoState);
593 // **** les retour des evenement shape sont significatifs
594 // (add ou remove fait ou non)
595 // le retour des evenement father n'indiquent pas que add ou remove fait
597 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
599 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
600 //SMESH_Gen* gen =_father->GetGen();
601 SMESH_Algo* algo = 0;
603 if (_subShape.ShapeType() == TopAbs_VERTEX )
605 if ( anHyp->GetDim() != 0) {
606 if (event == ADD_HYP || event == ADD_ALGO)
607 return SMESH_Hypothesis::HYP_BAD_DIM;
609 return SMESH_Hypothesis::HYP_OK;
612 else if ( _algoState == HYP_OK ) {
613 // update default _algoState
614 if ( event != REMOVE_FATHER_ALGO )
616 _algoState = NO_ALGO;
619 _algoState = MISSING_HYP;
620 if ( event == REMOVE_FATHER_HYP ||
621 algo->CheckHypothesis(*_father,_subShape, aux_ret))
628 int oldAlgoState = _algoState;
629 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
630 bool needFullClean = false;
632 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
634 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
636 // -------------------------------------------
637 // check if a shape needed by algo is present
638 // -------------------------------------------
639 algo = static_cast< SMESH_Algo* >( anHyp );
640 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
641 return SMESH_Hypothesis::HYP_NEED_SHAPE;
642 // ----------------------
643 // check mesh conformity
644 // ----------------------
645 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
646 return SMESH_Hypothesis::HYP_NOTCONFORM;
648 // check if all-dimensional algo is hidden by other local one
649 if ( event == ADD_ALGO ) {
650 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
651 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
652 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
653 if ( SMESH_Algo * curAlgo = (SMESH_Algo*) _father->GetHypothesis( _subShape, filter, true ))
654 needFullClean = ( !curAlgo->NeedDiscreteBoundary() );
658 // ----------------------------------
659 // add a hypothesis to DS if possible
660 // ----------------------------------
661 if (event == ADD_HYP || event == ADD_ALGO)
663 if ( ! CanAddHypothesis( anHyp )) // check dimension
664 return SMESH_Hypothesis::HYP_BAD_DIM;
666 if ( /*!anHyp->IsAuxiliary() &&*/ getSimilarAttached( _subShape, anHyp ) )
667 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
669 if ( !meshDS->AddHypothesis(_subShape, anHyp))
670 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
673 // --------------------------
674 // remove a hypothesis from DS
675 // --------------------------
676 if (event == REMOVE_HYP || event == REMOVE_ALGO)
678 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
679 return SMESH_Hypothesis::HYP_OK; // nothing changes
681 if (event == REMOVE_ALGO)
683 algo = dynamic_cast<SMESH_Algo*> (anHyp);
684 if (!algo->NeedDiscreteBoundary())
686 // clean all mesh in the tree of the current submesh;
687 // we must perform it now because later
688 // we will have no information about the type of the removed algo
689 needFullClean = true;
694 // ------------------
695 // analyse algo state
696 // ------------------
697 if (!isApplicableHyp)
698 return ret; // not applicable hypotheses do not change algo state
703 // ----------------------------------------------------------------------
712 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
713 setAlgoState(HYP_OK);
714 else if ( algo->IsStatusFatal( aux_ret )) {
715 meshDS->RemoveHypothesis(_subShape, anHyp);
719 setAlgoState(MISSING_HYP);
726 case ADD_FATHER_ALGO: { // Algo just added in father
729 if ( algo == anHyp ) {
730 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
731 setAlgoState(HYP_OK);
733 setAlgoState(MISSING_HYP);
737 case REMOVE_FATHER_HYP:
739 case REMOVE_FATHER_ALGO: {
743 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
744 setAlgoState(HYP_OK);
746 setAlgoState(MISSING_HYP);
750 case MODIF_HYP: break;
757 // ----------------------------------------------------------------------
765 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
766 setAlgoState(HYP_OK);
767 if (SMESH_Hypothesis::IsStatusFatal( ret ))
768 meshDS->RemoveHypothesis(_subShape, anHyp);
769 else if (!_father->IsUsedHypothesis( anHyp, this ))
771 meshDS->RemoveHypothesis(_subShape, anHyp);
772 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
776 case ADD_ALGO: { //already existing algo : on father ?
779 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
780 setAlgoState(HYP_OK);
781 else if ( algo->IsStatusFatal( aux_ret )) {
782 meshDS->RemoveHypothesis(_subShape, anHyp);
786 setAlgoState(MISSING_HYP);
791 case REMOVE_ALGO: { // perhaps a father algo applies ?
793 if (algo == NULL) // no more algo applying on sub-shape...
795 setAlgoState(NO_ALGO);
799 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
800 setAlgoState(HYP_OK);
802 setAlgoState(MISSING_HYP);
806 case MODIF_HYP: // assigned hypothesis value may become good
807 case ADD_FATHER_HYP: {
810 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
811 setAlgoState(HYP_OK);
813 setAlgoState(MISSING_HYP);
816 case ADD_FATHER_ALGO: { // new father algo
819 if ( algo == anHyp ) {
820 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
821 setAlgoState(HYP_OK);
823 setAlgoState(MISSING_HYP);
827 case REMOVE_FATHER_HYP: // nothing to do
829 case REMOVE_FATHER_ALGO: {
831 if (algo == NULL) // no more applying algo on father
833 setAlgoState(NO_ALGO);
837 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
838 setAlgoState(HYP_OK);
840 setAlgoState(MISSING_HYP);
850 // ----------------------------------------------------------------------
858 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
860 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
861 // ret should be fatal: anHyp was not added
862 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
864 else if (!_father->IsUsedHypothesis( anHyp, this ))
865 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
867 if (SMESH_Hypothesis::IsStatusFatal( ret ))
869 MESSAGE("do not add extra hypothesis");
870 meshDS->RemoveHypothesis(_subShape, anHyp);
878 case ADD_ALGO: { //already existing algo : on father ?
880 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
881 // check if algo changes
883 f.Init( SMESH_HypoFilter::IsAlgo() );
884 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
885 f.AndNot( SMESH_HypoFilter::Is( algo ));
886 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
888 string(algo->GetName()) != string(prevAlgo->GetName()) )
892 setAlgoState(MISSING_HYP);
898 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
899 setAlgoState(HYP_OK);
901 setAlgoState(MISSING_HYP);
905 case REMOVE_ALGO: { // perhaps a father algo applies ?
907 if (algo == NULL) // no more algo applying on sub-shape...
909 setAlgoState(NO_ALGO);
913 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
914 // check if algo remains
915 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
919 setAlgoState(MISSING_HYP);
923 case MODIF_HYP: // hypothesis value may become bad
924 case ADD_FATHER_HYP: { // new father hypothesis ?
927 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
929 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
933 setAlgoState(MISSING_HYP);
936 case ADD_FATHER_ALGO: {
938 if ( algo == anHyp ) { // a new algo on father
939 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
940 // check if algo changes
942 f.Init( SMESH_HypoFilter::IsAlgo() );
943 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
944 f.AndNot( SMESH_HypoFilter::Is( algo ));
945 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
947 string(algo->GetName()) != string(prevAlgo->GetName()) )
951 setAlgoState(MISSING_HYP);
955 case REMOVE_FATHER_HYP: {
958 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
959 // is there the same local hyp or maybe a new father algo applied?
960 if ( !getSimilarAttached( _subShape, anHyp ) )
964 setAlgoState(MISSING_HYP);
967 case REMOVE_FATHER_ALGO: {
968 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
969 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
970 algo = dynamic_cast<SMESH_Algo*> (anHyp);
971 if (!algo->NeedDiscreteBoundary())
972 needFullClean = true;
975 if (algo == NULL) // no more applying algo on father
977 setAlgoState(NO_ALGO);
981 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
982 // check if algo changes
983 if ( string(algo->GetName()) != string( anHyp->GetName()) )
987 setAlgoState(MISSING_HYP);
997 // ----------------------------------------------------------------------
1004 // detect algorithm hiding
1006 if ( ret == SMESH_Hypothesis::HYP_OK &&
1007 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
1008 algo->GetName() == anHyp->GetName() )
1011 SMESH_Gen* gen = _father->GetGen();
1012 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1013 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
1014 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
1015 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1016 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1019 if ( ret == SMESH_Hypothesis::HYP_OK &&
1020 !algo->NeedDiscreteBoundary() &&
1021 !algo->SupportSubmeshes()) {
1022 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1023 gen->GetAlgo( *_father, _subShape, &algoAssignedTo );
1024 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1025 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1026 if ( gen->GetAlgo( *_father, i_sm->second->_subShape, &otherAssignedTo ) &&
1027 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1028 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1032 bool stateChange = ( _algoState != oldAlgoState );
1034 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1035 algo->SetEventListener( this );
1037 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1039 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1040 deleteOwnListeners();
1041 SetIsAlwaysComputed( false );
1042 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1043 // restore default states
1044 _algoState = HYP_OK;
1045 _computeState = READY_TO_COMPUTE;
1049 if ( needFullClean ) {
1050 // added or removed algo is all-dimensional
1051 ComputeStateEngine( CLEAN );
1053 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1056 if (stateChange || modifiedHyp)
1057 ComputeStateEngine(MODIF_ALGO_STATE);
1062 //=======================================================================
1063 //function : IsConform
1064 //purpose : check if a conform mesh will be produced by the Algo
1065 //=======================================================================
1067 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1069 // MESSAGE( "SMESH_subMesh::IsConform" );
1070 if ( !theAlgo ) return false;
1072 // Suppose that theAlgo is applicable to _subShape, do not check it here
1073 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1075 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1076 // on a sub-shape will be ignored by theAlgo
1077 if ( theAlgo->NeedDiscreteBoundary() ||
1078 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1081 SMESH_Gen* gen =_father->GetGen();
1083 // only local algo is to be checked
1084 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1085 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1088 // check algo attached to adjacent shapes
1090 // loop on one level down sub-meshes
1091 TopoDS_Iterator itsub( _subShape );
1092 for (; itsub.More(); itsub.Next())
1094 // loop on adjacent subShapes
1095 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1096 for (; it.More(); it.Next())
1098 const TopoDS_Shape& adjacent = it.Value();
1099 if ( _subShape.IsSame( adjacent )) continue;
1100 if ( adjacent.ShapeType() != _subShape.ShapeType())
1103 // check algo attached to smAdjacent
1104 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1106 !algo->NeedDiscreteBoundary() &&
1107 algo->OnlyUnaryInput())
1108 return false; // NOT CONFORM MESH WILL BE PRODUCED
1115 //=============================================================================
1119 //=============================================================================
1121 void SMESH_subMesh::setAlgoState(int state)
1126 //=============================================================================
1130 //=============================================================================
1131 SMESH_Hypothesis::Hypothesis_Status
1132 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1133 SMESH_Hypothesis * anHyp)
1135 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1136 //EAP: a wire (dim==1) should notify edges (dim==1)
1137 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1138 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1140 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1141 while ( smIt->more() ) {
1142 SMESH_Hypothesis::Hypothesis_Status ret2 =
1143 smIt->next()->AlgoStateEngine(event, anHyp);
1151 //=============================================================================
1155 //=============================================================================
1157 void SMESH_subMesh::cleanDependsOn()
1159 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1160 while ( smIt->more() )
1161 smIt->next()->ComputeStateEngine(CLEAN);
1164 //=============================================================================
1168 //=============================================================================
1170 void SMESH_subMesh::DumpAlgoState(bool isMain)
1172 int dim = SMESH_Gen::GetShapeDim(_subShape);
1173 // if (dim < 1) return;
1176 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1178 map < int, SMESH_subMesh * >::const_iterator itsub;
1179 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1181 SMESH_subMesh *sm = (*itsub).second;
1182 sm->DumpAlgoState(false);
1185 int type = _subShape.ShapeType();
1186 MESSAGE("dim = " << dim << " type of shape " << type);
1190 MESSAGE(" AlgoState = NO_ALGO");
1193 MESSAGE(" AlgoState = MISSING_HYP");
1196 MESSAGE(" AlgoState = HYP_OK");
1199 switch (_computeState)
1202 MESSAGE(" ComputeState = NOT_READY");
1204 case READY_TO_COMPUTE:
1205 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1208 MESSAGE(" ComputeState = COMPUTE_OK");
1210 case FAILED_TO_COMPUTE:
1211 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1216 //================================================================================
1218 * \brief Remove nodes and elements bound to submesh
1219 * \param subMesh - submesh containing nodes and elements
1221 //================================================================================
1223 static void cleanSubMesh( SMESH_subMesh * subMesh )
1226 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1227 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1228 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1229 while (ite->more()) {
1230 const SMDS_MeshElement * elt = ite->next();
1231 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1232 //meshDS->RemoveElement(elt);
1233 meshDS->RemoveFreeElement(elt, subMeshDS);
1236 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1237 while (itn->more()) {
1238 const SMDS_MeshNode * node = itn->next();
1239 //MESSAGE( " RM node: "<<node->GetID());
1240 if ( node->NbInverseElements() == 0 )
1241 meshDS->RemoveFreeNode(node, subMeshDS);
1242 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1243 meshDS->RemoveNode(node);
1249 //=============================================================================
1253 //=============================================================================
1255 bool SMESH_subMesh::ComputeStateEngine(int event)
1257 _computeError.reset();
1259 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1260 //SCRUTE(_computeState);
1263 if (_subShape.ShapeType() == TopAbs_VERTEX)
1265 _computeState = READY_TO_COMPUTE;
1266 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1267 if ( smDS && smDS->NbNodes() ) {
1268 if ( event == CLEAN ) {
1270 cleanSubMesh( this );
1273 _computeState = COMPUTE_OK;
1275 else if ( event == COMPUTE && !_alwaysComputed ) {
1276 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1277 gp_Pnt P = BRep_Tool::Pnt(V);
1278 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1279 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1280 _computeState = COMPUTE_OK;
1283 if ( event == MODIF_ALGO_STATE )
1287 SMESH_Gen *gen = _father->GetGen();
1288 SMESH_Algo *algo = 0;
1290 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1291 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1293 switch (_computeState)
1296 // ----------------------------------------------------------------------
1301 case MODIF_ALGO_STATE:
1303 if (algo && !algo->NeedDiscreteBoundary())
1304 cleanDependsOn(); // clean sub-meshes with event CLEAN
1305 if ( _algoState == HYP_OK )
1306 _computeState = READY_TO_COMPUTE;
1308 case COMPUTE: // nothing to do
1310 #ifdef WITH_SMESH_CANCEL_COMPUTE
1311 case COMPUTE_CANCELED: // nothing to do
1316 removeSubMeshElementsAndNodes();
1318 case SUBMESH_COMPUTED: // nothing to do
1320 case SUBMESH_RESTORED:
1321 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1323 case MESH_ENTITY_REMOVED:
1325 case SUBMESH_LOADED:
1326 loadDependentMeshes();
1327 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1329 case CHECK_COMPUTE_STATE:
1330 if ( IsMeshComputed() )
1331 _computeState = COMPUTE_OK;
1339 // ----------------------------------------------------------------------
1341 case READY_TO_COMPUTE:
1344 case MODIF_ALGO_STATE:
1345 _computeState = NOT_READY;
1349 if (!algo->NeedDiscreteBoundary())
1350 cleanDependsOn(); // clean sub-meshes with event CLEAN
1351 if ( _algoState == HYP_OK )
1352 _computeState = READY_TO_COMPUTE;
1359 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1362 MESSAGE("***** verify compute state *****");
1363 _computeState = NOT_READY;
1364 setAlgoState(MISSING_HYP);
1367 TopoDS_Shape shape = _subShape;
1368 // check submeshes needed
1369 if (_father->HasShapeToMesh() ) {
1370 bool subComputed = false;
1371 if (!algo->OnlyUnaryInput())
1372 shape = getCollection( gen, algo, subComputed );
1374 subComputed = subMeshesComputed();
1375 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1376 algo->SupportSubmeshes() ? true :
1377 ( !subComputed || _father->IsNotConformAllowed() ));
1379 _computeState = FAILED_TO_COMPUTE;
1380 if ( !algo->NeedDiscreteBoundary() )
1382 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1383 "Unexpected computed submesh",algo);
1389 //cleanDependants(); for "UseExisting_*D" algos
1390 //removeSubMeshElementsAndNodes();
1391 loadDependentMeshes();
1393 _computeState = FAILED_TO_COMPUTE;
1394 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1396 #if OCC_VERSION_LARGE > 0x06010000
1399 algo->InitComputeError();
1400 MemoryReserve aMemoryReserve;
1401 SMDS_Mesh::CheckMemory();
1402 Kernel_Utils::Localizer loc;
1403 if ( !_father->HasShapeToMesh() ) // no shape
1405 SMESH_MesherHelper helper( *_father );
1406 helper.SetSubShape( shape );
1407 helper.SetElementsOnShape( true );
1408 ret = algo->Compute(*_father, &helper );
1412 ret = algo->Compute((*_father), shape);
1414 if ( !_computeError || ( !ret && _computeError->IsOK() ) ) // algo can set _computeError of submesh
1415 _computeError = algo->GetComputeError();
1417 catch ( ::SMESH_ComputeError& comperr ) {
1418 cout << " SMESH_ComputeError caught" << endl;
1419 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1420 *_computeError = comperr;
1422 catch ( std::bad_alloc& exc ) {
1423 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1424 if ( _computeError ) {
1425 _computeError->myName = COMPERR_MEMORY_PB;
1426 //_computeError->myComment = exc.what();
1428 cleanSubMesh( this );
1431 catch ( Standard_OutOfMemory& exc ) {
1432 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1433 if ( _computeError ) {
1434 _computeError->myName = COMPERR_MEMORY_PB;
1435 //_computeError->myComment = exc.what();
1437 cleanSubMesh( this );
1438 throw std::bad_alloc();
1440 catch (Standard_Failure& ex) {
1441 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1442 _computeError->myName = COMPERR_OCC_EXCEPTION;
1443 _computeError->myComment += ex.DynamicType()->Name();
1444 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1445 _computeError->myComment += ": ";
1446 _computeError->myComment += ex.GetMessageString();
1449 catch ( SALOME_Exception& S_ex ) {
1450 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1451 _computeError->myName = COMPERR_SLM_EXCEPTION;
1452 _computeError->myComment = S_ex.what();
1454 catch ( std::exception& exc ) {
1455 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1456 _computeError->myName = COMPERR_STD_EXCEPTION;
1457 _computeError->myComment = exc.what();
1460 if ( _computeError )
1461 _computeError->myName = COMPERR_EXCEPTION;
1465 TopExp_Explorer subS(shape, _subShape.ShapeType());
1466 if (ret) // check if anything was built
1468 for (; ret && subS.More(); subS.Next())
1469 ret = _father->GetSubMesh( subS.Current() )->IsMeshComputed();
1471 bool isComputeErrorSet = !checkComputeError( algo, shape );
1472 if (!ret && !isComputeErrorSet)
1474 // Set _computeError
1475 for (subS.ReInit(); subS.More(); subS.Next())
1477 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1478 if ( !sm->IsMeshComputed() )
1480 if ( !sm->_computeError )
1481 sm->_computeError = SMESH_ComputeError::New();
1482 if ( sm->_computeError->IsOK() )
1483 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1484 sm->_computeState = FAILED_TO_COMPUTE;
1485 sm->_computeError->myAlgo = algo;
1489 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1491 _computeError.reset();
1493 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1496 #ifdef WITH_SMESH_CANCEL_COMPUTE
1497 case COMPUTE_CANCELED: // nothing to do
1502 removeSubMeshElementsAndNodes();
1503 _computeState = NOT_READY;
1507 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1509 _computeState = READY_TO_COMPUTE;
1511 setAlgoState(MISSING_HYP);
1514 case SUBMESH_COMPUTED: // nothing to do
1516 case SUBMESH_RESTORED:
1517 // check if a mesh is already computed that may
1518 // happen after retrieval from a file
1519 ComputeStateEngine( CHECK_COMPUTE_STATE );
1520 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1522 if (algo) algo->SubmeshRestored( this );
1524 case MESH_ENTITY_REMOVED:
1526 case SUBMESH_LOADED:
1527 loadDependentMeshes();
1528 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1530 case CHECK_COMPUTE_STATE:
1531 if ( IsMeshComputed() )
1532 _computeState = COMPUTE_OK;
1540 // ----------------------------------------------------------------------
1545 case MODIF_ALGO_STATE:
1546 ComputeStateEngine( CLEAN );
1548 if (algo && !algo->NeedDiscreteBoundary())
1549 cleanDependsOn(); // clean sub-meshes with event CLEAN
1551 case COMPUTE: // nothing to do
1553 #ifdef WITH_SMESH_CANCEL_COMPUTE
1554 case COMPUTE_CANCELED: // nothing to do
1558 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1559 removeSubMeshElementsAndNodes();
1560 _computeState = NOT_READY;
1561 if ( _algoState == HYP_OK )
1562 _computeState = READY_TO_COMPUTE;
1564 case SUBMESH_COMPUTED: // nothing to do
1566 case SUBMESH_RESTORED:
1567 ComputeStateEngine( CHECK_COMPUTE_STATE );
1568 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1570 if (algo) algo->SubmeshRestored( this );
1572 case MESH_ENTITY_REMOVED:
1573 updateDependantsState ( CHECK_COMPUTE_STATE );
1574 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1575 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1577 case CHECK_COMPUTE_STATE:
1578 if ( !IsMeshComputed() ) {
1579 if (_algoState == HYP_OK)
1580 _computeState = READY_TO_COMPUTE;
1582 _computeState = NOT_READY;
1585 case SUBMESH_LOADED:
1586 // already treated event, thanks to which _computeState == COMPUTE_OK
1594 // ----------------------------------------------------------------------
1596 case FAILED_TO_COMPUTE:
1599 case MODIF_ALGO_STATE:
1601 ComputeStateEngine( CLEAN );
1603 if (algo && !algo->NeedDiscreteBoundary())
1604 cleanDependsOn(); // clean sub-meshes with event CLEAN
1605 if (_algoState == HYP_OK)
1606 _computeState = READY_TO_COMPUTE;
1608 _computeState = NOT_READY;
1610 case COMPUTE: // nothing to do
1612 #ifdef WITH_SMESH_CANCEL_COMPUTE
1613 case COMPUTE_CANCELED:
1616 algo->CancelCompute();
1621 cleanDependants(); // submeshes dependent on me should be cleaned as well
1622 removeSubMeshElementsAndNodes();
1624 case SUBMESH_COMPUTED: // allow retry compute
1625 if (_algoState == HYP_OK)
1626 _computeState = READY_TO_COMPUTE;
1628 _computeState = NOT_READY;
1630 case SUBMESH_RESTORED:
1631 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1633 case MESH_ENTITY_REMOVED:
1635 case CHECK_COMPUTE_STATE:
1636 if ( IsMeshComputed() )
1637 _computeState = COMPUTE_OK;
1639 if (_algoState == HYP_OK)
1640 _computeState = READY_TO_COMPUTE;
1642 _computeState = NOT_READY;
1644 // case SUBMESH_LOADED:
1652 // ----------------------------------------------------------------------
1658 notifyListenersOnEvent( event, COMPUTE_EVENT );
1664 //=============================================================================
1668 //=============================================================================
1670 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1672 _computeError.reset();
1676 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1677 vector<int> aVec(SMDSEntity_Last,0);
1678 aVec[SMDSEntity_Node] = 1;
1679 aResMap.insert(make_pair(this,aVec));
1683 //SMESH_Gen *gen = _father->GetGen();
1684 SMESH_Algo *algo = 0;
1685 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1688 if(algo && !aResMap.count(this) )
1690 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1691 if (!ret) return false;
1693 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary())
1695 // check submeshes needed
1696 bool subMeshEvaluated = true;
1697 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1698 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1699 while ( smIt->more() && subMeshEvaluated )
1701 SMESH_subMesh* sm = smIt->next();
1702 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1703 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1704 const vector<int> & nbs = aResMap[ sm ];
1705 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1707 if ( !subMeshEvaluated )
1710 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1711 ret = algo->Evaluate((*_father), _subShape, aResMap);
1713 aResMap.insert( make_pair( this,vector<int>(0)));
1720 //=======================================================================
1722 * \brief Update compute_state by _computeError and send proper events to
1723 * dependent submeshes
1724 * \retval bool - true if _computeError is NOT set
1726 //=======================================================================
1728 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1730 bool noErrors = true;
1732 if ( !theShape.IsNull() )
1734 // Check state of submeshes
1735 if ( !theAlgo->NeedDiscreteBoundary())
1737 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1738 while ( smIt->more() )
1739 if ( !smIt->next()->checkComputeError( theAlgo ))
1743 // Check state of neighbours
1744 if ( !theAlgo->OnlyUnaryInput() &&
1745 theShape.ShapeType() == TopAbs_COMPOUND &&
1746 !theShape.IsSame( _subShape ))
1748 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1749 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1751 if ( !sm->checkComputeError( theAlgo, sm->GetSubShape() ))
1753 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1760 if ( !_computeError || _computeError->IsOK() )
1762 // no error description is set to this sub-mesh, check if any mesh is computed
1763 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1767 if ( !_computeError->myAlgo )
1768 _computeError->myAlgo = theAlgo;
1772 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1773 if (_computeError->IsCommon() )
1774 text << _computeError->CommonName();
1776 text << _computeError->myName;
1777 if ( _computeError->myComment.size() > 0 )
1778 text << " \"" << _computeError->myComment << "\"";
1782 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1790 //=======================================================================
1791 //function : updateSubMeshState
1793 //=======================================================================
1795 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1797 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1798 while ( smIt->more() )
1799 smIt->next()->_computeState = theState;
1802 //=======================================================================
1803 //function : ComputeSubMeshStateEngine
1805 //=======================================================================
1807 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
1809 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
1810 while ( smIt->more() )
1811 smIt->next()->ComputeStateEngine(event);
1814 //=======================================================================
1815 //function : updateDependantsState
1817 //=======================================================================
1819 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
1821 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1822 for (; it.More(); it.Next())
1824 const TopoDS_Shape& ancestor = it.Value();
1825 SMESH_subMesh *aSubMesh =
1826 _father->GetSubMeshContaining(ancestor);
1828 aSubMesh->ComputeStateEngine( theEvent );
1832 //=============================================================================
1836 //=============================================================================
1838 void SMESH_subMesh::cleanDependants()
1840 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1842 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1843 for (; it.More(); it.Next())
1845 const TopoDS_Shape& ancestor = it.Value();
1846 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1847 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1848 // will erase mesh on other shapes in a compound
1849 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1850 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1852 !aSubMesh->IsEmpty() ) // prevent infinite CLEAN via event lesteners
1853 aSubMesh->ComputeStateEngine(CLEAN);
1859 //=============================================================================
1863 //=============================================================================
1865 void SMESH_subMesh::removeSubMeshElementsAndNodes()
1867 cleanSubMesh( this );
1869 // algo may bind a submesh not to _subShape, eg 3D algo
1870 // sets nodes on SHELL while _subShape may be SOLID
1872 int dim = SMESH_Gen::GetShapeDim( _subShape );
1873 int type = _subShape.ShapeType() + 1;
1874 for ( ; type <= TopAbs_EDGE; type++) {
1875 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1877 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1878 for ( ; exp.More(); exp.Next() )
1879 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1886 //=======================================================================
1887 //function : getCollection
1888 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1889 // meshed at once along with _subShape
1890 //=======================================================================
1892 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
1893 SMESH_Algo* theAlgo,
1894 bool & theSubComputed)
1896 theSubComputed = subMeshesComputed();
1898 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1900 if ( mainShape.IsSame( _subShape ))
1903 const bool ignoreAuxiliaryHyps = false;
1904 list<const SMESHDS_Hypothesis*> aUsedHyp =
1905 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1907 // put in a compound all shapes with the same hypothesis assigned
1908 // and a good ComputState
1910 TopoDS_Compound aCompound;
1911 BRep_Builder aBuilder;
1912 aBuilder.MakeCompound( aCompound );
1914 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1915 for ( ; anExplorer.More(); anExplorer.Next() )
1917 const TopoDS_Shape& S = anExplorer.Current();
1918 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1919 if ( subMesh == this )
1921 aBuilder.Add( aCompound, S );
1923 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1925 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1926 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1927 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1928 aBuilder.Add( aCompound, S );
1929 if ( !subMesh->subMeshesComputed() )
1930 theSubComputed = false;
1937 //=======================================================================
1938 //function : getSimilarAttached
1939 //purpose : return a hypothesis attached to theShape.
1940 // If theHyp is provided, similar but not same hypotheses
1941 // is returned; else only applicable ones having theHypType
1943 //=======================================================================
1945 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
1946 const SMESH_Hypothesis * theHyp,
1947 const int theHypType)
1949 SMESH_HypoFilter hypoKind;
1950 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1952 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1953 hypoKind.AndNot( hypoKind.Is( theHyp ));
1954 if ( theHyp->IsAuxiliary() )
1955 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1957 hypoKind.AndNot( hypoKind.IsAuxiliary());
1960 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1963 return _father->GetHypothesis( theShape, hypoKind, false );
1966 //=======================================================================
1967 //function : CheckConcurentHypothesis
1968 //purpose : check if there are several applicable hypothesis attached to
1970 //=======================================================================
1972 SMESH_Hypothesis::Hypothesis_Status
1973 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1975 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1977 // is there local hypothesis on me?
1978 if ( getSimilarAttached( _subShape, 0, theHypType ) )
1979 return SMESH_Hypothesis::HYP_OK;
1982 TopoDS_Shape aPrevWithHyp;
1983 const SMESH_Hypothesis* aPrevHyp = 0;
1984 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1985 for (; it.More(); it.Next())
1987 const TopoDS_Shape& ancestor = it.Value();
1988 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
1991 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1993 aPrevWithHyp = ancestor;
1996 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1997 return SMESH_Hypothesis::HYP_CONCURENT;
1999 return SMESH_Hypothesis::HYP_OK;
2002 return SMESH_Hypothesis::HYP_OK;
2005 //================================================================================
2007 * \brief Constructor of OwnListenerData
2009 //================================================================================
2011 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2013 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2014 mySubMeshID( sm ? sm->GetId() : -1 ),
2019 //================================================================================
2021 * \brief Sets an event listener and its data to a submesh
2022 * \param listener - the listener to store
2023 * \param data - the listener data to store
2024 * \param where - the submesh to store the listener and it's data
2026 * It remembers the submesh where it puts the listener in order to delete
2027 * them when HYP_OK algo_state is lost
2028 * After being set, event listener is notified on each event of where submesh.
2030 //================================================================================
2032 void SMESH_subMesh::SetEventListener(EventListener* listener,
2033 EventListenerData* data,
2034 SMESH_subMesh* where)
2036 if ( listener && where ) {
2037 where->setEventListener( listener, data );
2038 _ownListeners.push_back( OwnListenerData( where, listener ));
2042 //================================================================================
2044 * \brief Sets an event listener and its data to a submesh
2045 * \param listener - the listener to store
2046 * \param data - the listener data to store
2048 * After being set, event listener is notified on each event of a submesh.
2050 //================================================================================
2052 void SMESH_subMesh::setEventListener(EventListener* listener, EventListenerData* data)
2054 map< EventListener*, EventListenerData* >::iterator l_d =
2055 _eventListeners.find( listener );
2056 if ( l_d != _eventListeners.end() ) {
2057 EventListenerData* curData = l_d->second;
2058 if ( curData && curData != data && curData->IsDeletable() )
2063 _eventListeners.insert( make_pair( listener, data ));
2066 //================================================================================
2068 * \brief Return an event listener data
2069 * \param listener - the listener whose data is
2070 * \retval EventListenerData* - found data, maybe NULL
2072 //================================================================================
2074 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
2076 map< EventListener*, EventListenerData* >::const_iterator l_d =
2077 _eventListeners.find( listener );
2078 if ( l_d != _eventListeners.end() )
2083 //================================================================================
2085 * \brief Notify stored event listeners on the occured event
2086 * \param event - algo_event or compute_event itself
2087 * \param eventType - algo_event or compute_event
2088 * \param hyp - hypothesis, if eventType is algo_event
2090 //================================================================================
2092 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2093 const event_type eventType,
2094 SMESH_Hypothesis* hyp)
2096 map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2097 for ( ; l_d != _eventListeners.end(); ++l_d )
2099 std::pair< EventListener*, EventListenerData* > li_da = *l_d; /* copy to enable removal
2102 its ProcessEvent() */
2103 if ( li_da.first->myBusySM.insert( this ).second )
2105 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2106 li_da.first->myBusySM.erase( this );
2111 //================================================================================
2113 * \brief Unregister the listener and delete listener's data
2114 * \param listener - the event listener
2116 //================================================================================
2118 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2120 map< EventListener*, EventListenerData* >::iterator l_d =
2121 _eventListeners.find( listener );
2122 if ( l_d != _eventListeners.end() ) {
2123 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2124 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2125 _eventListeners.erase( l_d );
2129 //================================================================================
2131 * \brief Delete event listeners depending on algo of this submesh
2133 //================================================================================
2135 void SMESH_subMesh::deleteOwnListeners()
2137 list< OwnListenerData >::iterator d;
2138 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2140 if ( !_father->MeshExists( d->myMeshID ))
2142 if ( _father->GetId() == d->myMeshID && !_father->GetSubMeshContaining( d->mySubMeshID ))
2144 d->mySubMesh->DeleteEventListener( d->myListener );
2146 _ownListeners.clear();
2149 //=======================================================================
2150 //function : loadDependentMeshes
2151 //purpose : loads dependent meshes on SUBMESH_LOADED event
2152 //=======================================================================
2154 void SMESH_subMesh::loadDependentMeshes()
2156 list< OwnListenerData >::iterator d;
2157 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2158 if ( _father != d->mySubMesh->_father )
2159 d->mySubMesh->_father->Load();
2161 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2162 // for ( ; l_d != _eventListeners.end(); ++l_d )
2163 // if ( l_d->second )
2165 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2166 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2167 // for ( ; sm != smList.end(); ++sm )
2168 // if ( _father != (*sm)->_father )
2169 // (*sm)->_father->Load();
2173 //================================================================================
2175 * \brief Do something on a certain event
2176 * \param event - algo_event or compute_event itself
2177 * \param eventType - algo_event or compute_event
2178 * \param subMesh - the submesh where the event occures
2179 * \param data - listener data stored in the subMesh
2180 * \param hyp - hypothesis, if eventType is algo_event
2182 * The base implementation translates CLEAN event to the subMesh
2183 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2184 * successful COMPUTE event.
2186 //================================================================================
2188 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2189 const int eventType,
2190 SMESH_subMesh* subMesh,
2191 EventListenerData* data,
2192 const SMESH_Hypothesis* /*hyp*/)
2194 if ( data && !data->mySubMeshes.empty() &&
2195 eventType == SMESH_subMesh::COMPUTE_EVENT)
2197 ASSERT( data->mySubMeshes.front() != subMesh );
2198 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2199 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2201 case SMESH_subMesh::CLEAN:
2202 for ( ; smIt != smEnd; ++ smIt)
2203 (*smIt)->ComputeStateEngine( event );
2205 case SMESH_subMesh::COMPUTE:
2206 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2207 for ( ; smIt != smEnd; ++ smIt)
2208 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2217 //================================================================================
2219 * \brief Iterator over submeshes and optionally prepended or appended one
2221 //================================================================================
2223 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2225 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2226 SMESH_subMesh* prepend,
2227 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2229 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2230 if ( myCur == append ) append = 0;
2232 /// Return true if and only if there are other object in this iterator
2237 /// Return the current object and step to the next one
2238 virtual SMESH_subMesh* next()
2240 SMESH_subMesh* res = myCur;
2241 if ( myIt->more() ) { myCur = myIt->next(); }
2242 else { myCur = myAppend; myAppend = 0; }
2249 SMESH_subMesh *myAppend, *myCur;
2250 SMDS_Iterator<SMESH_subMesh*> *myIt;
2254 //================================================================================
2256 * \brief Return iterator on the submeshes this one depends on
2257 * \param includeSelf - this submesh to be returned also
2258 * \param reverse - if true, complex shape submeshes go first
2260 //================================================================================
2262 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2265 SMESH_subMesh *prepend=0, *append=0;
2266 if ( includeSelf ) {
2267 if ( reverse ) prepend = this;
2270 typedef map < int, SMESH_subMesh * > TMap;
2273 return SMESH_subMeshIteratorPtr
2274 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2277 return SMESH_subMeshIteratorPtr
2278 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));
2282 //================================================================================
2284 * \brief Find common submeshes (based on shared sub-shapes with other
2285 * \param theOther submesh to check
2286 * \param theSetOfCommon set of common submesh
2288 //================================================================================
2290 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2291 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2293 int oldNb = theSetOfCommon.size();
2294 // check main submeshes
2295 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2296 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2297 theSetOfCommon.insert( this );
2298 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2299 theSetOfCommon.insert( theOther );
2300 // check common submeshes
2301 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2302 for( ; mapIt != _mapDepend.end(); mapIt++ )
2303 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2304 theSetOfCommon.insert( (*mapIt).second );
2305 return oldNb < theSetOfCommon.size();