1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SMESH SMESH : implementaion of SMESH idl descriptions
23 // File : SMESH_subMesh.cxx
24 // Author : Paul RASCLE, EDF
27 #include "SMESH_subMesh.hxx"
29 #include "SMESH_Algo.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_HypoFilter.hxx"
32 #include "SMESH_Hypothesis.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_MesherHelper.hxx"
35 #include "SMESH_subMeshEventListener.hxx"
36 #include "SMESH_Comment.hxx"
37 #include "SMDS_SetIterator.hxx"
38 #include "SMDSAbs_ElementType.hxx"
40 #include <Basics_OCCTVersion.hxx>
42 #include "utilities.h"
44 #include "Basics_Utils.hxx"
46 #include <BRep_Builder.hxx>
47 #include <BRep_Tool.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
52 #include <TopoDS_Compound.hxx>
54 #include <TopExp_Explorer.hxx>
55 #include <TopoDS_Iterator.hxx>
57 #include <Standard_OutOfMemory.hxx>
58 #include <Standard_ErrorHandler.hxx>
64 //=============================================================================
66 * \brief Allocate some memory at construction and release it at destruction.
67 * Is used to be able to continue working after mesh generation breaks due to
70 //=============================================================================
75 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
76 ~MemoryReserve() { delete [] myBuf; }
79 //=============================================================================
81 * default constructor:
83 //=============================================================================
85 SMESH_subMesh::SMESH_subMesh(int Id,
87 SMESHDS_Mesh * meshDS,
88 const TopoDS_Shape & aSubShape)
90 _subShape = aSubShape;
91 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
94 _dependenceAnalysed = _alwaysComputed = false;
96 if (_subShape.ShapeType() == TopAbs_VERTEX)
99 _computeState = READY_TO_COMPUTE;
103 _algoState = NO_ALGO;
104 _computeState = NOT_READY;
108 //=============================================================================
112 //=============================================================================
114 SMESH_subMesh::~SMESH_subMesh()
116 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
118 DeleteOwnListeners();
121 //=============================================================================
125 //=============================================================================
127 int SMESH_subMesh::GetId() const
129 //MESSAGE("SMESH_subMesh::GetId");
133 //=============================================================================
137 //=============================================================================
139 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
141 // submesh appears in DS only when a mesher set nodes and elements on a shape
142 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
145 //=============================================================================
149 //=============================================================================
151 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
153 if ( !GetSubMeshDS() ) {
154 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
155 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
157 return GetSubMeshDS();
160 //=============================================================================
164 //=============================================================================
166 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
168 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
169 while ( smIt->more() ) {
170 SMESH_subMesh *sm = smIt->next();
171 if ( sm->GetComputeState() == READY_TO_COMPUTE )
174 return 0; // nothing to compute
177 //================================================================================
179 * \brief Returns a current algorithm
181 //================================================================================
183 SMESH_Algo* SMESH_subMesh::GetAlgo() const
185 return _father->GetGen()->GetAlgo(*_father, _subShape);
188 //================================================================================
190 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
191 * none mesh entity is bound to it (PAL13615, 2nd part)
193 //================================================================================
195 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
197 _alwaysComputed = isAlCo;
198 if ( _alwaysComputed )
199 _computeState = COMPUTE_OK;
201 ComputeStateEngine( CHECK_COMPUTE_STATE );
204 //=======================================================================
206 * \brief Return true if no mesh entities is bound to the submesh
208 //=======================================================================
210 bool SMESH_subMesh::IsEmpty() const
212 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
213 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
217 //=======================================================================
218 //function : IsMeshComputed
219 //purpose : check if _subMeshDS contains mesh elements
220 //=======================================================================
222 bool SMESH_subMesh::IsMeshComputed() const
224 if ( _alwaysComputed )
226 // algo may bind a submesh not to _subShape, eg 3D algo
227 // sets nodes on SHELL while _subShape may be SOLID
229 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
230 int dim = SMESH_Gen::GetShapeDim( _subShape );
231 int type = _subShape.ShapeType();
232 for ( ; type <= TopAbs_VERTEX; type++) {
233 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
235 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
236 for ( ; exp.More(); exp.Next() )
238 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
240 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
253 //=============================================================================
257 //=============================================================================
259 bool SMESH_subMesh::SubMeshesComputed()
261 int myDim = SMESH_Gen::GetShapeDim( _subShape );
262 int dimToCheck = myDim - 1;
263 bool subMeshesComputed = true;
264 // check subMeshes with upper dimension => reverse iteration
265 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
266 while ( smIt->more() )
268 SMESH_subMesh *sm = smIt->next();
269 if ( sm->_alwaysComputed )
271 const TopoDS_Shape & ss = sm->GetSubShape();
272 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
273 // in checking of existence of edges if the algo needs only faces. Moreover,
274 // degenerated edges may have no submesh, as after computing NETGEN_2D.
275 int dim = SMESH_Gen::GetShapeDim( ss );
276 if (dim < dimToCheck)
277 break; // the rest subMeshes are all of less dimension
278 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
279 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
280 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
283 int type = ss.ShapeType();
285 subMeshesComputed = false;
289 case TopAbs_COMPOUND:
291 MESSAGE("The not computed sub mesh is a COMPOUND");
294 case TopAbs_COMPSOLID:
296 MESSAGE("The not computed sub mesh is a COMPSOLID");
301 MESSAGE("The not computed sub mesh is a SHEL");
306 MESSAGE("The not computed sub mesh is a WIRE");
311 MESSAGE("The not computed sub mesh is a SOLID");
316 MESSAGE("The not computed sub mesh is a FACE");
321 MESSAGE("The not computed sub mesh is a EDGE");
326 MESSAGE("The not computed sub mesh is of unknown type");
334 return subMeshesComputed;
337 //=============================================================================
341 //=============================================================================
343 bool SMESH_subMesh::SubMeshesReady()
345 bool subMeshesReady = true;
346 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
347 while ( smIt->more() ) {
348 SMESH_subMesh *sm = smIt->next();
349 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
350 sm->GetComputeState() == READY_TO_COMPUTE);
353 subMeshesReady = false;
358 return subMeshesReady;
361 //=============================================================================
363 * Construct dependence on first level subMeshes. complex shapes (compsolid,
364 * shell, wire) are not analysed the same way as simple shapes (solid, face,
366 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
367 * with possible multiples occurences. Multiples occurences corresponds to
368 * internal frontiers within shapes of the collection and must not be keeped.
369 * See FinalizeDependence.
371 //=============================================================================
373 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
375 if (_dependenceAnalysed)
378 //MESSAGE("SMESH_subMesh::DependsOn");
380 int type = _subShape.ShapeType();
384 case TopAbs_COMPOUND:
386 //MESSAGE("compound");
387 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();exp.Next())
389 InsertDependence(exp.Current());
391 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More(); exp.Next())
393 if ( BRep_Tool::IsClosed(exp.Current() ))
394 InsertDependence(exp.Current()); //only shell not in solid
396 for (TopExp_Explorer expF(exp.Current(), TopAbs_FACE); expF.More();expF.Next())
397 InsertDependence(expF.Current()); // issue 0020959: HEXA_3D fails on shell
400 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();exp.Next())
402 InsertDependence(exp.Current());
404 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();exp.Next())
406 InsertDependence(exp.Current());
410 case TopAbs_COMPSOLID:
412 //MESSAGE("compsolid");
413 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More(); exp.Next())
415 InsertDependence(exp.Current());
422 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More(); exp.Next())
424 InsertDependence(exp.Current());
431 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More(); exp.Next())
433 InsertDependence(exp.Current());
440 if(_father->HasShapeToMesh()) {
441 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();exp.Next())
443 InsertDependence(exp.Current());
451 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();exp.Next())
453 InsertDependence(exp.Current());
460 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More(); exp.Next())
462 InsertDependence(exp.Current());
475 _dependenceAnalysed = true;
479 //=============================================================================
481 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
483 //=============================================================================
485 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
487 //MESSAGE("SMESH_subMesh::InsertDependence");
488 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
489 int type = aSubShape.ShapeType();
490 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
491 int cle = aSubMesh->GetId();
492 cle += 10000000 * ordType; // sort map by ordType then index
493 if ( _mapDepend.find( cle ) == _mapDepend.end())
495 _mapDepend[cle] = aSubMesh;
496 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
497 _mapDepend.insert( subMap.begin(), subMap.end() );
501 //=============================================================================
505 //=============================================================================
507 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
509 //MESSAGE("SMESH_subMesh::GetSubShape");
514 //=======================================================================
515 //function : CanAddHypothesis
516 //purpose : return true if theHypothesis can be attached to me:
517 // its dimention is checked
518 //=======================================================================
520 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
522 int aHypDim = theHypothesis->GetDim();
523 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
524 // issue 21106. Forbid 3D mesh on the SHELL
525 // if (aHypDim == 3 && aShapeDim == 3) {
526 // // check case of open shell
527 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
528 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
531 if ( aHypDim <= aShapeDim )
537 //=======================================================================
538 //function : IsApplicableHypotesis
540 //=======================================================================
542 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
543 const TopAbs_ShapeEnum theShapeType)
545 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
548 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
549 // issue 21106. Forbid 3D mesh on the SHELL
550 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
556 switch ( theShapeType ) {
561 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
564 // Special case for algorithms, building 2D mesh on a whole shell.
565 // Before this fix there was a problem after restoring from study,
566 // because in that case algorithm is assigned before hypothesis
567 // (on shell in problem case) and hypothesis is checked on faces
568 // (because it is 2D), where we have NO_ALGO state.
569 // Now 2D hypothesis is also applicable to shells.
570 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
573 // case TopAbs_COMPSOLID:
574 // case TopAbs_COMPOUND:
580 //=============================================================================
584 //=============================================================================
586 SMESH_Hypothesis::Hypothesis_Status
587 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
589 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
590 //SCRUTE(_algoState);
593 // **** les retour des evenement shape sont significatifs
594 // (add ou remove fait ou non)
595 // le retour des evenement father n'indiquent pas que add ou remove fait
597 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
599 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
600 //SMESH_Gen* gen =_father->GetGen();
601 SMESH_Algo* algo = 0;
603 if (_subShape.ShapeType() == TopAbs_VERTEX )
605 if ( anHyp->GetDim() != 0) {
606 if (event == ADD_HYP || event == ADD_ALGO)
607 return SMESH_Hypothesis::HYP_BAD_DIM;
609 return SMESH_Hypothesis::HYP_OK;
612 else if ( _algoState == HYP_OK ) {
613 // update default _algoState
614 if ( event != REMOVE_FATHER_ALGO )
616 _algoState = NO_ALGO;
619 _algoState = MISSING_HYP;
620 if ( event == REMOVE_FATHER_HYP ||
621 algo->CheckHypothesis(*_father,_subShape, aux_ret))
628 int oldAlgoState = _algoState;
629 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
630 bool needFullClean = false;
632 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
634 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
636 // -------------------------------------------
637 // check if a shape needed by algo is present
638 // -------------------------------------------
639 algo = static_cast< SMESH_Algo* >( anHyp );
640 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
641 return SMESH_Hypothesis::HYP_NEED_SHAPE;
642 // ----------------------
643 // check mesh conformity
644 // ----------------------
645 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
646 return SMESH_Hypothesis::HYP_NOTCONFORM;
648 // check if all-dimensional algo is hidden by other local one
649 if ( event == ADD_ALGO ) {
650 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
651 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
652 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
653 if ( SMESH_Algo * curAlgo = (SMESH_Algo*) _father->GetHypothesis( _subShape, filter, true ))
654 needFullClean = ( !curAlgo->NeedDescretBoundary() );
658 // ----------------------------------
659 // add a hypothesis to DS if possible
660 // ----------------------------------
661 if (event == ADD_HYP || event == ADD_ALGO)
663 if ( ! CanAddHypothesis( anHyp )) // check dimension
664 return SMESH_Hypothesis::HYP_BAD_DIM;
666 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
667 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
669 if ( !meshDS->AddHypothesis(_subShape, anHyp))
670 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
673 // --------------------------
674 // remove a hypothesis from DS
675 // --------------------------
676 if (event == REMOVE_HYP || event == REMOVE_ALGO)
678 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
679 return SMESH_Hypothesis::HYP_OK; // nothing changes
681 if (event == REMOVE_ALGO)
683 algo = dynamic_cast<SMESH_Algo*> (anHyp);
684 if (!algo->NeedDescretBoundary())
686 // clean all mesh in the tree of the current submesh;
687 // we must perform it now because later
688 // we will have no information about the type of the removed algo
689 needFullClean = true;
694 // ------------------
695 // analyse algo state
696 // ------------------
697 if (!isApplicableHyp)
698 return ret; // not applicable hypotheses do not change algo state
703 // ----------------------------------------------------------------------
712 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
713 SetAlgoState(HYP_OK);
714 else if ( algo->IsStatusFatal( aux_ret )) {
715 meshDS->RemoveHypothesis(_subShape, anHyp);
719 SetAlgoState(MISSING_HYP);
726 case ADD_FATHER_ALGO: { // Algo just added in father
729 if ( algo == anHyp ) {
730 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
731 SetAlgoState(HYP_OK);
733 SetAlgoState(MISSING_HYP);
737 case REMOVE_FATHER_HYP:
739 case REMOVE_FATHER_ALGO: {
743 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
744 SetAlgoState(HYP_OK);
746 SetAlgoState(MISSING_HYP);
750 case MODIF_HYP: break;
757 // ----------------------------------------------------------------------
765 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
766 SetAlgoState(HYP_OK);
767 if (SMESH_Hypothesis::IsStatusFatal( ret ))
768 meshDS->RemoveHypothesis(_subShape, anHyp);
769 else if (!_father->IsUsedHypothesis( anHyp, this ))
771 meshDS->RemoveHypothesis(_subShape, anHyp);
772 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
776 case ADD_ALGO: { //already existing algo : on father ?
779 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
780 SetAlgoState(HYP_OK);
781 else if ( algo->IsStatusFatal( aux_ret )) {
782 meshDS->RemoveHypothesis(_subShape, anHyp);
786 SetAlgoState(MISSING_HYP);
791 case REMOVE_ALGO: { // perhaps a father algo applies ?
793 if (algo == NULL) // no more algo applying on sub-shape...
795 SetAlgoState(NO_ALGO);
799 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
800 SetAlgoState(HYP_OK);
802 SetAlgoState(MISSING_HYP);
806 case MODIF_HYP: // assigned hypothesis value may become good
807 case ADD_FATHER_HYP: {
810 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
811 SetAlgoState(HYP_OK);
813 SetAlgoState(MISSING_HYP);
816 case ADD_FATHER_ALGO: { // new father algo
819 if ( algo == anHyp ) {
820 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
821 SetAlgoState(HYP_OK);
823 SetAlgoState(MISSING_HYP);
827 case REMOVE_FATHER_HYP: // nothing to do
829 case REMOVE_FATHER_ALGO: {
831 if (algo == NULL) // no more applying algo on father
833 SetAlgoState(NO_ALGO);
837 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
838 SetAlgoState(HYP_OK);
840 SetAlgoState(MISSING_HYP);
850 // ----------------------------------------------------------------------
858 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
860 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
861 // ret should be fatal: anHyp was not added
862 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
864 else if (!_father->IsUsedHypothesis( anHyp, this ))
865 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
867 if (SMESH_Hypothesis::IsStatusFatal( ret ))
869 MESSAGE("do not add extra hypothesis");
870 meshDS->RemoveHypothesis(_subShape, anHyp);
878 case ADD_ALGO: { //already existing algo : on father ?
880 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
881 // check if algo changes
883 f.Init( SMESH_HypoFilter::IsAlgo() );
884 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
885 f.AndNot( SMESH_HypoFilter::Is( algo ));
886 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
888 string(algo->GetName()) != string(prevAlgo->GetName()) )
892 SetAlgoState(MISSING_HYP);
898 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
899 SetAlgoState(HYP_OK);
901 SetAlgoState(MISSING_HYP);
905 case REMOVE_ALGO: { // perhaps a father algo applies ?
907 if (algo == NULL) // no more algo applying on sub-shape...
909 SetAlgoState(NO_ALGO);
913 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
914 // check if algo remains
915 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
919 SetAlgoState(MISSING_HYP);
923 case MODIF_HYP: // hypothesis value may become bad
924 case ADD_FATHER_HYP: { // new father hypothesis ?
927 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
929 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
933 SetAlgoState(MISSING_HYP);
936 case ADD_FATHER_ALGO: {
938 if ( algo == anHyp ) { // a new algo on father
939 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
940 // check if algo changes
942 f.Init( SMESH_HypoFilter::IsAlgo() );
943 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
944 f.AndNot( SMESH_HypoFilter::Is( algo ));
945 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
947 string(algo->GetName()) != string(prevAlgo->GetName()) )
951 SetAlgoState(MISSING_HYP);
955 case REMOVE_FATHER_HYP: {
958 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
959 // is there the same local hyp or maybe a new father algo applied?
960 if ( !GetSimilarAttached( _subShape, anHyp ) )
964 SetAlgoState(MISSING_HYP);
967 case REMOVE_FATHER_ALGO: {
968 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
969 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
970 algo = dynamic_cast<SMESH_Algo*> (anHyp);
971 if (!algo->NeedDescretBoundary())
972 needFullClean = true;
975 if (algo == NULL) // no more applying algo on father
977 SetAlgoState(NO_ALGO);
981 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
982 // check if algo changes
983 if ( string(algo->GetName()) != string( anHyp->GetName()) )
987 SetAlgoState(MISSING_HYP);
997 // ----------------------------------------------------------------------
1004 // detect algorithm hiding
1006 if ( ret == SMESH_Hypothesis::HYP_OK &&
1007 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
1008 algo->GetName() == anHyp->GetName() )
1011 SMESH_Gen* gen = _father->GetGen();
1012 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1013 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
1014 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
1015 if ( !upperAlgo->NeedDescretBoundary() && !upperAlgo->SupportSubmeshes())
1016 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1019 if ( ret == SMESH_Hypothesis::HYP_OK &&
1020 !algo->NeedDescretBoundary() &&
1021 !algo->SupportSubmeshes()) {
1022 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1023 gen->GetAlgo( *_father, _subShape, &algoAssignedTo );
1024 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1025 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1026 if ( gen->GetAlgo( *_father, i_sm->second->_subShape, &otherAssignedTo ) &&
1027 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1028 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1032 bool stateChange = ( _algoState != oldAlgoState );
1034 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1035 algo->SetEventListener( this );
1037 NotifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1039 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1040 DeleteOwnListeners();
1041 SetIsAlwaysComputed( false );
1042 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1043 // restore default states
1044 _algoState = HYP_OK;
1045 _computeState = READY_TO_COMPUTE;
1049 if ( needFullClean ) {
1050 // added or removed algo is all-dimensional
1051 ComputeStateEngine( CLEAN );
1053 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1056 if (stateChange || modifiedHyp)
1057 ComputeStateEngine(MODIF_ALGO_STATE);
1062 //=======================================================================
1063 //function : IsConform
1064 //purpose : check if a conform mesh will be produced by the Algo
1065 //=======================================================================
1067 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1069 // MESSAGE( "SMESH_subMesh::IsConform" );
1070 if ( !theAlgo ) return false;
1072 // Suppose that theAlgo is applicable to _subShape, do not check it here
1073 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1075 // check only algo that doesn't NeedDescretBoundary(): because mesh made
1076 // on a sub-shape will be ignored by theAlgo
1077 if ( theAlgo->NeedDescretBoundary() ||
1078 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1081 SMESH_Gen* gen =_father->GetGen();
1083 // only local algo is to be checked
1084 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1085 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1088 // check algo attached to adjacent shapes
1090 // loop on one level down sub-meshes
1091 TopoDS_Iterator itsub( _subShape );
1092 for (; itsub.More(); itsub.Next())
1094 // loop on adjacent subShapes
1095 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1096 for (; it.More(); it.Next())
1098 const TopoDS_Shape& adjacent = it.Value();
1099 if ( _subShape.IsSame( adjacent )) continue;
1100 if ( adjacent.ShapeType() != _subShape.ShapeType())
1103 // check algo attached to smAdjacent
1104 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1106 !algo->NeedDescretBoundary() &&
1107 algo->OnlyUnaryInput())
1108 return false; // NOT CONFORM MESH WILL BE PRODUCED
1115 //=============================================================================
1119 //=============================================================================
1121 void SMESH_subMesh::SetAlgoState(int state)
1126 //=============================================================================
1130 //=============================================================================
1131 SMESH_Hypothesis::Hypothesis_Status
1132 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1133 SMESH_Hypothesis * anHyp)
1135 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1136 //EAP: a wire (dim==1) should notify edges (dim==1)
1137 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1138 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1140 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1141 while ( smIt->more() ) {
1142 SMESH_Hypothesis::Hypothesis_Status ret2 =
1143 smIt->next()->AlgoStateEngine(event, anHyp);
1151 //=============================================================================
1155 //=============================================================================
1157 void SMESH_subMesh::CleanDependsOn()
1159 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1160 while ( smIt->more() )
1161 smIt->next()->ComputeStateEngine(CLEAN);
1164 //=============================================================================
1168 //=============================================================================
1170 void SMESH_subMesh::DumpAlgoState(bool isMain)
1172 int dim = SMESH_Gen::GetShapeDim(_subShape);
1173 // if (dim < 1) return;
1176 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1178 map < int, SMESH_subMesh * >::const_iterator itsub;
1179 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1181 SMESH_subMesh *sm = (*itsub).second;
1182 sm->DumpAlgoState(false);
1185 int type = _subShape.ShapeType();
1186 MESSAGE("dim = " << dim << " type of shape " << type);
1190 MESSAGE(" AlgoState = NO_ALGO");
1193 MESSAGE(" AlgoState = MISSING_HYP");
1196 MESSAGE(" AlgoState = HYP_OK");
1199 switch (_computeState)
1202 MESSAGE(" ComputeState = NOT_READY");
1204 case READY_TO_COMPUTE:
1205 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1208 MESSAGE(" ComputeState = COMPUTE_OK");
1210 case FAILED_TO_COMPUTE:
1211 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1216 //================================================================================
1218 * \brief Remove nodes and elements bound to submesh
1219 * \param subMesh - submesh containing nodes and elements
1221 //================================================================================
1223 static void cleanSubMesh( SMESH_subMesh * subMesh )
1226 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1227 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1228 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1229 while (ite->more()) {
1230 const SMDS_MeshElement * elt = ite->next();
1231 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1232 //meshDS->RemoveElement(elt);
1233 meshDS->RemoveFreeElement(elt, subMeshDS);
1236 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1237 while (itn->more()) {
1238 const SMDS_MeshNode * node = itn->next();
1239 //MESSAGE( " RM node: "<<node->GetID());
1240 if ( node->NbInverseElements() == 0 )
1241 meshDS->RemoveFreeNode(node, subMeshDS);
1242 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1243 meshDS->RemoveNode(node);
1249 //=============================================================================
1253 //=============================================================================
1255 bool SMESH_subMesh::ComputeStateEngine(int event)
1257 _computeError.reset();
1259 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1260 //SCRUTE(_computeState);
1263 if (_subShape.ShapeType() == TopAbs_VERTEX)
1265 _computeState = READY_TO_COMPUTE;
1266 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1267 if ( smDS && smDS->NbNodes() ) {
1268 if ( event == CLEAN ) {
1270 cleanSubMesh( this );
1273 _computeState = COMPUTE_OK;
1275 else if ( event == COMPUTE && !_alwaysComputed ) {
1276 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1277 gp_Pnt P = BRep_Tool::Pnt(V);
1278 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1279 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1280 _computeState = COMPUTE_OK;
1283 if ( event == MODIF_ALGO_STATE )
1287 SMESH_Gen *gen = _father->GetGen();
1288 SMESH_Algo *algo = 0;
1290 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1291 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1293 switch (_computeState)
1296 // ----------------------------------------------------------------------
1301 case MODIF_ALGO_STATE:
1303 if (algo && !algo->NeedDescretBoundary())
1304 CleanDependsOn(); // clean sub-meshes with event CLEAN
1305 if ( _algoState == HYP_OK )
1306 _computeState = READY_TO_COMPUTE;
1308 case COMPUTE: // nothing to do
1310 #ifdef WITH_SMESH_CANCEL_COMPUTE
1311 case COMPUTE_CANCELED: // nothing to do
1316 RemoveSubMeshElementsAndNodes();
1318 case SUBMESH_COMPUTED: // nothing to do
1320 case SUBMESH_RESTORED:
1321 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1323 case MESH_ENTITY_REMOVED:
1325 case CHECK_COMPUTE_STATE:
1326 if ( IsMeshComputed() )
1327 _computeState = COMPUTE_OK;
1335 // ----------------------------------------------------------------------
1337 case READY_TO_COMPUTE:
1340 case MODIF_ALGO_STATE:
1341 _computeState = NOT_READY;
1345 if (!algo->NeedDescretBoundary())
1346 CleanDependsOn(); // clean sub-meshes with event CLEAN
1347 if ( _algoState == HYP_OK )
1348 _computeState = READY_TO_COMPUTE;
1355 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1358 MESSAGE("***** verify compute state *****");
1359 _computeState = NOT_READY;
1360 SetAlgoState(MISSING_HYP);
1363 TopoDS_Shape shape = _subShape;
1364 // check submeshes needed
1365 if (_father->HasShapeToMesh() ) {
1366 bool subComputed = false;
1367 if (!algo->OnlyUnaryInput())
1368 shape = GetCollection( gen, algo, subComputed );
1370 subComputed = SubMeshesComputed();
1371 ret = ( algo->NeedDescretBoundary() ? subComputed :
1372 algo->SupportSubmeshes() ? true :
1373 ( !subComputed || _father->IsNotConformAllowed() ));
1375 _computeState = FAILED_TO_COMPUTE;
1376 if ( !algo->NeedDescretBoundary() )
1378 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1379 "Unexpected computed submesh",algo);
1384 // CleanDependants(); for "UseExisting_*D" algos
1385 // RemoveSubMeshElementsAndNodes();
1387 _computeState = FAILED_TO_COMPUTE;
1388 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1390 #if OCC_VERSION_LARGE > 0x06010000
1393 algo->InitComputeError();
1394 MemoryReserve aMemoryReserve;
1395 SMDS_Mesh::CheckMemory();
1396 Kernel_Utils::Localizer loc;
1397 if ( !_father->HasShapeToMesh() ) // no shape
1399 SMESH_MesherHelper helper( *_father );
1400 helper.SetSubShape( shape );
1401 helper.SetElementsOnShape( true );
1402 ret = algo->Compute(*_father, &helper );
1406 ret = algo->Compute((*_father), shape);
1408 if ( !_computeError || ( !ret && _computeError->IsOK() ) ) // algo can set _computeError of submesh
1409 _computeError = algo->GetComputeError();
1411 catch ( ::SMESH_ComputeError& comperr ) {
1412 cout << " SMESH_ComputeError caught" << endl;
1413 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1414 *_computeError = comperr;
1416 catch ( std::bad_alloc& exc ) {
1417 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1418 if ( _computeError ) {
1419 _computeError->myName = COMPERR_MEMORY_PB;
1420 //_computeError->myComment = exc.what();
1422 cleanSubMesh( this );
1425 catch ( Standard_OutOfMemory& exc ) {
1426 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1427 if ( _computeError ) {
1428 _computeError->myName = COMPERR_MEMORY_PB;
1429 //_computeError->myComment = exc.what();
1431 cleanSubMesh( this );
1432 throw std::bad_alloc();
1434 catch (Standard_Failure& ex) {
1435 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1436 _computeError->myName = COMPERR_OCC_EXCEPTION;
1437 _computeError->myComment += ex.DynamicType()->Name();
1438 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1439 _computeError->myComment += ": ";
1440 _computeError->myComment += ex.GetMessageString();
1443 catch ( SALOME_Exception& S_ex ) {
1444 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1445 _computeError->myName = COMPERR_SLM_EXCEPTION;
1446 _computeError->myComment = S_ex.what();
1448 catch ( std::exception& exc ) {
1449 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1450 _computeError->myName = COMPERR_STD_EXCEPTION;
1451 _computeError->myComment = exc.what();
1454 if ( _computeError )
1455 _computeError->myName = COMPERR_EXCEPTION;
1459 TopExp_Explorer subS(shape, _subShape.ShapeType());
1460 if (ret) // check if anything was built
1462 for (; ret && subS.More(); subS.Next())
1463 ret = _father->GetSubMesh( subS.Current() )->IsMeshComputed();
1465 bool isComputeErrorSet = !CheckComputeError( algo, shape );
1466 if (!ret && !isComputeErrorSet)
1468 // Set _computeError
1469 for (subS.ReInit(); subS.More(); subS.Next())
1471 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1472 if ( !sm->IsMeshComputed() )
1474 if ( !sm->_computeError )
1475 sm->_computeError = SMESH_ComputeError::New();
1476 if ( sm->_computeError->IsOK() )
1477 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1478 sm->_computeState = FAILED_TO_COMPUTE;
1479 sm->_computeError->myAlgo = algo;
1483 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1485 _computeError.reset();
1487 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1490 #ifdef WITH_SMESH_CANCEL_COMPUTE
1491 case COMPUTE_CANCELED: // nothing to do
1496 RemoveSubMeshElementsAndNodes();
1497 _computeState = NOT_READY;
1501 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1503 _computeState = READY_TO_COMPUTE;
1505 SetAlgoState(MISSING_HYP);
1508 case SUBMESH_COMPUTED: // nothing to do
1510 case SUBMESH_RESTORED:
1511 // check if a mesh is already computed that may
1512 // happen after retrieval from a file
1513 ComputeStateEngine( CHECK_COMPUTE_STATE );
1514 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1516 if (algo) algo->SubmeshRestored( this );
1518 case MESH_ENTITY_REMOVED:
1520 case CHECK_COMPUTE_STATE:
1521 if ( IsMeshComputed() )
1522 _computeState = COMPUTE_OK;
1530 // ----------------------------------------------------------------------
1535 case MODIF_ALGO_STATE:
1536 ComputeStateEngine( CLEAN );
1538 if (algo && !algo->NeedDescretBoundary())
1539 CleanDependsOn(); // clean sub-meshes with event CLEAN
1541 case COMPUTE: // nothing to do
1543 #ifdef WITH_SMESH_CANCEL_COMPUTE
1544 case COMPUTE_CANCELED: // nothing to do
1548 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1549 RemoveSubMeshElementsAndNodes();
1550 _computeState = NOT_READY;
1551 if ( _algoState == HYP_OK )
1552 _computeState = READY_TO_COMPUTE;
1554 case SUBMESH_COMPUTED: // nothing to do
1556 case SUBMESH_RESTORED:
1557 ComputeStateEngine( CHECK_COMPUTE_STATE );
1558 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1560 if (algo) algo->SubmeshRestored( this );
1562 case MESH_ENTITY_REMOVED:
1563 UpdateDependantsState( CHECK_COMPUTE_STATE );
1564 ComputeStateEngine( CHECK_COMPUTE_STATE );
1565 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1567 case CHECK_COMPUTE_STATE:
1568 if ( !IsMeshComputed() ) {
1569 if (_algoState == HYP_OK)
1570 _computeState = READY_TO_COMPUTE;
1572 _computeState = NOT_READY;
1581 // ----------------------------------------------------------------------
1583 case FAILED_TO_COMPUTE:
1586 case MODIF_ALGO_STATE:
1588 ComputeStateEngine( CLEAN );
1590 if (algo && !algo->NeedDescretBoundary())
1591 CleanDependsOn(); // clean sub-meshes with event CLEAN
1592 if (_algoState == HYP_OK)
1593 _computeState = READY_TO_COMPUTE;
1595 _computeState = NOT_READY;
1597 case COMPUTE: // nothing to do
1599 #ifdef WITH_SMESH_CANCEL_COMPUTE
1600 case COMPUTE_CANCELED:
1603 algo->CancelCompute();
1608 CleanDependants(); // submeshes dependent on me should be cleaned as well
1609 RemoveSubMeshElementsAndNodes();
1611 case SUBMESH_COMPUTED: // allow retry compute
1612 if (_algoState == HYP_OK)
1613 _computeState = READY_TO_COMPUTE;
1615 _computeState = NOT_READY;
1617 case SUBMESH_RESTORED:
1618 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1620 case MESH_ENTITY_REMOVED:
1622 case CHECK_COMPUTE_STATE:
1623 if ( IsMeshComputed() )
1624 _computeState = COMPUTE_OK;
1626 if (_algoState == HYP_OK)
1627 _computeState = READY_TO_COMPUTE;
1629 _computeState = NOT_READY;
1637 // ----------------------------------------------------------------------
1643 NotifyListenersOnEvent( event, COMPUTE_EVENT );
1649 //=============================================================================
1653 //=============================================================================
1655 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1657 _computeError.reset();
1661 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1662 vector<int> aVec(SMDSEntity_Last,0);
1663 aVec[SMDSEntity_Node] = 1;
1664 aResMap.insert(make_pair(this,aVec));
1668 //SMESH_Gen *gen = _father->GetGen();
1669 SMESH_Algo *algo = 0;
1670 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1673 if(algo && !aResMap.count(this) )
1675 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1676 if (!ret) return false;
1678 if (_father->HasShapeToMesh() && algo->NeedDescretBoundary())
1680 // check submeshes needed
1681 bool subMeshEvaluated = true;
1682 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1683 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1684 while ( smIt->more() && subMeshEvaluated )
1686 SMESH_subMesh* sm = smIt->next();
1687 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1688 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1689 const vector<int> & nbs = aResMap[ sm ];
1690 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1692 if ( !subMeshEvaluated )
1695 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1696 ret = algo->Evaluate((*_father), _subShape, aResMap);
1698 aResMap.insert( make_pair( this,vector<int>(0)));
1705 //=======================================================================
1707 * \brief Update compute_state by _computeError and send proper events to
1708 * dependent submeshes
1709 * \retval bool - true if _computeError is NOT set
1711 //=======================================================================
1713 bool SMESH_subMesh::CheckComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape)
1715 bool noErrors = true;
1717 if ( !theShape.IsNull() )
1719 // Check state of submeshes
1720 if ( !theAlgo->NeedDescretBoundary())
1722 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1723 while ( smIt->more() )
1724 if ( !smIt->next()->CheckComputeError( theAlgo ))
1728 // Check state of neighbours
1729 if ( !theAlgo->OnlyUnaryInput() &&
1730 theShape.ShapeType() == TopAbs_COMPOUND &&
1731 !theShape.IsSame( _subShape ))
1733 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1734 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1736 if ( !sm->CheckComputeError( theAlgo, sm->GetSubShape() ))
1738 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1745 if ( !_computeError || _computeError->IsOK() )
1747 // no error description is set to this sub-mesh, check if any mesh is computed
1748 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1752 if ( !_computeError->myAlgo )
1753 _computeError->myAlgo = theAlgo;
1757 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1758 if (_computeError->IsCommon() )
1759 text << _computeError->CommonName();
1761 text << _computeError->myName;
1762 if ( _computeError->myComment.size() > 0 )
1763 text << " \"" << _computeError->myComment << "\"";
1767 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1775 //=======================================================================
1776 //function : UpdateSubMeshState
1778 //=======================================================================
1780 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1782 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1783 while ( smIt->more() )
1784 smIt->next()->_computeState = theState;
1787 //=======================================================================
1788 //function : ComputeSubMeshStateEngine
1790 //=======================================================================
1792 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1794 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1795 while ( smIt->more() )
1796 smIt->next()->ComputeStateEngine(event);
1799 //=======================================================================
1800 //function : UpdateDependantsState
1802 //=======================================================================
1804 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1806 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1807 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1808 for (; it.More(); it.Next())
1810 const TopoDS_Shape& ancestor = it.Value();
1811 SMESH_subMesh *aSubMesh =
1812 _father->GetSubMeshContaining(ancestor);
1814 aSubMesh->ComputeStateEngine( theEvent );
1818 //=============================================================================
1822 //=============================================================================
1824 void SMESH_subMesh::CleanDependants()
1826 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1828 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1829 for (; it.More(); it.Next())
1831 const TopoDS_Shape& ancestor = it.Value();
1832 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1833 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1834 // will erase mesh on other shapes in a compound
1835 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1836 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1838 !aSubMesh->IsEmpty() ) // prevent infinite CLEAN via event lesteners
1839 aSubMesh->ComputeStateEngine(CLEAN);
1845 //=============================================================================
1849 //=============================================================================
1851 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1853 //SCRUTE(_subShape.ShapeType());
1855 cleanSubMesh( this );
1857 // algo may bind a submesh not to _subShape, eg 3D algo
1858 // sets nodes on SHELL while _subShape may be SOLID
1860 int dim = SMESH_Gen::GetShapeDim( _subShape );
1861 int type = _subShape.ShapeType() + 1;
1862 for ( ; type <= TopAbs_EDGE; type++) {
1863 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1865 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1866 for ( ; exp.More(); exp.Next() )
1867 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1874 //=======================================================================
1875 //function : GetCollection
1876 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1877 // meshed at once along with _subShape
1878 //=======================================================================
1880 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen,
1881 SMESH_Algo* theAlgo,
1882 bool & theSubComputed)
1884 MESSAGE("SMESH_subMesh::GetCollection");
1886 theSubComputed = SubMeshesComputed();
1888 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1890 if ( mainShape.IsSame( _subShape ))
1893 const bool ignoreAuxiliaryHyps = false;
1894 list<const SMESHDS_Hypothesis*> aUsedHyp =
1895 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1897 // put in a compound all shapes with the same hypothesis assigned
1898 // and a good ComputState
1900 TopoDS_Compound aCompound;
1901 BRep_Builder aBuilder;
1902 aBuilder.MakeCompound( aCompound );
1904 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1905 for ( ; anExplorer.More(); anExplorer.Next() )
1907 const TopoDS_Shape& S = anExplorer.Current();
1908 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1909 if ( subMesh == this )
1911 aBuilder.Add( aCompound, S );
1913 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1915 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1916 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
1917 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
1918 aBuilder.Add( aCompound, S );
1919 if ( !subMesh->SubMeshesComputed() )
1920 theSubComputed = false;
1927 //=======================================================================
1928 //function : GetSimilarAttached
1929 //purpose : return a hypothesis attached to theShape.
1930 // If theHyp is provided, similar but not same hypotheses
1931 // is returned; else only applicable ones having theHypType
1933 //=======================================================================
1935 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1936 const SMESH_Hypothesis * theHyp,
1937 const int theHypType)
1939 SMESH_HypoFilter hypoKind;
1940 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1942 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1943 hypoKind.AndNot( hypoKind.Is( theHyp ));
1944 if ( theHyp->IsAuxiliary() )
1945 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1947 hypoKind.AndNot( hypoKind.IsAuxiliary());
1950 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1953 return _father->GetHypothesis( theShape, hypoKind, false );
1956 //=======================================================================
1957 //function : CheckConcurentHypothesis
1958 //purpose : check if there are several applicable hypothesis attached to
1960 //=======================================================================
1962 SMESH_Hypothesis::Hypothesis_Status
1963 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1965 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1967 // is there local hypothesis on me?
1968 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1969 return SMESH_Hypothesis::HYP_OK;
1972 TopoDS_Shape aPrevWithHyp;
1973 const SMESH_Hypothesis* aPrevHyp = 0;
1974 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1975 for (; it.More(); it.Next())
1977 const TopoDS_Shape& ancestor = it.Value();
1978 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1981 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1983 aPrevWithHyp = ancestor;
1986 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1987 return SMESH_Hypothesis::HYP_CONCURENT;
1989 return SMESH_Hypothesis::HYP_OK;
1992 return SMESH_Hypothesis::HYP_OK;
1995 //================================================================================
1997 * \brief Sets an event listener and its data to a submesh
1998 * \param listener - the listener to store
1999 * \param data - the listener data to store
2000 * \param where - the submesh to store the listener and it's data
2001 * \param deleteListener - if true then the listener will be deleted as
2002 * it is removed from where submesh
2004 * It remembers the submesh where it puts the listener in order to delete
2005 * them when HYP_OK algo_state is lost
2006 * After being set, event listener is notified on each event of where submesh.
2008 //================================================================================
2010 void SMESH_subMesh::SetEventListener(EventListener* listener,
2011 EventListenerData* data,
2012 SMESH_subMesh* where)
2014 if ( listener && where ) {
2015 where->SetEventListener( listener, data );
2016 myOwnListeners.push_back( make_pair( where, listener ));
2020 //================================================================================
2022 * \brief Sets an event listener and its data to a submesh
2023 * \param listener - the listener to store
2024 * \param data - the listener data to store
2026 * After being set, event listener is notified on each event of a submesh.
2028 //================================================================================
2030 void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
2032 map< EventListener*, EventListenerData* >::iterator l_d =
2033 myEventListeners.find( listener );
2034 if ( l_d != myEventListeners.end() ) {
2035 EventListenerData* curData = l_d->second;
2036 if ( curData && curData != data && curData->IsDeletable() )
2041 myEventListeners.insert( make_pair( listener, data ));
2044 //================================================================================
2046 * \brief Return an event listener data
2047 * \param listener - the listener whose data is
2048 * \retval EventListenerData* - found data, maybe NULL
2050 //================================================================================
2052 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
2054 map< EventListener*, EventListenerData* >::const_iterator l_d =
2055 myEventListeners.find( listener );
2056 if ( l_d != myEventListeners.end() )
2061 //================================================================================
2063 * \brief Notify stored event listeners on the occured event
2064 * \param event - algo_event or compute_event itself
2065 * \param eventType - algo_event or compute_event
2066 * \param hyp - hypothesis, if eventType is algo_event
2068 //================================================================================
2070 void SMESH_subMesh::NotifyListenersOnEvent( const int event,
2071 const event_type eventType,
2072 SMESH_Hypothesis* hyp)
2074 map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
2075 for ( ; l_d != myEventListeners.end(); ++l_d )
2077 std::pair< EventListener*, EventListenerData* > li_da = *l_d; /* copy to enable removal
2080 its ProcessEvent() */
2081 if ( li_da.first->myBusySM.insert( this ).second )
2083 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2084 li_da.first->myBusySM.erase( this );
2089 //================================================================================
2091 * \brief Unregister the listener and delete listener's data
2092 * \param listener - the event listener
2094 //================================================================================
2096 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2098 map< EventListener*, EventListenerData* >::iterator l_d =
2099 myEventListeners.find( listener );
2100 if ( l_d != myEventListeners.end() ) {
2101 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2102 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2103 myEventListeners.erase( l_d );
2107 //================================================================================
2109 * \brief Delete event listeners depending on algo of this submesh
2111 //================================================================================
2113 void SMESH_subMesh::DeleteOwnListeners()
2115 list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
2116 for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
2117 sm_l->first->DeleteEventListener( sm_l->second );
2118 myOwnListeners.clear();
2121 //================================================================================
2123 * \brief Do something on a certain event
2124 * \param event - algo_event or compute_event itself
2125 * \param eventType - algo_event or compute_event
2126 * \param subMesh - the submesh where the event occures
2127 * \param data - listener data stored in the subMesh
2128 * \param hyp - hypothesis, if eventType is algo_event
2130 * The base implementation translates CLEAN event to the subMesh
2131 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2132 * successful COMPUTE event.
2134 //================================================================================
2136 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2137 const int eventType,
2138 SMESH_subMesh* subMesh,
2139 EventListenerData* data,
2140 const SMESH_Hypothesis* /*hyp*/)
2142 if ( data && !data->mySubMeshes.empty() &&
2143 eventType == SMESH_subMesh::COMPUTE_EVENT)
2145 ASSERT( data->mySubMeshes.front() != subMesh );
2146 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2147 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2149 case SMESH_subMesh::CLEAN:
2150 for ( ; smIt != smEnd; ++ smIt)
2151 (*smIt)->ComputeStateEngine( event );
2153 case SMESH_subMesh::COMPUTE:
2154 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2155 for ( ; smIt != smEnd; ++ smIt)
2156 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2165 //================================================================================
2167 * \brief Iterator over submeshes and optionally prepended or appended one
2169 //================================================================================
2171 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2173 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2174 SMESH_subMesh* prepend,
2175 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2177 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2178 if ( myCur == append ) append = 0;
2180 /// Return true if and only if there are other object in this iterator
2185 /// Return the current object and step to the next one
2186 virtual SMESH_subMesh* next()
2188 SMESH_subMesh* res = myCur;
2189 if ( myIt->more() ) { myCur = myIt->next(); }
2190 else { myCur = myAppend; myAppend = 0; }
2197 SMESH_subMesh *myAppend, *myCur;
2198 SMDS_Iterator<SMESH_subMesh*> *myIt;
2202 //================================================================================
2204 * \brief Return iterator on the submeshes this one depends on
2205 * \param includeSelf - this submesh to be returned also
2206 * \param reverse - if true, complex shape submeshes go first
2208 //================================================================================
2210 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2213 SMESH_subMesh *prepend=0, *append=0;
2214 if ( includeSelf ) {
2215 if ( reverse ) prepend = this;
2218 typedef map < int, SMESH_subMesh * > TMap;
2221 return SMESH_subMeshIteratorPtr
2222 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2225 return SMESH_subMeshIteratorPtr
2226 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));
2230 //================================================================================
2232 * \brief Find common submeshes (based on shared sub-shapes with other
2233 * \param theOther submesh to check
2234 * \param theSetOfCommon set of common submesh
2236 //================================================================================
2238 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2239 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2241 int oldNb = theSetOfCommon.size();
2242 // check main submeshes
2243 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2244 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2245 theSetOfCommon.insert( this );
2246 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2247 theSetOfCommon.insert( theOther );
2248 // check common submeshes
2249 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2250 for( ; mapIt != _mapDepend.end(); mapIt++ )
2251 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2252 theSetOfCommon.insert( (*mapIt).second );
2253 return oldNb < theSetOfCommon.size();