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"
38 #include "SMDSAbs_ElementType.hxx"
40 #include "utilities.h"
43 #include <BRep_Builder.hxx>
44 #include <BRep_Tool.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopoDS_Compound.hxx>
51 #include <TopExp_Explorer.hxx>
52 #include <TopoDS_Iterator.hxx>
54 #include <Standard_OutOfMemory.hxx>
55 #include <Standard_ErrorHandler.hxx>
59 //=============================================================================
61 * \brief Allocate some memory at construction and release it at destruction.
62 * Is used to be able to continue working after mesh generation breaks due to
65 //=============================================================================
70 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
71 ~MemoryReserve() { delete [] myBuf; }
74 //=============================================================================
76 * default constructor:
78 //=============================================================================
80 SMESH_subMesh::SMESH_subMesh(int Id,
82 SMESHDS_Mesh * meshDS,
83 const TopoDS_Shape & aSubShape)
85 _subShape = aSubShape;
86 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
89 _dependenceAnalysed = _alwaysComputed = false;
91 if (_subShape.ShapeType() == TopAbs_VERTEX)
94 _computeState = READY_TO_COMPUTE;
99 _computeState = NOT_READY;
103 //=============================================================================
107 //=============================================================================
109 SMESH_subMesh::~SMESH_subMesh()
111 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
113 DeleteOwnListeners();
116 //=============================================================================
120 //=============================================================================
122 int SMESH_subMesh::GetId() const
124 //MESSAGE("SMESH_subMesh::GetId");
128 //=============================================================================
132 //=============================================================================
134 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
136 // submesh appears in DS only when a mesher set nodes and elements on a shape
137 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
140 //=============================================================================
144 //=============================================================================
146 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
148 if ( !GetSubMeshDS() ) {
149 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
150 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
152 return GetSubMeshDS();
155 //=============================================================================
159 //=============================================================================
161 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
163 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
164 while ( smIt->more() ) {
165 SMESH_subMesh *sm = smIt->next();
166 if ( sm->GetComputeState() == READY_TO_COMPUTE )
169 return 0; // nothing to compute
172 //================================================================================
174 * \brief Allow algo->Compute() if a subshape of lower dim is meshed but
175 * none mesh entity is bound to it (PAL13615, 2nd part)
177 //================================================================================
179 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
181 _alwaysComputed = isAlCo;
182 if ( _alwaysComputed )
183 _computeState = COMPUTE_OK;
185 ComputeStateEngine( CHECK_COMPUTE_STATE );
188 //=======================================================================
190 * \brief Return true if no mesh entities is bound to the submesh
192 //=======================================================================
194 bool SMESH_subMesh::IsEmpty() const
196 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
197 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
201 //=======================================================================
202 //function : IsMeshComputed
203 //purpose : check if _subMeshDS contains mesh elements
204 //=======================================================================
206 bool SMESH_subMesh::IsMeshComputed() const
208 if ( _alwaysComputed )
210 // algo may bind a submesh not to _subShape, eg 3D algo
211 // sets nodes on SHELL while _subShape may be SOLID
213 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
214 int dim = SMESH_Gen::GetShapeDim( _subShape );
215 int type = _subShape.ShapeType();
216 for ( ; type <= TopAbs_VERTEX; type++) {
217 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
219 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
220 for ( ; exp.More(); exp.Next() )
222 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
224 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
237 //=============================================================================
241 //=============================================================================
243 bool SMESH_subMesh::SubMeshesComputed()
245 int myDim = SMESH_Gen::GetShapeDim( _subShape );
246 int dimToCheck = myDim - 1;
247 bool subMeshesComputed = true;
248 // check subMeshes with upper dimension => reverse iteration
249 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
250 while ( smIt->more() )
252 SMESH_subMesh *sm = smIt->next();
253 if ( sm->_alwaysComputed )
255 const TopoDS_Shape & ss = sm->GetSubShape();
256 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
257 // in checking of existence of edges if the algo needs only faces. Moreover,
258 // degenerated edges may have no submesh, as after computing NETGEN_2D.
259 int dim = SMESH_Gen::GetShapeDim( ss );
260 if (dim < dimToCheck)
261 break; // the rest subMeshes are all of less dimension
262 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
263 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
264 (ds && ( ds->NbNodes() || ds->NbElements() )));
267 int type = ss.ShapeType();
269 subMeshesComputed = false;
273 case TopAbs_COMPOUND:
275 MESSAGE("The not computed sub mesh is a COMPOUND");
278 case TopAbs_COMPSOLID:
280 MESSAGE("The not computed sub mesh is a COMPSOLID");
285 MESSAGE("The not computed sub mesh is a SHEL");
290 MESSAGE("The not computed sub mesh is a WIRE");
295 MESSAGE("The not computed sub mesh is a SOLID");
300 MESSAGE("The not computed sub mesh is a FACE");
305 MESSAGE("The not computed sub mesh is a EDGE");
310 MESSAGE("The not computed sub mesh is of unknown type");
318 return subMeshesComputed;
321 //=============================================================================
325 //=============================================================================
327 bool SMESH_subMesh::SubMeshesReady()
329 bool subMeshesReady = true;
330 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
331 while ( smIt->more() ) {
332 SMESH_subMesh *sm = smIt->next();
333 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
334 sm->GetComputeState() == READY_TO_COMPUTE);
337 subMeshesReady = false;
342 return subMeshesReady;
345 //=============================================================================
347 * Construct dependence on first level subMeshes. complex shapes (compsolid,
348 * shell, wire) are not analysed the same way as simple shapes (solid, face,
350 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
351 * with possible multiples occurences. Multiples occurences corresponds to
352 * internal frontiers within shapes of the collection and must not be keeped.
353 * See FinalizeDependence.
355 //=============================================================================
357 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
359 if (_dependenceAnalysed)
362 //MESSAGE("SMESH_subMesh::DependsOn");
364 int type = _subShape.ShapeType();
368 case TopAbs_COMPOUND:
370 //MESSAGE("compound");
371 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
374 InsertDependence(exp.Current());
376 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
379 InsertDependence(exp.Current()); //only shell not in solid
381 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
384 InsertDependence(exp.Current());
386 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
389 InsertDependence(exp.Current());
393 case TopAbs_COMPSOLID:
395 //MESSAGE("compsolid");
396 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
399 InsertDependence(exp.Current());
406 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
409 InsertDependence(exp.Current());
416 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
419 InsertDependence(exp.Current());
426 if(_father->HasShapeToMesh()) {
427 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
430 InsertDependence(exp.Current());
438 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
441 InsertDependence(exp.Current());
448 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
451 InsertDependence(exp.Current());
464 _dependenceAnalysed = true;
468 //=============================================================================
470 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
472 //=============================================================================
474 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
476 //MESSAGE("SMESH_subMesh::InsertDependence");
477 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
478 int type = aSubShape.ShapeType();
479 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
480 int cle = aSubMesh->GetId();
481 cle += 10000000 * ordType; // sort map by ordType then index
482 if ( _mapDepend.find( cle ) == _mapDepend.end())
484 _mapDepend[cle] = aSubMesh;
485 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
486 _mapDepend.insert( subMap.begin(), subMap.end() );
490 //=============================================================================
494 //=============================================================================
496 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
498 //MESSAGE("SMESH_subMesh::GetSubShape");
503 //=======================================================================
504 //function : CanAddHypothesis
505 //purpose : return true if theHypothesis can be attached to me:
506 // its dimention is checked
507 //=======================================================================
509 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
511 int aHypDim = theHypothesis->GetDim();
512 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
513 if (aHypDim == 3 && aShapeDim == 3) {
514 // check case of open shell
515 //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
516 if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
519 if ( aHypDim <= aShapeDim )
525 //=======================================================================
526 //function : IsApplicableHypotesis
528 //=======================================================================
530 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
531 const TopAbs_ShapeEnum theShapeType)
533 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
535 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
538 switch ( theShapeType ) {
543 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
546 // Special case for algorithms, building 2D mesh on a whole shell.
547 // Before this fix there was a problem after restoring from study,
548 // because in that case algorithm is assigned before hypothesis
549 // (on shell in problem case) and hypothesis is checked on faces
550 // (because it is 2D), where we have NO_ALGO state.
551 // Now 2D hypothesis is also applicable to shells.
552 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
555 // case TopAbs_COMPSOLID:
556 // case TopAbs_COMPOUND:
562 //=============================================================================
566 //=============================================================================
568 SMESH_Hypothesis::Hypothesis_Status
569 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
571 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
572 //SCRUTE(_algoState);
575 // **** les retour des evenement shape sont significatifs
576 // (add ou remove fait ou non)
577 // le retour des evenement father n'indiquent pas que add ou remove fait
579 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
581 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
582 SMESH_Gen* gen =_father->GetGen();
583 SMESH_Algo* algo = 0;
585 if (_subShape.ShapeType() == TopAbs_VERTEX )
587 if ( anHyp->GetDim() != 0) {
588 if (event == ADD_HYP || event == ADD_ALGO)
589 return SMESH_Hypothesis::HYP_BAD_DIM;
591 return SMESH_Hypothesis::HYP_OK;
594 else if ( _algoState == HYP_OK ) {
595 // update default _algoState
596 if ( event != REMOVE_FATHER_ALGO )
598 _algoState = NO_ALGO;
599 algo = gen->GetAlgo(*_father, _subShape);
601 _algoState = MISSING_HYP;
602 if ( event == REMOVE_FATHER_HYP ||
603 algo->CheckHypothesis(*_father,_subShape, aux_ret))
610 int oldAlgoState = _algoState;
611 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
612 bool needFullClean = false;
614 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
616 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
618 // -------------------------------------------
619 // check if a shape needed by algo is present
620 // -------------------------------------------
621 algo = static_cast< SMESH_Algo* >( anHyp );
622 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
623 return SMESH_Hypothesis::HYP_NEED_SHAPE;
624 // ----------------------
625 // check mesh conformity
626 // ----------------------
627 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
628 return SMESH_Hypothesis::HYP_NOTCONFORM;
630 // check if all-dimensional algo is hidden by other local one
631 if ( event == ADD_ALGO ) {
632 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
633 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
634 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
635 if ( SMESH_Algo * curAlgo = (SMESH_Algo*) _father->GetHypothesis( _subShape, filter, true ))
636 needFullClean = ( !curAlgo->NeedDescretBoundary() );
640 // ----------------------------------
641 // add a hypothesis to DS if possible
642 // ----------------------------------
643 if (event == ADD_HYP || event == ADD_ALGO)
645 if ( ! CanAddHypothesis( anHyp )) // check dimension
646 return SMESH_Hypothesis::HYP_BAD_DIM;
648 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
649 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
651 if ( !meshDS->AddHypothesis(_subShape, anHyp))
652 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
655 // --------------------------
656 // remove a hypothesis from DS
657 // --------------------------
658 if (event == REMOVE_HYP || event == REMOVE_ALGO)
660 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
661 return SMESH_Hypothesis::HYP_OK; // nothing changes
663 if (event == REMOVE_ALGO)
665 algo = dynamic_cast<SMESH_Algo*> (anHyp);
666 if (!algo->NeedDescretBoundary())
668 // clean all mesh in the tree of the current submesh;
669 // we must perform it now because later
670 // we will have no information about the type of the removed algo
671 needFullClean = true;
676 // ------------------
677 // analyse algo state
678 // ------------------
679 if (!isApplicableHyp)
680 return ret; // not applicable hypotheses do not change algo state
685 // ----------------------------------------------------------------------
692 algo = gen->GetAlgo((*_father), _subShape);
694 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
695 SetAlgoState(HYP_OK);
696 else if ( algo->IsStatusFatal( aux_ret )) {
697 meshDS->RemoveHypothesis(_subShape, anHyp);
701 SetAlgoState(MISSING_HYP);
708 case ADD_FATHER_ALGO: { // Algo just added in father
709 algo = gen->GetAlgo((*_father), _subShape);
711 if ( algo == anHyp ) {
712 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
713 SetAlgoState(HYP_OK);
715 SetAlgoState(MISSING_HYP);
719 case REMOVE_FATHER_HYP:
721 case REMOVE_FATHER_ALGO: {
722 algo = gen->GetAlgo((*_father), _subShape);
725 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
726 SetAlgoState(HYP_OK);
728 SetAlgoState(MISSING_HYP);
732 case MODIF_HYP: break;
739 // ----------------------------------------------------------------------
745 algo = gen->GetAlgo((*_father), _subShape);
747 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
748 SetAlgoState(HYP_OK);
749 if (SMESH_Hypothesis::IsStatusFatal( ret ))
750 meshDS->RemoveHypothesis(_subShape, anHyp);
751 else if (!_father->IsUsedHypothesis( anHyp, this ))
753 meshDS->RemoveHypothesis(_subShape, anHyp);
754 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
758 case ADD_ALGO: { //already existing algo : on father ?
759 algo = gen->GetAlgo((*_father), _subShape);
761 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
762 SetAlgoState(HYP_OK);
763 else if ( algo->IsStatusFatal( aux_ret )) {
764 meshDS->RemoveHypothesis(_subShape, anHyp);
768 SetAlgoState(MISSING_HYP);
773 case REMOVE_ALGO: { // perhaps a father algo applies ?
774 algo = gen->GetAlgo((*_father), _subShape);
775 if (algo == NULL) // no more algo applying on subShape...
777 SetAlgoState(NO_ALGO);
781 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
782 SetAlgoState(HYP_OK);
784 SetAlgoState(MISSING_HYP);
788 case MODIF_HYP: // assigned hypothesis value may become good
789 case ADD_FATHER_HYP: {
790 algo = gen->GetAlgo((*_father), _subShape);
792 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
793 SetAlgoState(HYP_OK);
795 SetAlgoState(MISSING_HYP);
798 case ADD_FATHER_ALGO: { // new father algo
799 algo = gen->GetAlgo((*_father), _subShape);
801 if ( algo == anHyp ) {
802 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
803 SetAlgoState(HYP_OK);
805 SetAlgoState(MISSING_HYP);
809 case REMOVE_FATHER_HYP: // nothing to do
811 case REMOVE_FATHER_ALGO: {
812 algo = gen->GetAlgo((*_father), _subShape);
813 if (algo == NULL) // no more applying algo on father
815 SetAlgoState(NO_ALGO);
819 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
820 SetAlgoState(HYP_OK);
822 SetAlgoState(MISSING_HYP);
832 // ----------------------------------------------------------------------
838 algo = gen->GetAlgo((*_father), _subShape);
840 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
842 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
843 // ret should be fatal: anHyp was not added
844 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
846 else if (!_father->IsUsedHypothesis( anHyp, this ))
847 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
849 if (SMESH_Hypothesis::IsStatusFatal( ret ))
851 MESSAGE("do not add extra hypothesis");
852 meshDS->RemoveHypothesis(_subShape, anHyp);
860 case ADD_ALGO: { //already existing algo : on father ?
861 algo = gen->GetAlgo((*_father), _subShape);
862 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
863 // check if algo changes
865 f.Init( SMESH_HypoFilter::IsAlgo() );
866 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
867 f.AndNot( SMESH_HypoFilter::Is( algo ));
868 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
870 string(algo->GetName()) != string(prevAlgo->GetName()) )
874 SetAlgoState(MISSING_HYP);
878 algo = gen->GetAlgo((*_father), _subShape);
880 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
881 SetAlgoState(HYP_OK);
883 SetAlgoState(MISSING_HYP);
887 case REMOVE_ALGO: { // perhaps a father algo applies ?
888 algo = gen->GetAlgo((*_father), _subShape);
889 if (algo == NULL) // no more algo applying on subShape...
891 SetAlgoState(NO_ALGO);
895 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
896 // check if algo remains
897 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
901 SetAlgoState(MISSING_HYP);
905 case MODIF_HYP: // hypothesis value may become bad
906 case ADD_FATHER_HYP: { // new father hypothesis ?
907 algo = gen->GetAlgo((*_father), _subShape);
909 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
911 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
915 SetAlgoState(MISSING_HYP);
918 case ADD_FATHER_ALGO: {
919 algo = gen->GetAlgo((*_father), _subShape);
920 if ( algo == anHyp ) { // a new algo on father
921 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
922 // check if algo changes
924 f.Init( SMESH_HypoFilter::IsAlgo() );
925 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
926 f.AndNot( SMESH_HypoFilter::Is( algo ));
927 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
929 string(algo->GetName()) != string(prevAlgo->GetName()) )
933 SetAlgoState(MISSING_HYP);
937 case REMOVE_FATHER_HYP: {
938 algo = gen->GetAlgo((*_father), _subShape);
940 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
941 // is there the same local hyp or maybe a new father algo applied?
942 if ( !GetSimilarAttached( _subShape, anHyp ) )
946 SetAlgoState(MISSING_HYP);
949 case REMOVE_FATHER_ALGO: {
950 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
951 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
952 algo = dynamic_cast<SMESH_Algo*> (anHyp);
953 if (!algo->NeedDescretBoundary())
954 needFullClean = true;
956 algo = gen->GetAlgo((*_father), _subShape);
957 if (algo == NULL) // no more applying algo on father
959 SetAlgoState(NO_ALGO);
963 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
964 // check if algo changes
965 if ( string(algo->GetName()) != string( anHyp->GetName()) )
969 SetAlgoState(MISSING_HYP);
979 // ----------------------------------------------------------------------
986 // detect algorithm hiding
988 if ( ret == SMESH_Hypothesis::HYP_OK &&
989 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
990 algo->GetName() == anHyp->GetName() )
993 SMESH_Gen* gen = _father->GetGen();
994 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
995 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
996 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
997 if ( !upperAlgo->NeedDescretBoundary() && !upperAlgo->SupportSubmeshes())
998 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1001 if ( ret == SMESH_Hypothesis::HYP_OK &&
1002 !algo->NeedDescretBoundary() &&
1003 !algo->SupportSubmeshes()) {
1004 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1005 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1006 if ( gen->GetAlgo( *_father, i_sm->second->_subShape ))
1007 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1011 bool stateChange = ( _algoState != oldAlgoState );
1013 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1014 algo->SetEventListener( this );
1016 NotifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1018 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1019 DeleteOwnListeners();
1020 SetIsAlwaysComputed( false );
1021 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1022 // restore default states
1023 _algoState = HYP_OK;
1024 _computeState = READY_TO_COMPUTE;
1028 if ( needFullClean ) {
1029 // added or removed algo is all-dimensional
1030 ComputeStateEngine( CLEAN );
1032 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1035 if (stateChange || modifiedHyp)
1036 ComputeStateEngine(MODIF_ALGO_STATE);
1041 //=======================================================================
1042 //function : IsConform
1043 //purpose : check if a conform mesh will be produced by the Algo
1044 //=======================================================================
1046 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1048 // MESSAGE( "SMESH_subMesh::IsConform" );
1049 if ( !theAlgo ) return false;
1051 // Suppose that theAlgo is applicable to _subShape, do not check it here
1052 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1054 // check only algo that doesn't NeedDescretBoundary(): because mesh made
1055 // on a sub-shape will be ignored by theAlgo
1056 if ( theAlgo->NeedDescretBoundary() ||
1057 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1060 SMESH_Gen* gen =_father->GetGen();
1062 // only local algo is to be checked
1063 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1064 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1067 // check algo attached to adjacent shapes
1069 // loop on one level down sub-meshes
1070 TopoDS_Iterator itsub( _subShape );
1071 for (; itsub.More(); itsub.Next())
1073 // loop on adjacent subShapes
1074 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1075 for (; it.More(); it.Next())
1077 const TopoDS_Shape& adjacent = it.Value();
1078 if ( _subShape.IsSame( adjacent )) continue;
1079 if ( adjacent.ShapeType() != _subShape.ShapeType())
1082 // check algo attached to smAdjacent
1083 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1085 !algo->NeedDescretBoundary() &&
1086 algo->OnlyUnaryInput())
1087 return false; // NOT CONFORM MESH WILL BE PRODUCED
1094 //=============================================================================
1098 //=============================================================================
1100 void SMESH_subMesh::SetAlgoState(int state)
1105 //=============================================================================
1109 //=============================================================================
1110 SMESH_Hypothesis::Hypothesis_Status
1111 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1112 SMESH_Hypothesis * anHyp)
1114 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1115 //EAP: a wire (dim==1) should notify edges (dim==1)
1116 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1117 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1119 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1120 while ( smIt->more() ) {
1121 SMESH_Hypothesis::Hypothesis_Status ret2 =
1122 smIt->next()->AlgoStateEngine(event, anHyp);
1130 //=============================================================================
1134 //=============================================================================
1136 void SMESH_subMesh::CleanDependsOn()
1138 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1139 while ( smIt->more() )
1140 smIt->next()->ComputeStateEngine(CLEAN);
1143 //=============================================================================
1147 //=============================================================================
1149 void SMESH_subMesh::DumpAlgoState(bool isMain)
1151 int dim = SMESH_Gen::GetShapeDim(_subShape);
1152 // if (dim < 1) return;
1155 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1157 map < int, SMESH_subMesh * >::const_iterator itsub;
1158 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1160 SMESH_subMesh *sm = (*itsub).second;
1161 sm->DumpAlgoState(false);
1164 int type = _subShape.ShapeType();
1165 MESSAGE("dim = " << dim << " type of shape " << type);
1169 MESSAGE(" AlgoState = NO_ALGO");
1172 MESSAGE(" AlgoState = MISSING_HYP");
1175 MESSAGE(" AlgoState = HYP_OK");
1178 switch (_computeState)
1181 MESSAGE(" ComputeState = NOT_READY");
1183 case READY_TO_COMPUTE:
1184 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1187 MESSAGE(" ComputeState = COMPUTE_OK");
1189 case FAILED_TO_COMPUTE:
1190 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1195 //================================================================================
1197 * \brief Remove nodes and elements bound to submesh
1198 * \param subMesh - submesh containing nodes and elements
1200 //================================================================================
1202 static void cleanSubMesh( SMESH_subMesh * subMesh )
1205 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1206 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1207 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1208 while (ite->more()) {
1209 const SMDS_MeshElement * elt = ite->next();
1210 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1211 //meshDS->RemoveElement(elt);
1212 meshDS->RemoveFreeElement(elt, subMeshDS);
1215 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1216 while (itn->more()) {
1217 const SMDS_MeshNode * node = itn->next();
1218 //MESSAGE( " RM node: "<<node->GetID());
1219 if ( node->NbInverseElements() == 0 )
1220 meshDS->RemoveFreeNode(node, subMeshDS);
1221 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1222 meshDS->RemoveNode(node);
1228 //=============================================================================
1232 //=============================================================================
1234 bool SMESH_subMesh::ComputeStateEngine(int event)
1236 _computeError.reset();
1238 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1239 //SCRUTE(_computeState);
1242 if (_subShape.ShapeType() == TopAbs_VERTEX)
1244 _computeState = READY_TO_COMPUTE;
1245 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1246 if ( smDS && smDS->NbNodes() ) {
1247 if ( event == CLEAN ) {
1249 cleanSubMesh( this );
1252 _computeState = COMPUTE_OK;
1254 else if ( event == COMPUTE && !_alwaysComputed ) {
1255 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1256 gp_Pnt P = BRep_Tool::Pnt(V);
1257 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1258 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1259 _computeState = COMPUTE_OK;
1262 if ( event == MODIF_ALGO_STATE )
1266 SMESH_Gen *gen = _father->GetGen();
1267 SMESH_Algo *algo = 0;
1269 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1270 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1272 switch (_computeState)
1275 // ----------------------------------------------------------------------
1280 case MODIF_ALGO_STATE:
1281 algo = gen->GetAlgo((*_father), _subShape);
1282 if (algo && !algo->NeedDescretBoundary())
1283 CleanDependsOn(); // clean sub-meshes with event CLEAN
1284 if ( _algoState == HYP_OK )
1285 _computeState = READY_TO_COMPUTE;
1287 case COMPUTE: // nothing to do
1291 RemoveSubMeshElementsAndNodes();
1293 case SUBMESH_COMPUTED: // nothing to do
1295 case SUBMESH_RESTORED:
1296 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1298 case MESH_ENTITY_REMOVED:
1300 case CHECK_COMPUTE_STATE:
1301 if ( IsMeshComputed() )
1302 _computeState = COMPUTE_OK;
1310 // ----------------------------------------------------------------------
1312 case READY_TO_COMPUTE:
1315 case MODIF_ALGO_STATE:
1316 _computeState = NOT_READY;
1317 algo = gen->GetAlgo((*_father), _subShape);
1320 if (!algo->NeedDescretBoundary())
1321 CleanDependsOn(); // clean sub-meshes with event CLEAN
1322 if ( _algoState == HYP_OK )
1323 _computeState = READY_TO_COMPUTE;
1328 algo = gen->GetAlgo((*_father), _subShape);
1330 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1333 MESSAGE("***** verify compute state *****");
1334 _computeState = NOT_READY;
1335 SetAlgoState(MISSING_HYP);
1338 TopoDS_Shape shape = _subShape;
1339 // check submeshes needed
1340 if (_father->HasShapeToMesh() ) {
1341 bool subComputed = false;
1342 if (!algo->OnlyUnaryInput())
1343 shape = GetCollection( gen, algo, subComputed );
1345 subComputed = SubMeshesComputed();
1346 ret = ( algo->NeedDescretBoundary() ? subComputed :
1347 algo->SupportSubmeshes() ? true :
1348 ( !subComputed || _father->IsNotConformAllowed() ));
1350 _computeState = FAILED_TO_COMPUTE;
1351 if ( !algo->NeedDescretBoundary() )
1353 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1354 "Unexpected computed submesh",algo);
1359 // CleanDependants(); for "UseExisting_*D" algos
1360 // RemoveSubMeshElementsAndNodes();
1362 _computeState = FAILED_TO_COMPUTE;
1363 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1365 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1368 algo->InitComputeError();
1369 MemoryReserve aMemoryReserve;
1370 SMDS_Mesh::CheckMemory();
1371 if ( !_father->HasShapeToMesh() ) // no shape
1373 SMESH_MesherHelper helper( *_father );
1374 helper.SetSubShape( shape );
1375 helper.SetElementsOnShape( true );
1376 ret = algo->Compute(*_father, &helper );
1380 ret = algo->Compute((*_father), shape);
1382 if ( !_computeError || ( !ret && _computeError->IsOK() ) ) // algo can set _computeError of submesh
1383 _computeError = algo->GetComputeError();
1385 catch ( std::bad_alloc& exc ) {
1386 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1387 if ( _computeError ) {
1388 _computeError->myName = COMPERR_MEMORY_PB;
1389 //_computeError->myComment = exc.what();
1391 cleanSubMesh( this );
1394 catch ( Standard_OutOfMemory& exc ) {
1395 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1396 if ( _computeError ) {
1397 _computeError->myName = COMPERR_MEMORY_PB;
1398 //_computeError->myComment = exc.what();
1400 cleanSubMesh( this );
1401 throw std::bad_alloc();
1403 catch (Standard_Failure& ex) {
1404 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1405 _computeError->myName = COMPERR_OCC_EXCEPTION;
1406 _computeError->myComment += ex.DynamicType()->Name();
1407 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1408 _computeError->myComment += ": ";
1409 _computeError->myComment += ex.GetMessageString();
1412 catch ( SALOME_Exception& S_ex ) {
1413 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1414 _computeError->myName = COMPERR_SLM_EXCEPTION;
1415 _computeError->myComment = S_ex.what();
1417 catch ( std::exception& exc ) {
1418 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1419 _computeError->myName = COMPERR_STD_EXCEPTION;
1420 _computeError->myComment = exc.what();
1423 if ( _computeError )
1424 _computeError->myName = COMPERR_EXCEPTION;
1428 if (ret && !_alwaysComputed && shape == _subShape) { // check if anything was built
1429 ret = ( GetSubMeshDS() && ( GetSubMeshDS()->NbElements() || GetSubMeshDS()->NbNodes() ));
1431 bool isComputeErrorSet = !CheckComputeError( algo, shape );
1432 if (!ret && !isComputeErrorSet)
1434 // Set _computeError
1435 if ( !_computeError )
1436 _computeError = SMESH_ComputeError::New();
1437 if ( _computeError->IsOK() )
1438 _computeError->myName = COMPERR_ALGO_FAILED;
1439 _computeState = FAILED_TO_COMPUTE;
1443 _computeError.reset();
1445 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1450 RemoveSubMeshElementsAndNodes();
1451 _computeState = NOT_READY;
1452 algo = gen->GetAlgo((*_father), _subShape);
1455 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1457 _computeState = READY_TO_COMPUTE;
1459 SetAlgoState(MISSING_HYP);
1462 case SUBMESH_COMPUTED: // nothing to do
1464 case SUBMESH_RESTORED:
1465 // check if a mesh is already computed that may
1466 // happen after retrieval from a file
1467 ComputeStateEngine( CHECK_COMPUTE_STATE );
1468 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1469 algo = gen->GetAlgo(*_father, _subShape);
1470 if (algo) algo->SubmeshRestored( this );
1472 case MESH_ENTITY_REMOVED:
1474 case CHECK_COMPUTE_STATE:
1475 if ( IsMeshComputed() )
1476 _computeState = COMPUTE_OK;
1484 // ----------------------------------------------------------------------
1489 case MODIF_ALGO_STATE:
1490 ComputeStateEngine( CLEAN );
1491 algo = gen->GetAlgo((*_father), _subShape);
1492 if (algo && !algo->NeedDescretBoundary())
1493 CleanDependsOn(); // clean sub-meshes with event CLEAN
1495 case COMPUTE: // nothing to do
1498 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1499 RemoveSubMeshElementsAndNodes();
1500 _computeState = NOT_READY;
1501 if ( _algoState == HYP_OK )
1502 _computeState = READY_TO_COMPUTE;
1504 case SUBMESH_COMPUTED: // nothing to do
1506 case SUBMESH_RESTORED:
1507 ComputeStateEngine( CHECK_COMPUTE_STATE );
1508 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1509 algo = gen->GetAlgo(*_father, _subShape);
1510 if (algo) algo->SubmeshRestored( this );
1512 case MESH_ENTITY_REMOVED:
1513 UpdateDependantsState( CHECK_COMPUTE_STATE );
1514 ComputeStateEngine( CHECK_COMPUTE_STATE );
1515 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1517 case CHECK_COMPUTE_STATE:
1518 if ( !IsMeshComputed() )
1519 if (_algoState == HYP_OK)
1520 _computeState = READY_TO_COMPUTE;
1522 _computeState = NOT_READY;
1530 // ----------------------------------------------------------------------
1532 case FAILED_TO_COMPUTE:
1535 case MODIF_ALGO_STATE:
1536 algo = gen->GetAlgo((*_father), _subShape);
1537 if (algo && !algo->NeedDescretBoundary())
1538 CleanDependsOn(); // clean sub-meshes with event CLEAN
1539 if (_algoState == HYP_OK)
1540 _computeState = READY_TO_COMPUTE;
1542 _computeState = NOT_READY;
1544 case COMPUTE: // nothing to do
1547 CleanDependants(); // submeshes dependent on me should be cleaned as well
1548 RemoveSubMeshElementsAndNodes();
1550 case SUBMESH_COMPUTED: // allow retry compute
1551 if (_algoState == HYP_OK)
1552 _computeState = READY_TO_COMPUTE;
1554 _computeState = NOT_READY;
1556 case SUBMESH_RESTORED:
1557 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1559 case MESH_ENTITY_REMOVED:
1561 case CHECK_COMPUTE_STATE:
1562 if ( IsMeshComputed() )
1563 _computeState = COMPUTE_OK;
1565 if (_algoState == HYP_OK)
1566 _computeState = READY_TO_COMPUTE;
1568 _computeState = NOT_READY;
1576 // ----------------------------------------------------------------------
1582 NotifyListenersOnEvent( event, COMPUTE_EVENT );
1588 //=============================================================================
1592 //=============================================================================
1594 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1596 _computeError.reset();
1600 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1601 std::vector<int> aVec(SMDSEntity_Last);
1602 for(int i= SMDSEntity_Node; i < SMDSEntity_Last; i++)
1604 aVec[SMDSEntity_Node] = 1;
1605 aResMap.insert(std::make_pair(this,aVec));
1609 SMESH_Gen *gen = _father->GetGen();
1610 SMESH_Algo *algo = 0;
1611 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1613 algo = gen->GetAlgo((*_father), _subShape);
1615 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1616 if (!ret) return false;
1618 TopoDS_Shape shape = _subShape;
1620 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1622 ret = algo->Evaluate((*_father), shape, aResMap);
1629 //=======================================================================
1631 * \brief Update compute_state by _computeError and send proper events to
1632 * dependent submeshes
1633 * \retval bool - true if _computeError is NOT set
1635 //=======================================================================
1637 bool SMESH_subMesh::CheckComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1639 bool noErrors = true;
1641 if ( !theShape.IsNull() )
1643 // Check state of submeshes
1644 if ( !theAlgo->NeedDescretBoundary())
1646 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1647 while ( smIt->more() )
1648 if ( !smIt->next()->CheckComputeError( theAlgo ))
1652 // Check state of neighbours
1653 if ( !theAlgo->OnlyUnaryInput() &&
1654 theShape.ShapeType() == TopAbs_COMPOUND &&
1655 !theShape.IsSame( _subShape ))
1657 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1658 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1660 if ( !sm->CheckComputeError( theAlgo, sm->GetSubShape() ))
1662 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1669 if ( !_computeError || _computeError->IsOK() )
1671 _computeState = COMPUTE_OK;
1675 if ( !_computeError->myAlgo )
1676 _computeError->myAlgo = theAlgo;
1680 text << theAlgo->GetName() << " failed on subshape #" << _Id << " with error ";
1681 if (_computeError->IsCommon() )
1682 text << _computeError->CommonName();
1684 text << _computeError->myName;
1685 if ( _computeError->myComment.size() > 0 )
1686 text << " \"" << _computeError->myComment << "\"";
1689 MESSAGE_BEGIN ( text );
1690 // Show vertices location of a failed shape
1691 TopTools_IndexedMapOfShape vMap;
1692 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1693 MESSAGE_ADD ( "Subshape vertices " << ( vMap.Extent()>10 ? "(first 10):" : ":") );
1694 for ( int iv = 1; iv <= vMap.Extent() && iv < 11; ++iv ) {
1695 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1696 MESSAGE_ADD ( "#" << _father->GetMeshDS()->ShapeToIndex( vMap( iv )) << " "
1697 << P.X() << " " << P.Y() << " " << P.Z() << " " );
1702 _computeState = FAILED_TO_COMPUTE;
1709 //=======================================================================
1710 //function : ApplyToCollection
1711 //purpose : Apply theAlgo to all subshapes in theCollection
1712 //=======================================================================
1714 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1715 const TopoDS_Shape& theCollection)
1717 MESSAGE("SMESH_subMesh::ApplyToCollection");
1718 ASSERT ( !theAlgo->NeedDescretBoundary() );
1720 if ( _computeError )
1721 _computeError->myName = COMPERR_OK;
1723 bool ok = theAlgo->Compute( *_father, theCollection );
1725 // set _computeState of subshapes
1726 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1727 for ( ; anExplorer.More(); anExplorer.Next() )
1729 if ( SMESH_subMesh* subMesh = _father->GetSubMeshContaining( anExplorer.Current() ))
1731 bool localOK = subMesh->CheckComputeError( theAlgo );
1732 if ( !ok && localOK && !subMesh->IsMeshComputed() )
1734 subMesh->_computeError = theAlgo->GetComputeError();
1735 if ( subMesh->_computeError->IsOK() )
1736 _computeError = SMESH_ComputeError::New(COMPERR_ALGO_FAILED);
1737 localOK = CheckComputeError( theAlgo );
1740 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1741 subMesh->UpdateSubMeshState( localOK ? COMPUTE_OK : FAILED_TO_COMPUTE );
1749 //=======================================================================
1750 //function : UpdateSubMeshState
1752 //=======================================================================
1754 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1756 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1757 while ( smIt->more() )
1758 smIt->next()->_computeState = theState;
1761 //=======================================================================
1762 //function : ComputeSubMeshStateEngine
1764 //=======================================================================
1766 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1768 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1769 while ( smIt->more() )
1770 smIt->next()->ComputeStateEngine(event);
1773 //=======================================================================
1774 //function : UpdateDependantsState
1776 //=======================================================================
1778 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1780 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1781 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1782 for (; it.More(); it.Next())
1784 const TopoDS_Shape& ancestor = it.Value();
1785 SMESH_subMesh *aSubMesh =
1786 _father->GetSubMeshContaining(ancestor);
1788 aSubMesh->ComputeStateEngine( theEvent );
1792 //=============================================================================
1796 //=============================================================================
1798 void SMESH_subMesh::CleanDependants()
1800 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1802 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1803 for (; it.More(); it.Next())
1805 const TopoDS_Shape& ancestor = it.Value();
1806 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1807 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1808 // will erase mesh on other shapes in a compound
1809 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1810 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1812 aSubMesh->ComputeStateEngine(CLEAN);
1818 //=============================================================================
1822 //=============================================================================
1824 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1826 //SCRUTE(_subShape.ShapeType());
1828 cleanSubMesh( this );
1830 // algo may bind a submesh not to _subShape, eg 3D algo
1831 // sets nodes on SHELL while _subShape may be SOLID
1833 int dim = SMESH_Gen::GetShapeDim( _subShape );
1834 int type = _subShape.ShapeType() + 1;
1835 for ( ; type <= TopAbs_EDGE; type++) {
1836 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1838 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1839 for ( ; exp.More(); exp.Next() )
1840 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1847 //=======================================================================
1848 //function : GetCollection
1849 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1850 // meshed at once along with _subShape
1851 //=======================================================================
1853 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen,
1854 SMESH_Algo* theAlgo,
1855 bool & theSubComputed)
1857 MESSAGE("SMESH_subMesh::GetCollection");
1859 theSubComputed = SubMeshesComputed();
1861 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1863 if ( mainShape.IsSame( _subShape ))
1866 const bool ignoreAuxiliaryHyps = false;
1867 list<const SMESHDS_Hypothesis*> aUsedHyp =
1868 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1870 // put in a compound all shapes with the same hypothesis assigned
1871 // and a good ComputState
1873 TopoDS_Compound aCompound;
1874 BRep_Builder aBuilder;
1875 aBuilder.MakeCompound( aCompound );
1877 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1878 for ( ; anExplorer.More(); anExplorer.Next() )
1880 const TopoDS_Shape& S = anExplorer.Current();
1881 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1882 if ( subMesh == this )
1884 aBuilder.Add( aCompound, S );
1886 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1888 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1889 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1890 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1891 aBuilder.Add( aCompound, S );
1892 if ( !subMesh->SubMeshesComputed() )
1893 theSubComputed = false;
1900 //=======================================================================
1901 //function : GetSimilarAttached
1902 //purpose : return a hypothesis attached to theShape.
1903 // If theHyp is provided, similar but not same hypotheses
1904 // is returned; else only applicable ones having theHypType
1906 //=======================================================================
1908 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1909 const SMESH_Hypothesis * theHyp,
1910 const int theHypType)
1912 SMESH_HypoFilter hypoKind;
1913 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1915 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1916 hypoKind.AndNot( hypoKind.Is( theHyp ));
1917 if ( theHyp->IsAuxiliary() )
1918 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1920 hypoKind.AndNot( hypoKind.IsAuxiliary());
1923 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1926 return _father->GetHypothesis( theShape, hypoKind, false );
1929 //=======================================================================
1930 //function : CheckConcurentHypothesis
1931 //purpose : check if there are several applicable hypothesis attached to
1933 //=======================================================================
1935 SMESH_Hypothesis::Hypothesis_Status
1936 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1938 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1940 // is there local hypothesis on me?
1941 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1942 return SMESH_Hypothesis::HYP_OK;
1945 TopoDS_Shape aPrevWithHyp;
1946 const SMESH_Hypothesis* aPrevHyp = 0;
1947 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1948 for (; it.More(); it.Next())
1950 const TopoDS_Shape& ancestor = it.Value();
1951 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1954 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1956 aPrevWithHyp = ancestor;
1959 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1960 return SMESH_Hypothesis::HYP_CONCURENT;
1962 return SMESH_Hypothesis::HYP_OK;
1965 return SMESH_Hypothesis::HYP_OK;
1968 //================================================================================
1970 * \brief Sets an event listener and its data to a submesh
1971 * \param listener - the listener to store
1972 * \param data - the listener data to store
1973 * \param where - the submesh to store the listener and it's data
1974 * \param deleteListener - if true then the listener will be deleted as
1975 * it is removed from where submesh
1977 * It remembers the submesh where it puts the listener in order to delete
1978 * them when HYP_OK algo_state is lost
1979 * After being set, event listener is notified on each event of where submesh.
1981 //================================================================================
1983 void SMESH_subMesh::SetEventListener(EventListener* listener,
1984 EventListenerData* data,
1985 SMESH_subMesh* where)
1987 if ( listener && where ) {
1988 where->SetEventListener( listener, data );
1989 myOwnListeners.push_back( make_pair( where, listener ));
1993 //================================================================================
1995 * \brief Sets an event listener and its data to a submesh
1996 * \param listener - the listener to store
1997 * \param data - the listener data to store
1999 * After being set, event listener is notified on each event of a submesh.
2001 //================================================================================
2003 void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
2005 map< EventListener*, EventListenerData* >::iterator l_d =
2006 myEventListeners.find( listener );
2007 if ( l_d != myEventListeners.end() ) {
2008 EventListenerData* curData = l_d->second;
2009 if ( curData && curData != data && curData->IsDeletable() )
2014 myEventListeners.insert( make_pair( listener, data ));
2017 //================================================================================
2019 * \brief Return an event listener data
2020 * \param listener - the listener whose data is
2021 * \retval EventListenerData* - found data, maybe NULL
2023 //================================================================================
2025 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
2027 map< EventListener*, EventListenerData* >::const_iterator l_d =
2028 myEventListeners.find( listener );
2029 if ( l_d != myEventListeners.end() )
2034 //================================================================================
2036 * \brief Notify stored event listeners on the occured event
2037 * \param event - algo_event or compute_event itself
2038 * \param eventType - algo_event or compute_event
2039 * \param subMesh - the submesh where the event occures
2040 * \param data - listener data stored in the subMesh
2041 * \param hyp - hypothesis, if eventType is algo_event
2043 //================================================================================
2045 void SMESH_subMesh::NotifyListenersOnEvent( const int event,
2046 const event_type eventType,
2047 SMESH_Hypothesis* hyp)
2049 map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
2050 for ( ; l_d != myEventListeners.end(); ++l_d )
2051 l_d->first->ProcessEvent( event, eventType, this, l_d->second, hyp );
2054 //================================================================================
2056 * \brief Unregister the listener and delete listener's data
2057 * \param listener - the event listener
2059 //================================================================================
2061 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2063 map< EventListener*, EventListenerData* >::iterator l_d =
2064 myEventListeners.find( listener );
2065 if ( l_d != myEventListeners.end() ) {
2066 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2067 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2068 myEventListeners.erase( l_d );
2072 //================================================================================
2074 * \brief Delete event listeners depending on algo of this submesh
2076 //================================================================================
2078 void SMESH_subMesh::DeleteOwnListeners()
2080 list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
2081 for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
2082 sm_l->first->DeleteEventListener( sm_l->second );
2083 myOwnListeners.clear();
2086 //================================================================================
2088 * \brief Do something on a certain event
2089 * \param event - algo_event or compute_event itself
2090 * \param eventType - algo_event or compute_event
2091 * \param subMesh - the submesh where the event occures
2092 * \param data - listener data stored in the subMesh
2093 * \param hyp - hypothesis, if eventType is algo_event
2095 * The base implementation translates CLEAN event to the subMesh
2096 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2097 * successful COMPUTE event.
2099 //================================================================================
2101 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2102 const int eventType,
2103 SMESH_subMesh* subMesh,
2104 EventListenerData* data,
2105 const SMESH_Hypothesis* /*hyp*/)
2107 if ( data && !data->mySubMeshes.empty() &&
2108 eventType == SMESH_subMesh::COMPUTE_EVENT)
2110 ASSERT( data->mySubMeshes.front() != subMesh );
2111 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2112 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2114 case SMESH_subMesh::CLEAN:
2115 for ( ; smIt != smEnd; ++ smIt)
2116 (*smIt)->ComputeStateEngine( event );
2118 case SMESH_subMesh::COMPUTE:
2119 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2120 for ( ; smIt != smEnd; ++ smIt)
2121 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2130 //================================================================================
2132 * \brief Iterator over submeshes and optionally prepended or appended one
2134 //================================================================================
2136 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2138 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2139 SMESH_subMesh* prepend,
2140 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2142 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2143 if ( myCur == append ) append = 0;
2145 /// Return true if and only if there are other object in this iterator
2150 /// Return the current object and step to the next one
2151 virtual SMESH_subMesh* next()
2153 SMESH_subMesh* res = myCur;
2154 if ( myIt->more() ) { myCur = myIt->next(); }
2155 else { myCur = myAppend; myAppend = 0; }
2162 SMESH_subMesh *myAppend, *myCur;
2163 SMDS_Iterator<SMESH_subMesh*> *myIt;
2167 //================================================================================
2169 * \brief Return iterator on the submeshes this one depends on
2170 * \param includeSelf - this submesh to be returned also
2171 * \param reverse - if true, complex shape submeshes go first
2173 //================================================================================
2175 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2178 SMESH_subMesh *prepend=0, *append=0;
2179 if ( includeSelf ) {
2180 if ( reverse ) prepend = this;
2183 typedef map < int, SMESH_subMesh * > TMap;
2186 return SMESH_subMeshIteratorPtr
2187 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2190 return SMESH_subMeshIteratorPtr
2191 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));
2195 //================================================================================
2197 * \brief Find common submeshes (based on shared subshapes with other
2198 * \param theOther submesh to check
2199 * \param theSetOfCommon set of common submesh
2201 //================================================================================
2203 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2204 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2206 int oldNb = theSetOfCommon.size();
2207 // check main submeshes
2208 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2209 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2210 theSetOfCommon.insert( this );
2211 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2212 theSetOfCommon.insert( theOther );
2213 // check common submeshes
2214 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2215 for( ; mapIt != _mapDepend.end(); mapIt++ )
2216 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2217 theSetOfCommon.insert( (*mapIt).second );
2218 return oldNb < theSetOfCommon.size();