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"
42 #include "Basics_Utils.hxx"
44 #include <BRep_Builder.hxx>
45 #include <BRep_Tool.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopoDS_Compound.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopoDS_Iterator.hxx>
55 #include <Standard_OutOfMemory.hxx>
56 #include <Standard_ErrorHandler.hxx>
62 //=============================================================================
64 * \brief Allocate some memory at construction and release it at destruction.
65 * Is used to be able to continue working after mesh generation breaks due to
68 //=============================================================================
73 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
74 ~MemoryReserve() { delete [] myBuf; }
77 //=============================================================================
79 * default constructor:
81 //=============================================================================
83 SMESH_subMesh::SMESH_subMesh(int Id,
85 SMESHDS_Mesh * meshDS,
86 const TopoDS_Shape & aSubShape)
88 _subShape = aSubShape;
89 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
92 _dependenceAnalysed = _alwaysComputed = false;
94 if (_subShape.ShapeType() == TopAbs_VERTEX)
97 _computeState = READY_TO_COMPUTE;
101 _algoState = NO_ALGO;
102 _computeState = NOT_READY;
106 //=============================================================================
110 //=============================================================================
112 SMESH_subMesh::~SMESH_subMesh()
114 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
116 DeleteOwnListeners();
119 //=============================================================================
123 //=============================================================================
125 int SMESH_subMesh::GetId() const
127 //MESSAGE("SMESH_subMesh::GetId");
131 //=============================================================================
135 //=============================================================================
137 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
139 // submesh appears in DS only when a mesher set nodes and elements on a shape
140 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
143 //=============================================================================
147 //=============================================================================
149 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
151 if ( !GetSubMeshDS() ) {
152 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
153 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
155 return GetSubMeshDS();
158 //=============================================================================
162 //=============================================================================
164 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
166 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
167 while ( smIt->more() ) {
168 SMESH_subMesh *sm = smIt->next();
169 if ( sm->GetComputeState() == READY_TO_COMPUTE )
172 return 0; // nothing to compute
175 //================================================================================
177 * \brief Allow algo->Compute() if a subshape of lower dim is meshed but
178 * none mesh entity is bound to it (PAL13615, 2nd part)
180 //================================================================================
182 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
184 _alwaysComputed = isAlCo;
185 if ( _alwaysComputed )
186 _computeState = COMPUTE_OK;
188 ComputeStateEngine( CHECK_COMPUTE_STATE );
191 //=======================================================================
193 * \brief Return true if no mesh entities is bound to the submesh
195 //=======================================================================
197 bool SMESH_subMesh::IsEmpty() const
199 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
200 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
204 //=======================================================================
205 //function : IsMeshComputed
206 //purpose : check if _subMeshDS contains mesh elements
207 //=======================================================================
209 bool SMESH_subMesh::IsMeshComputed() const
211 if ( _alwaysComputed )
213 // algo may bind a submesh not to _subShape, eg 3D algo
214 // sets nodes on SHELL while _subShape may be SOLID
216 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
217 int dim = SMESH_Gen::GetShapeDim( _subShape );
218 int type = _subShape.ShapeType();
219 for ( ; type <= TopAbs_VERTEX; type++) {
220 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
222 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
223 for ( ; exp.More(); exp.Next() )
225 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
227 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
240 //=============================================================================
244 //=============================================================================
246 bool SMESH_subMesh::SubMeshesComputed()
248 int myDim = SMESH_Gen::GetShapeDim( _subShape );
249 int dimToCheck = myDim - 1;
250 bool subMeshesComputed = true;
251 // check subMeshes with upper dimension => reverse iteration
252 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
253 while ( smIt->more() )
255 SMESH_subMesh *sm = smIt->next();
256 if ( sm->_alwaysComputed )
258 const TopoDS_Shape & ss = sm->GetSubShape();
259 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
260 // in checking of existence of edges if the algo needs only faces. Moreover,
261 // degenerated edges may have no submesh, as after computing NETGEN_2D.
262 int dim = SMESH_Gen::GetShapeDim( ss );
263 if (dim < dimToCheck)
264 break; // the rest subMeshes are all of less dimension
265 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
266 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
267 (ds && ( ds->NbNodes() || ds->NbElements() )));
270 int type = ss.ShapeType();
272 subMeshesComputed = false;
276 case TopAbs_COMPOUND:
278 MESSAGE("The not computed sub mesh is a COMPOUND");
281 case TopAbs_COMPSOLID:
283 MESSAGE("The not computed sub mesh is a COMPSOLID");
288 MESSAGE("The not computed sub mesh is a SHEL");
293 MESSAGE("The not computed sub mesh is a WIRE");
298 MESSAGE("The not computed sub mesh is a SOLID");
303 MESSAGE("The not computed sub mesh is a FACE");
308 MESSAGE("The not computed sub mesh is a EDGE");
313 MESSAGE("The not computed sub mesh is of unknown type");
321 return subMeshesComputed;
324 //=============================================================================
328 //=============================================================================
330 bool SMESH_subMesh::SubMeshesReady()
332 bool subMeshesReady = true;
333 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
334 while ( smIt->more() ) {
335 SMESH_subMesh *sm = smIt->next();
336 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
337 sm->GetComputeState() == READY_TO_COMPUTE);
340 subMeshesReady = false;
345 return subMeshesReady;
348 //=============================================================================
350 * Construct dependence on first level subMeshes. complex shapes (compsolid,
351 * shell, wire) are not analysed the same way as simple shapes (solid, face,
353 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
354 * with possible multiples occurences. Multiples occurences corresponds to
355 * internal frontiers within shapes of the collection and must not be keeped.
356 * See FinalizeDependence.
358 //=============================================================================
360 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
362 if (_dependenceAnalysed)
365 //MESSAGE("SMESH_subMesh::DependsOn");
367 int type = _subShape.ShapeType();
371 case TopAbs_COMPOUND:
373 //MESSAGE("compound");
374 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
377 InsertDependence(exp.Current());
379 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
382 InsertDependence(exp.Current()); //only shell not in solid
384 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
387 InsertDependence(exp.Current());
389 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
392 InsertDependence(exp.Current());
396 case TopAbs_COMPSOLID:
398 //MESSAGE("compsolid");
399 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
402 InsertDependence(exp.Current());
409 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
412 InsertDependence(exp.Current());
419 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
422 InsertDependence(exp.Current());
429 if(_father->HasShapeToMesh()) {
430 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
433 InsertDependence(exp.Current());
441 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
444 InsertDependence(exp.Current());
451 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
454 InsertDependence(exp.Current());
467 _dependenceAnalysed = true;
471 //=============================================================================
473 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
475 //=============================================================================
477 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
479 //MESSAGE("SMESH_subMesh::InsertDependence");
480 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
481 int type = aSubShape.ShapeType();
482 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
483 int cle = aSubMesh->GetId();
484 cle += 10000000 * ordType; // sort map by ordType then index
485 if ( _mapDepend.find( cle ) == _mapDepend.end())
487 _mapDepend[cle] = aSubMesh;
488 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
489 _mapDepend.insert( subMap.begin(), subMap.end() );
493 //=============================================================================
497 //=============================================================================
499 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
501 //MESSAGE("SMESH_subMesh::GetSubShape");
506 //=======================================================================
507 //function : CanAddHypothesis
508 //purpose : return true if theHypothesis can be attached to me:
509 // its dimention is checked
510 //=======================================================================
512 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
514 int aHypDim = theHypothesis->GetDim();
515 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
516 if (aHypDim == 3 && aShapeDim == 3) {
517 // check case of open shell
518 //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
519 if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
522 if ( aHypDim <= aShapeDim )
528 //=======================================================================
529 //function : IsApplicableHypotesis
531 //=======================================================================
533 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
534 const TopAbs_ShapeEnum theShapeType)
536 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
538 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
541 switch ( theShapeType ) {
546 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
549 // Special case for algorithms, building 2D mesh on a whole shell.
550 // Before this fix there was a problem after restoring from study,
551 // because in that case algorithm is assigned before hypothesis
552 // (on shell in problem case) and hypothesis is checked on faces
553 // (because it is 2D), where we have NO_ALGO state.
554 // Now 2D hypothesis is also applicable to shells.
555 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
558 // case TopAbs_COMPSOLID:
559 // case TopAbs_COMPOUND:
565 //=============================================================================
569 //=============================================================================
571 SMESH_Hypothesis::Hypothesis_Status
572 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
574 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
575 //SCRUTE(_algoState);
578 // **** les retour des evenement shape sont significatifs
579 // (add ou remove fait ou non)
580 // le retour des evenement father n'indiquent pas que add ou remove fait
582 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
584 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
585 SMESH_Gen* gen =_father->GetGen();
586 SMESH_Algo* algo = 0;
588 if (_subShape.ShapeType() == TopAbs_VERTEX )
590 if ( anHyp->GetDim() != 0) {
591 if (event == ADD_HYP || event == ADD_ALGO)
592 return SMESH_Hypothesis::HYP_BAD_DIM;
594 return SMESH_Hypothesis::HYP_OK;
597 else if ( _algoState == HYP_OK ) {
598 // update default _algoState
599 if ( event != REMOVE_FATHER_ALGO )
601 _algoState = NO_ALGO;
602 algo = gen->GetAlgo(*_father, _subShape);
604 _algoState = MISSING_HYP;
605 if ( event == REMOVE_FATHER_HYP ||
606 algo->CheckHypothesis(*_father,_subShape, aux_ret))
613 int oldAlgoState = _algoState;
614 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
615 bool needFullClean = false;
617 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
619 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
621 // -------------------------------------------
622 // check if a shape needed by algo is present
623 // -------------------------------------------
624 algo = static_cast< SMESH_Algo* >( anHyp );
625 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
626 return SMESH_Hypothesis::HYP_NEED_SHAPE;
627 // ----------------------
628 // check mesh conformity
629 // ----------------------
630 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
631 return SMESH_Hypothesis::HYP_NOTCONFORM;
633 // check if all-dimensional algo is hidden by other local one
634 if ( event == ADD_ALGO ) {
635 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
636 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
637 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
638 if ( SMESH_Algo * curAlgo = (SMESH_Algo*) _father->GetHypothesis( _subShape, filter, true ))
639 needFullClean = ( !curAlgo->NeedDescretBoundary() );
643 // ----------------------------------
644 // add a hypothesis to DS if possible
645 // ----------------------------------
646 if (event == ADD_HYP || event == ADD_ALGO)
648 if ( ! CanAddHypothesis( anHyp )) // check dimension
649 return SMESH_Hypothesis::HYP_BAD_DIM;
651 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
652 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
654 if ( !meshDS->AddHypothesis(_subShape, anHyp))
655 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
658 // --------------------------
659 // remove a hypothesis from DS
660 // --------------------------
661 if (event == REMOVE_HYP || event == REMOVE_ALGO)
663 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
664 return SMESH_Hypothesis::HYP_OK; // nothing changes
666 if (event == REMOVE_ALGO)
668 algo = dynamic_cast<SMESH_Algo*> (anHyp);
669 if (!algo->NeedDescretBoundary())
671 // clean all mesh in the tree of the current submesh;
672 // we must perform it now because later
673 // we will have no information about the type of the removed algo
674 needFullClean = true;
679 // ------------------
680 // analyse algo state
681 // ------------------
682 if (!isApplicableHyp)
683 return ret; // not applicable hypotheses do not change algo state
688 // ----------------------------------------------------------------------
695 algo = gen->GetAlgo((*_father), _subShape);
697 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
698 SetAlgoState(HYP_OK);
699 else if ( algo->IsStatusFatal( aux_ret )) {
700 meshDS->RemoveHypothesis(_subShape, anHyp);
704 SetAlgoState(MISSING_HYP);
711 case ADD_FATHER_ALGO: { // Algo just added in father
712 algo = gen->GetAlgo((*_father), _subShape);
714 if ( algo == anHyp ) {
715 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
716 SetAlgoState(HYP_OK);
718 SetAlgoState(MISSING_HYP);
722 case REMOVE_FATHER_HYP:
724 case REMOVE_FATHER_ALGO: {
725 algo = gen->GetAlgo((*_father), _subShape);
728 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
729 SetAlgoState(HYP_OK);
731 SetAlgoState(MISSING_HYP);
735 case MODIF_HYP: break;
742 // ----------------------------------------------------------------------
748 algo = gen->GetAlgo((*_father), _subShape);
750 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
751 SetAlgoState(HYP_OK);
752 if (SMESH_Hypothesis::IsStatusFatal( ret ))
753 meshDS->RemoveHypothesis(_subShape, anHyp);
754 else if (!_father->IsUsedHypothesis( anHyp, this ))
756 meshDS->RemoveHypothesis(_subShape, anHyp);
757 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
761 case ADD_ALGO: { //already existing algo : on father ?
762 algo = gen->GetAlgo((*_father), _subShape);
764 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
765 SetAlgoState(HYP_OK);
766 else if ( algo->IsStatusFatal( aux_ret )) {
767 meshDS->RemoveHypothesis(_subShape, anHyp);
771 SetAlgoState(MISSING_HYP);
776 case REMOVE_ALGO: { // perhaps a father algo applies ?
777 algo = gen->GetAlgo((*_father), _subShape);
778 if (algo == NULL) // no more algo applying on subShape...
780 SetAlgoState(NO_ALGO);
784 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
785 SetAlgoState(HYP_OK);
787 SetAlgoState(MISSING_HYP);
791 case MODIF_HYP: // assigned hypothesis value may become good
792 case ADD_FATHER_HYP: {
793 algo = gen->GetAlgo((*_father), _subShape);
795 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
796 SetAlgoState(HYP_OK);
798 SetAlgoState(MISSING_HYP);
801 case ADD_FATHER_ALGO: { // new father algo
802 algo = gen->GetAlgo((*_father), _subShape);
804 if ( algo == anHyp ) {
805 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
806 SetAlgoState(HYP_OK);
808 SetAlgoState(MISSING_HYP);
812 case REMOVE_FATHER_HYP: // nothing to do
814 case REMOVE_FATHER_ALGO: {
815 algo = gen->GetAlgo((*_father), _subShape);
816 if (algo == NULL) // no more applying algo on father
818 SetAlgoState(NO_ALGO);
822 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
823 SetAlgoState(HYP_OK);
825 SetAlgoState(MISSING_HYP);
835 // ----------------------------------------------------------------------
841 algo = gen->GetAlgo((*_father), _subShape);
843 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
845 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
846 // ret should be fatal: anHyp was not added
847 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
849 else if (!_father->IsUsedHypothesis( anHyp, this ))
850 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
852 if (SMESH_Hypothesis::IsStatusFatal( ret ))
854 MESSAGE("do not add extra hypothesis");
855 meshDS->RemoveHypothesis(_subShape, anHyp);
863 case ADD_ALGO: { //already existing algo : on father ?
864 algo = gen->GetAlgo((*_father), _subShape);
865 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
866 // check if algo changes
868 f.Init( SMESH_HypoFilter::IsAlgo() );
869 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
870 f.AndNot( SMESH_HypoFilter::Is( algo ));
871 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
873 string(algo->GetName()) != string(prevAlgo->GetName()) )
877 SetAlgoState(MISSING_HYP);
881 algo = gen->GetAlgo((*_father), _subShape);
883 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
884 SetAlgoState(HYP_OK);
886 SetAlgoState(MISSING_HYP);
890 case REMOVE_ALGO: { // perhaps a father algo applies ?
891 algo = gen->GetAlgo((*_father), _subShape);
892 if (algo == NULL) // no more algo applying on subShape...
894 SetAlgoState(NO_ALGO);
898 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
899 // check if algo remains
900 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
904 SetAlgoState(MISSING_HYP);
908 case MODIF_HYP: // hypothesis value may become bad
909 case ADD_FATHER_HYP: { // new father hypothesis ?
910 algo = gen->GetAlgo((*_father), _subShape);
912 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
914 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
918 SetAlgoState(MISSING_HYP);
921 case ADD_FATHER_ALGO: {
922 algo = gen->GetAlgo((*_father), _subShape);
923 if ( algo == anHyp ) { // a new algo on father
924 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
925 // check if algo changes
927 f.Init( SMESH_HypoFilter::IsAlgo() );
928 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
929 f.AndNot( SMESH_HypoFilter::Is( algo ));
930 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
932 string(algo->GetName()) != string(prevAlgo->GetName()) )
936 SetAlgoState(MISSING_HYP);
940 case REMOVE_FATHER_HYP: {
941 algo = gen->GetAlgo((*_father), _subShape);
943 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
944 // is there the same local hyp or maybe a new father algo applied?
945 if ( !GetSimilarAttached( _subShape, anHyp ) )
949 SetAlgoState(MISSING_HYP);
952 case REMOVE_FATHER_ALGO: {
953 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
954 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
955 algo = dynamic_cast<SMESH_Algo*> (anHyp);
956 if (!algo->NeedDescretBoundary())
957 needFullClean = true;
959 algo = gen->GetAlgo((*_father), _subShape);
960 if (algo == NULL) // no more applying algo on father
962 SetAlgoState(NO_ALGO);
966 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
967 // check if algo changes
968 if ( string(algo->GetName()) != string( anHyp->GetName()) )
972 SetAlgoState(MISSING_HYP);
982 // ----------------------------------------------------------------------
989 // detect algorithm hiding
991 if ( ret == SMESH_Hypothesis::HYP_OK &&
992 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
993 algo->GetName() == anHyp->GetName() )
996 SMESH_Gen* gen = _father->GetGen();
997 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
998 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
999 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
1000 if ( !upperAlgo->NeedDescretBoundary() && !upperAlgo->SupportSubmeshes())
1001 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1004 if ( ret == SMESH_Hypothesis::HYP_OK &&
1005 !algo->NeedDescretBoundary() &&
1006 !algo->SupportSubmeshes()) {
1007 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1008 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1009 if ( gen->GetAlgo( *_father, i_sm->second->_subShape ))
1010 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1014 bool stateChange = ( _algoState != oldAlgoState );
1016 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1017 algo->SetEventListener( this );
1019 NotifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1021 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1022 DeleteOwnListeners();
1023 SetIsAlwaysComputed( false );
1024 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1025 // restore default states
1026 _algoState = HYP_OK;
1027 _computeState = READY_TO_COMPUTE;
1031 if ( needFullClean ) {
1032 // added or removed algo is all-dimensional
1033 ComputeStateEngine( CLEAN );
1035 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1038 if (stateChange || modifiedHyp)
1039 ComputeStateEngine(MODIF_ALGO_STATE);
1044 //=======================================================================
1045 //function : IsConform
1046 //purpose : check if a conform mesh will be produced by the Algo
1047 //=======================================================================
1049 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1051 // MESSAGE( "SMESH_subMesh::IsConform" );
1052 if ( !theAlgo ) return false;
1054 // Suppose that theAlgo is applicable to _subShape, do not check it here
1055 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1057 // check only algo that doesn't NeedDescretBoundary(): because mesh made
1058 // on a sub-shape will be ignored by theAlgo
1059 if ( theAlgo->NeedDescretBoundary() ||
1060 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1063 SMESH_Gen* gen =_father->GetGen();
1065 // only local algo is to be checked
1066 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1067 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1070 // check algo attached to adjacent shapes
1072 // loop on one level down sub-meshes
1073 TopoDS_Iterator itsub( _subShape );
1074 for (; itsub.More(); itsub.Next())
1076 // loop on adjacent subShapes
1077 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1078 for (; it.More(); it.Next())
1080 const TopoDS_Shape& adjacent = it.Value();
1081 if ( _subShape.IsSame( adjacent )) continue;
1082 if ( adjacent.ShapeType() != _subShape.ShapeType())
1085 // check algo attached to smAdjacent
1086 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1088 !algo->NeedDescretBoundary() &&
1089 algo->OnlyUnaryInput())
1090 return false; // NOT CONFORM MESH WILL BE PRODUCED
1097 //=============================================================================
1101 //=============================================================================
1103 void SMESH_subMesh::SetAlgoState(int state)
1108 //=============================================================================
1112 //=============================================================================
1113 SMESH_Hypothesis::Hypothesis_Status
1114 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1115 SMESH_Hypothesis * anHyp)
1117 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1118 //EAP: a wire (dim==1) should notify edges (dim==1)
1119 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1120 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1122 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1123 while ( smIt->more() ) {
1124 SMESH_Hypothesis::Hypothesis_Status ret2 =
1125 smIt->next()->AlgoStateEngine(event, anHyp);
1133 //=============================================================================
1137 //=============================================================================
1139 void SMESH_subMesh::CleanDependsOn()
1141 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1142 while ( smIt->more() )
1143 smIt->next()->ComputeStateEngine(CLEAN);
1146 //=============================================================================
1150 //=============================================================================
1152 void SMESH_subMesh::DumpAlgoState(bool isMain)
1154 int dim = SMESH_Gen::GetShapeDim(_subShape);
1155 // if (dim < 1) return;
1158 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1160 map < int, SMESH_subMesh * >::const_iterator itsub;
1161 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1163 SMESH_subMesh *sm = (*itsub).second;
1164 sm->DumpAlgoState(false);
1167 int type = _subShape.ShapeType();
1168 MESSAGE("dim = " << dim << " type of shape " << type);
1172 MESSAGE(" AlgoState = NO_ALGO");
1175 MESSAGE(" AlgoState = MISSING_HYP");
1178 MESSAGE(" AlgoState = HYP_OK");
1181 switch (_computeState)
1184 MESSAGE(" ComputeState = NOT_READY");
1186 case READY_TO_COMPUTE:
1187 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1190 MESSAGE(" ComputeState = COMPUTE_OK");
1192 case FAILED_TO_COMPUTE:
1193 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1198 //================================================================================
1200 * \brief Remove nodes and elements bound to submesh
1201 * \param subMesh - submesh containing nodes and elements
1203 //================================================================================
1205 static void cleanSubMesh( SMESH_subMesh * subMesh )
1208 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1209 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1210 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1211 while (ite->more()) {
1212 const SMDS_MeshElement * elt = ite->next();
1213 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1214 //meshDS->RemoveElement(elt);
1215 meshDS->RemoveFreeElement(elt, subMeshDS);
1218 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1219 while (itn->more()) {
1220 const SMDS_MeshNode * node = itn->next();
1221 //MESSAGE( " RM node: "<<node->GetID());
1222 if ( node->NbInverseElements() == 0 )
1223 meshDS->RemoveFreeNode(node, subMeshDS);
1224 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1225 meshDS->RemoveNode(node);
1231 //=============================================================================
1235 //=============================================================================
1237 bool SMESH_subMesh::ComputeStateEngine(int event)
1239 _computeError.reset();
1241 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1242 //SCRUTE(_computeState);
1245 if (_subShape.ShapeType() == TopAbs_VERTEX)
1247 _computeState = READY_TO_COMPUTE;
1248 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1249 if ( smDS && smDS->NbNodes() ) {
1250 if ( event == CLEAN ) {
1252 cleanSubMesh( this );
1255 _computeState = COMPUTE_OK;
1257 else if ( event == COMPUTE && !_alwaysComputed ) {
1258 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1259 gp_Pnt P = BRep_Tool::Pnt(V);
1260 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1261 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1262 _computeState = COMPUTE_OK;
1265 if ( event == MODIF_ALGO_STATE )
1269 SMESH_Gen *gen = _father->GetGen();
1270 SMESH_Algo *algo = 0;
1272 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1273 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1275 switch (_computeState)
1278 // ----------------------------------------------------------------------
1283 case MODIF_ALGO_STATE:
1284 algo = gen->GetAlgo((*_father), _subShape);
1285 if (algo && !algo->NeedDescretBoundary())
1286 CleanDependsOn(); // clean sub-meshes with event CLEAN
1287 if ( _algoState == HYP_OK )
1288 _computeState = READY_TO_COMPUTE;
1290 case COMPUTE: // nothing to do
1294 RemoveSubMeshElementsAndNodes();
1296 case SUBMESH_COMPUTED: // nothing to do
1298 case SUBMESH_RESTORED:
1299 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1301 case MESH_ENTITY_REMOVED:
1303 case CHECK_COMPUTE_STATE:
1304 if ( IsMeshComputed() )
1305 _computeState = COMPUTE_OK;
1313 // ----------------------------------------------------------------------
1315 case READY_TO_COMPUTE:
1318 case MODIF_ALGO_STATE:
1319 _computeState = NOT_READY;
1320 algo = gen->GetAlgo((*_father), _subShape);
1323 if (!algo->NeedDescretBoundary())
1324 CleanDependsOn(); // clean sub-meshes with event CLEAN
1325 if ( _algoState == HYP_OK )
1326 _computeState = READY_TO_COMPUTE;
1331 algo = gen->GetAlgo((*_father), _subShape);
1333 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1336 MESSAGE("***** verify compute state *****");
1337 _computeState = NOT_READY;
1338 SetAlgoState(MISSING_HYP);
1341 TopoDS_Shape shape = _subShape;
1342 // check submeshes needed
1343 if (_father->HasShapeToMesh() ) {
1344 bool subComputed = false;
1345 if (!algo->OnlyUnaryInput())
1346 shape = GetCollection( gen, algo, subComputed );
1348 subComputed = SubMeshesComputed();
1349 ret = ( algo->NeedDescretBoundary() ? subComputed :
1350 algo->SupportSubmeshes() ? true :
1351 ( !subComputed || _father->IsNotConformAllowed() ));
1353 _computeState = FAILED_TO_COMPUTE;
1354 if ( !algo->NeedDescretBoundary() )
1356 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1357 "Unexpected computed submesh",algo);
1362 // CleanDependants(); for "UseExisting_*D" algos
1363 // RemoveSubMeshElementsAndNodes();
1365 _computeState = FAILED_TO_COMPUTE;
1366 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1368 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1371 algo->InitComputeError();
1372 MemoryReserve aMemoryReserve;
1373 SMDS_Mesh::CheckMemory();
1374 Kernel_Utils::Localizer loc;
1375 if ( !_father->HasShapeToMesh() ) // no shape
1377 SMESH_MesherHelper helper( *_father );
1378 helper.SetSubShape( shape );
1379 helper.SetElementsOnShape( true );
1380 ret = algo->Compute(*_father, &helper );
1384 ret = algo->Compute((*_father), shape);
1386 if ( !_computeError || ( !ret && _computeError->IsOK() ) ) // algo can set _computeError of submesh
1387 _computeError = algo->GetComputeError();
1389 catch ( std::bad_alloc& exc ) {
1390 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1391 if ( _computeError ) {
1392 _computeError->myName = COMPERR_MEMORY_PB;
1393 //_computeError->myComment = exc.what();
1395 cleanSubMesh( this );
1398 catch ( Standard_OutOfMemory& exc ) {
1399 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1400 if ( _computeError ) {
1401 _computeError->myName = COMPERR_MEMORY_PB;
1402 //_computeError->myComment = exc.what();
1404 cleanSubMesh( this );
1405 throw std::bad_alloc();
1407 catch (Standard_Failure& ex) {
1408 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1409 _computeError->myName = COMPERR_OCC_EXCEPTION;
1410 _computeError->myComment += ex.DynamicType()->Name();
1411 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1412 _computeError->myComment += ": ";
1413 _computeError->myComment += ex.GetMessageString();
1416 catch ( SALOME_Exception& S_ex ) {
1417 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1418 _computeError->myName = COMPERR_SLM_EXCEPTION;
1419 _computeError->myComment = S_ex.what();
1421 catch ( std::exception& exc ) {
1422 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1423 _computeError->myName = COMPERR_STD_EXCEPTION;
1424 _computeError->myComment = exc.what();
1427 if ( _computeError )
1428 _computeError->myName = COMPERR_EXCEPTION;
1432 if (ret && !_alwaysComputed && shape == _subShape) { // check if anything was built
1433 ret = ( GetSubMeshDS() && ( GetSubMeshDS()->NbElements() || GetSubMeshDS()->NbNodes() ));
1435 bool isComputeErrorSet = !CheckComputeError( algo, shape );
1436 if (!ret && !isComputeErrorSet)
1438 // Set _computeError
1439 if ( !_computeError )
1440 _computeError = SMESH_ComputeError::New();
1441 if ( _computeError->IsOK() )
1442 _computeError->myName = COMPERR_ALGO_FAILED;
1443 _computeState = FAILED_TO_COMPUTE;
1447 _computeError.reset();
1449 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1454 RemoveSubMeshElementsAndNodes();
1455 _computeState = NOT_READY;
1456 algo = gen->GetAlgo((*_father), _subShape);
1459 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1461 _computeState = READY_TO_COMPUTE;
1463 SetAlgoState(MISSING_HYP);
1466 case SUBMESH_COMPUTED: // nothing to do
1468 case SUBMESH_RESTORED:
1469 // check if a mesh is already computed that may
1470 // happen after retrieval from a file
1471 ComputeStateEngine( CHECK_COMPUTE_STATE );
1472 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1473 algo = gen->GetAlgo(*_father, _subShape);
1474 if (algo) algo->SubmeshRestored( this );
1476 case MESH_ENTITY_REMOVED:
1478 case CHECK_COMPUTE_STATE:
1479 if ( IsMeshComputed() )
1480 _computeState = COMPUTE_OK;
1488 // ----------------------------------------------------------------------
1493 case MODIF_ALGO_STATE:
1494 ComputeStateEngine( CLEAN );
1495 algo = gen->GetAlgo((*_father), _subShape);
1496 if (algo && !algo->NeedDescretBoundary())
1497 CleanDependsOn(); // clean sub-meshes with event CLEAN
1499 case COMPUTE: // nothing to do
1502 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1503 RemoveSubMeshElementsAndNodes();
1504 _computeState = NOT_READY;
1505 if ( _algoState == HYP_OK )
1506 _computeState = READY_TO_COMPUTE;
1508 case SUBMESH_COMPUTED: // nothing to do
1510 case SUBMESH_RESTORED:
1511 ComputeStateEngine( CHECK_COMPUTE_STATE );
1512 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1513 algo = gen->GetAlgo(*_father, _subShape);
1514 if (algo) algo->SubmeshRestored( this );
1516 case MESH_ENTITY_REMOVED:
1517 UpdateDependantsState( CHECK_COMPUTE_STATE );
1518 ComputeStateEngine( CHECK_COMPUTE_STATE );
1519 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1521 case CHECK_COMPUTE_STATE:
1522 if ( !IsMeshComputed() )
1523 if (_algoState == HYP_OK)
1524 _computeState = READY_TO_COMPUTE;
1526 _computeState = NOT_READY;
1534 // ----------------------------------------------------------------------
1536 case FAILED_TO_COMPUTE:
1539 case MODIF_ALGO_STATE:
1540 algo = gen->GetAlgo((*_father), _subShape);
1541 if (algo && !algo->NeedDescretBoundary())
1542 CleanDependsOn(); // clean sub-meshes with event CLEAN
1543 if (_algoState == HYP_OK)
1544 _computeState = READY_TO_COMPUTE;
1546 _computeState = NOT_READY;
1548 case COMPUTE: // nothing to do
1551 CleanDependants(); // submeshes dependent on me should be cleaned as well
1552 RemoveSubMeshElementsAndNodes();
1554 case SUBMESH_COMPUTED: // allow retry compute
1555 if (_algoState == HYP_OK)
1556 _computeState = READY_TO_COMPUTE;
1558 _computeState = NOT_READY;
1560 case SUBMESH_RESTORED:
1561 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1563 case MESH_ENTITY_REMOVED:
1565 case CHECK_COMPUTE_STATE:
1566 if ( IsMeshComputed() )
1567 _computeState = COMPUTE_OK;
1569 if (_algoState == HYP_OK)
1570 _computeState = READY_TO_COMPUTE;
1572 _computeState = NOT_READY;
1580 // ----------------------------------------------------------------------
1586 NotifyListenersOnEvent( event, COMPUTE_EVENT );
1592 //=============================================================================
1596 //=============================================================================
1598 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1600 _computeError.reset();
1604 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1605 vector<int> aVec(SMDSEntity_Last,0);
1606 aVec[SMDSEntity_Node] = 1;
1607 aResMap.insert(make_pair(this,aVec));
1611 SMESH_Gen *gen = _father->GetGen();
1612 SMESH_Algo *algo = 0;
1613 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1615 algo = gen->GetAlgo((*_father), _subShape);
1616 if(algo && !aResMap.count(this) )
1618 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1619 if (!ret) return false;
1621 if (_father->HasShapeToMesh() && algo->NeedDescretBoundary())
1623 // check submeshes needed
1624 bool subMeshEvaluated = true;
1625 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1626 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1627 while ( smIt->more() && subMeshEvaluated )
1629 SMESH_subMesh* sm = smIt->next();
1630 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1631 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1632 const vector<int> & nbs = aResMap[ sm ];
1633 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1635 if ( !subMeshEvaluated )
1638 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1639 ret = algo->Evaluate((*_father), _subShape, aResMap);
1641 aResMap.insert( make_pair( this,vector<int>(0)));
1648 //=======================================================================
1650 * \brief Update compute_state by _computeError and send proper events to
1651 * dependent submeshes
1652 * \retval bool - true if _computeError is NOT set
1654 //=======================================================================
1656 bool SMESH_subMesh::CheckComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1658 bool noErrors = true;
1660 if ( !theShape.IsNull() )
1662 // Check state of submeshes
1663 if ( !theAlgo->NeedDescretBoundary())
1665 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1666 while ( smIt->more() )
1667 if ( !smIt->next()->CheckComputeError( theAlgo ))
1671 // Check state of neighbours
1672 if ( !theAlgo->OnlyUnaryInput() &&
1673 theShape.ShapeType() == TopAbs_COMPOUND &&
1674 !theShape.IsSame( _subShape ))
1676 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1677 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1679 if ( !sm->CheckComputeError( theAlgo, sm->GetSubShape() ))
1681 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1688 if ( !_computeError || _computeError->IsOK() )
1690 _computeState = COMPUTE_OK;
1694 if ( !_computeError->myAlgo )
1695 _computeError->myAlgo = theAlgo;
1699 text << theAlgo->GetName() << " failed on subshape #" << _Id << " with error ";
1700 if (_computeError->IsCommon() )
1701 text << _computeError->CommonName();
1703 text << _computeError->myName;
1704 if ( _computeError->myComment.size() > 0 )
1705 text << " \"" << _computeError->myComment << "\"";
1708 MESSAGE_BEGIN ( text );
1709 // Show vertices location of a failed shape
1710 TopTools_IndexedMapOfShape vMap;
1711 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1712 MESSAGE_ADD ( "Subshape vertices " << ( vMap.Extent()>10 ? "(first 10):" : ":") );
1713 for ( int iv = 1; iv <= vMap.Extent() && iv < 11; ++iv ) {
1714 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1715 MESSAGE_ADD ( "#" << _father->GetMeshDS()->ShapeToIndex( vMap( iv )) << " "
1716 << P.X() << " " << P.Y() << " " << P.Z() << " " );
1721 _computeState = FAILED_TO_COMPUTE;
1728 //=======================================================================
1729 //function : ApplyToCollection
1730 //purpose : Apply theAlgo to all subshapes in theCollection
1731 //=======================================================================
1733 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1734 const TopoDS_Shape& theCollection)
1736 MESSAGE("SMESH_subMesh::ApplyToCollection");
1737 ASSERT ( !theAlgo->NeedDescretBoundary() );
1739 if ( _computeError )
1740 _computeError->myName = COMPERR_OK;
1742 bool ok = theAlgo->Compute( *_father, theCollection );
1744 // set _computeState of subshapes
1745 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1746 for ( ; anExplorer.More(); anExplorer.Next() )
1748 if ( SMESH_subMesh* subMesh = _father->GetSubMeshContaining( anExplorer.Current() ))
1750 bool localOK = subMesh->CheckComputeError( theAlgo );
1751 if ( !ok && localOK && !subMesh->IsMeshComputed() )
1753 subMesh->_computeError = theAlgo->GetComputeError();
1754 if ( subMesh->_computeError->IsOK() )
1755 _computeError = SMESH_ComputeError::New(COMPERR_ALGO_FAILED);
1756 localOK = CheckComputeError( theAlgo );
1759 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1760 subMesh->UpdateSubMeshState( localOK ? COMPUTE_OK : FAILED_TO_COMPUTE );
1768 //=======================================================================
1769 //function : UpdateSubMeshState
1771 //=======================================================================
1773 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1775 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1776 while ( smIt->more() )
1777 smIt->next()->_computeState = theState;
1780 //=======================================================================
1781 //function : ComputeSubMeshStateEngine
1783 //=======================================================================
1785 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1787 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1788 while ( smIt->more() )
1789 smIt->next()->ComputeStateEngine(event);
1792 //=======================================================================
1793 //function : UpdateDependantsState
1795 //=======================================================================
1797 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1799 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1800 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1801 for (; it.More(); it.Next())
1803 const TopoDS_Shape& ancestor = it.Value();
1804 SMESH_subMesh *aSubMesh =
1805 _father->GetSubMeshContaining(ancestor);
1807 aSubMesh->ComputeStateEngine( theEvent );
1811 //=============================================================================
1815 //=============================================================================
1817 void SMESH_subMesh::CleanDependants()
1819 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1821 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1822 for (; it.More(); it.Next())
1824 const TopoDS_Shape& ancestor = it.Value();
1825 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1826 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1827 // will erase mesh on other shapes in a compound
1828 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1829 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1831 aSubMesh->ComputeStateEngine(CLEAN);
1837 //=============================================================================
1841 //=============================================================================
1843 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1845 //SCRUTE(_subShape.ShapeType());
1847 cleanSubMesh( this );
1849 // algo may bind a submesh not to _subShape, eg 3D algo
1850 // sets nodes on SHELL while _subShape may be SOLID
1852 int dim = SMESH_Gen::GetShapeDim( _subShape );
1853 int type = _subShape.ShapeType() + 1;
1854 for ( ; type <= TopAbs_EDGE; type++) {
1855 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1857 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1858 for ( ; exp.More(); exp.Next() )
1859 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1866 //=======================================================================
1867 //function : GetCollection
1868 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1869 // meshed at once along with _subShape
1870 //=======================================================================
1872 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen,
1873 SMESH_Algo* theAlgo,
1874 bool & theSubComputed)
1876 MESSAGE("SMESH_subMesh::GetCollection");
1878 theSubComputed = SubMeshesComputed();
1880 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1882 if ( mainShape.IsSame( _subShape ))
1885 const bool ignoreAuxiliaryHyps = false;
1886 list<const SMESHDS_Hypothesis*> aUsedHyp =
1887 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1889 // put in a compound all shapes with the same hypothesis assigned
1890 // and a good ComputState
1892 TopoDS_Compound aCompound;
1893 BRep_Builder aBuilder;
1894 aBuilder.MakeCompound( aCompound );
1896 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1897 for ( ; anExplorer.More(); anExplorer.Next() )
1899 const TopoDS_Shape& S = anExplorer.Current();
1900 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1901 if ( subMesh == this )
1903 aBuilder.Add( aCompound, S );
1905 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1907 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1908 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1909 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1910 aBuilder.Add( aCompound, S );
1911 if ( !subMesh->SubMeshesComputed() )
1912 theSubComputed = false;
1919 //=======================================================================
1920 //function : GetSimilarAttached
1921 //purpose : return a hypothesis attached to theShape.
1922 // If theHyp is provided, similar but not same hypotheses
1923 // is returned; else only applicable ones having theHypType
1925 //=======================================================================
1927 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1928 const SMESH_Hypothesis * theHyp,
1929 const int theHypType)
1931 SMESH_HypoFilter hypoKind;
1932 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1934 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1935 hypoKind.AndNot( hypoKind.Is( theHyp ));
1936 if ( theHyp->IsAuxiliary() )
1937 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1939 hypoKind.AndNot( hypoKind.IsAuxiliary());
1942 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1945 return _father->GetHypothesis( theShape, hypoKind, false );
1948 //=======================================================================
1949 //function : CheckConcurentHypothesis
1950 //purpose : check if there are several applicable hypothesis attached to
1952 //=======================================================================
1954 SMESH_Hypothesis::Hypothesis_Status
1955 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1957 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1959 // is there local hypothesis on me?
1960 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1961 return SMESH_Hypothesis::HYP_OK;
1964 TopoDS_Shape aPrevWithHyp;
1965 const SMESH_Hypothesis* aPrevHyp = 0;
1966 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1967 for (; it.More(); it.Next())
1969 const TopoDS_Shape& ancestor = it.Value();
1970 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1973 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1975 aPrevWithHyp = ancestor;
1978 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1979 return SMESH_Hypothesis::HYP_CONCURENT;
1981 return SMESH_Hypothesis::HYP_OK;
1984 return SMESH_Hypothesis::HYP_OK;
1987 //================================================================================
1989 * \brief Sets an event listener and its data to a submesh
1990 * \param listener - the listener to store
1991 * \param data - the listener data to store
1992 * \param where - the submesh to store the listener and it's data
1993 * \param deleteListener - if true then the listener will be deleted as
1994 * it is removed from where submesh
1996 * It remembers the submesh where it puts the listener in order to delete
1997 * them when HYP_OK algo_state is lost
1998 * After being set, event listener is notified on each event of where submesh.
2000 //================================================================================
2002 void SMESH_subMesh::SetEventListener(EventListener* listener,
2003 EventListenerData* data,
2004 SMESH_subMesh* where)
2006 if ( listener && where ) {
2007 where->SetEventListener( listener, data );
2008 myOwnListeners.push_back( make_pair( where, listener ));
2012 //================================================================================
2014 * \brief Sets an event listener and its data to a submesh
2015 * \param listener - the listener to store
2016 * \param data - the listener data to store
2018 * After being set, event listener is notified on each event of a submesh.
2020 //================================================================================
2022 void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
2024 map< EventListener*, EventListenerData* >::iterator l_d =
2025 myEventListeners.find( listener );
2026 if ( l_d != myEventListeners.end() ) {
2027 EventListenerData* curData = l_d->second;
2028 if ( curData && curData != data && curData->IsDeletable() )
2033 myEventListeners.insert( make_pair( listener, data ));
2036 //================================================================================
2038 * \brief Return an event listener data
2039 * \param listener - the listener whose data is
2040 * \retval EventListenerData* - found data, maybe NULL
2042 //================================================================================
2044 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
2046 map< EventListener*, EventListenerData* >::const_iterator l_d =
2047 myEventListeners.find( listener );
2048 if ( l_d != myEventListeners.end() )
2053 //================================================================================
2055 * \brief Notify stored event listeners on the occured event
2056 * \param event - algo_event or compute_event itself
2057 * \param eventType - algo_event or compute_event
2058 * \param subMesh - the submesh where the event occures
2059 * \param data - listener data stored in the subMesh
2060 * \param hyp - hypothesis, if eventType is algo_event
2062 //================================================================================
2064 void SMESH_subMesh::NotifyListenersOnEvent( const int event,
2065 const event_type eventType,
2066 SMESH_Hypothesis* hyp)
2068 map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
2069 for ( ; l_d != myEventListeners.end(); ++l_d )
2070 l_d->first->ProcessEvent( event, eventType, this, l_d->second, hyp );
2073 //================================================================================
2075 * \brief Unregister the listener and delete listener's data
2076 * \param listener - the event listener
2078 //================================================================================
2080 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2082 map< EventListener*, EventListenerData* >::iterator l_d =
2083 myEventListeners.find( listener );
2084 if ( l_d != myEventListeners.end() ) {
2085 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2086 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2087 myEventListeners.erase( l_d );
2091 //================================================================================
2093 * \brief Delete event listeners depending on algo of this submesh
2095 //================================================================================
2097 void SMESH_subMesh::DeleteOwnListeners()
2099 list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
2100 for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
2101 sm_l->first->DeleteEventListener( sm_l->second );
2102 myOwnListeners.clear();
2105 //================================================================================
2107 * \brief Do something on a certain event
2108 * \param event - algo_event or compute_event itself
2109 * \param eventType - algo_event or compute_event
2110 * \param subMesh - the submesh where the event occures
2111 * \param data - listener data stored in the subMesh
2112 * \param hyp - hypothesis, if eventType is algo_event
2114 * The base implementation translates CLEAN event to the subMesh
2115 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2116 * successful COMPUTE event.
2118 //================================================================================
2120 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2121 const int eventType,
2122 SMESH_subMesh* subMesh,
2123 EventListenerData* data,
2124 const SMESH_Hypothesis* /*hyp*/)
2126 if ( data && !data->mySubMeshes.empty() &&
2127 eventType == SMESH_subMesh::COMPUTE_EVENT)
2129 ASSERT( data->mySubMeshes.front() != subMesh );
2130 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2131 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2133 case SMESH_subMesh::CLEAN:
2134 for ( ; smIt != smEnd; ++ smIt)
2135 (*smIt)->ComputeStateEngine( event );
2137 case SMESH_subMesh::COMPUTE:
2138 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2139 for ( ; smIt != smEnd; ++ smIt)
2140 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2149 //================================================================================
2151 * \brief Iterator over submeshes and optionally prepended or appended one
2153 //================================================================================
2155 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2157 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2158 SMESH_subMesh* prepend,
2159 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2161 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2162 if ( myCur == append ) append = 0;
2164 /// Return true if and only if there are other object in this iterator
2169 /// Return the current object and step to the next one
2170 virtual SMESH_subMesh* next()
2172 SMESH_subMesh* res = myCur;
2173 if ( myIt->more() ) { myCur = myIt->next(); }
2174 else { myCur = myAppend; myAppend = 0; }
2181 SMESH_subMesh *myAppend, *myCur;
2182 SMDS_Iterator<SMESH_subMesh*> *myIt;
2186 //================================================================================
2188 * \brief Return iterator on the submeshes this one depends on
2189 * \param includeSelf - this submesh to be returned also
2190 * \param reverse - if true, complex shape submeshes go first
2192 //================================================================================
2194 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2197 SMESH_subMesh *prepend=0, *append=0;
2198 if ( includeSelf ) {
2199 if ( reverse ) prepend = this;
2202 typedef map < int, SMESH_subMesh * > TMap;
2205 return SMESH_subMeshIteratorPtr
2206 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2209 return SMESH_subMeshIteratorPtr
2210 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));
2214 //================================================================================
2216 * \brief Find common submeshes (based on shared subshapes with other
2217 * \param theOther submesh to check
2218 * \param theSetOfCommon set of common submesh
2220 //================================================================================
2222 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2223 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2225 int oldNb = theSetOfCommon.size();
2226 // check main submeshes
2227 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2228 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2229 theSetOfCommon.insert( this );
2230 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2231 theSetOfCommon.insert( theOther );
2232 // check common submeshes
2233 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2234 for( ; mapIt != _mapDepend.end(); mapIt++ )
2235 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2236 theSetOfCommon.insert( (*mapIt).second );
2237 return oldNb < theSetOfCommon.size();