1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
30 #include "SMESH_subMesh.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_Hypothesis.hxx"
34 #include "SMESH_Algo.hxx"
35 #include "SMESH_HypoFilter.hxx"
37 #include "utilities.h"
40 #include <BRep_Builder.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <TopTools_MapOfShape.hxx>
45 #include <TopTools_ListOfShape.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
51 #include <BRep_Tool.hxx>
53 #include <TopTools_IndexedMapOfShape.hxx>
56 //=============================================================================
58 * default constructor:
60 //=============================================================================
62 SMESH_subMesh::SMESH_subMesh(int Id, SMESH_Mesh * father, SMESHDS_Mesh * meshDS,
63 const TopoDS_Shape & aSubShape)
65 _subShape = aSubShape;
67 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
70 _dependenceAnalysed = false;
72 if (_subShape.ShapeType() == TopAbs_VERTEX)
75 _computeState = READY_TO_COMPUTE;
80 _computeState = NOT_READY;
84 //=============================================================================
88 //=============================================================================
90 SMESH_subMesh::~SMESH_subMesh()
92 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
96 //=============================================================================
100 //=============================================================================
102 int SMESH_subMesh::GetId() const
104 //MESSAGE("SMESH_subMesh::GetId");
108 //=============================================================================
112 //=============================================================================
114 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
116 //MESSAGE("SMESH_subMesh::GetSubMeshDS");
117 if (_subMeshDS==NULL)
119 //MESSAGE("subMesh pointer still null, trying to get it...");
120 _subMeshDS = _meshDS->MeshElements(_subShape); // may be null ...
121 if (_subMeshDS==NULL)
123 MESSAGE("problem... subMesh still empty");
125 //NRI throw SALOME_Exception(LOCALIZED(subMesh still empty));
131 //=============================================================================
135 //=============================================================================
137 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
139 if ( !GetSubMeshDS() )
140 _meshDS->NewSubMesh( _meshDS->ShapeToIndex( _subShape ) );
142 return GetSubMeshDS();
145 //=============================================================================
149 //=============================================================================
151 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
153 //MESSAGE("SMESH_subMesh::GetFirstToCompute");
154 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
155 SMESH_subMesh *firstToCompute = 0;
157 map < int, SMESH_subMesh * >::const_iterator itsub;
158 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
160 SMESH_subMesh *sm = (*itsub).second;
161 // SCRUTE(sm->GetId());
162 // SCRUTE(sm->GetComputeState());
163 bool readyToCompute = (sm->GetComputeState() == READY_TO_COMPUTE);
167 //SCRUTE(sm->GetId());
173 return firstToCompute; // a subMesh of this
175 if (_computeState == READY_TO_COMPUTE)
179 return 0; // nothing to compute
182 //=============================================================================
186 //=============================================================================
188 bool SMESH_subMesh::SubMeshesComputed()
190 //MESSAGE("SMESH_subMesh::SubMeshesComputed");
191 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
193 bool subMeshesComputed = true;
194 map < int, SMESH_subMesh * >::const_iterator itsub;
195 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
197 SMESH_subMesh *sm = (*itsub).second;
198 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
200 // There are some tricks with compute states, e.g. Penta_3D leaves
201 // one face with READY_TO_COMPUTE state in order to be able to
202 // recompute 3D when a locale triangle hypo changes (see PAL7428).
203 // So we check if mesh is really present
204 //bool computeOk = (sm->GetComputeState() == COMPUTE_OK);
205 bool computeOk = ( ds && ds->GetNodes()->more() );
208 const TopoDS_Shape & ss = sm->GetSubShape();
209 int type = ss.ShapeType();
211 subMeshesComputed = false;
215 case TopAbs_COMPOUND:
217 MESSAGE("The not computed sub mesh is a COMPOUND");
220 case TopAbs_COMPSOLID:
222 MESSAGE("The not computed sub mesh is a COMPSOLID");
227 MESSAGE("The not computed sub mesh is a SHEL");
232 MESSAGE("The not computed sub mesh is a WIRE");
237 MESSAGE("The not computed sub mesh is a SOLID");
242 MESSAGE("The not computed sub mesh is a FACE");
247 MESSAGE("The not computed sub mesh is a EDGE");
252 MESSAGE("The not computed sub mesh is of unknown type");
260 return subMeshesComputed;
263 //=============================================================================
267 //=============================================================================
269 bool SMESH_subMesh::SubMeshesReady()
271 MESSAGE("SMESH_subMesh::SubMeshesReady");
272 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
274 bool subMeshesReady = true;
275 map < int, SMESH_subMesh * >::const_iterator itsub;
276 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
278 SMESH_subMesh *sm = (*itsub).second;
279 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK)
280 || (sm->GetComputeState() == READY_TO_COMPUTE));
283 subMeshesReady = false;
288 return subMeshesReady;
291 //=============================================================================
293 * Construct dependence on first level subMeshes. complex shapes (compsolid,
294 * shell, wire) are not analysed the same way as simple shapes (solid, face,
296 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
297 * with possible multiples occurences. Multiples occurences corresponds to
298 * internal frontiers within shapes of the collection and must not be keeped.
299 * See FinalizeDependence.
301 //=============================================================================
303 const map < int, SMESH_subMesh * >&SMESH_subMesh::DependsOn()
305 if (_dependenceAnalysed)
308 //MESSAGE("SMESH_subMesh::DependsOn");
310 int type = _subShape.ShapeType();
314 case TopAbs_COMPOUND:
316 //MESSAGE("compound");
317 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
320 InsertDependence(exp.Current());
322 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
325 InsertDependence(exp.Current()); //only shell not in solid
327 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
330 InsertDependence(exp.Current());
332 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
335 InsertDependence(exp.Current());
339 case TopAbs_COMPSOLID:
341 //MESSAGE("compsolid");
342 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
345 InsertDependence(exp.Current());
352 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
355 InsertDependence(exp.Current());
362 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
365 InsertDependence(exp.Current());
372 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
375 InsertDependence(exp.Current());
382 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
385 InsertDependence(exp.Current());
392 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
395 InsertDependence(exp.Current());
408 _dependenceAnalysed = true;
412 //=============================================================================
414 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
416 //=============================================================================
418 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
420 //MESSAGE("SMESH_subMesh::InsertDependence");
421 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
422 int type = aSubShape.ShapeType();
423 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
424 int cle = aSubMesh->GetId();
425 cle += 10000000 * ordType; // sort map by ordType then index
426 if (_mapDepend.find(cle) == _mapDepend.end())
428 _mapDepend[cle] = aSubMesh;
429 const map < int, SMESH_subMesh * >&subMap = aSubMesh->DependsOn();
430 map < int, SMESH_subMesh * >::const_iterator im;
431 for (im = subMap.begin(); im != subMap.end(); im++)
433 int clesub = (*im).first;
434 SMESH_subMesh *sm = (*im).second;
435 if (_mapDepend.find(clesub) == _mapDepend.end())
436 _mapDepend[clesub] = sm;
442 //=============================================================================
446 //=============================================================================
448 const TopoDS_Shape & SMESH_subMesh::GetSubShape()
450 //MESSAGE("SMESH_subMesh::GetSubShape");
455 //=======================================================================
456 //function : CanAddHypothesis
457 //purpose : return true if theHypothesis can be attached to me:
458 // its dimention is checked
459 //=======================================================================
461 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
463 int aHypDim = theHypothesis->GetDim();
464 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
465 if ( aHypDim <= aShapeDim )
467 // if ( aHypDim < aShapeDim )
468 // return ( _father->IsMainShape( _subShape ));
473 //=======================================================================
474 //function : IsApplicableHypotesis
476 //=======================================================================
478 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
479 const TopAbs_ShapeEnum theShapeType)
481 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
483 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
487 switch ( theShapeType ) {
488 case TopAbs_EDGE: aShapeDim = 1; break;
489 case TopAbs_FACE: aShapeDim = 2; break;
490 case TopAbs_SHELL:aShapeDim = 3; break;
491 case TopAbs_SOLID:aShapeDim = 3; break;
492 // case TopAbs_VERTEX:
494 // case TopAbs_COMPSOLID:
495 // case TopAbs_COMPOUND:
496 default: return false;
499 return ( theHypothesis->GetDim() == aShapeDim );
502 //=============================================================================
506 //=============================================================================
508 SMESH_Hypothesis::Hypothesis_Status
509 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
511 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
512 //SCRUTE(_algoState);
515 // **** les retour des evenement shape sont significatifs
516 // (add ou remove fait ou non)
517 // le retour des evenement father n'indiquent pas que add ou remove fait
519 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
521 int dim = SMESH_Gen::GetShapeDim(_subShape);
526 if (event == ADD_HYP || event == ADD_ALGO)
527 return SMESH_Hypothesis::HYP_BAD_DIM; // do not allow to assign any hyp
529 return SMESH_Hypothesis::HYP_OK;
532 SMESH_Gen* gen =_father->GetGen();
534 int oldAlgoState = _algoState;
535 bool modifiedHyp = false; // if set to true, force event MODIF_ALGO_STATE
536 // in ComputeStateEngine
538 // ----------------------
539 // check mesh conformity
540 // ----------------------
541 if (event == ADD_ALGO)
543 if (IsApplicableHypotesis( anHyp ) &&
544 !_father->IsNotConformAllowed() &&
545 !IsConform( static_cast< SMESH_Algo* >( anHyp )))
546 return SMESH_Hypothesis::HYP_NOTCONFORM;
549 // ----------------------------------
550 // add a hypothesis to DS if possible
551 // ----------------------------------
552 if (event == ADD_HYP || event == ADD_ALGO)
554 if ( ! CanAddHypothesis( anHyp ))
555 return SMESH_Hypothesis::HYP_BAD_DIM;
557 if ( GetSimilarAttached( _subShape, anHyp ) )
558 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
560 if ( !_meshDS->AddHypothesis(_subShape, anHyp))
561 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
563 // Serve Propagation of 1D hypothesis
564 if (event == ADD_HYP) {
565 bool isPropagationOk = true;
566 string hypName = anHyp->GetName();
568 if (hypName == "Propagation") {
569 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
570 TopTools_MapOfShape aMap;
571 for (; exp.More(); exp.Next()) {
572 if (aMap.Add(exp.Current())) {
573 if (!_father->BuildPropagationChain(exp.Current())) {
574 isPropagationOk = false;
579 else if (anHyp->GetDim() == 1) { // Only 1D hypothesis can be propagated
580 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
581 TopTools_MapOfShape aMap;
582 for (; exp.More(); exp.Next()) {
583 if (aMap.Add(exp.Current())) {
584 TopoDS_Shape aMainEdge;
585 if (_father->IsPropagatedHypothesis(exp.Current(), aMainEdge)) {
586 isPropagationOk = _father->RebuildPropagationChains();
587 } else if (_father->IsPropagationHypothesis(exp.Current())) {
588 isPropagationOk = _father->BuildPropagationChain(exp.Current());
596 if (!isPropagationOk && ret < SMESH_Hypothesis::HYP_CONCURENT) {
597 ret = SMESH_Hypothesis::HYP_CONCURENT;
599 } // Serve Propagation of 1D hypothesis
602 // --------------------------
603 // remove a hypothesis from DS
604 // --------------------------
605 if (event == REMOVE_HYP || event == REMOVE_ALGO)
607 if (!_meshDS->RemoveHypothesis(_subShape, anHyp))
608 return SMESH_Hypothesis::HYP_OK; // nothing changes
610 // Serve Propagation of 1D hypothesis
611 if (event == REMOVE_HYP)
613 bool isPropagationOk = true;
614 SMESH_HypoFilter propagFilter( SMESH_HypoFilter::HasName( "Propagation" ));
615 if ( propagFilter.IsOk( anHyp, _subShape ))
617 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
618 TopTools_MapOfShape aMap;
619 for (; exp.More(); exp.Next()) {
620 if (aMap.Add(exp.Current()) &&
621 !_father->GetHypothesis( exp.Current(), propagFilter, true )) {
622 // no more Propagation on the current edge
623 if (!_father->RemovePropagationChain(exp.Current())) {
624 return SMESH_Hypothesis::HYP_UNKNOWN_FATAL;
628 // rebuild propagation chains, because removing one
629 // chain can resolve concurention, existing before
630 isPropagationOk = _father->RebuildPropagationChains();
632 else if (anHyp->GetDim() == 1) // Only 1D hypothesis can be propagated
634 isPropagationOk = _father->RebuildPropagationChains();
637 if (!isPropagationOk && ret < SMESH_Hypothesis::HYP_CONCURENT) {
638 ret = SMESH_Hypothesis::HYP_CONCURENT;
640 } // Serve Propagation of 1D hypothesis
643 // ------------------
644 // analyse algo state
645 // ------------------
646 if (!IsApplicableHypotesis( anHyp ))
647 return ret; // not applicable hypotheses do not change algo state
652 // ----------------------------------------------------------------------
659 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
661 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
662 SetAlgoState(HYP_OK);
664 SetAlgoState(MISSING_HYP);
673 case ADD_FATHER_ALGO: { // Algo just added in father
674 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
676 if ( algo == anHyp ) {
677 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
678 SetAlgoState(HYP_OK);
680 SetAlgoState(MISSING_HYP);
684 case REMOVE_FATHER_HYP:
686 case REMOVE_FATHER_ALGO: {
687 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
690 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
691 SetAlgoState(HYP_OK);
693 SetAlgoState(MISSING_HYP);
703 // ----------------------------------------------------------------------
709 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
711 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
712 SetAlgoState(HYP_OK);
713 if (SMESH_Hypothesis::IsStatusFatal( ret ))
714 _meshDS->RemoveHypothesis(_subShape, anHyp);
715 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
717 _meshDS->RemoveHypothesis(_subShape, anHyp);
718 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
722 case ADD_ALGO: { //already existing algo : on father ?
723 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
725 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
726 SetAlgoState(HYP_OK);
728 SetAlgoState(MISSING_HYP);
733 case REMOVE_ALGO: { // perhaps a father algo applies ?
734 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
735 if (algo == NULL) // no more algo applying on subShape...
737 SetAlgoState(NO_ALGO);
741 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
742 SetAlgoState(HYP_OK);
744 SetAlgoState(MISSING_HYP);
748 case ADD_FATHER_HYP: {
749 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
751 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
752 SetAlgoState(HYP_OK);
754 SetAlgoState(MISSING_HYP);
757 case ADD_FATHER_ALGO: { // new father algo
758 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
760 if ( algo == anHyp ) {
761 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
762 SetAlgoState(HYP_OK);
764 SetAlgoState(MISSING_HYP);
768 case REMOVE_FATHER_HYP: // nothing to do
770 case REMOVE_FATHER_ALGO: {
771 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
772 if (algo == NULL) // no more applying algo on father
774 SetAlgoState(NO_ALGO);
778 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
779 SetAlgoState(HYP_OK);
781 SetAlgoState(MISSING_HYP);
791 // ----------------------------------------------------------------------
797 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
799 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
801 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
802 // ret should be fatal: anHyp was not added
803 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
805 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
806 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
808 if (SMESH_Hypothesis::IsStatusFatal( ret ))
810 MESSAGE("do not add extra hypothesis");
811 _meshDS->RemoveHypothesis(_subShape, anHyp);
819 case ADD_ALGO: { //already existing algo : on father ?
820 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
821 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
822 // check if algo changes
824 f.Init( SMESH_HypoFilter::IsAlgo() );
825 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
826 f.AndNot( SMESH_HypoFilter::Is( algo ));
827 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
829 string(algo->GetName()) != string(prevAlgo->GetName()) )
833 SetAlgoState(MISSING_HYP);
837 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
839 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
840 SetAlgoState(HYP_OK);
842 SetAlgoState(MISSING_HYP);
846 case REMOVE_ALGO: { // perhaps a father algo applies ?
847 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
848 if (algo == NULL) // no more algo applying on subShape...
850 SetAlgoState(NO_ALGO);
854 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
855 // check if algo remains
856 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
860 SetAlgoState(MISSING_HYP);
864 case ADD_FATHER_HYP: { // new father hypothesis ?
865 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
867 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
869 if (_father->IsUsedHypothesis( anHyp, _subShape )) // new Hyp
873 SetAlgoState(MISSING_HYP);
876 case ADD_FATHER_ALGO: {
877 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
878 if ( algo == anHyp ) { // a new algo on father
879 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
880 // check if algo changes
882 f.Init( SMESH_HypoFilter::IsAlgo() );
883 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
884 f.AndNot( SMESH_HypoFilter::Is( algo ));
885 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
887 string(algo->GetName()) != string(prevAlgo->GetName()) )
891 SetAlgoState(MISSING_HYP);
895 case REMOVE_FATHER_HYP: {
896 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
898 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
899 // is there the same local hyp or maybe a new father algo applied?
900 if ( !GetSimilarAttached( _subShape, anHyp ) )
904 SetAlgoState(MISSING_HYP);
907 case REMOVE_FATHER_ALGO: {
908 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
909 if (algo == NULL) // no more applying algo on father
911 SetAlgoState(NO_ALGO);
915 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
916 // check if algo changes
917 if ( string(algo->GetName()) != string( anHyp->GetName()) )
921 SetAlgoState(MISSING_HYP);
931 // ----------------------------------------------------------------------
938 if ((_algoState != oldAlgoState) || modifiedHyp)
939 ComputeStateEngine(MODIF_ALGO_STATE);
945 //=======================================================================
946 //function : IsConform
947 //purpose : check if a conform mesh will be produced by the Algo
948 //=======================================================================
950 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
952 // MESSAGE( "SMESH_subMesh::IsConform" );
954 if ( !theAlgo ) return false;
956 // check only algo that doesn't NeedDescretBoundary(): because mesh made
957 // on a sub-shape will be ignored by theAlgo
958 if ( theAlgo->NeedDescretBoundary() )
961 SMESH_Gen* gen =_father->GetGen();
963 // only local algo is to be checked
964 if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
967 // check algo attached to adjacent shapes
969 // loop on one level down sub-meshes
970 TopoDS_Iterator itsub( _subShape );
971 for (; itsub.More(); itsub.Next())
973 // loop on adjacent subShapes
974 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
975 for (; it.More(); it.Next())
977 const TopoDS_Shape& adjacent = it.Value();
978 if ( _subShape.IsSame( adjacent )) continue;
979 if ( adjacent.ShapeType() != _subShape.ShapeType())
982 // check algo attached to smAdjacent
983 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
986 !algo->NeedDescretBoundary() /*&&
987 !gen->IsGlobalHypothesis( algo, *_father )*/)
988 return false; // NOT CONFORM MESH WILL BE PRODUCED
995 //=============================================================================
999 //=============================================================================
1001 void SMESH_subMesh::SetAlgoState(int state)
1003 // if (state != _oldAlgoState)
1004 // int retc = ComputeStateEngine(MODIF_ALGO_STATE);
1008 //=============================================================================
1012 //=============================================================================
1013 SMESH_Hypothesis::Hypothesis_Status
1014 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1015 SMESH_Hypothesis * anHyp)
1017 //MESSAGE("SMESH_subMesh::SubMeshesAlgoStateEngine");
1018 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1019 //EAP: a wire (dim==1) should notify edges (dim==1)
1020 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1021 if (/*EAP:dim > 1*/ _subShape.ShapeType() < TopAbs_EDGE )
1023 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1025 map < int, SMESH_subMesh * >::const_iterator itsub;
1026 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1028 SMESH_subMesh *sm = (*itsub).second;
1029 SMESH_Hypothesis::Hypothesis_Status ret2 =
1030 sm->AlgoStateEngine(event, anHyp);
1038 //=============================================================================
1042 //=============================================================================
1044 void SMESH_subMesh::CleanDependsOn()
1046 MESSAGE("SMESH_subMesh::CleanDependsOn");
1047 // **** parcourir les ancetres dans l'ordre de dépendance
1049 ComputeStateEngine(CLEAN);
1051 const map < int, SMESH_subMesh * >&dependson = DependsOn();
1052 map < int, SMESH_subMesh * >::const_iterator its;
1053 for (its = dependson.begin(); its != dependson.end(); its++)
1055 SMESH_subMesh *sm = (*its).second;
1056 SCRUTE((*its).first);
1057 sm->ComputeStateEngine(CLEAN);
1061 //=============================================================================
1065 //=============================================================================
1067 void SMESH_subMesh::DumpAlgoState(bool isMain)
1069 int dim = SMESH_Gen::GetShapeDim(_subShape);
1070 // if (dim < 1) return;
1073 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1075 map < int, SMESH_subMesh * >::const_iterator itsub;
1076 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1078 SMESH_subMesh *sm = (*itsub).second;
1079 sm->DumpAlgoState(false);
1082 int type = _subShape.ShapeType();
1083 MESSAGE("dim = " << dim << " type of shape " << type);
1087 MESSAGE(" AlgoState = NO_ALGO");
1090 MESSAGE(" AlgoState = MISSING_HYP");
1093 MESSAGE(" AlgoState = HYP_OK");
1096 switch (_computeState)
1099 MESSAGE(" ComputeState = NOT_READY");
1101 case READY_TO_COMPUTE:
1102 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1105 MESSAGE(" ComputeState = COMPUTE_OK");
1107 case FAILED_TO_COMPUTE:
1108 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1113 //=============================================================================
1117 //=============================================================================
1119 static void removeSubMesh( SMESHDS_Mesh * meshDS, const TopoDS_Shape& subShape)
1121 SMESHDS_SubMesh * subMeshDS = meshDS->MeshElements(subShape);
1122 if (subMeshDS!=NULL)
1124 SMDS_ElemIteratorPtr ite=subMeshDS->GetElements();
1127 const SMDS_MeshElement * elt = ite->next();
1128 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1129 meshDS->RemoveElement(elt);
1132 SMDS_NodeIteratorPtr itn=subMeshDS->GetNodes();
1135 const SMDS_MeshNode * node = itn->next();
1136 //MESSAGE( " RM node: "<<node->GetID());
1137 meshDS->RemoveNode(node);
1142 //=============================================================================
1146 //=============================================================================
1148 bool SMESH_subMesh::ComputeStateEngine(int event)
1150 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1151 //SCRUTE(_computeState);
1154 int dim = SMESH_Gen::GetShapeDim(_subShape);
1158 if ( IsMeshComputed() )
1159 _computeState = COMPUTE_OK;
1161 _computeState = READY_TO_COMPUTE;
1164 SMESH_Gen *gen = _father->GetGen();
1165 SMESH_Algo *algo = 0;
1167 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1169 switch (_computeState)
1172 // ----------------------------------------------------------------------
1177 case MODIF_HYP: // nothing to do
1179 case MODIF_ALGO_STATE:
1180 if (_algoState == HYP_OK)
1182 _computeState = READY_TO_COMPUTE;
1185 case COMPUTE: // nothing to do
1188 RemoveSubMeshElementsAndNodes();
1193 case SUBMESH_COMPUTED: // nothing to do
1195 case SUBMESH_RESTORED:
1196 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1198 case MESH_ENTITY_REMOVED:
1200 case CHECK_COMPUTE_STATE:
1201 if ( IsMeshComputed() )
1202 _computeState = COMPUTE_OK;
1210 // ----------------------------------------------------------------------
1212 case READY_TO_COMPUTE:
1215 case MODIF_HYP: // nothing to do
1217 case MODIF_ALGO_STATE:
1218 _computeState = NOT_READY;
1219 algo = gen->GetAlgo((*_father), _subShape);
1222 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1224 _computeState = READY_TO_COMPUTE;
1229 algo = gen->GetAlgo((*_father), _subShape);
1231 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1234 MESSAGE("***** verify compute state *****");
1235 _computeState = NOT_READY;
1238 // check submeshes needed
1239 if (algo->NeedDescretBoundary())
1240 ret = SubMeshesComputed();
1243 MESSAGE("Some SubMeshes not computed");
1244 _computeState = FAILED_TO_COMPUTE;
1248 removeSubMesh( _meshDS, _subShape );
1249 if (!algo->NeedDescretBoundary() && !algo->OnlyUnaryInput())
1250 ret = ApplyToCollection( algo, GetCollection( gen, algo ) );
1252 ret = algo->Compute((*_father), _subShape);
1256 MESSAGE("problem in algo execution: failed to compute");
1257 _computeState = FAILED_TO_COMPUTE;
1258 if (!algo->NeedDescretBoundary())
1259 UpdateSubMeshState( FAILED_TO_COMPUTE );
1262 // Show vertices location of a failed shape
1263 TopTools_IndexedMapOfShape vMap;
1264 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1265 for ( int iv = 1; iv <= vMap.Extent(); ++iv ) {
1266 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1267 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1274 _computeState = COMPUTE_OK;
1275 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1276 if (!algo->NeedDescretBoundary())
1277 UpdateSubMeshState( COMPUTE_OK );
1282 RemoveSubMeshElementsAndNodes();
1283 _computeState = NOT_READY;
1284 algo = gen->GetAlgo((*_father), _subShape);
1287 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1289 _computeState = READY_TO_COMPUTE;
1295 case SUBMESH_COMPUTED: // nothing to do
1297 case SUBMESH_RESTORED:
1298 // check if a mesh is already computed that may
1299 // happen after retrieval from a file
1300 ComputeStateEngine( CHECK_COMPUTE_STATE );
1301 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1303 case MESH_ENTITY_REMOVED:
1305 case CHECK_COMPUTE_STATE:
1306 if ( IsMeshComputed() )
1307 _computeState = COMPUTE_OK;
1315 // ----------------------------------------------------------------------
1321 CleanDependants(); // recursive recall with event CLEANDEP
1322 algo = gen->GetAlgo((*_father), _subShape);
1323 if (algo && !algo->NeedDescretBoundary())
1324 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1326 case MODIF_ALGO_STATE:
1327 CleanDependants(); // recursive recall with event CLEANDEP
1328 algo = gen->GetAlgo((*_father), _subShape);
1329 if (algo && !algo->NeedDescretBoundary())
1330 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1332 case COMPUTE: // nothing to do
1335 RemoveSubMeshElementsAndNodes();
1336 _computeState = NOT_READY;
1337 algo = gen->GetAlgo((*_father), _subShape);
1340 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1342 _computeState = READY_TO_COMPUTE;
1346 CleanDependants(); // recursive recall with event CLEANDEP
1348 case SUBMESH_COMPUTED: // nothing to do
1350 case SUBMESH_RESTORED:
1351 ComputeStateEngine( CHECK_COMPUTE_STATE );
1352 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1354 case MESH_ENTITY_REMOVED:
1355 UpdateDependantsState( CHECK_COMPUTE_STATE );
1356 ComputeStateEngine( CHECK_COMPUTE_STATE );
1357 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1359 case CHECK_COMPUTE_STATE:
1360 if ( !IsMeshComputed() )
1361 if (_algoState == HYP_OK)
1362 _computeState = READY_TO_COMPUTE;
1364 _computeState = NOT_READY;
1372 // ----------------------------------------------------------------------
1374 case FAILED_TO_COMPUTE:
1378 if (_algoState == HYP_OK)
1379 _computeState = READY_TO_COMPUTE;
1381 _computeState = NOT_READY;
1383 case MODIF_ALGO_STATE:
1384 if (_algoState == HYP_OK)
1385 _computeState = READY_TO_COMPUTE;
1387 _computeState = NOT_READY;
1389 case COMPUTE: // nothing to do
1392 RemoveSubMeshElementsAndNodes();
1393 if (_algoState == HYP_OK)
1394 _computeState = READY_TO_COMPUTE;
1396 _computeState = NOT_READY;
1401 case SUBMESH_COMPUTED: // allow retry compute
1402 if (_algoState == HYP_OK)
1403 _computeState = READY_TO_COMPUTE;
1405 _computeState = NOT_READY;
1407 case SUBMESH_RESTORED:
1408 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1410 case MESH_ENTITY_REMOVED:
1412 case CHECK_COMPUTE_STATE:
1413 if ( IsMeshComputed() )
1414 _computeState = COMPUTE_OK;
1416 if (_algoState == HYP_OK)
1417 _computeState = READY_TO_COMPUTE;
1419 _computeState = NOT_READY;
1427 // ----------------------------------------------------------------------
1433 //SCRUTE(_computeState);
1437 //=======================================================================
1438 //function : ApplyToCollection
1439 //purpose : Apply theAlgo to all subshapes in theCollection
1440 //=======================================================================
1442 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1443 const TopoDS_Shape& theCollection)
1445 MESSAGE("SMESH_subMesh::ApplyToCollection");
1446 ASSERT ( !theAlgo->NeedDescretBoundary() );
1451 ret = theAlgo->Compute( *_father, theCollection );
1453 // set _computeState of subshapes
1454 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1455 for ( ; anExplorer.More(); anExplorer.Next() )
1457 const TopoDS_Shape& aSubShape = anExplorer.Current();
1458 SMESH_subMesh* subMesh = _father->GetSubMeshContaining( aSubShape );
1463 subMesh->_computeState = COMPUTE_OK;
1464 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1465 subMesh->UpdateSubMeshState( COMPUTE_OK );
1469 subMesh->_computeState = FAILED_TO_COMPUTE;
1477 //=======================================================================
1478 //function : UpdateSubMeshState
1480 //=======================================================================
1482 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1484 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1485 map<int, SMESH_subMesh*>::const_iterator itsub;
1486 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1488 SMESH_subMesh* sm = (*itsub).second;
1489 sm->_computeState = theState;
1493 //=======================================================================
1494 //function : ComputeSubMeshStateEngine
1496 //=======================================================================
1498 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1500 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1501 map<int, SMESH_subMesh*>::const_iterator itsub;
1502 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1504 SMESH_subMesh* sm = (*itsub).second;
1505 sm->ComputeStateEngine(event);
1509 //=======================================================================
1510 //function : UpdateDependantsState
1512 //=======================================================================
1514 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1516 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1517 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1518 for (; it.More(); it.Next())
1520 const TopoDS_Shape& ancestor = it.Value();
1521 SMESH_subMesh *aSubMesh =
1522 _father->GetSubMeshContaining(ancestor);
1524 aSubMesh->ComputeStateEngine( theEvent );
1528 //=============================================================================
1532 //=============================================================================
1534 void SMESH_subMesh::CleanDependants()
1536 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1537 for (; it.More(); it.Next())
1539 const TopoDS_Shape& ancestor = it.Value();
1540 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEANDEP)
1541 // will erase mesh on other shapes in a compound
1542 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1543 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1545 aSubMesh->ComputeStateEngine(CLEANDEP);
1548 ComputeStateEngine(CLEAN);
1551 //=============================================================================
1555 //=============================================================================
1557 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1559 //SCRUTE(_subShape.ShapeType());
1561 removeSubMesh( _meshDS, _subShape );
1563 // algo may bind a submesh not to _subShape, eg 3D algo
1564 // sets nodes on SHELL while _subShape may be SOLID
1566 int dim = SMESH_Gen::GetShapeDim( _subShape );
1567 int type = _subShape.ShapeType() + 1;
1568 for ( ; type <= TopAbs_EDGE; type++)
1569 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1571 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1572 for ( ; exp.More(); exp.Next() )
1573 removeSubMesh( _meshDS, exp.Current() );
1579 //=======================================================================
1580 //function : IsMeshComputed
1581 //purpose : check if _subMeshDS contains mesh elements
1582 //=======================================================================
1584 bool SMESH_subMesh::IsMeshComputed() const
1586 // algo may bind a submesh not to _subShape, eg 3D algo
1587 // sets nodes on SHELL while _subShape may be SOLID
1589 int dim = SMESH_Gen::GetShapeDim( _subShape );
1590 int type = _subShape.ShapeType();
1591 for ( ; type <= TopAbs_VERTEX; type++) {
1592 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1594 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1595 for ( ; exp.More(); exp.Next() )
1597 SMESHDS_SubMesh * subMeshDS = _meshDS->MeshElements( exp.Current() );
1598 if ( subMeshDS != NULL &&
1599 (subMeshDS->GetElements()->more() || subMeshDS->GetNodes()->more())) {
1612 //=======================================================================
1613 //function : GetCollection
1614 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1615 // meshed at once along with _subShape
1616 //=======================================================================
1618 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1620 MESSAGE("SMESH_subMesh::GetCollection");
1621 ASSERT (!theAlgo->NeedDescretBoundary());
1623 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1625 if ( mainShape.IsSame( _subShape ))
1628 list<const SMESHDS_Hypothesis*> aUsedHyp =
1629 theAlgo->GetUsedHypothesis( *_father, _subShape ); // copy
1631 // put in a compound all shapes with the same hypothesis assigned
1632 // and a good ComputState
1634 TopoDS_Compound aCompound;
1635 BRep_Builder aBuilder;
1636 aBuilder.MakeCompound( aCompound );
1638 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1639 for ( ; anExplorer.More(); anExplorer.Next() )
1641 const TopoDS_Shape& S = anExplorer.Current();
1642 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1643 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1645 if (subMesh->GetComputeState() == READY_TO_COMPUTE &&
1646 anAlgo == theAlgo &&
1647 anAlgo->GetUsedHypothesis( *_father, S ) == aUsedHyp)
1649 aBuilder.Add( aCompound, S );
1656 //=======================================================================
1657 //function : GetSimilarAttached
1658 //purpose : return nb of hypotheses attached to theShape.
1659 // If theHyp is provided, similar but not same hypotheses
1660 // are countered; else only applicable ones having theHypType
1662 //=======================================================================
1664 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1665 const SMESH_Hypothesis * theHyp,
1666 const int theHypType)
1668 SMESH_HypoFilter filter;
1669 filter.Init( SMESH_HypoFilter::HasType( theHyp ? theHyp->GetType() : theHypType ));
1671 filter.And( SMESH_HypoFilter::HasDim( theHyp->GetDim() ));
1672 filter.AndNot( SMESH_HypoFilter::Is( theHyp ));
1675 filter.And( SMESH_HypoFilter::IsApplicableTo( theShape ));
1677 return _father->GetHypothesis( theShape, filter, false );
1680 //=======================================================================
1681 //function : CheckConcurentHypothesis
1682 //purpose : check if there are several applicable hypothesis attached to
1684 //=======================================================================
1686 SMESH_Hypothesis::Hypothesis_Status
1687 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1689 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1691 // is there local hypothesis on me?
1692 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1693 return SMESH_Hypothesis::HYP_OK;
1696 TopoDS_Shape aPrevWithHyp;
1697 const SMESH_Hypothesis* aPrevHyp = 0;
1698 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1699 for (; it.More(); it.Next())
1701 const TopoDS_Shape& ancestor = it.Value();
1702 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1705 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1707 aPrevWithHyp = ancestor;
1710 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1711 return SMESH_Hypothesis::HYP_CONCURENT;
1713 return SMESH_Hypothesis::HYP_OK;
1716 return SMESH_Hypothesis::HYP_OK;