1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 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
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
29 #include "SMESH_subMesh.hxx"
31 #include "SMESH_Algo.hxx"
32 #include "SMESH_Gen.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMESH_Hypothesis.hxx"
35 #include "SMESH_Mesh.hxx"
36 #include "SMESH_MesherHelper.hxx"
37 #include "SMESH_subMeshEventListener.hxx"
38 #include "SMESH_Comment.hxx"
39 #include "SMDS_SetIterator.hxx"
41 #include "utilities.h"
44 #include <BRep_Builder.hxx>
45 #include <BRep_Tool.hxx>
47 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_ListOfShape.hxx>
51 #include <TopTools_MapOfShape.hxx>
53 #include <TopoDS_Compound.hxx>
56 #include <Standard_OutOfMemory.hxx>
57 #include <Standard_ErrorHandler.hxx>
61 //=============================================================================
63 * \brief Allocate some memory at construction and release it at destruction.
64 * Is used to be able to continue working after mesh generation breaks due to
67 //=============================================================================
72 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
73 ~MemoryReserve() { delete [] myBuf; }
76 //=============================================================================
78 * default constructor:
80 //=============================================================================
82 SMESH_subMesh::SMESH_subMesh(int Id,
84 SMESHDS_Mesh * meshDS,
85 const TopoDS_Shape & aSubShape)
87 _subShape = aSubShape;
88 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
91 _dependenceAnalysed = _alwaysComputed = false;
93 if (_subShape.ShapeType() == TopAbs_VERTEX)
96 _computeState = READY_TO_COMPUTE;
100 _algoState = NO_ALGO;
101 _computeState = NOT_READY;
105 //=============================================================================
109 //=============================================================================
111 SMESH_subMesh::~SMESH_subMesh()
113 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
115 DeleteOwnListeners();
118 //=============================================================================
122 //=============================================================================
124 int SMESH_subMesh::GetId() const
126 //MESSAGE("SMESH_subMesh::GetId");
130 //=============================================================================
134 //=============================================================================
136 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
138 // submesh appears in DS only when a mesher set nodes and elements on a shape
139 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
142 //=============================================================================
146 //=============================================================================
148 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
150 if ( !GetSubMeshDS() ) {
151 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
152 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
154 return GetSubMeshDS();
157 //=============================================================================
161 //=============================================================================
163 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
165 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
166 while ( smIt->more() ) {
167 SMESH_subMesh *sm = smIt->next();
168 if ( sm->GetComputeState() == READY_TO_COMPUTE )
171 return 0; // nothing to compute
174 //================================================================================
176 * \brief Allow algo->Compute() if a subshape of lower dim is meshed but
177 * none mesh entity is bound to it (PAL13615, 2nd part)
179 //================================================================================
181 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
183 _alwaysComputed = isAlCo;
184 if ( _alwaysComputed )
185 _computeState = COMPUTE_OK;
187 ComputeStateEngine( CHECK_COMPUTE_STATE );
190 //=======================================================================
191 //function : IsMeshComputed
192 //purpose : check if _subMeshDS contains mesh elements
193 //=======================================================================
195 bool SMESH_subMesh::IsMeshComputed() const
197 if ( _alwaysComputed )
199 // algo may bind a submesh not to _subShape, eg 3D algo
200 // sets nodes on SHELL while _subShape may be SOLID
202 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
203 int dim = SMESH_Gen::GetShapeDim( _subShape );
204 int type = _subShape.ShapeType();
205 for ( ; type <= TopAbs_VERTEX; type++) {
206 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
208 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
209 for ( ; exp.More(); exp.Next() )
211 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
213 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
226 //=============================================================================
230 //=============================================================================
232 bool SMESH_subMesh::SubMeshesComputed()
234 int myDim = SMESH_Gen::GetShapeDim( _subShape );
235 int dimToCheck = myDim - 1;
236 bool subMeshesComputed = true;
237 // check subMeshes with upper dimension => reverse iteration
238 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
239 while ( smIt->more() )
241 SMESH_subMesh *sm = smIt->next();
242 if ( sm->_alwaysComputed )
244 const TopoDS_Shape & ss = sm->GetSubShape();
245 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
246 // in checking of existence of edges if the algo needs only faces. Moreover,
247 // degenerated edges may have no submesh, as after computing NETGEN_2D.
248 int dim = SMESH_Gen::GetShapeDim( ss );
249 if (dim < dimToCheck)
250 break; // the rest subMeshes are all of less dimension
251 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
252 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
253 (ds && ( ds->NbNodes() || ds->NbElements() )));
256 int type = ss.ShapeType();
258 subMeshesComputed = false;
262 case TopAbs_COMPOUND:
264 MESSAGE("The not computed sub mesh is a COMPOUND");
267 case TopAbs_COMPSOLID:
269 MESSAGE("The not computed sub mesh is a COMPSOLID");
274 MESSAGE("The not computed sub mesh is a SHEL");
279 MESSAGE("The not computed sub mesh is a WIRE");
284 MESSAGE("The not computed sub mesh is a SOLID");
289 MESSAGE("The not computed sub mesh is a FACE");
294 MESSAGE("The not computed sub mesh is a EDGE");
299 MESSAGE("The not computed sub mesh is of unknown type");
307 return subMeshesComputed;
310 //=============================================================================
314 //=============================================================================
316 bool SMESH_subMesh::SubMeshesReady()
318 bool subMeshesReady = true;
319 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
320 while ( smIt->more() ) {
321 SMESH_subMesh *sm = smIt->next();
322 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
323 sm->GetComputeState() == READY_TO_COMPUTE);
326 subMeshesReady = false;
331 return subMeshesReady;
334 //=============================================================================
336 * Construct dependence on first level subMeshes. complex shapes (compsolid,
337 * shell, wire) are not analysed the same way as simple shapes (solid, face,
339 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
340 * with possible multiples occurences. Multiples occurences corresponds to
341 * internal frontiers within shapes of the collection and must not be keeped.
342 * See FinalizeDependence.
344 //=============================================================================
346 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
348 if (_dependenceAnalysed)
351 //MESSAGE("SMESH_subMesh::DependsOn");
353 int type = _subShape.ShapeType();
357 case TopAbs_COMPOUND:
359 //MESSAGE("compound");
360 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
363 InsertDependence(exp.Current());
365 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
368 InsertDependence(exp.Current()); //only shell not in solid
370 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
373 InsertDependence(exp.Current());
375 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
378 InsertDependence(exp.Current());
382 case TopAbs_COMPSOLID:
384 //MESSAGE("compsolid");
385 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
388 InsertDependence(exp.Current());
395 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
398 InsertDependence(exp.Current());
405 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
408 InsertDependence(exp.Current());
415 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
418 InsertDependence(exp.Current());
425 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
428 InsertDependence(exp.Current());
435 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
438 InsertDependence(exp.Current());
451 _dependenceAnalysed = true;
455 //=============================================================================
457 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
459 //=============================================================================
461 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
463 //MESSAGE("SMESH_subMesh::InsertDependence");
464 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
465 int type = aSubShape.ShapeType();
466 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
467 int cle = aSubMesh->GetId();
468 cle += 10000000 * ordType; // sort map by ordType then index
469 if ( _mapDepend.find( cle ) == _mapDepend.end())
471 _mapDepend[cle] = aSubMesh;
472 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
473 _mapDepend.insert( subMap.begin(), subMap.end() );
477 //=============================================================================
481 //=============================================================================
483 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
485 //MESSAGE("SMESH_subMesh::GetSubShape");
490 //=======================================================================
491 //function : CanAddHypothesis
492 //purpose : return true if theHypothesis can be attached to me:
493 // its dimention is checked
494 //=======================================================================
496 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
498 int aHypDim = theHypothesis->GetDim();
499 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
500 if ( aHypDim <= aShapeDim )
502 // if ( aHypDim < aShapeDim )
503 // return ( _father->IsMainShape( _subShape ));
508 //=======================================================================
509 //function : IsApplicableHypotesis
511 //=======================================================================
513 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
514 const TopAbs_ShapeEnum theShapeType)
516 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
518 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
521 switch ( theShapeType ) {
526 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
529 // Special case for algorithms, building 2D mesh on a whole shell.
530 // Before this fix there was a problem after restoring from study,
531 // because in that case algorithm is assigned before hypothesis
532 // (on shell in problem case) and hypothesis is checked on faces
533 // (because it is 2D), where we have NO_ALGO state.
534 // Now 2D hypothesis is also applicable to shells.
535 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
538 // case TopAbs_COMPSOLID:
539 // case TopAbs_COMPOUND:
545 //=============================================================================
549 //=============================================================================
551 SMESH_Hypothesis::Hypothesis_Status
552 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
554 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
555 //SCRUTE(_algoState);
558 // **** les retour des evenement shape sont significatifs
559 // (add ou remove fait ou non)
560 // le retour des evenement father n'indiquent pas que add ou remove fait
562 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
564 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
565 SMESH_Gen* gen =_father->GetGen();
566 SMESH_Algo* algo = 0;
568 if (_subShape.ShapeType() == TopAbs_VERTEX )
570 if ( anHyp->GetDim() != 0) {
571 if (event == ADD_HYP || event == ADD_ALGO)
572 return SMESH_Hypothesis::HYP_BAD_DIM;
574 return SMESH_Hypothesis::HYP_OK;
577 else if ( _algoState == HYP_OK ) {
578 // update default _algoState
579 if ( event != REMOVE_FATHER_ALGO )
581 _algoState = NO_ALGO;
582 algo = gen->GetAlgo(*_father, _subShape);
584 _algoState = MISSING_HYP;
585 if ( event == REMOVE_FATHER_HYP ||
586 algo->CheckHypothesis(*_father,_subShape, aux_ret))
593 int oldAlgoState = _algoState;
594 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
596 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
598 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
600 // -------------------------------------------
601 // check if a shape needed by algo is present
602 // -------------------------------------------
603 algo = static_cast< SMESH_Algo* >( anHyp );
604 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
605 return SMESH_Hypothesis::HYP_BAD_GEOMETRY;
606 // ----------------------
607 // check mesh conformity
608 // ----------------------
609 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
610 return SMESH_Hypothesis::HYP_NOTCONFORM;
613 // ----------------------------------
614 // add a hypothesis to DS if possible
615 // ----------------------------------
616 if (event == ADD_HYP || event == ADD_ALGO)
618 if ( ! CanAddHypothesis( anHyp )) // check dimension
619 return SMESH_Hypothesis::HYP_BAD_DIM;
621 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
622 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
624 if ( !meshDS->AddHypothesis(_subShape, anHyp))
625 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
627 // Serve Propagation of 1D hypothesis
628 // NOTE: it is possible to re-implement Propagation using EventListener
629 if (event == ADD_HYP) {
630 bool isPropagationOk = true;
631 bool isPropagationHyp = ( strcmp( "Propagation", anHyp->GetName() ) == 0 );
633 if ( isPropagationHyp ) {
634 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
635 TopTools_MapOfShape aMap;
636 for (; exp.More(); exp.Next()) {
637 if (aMap.Add(exp.Current())) {
638 if (!_father->BuildPropagationChain(exp.Current())) {
639 isPropagationOk = false;
644 else if (anHyp->GetDim() == 1) { // Only 1D hypothesis can be propagated
645 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
646 TopTools_MapOfShape aMap;
647 for (; exp.More(); exp.Next()) {
648 if (aMap.Add(exp.Current())) {
649 TopoDS_Shape aMainEdge;
650 if (_father->IsPropagatedHypothesis(exp.Current(), aMainEdge)) {
651 isPropagationOk = _father->RebuildPropagationChains();
652 } else if (_father->IsPropagationHypothesis(exp.Current())) {
653 isPropagationOk = _father->BuildPropagationChain(exp.Current());
661 if ( isPropagationOk ) {
662 if ( isPropagationHyp )
663 return ret; // nothing more to do for "Propagation" hypothesis
665 else if ( ret < SMESH_Hypothesis::HYP_CONCURENT) {
666 ret = SMESH_Hypothesis::HYP_CONCURENT;
668 } // Serve Propagation of 1D hypothesis
671 // --------------------------
672 // remove a hypothesis from DS
673 // --------------------------
674 if (event == REMOVE_HYP || event == REMOVE_ALGO)
676 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
677 return SMESH_Hypothesis::HYP_OK; // nothing changes
679 // Serve Propagation of 1D hypothesis
680 // NOTE: it is possible to re-implement Propagation using EventListener
681 if (event == REMOVE_HYP)
683 bool isPropagationOk = true;
684 SMESH_HypoFilter propagFilter( SMESH_HypoFilter::HasName( "Propagation" ));
685 bool isPropagationHyp = propagFilter.IsOk( anHyp, _subShape );
687 if ( isPropagationHyp )
689 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
690 TopTools_MapOfShape aMap;
691 for (; exp.More(); exp.Next()) {
692 if (aMap.Add(exp.Current()) &&
693 !_father->GetHypothesis( exp.Current(), propagFilter, true )) {
694 // no more Propagation on the current edge
695 if (!_father->RemovePropagationChain(exp.Current())) {
696 return SMESH_Hypothesis::HYP_UNKNOWN_FATAL;
700 // rebuild propagation chains, because removing one
701 // chain can resolve concurention, existing before
702 isPropagationOk = _father->RebuildPropagationChains();
704 else if (anHyp->GetDim() == 1) // Only 1D hypothesis can be propagated
706 isPropagationOk = _father->RebuildPropagationChains();
709 if ( isPropagationOk ) {
710 if ( isPropagationHyp )
711 return ret; // nothing more to do for "Propagation" hypothesis
713 else if ( ret < SMESH_Hypothesis::HYP_CONCURENT) {
714 ret = SMESH_Hypothesis::HYP_CONCURENT;
716 } // Serve Propagation of 1D hypothesis
717 else // event == REMOVE_ALGO
719 algo = dynamic_cast<SMESH_Algo*> (anHyp);
720 if (!algo->NeedDescretBoundary())
722 // clean all mesh in the tree of the current submesh;
723 // we must perform it now because later
724 // we will have no information about the type of the removed algo
726 ComputeStateEngine( CLEAN );
728 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
733 // ------------------
734 // analyse algo state
735 // ------------------
736 if (!isApplicableHyp)
737 return ret; // not applicable hypotheses do not change algo state
742 // ----------------------------------------------------------------------
749 algo = gen->GetAlgo((*_father), _subShape);
751 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
752 SetAlgoState(HYP_OK);
753 else if ( algo->IsStatusFatal( aux_ret )) {
754 meshDS->RemoveHypothesis(_subShape, anHyp);
758 SetAlgoState(MISSING_HYP);
765 case ADD_FATHER_ALGO: { // Algo just added in father
766 algo = gen->GetAlgo((*_father), _subShape);
768 if ( algo == anHyp ) {
769 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
770 SetAlgoState(HYP_OK);
772 SetAlgoState(MISSING_HYP);
776 case REMOVE_FATHER_HYP:
778 case REMOVE_FATHER_ALGO: {
779 algo = gen->GetAlgo((*_father), _subShape);
782 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
783 SetAlgoState(HYP_OK);
785 SetAlgoState(MISSING_HYP);
789 case MODIF_HYP: break;
796 // ----------------------------------------------------------------------
802 algo = gen->GetAlgo((*_father), _subShape);
804 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
805 SetAlgoState(HYP_OK);
806 if (SMESH_Hypothesis::IsStatusFatal( ret ))
807 meshDS->RemoveHypothesis(_subShape, anHyp);
808 else if (!_father->IsUsedHypothesis( anHyp, this ))
810 meshDS->RemoveHypothesis(_subShape, anHyp);
811 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
815 case ADD_ALGO: { //already existing algo : on father ?
816 algo = gen->GetAlgo((*_father), _subShape);
818 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
819 SetAlgoState(HYP_OK);
820 else if ( algo->IsStatusFatal( aux_ret )) {
821 meshDS->RemoveHypothesis(_subShape, anHyp);
825 SetAlgoState(MISSING_HYP);
830 case REMOVE_ALGO: { // perhaps a father algo applies ?
831 algo = gen->GetAlgo((*_father), _subShape);
832 if (algo == NULL) // no more algo applying on subShape...
834 SetAlgoState(NO_ALGO);
838 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
839 SetAlgoState(HYP_OK);
841 SetAlgoState(MISSING_HYP);
845 case MODIF_HYP: // assigned hypothesis value may become good
846 case ADD_FATHER_HYP: {
847 algo = gen->GetAlgo((*_father), _subShape);
849 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
850 SetAlgoState(HYP_OK);
852 SetAlgoState(MISSING_HYP);
855 case ADD_FATHER_ALGO: { // new father algo
856 algo = gen->GetAlgo((*_father), _subShape);
858 if ( algo == anHyp ) {
859 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
860 SetAlgoState(HYP_OK);
862 SetAlgoState(MISSING_HYP);
866 case REMOVE_FATHER_HYP: // nothing to do
868 case REMOVE_FATHER_ALGO: {
869 algo = gen->GetAlgo((*_father), _subShape);
870 if (algo == NULL) // no more applying algo on father
872 SetAlgoState(NO_ALGO);
876 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
877 SetAlgoState(HYP_OK);
879 SetAlgoState(MISSING_HYP);
889 // ----------------------------------------------------------------------
895 algo = gen->GetAlgo((*_father), _subShape);
897 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
899 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
900 // ret should be fatal: anHyp was not added
901 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
903 else if (!_father->IsUsedHypothesis( anHyp, this ))
904 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
906 if (SMESH_Hypothesis::IsStatusFatal( ret ))
908 MESSAGE("do not add extra hypothesis");
909 meshDS->RemoveHypothesis(_subShape, anHyp);
917 case ADD_ALGO: { //already existing algo : on father ?
918 algo = gen->GetAlgo((*_father), _subShape);
919 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
920 // check if algo changes
922 f.Init( SMESH_HypoFilter::IsAlgo() );
923 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
924 f.AndNot( SMESH_HypoFilter::Is( algo ));
925 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
927 string(algo->GetName()) != string(prevAlgo->GetName()) )
931 SetAlgoState(MISSING_HYP);
935 algo = gen->GetAlgo((*_father), _subShape);
937 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
938 SetAlgoState(HYP_OK);
940 SetAlgoState(MISSING_HYP);
944 case REMOVE_ALGO: { // perhaps a father algo applies ?
945 algo = gen->GetAlgo((*_father), _subShape);
946 if (algo == NULL) // no more algo applying on subShape...
948 SetAlgoState(NO_ALGO);
952 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
953 // check if algo remains
954 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
958 SetAlgoState(MISSING_HYP);
962 case MODIF_HYP: // hypothesis value may become bad
963 case ADD_FATHER_HYP: { // new father hypothesis ?
964 algo = gen->GetAlgo((*_father), _subShape);
966 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
968 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
972 SetAlgoState(MISSING_HYP);
975 case ADD_FATHER_ALGO: {
976 algo = gen->GetAlgo((*_father), _subShape);
977 if ( algo == anHyp ) { // a new algo on father
978 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
979 // check if algo changes
981 f.Init( SMESH_HypoFilter::IsAlgo() );
982 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
983 f.AndNot( SMESH_HypoFilter::Is( algo ));
984 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
986 string(algo->GetName()) != string(prevAlgo->GetName()) )
990 SetAlgoState(MISSING_HYP);
994 case REMOVE_FATHER_HYP: {
995 algo = gen->GetAlgo((*_father), _subShape);
997 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
998 // is there the same local hyp or maybe a new father algo applied?
999 if ( !GetSimilarAttached( _subShape, anHyp ) )
1003 SetAlgoState(MISSING_HYP);
1006 case REMOVE_FATHER_ALGO: {
1007 algo = gen->GetAlgo((*_father), _subShape);
1008 if (algo == NULL) // no more applying algo on father
1010 SetAlgoState(NO_ALGO);
1014 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1015 // check if algo changes
1016 if ( string(algo->GetName()) != string( anHyp->GetName()) )
1020 SetAlgoState(MISSING_HYP);
1030 // ----------------------------------------------------------------------
1037 // detect algorithm hiding
1039 if ( ret == SMESH_Hypothesis::HYP_OK &&
1040 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
1041 algo->GetName() == anHyp->GetName() )
1044 SMESH_Gen* gen = _father->GetGen();
1045 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1046 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && it.More()); it.Next() ) {
1047 if ( SMESH_Algo* upperAlgo = gen->GetAlgo( *_father, it.Value() ))
1048 if ( !upperAlgo->NeedDescretBoundary() )
1049 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1052 if ( ret == SMESH_Hypothesis::HYP_OK && !algo->NeedDescretBoundary() ) {
1053 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1054 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1055 if ( gen->GetAlgo( *_father, i_sm->second->_subShape ))
1056 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1060 bool stateChange = ( _algoState != oldAlgoState );
1062 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1063 algo->SetEventListener( this );
1065 NotifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1067 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1068 DeleteOwnListeners();
1069 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1070 // restore default states
1071 _algoState = HYP_OK;
1072 _computeState = READY_TO_COMPUTE;
1076 if (stateChange || modifiedHyp)
1077 ComputeStateEngine(MODIF_ALGO_STATE);
1082 //=======================================================================
1083 //function : IsConform
1084 //purpose : check if a conform mesh will be produced by the Algo
1085 //=======================================================================
1087 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1089 // MESSAGE( "SMESH_subMesh::IsConform" );
1090 if ( !theAlgo ) return false;
1092 // Suppose that theAlgo is applicable to _subShape, do not check it here
1093 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1095 // check only algo that doesn't NeedDescretBoundary(): because mesh made
1096 // on a sub-shape will be ignored by theAlgo
1097 if ( theAlgo->NeedDescretBoundary() ||
1098 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1101 SMESH_Gen* gen =_father->GetGen();
1103 // only local algo is to be checked
1104 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1105 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1108 // check algo attached to adjacent shapes
1110 // loop on one level down sub-meshes
1111 TopoDS_Iterator itsub( _subShape );
1112 for (; itsub.More(); itsub.Next())
1114 // loop on adjacent subShapes
1115 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
1116 for (; it.More(); it.Next())
1118 const TopoDS_Shape& adjacent = it.Value();
1119 if ( _subShape.IsSame( adjacent )) continue;
1120 if ( adjacent.ShapeType() != _subShape.ShapeType())
1123 // check algo attached to smAdjacent
1124 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1126 !algo->NeedDescretBoundary() &&
1127 algo->OnlyUnaryInput())
1128 return false; // NOT CONFORM MESH WILL BE PRODUCED
1135 //=============================================================================
1139 //=============================================================================
1141 void SMESH_subMesh::SetAlgoState(int state)
1146 //=============================================================================
1150 //=============================================================================
1151 SMESH_Hypothesis::Hypothesis_Status
1152 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1153 SMESH_Hypothesis * anHyp)
1155 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1156 //EAP: a wire (dim==1) should notify edges (dim==1)
1157 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1158 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1160 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1161 while ( smIt->more() ) {
1162 SMESH_Hypothesis::Hypothesis_Status ret2 =
1163 smIt->next()->AlgoStateEngine(event, anHyp);
1171 //=============================================================================
1175 //=============================================================================
1177 void SMESH_subMesh::CleanDependsOn()
1179 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1180 while ( smIt->more() )
1181 smIt->next()->ComputeStateEngine(CLEAN);
1184 //=============================================================================
1188 //=============================================================================
1190 void SMESH_subMesh::DumpAlgoState(bool isMain)
1192 int dim = SMESH_Gen::GetShapeDim(_subShape);
1193 // if (dim < 1) return;
1196 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1198 map < int, SMESH_subMesh * >::const_iterator itsub;
1199 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1201 SMESH_subMesh *sm = (*itsub).second;
1202 sm->DumpAlgoState(false);
1205 int type = _subShape.ShapeType();
1206 MESSAGE("dim = " << dim << " type of shape " << type);
1210 MESSAGE(" AlgoState = NO_ALGO");
1213 MESSAGE(" AlgoState = MISSING_HYP");
1216 MESSAGE(" AlgoState = HYP_OK");
1219 switch (_computeState)
1222 MESSAGE(" ComputeState = NOT_READY");
1224 case READY_TO_COMPUTE:
1225 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1228 MESSAGE(" ComputeState = COMPUTE_OK");
1230 case FAILED_TO_COMPUTE:
1231 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1236 //================================================================================
1238 * \brief Remove nodes and elements bound to submesh
1239 * \param subMesh - submesh containing nodes and elements
1241 //================================================================================
1243 static void cleanSubMesh( SMESH_subMesh * subMesh )
1246 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1247 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1248 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1249 while (ite->more()) {
1250 const SMDS_MeshElement * elt = ite->next();
1251 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1252 //meshDS->RemoveElement(elt);
1253 meshDS->RemoveFreeElement(elt, subMeshDS);
1256 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1257 while (itn->more()) {
1258 const SMDS_MeshNode * node = itn->next();
1259 //MESSAGE( " RM node: "<<node->GetID());
1260 if ( node->NbInverseNodes() == 0 )
1261 meshDS->RemoveFreeNode(node, subMeshDS);
1262 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1263 meshDS->RemoveNode(node);
1269 //=============================================================================
1273 //=============================================================================
1275 bool SMESH_subMesh::ComputeStateEngine(int event)
1277 _computeError.reset();
1279 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1280 //SCRUTE(_computeState);
1283 if (_subShape.ShapeType() == TopAbs_VERTEX)
1285 _computeState = READY_TO_COMPUTE;
1286 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1287 if ( smDS && smDS->NbNodes() ) {
1288 _computeState = COMPUTE_OK;
1290 else if ( event == COMPUTE && !_alwaysComputed ) {
1291 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1292 gp_Pnt P = BRep_Tool::Pnt(V);
1293 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1294 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1295 _computeState = COMPUTE_OK;
1298 if ( event == MODIF_ALGO_STATE )
1302 SMESH_Gen *gen = _father->GetGen();
1303 SMESH_Algo *algo = 0;
1305 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1306 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1308 switch (_computeState)
1311 // ----------------------------------------------------------------------
1316 case MODIF_ALGO_STATE:
1317 algo = gen->GetAlgo((*_father), _subShape);
1318 if (algo && !algo->NeedDescretBoundary())
1319 CleanDependsOn(); // clean sub-meshes with event CLEAN
1320 if ( _algoState == HYP_OK )
1321 _computeState = READY_TO_COMPUTE;
1323 case COMPUTE: // nothing to do
1327 RemoveSubMeshElementsAndNodes();
1329 case SUBMESH_COMPUTED: // nothing to do
1331 case SUBMESH_RESTORED:
1332 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1334 case MESH_ENTITY_REMOVED:
1336 case CHECK_COMPUTE_STATE:
1337 if ( IsMeshComputed() )
1338 _computeState = COMPUTE_OK;
1346 // ----------------------------------------------------------------------
1348 case READY_TO_COMPUTE:
1351 case MODIF_ALGO_STATE:
1352 _computeState = NOT_READY;
1353 algo = gen->GetAlgo((*_father), _subShape);
1356 if (!algo->NeedDescretBoundary())
1357 CleanDependsOn(); // clean sub-meshes with event CLEAN
1358 if ( _algoState == HYP_OK )
1359 _computeState = READY_TO_COMPUTE;
1364 algo = gen->GetAlgo((*_father), _subShape);
1366 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1369 MESSAGE("***** verify compute state *****");
1370 _computeState = NOT_READY;
1371 SetAlgoState(MISSING_HYP);
1374 // check submeshes needed
1375 if (_father->HasShapeToMesh() ) {
1376 bool subComputed = SubMeshesComputed();
1377 ret = ( algo->NeedDescretBoundary() ? subComputed :
1378 ( !subComputed || _father->IsNotConformAllowed() ));
1380 _computeState = FAILED_TO_COMPUTE;
1381 if ( !algo->NeedDescretBoundary() )
1383 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,"Unexpected submesh",algo);
1389 RemoveSubMeshElementsAndNodes();
1391 _computeState = FAILED_TO_COMPUTE;
1392 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1394 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1397 MemoryReserve aMemoryReserve;
1398 algo->InitComputeError();
1399 if ( !_father->HasShapeToMesh() ) // no shape
1401 SMESH_MesherHelper helper( *_father );
1402 helper.SetSubShape( _subShape );
1403 helper.SetElementsOnShape( true );
1404 ret = algo->Compute(*_father, &helper );
1408 if (!algo->NeedDescretBoundary() && !algo->OnlyUnaryInput()) {
1409 ret = ApplyToCollection( algo, GetCollection( gen, algo ) );
1413 ret = algo->Compute((*_father), _subShape);
1417 _computeError = algo->GetComputeError();
1419 catch ( std::bad_alloc& exc ) {
1420 printf("std::bad_alloc\n");
1421 if ( _computeError ) {
1422 _computeError->myName = COMPERR_MEMORY_PB;
1423 //_computeError->myComment = exc.what();
1425 cleanSubMesh( this );
1428 catch ( Standard_OutOfMemory& exc ) {
1429 printf("Standard_OutOfMemory\n");
1430 if ( _computeError ) {
1431 _computeError->myName = COMPERR_MEMORY_PB;
1432 //_computeError->myComment = exc.what();
1434 cleanSubMesh( this );
1435 throw std::bad_alloc();
1437 catch (Standard_Failure& exc) {
1438 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1439 _computeError->myName = COMPERR_OCC_EXCEPTION;
1440 _computeError->myComment = exc.GetMessageString();
1442 catch ( SALOME_Exception& S_ex ) {
1443 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1444 _computeError->myName = COMPERR_SLM_EXCEPTION;
1445 _computeError->myComment = S_ex.what();
1447 catch ( std::exception& exc ) {
1448 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1449 _computeError->myName = COMPERR_STD_EXCEPTION;
1450 _computeError->myComment = exc.what();
1453 if ( _computeError )
1454 _computeError->myName = COMPERR_EXCEPTION;
1458 if ( ret && _computeError && !_computeError->IsOK() ) {
1461 if (ret && !_alwaysComputed) { // check if anything was built
1462 ret = ( GetSubMeshDS() && ( GetSubMeshDS()->NbElements() || GetSubMeshDS()->NbNodes() ));
1466 // Set _computeError
1467 if ( !_computeError )
1468 _computeError = SMESH_ComputeError::New();
1469 if ( _computeError->IsOK() )
1470 _computeError->myName = COMPERR_ALGO_FAILED;
1474 _computeError.reset();
1475 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1477 if ( !algo->NeedDescretBoundary() )
1478 UpdateSubMeshState( ret ? COMPUTE_OK : FAILED_TO_COMPUTE );
1479 CheckComputeError( algo );
1484 RemoveSubMeshElementsAndNodes();
1485 _computeState = NOT_READY;
1486 algo = gen->GetAlgo((*_father), _subShape);
1489 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1491 _computeState = READY_TO_COMPUTE;
1493 SetAlgoState(MISSING_HYP);
1496 case SUBMESH_COMPUTED: // nothing to do
1498 case SUBMESH_RESTORED:
1499 // check if a mesh is already computed that may
1500 // happen after retrieval from a file
1501 ComputeStateEngine( CHECK_COMPUTE_STATE );
1502 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1503 algo = gen->GetAlgo(*_father, _subShape);
1504 if (algo) algo->SubmeshRestored( this );
1506 case MESH_ENTITY_REMOVED:
1508 case CHECK_COMPUTE_STATE:
1509 if ( IsMeshComputed() )
1510 _computeState = COMPUTE_OK;
1518 // ----------------------------------------------------------------------
1523 case MODIF_ALGO_STATE:
1524 ComputeStateEngine( CLEAN );
1525 algo = gen->GetAlgo((*_father), _subShape);
1526 if (algo && !algo->NeedDescretBoundary())
1527 CleanDependsOn(); // clean sub-meshes with event CLEAN
1529 case COMPUTE: // nothing to do
1532 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1533 RemoveSubMeshElementsAndNodes();
1534 _computeState = NOT_READY;
1535 if ( _algoState == HYP_OK )
1536 _computeState = READY_TO_COMPUTE;
1538 case SUBMESH_COMPUTED: // nothing to do
1540 case SUBMESH_RESTORED:
1541 ComputeStateEngine( CHECK_COMPUTE_STATE );
1542 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1543 algo = gen->GetAlgo(*_father, _subShape);
1544 if (algo) algo->SubmeshRestored( this );
1546 case MESH_ENTITY_REMOVED:
1547 UpdateDependantsState( CHECK_COMPUTE_STATE );
1548 ComputeStateEngine( CHECK_COMPUTE_STATE );
1549 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1551 case CHECK_COMPUTE_STATE:
1552 if ( !IsMeshComputed() )
1553 if (_algoState == HYP_OK)
1554 _computeState = READY_TO_COMPUTE;
1556 _computeState = NOT_READY;
1564 // ----------------------------------------------------------------------
1566 case FAILED_TO_COMPUTE:
1569 case MODIF_ALGO_STATE:
1570 if (_algoState == HYP_OK)
1571 _computeState = READY_TO_COMPUTE;
1573 _computeState = NOT_READY;
1575 case COMPUTE: // nothing to do
1578 CleanDependants(); // submeshes dependent on me should be cleaned as well
1579 RemoveSubMeshElementsAndNodes();
1581 case SUBMESH_COMPUTED: // allow retry compute
1582 if (_algoState == HYP_OK)
1583 _computeState = READY_TO_COMPUTE;
1585 _computeState = NOT_READY;
1587 case SUBMESH_RESTORED:
1588 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1590 case MESH_ENTITY_REMOVED:
1592 case CHECK_COMPUTE_STATE:
1593 if ( IsMeshComputed() )
1594 _computeState = COMPUTE_OK;
1596 if (_algoState == HYP_OK)
1597 _computeState = READY_TO_COMPUTE;
1599 _computeState = NOT_READY;
1607 // ----------------------------------------------------------------------
1613 NotifyListenersOnEvent( event, COMPUTE_EVENT );
1618 //=======================================================================
1620 * \brief Update compute_state by _computeError
1622 //=======================================================================
1624 bool SMESH_subMesh::CheckComputeError(SMESH_Algo* theAlgo)
1626 bool noErrors = ( !_computeError || _computeError->IsOK() );
1629 if ( !_computeError->myAlgo )
1630 _computeError->myAlgo = theAlgo;
1634 text << theAlgo->GetName() << " failed on subshape #" << _Id << " with error ";
1635 if (_computeError->IsCommon() )
1636 text << _computeError->CommonName();
1638 text << _computeError->myName;
1639 if ( _computeError->myComment.size() > 0 )
1640 text << " \"" << _computeError->myComment << "\"";
1643 cout << text << endl;
1644 // Show vertices location of a failed shape
1645 TopTools_IndexedMapOfShape vMap;
1646 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1647 cout << "Subshape vertices " << ( vMap.Extent()>10 ? "(first 10):" : ":") << endl;
1648 for ( int iv = 1; iv <= vMap.Extent() && iv < 11; ++iv ) {
1649 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1650 cout << "#" << _father->GetMeshDS()->ShapeToIndex( vMap( iv )) << " ";
1651 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1656 _computeState = FAILED_TO_COMPUTE;
1660 _computeState = COMPUTE_OK;
1662 // Check state of submeshes
1663 if ( !theAlgo->NeedDescretBoundary() /*&& theAlgo->OnlyUnaryInput()*/ ) {
1664 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1665 while ( smIt->more() )
1666 if ( !smIt->next()->CheckComputeError( theAlgo ))
1672 //=======================================================================
1673 //function : ApplyToCollection
1674 //purpose : Apply theAlgo to all subshapes in theCollection
1675 //=======================================================================
1677 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1678 const TopoDS_Shape& theCollection)
1680 MESSAGE("SMESH_subMesh::ApplyToCollection");
1681 ASSERT ( !theAlgo->NeedDescretBoundary() );
1683 if ( _computeError )
1684 _computeError->myName = COMPERR_OK;
1686 bool ok = theAlgo->Compute( *_father, theCollection );
1688 // set _computeState of subshapes
1689 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1690 for ( ; anExplorer.More(); anExplorer.Next() )
1692 if ( SMESH_subMesh* subMesh = _father->GetSubMeshContaining( anExplorer.Current() ))
1694 bool localOK = subMesh->CheckComputeError( theAlgo );
1695 if ( !ok && localOK && !subMesh->IsMeshComputed() )
1697 subMesh->_computeError = theAlgo->GetComputeError();
1698 if ( subMesh->_computeError->IsOK() )
1699 _computeError = SMESH_ComputeError::New(COMPERR_ALGO_FAILED);
1700 localOK = CheckComputeError( theAlgo );
1703 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1704 subMesh->UpdateSubMeshState( localOK ? COMPUTE_OK : FAILED_TO_COMPUTE );
1712 //=======================================================================
1713 //function : UpdateSubMeshState
1715 //=======================================================================
1717 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1719 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1720 while ( smIt->more() )
1721 smIt->next()->_computeState = theState;
1724 //=======================================================================
1725 //function : ComputeSubMeshStateEngine
1727 //=======================================================================
1729 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1731 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1732 while ( smIt->more() )
1733 smIt->next()->ComputeStateEngine(event);
1736 //=======================================================================
1737 //function : UpdateDependantsState
1739 //=======================================================================
1741 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1743 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1744 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1745 for (; it.More(); it.Next())
1747 const TopoDS_Shape& ancestor = it.Value();
1748 SMESH_subMesh *aSubMesh =
1749 _father->GetSubMeshContaining(ancestor);
1751 aSubMesh->ComputeStateEngine( theEvent );
1755 //=============================================================================
1759 //=============================================================================
1761 void SMESH_subMesh::CleanDependants()
1763 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1765 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1766 for (; it.More(); it.Next())
1768 const TopoDS_Shape& ancestor = it.Value();
1769 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1770 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1771 // will erase mesh on other shapes in a compound
1772 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1773 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1775 aSubMesh->ComputeStateEngine(CLEAN);
1781 //=============================================================================
1785 //=============================================================================
1787 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1789 //SCRUTE(_subShape.ShapeType());
1791 cleanSubMesh( this );
1793 // algo may bind a submesh not to _subShape, eg 3D algo
1794 // sets nodes on SHELL while _subShape may be SOLID
1796 int dim = SMESH_Gen::GetShapeDim( _subShape );
1797 int type = _subShape.ShapeType() + 1;
1798 for ( ; type <= TopAbs_EDGE; type++) {
1799 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1801 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1802 for ( ; exp.More(); exp.Next() )
1803 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1810 //=======================================================================
1811 //function : GetCollection
1812 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1813 // meshed at once along with _subShape
1814 //=======================================================================
1816 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1818 MESSAGE("SMESH_subMesh::GetCollection");
1819 ASSERT (!theAlgo->NeedDescretBoundary());
1821 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1823 if ( mainShape.IsSame( _subShape ))
1826 const bool ignoreAuxiliaryHyps = false;
1827 list<const SMESHDS_Hypothesis*> aUsedHyp =
1828 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1830 // put in a compound all shapes with the same hypothesis assigned
1831 // and a good ComputState
1833 TopoDS_Compound aCompound;
1834 BRep_Builder aBuilder;
1835 aBuilder.MakeCompound( aCompound );
1837 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1838 for ( ; anExplorer.More(); anExplorer.Next() )
1840 const TopoDS_Shape& S = anExplorer.Current();
1841 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1842 if ( subMesh == this )
1844 aBuilder.Add( aCompound, S );
1846 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
1848 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1849 if (anAlgo == theAlgo &&
1850 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp)
1851 aBuilder.Add( aCompound, S );
1858 //=======================================================================
1859 //function : GetSimilarAttached
1860 //purpose : return a hypothesis attached to theShape.
1861 // If theHyp is provided, similar but not same hypotheses
1862 // is returned; else only applicable ones having theHypType
1864 //=======================================================================
1866 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1867 const SMESH_Hypothesis * theHyp,
1868 const int theHypType)
1870 SMESH_HypoFilter hypoKind;
1871 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1873 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1874 hypoKind.AndNot( hypoKind.Is( theHyp ));
1875 if ( theHyp->IsAuxiliary() )
1876 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1878 hypoKind.AndNot( hypoKind.IsAuxiliary());
1881 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1884 return _father->GetHypothesis( theShape, hypoKind, false );
1887 //=======================================================================
1888 //function : CheckConcurentHypothesis
1889 //purpose : check if there are several applicable hypothesis attached to
1891 //=======================================================================
1893 SMESH_Hypothesis::Hypothesis_Status
1894 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1896 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1898 // is there local hypothesis on me?
1899 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1900 return SMESH_Hypothesis::HYP_OK;
1903 TopoDS_Shape aPrevWithHyp;
1904 const SMESH_Hypothesis* aPrevHyp = 0;
1905 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1906 for (; it.More(); it.Next())
1908 const TopoDS_Shape& ancestor = it.Value();
1909 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1912 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1914 aPrevWithHyp = ancestor;
1917 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1918 return SMESH_Hypothesis::HYP_CONCURENT;
1920 return SMESH_Hypothesis::HYP_OK;
1923 return SMESH_Hypothesis::HYP_OK;
1926 //================================================================================
1928 * \brief Sets an event listener and its data to a submesh
1929 * \param listener - the listener to store
1930 * \param data - the listener data to store
1931 * \param where - the submesh to store the listener and it's data
1932 * \param deleteListener - if true then the listener will be deleted as
1933 * it is removed from where submesh
1935 * It remembers the submesh where it puts the listener in order to delete
1936 * them when HYP_OK algo_state is lost
1937 * After being set, event listener is notified on each event of where submesh.
1939 //================================================================================
1941 void SMESH_subMesh::SetEventListener(EventListener* listener,
1942 EventListenerData* data,
1943 SMESH_subMesh* where)
1945 if ( listener && where ) {
1946 where->SetEventListener( listener, data );
1947 myOwnListeners.push_back( make_pair( where, listener ));
1951 //================================================================================
1953 * \brief Sets an event listener and its data to a submesh
1954 * \param listener - the listener to store
1955 * \param data - the listener data to store
1957 * After being set, event listener is notified on each event of a submesh.
1959 //================================================================================
1961 void SMESH_subMesh::SetEventListener(EventListener* listener, EventListenerData* data)
1963 map< EventListener*, EventListenerData* >::iterator l_d =
1964 myEventListeners.find( listener );
1965 if ( l_d != myEventListeners.end() ) {
1966 if ( l_d->second && l_d->second->IsDeletable() )
1971 myEventListeners.insert( make_pair( listener, data ));
1974 //================================================================================
1976 * \brief Return an event listener data
1977 * \param listener - the listener whose data is
1978 * \retval EventListenerData* - found data, maybe NULL
1980 //================================================================================
1982 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener) const
1984 map< EventListener*, EventListenerData* >::const_iterator l_d =
1985 myEventListeners.find( listener );
1986 if ( l_d != myEventListeners.end() )
1991 //================================================================================
1993 * \brief Notify stored event listeners on the occured event
1994 * \param event - algo_event or compute_event itself
1995 * \param eventType - algo_event or compute_event
1996 * \param subMesh - the submesh where the event occures
1997 * \param data - listener data stored in the subMesh
1998 * \param hyp - hypothesis, if eventType is algo_event
2000 //================================================================================
2002 void SMESH_subMesh::NotifyListenersOnEvent( const int event,
2003 const event_type eventType,
2004 SMESH_Hypothesis* hyp)
2006 map< EventListener*, EventListenerData* >::iterator l_d = myEventListeners.begin();
2007 for ( ; l_d != myEventListeners.end(); ++l_d )
2008 l_d->first->ProcessEvent( event, eventType, this, l_d->second, hyp );
2011 //================================================================================
2013 * \brief Unregister the listener and delete listener's data
2014 * \param listener - the event listener
2016 //================================================================================
2018 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2020 map< EventListener*, EventListenerData* >::iterator l_d =
2021 myEventListeners.find( listener );
2022 if ( l_d != myEventListeners.end() ) {
2023 if ( l_d->first && l_d->first->IsDeletable() ) delete l_d->first;
2024 if ( l_d->second && l_d->second->IsDeletable() ) delete l_d->second;
2025 myEventListeners.erase( l_d );
2029 //================================================================================
2031 * \brief Delete event listeners depending on algo of this submesh
2033 //================================================================================
2035 void SMESH_subMesh::DeleteOwnListeners()
2037 list< pair< SMESH_subMesh*, EventListener* > >::iterator sm_l;
2038 for ( sm_l = myOwnListeners.begin(); sm_l != myOwnListeners.end(); ++sm_l)
2039 sm_l->first->DeleteEventListener( sm_l->second );
2040 myOwnListeners.clear();
2043 //================================================================================
2045 * \brief Do something on a certain event
2046 * \param event - algo_event or compute_event itself
2047 * \param eventType - algo_event or compute_event
2048 * \param subMesh - the submesh where the event occures
2049 * \param data - listener data stored in the subMesh
2050 * \param hyp - hypothesis, if eventType is algo_event
2052 * The base implementation translates CLEAN event to the subMesh
2053 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2054 * successful COMPUTE event.
2056 //================================================================================
2058 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2059 const int eventType,
2060 SMESH_subMesh* subMesh,
2061 EventListenerData* data,
2062 const SMESH_Hypothesis* /*hyp*/)
2064 if ( data && !data->mySubMeshes.empty() &&
2065 eventType == SMESH_subMesh::COMPUTE_EVENT)
2067 ASSERT( data->mySubMeshes.front() != subMesh );
2069 case SMESH_subMesh::CLEAN:
2070 data->mySubMeshes.front()->ComputeStateEngine( event );
2072 case SMESH_subMesh::COMPUTE:
2073 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2074 data->mySubMeshes.front()->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2083 //================================================================================
2085 * \brief Iterator over submeshes and optionally prepended or appended one
2087 //================================================================================
2089 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2091 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2092 SMESH_subMesh* prepend,
2093 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2095 myCur = prepend ? prepend : myIt->more() ? myIt->next() : 0;
2097 /// Return true if and only if there are other object in this iterator
2102 /// Return the current object and step to the next one
2103 virtual SMESH_subMesh* next()
2105 SMESH_subMesh* res = myCur;
2106 if ( myIt->more() ) { myCur = myIt->next(); }
2107 else { myCur = myAppend; myAppend = 0; }
2114 SMESH_subMesh *myAppend, *myCur;
2115 SMDS_Iterator<SMESH_subMesh*> *myIt;
2119 //================================================================================
2121 * \brief Return iterator on the submeshes this one depends on
2122 * \param includeSelf - this submesh to be returned also
2123 * \param reverse - if true, complex shape submeshes go first
2125 //================================================================================
2127 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2130 SMESH_subMesh *prepend=0, *append=0;
2131 if ( includeSelf ) {
2132 if ( reverse ) prepend = this;
2135 typedef map < int, SMESH_subMesh * > TMap;
2138 return SMESH_subMeshIteratorPtr
2139 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( DependsOn() ), prepend, append ));
2142 return SMESH_subMeshIteratorPtr
2143 ( new _Iterator( new SMDS_mapIterator<TMap>( DependsOn() ), prepend, append ));