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 TopExp_Explorer subS(shape, _subShape.ShapeType());
1470 if (ret) // check if anything was built
1472 for (; ret && subS.More(); subS.Next())
1473 ret = _father->GetSubMesh( subS.Current() )->IsMeshComputed();
1475 bool isComputeErrorSet = !checkComputeError( algo, shape );
1476 if (!ret && !isComputeErrorSet)
1478 // Set _computeError
1479 for (subS.ReInit(); subS.More(); subS.Next())
1481 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1482 if ( !sm->IsMeshComputed() )
1484 if ( !sm->_computeError )
1485 sm->_computeError = SMESH_ComputeError::New();
1486 if ( sm->_computeError->IsOK() )
1487 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1488 sm->_computeState = FAILED_TO_COMPUTE;
1489 sm->_computeError->myAlgo = algo;
1493 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1495 _computeError.reset();
1497 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1500 #ifdef WITH_SMESH_CANCEL_COMPUTE
1501 case COMPUTE_CANCELED: // nothing to do
1506 removeSubMeshElementsAndNodes();
1507 _computeState = NOT_READY;
1511 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1513 _computeState = READY_TO_COMPUTE;
1515 setAlgoState(MISSING_HYP);
1518 case SUBMESH_COMPUTED: // nothing to do
1520 case SUBMESH_RESTORED:
1521 // check if a mesh is already computed that may
1522 // happen after retrieval from a file
1523 ComputeStateEngine( CHECK_COMPUTE_STATE );
1524 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1526 if (algo) algo->SubmeshRestored( this );
1528 case MESH_ENTITY_REMOVED:
1530 case SUBMESH_LOADED:
1531 loadDependentMeshes();
1532 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1534 case CHECK_COMPUTE_STATE:
1535 if ( IsMeshComputed() )
1536 _computeState = COMPUTE_OK;
1544 // ----------------------------------------------------------------------
1549 case MODIF_ALGO_STATE:
1550 ComputeStateEngine( CLEAN );
1552 if (algo && !algo->NeedDiscreteBoundary())
1553 cleanDependsOn(); // clean sub-meshes with event CLEAN
1555 case COMPUTE: // nothing to do
1557 #ifdef WITH_SMESH_CANCEL_COMPUTE
1558 case COMPUTE_CANCELED: // nothing to do
1562 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1563 removeSubMeshElementsAndNodes();
1564 _computeState = NOT_READY;
1565 if ( _algoState == HYP_OK )
1566 _computeState = READY_TO_COMPUTE;
1568 case SUBMESH_COMPUTED: // nothing to do
1570 case SUBMESH_RESTORED:
1571 ComputeStateEngine( CHECK_COMPUTE_STATE );
1572 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1574 if (algo) algo->SubmeshRestored( this );
1576 case MESH_ENTITY_REMOVED:
1577 updateDependantsState ( CHECK_COMPUTE_STATE );
1578 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1579 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1581 case CHECK_COMPUTE_STATE:
1582 if ( !IsMeshComputed() ) {
1583 if (_algoState == HYP_OK)
1584 _computeState = READY_TO_COMPUTE;
1586 _computeState = NOT_READY;
1589 case SUBMESH_LOADED:
1590 // already treated event, thanks to which _computeState == COMPUTE_OK
1598 // ----------------------------------------------------------------------
1600 case FAILED_TO_COMPUTE:
1603 case MODIF_ALGO_STATE:
1605 ComputeStateEngine( CLEAN );
1607 if (algo && !algo->NeedDiscreteBoundary())
1608 cleanDependsOn(); // clean sub-meshes with event CLEAN
1609 if (_algoState == HYP_OK)
1610 _computeState = READY_TO_COMPUTE;
1612 _computeState = NOT_READY;
1614 case COMPUTE: // nothing to do
1616 case COMPUTE_CANCELED:
1619 algo->CancelCompute();
1623 cleanDependants(); // submeshes dependent on me should be cleaned as well
1624 removeSubMeshElementsAndNodes();
1626 case SUBMESH_COMPUTED: // allow retry compute
1627 if (_algoState == HYP_OK)
1628 _computeState = READY_TO_COMPUTE;
1630 _computeState = NOT_READY;
1632 case SUBMESH_RESTORED:
1633 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1635 case MESH_ENTITY_REMOVED:
1637 case CHECK_COMPUTE_STATE:
1638 if ( IsMeshComputed() )
1639 _computeState = COMPUTE_OK;
1641 if (_algoState == HYP_OK)
1642 _computeState = READY_TO_COMPUTE;
1644 _computeState = NOT_READY;
1646 // case SUBMESH_LOADED:
1654 // ----------------------------------------------------------------------
1660 notifyListenersOnEvent( event, COMPUTE_EVENT );
1666 //=============================================================================
1670 //=============================================================================
1672 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1674 _computeError.reset();
1678 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1679 vector<int> aVec(SMDSEntity_Last,0);
1680 aVec[SMDSEntity_Node] = 1;
1681 aResMap.insert(make_pair(this,aVec));
1685 //SMESH_Gen *gen = _father->GetGen();
1686 SMESH_Algo *algo = 0;
1687 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1690 if(algo && !aResMap.count(this) )
1692 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1693 if (!ret) return false;
1695 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary())
1697 // check submeshes needed
1698 bool subMeshEvaluated = true;
1699 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1700 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1701 while ( smIt->more() && subMeshEvaluated )
1703 SMESH_subMesh* sm = smIt->next();
1704 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1705 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1706 const vector<int> & nbs = aResMap[ sm ];
1707 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1709 if ( !subMeshEvaluated )
1712 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1713 ret = algo->Evaluate((*_father), _subShape, aResMap);
1715 aResMap.insert( make_pair( this,vector<int>(0)));
1722 //=======================================================================
1724 * \brief Update compute_state by _computeError and send proper events to
1725 * dependent submeshes
1726 * \retval bool - true if _computeError is NOT set
1728 //=======================================================================
1730 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1732 bool noErrors = true;
1734 if ( !theShape.IsNull() )
1736 // Check state of submeshes
1737 if ( !theAlgo->NeedDiscreteBoundary())
1739 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1740 while ( smIt->more() )
1741 if ( !smIt->next()->checkComputeError( theAlgo ))
1745 // Check state of neighbours
1746 if ( !theAlgo->OnlyUnaryInput() &&
1747 theShape.ShapeType() == TopAbs_COMPOUND &&
1748 !theShape.IsSame( _subShape ))
1750 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1751 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1753 if ( !sm->checkComputeError( theAlgo, sm->GetSubShape() ))
1755 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1762 if ( !_computeError || _computeError->IsOK() )
1764 // no error description is set to this sub-mesh, check if any mesh is computed
1765 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1769 if ( !_computeError->myAlgo )
1770 _computeError->myAlgo = theAlgo;
1774 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1775 if (_computeError->IsCommon() )
1776 text << _computeError->CommonName();
1778 text << _computeError->myName;
1779 if ( _computeError->myComment.size() > 0 )
1780 text << " \"" << _computeError->myComment << "\"";
1784 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1792 //=======================================================================
1793 //function : updateSubMeshState
1795 //=======================================================================
1797 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1799 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1800 while ( smIt->more() )
1801 smIt->next()->_computeState = theState;
1804 //=======================================================================
1805 //function : ComputeSubMeshStateEngine
1807 //=======================================================================
1809 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
1811 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
1812 while ( smIt->more() )
1813 smIt->next()->ComputeStateEngine(event);
1816 //=======================================================================
1817 //function : updateDependantsState
1819 //=======================================================================
1821 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
1823 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1824 for (; it.More(); it.Next())
1826 const TopoDS_Shape& ancestor = it.Value();
1827 SMESH_subMesh *aSubMesh =
1828 _father->GetSubMeshContaining(ancestor);
1830 aSubMesh->ComputeStateEngine( theEvent );
1834 //=============================================================================
1838 //=============================================================================
1840 void SMESH_subMesh::cleanDependants()
1842 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1844 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1845 for (; it.More(); it.Next())
1847 const TopoDS_Shape& ancestor = it.Value();
1848 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1849 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1850 // will erase mesh on other shapes in a compound
1851 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1852 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1854 !aSubMesh->IsEmpty() ) // prevent infinite CLEAN via event lesteners
1855 aSubMesh->ComputeStateEngine(CLEAN);
1861 //=============================================================================
1865 //=============================================================================
1867 void SMESH_subMesh::removeSubMeshElementsAndNodes()
1869 cleanSubMesh( this );
1871 // algo may bind a submesh not to _subShape, eg 3D algo
1872 // sets nodes on SHELL while _subShape may be SOLID
1874 int dim = SMESH_Gen::GetShapeDim( _subShape );
1875 int type = _subShape.ShapeType() + 1;
1876 for ( ; type <= TopAbs_EDGE; type++) {
1877 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1879 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1880 for ( ; exp.More(); exp.Next() )
1881 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1888 //=======================================================================
1889 //function : getCollection
1890 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1891 // meshed at once along with _subShape
1892 //=======================================================================
1894 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
1895 SMESH_Algo* theAlgo,
1896 bool & theSubComputed)
1898 theSubComputed = subMeshesComputed();
1900 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1902 if ( mainShape.IsSame( _subShape ))
1905 const bool ignoreAuxiliaryHyps = false;
1906 list<const SMESHDS_Hypothesis*> aUsedHyp =
1907 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1909 // put in a compound all shapes with the same hypothesis assigned
1910 // and a good ComputState
1912 TopoDS_Compound aCompound;
1913 BRep_Builder aBuilder;
1914 aBuilder.MakeCompound( aCompound );
1916 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1917 for ( ; anExplorer.More(); anExplorer.Next() )
1919 const TopoDS_Shape& S = anExplorer.Current();
1920 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1921 if ( subMesh == this )
1923 aBuilder.Add( aCompound, S );
1925 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1927 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1928 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1929 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1930 aBuilder.Add( aCompound, S );
1931 if ( !subMesh->subMeshesComputed() )
1932 theSubComputed = false;
1939 //=======================================================================
1940 //function : getSimilarAttached
1941 //purpose : return a hypothesis attached to theShape.
1942 // If theHyp is provided, similar but not same hypotheses
1943 // is returned; else only applicable ones having theHypType
1945 //=======================================================================
1947 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
1948 const SMESH_Hypothesis * theHyp,
1949 const int theHypType)
1951 SMESH_HypoFilter hypoKind;
1952 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1954 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1955 hypoKind.AndNot( hypoKind.Is( theHyp ));
1956 if ( theHyp->IsAuxiliary() )
1957 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1959 hypoKind.AndNot( hypoKind.IsAuxiliary());
1962 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1965 return _father->GetHypothesis( theShape, hypoKind, false );
1968 //=======================================================================
1969 //function : CheckConcurentHypothesis
1970 //purpose : check if there are several applicable hypothesis attached to
1972 //=======================================================================
1974 SMESH_Hypothesis::Hypothesis_Status
1975 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1977 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1979 // is there local hypothesis on me?
1980 if ( getSimilarAttached( _subShape, 0, theHypType ) )
1981 return SMESH_Hypothesis::HYP_OK;
1984 TopoDS_Shape aPrevWithHyp;
1985 const SMESH_Hypothesis* aPrevHyp = 0;
1986 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1987 for (; it.More(); it.Next())
1989 const TopoDS_Shape& ancestor = it.Value();
1990 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
1993 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1995 aPrevWithHyp = ancestor;
1998 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1999 return SMESH_Hypothesis::HYP_CONCURENT;
2001 return SMESH_Hypothesis::HYP_OK;
2004 return SMESH_Hypothesis::HYP_OK;
2007 //================================================================================
2009 * \brief Constructor of OwnListenerData
2011 //================================================================================
2013 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2015 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2016 mySubMeshID( sm ? sm->GetId() : -1 ),
2021 //================================================================================
2023 * \brief Sets an event listener and its data to a submesh
2024 * \param listener - the listener to store
2025 * \param data - the listener data to store
2026 * \param where - the submesh to store the listener and it's data
2028 * It remembers the submesh where it puts the listener in order to delete
2029 * them when HYP_OK algo_state is lost
2030 * After being set, event listener is notified on each event of where submesh.
2032 //================================================================================
2034 void SMESH_subMesh::SetEventListener(EventListener* listener,
2035 EventListenerData* data,
2036 SMESH_subMesh* where)
2038 if ( listener && where ) {
2039 where->setEventListener( listener, data );
2040 _ownListeners.push_back( OwnListenerData( where, listener ));
2044 //================================================================================
2046 * \brief Sets an event listener and its data to a submesh
2047 * \param listener - the listener to store
2048 * \param data - the listener data to store
2050 * After being set, event listener is notified on each event of a submesh.
2052 //================================================================================
2054 void SMESH_subMesh::setEventListener(EventListener* listener, EventListenerData* data)
2056 map< EventListener*, EventListenerData* >::iterator l_d =
2057 _eventListeners.find( listener );
2058 if ( l_d != _eventListeners.end() ) {
2059 EventListenerData* curData = l_d->second;
2060 if ( curData && curData != data && curData->IsDeletable() )
2065 _eventListeners.insert( make_pair( listener, data ));
2068 //================================================================================
2070 * \brief Return an event listener data
2071 * \param listener - the listener whose data is
2072 * \retval EventListenerData* - found data, maybe NULL
2074 //================================================================================
2076 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
2078 map< EventListener*, EventListenerData* >::const_iterator l_d =
2079 _eventListeners.find( listener );
2080 if ( l_d != _eventListeners.end() )
2085 //================================================================================
2087 * \brief Notify stored event listeners on the occured event
2088 * \param event - algo_event or compute_event itself
2089 * \param eventType - algo_event or compute_event
2090 * \param hyp - hypothesis, if eventType is algo_event
2092 //================================================================================
2094 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2095 const event_type eventType,
2096 SMESH_Hypothesis* hyp)
2098 map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2099 for ( ; l_d != _eventListeners.end(); ++l_d )
2101 std::pair< EventListener*, EventListenerData* > li_da = *l_d; /* copy to enable removal
2104 its ProcessEvent() */
2105 if ( li_da.first->myBusySM.insert( this ).second )
2107 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2108 li_da.first->myBusySM.erase( this );
2113 //================================================================================
2115 * \brief Unregister the listener and delete listener's data
2116 * \param listener - the event listener
2118 //================================================================================
2120 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2122 map< EventListener*, EventListenerData* >::iterator l_d =
2123 _eventListeners.find( listener );
2124 if ( l_d != _eventListeners.end() ) {
2125 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2126 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2127 _eventListeners.erase( l_d );
2131 //================================================================================
2133 * \brief Delete event listeners depending on algo of this submesh
2135 //================================================================================
2137 void SMESH_subMesh::deleteOwnListeners()
2139 list< OwnListenerData >::iterator d;
2140 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2142 if ( !_father->MeshExists( d->myMeshID ))
2144 if ( _father->GetId() == d->myMeshID && !_father->GetSubMeshContaining( d->mySubMeshID ))
2146 d->mySubMesh->DeleteEventListener( d->myListener );
2148 _ownListeners.clear();
2151 //=======================================================================
2152 //function : loadDependentMeshes
2153 //purpose : loads dependent meshes on SUBMESH_LOADED event
2154 //=======================================================================
2156 void SMESH_subMesh::loadDependentMeshes()
2158 list< OwnListenerData >::iterator d;
2159 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2160 if ( _father != d->mySubMesh->_father )
2161 d->mySubMesh->_father->Load();
2163 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2164 // for ( ; l_d != _eventListeners.end(); ++l_d )
2165 // if ( l_d->second )
2167 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2168 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2169 // for ( ; sm != smList.end(); ++sm )
2170 // if ( _father != (*sm)->_father )
2171 // (*sm)->_father->Load();
2175 //================================================================================
2177 * \brief Do something on a certain event
2178 * \param event - algo_event or compute_event itself
2179 * \param eventType - algo_event or compute_event
2180 * \param subMesh - the submesh where the event occures
2181 * \param data - listener data stored in the subMesh
2182 * \param hyp - hypothesis, if eventType is algo_event
2184 * The base implementation translates CLEAN event to the subMesh
2185 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2186 * successful COMPUTE event.
2188 //================================================================================
2190 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2191 const int eventType,
2192 SMESH_subMesh* subMesh,
2193 EventListenerData* data,
2194 const SMESH_Hypothesis* /*hyp*/)
2196 if ( data && !data->mySubMeshes.empty() &&
2197 eventType == SMESH_subMesh::COMPUTE_EVENT)
2199 ASSERT( data->mySubMeshes.front() != subMesh );
2200 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2201 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2203 case SMESH_subMesh::CLEAN:
2204 for ( ; smIt != smEnd; ++ smIt)
2205 (*smIt)->ComputeStateEngine( event );
2207 case SMESH_subMesh::COMPUTE:
2208 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2209 for ( ; smIt != smEnd; ++ smIt)
2210 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2219 //================================================================================
2221 * \brief Iterator over submeshes and optionally prepended or appended one
2223 //================================================================================
2225 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2227 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2228 SMESH_subMesh* prepend,
2229 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2231 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2232 if ( myCur == append ) append = 0;
2234 /// Return true if and only if there are other object in this iterator
2239 /// Return the current object and step to the next one
2240 virtual SMESH_subMesh* next()
2242 SMESH_subMesh* res = myCur;
2243 if ( myIt->more() ) { myCur = myIt->next(); }
2244 else { myCur = myAppend; myAppend = 0; }
2251 SMESH_subMesh *myAppend, *myCur;
2252 SMDS_Iterator<SMESH_subMesh*> *myIt;
2256 //================================================================================
2258 * \brief Return iterator on the submeshes this one depends on
2259 * \param includeSelf - this submesh to be returned also
2260 * \param reverse - if true, complex shape submeshes go first
2262 //================================================================================
2264 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2267 SMESH_subMesh *prepend=0, *append=0;
2268 if ( includeSelf ) {
2269 if ( reverse ) prepend = this;
2272 typedef map < int, SMESH_subMesh * > TMap;
2275 return SMESH_subMeshIteratorPtr
2276 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2279 return SMESH_subMeshIteratorPtr
2280 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));
2284 //================================================================================
2286 * \brief Find common submeshes (based on shared sub-shapes with other
2287 * \param theOther submesh to check
2288 * \param theSetOfCommon set of common submesh
2290 //================================================================================
2292 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2293 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2295 int oldNb = theSetOfCommon.size();
2296 // check main submeshes
2297 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2298 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2299 theSetOfCommon.insert( this );
2300 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2301 theSetOfCommon.insert( theOther );
2302 // check common submeshes
2303 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2304 for( ; mapIt != _mapDepend.end(); mapIt++ )
2305 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2306 theSetOfCommon.insert( (*mapIt).second );
2307 return oldNb < theSetOfCommon.size();