1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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 <TopExp_Explorer.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <TopTools_ListOfShape.hxx>
55 #include <TopoDS_Compound.hxx>
56 #include <TopoDS_Iterator.hxx>
59 #include <Standard_OutOfMemory.hxx>
60 #include <Standard_ErrorHandler.hxx>
66 //=============================================================================
68 * \brief Allocate some memory at construction and release it at destruction.
69 * Is used to be able to continue working after mesh generation breaks due to
72 //=============================================================================
77 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
78 ~MemoryReserve() { delete [] myBuf; }
81 //=============================================================================
83 * default constructor:
85 //=============================================================================
87 SMESH_subMesh::SMESH_subMesh(int Id,
89 SMESHDS_Mesh * meshDS,
90 const TopoDS_Shape & aSubShape)
92 _subShape = aSubShape;
93 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
96 _dependenceAnalysed = _alwaysComputed = false;
98 if (_subShape.ShapeType() == TopAbs_VERTEX)
101 _computeState = READY_TO_COMPUTE;
105 _algoState = NO_ALGO;
106 _computeState = NOT_READY;
108 _computeCost = 0; // how costly is to compute this sub-mesh
109 _realComputeCost = 0;
112 //=============================================================================
116 //=============================================================================
118 SMESH_subMesh::~SMESH_subMesh()
120 deleteOwnListeners();
123 //=============================================================================
127 //=============================================================================
129 int SMESH_subMesh::GetId() const
131 //MESSAGE("SMESH_subMesh::GetId");
135 //=============================================================================
139 //=============================================================================
141 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
143 // submesh appears in DS only when a mesher set nodes and elements on a shape
144 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
147 //=============================================================================
151 //=============================================================================
153 const SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS() const
155 return ((SMESH_subMesh*) this )->GetSubMeshDS();
158 //=============================================================================
162 //=============================================================================
164 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
166 if ( !GetSubMeshDS() ) {
167 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
168 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
170 return GetSubMeshDS();
173 //=============================================================================
177 //=============================================================================
179 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
181 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
182 while ( smIt->more() ) {
183 SMESH_subMesh *sm = smIt->next();
184 if ( sm->GetComputeState() == READY_TO_COMPUTE )
187 return 0; // nothing to compute
190 //================================================================================
192 * \brief Returns a current algorithm
194 //================================================================================
196 SMESH_Algo* SMESH_subMesh::GetAlgo() const
200 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
201 me->_algo = _father->GetGen()->GetAlgo( me );
206 //================================================================================
208 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
209 * none mesh entity is bound to it (PAL13615, 2nd part)
211 //================================================================================
213 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
215 _alwaysComputed = isAlCo;
216 if ( _alwaysComputed )
217 _computeState = COMPUTE_OK;
219 ComputeStateEngine( CHECK_COMPUTE_STATE );
222 //=======================================================================
224 * \brief Return true if no mesh entities is bound to the submesh
226 //=======================================================================
228 bool SMESH_subMesh::IsEmpty() const
230 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
231 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
235 //=======================================================================
236 //function : IsMeshComputed
237 //purpose : check if _subMeshDS contains mesh elements
238 //=======================================================================
240 bool SMESH_subMesh::IsMeshComputed() const
242 if ( _alwaysComputed )
244 // algo may bind a submesh not to _subShape, eg 3D algo
245 // sets nodes on SHELL while _subShape may be SOLID
247 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
248 int dim = SMESH_Gen::GetShapeDim( _subShape );
249 int type = _subShape.ShapeType();
250 for ( ; type <= TopAbs_VERTEX; type++) {
251 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
253 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
254 for ( ; exp.More(); exp.Next() )
256 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
258 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
271 //=============================================================================
273 * Return true if all sub-meshes have been meshed
275 //=============================================================================
277 bool SMESH_subMesh::SubMeshesComputed(bool * isFailedToCompute/*=0*/) const
279 int myDim = SMESH_Gen::GetShapeDim( _subShape );
280 int dimToCheck = myDim - 1;
281 bool subMeshesComputed = true;
282 if ( isFailedToCompute ) *isFailedToCompute = false;
283 // check subMeshes with upper dimension => reverse iteration
284 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
285 while ( smIt->more() )
287 SMESH_subMesh *sm = smIt->next();
288 if ( sm->_alwaysComputed )
290 const TopoDS_Shape & ss = sm->GetSubShape();
292 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
293 // in checking of existence of edges if the algo needs only faces. Moreover,
294 // degenerated edges may have no submesh, as after computing NETGEN_2D.
295 if ( !_algo || _algo->NeedDiscreteBoundary() ) {
296 int dim = SMESH_Gen::GetShapeDim( ss );
297 if (dim < dimToCheck)
298 break; // the rest subMeshes are all of less dimension
300 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
301 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
302 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
305 subMeshesComputed = false;
306 if ( isFailedToCompute && !(*isFailedToCompute) )
307 *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
309 // int type = ss.ShapeType();
313 // case TopAbs_COMPOUND:
315 // MESSAGE("The not computed sub mesh is a COMPOUND");
318 // case TopAbs_COMPSOLID:
320 // MESSAGE("The not computed sub mesh is a COMPSOLID");
323 // case TopAbs_SHELL:
325 // MESSAGE("The not computed sub mesh is a SHEL");
330 // MESSAGE("The not computed sub mesh is a WIRE");
333 // case TopAbs_SOLID:
335 // MESSAGE("The not computed sub mesh is a SOLID");
340 // MESSAGE("The not computed sub mesh is a FACE");
345 // MESSAGE("The not computed sub mesh is a EDGE");
350 // MESSAGE("The not computed sub mesh is of unknown type");
355 if ( !isFailedToCompute )
359 return subMeshesComputed;
362 //================================================================================
364 * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
366 * \return int - the computation cost in abstract units.
368 //================================================================================
370 int SMESH_subMesh::GetComputeCost() const
372 return _realComputeCost;
375 //================================================================================
377 * \brief Return cost of computing this sub-mesh. The cost depends on the shape type
378 * and number of sub-meshes this one DependsOn().
379 * \return int - the computation cost in abstract units.
381 //================================================================================
383 int SMESH_subMesh::computeCost() const
388 switch ( _subShape.ShapeType() ) {
390 case TopAbs_SHELL: computeCost = 5000; break;
391 case TopAbs_FACE: computeCost = 500; break;
392 case TopAbs_EDGE: computeCost = 2; break;
393 default: computeCost = 1;
395 SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
396 while ( childIt->more() )
397 computeCost += childIt->next()->computeCost();
399 ((SMESH_subMesh*)this)->_computeCost = computeCost;
404 //=============================================================================
408 //=============================================================================
410 // bool SMESH_subMesh::SubMeshesReady()
412 // bool subMeshesReady = true;
413 // SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
414 // while ( smIt->more() ) {
415 // SMESH_subMesh *sm = smIt->next();
416 // bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
417 // sm->GetComputeState() == READY_TO_COMPUTE);
420 // subMeshesReady = false;
421 // SCRUTE(sm->GetId());
425 // return subMeshesReady;
428 //=============================================================================
430 * Construct dependence on first level subMeshes. complex shapes (compsolid,
431 * shell, wire) are not analysed the same way as simple shapes (solid, face,
433 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
434 * with possible multiples occurences. Multiples occurences corresponds to
435 * internal frontiers within shapes of the collection and must not be keeped.
436 * See FinalizeDependence.
438 //=============================================================================
440 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
442 if (_dependenceAnalysed)
445 //MESSAGE("SMESH_subMesh::DependsOn");
447 int type = _subShape.ShapeType();
451 case TopAbs_COMPOUND:
453 //MESSAGE("compound");
454 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();exp.Next())
456 insertDependence(exp.Current());
458 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More(); exp.Next())
460 if ( BRep_Tool::IsClosed(exp.Current() ))
461 insertDependence(exp.Current()); //only shell not in solid
463 for (TopExp_Explorer expF(exp.Current(), TopAbs_FACE); expF.More();expF.Next())
464 insertDependence(expF.Current()); // issue 0020959: HEXA_3D fails on shell
467 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();exp.Next())
469 insertDependence(exp.Current());
471 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();exp.Next())
473 insertDependence(exp.Current());
475 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX, TopAbs_EDGE); exp.More();exp.Next())
477 insertDependence(exp.Current());
481 case TopAbs_COMPSOLID:
483 //MESSAGE("compsolid");
484 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More(); exp.Next())
486 insertDependence(exp.Current());
493 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More(); exp.Next())
495 insertDependence(exp.Current());
502 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More(); exp.Next())
504 insertDependence(exp.Current());
511 if(_father->HasShapeToMesh()) {
512 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();exp.Next())
514 insertDependence(exp.Current());
522 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();exp.Next())
524 insertDependence(exp.Current());
531 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More(); exp.Next())
533 insertDependence(exp.Current());
546 _dependenceAnalysed = true;
550 //================================================================================
552 * \brief Return a key for SMESH_subMesh::_mapDepend map
554 //================================================================================
557 int dependsOnMapKey( const SMESH_subMesh* sm )
559 int type = sm->GetSubShape().ShapeType();
560 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
561 int cle = sm->GetId();
562 cle += 10000000 * ordType; // sort map by ordType then index
567 //=============================================================================
569 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
571 //=============================================================================
573 void SMESH_subMesh::insertDependence(const TopoDS_Shape aSubShape)
575 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
576 int cle = dependsOnMapKey( aSubMesh );
577 if ( _mapDepend.find( cle ) == _mapDepend.end())
579 _mapDepend[cle] = aSubMesh;
580 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
581 _mapDepend.insert( subMap.begin(), subMap.end() );
585 //================================================================================
587 * \brief Return \c true if \a this sub-mesh depends on \a other
589 //================================================================================
591 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
593 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
596 //=============================================================================
598 * Return a shape of \a this sub-mesh
600 //=============================================================================
602 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
607 //=======================================================================
608 //function : CanAddHypothesis
609 //purpose : return true if theHypothesis can be attached to me:
610 // its dimention is checked
611 //=======================================================================
613 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
615 int aHypDim = theHypothesis->GetDim();
616 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
617 // issue 21106. Forbid 3D mesh on the SHELL
618 // if (aHypDim == 3 && aShapeDim == 3) {
619 // // check case of open shell
620 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
621 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
624 if ( aHypDim <= aShapeDim )
630 //=======================================================================
631 //function : IsApplicableHypotesis
633 //=======================================================================
635 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
636 const TopAbs_ShapeEnum theShapeType)
638 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
641 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
642 // issue 21106. Forbid 3D mesh on the SHELL
643 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
649 switch ( theShapeType ) {
654 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
657 // Special case for algorithms, building 2D mesh on a whole shell.
658 // Before this fix there was a problem after restoring from study,
659 // because in that case algorithm is assigned before hypothesis
660 // (on shell in problem case) and hypothesis is checked on faces
661 // (because it is 2D), where we have NO_ALGO state.
662 // Now 2D hypothesis is also applicable to shells.
663 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
666 // case TopAbs_COMPSOLID:
667 // case TopAbs_COMPOUND:
673 //================================================================================
675 * \brief Treats modification of hypotheses definition
676 * \param [in] event - what happens
677 * \param [in] anHyp - a hypothesis
678 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
680 * Optional description of a problematic situation (if any) can be retrieved
681 * via GetComputeError().
683 //================================================================================
685 SMESH_Hypothesis::Hypothesis_Status
686 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
688 // **** les retour des evenement shape sont significatifs
689 // (add ou remove fait ou non)
690 // le retour des evenement father n'indiquent pas que add ou remove fait
692 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
694 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
695 SMESH_Algo* algo = 0;
698 if (_subShape.ShapeType() == TopAbs_VERTEX )
700 if ( anHyp->GetDim() != 0) {
701 if (event == ADD_HYP || event == ADD_ALGO)
702 return SMESH_Hypothesis::HYP_BAD_DIM;
704 return SMESH_Hypothesis::HYP_OK;
707 else if ( _algoState == HYP_OK ) {
708 // update default _algoState
709 if ( event != REMOVE_FATHER_ALGO )
711 _algoState = NO_ALGO;
714 _algoState = MISSING_HYP;
715 if ( event == REMOVE_FATHER_HYP ||
716 algo->CheckHypothesis(*_father,_subShape, aux_ret))
723 int oldAlgoState = _algoState;
724 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
725 SMESH_Algo* algoRequiringCleaning = 0;
727 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
729 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
731 // -------------------------------------------
732 // check if a shape needed by algo is present
733 // -------------------------------------------
734 algo = static_cast< SMESH_Algo* >( anHyp );
735 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
736 return SMESH_Hypothesis::HYP_NEED_SHAPE;
737 // ----------------------
738 // check mesh conformity
739 // ----------------------
740 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
741 return SMESH_Hypothesis::HYP_NOTCONFORM;
743 // check if all-dimensional algo is hidden by other local one
744 if ( event == ADD_ALGO ) {
745 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
746 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
747 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
748 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
749 if ( !curAlgo->NeedDiscreteBoundary() )
750 algoRequiringCleaning = curAlgo;
754 // ----------------------------------
755 // add a hypothesis to DS if possible
756 // ----------------------------------
757 if (event == ADD_HYP || event == ADD_ALGO)
759 if ( ! CanAddHypothesis( anHyp )) // check dimension
760 return SMESH_Hypothesis::HYP_BAD_DIM;
762 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
763 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
765 if ( !meshDS->AddHypothesis(_subShape, anHyp))
766 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
769 // --------------------------
770 // remove a hypothesis from DS
771 // --------------------------
772 if (event == REMOVE_HYP || event == REMOVE_ALGO)
774 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
775 return SMESH_Hypothesis::HYP_OK; // nothing changes
777 if (event == REMOVE_ALGO)
779 algo = dynamic_cast<SMESH_Algo*> (anHyp);
780 if (!algo->NeedDiscreteBoundary())
781 algoRequiringCleaning = algo;
785 // ------------------
786 // analyse algo state
787 // ------------------
788 if (!isApplicableHyp)
789 return ret; // not applicable hypotheses do not change algo state
791 if (( algo = GetAlgo()))
792 algo->InitComputeError();
797 // ----------------------------------------------------------------------
806 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
807 setAlgoState(HYP_OK);
808 else if ( algo->IsStatusFatal( aux_ret )) {
809 meshDS->RemoveHypothesis(_subShape, anHyp);
813 setAlgoState(MISSING_HYP);
820 case ADD_FATHER_ALGO: { // Algo just added in father
823 if ( algo == anHyp ) {
824 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
825 setAlgoState(HYP_OK);
827 setAlgoState(MISSING_HYP);
831 case REMOVE_FATHER_HYP:
833 case REMOVE_FATHER_ALGO: {
837 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
838 setAlgoState(HYP_OK);
840 setAlgoState(MISSING_HYP);
844 case MODIF_HYP: break;
851 // ----------------------------------------------------------------------
859 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
860 setAlgoState(HYP_OK);
861 if (SMESH_Hypothesis::IsStatusFatal( ret ))
862 meshDS->RemoveHypothesis(_subShape, anHyp);
863 else if (!_father->IsUsedHypothesis( anHyp, this ))
865 meshDS->RemoveHypothesis(_subShape, anHyp);
866 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
870 case ADD_ALGO: { //already existing algo : on father ?
873 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
874 setAlgoState(HYP_OK);
875 else if ( algo->IsStatusFatal( aux_ret )) {
876 meshDS->RemoveHypothesis(_subShape, anHyp);
880 setAlgoState(MISSING_HYP);
885 case REMOVE_ALGO: { // perhaps a father algo applies ?
887 if (algo == NULL) // no more algo applying on sub-shape...
889 setAlgoState(NO_ALGO);
893 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
894 setAlgoState(HYP_OK);
896 setAlgoState(MISSING_HYP);
900 case MODIF_HYP: // assigned hypothesis value may become good
901 case ADD_FATHER_HYP: {
904 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
905 setAlgoState(HYP_OK);
907 setAlgoState(MISSING_HYP);
910 case ADD_FATHER_ALGO: { // new father algo
913 if ( algo == anHyp ) {
914 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
915 setAlgoState(HYP_OK);
917 setAlgoState(MISSING_HYP);
921 case REMOVE_FATHER_HYP: // nothing to do
923 case REMOVE_FATHER_ALGO: {
925 if (algo == NULL) // no more applying algo on father
927 setAlgoState(NO_ALGO);
931 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
932 setAlgoState(HYP_OK);
934 setAlgoState(MISSING_HYP);
944 // ----------------------------------------------------------------------
952 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
954 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
955 // ret should be fatal: anHyp was not added
956 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
958 else if (!_father->IsUsedHypothesis( anHyp, this ))
959 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
961 if (SMESH_Hypothesis::IsStatusFatal( ret ))
963 MESSAGE("do not add extra hypothesis");
964 meshDS->RemoveHypothesis(_subShape, anHyp);
972 case ADD_ALGO: { //already existing algo : on father ?
974 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
975 // check if algo changes
977 f.Init( SMESH_HypoFilter::IsAlgo() );
978 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
979 f.AndNot( SMESH_HypoFilter::Is( algo ));
980 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
982 string(algo->GetName()) != string(prevAlgo->GetName()) )
986 setAlgoState(MISSING_HYP);
992 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
993 setAlgoState(HYP_OK);
995 setAlgoState(MISSING_HYP);
999 case REMOVE_ALGO: { // perhaps a father algo applies ?
1001 if (algo == NULL) // no more algo applying on sub-shape...
1003 setAlgoState(NO_ALGO);
1007 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1008 // check if algo remains
1009 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
1013 setAlgoState(MISSING_HYP);
1017 case MODIF_HYP: // hypothesis value may become bad
1018 case ADD_FATHER_HYP: { // new father hypothesis ?
1021 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
1023 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
1027 setAlgoState(MISSING_HYP);
1030 case ADD_FATHER_ALGO: {
1032 if ( algo == anHyp ) { // a new algo on father
1033 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1034 // check if algo changes
1036 f.Init( SMESH_HypoFilter::IsAlgo() );
1037 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
1038 f.AndNot( SMESH_HypoFilter::Is( algo ));
1039 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
1041 string(algo->GetName()) != string(prevAlgo->GetName()) )
1045 setAlgoState(MISSING_HYP);
1049 case REMOVE_FATHER_HYP: {
1052 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1053 // is there the same local hyp or maybe a new father algo applied?
1054 if ( !getSimilarAttached( _subShape, anHyp ) )
1058 setAlgoState(MISSING_HYP);
1061 case REMOVE_FATHER_ALGO: {
1062 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
1063 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
1064 algo = dynamic_cast<SMESH_Algo*> (anHyp);
1065 if (!algo->NeedDiscreteBoundary())
1066 algoRequiringCleaning = algo;
1068 if (algo == NULL) // no more applying algo on father
1070 setAlgoState(NO_ALGO);
1074 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1075 // check if algo changes
1076 if ( string(algo->GetName()) != string( anHyp->GetName()) )
1080 setAlgoState(MISSING_HYP);
1090 // ----------------------------------------------------------------------
1097 // detect algorithm hiding
1099 if ( ret == SMESH_Hypothesis::HYP_OK &&
1100 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
1101 algo->GetName() == anHyp->GetName() )
1104 SMESH_Gen* gen = _father->GetGen();
1105 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1106 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1107 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1108 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1109 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1112 if ( ret == SMESH_Hypothesis::HYP_OK &&
1113 !algo->NeedDiscreteBoundary() &&
1114 !algo->SupportSubmeshes())
1116 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1117 gen->GetAlgo( this, &algoAssignedTo );
1118 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1119 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1120 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1121 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1122 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1126 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1127 _computeError = _algo->GetComputeError();
1128 _algo->InitComputeError();
1131 bool stateChange = ( _algoState != oldAlgoState );
1133 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1134 algo->SetEventListener( this );
1136 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1139 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1141 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1142 deleteOwnListeners();
1143 SetIsAlwaysComputed( false );
1144 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1145 // restore default states
1146 _algoState = HYP_OK;
1147 _computeState = READY_TO_COMPUTE;
1151 if ( algoRequiringCleaning ) {
1152 // added or removed algo is all-dimensional
1153 ComputeStateEngine( CLEAN );
1154 cleanDependsOn( algoRequiringCleaning );
1155 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1158 if ( stateChange || modifiedHyp )
1159 ComputeStateEngine( MODIF_ALGO_STATE );
1161 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1166 //=======================================================================
1167 //function : IsConform
1168 //purpose : check if a conform mesh will be produced by the Algo
1169 //=======================================================================
1171 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1173 // MESSAGE( "SMESH_subMesh::IsConform" );
1174 if ( !theAlgo ) return false;
1176 // Suppose that theAlgo is applicable to _subShape, do not check it here
1177 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1179 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1180 // on a sub-shape will be ignored by theAlgo
1181 if ( theAlgo->NeedDiscreteBoundary() ||
1182 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1185 SMESH_Gen* gen =_father->GetGen();
1187 // only local algo is to be checked
1188 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1189 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1192 // check algo attached to adjacent shapes
1194 // loop on one level down sub-meshes
1195 TopoDS_Iterator itsub( _subShape );
1196 for (; itsub.More(); itsub.Next())
1198 // loop on adjacent subShapes
1199 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1200 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1202 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1203 if ( _subShape.IsSame( adjacent )) continue;
1204 if ( adjacent.ShapeType() != _subShape.ShapeType())
1207 // check algo attached to smAdjacent
1208 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1210 !algo->NeedDiscreteBoundary() &&
1211 algo->OnlyUnaryInput())
1212 return false; // NOT CONFORM MESH WILL BE PRODUCED
1219 //=============================================================================
1223 //=============================================================================
1225 void SMESH_subMesh::setAlgoState(algo_state state)
1230 //================================================================================
1232 * \brief Send an event to sub-meshes
1233 * \param [in] event - the event
1234 * \param [in] anHyp - an hypothesis
1235 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1236 * reports a fatal result
1237 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1239 * Optional description of a problematic situation (if any) can be retrieved
1240 * via GetComputeError().
1242 //================================================================================
1244 SMESH_Hypothesis::Hypothesis_Status
1245 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1246 SMESH_Hypothesis * anHyp,
1249 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1250 //EAP: a wire (dim==1) should notify edges (dim==1)
1251 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1252 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1254 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1255 while ( smIt->more() ) {
1256 SMESH_subMesh* sm = smIt->next();
1257 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1261 _computeError = sm->_computeError;
1262 sm->_computeError.reset();
1263 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1271 //================================================================================
1273 * \brief Remove elements from sub-meshes.
1274 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1275 * causes the cleaning.
1277 //================================================================================
1279 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1281 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1282 /*complexShapeFirst=*/true);
1283 if ( _father->NbNodes() == 0 )
1285 while ( smIt->more() )
1286 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1288 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1290 while ( smIt->more() )
1291 smIt->next()->ComputeStateEngine(CLEAN);
1293 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1295 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
1297 // find sub-meshes to keep elements on
1298 set< SMESH_subMesh* > smToKeep;
1299 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1300 bool toKeepPrevShapeType = false;
1301 while ( smIt->more() )
1303 SMESH_subMesh* sm = smIt->next();
1304 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1305 if ( !sm->IsEmpty() )
1307 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1308 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1309 if ( !sameShapeType )
1311 // check if the algo allows presence of global algos of dimension the algo
1312 // can generate it-self
1313 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1314 keepSubMeshes = algoRequiringCleaning->NeedLowerHyps( shapeDim );
1315 prevShapeType = sm->GetSubShape().ShapeType();
1316 toKeepPrevShapeType = keepSubMeshes;
1318 if ( !keepSubMeshes )
1320 // look for an algo assigned to sm
1321 bool algoFound = false;
1322 const list<const SMESHDS_Hypothesis*>& hyps = meshDS->GetHypothesis( sm->_subShape );
1323 list<const SMESHDS_Hypothesis*>::const_iterator h = hyps.begin();
1324 for ( ; ( !algoFound && h != hyps.end() ); ++h )
1325 algoFound = ((*h)->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
1326 keepSubMeshes = algoFound;
1328 // remember all sub-meshes of sm
1329 if ( keepSubMeshes )
1331 SMESH_subMeshIteratorPtr smIt2 = getDependsOnIterator(false,true);
1332 while ( smIt2->more() )
1333 smToKeep.insert( smIt2->next() );
1338 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1339 while ( smIt->more() )
1341 SMESH_subMesh* sm = smIt->next();
1342 if ( !smToKeep.count( sm ))
1343 sm->ComputeStateEngine(CLEAN);
1348 //=============================================================================
1352 //=============================================================================
1354 void SMESH_subMesh::DumpAlgoState(bool isMain)
1356 // if (dim < 1) return;
1359 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1361 map < int, SMESH_subMesh * >::const_iterator itsub;
1362 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1364 SMESH_subMesh *sm = (*itsub).second;
1365 sm->DumpAlgoState(false);
1368 //int type = _subShape.ShapeType();
1369 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1370 " type of shape " << _subShape.ShapeType());
1374 MESSAGE(" AlgoState = NO_ALGO");
1377 MESSAGE(" AlgoState = MISSING_HYP");
1380 MESSAGE(" AlgoState = HYP_OK");
1383 switch (_computeState)
1386 MESSAGE(" ComputeState = NOT_READY");
1388 case READY_TO_COMPUTE:
1389 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1392 MESSAGE(" ComputeState = COMPUTE_OK");
1394 case FAILED_TO_COMPUTE:
1395 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1400 //================================================================================
1402 * \brief Remove nodes and elements bound to submesh
1403 * \param subMesh - submesh containing nodes and elements
1405 //================================================================================
1407 static void cleanSubMesh( SMESH_subMesh * subMesh )
1410 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1411 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1412 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1413 while (ite->more()) {
1414 const SMDS_MeshElement * elt = ite->next();
1415 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1416 //meshDS->RemoveElement(elt);
1417 meshDS->RemoveFreeElement(elt, 0);
1420 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1421 while (itn->more()) {
1422 const SMDS_MeshNode * node = itn->next();
1423 //MESSAGE( " RM node: "<<node->GetID());
1424 if ( node->NbInverseElements() == 0 )
1425 meshDS->RemoveFreeNode(node, 0);
1426 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1427 meshDS->RemoveNode(node);
1434 //=============================================================================
1438 //=============================================================================
1440 bool SMESH_subMesh::ComputeStateEngine(int event)
1443 case MODIF_ALGO_STATE:
1445 case COMPUTE_SUBMESH:
1446 //case COMPUTE_CANCELED:
1448 //case SUBMESH_COMPUTED:
1449 //case SUBMESH_RESTORED:
1450 //case SUBMESH_LOADED:
1451 //case MESH_ENTITY_REMOVED:
1452 //case CHECK_COMPUTE_STATE:
1453 _computeError.reset(); break;
1457 if ( event == CLEAN )
1458 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1460 if (_subShape.ShapeType() == TopAbs_VERTEX)
1462 _computeState = READY_TO_COMPUTE;
1463 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1464 if ( smDS && smDS->NbNodes() )
1466 if ( event == CLEAN ) {
1468 cleanSubMesh( this );
1471 _computeState = COMPUTE_OK;
1473 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1474 && !_alwaysComputed )
1476 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1477 gp_Pnt P = BRep_Tool::Pnt(V);
1478 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1479 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1480 _computeState = COMPUTE_OK;
1483 if ( event == MODIF_ALGO_STATE )
1487 SMESH_Gen *gen = _father->GetGen();
1488 SMESH_Algo *algo = 0;
1490 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1491 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1493 switch (_computeState)
1496 // ----------------------------------------------------------------------
1501 case MODIF_ALGO_STATE:
1503 if (algo && !algo->NeedDiscreteBoundary())
1504 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1505 if ( _algoState == HYP_OK )
1506 _computeState = READY_TO_COMPUTE;
1508 case COMPUTE: // nothing to do
1509 case COMPUTE_SUBMESH:
1511 case COMPUTE_CANCELED: // nothing to do
1515 removeSubMeshElementsAndNodes();
1517 case SUBMESH_COMPUTED: // nothing to do
1519 case SUBMESH_RESTORED:
1520 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1522 case MESH_ENTITY_REMOVED:
1524 case SUBMESH_LOADED:
1525 loadDependentMeshes();
1526 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1528 case CHECK_COMPUTE_STATE:
1529 if ( IsMeshComputed() )
1530 _computeState = COMPUTE_OK;
1538 // ----------------------------------------------------------------------
1540 case READY_TO_COMPUTE:
1543 case MODIF_ALGO_STATE:
1544 _computeState = NOT_READY;
1548 if (!algo->NeedDiscreteBoundary())
1549 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1550 if ( _algoState == HYP_OK )
1551 _computeState = READY_TO_COMPUTE;
1555 case COMPUTE_SUBMESH:
1559 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1562 MESSAGE("***** verify compute state *****");
1563 _computeState = NOT_READY;
1564 setAlgoState(MISSING_HYP);
1567 TopoDS_Shape shape = _subShape;
1568 algo->SubMeshesToCompute().assign( 1, this );
1569 // check submeshes needed
1570 if (_father->HasShapeToMesh() ) {
1571 bool subComputed = false, subFailed = false;
1572 if (!algo->OnlyUnaryInput()) {
1573 if ( event == COMPUTE /*&&
1574 ( algo->NeedDiscreteBoundary() || algo->SupportSubmeshes() )*/)
1575 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1577 subComputed = SubMeshesComputed( & subFailed );
1580 subComputed = SubMeshesComputed();
1582 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1583 algo->SupportSubmeshes() ? !subFailed :
1584 ( !subComputed || _father->IsNotConformAllowed() ));
1587 _computeState = FAILED_TO_COMPUTE;
1588 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1590 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1591 "Unexpected computed sub-mesh",algo);
1597 // to restore cout that may be redirected by algo
1598 std::streambuf* coutBuffer = std::cout.rdbuf();
1600 //cleanDependants(); for "UseExisting_*D" algos
1601 //removeSubMeshElementsAndNodes();
1602 loadDependentMeshes();
1604 _computeState = FAILED_TO_COMPUTE;
1605 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1609 algo->InitComputeError();
1611 MemoryReserve aMemoryReserve;
1612 SMDS_Mesh::CheckMemory();
1613 Kernel_Utils::Localizer loc;
1614 if ( !_father->HasShapeToMesh() ) // no shape
1616 SMESH_MesherHelper helper( *_father );
1617 helper.SetSubShape( shape );
1618 helper.SetElementsOnShape( true );
1619 ret = algo->Compute(*_father, &helper );
1623 ret = algo->Compute((*_father), shape);
1625 // algo can set _computeError of submesh
1626 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1628 catch ( ::SMESH_ComputeError& comperr ) {
1629 cout << " SMESH_ComputeError caught" << endl;
1630 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1631 *_computeError = comperr;
1633 catch ( std::bad_alloc& exc ) {
1634 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1635 if ( _computeError ) {
1636 _computeError->myName = COMPERR_MEMORY_PB;
1637 //_computeError->myComment = exc.what();
1639 cleanSubMesh( this );
1642 catch ( Standard_OutOfMemory& exc ) {
1643 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1644 if ( _computeError ) {
1645 _computeError->myName = COMPERR_MEMORY_PB;
1646 //_computeError->myComment = exc.what();
1648 cleanSubMesh( this );
1649 throw std::bad_alloc();
1651 catch (Standard_Failure& ex) {
1652 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1653 _computeError->myName = COMPERR_OCC_EXCEPTION;
1654 _computeError->myComment += ex.DynamicType()->Name();
1655 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1656 _computeError->myComment += ": ";
1657 _computeError->myComment += ex.GetMessageString();
1660 catch ( SALOME_Exception& S_ex ) {
1661 const int skipSalomeShift = 7; /* to skip "Salome " of
1662 "Salome Exception" prefix returned
1663 by SALOME_Exception::what() */
1664 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1665 _computeError->myName = COMPERR_SLM_EXCEPTION;
1666 _computeError->myComment = S_ex.what() + skipSalomeShift;
1668 catch ( std::exception& exc ) {
1669 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1670 _computeError->myName = COMPERR_STD_EXCEPTION;
1671 _computeError->myComment = exc.what();
1674 if ( _computeError )
1675 _computeError->myName = COMPERR_EXCEPTION;
1679 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1681 // check if an error reported on any sub-shape
1682 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1683 if ( isComputeErrorSet )
1685 // check if anything was built
1686 TopExp_Explorer subS(shape, _subShape.ShapeType());
1689 for (; ret && subS.More(); subS.Next())
1690 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1691 ( _subShape.ShapeType() != TopAbs_EDGE ||
1692 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1695 // Set _computeError
1696 if (!ret && !isComputeErrorSet)
1698 for (subS.ReInit(); subS.More(); subS.Next())
1700 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1701 if ( !sm->IsMeshComputed() )
1703 if ( !sm->_computeError )
1704 sm->_computeError = SMESH_ComputeError::New();
1705 if ( sm->_computeError->IsOK() )
1706 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1707 sm->_computeState = FAILED_TO_COMPUTE;
1708 sm->_computeError->myAlgo = algo;
1712 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1714 _computeError.reset();
1717 // send event SUBMESH_COMPUTED
1719 if ( !algo->NeedDiscreteBoundary() )
1720 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1721 for (subS.ReInit(); subS.More(); subS.Next())
1723 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1724 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1725 while ( smIt->more() ) {
1727 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1728 sm->updateDependantsState( SUBMESH_COMPUTED );
1734 updateDependantsState( SUBMESH_COMPUTED );
1738 case COMPUTE_CANCELED: // nothing to do
1742 removeSubMeshElementsAndNodes();
1743 _computeState = NOT_READY;
1747 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1749 _computeState = READY_TO_COMPUTE;
1751 setAlgoState(MISSING_HYP);
1754 case SUBMESH_COMPUTED: // nothing to do
1756 case SUBMESH_RESTORED:
1757 // check if a mesh is already computed that may
1758 // happen after retrieval from a file
1759 ComputeStateEngine( CHECK_COMPUTE_STATE );
1760 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1762 if (algo) algo->SubmeshRestored( this );
1764 case MESH_ENTITY_REMOVED:
1766 case SUBMESH_LOADED:
1767 loadDependentMeshes();
1768 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1770 case CHECK_COMPUTE_STATE:
1771 if ( IsMeshComputed() )
1772 _computeState = COMPUTE_OK;
1773 else if ( _computeError && _computeError->IsKO() )
1774 _computeState = FAILED_TO_COMPUTE;
1782 // ----------------------------------------------------------------------
1787 case MODIF_ALGO_STATE:
1788 ComputeStateEngine( CLEAN );
1790 if (algo && !algo->NeedDiscreteBoundary())
1791 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1793 case COMPUTE: // nothing to do
1795 case COMPUTE_CANCELED: // nothing to do
1798 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1799 removeSubMeshElementsAndNodes();
1800 _computeState = NOT_READY;
1801 if ( _algoState == HYP_OK )
1802 _computeState = READY_TO_COMPUTE;
1804 case SUBMESH_COMPUTED: // nothing to do
1806 case SUBMESH_RESTORED:
1807 ComputeStateEngine( CHECK_COMPUTE_STATE );
1808 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1810 if (algo) algo->SubmeshRestored( this );
1812 case MESH_ENTITY_REMOVED:
1813 updateDependantsState ( CHECK_COMPUTE_STATE );
1814 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1815 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1817 case CHECK_COMPUTE_STATE:
1818 if ( !IsMeshComputed() ) {
1819 if (_algoState == HYP_OK)
1820 _computeState = READY_TO_COMPUTE;
1822 _computeState = NOT_READY;
1825 case SUBMESH_LOADED:
1826 // already treated event, thanks to which _computeState == COMPUTE_OK
1834 // ----------------------------------------------------------------------
1836 case FAILED_TO_COMPUTE:
1839 case MODIF_ALGO_STATE:
1841 ComputeStateEngine( CLEAN );
1843 if (algo && !algo->NeedDiscreteBoundary())
1844 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1845 if (_algoState == HYP_OK)
1846 _computeState = READY_TO_COMPUTE;
1848 _computeState = NOT_READY;
1850 case COMPUTE: // nothing to do
1851 case COMPUTE_SUBMESH:
1853 case COMPUTE_CANCELED:
1856 algo->CancelCompute();
1860 cleanDependants(); // submeshes dependent on me should be cleaned as well
1861 removeSubMeshElementsAndNodes();
1863 case SUBMESH_COMPUTED: // allow retry compute
1864 if (_algoState == HYP_OK)
1865 _computeState = READY_TO_COMPUTE;
1867 _computeState = NOT_READY;
1869 case SUBMESH_RESTORED:
1870 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1872 case MESH_ENTITY_REMOVED:
1874 case CHECK_COMPUTE_STATE:
1875 if ( IsMeshComputed() )
1876 _computeState = COMPUTE_OK;
1878 if (_algoState == HYP_OK)
1879 _computeState = READY_TO_COMPUTE;
1881 _computeState = NOT_READY;
1883 // case SUBMESH_LOADED:
1891 // ----------------------------------------------------------------------
1897 notifyListenersOnEvent( event, COMPUTE_EVENT );
1903 //=============================================================================
1907 //=============================================================================
1909 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1911 _computeError.reset();
1915 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1916 vector<int> aVec(SMDSEntity_Last,0);
1917 aVec[SMDSEntity_Node] = 1;
1918 aResMap.insert(make_pair(this,aVec));
1922 //SMESH_Gen *gen = _father->GetGen();
1923 SMESH_Algo *algo = 0;
1924 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1927 if(algo && !aResMap.count(this) )
1929 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1930 if (!ret) return false;
1932 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary())
1934 // check submeshes needed
1935 bool subMeshEvaluated = true;
1936 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1937 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1938 while ( smIt->more() && subMeshEvaluated )
1940 SMESH_subMesh* sm = smIt->next();
1941 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1942 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1943 const vector<int> & nbs = aResMap[ sm ];
1944 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1946 if ( !subMeshEvaluated )
1949 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1950 ret = algo->Evaluate((*_father), _subShape, aResMap);
1952 aResMap.insert( make_pair( this,vector<int>(0)));
1959 //=======================================================================
1961 * \brief Update compute_state by _computeError and send proper events to
1962 * dependent submeshes
1963 * \retval bool - true if _computeError is NOT set
1965 //=======================================================================
1967 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1968 const bool theComputeOK,
1969 const TopoDS_Shape& theShape)
1971 bool noErrors = true;
1973 if ( !theShape.IsNull() )
1975 // Check state of submeshes
1976 if ( !theAlgo->NeedDiscreteBoundary())
1978 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1979 while ( smIt->more() )
1980 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1984 // Check state of neighbours
1985 if ( !theAlgo->OnlyUnaryInput() &&
1986 theShape.ShapeType() == TopAbs_COMPOUND &&
1987 !theShape.IsSame( _subShape ))
1989 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1990 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1992 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1994 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
2001 // Set my _computeState
2003 if ( !_computeError || _computeError->IsOK() )
2005 // no error description is set to this sub-mesh, check if any mesh is computed
2006 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
2007 if ( _computeState != COMPUTE_OK )
2009 if ( _subShape.ShapeType() == TopAbs_EDGE &&
2010 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
2011 _computeState = COMPUTE_OK;
2012 else if ( theComputeOK )
2013 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
2017 if ( _computeError && !_computeError->IsOK() )
2019 if ( !_computeError->myAlgo )
2020 _computeError->myAlgo = theAlgo;
2024 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
2025 if (_computeError->IsCommon() )
2026 text << _computeError->CommonName();
2028 text << _computeError->myName;
2029 if ( _computeError->myComment.size() > 0 )
2030 text << " \"" << _computeError->myComment << "\"";
2034 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
2042 //=======================================================================
2043 //function : updateSubMeshState
2045 //=======================================================================
2047 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
2049 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
2050 while ( smIt->more() )
2051 smIt->next()->_computeState = theState;
2054 //=======================================================================
2055 //function : ComputeSubMeshStateEngine
2057 //=======================================================================
2059 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
2061 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
2062 while ( smIt->more() )
2063 smIt->next()->ComputeStateEngine(event);
2066 //=======================================================================
2067 //function : updateDependantsState
2069 //=======================================================================
2071 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
2073 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2074 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2076 ancestors[ iA ]->ComputeStateEngine( theEvent );
2080 //=============================================================================
2084 //=============================================================================
2086 void SMESH_subMesh::cleanDependants()
2088 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2090 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2091 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2093 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2094 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2096 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2097 // will erase mesh on other shapes in a compound
2098 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2099 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2100 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2105 //=============================================================================
2109 //=============================================================================
2111 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2113 cleanSubMesh( this );
2115 // algo may bind a submesh not to _subShape, eg 3D algo
2116 // sets nodes on SHELL while _subShape may be SOLID
2118 int dim = SMESH_Gen::GetShapeDim( _subShape );
2119 int type = _subShape.ShapeType() + 1;
2120 for ( ; type <= TopAbs_EDGE; type++) {
2121 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2123 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2124 for ( ; exp.More(); exp.Next() )
2125 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2132 //=======================================================================
2133 //function : getCollection
2134 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2135 // meshed at once along with _subShape
2136 //=======================================================================
2138 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2139 SMESH_Algo* theAlgo,
2140 bool & theSubComputed,
2141 bool & theSubFailed,
2142 std::vector<SMESH_subMesh*>& theSubs)
2144 theSubComputed = SubMeshesComputed( & theSubFailed );
2146 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2148 if ( mainShape.IsSame( _subShape ))
2151 const bool skipAuxHyps = false;
2152 list<const SMESHDS_Hypothesis*> aUsedHyp =
2153 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2155 // put in a compound all shapes with the same hypothesis assigned
2156 // and a good ComputeState
2158 TopoDS_Compound aCompound;
2159 BRep_Builder aBuilder;
2160 aBuilder.MakeCompound( aCompound );
2164 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2165 while ( smIt->more() )
2167 SMESH_subMesh* subMesh = smIt->next();
2168 const TopoDS_Shape& S = subMesh->_subShape;
2169 if ( S.ShapeType() != this->_subShape.ShapeType() )
2171 theSubs.push_back( subMesh );
2172 if ( subMesh == this )
2174 aBuilder.Add( aCompound, S );
2176 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2178 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2179 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2180 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == aUsedHyp )) // same hyps
2182 aBuilder.Add( aCompound, S );
2183 if ( !subMesh->SubMeshesComputed() )
2184 theSubComputed = false;
2192 //=======================================================================
2193 //function : getSimilarAttached
2194 //purpose : return a hypothesis attached to theShape.
2195 // If theHyp is provided, similar but not same hypotheses
2196 // is returned; else only applicable ones having theHypType
2198 //=======================================================================
2200 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2201 const SMESH_Hypothesis * theHyp,
2202 const int theHypType)
2204 SMESH_HypoFilter hypoKind;
2205 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2207 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2208 hypoKind.AndNot( hypoKind.Is( theHyp ));
2209 if ( theHyp->IsAuxiliary() )
2210 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2212 hypoKind.AndNot( hypoKind.IsAuxiliary());
2215 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2218 return _father->GetHypothesis( theShape, hypoKind, false );
2221 //=======================================================================
2222 //function : CheckConcurentHypothesis
2223 //purpose : check if there are several applicable hypothesis attached to
2225 //=======================================================================
2227 SMESH_Hypothesis::Hypothesis_Status
2228 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
2230 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
2232 // is there local hypothesis on me?
2233 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2234 return SMESH_Hypothesis::HYP_OK;
2237 TopoDS_Shape aPrevWithHyp;
2238 const SMESH_Hypothesis* aPrevHyp = 0;
2239 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2240 for (; it.More(); it.Next())
2242 const TopoDS_Shape& ancestor = it.Value();
2243 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2246 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2248 aPrevWithHyp = ancestor;
2251 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2252 return SMESH_Hypothesis::HYP_CONCURENT;
2254 return SMESH_Hypothesis::HYP_OK;
2257 return SMESH_Hypothesis::HYP_OK;
2260 //================================================================================
2262 * \brief Constructor of OwnListenerData
2264 //================================================================================
2266 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2268 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2269 mySubMeshID( sm ? sm->GetId() : -1 ),
2274 //================================================================================
2276 * \brief Sets an event listener and its data to a submesh
2277 * \param listener - the listener to store
2278 * \param data - the listener data to store
2279 * \param where - the submesh to store the listener and it's data
2281 * It remembers the submesh where it puts the listener in order to delete
2282 * them when HYP_OK algo_state is lost
2283 * After being set, event listener is notified on each event of where submesh.
2285 //================================================================================
2287 void SMESH_subMesh::SetEventListener(EventListener* listener,
2288 EventListenerData* data,
2289 SMESH_subMesh* where)
2291 if ( listener && where ) {
2292 where->setEventListener( listener, data );
2293 _ownListeners.push_back( OwnListenerData( where, listener ));
2297 //================================================================================
2299 * \brief Sets an event listener and its data to a submesh
2300 * \param listener - the listener to store
2301 * \param data - the listener data to store
2303 * After being set, event listener is notified on each event of a submesh.
2305 //================================================================================
2307 void SMESH_subMesh::setEventListener(EventListener* listener,
2308 EventListenerData* data)
2310 map< EventListener*, EventListenerData* >::iterator l_d =
2311 _eventListeners.find( listener );
2312 if ( l_d != _eventListeners.end() ) {
2313 EventListenerData* curData = l_d->second;
2314 if ( curData && curData != data && curData->IsDeletable() )
2320 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2321 if ( listener->GetName() == l_d->first->GetName() )
2323 EventListenerData* curData = l_d->second;
2324 if ( curData && curData != data && curData->IsDeletable() )
2326 if ( l_d->first != listener && l_d->first->IsDeletable() )
2328 _eventListeners.erase( l_d );
2331 _eventListeners.insert( make_pair( listener, data ));
2335 //================================================================================
2337 * \brief Return an event listener data
2338 * \param listener - the listener whose data is
2339 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2340 * else returns a listener listening to events of this sub-mesh
2341 * \retval EventListenerData* - found data, maybe NULL
2343 //================================================================================
2345 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2346 const bool myOwn) const
2350 list< OwnListenerData >::const_iterator d;
2351 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2353 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2354 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2359 map< EventListener*, EventListenerData* >::const_iterator l_d =
2360 _eventListeners.find( listener );
2361 if ( l_d != _eventListeners.end() )
2367 //================================================================================
2369 * \brief Return an event listener data
2370 * \param listenerName - the listener name
2371 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2372 * else returns a listener listening to events of this sub-mesh
2373 * \retval EventListenerData* - found data, maybe NULL
2375 //================================================================================
2377 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2378 const bool myOwn) const
2382 list< OwnListenerData >::const_iterator d;
2383 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2385 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2386 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2391 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2392 for ( ; l_d != _eventListeners.end(); ++l_d )
2393 if ( listenerName == l_d->first->GetName() )
2399 //================================================================================
2401 * \brief Notify stored event listeners on the occured event
2402 * \param event - algo_event or compute_event itself
2403 * \param eventType - algo_event or compute_event
2404 * \param hyp - hypothesis, if eventType is algo_event
2406 //================================================================================
2408 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2409 const event_type eventType,
2410 SMESH_Hypothesis* hyp)
2412 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2413 _eventListeners.end());
2414 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2415 for ( ; l_d != eventListeners.end(); ++l_d )
2417 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2418 if ( !_eventListeners.count( li_da.first )) continue;
2420 if ( li_da.first->myBusySM.insert( this ).second )
2422 const bool isDeletable = li_da.first->IsDeletable();
2424 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2426 if ( !isDeletable || _eventListeners.count( li_da.first ))
2427 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2432 //================================================================================
2434 * \brief Unregister the listener and delete listener's data
2435 * \param listener - the event listener
2437 //================================================================================
2439 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2441 map< EventListener*, EventListenerData* >::iterator l_d =
2442 _eventListeners.find( listener );
2443 if ( l_d != _eventListeners.end() && l_d->first )
2445 if ( l_d->second && l_d->second->IsDeletable() )
2449 l_d->first->myBusySM.erase( this );
2450 if ( l_d->first->IsDeletable() )
2452 l_d->first->BeforeDelete( this, l_d->second );
2455 _eventListeners.erase( l_d );
2459 //================================================================================
2461 * \brief Delete event listeners depending on algo of this submesh
2463 //================================================================================
2465 void SMESH_subMesh::deleteOwnListeners()
2467 list< OwnListenerData >::iterator d;
2468 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2470 if ( !_father->MeshExists( d->myMeshID ))
2472 if ( _father->GetId() == d->myMeshID && !_father->GetSubMeshContaining( d->mySubMeshID ))
2474 d->mySubMesh->DeleteEventListener( d->myListener );
2476 _ownListeners.clear();
2479 //=======================================================================
2480 //function : loadDependentMeshes
2481 //purpose : loads dependent meshes on SUBMESH_LOADED event
2482 //=======================================================================
2484 void SMESH_subMesh::loadDependentMeshes()
2486 list< OwnListenerData >::iterator d;
2487 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2488 if ( _father != d->mySubMesh->_father )
2489 d->mySubMesh->_father->Load();
2491 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2492 // for ( ; l_d != _eventListeners.end(); ++l_d )
2493 // if ( l_d->second )
2495 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2496 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2497 // for ( ; sm != smList.end(); ++sm )
2498 // if ( _father != (*sm)->_father )
2499 // (*sm)->_father->Load();
2503 //================================================================================
2505 * \brief Do something on a certain event
2506 * \param event - algo_event or compute_event itself
2507 * \param eventType - algo_event or compute_event
2508 * \param subMesh - the submesh where the event occures
2509 * \param data - listener data stored in the subMesh
2510 * \param hyp - hypothesis, if eventType is algo_event
2512 * The base implementation translates CLEAN event to the subMesh
2513 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2514 * successful COMPUTE event.
2516 //================================================================================
2518 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2519 const int eventType,
2520 SMESH_subMesh* subMesh,
2521 EventListenerData* data,
2522 const SMESH_Hypothesis* /*hyp*/)
2524 if ( data && !data->mySubMeshes.empty() &&
2525 eventType == SMESH_subMesh::COMPUTE_EVENT)
2527 ASSERT( data->mySubMeshes.front() != subMesh );
2528 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2529 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2531 case SMESH_subMesh::CLEAN:
2532 for ( ; smIt != smEnd; ++ smIt)
2533 (*smIt)->ComputeStateEngine( event );
2535 case SMESH_subMesh::COMPUTE:
2536 case SMESH_subMesh::COMPUTE_SUBMESH:
2537 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2538 for ( ; smIt != smEnd; ++ smIt)
2539 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2548 //================================================================================
2550 * \brief Iterator over submeshes and optionally prepended or appended one
2552 //================================================================================
2554 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2556 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2557 SMESH_subMesh* prepend,
2558 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2560 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2561 if ( myCur == append ) append = 0;
2563 /// Return true if and only if there are other object in this iterator
2568 /// Return the current object and step to the next one
2569 virtual SMESH_subMesh* next()
2571 SMESH_subMesh* res = myCur;
2572 if ( myIt->more() ) { myCur = myIt->next(); }
2573 else { myCur = myAppend; myAppend = 0; }
2580 SMESH_subMesh *myAppend, *myCur;
2581 SMDS_Iterator<SMESH_subMesh*> *myIt;
2585 //================================================================================
2587 * \brief Return iterator on the submeshes this one depends on
2588 * \param includeSelf - this submesh to be returned also
2589 * \param reverse - if true, complex shape submeshes go first
2591 //================================================================================
2593 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2594 const bool reverse) const
2596 SMESH_subMesh *me = (SMESH_subMesh*) this;
2597 SMESH_subMesh *prepend=0, *append=0;
2598 if ( includeSelf ) {
2599 if ( reverse ) prepend = me;
2602 typedef map < int, SMESH_subMesh * > TMap;
2605 return SMESH_subMeshIteratorPtr
2606 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2609 return SMESH_subMeshIteratorPtr
2610 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2614 //================================================================================
2616 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2618 //================================================================================
2620 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2622 if ( _ancestors.empty() &&
2623 !_subShape.IsSame( _father->GetShapeToMesh() ))
2625 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2627 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2628 me->_ancestors.reserve( ancShapes.Extent() );
2630 TopTools_MapOfShape map;
2632 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2633 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2634 if ( map.Add( it.Value() ))
2635 me->_ancestors.push_back( sm );
2641 //================================================================================
2643 * \brief Clears the vector of ancestor sub-meshes
2645 //================================================================================
2647 void SMESH_subMesh::ClearAncestors()
2652 //================================================================================
2654 * \brief Find common submeshes (based on shared sub-shapes with other
2655 * \param theOther submesh to check
2656 * \param theSetOfCommon set of common submesh
2658 //================================================================================
2660 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2661 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2663 int oldNb = theSetOfCommon.size();
2664 // check main submeshes
2665 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2666 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2667 theSetOfCommon.insert( this );
2668 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2669 theSetOfCommon.insert( theOther );
2670 // check common submeshes
2671 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2672 for( ; mapIt != _mapDepend.end(); mapIt++ )
2673 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2674 theSetOfCommon.insert( (*mapIt).second );
2675 return oldNb < theSetOfCommon.size();