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 #define PRINT_WHO_COMPUTE_WHAT
69 //=============================================================================
71 * \brief Allocate some memory at construction and release it at destruction.
72 * Is used to be able to continue working after mesh generation breaks due to
75 //=============================================================================
80 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
81 ~MemoryReserve() { delete [] myBuf; }
84 //=============================================================================
86 * default constructor:
88 //=============================================================================
90 SMESH_subMesh::SMESH_subMesh(int Id,
92 SMESHDS_Mesh * meshDS,
93 const TopoDS_Shape & aSubShape)
95 _subShape = aSubShape;
96 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
99 _dependenceAnalysed = _alwaysComputed = false;
101 if (_subShape.ShapeType() == TopAbs_VERTEX)
104 _computeState = READY_TO_COMPUTE;
108 _algoState = NO_ALGO;
109 _computeState = NOT_READY;
111 _computeCost = 0; // how costly is to compute this sub-mesh
112 _realComputeCost = 0;
113 _allowedSubShapes = nullptr;
116 //=============================================================================
120 //=============================================================================
122 SMESH_subMesh::~SMESH_subMesh()
124 deleteOwnListeners();
127 //=============================================================================
131 //=============================================================================
133 int SMESH_subMesh::GetId() const
135 //MESSAGE("SMESH_subMesh::GetId");
139 //=============================================================================
143 //=============================================================================
145 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
147 // submesh appears in DS only when a mesher set nodes and elements on a shape
148 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
151 //=============================================================================
155 //=============================================================================
157 const SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS() const
159 return ((SMESH_subMesh*) this )->GetSubMeshDS();
162 //=============================================================================
166 //=============================================================================
168 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
170 if ( !GetSubMeshDS() ) {
171 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
172 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
174 return GetSubMeshDS();
177 //=============================================================================
181 //=============================================================================
183 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
185 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
186 while ( smIt->more() ) {
187 SMESH_subMesh *sm = smIt->next();
188 if ( sm->GetComputeState() == READY_TO_COMPUTE )
191 return 0; // nothing to compute
194 //================================================================================
196 * \brief Returns a current algorithm
198 //================================================================================
200 SMESH_Algo* SMESH_subMesh::GetAlgo() const
204 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
205 me->_algo = _father->GetGen()->GetAlgo( me, & me->_algoShape );
210 //================================================================================
212 * \brief Returns a current algorithm
214 //================================================================================
216 SMESH_Algo* SMESH_subMesh::CopyAlgo() const
218 //SMESH_Algo* algo = (SMESH_Algo*) new StdMeshers_Regular_1D(666, _father->GetGent());
224 //================================================================================
226 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
227 * none mesh entity is bound to it (PAL13615, 2nd part)
229 //================================================================================
231 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
233 _alwaysComputed = isAlCo;
234 if ( _alwaysComputed )
235 _computeState = COMPUTE_OK;
237 ComputeStateEngine( CHECK_COMPUTE_STATE );
240 //=======================================================================
242 * \brief Return true if no mesh entities is bound to the submesh
244 //=======================================================================
246 bool SMESH_subMesh::IsEmpty() const
248 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
249 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
253 //=======================================================================
254 //function : IsMeshComputed
255 //purpose : check if _subMeshDS contains mesh elements
256 //=======================================================================
258 bool SMESH_subMesh::IsMeshComputed() const
260 if ( _alwaysComputed )
262 // algo may bind a sub-mesh not to _subShape, eg 3D algo
263 // sets nodes on SHELL while _subShape may be SOLID
265 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
266 int dim = SMESH_Gen::GetShapeDim( _subShape );
267 int type = _subShape.ShapeType();
268 for ( ; type <= TopAbs_VERTEX; type++) {
269 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
271 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
272 for ( ; exp.More(); exp.Next() )
274 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
276 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
289 //================================================================================
291 * \brief Check if any upper level sub-shape is not computed.
292 * Used to update a sub-mesh icon
294 //================================================================================
296 bool SMESH_subMesh::IsComputedPartially() const
298 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(/*includeSelf=*/true,
299 /*SolidFirst=*/true);
300 bool allComputed = true;
301 TopAbs_ShapeEnum readyType = TopAbs_VERTEX; // max value
302 while ( smIt->more() && allComputed )
304 SMESH_subMesh* sm = smIt->next();
306 if ( sm->GetSubShape().ShapeType() > readyType )
307 break; // lower dimension -> stop
308 if ( sm->GetComputeState() != SMESH_subMesh::NOT_READY )
309 readyType = sm->GetSubShape().ShapeType();
311 switch ( sm->GetComputeState() )
313 case SMESH_subMesh::READY_TO_COMPUTE:
314 case SMESH_subMesh::FAILED_TO_COMPUTE:
315 allComputed = false;// sm->IsMeshComputed();
317 case SMESH_subMesh::NOT_READY:
318 case SMESH_subMesh::COMPUTE_OK:
325 //=============================================================================
327 * Return true if all sub-meshes have been meshed
329 //=============================================================================
331 bool SMESH_subMesh::SubMeshesComputed(bool * isFailedToCompute/*=0*/) const
333 int myDim = SMESH_Gen::GetShapeDim( _subShape );
334 int dimToCheck = myDim - 1;
335 bool subMeshesComputed = true;
336 if ( isFailedToCompute ) *isFailedToCompute = false;
337 // check sub-meshes with upper dimension => reverse iteration
338 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
339 while ( smIt->more() )
341 SMESH_subMesh *sm = smIt->next();
342 if ( sm->_alwaysComputed )
344 const TopoDS_Shape & ss = sm->GetSubShape();
346 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
347 // in checking of existence of edges if the algo needs only faces. Moreover,
348 // degenerated edges may have no sub-mesh, as after computing NETGEN_2D.
349 if ( !_algo || _algo->NeedDiscreteBoundary() ) {
350 int dim = SMESH_Gen::GetShapeDim( ss );
351 if (dim < dimToCheck)
352 break; // the rest sub-meshes are all of less dimension
354 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
355 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK ) ||
356 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
359 subMeshesComputed = false;
361 if ( isFailedToCompute && !(*isFailedToCompute) )
362 *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
364 if ( !isFailedToCompute )
368 return subMeshesComputed;
371 //================================================================================
373 * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
375 * \return int - the computation cost in abstract units.
377 //================================================================================
379 int SMESH_subMesh::GetComputeCost() const
381 return _realComputeCost;
384 //================================================================================
386 * \brief Return cost of computing this sub-mesh. The cost depends on the shape type
387 * and number of sub-meshes this one DependsOn().
388 * \return int - the computation cost in abstract units.
390 //================================================================================
392 int SMESH_subMesh::computeCost() const
397 switch ( _subShape.ShapeType() ) {
399 case TopAbs_SHELL: computeCost = 5000; break;
400 case TopAbs_FACE: computeCost = 500; break;
401 case TopAbs_EDGE: computeCost = 2; break;
402 default: computeCost = 1;
404 SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
405 while ( childIt->more() )
406 computeCost += childIt->next()->computeCost();
408 ((SMESH_subMesh*)this)->_computeCost = computeCost;
413 //=============================================================================
415 * Returns all sub-meshes this one depend on
417 //=============================================================================
419 const std::map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
421 if ( _dependenceAnalysed || !_father->HasShapeToMesh() )
424 int type = _subShape.ShapeType();
427 case TopAbs_COMPOUND:
429 list< TopoDS_Shape > compounds( 1, _subShape );
430 list< TopoDS_Shape >::iterator comp = compounds.begin();
431 for ( ; comp != compounds.end(); ++comp )
433 for ( TopoDS_Iterator sub( *comp ); sub.More(); sub.Next() )
434 switch ( sub.Value().ShapeType() )
436 case TopAbs_COMPOUND: compounds.push_back( sub.Value() ); break;
437 case TopAbs_COMPSOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
438 case TopAbs_SOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
439 case TopAbs_SHELL: insertDependence( sub.Value(), TopAbs_FACE ); break;
440 case TopAbs_FACE: insertDependence( sub.Value(), TopAbs_FACE ); break;
441 case TopAbs_WIRE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
442 case TopAbs_EDGE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
443 case TopAbs_VERTEX: insertDependence( sub.Value(), TopAbs_VERTEX ); break;
449 case TopAbs_COMPSOLID: insertDependence( _subShape, TopAbs_SOLID ); break;
450 case TopAbs_SOLID: insertDependence( _subShape, TopAbs_FACE );
451 { /*internal EDGE*/ insertDependence( _subShape, TopAbs_EDGE, TopAbs_WIRE ); break; }
452 case TopAbs_SHELL: insertDependence( _subShape, TopAbs_FACE ); break;
453 case TopAbs_FACE: insertDependence( _subShape, TopAbs_EDGE ); break;
454 case TopAbs_WIRE: insertDependence( _subShape, TopAbs_EDGE ); break;
455 case TopAbs_EDGE: insertDependence( _subShape, TopAbs_VERTEX ); break;
458 _dependenceAnalysed = true;
462 //================================================================================
464 * \brief Return a key for SMESH_subMesh::_mapDepend map
466 //================================================================================
470 int dependsOnMapKey( TopAbs_ShapeEnum type, int shapeID )
472 int ordType = 9 - int(type); // 2 = Vertex, 8 = CompSolid
474 cle += 10000000 * ordType; // sort map by ordType then index
477 int dependsOnMapKey( const SMESH_subMesh* sm )
479 return dependsOnMapKey( sm->GetSubShape().ShapeType(), sm->GetId() );
483 //=============================================================================
485 * Add sub-meshes on sub-shapes of a given type into the dependence map.
487 //=============================================================================
489 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
490 TopAbs_ShapeEnum aSubType,
491 TopAbs_ShapeEnum avoidType)
493 TopExp_Explorer sub( aShape, aSubType, avoidType );
494 for ( ; sub.More(); sub.Next() )
496 SMESH_subMesh *aSubMesh = _father->GetSubMesh( sub.Current() );
497 if ( aSubMesh->GetId() == 0 )
498 continue; // not a sub-shape of the shape to mesh
499 int cle = dependsOnMapKey( aSubMesh );
500 if ( _mapDepend.find( cle ) == _mapDepend.end())
502 _mapDepend[cle] = aSubMesh;
503 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
504 _mapDepend.insert( subMap.begin(), subMap.end() );
509 //================================================================================
511 * \brief Return \c true if \a this sub-mesh depends on \a other
513 //================================================================================
515 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
517 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
520 //================================================================================
522 * \brief Return \c true if \a this sub-mesh depends on a \a shape
524 //================================================================================
526 bool SMESH_subMesh::DependsOn( const int shapeID ) const
528 return DependsOn( _father->GetSubMeshContaining( shapeID ));
531 //=============================================================================
533 * Return a shape of \a this sub-mesh
535 //=============================================================================
537 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
542 //=======================================================================
543 //function : CanAddHypothesis
544 //purpose : return true if theHypothesis can be attached to me:
545 // its dimension is checked
546 //=======================================================================
548 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
550 int aHypDim = theHypothesis->GetDim();
551 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
552 // issue 21106. Forbid 3D mesh on the SHELL
553 // if (aHypDim == 3 && aShapeDim == 3) {
554 // // check case of open shell
555 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
556 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
559 if ( aHypDim <= aShapeDim )
565 //=======================================================================
566 //function : IsApplicableHypothesis
567 //purpose : check if this sub-mesh can be computed using a hypothesis
568 //=======================================================================
570 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis) const
572 if ( !_father->HasShapeToMesh() && _subShape.ShapeType() == TopAbs_SOLID )
573 return true; // true for the PseudoShape
575 return IsApplicableHypothesis( theHypothesis, _subShape.ShapeType() );
578 //=======================================================================
579 //function : IsApplicableHypothesis
580 //purpose : compare shape type and hypothesis type
581 //=======================================================================
583 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis,
584 const TopAbs_ShapeEnum theShapeType)
586 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
589 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
590 // issue 21106. Forbid 3D mesh on the SHELL
591 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
597 switch ( theShapeType ) {
602 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
605 // Special case for algorithms, building 2D mesh on a whole shell.
606 // Before this fix there was a problem after restoring from study,
607 // because in that case algorithm is assigned before hypothesis
608 // (on shell in problem case) and hypothesis is checked on faces
609 // (because it is 2D), where we have NO_ALGO state.
610 // Now 2D hypothesis is also applicable to shells.
611 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
614 // case TopAbs_COMPSOLID:
615 // case TopAbs_COMPOUND:
621 //================================================================================
623 * \brief Treats modification of hypotheses definition
624 * \param [in] event - what happens
625 * \param [in] anHyp - a hypothesis
626 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
628 * Optional description of a problematic situation (if any) can be retrieved
629 * via GetComputeError().
631 //================================================================================
633 SMESH_Hypothesis::Hypothesis_Status
634 SMESH_subMesh::AlgoStateEngine(algo_event event, SMESH_Hypothesis * anHyp)
636 // **** les retour des evenement shape sont significatifs
637 // (add ou remove fait ou non)
638 // le retour des evenement father n'indiquent pas que add ou remove fait
640 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
641 if ( _Id == 0 ) return ret; // not a sub-shape of the shape to mesh
643 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
644 SMESH_Algo* algo = 0;
647 if (_subShape.ShapeType() == TopAbs_VERTEX )
649 if ( anHyp->GetDim() != 0) {
650 if (event == ADD_HYP || event == ADD_ALGO)
651 return SMESH_Hypothesis::HYP_BAD_DIM;
653 return SMESH_Hypothesis::HYP_OK;
656 else if ( _algoState == HYP_OK ) {
657 // update default _algoState
658 if ( event != REMOVE_FATHER_ALGO )
660 _algoState = NO_ALGO;
663 _algoState = MISSING_HYP;
664 if ( event == REMOVE_FATHER_HYP ||
665 algo->CheckHypothesis(*_father,_subShape, aux_ret))
672 int oldAlgoState = _algoState;
673 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
674 SMESH_Algo* algoRequiringCleaning = 0;
676 bool isApplicableHyp = IsApplicableHypothesis( anHyp );
678 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
680 // -------------------------------------------
681 // check if a shape needed by algo is present
682 // -------------------------------------------
683 algo = static_cast< SMESH_Algo* >( anHyp );
684 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
685 return SMESH_Hypothesis::HYP_NEED_SHAPE;
686 // ----------------------
687 // check mesh conformity
688 // ----------------------
689 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
690 return SMESH_Hypothesis::HYP_NOTCONFORM;
692 // check if all-dimensional algo is hidden by other local one
693 if ( event == ADD_ALGO ) {
694 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
695 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
696 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
697 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
698 if ( !curAlgo->NeedDiscreteBoundary() && curAlgo != anHyp )
699 algoRequiringCleaning = curAlgo;
703 // ----------------------------------
704 // add a hypothesis to DS if possible
705 // ----------------------------------
706 if (event == ADD_HYP || event == ADD_ALGO)
708 if ( ! CanAddHypothesis( anHyp )) // check dimension
709 return SMESH_Hypothesis::HYP_BAD_DIM;
711 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
712 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
714 if ( !meshDS->AddHypothesis(_subShape, anHyp))
715 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
718 // --------------------------
719 // remove a hypothesis from DS
720 // --------------------------
721 if (event == REMOVE_HYP || event == REMOVE_ALGO)
723 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
724 return SMESH_Hypothesis::HYP_OK; // nothing changes
726 if (event == REMOVE_ALGO)
728 algo = dynamic_cast<SMESH_Algo*> (anHyp);
729 if (!algo->NeedDiscreteBoundary())
730 algoRequiringCleaning = algo;
734 // ------------------
735 // analyse algo state
736 // ------------------
737 if (!isApplicableHyp)
738 return ret; // not applicable hypotheses do not change algo state
740 if (( algo = GetAlgo()))
741 algo->InitComputeError();
746 // ----------------------------------------------------------------------
755 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
756 setAlgoState(HYP_OK);
757 else if ( algo->IsStatusFatal( aux_ret )) {
758 meshDS->RemoveHypothesis(_subShape, anHyp);
762 setAlgoState(MISSING_HYP);
769 case ADD_FATHER_ALGO: { // Algo just added in father
772 if ( algo == anHyp ) {
773 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
774 setAlgoState(HYP_OK);
776 setAlgoState(MISSING_HYP);
780 case REMOVE_FATHER_HYP:
782 case REMOVE_FATHER_ALGO: {
786 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
787 setAlgoState(HYP_OK);
789 setAlgoState(MISSING_HYP);
793 case MODIF_HYP: break;
800 // ----------------------------------------------------------------------
808 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
809 setAlgoState(HYP_OK);
810 if (SMESH_Hypothesis::IsStatusFatal( ret ))
811 meshDS->RemoveHypothesis(_subShape, anHyp);
812 else if (!_father->IsUsedHypothesis( anHyp, this ))
814 meshDS->RemoveHypothesis(_subShape, anHyp);
815 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
819 case ADD_ALGO: { //already existing algo : on father ?
822 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
823 setAlgoState(HYP_OK);
824 else if ( algo->IsStatusFatal( aux_ret )) {
825 meshDS->RemoveHypothesis(_subShape, anHyp);
829 setAlgoState(MISSING_HYP);
834 case REMOVE_ALGO: { // perhaps a father algo applies ?
836 if (algo == NULL) // no more algo applying on sub-shape...
838 setAlgoState(NO_ALGO);
842 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
843 setAlgoState(HYP_OK);
845 setAlgoState(MISSING_HYP);
849 case MODIF_HYP: // assigned hypothesis value may become good
850 case ADD_FATHER_HYP: {
853 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
854 setAlgoState(HYP_OK);
856 setAlgoState(MISSING_HYP);
859 case ADD_FATHER_ALGO: { // new father algo
862 if ( algo == anHyp ) {
863 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
864 setAlgoState(HYP_OK);
866 setAlgoState(MISSING_HYP);
870 case REMOVE_FATHER_HYP: // nothing to do
872 case REMOVE_FATHER_ALGO: {
874 if (algo == NULL) // no more applying algo on father
876 setAlgoState(NO_ALGO);
880 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
881 setAlgoState(HYP_OK);
883 setAlgoState(MISSING_HYP);
893 // ----------------------------------------------------------------------
901 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
903 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
904 // ret should be fatal: anHyp was not added
905 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
907 else if (!_father->IsUsedHypothesis( anHyp, this ))
908 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
910 if (SMESH_Hypothesis::IsStatusFatal( ret ))
912 MESSAGE("do not add extra hypothesis");
913 meshDS->RemoveHypothesis(_subShape, anHyp);
921 case ADD_ALGO: { //already existing algo : on father ?
923 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
924 // check if algo changes
926 f.Init( SMESH_HypoFilter::IsAlgo() );
927 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
928 f.AndNot( SMESH_HypoFilter::Is( algo ));
929 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
931 string( algo->GetName()) != prevAlgo->GetName())
933 oldAlgoState = NO_ALGO; // force setting event listener (#16648)
938 setAlgoState(MISSING_HYP);
944 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
945 setAlgoState(HYP_OK);
947 setAlgoState(MISSING_HYP);
951 case REMOVE_ALGO: { // perhaps a father algo applies ?
953 if (algo == NULL) // no more algo applying on sub-shape...
955 setAlgoState(NO_ALGO);
959 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
960 // check if algo remains
961 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
965 setAlgoState(MISSING_HYP);
969 case MODIF_HYP: // hypothesis value may become bad
970 case ADD_FATHER_HYP: { // new father hypothesis ?
973 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
975 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
979 setAlgoState(MISSING_HYP);
982 case ADD_FATHER_ALGO: {
984 if ( algo == anHyp ) { // a new algo on father
985 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
986 // check if algo changes
988 f.Init( SMESH_HypoFilter::IsAlgo() );
989 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
990 f.AndNot( SMESH_HypoFilter::Is( algo ));
991 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
993 string(algo->GetName()) != string(prevAlgo->GetName()) )
997 setAlgoState(MISSING_HYP);
1001 case REMOVE_FATHER_HYP: {
1004 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1005 // is there the same local hyp or maybe a new father algo applied?
1006 if ( !getSimilarAttached( _subShape, anHyp ) )
1010 setAlgoState(MISSING_HYP);
1013 case REMOVE_FATHER_ALGO: {
1014 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
1015 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
1016 algo = dynamic_cast<SMESH_Algo*> (anHyp);
1017 if (!algo->NeedDiscreteBoundary())
1018 algoRequiringCleaning = algo;
1020 if (algo == NULL) // no more applying algo on father
1022 setAlgoState(NO_ALGO);
1026 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1027 // check if algo changes
1028 if ( string(algo->GetName()) != string( anHyp->GetName()) )
1032 setAlgoState(MISSING_HYP);
1042 // ----------------------------------------------------------------------
1049 // detect algorithm hiding
1051 if ( ret == SMESH_Hypothesis::HYP_OK &&
1052 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) && algo &&
1053 algo->GetName() == anHyp->GetName() )
1056 SMESH_Gen* gen = _father->GetGen();
1057 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1058 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1059 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1060 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1061 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1064 if ( ret == SMESH_Hypothesis::HYP_OK &&
1065 !algo->NeedDiscreteBoundary() &&
1066 !algo->SupportSubmeshes())
1068 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1069 gen->GetAlgo( this, &algoAssignedTo );
1070 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1071 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1072 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1073 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1074 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1078 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1079 _computeError = _algo->GetComputeError();
1080 _algo->InitComputeError();
1083 bool stateChange = ( _algoState != oldAlgoState );
1085 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1086 algo->SetEventListener( this );
1088 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1091 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1093 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1094 deleteOwnListeners();
1095 SetIsAlwaysComputed( false );
1096 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1097 // restore default states
1098 _algoState = HYP_OK;
1099 _computeState = READY_TO_COMPUTE;
1103 if ( algoRequiringCleaning ) {
1104 // added or removed algo is all-dimensional
1105 ComputeStateEngine( CLEAN );
1106 cleanDependsOn( algoRequiringCleaning );
1107 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1110 if ( stateChange || modifiedHyp )
1111 ComputeStateEngine( MODIF_ALGO_STATE );
1113 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1118 //=======================================================================
1119 //function : IsConform
1120 //purpose : check if a conform mesh will be produced by the Algo
1121 //=======================================================================
1123 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1125 // MESSAGE( "SMESH_subMesh::IsConform" );
1126 if ( !theAlgo ) return false;
1128 // Suppose that theAlgo is applicable to _subShape, do not check it here
1129 //if ( !IsApplicableHypothesis( theAlgo )) return false;
1131 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1132 // on a sub-shape will be ignored by theAlgo
1133 if ( theAlgo->NeedDiscreteBoundary() ||
1134 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1137 // only local algo is to be checked
1138 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1139 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1142 // check algo attached to adjacent shapes
1144 // loop on one level down sub-meshes
1145 TopoDS_Iterator itsub( _subShape );
1146 for (; itsub.More(); itsub.Next())
1148 // loop on adjacent subShapes
1149 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1150 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1152 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1153 if ( _subShape.IsSame( adjacent )) continue;
1154 if ( adjacent.ShapeType() != _subShape.ShapeType())
1157 // check algo attached to smAdjacent
1158 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1160 !algo->NeedDiscreteBoundary() &&
1161 algo->OnlyUnaryInput())
1162 return false; // NOT CONFORM MESH WILL BE PRODUCED
1169 //=============================================================================
1173 //=============================================================================
1175 void SMESH_subMesh::setAlgoState(algo_state state)
1180 //================================================================================
1182 * \brief Send an event to sub-meshes
1183 * \param [in] event - the event
1184 * \param [in] anHyp - an hypothesis
1185 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1186 * reports a fatal result
1187 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1189 * Optional description of a problematic situation (if any) can be retrieved
1190 * via GetComputeError().
1192 //================================================================================
1194 SMESH_Hypothesis::Hypothesis_Status
1195 SMESH_subMesh::SubMeshesAlgoStateEngine(algo_event event,
1196 SMESH_Hypothesis * anHyp,
1199 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1200 //EAP: a wire (dim==1) should notify edges (dim==1)
1201 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1202 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1204 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1205 while ( smIt->more() ) {
1206 SMESH_subMesh* sm = smIt->next();
1207 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1211 _computeError = sm->_computeError;
1212 sm->_computeError.reset();
1213 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1221 //================================================================================
1223 * \brief Remove elements from sub-meshes.
1224 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1225 * causes the cleaning.
1227 //================================================================================
1229 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1231 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1232 /*complexShapeFirst=*/true);
1233 if ( _father->NbNodes() == 0 )
1235 while ( smIt->more() )
1236 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1238 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1240 while ( smIt->more() )
1241 smIt->next()->ComputeStateEngine(CLEAN);
1243 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1245 // find sub-meshes to keep elements on
1246 set< SMESH_subMesh* > smToKeep;
1247 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1248 bool toKeepPrevShapeType = false;
1249 while ( smIt->more() )
1251 SMESH_subMesh* sm = smIt->next();
1252 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1253 if ( !sm->IsEmpty() )
1255 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1256 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1257 if ( !sameShapeType )
1259 // check if the algo allows presence of global algos of dimension the algo
1260 // can generate it-self;
1261 // always keep a node on VERTEX, as this node can be shared by segments
1262 // lying on EDGEs not shared by the VERTEX of sm, due to MergeNodes (PAL23068)
1263 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1264 keepSubMeshes = ( algoRequiringCleaning->NeedLowerHyps( shapeDim ) || shapeDim == 0 );
1265 prevShapeType = sm->GetSubShape().ShapeType();
1266 toKeepPrevShapeType = keepSubMeshes;
1268 if ( !keepSubMeshes )
1270 // look for a local algo used to mesh sm
1271 TopoDS_Shape algoShape = SMESH_MesherHelper::GetShapeOfHypothesis
1272 ( algoRequiringCleaning, _subShape, _father );
1273 SMESH_HypoFilter moreLocalAlgo;
1274 moreLocalAlgo.Init( SMESH_HypoFilter::IsMoreLocalThan( algoShape, *_father ));
1275 moreLocalAlgo.And ( SMESH_HypoFilter::IsAlgo() );
1276 bool localAlgoFound = _father->GetHypothesis( sm->_subShape, moreLocalAlgo, true );
1277 keepSubMeshes = localAlgoFound;
1279 // remember all sub-meshes of sm
1280 if ( keepSubMeshes )
1282 SMESH_subMeshIteratorPtr smIt2 = sm->getDependsOnIterator(true);
1283 while ( smIt2->more() )
1284 smToKeep.insert( smIt2->next() );
1289 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1290 while ( smIt->more() )
1292 SMESH_subMesh* sm = smIt->next();
1293 if ( !smToKeep.count( sm ))
1294 sm->ComputeStateEngine(CLEAN);
1299 //=============================================================================
1303 //=============================================================================
1305 void SMESH_subMesh::DumpAlgoState(bool isMain)
1309 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1311 map < int, SMESH_subMesh * >::const_iterator itsub;
1312 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1314 SMESH_subMesh *sm = (*itsub).second;
1315 sm->DumpAlgoState(false);
1318 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1319 " type of shape " << _subShape.ShapeType());
1322 case NO_ALGO : MESSAGE(" AlgoState = NO_ALGO"); break;
1323 case MISSING_HYP : MESSAGE(" AlgoState = MISSING_HYP"); break;
1324 case HYP_OK : MESSAGE(" AlgoState = HYP_OK");break;
1326 switch (_computeState)
1328 case NOT_READY : MESSAGE(" ComputeState = NOT_READY");break;
1329 case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1330 case COMPUTE_OK : MESSAGE(" ComputeState = COMPUTE_OK");break;
1331 case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1335 //================================================================================
1337 * \brief Remove nodes and elements bound to submesh
1338 * \param subMesh - submesh containing nodes and elements
1340 //================================================================================
1342 static void cleanSubMesh( SMESH_subMesh * subMesh )
1345 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS())
1347 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1348 smIdType nbElems = subMeshDS->NbElements();
1350 for ( SMDS_ElemIteratorPtr ite = subMeshDS->GetElements(); ite->more(); )
1351 meshDS->RemoveFreeElement( ite->next(), subMeshDS );
1353 smIdType nbNodes = subMeshDS->NbNodes();
1355 for ( SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes(); itn->more() ; )
1357 const SMDS_MeshNode * node = itn->next();
1358 if ( node->NbInverseElements() == 0 )
1359 meshDS->RemoveFreeNode( node, subMeshDS );
1360 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1361 meshDS->RemoveNode( node );
1368 //=============================================================================
1372 //=============================================================================
1374 bool SMESH_subMesh::ComputeStateEngine(compute_event event)
1377 case MODIF_ALGO_STATE:
1379 case COMPUTE_SUBMESH:
1380 //case COMPUTE_CANCELED:
1382 //case SUBMESH_COMPUTED:
1383 //case SUBMESH_RESTORED:
1384 //case SUBMESH_LOADED:
1385 //case MESH_ENTITY_REMOVED:
1386 //case CHECK_COMPUTE_STATE:
1387 _computeError.reset(); break;
1391 if ( event == CLEAN )
1392 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1394 if (_subShape.ShapeType() == TopAbs_VERTEX)
1396 _computeState = READY_TO_COMPUTE;
1397 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1398 if ( smDS && smDS->NbNodes() )
1400 if ( event == CLEAN ) {
1402 cleanSubMesh( this );
1405 _computeState = COMPUTE_OK;
1407 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1408 && !_alwaysComputed )
1410 // LOCK: Adding node to mesh
1412 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1413 gp_Pnt P = BRep_Tool::Pnt(V);
1414 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1415 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1416 _computeState = COMPUTE_OK;
1421 if ( event == MODIF_ALGO_STATE )
1425 SMESH_Gen *gen = _father->GetGen();
1426 SMESH_Algo *algo = 0;
1428 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1429 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1431 switch (_computeState)
1434 // ----------------------------------------------------------------------
1439 case MODIF_ALGO_STATE:
1441 if (algo && !algo->NeedDiscreteBoundary())
1442 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1443 if ( _algoState == HYP_OK )
1444 _computeState = READY_TO_COMPUTE;
1446 case COMPUTE: // nothing to do
1447 case COMPUTE_SUBMESH:
1449 case COMPUTE_CANCELED: // nothing to do
1453 removeSubMeshElementsAndNodes();
1455 case SUBMESH_COMPUTED: // nothing to do
1457 case SUBMESH_RESTORED:
1458 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1460 case MESH_ENTITY_REMOVED:
1462 case SUBMESH_LOADED:
1463 loadDependentMeshes();
1464 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1467 case CHECK_COMPUTE_STATE:
1468 if ( IsMeshComputed() )
1469 _computeState = COMPUTE_OK;
1477 // ----------------------------------------------------------------------
1479 case READY_TO_COMPUTE:
1482 case MODIF_ALGO_STATE:
1483 _computeState = NOT_READY;
1487 if (!algo->NeedDiscreteBoundary())
1488 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1489 if ( _algoState == HYP_OK )
1490 _computeState = READY_TO_COMPUTE;
1494 case COMPUTE_NOGEOM: // no geometry; can be several algos
1495 if ( !_father->HasShapeToMesh() )
1497 algo = GetAlgo(); // current algo
1500 // apply algos in the order of increasing dimension
1501 std::list< const SMESHDS_Hypothesis * > algos = _father->GetHypothesisList( _subShape );
1502 for ( int t = SMESHDS_Hypothesis::ALGO_1D; t <= SMESHDS_Hypothesis::ALGO_3D; ++t )
1504 std::list<const SMESHDS_Hypothesis *>::iterator al = algos.begin();
1505 for ( ; al != algos.end(); ++al )
1506 if ( (*al)->GetType() == t )
1508 _algo = (SMESH_Algo*) *al;
1509 _computeState = READY_TO_COMPUTE;
1510 if ( !ComputeStateEngine( COMPUTE ))
1514 _algo = algo; // restore
1520 case COMPUTE_SUBMESH:
1523 SMESH_Algo* algo2 = CopyAlgo();
1524 cout << "Algo2" << algo2;
1527 //ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1528 //_father->Unlock();
1531 MESSAGE("***** verify compute state *****");
1532 _computeState = NOT_READY;
1533 setAlgoState(MISSING_HYP);
1536 TopoDS_Shape shape = _subShape;
1537 algo->SubMeshesToCompute().assign( 1, this );
1538 // check submeshes needed
1539 // Forcing to false for parallel run
1540 // TODO: Remove forced false
1541 if (_father->HasShapeToMesh() && false) {
1542 bool subComputed = false, subFailed = false;
1543 if (!algo->OnlyUnaryInput()) {
1544 // --- commented for bos#22320 to compute all sub-shapes at once if possible;
1545 // --- in case COMPUTE_SUBMESH, set of sub-shapes is limited
1546 // --- by calling SetAllowedSubShapes()
1547 // if ( event == COMPUTE )
1548 // shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToComput;
1550 // subComputed = SubMeshesComputed( & subFailed );
1551 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1554 subComputed = SubMeshesComputed();
1556 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1557 algo->SupportSubmeshes() ? !subFailed :
1558 ( !subComputed || _father->IsNotConformAllowed() ));
1561 _computeState = FAILED_TO_COMPUTE;
1562 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1564 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1565 "Unexpected computed sub-mesh",algo);
1571 // to restore cout that may be redirected by algo
1572 std::streambuf* coutBuffer = std::cout.rdbuf();
1574 //cleanDependants(); for "UseExisting_*D" algos
1575 //removeSubMeshElementsAndNodes();
1576 loadDependentMeshes();
1578 _computeState = FAILED_TO_COMPUTE;
1579 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1583 algo->InitComputeError();
1585 MemoryReserve aMemoryReserve;
1586 SMDS_Mesh::CheckMemory();
1587 Kernel_Utils::Localizer loc;
1588 if ( !_father->HasShapeToMesh() ) // no shape
1590 SMESH_MesherHelper helper( *_father );
1591 helper.SetSubShape( shape );
1592 helper.SetElementsOnShape( true );
1593 ret = algo->Compute(*_father, &helper );
1597 std::cout<<"Running compute for " << _father << " of shape type " << shape.ShapeType() << std::endl;
1598 ret = algo->Compute((*_father), shape);
1600 // algo can set _computeError of submesh
1601 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1603 catch ( ::SMESH_ComputeError& comperr ) {
1604 cout << " SMESH_ComputeError caught" << endl;
1605 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1606 *_computeError = comperr;
1608 catch ( std::bad_alloc& exc ) {
1609 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1610 if ( _computeError ) {
1611 _computeError->myName = COMPERR_MEMORY_PB;
1613 cleanSubMesh( this );
1616 catch ( Standard_OutOfMemory& exc ) {
1617 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1618 if ( _computeError ) {
1619 _computeError->myName = COMPERR_MEMORY_PB;
1621 cleanSubMesh( this );
1622 throw std::bad_alloc();
1624 catch (Standard_Failure& ex) {
1625 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1626 _computeError->myName = COMPERR_OCC_EXCEPTION;
1627 _computeError->myComment += ex.DynamicType()->Name();
1628 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1629 _computeError->myComment += ": ";
1630 _computeError->myComment += ex.GetMessageString();
1633 catch ( SALOME_Exception& S_ex ) {
1634 const int skipSalomeShift = 7; /* to skip "Salome " of
1635 "Salome Exception" prefix returned
1636 by SALOME_Exception::what() */
1637 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1638 _computeError->myName = COMPERR_SLM_EXCEPTION;
1639 _computeError->myComment = S_ex.what() + skipSalomeShift;
1641 catch ( std::exception& exc ) {
1642 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1643 _computeError->myName = COMPERR_STD_EXCEPTION;
1644 _computeError->myComment = exc.what();
1647 if ( _computeError )
1648 _computeError->myName = COMPERR_EXCEPTION;
1652 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1654 // check if an error reported on any sub-shape
1655 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1656 if ( isComputeErrorSet )
1658 // check if anything was built
1659 TopExp_Explorer subS(shape, _subShape.ShapeType());
1662 for (; ret && subS.More(); subS.Next())
1663 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1664 ( _subShape.ShapeType() != TopAbs_EDGE ||
1665 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1668 #ifdef PRINT_WHO_COMPUTE_WHAT
1669 for (subS.ReInit(); subS.More(); subS.Next())
1672 const std::list <const SMESHDS_Hypothesis *> hyps =
1673 _algo->GetUsedHypothesis( *_father, _subShape );
1675 SMESH_Comment hypStr;
1676 if ( !hyps.empty() )
1678 hypStr << hyps.front()->GetName() << " ";
1679 ((SMESHDS_Hypothesis*)hyps.front())->SaveTo( hypStr.Stream() );
1682 cout << _father->GetSubMesh( subS.Current() )->GetId()
1683 << " " << ( ret ? "OK" : "FAIL" )
1684 << " " << _algo->GetName()
1685 << " " << hypStr << endl;
1688 // Set _computeError
1689 if ( !ret && !isComputeErrorSet )
1691 for ( subS.ReInit(); subS.More(); subS.Next() )
1693 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1694 if ( !sm->IsMeshComputed() )
1696 if ( !sm->_computeError )
1697 sm->_computeError = SMESH_ComputeError::New();
1698 if ( sm->_computeError->IsOK() )
1699 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1700 sm->_computeState = FAILED_TO_COMPUTE;
1701 sm->_computeError->myAlgo = algo;
1705 if ( ret && _computeError && _computeError->myName != COMPERR_WARNING )
1707 _computeError.reset();
1710 // transform errors into warnings if it is caused by mesh edition (imp 0023068)
1711 if (!ret && _father->GetIsModified() )
1713 for (subS.ReInit(); subS.More(); subS.Next())
1715 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1716 if ( !sm->IsMeshComputed() && sm->_computeError )
1718 // check if there is a VERTEX w/o nodes
1719 // with READY_TO_COMPUTE state (after MergeNodes())
1720 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1721 while ( smIt->more() )
1723 SMESH_subMesh * vertSM = smIt->next();
1724 if ( vertSM->_subShape.ShapeType() != TopAbs_VERTEX ) break;
1725 if ( vertSM->GetComputeState() == READY_TO_COMPUTE )
1727 SMESHDS_SubMesh * ds = vertSM->GetSubMeshDS();
1728 if ( !ds || ds->NbNodes() == 0 )
1730 sm->_computeState = READY_TO_COMPUTE;
1731 sm->_computeError->myName = COMPERR_WARNING;
1740 // send event SUBMESH_COMPUTED
1742 if ( !algo->NeedDiscreteBoundary() )
1743 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1744 for (subS.ReInit(); subS.More(); subS.Next())
1746 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1747 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1748 while ( smIt->more() ) {
1750 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1751 sm->updateDependantsState( SUBMESH_COMPUTED );
1757 updateDependantsState( SUBMESH_COMPUTED );
1759 // let algo clear its data gathered while algo->Compute()
1761 //algo->CheckHypothesis((*_father), _subShape, hyp_status);
1762 //_father->Unlock();
1765 case COMPUTE_CANCELED: // nothing to do
1769 removeSubMeshElementsAndNodes();
1770 _computeState = NOT_READY;
1774 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1776 _computeState = READY_TO_COMPUTE;
1778 setAlgoState(MISSING_HYP);
1781 case SUBMESH_COMPUTED: // nothing to do
1783 case SUBMESH_RESTORED:
1784 // check if a mesh is already computed that may
1785 // happen after retrieval from a file
1786 ComputeStateEngine( CHECK_COMPUTE_STATE );
1787 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1789 if (algo) algo->SubmeshRestored( this );
1791 case MESH_ENTITY_REMOVED:
1793 case SUBMESH_LOADED:
1794 loadDependentMeshes();
1795 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1798 case CHECK_COMPUTE_STATE:
1799 if ( IsMeshComputed() )
1800 _computeState = COMPUTE_OK;
1801 else if ( _computeError && _computeError->IsKO() )
1802 _computeState = FAILED_TO_COMPUTE;
1810 // ----------------------------------------------------------------------
1815 case MODIF_ALGO_STATE:
1816 ComputeStateEngine( CLEAN );
1818 if (algo && !algo->NeedDiscreteBoundary())
1819 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1821 case COMPUTE: // nothing to do
1823 case COMPUTE_SUBMESH: // nothing to do
1825 case COMPUTE_CANCELED: // nothing to do
1828 cleanDependants(); // clean sub-meshes, dependent on this one, with event CLEAN
1829 removeSubMeshElementsAndNodes();
1830 _computeState = NOT_READY;
1831 if ( _algoState == HYP_OK )
1832 _computeState = READY_TO_COMPUTE;
1834 case SUBMESH_COMPUTED: // nothing to do
1836 case SUBMESH_RESTORED:
1837 ComputeStateEngine( CHECK_COMPUTE_STATE );
1838 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1840 if (algo) algo->SubmeshRestored( this );
1842 case MESH_ENTITY_REMOVED:
1843 updateDependantsState ( CHECK_COMPUTE_STATE );
1844 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1845 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1847 case CHECK_COMPUTE_STATE:
1848 if ( !IsMeshComputed() ) {
1849 if (_algoState == HYP_OK)
1850 _computeState = READY_TO_COMPUTE;
1852 _computeState = NOT_READY;
1855 case SUBMESH_LOADED:
1856 // already treated event, thanks to which _computeState == COMPUTE_OK
1864 // ----------------------------------------------------------------------
1866 case FAILED_TO_COMPUTE:
1869 case MODIF_ALGO_STATE:
1871 ComputeStateEngine( CLEAN );
1873 if (algo && !algo->NeedDiscreteBoundary())
1874 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1875 if (_algoState == HYP_OK)
1876 _computeState = READY_TO_COMPUTE;
1878 _computeState = NOT_READY;
1880 case COMPUTE: // nothing to do
1881 case COMPUTE_SUBMESH:
1883 case COMPUTE_CANCELED:
1886 algo->CancelCompute();
1890 cleanDependants(); // submeshes dependent on me should be cleaned as well
1891 removeSubMeshElementsAndNodes();
1893 case SUBMESH_COMPUTED: // allow retry compute
1894 if ( IsEmpty() ) // 23061
1896 if (_algoState == HYP_OK)
1897 _computeState = READY_TO_COMPUTE;
1899 _computeState = NOT_READY;
1902 case SUBMESH_RESTORED:
1903 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1905 case MESH_ENTITY_REMOVED:
1907 case CHECK_COMPUTE_STATE:
1908 if ( IsMeshComputed() )
1909 _computeState = COMPUTE_OK;
1911 if (_algoState == HYP_OK)
1912 _computeState = READY_TO_COMPUTE;
1914 _computeState = NOT_READY;
1916 // case SUBMESH_LOADED:
1924 // ----------------------------------------------------------------------
1930 //notifyListenersOnEvent( event, COMPUTE_EVENT );
1936 //=============================================================================
1940 //=============================================================================
1942 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1944 _computeError.reset();
1948 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1949 vector<smIdType> aVec(SMDSEntity_Last,0);
1950 aVec[SMDSEntity_Node] = 1;
1951 aResMap.insert(make_pair(this,aVec));
1955 //SMESH_Gen *gen = _father->GetGen();
1956 SMESH_Algo *algo = 0;
1957 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1960 if( algo && !aResMap.count( this ))
1962 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1963 if (!ret) return false;
1965 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1967 // check submeshes needed
1968 bool subMeshEvaluated = true;
1969 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1970 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1971 while ( smIt->more() && subMeshEvaluated )
1973 SMESH_subMesh* sm = smIt->next();
1974 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1975 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1976 const vector<smIdType> & nbs = aResMap[ sm ];
1977 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1979 if ( !subMeshEvaluated )
1982 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1984 if ( IsMeshComputed() )
1986 vector<smIdType> & nbEntities = aResMap[ this ];
1987 nbEntities.resize( SMDSEntity_Last, 0 );
1988 if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1990 nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1991 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
1992 while ( elemIt->more() )
1993 nbEntities[ elemIt->next()->GetEntityType() ]++;
1998 ret = algo->Evaluate((*_father), _subShape, aResMap);
2000 aResMap.insert( make_pair( this,vector<smIdType>(0)));
2007 //=======================================================================
2009 * \brief Update compute_state by _computeError and send proper events to
2010 * dependent submeshes
2011 * \retval bool - true if _computeError is NOT set
2013 //=======================================================================
2015 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
2016 const bool theComputeOK,
2017 const TopoDS_Shape& theShape)
2019 bool noErrors = true;
2021 if ( !theShape.IsNull() )
2023 // Check state of submeshes
2024 if ( !theAlgo->NeedDiscreteBoundary())
2026 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
2027 while ( smIt->more() )
2028 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
2032 // Check state of neighbours
2033 if ( !theAlgo->OnlyUnaryInput() &&
2034 theShape.ShapeType() == TopAbs_COMPOUND &&
2035 !theShape.IsSame( _subShape ))
2037 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
2038 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
2040 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
2042 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
2049 // Set my _computeState
2051 if ( !_computeError || _computeError->IsOK() )
2053 // no error description is set to this sub-mesh, check if any mesh is computed
2054 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
2055 if ( _computeState != COMPUTE_OK )
2057 if ( _subShape.ShapeType() == TopAbs_EDGE &&
2058 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
2059 _computeState = COMPUTE_OK;
2060 else if ( theComputeOK )
2061 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
2065 if ( _computeError && !_computeError->IsOK() )
2067 if ( !_computeError->myAlgo )
2068 _computeError->myAlgo = theAlgo;
2072 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
2073 if (_computeError->IsCommon() )
2074 text << _computeError->CommonName();
2076 text << _computeError->myName;
2077 if ( _computeError->myComment.size() > 0 )
2078 text << " \"" << _computeError->myComment << "\"";
2082 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
2090 //=======================================================================
2091 //function : updateSubMeshState
2093 //=======================================================================
2095 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
2097 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
2098 while ( smIt->more() )
2099 smIt->next()->_computeState = theState;
2102 //=======================================================================
2103 //function : ComputeSubMeshStateEngine
2105 //=======================================================================
2107 void SMESH_subMesh::ComputeSubMeshStateEngine(compute_event event, const bool includeSelf)
2109 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
2110 while ( smIt->more() )
2111 smIt->next()->ComputeStateEngine(event);
2114 //=======================================================================
2115 //function : updateDependantsState
2117 //=======================================================================
2119 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
2121 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2122 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2124 ancestors[ iA ]->ComputeStateEngine( theEvent );
2128 //=======================================================================
2129 //function : cleanDependants
2131 //=======================================================================
2133 void SMESH_subMesh::cleanDependants()
2135 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2137 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2138 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2140 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2141 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2143 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2144 // will erase mesh on other shapes in a compound
2145 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2146 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2147 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2152 //=======================================================================
2153 //function : removeSubMeshElementsAndNodes
2155 //=======================================================================
2157 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2159 cleanSubMesh( this );
2161 // algo may bind a submesh not to _subShape, eg 3D algo
2162 // sets nodes on SHELL while _subShape may be SOLID
2164 int dim = SMESH_Gen::GetShapeDim( _subShape );
2165 int type = _subShape.ShapeType() + 1;
2166 for ( ; type <= TopAbs_EDGE; type++) {
2167 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2169 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2170 for ( ; exp.More(); exp.Next() )
2171 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2178 //=======================================================================
2179 //function : getCollection
2180 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2181 // meshed at once along with _subShape
2182 //=======================================================================
2184 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * /*theGen*/,
2185 SMESH_Algo* theAlgo,
2186 bool & theSubComputed,
2187 bool & theSubFailed,
2188 std::vector<SMESH_subMesh*>& theSubs)
2190 theSubComputed = SubMeshesComputed( & theSubFailed );
2192 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2194 if ( mainShape.IsSame( _subShape ))
2197 const bool skipAuxHyps = false;
2199 list<const SMESHDS_Hypothesis*> usedHyps =
2200 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2202 std::list < TopoDS_Shape > assiShapes = theAlgo->GetAssignedShapes();
2204 // put in a compound all shapes with the same hypothesis assigned
2205 // and a good ComputeState
2207 TopoDS_Compound aCompound;
2208 BRep_Builder aBuilder;
2209 aBuilder.MakeCompound( aCompound );
2213 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2214 while ( smIt->more() )
2216 SMESH_subMesh* subMesh = smIt->next();
2217 const TopoDS_Shape& S = subMesh->_subShape;
2218 if ( S.ShapeType() != this->_subShape.ShapeType() )
2220 if ( _allowedSubShapes && !_allowedSubShapes->IsEmpty() && !_allowedSubShapes->Contains( S ))
2222 if ( subMesh == this )
2224 aBuilder.Add( aCompound, S );
2225 theSubs.push_back( subMesh );
2227 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2230 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2231 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2232 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == usedHyps ) && // same hyps
2233 ( anAlgo->GetAssignedShapes() == assiShapes ) && // on same sub-shapes
2234 ( _algoShape == subMesh->_algoShape ))
2236 aBuilder.Add( aCompound, S );
2237 if ( !subMesh->SubMeshesComputed() )
2238 theSubComputed = false;
2239 theSubs.push_back( subMesh );
2245 return theSubs.size() == 1 ? theSubs[0]->GetSubShape() : aCompound;
2248 //=======================================================================
2249 //function : getSimilarAttached
2250 //purpose : return a hypothesis attached to theShape.
2251 // If theHyp is provided, similar but not same hypotheses
2252 // is returned; else only applicable ones having theHypType
2254 //=======================================================================
2256 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2257 const SMESH_Hypothesis * theHyp,
2258 const int theHypType)
2260 SMESH_HypoFilter hypoKind;
2261 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2263 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2264 hypoKind.AndNot( hypoKind.Is( theHyp ));
2265 if ( theHyp->IsAuxiliary() )
2266 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2268 hypoKind.AndNot( hypoKind.IsAuxiliary());
2271 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2274 return _father->GetHypothesis( theShape, hypoKind, false );
2277 //=======================================================================
2278 //function : CheckConcurrentHypothesis
2279 //purpose : check if there are several applicable hypothesis attached to
2281 //=======================================================================
2283 SMESH_Hypothesis::Hypothesis_Status
2284 SMESH_subMesh::CheckConcurrentHypothesis( SMESH_Hypothesis* theHypothesis)
2286 // is there local hypothesis on me?
2287 if ( getSimilarAttached( _subShape, theHypothesis ) )
2288 return SMESH_Hypothesis::HYP_OK;
2291 TopoDS_Shape aPrevWithHyp;
2292 const SMESH_Hypothesis* aPrevHyp = 0;
2293 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2294 for (; it.More(); it.Next())
2296 const TopoDS_Shape& ancestor = it.Value();
2297 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, theHypothesis );
2300 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2302 aPrevWithHyp = ancestor;
2305 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2306 return SMESH_Hypothesis::HYP_CONCURRENT;
2308 return SMESH_Hypothesis::HYP_OK;
2311 return SMESH_Hypothesis::HYP_OK;
2314 //================================================================================
2316 * \brief Constructor of OwnListenerData
2318 //================================================================================
2320 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2322 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2323 mySubMeshID( sm ? sm->GetId() : -1 ),
2328 //================================================================================
2330 * \brief Sets an event listener and its data to a submesh
2331 * \param listener - the listener to store
2332 * \param data - the listener data to store
2333 * \param where - the submesh to store the listener and it's data
2335 * It remembers the submesh where it puts the listener in order to delete
2336 * them when HYP_OK algo_state is lost
2337 * After being set, event listener is notified on each event of where submesh.
2339 //================================================================================
2341 void SMESH_subMesh::SetEventListener(EventListener* listener,
2342 EventListenerData* data,
2343 SMESH_subMesh* where)
2345 if ( listener && where ) {
2346 where->setEventListener( listener, data );
2347 _ownListeners.push_back( OwnListenerData( where, listener ));
2351 //================================================================================
2353 * \brief Sets an event listener and its data to a submesh
2354 * \param listener - the listener to store
2355 * \param data - the listener data to store
2357 * After being set, event listener is notified on each event of a submesh.
2359 //================================================================================
2361 void SMESH_subMesh::setEventListener(EventListener* listener,
2362 EventListenerData* data)
2364 map< EventListener*, EventListenerData* >::iterator l_d =
2365 _eventListeners.find( listener );
2366 if ( l_d != _eventListeners.end() ) {
2367 EventListenerData* curData = l_d->second;
2369 if ( curData && curData != data && curData->IsDeletable() )
2374 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2375 if ( listener->GetName() == l_d->first->GetName() )
2377 EventListenerData* curData = l_d->second;
2379 if ( curData && curData != data && curData->IsDeletable() )
2381 if ( l_d->first != listener && l_d->first->IsDeletable() )
2383 _eventListeners.erase( l_d );
2386 _eventListeners.insert( make_pair( listener, data ));
2390 //================================================================================
2392 * \brief Return an event listener data
2393 * \param listener - the listener whose data is
2394 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2395 * else returns a listener listening to events of this sub-mesh
2396 * \retval EventListenerData* - found data, maybe NULL
2398 //================================================================================
2400 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2401 const bool myOwn) const
2405 list< OwnListenerData >::const_iterator d;
2406 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2408 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2409 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2414 map< EventListener*, EventListenerData* >::const_iterator l_d =
2415 _eventListeners.find( listener );
2416 if ( l_d != _eventListeners.end() )
2422 //================================================================================
2424 * \brief Return an event listener data
2425 * \param listenerName - the listener name
2426 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2427 * else returns a listener listening to events of this sub-mesh
2428 * \retval EventListenerData* - found data, maybe NULL
2430 //================================================================================
2432 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2433 const bool myOwn) const
2437 list< OwnListenerData >::const_iterator d;
2438 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2440 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2441 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2446 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2447 for ( ; l_d != _eventListeners.end(); ++l_d )
2448 if ( listenerName == l_d->first->GetName() )
2454 //================================================================================
2456 * \brief Notify stored event listeners on the occurred event
2457 * \param event - algo_event or compute_event itself
2458 * \param eventType - algo_event or compute_event
2459 * \param hyp - hypothesis, if eventType is algo_event
2461 //================================================================================
2463 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2464 const event_type eventType,
2465 SMESH_Hypothesis* hyp)
2467 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2468 _eventListeners.end());
2469 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2470 for ( ; l_d != eventListeners.end(); ++l_d )
2472 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2473 if ( !_eventListeners.count( li_da.first )) continue;
2475 if ( li_da.first->myBusySM.insert( this ).second )
2477 const bool isDeletable = li_da.first->IsDeletable();
2479 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2481 if ( !isDeletable || _eventListeners.count( li_da.first ))
2482 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2487 //================================================================================
2489 * \brief Unregister the listener and delete listener's data
2490 * \param listener - the event listener
2492 //================================================================================
2494 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2496 map< EventListener*, EventListenerData* >::iterator l_d =
2497 _eventListeners.find( listener );
2498 if ( l_d != _eventListeners.end() && l_d->first )
2500 if ( l_d->second && l_d->second->IsDeletable() )
2504 l_d->first->myBusySM.erase( this );
2505 if ( l_d->first->IsDeletable() )
2507 l_d->first->BeforeDelete( this, l_d->second );
2510 _eventListeners.erase( l_d );
2514 //================================================================================
2516 * \brief Delete event listeners depending on algo of this submesh
2518 //================================================================================
2520 void SMESH_subMesh::deleteOwnListeners()
2522 list< OwnListenerData >::iterator d;
2523 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2525 SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2526 if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2528 d->mySubMesh->DeleteEventListener( d->myListener );
2530 _ownListeners.clear();
2533 //=======================================================================
2534 //function : loadDependentMeshes
2535 //purpose : loads dependent meshes on SUBMESH_LOADED event
2536 //=======================================================================
2538 void SMESH_subMesh::loadDependentMeshes()
2540 list< OwnListenerData >::iterator d;
2541 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2542 if ( _father != d->mySubMesh->_father &&
2543 _father->FindMesh( d->myMeshID ))
2544 d->mySubMesh->_father->Load();
2546 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2547 // for ( ; l_d != _eventListeners.end(); ++l_d )
2548 // if ( l_d->second )
2550 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2551 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2552 // for ( ; sm != smList.end(); ++sm )
2553 // if ( _father != (*sm)->_father )
2554 // (*sm)->_father->Load();
2558 //================================================================================
2560 * \brief Do something on a certain event
2561 * \param event - algo_event or compute_event itself
2562 * \param eventType - algo_event or compute_event
2563 * \param subMesh - the submesh where the event occurs
2564 * \param data - listener data stored in the subMesh
2565 * \param hyp - hypothesis, if eventType is algo_event
2567 * The base implementation translates CLEAN event to the subMesh
2568 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2569 * successful COMPUTE event.
2571 //================================================================================
2573 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2574 const int eventType,
2575 SMESH_subMesh* subMesh,
2576 EventListenerData* data,
2577 const SMESH_Hypothesis* /*hyp*/)
2579 if ( data && !data->mySubMeshes.empty() &&
2580 eventType == SMESH_subMesh::COMPUTE_EVENT)
2582 ASSERT( data->mySubMeshes.front() != subMesh );
2583 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2584 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2586 case SMESH_subMesh::CLEAN:
2587 for ( ; smIt != smEnd; ++ smIt)
2588 (*smIt)->ComputeStateEngine( SMESH_subMesh::compute_event( event ));
2590 case SMESH_subMesh::COMPUTE:
2591 case SMESH_subMesh::COMPUTE_SUBMESH:
2592 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2593 for ( ; smIt != smEnd; ++ smIt)
2594 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2603 //================================================================================
2605 * \brief Iterator over submeshes and optionally prepended or appended one
2607 //================================================================================
2609 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2611 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2612 SMESH_subMesh* prepend,
2613 SMESH_subMesh* append): myAppend(append), myIt(subIt)
2615 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2616 if ( myCur == append ) append = 0;
2618 /// Return true if and only if there are other object in this iterator
2623 /// Return the current object and step to the next one
2624 virtual SMESH_subMesh* next()
2626 SMESH_subMesh* res = myCur;
2627 if ( myIt->more() ) { myCur = myIt->next(); }
2628 else { myCur = myAppend; myAppend = 0; }
2635 SMESH_subMesh *myAppend, *myCur;
2636 SMDS_Iterator<SMESH_subMesh*> *myIt;
2640 //================================================================================
2642 * \brief Return iterator on the submeshes this one depends on
2643 * \param includeSelf - this submesh to be returned also
2644 * \param reverse - if true, complex shape submeshes go first
2646 //================================================================================
2648 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2649 const bool reverse) const
2651 SMESH_subMesh *me = (SMESH_subMesh*) this;
2652 SMESH_subMesh *prepend=0, *append=0;
2653 if ( includeSelf ) {
2654 if ( reverse ) prepend = me;
2657 typedef map < int, SMESH_subMesh * > TMap;
2660 return SMESH_subMeshIteratorPtr
2661 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2664 return SMESH_subMeshIteratorPtr
2665 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2669 //================================================================================
2671 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2673 //================================================================================
2675 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2677 if ( _ancestors.empty() &&
2678 !_subShape.IsSame( _father->GetShapeToMesh() ))
2680 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2682 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2683 me->_ancestors.reserve( ancShapes.Extent() );
2685 // assure that all sub-meshes exist
2686 TopoDS_Shape mainShape = _father->GetShapeToMesh();
2687 if ( !mainShape.IsNull() )
2688 _father->GetSubMesh( mainShape )->DependsOn();
2690 TopTools_MapOfShape map;
2692 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2693 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2694 if ( map.Add( it.Value() ))
2695 me->_ancestors.push_back( sm );
2701 //================================================================================
2703 * \brief Clears the vector of ancestor sub-meshes
2705 //================================================================================
2707 void SMESH_subMesh::ClearAncestors()
2712 //================================================================================
2714 * \brief Find common submeshes (based on shared sub-shapes with other
2715 * \param theOther submesh to check
2716 * \param theSetOfCommon set of common submesh
2718 //================================================================================
2720 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2721 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2723 size_t oldNb = theSetOfCommon.size();
2725 // check main submeshes
2726 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2727 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2728 theSetOfCommon.insert( this );
2729 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2730 theSetOfCommon.insert( theOther );
2732 // check common submeshes
2733 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2734 for( ; mapIt != _mapDepend.end(); mapIt++ )
2735 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2736 theSetOfCommon.insert( (*mapIt).second );
2737 return oldNb < theSetOfCommon.size();