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_IndexedDataMapOfShapeListOfShape.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_ListOfShape.hxx>
51 #include <TopTools_MapOfShape.hxx>
53 #include <TopoDS_Compound.hxx>
56 #include <Standard_OutOfMemory.hxx>
57 #include <Standard_ErrorHandler.hxx>
61 //=============================================================================
63 * \brief Allocate some memory at construction and release it at destruction.
64 * Is used to be able to continue working after mesh generation breaks due to
67 //=============================================================================
72 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
73 ~MemoryReserve() { delete [] myBuf; }
76 //=============================================================================
78 * default constructor:
80 //=============================================================================
82 SMESH_subMesh::SMESH_subMesh(int Id,
84 SMESHDS_Mesh * meshDS,
85 const TopoDS_Shape & aSubShape)
87 _subShape = aSubShape;
88 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
91 _dependenceAnalysed = _alwaysComputed = false;
93 if (_subShape.ShapeType() == TopAbs_VERTEX)
96 _computeState = READY_TO_COMPUTE;
100 _algoState = NO_ALGO;
101 _computeState = NOT_READY;
105 //=============================================================================
109 //=============================================================================
111 SMESH_subMesh::~SMESH_subMesh()
113 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
115 DeleteOwnListeners();
118 //=============================================================================
122 //=============================================================================
124 int SMESH_subMesh::GetId() const
126 //MESSAGE("SMESH_subMesh::GetId");
130 //=============================================================================
134 //=============================================================================
136 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
138 // submesh appears in DS only when a mesher set nodes and elements on a shape
139 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
142 //=============================================================================
146 //=============================================================================
148 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
150 if ( !GetSubMeshDS() ) {
151 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
152 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
154 return GetSubMeshDS();
157 //=============================================================================
161 //=============================================================================
163 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
165 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
166 while ( smIt->more() ) {
167 SMESH_subMesh *sm = smIt->next();
168 if ( sm->GetComputeState() == READY_TO_COMPUTE )
171 return 0; // nothing to compute
174 //================================================================================
176 * \brief Allow algo->Compute() if a subshape of lower dim is meshed but
177 * none mesh entity is bound to it (PAL13615, 2nd part)
179 //================================================================================
181 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
183 _alwaysComputed = isAlCo;
184 if ( _alwaysComputed )
185 _computeState = COMPUTE_OK;
187 ComputeStateEngine( CHECK_COMPUTE_STATE );
190 //=======================================================================
191 //function : IsMeshComputed
192 //purpose : check if _subMeshDS contains mesh elements
193 //=======================================================================
195 bool SMESH_subMesh::IsMeshComputed() const
197 if ( _alwaysComputed )
199 // algo may bind a submesh not to _subShape, eg 3D algo
200 // sets nodes on SHELL while _subShape may be SOLID
202 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
203 int dim = SMESH_Gen::GetShapeDim( _subShape );
204 int type = _subShape.ShapeType();
205 for ( ; type <= TopAbs_VERTEX; type++) {
206 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
208 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
209 for ( ; exp.More(); exp.Next() )
211 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
213 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
226 //=============================================================================
230 //=============================================================================
232 bool SMESH_subMesh::SubMeshesComputed()
234 int myDim = SMESH_Gen::GetShapeDim( _subShape );
235 int dimToCheck = myDim - 1;
236 bool subMeshesComputed = true;
237 // check subMeshes with upper dimension => reverse iteration
238 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
239 while ( smIt->more() )
241 SMESH_subMesh *sm = smIt->next();
242 if ( sm->_alwaysComputed )
244 const TopoDS_Shape & ss = sm->GetSubShape();
245 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
246 // in checking of existence of edges if the algo needs only faces. Moreover,
247 // degenerated edges may have no submesh, as after computing NETGEN_2D.
248 int dim = SMESH_Gen::GetShapeDim( ss );
249 if (dim < dimToCheck)
250 break; // the rest subMeshes are all of less dimension
251 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
252 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
253 (ds && ( ds->NbNodes() || ds->NbElements() )));
256 int type = ss.ShapeType();
258 subMeshesComputed = false;
262 case TopAbs_COMPOUND:
264 MESSAGE("The not computed sub mesh is a COMPOUND");
267 case TopAbs_COMPSOLID:
269 MESSAGE("The not computed sub mesh is a COMPSOLID");
274 MESSAGE("The not computed sub mesh is a SHEL");
279 MESSAGE("The not computed sub mesh is a WIRE");
284 MESSAGE("The not computed sub mesh is a SOLID");
289 MESSAGE("The not computed sub mesh is a FACE");
294 MESSAGE("The not computed sub mesh is a EDGE");
299 MESSAGE("The not computed sub mesh is of unknown type");
307 return subMeshesComputed;
310 //=============================================================================
314 //=============================================================================
316 bool SMESH_subMesh::SubMeshesReady()
318 bool subMeshesReady = true;
319 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
320 while ( smIt->more() ) {
321 SMESH_subMesh *sm = smIt->next();
322 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
323 sm->GetComputeState() == READY_TO_COMPUTE);
326 subMeshesReady = false;
331 return subMeshesReady;
334 //=============================================================================
336 * Construct dependence on first level subMeshes. complex shapes (compsolid,
337 * shell, wire) are not analysed the same way as simple shapes (solid, face,
339 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
340 * with possible multiples occurences. Multiples occurences corresponds to
341 * internal frontiers within shapes of the collection and must not be keeped.
342 * See FinalizeDependence.
344 //=============================================================================
346 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
348 if (_dependenceAnalysed)
351 //MESSAGE("SMESH_subMesh::DependsOn");
353 int type = _subShape.ShapeType();
357 case TopAbs_COMPOUND:
359 //MESSAGE("compound");
360 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
363 InsertDependence(exp.Current());
365 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
368 InsertDependence(exp.Current()); //only shell not in solid
370 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
373 InsertDependence(exp.Current());
375 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
378 InsertDependence(exp.Current());
382 case TopAbs_COMPSOLID:
384 //MESSAGE("compsolid");
385 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
388 InsertDependence(exp.Current());
395 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
398 InsertDependence(exp.Current());
405 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
408 InsertDependence(exp.Current());
415 if(_father->HasShapeToMesh()) {
416 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
419 InsertDependence(exp.Current());
427 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
430 InsertDependence(exp.Current());
437 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
440 InsertDependence(exp.Current());
453 _dependenceAnalysed = true;
457 //=============================================================================
459 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
461 //=============================================================================
463 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
465 //MESSAGE("SMESH_subMesh::InsertDependence");
466 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
467 int type = aSubShape.ShapeType();
468 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
469 int cle = aSubMesh->GetId();
470 cle += 10000000 * ordType; // sort map by ordType then index
471 if ( _mapDepend.find( cle ) == _mapDepend.end())
473 _mapDepend[cle] = aSubMesh;
474 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
475 _mapDepend.insert( subMap.begin(), subMap.end() );
479 //=============================================================================
483 //=============================================================================
485 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
487 //MESSAGE("SMESH_subMesh::GetSubShape");
492 //=======================================================================
493 //function : CanAddHypothesis
494 //purpose : return true if theHypothesis can be attached to me:
495 // its dimention is checked
496 //=======================================================================
498 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
500 int aHypDim = theHypothesis->GetDim();
501 if(_father->HasShapeToMesh()) {
502 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
503 if ( aHypDim <= aShapeDim )
507 //Only 3D hypothesis may be assigned to the mesh w/o geometry
509 // if ( aHypDim < aShapeDim )
510 // return ( _father->IsMainShape( _subShape ));
515 //=======================================================================
516 //function : IsApplicableHypotesis
518 //=======================================================================
520 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
521 const TopAbs_ShapeEnum theShapeType)
523 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
525 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
528 switch ( theShapeType ) {
533 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
536 // Special case for algorithms, building 2D mesh on a whole shell.
537 // Before this fix there was a problem after restoring from study,
538 // because in that case algorithm is assigned before hypothesis
539 // (on shell in problem case) and hypothesis is checked on faces
540 // (because it is 2D), where we have NO_ALGO state.
541 // Now 2D hypothesis is also applicable to shells.
542 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
545 // case TopAbs_COMPSOLID:
546 // case TopAbs_COMPOUND:
552 //=============================================================================
556 //=============================================================================
558 SMESH_Hypothesis::Hypothesis_Status
559 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
561 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
562 //SCRUTE(_algoState);
565 // **** les retour des evenement shape sont significatifs
566 // (add ou remove fait ou non)
567 // le retour des evenement father n'indiquent pas que add ou remove fait
569 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
571 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
572 SMESH_Gen* gen =_father->GetGen();
573 SMESH_Algo* algo = 0;
575 if (_subShape.ShapeType() == TopAbs_VERTEX )
577 if ( anHyp->GetDim() != 0) {
578 if (event == ADD_HYP || event == ADD_ALGO)
579 return SMESH_Hypothesis::HYP_BAD_DIM;
581 return SMESH_Hypothesis::HYP_OK;
584 else if ( _algoState == HYP_OK ) {
585 // update default _algoState
586 if ( event != REMOVE_FATHER_ALGO )
588 _algoState = NO_ALGO;
589 algo = gen->GetAlgo(*_father, _subShape);
591 _algoState = MISSING_HYP;
592 if ( event == REMOVE_FATHER_HYP ||
593 algo->CheckHypothesis(*_father,_subShape, aux_ret))
600 int oldAlgoState = _algoState;
601 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
603 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
605 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
607 // -------------------------------------------
608 // check if a shape needed by algo is present
609 // -------------------------------------------
610 algo = static_cast< SMESH_Algo* >( anHyp );
611 if(_father->GetShapeToMesh() != SMESH_Mesh::PseudoShape())
612 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
613 return SMESH_Hypothesis::HYP_BAD_GEOMETRY;
614 // ----------------------
615 // check mesh conformity
616 // ----------------------
617 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
618 return SMESH_Hypothesis::HYP_NOTCONFORM;
621 // ----------------------------------
622 // add a hypothesis to DS if possible
623 // ----------------------------------
624 if (event == ADD_HYP || event == ADD_ALGO)
626 if ( ! CanAddHypothesis( anHyp )) // check dimension
627 return SMESH_Hypothesis::HYP_BAD_DIM;
629 if(anHyp->GetDim() == 3 && !_father->HasShapeToMesh()
630 && event == ADD_ALGO) {
631 //Only NETGEN_3D and GHS3D_3D can be assigned to the Mesh w/o geometryy
632 bool isNetgen3D = (strcmp( "NETGEN_3D", anHyp->GetName()) == 0);
633 bool isGhs3d = (strcmp( "GHS3D_3D", anHyp->GetName()) == 0);
634 if( !isNetgen3D && !isGhs3d)
635 return SMESH_Hypothesis::HYP_BAD_DIM;
640 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
641 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
643 if ( !meshDS->AddHypothesis(_subShape, anHyp))
644 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
647 // --------------------------
648 // remove a hypothesis from DS
649 // --------------------------
650 if (event == REMOVE_HYP || event == REMOVE_ALGO)
652 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
653 return SMESH_Hypothesis::HYP_OK; // nothing changes
655 if (event == REMOVE_ALGO)
657 algo = dynamic_cast<SMESH_Algo*> (anHyp);
658 if (!algo->NeedDescretBoundary())
660 // clean all mesh in the tree of the current submesh;
661 // we must perform it now because later
662 // we will have no information about the type of the removed algo
664 ComputeStateEngine( CLEAN );
666 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
671 // ------------------
672 // analyse algo state
673 // ------------------
674 if (!isApplicableHyp)
675 return ret; // not applicable hypotheses do not change algo state
680 // ----------------------------------------------------------------------
687 algo = gen->GetAlgo((*_father), _subShape);
689 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
690 SetAlgoState(HYP_OK);
691 else if ( algo->IsStatusFatal( aux_ret )) {
692 meshDS->RemoveHypothesis(_subShape, anHyp);
696 SetAlgoState(MISSING_HYP);
703 case ADD_FATHER_ALGO: { // Algo just added in father
704 algo = gen->GetAlgo((*_father), _subShape);
706 if ( algo == anHyp ) {
707 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
708 SetAlgoState(HYP_OK);
710 SetAlgoState(MISSING_HYP);
714 case REMOVE_FATHER_HYP:
716 case REMOVE_FATHER_ALGO: {
717 algo = gen->GetAlgo((*_father), _subShape);
720 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
721 SetAlgoState(HYP_OK);
723 SetAlgoState(MISSING_HYP);
727 case MODIF_HYP: break;
734 // ----------------------------------------------------------------------
740 algo = gen->GetAlgo((*_father), _subShape);
742 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
743 SetAlgoState(HYP_OK);
744 if (SMESH_Hypothesis::IsStatusFatal( ret ))
745 meshDS->RemoveHypothesis(_subShape, anHyp);
746 else if (!_father->IsUsedHypothesis( anHyp, this ))
748 meshDS->RemoveHypothesis(_subShape, anHyp);
749 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
753 case ADD_ALGO: { //already existing algo : on father ?
754 algo = gen->GetAlgo((*_father), _subShape);
756 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
757 SetAlgoState(HYP_OK);
758 else if ( algo->IsStatusFatal( aux_ret )) {
759 meshDS->RemoveHypothesis(_subShape, anHyp);
763 SetAlgoState(MISSING_HYP);
768 case REMOVE_ALGO: { // perhaps a father algo applies ?
769 algo = gen->GetAlgo((*_father), _subShape);
770 if (algo == NULL) // no more algo applying on subShape...
772 SetAlgoState(NO_ALGO);
776 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
777 SetAlgoState(HYP_OK);
779 SetAlgoState(MISSING_HYP);
783 case MODIF_HYP: // assigned hypothesis value may become good
784 case ADD_FATHER_HYP: {
785 algo = gen->GetAlgo((*_father), _subShape);
787 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
788 SetAlgoState(HYP_OK);
790 SetAlgoState(MISSING_HYP);
793 case ADD_FATHER_ALGO: { // new father algo
794 algo = gen->GetAlgo((*_father), _subShape);
796 if ( algo == anHyp ) {
797 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
798 SetAlgoState(HYP_OK);
800 SetAlgoState(MISSING_HYP);
804 case REMOVE_FATHER_HYP: // nothing to do
806 case REMOVE_FATHER_ALGO: {
807 algo = gen->GetAlgo((*_father), _subShape);
808 if (algo == NULL) // no more applying algo on father
810 SetAlgoState(NO_ALGO);
814 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
815 SetAlgoState(HYP_OK);
817 SetAlgoState(MISSING_HYP);
827 // ----------------------------------------------------------------------
833 algo = gen->GetAlgo((*_father), _subShape);
835 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
837 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
838 // ret should be fatal: anHyp was not added
839 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
841 else if (!_father->IsUsedHypothesis( anHyp, this ))
842 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
844 if (SMESH_Hypothesis::IsStatusFatal( ret ))
846 MESSAGE("do not add extra hypothesis");
847 meshDS->RemoveHypothesis(_subShape, anHyp);
855 case ADD_ALGO: { //already existing algo : on father ?
856 algo = gen->GetAlgo((*_father), _subShape);
857 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
858 // check if algo changes
860 f.Init( SMESH_HypoFilter::IsAlgo() );
861 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
862 f.AndNot( SMESH_HypoFilter::Is( algo ));
863 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
865 string(algo->GetName()) != string(prevAlgo->GetName()) )
869 SetAlgoState(MISSING_HYP);
873 algo = gen->GetAlgo((*_father), _subShape);
875 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
876 SetAlgoState(HYP_OK);
878 SetAlgoState(MISSING_HYP);
882 case REMOVE_ALGO: { // perhaps a father algo applies ?
883 algo = gen->GetAlgo((*_father), _subShape);
884 if (algo == NULL) // no more algo applying on subShape...
886 SetAlgoState(NO_ALGO);
890 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
891 // check if algo remains
892 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
896 SetAlgoState(MISSING_HYP);
900 case MODIF_HYP: // hypothesis value may become bad
901 case ADD_FATHER_HYP: { // new father hypothesis ?
902 algo = gen->GetAlgo((*_father), _subShape);
904 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
906 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
910 SetAlgoState(MISSING_HYP);
913 case ADD_FATHER_ALGO: {
914 algo = gen->GetAlgo((*_father), _subShape);
915 if ( algo == anHyp ) { // a new algo on father
916 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
917 // check if algo changes
919 f.Init( SMESH_HypoFilter::IsAlgo() );
920 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
921 f.AndNot( SMESH_HypoFilter::Is( algo ));
922 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
924 string(algo->GetName()) != string(prevAlgo->GetName()) )
928 SetAlgoState(MISSING_HYP);
932 case REMOVE_FATHER_HYP: {
933 algo = gen->GetAlgo((*_father), _subShape);
935 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
936 // is there the same local hyp or maybe a new father algo applied?
937 if ( !GetSimilarAttached( _subShape, anHyp ) )
941 SetAlgoState(MISSING_HYP);
944 case REMOVE_FATHER_ALGO: {
945 algo = gen->GetAlgo((*_father), _subShape);
946 if (algo == NULL) // no more applying algo on father
948 SetAlgoState(NO_ALGO);
952 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
953 // check if algo changes
954 if ( string(algo->GetName()) != string( anHyp->GetName()) )
958 SetAlgoState(MISSING_HYP);
968 // ----------------------------------------------------------------------
975 // detect algorithm hiding
977 if ( ret == SMESH_Hypothesis::HYP_OK &&
978 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
979 algo->GetName() == anHyp->GetName() )
982 SMESH_Gen* gen = _father->GetGen();
983 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
984 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
985 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
986 if ( !upperAlgo->NeedDescretBoundary() )
987 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
990 if ( ret == SMESH_Hypothesis::HYP_OK && !algo->NeedDescretBoundary() ) {
991 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
992 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
993 if ( gen->GetAlgo( *_father, i_sm->second->_subShape ))
994 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
998 bool stateChange = ( _algoState != oldAlgoState );
1000 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1001 algo->SetEventListener( this );
1003 NotifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1005 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1006 DeleteOwnListeners();
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->NbInverseNodes() == 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 _computeState = COMPUTE_OK;
1228 else if ( event == COMPUTE && !_alwaysComputed ) {
1229 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1230 gp_Pnt P = BRep_Tool::Pnt(V);
1231 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1232 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1233 _computeState = COMPUTE_OK;
1236 if ( event == MODIF_ALGO_STATE )
1240 SMESH_Gen *gen = _father->GetGen();
1241 SMESH_Algo *algo = 0;
1243 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1244 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1246 switch (_computeState)
1249 // ----------------------------------------------------------------------
1254 case MODIF_ALGO_STATE:
1255 algo = gen->GetAlgo((*_father), _subShape);
1256 if (algo && !algo->NeedDescretBoundary())
1257 CleanDependsOn(); // clean sub-meshes with event CLEAN
1258 if ( _algoState == HYP_OK )
1259 _computeState = READY_TO_COMPUTE;
1261 case COMPUTE: // nothing to do
1265 RemoveSubMeshElementsAndNodes();
1267 case SUBMESH_COMPUTED: // nothing to do
1269 case SUBMESH_RESTORED:
1270 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1272 case MESH_ENTITY_REMOVED:
1274 case CHECK_COMPUTE_STATE:
1275 if ( IsMeshComputed() )
1276 _computeState = COMPUTE_OK;
1284 // ----------------------------------------------------------------------
1286 case READY_TO_COMPUTE:
1289 case MODIF_ALGO_STATE:
1290 _computeState = NOT_READY;
1291 algo = gen->GetAlgo((*_father), _subShape);
1294 if (!algo->NeedDescretBoundary())
1295 CleanDependsOn(); // clean sub-meshes with event CLEAN
1296 if ( _algoState == HYP_OK )
1297 _computeState = READY_TO_COMPUTE;
1302 algo = gen->GetAlgo((*_father), _subShape);
1304 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1307 MESSAGE("***** verify compute state *****");
1308 _computeState = NOT_READY;
1309 SetAlgoState(MISSING_HYP);
1312 // check submeshes needed
1313 if (_father->HasShapeToMesh() ) {
1314 bool subComputed = SubMeshesComputed();
1315 ret = ( algo->NeedDescretBoundary() ? subComputed :
1316 ( !subComputed || _father->IsNotConformAllowed() ));
1318 _computeState = FAILED_TO_COMPUTE;
1319 if ( !algo->NeedDescretBoundary() )
1321 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1322 "Unexpected computed submesh",algo);
1328 RemoveSubMeshElementsAndNodes();
1330 _computeState = FAILED_TO_COMPUTE;
1331 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1332 TopoDS_Shape shape = _subShape;
1334 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1337 algo->InitComputeError();
1338 MemoryReserve aMemoryReserve;
1339 SMDS_Mesh::CheckMemory();
1340 if ( !_father->HasShapeToMesh() ) // no shape
1342 SMESH_MesherHelper helper( *_father );
1343 helper.SetSubShape( shape );
1344 helper.SetElementsOnShape( true );
1345 ret = algo->Compute(*_father, &helper );
1349 if (!algo->OnlyUnaryInput()) {
1350 shape = GetCollection( gen, algo );
1352 ret = algo->Compute((*_father), shape);
1355 _computeError = algo->GetComputeError();
1357 catch ( std::bad_alloc& exc ) {
1358 printf("std::bad_alloc thrown inside algo->Compute()\n");
1359 if ( _computeError ) {
1360 _computeError->myName = COMPERR_MEMORY_PB;
1361 //_computeError->myComment = exc.what();
1363 cleanSubMesh( this );
1366 catch ( Standard_OutOfMemory& exc ) {
1367 printf("Standard_OutOfMemory thrown inside algo->Compute()\n");
1368 if ( _computeError ) {
1369 _computeError->myName = COMPERR_MEMORY_PB;
1370 //_computeError->myComment = exc.what();
1372 cleanSubMesh( this );
1373 throw std::bad_alloc();
1375 catch (Standard_Failure& ex) {
1376 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1377 _computeError->myName = COMPERR_OCC_EXCEPTION;
1378 _computeError->myComment += ex.DynamicType()->Name();
1379 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1380 _computeError->myComment += ": ";
1381 _computeError->myComment += ex.GetMessageString();
1384 catch ( SALOME_Exception& S_ex ) {
1385 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1386 _computeError->myName = COMPERR_SLM_EXCEPTION;
1387 _computeError->myComment = S_ex.what();
1389 catch ( std::exception& exc ) {
1390 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1391 _computeError->myName = COMPERR_STD_EXCEPTION;
1392 _computeError->myComment = exc.what();
1395 if ( _computeError )
1396 _computeError->myName = COMPERR_EXCEPTION;
1400 if (ret && !_alwaysComputed) { // check if anything was built
1401 ret = ( GetSubMeshDS() && ( GetSubMeshDS()->NbElements() || GetSubMeshDS()->NbNodes() ));
1403 bool isComputeErrorSet = !CheckComputeError( algo, shape );
1404 if (!ret && !isComputeErrorSet)
1406 // Set _computeError
1407 if ( !_computeError )
1408 _computeError = SMESH_ComputeError::New();
1409 if ( _computeError->IsOK() )
1410 _computeError->myName = COMPERR_ALGO_FAILED;
1411 _computeState = FAILED_TO_COMPUTE;
1415 _computeError.reset();
1417 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1422 RemoveSubMeshElementsAndNodes();
1423 _computeState = NOT_READY;
1424 algo = gen->GetAlgo((*_father), _subShape);
1427 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1429 _computeState = READY_TO_COMPUTE;
1431 SetAlgoState(MISSING_HYP);
1434 case SUBMESH_COMPUTED: // nothing to do
1436 case SUBMESH_RESTORED:
1437 // check if a mesh is already computed that may
1438 // happen after retrieval from a file
1439 ComputeStateEngine( CHECK_COMPUTE_STATE );
1440 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1441 algo = gen->GetAlgo(*_father, _subShape);
1442 if (algo) algo->SubmeshRestored( this );
1444 case MESH_ENTITY_REMOVED:
1446 case CHECK_COMPUTE_STATE:
1447 if ( IsMeshComputed() )
1448 _computeState = COMPUTE_OK;
1456 // ----------------------------------------------------------------------
1461 case MODIF_ALGO_STATE:
1462 ComputeStateEngine( CLEAN );
1463 algo = gen->GetAlgo((*_father), _subShape);
1464 if (algo && !algo->NeedDescretBoundary())
1465 CleanDependsOn(); // clean sub-meshes with event CLEAN
1467 case COMPUTE: // nothing to do
1470 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1471 RemoveSubMeshElementsAndNodes();
1472 _computeState = NOT_READY;
1473 if ( _algoState == HYP_OK )
1474 _computeState = READY_TO_COMPUTE;
1476 case SUBMESH_COMPUTED: // nothing to do
1478 case SUBMESH_RESTORED:
1479 ComputeStateEngine( CHECK_COMPUTE_STATE );
1480 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1481 algo = gen->GetAlgo(*_father, _subShape);
1482 if (algo) algo->SubmeshRestored( this );
1484 case MESH_ENTITY_REMOVED:
1485 UpdateDependantsState( CHECK_COMPUTE_STATE );
1486 ComputeStateEngine( CHECK_COMPUTE_STATE );
1487 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1489 case CHECK_COMPUTE_STATE:
1490 if ( !IsMeshComputed() )
1491 if (_algoState == HYP_OK)
1492 _computeState = READY_TO_COMPUTE;
1494 _computeState = NOT_READY;
1502 // ----------------------------------------------------------------------
1504 case FAILED_TO_COMPUTE:
1507 case MODIF_ALGO_STATE:
1508 if (_algoState == HYP_OK)
1509 _computeState = READY_TO_COMPUTE;
1511 _computeState = NOT_READY;
1513 case COMPUTE: // nothing to do
1516 CleanDependants(); // submeshes dependent on me should be cleaned as well
1517 RemoveSubMeshElementsAndNodes();
1519 case SUBMESH_COMPUTED: // allow retry compute
1520 if (_algoState == HYP_OK)
1521 _computeState = READY_TO_COMPUTE;
1523 _computeState = NOT_READY;
1525 case SUBMESH_RESTORED:
1526 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1528 case MESH_ENTITY_REMOVED:
1530 case CHECK_COMPUTE_STATE:
1531 if ( IsMeshComputed() )
1532 _computeState = COMPUTE_OK;
1534 if (_algoState == HYP_OK)
1535 _computeState = READY_TO_COMPUTE;
1537 _computeState = NOT_READY;
1545 // ----------------------------------------------------------------------
1551 NotifyListenersOnEvent( event, COMPUTE_EVENT );
1556 //=======================================================================
1558 * \brief Update compute_state by _computeError and send proper events to
1559 * dependent submeshes
1560 * \retval bool - true if _computeError is NOT set
1562 //=======================================================================
1564 bool SMESH_subMesh::CheckComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1566 bool noErrors = true;
1568 if ( !theShape.IsNull() )
1570 // Check state of submeshes
1571 if ( !theAlgo->NeedDescretBoundary())
1573 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1574 while ( smIt->more() )
1575 if ( !smIt->next()->CheckComputeError( theAlgo ))
1579 // Check state of neighbours
1580 if ( !theAlgo->OnlyUnaryInput() &&
1581 theShape.ShapeType() == TopAbs_COMPOUND &&
1582 !theShape.IsSame( _subShape ))
1584 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1585 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1587 if ( !sm->CheckComputeError( theAlgo ))
1589 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1596 if ( !_computeError || _computeError->IsOK() )
1598 _computeState = COMPUTE_OK;
1602 if ( !_computeError->myAlgo )
1603 _computeError->myAlgo = theAlgo;
1607 text << theAlgo->GetName() << " failed on subshape #" << _Id << " with error ";
1608 if (_computeError->IsCommon() )
1609 text << _computeError->CommonName();
1611 text << _computeError->myName;
1612 if ( _computeError->myComment.size() > 0 )
1613 text << " \"" << _computeError->myComment << "\"";
1616 cout << text << endl;
1617 // Show vertices location of a failed shape
1618 TopTools_IndexedMapOfShape vMap;
1619 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1620 cout << "Subshape vertices " << ( vMap.Extent()>10 ? "(first 10):" : ":") << endl;
1621 for ( int iv = 1; iv <= vMap.Extent() && iv < 11; ++iv ) {
1622 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1623 cout << "#" << _father->GetMeshDS()->ShapeToIndex( vMap( iv )) << " ";
1624 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1629 _computeState = FAILED_TO_COMPUTE;
1636 //=======================================================================
1637 //function : ApplyToCollection
1638 //purpose : Apply theAlgo to all subshapes in theCollection
1639 //=======================================================================
1641 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1642 const TopoDS_Shape& theCollection)
1644 MESSAGE("SMESH_subMesh::ApplyToCollection");
1645 ASSERT ( !theAlgo->NeedDescretBoundary() );
1647 if ( _computeError )
1648 _computeError->myName = COMPERR_OK;
1650 bool ok = theAlgo->Compute( *_father, theCollection );
1652 // set _computeState of subshapes
1653 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1654 for ( ; anExplorer.More(); anExplorer.Next() )
1656 if ( SMESH_subMesh* subMesh = _father->GetSubMeshContaining( anExplorer.Current() ))
1658 bool localOK = subMesh->CheckComputeError( theAlgo );
1659 if ( !ok && localOK && !subMesh->IsMeshComputed() )
1661 subMesh->_computeError = theAlgo->GetComputeError();
1662 if ( subMesh->_computeError->IsOK() )
1663 _computeError = SMESH_ComputeError::New(COMPERR_ALGO_FAILED);
1664 localOK = CheckComputeError( theAlgo );
1667 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1668 subMesh->UpdateSubMeshState( localOK ? COMPUTE_OK : FAILED_TO_COMPUTE );
1676 //=======================================================================
1677 //function : UpdateSubMeshState
1679 //=======================================================================
1681 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1683 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1684 while ( smIt->more() )
1685 smIt->next()->_computeState = theState;
1688 //=======================================================================
1689 //function : ComputeSubMeshStateEngine
1691 //=======================================================================
1693 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1695 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1696 while ( smIt->more() )
1697 smIt->next()->ComputeStateEngine(event);
1700 //=======================================================================
1701 //function : UpdateDependantsState
1703 //=======================================================================
1705 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1707 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1708 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1709 for (; it.More(); it.Next())
1711 const TopoDS_Shape& ancestor = it.Value();
1712 SMESH_subMesh *aSubMesh =
1713 _father->GetSubMeshContaining(ancestor);
1715 aSubMesh->ComputeStateEngine( theEvent );
1719 //=============================================================================
1723 //=============================================================================
1725 void SMESH_subMesh::CleanDependants()
1727 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1729 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1730 for (; it.More(); it.Next())
1732 const TopoDS_Shape& ancestor = it.Value();
1733 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1734 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1735 // will erase mesh on other shapes in a compound
1736 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1737 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1739 aSubMesh->ComputeStateEngine(CLEAN);
1745 //=============================================================================
1749 //=============================================================================
1751 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1753 //SCRUTE(_subShape.ShapeType());
1755 cleanSubMesh( this );
1757 // algo may bind a submesh not to _subShape, eg 3D algo
1758 // sets nodes on SHELL while _subShape may be SOLID
1760 int dim = SMESH_Gen::GetShapeDim( _subShape );
1761 int type = _subShape.ShapeType() + 1;
1762 for ( ; type <= TopAbs_EDGE; type++) {
1763 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1765 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1766 for ( ; exp.More(); exp.Next() )
1767 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1774 //=======================================================================
1775 //function : GetCollection
1776 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1777 // meshed at once along with _subShape
1778 //=======================================================================
1780 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1782 MESSAGE("SMESH_subMesh::GetCollection");
1784 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1786 if ( mainShape.IsSame( _subShape ))
1789 const bool ignoreAuxiliaryHyps = false;
1790 list<const SMESHDS_Hypothesis*> aUsedHyp =
1791 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1793 // put in a compound all shapes with the same hypothesis assigned
1794 // and a good ComputState
1796 TopoDS_Compound aCompound;
1797 BRep_Builder aBuilder;
1798 aBuilder.MakeCompound( aCompound );
1800 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1801 for ( ; anExplorer.More(); anExplorer.Next() )
1803 const TopoDS_Shape& S = anExplorer.Current();
1804 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1805 if ( subMesh == this )
1807 aBuilder.Add( aCompound, S );
1809 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1811 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1812 if (anAlgo == theAlgo &&
1813 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp)
1814 aBuilder.Add( aCompound, S );
1821 //=======================================================================
1822 //function : GetSimilarAttached
1823 //purpose : return a hypothesis attached to theShape.
1824 // If theHyp is provided, similar but not same hypotheses
1825 // is returned; else only applicable ones having theHypType
1827 //=======================================================================
1829 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1830 const SMESH_Hypothesis * theHyp,
1831 const int theHypType)
1833 SMESH_HypoFilter hypoKind;
1834 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1836 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1837 hypoKind.AndNot( hypoKind.Is( theHyp ));
1838 if ( theHyp->IsAuxiliary() )
1839 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1841 hypoKind.AndNot( hypoKind.IsAuxiliary());
1844 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1847 return _father->GetHypothesis( theShape, hypoKind, false );
1850 //=======================================================================
1851 //function : CheckConcurentHypothesis
1852 //purpose : check if there are several applicable hypothesis attached to
1854 //=======================================================================
1856 SMESH_Hypothesis::Hypothesis_Status
1857 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1859 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1861 // is there local hypothesis on me?
1862 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1863 return SMESH_Hypothesis::HYP_OK;
1866 TopoDS_Shape aPrevWithHyp;
1867 const SMESH_Hypothesis* aPrevHyp = 0;
1868 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1869 for (; it.More(); it.Next())
1871 const TopoDS_Shape& ancestor = it.Value();
1872 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1875 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1877 aPrevWithHyp = ancestor;
1880 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1881 return SMESH_Hypothesis::HYP_CONCURENT;
1883 return SMESH_Hypothesis::HYP_OK;
1886 return SMESH_Hypothesis::HYP_OK;
1889 //================================================================================
1891 * \brief Sets an event listener and its data to a submesh
1892 * \param listener - the listener to store
1893 * \param data - the listener data to store
1894 * \param where - the submesh to store the listener and it's data
1895 * \param deleteListener - if true then the listener will be deleted as
1896 * it is removed from where submesh
1898 * It remembers the submesh where it puts the listener in order to delete
1899 * them when HYP_OK algo_state is lost
1900 * After being set, event listener is notified on each event of where submesh.
1902 //================================================================================
1904 void SMESH_subMesh::SetEventListener(EventListener* listener,
1905 EventListenerData* data,
1906 SMESH_subMesh* where)
1908 if ( listener && where ) {
1909 where->SetEventListener( listener, data );
1910 myOwnListeners.push_back( make_pair( where, listener ));
1914 //================================================================================
1916 * \brief Sets an event listener and its data to a submesh
1917 * \param listener - the listener to store
1918 * \param data - the listener data to store
1920 * After being set, event listener is notified on each event of a submesh.
1922 //================================================================================
1924 void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
1926 map< EventListener*, EventListenerData* >::iterator l_d =
1927 myEventListeners.find( listener );
1928 if ( l_d != myEventListeners.end() ) {
1929 if ( l_d->second && l_d->second->IsDeletable() )
1934 myEventListeners.insert( make_pair( listener, data ));
1937 //================================================================================
1939 * \brief Return an event listener data
1940 * \param listener - the listener whose data is
1941 * \retval EventListenerData* - found data, maybe NULL
1943 //================================================================================
1945 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
1947 map< EventListener*, EventListenerData* >::const_iterator l_d =
1948 myEventListeners.find( listener );
1949 if ( l_d != myEventListeners.end() )
1954 //================================================================================
1956 * \brief Notify stored event listeners on the occured event
1957 * \param event - algo_event or compute_event itself
1958 * \param eventType - algo_event or compute_event
1959 * \param subMesh - the submesh where the event occures
1960 * \param data - listener data stored in the subMesh
1961 * \param hyp - hypothesis, if eventType is algo_event
1963 //================================================================================
1965 void SMESH_subMesh::NotifyListenersOnEvent( const int event,
1966 const event_type eventType,
1967 SMESH_Hypothesis* hyp)
1969 map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
1970 for ( ; l_d != myEventListeners.end(); ++l_d )
1971 l_d->first->ProcessEvent( event, eventType, this, l_d->second, hyp );
1974 //================================================================================
1976 * \brief Unregister the listener and delete listener's data
1977 * \param listener - the event listener
1979 //================================================================================
1981 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
1983 map< EventListener*, EventListenerData* >::iterator l_d =
1984 myEventListeners.find( listener );
1985 if ( l_d != myEventListeners.end() ) {
1986 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
1987 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
1988 myEventListeners.erase( l_d );
1992 //================================================================================
1994 * \brief Delete event listeners depending on algo of this submesh
1996 //================================================================================
1998 void SMESH_subMesh::DeleteOwnListeners()
2000 list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
2001 for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
2002 sm_l->first->DeleteEventListener( sm_l->second );
2003 myOwnListeners.clear();
2006 //================================================================================
2008 * \brief Do something on a certain event
2009 * \param event - algo_event or compute_event itself
2010 * \param eventType - algo_event or compute_event
2011 * \param subMesh - the submesh where the event occures
2012 * \param data - listener data stored in the subMesh
2013 * \param hyp - hypothesis, if eventType is algo_event
2015 * The base implementation translates CLEAN event to the subMesh
2016 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2017 * successful COMPUTE event.
2019 //================================================================================
2021 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2022 const int eventType,
2023 SMESH_subMesh* subMesh,
2024 EventListenerData* data,
2025 const SMESH_Hypothesis* /*hyp*/)
2027 if ( data && !data->mySubMeshes.empty() &&
2028 eventType == SMESH_subMesh::COMPUTE_EVENT)
2030 ASSERT( data->mySubMeshes.front() != subMesh );
2032 case SMESH_subMesh::CLEAN:
2033 data->mySubMeshes.front()->ComputeStateEngine( event );
2035 case SMESH_subMesh::COMPUTE:
2036 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2037 data->mySubMeshes.front()->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2046 //================================================================================
2048 * \brief Iterator over submeshes and optionally prepended or appended one
2050 //================================================================================
2052 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2054 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2055 SMESH_subMesh* prepend,
2056 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2058 myCur = prepend ? prepend : myIt->more() ? myIt->next() : 0;
2060 /// Return true if and only if there are other object in this iterator
2065 /// Return the current object and step to the next one
2066 virtual SMESH_subMesh* next()
2068 SMESH_subMesh* res = myCur;
2069 if ( myIt->more() ) { myCur = myIt->next(); }
2070 else { myCur = myAppend; myAppend = 0; }
2077 SMESH_subMesh *myAppend, *myCur;
2078 SMDS_Iterator<SMESH_subMesh*> *myIt;
2082 //================================================================================
2084 * \brief Return iterator on the submeshes this one depends on
2085 * \param includeSelf - this submesh to be returned also
2086 * \param reverse - if true, complex shape submeshes go first
2088 //================================================================================
2090 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2093 SMESH_subMesh *prepend=0, *append=0;
2094 if ( includeSelf ) {
2095 if ( reverse ) prepend = this;
2098 typedef map < int, SMESH_subMesh * > TMap;
2101 return SMESH_subMeshIteratorPtr
2102 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2105 return SMESH_subMeshIteratorPtr
2106 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));