1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_subMesh.hxx"
30 #include "SMESH_Algo.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_HypoFilter.hxx"
33 #include "SMESH_Hypothesis.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "SMESH_MesherHelper.hxx"
36 #include "SMESH_subMeshEventListener.hxx"
37 #include "SMESH_Comment.hxx"
38 #include "SMDS_SetIterator.hxx"
39 #include "SMDSAbs_ElementType.hxx"
41 #include "utilities.h"
43 #include "Basics_Utils.hxx"
45 #include <BRep_Builder.hxx>
46 #include <BRep_Tool.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <TopoDS_Compound.hxx>
53 #include <TopExp_Explorer.hxx>
54 #include <TopoDS_Iterator.hxx>
56 #include <Standard_OutOfMemory.hxx>
57 #include <Standard_ErrorHandler.hxx>
63 //=============================================================================
65 * \brief Allocate some memory at construction and release it at destruction.
66 * Is used to be able to continue working after mesh generation breaks due to
69 //=============================================================================
74 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
75 ~MemoryReserve() { delete [] myBuf; }
78 //=============================================================================
80 * default constructor:
82 //=============================================================================
84 SMESH_subMesh::SMESH_subMesh(int Id,
86 SMESHDS_Mesh * meshDS,
87 const TopoDS_Shape & aSubShape)
89 _subShape = aSubShape;
90 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
93 _dependenceAnalysed = _alwaysComputed = false;
95 if (_subShape.ShapeType() == TopAbs_VERTEX)
98 _computeState = READY_TO_COMPUTE;
102 _algoState = NO_ALGO;
103 _computeState = NOT_READY;
107 //=============================================================================
111 //=============================================================================
113 SMESH_subMesh::~SMESH_subMesh()
115 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
117 DeleteOwnListeners();
120 //=============================================================================
124 //=============================================================================
126 int SMESH_subMesh::GetId() const
128 //MESSAGE("SMESH_subMesh::GetId");
132 //=============================================================================
136 //=============================================================================
138 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
140 // submesh appears in DS only when a mesher set nodes and elements on a shape
141 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
144 //=============================================================================
148 //=============================================================================
150 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
152 if ( !GetSubMeshDS() ) {
153 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
154 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
156 return GetSubMeshDS();
159 //=============================================================================
163 //=============================================================================
165 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
167 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
168 while ( smIt->more() ) {
169 SMESH_subMesh *sm = smIt->next();
170 if ( sm->GetComputeState() == READY_TO_COMPUTE )
173 return 0; // nothing to compute
176 //================================================================================
178 * \brief Allow algo->Compute() if a subshape of lower dim is meshed but
179 * none mesh entity is bound to it (PAL13615, 2nd part)
181 //================================================================================
183 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
185 _alwaysComputed = isAlCo;
186 if ( _alwaysComputed )
187 _computeState = COMPUTE_OK;
189 ComputeStateEngine( CHECK_COMPUTE_STATE );
192 //=======================================================================
194 * \brief Return true if no mesh entities is bound to the submesh
196 //=======================================================================
198 bool SMESH_subMesh::IsEmpty() const
200 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
201 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
205 //=======================================================================
206 //function : IsMeshComputed
207 //purpose : check if _subMeshDS contains mesh elements
208 //=======================================================================
210 bool SMESH_subMesh::IsMeshComputed() const
212 if ( _alwaysComputed )
214 // algo may bind a submesh not to _subShape, eg 3D algo
215 // sets nodes on SHELL while _subShape may be SOLID
217 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
218 int dim = SMESH_Gen::GetShapeDim( _subShape );
219 int type = _subShape.ShapeType();
220 for ( ; type <= TopAbs_VERTEX; type++) {
221 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
223 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
224 for ( ; exp.More(); exp.Next() )
226 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
228 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
241 //=============================================================================
245 //=============================================================================
247 bool SMESH_subMesh::SubMeshesComputed()
249 int myDim = SMESH_Gen::GetShapeDim( _subShape );
250 int dimToCheck = myDim - 1;
251 bool subMeshesComputed = true;
252 // check subMeshes with upper dimension => reverse iteration
253 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
254 while ( smIt->more() )
256 SMESH_subMesh *sm = smIt->next();
257 if ( sm->_alwaysComputed )
259 const TopoDS_Shape & ss = sm->GetSubShape();
260 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
261 // in checking of existence of edges if the algo needs only faces. Moreover,
262 // degenerated edges may have no submesh, as after computing NETGEN_2D.
263 int dim = SMESH_Gen::GetShapeDim( ss );
264 if (dim < dimToCheck)
265 break; // the rest subMeshes are all of less dimension
266 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
267 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
268 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
271 int type = ss.ShapeType();
273 subMeshesComputed = false;
277 case TopAbs_COMPOUND:
279 MESSAGE("The not computed sub mesh is a COMPOUND");
282 case TopAbs_COMPSOLID:
284 MESSAGE("The not computed sub mesh is a COMPSOLID");
289 MESSAGE("The not computed sub mesh is a SHEL");
294 MESSAGE("The not computed sub mesh is a WIRE");
299 MESSAGE("The not computed sub mesh is a SOLID");
304 MESSAGE("The not computed sub mesh is a FACE");
309 MESSAGE("The not computed sub mesh is a EDGE");
314 MESSAGE("The not computed sub mesh is of unknown type");
322 return subMeshesComputed;
325 //=============================================================================
329 //=============================================================================
331 bool SMESH_subMesh::SubMeshesReady()
333 bool subMeshesReady = true;
334 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
335 while ( smIt->more() ) {
336 SMESH_subMesh *sm = smIt->next();
337 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
338 sm->GetComputeState() == READY_TO_COMPUTE);
341 subMeshesReady = false;
346 return subMeshesReady;
349 //=============================================================================
351 * Construct dependence on first level subMeshes. complex shapes (compsolid,
352 * shell, wire) are not analysed the same way as simple shapes (solid, face,
354 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
355 * with possible multiples occurences. Multiples occurences corresponds to
356 * internal frontiers within shapes of the collection and must not be keeped.
357 * See FinalizeDependence.
359 //=============================================================================
361 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
363 if (_dependenceAnalysed)
366 //MESSAGE("SMESH_subMesh::DependsOn");
368 int type = _subShape.ShapeType();
372 case TopAbs_COMPOUND:
374 //MESSAGE("compound");
375 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();exp.Next())
377 InsertDependence(exp.Current());
379 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More(); exp.Next())
381 if ( BRep_Tool::IsClosed(exp.Current() ))
382 InsertDependence(exp.Current()); //only shell not in solid
384 for (TopExp_Explorer expF(exp.Current(), TopAbs_FACE); expF.More();expF.Next())
385 InsertDependence(expF.Current()); // issue 0020959: HEXA_3D fails on shell
388 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();exp.Next())
390 InsertDependence(exp.Current());
392 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();exp.Next())
394 InsertDependence(exp.Current());
398 case TopAbs_COMPSOLID:
400 //MESSAGE("compsolid");
401 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More(); exp.Next())
403 InsertDependence(exp.Current());
410 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More(); exp.Next())
412 InsertDependence(exp.Current());
419 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More(); exp.Next())
421 InsertDependence(exp.Current());
428 if(_father->HasShapeToMesh()) {
429 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();exp.Next())
431 InsertDependence(exp.Current());
439 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();exp.Next())
441 InsertDependence(exp.Current());
448 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More(); exp.Next())
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 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
950 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
951 algo = dynamic_cast<SMESH_Algo*> (anHyp);
952 if (!algo->NeedDescretBoundary())
953 needFullClean = true;
955 algo = gen->GetAlgo((*_father), _subShape);
956 if (algo == NULL) // no more applying algo on father
958 SetAlgoState(NO_ALGO);
962 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
963 // check if algo changes
964 if ( string(algo->GetName()) != string( anHyp->GetName()) )
968 SetAlgoState(MISSING_HYP);
978 // ----------------------------------------------------------------------
985 // detect algorithm hiding
987 if ( ret == SMESH_Hypothesis::HYP_OK &&
988 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
989 algo->GetName() == anHyp->GetName() )
992 SMESH_Gen* gen = _father->GetGen();
993 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
994 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
995 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
996 if ( !upperAlgo->NeedDescretBoundary() && !upperAlgo->SupportSubmeshes())
997 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1000 if ( ret == SMESH_Hypothesis::HYP_OK &&
1001 !algo->NeedDescretBoundary() &&
1002 !algo->SupportSubmeshes()) {
1003 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1004 gen->GetAlgo( *_father, _subShape, &algoAssignedTo );
1005 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1006 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1007 if ( gen->GetAlgo( *_father, i_sm->second->_subShape, &otherAssignedTo ) &&
1008 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1009 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1013 bool stateChange = ( _algoState != oldAlgoState );
1015 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1016 algo->SetEventListener( this );
1018 NotifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1020 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1021 DeleteOwnListeners();
1022 SetIsAlwaysComputed( false );
1023 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1024 // restore default states
1025 _algoState = HYP_OK;
1026 _computeState = READY_TO_COMPUTE;
1030 if ( needFullClean ) {
1031 // added or removed algo is all-dimensional
1032 ComputeStateEngine( CLEAN );
1034 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1037 if (stateChange || modifiedHyp)
1038 ComputeStateEngine(MODIF_ALGO_STATE);
1043 //=======================================================================
1044 //function : IsConform
1045 //purpose : check if a conform mesh will be produced by the Algo
1046 //=======================================================================
1048 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1050 // MESSAGE( "SMESH_subMesh::IsConform" );
1051 if ( !theAlgo ) return false;
1053 // Suppose that theAlgo is applicable to _subShape, do not check it here
1054 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1056 // check only algo that doesn't NeedDescretBoundary(): because mesh made
1057 // on a sub-shape will be ignored by theAlgo
1058 if ( theAlgo->NeedDescretBoundary() ||
1059 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1062 SMESH_Gen* gen =_father->GetGen();
1064 // only local algo is to be checked
1065 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1066 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1069 // check algo attached to adjacent shapes
1071 // loop on one level down sub-meshes
1072 TopoDS_Iterator itsub( _subShape );
1073 for (; itsub.More(); itsub.Next())
1075 // loop on adjacent subShapes
1076 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1077 for (; it.More(); it.Next())
1079 const TopoDS_Shape& adjacent = it.Value();
1080 if ( _subShape.IsSame( adjacent )) continue;
1081 if ( adjacent.ShapeType() != _subShape.ShapeType())
1084 // check algo attached to smAdjacent
1085 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1087 !algo->NeedDescretBoundary() &&
1088 algo->OnlyUnaryInput())
1089 return false; // NOT CONFORM MESH WILL BE PRODUCED
1096 //=============================================================================
1100 //=============================================================================
1102 void SMESH_subMesh::SetAlgoState(int state)
1107 //=============================================================================
1111 //=============================================================================
1112 SMESH_Hypothesis::Hypothesis_Status
1113 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1114 SMESH_Hypothesis * anHyp)
1116 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1117 //EAP: a wire (dim==1) should notify edges (dim==1)
1118 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1119 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1121 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1122 while ( smIt->more() ) {
1123 SMESH_Hypothesis::Hypothesis_Status ret2 =
1124 smIt->next()->AlgoStateEngine(event, anHyp);
1132 //=============================================================================
1136 //=============================================================================
1138 void SMESH_subMesh::CleanDependsOn()
1140 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1141 while ( smIt->more() )
1142 smIt->next()->ComputeStateEngine(CLEAN);
1145 //=============================================================================
1149 //=============================================================================
1151 void SMESH_subMesh::DumpAlgoState(bool isMain)
1153 int dim = SMESH_Gen::GetShapeDim(_subShape);
1154 // if (dim < 1) return;
1157 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1159 map < int, SMESH_subMesh * >::const_iterator itsub;
1160 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1162 SMESH_subMesh *sm = (*itsub).second;
1163 sm->DumpAlgoState(false);
1166 int type = _subShape.ShapeType();
1167 MESSAGE("dim = " << dim << " type of shape " << type);
1171 MESSAGE(" AlgoState = NO_ALGO");
1174 MESSAGE(" AlgoState = MISSING_HYP");
1177 MESSAGE(" AlgoState = HYP_OK");
1180 switch (_computeState)
1183 MESSAGE(" ComputeState = NOT_READY");
1185 case READY_TO_COMPUTE:
1186 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1189 MESSAGE(" ComputeState = COMPUTE_OK");
1191 case FAILED_TO_COMPUTE:
1192 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1197 //================================================================================
1199 * \brief Remove nodes and elements bound to submesh
1200 * \param subMesh - submesh containing nodes and elements
1202 //================================================================================
1204 static void cleanSubMesh( SMESH_subMesh * subMesh )
1207 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1208 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1209 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1210 while (ite->more()) {
1211 const SMDS_MeshElement * elt = ite->next();
1212 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1213 //meshDS->RemoveElement(elt);
1214 meshDS->RemoveFreeElement(elt, subMeshDS);
1217 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1218 while (itn->more()) {
1219 const SMDS_MeshNode * node = itn->next();
1220 //MESSAGE( " RM node: "<<node->GetID());
1221 if ( node->NbInverseElements() == 0 )
1222 meshDS->RemoveFreeNode(node, subMeshDS);
1223 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1224 meshDS->RemoveNode(node);
1230 //=============================================================================
1234 //=============================================================================
1236 bool SMESH_subMesh::ComputeStateEngine(int event)
1238 _computeError.reset();
1240 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1241 //SCRUTE(_computeState);
1244 if (_subShape.ShapeType() == TopAbs_VERTEX)
1246 _computeState = READY_TO_COMPUTE;
1247 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1248 if ( smDS && smDS->NbNodes() ) {
1249 if ( event == CLEAN ) {
1251 cleanSubMesh( this );
1254 _computeState = COMPUTE_OK;
1256 else if ( event == COMPUTE && !_alwaysComputed ) {
1257 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1258 gp_Pnt P = BRep_Tool::Pnt(V);
1259 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1260 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1261 _computeState = COMPUTE_OK;
1264 if ( event == MODIF_ALGO_STATE )
1268 SMESH_Gen *gen = _father->GetGen();
1269 SMESH_Algo *algo = 0;
1271 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1272 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1274 switch (_computeState)
1277 // ----------------------------------------------------------------------
1282 case MODIF_ALGO_STATE:
1283 algo = gen->GetAlgo((*_father), _subShape);
1284 if (algo && !algo->NeedDescretBoundary())
1285 CleanDependsOn(); // clean sub-meshes with event CLEAN
1286 if ( _algoState == HYP_OK )
1287 _computeState = READY_TO_COMPUTE;
1289 case COMPUTE: // nothing to do
1293 RemoveSubMeshElementsAndNodes();
1295 case SUBMESH_COMPUTED: // nothing to do
1297 case SUBMESH_RESTORED:
1298 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1300 case MESH_ENTITY_REMOVED:
1302 case CHECK_COMPUTE_STATE:
1303 if ( IsMeshComputed() )
1304 _computeState = COMPUTE_OK;
1312 // ----------------------------------------------------------------------
1314 case READY_TO_COMPUTE:
1317 case MODIF_ALGO_STATE:
1318 _computeState = NOT_READY;
1319 algo = gen->GetAlgo((*_father), _subShape);
1322 if (!algo->NeedDescretBoundary())
1323 CleanDependsOn(); // clean sub-meshes with event CLEAN
1324 if ( _algoState == HYP_OK )
1325 _computeState = READY_TO_COMPUTE;
1330 algo = gen->GetAlgo((*_father), _subShape);
1332 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1335 MESSAGE("***** verify compute state *****");
1336 _computeState = NOT_READY;
1337 SetAlgoState(MISSING_HYP);
1340 TopoDS_Shape shape = _subShape;
1341 // check submeshes needed
1342 if (_father->HasShapeToMesh() ) {
1343 bool subComputed = false;
1344 if (!algo->OnlyUnaryInput())
1345 shape = GetCollection( gen, algo, subComputed );
1347 subComputed = SubMeshesComputed();
1348 ret = ( algo->NeedDescretBoundary() ? subComputed :
1349 algo->SupportSubmeshes() ? true :
1350 ( !subComputed || _father->IsNotConformAllowed() ));
1352 _computeState = FAILED_TO_COMPUTE;
1353 if ( !algo->NeedDescretBoundary() )
1355 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1356 "Unexpected computed submesh",algo);
1361 // CleanDependants(); for "UseExisting_*D" algos
1362 // RemoveSubMeshElementsAndNodes();
1364 _computeState = FAILED_TO_COMPUTE;
1365 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1367 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1370 algo->InitComputeError();
1371 MemoryReserve aMemoryReserve;
1372 SMDS_Mesh::CheckMemory();
1373 Kernel_Utils::Localizer loc;
1374 if ( !_father->HasShapeToMesh() ) // no shape
1376 SMESH_MesherHelper helper( *_father );
1377 helper.SetSubShape( shape );
1378 helper.SetElementsOnShape( true );
1379 ret = algo->Compute(*_father, &helper );
1383 ret = algo->Compute((*_father), shape);
1385 if ( !_computeError || ( !ret && _computeError->IsOK() ) ) // algo can set _computeError of submesh
1386 _computeError = algo->GetComputeError();
1388 catch ( std::bad_alloc& exc ) {
1389 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1390 if ( _computeError ) {
1391 _computeError->myName = COMPERR_MEMORY_PB;
1392 //_computeError->myComment = exc.what();
1394 cleanSubMesh( this );
1397 catch ( Standard_OutOfMemory& exc ) {
1398 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1399 if ( _computeError ) {
1400 _computeError->myName = COMPERR_MEMORY_PB;
1401 //_computeError->myComment = exc.what();
1403 cleanSubMesh( this );
1404 throw std::bad_alloc();
1406 catch (Standard_Failure& ex) {
1407 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1408 _computeError->myName = COMPERR_OCC_EXCEPTION;
1409 _computeError->myComment += ex.DynamicType()->Name();
1410 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1411 _computeError->myComment += ": ";
1412 _computeError->myComment += ex.GetMessageString();
1415 catch ( SALOME_Exception& S_ex ) {
1416 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1417 _computeError->myName = COMPERR_SLM_EXCEPTION;
1418 _computeError->myComment = S_ex.what();
1420 catch ( std::exception& exc ) {
1421 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1422 _computeError->myName = COMPERR_STD_EXCEPTION;
1423 _computeError->myComment = exc.what();
1426 if ( _computeError )
1427 _computeError->myName = COMPERR_EXCEPTION;
1431 TopExp_Explorer subS(shape, _subShape.ShapeType());
1432 if (ret) // check if anything was built
1434 for (; ret && subS.More(); subS.Next())
1435 ret = _father->GetSubMesh( subS.Current() )->IsMeshComputed();
1437 bool isComputeErrorSet = !CheckComputeError( algo, shape );
1438 if (!ret && !isComputeErrorSet)
1440 // Set _computeError
1441 for (subS.ReInit(); subS.More(); subS.Next())
1443 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1444 if ( !sm->IsMeshComputed() )
1446 if ( !sm->_computeError )
1447 sm->_computeError = SMESH_ComputeError::New();
1448 if ( sm->_computeError->IsOK() )
1449 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1450 sm->_computeState = FAILED_TO_COMPUTE;
1451 sm->_computeError->myAlgo = algo;
1457 _computeError.reset();
1459 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1464 RemoveSubMeshElementsAndNodes();
1465 _computeState = NOT_READY;
1466 algo = gen->GetAlgo((*_father), _subShape);
1469 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1471 _computeState = READY_TO_COMPUTE;
1473 SetAlgoState(MISSING_HYP);
1476 case SUBMESH_COMPUTED: // nothing to do
1478 case SUBMESH_RESTORED:
1479 // check if a mesh is already computed that may
1480 // happen after retrieval from a file
1481 ComputeStateEngine( CHECK_COMPUTE_STATE );
1482 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1483 algo = gen->GetAlgo(*_father, _subShape);
1484 if (algo) algo->SubmeshRestored( this );
1486 case MESH_ENTITY_REMOVED:
1488 case CHECK_COMPUTE_STATE:
1489 if ( IsMeshComputed() )
1490 _computeState = COMPUTE_OK;
1498 // ----------------------------------------------------------------------
1503 case MODIF_ALGO_STATE:
1504 ComputeStateEngine( CLEAN );
1505 algo = gen->GetAlgo((*_father), _subShape);
1506 if (algo && !algo->NeedDescretBoundary())
1507 CleanDependsOn(); // clean sub-meshes with event CLEAN
1509 case COMPUTE: // nothing to do
1512 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1513 RemoveSubMeshElementsAndNodes();
1514 _computeState = NOT_READY;
1515 if ( _algoState == HYP_OK )
1516 _computeState = READY_TO_COMPUTE;
1518 case SUBMESH_COMPUTED: // nothing to do
1520 case SUBMESH_RESTORED:
1521 ComputeStateEngine( CHECK_COMPUTE_STATE );
1522 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1523 algo = gen->GetAlgo(*_father, _subShape);
1524 if (algo) algo->SubmeshRestored( this );
1526 case MESH_ENTITY_REMOVED:
1527 UpdateDependantsState( CHECK_COMPUTE_STATE );
1528 ComputeStateEngine( CHECK_COMPUTE_STATE );
1529 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1531 case CHECK_COMPUTE_STATE:
1532 if ( !IsMeshComputed() )
1533 if (_algoState == HYP_OK)
1534 _computeState = READY_TO_COMPUTE;
1536 _computeState = NOT_READY;
1544 // ----------------------------------------------------------------------
1546 case FAILED_TO_COMPUTE:
1549 case MODIF_ALGO_STATE:
1551 ComputeStateEngine( CLEAN );
1552 algo = gen->GetAlgo((*_father), _subShape);
1553 if (algo && !algo->NeedDescretBoundary())
1554 CleanDependsOn(); // clean sub-meshes with event CLEAN
1555 if (_algoState == HYP_OK)
1556 _computeState = READY_TO_COMPUTE;
1558 _computeState = NOT_READY;
1560 case COMPUTE: // nothing to do
1563 CleanDependants(); // submeshes dependent on me should be cleaned as well
1564 RemoveSubMeshElementsAndNodes();
1566 case SUBMESH_COMPUTED: // allow retry compute
1567 if (_algoState == HYP_OK)
1568 _computeState = READY_TO_COMPUTE;
1570 _computeState = NOT_READY;
1572 case SUBMESH_RESTORED:
1573 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1575 case MESH_ENTITY_REMOVED:
1577 case CHECK_COMPUTE_STATE:
1578 if ( IsMeshComputed() )
1579 _computeState = COMPUTE_OK;
1581 if (_algoState == HYP_OK)
1582 _computeState = READY_TO_COMPUTE;
1584 _computeState = NOT_READY;
1592 // ----------------------------------------------------------------------
1598 NotifyListenersOnEvent( event, COMPUTE_EVENT );
1604 //=============================================================================
1608 //=============================================================================
1610 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1612 _computeError.reset();
1616 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1617 vector<int> aVec(SMDSEntity_Last,0);
1618 aVec[SMDSEntity_Node] = 1;
1619 aResMap.insert(make_pair(this,aVec));
1623 SMESH_Gen *gen = _father->GetGen();
1624 SMESH_Algo *algo = 0;
1625 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1627 algo = gen->GetAlgo((*_father), _subShape);
1628 if(algo && !aResMap.count(this) )
1630 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1631 if (!ret) return false;
1633 if (_father->HasShapeToMesh() && algo->NeedDescretBoundary())
1635 // check submeshes needed
1636 bool subMeshEvaluated = true;
1637 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1638 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1639 while ( smIt->more() && subMeshEvaluated )
1641 SMESH_subMesh* sm = smIt->next();
1642 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1643 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1644 const vector<int> & nbs = aResMap[ sm ];
1645 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1647 if ( !subMeshEvaluated )
1650 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1651 ret = algo->Evaluate((*_father), _subShape, aResMap);
1653 aResMap.insert( make_pair( this,vector<int>(0)));
1660 //=======================================================================
1662 * \brief Update compute_state by _computeError and send proper events to
1663 * dependent submeshes
1664 * \retval bool - true if _computeError is NOT set
1666 //=======================================================================
1668 bool SMESH_subMesh::CheckComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1670 bool noErrors = true;
1672 if ( !theShape.IsNull() )
1674 // Check state of submeshes
1675 if ( !theAlgo->NeedDescretBoundary())
1677 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1678 while ( smIt->more() )
1679 if ( !smIt->next()->CheckComputeError( theAlgo ))
1683 // Check state of neighbours
1684 if ( !theAlgo->OnlyUnaryInput() &&
1685 theShape.ShapeType() == TopAbs_COMPOUND &&
1686 !theShape.IsSame( _subShape ))
1688 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1689 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1691 if ( !sm->CheckComputeError( theAlgo, sm->GetSubShape() ))
1693 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1700 if ( !_computeError || _computeError->IsOK() )
1702 _computeState = COMPUTE_OK;
1706 if ( !_computeError->myAlgo )
1707 _computeError->myAlgo = theAlgo;
1711 text << theAlgo->GetName() << " failed on subshape #" << _Id << " with error ";
1712 if (_computeError->IsCommon() )
1713 text << _computeError->CommonName();
1715 text << _computeError->myName;
1716 if ( _computeError->myComment.size() > 0 )
1717 text << " \"" << _computeError->myComment << "\"";
1720 MESSAGE_BEGIN ( text );
1721 // Show vertices location of a failed shape
1722 TopTools_IndexedMapOfShape vMap;
1723 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1724 MESSAGE_ADD ( "Subshape vertices " << ( vMap.Extent()>10 ? "(first 10):" : ":") );
1725 for ( int iv = 1; iv <= vMap.Extent() && iv < 11; ++iv ) {
1726 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1727 MESSAGE_ADD ( "#" << _father->GetMeshDS()->ShapeToIndex( vMap( iv )) << " "
1728 << P.X() << " " << P.Y() << " " << P.Z() << " " );
1733 _computeState = FAILED_TO_COMPUTE;
1740 //=======================================================================
1741 //function : ApplyToCollection
1742 //purpose : Apply theAlgo to all subshapes in theCollection
1743 //=======================================================================
1745 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1746 const TopoDS_Shape& theCollection)
1748 MESSAGE("SMESH_subMesh::ApplyToCollection");
1749 ASSERT ( !theAlgo->NeedDescretBoundary() );
1751 if ( _computeError )
1752 _computeError->myName = COMPERR_OK;
1754 bool ok = theAlgo->Compute( *_father, theCollection );
1756 // set _computeState of subshapes
1757 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1758 for ( ; anExplorer.More(); anExplorer.Next() )
1760 if ( SMESH_subMesh* subMesh = _father->GetSubMeshContaining( anExplorer.Current() ))
1762 bool localOK = subMesh->CheckComputeError( theAlgo );
1763 if ( !ok && localOK && !subMesh->IsMeshComputed() )
1765 subMesh->_computeError = theAlgo->GetComputeError();
1766 if ( subMesh->_computeError->IsOK() )
1767 _computeError = SMESH_ComputeError::New(COMPERR_ALGO_FAILED);
1768 localOK = CheckComputeError( theAlgo );
1771 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1772 subMesh->UpdateSubMeshState( localOK ? COMPUTE_OK : FAILED_TO_COMPUTE );
1780 //=======================================================================
1781 //function : UpdateSubMeshState
1783 //=======================================================================
1785 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1787 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1788 while ( smIt->more() )
1789 smIt->next()->_computeState = theState;
1792 //=======================================================================
1793 //function : ComputeSubMeshStateEngine
1795 //=======================================================================
1797 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1799 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1800 while ( smIt->more() )
1801 smIt->next()->ComputeStateEngine(event);
1804 //=======================================================================
1805 //function : UpdateDependantsState
1807 //=======================================================================
1809 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1811 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1812 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1813 for (; it.More(); it.Next())
1815 const TopoDS_Shape& ancestor = it.Value();
1816 SMESH_subMesh *aSubMesh =
1817 _father->GetSubMeshContaining(ancestor);
1819 aSubMesh->ComputeStateEngine( theEvent );
1823 //=============================================================================
1827 //=============================================================================
1829 void SMESH_subMesh::CleanDependants()
1831 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1833 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1834 for (; it.More(); it.Next())
1836 const TopoDS_Shape& ancestor = it.Value();
1837 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1838 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1839 // will erase mesh on other shapes in a compound
1840 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1841 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1843 aSubMesh->ComputeStateEngine(CLEAN);
1849 //=============================================================================
1853 //=============================================================================
1855 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1857 //SCRUTE(_subShape.ShapeType());
1859 cleanSubMesh( this );
1861 // algo may bind a submesh not to _subShape, eg 3D algo
1862 // sets nodes on SHELL while _subShape may be SOLID
1864 int dim = SMESH_Gen::GetShapeDim( _subShape );
1865 int type = _subShape.ShapeType() + 1;
1866 for ( ; type <= TopAbs_EDGE; type++) {
1867 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1869 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1870 for ( ; exp.More(); exp.Next() )
1871 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1878 //=======================================================================
1879 //function : GetCollection
1880 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1881 // meshed at once along with _subShape
1882 //=======================================================================
1884 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen,
1885 SMESH_Algo* theAlgo,
1886 bool & theSubComputed)
1888 MESSAGE("SMESH_subMesh::GetCollection");
1890 theSubComputed = SubMeshesComputed();
1892 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1894 if ( mainShape.IsSame( _subShape ))
1897 const bool ignoreAuxiliaryHyps = false;
1898 list<const SMESHDS_Hypothesis*> aUsedHyp =
1899 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1901 // put in a compound all shapes with the same hypothesis assigned
1902 // and a good ComputState
1904 TopoDS_Compound aCompound;
1905 BRep_Builder aBuilder;
1906 aBuilder.MakeCompound( aCompound );
1908 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1909 for ( ; anExplorer.More(); anExplorer.Next() )
1911 const TopoDS_Shape& S = anExplorer.Current();
1912 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1913 if ( subMesh == this )
1915 aBuilder.Add( aCompound, S );
1917 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1919 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1920 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1921 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1922 aBuilder.Add( aCompound, S );
1923 if ( !subMesh->SubMeshesComputed() )
1924 theSubComputed = false;
1931 //=======================================================================
1932 //function : GetSimilarAttached
1933 //purpose : return a hypothesis attached to theShape.
1934 // If theHyp is provided, similar but not same hypotheses
1935 // is returned; else only applicable ones having theHypType
1937 //=======================================================================
1939 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1940 const SMESH_Hypothesis * theHyp,
1941 const int theHypType)
1943 SMESH_HypoFilter hypoKind;
1944 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1946 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1947 hypoKind.AndNot( hypoKind.Is( theHyp ));
1948 if ( theHyp->IsAuxiliary() )
1949 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1951 hypoKind.AndNot( hypoKind.IsAuxiliary());
1954 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1957 return _father->GetHypothesis( theShape, hypoKind, false );
1960 //=======================================================================
1961 //function : CheckConcurentHypothesis
1962 //purpose : check if there are several applicable hypothesis attached to
1964 //=======================================================================
1966 SMESH_Hypothesis::Hypothesis_Status
1967 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1969 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1971 // is there local hypothesis on me?
1972 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1973 return SMESH_Hypothesis::HYP_OK;
1976 TopoDS_Shape aPrevWithHyp;
1977 const SMESH_Hypothesis* aPrevHyp = 0;
1978 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1979 for (; it.More(); it.Next())
1981 const TopoDS_Shape& ancestor = it.Value();
1982 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1985 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1987 aPrevWithHyp = ancestor;
1990 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1991 return SMESH_Hypothesis::HYP_CONCURENT;
1993 return SMESH_Hypothesis::HYP_OK;
1996 return SMESH_Hypothesis::HYP_OK;
1999 //================================================================================
2001 * \brief Sets an event listener and its data to a submesh
2002 * \param listener - the listener to store
2003 * \param data - the listener data to store
2004 * \param where - the submesh to store the listener and it's data
2005 * \param deleteListener - if true then the listener will be deleted as
2006 * it is removed from where submesh
2008 * It remembers the submesh where it puts the listener in order to delete
2009 * them when HYP_OK algo_state is lost
2010 * After being set, event listener is notified on each event of where submesh.
2012 //================================================================================
2014 void SMESH_subMesh::SetEventListener(EventListener* listener,
2015 EventListenerData* data,
2016 SMESH_subMesh* where)
2018 if ( listener && where ) {
2019 where->SetEventListener( listener, data );
2020 myOwnListeners.push_back( make_pair( where, listener ));
2024 //================================================================================
2026 * \brief Sets an event listener and its data to a submesh
2027 * \param listener - the listener to store
2028 * \param data - the listener data to store
2030 * After being set, event listener is notified on each event of a submesh.
2032 //================================================================================
2034 void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
2036 map< EventListener*, EventListenerData* >::iterator l_d =
2037 myEventListeners.find( listener );
2038 if ( l_d != myEventListeners.end() ) {
2039 EventListenerData* curData = l_d->second;
2040 if ( curData && curData != data && curData->IsDeletable() )
2045 myEventListeners.insert( make_pair( listener, data ));
2048 //================================================================================
2050 * \brief Return an event listener data
2051 * \param listener - the listener whose data is
2052 * \retval EventListenerData* - found data, maybe NULL
2054 //================================================================================
2056 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
2058 map< EventListener*, EventListenerData* >::const_iterator l_d =
2059 myEventListeners.find( listener );
2060 if ( l_d != myEventListeners.end() )
2065 //================================================================================
2067 * \brief Notify stored event listeners on the occured event
2068 * \param event - algo_event or compute_event itself
2069 * \param eventType - algo_event or compute_event
2070 * \param subMesh - the submesh where the event occures
2071 * \param data - listener data stored in the subMesh
2072 * \param hyp - hypothesis, if eventType is algo_event
2074 //================================================================================
2076 void SMESH_subMesh::NotifyListenersOnEvent( const int event,
2077 const event_type eventType,
2078 SMESH_Hypothesis* hyp)
2080 map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
2081 for ( ; l_d != myEventListeners.end(); ++l_d )
2082 l_d->first->ProcessEvent( event, eventType, this, l_d->second, hyp );
2085 //================================================================================
2087 * \brief Unregister the listener and delete listener's data
2088 * \param listener - the event listener
2090 //================================================================================
2092 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2094 map< EventListener*, EventListenerData* >::iterator l_d =
2095 myEventListeners.find( listener );
2096 if ( l_d != myEventListeners.end() ) {
2097 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2098 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2099 myEventListeners.erase( l_d );
2103 //================================================================================
2105 * \brief Delete event listeners depending on algo of this submesh
2107 //================================================================================
2109 void SMESH_subMesh::DeleteOwnListeners()
2111 list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
2112 for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
2113 sm_l->first->DeleteEventListener( sm_l->second );
2114 myOwnListeners.clear();
2117 //================================================================================
2119 * \brief Do something on a certain event
2120 * \param event - algo_event or compute_event itself
2121 * \param eventType - algo_event or compute_event
2122 * \param subMesh - the submesh where the event occures
2123 * \param data - listener data stored in the subMesh
2124 * \param hyp - hypothesis, if eventType is algo_event
2126 * The base implementation translates CLEAN event to the subMesh
2127 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2128 * successful COMPUTE event.
2130 //================================================================================
2132 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2133 const int eventType,
2134 SMESH_subMesh* subMesh,
2135 EventListenerData* data,
2136 const SMESH_Hypothesis* /*hyp*/)
2138 if ( data && !data->mySubMeshes.empty() &&
2139 eventType == SMESH_subMesh::COMPUTE_EVENT)
2141 ASSERT( data->mySubMeshes.front() != subMesh );
2142 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2143 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2145 case SMESH_subMesh::CLEAN:
2146 for ( ; smIt != smEnd; ++ smIt)
2147 (*smIt)->ComputeStateEngine( event );
2149 case SMESH_subMesh::COMPUTE:
2150 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2151 for ( ; smIt != smEnd; ++ smIt)
2152 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2161 //================================================================================
2163 * \brief Iterator over submeshes and optionally prepended or appended one
2165 //================================================================================
2167 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2169 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2170 SMESH_subMesh* prepend,
2171 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2173 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2174 if ( myCur == append ) append = 0;
2176 /// Return true if and only if there are other object in this iterator
2181 /// Return the current object and step to the next one
2182 virtual SMESH_subMesh* next()
2184 SMESH_subMesh* res = myCur;
2185 if ( myIt->more() ) { myCur = myIt->next(); }
2186 else { myCur = myAppend; myAppend = 0; }
2193 SMESH_subMesh *myAppend, *myCur;
2194 SMDS_Iterator<SMESH_subMesh*> *myIt;
2198 //================================================================================
2200 * \brief Return iterator on the submeshes this one depends on
2201 * \param includeSelf - this submesh to be returned also
2202 * \param reverse - if true, complex shape submeshes go first
2204 //================================================================================
2206 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2209 SMESH_subMesh *prepend=0, *append=0;
2210 if ( includeSelf ) {
2211 if ( reverse ) prepend = this;
2214 typedef map < int, SMESH_subMesh * > TMap;
2217 return SMESH_subMeshIteratorPtr
2218 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2221 return SMESH_subMeshIteratorPtr
2222 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));
2226 //================================================================================
2228 * \brief Find common submeshes (based on shared subshapes with other
2229 * \param theOther submesh to check
2230 * \param theSetOfCommon set of common submesh
2232 //================================================================================
2234 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2235 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2237 int oldNb = theSetOfCommon.size();
2238 // check main submeshes
2239 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2240 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2241 theSetOfCommon.insert( this );
2242 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2243 theSetOfCommon.insert( theOther );
2244 // check common submeshes
2245 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2246 for( ; mapIt != _mapDepend.end(); mapIt++ )
2247 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2248 theSetOfCommon.insert( (*mapIt).second );
2249 return oldNb < theSetOfCommon.size();