1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_subMesh.hxx"
30 #include "SMESH_Algo.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_HypoFilter.hxx"
33 #include "SMESH_Hypothesis.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "SMESH_MesherHelper.hxx"
36 #include "SMESH_subMeshEventListener.hxx"
37 #include "SMESH_Comment.hxx"
38 #include "SMDS_SetIterator.hxx"
39 #include "SMDSAbs_ElementType.hxx"
41 #include <Basics_OCCTVersion.hxx>
43 #include "utilities.h"
45 #include "Basics_Utils.hxx"
47 #include <BRep_Builder.hxx>
48 #include <BRep_Tool.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <TopoDS_Compound.hxx>
55 #include <TopExp_Explorer.hxx>
56 #include <TopoDS_Iterator.hxx>
58 #include <Standard_OutOfMemory.hxx>
59 #include <Standard_ErrorHandler.hxx>
65 //=============================================================================
67 * \brief Allocate some memory at construction and release it at destruction.
68 * Is used to be able to continue working after mesh generation breaks due to
71 //=============================================================================
76 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
77 ~MemoryReserve() { delete [] myBuf; }
80 //=============================================================================
82 * default constructor:
84 //=============================================================================
86 SMESH_subMesh::SMESH_subMesh(int Id,
88 SMESHDS_Mesh * meshDS,
89 const TopoDS_Shape & aSubShape)
91 _subShape = aSubShape;
92 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
95 _dependenceAnalysed = _alwaysComputed = false;
97 if (_subShape.ShapeType() == TopAbs_VERTEX)
100 _computeState = READY_TO_COMPUTE;
104 _algoState = NO_ALGO;
105 _computeState = NOT_READY;
109 //=============================================================================
113 //=============================================================================
115 SMESH_subMesh::~SMESH_subMesh()
117 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
119 deleteOwnListeners();
122 //=============================================================================
126 //=============================================================================
128 int SMESH_subMesh::GetId() const
130 //MESSAGE("SMESH_subMesh::GetId");
134 //=============================================================================
138 //=============================================================================
140 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
142 // submesh appears in DS only when a mesher set nodes and elements on a shape
143 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
146 //=============================================================================
150 //=============================================================================
152 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
154 if ( !GetSubMeshDS() ) {
155 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
156 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
158 return GetSubMeshDS();
161 //=============================================================================
165 //=============================================================================
167 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
169 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
170 while ( smIt->more() ) {
171 SMESH_subMesh *sm = smIt->next();
172 if ( sm->GetComputeState() == READY_TO_COMPUTE )
175 return 0; // nothing to compute
178 //================================================================================
180 * \brief Returns a current algorithm
182 //================================================================================
184 SMESH_Algo* SMESH_subMesh::GetAlgo() const
186 return _father->GetGen()->GetAlgo(*_father, _subShape);
189 //================================================================================
191 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
192 * none mesh entity is bound to it (PAL13615, 2nd part)
194 //================================================================================
196 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
198 _alwaysComputed = isAlCo;
199 if ( _alwaysComputed )
200 _computeState = COMPUTE_OK;
202 ComputeStateEngine( CHECK_COMPUTE_STATE );
205 //=======================================================================
207 * \brief Return true if no mesh entities is bound to the submesh
209 //=======================================================================
211 bool SMESH_subMesh::IsEmpty() const
213 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
214 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
218 //=======================================================================
219 //function : IsMeshComputed
220 //purpose : check if _subMeshDS contains mesh elements
221 //=======================================================================
223 bool SMESH_subMesh::IsMeshComputed() const
225 if ( _alwaysComputed )
227 // algo may bind a submesh not to _subShape, eg 3D algo
228 // sets nodes on SHELL while _subShape may be SOLID
230 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
231 int dim = SMESH_Gen::GetShapeDim( _subShape );
232 int type = _subShape.ShapeType();
233 for ( ; type <= TopAbs_VERTEX; type++) {
234 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
236 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
237 for ( ; exp.More(); exp.Next() )
239 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
241 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
254 //=============================================================================
258 //=============================================================================
260 bool SMESH_subMesh::subMeshesComputed()
262 int myDim = SMESH_Gen::GetShapeDim( _subShape );
263 int dimToCheck = myDim - 1;
264 bool subMeshesComputed = true;
265 // check subMeshes with upper dimension => reverse iteration
266 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
267 while ( smIt->more() )
269 SMESH_subMesh *sm = smIt->next();
270 if ( sm->_alwaysComputed )
272 const TopoDS_Shape & ss = sm->GetSubShape();
273 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
274 // in checking of existence of edges if the algo needs only faces. Moreover,
275 // degenerated edges may have no submesh, as after computing NETGEN_2D.
276 int dim = SMESH_Gen::GetShapeDim( ss );
277 if (dim < dimToCheck)
278 break; // the rest subMeshes are all of less dimension
279 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
280 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
281 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
284 int type = ss.ShapeType();
286 subMeshesComputed = false;
290 case TopAbs_COMPOUND:
292 MESSAGE("The not computed sub mesh is a COMPOUND");
295 case TopAbs_COMPSOLID:
297 MESSAGE("The not computed sub mesh is a COMPSOLID");
302 MESSAGE("The not computed sub mesh is a SHEL");
307 MESSAGE("The not computed sub mesh is a WIRE");
312 MESSAGE("The not computed sub mesh is a SOLID");
317 MESSAGE("The not computed sub mesh is a FACE");
322 MESSAGE("The not computed sub mesh is a EDGE");
327 MESSAGE("The not computed sub mesh is of unknown type");
335 return subMeshesComputed;
338 //=============================================================================
342 //=============================================================================
344 // bool SMESH_subMesh::SubMeshesReady()
346 // bool subMeshesReady = true;
347 // SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
348 // while ( smIt->more() ) {
349 // SMESH_subMesh *sm = smIt->next();
350 // bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
351 // sm->GetComputeState() == READY_TO_COMPUTE);
354 // subMeshesReady = false;
355 // SCRUTE(sm->GetId());
359 // return subMeshesReady;
362 //=============================================================================
364 * Construct dependence on first level subMeshes. complex shapes (compsolid,
365 * shell, wire) are not analysed the same way as simple shapes (solid, face,
367 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
368 * with possible multiples occurences. Multiples occurences corresponds to
369 * internal frontiers within shapes of the collection and must not be keeped.
370 * See FinalizeDependence.
372 //=============================================================================
374 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
376 if (_dependenceAnalysed)
379 //MESSAGE("SMESH_subMesh::DependsOn");
381 int type = _subShape.ShapeType();
385 case TopAbs_COMPOUND:
387 //MESSAGE("compound");
388 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();exp.Next())
390 insertDependence(exp.Current());
392 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More(); exp.Next())
394 if ( BRep_Tool::IsClosed(exp.Current() ))
395 insertDependence(exp.Current()); //only shell not in solid
397 for (TopExp_Explorer expF(exp.Current(), TopAbs_FACE); expF.More();expF.Next())
398 insertDependence(expF.Current()); // issue 0020959: HEXA_3D fails on shell
401 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();exp.Next())
403 insertDependence(exp.Current());
405 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();exp.Next())
407 insertDependence(exp.Current());
409 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX, TopAbs_EDGE); exp.More();exp.Next())
411 insertDependence(exp.Current());
415 case TopAbs_COMPSOLID:
417 //MESSAGE("compsolid");
418 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More(); exp.Next())
420 insertDependence(exp.Current());
427 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More(); exp.Next())
429 insertDependence(exp.Current());
436 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More(); exp.Next())
438 insertDependence(exp.Current());
445 if(_father->HasShapeToMesh()) {
446 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();exp.Next())
448 insertDependence(exp.Current());
456 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();exp.Next())
458 insertDependence(exp.Current());
465 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More(); exp.Next())
467 insertDependence(exp.Current());
480 _dependenceAnalysed = true;
484 //=============================================================================
486 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
488 //=============================================================================
490 void SMESH_subMesh::insertDependence(const TopoDS_Shape aSubShape)
492 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
493 int type = aSubShape.ShapeType();
494 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
495 int cle = aSubMesh->GetId();
496 cle += 10000000 * ordType; // sort map by ordType then index
497 if ( _mapDepend.find( cle ) == _mapDepend.end())
499 _mapDepend[cle] = aSubMesh;
500 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
501 _mapDepend.insert( subMap.begin(), subMap.end() );
505 //=============================================================================
509 //=============================================================================
511 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
513 //MESSAGE("SMESH_subMesh::GetSubShape");
518 //=======================================================================
519 //function : CanAddHypothesis
520 //purpose : return true if theHypothesis can be attached to me:
521 // its dimention is checked
522 //=======================================================================
524 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
526 int aHypDim = theHypothesis->GetDim();
527 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
528 // issue 21106. Forbid 3D mesh on the SHELL
529 // if (aHypDim == 3 && aShapeDim == 3) {
530 // // check case of open shell
531 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
532 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
535 if ( aHypDim <= aShapeDim )
541 //=======================================================================
542 //function : IsApplicableHypotesis
544 //=======================================================================
546 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
547 const TopAbs_ShapeEnum theShapeType)
549 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
552 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
553 // issue 21106. Forbid 3D mesh on the SHELL
554 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
560 switch ( theShapeType ) {
565 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
568 // Special case for algorithms, building 2D mesh on a whole shell.
569 // Before this fix there was a problem after restoring from study,
570 // because in that case algorithm is assigned before hypothesis
571 // (on shell in problem case) and hypothesis is checked on faces
572 // (because it is 2D), where we have NO_ALGO state.
573 // Now 2D hypothesis is also applicable to shells.
574 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
577 // case TopAbs_COMPSOLID:
578 // case TopAbs_COMPOUND:
584 //=============================================================================
588 //=============================================================================
590 SMESH_Hypothesis::Hypothesis_Status
591 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
593 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
594 //SCRUTE(_algoState);
597 // **** les retour des evenement shape sont significatifs
598 // (add ou remove fait ou non)
599 // le retour des evenement father n'indiquent pas que add ou remove fait
601 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
603 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
604 //SMESH_Gen* gen =_father->GetGen();
605 SMESH_Algo* algo = 0;
607 if (_subShape.ShapeType() == TopAbs_VERTEX )
609 if ( anHyp->GetDim() != 0) {
610 if (event == ADD_HYP || event == ADD_ALGO)
611 return SMESH_Hypothesis::HYP_BAD_DIM;
613 return SMESH_Hypothesis::HYP_OK;
616 else if ( _algoState == HYP_OK ) {
617 // update default _algoState
618 if ( event != REMOVE_FATHER_ALGO )
620 _algoState = NO_ALGO;
623 _algoState = MISSING_HYP;
624 if ( event == REMOVE_FATHER_HYP ||
625 algo->CheckHypothesis(*_father,_subShape, aux_ret))
632 int oldAlgoState = _algoState;
633 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
634 bool needFullClean = false;
636 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
638 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
640 // -------------------------------------------
641 // check if a shape needed by algo is present
642 // -------------------------------------------
643 algo = static_cast< SMESH_Algo* >( anHyp );
644 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
645 return SMESH_Hypothesis::HYP_NEED_SHAPE;
646 // ----------------------
647 // check mesh conformity
648 // ----------------------
649 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
650 return SMESH_Hypothesis::HYP_NOTCONFORM;
652 // check if all-dimensional algo is hidden by other local one
653 if ( event == ADD_ALGO ) {
654 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
655 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
656 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
657 if ( SMESH_Algo * curAlgo = (SMESH_Algo*) _father->GetHypothesis( _subShape, filter, true ))
658 needFullClean = ( !curAlgo->NeedDiscreteBoundary() );
662 // ----------------------------------
663 // add a hypothesis to DS if possible
664 // ----------------------------------
665 if (event == ADD_HYP || event == ADD_ALGO)
667 if ( ! CanAddHypothesis( anHyp )) // check dimension
668 return SMESH_Hypothesis::HYP_BAD_DIM;
670 if ( /*!anHyp->IsAuxiliary() &&*/ getSimilarAttached( _subShape, anHyp ) )
671 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
673 if ( !meshDS->AddHypothesis(_subShape, anHyp))
674 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
677 // --------------------------
678 // remove a hypothesis from DS
679 // --------------------------
680 if (event == REMOVE_HYP || event == REMOVE_ALGO)
682 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
683 return SMESH_Hypothesis::HYP_OK; // nothing changes
685 if (event == REMOVE_ALGO)
687 algo = dynamic_cast<SMESH_Algo*> (anHyp);
688 if (!algo->NeedDiscreteBoundary())
690 // clean all mesh in the tree of the current submesh;
691 // we must perform it now because later
692 // we will have no information about the type of the removed algo
693 needFullClean = true;
698 // ------------------
699 // analyse algo state
700 // ------------------
701 if (!isApplicableHyp)
702 return ret; // not applicable hypotheses do not change algo state
707 // ----------------------------------------------------------------------
716 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
717 setAlgoState(HYP_OK);
718 else if ( algo->IsStatusFatal( aux_ret )) {
719 meshDS->RemoveHypothesis(_subShape, anHyp);
723 setAlgoState(MISSING_HYP);
730 case ADD_FATHER_ALGO: { // Algo just added in father
733 if ( algo == anHyp ) {
734 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
735 setAlgoState(HYP_OK);
737 setAlgoState(MISSING_HYP);
741 case REMOVE_FATHER_HYP:
743 case REMOVE_FATHER_ALGO: {
747 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
748 setAlgoState(HYP_OK);
750 setAlgoState(MISSING_HYP);
754 case MODIF_HYP: break;
761 // ----------------------------------------------------------------------
769 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
770 setAlgoState(HYP_OK);
771 if (SMESH_Hypothesis::IsStatusFatal( ret ))
772 meshDS->RemoveHypothesis(_subShape, anHyp);
773 else if (!_father->IsUsedHypothesis( anHyp, this ))
775 meshDS->RemoveHypothesis(_subShape, anHyp);
776 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
780 case ADD_ALGO: { //already existing algo : on father ?
783 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
784 setAlgoState(HYP_OK);
785 else if ( algo->IsStatusFatal( aux_ret )) {
786 meshDS->RemoveHypothesis(_subShape, anHyp);
790 setAlgoState(MISSING_HYP);
795 case REMOVE_ALGO: { // perhaps a father algo applies ?
797 if (algo == NULL) // no more algo applying on sub-shape...
799 setAlgoState(NO_ALGO);
803 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
804 setAlgoState(HYP_OK);
806 setAlgoState(MISSING_HYP);
810 case MODIF_HYP: // assigned hypothesis value may become good
811 case ADD_FATHER_HYP: {
814 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
815 setAlgoState(HYP_OK);
817 setAlgoState(MISSING_HYP);
820 case ADD_FATHER_ALGO: { // new father algo
823 if ( algo == anHyp ) {
824 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
825 setAlgoState(HYP_OK);
827 setAlgoState(MISSING_HYP);
831 case REMOVE_FATHER_HYP: // nothing to do
833 case REMOVE_FATHER_ALGO: {
835 if (algo == NULL) // no more applying algo on father
837 setAlgoState(NO_ALGO);
841 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
842 setAlgoState(HYP_OK);
844 setAlgoState(MISSING_HYP);
854 // ----------------------------------------------------------------------
862 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
864 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
865 // ret should be fatal: anHyp was not added
866 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
868 else if (!_father->IsUsedHypothesis( anHyp, this ))
869 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
871 if (SMESH_Hypothesis::IsStatusFatal( ret ))
873 MESSAGE("do not add extra hypothesis");
874 meshDS->RemoveHypothesis(_subShape, anHyp);
882 case ADD_ALGO: { //already existing algo : on father ?
884 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
885 // check if algo changes
887 f.Init( SMESH_HypoFilter::IsAlgo() );
888 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
889 f.AndNot( SMESH_HypoFilter::Is( algo ));
890 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
892 string(algo->GetName()) != string(prevAlgo->GetName()) )
896 setAlgoState(MISSING_HYP);
902 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
903 setAlgoState(HYP_OK);
905 setAlgoState(MISSING_HYP);
909 case REMOVE_ALGO: { // perhaps a father algo applies ?
911 if (algo == NULL) // no more algo applying on sub-shape...
913 setAlgoState(NO_ALGO);
917 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
918 // check if algo remains
919 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
923 setAlgoState(MISSING_HYP);
927 case MODIF_HYP: // hypothesis value may become bad
928 case ADD_FATHER_HYP: { // new father hypothesis ?
931 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
933 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
937 setAlgoState(MISSING_HYP);
940 case ADD_FATHER_ALGO: {
942 if ( algo == anHyp ) { // a new algo on father
943 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
944 // check if algo changes
946 f.Init( SMESH_HypoFilter::IsAlgo() );
947 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
948 f.AndNot( SMESH_HypoFilter::Is( algo ));
949 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
951 string(algo->GetName()) != string(prevAlgo->GetName()) )
955 setAlgoState(MISSING_HYP);
959 case REMOVE_FATHER_HYP: {
962 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
963 // is there the same local hyp or maybe a new father algo applied?
964 if ( !getSimilarAttached( _subShape, anHyp ) )
968 setAlgoState(MISSING_HYP);
971 case REMOVE_FATHER_ALGO: {
972 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
973 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
974 algo = dynamic_cast<SMESH_Algo*> (anHyp);
975 if (!algo->NeedDiscreteBoundary())
976 needFullClean = true;
979 if (algo == NULL) // no more applying algo on father
981 setAlgoState(NO_ALGO);
985 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
986 // check if algo changes
987 if ( string(algo->GetName()) != string( anHyp->GetName()) )
991 setAlgoState(MISSING_HYP);
1001 // ----------------------------------------------------------------------
1008 // detect algorithm hiding
1010 if ( ret == SMESH_Hypothesis::HYP_OK &&
1011 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
1012 algo->GetName() == anHyp->GetName() )
1015 SMESH_Gen* gen = _father->GetGen();
1016 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1017 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
1018 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
1019 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1020 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1023 if ( ret == SMESH_Hypothesis::HYP_OK &&
1024 !algo->NeedDiscreteBoundary() &&
1025 !algo->SupportSubmeshes()) {
1026 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1027 gen->GetAlgo( *_father, _subShape, &algoAssignedTo );
1028 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1029 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1030 if ( gen->GetAlgo( *_father, i_sm->second->_subShape, &otherAssignedTo ) &&
1031 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1032 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1036 bool stateChange = ( _algoState != oldAlgoState );
1038 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1039 algo->SetEventListener( this );
1041 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1043 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1044 deleteOwnListeners();
1045 SetIsAlwaysComputed( false );
1046 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1047 // restore default states
1048 _algoState = HYP_OK;
1049 _computeState = READY_TO_COMPUTE;
1053 if ( needFullClean ) {
1054 // added or removed algo is all-dimensional
1055 ComputeStateEngine( CLEAN );
1057 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1060 if (stateChange || modifiedHyp)
1061 ComputeStateEngine(MODIF_ALGO_STATE);
1066 //=======================================================================
1067 //function : IsConform
1068 //purpose : check if a conform mesh will be produced by the Algo
1069 //=======================================================================
1071 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1073 // MESSAGE( "SMESH_subMesh::IsConform" );
1074 if ( !theAlgo ) return false;
1076 // Suppose that theAlgo is applicable to _subShape, do not check it here
1077 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1079 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1080 // on a sub-shape will be ignored by theAlgo
1081 if ( theAlgo->NeedDiscreteBoundary() ||
1082 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1085 SMESH_Gen* gen =_father->GetGen();
1087 // only local algo is to be checked
1088 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1089 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1092 // check algo attached to adjacent shapes
1094 // loop on one level down sub-meshes
1095 TopoDS_Iterator itsub( _subShape );
1096 for (; itsub.More(); itsub.Next())
1098 // loop on adjacent subShapes
1099 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1100 for (; it.More(); it.Next())
1102 const TopoDS_Shape& adjacent = it.Value();
1103 if ( _subShape.IsSame( adjacent )) continue;
1104 if ( adjacent.ShapeType() != _subShape.ShapeType())
1107 // check algo attached to smAdjacent
1108 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1110 !algo->NeedDiscreteBoundary() &&
1111 algo->OnlyUnaryInput())
1112 return false; // NOT CONFORM MESH WILL BE PRODUCED
1119 //=============================================================================
1123 //=============================================================================
1125 void SMESH_subMesh::setAlgoState(int state)
1130 //=============================================================================
1134 //=============================================================================
1135 SMESH_Hypothesis::Hypothesis_Status
1136 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1137 SMESH_Hypothesis * anHyp)
1139 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1140 //EAP: a wire (dim==1) should notify edges (dim==1)
1141 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1142 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1144 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1145 while ( smIt->more() ) {
1146 SMESH_Hypothesis::Hypothesis_Status ret2 =
1147 smIt->next()->AlgoStateEngine(event, anHyp);
1155 //=============================================================================
1159 //=============================================================================
1161 void SMESH_subMesh::cleanDependsOn()
1163 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1164 while ( smIt->more() )
1165 smIt->next()->ComputeStateEngine(CLEAN);
1168 //=============================================================================
1172 //=============================================================================
1174 void SMESH_subMesh::DumpAlgoState(bool isMain)
1176 int dim = SMESH_Gen::GetShapeDim(_subShape);
1177 // if (dim < 1) return;
1180 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1182 map < int, SMESH_subMesh * >::const_iterator itsub;
1183 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1185 SMESH_subMesh *sm = (*itsub).second;
1186 sm->DumpAlgoState(false);
1189 int type = _subShape.ShapeType();
1190 MESSAGE("dim = " << dim << " type of shape " << type);
1194 MESSAGE(" AlgoState = NO_ALGO");
1197 MESSAGE(" AlgoState = MISSING_HYP");
1200 MESSAGE(" AlgoState = HYP_OK");
1203 switch (_computeState)
1206 MESSAGE(" ComputeState = NOT_READY");
1208 case READY_TO_COMPUTE:
1209 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1212 MESSAGE(" ComputeState = COMPUTE_OK");
1214 case FAILED_TO_COMPUTE:
1215 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1220 //================================================================================
1222 * \brief Remove nodes and elements bound to submesh
1223 * \param subMesh - submesh containing nodes and elements
1225 //================================================================================
1227 static void cleanSubMesh( SMESH_subMesh * subMesh )
1230 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1231 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1232 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1233 while (ite->more()) {
1234 const SMDS_MeshElement * elt = ite->next();
1235 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1236 //meshDS->RemoveElement(elt);
1237 meshDS->RemoveFreeElement(elt, subMeshDS);
1240 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1241 while (itn->more()) {
1242 const SMDS_MeshNode * node = itn->next();
1243 //MESSAGE( " RM node: "<<node->GetID());
1244 if ( node->NbInverseElements() == 0 )
1245 meshDS->RemoveFreeNode(node, subMeshDS);
1246 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1247 meshDS->RemoveNode(node);
1253 //=============================================================================
1257 //=============================================================================
1259 bool SMESH_subMesh::ComputeStateEngine(int event)
1261 _computeError.reset();
1263 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1264 //SCRUTE(_computeState);
1267 if (_subShape.ShapeType() == TopAbs_VERTEX)
1269 _computeState = READY_TO_COMPUTE;
1270 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1271 if ( smDS && smDS->NbNodes() ) {
1272 if ( event == CLEAN ) {
1274 cleanSubMesh( this );
1277 _computeState = COMPUTE_OK;
1279 else if ( event == COMPUTE && !_alwaysComputed ) {
1280 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1281 gp_Pnt P = BRep_Tool::Pnt(V);
1282 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1283 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1284 _computeState = COMPUTE_OK;
1287 if ( event == MODIF_ALGO_STATE )
1291 SMESH_Gen *gen = _father->GetGen();
1292 SMESH_Algo *algo = 0;
1294 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1295 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1297 switch (_computeState)
1300 // ----------------------------------------------------------------------
1305 case MODIF_ALGO_STATE:
1307 if (algo && !algo->NeedDiscreteBoundary())
1308 cleanDependsOn(); // clean sub-meshes with event CLEAN
1309 if ( _algoState == HYP_OK )
1310 _computeState = READY_TO_COMPUTE;
1312 case COMPUTE: // nothing to do
1314 #ifdef WITH_SMESH_CANCEL_COMPUTE
1315 case COMPUTE_CANCELED: // nothing to do
1320 removeSubMeshElementsAndNodes();
1322 case SUBMESH_COMPUTED: // nothing to do
1324 case SUBMESH_RESTORED:
1325 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1327 case MESH_ENTITY_REMOVED:
1329 case SUBMESH_LOADED:
1330 loadDependentMeshes();
1331 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1333 case CHECK_COMPUTE_STATE:
1334 if ( IsMeshComputed() )
1335 _computeState = COMPUTE_OK;
1343 // ----------------------------------------------------------------------
1345 case READY_TO_COMPUTE:
1348 case MODIF_ALGO_STATE:
1349 _computeState = NOT_READY;
1353 if (!algo->NeedDiscreteBoundary())
1354 cleanDependsOn(); // clean sub-meshes with event CLEAN
1355 if ( _algoState == HYP_OK )
1356 _computeState = READY_TO_COMPUTE;
1363 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1366 MESSAGE("***** verify compute state *****");
1367 _computeState = NOT_READY;
1368 setAlgoState(MISSING_HYP);
1371 TopoDS_Shape shape = _subShape;
1372 // check submeshes needed
1373 if (_father->HasShapeToMesh() ) {
1374 bool subComputed = false;
1375 if (!algo->OnlyUnaryInput())
1376 shape = getCollection( gen, algo, subComputed );
1378 subComputed = subMeshesComputed();
1379 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1380 algo->SupportSubmeshes() ? true :
1381 ( !subComputed || _father->IsNotConformAllowed() ));
1383 _computeState = FAILED_TO_COMPUTE;
1384 if ( !algo->NeedDiscreteBoundary() )
1386 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1387 "Unexpected computed submesh",algo);
1393 //cleanDependants(); for "UseExisting_*D" algos
1394 //removeSubMeshElementsAndNodes();
1395 loadDependentMeshes();
1397 _computeState = FAILED_TO_COMPUTE;
1398 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1400 #if OCC_VERSION_LARGE > 0x06010000
1403 algo->InitComputeError();
1404 MemoryReserve aMemoryReserve;
1405 SMDS_Mesh::CheckMemory();
1406 Kernel_Utils::Localizer loc;
1407 if ( !_father->HasShapeToMesh() ) // no shape
1409 SMESH_MesherHelper helper( *_father );
1410 helper.SetSubShape( shape );
1411 helper.SetElementsOnShape( true );
1412 ret = algo->Compute(*_father, &helper );
1416 ret = algo->Compute((*_father), shape);
1418 if ( !_computeError || ( !ret && _computeError->IsOK() ) ) // algo can set _computeError of submesh
1419 _computeError = algo->GetComputeError();
1421 catch ( ::SMESH_ComputeError& comperr ) {
1422 cout << " SMESH_ComputeError caught" << endl;
1423 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1424 *_computeError = comperr;
1426 catch ( std::bad_alloc& exc ) {
1427 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1428 if ( _computeError ) {
1429 _computeError->myName = COMPERR_MEMORY_PB;
1430 //_computeError->myComment = exc.what();
1432 cleanSubMesh( this );
1435 catch ( Standard_OutOfMemory& exc ) {
1436 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1437 if ( _computeError ) {
1438 _computeError->myName = COMPERR_MEMORY_PB;
1439 //_computeError->myComment = exc.what();
1441 cleanSubMesh( this );
1442 throw std::bad_alloc();
1444 catch (Standard_Failure& ex) {
1445 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1446 _computeError->myName = COMPERR_OCC_EXCEPTION;
1447 _computeError->myComment += ex.DynamicType()->Name();
1448 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1449 _computeError->myComment += ": ";
1450 _computeError->myComment += ex.GetMessageString();
1453 catch ( SALOME_Exception& S_ex ) {
1454 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1455 _computeError->myName = COMPERR_SLM_EXCEPTION;
1456 _computeError->myComment = S_ex.what();
1458 catch ( std::exception& exc ) {
1459 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1460 _computeError->myName = COMPERR_STD_EXCEPTION;
1461 _computeError->myComment = exc.what();
1464 if ( _computeError )
1465 _computeError->myName = COMPERR_EXCEPTION;
1469 // check if an error reported on any sub-shape
1470 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1471 // check if anything was built
1472 TopExp_Explorer subS(shape, _subShape.ShapeType());
1475 for (; ret && subS.More(); subS.Next())
1476 ret = _father->GetSubMesh( subS.Current() )->IsMeshComputed();
1478 // Set _computeError
1479 if (!ret && !isComputeErrorSet)
1481 for (subS.ReInit(); subS.More(); subS.Next())
1483 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1484 if ( !sm->IsMeshComputed() )
1486 if ( !sm->_computeError )
1487 sm->_computeError = SMESH_ComputeError::New();
1488 if ( sm->_computeError->IsOK() )
1489 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1490 sm->_computeState = FAILED_TO_COMPUTE;
1491 sm->_computeError->myAlgo = algo;
1495 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1497 _computeError.reset();
1499 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1502 #ifdef WITH_SMESH_CANCEL_COMPUTE
1503 case COMPUTE_CANCELED: // nothing to do
1508 removeSubMeshElementsAndNodes();
1509 _computeState = NOT_READY;
1513 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1515 _computeState = READY_TO_COMPUTE;
1517 setAlgoState(MISSING_HYP);
1520 case SUBMESH_COMPUTED: // nothing to do
1522 case SUBMESH_RESTORED:
1523 // check if a mesh is already computed that may
1524 // happen after retrieval from a file
1525 ComputeStateEngine( CHECK_COMPUTE_STATE );
1526 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1528 if (algo) algo->SubmeshRestored( this );
1530 case MESH_ENTITY_REMOVED:
1532 case SUBMESH_LOADED:
1533 loadDependentMeshes();
1534 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1536 case CHECK_COMPUTE_STATE:
1537 if ( IsMeshComputed() )
1538 _computeState = COMPUTE_OK;
1546 // ----------------------------------------------------------------------
1551 case MODIF_ALGO_STATE:
1552 ComputeStateEngine( CLEAN );
1554 if (algo && !algo->NeedDiscreteBoundary())
1555 cleanDependsOn(); // clean sub-meshes with event CLEAN
1557 case COMPUTE: // nothing to do
1559 #ifdef WITH_SMESH_CANCEL_COMPUTE
1560 case COMPUTE_CANCELED: // nothing to do
1564 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1565 removeSubMeshElementsAndNodes();
1566 _computeState = NOT_READY;
1567 if ( _algoState == HYP_OK )
1568 _computeState = READY_TO_COMPUTE;
1570 case SUBMESH_COMPUTED: // nothing to do
1572 case SUBMESH_RESTORED:
1573 ComputeStateEngine( CHECK_COMPUTE_STATE );
1574 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1576 if (algo) algo->SubmeshRestored( this );
1578 case MESH_ENTITY_REMOVED:
1579 updateDependantsState ( CHECK_COMPUTE_STATE );
1580 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1581 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1583 case CHECK_COMPUTE_STATE:
1584 if ( !IsMeshComputed() ) {
1585 if (_algoState == HYP_OK)
1586 _computeState = READY_TO_COMPUTE;
1588 _computeState = NOT_READY;
1591 case SUBMESH_LOADED:
1592 // already treated event, thanks to which _computeState == COMPUTE_OK
1600 // ----------------------------------------------------------------------
1602 case FAILED_TO_COMPUTE:
1605 case MODIF_ALGO_STATE:
1607 ComputeStateEngine( CLEAN );
1609 if (algo && !algo->NeedDiscreteBoundary())
1610 cleanDependsOn(); // clean sub-meshes with event CLEAN
1611 if (_algoState == HYP_OK)
1612 _computeState = READY_TO_COMPUTE;
1614 _computeState = NOT_READY;
1616 case COMPUTE: // nothing to do
1618 case COMPUTE_CANCELED:
1621 algo->CancelCompute();
1625 cleanDependants(); // submeshes dependent on me should be cleaned as well
1626 removeSubMeshElementsAndNodes();
1628 case SUBMESH_COMPUTED: // allow retry compute
1629 if (_algoState == HYP_OK)
1630 _computeState = READY_TO_COMPUTE;
1632 _computeState = NOT_READY;
1634 case SUBMESH_RESTORED:
1635 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1637 case MESH_ENTITY_REMOVED:
1639 case CHECK_COMPUTE_STATE:
1640 if ( IsMeshComputed() )
1641 _computeState = COMPUTE_OK;
1643 if (_algoState == HYP_OK)
1644 _computeState = READY_TO_COMPUTE;
1646 _computeState = NOT_READY;
1648 // case SUBMESH_LOADED:
1656 // ----------------------------------------------------------------------
1662 notifyListenersOnEvent( event, COMPUTE_EVENT );
1668 //=============================================================================
1672 //=============================================================================
1674 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1676 _computeError.reset();
1680 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1681 vector<int> aVec(SMDSEntity_Last,0);
1682 aVec[SMDSEntity_Node] = 1;
1683 aResMap.insert(make_pair(this,aVec));
1687 //SMESH_Gen *gen = _father->GetGen();
1688 SMESH_Algo *algo = 0;
1689 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1692 if(algo && !aResMap.count(this) )
1694 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1695 if (!ret) return false;
1697 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary())
1699 // check submeshes needed
1700 bool subMeshEvaluated = true;
1701 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1702 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1703 while ( smIt->more() && subMeshEvaluated )
1705 SMESH_subMesh* sm = smIt->next();
1706 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1707 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1708 const vector<int> & nbs = aResMap[ sm ];
1709 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1711 if ( !subMeshEvaluated )
1714 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1715 ret = algo->Evaluate((*_father), _subShape, aResMap);
1717 aResMap.insert( make_pair( this,vector<int>(0)));
1724 //=======================================================================
1726 * \brief Update compute_state by _computeError and send proper events to
1727 * dependent submeshes
1728 * \retval bool - true if _computeError is NOT set
1730 //=======================================================================
1732 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1733 const bool theComputeOK,
1734 const TopoDS_Shape& theShape)
1736 bool noErrors = true;
1738 if ( !theShape.IsNull() )
1740 // Check state of submeshes
1741 if ( !theAlgo->NeedDiscreteBoundary())
1743 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1744 while ( smIt->more() )
1745 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1749 // Check state of neighbours
1750 if ( !theAlgo->OnlyUnaryInput() &&
1751 theShape.ShapeType() == TopAbs_COMPOUND &&
1752 !theShape.IsSame( _subShape ))
1754 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1755 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1757 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1759 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1766 // Set my _computeState
1768 if ( !_computeError || _computeError->IsOK() )
1770 // no error description is set to this sub-mesh, check if any mesh is computed
1771 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1772 if ( _computeState != COMPUTE_OK )
1774 if ( _subShape.ShapeType() == TopAbs_EDGE &&
1775 BRep_Tool::Degenerated( TopoDS::Edge( _subShape )) )
1776 _computeState = COMPUTE_OK;
1777 else if ( theComputeOK )
1778 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1782 if ( _computeError && !_computeError->IsOK() )
1784 if ( !_computeError->myAlgo )
1785 _computeError->myAlgo = theAlgo;
1789 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1790 if (_computeError->IsCommon() )
1791 text << _computeError->CommonName();
1793 text << _computeError->myName;
1794 if ( _computeError->myComment.size() > 0 )
1795 text << " \"" << _computeError->myComment << "\"";
1799 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1807 //=======================================================================
1808 //function : updateSubMeshState
1810 //=======================================================================
1812 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1814 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1815 while ( smIt->more() )
1816 smIt->next()->_computeState = theState;
1819 //=======================================================================
1820 //function : ComputeSubMeshStateEngine
1822 //=======================================================================
1824 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
1826 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
1827 while ( smIt->more() )
1828 smIt->next()->ComputeStateEngine(event);
1831 //=======================================================================
1832 //function : updateDependantsState
1834 //=======================================================================
1836 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
1838 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1839 for (; it.More(); it.Next())
1841 const TopoDS_Shape& ancestor = it.Value();
1842 SMESH_subMesh *aSubMesh =
1843 _father->GetSubMeshContaining(ancestor);
1845 aSubMesh->ComputeStateEngine( theEvent );
1849 //=============================================================================
1853 //=============================================================================
1855 void SMESH_subMesh::cleanDependants()
1857 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1859 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1860 for (; it.More(); it.Next())
1862 const TopoDS_Shape& ancestor = it.Value();
1863 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1864 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1865 // will erase mesh on other shapes in a compound
1866 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1867 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1869 !aSubMesh->IsEmpty() ) // prevent infinite CLEAN via event lesteners
1870 aSubMesh->ComputeStateEngine(CLEAN);
1876 //=============================================================================
1880 //=============================================================================
1882 void SMESH_subMesh::removeSubMeshElementsAndNodes()
1884 cleanSubMesh( this );
1886 // algo may bind a submesh not to _subShape, eg 3D algo
1887 // sets nodes on SHELL while _subShape may be SOLID
1889 int dim = SMESH_Gen::GetShapeDim( _subShape );
1890 int type = _subShape.ShapeType() + 1;
1891 for ( ; type <= TopAbs_EDGE; type++) {
1892 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1894 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1895 for ( ; exp.More(); exp.Next() )
1896 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1903 //=======================================================================
1904 //function : getCollection
1905 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1906 // meshed at once along with _subShape
1907 //=======================================================================
1909 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
1910 SMESH_Algo* theAlgo,
1911 bool & theSubComputed)
1913 theSubComputed = subMeshesComputed();
1915 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1917 if ( mainShape.IsSame( _subShape ))
1920 const bool ignoreAuxiliaryHyps = false;
1921 list<const SMESHDS_Hypothesis*> aUsedHyp =
1922 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1924 // put in a compound all shapes with the same hypothesis assigned
1925 // and a good ComputState
1927 TopoDS_Compound aCompound;
1928 BRep_Builder aBuilder;
1929 aBuilder.MakeCompound( aCompound );
1931 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1932 for ( ; anExplorer.More(); anExplorer.Next() )
1934 const TopoDS_Shape& S = anExplorer.Current();
1935 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1936 if ( subMesh == this )
1938 aBuilder.Add( aCompound, S );
1940 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1942 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1943 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1944 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1945 aBuilder.Add( aCompound, S );
1946 if ( !subMesh->subMeshesComputed() )
1947 theSubComputed = false;
1954 //=======================================================================
1955 //function : getSimilarAttached
1956 //purpose : return a hypothesis attached to theShape.
1957 // If theHyp is provided, similar but not same hypotheses
1958 // is returned; else only applicable ones having theHypType
1960 //=======================================================================
1962 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
1963 const SMESH_Hypothesis * theHyp,
1964 const int theHypType)
1966 SMESH_HypoFilter hypoKind;
1967 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1969 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1970 hypoKind.AndNot( hypoKind.Is( theHyp ));
1971 if ( theHyp->IsAuxiliary() )
1972 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1974 hypoKind.AndNot( hypoKind.IsAuxiliary());
1977 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1980 return _father->GetHypothesis( theShape, hypoKind, false );
1983 //=======================================================================
1984 //function : CheckConcurentHypothesis
1985 //purpose : check if there are several applicable hypothesis attached to
1987 //=======================================================================
1989 SMESH_Hypothesis::Hypothesis_Status
1990 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1992 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1994 // is there local hypothesis on me?
1995 if ( getSimilarAttached( _subShape, 0, theHypType ) )
1996 return SMESH_Hypothesis::HYP_OK;
1999 TopoDS_Shape aPrevWithHyp;
2000 const SMESH_Hypothesis* aPrevHyp = 0;
2001 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2002 for (; it.More(); it.Next())
2004 const TopoDS_Shape& ancestor = it.Value();
2005 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2008 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2010 aPrevWithHyp = ancestor;
2013 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2014 return SMESH_Hypothesis::HYP_CONCURENT;
2016 return SMESH_Hypothesis::HYP_OK;
2019 return SMESH_Hypothesis::HYP_OK;
2022 //================================================================================
2024 * \brief Constructor of OwnListenerData
2026 //================================================================================
2028 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2030 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2031 mySubMeshID( sm ? sm->GetId() : -1 ),
2036 //================================================================================
2038 * \brief Sets an event listener and its data to a submesh
2039 * \param listener - the listener to store
2040 * \param data - the listener data to store
2041 * \param where - the submesh to store the listener and it's data
2043 * It remembers the submesh where it puts the listener in order to delete
2044 * them when HYP_OK algo_state is lost
2045 * After being set, event listener is notified on each event of where submesh.
2047 //================================================================================
2049 void SMESH_subMesh::SetEventListener(EventListener* listener,
2050 EventListenerData* data,
2051 SMESH_subMesh* where)
2053 if ( listener && where ) {
2054 where->setEventListener( listener, data );
2055 _ownListeners.push_back( OwnListenerData( where, listener ));
2059 //================================================================================
2061 * \brief Sets an event listener and its data to a submesh
2062 * \param listener - the listener to store
2063 * \param data - the listener data to store
2065 * After being set, event listener is notified on each event of a submesh.
2067 //================================================================================
2069 void SMESH_subMesh::setEventListener(EventListener* listener, EventListenerData* data)
2071 map< EventListener*, EventListenerData* >::iterator l_d =
2072 _eventListeners.find( listener );
2073 if ( l_d != _eventListeners.end() ) {
2074 EventListenerData* curData = l_d->second;
2075 if ( curData && curData != data && curData->IsDeletable() )
2080 _eventListeners.insert( make_pair( listener, data ));
2083 //================================================================================
2085 * \brief Return an event listener data
2086 * \param listener - the listener whose data is
2087 * \retval EventListenerData* - found data, maybe NULL
2089 //================================================================================
2091 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
2093 map< EventListener*, EventListenerData* >::const_iterator l_d =
2094 _eventListeners.find( listener );
2095 if ( l_d != _eventListeners.end() )
2100 //================================================================================
2102 * \brief Notify stored event listeners on the occured event
2103 * \param event - algo_event or compute_event itself
2104 * \param eventType - algo_event or compute_event
2105 * \param hyp - hypothesis, if eventType is algo_event
2107 //================================================================================
2109 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2110 const event_type eventType,
2111 SMESH_Hypothesis* hyp)
2113 map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2114 for ( ; l_d != _eventListeners.end(); ++l_d )
2116 std::pair< EventListener*, EventListenerData* > li_da = *l_d; /* copy to enable removal
2119 its ProcessEvent() */
2120 if ( li_da.first->myBusySM.insert( this ).second )
2122 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2123 li_da.first->myBusySM.erase( this );
2128 //================================================================================
2130 * \brief Unregister the listener and delete listener's data
2131 * \param listener - the event listener
2133 //================================================================================
2135 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2137 map< EventListener*, EventListenerData* >::iterator l_d =
2138 _eventListeners.find( listener );
2139 if ( l_d != _eventListeners.end() ) {
2140 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2141 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2142 _eventListeners.erase( l_d );
2146 //================================================================================
2148 * \brief Delete event listeners depending on algo of this submesh
2150 //================================================================================
2152 void SMESH_subMesh::deleteOwnListeners()
2154 list< OwnListenerData >::iterator d;
2155 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2157 if ( !_father->MeshExists( d->myMeshID ))
2159 if ( _father->GetId() == d->myMeshID && !_father->GetSubMeshContaining( d->mySubMeshID ))
2161 d->mySubMesh->DeleteEventListener( d->myListener );
2163 _ownListeners.clear();
2166 //=======================================================================
2167 //function : loadDependentMeshes
2168 //purpose : loads dependent meshes on SUBMESH_LOADED event
2169 //=======================================================================
2171 void SMESH_subMesh::loadDependentMeshes()
2173 list< OwnListenerData >::iterator d;
2174 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2175 if ( _father != d->mySubMesh->_father )
2176 d->mySubMesh->_father->Load();
2178 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2179 // for ( ; l_d != _eventListeners.end(); ++l_d )
2180 // if ( l_d->second )
2182 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2183 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2184 // for ( ; sm != smList.end(); ++sm )
2185 // if ( _father != (*sm)->_father )
2186 // (*sm)->_father->Load();
2190 //================================================================================
2192 * \brief Do something on a certain event
2193 * \param event - algo_event or compute_event itself
2194 * \param eventType - algo_event or compute_event
2195 * \param subMesh - the submesh where the event occures
2196 * \param data - listener data stored in the subMesh
2197 * \param hyp - hypothesis, if eventType is algo_event
2199 * The base implementation translates CLEAN event to the subMesh
2200 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2201 * successful COMPUTE event.
2203 //================================================================================
2205 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2206 const int eventType,
2207 SMESH_subMesh* subMesh,
2208 EventListenerData* data,
2209 const SMESH_Hypothesis* /*hyp*/)
2211 if ( data && !data->mySubMeshes.empty() &&
2212 eventType == SMESH_subMesh::COMPUTE_EVENT)
2214 ASSERT( data->mySubMeshes.front() != subMesh );
2215 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2216 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2218 case SMESH_subMesh::CLEAN:
2219 for ( ; smIt != smEnd; ++ smIt)
2220 (*smIt)->ComputeStateEngine( event );
2222 case SMESH_subMesh::COMPUTE:
2223 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2224 for ( ; smIt != smEnd; ++ smIt)
2225 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2234 //================================================================================
2236 * \brief Iterator over submeshes and optionally prepended or appended one
2238 //================================================================================
2240 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2242 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2243 SMESH_subMesh* prepend,
2244 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2246 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2247 if ( myCur == append ) append = 0;
2249 /// Return true if and only if there are other object in this iterator
2254 /// Return the current object and step to the next one
2255 virtual SMESH_subMesh* next()
2257 SMESH_subMesh* res = myCur;
2258 if ( myIt->more() ) { myCur = myIt->next(); }
2259 else { myCur = myAppend; myAppend = 0; }
2266 SMESH_subMesh *myAppend, *myCur;
2267 SMDS_Iterator<SMESH_subMesh*> *myIt;
2271 //================================================================================
2273 * \brief Return iterator on the submeshes this one depends on
2274 * \param includeSelf - this submesh to be returned also
2275 * \param reverse - if true, complex shape submeshes go first
2277 //================================================================================
2279 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2282 SMESH_subMesh *prepend=0, *append=0;
2283 if ( includeSelf ) {
2284 if ( reverse ) prepend = this;
2287 typedef map < int, SMESH_subMesh * > TMap;
2290 return SMESH_subMeshIteratorPtr
2291 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2294 return SMESH_subMeshIteratorPtr
2295 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));
2299 //================================================================================
2301 * \brief Find common submeshes (based on shared sub-shapes with other
2302 * \param theOther submesh to check
2303 * \param theSetOfCommon set of common submesh
2305 //================================================================================
2307 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2308 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2310 int oldNb = theSetOfCommon.size();
2311 // check main submeshes
2312 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2313 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2314 theSetOfCommon.insert( this );
2315 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2316 theSetOfCommon.insert( theOther );
2317 // check common submeshes
2318 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2319 for( ; mapIt != _mapDepend.end(); mapIt++ )
2320 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2321 theSetOfCommon.insert( (*mapIt).second );
2322 return oldNb < theSetOfCommon.size();