1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 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
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
29 #include "SMESH_subMesh.hxx"
31 #include "SMESH_Algo.hxx"
32 #include "SMESH_Gen.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMESH_Hypothesis.hxx"
35 #include "SMESH_Mesh.hxx"
36 #include "SMESH_MesherHelper.hxx"
37 #include "SMESH_subMeshEventListener.hxx"
38 #include "SMESH_Comment.hxx"
39 #include "SMDS_SetIterator.hxx"
41 #include "utilities.h"
44 #include <BRep_Builder.hxx>
45 #include <BRep_Tool.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopoDS_Compound.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopoDS_Iterator.hxx>
55 #include <Standard_OutOfMemory.hxx>
56 #include <Standard_ErrorHandler.hxx>
60 //=============================================================================
62 * \brief Allocate some memory at construction and release it at destruction.
63 * Is used to be able to continue working after mesh generation breaks due to
66 //=============================================================================
71 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
72 ~MemoryReserve() { delete [] myBuf; }
75 //=============================================================================
77 * default constructor:
79 //=============================================================================
81 SMESH_subMesh::SMESH_subMesh(int Id,
83 SMESHDS_Mesh * meshDS,
84 const TopoDS_Shape & aSubShape)
86 _subShape = aSubShape;
87 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
90 _dependenceAnalysed = _alwaysComputed = false;
92 if (_subShape.ShapeType() == TopAbs_VERTEX)
95 _computeState = READY_TO_COMPUTE;
100 _computeState = NOT_READY;
104 //=============================================================================
108 //=============================================================================
110 SMESH_subMesh::~SMESH_subMesh()
112 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
114 DeleteOwnListeners();
117 //=============================================================================
121 //=============================================================================
123 int SMESH_subMesh::GetId() const
125 //MESSAGE("SMESH_subMesh::GetId");
129 //=============================================================================
133 //=============================================================================
135 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
137 // submesh appears in DS only when a mesher set nodes and elements on a shape
138 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
141 //=============================================================================
145 //=============================================================================
147 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
149 if ( !GetSubMeshDS() ) {
150 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
151 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
153 return GetSubMeshDS();
156 //=============================================================================
160 //=============================================================================
162 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
164 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
165 while ( smIt->more() ) {
166 SMESH_subMesh *sm = smIt->next();
167 if ( sm->GetComputeState() == READY_TO_COMPUTE )
170 return 0; // nothing to compute
173 //================================================================================
175 * \brief Allow algo->Compute() if a subshape of lower dim is meshed but
176 * none mesh entity is bound to it (PAL13615, 2nd part)
178 //================================================================================
180 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
182 _alwaysComputed = isAlCo;
183 if ( _alwaysComputed )
184 _computeState = COMPUTE_OK;
186 ComputeStateEngine( CHECK_COMPUTE_STATE );
189 //=======================================================================
190 //function : IsMeshComputed
191 //purpose : check if _subMeshDS contains mesh elements
192 //=======================================================================
194 bool SMESH_subMesh::IsMeshComputed() const
196 if ( _alwaysComputed )
198 // algo may bind a submesh not to _subShape, eg 3D algo
199 // sets nodes on SHELL while _subShape may be SOLID
201 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
202 int dim = SMESH_Gen::GetShapeDim( _subShape );
203 int type = _subShape.ShapeType();
204 for ( ; type <= TopAbs_VERTEX; type++) {
205 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
207 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
208 for ( ; exp.More(); exp.Next() )
210 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
212 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
225 //=============================================================================
229 //=============================================================================
231 bool SMESH_subMesh::SubMeshesComputed()
233 int myDim = SMESH_Gen::GetShapeDim( _subShape );
234 int dimToCheck = myDim - 1;
235 bool subMeshesComputed = true;
236 // check subMeshes with upper dimension => reverse iteration
237 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
238 while ( smIt->more() )
240 SMESH_subMesh *sm = smIt->next();
241 if ( sm->_alwaysComputed )
243 const TopoDS_Shape & ss = sm->GetSubShape();
244 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
245 // in checking of existence of edges if the algo needs only faces. Moreover,
246 // degenerated edges may have no submesh, as after computing NETGEN_2D.
247 int dim = SMESH_Gen::GetShapeDim( ss );
248 if (dim < dimToCheck)
249 break; // the rest subMeshes are all of less dimension
250 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
251 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
252 (ds && ( ds->NbNodes() || ds->NbElements() )));
255 int type = ss.ShapeType();
257 subMeshesComputed = false;
261 case TopAbs_COMPOUND:
263 MESSAGE("The not computed sub mesh is a COMPOUND");
266 case TopAbs_COMPSOLID:
268 MESSAGE("The not computed sub mesh is a COMPSOLID");
273 MESSAGE("The not computed sub mesh is a SHEL");
278 MESSAGE("The not computed sub mesh is a WIRE");
283 MESSAGE("The not computed sub mesh is a SOLID");
288 MESSAGE("The not computed sub mesh is a FACE");
293 MESSAGE("The not computed sub mesh is a EDGE");
298 MESSAGE("The not computed sub mesh is of unknown type");
306 return subMeshesComputed;
309 //=============================================================================
313 //=============================================================================
315 bool SMESH_subMesh::SubMeshesReady()
317 bool subMeshesReady = true;
318 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
319 while ( smIt->more() ) {
320 SMESH_subMesh *sm = smIt->next();
321 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
322 sm->GetComputeState() == READY_TO_COMPUTE);
325 subMeshesReady = false;
330 return subMeshesReady;
333 //=============================================================================
335 * Construct dependence on first level subMeshes. complex shapes (compsolid,
336 * shell, wire) are not analysed the same way as simple shapes (solid, face,
338 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
339 * with possible multiples occurences. Multiples occurences corresponds to
340 * internal frontiers within shapes of the collection and must not be keeped.
341 * See FinalizeDependence.
343 //=============================================================================
345 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
347 if (_dependenceAnalysed)
350 //MESSAGE("SMESH_subMesh::DependsOn");
352 int type = _subShape.ShapeType();
356 case TopAbs_COMPOUND:
358 //MESSAGE("compound");
359 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
362 InsertDependence(exp.Current());
364 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
367 InsertDependence(exp.Current()); //only shell not in solid
369 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
372 InsertDependence(exp.Current());
374 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
377 InsertDependence(exp.Current());
381 case TopAbs_COMPSOLID:
383 //MESSAGE("compsolid");
384 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
387 InsertDependence(exp.Current());
394 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
397 InsertDependence(exp.Current());
404 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
407 InsertDependence(exp.Current());
414 if(_father->HasShapeToMesh()) {
415 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
418 InsertDependence(exp.Current());
426 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
429 InsertDependence(exp.Current());
436 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
439 InsertDependence(exp.Current());
452 _dependenceAnalysed = true;
456 //=============================================================================
458 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
460 //=============================================================================
462 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
464 //MESSAGE("SMESH_subMesh::InsertDependence");
465 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
466 int type = aSubShape.ShapeType();
467 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
468 int cle = aSubMesh->GetId();
469 cle += 10000000 * ordType; // sort map by ordType then index
470 if ( _mapDepend.find( cle ) == _mapDepend.end())
472 _mapDepend[cle] = aSubMesh;
473 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
474 _mapDepend.insert( subMap.begin(), subMap.end() );
478 //=============================================================================
482 //=============================================================================
484 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
486 //MESSAGE("SMESH_subMesh::GetSubShape");
491 //=======================================================================
492 //function : CanAddHypothesis
493 //purpose : return true if theHypothesis can be attached to me:
494 // its dimention is checked
495 //=======================================================================
497 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
499 int aHypDim = theHypothesis->GetDim();
500 if(_father->HasShapeToMesh()) {
501 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
502 if ( aHypDim <= aShapeDim )
506 //Only 3D hypothesis may be assigned to the mesh w/o geometry
508 // if ( aHypDim < aShapeDim )
509 // return ( _father->IsMainShape( _subShape ));
514 //=======================================================================
515 //function : IsApplicableHypotesis
517 //=======================================================================
519 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
520 const TopAbs_ShapeEnum theShapeType)
522 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
524 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
527 switch ( theShapeType ) {
532 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
535 // Special case for algorithms, building 2D mesh on a whole shell.
536 // Before this fix there was a problem after restoring from study,
537 // because in that case algorithm is assigned before hypothesis
538 // (on shell in problem case) and hypothesis is checked on faces
539 // (because it is 2D), where we have NO_ALGO state.
540 // Now 2D hypothesis is also applicable to shells.
541 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
544 // case TopAbs_COMPSOLID:
545 // case TopAbs_COMPOUND:
551 //=============================================================================
555 //=============================================================================
557 SMESH_Hypothesis::Hypothesis_Status
558 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
560 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
561 //SCRUTE(_algoState);
564 // **** les retour des evenement shape sont significatifs
565 // (add ou remove fait ou non)
566 // le retour des evenement father n'indiquent pas que add ou remove fait
568 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
570 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
571 SMESH_Gen* gen =_father->GetGen();
572 SMESH_Algo* algo = 0;
574 if (_subShape.ShapeType() == TopAbs_VERTEX )
576 if ( anHyp->GetDim() != 0) {
577 if (event == ADD_HYP || event == ADD_ALGO)
578 return SMESH_Hypothesis::HYP_BAD_DIM;
580 return SMESH_Hypothesis::HYP_OK;
583 else if ( _algoState == HYP_OK ) {
584 // update default _algoState
585 if ( event != REMOVE_FATHER_ALGO )
587 _algoState = NO_ALGO;
588 algo = gen->GetAlgo(*_father, _subShape);
590 _algoState = MISSING_HYP;
591 if ( event == REMOVE_FATHER_HYP ||
592 algo->CheckHypothesis(*_father,_subShape, aux_ret))
599 int oldAlgoState = _algoState;
600 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
602 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
604 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
606 // -------------------------------------------
607 // check if a shape needed by algo is present
608 // -------------------------------------------
609 algo = static_cast< SMESH_Algo* >( anHyp );
610 if(_father->GetShapeToMesh() != SMESH_Mesh::PseudoShape())
611 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
612 return SMESH_Hypothesis::HYP_BAD_GEOMETRY;
613 // ----------------------
614 // check mesh conformity
615 // ----------------------
616 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
617 return SMESH_Hypothesis::HYP_NOTCONFORM;
620 // ----------------------------------
621 // add a hypothesis to DS if possible
622 // ----------------------------------
623 if (event == ADD_HYP || event == ADD_ALGO)
625 if ( ! CanAddHypothesis( anHyp )) // check dimension
626 return SMESH_Hypothesis::HYP_BAD_DIM;
628 // EAP: __NOT__ Only NETGEN_3D and GHS3D_3D can be assigned to the Mesh w/o geometryy,
629 // but any algo which !NeedShape()
630 // if(anHyp->GetDim() == 3 && !_father->HasShapeToMesh()
631 // && event == ADD_ALGO) {
632 // //Only NETGEN_3D and GHS3D_3D can be assigned to the Mesh w/o geometryy
633 // bool isNetgen3D = (strcmp( "NETGEN_3D", anHyp->GetName()) == 0);
634 // bool isGhs3d = (strcmp( "GHS3D_3D", anHyp->GetName()) == 0);
635 // if( !isNetgen3D && !isGhs3d)
636 // return SMESH_Hypothesis::HYP_BAD_DIM;
639 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
640 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
642 if ( !meshDS->AddHypothesis(_subShape, anHyp))
643 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
646 // --------------------------
647 // remove a hypothesis from DS
648 // --------------------------
649 if (event == REMOVE_HYP || event == REMOVE_ALGO)
651 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
652 return SMESH_Hypothesis::HYP_OK; // nothing changes
654 if (event == REMOVE_ALGO)
656 algo = dynamic_cast<SMESH_Algo*> (anHyp);
657 if (!algo->NeedDescretBoundary())
659 // clean all mesh in the tree of the current submesh;
660 // we must perform it now because later
661 // we will have no information about the type of the removed algo
663 ComputeStateEngine( CLEAN );
665 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
670 // ------------------
671 // analyse algo state
672 // ------------------
673 if (!isApplicableHyp)
674 return ret; // not applicable hypotheses do not change algo state
679 // ----------------------------------------------------------------------
686 algo = gen->GetAlgo((*_father), _subShape);
688 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
689 SetAlgoState(HYP_OK);
690 else if ( algo->IsStatusFatal( aux_ret )) {
691 meshDS->RemoveHypothesis(_subShape, anHyp);
695 SetAlgoState(MISSING_HYP);
702 case ADD_FATHER_ALGO: { // Algo just added in father
703 algo = gen->GetAlgo((*_father), _subShape);
705 if ( algo == anHyp ) {
706 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
707 SetAlgoState(HYP_OK);
709 SetAlgoState(MISSING_HYP);
713 case REMOVE_FATHER_HYP:
715 case REMOVE_FATHER_ALGO: {
716 algo = gen->GetAlgo((*_father), _subShape);
719 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
720 SetAlgoState(HYP_OK);
722 SetAlgoState(MISSING_HYP);
726 case MODIF_HYP: break;
733 // ----------------------------------------------------------------------
739 algo = gen->GetAlgo((*_father), _subShape);
741 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
742 SetAlgoState(HYP_OK);
743 if (SMESH_Hypothesis::IsStatusFatal( ret ))
744 meshDS->RemoveHypothesis(_subShape, anHyp);
745 else if (!_father->IsUsedHypothesis( anHyp, this ))
747 meshDS->RemoveHypothesis(_subShape, anHyp);
748 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
752 case ADD_ALGO: { //already existing algo : on father ?
753 algo = gen->GetAlgo((*_father), _subShape);
755 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
756 SetAlgoState(HYP_OK);
757 else if ( algo->IsStatusFatal( aux_ret )) {
758 meshDS->RemoveHypothesis(_subShape, anHyp);
762 SetAlgoState(MISSING_HYP);
767 case REMOVE_ALGO: { // perhaps a father algo applies ?
768 algo = gen->GetAlgo((*_father), _subShape);
769 if (algo == NULL) // no more algo applying on subShape...
771 SetAlgoState(NO_ALGO);
775 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
776 SetAlgoState(HYP_OK);
778 SetAlgoState(MISSING_HYP);
782 case MODIF_HYP: // assigned hypothesis value may become good
783 case ADD_FATHER_HYP: {
784 algo = gen->GetAlgo((*_father), _subShape);
786 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
787 SetAlgoState(HYP_OK);
789 SetAlgoState(MISSING_HYP);
792 case ADD_FATHER_ALGO: { // new father algo
793 algo = gen->GetAlgo((*_father), _subShape);
795 if ( algo == anHyp ) {
796 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
797 SetAlgoState(HYP_OK);
799 SetAlgoState(MISSING_HYP);
803 case REMOVE_FATHER_HYP: // nothing to do
805 case REMOVE_FATHER_ALGO: {
806 algo = gen->GetAlgo((*_father), _subShape);
807 if (algo == NULL) // no more applying algo on father
809 SetAlgoState(NO_ALGO);
813 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
814 SetAlgoState(HYP_OK);
816 SetAlgoState(MISSING_HYP);
826 // ----------------------------------------------------------------------
832 algo = gen->GetAlgo((*_father), _subShape);
834 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
836 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
837 // ret should be fatal: anHyp was not added
838 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
840 else if (!_father->IsUsedHypothesis( anHyp, this ))
841 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
843 if (SMESH_Hypothesis::IsStatusFatal( ret ))
845 MESSAGE("do not add extra hypothesis");
846 meshDS->RemoveHypothesis(_subShape, anHyp);
854 case ADD_ALGO: { //already existing algo : on father ?
855 algo = gen->GetAlgo((*_father), _subShape);
856 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
857 // check if algo changes
859 f.Init( SMESH_HypoFilter::IsAlgo() );
860 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
861 f.AndNot( SMESH_HypoFilter::Is( algo ));
862 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
864 string(algo->GetName()) != string(prevAlgo->GetName()) )
868 SetAlgoState(MISSING_HYP);
872 algo = gen->GetAlgo((*_father), _subShape);
874 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
875 SetAlgoState(HYP_OK);
877 SetAlgoState(MISSING_HYP);
881 case REMOVE_ALGO: { // perhaps a father algo applies ?
882 algo = gen->GetAlgo((*_father), _subShape);
883 if (algo == NULL) // no more algo applying on subShape...
885 SetAlgoState(NO_ALGO);
889 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
890 // check if algo remains
891 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
895 SetAlgoState(MISSING_HYP);
899 case MODIF_HYP: // hypothesis value may become bad
900 case ADD_FATHER_HYP: { // new father hypothesis ?
901 algo = gen->GetAlgo((*_father), _subShape);
903 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
905 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
909 SetAlgoState(MISSING_HYP);
912 case ADD_FATHER_ALGO: {
913 algo = gen->GetAlgo((*_father), _subShape);
914 if ( algo == anHyp ) { // a new algo on father
915 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
916 // check if algo changes
918 f.Init( SMESH_HypoFilter::IsAlgo() );
919 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
920 f.AndNot( SMESH_HypoFilter::Is( algo ));
921 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
923 string(algo->GetName()) != string(prevAlgo->GetName()) )
927 SetAlgoState(MISSING_HYP);
931 case REMOVE_FATHER_HYP: {
932 algo = gen->GetAlgo((*_father), _subShape);
934 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
935 // is there the same local hyp or maybe a new father algo applied?
936 if ( !GetSimilarAttached( _subShape, anHyp ) )
940 SetAlgoState(MISSING_HYP);
943 case REMOVE_FATHER_ALGO: {
944 algo = gen->GetAlgo((*_father), _subShape);
945 if (algo == NULL) // no more applying algo on father
947 SetAlgoState(NO_ALGO);
951 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
952 // check if algo changes
953 if ( string(algo->GetName()) != string( anHyp->GetName()) )
957 SetAlgoState(MISSING_HYP);
967 // ----------------------------------------------------------------------
974 // detect algorithm hiding
976 if ( ret == SMESH_Hypothesis::HYP_OK &&
977 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
978 algo->GetName() == anHyp->GetName() )
981 SMESH_Gen* gen = _father->GetGen();
982 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
983 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
984 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
985 if ( !upperAlgo->NeedDescretBoundary() )
986 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
989 if ( ret == SMESH_Hypothesis::HYP_OK && !algo->NeedDescretBoundary() ) {
990 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
991 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
992 if ( gen->GetAlgo( *_father, i_sm->second->_subShape ))
993 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
997 bool stateChange = ( _algoState != oldAlgoState );
999 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1000 algo->SetEventListener( this );
1002 NotifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1004 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1005 DeleteOwnListeners();
1006 SetIsAlwaysComputed( false );
1007 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1008 // restore default states
1009 _algoState = HYP_OK;
1010 _computeState = READY_TO_COMPUTE;
1014 if (stateChange || modifiedHyp)
1015 ComputeStateEngine(MODIF_ALGO_STATE);
1020 //=======================================================================
1021 //function : IsConform
1022 //purpose : check if a conform mesh will be produced by the Algo
1023 //=======================================================================
1025 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1027 // MESSAGE( "SMESH_subMesh::IsConform" );
1028 if ( !theAlgo ) return false;
1030 // Suppose that theAlgo is applicable to _subShape, do not check it here
1031 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1033 // check only algo that doesn't NeedDescretBoundary(): because mesh made
1034 // on a sub-shape will be ignored by theAlgo
1035 if ( theAlgo->NeedDescretBoundary() ||
1036 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1039 SMESH_Gen* gen =_father->GetGen();
1041 // only local algo is to be checked
1042 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1043 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1046 // check algo attached to adjacent shapes
1048 // loop on one level down sub-meshes
1049 TopoDS_Iterator itsub( _subShape );
1050 for (; itsub.More(); itsub.Next())
1052 // loop on adjacent subShapes
1053 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1054 for (; it.More(); it.Next())
1056 const TopoDS_Shape& adjacent = it.Value();
1057 if ( _subShape.IsSame( adjacent )) continue;
1058 if ( adjacent.ShapeType() != _subShape.ShapeType())
1061 // check algo attached to smAdjacent
1062 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1064 !algo->NeedDescretBoundary() &&
1065 algo->OnlyUnaryInput())
1066 return false; // NOT CONFORM MESH WILL BE PRODUCED
1073 //=============================================================================
1077 //=============================================================================
1079 void SMESH_subMesh::SetAlgoState(int state)
1084 //=============================================================================
1088 //=============================================================================
1089 SMESH_Hypothesis::Hypothesis_Status
1090 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1091 SMESH_Hypothesis * anHyp)
1093 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1094 //EAP: a wire (dim==1) should notify edges (dim==1)
1095 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1096 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1098 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1099 while ( smIt->more() ) {
1100 SMESH_Hypothesis::Hypothesis_Status ret2 =
1101 smIt->next()->AlgoStateEngine(event, anHyp);
1109 //=============================================================================
1113 //=============================================================================
1115 void SMESH_subMesh::CleanDependsOn()
1117 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1118 while ( smIt->more() )
1119 smIt->next()->ComputeStateEngine(CLEAN);
1122 //=============================================================================
1126 //=============================================================================
1128 void SMESH_subMesh::DumpAlgoState(bool isMain)
1130 int dim = SMESH_Gen::GetShapeDim(_subShape);
1131 // if (dim < 1) return;
1134 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1136 map < int, SMESH_subMesh * >::const_iterator itsub;
1137 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1139 SMESH_subMesh *sm = (*itsub).second;
1140 sm->DumpAlgoState(false);
1143 int type = _subShape.ShapeType();
1144 MESSAGE("dim = " << dim << " type of shape " << type);
1148 MESSAGE(" AlgoState = NO_ALGO");
1151 MESSAGE(" AlgoState = MISSING_HYP");
1154 MESSAGE(" AlgoState = HYP_OK");
1157 switch (_computeState)
1160 MESSAGE(" ComputeState = NOT_READY");
1162 case READY_TO_COMPUTE:
1163 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1166 MESSAGE(" ComputeState = COMPUTE_OK");
1168 case FAILED_TO_COMPUTE:
1169 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1174 //================================================================================
1176 * \brief Remove nodes and elements bound to submesh
1177 * \param subMesh - submesh containing nodes and elements
1179 //================================================================================
1181 static void cleanSubMesh( SMESH_subMesh * subMesh )
1184 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1185 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1186 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1187 while (ite->more()) {
1188 const SMDS_MeshElement * elt = ite->next();
1189 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1190 //meshDS->RemoveElement(elt);
1191 meshDS->RemoveFreeElement(elt, subMeshDS);
1194 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1195 while (itn->more()) {
1196 const SMDS_MeshNode * node = itn->next();
1197 //MESSAGE( " RM node: "<<node->GetID());
1198 if ( node->NbInverseElements() == 0 )
1199 meshDS->RemoveFreeNode(node, subMeshDS);
1200 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1201 meshDS->RemoveNode(node);
1207 //=============================================================================
1211 //=============================================================================
1213 bool SMESH_subMesh::ComputeStateEngine(int event)
1215 _computeError.reset();
1217 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1218 //SCRUTE(_computeState);
1221 if (_subShape.ShapeType() == TopAbs_VERTEX)
1223 _computeState = READY_TO_COMPUTE;
1224 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1225 if ( smDS && smDS->NbNodes() ) {
1226 if ( event == CLEAN ) // this occures for algo which !NeedDescretBoundary() (PAL19272)
1227 cleanSubMesh( this );
1229 _computeState = COMPUTE_OK;
1231 else if ( event == COMPUTE && !_alwaysComputed ) {
1232 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1233 gp_Pnt P = BRep_Tool::Pnt(V);
1234 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1235 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1236 _computeState = COMPUTE_OK;
1239 if ( event == MODIF_ALGO_STATE )
1243 SMESH_Gen *gen = _father->GetGen();
1244 SMESH_Algo *algo = 0;
1246 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1247 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1249 switch (_computeState)
1252 // ----------------------------------------------------------------------
1257 case MODIF_ALGO_STATE:
1258 algo = gen->GetAlgo((*_father), _subShape);
1259 if (algo && !algo->NeedDescretBoundary())
1260 CleanDependsOn(); // clean sub-meshes with event CLEAN
1261 if ( _algoState == HYP_OK )
1262 _computeState = READY_TO_COMPUTE;
1264 case COMPUTE: // nothing to do
1268 RemoveSubMeshElementsAndNodes();
1270 case SUBMESH_COMPUTED: // nothing to do
1272 case SUBMESH_RESTORED:
1273 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1275 case MESH_ENTITY_REMOVED:
1277 case CHECK_COMPUTE_STATE:
1278 if ( IsMeshComputed() )
1279 _computeState = COMPUTE_OK;
1287 // ----------------------------------------------------------------------
1289 case READY_TO_COMPUTE:
1292 case MODIF_ALGO_STATE:
1293 _computeState = NOT_READY;
1294 algo = gen->GetAlgo((*_father), _subShape);
1297 if (!algo->NeedDescretBoundary())
1298 CleanDependsOn(); // clean sub-meshes with event CLEAN
1299 if ( _algoState == HYP_OK )
1300 _computeState = READY_TO_COMPUTE;
1305 algo = gen->GetAlgo((*_father), _subShape);
1307 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1310 MESSAGE("***** verify compute state *****");
1311 _computeState = NOT_READY;
1312 SetAlgoState(MISSING_HYP);
1315 TopoDS_Shape shape = _subShape;
1316 // check submeshes needed
1317 if (_father->HasShapeToMesh() ) {
1318 bool subComputed = false;
1319 if (!algo->OnlyUnaryInput())
1320 shape = GetCollection( gen, algo, subComputed );
1322 subComputed = SubMeshesComputed();
1323 ret = ( algo->NeedDescretBoundary() ? subComputed :
1324 ( !subComputed || _father->IsNotConformAllowed() ));
1326 _computeState = FAILED_TO_COMPUTE;
1327 if ( !algo->NeedDescretBoundary() )
1329 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1330 "Unexpected computed submesh",algo);
1335 // CleanDependants(); for "UseExisting_*D" algos
1336 // RemoveSubMeshElementsAndNodes();
1338 _computeState = FAILED_TO_COMPUTE;
1339 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1341 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1344 algo->InitComputeError();
1345 MemoryReserve aMemoryReserve;
1346 SMDS_Mesh::CheckMemory();
1347 if ( !_father->HasShapeToMesh() ) // no shape
1349 SMESH_MesherHelper helper( *_father );
1350 helper.SetSubShape( shape );
1351 helper.SetElementsOnShape( true );
1352 ret = algo->Compute(*_father, &helper );
1356 ret = algo->Compute((*_father), shape);
1359 _computeError = algo->GetComputeError();
1361 catch ( std::bad_alloc& exc ) {
1362 printf("std::bad_alloc thrown inside algo->Compute()\n");
1363 if ( _computeError ) {
1364 _computeError->myName = COMPERR_MEMORY_PB;
1365 //_computeError->myComment = exc.what();
1367 cleanSubMesh( this );
1370 catch ( Standard_OutOfMemory& exc ) {
1371 printf("Standard_OutOfMemory thrown inside algo->Compute()\n");
1372 if ( _computeError ) {
1373 _computeError->myName = COMPERR_MEMORY_PB;
1374 //_computeError->myComment = exc.what();
1376 cleanSubMesh( this );
1377 throw std::bad_alloc();
1379 catch (Standard_Failure& ex) {
1380 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1381 _computeError->myName = COMPERR_OCC_EXCEPTION;
1382 _computeError->myComment += ex.DynamicType()->Name();
1383 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1384 _computeError->myComment += ": ";
1385 _computeError->myComment += ex.GetMessageString();
1388 catch ( SALOME_Exception& S_ex ) {
1389 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1390 _computeError->myName = COMPERR_SLM_EXCEPTION;
1391 _computeError->myComment = S_ex.what();
1393 catch ( std::exception& exc ) {
1394 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1395 _computeError->myName = COMPERR_STD_EXCEPTION;
1396 _computeError->myComment = exc.what();
1399 if ( _computeError )
1400 _computeError->myName = COMPERR_EXCEPTION;
1404 if (ret && !_alwaysComputed) { // check if anything was built
1405 ret = ( GetSubMeshDS() && ( GetSubMeshDS()->NbElements() || GetSubMeshDS()->NbNodes() ));
1407 bool isComputeErrorSet = !CheckComputeError( algo, shape );
1408 if (!ret && !isComputeErrorSet)
1410 // Set _computeError
1411 if ( !_computeError )
1412 _computeError = SMESH_ComputeError::New();
1413 if ( _computeError->IsOK() )
1414 _computeError->myName = COMPERR_ALGO_FAILED;
1415 _computeState = FAILED_TO_COMPUTE;
1419 _computeError.reset();
1421 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1426 RemoveSubMeshElementsAndNodes();
1427 _computeState = NOT_READY;
1428 algo = gen->GetAlgo((*_father), _subShape);
1431 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1433 _computeState = READY_TO_COMPUTE;
1435 SetAlgoState(MISSING_HYP);
1438 case SUBMESH_COMPUTED: // nothing to do
1440 case SUBMESH_RESTORED:
1441 // check if a mesh is already computed that may
1442 // happen after retrieval from a file
1443 ComputeStateEngine( CHECK_COMPUTE_STATE );
1444 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1445 algo = gen->GetAlgo(*_father, _subShape);
1446 if (algo) algo->SubmeshRestored( this );
1448 case MESH_ENTITY_REMOVED:
1450 case CHECK_COMPUTE_STATE:
1451 if ( IsMeshComputed() )
1452 _computeState = COMPUTE_OK;
1460 // ----------------------------------------------------------------------
1465 case MODIF_ALGO_STATE:
1466 ComputeStateEngine( CLEAN );
1467 algo = gen->GetAlgo((*_father), _subShape);
1468 if (algo && !algo->NeedDescretBoundary())
1469 CleanDependsOn(); // clean sub-meshes with event CLEAN
1471 case COMPUTE: // nothing to do
1474 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1475 RemoveSubMeshElementsAndNodes();
1476 _computeState = NOT_READY;
1477 if ( _algoState == HYP_OK )
1478 _computeState = READY_TO_COMPUTE;
1480 case SUBMESH_COMPUTED: // nothing to do
1482 case SUBMESH_RESTORED:
1483 ComputeStateEngine( CHECK_COMPUTE_STATE );
1484 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1485 algo = gen->GetAlgo(*_father, _subShape);
1486 if (algo) algo->SubmeshRestored( this );
1488 case MESH_ENTITY_REMOVED:
1489 UpdateDependantsState( CHECK_COMPUTE_STATE );
1490 ComputeStateEngine( CHECK_COMPUTE_STATE );
1491 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1493 case CHECK_COMPUTE_STATE:
1494 if ( !IsMeshComputed() )
1495 if (_algoState == HYP_OK)
1496 _computeState = READY_TO_COMPUTE;
1498 _computeState = NOT_READY;
1506 // ----------------------------------------------------------------------
1508 case FAILED_TO_COMPUTE:
1511 case MODIF_ALGO_STATE:
1512 algo = gen->GetAlgo((*_father), _subShape);
1513 if (algo && !algo->NeedDescretBoundary())
1514 CleanDependsOn(); // clean sub-meshes with event CLEAN
1515 if (_algoState == HYP_OK)
1516 _computeState = READY_TO_COMPUTE;
1518 _computeState = NOT_READY;
1520 case COMPUTE: // nothing to do
1523 CleanDependants(); // submeshes dependent on me should be cleaned as well
1524 RemoveSubMeshElementsAndNodes();
1526 case SUBMESH_COMPUTED: // allow retry compute
1527 if (_algoState == HYP_OK)
1528 _computeState = READY_TO_COMPUTE;
1530 _computeState = NOT_READY;
1532 case SUBMESH_RESTORED:
1533 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1535 case MESH_ENTITY_REMOVED:
1537 case CHECK_COMPUTE_STATE:
1538 if ( IsMeshComputed() )
1539 _computeState = COMPUTE_OK;
1541 if (_algoState == HYP_OK)
1542 _computeState = READY_TO_COMPUTE;
1544 _computeState = NOT_READY;
1552 // ----------------------------------------------------------------------
1558 NotifyListenersOnEvent( event, COMPUTE_EVENT );
1563 //=======================================================================
1565 * \brief Update compute_state by _computeError and send proper events to
1566 * dependent submeshes
1567 * \retval bool - true if _computeError is NOT set
1569 //=======================================================================
1571 bool SMESH_subMesh::CheckComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1573 bool noErrors = true;
1575 if ( !theShape.IsNull() )
1577 // Check state of submeshes
1578 if ( !theAlgo->NeedDescretBoundary())
1580 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1581 while ( smIt->more() )
1582 if ( !smIt->next()->CheckComputeError( theAlgo ))
1586 // Check state of neighbours
1587 if ( !theAlgo->OnlyUnaryInput() &&
1588 theShape.ShapeType() == TopAbs_COMPOUND &&
1589 !theShape.IsSame( _subShape ))
1591 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1592 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1594 if ( !sm->CheckComputeError( theAlgo ))
1596 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1603 if ( !_computeError || _computeError->IsOK() )
1605 _computeState = COMPUTE_OK;
1609 if ( !_computeError->myAlgo )
1610 _computeError->myAlgo = theAlgo;
1614 text << theAlgo->GetName() << " failed on subshape #" << _Id << " with error ";
1615 if (_computeError->IsCommon() )
1616 text << _computeError->CommonName();
1618 text << _computeError->myName;
1619 if ( _computeError->myComment.size() > 0 )
1620 text << " \"" << _computeError->myComment << "\"";
1623 cout << text << endl;
1624 // Show vertices location of a failed shape
1625 TopTools_IndexedMapOfShape vMap;
1626 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1627 cout << "Subshape vertices " << ( vMap.Extent()>10 ? "(first 10):" : ":") << endl;
1628 for ( int iv = 1; iv <= vMap.Extent() && iv < 11; ++iv ) {
1629 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1630 cout << "#" << _father->GetMeshDS()->ShapeToIndex( vMap( iv )) << " ";
1631 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1636 _computeState = FAILED_TO_COMPUTE;
1643 //=======================================================================
1644 //function : ApplyToCollection
1645 //purpose : Apply theAlgo to all subshapes in theCollection
1646 //=======================================================================
1648 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1649 const TopoDS_Shape& theCollection)
1651 MESSAGE("SMESH_subMesh::ApplyToCollection");
1652 ASSERT ( !theAlgo->NeedDescretBoundary() );
1654 if ( _computeError )
1655 _computeError->myName = COMPERR_OK;
1657 bool ok = theAlgo->Compute( *_father, theCollection );
1659 // set _computeState of subshapes
1660 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1661 for ( ; anExplorer.More(); anExplorer.Next() )
1663 if ( SMESH_subMesh* subMesh = _father->GetSubMeshContaining( anExplorer.Current() ))
1665 bool localOK = subMesh->CheckComputeError( theAlgo );
1666 if ( !ok && localOK && !subMesh->IsMeshComputed() )
1668 subMesh->_computeError = theAlgo->GetComputeError();
1669 if ( subMesh->_computeError->IsOK() )
1670 _computeError = SMESH_ComputeError::New(COMPERR_ALGO_FAILED);
1671 localOK = CheckComputeError( theAlgo );
1674 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1675 subMesh->UpdateSubMeshState( localOK ? COMPUTE_OK : FAILED_TO_COMPUTE );
1683 //=======================================================================
1684 //function : UpdateSubMeshState
1686 //=======================================================================
1688 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1690 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1691 while ( smIt->more() )
1692 smIt->next()->_computeState = theState;
1695 //=======================================================================
1696 //function : ComputeSubMeshStateEngine
1698 //=======================================================================
1700 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1702 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1703 while ( smIt->more() )
1704 smIt->next()->ComputeStateEngine(event);
1707 //=======================================================================
1708 //function : UpdateDependantsState
1710 //=======================================================================
1712 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1714 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1715 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1716 for (; it.More(); it.Next())
1718 const TopoDS_Shape& ancestor = it.Value();
1719 SMESH_subMesh *aSubMesh =
1720 _father->GetSubMeshContaining(ancestor);
1722 aSubMesh->ComputeStateEngine( theEvent );
1726 //=============================================================================
1730 //=============================================================================
1732 void SMESH_subMesh::CleanDependants()
1734 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1736 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1737 for (; it.More(); it.Next())
1739 const TopoDS_Shape& ancestor = it.Value();
1740 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1741 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1742 // will erase mesh on other shapes in a compound
1743 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1744 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1746 aSubMesh->ComputeStateEngine(CLEAN);
1752 //=============================================================================
1756 //=============================================================================
1758 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1760 //SCRUTE(_subShape.ShapeType());
1762 cleanSubMesh( this );
1764 // algo may bind a submesh not to _subShape, eg 3D algo
1765 // sets nodes on SHELL while _subShape may be SOLID
1767 int dim = SMESH_Gen::GetShapeDim( _subShape );
1768 int type = _subShape.ShapeType() + 1;
1769 for ( ; type <= TopAbs_EDGE; type++) {
1770 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1772 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1773 for ( ; exp.More(); exp.Next() )
1774 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1781 //=======================================================================
1782 //function : GetCollection
1783 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1784 // meshed at once along with _subShape
1785 //=======================================================================
1787 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen,
1788 SMESH_Algo* theAlgo,
1789 bool & theSubComputed)
1791 MESSAGE("SMESH_subMesh::GetCollection");
1793 theSubComputed = SubMeshesComputed();
1795 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1797 if ( mainShape.IsSame( _subShape ))
1800 const bool ignoreAuxiliaryHyps = false;
1801 list<const SMESHDS_Hypothesis*> aUsedHyp =
1802 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1804 // put in a compound all shapes with the same hypothesis assigned
1805 // and a good ComputState
1807 TopoDS_Compound aCompound;
1808 BRep_Builder aBuilder;
1809 aBuilder.MakeCompound( aCompound );
1811 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1812 for ( ; anExplorer.More(); anExplorer.Next() )
1814 const TopoDS_Shape& S = anExplorer.Current();
1815 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1816 if ( subMesh == this )
1818 aBuilder.Add( aCompound, S );
1820 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1822 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1823 if (anAlgo == theAlgo &&
1824 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp)
1825 aBuilder.Add( aCompound, S );
1826 if ( !subMesh->SubMeshesComputed() )
1827 theSubComputed = false;
1834 //=======================================================================
1835 //function : GetSimilarAttached
1836 //purpose : return a hypothesis attached to theShape.
1837 // If theHyp is provided, similar but not same hypotheses
1838 // is returned; else only applicable ones having theHypType
1840 //=======================================================================
1842 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1843 const SMESH_Hypothesis * theHyp,
1844 const int theHypType)
1846 SMESH_HypoFilter hypoKind;
1847 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1849 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1850 hypoKind.AndNot( hypoKind.Is( theHyp ));
1851 if ( theHyp->IsAuxiliary() )
1852 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1854 hypoKind.AndNot( hypoKind.IsAuxiliary());
1857 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1860 return _father->GetHypothesis( theShape, hypoKind, false );
1863 //=======================================================================
1864 //function : CheckConcurentHypothesis
1865 //purpose : check if there are several applicable hypothesis attached to
1867 //=======================================================================
1869 SMESH_Hypothesis::Hypothesis_Status
1870 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1872 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1874 // is there local hypothesis on me?
1875 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1876 return SMESH_Hypothesis::HYP_OK;
1879 TopoDS_Shape aPrevWithHyp;
1880 const SMESH_Hypothesis* aPrevHyp = 0;
1881 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1882 for (; it.More(); it.Next())
1884 const TopoDS_Shape& ancestor = it.Value();
1885 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1888 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1890 aPrevWithHyp = ancestor;
1893 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1894 return SMESH_Hypothesis::HYP_CONCURENT;
1896 return SMESH_Hypothesis::HYP_OK;
1899 return SMESH_Hypothesis::HYP_OK;
1902 //================================================================================
1904 * \brief Sets an event listener and its data to a submesh
1905 * \param listener - the listener to store
1906 * \param data - the listener data to store
1907 * \param where - the submesh to store the listener and it's data
1908 * \param deleteListener - if true then the listener will be deleted as
1909 * it is removed from where submesh
1911 * It remembers the submesh where it puts the listener in order to delete
1912 * them when HYP_OK algo_state is lost
1913 * After being set, event listener is notified on each event of where submesh.
1915 //================================================================================
1917 void SMESH_subMesh::SetEventListener(EventListener* listener,
1918 EventListenerData* data,
1919 SMESH_subMesh* where)
1921 if ( listener && where ) {
1922 where->SetEventListener( listener, data );
1923 myOwnListeners.push_back( make_pair( where, listener ));
1927 //================================================================================
1929 * \brief Sets an event listener and its data to a submesh
1930 * \param listener - the listener to store
1931 * \param data - the listener data to store
1933 * After being set, event listener is notified on each event of a submesh.
1935 //================================================================================
1937 void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
1939 map< EventListener*, EventListenerData* >::iterator l_d =
1940 myEventListeners.find( listener );
1941 if ( l_d != myEventListeners.end() ) {
1942 EventListenerData* curData = l_d->second;
1943 if ( curData && curData != data && curData->IsDeletable() )
1948 myEventListeners.insert( make_pair( listener, data ));
1951 //================================================================================
1953 * \brief Return an event listener data
1954 * \param listener - the listener whose data is
1955 * \retval EventListenerData* - found data, maybe NULL
1957 //================================================================================
1959 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
1961 map< EventListener*, EventListenerData* >::const_iterator l_d =
1962 myEventListeners.find( listener );
1963 if ( l_d != myEventListeners.end() )
1968 //================================================================================
1970 * \brief Notify stored event listeners on the occured event
1971 * \param event - algo_event or compute_event itself
1972 * \param eventType - algo_event or compute_event
1973 * \param subMesh - the submesh where the event occures
1974 * \param data - listener data stored in the subMesh
1975 * \param hyp - hypothesis, if eventType is algo_event
1977 //================================================================================
1979 void SMESH_subMesh::NotifyListenersOnEvent( const int event,
1980 const event_type eventType,
1981 SMESH_Hypothesis* hyp)
1983 map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
1984 for ( ; l_d != myEventListeners.end(); ++l_d )
1985 l_d->first->ProcessEvent( event, eventType, this, l_d->second, hyp );
1988 //================================================================================
1990 * \brief Unregister the listener and delete listener's data
1991 * \param listener - the event listener
1993 //================================================================================
1995 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
1997 map< EventListener*, EventListenerData* >::iterator l_d =
1998 myEventListeners.find( listener );
1999 if ( l_d != myEventListeners.end() ) {
2000 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2001 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2002 myEventListeners.erase( l_d );
2006 //================================================================================
2008 * \brief Delete event listeners depending on algo of this submesh
2010 //================================================================================
2012 void SMESH_subMesh::DeleteOwnListeners()
2014 list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
2015 for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
2016 sm_l->first->DeleteEventListener( sm_l->second );
2017 myOwnListeners.clear();
2020 //================================================================================
2022 * \brief Do something on a certain event
2023 * \param event - algo_event or compute_event itself
2024 * \param eventType - algo_event or compute_event
2025 * \param subMesh - the submesh where the event occures
2026 * \param data - listener data stored in the subMesh
2027 * \param hyp - hypothesis, if eventType is algo_event
2029 * The base implementation translates CLEAN event to the subMesh
2030 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2031 * successful COMPUTE event.
2033 //================================================================================
2035 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2036 const int eventType,
2037 SMESH_subMesh* subMesh,
2038 EventListenerData* data,
2039 const SMESH_Hypothesis* /*hyp*/)
2041 if ( data && !data->mySubMeshes.empty() &&
2042 eventType == SMESH_subMesh::COMPUTE_EVENT)
2044 ASSERT( data->mySubMeshes.front() != subMesh );
2045 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2046 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2048 case SMESH_subMesh::CLEAN:
2049 for ( ; smIt != smEnd; ++ smIt)
2050 (*smIt)->ComputeStateEngine( event );
2052 case SMESH_subMesh::COMPUTE:
2053 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2054 for ( ; smIt != smEnd; ++ smIt)
2055 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2064 //================================================================================
2066 * \brief Iterator over submeshes and optionally prepended or appended one
2068 //================================================================================
2070 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2072 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2073 SMESH_subMesh* prepend,
2074 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2076 myCur = prepend ? prepend : myIt->more() ? myIt->next() : 0;
2078 /// Return true if and only if there are other object in this iterator
2083 /// Return the current object and step to the next one
2084 virtual SMESH_subMesh* next()
2086 SMESH_subMesh* res = myCur;
2087 if ( myIt->more() ) { myCur = myIt->next(); }
2088 else { myCur = myAppend; myAppend = 0; }
2095 SMESH_subMesh *myAppend, *myCur;
2096 SMDS_Iterator<SMESH_subMesh*> *myIt;
2100 //================================================================================
2102 * \brief Return iterator on the submeshes this one depends on
2103 * \param includeSelf - this submesh to be returned also
2104 * \param reverse - if true, complex shape submeshes go first
2106 //================================================================================
2108 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2111 SMESH_subMesh *prepend=0, *append=0;
2112 if ( includeSelf ) {
2113 if ( reverse ) prepend = this;
2116 typedef map < int, SMESH_subMesh * > TMap;
2119 return SMESH_subMeshIteratorPtr
2120 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2123 return SMESH_subMeshIteratorPtr
2124 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));