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() || ds->GetElements()->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 sm->ComputeStateEngine(CLEAN);
1060 //=============================================================================
1064 //=============================================================================
1066 void SMESH_subMesh::DumpAlgoState(bool isMain)
1068 int dim = SMESH_Gen::GetShapeDim(_subShape);
1069 // if (dim < 1) return;
1072 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1074 map < int, SMESH_subMesh * >::const_iterator itsub;
1075 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1077 SMESH_subMesh *sm = (*itsub).second;
1078 sm->DumpAlgoState(false);
1081 int type = _subShape.ShapeType();
1082 MESSAGE("dim = " << dim << " type of shape " << type);
1086 MESSAGE(" AlgoState = NO_ALGO");
1089 MESSAGE(" AlgoState = MISSING_HYP");
1092 MESSAGE(" AlgoState = HYP_OK");
1095 switch (_computeState)
1098 MESSAGE(" ComputeState = NOT_READY");
1100 case READY_TO_COMPUTE:
1101 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1104 MESSAGE(" ComputeState = COMPUTE_OK");
1106 case FAILED_TO_COMPUTE:
1107 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1112 //=============================================================================
1116 //=============================================================================
1118 static void removeSubMesh( SMESHDS_Mesh * meshDS, const TopoDS_Shape& subShape)
1120 SMESHDS_SubMesh * subMeshDS = meshDS->MeshElements(subShape);
1121 if (subMeshDS!=NULL)
1123 SMDS_ElemIteratorPtr ite=subMeshDS->GetElements();
1126 const SMDS_MeshElement * elt = ite->next();
1127 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1128 meshDS->RemoveElement(elt);
1131 SMDS_NodeIteratorPtr itn=subMeshDS->GetNodes();
1134 const SMDS_MeshNode * node = itn->next();
1135 //MESSAGE( " RM node: "<<node->GetID());
1136 meshDS->RemoveNode(node);
1141 //=============================================================================
1145 //=============================================================================
1147 bool SMESH_subMesh::ComputeStateEngine(int event)
1149 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1150 //SCRUTE(_computeState);
1153 int dim = SMESH_Gen::GetShapeDim(_subShape);
1157 if ( IsMeshComputed() )
1158 _computeState = COMPUTE_OK;
1160 _computeState = READY_TO_COMPUTE;
1163 SMESH_Gen *gen = _father->GetGen();
1164 SMESH_Algo *algo = 0;
1166 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1168 switch (_computeState)
1171 // ----------------------------------------------------------------------
1176 case MODIF_HYP: // nothing to do
1178 case MODIF_ALGO_STATE:
1179 if (_algoState == HYP_OK)
1181 _computeState = READY_TO_COMPUTE;
1184 case COMPUTE: // nothing to do
1187 RemoveSubMeshElementsAndNodes();
1192 case SUBMESH_COMPUTED: // nothing to do
1194 case SUBMESH_RESTORED:
1195 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1197 case MESH_ENTITY_REMOVED:
1199 case CHECK_COMPUTE_STATE:
1200 if ( IsMeshComputed() )
1201 _computeState = COMPUTE_OK;
1209 // ----------------------------------------------------------------------
1211 case READY_TO_COMPUTE:
1214 case MODIF_HYP: // nothing to do
1216 case MODIF_ALGO_STATE:
1217 _computeState = NOT_READY;
1218 algo = gen->GetAlgo((*_father), _subShape);
1221 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1223 _computeState = READY_TO_COMPUTE;
1228 algo = gen->GetAlgo((*_father), _subShape);
1230 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1233 MESSAGE("***** verify compute state *****");
1234 _computeState = NOT_READY;
1237 // check submeshes needed
1238 if (algo->NeedDescretBoundary())
1239 ret = SubMeshesComputed();
1242 MESSAGE("Some SubMeshes not computed");
1243 _computeState = FAILED_TO_COMPUTE;
1248 //RemoveSubMeshElementsAndNodes();
1249 //removeSubMesh( _meshDS, _subShape );
1250 if (!algo->NeedDescretBoundary() && !algo->OnlyUnaryInput())
1251 ret = ApplyToCollection( algo, GetCollection( gen, algo ) );
1253 ret = algo->Compute((*_father), _subShape);
1257 MESSAGE("problem in algo execution: failed to compute");
1258 _computeState = FAILED_TO_COMPUTE;
1259 if (!algo->NeedDescretBoundary())
1260 UpdateSubMeshState( FAILED_TO_COMPUTE );
1263 // Show vertices location of a failed shape
1264 TopTools_IndexedMapOfShape vMap;
1265 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1266 for ( int iv = 1; iv <= vMap.Extent(); ++iv ) {
1267 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1268 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1275 _computeState = COMPUTE_OK;
1276 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1277 if (!algo->NeedDescretBoundary())
1278 UpdateSubMeshState( COMPUTE_OK );
1283 RemoveSubMeshElementsAndNodes();
1284 _computeState = NOT_READY;
1285 algo = gen->GetAlgo((*_father), _subShape);
1288 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1290 _computeState = READY_TO_COMPUTE;
1296 case SUBMESH_COMPUTED: // nothing to do
1298 case SUBMESH_RESTORED:
1299 // check if a mesh is already computed that may
1300 // happen after retrieval from a file
1301 ComputeStateEngine( CHECK_COMPUTE_STATE );
1302 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1304 case MESH_ENTITY_REMOVED:
1306 case CHECK_COMPUTE_STATE:
1307 if ( IsMeshComputed() )
1308 _computeState = COMPUTE_OK;
1316 // ----------------------------------------------------------------------
1322 CleanDependants(); // recursive recall with event CLEANDEP
1323 algo = gen->GetAlgo((*_father), _subShape);
1324 if (algo && !algo->NeedDescretBoundary())
1325 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1327 case MODIF_ALGO_STATE:
1328 CleanDependants(); // recursive recall with event CLEANDEP
1329 algo = gen->GetAlgo((*_father), _subShape);
1330 if (algo && !algo->NeedDescretBoundary())
1331 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1333 case COMPUTE: // nothing to do
1336 RemoveSubMeshElementsAndNodes();
1337 _computeState = NOT_READY;
1338 algo = gen->GetAlgo((*_father), _subShape);
1341 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1343 _computeState = READY_TO_COMPUTE;
1347 CleanDependants(); // recursive recall with event CLEANDEP
1349 case SUBMESH_COMPUTED: // nothing to do
1351 case SUBMESH_RESTORED:
1352 ComputeStateEngine( CHECK_COMPUTE_STATE );
1353 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1355 case MESH_ENTITY_REMOVED:
1356 UpdateDependantsState( CHECK_COMPUTE_STATE );
1357 ComputeStateEngine( CHECK_COMPUTE_STATE );
1358 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1360 case CHECK_COMPUTE_STATE:
1361 if ( !IsMeshComputed() )
1362 if (_algoState == HYP_OK)
1363 _computeState = READY_TO_COMPUTE;
1365 _computeState = NOT_READY;
1373 // ----------------------------------------------------------------------
1375 case FAILED_TO_COMPUTE:
1379 if (_algoState == HYP_OK)
1380 _computeState = READY_TO_COMPUTE;
1382 _computeState = NOT_READY;
1384 case MODIF_ALGO_STATE:
1385 if (_algoState == HYP_OK)
1386 _computeState = READY_TO_COMPUTE;
1388 _computeState = NOT_READY;
1390 case COMPUTE: // nothing to do
1393 RemoveSubMeshElementsAndNodes();
1394 if (_algoState == HYP_OK)
1395 _computeState = READY_TO_COMPUTE;
1397 _computeState = NOT_READY;
1402 case SUBMESH_COMPUTED: // allow retry compute
1403 if (_algoState == HYP_OK)
1404 _computeState = READY_TO_COMPUTE;
1406 _computeState = NOT_READY;
1408 case SUBMESH_RESTORED:
1409 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1411 case MESH_ENTITY_REMOVED:
1413 case CHECK_COMPUTE_STATE:
1414 if ( IsMeshComputed() )
1415 _computeState = COMPUTE_OK;
1417 if (_algoState == HYP_OK)
1418 _computeState = READY_TO_COMPUTE;
1420 _computeState = NOT_READY;
1428 // ----------------------------------------------------------------------
1434 //SCRUTE(_computeState);
1438 //=======================================================================
1439 //function : ApplyToCollection
1440 //purpose : Apply theAlgo to all subshapes in theCollection
1441 //=======================================================================
1443 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1444 const TopoDS_Shape& theCollection)
1446 MESSAGE("SMESH_subMesh::ApplyToCollection");
1447 ASSERT ( !theAlgo->NeedDescretBoundary() );
1452 ret = theAlgo->Compute( *_father, theCollection );
1454 // set _computeState of subshapes
1455 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1456 for ( ; anExplorer.More(); anExplorer.Next() )
1458 const TopoDS_Shape& aSubShape = anExplorer.Current();
1459 SMESH_subMesh* subMesh = _father->GetSubMeshContaining( aSubShape );
1464 subMesh->_computeState = COMPUTE_OK;
1465 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1466 subMesh->UpdateSubMeshState( COMPUTE_OK );
1470 subMesh->_computeState = FAILED_TO_COMPUTE;
1478 //=======================================================================
1479 //function : UpdateSubMeshState
1481 //=======================================================================
1483 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1485 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1486 map<int, SMESH_subMesh*>::const_iterator itsub;
1487 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1489 SMESH_subMesh* sm = (*itsub).second;
1490 sm->_computeState = theState;
1494 //=======================================================================
1495 //function : ComputeSubMeshStateEngine
1497 //=======================================================================
1499 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1501 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1502 map<int, SMESH_subMesh*>::const_iterator itsub;
1503 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1505 SMESH_subMesh* sm = (*itsub).second;
1506 sm->ComputeStateEngine(event);
1510 //=======================================================================
1511 //function : UpdateDependantsState
1513 //=======================================================================
1515 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1517 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1518 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1519 for (; it.More(); it.Next())
1521 const TopoDS_Shape& ancestor = it.Value();
1522 SMESH_subMesh *aSubMesh =
1523 _father->GetSubMeshContaining(ancestor);
1525 aSubMesh->ComputeStateEngine( theEvent );
1529 //=============================================================================
1533 //=============================================================================
1535 void SMESH_subMesh::CleanDependants()
1537 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1538 for (; it.More(); it.Next())
1540 const TopoDS_Shape& ancestor = it.Value();
1541 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEANDEP)
1542 // will erase mesh on other shapes in a compound
1543 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1544 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1546 aSubMesh->ComputeStateEngine(CLEANDEP);
1549 ComputeStateEngine(CLEAN);
1552 //=============================================================================
1556 //=============================================================================
1558 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1560 //SCRUTE(_subShape.ShapeType());
1562 removeSubMesh( _meshDS, _subShape );
1564 // algo may bind a submesh not to _subShape, eg 3D algo
1565 // sets nodes on SHELL while _subShape may be SOLID
1567 int dim = SMESH_Gen::GetShapeDim( _subShape );
1568 int type = _subShape.ShapeType() + 1;
1569 for ( ; type <= TopAbs_EDGE; type++)
1570 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1572 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1573 for ( ; exp.More(); exp.Next() )
1574 removeSubMesh( _meshDS, exp.Current() );
1580 //=======================================================================
1581 //function : IsMeshComputed
1582 //purpose : check if _subMeshDS contains mesh elements
1583 //=======================================================================
1585 bool SMESH_subMesh::IsMeshComputed() const
1587 // algo may bind a submesh not to _subShape, eg 3D algo
1588 // sets nodes on SHELL while _subShape may be SOLID
1590 int dim = SMESH_Gen::GetShapeDim( _subShape );
1591 int type = _subShape.ShapeType();
1592 for ( ; type <= TopAbs_VERTEX; type++) {
1593 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1595 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1596 for ( ; exp.More(); exp.Next() )
1598 SMESHDS_SubMesh * subMeshDS = _meshDS->MeshElements( exp.Current() );
1599 if ( subMeshDS != NULL &&
1600 (subMeshDS->GetElements()->more() || subMeshDS->GetNodes()->more())) {
1613 //=======================================================================
1614 //function : GetCollection
1615 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1616 // meshed at once along with _subShape
1617 //=======================================================================
1619 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1621 MESSAGE("SMESH_subMesh::GetCollection");
1622 ASSERT (!theAlgo->NeedDescretBoundary());
1624 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1626 if ( mainShape.IsSame( _subShape ))
1629 list<const SMESHDS_Hypothesis*> aUsedHyp =
1630 theAlgo->GetUsedHypothesis( *_father, _subShape ); // copy
1632 // put in a compound all shapes with the same hypothesis assigned
1633 // and a good ComputState
1635 TopoDS_Compound aCompound;
1636 BRep_Builder aBuilder;
1637 aBuilder.MakeCompound( aCompound );
1639 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1640 for ( ; anExplorer.More(); anExplorer.Next() )
1642 const TopoDS_Shape& S = anExplorer.Current();
1643 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1644 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1646 if (subMesh->GetComputeState() == READY_TO_COMPUTE &&
1647 anAlgo == theAlgo &&
1648 anAlgo->GetUsedHypothesis( *_father, S ) == aUsedHyp)
1650 aBuilder.Add( aCompound, S );
1657 //=======================================================================
1658 //function : GetSimilarAttached
1659 //purpose : return nb of hypotheses attached to theShape.
1660 // If theHyp is provided, similar but not same hypotheses
1661 // are countered; else only applicable ones having theHypType
1663 //=======================================================================
1665 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1666 const SMESH_Hypothesis * theHyp,
1667 const int theHypType)
1669 SMESH_HypoFilter filter;
1670 filter.Init( SMESH_HypoFilter::HasType( theHyp ? theHyp->GetType() : theHypType ));
1672 filter.And( SMESH_HypoFilter::HasDim( theHyp->GetDim() ));
1673 filter.AndNot( SMESH_HypoFilter::Is( theHyp ));
1676 filter.And( SMESH_HypoFilter::IsApplicableTo( theShape ));
1678 return _father->GetHypothesis( theShape, filter, false );
1681 //=======================================================================
1682 //function : CheckConcurentHypothesis
1683 //purpose : check if there are several applicable hypothesis attached to
1685 //=======================================================================
1687 SMESH_Hypothesis::Hypothesis_Status
1688 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1690 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1692 // is there local hypothesis on me?
1693 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1694 return SMESH_Hypothesis::HYP_OK;
1697 TopoDS_Shape aPrevWithHyp;
1698 const SMESH_Hypothesis* aPrevHyp = 0;
1699 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1700 for (; it.More(); it.Next())
1702 const TopoDS_Shape& ancestor = it.Value();
1703 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1706 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1708 aPrevWithHyp = ancestor;
1711 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1712 return SMESH_Hypothesis::HYP_CONCURENT;
1714 return SMESH_Hypothesis::HYP_OK;
1717 return SMESH_Hypothesis::HYP_OK;