1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SMESH SMESH : implementaion of SMESH idl descriptions
23 // File : SMESH_subMesh.cxx
24 // Author : Paul RASCLE, EDF
27 #include "SMESH_subMesh.hxx"
29 #include "SMESH_Algo.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_HypoFilter.hxx"
32 #include "SMESH_Hypothesis.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_MesherHelper.hxx"
35 #include "SMESH_subMeshEventListener.hxx"
36 #include "SMESH_Comment.hxx"
37 #include "SMDS_SetIterator.hxx"
39 #include "utilities.h"
42 #include <BRep_Builder.hxx>
43 #include <BRep_Tool.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
48 #include <TopoDS_Compound.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopoDS_Iterator.hxx>
53 #include <Standard_OutOfMemory.hxx>
54 #include <Standard_ErrorHandler.hxx>
58 //=============================================================================
60 * \brief Allocate some memory at construction and release it at destruction.
61 * Is used to be able to continue working after mesh generation breaks due to
64 //=============================================================================
69 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
70 ~MemoryReserve() { delete [] myBuf; }
73 //=============================================================================
75 * default constructor:
77 //=============================================================================
79 SMESH_subMesh::SMESH_subMesh(int Id,
81 SMESHDS_Mesh * meshDS,
82 const TopoDS_Shape & aSubShape)
84 _subShape = aSubShape;
85 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
88 _dependenceAnalysed = _alwaysComputed = false;
90 if (_subShape.ShapeType() == TopAbs_VERTEX)
93 _computeState = READY_TO_COMPUTE;
98 _computeState = NOT_READY;
102 //=============================================================================
106 //=============================================================================
108 SMESH_subMesh::~SMESH_subMesh()
110 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
112 DeleteOwnListeners();
115 //=============================================================================
119 //=============================================================================
121 int SMESH_subMesh::GetId() const
123 //MESSAGE("SMESH_subMesh::GetId");
127 //=============================================================================
131 //=============================================================================
133 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
135 // submesh appears in DS only when a mesher set nodes and elements on a shape
136 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
139 //=============================================================================
143 //=============================================================================
145 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
147 if ( !GetSubMeshDS() ) {
148 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
149 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
151 return GetSubMeshDS();
154 //=============================================================================
158 //=============================================================================
160 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
162 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
163 while ( smIt->more() ) {
164 SMESH_subMesh *sm = smIt->next();
165 if ( sm->GetComputeState() == READY_TO_COMPUTE )
168 return 0; // nothing to compute
171 //================================================================================
173 * \brief Allow algo->Compute() if a subshape of lower dim is meshed but
174 * none mesh entity is bound to it (PAL13615, 2nd part)
176 //================================================================================
178 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
180 _alwaysComputed = isAlCo;
181 if ( _alwaysComputed )
182 _computeState = COMPUTE_OK;
184 ComputeStateEngine( CHECK_COMPUTE_STATE );
187 //=======================================================================
189 * \brief Return true if no mesh entities is bound to the submesh
191 //=======================================================================
193 bool SMESH_subMesh::IsEmpty() const
195 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
196 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
200 //=======================================================================
201 //function : IsMeshComputed
202 //purpose : check if _subMeshDS contains mesh elements
203 //=======================================================================
205 bool SMESH_subMesh::IsMeshComputed() const
207 if ( _alwaysComputed )
209 // algo may bind a submesh not to _subShape, eg 3D algo
210 // sets nodes on SHELL while _subShape may be SOLID
212 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
213 int dim = SMESH_Gen::GetShapeDim( _subShape );
214 int type = _subShape.ShapeType();
215 for ( ; type <= TopAbs_VERTEX; type++) {
216 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
218 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
219 for ( ; exp.More(); exp.Next() )
221 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
223 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
236 //=============================================================================
240 //=============================================================================
242 bool SMESH_subMesh::SubMeshesComputed()
244 int myDim = SMESH_Gen::GetShapeDim( _subShape );
245 int dimToCheck = myDim - 1;
246 bool subMeshesComputed = true;
247 // check subMeshes with upper dimension => reverse iteration
248 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
249 while ( smIt->more() )
251 SMESH_subMesh *sm = smIt->next();
252 if ( sm->_alwaysComputed )
254 const TopoDS_Shape & ss = sm->GetSubShape();
255 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
256 // in checking of existence of edges if the algo needs only faces. Moreover,
257 // degenerated edges may have no submesh, as after computing NETGEN_2D.
258 int dim = SMESH_Gen::GetShapeDim( ss );
259 if (dim < dimToCheck)
260 break; // the rest subMeshes are all of less dimension
261 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
262 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
263 (ds && ( ds->NbNodes() || ds->NbElements() )));
266 int type = ss.ShapeType();
268 subMeshesComputed = false;
272 case TopAbs_COMPOUND:
274 MESSAGE("The not computed sub mesh is a COMPOUND");
277 case TopAbs_COMPSOLID:
279 MESSAGE("The not computed sub mesh is a COMPSOLID");
284 MESSAGE("The not computed sub mesh is a SHEL");
289 MESSAGE("The not computed sub mesh is a WIRE");
294 MESSAGE("The not computed sub mesh is a SOLID");
299 MESSAGE("The not computed sub mesh is a FACE");
304 MESSAGE("The not computed sub mesh is a EDGE");
309 MESSAGE("The not computed sub mesh is of unknown type");
317 return subMeshesComputed;
320 //=============================================================================
324 //=============================================================================
326 bool SMESH_subMesh::SubMeshesReady()
328 bool subMeshesReady = true;
329 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
330 while ( smIt->more() ) {
331 SMESH_subMesh *sm = smIt->next();
332 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
333 sm->GetComputeState() == READY_TO_COMPUTE);
336 subMeshesReady = false;
341 return subMeshesReady;
344 //=============================================================================
346 * Construct dependence on first level subMeshes. complex shapes (compsolid,
347 * shell, wire) are not analysed the same way as simple shapes (solid, face,
349 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
350 * with possible multiples occurences. Multiples occurences corresponds to
351 * internal frontiers within shapes of the collection and must not be keeped.
352 * See FinalizeDependence.
354 //=============================================================================
356 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
358 if (_dependenceAnalysed)
361 //MESSAGE("SMESH_subMesh::DependsOn");
363 int type = _subShape.ShapeType();
367 case TopAbs_COMPOUND:
369 //MESSAGE("compound");
370 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
373 InsertDependence(exp.Current());
375 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
378 InsertDependence(exp.Current()); //only shell not in solid
380 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
383 InsertDependence(exp.Current());
385 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
388 InsertDependence(exp.Current());
392 case TopAbs_COMPSOLID:
394 //MESSAGE("compsolid");
395 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
398 InsertDependence(exp.Current());
405 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
408 InsertDependence(exp.Current());
415 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
418 InsertDependence(exp.Current());
425 if(_father->HasShapeToMesh()) {
426 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
429 InsertDependence(exp.Current());
437 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
440 InsertDependence(exp.Current());
447 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
450 InsertDependence(exp.Current());
463 _dependenceAnalysed = true;
467 //=============================================================================
469 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
471 //=============================================================================
473 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
475 //MESSAGE("SMESH_subMesh::InsertDependence");
476 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
477 int type = aSubShape.ShapeType();
478 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
479 int cle = aSubMesh->GetId();
480 cle += 10000000 * ordType; // sort map by ordType then index
481 if ( _mapDepend.find( cle ) == _mapDepend.end())
483 _mapDepend[cle] = aSubMesh;
484 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
485 _mapDepend.insert( subMap.begin(), subMap.end() );
489 //=============================================================================
493 //=============================================================================
495 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
497 //MESSAGE("SMESH_subMesh::GetSubShape");
502 //=======================================================================
503 //function : CanAddHypothesis
504 //purpose : return true if theHypothesis can be attached to me:
505 // its dimention is checked
506 //=======================================================================
508 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
510 int aHypDim = theHypothesis->GetDim();
511 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
512 if (aHypDim == 3 && aShapeDim == 3) {
513 // check case of open shell
514 //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
515 if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
518 if ( aHypDim <= aShapeDim )
524 //=======================================================================
525 //function : IsApplicableHypotesis
527 //=======================================================================
529 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
530 const TopAbs_ShapeEnum theShapeType)
532 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
534 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
537 switch ( theShapeType ) {
542 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
545 // Special case for algorithms, building 2D mesh on a whole shell.
546 // Before this fix there was a problem after restoring from study,
547 // because in that case algorithm is assigned before hypothesis
548 // (on shell in problem case) and hypothesis is checked on faces
549 // (because it is 2D), where we have NO_ALGO state.
550 // Now 2D hypothesis is also applicable to shells.
551 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
554 // case TopAbs_COMPSOLID:
555 // case TopAbs_COMPOUND:
561 //=============================================================================
565 //=============================================================================
567 SMESH_Hypothesis::Hypothesis_Status
568 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
570 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
571 //SCRUTE(_algoState);
574 // **** les retour des evenement shape sont significatifs
575 // (add ou remove fait ou non)
576 // le retour des evenement father n'indiquent pas que add ou remove fait
578 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
580 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
581 SMESH_Gen* gen =_father->GetGen();
582 SMESH_Algo* algo = 0;
584 if (_subShape.ShapeType() == TopAbs_VERTEX )
586 if ( anHyp->GetDim() != 0) {
587 if (event == ADD_HYP || event == ADD_ALGO)
588 return SMESH_Hypothesis::HYP_BAD_DIM;
590 return SMESH_Hypothesis::HYP_OK;
593 else if ( _algoState == HYP_OK ) {
594 // update default _algoState
595 if ( event != REMOVE_FATHER_ALGO )
597 _algoState = NO_ALGO;
598 algo = gen->GetAlgo(*_father, _subShape);
600 _algoState = MISSING_HYP;
601 if ( event == REMOVE_FATHER_HYP ||
602 algo->CheckHypothesis(*_father,_subShape, aux_ret))
609 int oldAlgoState = _algoState;
610 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
611 bool needFullClean = false;
613 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
615 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
617 // -------------------------------------------
618 // check if a shape needed by algo is present
619 // -------------------------------------------
620 algo = static_cast< SMESH_Algo* >( anHyp );
621 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
622 return SMESH_Hypothesis::HYP_NEED_SHAPE;
623 // ----------------------
624 // check mesh conformity
625 // ----------------------
626 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
627 return SMESH_Hypothesis::HYP_NOTCONFORM;
629 // check if all-dimensional algo is hidden by other local one
630 if ( event == ADD_ALGO ) {
631 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
632 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
633 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
634 if ( SMESH_Algo * curAlgo = (SMESH_Algo*) _father->GetHypothesis( _subShape, filter, true ))
635 needFullClean = ( !curAlgo->NeedDescretBoundary() );
639 // ----------------------------------
640 // add a hypothesis to DS if possible
641 // ----------------------------------
642 if (event == ADD_HYP || event == ADD_ALGO)
644 if ( ! CanAddHypothesis( anHyp )) // check dimension
645 return SMESH_Hypothesis::HYP_BAD_DIM;
647 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
648 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
650 if ( !meshDS->AddHypothesis(_subShape, anHyp))
651 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
654 // --------------------------
655 // remove a hypothesis from DS
656 // --------------------------
657 if (event == REMOVE_HYP || event == REMOVE_ALGO)
659 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
660 return SMESH_Hypothesis::HYP_OK; // nothing changes
662 if (event == REMOVE_ALGO)
664 algo = dynamic_cast<SMESH_Algo*> (anHyp);
665 if (!algo->NeedDescretBoundary())
667 // clean all mesh in the tree of the current submesh;
668 // we must perform it now because later
669 // we will have no information about the type of the removed algo
670 needFullClean = true;
675 // ------------------
676 // analyse algo state
677 // ------------------
678 if (!isApplicableHyp)
679 return ret; // not applicable hypotheses do not change algo state
684 // ----------------------------------------------------------------------
691 algo = gen->GetAlgo((*_father), _subShape);
693 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
694 SetAlgoState(HYP_OK);
695 else if ( algo->IsStatusFatal( aux_ret )) {
696 meshDS->RemoveHypothesis(_subShape, anHyp);
700 SetAlgoState(MISSING_HYP);
707 case ADD_FATHER_ALGO: { // Algo just added in father
708 algo = gen->GetAlgo((*_father), _subShape);
710 if ( algo == anHyp ) {
711 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
712 SetAlgoState(HYP_OK);
714 SetAlgoState(MISSING_HYP);
718 case REMOVE_FATHER_HYP:
720 case REMOVE_FATHER_ALGO: {
721 algo = gen->GetAlgo((*_father), _subShape);
724 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
725 SetAlgoState(HYP_OK);
727 SetAlgoState(MISSING_HYP);
731 case MODIF_HYP: break;
738 // ----------------------------------------------------------------------
744 algo = gen->GetAlgo((*_father), _subShape);
746 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
747 SetAlgoState(HYP_OK);
748 if (SMESH_Hypothesis::IsStatusFatal( ret ))
749 meshDS->RemoveHypothesis(_subShape, anHyp);
750 else if (!_father->IsUsedHypothesis( anHyp, this ))
752 meshDS->RemoveHypothesis(_subShape, anHyp);
753 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
757 case ADD_ALGO: { //already existing algo : on father ?
758 algo = gen->GetAlgo((*_father), _subShape);
760 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
761 SetAlgoState(HYP_OK);
762 else if ( algo->IsStatusFatal( aux_ret )) {
763 meshDS->RemoveHypothesis(_subShape, anHyp);
767 SetAlgoState(MISSING_HYP);
772 case REMOVE_ALGO: { // perhaps a father algo applies ?
773 algo = gen->GetAlgo((*_father), _subShape);
774 if (algo == NULL) // no more algo applying on subShape...
776 SetAlgoState(NO_ALGO);
780 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
781 SetAlgoState(HYP_OK);
783 SetAlgoState(MISSING_HYP);
787 case MODIF_HYP: // assigned hypothesis value may become good
788 case ADD_FATHER_HYP: {
789 algo = gen->GetAlgo((*_father), _subShape);
791 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
792 SetAlgoState(HYP_OK);
794 SetAlgoState(MISSING_HYP);
797 case ADD_FATHER_ALGO: { // new father algo
798 algo = gen->GetAlgo((*_father), _subShape);
800 if ( algo == anHyp ) {
801 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
802 SetAlgoState(HYP_OK);
804 SetAlgoState(MISSING_HYP);
808 case REMOVE_FATHER_HYP: // nothing to do
810 case REMOVE_FATHER_ALGO: {
811 algo = gen->GetAlgo((*_father), _subShape);
812 if (algo == NULL) // no more applying algo on father
814 SetAlgoState(NO_ALGO);
818 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
819 SetAlgoState(HYP_OK);
821 SetAlgoState(MISSING_HYP);
831 // ----------------------------------------------------------------------
837 algo = gen->GetAlgo((*_father), _subShape);
839 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
841 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
842 // ret should be fatal: anHyp was not added
843 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
845 else if (!_father->IsUsedHypothesis( anHyp, this ))
846 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
848 if (SMESH_Hypothesis::IsStatusFatal( ret ))
850 MESSAGE("do not add extra hypothesis");
851 meshDS->RemoveHypothesis(_subShape, anHyp);
859 case ADD_ALGO: { //already existing algo : on father ?
860 algo = gen->GetAlgo((*_father), _subShape);
861 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
862 // check if algo changes
864 f.Init( SMESH_HypoFilter::IsAlgo() );
865 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
866 f.AndNot( SMESH_HypoFilter::Is( algo ));
867 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
869 string(algo->GetName()) != string(prevAlgo->GetName()) )
873 SetAlgoState(MISSING_HYP);
877 algo = gen->GetAlgo((*_father), _subShape);
879 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
880 SetAlgoState(HYP_OK);
882 SetAlgoState(MISSING_HYP);
886 case REMOVE_ALGO: { // perhaps a father algo applies ?
887 algo = gen->GetAlgo((*_father), _subShape);
888 if (algo == NULL) // no more algo applying on subShape...
890 SetAlgoState(NO_ALGO);
894 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
895 // check if algo remains
896 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
900 SetAlgoState(MISSING_HYP);
904 case MODIF_HYP: // hypothesis value may become bad
905 case ADD_FATHER_HYP: { // new father hypothesis ?
906 algo = gen->GetAlgo((*_father), _subShape);
908 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
910 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
914 SetAlgoState(MISSING_HYP);
917 case ADD_FATHER_ALGO: {
918 algo = gen->GetAlgo((*_father), _subShape);
919 if ( algo == anHyp ) { // a new algo on father
920 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
921 // check if algo changes
923 f.Init( SMESH_HypoFilter::IsAlgo() );
924 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
925 f.AndNot( SMESH_HypoFilter::Is( algo ));
926 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
928 string(algo->GetName()) != string(prevAlgo->GetName()) )
932 SetAlgoState(MISSING_HYP);
936 case REMOVE_FATHER_HYP: {
937 algo = gen->GetAlgo((*_father), _subShape);
939 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
940 // is there the same local hyp or maybe a new father algo applied?
941 if ( !GetSimilarAttached( _subShape, anHyp ) )
945 SetAlgoState(MISSING_HYP);
948 case REMOVE_FATHER_ALGO: {
949 algo = gen->GetAlgo((*_father), _subShape);
950 if (algo == NULL) // no more applying algo on father
952 SetAlgoState(NO_ALGO);
956 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
957 // check if algo changes
958 if ( string(algo->GetName()) != string( anHyp->GetName()) )
962 SetAlgoState(MISSING_HYP);
972 // ----------------------------------------------------------------------
979 // detect algorithm hiding
981 if ( ret == SMESH_Hypothesis::HYP_OK &&
982 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
983 algo->GetName() == anHyp->GetName() )
986 SMESH_Gen* gen = _father->GetGen();
987 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
988 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
989 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
990 if ( !upperAlgo->NeedDescretBoundary() && !upperAlgo->SupportSubmeshes())
991 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
994 if ( ret == SMESH_Hypothesis::HYP_OK &&
995 !algo->NeedDescretBoundary() &&
996 !algo->SupportSubmeshes()) {
997 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
998 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
999 if ( gen->GetAlgo( *_father, i_sm->second->_subShape ))
1000 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1004 bool stateChange = ( _algoState != oldAlgoState );
1006 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1007 algo->SetEventListener( this );
1009 NotifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1011 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1012 DeleteOwnListeners();
1013 SetIsAlwaysComputed( false );
1014 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1015 // restore default states
1016 _algoState = HYP_OK;
1017 _computeState = READY_TO_COMPUTE;
1021 if ( needFullClean ) {
1022 // added or removed algo is all-dimensional
1023 ComputeStateEngine( CLEAN );
1025 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1028 if (stateChange || modifiedHyp)
1029 ComputeStateEngine(MODIF_ALGO_STATE);
1034 //=======================================================================
1035 //function : IsConform
1036 //purpose : check if a conform mesh will be produced by the Algo
1037 //=======================================================================
1039 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1041 // MESSAGE( "SMESH_subMesh::IsConform" );
1042 if ( !theAlgo ) return false;
1044 // Suppose that theAlgo is applicable to _subShape, do not check it here
1045 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1047 // check only algo that doesn't NeedDescretBoundary(): because mesh made
1048 // on a sub-shape will be ignored by theAlgo
1049 if ( theAlgo->NeedDescretBoundary() ||
1050 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1053 SMESH_Gen* gen =_father->GetGen();
1055 // only local algo is to be checked
1056 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1057 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1060 // check algo attached to adjacent shapes
1062 // loop on one level down sub-meshes
1063 TopoDS_Iterator itsub( _subShape );
1064 for (; itsub.More(); itsub.Next())
1066 // loop on adjacent subShapes
1067 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1068 for (; it.More(); it.Next())
1070 const TopoDS_Shape& adjacent = it.Value();
1071 if ( _subShape.IsSame( adjacent )) continue;
1072 if ( adjacent.ShapeType() != _subShape.ShapeType())
1075 // check algo attached to smAdjacent
1076 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1078 !algo->NeedDescretBoundary() &&
1079 algo->OnlyUnaryInput())
1080 return false; // NOT CONFORM MESH WILL BE PRODUCED
1087 //=============================================================================
1091 //=============================================================================
1093 void SMESH_subMesh::SetAlgoState(int state)
1098 //=============================================================================
1102 //=============================================================================
1103 SMESH_Hypothesis::Hypothesis_Status
1104 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1105 SMESH_Hypothesis * anHyp)
1107 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1108 //EAP: a wire (dim==1) should notify edges (dim==1)
1109 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1110 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1112 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1113 while ( smIt->more() ) {
1114 SMESH_Hypothesis::Hypothesis_Status ret2 =
1115 smIt->next()->AlgoStateEngine(event, anHyp);
1123 //=============================================================================
1127 //=============================================================================
1129 void SMESH_subMesh::CleanDependsOn()
1131 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1132 while ( smIt->more() )
1133 smIt->next()->ComputeStateEngine(CLEAN);
1136 //=============================================================================
1140 //=============================================================================
1142 void SMESH_subMesh::DumpAlgoState(bool isMain)
1144 int dim = SMESH_Gen::GetShapeDim(_subShape);
1145 // if (dim < 1) return;
1148 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1150 map < int, SMESH_subMesh * >::const_iterator itsub;
1151 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1153 SMESH_subMesh *sm = (*itsub).second;
1154 sm->DumpAlgoState(false);
1157 int type = _subShape.ShapeType();
1158 MESSAGE("dim = " << dim << " type of shape " << type);
1162 MESSAGE(" AlgoState = NO_ALGO");
1165 MESSAGE(" AlgoState = MISSING_HYP");
1168 MESSAGE(" AlgoState = HYP_OK");
1171 switch (_computeState)
1174 MESSAGE(" ComputeState = NOT_READY");
1176 case READY_TO_COMPUTE:
1177 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1180 MESSAGE(" ComputeState = COMPUTE_OK");
1182 case FAILED_TO_COMPUTE:
1183 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1188 //================================================================================
1190 * \brief Remove nodes and elements bound to submesh
1191 * \param subMesh - submesh containing nodes and elements
1193 //================================================================================
1195 static void cleanSubMesh( SMESH_subMesh * subMesh )
1198 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1199 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1200 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1201 while (ite->more()) {
1202 const SMDS_MeshElement * elt = ite->next();
1203 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1204 //meshDS->RemoveElement(elt);
1205 meshDS->RemoveFreeElement(elt, subMeshDS);
1208 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1209 while (itn->more()) {
1210 const SMDS_MeshNode * node = itn->next();
1211 //MESSAGE( " RM node: "<<node->GetID());
1212 if ( node->NbInverseElements() == 0 )
1213 meshDS->RemoveFreeNode(node, subMeshDS);
1214 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1215 meshDS->RemoveNode(node);
1221 //=============================================================================
1225 //=============================================================================
1227 bool SMESH_subMesh::ComputeStateEngine(int event)
1229 _computeError.reset();
1231 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1232 //SCRUTE(_computeState);
1235 if (_subShape.ShapeType() == TopAbs_VERTEX)
1237 _computeState = READY_TO_COMPUTE;
1238 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1239 if ( smDS && smDS->NbNodes() ) {
1240 if ( event == CLEAN ) {
1242 cleanSubMesh( this );
1245 _computeState = COMPUTE_OK;
1247 else if ( event == COMPUTE && !_alwaysComputed ) {
1248 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1249 gp_Pnt P = BRep_Tool::Pnt(V);
1250 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1251 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1252 _computeState = COMPUTE_OK;
1255 if ( event == MODIF_ALGO_STATE )
1259 SMESH_Gen *gen = _father->GetGen();
1260 SMESH_Algo *algo = 0;
1262 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1263 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1265 switch (_computeState)
1268 // ----------------------------------------------------------------------
1273 case MODIF_ALGO_STATE:
1274 algo = gen->GetAlgo((*_father), _subShape);
1275 if (algo && !algo->NeedDescretBoundary())
1276 CleanDependsOn(); // clean sub-meshes with event CLEAN
1277 if ( _algoState == HYP_OK )
1278 _computeState = READY_TO_COMPUTE;
1280 case COMPUTE: // nothing to do
1284 RemoveSubMeshElementsAndNodes();
1286 case SUBMESH_COMPUTED: // nothing to do
1288 case SUBMESH_RESTORED:
1289 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1291 case MESH_ENTITY_REMOVED:
1293 case CHECK_COMPUTE_STATE:
1294 if ( IsMeshComputed() )
1295 _computeState = COMPUTE_OK;
1303 // ----------------------------------------------------------------------
1305 case READY_TO_COMPUTE:
1308 case MODIF_ALGO_STATE:
1309 _computeState = NOT_READY;
1310 algo = gen->GetAlgo((*_father), _subShape);
1313 if (!algo->NeedDescretBoundary())
1314 CleanDependsOn(); // clean sub-meshes with event CLEAN
1315 if ( _algoState == HYP_OK )
1316 _computeState = READY_TO_COMPUTE;
1321 algo = gen->GetAlgo((*_father), _subShape);
1323 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1326 MESSAGE("***** verify compute state *****");
1327 _computeState = NOT_READY;
1328 SetAlgoState(MISSING_HYP);
1331 TopoDS_Shape shape = _subShape;
1332 // check submeshes needed
1333 if (_father->HasShapeToMesh() ) {
1334 bool subComputed = false;
1335 if (!algo->OnlyUnaryInput())
1336 shape = GetCollection( gen, algo, subComputed );
1338 subComputed = SubMeshesComputed();
1339 ret = ( algo->NeedDescretBoundary() ? subComputed :
1340 algo->SupportSubmeshes() ? true :
1341 ( !subComputed || _father->IsNotConformAllowed() ));
1343 _computeState = FAILED_TO_COMPUTE;
1344 if ( !algo->NeedDescretBoundary() )
1346 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1347 "Unexpected computed submesh",algo);
1352 // CleanDependants(); for "UseExisting_*D" algos
1353 // RemoveSubMeshElementsAndNodes();
1355 _computeState = FAILED_TO_COMPUTE;
1356 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1358 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1361 algo->InitComputeError();
1362 MemoryReserve aMemoryReserve;
1363 SMDS_Mesh::CheckMemory();
1364 if ( !_father->HasShapeToMesh() ) // no shape
1366 SMESH_MesherHelper helper( *_father );
1367 helper.SetSubShape( shape );
1368 helper.SetElementsOnShape( true );
1369 ret = algo->Compute(*_father, &helper );
1373 ret = algo->Compute((*_father), shape);
1375 if ( !_computeError || ( !ret && _computeError->IsOK() ) ) // algo can set _computeError of submesh
1376 _computeError = algo->GetComputeError();
1378 catch ( std::bad_alloc& exc ) {
1379 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1380 if ( _computeError ) {
1381 _computeError->myName = COMPERR_MEMORY_PB;
1382 //_computeError->myComment = exc.what();
1384 cleanSubMesh( this );
1387 catch ( Standard_OutOfMemory& exc ) {
1388 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1389 if ( _computeError ) {
1390 _computeError->myName = COMPERR_MEMORY_PB;
1391 //_computeError->myComment = exc.what();
1393 cleanSubMesh( this );
1394 throw std::bad_alloc();
1396 catch (Standard_Failure& ex) {
1397 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1398 _computeError->myName = COMPERR_OCC_EXCEPTION;
1399 _computeError->myComment += ex.DynamicType()->Name();
1400 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1401 _computeError->myComment += ": ";
1402 _computeError->myComment += ex.GetMessageString();
1405 catch ( SALOME_Exception& S_ex ) {
1406 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1407 _computeError->myName = COMPERR_SLM_EXCEPTION;
1408 _computeError->myComment = S_ex.what();
1410 catch ( std::exception& exc ) {
1411 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1412 _computeError->myName = COMPERR_STD_EXCEPTION;
1413 _computeError->myComment = exc.what();
1416 if ( _computeError )
1417 _computeError->myName = COMPERR_EXCEPTION;
1421 if (ret && !_alwaysComputed && shape == _subShape) { // check if anything was built
1422 ret = ( GetSubMeshDS() && ( GetSubMeshDS()->NbElements() || GetSubMeshDS()->NbNodes() ));
1424 bool isComputeErrorSet = !CheckComputeError( algo, shape );
1425 if (!ret && !isComputeErrorSet)
1427 // Set _computeError
1428 if ( !_computeError )
1429 _computeError = SMESH_ComputeError::New();
1430 if ( _computeError->IsOK() )
1431 _computeError->myName = COMPERR_ALGO_FAILED;
1432 _computeState = FAILED_TO_COMPUTE;
1436 _computeError.reset();
1438 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1443 RemoveSubMeshElementsAndNodes();
1444 _computeState = NOT_READY;
1445 algo = gen->GetAlgo((*_father), _subShape);
1448 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1450 _computeState = READY_TO_COMPUTE;
1452 SetAlgoState(MISSING_HYP);
1455 case SUBMESH_COMPUTED: // nothing to do
1457 case SUBMESH_RESTORED:
1458 // check if a mesh is already computed that may
1459 // happen after retrieval from a file
1460 ComputeStateEngine( CHECK_COMPUTE_STATE );
1461 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1462 algo = gen->GetAlgo(*_father, _subShape);
1463 if (algo) algo->SubmeshRestored( this );
1465 case MESH_ENTITY_REMOVED:
1467 case CHECK_COMPUTE_STATE:
1468 if ( IsMeshComputed() )
1469 _computeState = COMPUTE_OK;
1477 // ----------------------------------------------------------------------
1482 case MODIF_ALGO_STATE:
1483 ComputeStateEngine( CLEAN );
1484 algo = gen->GetAlgo((*_father), _subShape);
1485 if (algo && !algo->NeedDescretBoundary())
1486 CleanDependsOn(); // clean sub-meshes with event CLEAN
1488 case COMPUTE: // nothing to do
1491 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1492 RemoveSubMeshElementsAndNodes();
1493 _computeState = NOT_READY;
1494 if ( _algoState == HYP_OK )
1495 _computeState = READY_TO_COMPUTE;
1497 case SUBMESH_COMPUTED: // nothing to do
1499 case SUBMESH_RESTORED:
1500 ComputeStateEngine( CHECK_COMPUTE_STATE );
1501 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1502 algo = gen->GetAlgo(*_father, _subShape);
1503 if (algo) algo->SubmeshRestored( this );
1505 case MESH_ENTITY_REMOVED:
1506 UpdateDependantsState( CHECK_COMPUTE_STATE );
1507 ComputeStateEngine( CHECK_COMPUTE_STATE );
1508 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1510 case CHECK_COMPUTE_STATE:
1511 if ( !IsMeshComputed() )
1512 if (_algoState == HYP_OK)
1513 _computeState = READY_TO_COMPUTE;
1515 _computeState = NOT_READY;
1523 // ----------------------------------------------------------------------
1525 case FAILED_TO_COMPUTE:
1528 case MODIF_ALGO_STATE:
1529 algo = gen->GetAlgo((*_father), _subShape);
1530 if (algo && !algo->NeedDescretBoundary())
1531 CleanDependsOn(); // clean sub-meshes with event CLEAN
1532 if (_algoState == HYP_OK)
1533 _computeState = READY_TO_COMPUTE;
1535 _computeState = NOT_READY;
1537 case COMPUTE: // nothing to do
1540 CleanDependants(); // submeshes dependent on me should be cleaned as well
1541 RemoveSubMeshElementsAndNodes();
1543 case SUBMESH_COMPUTED: // allow retry compute
1544 if (_algoState == HYP_OK)
1545 _computeState = READY_TO_COMPUTE;
1547 _computeState = NOT_READY;
1549 case SUBMESH_RESTORED:
1550 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1552 case MESH_ENTITY_REMOVED:
1554 case CHECK_COMPUTE_STATE:
1555 if ( IsMeshComputed() )
1556 _computeState = COMPUTE_OK;
1558 if (_algoState == HYP_OK)
1559 _computeState = READY_TO_COMPUTE;
1561 _computeState = NOT_READY;
1569 // ----------------------------------------------------------------------
1575 NotifyListenersOnEvent( event, COMPUTE_EVENT );
1580 //=======================================================================
1582 * \brief Update compute_state by _computeError and send proper events to
1583 * dependent submeshes
1584 * \retval bool - true if _computeError is NOT set
1586 //=======================================================================
1588 bool SMESH_subMesh::CheckComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1590 bool noErrors = true;
1592 if ( !theShape.IsNull() )
1594 // Check state of submeshes
1595 if ( !theAlgo->NeedDescretBoundary())
1597 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1598 while ( smIt->more() )
1599 if ( !smIt->next()->CheckComputeError( theAlgo ))
1603 // Check state of neighbours
1604 if ( !theAlgo->OnlyUnaryInput() &&
1605 theShape.ShapeType() == TopAbs_COMPOUND &&
1606 !theShape.IsSame( _subShape ))
1608 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1609 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1611 if ( !sm->CheckComputeError( theAlgo, sm->GetSubShape() ))
1613 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1620 if ( !_computeError || _computeError->IsOK() )
1622 _computeState = COMPUTE_OK;
1626 if ( !_computeError->myAlgo )
1627 _computeError->myAlgo = theAlgo;
1631 text << theAlgo->GetName() << " failed on subshape #" << _Id << " with error ";
1632 if (_computeError->IsCommon() )
1633 text << _computeError->CommonName();
1635 text << _computeError->myName;
1636 if ( _computeError->myComment.size() > 0 )
1637 text << " \"" << _computeError->myComment << "\"";
1640 MESSAGE_BEGIN ( text );
1641 // Show vertices location of a failed shape
1642 TopTools_IndexedMapOfShape vMap;
1643 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1644 MESSAGE_ADD ( "Subshape vertices " << ( vMap.Extent()>10 ? "(first 10):" : ":") );
1645 for ( int iv = 1; iv <= vMap.Extent() && iv < 11; ++iv ) {
1646 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1647 MESSAGE_ADD ( "#" << _father->GetMeshDS()->ShapeToIndex( vMap( iv )) << " "
1648 << P.X() << " " << P.Y() << " " << P.Z() << " " );
1653 _computeState = FAILED_TO_COMPUTE;
1660 //=======================================================================
1661 //function : ApplyToCollection
1662 //purpose : Apply theAlgo to all subshapes in theCollection
1663 //=======================================================================
1665 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1666 const TopoDS_Shape& theCollection)
1668 MESSAGE("SMESH_subMesh::ApplyToCollection");
1669 ASSERT ( !theAlgo->NeedDescretBoundary() );
1671 if ( _computeError )
1672 _computeError->myName = COMPERR_OK;
1674 bool ok = theAlgo->Compute( *_father, theCollection );
1676 // set _computeState of subshapes
1677 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1678 for ( ; anExplorer.More(); anExplorer.Next() )
1680 if ( SMESH_subMesh* subMesh = _father->GetSubMeshContaining( anExplorer.Current() ))
1682 bool localOK = subMesh->CheckComputeError( theAlgo );
1683 if ( !ok && localOK && !subMesh->IsMeshComputed() )
1685 subMesh->_computeError = theAlgo->GetComputeError();
1686 if ( subMesh->_computeError->IsOK() )
1687 _computeError = SMESH_ComputeError::New(COMPERR_ALGO_FAILED);
1688 localOK = CheckComputeError( theAlgo );
1691 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1692 subMesh->UpdateSubMeshState( localOK ? COMPUTE_OK : FAILED_TO_COMPUTE );
1700 //=======================================================================
1701 //function : UpdateSubMeshState
1703 //=======================================================================
1705 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1707 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1708 while ( smIt->more() )
1709 smIt->next()->_computeState = theState;
1712 //=======================================================================
1713 //function : ComputeSubMeshStateEngine
1715 //=======================================================================
1717 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1719 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1720 while ( smIt->more() )
1721 smIt->next()->ComputeStateEngine(event);
1724 //=======================================================================
1725 //function : UpdateDependantsState
1727 //=======================================================================
1729 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1731 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1732 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1733 for (; it.More(); it.Next())
1735 const TopoDS_Shape& ancestor = it.Value();
1736 SMESH_subMesh *aSubMesh =
1737 _father->GetSubMeshContaining(ancestor);
1739 aSubMesh->ComputeStateEngine( theEvent );
1743 //=============================================================================
1747 //=============================================================================
1749 void SMESH_subMesh::CleanDependants()
1751 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1753 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1754 for (; it.More(); it.Next())
1756 const TopoDS_Shape& ancestor = it.Value();
1757 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1758 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1759 // will erase mesh on other shapes in a compound
1760 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1761 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1763 aSubMesh->ComputeStateEngine(CLEAN);
1769 //=============================================================================
1773 //=============================================================================
1775 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1777 //SCRUTE(_subShape.ShapeType());
1779 cleanSubMesh( this );
1781 // algo may bind a submesh not to _subShape, eg 3D algo
1782 // sets nodes on SHELL while _subShape may be SOLID
1784 int dim = SMESH_Gen::GetShapeDim( _subShape );
1785 int type = _subShape.ShapeType() + 1;
1786 for ( ; type <= TopAbs_EDGE; type++) {
1787 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1789 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1790 for ( ; exp.More(); exp.Next() )
1791 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1798 //=======================================================================
1799 //function : GetCollection
1800 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1801 // meshed at once along with _subShape
1802 //=======================================================================
1804 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen,
1805 SMESH_Algo* theAlgo,
1806 bool & theSubComputed)
1808 MESSAGE("SMESH_subMesh::GetCollection");
1810 theSubComputed = SubMeshesComputed();
1812 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1814 if ( mainShape.IsSame( _subShape ))
1817 const bool ignoreAuxiliaryHyps = false;
1818 list<const SMESHDS_Hypothesis*> aUsedHyp =
1819 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1821 // put in a compound all shapes with the same hypothesis assigned
1822 // and a good ComputState
1824 TopoDS_Compound aCompound;
1825 BRep_Builder aBuilder;
1826 aBuilder.MakeCompound( aCompound );
1828 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1829 for ( ; anExplorer.More(); anExplorer.Next() )
1831 const TopoDS_Shape& S = anExplorer.Current();
1832 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1833 if ( subMesh == this )
1835 aBuilder.Add( aCompound, S );
1837 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1839 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1840 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1841 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1842 aBuilder.Add( aCompound, S );
1843 if ( !subMesh->SubMeshesComputed() )
1844 theSubComputed = false;
1851 //=======================================================================
1852 //function : GetSimilarAttached
1853 //purpose : return a hypothesis attached to theShape.
1854 // If theHyp is provided, similar but not same hypotheses
1855 // is returned; else only applicable ones having theHypType
1857 //=======================================================================
1859 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1860 const SMESH_Hypothesis * theHyp,
1861 const int theHypType)
1863 SMESH_HypoFilter hypoKind;
1864 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1866 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1867 hypoKind.AndNot( hypoKind.Is( theHyp ));
1868 if ( theHyp->IsAuxiliary() )
1869 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1871 hypoKind.AndNot( hypoKind.IsAuxiliary());
1874 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1877 return _father->GetHypothesis( theShape, hypoKind, false );
1880 //=======================================================================
1881 //function : CheckConcurentHypothesis
1882 //purpose : check if there are several applicable hypothesis attached to
1884 //=======================================================================
1886 SMESH_Hypothesis::Hypothesis_Status
1887 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1889 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1891 // is there local hypothesis on me?
1892 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1893 return SMESH_Hypothesis::HYP_OK;
1896 TopoDS_Shape aPrevWithHyp;
1897 const SMESH_Hypothesis* aPrevHyp = 0;
1898 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1899 for (; it.More(); it.Next())
1901 const TopoDS_Shape& ancestor = it.Value();
1902 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1905 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1907 aPrevWithHyp = ancestor;
1910 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1911 return SMESH_Hypothesis::HYP_CONCURENT;
1913 return SMESH_Hypothesis::HYP_OK;
1916 return SMESH_Hypothesis::HYP_OK;
1919 //================================================================================
1921 * \brief Sets an event listener and its data to a submesh
1922 * \param listener - the listener to store
1923 * \param data - the listener data to store
1924 * \param where - the submesh to store the listener and it's data
1925 * \param deleteListener - if true then the listener will be deleted as
1926 * it is removed from where submesh
1928 * It remembers the submesh where it puts the listener in order to delete
1929 * them when HYP_OK algo_state is lost
1930 * After being set, event listener is notified on each event of where submesh.
1932 //================================================================================
1934 void SMESH_subMesh::SetEventListener(EventListener* listener,
1935 EventListenerData* data,
1936 SMESH_subMesh* where)
1938 if ( listener && where ) {
1939 where->SetEventListener( listener, data );
1940 myOwnListeners.push_back( make_pair( where, listener ));
1944 //================================================================================
1946 * \brief Sets an event listener and its data to a submesh
1947 * \param listener - the listener to store
1948 * \param data - the listener data to store
1950 * After being set, event listener is notified on each event of a submesh.
1952 //================================================================================
1954 void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
1956 map< EventListener*, EventListenerData* >::iterator l_d =
1957 myEventListeners.find( listener );
1958 if ( l_d != myEventListeners.end() ) {
1959 EventListenerData* curData = l_d->second;
1960 if ( curData && curData != data && curData->IsDeletable() )
1965 myEventListeners.insert( make_pair( listener, data ));
1968 //================================================================================
1970 * \brief Return an event listener data
1971 * \param listener - the listener whose data is
1972 * \retval EventListenerData* - found data, maybe NULL
1974 //================================================================================
1976 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
1978 map< EventListener*, EventListenerData* >::const_iterator l_d =
1979 myEventListeners.find( listener );
1980 if ( l_d != myEventListeners.end() )
1985 //================================================================================
1987 * \brief Notify stored event listeners on the occured event
1988 * \param event - algo_event or compute_event itself
1989 * \param eventType - algo_event or compute_event
1990 * \param subMesh - the submesh where the event occures
1991 * \param data - listener data stored in the subMesh
1992 * \param hyp - hypothesis, if eventType is algo_event
1994 //================================================================================
1996 void SMESH_subMesh::NotifyListenersOnEvent( const int event,
1997 const event_type eventType,
1998 SMESH_Hypothesis* hyp)
2000 map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
2001 for ( ; l_d != myEventListeners.end(); ++l_d )
2002 l_d->first->ProcessEvent( event, eventType, this, l_d->second, hyp );
2005 //================================================================================
2007 * \brief Unregister the listener and delete listener's data
2008 * \param listener - the event listener
2010 //================================================================================
2012 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2014 map< EventListener*, EventListenerData* >::iterator l_d =
2015 myEventListeners.find( listener );
2016 if ( l_d != myEventListeners.end() ) {
2017 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2018 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2019 myEventListeners.erase( l_d );
2023 //================================================================================
2025 * \brief Delete event listeners depending on algo of this submesh
2027 //================================================================================
2029 void SMESH_subMesh::DeleteOwnListeners()
2031 list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
2032 for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
2033 sm_l->first->DeleteEventListener( sm_l->second );
2034 myOwnListeners.clear();
2037 //================================================================================
2039 * \brief Do something on a certain event
2040 * \param event - algo_event or compute_event itself
2041 * \param eventType - algo_event or compute_event
2042 * \param subMesh - the submesh where the event occures
2043 * \param data - listener data stored in the subMesh
2044 * \param hyp - hypothesis, if eventType is algo_event
2046 * The base implementation translates CLEAN event to the subMesh
2047 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2048 * successful COMPUTE event.
2050 //================================================================================
2052 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2053 const int eventType,
2054 SMESH_subMesh* subMesh,
2055 EventListenerData* data,
2056 const SMESH_Hypothesis* /*hyp*/)
2058 if ( data && !data->mySubMeshes.empty() &&
2059 eventType == SMESH_subMesh::COMPUTE_EVENT)
2061 ASSERT( data->mySubMeshes.front() != subMesh );
2062 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2063 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2065 case SMESH_subMesh::CLEAN:
2066 for ( ; smIt != smEnd; ++ smIt)
2067 (*smIt)->ComputeStateEngine( event );
2069 case SMESH_subMesh::COMPUTE:
2070 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2071 for ( ; smIt != smEnd; ++ smIt)
2072 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2081 //================================================================================
2083 * \brief Iterator over submeshes and optionally prepended or appended one
2085 //================================================================================
2087 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2089 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2090 SMESH_subMesh* prepend,
2091 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2093 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2094 if ( myCur == append ) append = 0;
2096 /// Return true if and only if there are other object in this iterator
2101 /// Return the current object and step to the next one
2102 virtual SMESH_subMesh* next()
2104 SMESH_subMesh* res = myCur;
2105 if ( myIt->more() ) { myCur = myIt->next(); }
2106 else { myCur = myAppend; myAppend = 0; }
2113 SMESH_subMesh *myAppend, *myCur;
2114 SMDS_Iterator<SMESH_subMesh*> *myIt;
2118 //================================================================================
2120 * \brief Return iterator on the submeshes this one depends on
2121 * \param includeSelf - this submesh to be returned also
2122 * \param reverse - if true, complex shape submeshes go first
2124 //================================================================================
2126 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2129 SMESH_subMesh *prepend=0, *append=0;
2130 if ( includeSelf ) {
2131 if ( reverse ) prepend = this;
2134 typedef map < int, SMESH_subMesh * > TMap;
2137 return SMESH_subMeshIteratorPtr
2138 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2141 return SMESH_subMeshIteratorPtr
2142 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));