1 // Copyright (C) 2007-2022 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, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementation of SMESH idl descriptions
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_subMesh.hxx"
30 #include "SMDS_SetIterator.hxx"
31 #include "SMESHDS_Mesh.hxx"
32 #include "SMESH_Algo.hxx"
33 #include "SMESH_Comment.hxx"
34 #include "SMESH_Gen.hxx"
35 #include "SMESH_HypoFilter.hxx"
36 #include "SMESH_Hypothesis.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_MesherHelper.hxx"
39 #include "SMESH_subMeshEventListener.hxx"
41 #include "utilities.h"
42 #include "Basics_Utils.hxx"
44 #include <BRep_Builder.hxx>
45 #include <BRep_Tool.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_ListOfShape.hxx>
52 #include <TopoDS_Compound.hxx>
53 #include <TopoDS_Iterator.hxx>
56 #include <Standard_OutOfMemory.hxx>
57 #include <Standard_ErrorHandler.hxx>
64 // enable printing algo + shape id + hypo used while meshing
65 //#define PRINT_WHO_COMPUTE_WHAT
68 //=============================================================================
70 * \brief Allocate some memory at construction and release it at destruction.
71 * Is used to be able to continue working after mesh generation breaks due to
74 //=============================================================================
79 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
80 ~MemoryReserve() { delete [] myBuf; }
83 //=============================================================================
85 * default constructor:
87 //=============================================================================
89 SMESH_subMesh::SMESH_subMesh(int Id,
91 SMESHDS_Mesh * meshDS,
92 const TopoDS_Shape & aSubShape)
94 _subShape = aSubShape;
95 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
98 _dependenceAnalysed = _alwaysComputed = false;
100 if (_subShape.ShapeType() == TopAbs_VERTEX)
103 _computeState = READY_TO_COMPUTE;
107 _algoState = NO_ALGO;
108 _computeState = NOT_READY;
110 _computeCost = 0; // how costly is to compute this sub-mesh
111 _realComputeCost = 0;
112 _allowedSubShapes = nullptr;
115 //=============================================================================
119 //=============================================================================
121 SMESH_subMesh::~SMESH_subMesh()
123 deleteOwnListeners();
126 //=============================================================================
130 //=============================================================================
132 int SMESH_subMesh::GetId() const
134 //MESSAGE("SMESH_subMesh::GetId");
138 //=============================================================================
142 //=============================================================================
144 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
146 // submesh appears in DS only when a mesher set nodes and elements on a shape
147 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
150 //=============================================================================
154 //=============================================================================
156 const SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS() const
158 return ((SMESH_subMesh*) this )->GetSubMeshDS();
161 //=============================================================================
165 //=============================================================================
167 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
169 if ( !GetSubMeshDS() ) {
170 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
171 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
173 return GetSubMeshDS();
176 //=============================================================================
180 //=============================================================================
182 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
184 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
185 while ( smIt->more() ) {
186 SMESH_subMesh *sm = smIt->next();
187 if ( sm->GetComputeState() == READY_TO_COMPUTE )
190 return 0; // nothing to compute
193 //================================================================================
195 * \brief Returns a current algorithm
197 //================================================================================
199 SMESH_Algo* SMESH_subMesh::GetAlgo() const
203 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
204 me->_algo = _father->GetGen()->GetAlgo( me, & me->_algoShape );
209 //================================================================================
211 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
212 * none mesh entity is bound to it (PAL13615, 2nd part)
214 //================================================================================
216 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
218 _alwaysComputed = isAlCo;
219 if ( _alwaysComputed )
220 _computeState = COMPUTE_OK;
222 ComputeStateEngine( CHECK_COMPUTE_STATE );
225 //=======================================================================
227 * \brief Return true if no mesh entities is bound to the submesh
229 //=======================================================================
231 bool SMESH_subMesh::IsEmpty() const
233 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
234 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
238 //=======================================================================
239 //function : IsMeshComputed
240 //purpose : check if _subMeshDS contains mesh elements
241 //=======================================================================
243 bool SMESH_subMesh::IsMeshComputed() const
245 if ( _alwaysComputed )
247 // algo may bind a sub-mesh not to _subShape, eg 3D algo
248 // sets nodes on SHELL while _subShape may be SOLID
250 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
251 int dim = SMESH_Gen::GetShapeDim( _subShape );
252 int type = _subShape.ShapeType();
253 for ( ; type <= TopAbs_VERTEX; type++) {
254 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
256 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
257 for ( ; exp.More(); exp.Next() )
259 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
261 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
274 //================================================================================
276 * \brief Check if any upper level sub-shape is not computed.
277 * Used to update a sub-mesh icon
279 //================================================================================
281 bool SMESH_subMesh::IsComputedPartially() const
283 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(/*includeSelf=*/true,
284 /*SolidFirst=*/true);
285 bool allComputed = true;
286 TopAbs_ShapeEnum readyType = TopAbs_VERTEX; // max value
287 while ( smIt->more() && allComputed )
289 SMESH_subMesh* sm = smIt->next();
291 if ( sm->GetSubShape().ShapeType() > readyType )
292 break; // lower dimension -> stop
293 if ( sm->GetComputeState() != SMESH_subMesh::NOT_READY )
294 readyType = sm->GetSubShape().ShapeType();
296 switch ( sm->GetComputeState() )
298 case SMESH_subMesh::READY_TO_COMPUTE:
299 case SMESH_subMesh::FAILED_TO_COMPUTE:
300 allComputed = false;// sm->IsMeshComputed();
302 case SMESH_subMesh::NOT_READY:
303 case SMESH_subMesh::COMPUTE_OK:
310 //=============================================================================
312 * Return true if all sub-meshes have been meshed
314 //=============================================================================
316 bool SMESH_subMesh::SubMeshesComputed(bool * isFailedToCompute/*=0*/) const
318 int myDim = SMESH_Gen::GetShapeDim( _subShape );
319 int dimToCheck = myDim - 1;
320 bool subMeshesComputed = true;
321 if ( isFailedToCompute ) *isFailedToCompute = false;
322 // check sub-meshes with upper dimension => reverse iteration
323 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
324 while ( smIt->more() )
326 SMESH_subMesh *sm = smIt->next();
327 if ( sm->_alwaysComputed )
329 const TopoDS_Shape & ss = sm->GetSubShape();
331 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
332 // in checking of existence of edges if the algo needs only faces. Moreover,
333 // degenerated edges may have no sub-mesh, as after computing NETGEN_2D.
334 if ( !_algo || _algo->NeedDiscreteBoundary() ) {
335 int dim = SMESH_Gen::GetShapeDim( ss );
336 if (dim < dimToCheck)
337 break; // the rest sub-meshes are all of less dimension
339 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
340 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK ) ||
341 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
344 subMeshesComputed = false;
346 if ( isFailedToCompute && !(*isFailedToCompute) )
347 *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
349 if ( !isFailedToCompute )
353 return subMeshesComputed;
356 //================================================================================
358 * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
360 * \return int - the computation cost in abstract units.
362 //================================================================================
364 int SMESH_subMesh::GetComputeCost() const
366 return _realComputeCost;
369 //================================================================================
371 * \brief Return cost of computing this sub-mesh. The cost depends on the shape type
372 * and number of sub-meshes this one DependsOn().
373 * \return int - the computation cost in abstract units.
375 //================================================================================
377 int SMESH_subMesh::computeCost() const
382 switch ( _subShape.ShapeType() ) {
384 case TopAbs_SHELL: computeCost = 5000; break;
385 case TopAbs_FACE: computeCost = 500; break;
386 case TopAbs_EDGE: computeCost = 2; break;
387 default: computeCost = 1;
389 SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
390 while ( childIt->more() )
391 computeCost += childIt->next()->computeCost();
393 ((SMESH_subMesh*)this)->_computeCost = computeCost;
398 //=============================================================================
400 * Returns all sub-meshes this one depend on
402 //=============================================================================
404 const std::map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
406 if ( _dependenceAnalysed || !_father->HasShapeToMesh() )
409 int type = _subShape.ShapeType();
412 case TopAbs_COMPOUND:
414 list< TopoDS_Shape > compounds( 1, _subShape );
415 list< TopoDS_Shape >::iterator comp = compounds.begin();
416 for ( ; comp != compounds.end(); ++comp )
418 for ( TopoDS_Iterator sub( *comp ); sub.More(); sub.Next() )
419 switch ( sub.Value().ShapeType() )
421 case TopAbs_COMPOUND: compounds.push_back( sub.Value() ); break;
422 case TopAbs_COMPSOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
423 case TopAbs_SOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
424 case TopAbs_SHELL: insertDependence( sub.Value(), TopAbs_FACE ); break;
425 case TopAbs_FACE: insertDependence( sub.Value(), TopAbs_FACE ); break;
426 case TopAbs_WIRE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
427 case TopAbs_EDGE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
428 case TopAbs_VERTEX: insertDependence( sub.Value(), TopAbs_VERTEX ); break;
434 case TopAbs_COMPSOLID: insertDependence( _subShape, TopAbs_SOLID ); break;
435 case TopAbs_SOLID: insertDependence( _subShape, TopAbs_FACE );
436 { /*internal EDGE*/ insertDependence( _subShape, TopAbs_EDGE, TopAbs_WIRE ); break; }
437 case TopAbs_SHELL: insertDependence( _subShape, TopAbs_FACE ); break;
438 case TopAbs_FACE: insertDependence( _subShape, TopAbs_EDGE ); break;
439 case TopAbs_WIRE: insertDependence( _subShape, TopAbs_EDGE ); break;
440 case TopAbs_EDGE: insertDependence( _subShape, TopAbs_VERTEX ); break;
443 _dependenceAnalysed = true;
447 //================================================================================
449 * \brief Return a key for SMESH_subMesh::_mapDepend map
451 //================================================================================
455 int dependsOnMapKey( TopAbs_ShapeEnum type, int shapeID )
457 int ordType = 9 - int(type); // 2 = Vertex, 8 = CompSolid
459 cle += 10000000 * ordType; // sort map by ordType then index
462 int dependsOnMapKey( const SMESH_subMesh* sm )
464 return dependsOnMapKey( sm->GetSubShape().ShapeType(), sm->GetId() );
468 //=============================================================================
470 * Add sub-meshes on sub-shapes of a given type into the dependence map.
472 //=============================================================================
474 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
475 TopAbs_ShapeEnum aSubType,
476 TopAbs_ShapeEnum avoidType)
478 TopExp_Explorer sub( aShape, aSubType, avoidType );
479 for ( ; sub.More(); sub.Next() )
481 SMESH_subMesh *aSubMesh = _father->GetSubMesh( sub.Current() );
482 if ( aSubMesh->GetId() == 0 )
483 continue; // not a sub-shape of the shape to mesh
484 int cle = dependsOnMapKey( aSubMesh );
485 if ( _mapDepend.find( cle ) == _mapDepend.end())
487 _mapDepend[cle] = aSubMesh;
488 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
489 _mapDepend.insert( subMap.begin(), subMap.end() );
494 //================================================================================
496 * \brief Return \c true if \a this sub-mesh depends on \a other
498 //================================================================================
500 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
502 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
505 //================================================================================
507 * \brief Return \c true if \a this sub-mesh depends on a \a shape
509 //================================================================================
511 bool SMESH_subMesh::DependsOn( const int shapeID ) const
513 return DependsOn( _father->GetSubMeshContaining( shapeID ));
516 //=============================================================================
518 * Return a shape of \a this sub-mesh
520 //=============================================================================
522 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
527 //=======================================================================
528 //function : CanAddHypothesis
529 //purpose : return true if theHypothesis can be attached to me:
530 // its dimension is checked
531 //=======================================================================
533 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
535 int aHypDim = theHypothesis->GetDim();
536 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
537 // issue 21106. Forbid 3D mesh on the SHELL
538 // if (aHypDim == 3 && aShapeDim == 3) {
539 // // check case of open shell
540 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
541 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
544 if ( aHypDim <= aShapeDim )
550 //=======================================================================
551 //function : IsApplicableHypothesis
552 //purpose : check if this sub-mesh can be computed using a hypothesis
553 //=======================================================================
555 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis) const
557 if ( !_father->HasShapeToMesh() && _subShape.ShapeType() == TopAbs_SOLID )
558 return true; // true for the PseudoShape
560 return IsApplicableHypothesis( theHypothesis, _subShape.ShapeType() );
563 //=======================================================================
564 //function : IsApplicableHypothesis
565 //purpose : compare shape type and hypothesis type
566 //=======================================================================
568 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis,
569 const TopAbs_ShapeEnum theShapeType)
571 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
574 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
575 // issue 21106. Forbid 3D mesh on the SHELL
576 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
582 switch ( theShapeType ) {
587 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
590 // Special case for algorithms, building 2D mesh on a whole shell.
591 // Before this fix there was a problem after restoring from study,
592 // because in that case algorithm is assigned before hypothesis
593 // (on shell in problem case) and hypothesis is checked on faces
594 // (because it is 2D), where we have NO_ALGO state.
595 // Now 2D hypothesis is also applicable to shells.
596 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
599 // case TopAbs_COMPSOLID:
600 // case TopAbs_COMPOUND:
606 //================================================================================
608 * \brief Treats modification of hypotheses definition
609 * \param [in] event - what happens
610 * \param [in] anHyp - a hypothesis
611 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
613 * Optional description of a problematic situation (if any) can be retrieved
614 * via GetComputeError().
616 //================================================================================
618 SMESH_Hypothesis::Hypothesis_Status
619 SMESH_subMesh::AlgoStateEngine(algo_event event, SMESH_Hypothesis * anHyp)
621 // **** les retour des evenement shape sont significatifs
622 // (add ou remove fait ou non)
623 // le retour des evenement father n'indiquent pas que add ou remove fait
625 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
626 if ( _Id == 0 ) return ret; // not a sub-shape of the shape to mesh
628 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
629 SMESH_Algo* algo = 0;
632 if (_subShape.ShapeType() == TopAbs_VERTEX )
634 if ( anHyp->GetDim() != 0) {
635 if (event == ADD_HYP || event == ADD_ALGO)
636 return SMESH_Hypothesis::HYP_BAD_DIM;
638 return SMESH_Hypothesis::HYP_OK;
641 else if ( _algoState == HYP_OK ) {
642 // update default _algoState
643 if ( event != REMOVE_FATHER_ALGO )
645 _algoState = NO_ALGO;
648 _algoState = MISSING_HYP;
649 if ( event == REMOVE_FATHER_HYP ||
650 algo->CheckHypothesis(*_father,_subShape, aux_ret))
657 int oldAlgoState = _algoState;
658 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
659 SMESH_Algo* algoRequiringCleaning = 0;
661 bool isApplicableHyp = IsApplicableHypothesis( anHyp );
663 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
665 // -------------------------------------------
666 // check if a shape needed by algo is present
667 // -------------------------------------------
668 algo = static_cast< SMESH_Algo* >( anHyp );
669 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
670 return SMESH_Hypothesis::HYP_NEED_SHAPE;
671 // ----------------------
672 // check mesh conformity
673 // ----------------------
674 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
675 return SMESH_Hypothesis::HYP_NOTCONFORM;
677 // check if all-dimensional algo is hidden by other local one
678 if ( event == ADD_ALGO ) {
679 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
680 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
681 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
682 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
683 if ( !curAlgo->NeedDiscreteBoundary() && curAlgo != anHyp )
684 algoRequiringCleaning = curAlgo;
688 // ----------------------------------
689 // add a hypothesis to DS if possible
690 // ----------------------------------
691 if (event == ADD_HYP || event == ADD_ALGO)
693 if ( ! CanAddHypothesis( anHyp )) // check dimension
694 return SMESH_Hypothesis::HYP_BAD_DIM;
696 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
697 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
699 if ( !meshDS->AddHypothesis(_subShape, anHyp))
700 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
703 // --------------------------
704 // remove a hypothesis from DS
705 // --------------------------
706 if (event == REMOVE_HYP || event == REMOVE_ALGO)
708 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
709 return SMESH_Hypothesis::HYP_OK; // nothing changes
711 if (event == REMOVE_ALGO)
713 algo = dynamic_cast<SMESH_Algo*> (anHyp);
714 if (!algo->NeedDiscreteBoundary())
715 algoRequiringCleaning = algo;
719 // ------------------
720 // analyse algo state
721 // ------------------
722 if (!isApplicableHyp)
723 return ret; // not applicable hypotheses do not change algo state
725 if (( algo = GetAlgo()))
726 algo->InitComputeError();
731 // ----------------------------------------------------------------------
740 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
741 setAlgoState(HYP_OK);
742 else if ( algo->IsStatusFatal( aux_ret )) {
743 meshDS->RemoveHypothesis(_subShape, anHyp);
747 setAlgoState(MISSING_HYP);
754 case ADD_FATHER_ALGO: { // Algo just added in father
757 if ( algo == anHyp ) {
758 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
759 setAlgoState(HYP_OK);
761 setAlgoState(MISSING_HYP);
765 case REMOVE_FATHER_HYP:
767 case REMOVE_FATHER_ALGO: {
771 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
772 setAlgoState(HYP_OK);
774 setAlgoState(MISSING_HYP);
778 case MODIF_HYP: break;
785 // ----------------------------------------------------------------------
793 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
794 setAlgoState(HYP_OK);
795 if (SMESH_Hypothesis::IsStatusFatal( ret ))
796 meshDS->RemoveHypothesis(_subShape, anHyp);
797 else if (!_father->IsUsedHypothesis( anHyp, this ))
799 meshDS->RemoveHypothesis(_subShape, anHyp);
800 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
804 case ADD_ALGO: { //already existing algo : on father ?
807 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
808 setAlgoState(HYP_OK);
809 else if ( algo->IsStatusFatal( aux_ret )) {
810 meshDS->RemoveHypothesis(_subShape, anHyp);
814 setAlgoState(MISSING_HYP);
819 case REMOVE_ALGO: { // perhaps a father algo applies ?
821 if (algo == NULL) // no more algo applying on sub-shape...
823 setAlgoState(NO_ALGO);
827 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
828 setAlgoState(HYP_OK);
830 setAlgoState(MISSING_HYP);
834 case MODIF_HYP: // assigned hypothesis value may become good
835 case ADD_FATHER_HYP: {
838 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
839 setAlgoState(HYP_OK);
841 setAlgoState(MISSING_HYP);
844 case ADD_FATHER_ALGO: { // new father algo
847 if ( algo == anHyp ) {
848 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
849 setAlgoState(HYP_OK);
851 setAlgoState(MISSING_HYP);
855 case REMOVE_FATHER_HYP: // nothing to do
857 case REMOVE_FATHER_ALGO: {
859 if (algo == NULL) // no more applying algo on father
861 setAlgoState(NO_ALGO);
865 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
866 setAlgoState(HYP_OK);
868 setAlgoState(MISSING_HYP);
878 // ----------------------------------------------------------------------
886 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
888 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
889 // ret should be fatal: anHyp was not added
890 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
892 else if (!_father->IsUsedHypothesis( anHyp, this ))
893 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
895 if (SMESH_Hypothesis::IsStatusFatal( ret ))
897 MESSAGE("do not add extra hypothesis");
898 meshDS->RemoveHypothesis(_subShape, anHyp);
906 case ADD_ALGO: { //already existing algo : on father ?
908 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
909 // check if algo changes
911 f.Init( SMESH_HypoFilter::IsAlgo() );
912 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
913 f.AndNot( SMESH_HypoFilter::Is( algo ));
914 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
916 string( algo->GetName()) != prevAlgo->GetName())
918 oldAlgoState = NO_ALGO; // force setting event listener (#16648)
923 setAlgoState(MISSING_HYP);
929 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
930 setAlgoState(HYP_OK);
932 setAlgoState(MISSING_HYP);
936 case REMOVE_ALGO: { // perhaps a father algo applies ?
938 if (algo == NULL) // no more algo applying on sub-shape...
940 setAlgoState(NO_ALGO);
944 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
945 // check if algo remains
946 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
950 setAlgoState(MISSING_HYP);
954 case MODIF_HYP: // hypothesis value may become bad
955 case ADD_FATHER_HYP: { // new father hypothesis ?
958 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
960 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
964 setAlgoState(MISSING_HYP);
967 case ADD_FATHER_ALGO: {
969 if ( algo == anHyp ) { // a new algo on father
970 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
971 // check if algo changes
973 f.Init( SMESH_HypoFilter::IsAlgo() );
974 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
975 f.AndNot( SMESH_HypoFilter::Is( algo ));
976 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
978 string(algo->GetName()) != string(prevAlgo->GetName()) )
982 setAlgoState(MISSING_HYP);
986 case REMOVE_FATHER_HYP: {
989 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
990 // is there the same local hyp or maybe a new father algo applied?
991 if ( !getSimilarAttached( _subShape, anHyp ) )
995 setAlgoState(MISSING_HYP);
998 case REMOVE_FATHER_ALGO: {
999 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
1000 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
1001 algo = dynamic_cast<SMESH_Algo*> (anHyp);
1002 if (!algo->NeedDiscreteBoundary())
1003 algoRequiringCleaning = algo;
1005 if (algo == NULL) // no more applying algo on father
1007 setAlgoState(NO_ALGO);
1011 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1012 // check if algo changes
1013 if ( string(algo->GetName()) != string( anHyp->GetName()) )
1017 setAlgoState(MISSING_HYP);
1027 // ----------------------------------------------------------------------
1034 // detect algorithm hiding
1036 if ( ret == SMESH_Hypothesis::HYP_OK &&
1037 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) && algo &&
1038 algo->GetName() == anHyp->GetName() )
1041 SMESH_Gen* gen = _father->GetGen();
1042 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1043 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1044 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1045 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1046 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1049 if ( ret == SMESH_Hypothesis::HYP_OK &&
1050 !algo->NeedDiscreteBoundary() &&
1051 !algo->SupportSubmeshes())
1053 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1054 gen->GetAlgo( this, &algoAssignedTo );
1055 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1056 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1057 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1058 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1059 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1063 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1064 _computeError = _algo->GetComputeError();
1065 _algo->InitComputeError();
1068 bool stateChange = ( _algoState != oldAlgoState );
1070 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1071 algo->SetEventListener( this );
1073 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1076 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1078 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1079 deleteOwnListeners();
1080 SetIsAlwaysComputed( false );
1081 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1082 // restore default states
1083 _algoState = HYP_OK;
1084 _computeState = READY_TO_COMPUTE;
1088 if ( algoRequiringCleaning ) {
1089 // added or removed algo is all-dimensional
1090 ComputeStateEngine( CLEAN );
1091 cleanDependsOn( algoRequiringCleaning );
1092 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1095 if ( stateChange || modifiedHyp )
1096 ComputeStateEngine( MODIF_ALGO_STATE );
1098 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1103 //=======================================================================
1104 //function : IsConform
1105 //purpose : check if a conform mesh will be produced by the Algo
1106 //=======================================================================
1108 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1110 // MESSAGE( "SMESH_subMesh::IsConform" );
1111 if ( !theAlgo ) return false;
1113 // Suppose that theAlgo is applicable to _subShape, do not check it here
1114 //if ( !IsApplicableHypothesis( theAlgo )) return false;
1116 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1117 // on a sub-shape will be ignored by theAlgo
1118 if ( theAlgo->NeedDiscreteBoundary() ||
1119 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1122 // only local algo is to be checked
1123 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1124 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1127 // check algo attached to adjacent shapes
1129 // loop on one level down sub-meshes
1130 TopoDS_Iterator itsub( _subShape );
1131 for (; itsub.More(); itsub.Next())
1133 // loop on adjacent subShapes
1134 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1135 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1137 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1138 if ( _subShape.IsSame( adjacent )) continue;
1139 if ( adjacent.ShapeType() != _subShape.ShapeType())
1142 // check algo attached to smAdjacent
1143 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1145 !algo->NeedDiscreteBoundary() &&
1146 algo->OnlyUnaryInput())
1147 return false; // NOT CONFORM MESH WILL BE PRODUCED
1154 //=============================================================================
1158 //=============================================================================
1160 void SMESH_subMesh::setAlgoState(algo_state state)
1165 //================================================================================
1167 * \brief Send an event to sub-meshes
1168 * \param [in] event - the event
1169 * \param [in] anHyp - an hypothesis
1170 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1171 * reports a fatal result
1172 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1174 * Optional description of a problematic situation (if any) can be retrieved
1175 * via GetComputeError().
1177 //================================================================================
1179 SMESH_Hypothesis::Hypothesis_Status
1180 SMESH_subMesh::SubMeshesAlgoStateEngine(algo_event event,
1181 SMESH_Hypothesis * anHyp,
1184 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1185 //EAP: a wire (dim==1) should notify edges (dim==1)
1186 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1187 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1189 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1190 while ( smIt->more() ) {
1191 SMESH_subMesh* sm = smIt->next();
1192 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1196 _computeError = sm->_computeError;
1197 sm->_computeError.reset();
1198 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1206 //================================================================================
1208 * \brief Remove elements from sub-meshes.
1209 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1210 * causes the cleaning.
1212 //================================================================================
1214 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1216 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1217 /*complexShapeFirst=*/true);
1218 if ( _father->NbNodes() == 0 )
1220 while ( smIt->more() )
1221 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1223 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1225 while ( smIt->more() )
1226 smIt->next()->ComputeStateEngine(CLEAN);
1228 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1230 // find sub-meshes to keep elements on
1231 set< SMESH_subMesh* > smToKeep;
1232 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1233 bool toKeepPrevShapeType = false;
1234 while ( smIt->more() )
1236 SMESH_subMesh* sm = smIt->next();
1237 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1238 if ( !sm->IsEmpty() )
1240 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1241 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1242 if ( !sameShapeType )
1244 // check if the algo allows presence of global algos of dimension the algo
1245 // can generate it-self;
1246 // always keep a node on VERTEX, as this node can be shared by segments
1247 // lying on EDGEs not shared by the VERTEX of sm, due to MergeNodes (PAL23068)
1248 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1249 keepSubMeshes = ( algoRequiringCleaning->NeedLowerHyps( shapeDim ) || shapeDim == 0 );
1250 prevShapeType = sm->GetSubShape().ShapeType();
1251 toKeepPrevShapeType = keepSubMeshes;
1253 if ( !keepSubMeshes )
1255 // look for a local algo used to mesh sm
1256 TopoDS_Shape algoShape = SMESH_MesherHelper::GetShapeOfHypothesis
1257 ( algoRequiringCleaning, _subShape, _father );
1258 SMESH_HypoFilter moreLocalAlgo;
1259 moreLocalAlgo.Init( SMESH_HypoFilter::IsMoreLocalThan( algoShape, *_father ));
1260 moreLocalAlgo.And ( SMESH_HypoFilter::IsAlgo() );
1261 bool localAlgoFound = _father->GetHypothesis( sm->_subShape, moreLocalAlgo, true );
1262 keepSubMeshes = localAlgoFound;
1264 // remember all sub-meshes of sm
1265 if ( keepSubMeshes )
1267 SMESH_subMeshIteratorPtr smIt2 = sm->getDependsOnIterator(true);
1268 while ( smIt2->more() )
1269 smToKeep.insert( smIt2->next() );
1274 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1275 while ( smIt->more() )
1277 SMESH_subMesh* sm = smIt->next();
1278 if ( !smToKeep.count( sm ))
1279 sm->ComputeStateEngine(CLEAN);
1284 //=============================================================================
1288 //=============================================================================
1290 void SMESH_subMesh::DumpAlgoState(bool isMain)
1294 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1296 map < int, SMESH_subMesh * >::const_iterator itsub;
1297 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1299 SMESH_subMesh *sm = (*itsub).second;
1300 sm->DumpAlgoState(false);
1303 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1304 " type of shape " << _subShape.ShapeType());
1307 case NO_ALGO : MESSAGE(" AlgoState = NO_ALGO"); break;
1308 case MISSING_HYP : MESSAGE(" AlgoState = MISSING_HYP"); break;
1309 case HYP_OK : MESSAGE(" AlgoState = HYP_OK");break;
1311 switch (_computeState)
1313 case NOT_READY : MESSAGE(" ComputeState = NOT_READY");break;
1314 case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1315 case COMPUTE_OK : MESSAGE(" ComputeState = COMPUTE_OK");break;
1316 case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1320 //================================================================================
1322 * \brief Remove nodes and elements bound to submesh
1323 * \param subMesh - submesh containing nodes and elements
1325 //================================================================================
1327 static void cleanSubMesh( SMESH_subMesh * subMesh )
1330 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS())
1332 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1333 smIdType nbElems = subMeshDS->NbElements();
1335 for ( SMDS_ElemIteratorPtr ite = subMeshDS->GetElements(); ite->more(); )
1336 meshDS->RemoveFreeElement( ite->next(), subMeshDS );
1338 smIdType nbNodes = subMeshDS->NbNodes();
1340 for ( SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes(); itn->more() ; )
1342 const SMDS_MeshNode * node = itn->next();
1343 if ( node->NbInverseElements() == 0 )
1344 meshDS->RemoveFreeNode( node, subMeshDS );
1345 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1346 meshDS->RemoveNode( node );
1353 //=============================================================================
1357 //=============================================================================
1359 bool SMESH_subMesh::ComputeStateEngine(compute_event event)
1362 case MODIF_ALGO_STATE:
1364 case COMPUTE_SUBMESH:
1365 //case COMPUTE_CANCELED:
1367 //case SUBMESH_COMPUTED:
1368 //case SUBMESH_RESTORED:
1369 //case SUBMESH_LOADED:
1370 //case MESH_ENTITY_REMOVED:
1371 //case CHECK_COMPUTE_STATE:
1372 _computeError.reset(); break;
1376 if ( event == CLEAN )
1377 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1379 if (_subShape.ShapeType() == TopAbs_VERTEX)
1381 _computeState = READY_TO_COMPUTE;
1382 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1383 if ( smDS && smDS->NbNodes() )
1385 if ( event == CLEAN ) {
1387 cleanSubMesh( this );
1390 _computeState = COMPUTE_OK;
1392 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1393 && !_alwaysComputed )
1396 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1397 gp_Pnt P = BRep_Tool::Pnt(V);
1398 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1399 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1400 _computeState = COMPUTE_OK;
1404 if ( event == MODIF_ALGO_STATE )
1408 SMESH_Gen *gen = _father->GetGen();
1409 SMESH_Algo *algo = 0;
1411 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1412 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1414 switch (_computeState)
1417 // ----------------------------------------------------------------------
1422 case MODIF_ALGO_STATE:
1424 if (algo && !algo->NeedDiscreteBoundary())
1425 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1426 if ( _algoState == HYP_OK )
1427 _computeState = READY_TO_COMPUTE;
1429 case COMPUTE: // nothing to do
1430 case COMPUTE_SUBMESH:
1432 case COMPUTE_CANCELED: // nothing to do
1436 removeSubMeshElementsAndNodes();
1438 case SUBMESH_COMPUTED: // nothing to do
1440 case SUBMESH_RESTORED:
1441 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1443 case MESH_ENTITY_REMOVED:
1445 case SUBMESH_LOADED:
1446 loadDependentMeshes();
1447 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1450 case CHECK_COMPUTE_STATE:
1451 if ( IsMeshComputed() )
1452 _computeState = COMPUTE_OK;
1460 // ----------------------------------------------------------------------
1462 case READY_TO_COMPUTE:
1465 case MODIF_ALGO_STATE:
1466 _computeState = NOT_READY;
1470 if (!algo->NeedDiscreteBoundary())
1471 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1472 if ( _algoState == HYP_OK )
1473 _computeState = READY_TO_COMPUTE;
1477 case COMPUTE_NOGEOM: // no geometry; can be several algos
1478 if ( !_father->HasShapeToMesh() )
1480 algo = GetAlgo(); // current algo
1483 // apply algos in the order of increasing dimension
1484 std::list< const SMESHDS_Hypothesis * > algos = _father->GetHypothesisList( _subShape );
1485 for ( int t = SMESHDS_Hypothesis::ALGO_1D; t <= SMESHDS_Hypothesis::ALGO_3D; ++t )
1487 std::list<const SMESHDS_Hypothesis *>::iterator al = algos.begin();
1488 for ( ; al != algos.end(); ++al )
1489 if ( (*al)->GetType() == t )
1491 _algo = (SMESH_Algo*) *al;
1492 _computeState = READY_TO_COMPUTE;
1493 if ( !ComputeStateEngine( COMPUTE ))
1497 _algo = algo; // restore
1503 case COMPUTE_SUBMESH:
1508 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1511 MESSAGE("***** verify compute state *****");
1512 _computeState = NOT_READY;
1513 setAlgoState(MISSING_HYP);
1516 TopoDS_Shape shape = _subShape;
1517 algo->SubMeshesToCompute().assign( 1, this );
1518 // check submeshes needed
1519 if (_father->HasShapeToMesh() ) {
1520 bool subComputed = false, subFailed = false;
1521 if (!algo->OnlyUnaryInput()) {
1522 // --- commented for bos#22320 to compute all sub-shapes at once if possible;
1523 // --- in case COMPUTE_SUBMESH, set of sub-shapes is limited
1524 // --- by calling SetAllowedSubShapes()
1525 // if ( event == COMPUTE )
1526 // shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToComput;
1528 // subComputed = SubMeshesComputed( & subFailed );
1529 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1532 subComputed = SubMeshesComputed();
1534 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1535 algo->SupportSubmeshes() ? !subFailed :
1536 ( !subComputed || _father->IsNotConformAllowed() ));
1539 _computeState = FAILED_TO_COMPUTE;
1540 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1542 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1543 "Unexpected computed sub-mesh",algo);
1550 // to restore cout that may be redirected by algo
1551 std::streambuf* coutBuffer = std::cout.rdbuf();
1553 //cleanDependants(); for "UseExisting_*D" algos
1554 //removeSubMeshElementsAndNodes();
1555 loadDependentMeshes();
1557 _computeState = FAILED_TO_COMPUTE;
1558 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1562 algo->InitComputeError();
1564 MemoryReserve aMemoryReserve;
1565 SMDS_Mesh::CheckMemory();
1566 Kernel_Utils::Localizer loc;
1567 if ( !_father->HasShapeToMesh() ) // no shape
1569 SMESH_MesherHelper helper( *_father );
1570 helper.SetSubShape( shape );
1571 helper.SetElementsOnShape( true );
1572 ret = algo->Compute(*_father, &helper );
1576 std::cout<<"Running compute for " << _father << " of shape type " << shape.ShapeType() << std::endl;
1577 ret = algo->Compute((*_father), shape);
1579 // algo can set _computeError of submesh
1580 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1582 catch ( ::SMESH_ComputeError& comperr ) {
1583 cout << " SMESH_ComputeError caught" << endl;
1584 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1585 *_computeError = comperr;
1587 catch ( std::bad_alloc& exc ) {
1588 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1589 if ( _computeError ) {
1590 _computeError->myName = COMPERR_MEMORY_PB;
1592 cleanSubMesh( this );
1595 catch ( Standard_OutOfMemory& exc ) {
1596 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1597 if ( _computeError ) {
1598 _computeError->myName = COMPERR_MEMORY_PB;
1600 cleanSubMesh( this );
1601 throw std::bad_alloc();
1603 catch (Standard_Failure& ex) {
1604 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1605 _computeError->myName = COMPERR_OCC_EXCEPTION;
1606 _computeError->myComment += ex.DynamicType()->Name();
1607 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1608 _computeError->myComment += ": ";
1609 _computeError->myComment += ex.GetMessageString();
1612 catch ( SALOME_Exception& S_ex ) {
1613 const int skipSalomeShift = 7; /* to skip "Salome " of
1614 "Salome Exception" prefix returned
1615 by SALOME_Exception::what() */
1616 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1617 _computeError->myName = COMPERR_SLM_EXCEPTION;
1618 _computeError->myComment = S_ex.what() + skipSalomeShift;
1620 catch ( std::exception& exc ) {
1621 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1622 _computeError->myName = COMPERR_STD_EXCEPTION;
1623 _computeError->myComment = exc.what();
1626 if ( _computeError )
1627 _computeError->myName = COMPERR_EXCEPTION;
1631 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1633 // check if an error reported on any sub-shape
1634 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1635 if ( isComputeErrorSet )
1637 // check if anything was built
1638 TopExp_Explorer subS(shape, _subShape.ShapeType());
1641 for (; ret && subS.More(); subS.Next())
1642 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1643 ( _subShape.ShapeType() != TopAbs_EDGE ||
1644 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1647 #ifdef PRINT_WHO_COMPUTE_WHAT
1648 for (subS.ReInit(); subS.More(); subS.Next())
1650 const std::list <const SMESHDS_Hypothesis *> & hyps =
1651 _algo->GetUsedHypothesis( *_father, _subShape );
1652 SMESH_Comment hypStr;
1653 if ( !hyps.empty() )
1655 hypStr << hyps.front()->GetName() << " ";
1656 ((SMESHDS_Hypothesis*)hyps.front())->SaveTo( hypStr.Stream() );
1659 cout << _father->GetSubMesh( subS.Current() )->GetId()
1660 << " " << ( ret ? "OK" : "FAIL" )
1661 << " " << _algo->GetName()
1662 << " " << hypStr << endl;
1665 // Set _computeError
1666 if ( !ret && !isComputeErrorSet )
1668 for ( subS.ReInit(); subS.More(); subS.Next() )
1670 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1671 if ( !sm->IsMeshComputed() )
1673 if ( !sm->_computeError )
1674 sm->_computeError = SMESH_ComputeError::New();
1675 if ( sm->_computeError->IsOK() )
1676 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1677 sm->_computeState = FAILED_TO_COMPUTE;
1678 sm->_computeError->myAlgo = algo;
1682 if ( ret && _computeError && _computeError->myName != COMPERR_WARNING )
1684 _computeError.reset();
1687 // transform errors into warnings if it is caused by mesh edition (imp 0023068)
1688 if (!ret && _father->GetIsModified() )
1690 for (subS.ReInit(); subS.More(); subS.Next())
1692 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1693 if ( !sm->IsMeshComputed() && sm->_computeError )
1695 // check if there is a VERTEX w/o nodes
1696 // with READY_TO_COMPUTE state (after MergeNodes())
1697 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1698 while ( smIt->more() )
1700 SMESH_subMesh * vertSM = smIt->next();
1701 if ( vertSM->_subShape.ShapeType() != TopAbs_VERTEX ) break;
1702 if ( vertSM->GetComputeState() == READY_TO_COMPUTE )
1704 SMESHDS_SubMesh * ds = vertSM->GetSubMeshDS();
1705 if ( !ds || ds->NbNodes() == 0 )
1707 sm->_computeState = READY_TO_COMPUTE;
1708 sm->_computeError->myName = COMPERR_WARNING;
1717 // send event SUBMESH_COMPUTED
1719 if ( !algo->NeedDiscreteBoundary() )
1720 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1721 for (subS.ReInit(); subS.More(); subS.Next())
1723 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1724 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1725 while ( smIt->more() ) {
1727 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1728 sm->updateDependantsState( SUBMESH_COMPUTED );
1734 updateDependantsState( SUBMESH_COMPUTED );
1736 // let algo clear its data gathered while algo->Compute()
1738 algo->CheckHypothesis((*_father), _subShape, hyp_status);
1742 case COMPUTE_CANCELED: // nothing to do
1746 removeSubMeshElementsAndNodes();
1747 _computeState = NOT_READY;
1751 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1753 _computeState = READY_TO_COMPUTE;
1755 setAlgoState(MISSING_HYP);
1758 case SUBMESH_COMPUTED: // nothing to do
1760 case SUBMESH_RESTORED:
1761 // check if a mesh is already computed that may
1762 // happen after retrieval from a file
1763 ComputeStateEngine( CHECK_COMPUTE_STATE );
1764 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1766 if (algo) algo->SubmeshRestored( this );
1768 case MESH_ENTITY_REMOVED:
1770 case SUBMESH_LOADED:
1771 loadDependentMeshes();
1772 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1775 case CHECK_COMPUTE_STATE:
1776 if ( IsMeshComputed() )
1777 _computeState = COMPUTE_OK;
1778 else if ( _computeError && _computeError->IsKO() )
1779 _computeState = FAILED_TO_COMPUTE;
1787 // ----------------------------------------------------------------------
1792 case MODIF_ALGO_STATE:
1793 ComputeStateEngine( CLEAN );
1795 if (algo && !algo->NeedDiscreteBoundary())
1796 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1798 case COMPUTE: // nothing to do
1800 case COMPUTE_SUBMESH: // nothing to do
1802 case COMPUTE_CANCELED: // nothing to do
1805 cleanDependants(); // clean sub-meshes, dependent on this one, with event CLEAN
1806 removeSubMeshElementsAndNodes();
1807 _computeState = NOT_READY;
1808 if ( _algoState == HYP_OK )
1809 _computeState = READY_TO_COMPUTE;
1811 case SUBMESH_COMPUTED: // nothing to do
1813 case SUBMESH_RESTORED:
1814 ComputeStateEngine( CHECK_COMPUTE_STATE );
1815 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1817 if (algo) algo->SubmeshRestored( this );
1819 case MESH_ENTITY_REMOVED:
1820 updateDependantsState ( CHECK_COMPUTE_STATE );
1821 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1822 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1824 case CHECK_COMPUTE_STATE:
1825 if ( !IsMeshComputed() ) {
1826 if (_algoState == HYP_OK)
1827 _computeState = READY_TO_COMPUTE;
1829 _computeState = NOT_READY;
1832 case SUBMESH_LOADED:
1833 // already treated event, thanks to which _computeState == COMPUTE_OK
1841 // ----------------------------------------------------------------------
1843 case FAILED_TO_COMPUTE:
1846 case MODIF_ALGO_STATE:
1848 ComputeStateEngine( CLEAN );
1850 if (algo && !algo->NeedDiscreteBoundary())
1851 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1852 if (_algoState == HYP_OK)
1853 _computeState = READY_TO_COMPUTE;
1855 _computeState = NOT_READY;
1857 case COMPUTE: // nothing to do
1858 case COMPUTE_SUBMESH:
1860 case COMPUTE_CANCELED:
1863 algo->CancelCompute();
1867 cleanDependants(); // submeshes dependent on me should be cleaned as well
1868 removeSubMeshElementsAndNodes();
1870 case SUBMESH_COMPUTED: // allow retry compute
1871 if ( IsEmpty() ) // 23061
1873 if (_algoState == HYP_OK)
1874 _computeState = READY_TO_COMPUTE;
1876 _computeState = NOT_READY;
1879 case SUBMESH_RESTORED:
1880 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1882 case MESH_ENTITY_REMOVED:
1884 case CHECK_COMPUTE_STATE:
1885 if ( IsMeshComputed() )
1886 _computeState = COMPUTE_OK;
1888 if (_algoState == HYP_OK)
1889 _computeState = READY_TO_COMPUTE;
1891 _computeState = NOT_READY;
1893 // case SUBMESH_LOADED:
1901 // ----------------------------------------------------------------------
1907 //notifyListenersOnEvent( event, COMPUTE_EVENT );
1913 //=============================================================================
1917 //=============================================================================
1919 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1921 _computeError.reset();
1925 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1926 vector<smIdType> aVec(SMDSEntity_Last,0);
1927 aVec[SMDSEntity_Node] = 1;
1928 aResMap.insert(make_pair(this,aVec));
1932 //SMESH_Gen *gen = _father->GetGen();
1933 SMESH_Algo *algo = 0;
1934 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1937 if( algo && !aResMap.count( this ))
1939 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1940 if (!ret) return false;
1942 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1944 // check submeshes needed
1945 bool subMeshEvaluated = true;
1946 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1947 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1948 while ( smIt->more() && subMeshEvaluated )
1950 SMESH_subMesh* sm = smIt->next();
1951 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1952 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1953 const vector<smIdType> & nbs = aResMap[ sm ];
1954 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1956 if ( !subMeshEvaluated )
1959 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1961 if ( IsMeshComputed() )
1963 vector<smIdType> & nbEntities = aResMap[ this ];
1964 nbEntities.resize( SMDSEntity_Last, 0 );
1965 if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1967 nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1968 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
1969 while ( elemIt->more() )
1970 nbEntities[ elemIt->next()->GetEntityType() ]++;
1975 ret = algo->Evaluate((*_father), _subShape, aResMap);
1977 aResMap.insert( make_pair( this,vector<smIdType>(0)));
1984 //=======================================================================
1986 * \brief Update compute_state by _computeError and send proper events to
1987 * dependent submeshes
1988 * \retval bool - true if _computeError is NOT set
1990 //=======================================================================
1992 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1993 const bool theComputeOK,
1994 const TopoDS_Shape& theShape)
1996 bool noErrors = true;
1998 if ( !theShape.IsNull() )
2000 // Check state of submeshes
2001 if ( !theAlgo->NeedDiscreteBoundary())
2003 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
2004 while ( smIt->more() )
2005 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
2009 // Check state of neighbours
2010 if ( !theAlgo->OnlyUnaryInput() &&
2011 theShape.ShapeType() == TopAbs_COMPOUND &&
2012 !theShape.IsSame( _subShape ))
2014 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
2015 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
2017 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
2019 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
2026 // Set my _computeState
2028 if ( !_computeError || _computeError->IsOK() )
2030 // no error description is set to this sub-mesh, check if any mesh is computed
2031 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
2032 if ( _computeState != COMPUTE_OK )
2034 if ( _subShape.ShapeType() == TopAbs_EDGE &&
2035 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
2036 _computeState = COMPUTE_OK;
2037 else if ( theComputeOK )
2038 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
2042 if ( _computeError && !_computeError->IsOK() )
2044 if ( !_computeError->myAlgo )
2045 _computeError->myAlgo = theAlgo;
2049 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
2050 if (_computeError->IsCommon() )
2051 text << _computeError->CommonName();
2053 text << _computeError->myName;
2054 if ( _computeError->myComment.size() > 0 )
2055 text << " \"" << _computeError->myComment << "\"";
2059 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
2067 //=======================================================================
2068 //function : updateSubMeshState
2070 //=======================================================================
2072 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
2074 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
2075 while ( smIt->more() )
2076 smIt->next()->_computeState = theState;
2079 //=======================================================================
2080 //function : ComputeSubMeshStateEngine
2082 //=======================================================================
2084 void SMESH_subMesh::ComputeSubMeshStateEngine(compute_event event, const bool includeSelf)
2086 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
2087 while ( smIt->more() )
2088 smIt->next()->ComputeStateEngine(event);
2091 //=======================================================================
2092 //function : updateDependantsState
2094 //=======================================================================
2096 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
2098 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2099 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2101 ancestors[ iA ]->ComputeStateEngine( theEvent );
2105 //=======================================================================
2106 //function : cleanDependants
2108 //=======================================================================
2110 void SMESH_subMesh::cleanDependants()
2112 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2114 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2115 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2117 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2118 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2120 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2121 // will erase mesh on other shapes in a compound
2122 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2123 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2124 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2129 //=======================================================================
2130 //function : removeSubMeshElementsAndNodes
2132 //=======================================================================
2134 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2136 cleanSubMesh( this );
2138 // algo may bind a submesh not to _subShape, eg 3D algo
2139 // sets nodes on SHELL while _subShape may be SOLID
2141 int dim = SMESH_Gen::GetShapeDim( _subShape );
2142 int type = _subShape.ShapeType() + 1;
2143 for ( ; type <= TopAbs_EDGE; type++) {
2144 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2146 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2147 for ( ; exp.More(); exp.Next() )
2148 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2155 //=======================================================================
2156 //function : getCollection
2157 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2158 // meshed at once along with _subShape
2159 //=======================================================================
2161 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * /*theGen*/,
2162 SMESH_Algo* theAlgo,
2163 bool & theSubComputed,
2164 bool & theSubFailed,
2165 std::vector<SMESH_subMesh*>& theSubs)
2167 theSubComputed = SubMeshesComputed( & theSubFailed );
2169 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2171 if ( mainShape.IsSame( _subShape ))
2174 const bool skipAuxHyps = false;
2175 list<const SMESHDS_Hypothesis*> usedHyps =
2176 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2177 std::list < TopoDS_Shape > assiShapes = theAlgo->GetAssignedShapes();
2179 // put in a compound all shapes with the same hypothesis assigned
2180 // and a good ComputeState
2182 TopoDS_Compound aCompound;
2183 BRep_Builder aBuilder;
2184 aBuilder.MakeCompound( aCompound );
2188 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2189 while ( smIt->more() )
2191 SMESH_subMesh* subMesh = smIt->next();
2192 const TopoDS_Shape& S = subMesh->_subShape;
2193 if ( S.ShapeType() != this->_subShape.ShapeType() )
2195 if ( _allowedSubShapes && !_allowedSubShapes->IsEmpty() && !_allowedSubShapes->Contains( S ))
2197 if ( subMesh == this )
2199 aBuilder.Add( aCompound, S );
2200 theSubs.push_back( subMesh );
2202 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2204 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2205 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2206 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == usedHyps ) && // same hyps
2207 ( anAlgo->GetAssignedShapes() == assiShapes ) && // on same sub-shapes
2208 ( _algoShape == subMesh->_algoShape ))
2210 aBuilder.Add( aCompound, S );
2211 if ( !subMesh->SubMeshesComputed() )
2212 theSubComputed = false;
2213 theSubs.push_back( subMesh );
2218 return theSubs.size() == 1 ? theSubs[0]->GetSubShape() : aCompound;
2221 //=======================================================================
2222 //function : getSimilarAttached
2223 //purpose : return a hypothesis attached to theShape.
2224 // If theHyp is provided, similar but not same hypotheses
2225 // is returned; else only applicable ones having theHypType
2227 //=======================================================================
2229 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2230 const SMESH_Hypothesis * theHyp,
2231 const int theHypType)
2233 SMESH_HypoFilter hypoKind;
2234 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2236 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2237 hypoKind.AndNot( hypoKind.Is( theHyp ));
2238 if ( theHyp->IsAuxiliary() )
2239 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2241 hypoKind.AndNot( hypoKind.IsAuxiliary());
2244 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2247 return _father->GetHypothesis( theShape, hypoKind, false );
2250 //=======================================================================
2251 //function : CheckConcurrentHypothesis
2252 //purpose : check if there are several applicable hypothesis attached to
2254 //=======================================================================
2256 SMESH_Hypothesis::Hypothesis_Status
2257 SMESH_subMesh::CheckConcurrentHypothesis( SMESH_Hypothesis* theHypothesis)
2259 // is there local hypothesis on me?
2260 if ( getSimilarAttached( _subShape, theHypothesis ) )
2261 return SMESH_Hypothesis::HYP_OK;
2264 TopoDS_Shape aPrevWithHyp;
2265 const SMESH_Hypothesis* aPrevHyp = 0;
2266 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2267 for (; it.More(); it.Next())
2269 const TopoDS_Shape& ancestor = it.Value();
2270 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, theHypothesis );
2273 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2275 aPrevWithHyp = ancestor;
2278 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2279 return SMESH_Hypothesis::HYP_CONCURRENT;
2281 return SMESH_Hypothesis::HYP_OK;
2284 return SMESH_Hypothesis::HYP_OK;
2287 //================================================================================
2289 * \brief Constructor of OwnListenerData
2291 //================================================================================
2293 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2295 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2296 mySubMeshID( sm ? sm->GetId() : -1 ),
2301 //================================================================================
2303 * \brief Sets an event listener and its data to a submesh
2304 * \param listener - the listener to store
2305 * \param data - the listener data to store
2306 * \param where - the submesh to store the listener and it's data
2308 * It remembers the submesh where it puts the listener in order to delete
2309 * them when HYP_OK algo_state is lost
2310 * After being set, event listener is notified on each event of where submesh.
2312 //================================================================================
2314 void SMESH_subMesh::SetEventListener(EventListener* listener,
2315 EventListenerData* data,
2316 SMESH_subMesh* where)
2318 if ( listener && where ) {
2319 where->setEventListener( listener, data );
2320 _ownListeners.push_back( OwnListenerData( where, listener ));
2324 //================================================================================
2326 * \brief Sets an event listener and its data to a submesh
2327 * \param listener - the listener to store
2328 * \param data - the listener data to store
2330 * After being set, event listener is notified on each event of a submesh.
2332 //================================================================================
2334 void SMESH_subMesh::setEventListener(EventListener* listener,
2335 EventListenerData* data)
2337 map< EventListener*, EventListenerData* >::iterator l_d =
2338 _eventListeners.find( listener );
2339 if ( l_d != _eventListeners.end() ) {
2340 EventListenerData* curData = l_d->second;
2342 if ( curData && curData != data && curData->IsDeletable() )
2347 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2348 if ( listener->GetName() == l_d->first->GetName() )
2350 EventListenerData* curData = l_d->second;
2352 if ( curData && curData != data && curData->IsDeletable() )
2354 if ( l_d->first != listener && l_d->first->IsDeletable() )
2356 _eventListeners.erase( l_d );
2359 _eventListeners.insert( make_pair( listener, data ));
2363 //================================================================================
2365 * \brief Return an event listener data
2366 * \param listener - the listener whose data is
2367 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2368 * else returns a listener listening to events of this sub-mesh
2369 * \retval EventListenerData* - found data, maybe NULL
2371 //================================================================================
2373 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2374 const bool myOwn) const
2378 list< OwnListenerData >::const_iterator d;
2379 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2381 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2382 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2387 map< EventListener*, EventListenerData* >::const_iterator l_d =
2388 _eventListeners.find( listener );
2389 if ( l_d != _eventListeners.end() )
2395 //================================================================================
2397 * \brief Return an event listener data
2398 * \param listenerName - the listener name
2399 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2400 * else returns a listener listening to events of this sub-mesh
2401 * \retval EventListenerData* - found data, maybe NULL
2403 //================================================================================
2405 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2406 const bool myOwn) const
2410 list< OwnListenerData >::const_iterator d;
2411 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2413 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2414 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2419 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2420 for ( ; l_d != _eventListeners.end(); ++l_d )
2421 if ( listenerName == l_d->first->GetName() )
2427 //================================================================================
2429 * \brief Notify stored event listeners on the occurred event
2430 * \param event - algo_event or compute_event itself
2431 * \param eventType - algo_event or compute_event
2432 * \param hyp - hypothesis, if eventType is algo_event
2434 //================================================================================
2436 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2437 const event_type eventType,
2438 SMESH_Hypothesis* hyp)
2440 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2441 _eventListeners.end());
2442 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2443 for ( ; l_d != eventListeners.end(); ++l_d )
2445 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2446 if ( !_eventListeners.count( li_da.first )) continue;
2448 if ( li_da.first->myBusySM.insert( this ).second )
2450 const bool isDeletable = li_da.first->IsDeletable();
2452 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2454 if ( !isDeletable || _eventListeners.count( li_da.first ))
2455 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2460 //================================================================================
2462 * \brief Unregister the listener and delete listener's data
2463 * \param listener - the event listener
2465 //================================================================================
2467 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2469 map< EventListener*, EventListenerData* >::iterator l_d =
2470 _eventListeners.find( listener );
2471 if ( l_d != _eventListeners.end() && l_d->first )
2473 if ( l_d->second && l_d->second->IsDeletable() )
2477 l_d->first->myBusySM.erase( this );
2478 if ( l_d->first->IsDeletable() )
2480 l_d->first->BeforeDelete( this, l_d->second );
2483 _eventListeners.erase( l_d );
2487 //================================================================================
2489 * \brief Delete event listeners depending on algo of this submesh
2491 //================================================================================
2493 void SMESH_subMesh::deleteOwnListeners()
2495 list< OwnListenerData >::iterator d;
2496 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2498 SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2499 if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2501 d->mySubMesh->DeleteEventListener( d->myListener );
2503 _ownListeners.clear();
2506 //=======================================================================
2507 //function : loadDependentMeshes
2508 //purpose : loads dependent meshes on SUBMESH_LOADED event
2509 //=======================================================================
2511 void SMESH_subMesh::loadDependentMeshes()
2513 list< OwnListenerData >::iterator d;
2514 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2515 if ( _father != d->mySubMesh->_father &&
2516 _father->FindMesh( d->myMeshID ))
2517 d->mySubMesh->_father->Load();
2519 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2520 // for ( ; l_d != _eventListeners.end(); ++l_d )
2521 // if ( l_d->second )
2523 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2524 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2525 // for ( ; sm != smList.end(); ++sm )
2526 // if ( _father != (*sm)->_father )
2527 // (*sm)->_father->Load();
2531 //================================================================================
2533 * \brief Do something on a certain event
2534 * \param event - algo_event or compute_event itself
2535 * \param eventType - algo_event or compute_event
2536 * \param subMesh - the submesh where the event occurs
2537 * \param data - listener data stored in the subMesh
2538 * \param hyp - hypothesis, if eventType is algo_event
2540 * The base implementation translates CLEAN event to the subMesh
2541 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2542 * successful COMPUTE event.
2544 //================================================================================
2546 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2547 const int eventType,
2548 SMESH_subMesh* subMesh,
2549 EventListenerData* data,
2550 const SMESH_Hypothesis* /*hyp*/)
2552 if ( data && !data->mySubMeshes.empty() &&
2553 eventType == SMESH_subMesh::COMPUTE_EVENT)
2555 ASSERT( data->mySubMeshes.front() != subMesh );
2556 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2557 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2559 case SMESH_subMesh::CLEAN:
2560 for ( ; smIt != smEnd; ++ smIt)
2561 (*smIt)->ComputeStateEngine( SMESH_subMesh::compute_event( event ));
2563 case SMESH_subMesh::COMPUTE:
2564 case SMESH_subMesh::COMPUTE_SUBMESH:
2565 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2566 for ( ; smIt != smEnd; ++ smIt)
2567 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2576 //================================================================================
2578 * \brief Iterator over submeshes and optionally prepended or appended one
2580 //================================================================================
2582 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2584 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2585 SMESH_subMesh* prepend,
2586 SMESH_subMesh* append): myAppend(append), myIt(subIt)
2588 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2589 if ( myCur == append ) append = 0;
2591 /// Return true if and only if there are other object in this iterator
2596 /// Return the current object and step to the next one
2597 virtual SMESH_subMesh* next()
2599 SMESH_subMesh* res = myCur;
2600 if ( myIt->more() ) { myCur = myIt->next(); }
2601 else { myCur = myAppend; myAppend = 0; }
2608 SMESH_subMesh *myAppend, *myCur;
2609 SMDS_Iterator<SMESH_subMesh*> *myIt;
2613 //================================================================================
2615 * \brief Return iterator on the submeshes this one depends on
2616 * \param includeSelf - this submesh to be returned also
2617 * \param reverse - if true, complex shape submeshes go first
2619 //================================================================================
2621 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2622 const bool reverse) const
2624 SMESH_subMesh *me = (SMESH_subMesh*) this;
2625 SMESH_subMesh *prepend=0, *append=0;
2626 if ( includeSelf ) {
2627 if ( reverse ) prepend = me;
2630 typedef map < int, SMESH_subMesh * > TMap;
2633 return SMESH_subMeshIteratorPtr
2634 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2637 return SMESH_subMeshIteratorPtr
2638 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2642 //================================================================================
2644 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2646 //================================================================================
2648 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2650 if ( _ancestors.empty() &&
2651 !_subShape.IsSame( _father->GetShapeToMesh() ))
2653 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2655 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2656 me->_ancestors.reserve( ancShapes.Extent() );
2658 // assure that all sub-meshes exist
2659 TopoDS_Shape mainShape = _father->GetShapeToMesh();
2660 if ( !mainShape.IsNull() )
2661 _father->GetSubMesh( mainShape )->DependsOn();
2663 TopTools_MapOfShape map;
2665 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2666 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2667 if ( map.Add( it.Value() ))
2668 me->_ancestors.push_back( sm );
2674 //================================================================================
2676 * \brief Clears the vector of ancestor sub-meshes
2678 //================================================================================
2680 void SMESH_subMesh::ClearAncestors()
2685 //================================================================================
2687 * \brief Find common submeshes (based on shared sub-shapes with other
2688 * \param theOther submesh to check
2689 * \param theSetOfCommon set of common submesh
2691 //================================================================================
2693 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2694 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2696 size_t oldNb = theSetOfCommon.size();
2698 // check main submeshes
2699 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2700 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2701 theSetOfCommon.insert( this );
2702 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2703 theSetOfCommon.insert( theOther );
2705 // check common submeshes
2706 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2707 for( ; mapIt != _mapDepend.end(); mapIt++ )
2708 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2709 theSetOfCommon.insert( (*mapIt).second );
2710 return oldNb < theSetOfCommon.size();