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 const SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS() const
154 return ((SMESH_subMesh*) this )->GetSubMeshDS();
157 //=============================================================================
161 //=============================================================================
163 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
165 if ( !GetSubMeshDS() ) {
166 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
167 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
169 return GetSubMeshDS();
172 //=============================================================================
176 //=============================================================================
178 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
180 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
181 while ( smIt->more() ) {
182 SMESH_subMesh *sm = smIt->next();
183 if ( sm->GetComputeState() == READY_TO_COMPUTE )
186 return 0; // nothing to compute
189 //================================================================================
191 * \brief Returns a current algorithm
193 //================================================================================
195 SMESH_Algo* SMESH_subMesh::GetAlgo() const
197 return _father->GetGen()->GetAlgo(*_father, _subShape);
200 //================================================================================
202 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
203 * none mesh entity is bound to it (PAL13615, 2nd part)
205 //================================================================================
207 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
209 _alwaysComputed = isAlCo;
210 if ( _alwaysComputed )
211 _computeState = COMPUTE_OK;
213 ComputeStateEngine( CHECK_COMPUTE_STATE );
216 //=======================================================================
218 * \brief Return true if no mesh entities is bound to the submesh
220 //=======================================================================
222 bool SMESH_subMesh::IsEmpty() const
224 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
225 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
229 //=======================================================================
230 //function : IsMeshComputed
231 //purpose : check if _subMeshDS contains mesh elements
232 //=======================================================================
234 bool SMESH_subMesh::IsMeshComputed() const
236 if ( _alwaysComputed )
238 // algo may bind a submesh not to _subShape, eg 3D algo
239 // sets nodes on SHELL while _subShape may be SOLID
241 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
242 int dim = SMESH_Gen::GetShapeDim( _subShape );
243 int type = _subShape.ShapeType();
244 for ( ; type <= TopAbs_VERTEX; type++) {
245 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
247 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
248 for ( ; exp.More(); exp.Next() )
250 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
252 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
265 //=============================================================================
269 //=============================================================================
271 bool SMESH_subMesh::subMeshesComputed()
273 int myDim = SMESH_Gen::GetShapeDim( _subShape );
274 int dimToCheck = myDim - 1;
275 bool subMeshesComputed = true;
276 // check subMeshes with upper dimension => reverse iteration
277 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
278 while ( smIt->more() )
280 SMESH_subMesh *sm = smIt->next();
281 if ( sm->_alwaysComputed )
283 const TopoDS_Shape & ss = sm->GetSubShape();
284 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
285 // in checking of existence of edges if the algo needs only faces. Moreover,
286 // degenerated edges may have no submesh, as after computing NETGEN_2D.
287 int dim = SMESH_Gen::GetShapeDim( ss );
288 if (dim < dimToCheck)
289 break; // the rest subMeshes are all of less dimension
290 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
291 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
292 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
295 int type = ss.ShapeType();
297 subMeshesComputed = false;
301 case TopAbs_COMPOUND:
303 MESSAGE("The not computed sub mesh is a COMPOUND");
306 case TopAbs_COMPSOLID:
308 MESSAGE("The not computed sub mesh is a COMPSOLID");
313 MESSAGE("The not computed sub mesh is a SHEL");
318 MESSAGE("The not computed sub mesh is a WIRE");
323 MESSAGE("The not computed sub mesh is a SOLID");
328 MESSAGE("The not computed sub mesh is a FACE");
333 MESSAGE("The not computed sub mesh is a EDGE");
338 MESSAGE("The not computed sub mesh is of unknown type");
346 return subMeshesComputed;
349 //=============================================================================
353 //=============================================================================
355 // bool SMESH_subMesh::SubMeshesReady()
357 // bool subMeshesReady = true;
358 // SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
359 // while ( smIt->more() ) {
360 // SMESH_subMesh *sm = smIt->next();
361 // bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
362 // sm->GetComputeState() == READY_TO_COMPUTE);
365 // subMeshesReady = false;
366 // SCRUTE(sm->GetId());
370 // return subMeshesReady;
373 //=============================================================================
375 * Construct dependence on first level subMeshes. complex shapes (compsolid,
376 * shell, wire) are not analysed the same way as simple shapes (solid, face,
378 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
379 * with possible multiples occurences. Multiples occurences corresponds to
380 * internal frontiers within shapes of the collection and must not be keeped.
381 * See FinalizeDependence.
383 //=============================================================================
385 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
387 if (_dependenceAnalysed)
390 //MESSAGE("SMESH_subMesh::DependsOn");
392 int type = _subShape.ShapeType();
396 case TopAbs_COMPOUND:
398 //MESSAGE("compound");
399 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();exp.Next())
401 insertDependence(exp.Current());
403 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More(); exp.Next())
405 if ( BRep_Tool::IsClosed(exp.Current() ))
406 insertDependence(exp.Current()); //only shell not in solid
408 for (TopExp_Explorer expF(exp.Current(), TopAbs_FACE); expF.More();expF.Next())
409 insertDependence(expF.Current()); // issue 0020959: HEXA_3D fails on shell
412 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();exp.Next())
414 insertDependence(exp.Current());
416 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();exp.Next())
418 insertDependence(exp.Current());
420 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX, TopAbs_EDGE); exp.More();exp.Next())
422 insertDependence(exp.Current());
426 case TopAbs_COMPSOLID:
428 //MESSAGE("compsolid");
429 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More(); exp.Next())
431 insertDependence(exp.Current());
438 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More(); exp.Next())
440 insertDependence(exp.Current());
447 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More(); exp.Next())
449 insertDependence(exp.Current());
456 if(_father->HasShapeToMesh()) {
457 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();exp.Next())
459 insertDependence(exp.Current());
467 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();exp.Next())
469 insertDependence(exp.Current());
476 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More(); exp.Next())
478 insertDependence(exp.Current());
491 _dependenceAnalysed = true;
495 //=============================================================================
497 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
499 //=============================================================================
501 void SMESH_subMesh::insertDependence(const TopoDS_Shape aSubShape)
503 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
504 int type = aSubShape.ShapeType();
505 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
506 int cle = aSubMesh->GetId();
507 cle += 10000000 * ordType; // sort map by ordType then index
508 if ( _mapDepend.find( cle ) == _mapDepend.end())
510 _mapDepend[cle] = aSubMesh;
511 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
512 _mapDepend.insert( subMap.begin(), subMap.end() );
516 //=============================================================================
520 //=============================================================================
522 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
524 //MESSAGE("SMESH_subMesh::GetSubShape");
529 //=======================================================================
530 //function : CanAddHypothesis
531 //purpose : return true if theHypothesis can be attached to me:
532 // its dimention is checked
533 //=======================================================================
535 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
537 int aHypDim = theHypothesis->GetDim();
538 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
539 // issue 21106. Forbid 3D mesh on the SHELL
540 // if (aHypDim == 3 && aShapeDim == 3) {
541 // // check case of open shell
542 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
543 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
546 if ( aHypDim <= aShapeDim )
552 //=======================================================================
553 //function : IsApplicableHypotesis
555 //=======================================================================
557 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
558 const TopAbs_ShapeEnum theShapeType)
560 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
563 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
564 // issue 21106. Forbid 3D mesh on the SHELL
565 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
571 switch ( theShapeType ) {
576 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
579 // Special case for algorithms, building 2D mesh on a whole shell.
580 // Before this fix there was a problem after restoring from study,
581 // because in that case algorithm is assigned before hypothesis
582 // (on shell in problem case) and hypothesis is checked on faces
583 // (because it is 2D), where we have NO_ALGO state.
584 // Now 2D hypothesis is also applicable to shells.
585 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
588 // case TopAbs_COMPSOLID:
589 // case TopAbs_COMPOUND:
595 //=============================================================================
599 //=============================================================================
601 SMESH_Hypothesis::Hypothesis_Status
602 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
604 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
605 //SCRUTE(_algoState);
608 // **** les retour des evenement shape sont significatifs
609 // (add ou remove fait ou non)
610 // le retour des evenement father n'indiquent pas que add ou remove fait
612 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
614 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
615 //SMESH_Gen* gen =_father->GetGen();
616 SMESH_Algo* algo = 0;
618 if (_subShape.ShapeType() == TopAbs_VERTEX )
620 if ( anHyp->GetDim() != 0) {
621 if (event == ADD_HYP || event == ADD_ALGO)
622 return SMESH_Hypothesis::HYP_BAD_DIM;
624 return SMESH_Hypothesis::HYP_OK;
627 else if ( _algoState == HYP_OK ) {
628 // update default _algoState
629 if ( event != REMOVE_FATHER_ALGO )
631 _algoState = NO_ALGO;
634 _algoState = MISSING_HYP;
635 if ( event == REMOVE_FATHER_HYP ||
636 algo->CheckHypothesis(*_father,_subShape, aux_ret))
643 int oldAlgoState = _algoState;
644 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
645 bool needFullClean = false;
647 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
649 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
651 // -------------------------------------------
652 // check if a shape needed by algo is present
653 // -------------------------------------------
654 algo = static_cast< SMESH_Algo* >( anHyp );
655 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
656 return SMESH_Hypothesis::HYP_NEED_SHAPE;
657 // ----------------------
658 // check mesh conformity
659 // ----------------------
660 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
661 return SMESH_Hypothesis::HYP_NOTCONFORM;
663 // check if all-dimensional algo is hidden by other local one
664 if ( event == ADD_ALGO ) {
665 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
666 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
667 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
668 if ( SMESH_Algo * curAlgo = (SMESH_Algo*) _father->GetHypothesis( _subShape, filter, true ))
669 needFullClean = ( !curAlgo->NeedDiscreteBoundary() );
673 // ----------------------------------
674 // add a hypothesis to DS if possible
675 // ----------------------------------
676 if (event == ADD_HYP || event == ADD_ALGO)
678 if ( ! CanAddHypothesis( anHyp )) // check dimension
679 return SMESH_Hypothesis::HYP_BAD_DIM;
681 if ( /*!anHyp->IsAuxiliary() &&*/ getSimilarAttached( _subShape, anHyp ) )
682 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
684 if ( !meshDS->AddHypothesis(_subShape, anHyp))
685 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
688 // --------------------------
689 // remove a hypothesis from DS
690 // --------------------------
691 if (event == REMOVE_HYP || event == REMOVE_ALGO)
693 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
694 return SMESH_Hypothesis::HYP_OK; // nothing changes
696 if (event == REMOVE_ALGO)
698 algo = dynamic_cast<SMESH_Algo*> (anHyp);
699 if (!algo->NeedDiscreteBoundary())
701 // clean all mesh in the tree of the current submesh;
702 // we must perform it now because later
703 // we will have no information about the type of the removed algo
704 needFullClean = true;
709 // ------------------
710 // analyse algo state
711 // ------------------
712 if (!isApplicableHyp)
713 return ret; // not applicable hypotheses do not change algo state
718 // ----------------------------------------------------------------------
727 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
728 setAlgoState(HYP_OK);
729 else if ( algo->IsStatusFatal( aux_ret )) {
730 meshDS->RemoveHypothesis(_subShape, anHyp);
734 setAlgoState(MISSING_HYP);
741 case ADD_FATHER_ALGO: { // Algo just added in father
744 if ( algo == anHyp ) {
745 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
746 setAlgoState(HYP_OK);
748 setAlgoState(MISSING_HYP);
752 case REMOVE_FATHER_HYP:
754 case REMOVE_FATHER_ALGO: {
758 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
759 setAlgoState(HYP_OK);
761 setAlgoState(MISSING_HYP);
765 case MODIF_HYP: break;
772 // ----------------------------------------------------------------------
780 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
781 setAlgoState(HYP_OK);
782 if (SMESH_Hypothesis::IsStatusFatal( ret ))
783 meshDS->RemoveHypothesis(_subShape, anHyp);
784 else if (!_father->IsUsedHypothesis( anHyp, this ))
786 meshDS->RemoveHypothesis(_subShape, anHyp);
787 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
791 case ADD_ALGO: { //already existing algo : on father ?
794 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
795 setAlgoState(HYP_OK);
796 else if ( algo->IsStatusFatal( aux_ret )) {
797 meshDS->RemoveHypothesis(_subShape, anHyp);
801 setAlgoState(MISSING_HYP);
806 case REMOVE_ALGO: { // perhaps a father algo applies ?
808 if (algo == NULL) // no more algo applying on sub-shape...
810 setAlgoState(NO_ALGO);
814 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
815 setAlgoState(HYP_OK);
817 setAlgoState(MISSING_HYP);
821 case MODIF_HYP: // assigned hypothesis value may become good
822 case ADD_FATHER_HYP: {
825 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
826 setAlgoState(HYP_OK);
828 setAlgoState(MISSING_HYP);
831 case ADD_FATHER_ALGO: { // new father algo
834 if ( algo == anHyp ) {
835 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
836 setAlgoState(HYP_OK);
838 setAlgoState(MISSING_HYP);
842 case REMOVE_FATHER_HYP: // nothing to do
844 case REMOVE_FATHER_ALGO: {
846 if (algo == NULL) // no more applying algo on father
848 setAlgoState(NO_ALGO);
852 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
853 setAlgoState(HYP_OK);
855 setAlgoState(MISSING_HYP);
865 // ----------------------------------------------------------------------
873 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
875 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
876 // ret should be fatal: anHyp was not added
877 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
879 else if (!_father->IsUsedHypothesis( anHyp, this ))
880 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
882 if (SMESH_Hypothesis::IsStatusFatal( ret ))
884 MESSAGE("do not add extra hypothesis");
885 meshDS->RemoveHypothesis(_subShape, anHyp);
893 case ADD_ALGO: { //already existing algo : on father ?
895 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
896 // check if algo changes
898 f.Init( SMESH_HypoFilter::IsAlgo() );
899 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
900 f.AndNot( SMESH_HypoFilter::Is( algo ));
901 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
903 string(algo->GetName()) != string(prevAlgo->GetName()) )
907 setAlgoState(MISSING_HYP);
913 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
914 setAlgoState(HYP_OK);
916 setAlgoState(MISSING_HYP);
920 case REMOVE_ALGO: { // perhaps a father algo applies ?
922 if (algo == NULL) // no more algo applying on sub-shape...
924 setAlgoState(NO_ALGO);
928 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
929 // check if algo remains
930 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
934 setAlgoState(MISSING_HYP);
938 case MODIF_HYP: // hypothesis value may become bad
939 case ADD_FATHER_HYP: { // new father hypothesis ?
942 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
944 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
948 setAlgoState(MISSING_HYP);
951 case ADD_FATHER_ALGO: {
953 if ( algo == anHyp ) { // a new algo on father
954 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
955 // check if algo changes
957 f.Init( SMESH_HypoFilter::IsAlgo() );
958 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
959 f.AndNot( SMESH_HypoFilter::Is( algo ));
960 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
962 string(algo->GetName()) != string(prevAlgo->GetName()) )
966 setAlgoState(MISSING_HYP);
970 case REMOVE_FATHER_HYP: {
973 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
974 // is there the same local hyp or maybe a new father algo applied?
975 if ( !getSimilarAttached( _subShape, anHyp ) )
979 setAlgoState(MISSING_HYP);
982 case REMOVE_FATHER_ALGO: {
983 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
984 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
985 algo = dynamic_cast<SMESH_Algo*> (anHyp);
986 if (!algo->NeedDiscreteBoundary())
987 needFullClean = true;
990 if (algo == NULL) // no more applying algo on father
992 setAlgoState(NO_ALGO);
996 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
997 // check if algo changes
998 if ( string(algo->GetName()) != string( anHyp->GetName()) )
1002 setAlgoState(MISSING_HYP);
1012 // ----------------------------------------------------------------------
1019 // detect algorithm hiding
1021 if ( ret == SMESH_Hypothesis::HYP_OK &&
1022 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
1023 algo->GetName() == anHyp->GetName() )
1026 SMESH_Gen* gen = _father->GetGen();
1027 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1028 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
1029 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
1030 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1031 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1034 if ( ret == SMESH_Hypothesis::HYP_OK &&
1035 !algo->NeedDiscreteBoundary() &&
1036 !algo->SupportSubmeshes()) {
1037 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1038 gen->GetAlgo( *_father, _subShape, &algoAssignedTo );
1039 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1040 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1041 if ( gen->GetAlgo( *_father, i_sm->second->_subShape, &otherAssignedTo ) &&
1042 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1043 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1047 bool stateChange = ( _algoState != oldAlgoState );
1049 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1050 algo->SetEventListener( this );
1052 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1054 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1055 deleteOwnListeners();
1056 SetIsAlwaysComputed( false );
1057 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1058 // restore default states
1059 _algoState = HYP_OK;
1060 _computeState = READY_TO_COMPUTE;
1064 if ( needFullClean ) {
1065 // added or removed algo is all-dimensional
1066 ComputeStateEngine( CLEAN );
1068 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1071 if (stateChange || modifiedHyp)
1072 ComputeStateEngine(MODIF_ALGO_STATE);
1077 //=======================================================================
1078 //function : IsConform
1079 //purpose : check if a conform mesh will be produced by the Algo
1080 //=======================================================================
1082 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1084 // MESSAGE( "SMESH_subMesh::IsConform" );
1085 if ( !theAlgo ) return false;
1087 // Suppose that theAlgo is applicable to _subShape, do not check it here
1088 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1090 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1091 // on a sub-shape will be ignored by theAlgo
1092 if ( theAlgo->NeedDiscreteBoundary() ||
1093 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1096 SMESH_Gen* gen =_father->GetGen();
1098 // only local algo is to be checked
1099 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1100 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1103 // check algo attached to adjacent shapes
1105 // loop on one level down sub-meshes
1106 TopoDS_Iterator itsub( _subShape );
1107 for (; itsub.More(); itsub.Next())
1109 // loop on adjacent subShapes
1110 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1111 for (; it.More(); it.Next())
1113 const TopoDS_Shape& adjacent = it.Value();
1114 if ( _subShape.IsSame( adjacent )) continue;
1115 if ( adjacent.ShapeType() != _subShape.ShapeType())
1118 // check algo attached to smAdjacent
1119 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1121 !algo->NeedDiscreteBoundary() &&
1122 algo->OnlyUnaryInput())
1123 return false; // NOT CONFORM MESH WILL BE PRODUCED
1130 //=============================================================================
1134 //=============================================================================
1136 void SMESH_subMesh::setAlgoState(algo_state state)
1141 //=============================================================================
1145 //=============================================================================
1146 SMESH_Hypothesis::Hypothesis_Status
1147 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1148 SMESH_Hypothesis * anHyp)
1150 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1151 //EAP: a wire (dim==1) should notify edges (dim==1)
1152 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1153 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1155 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1156 while ( smIt->more() ) {
1157 SMESH_Hypothesis::Hypothesis_Status ret2 =
1158 smIt->next()->AlgoStateEngine(event, anHyp);
1166 //=============================================================================
1170 //=============================================================================
1172 void SMESH_subMesh::cleanDependsOn()
1174 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1175 while ( smIt->more() )
1176 smIt->next()->ComputeStateEngine(CLEAN);
1179 //=============================================================================
1183 //=============================================================================
1185 void SMESH_subMesh::DumpAlgoState(bool isMain)
1187 int dim = SMESH_Gen::GetShapeDim(_subShape);
1188 // if (dim < 1) return;
1191 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1193 map < int, SMESH_subMesh * >::const_iterator itsub;
1194 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1196 SMESH_subMesh *sm = (*itsub).second;
1197 sm->DumpAlgoState(false);
1200 int type = _subShape.ShapeType();
1201 MESSAGE("dim = " << dim << " type of shape " << type);
1205 MESSAGE(" AlgoState = NO_ALGO");
1208 MESSAGE(" AlgoState = MISSING_HYP");
1211 MESSAGE(" AlgoState = HYP_OK");
1214 switch (_computeState)
1217 MESSAGE(" ComputeState = NOT_READY");
1219 case READY_TO_COMPUTE:
1220 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1223 MESSAGE(" ComputeState = COMPUTE_OK");
1225 case FAILED_TO_COMPUTE:
1226 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1231 //================================================================================
1233 * \brief Remove nodes and elements bound to submesh
1234 * \param subMesh - submesh containing nodes and elements
1236 //================================================================================
1238 static void cleanSubMesh( SMESH_subMesh * subMesh )
1241 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1242 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1243 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1244 while (ite->more()) {
1245 const SMDS_MeshElement * elt = ite->next();
1246 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1247 //meshDS->RemoveElement(elt);
1248 meshDS->RemoveFreeElement(elt, subMeshDS);
1251 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1252 while (itn->more()) {
1253 const SMDS_MeshNode * node = itn->next();
1254 //MESSAGE( " RM node: "<<node->GetID());
1255 if ( node->NbInverseElements() == 0 )
1256 meshDS->RemoveFreeNode(node, subMeshDS);
1257 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1258 meshDS->RemoveNode(node);
1264 //=============================================================================
1268 //=============================================================================
1270 bool SMESH_subMesh::ComputeStateEngine(int event)
1273 case MODIF_ALGO_STATE:
1275 //case COMPUTE_CANCELED:
1277 //case SUBMESH_COMPUTED:
1278 //case SUBMESH_RESTORED:
1279 //case SUBMESH_LOADED:
1280 //case MESH_ENTITY_REMOVED:
1281 //case CHECK_COMPUTE_STATE:
1282 _computeError.reset(); break;
1286 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1287 //SCRUTE(_computeState);
1290 if (_subShape.ShapeType() == TopAbs_VERTEX)
1292 _computeState = READY_TO_COMPUTE;
1293 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1294 if ( smDS && smDS->NbNodes() ) {
1295 if ( event == CLEAN ) {
1297 cleanSubMesh( this );
1300 _computeState = COMPUTE_OK;
1302 else if ( event == COMPUTE && !_alwaysComputed ) {
1303 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1304 gp_Pnt P = BRep_Tool::Pnt(V);
1305 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1306 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1307 _computeState = COMPUTE_OK;
1310 if ( event == MODIF_ALGO_STATE )
1314 SMESH_Gen *gen = _father->GetGen();
1315 SMESH_Algo *algo = 0;
1317 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1318 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1320 switch (_computeState)
1323 // ----------------------------------------------------------------------
1328 case MODIF_ALGO_STATE:
1330 if (algo && !algo->NeedDiscreteBoundary())
1331 cleanDependsOn(); // clean sub-meshes with event CLEAN
1332 if ( _algoState == HYP_OK )
1333 _computeState = READY_TO_COMPUTE;
1335 case COMPUTE: // nothing to do
1337 #ifdef WITH_SMESH_CANCEL_COMPUTE
1338 case COMPUTE_CANCELED: // nothing to do
1343 removeSubMeshElementsAndNodes();
1345 case SUBMESH_COMPUTED: // nothing to do
1347 case SUBMESH_RESTORED:
1348 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1350 case MESH_ENTITY_REMOVED:
1352 case SUBMESH_LOADED:
1353 loadDependentMeshes();
1354 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1356 case CHECK_COMPUTE_STATE:
1357 if ( IsMeshComputed() )
1358 _computeState = COMPUTE_OK;
1366 // ----------------------------------------------------------------------
1368 case READY_TO_COMPUTE:
1371 case MODIF_ALGO_STATE:
1372 _computeState = NOT_READY;
1376 if (!algo->NeedDiscreteBoundary())
1377 cleanDependsOn(); // clean sub-meshes with event CLEAN
1378 if ( _algoState == HYP_OK )
1379 _computeState = READY_TO_COMPUTE;
1386 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1389 MESSAGE("***** verify compute state *****");
1390 _computeState = NOT_READY;
1391 setAlgoState(MISSING_HYP);
1394 TopoDS_Shape shape = _subShape;
1395 // check submeshes needed
1396 if (_father->HasShapeToMesh() ) {
1397 bool subComputed = false;
1398 if (!algo->OnlyUnaryInput())
1399 shape = getCollection( gen, algo, subComputed );
1401 subComputed = subMeshesComputed();
1402 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1403 algo->SupportSubmeshes() ? true :
1404 ( !subComputed || _father->IsNotConformAllowed() ));
1406 _computeState = FAILED_TO_COMPUTE;
1407 if ( !algo->NeedDiscreteBoundary() )
1409 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1410 "Unexpected computed submesh",algo);
1416 //cleanDependants(); for "UseExisting_*D" algos
1417 //removeSubMeshElementsAndNodes();
1418 loadDependentMeshes();
1420 _computeState = FAILED_TO_COMPUTE;
1421 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1423 #if OCC_VERSION_LARGE > 0x06010000
1426 algo->InitComputeError();
1427 MemoryReserve aMemoryReserve;
1428 SMDS_Mesh::CheckMemory();
1429 Kernel_Utils::Localizer loc;
1430 if ( !_father->HasShapeToMesh() ) // no shape
1432 SMESH_MesherHelper helper( *_father );
1433 helper.SetSubShape( shape );
1434 helper.SetElementsOnShape( true );
1435 ret = algo->Compute(*_father, &helper );
1439 ret = algo->Compute((*_father), shape);
1441 if ( !_computeError || ( !ret && _computeError->IsOK() ) ) // algo can set _computeError of submesh
1442 _computeError = algo->GetComputeError();
1444 catch ( ::SMESH_ComputeError& comperr ) {
1445 cout << " SMESH_ComputeError caught" << endl;
1446 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1447 *_computeError = comperr;
1449 catch ( std::bad_alloc& exc ) {
1450 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1451 if ( _computeError ) {
1452 _computeError->myName = COMPERR_MEMORY_PB;
1453 //_computeError->myComment = exc.what();
1455 cleanSubMesh( this );
1458 catch ( Standard_OutOfMemory& exc ) {
1459 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1460 if ( _computeError ) {
1461 _computeError->myName = COMPERR_MEMORY_PB;
1462 //_computeError->myComment = exc.what();
1464 cleanSubMesh( this );
1465 throw std::bad_alloc();
1467 catch (Standard_Failure& ex) {
1468 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1469 _computeError->myName = COMPERR_OCC_EXCEPTION;
1470 _computeError->myComment += ex.DynamicType()->Name();
1471 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1472 _computeError->myComment += ": ";
1473 _computeError->myComment += ex.GetMessageString();
1476 catch ( SALOME_Exception& S_ex ) {
1477 const int skipSalomeShift = 7; /* to skip "Salome " of
1478 "Salome Exception" prefix returned
1479 by SALOME_Exception::what() */
1480 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1481 _computeError->myName = COMPERR_SLM_EXCEPTION;
1482 _computeError->myComment = S_ex.what() + skipSalomeShift;
1484 catch ( std::exception& exc ) {
1485 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1486 _computeError->myName = COMPERR_STD_EXCEPTION;
1487 _computeError->myComment = exc.what();
1490 if ( _computeError )
1491 _computeError->myName = COMPERR_EXCEPTION;
1495 // check if an error reported on any sub-shape
1496 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1497 // check if anything was built
1498 TopExp_Explorer subS(shape, _subShape.ShapeType());
1501 for (; ret && subS.More(); subS.Next())
1502 ret = _father->GetSubMesh( subS.Current() )->IsMeshComputed();
1504 // Set _computeError
1505 if (!ret && !isComputeErrorSet)
1507 for (subS.ReInit(); subS.More(); subS.Next())
1509 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1510 if ( !sm->IsMeshComputed() )
1512 if ( !sm->_computeError )
1513 sm->_computeError = SMESH_ComputeError::New();
1514 if ( sm->_computeError->IsOK() )
1515 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1516 sm->_computeState = FAILED_TO_COMPUTE;
1517 sm->_computeError->myAlgo = algo;
1521 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1523 _computeError.reset();
1525 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1528 #ifdef WITH_SMESH_CANCEL_COMPUTE
1529 case COMPUTE_CANCELED: // nothing to do
1534 removeSubMeshElementsAndNodes();
1535 _computeState = NOT_READY;
1539 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1541 _computeState = READY_TO_COMPUTE;
1543 setAlgoState(MISSING_HYP);
1546 case SUBMESH_COMPUTED: // nothing to do
1548 case SUBMESH_RESTORED:
1549 // check if a mesh is already computed that may
1550 // happen after retrieval from a file
1551 ComputeStateEngine( CHECK_COMPUTE_STATE );
1552 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1554 if (algo) algo->SubmeshRestored( this );
1556 case MESH_ENTITY_REMOVED:
1558 case SUBMESH_LOADED:
1559 loadDependentMeshes();
1560 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1562 case CHECK_COMPUTE_STATE:
1563 if ( IsMeshComputed() )
1564 _computeState = COMPUTE_OK;
1572 // ----------------------------------------------------------------------
1577 case MODIF_ALGO_STATE:
1578 ComputeStateEngine( CLEAN );
1580 if (algo && !algo->NeedDiscreteBoundary())
1581 cleanDependsOn(); // clean sub-meshes with event CLEAN
1583 case COMPUTE: // nothing to do
1585 #ifdef WITH_SMESH_CANCEL_COMPUTE
1586 case COMPUTE_CANCELED: // nothing to do
1590 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1591 removeSubMeshElementsAndNodes();
1592 _computeState = NOT_READY;
1593 if ( _algoState == HYP_OK )
1594 _computeState = READY_TO_COMPUTE;
1596 case SUBMESH_COMPUTED: // nothing to do
1598 case SUBMESH_RESTORED:
1599 ComputeStateEngine( CHECK_COMPUTE_STATE );
1600 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1602 if (algo) algo->SubmeshRestored( this );
1604 case MESH_ENTITY_REMOVED:
1605 updateDependantsState ( CHECK_COMPUTE_STATE );
1606 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1607 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1609 case CHECK_COMPUTE_STATE:
1610 if ( !IsMeshComputed() ) {
1611 if (_algoState == HYP_OK)
1612 _computeState = READY_TO_COMPUTE;
1614 _computeState = NOT_READY;
1617 case SUBMESH_LOADED:
1618 // already treated event, thanks to which _computeState == COMPUTE_OK
1626 // ----------------------------------------------------------------------
1628 case FAILED_TO_COMPUTE:
1631 case MODIF_ALGO_STATE:
1633 ComputeStateEngine( CLEAN );
1635 if (algo && !algo->NeedDiscreteBoundary())
1636 cleanDependsOn(); // clean sub-meshes with event CLEAN
1637 if (_algoState == HYP_OK)
1638 _computeState = READY_TO_COMPUTE;
1640 _computeState = NOT_READY;
1642 case COMPUTE: // nothing to do
1644 case COMPUTE_CANCELED:
1647 algo->CancelCompute();
1651 cleanDependants(); // submeshes dependent on me should be cleaned as well
1652 removeSubMeshElementsAndNodes();
1654 case SUBMESH_COMPUTED: // allow retry compute
1655 if (_algoState == HYP_OK)
1656 _computeState = READY_TO_COMPUTE;
1658 _computeState = NOT_READY;
1660 case SUBMESH_RESTORED:
1661 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1663 case MESH_ENTITY_REMOVED:
1665 case CHECK_COMPUTE_STATE:
1666 if ( IsMeshComputed() )
1667 _computeState = COMPUTE_OK;
1669 if (_algoState == HYP_OK)
1670 _computeState = READY_TO_COMPUTE;
1672 _computeState = NOT_READY;
1674 // case SUBMESH_LOADED:
1682 // ----------------------------------------------------------------------
1688 notifyListenersOnEvent( event, COMPUTE_EVENT );
1694 //=============================================================================
1698 //=============================================================================
1700 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1702 _computeError.reset();
1706 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1707 vector<int> aVec(SMDSEntity_Last,0);
1708 aVec[SMDSEntity_Node] = 1;
1709 aResMap.insert(make_pair(this,aVec));
1713 //SMESH_Gen *gen = _father->GetGen();
1714 SMESH_Algo *algo = 0;
1715 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1718 if(algo && !aResMap.count(this) )
1720 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1721 if (!ret) return false;
1723 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary())
1725 // check submeshes needed
1726 bool subMeshEvaluated = true;
1727 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1728 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1729 while ( smIt->more() && subMeshEvaluated )
1731 SMESH_subMesh* sm = smIt->next();
1732 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1733 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1734 const vector<int> & nbs = aResMap[ sm ];
1735 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1737 if ( !subMeshEvaluated )
1740 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1741 ret = algo->Evaluate((*_father), _subShape, aResMap);
1743 aResMap.insert( make_pair( this,vector<int>(0)));
1750 //=======================================================================
1752 * \brief Update compute_state by _computeError and send proper events to
1753 * dependent submeshes
1754 * \retval bool - true if _computeError is NOT set
1756 //=======================================================================
1758 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1759 const bool theComputeOK,
1760 const TopoDS_Shape& theShape)
1762 bool noErrors = true;
1764 if ( !theShape.IsNull() )
1766 // Check state of submeshes
1767 if ( !theAlgo->NeedDiscreteBoundary())
1769 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1770 while ( smIt->more() )
1771 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1775 // Check state of neighbours
1776 if ( !theAlgo->OnlyUnaryInput() &&
1777 theShape.ShapeType() == TopAbs_COMPOUND &&
1778 !theShape.IsSame( _subShape ))
1780 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1781 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1783 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1785 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1792 // Set my _computeState
1794 if ( !_computeError || _computeError->IsOK() )
1796 // no error description is set to this sub-mesh, check if any mesh is computed
1797 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1798 if ( _computeState != COMPUTE_OK )
1800 if ( _subShape.ShapeType() == TopAbs_EDGE &&
1801 BRep_Tool::Degenerated( TopoDS::Edge( _subShape )) )
1802 _computeState = COMPUTE_OK;
1803 else if ( theComputeOK )
1804 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1808 if ( _computeError && !_computeError->IsOK() )
1810 if ( !_computeError->myAlgo )
1811 _computeError->myAlgo = theAlgo;
1815 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1816 if (_computeError->IsCommon() )
1817 text << _computeError->CommonName();
1819 text << _computeError->myName;
1820 if ( _computeError->myComment.size() > 0 )
1821 text << " \"" << _computeError->myComment << "\"";
1825 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1833 //=======================================================================
1834 //function : updateSubMeshState
1836 //=======================================================================
1838 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1840 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1841 while ( smIt->more() )
1842 smIt->next()->_computeState = theState;
1845 //=======================================================================
1846 //function : ComputeSubMeshStateEngine
1848 //=======================================================================
1850 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
1852 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
1853 while ( smIt->more() )
1854 smIt->next()->ComputeStateEngine(event);
1857 //=======================================================================
1858 //function : updateDependantsState
1860 //=======================================================================
1862 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
1864 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1865 for (; it.More(); it.Next())
1867 const TopoDS_Shape& ancestor = it.Value();
1868 SMESH_subMesh *aSubMesh =
1869 _father->GetSubMeshContaining(ancestor);
1871 aSubMesh->ComputeStateEngine( theEvent );
1875 //=============================================================================
1879 //=============================================================================
1881 void SMESH_subMesh::cleanDependants()
1883 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1885 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1886 for (; it.More(); it.Next())
1888 const TopoDS_Shape& ancestor = it.Value();
1889 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1890 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1891 // will erase mesh on other shapes in a compound
1892 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1893 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1895 !aSubMesh->IsEmpty() ) // prevent infinite CLEAN via event lesteners
1896 aSubMesh->ComputeStateEngine(CLEAN);
1902 //=============================================================================
1906 //=============================================================================
1908 void SMESH_subMesh::removeSubMeshElementsAndNodes()
1910 cleanSubMesh( this );
1912 // algo may bind a submesh not to _subShape, eg 3D algo
1913 // sets nodes on SHELL while _subShape may be SOLID
1915 int dim = SMESH_Gen::GetShapeDim( _subShape );
1916 int type = _subShape.ShapeType() + 1;
1917 for ( ; type <= TopAbs_EDGE; type++) {
1918 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1920 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1921 for ( ; exp.More(); exp.Next() )
1922 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1929 //=======================================================================
1930 //function : getCollection
1931 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1932 // meshed at once along with _subShape
1933 //=======================================================================
1935 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
1936 SMESH_Algo* theAlgo,
1937 bool & theSubComputed)
1939 theSubComputed = subMeshesComputed();
1941 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1943 if ( mainShape.IsSame( _subShape ))
1946 const bool ignoreAuxiliaryHyps = false;
1947 list<const SMESHDS_Hypothesis*> aUsedHyp =
1948 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1950 // put in a compound all shapes with the same hypothesis assigned
1951 // and a good ComputState
1953 TopoDS_Compound aCompound;
1954 BRep_Builder aBuilder;
1955 aBuilder.MakeCompound( aCompound );
1957 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1958 for ( ; anExplorer.More(); anExplorer.Next() )
1960 const TopoDS_Shape& S = anExplorer.Current();
1961 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1962 if ( subMesh == this )
1964 aBuilder.Add( aCompound, S );
1966 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1968 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1969 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1970 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1971 aBuilder.Add( aCompound, S );
1972 if ( !subMesh->subMeshesComputed() )
1973 theSubComputed = false;
1980 //=======================================================================
1981 //function : getSimilarAttached
1982 //purpose : return a hypothesis attached to theShape.
1983 // If theHyp is provided, similar but not same hypotheses
1984 // is returned; else only applicable ones having theHypType
1986 //=======================================================================
1988 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
1989 const SMESH_Hypothesis * theHyp,
1990 const int theHypType)
1992 SMESH_HypoFilter hypoKind;
1993 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1995 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1996 hypoKind.AndNot( hypoKind.Is( theHyp ));
1997 if ( theHyp->IsAuxiliary() )
1998 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2000 hypoKind.AndNot( hypoKind.IsAuxiliary());
2003 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2006 return _father->GetHypothesis( theShape, hypoKind, false );
2009 //=======================================================================
2010 //function : CheckConcurentHypothesis
2011 //purpose : check if there are several applicable hypothesis attached to
2013 //=======================================================================
2015 SMESH_Hypothesis::Hypothesis_Status
2016 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
2018 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
2020 // is there local hypothesis on me?
2021 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2022 return SMESH_Hypothesis::HYP_OK;
2025 TopoDS_Shape aPrevWithHyp;
2026 const SMESH_Hypothesis* aPrevHyp = 0;
2027 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2028 for (; it.More(); it.Next())
2030 const TopoDS_Shape& ancestor = it.Value();
2031 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2034 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2036 aPrevWithHyp = ancestor;
2039 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2040 return SMESH_Hypothesis::HYP_CONCURENT;
2042 return SMESH_Hypothesis::HYP_OK;
2045 return SMESH_Hypothesis::HYP_OK;
2048 //================================================================================
2050 * \brief Constructor of OwnListenerData
2052 //================================================================================
2054 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2056 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2057 mySubMeshID( sm ? sm->GetId() : -1 ),
2062 //================================================================================
2064 * \brief Sets an event listener and its data to a submesh
2065 * \param listener - the listener to store
2066 * \param data - the listener data to store
2067 * \param where - the submesh to store the listener and it's data
2069 * It remembers the submesh where it puts the listener in order to delete
2070 * them when HYP_OK algo_state is lost
2071 * After being set, event listener is notified on each event of where submesh.
2073 //================================================================================
2075 void SMESH_subMesh::SetEventListener(EventListener* listener,
2076 EventListenerData* data,
2077 SMESH_subMesh* where)
2079 if ( listener && where ) {
2080 where->setEventListener( listener, data );
2081 _ownListeners.push_back( OwnListenerData( where, listener ));
2085 //================================================================================
2087 * \brief Sets an event listener and its data to a submesh
2088 * \param listener - the listener to store
2089 * \param data - the listener data to store
2091 * After being set, event listener is notified on each event of a submesh.
2093 //================================================================================
2095 void SMESH_subMesh::setEventListener(EventListener* listener,
2096 EventListenerData* data)
2098 map< EventListener*, EventListenerData* >::iterator l_d =
2099 _eventListeners.find( listener );
2100 if ( l_d != _eventListeners.end() ) {
2101 EventListenerData* curData = l_d->second;
2102 if ( curData && curData != data && curData->IsDeletable() )
2108 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2109 if ( listener->GetName() == l_d->first->GetName() )
2111 EventListenerData* curData = l_d->second;
2112 if ( curData && curData != data && curData->IsDeletable() )
2114 if ( l_d->first->IsDeletable() )
2116 _eventListeners.erase( l_d );
2119 _eventListeners.insert( make_pair( listener, data ));
2123 //================================================================================
2125 * \brief Return an event listener data
2126 * \param listener - the listener whose data is
2127 * \retval EventListenerData* - found data, maybe NULL
2129 //================================================================================
2131 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
2133 map< EventListener*, EventListenerData* >::const_iterator l_d =
2134 _eventListeners.find( listener );
2135 if ( l_d != _eventListeners.end() )
2140 //================================================================================
2142 * \brief Return an event listener data
2143 * \param listenerName - the listener name
2144 * \retval EventListenerData* - found data, maybe NULL
2146 //================================================================================
2148 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName) const
2150 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2151 for ( ; l_d != _eventListeners.end(); ++l_d )
2152 if ( listenerName == l_d->first->GetName() )
2157 //================================================================================
2159 * \brief Notify stored event listeners on the occured event
2160 * \param event - algo_event or compute_event itself
2161 * \param eventType - algo_event or compute_event
2162 * \param hyp - hypothesis, if eventType is algo_event
2164 //================================================================================
2166 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2167 const event_type eventType,
2168 SMESH_Hypothesis* hyp)
2170 map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2171 for ( ; l_d != _eventListeners.end(); )
2173 std::pair< EventListener*, EventListenerData* > li_da = *l_d++; /* copy to enable removal
2176 its ProcessEvent() */
2177 if ( li_da.first->myBusySM.insert( this ).second )
2179 const size_t nbListenersBefore = _eventListeners.size();
2180 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2181 if ( nbListenersBefore == _eventListeners.size() )
2182 li_da.first->myBusySM.erase( this ); // a listener hopefully not removed
2187 //================================================================================
2189 * \brief Unregister the listener and delete listener's data
2190 * \param listener - the event listener
2192 //================================================================================
2194 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2196 map< EventListener*, EventListenerData* >::iterator l_d =
2197 _eventListeners.find( listener );
2198 if ( l_d != _eventListeners.end() && l_d->first )
2200 if ( l_d->second && l_d->second->IsDeletable() )
2204 l_d->first->myBusySM.erase( this );
2205 if ( l_d->first->IsDeletable() )
2207 l_d->first->BeforeDelete( this, l_d->second );
2210 _eventListeners.erase( l_d );
2214 //================================================================================
2216 * \brief Delete event listeners depending on algo of this submesh
2218 //================================================================================
2220 void SMESH_subMesh::deleteOwnListeners()
2222 list< OwnListenerData >::iterator d;
2223 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2225 if ( !_father->MeshExists( d->myMeshID ))
2227 if ( _father->GetId() == d->myMeshID && !_father->GetSubMeshContaining( d->mySubMeshID ))
2229 d->mySubMesh->DeleteEventListener( d->myListener );
2231 _ownListeners.clear();
2234 //=======================================================================
2235 //function : loadDependentMeshes
2236 //purpose : loads dependent meshes on SUBMESH_LOADED event
2237 //=======================================================================
2239 void SMESH_subMesh::loadDependentMeshes()
2241 list< OwnListenerData >::iterator d;
2242 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2243 if ( _father != d->mySubMesh->_father )
2244 d->mySubMesh->_father->Load();
2246 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2247 // for ( ; l_d != _eventListeners.end(); ++l_d )
2248 // if ( l_d->second )
2250 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2251 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2252 // for ( ; sm != smList.end(); ++sm )
2253 // if ( _father != (*sm)->_father )
2254 // (*sm)->_father->Load();
2258 //================================================================================
2260 * \brief Do something on a certain event
2261 * \param event - algo_event or compute_event itself
2262 * \param eventType - algo_event or compute_event
2263 * \param subMesh - the submesh where the event occures
2264 * \param data - listener data stored in the subMesh
2265 * \param hyp - hypothesis, if eventType is algo_event
2267 * The base implementation translates CLEAN event to the subMesh
2268 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2269 * successful COMPUTE event.
2271 //================================================================================
2273 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2274 const int eventType,
2275 SMESH_subMesh* subMesh,
2276 EventListenerData* data,
2277 const SMESH_Hypothesis* /*hyp*/)
2279 if ( data && !data->mySubMeshes.empty() &&
2280 eventType == SMESH_subMesh::COMPUTE_EVENT)
2282 ASSERT( data->mySubMeshes.front() != subMesh );
2283 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2284 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2286 case SMESH_subMesh::CLEAN:
2287 for ( ; smIt != smEnd; ++ smIt)
2288 (*smIt)->ComputeStateEngine( event );
2290 case SMESH_subMesh::COMPUTE:
2291 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2292 for ( ; smIt != smEnd; ++ smIt)
2293 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2302 //================================================================================
2304 * \brief Iterator over submeshes and optionally prepended or appended one
2306 //================================================================================
2308 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2310 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2311 SMESH_subMesh* prepend,
2312 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2314 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2315 if ( myCur == append ) append = 0;
2317 /// Return true if and only if there are other object in this iterator
2322 /// Return the current object and step to the next one
2323 virtual SMESH_subMesh* next()
2325 SMESH_subMesh* res = myCur;
2326 if ( myIt->more() ) { myCur = myIt->next(); }
2327 else { myCur = myAppend; myAppend = 0; }
2334 SMESH_subMesh *myAppend, *myCur;
2335 SMDS_Iterator<SMESH_subMesh*> *myIt;
2339 //================================================================================
2341 * \brief Return iterator on the submeshes this one depends on
2342 * \param includeSelf - this submesh to be returned also
2343 * \param reverse - if true, complex shape submeshes go first
2345 //================================================================================
2347 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2350 SMESH_subMesh *prepend=0, *append=0;
2351 if ( includeSelf ) {
2352 if ( reverse ) prepend = this;
2355 typedef map < int, SMESH_subMesh * > TMap;
2358 return SMESH_subMeshIteratorPtr
2359 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2362 return SMESH_subMeshIteratorPtr
2363 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));
2367 //================================================================================
2369 * \brief Find common submeshes (based on shared sub-shapes with other
2370 * \param theOther submesh to check
2371 * \param theSetOfCommon set of common submesh
2373 //================================================================================
2375 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2376 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2378 int oldNb = theSetOfCommon.size();
2379 // check main submeshes
2380 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2381 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2382 theSetOfCommon.insert( this );
2383 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2384 theSetOfCommon.insert( theOther );
2385 // check common submeshes
2386 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2387 for( ; mapIt != _mapDepend.end(); mapIt++ )
2388 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2389 theSetOfCommon.insert( (*mapIt).second );
2390 return oldNb < theSetOfCommon.size();