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>
49 #include <TColStd_ListIteratorOfListOfInteger.hxx>
53 #include <BRep_Tool.hxx>
57 //=============================================================================
59 * default constructor:
61 //=============================================================================
63 SMESH_subMesh::SMESH_subMesh(int Id, SMESH_Mesh * father, SMESHDS_Mesh * meshDS,
64 const TopoDS_Shape & aSubShape)
66 _subShape = aSubShape;
68 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
71 _dependenceAnalysed = false;
73 if (_subShape.ShapeType() == TopAbs_VERTEX)
76 _computeState = READY_TO_COMPUTE;
81 _computeState = NOT_READY;
85 //=============================================================================
89 //=============================================================================
91 SMESH_subMesh::~SMESH_subMesh()
93 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
97 //=============================================================================
101 //=============================================================================
103 int SMESH_subMesh::GetId() const
105 //MESSAGE("SMESH_subMesh::GetId");
109 //=============================================================================
113 //=============================================================================
115 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
117 //MESSAGE("SMESH_subMesh::GetSubMeshDS");
118 if (_subMeshDS==NULL)
120 //MESSAGE("subMesh pointer still null, trying to get it...");
121 _subMeshDS = _meshDS->MeshElements(_subShape); // may be null ...
122 if (_subMeshDS==NULL)
124 MESSAGE("problem... subMesh still empty");
126 //NRI throw SALOME_Exception(LOCALIZED(subMesh still empty));
132 //=============================================================================
136 //=============================================================================
138 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
140 if ( !GetSubMeshDS() )
141 _meshDS->NewSubMesh( _meshDS->ShapeToIndex( _subShape ) );
143 return GetSubMeshDS();
146 //=============================================================================
150 //=============================================================================
152 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
154 //MESSAGE("SMESH_subMesh::GetFirstToCompute");
155 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
156 SMESH_subMesh *firstToCompute = 0;
158 map < int, SMESH_subMesh * >::const_iterator itsub;
159 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
161 SMESH_subMesh *sm = (*itsub).second;
162 // SCRUTE(sm->GetId());
163 // SCRUTE(sm->GetComputeState());
164 bool readyToCompute = (sm->GetComputeState() == READY_TO_COMPUTE);
168 //SCRUTE(sm->GetId());
174 return firstToCompute; // a subMesh of this
176 if (_computeState == READY_TO_COMPUTE)
180 return 0; // nothing to compute
183 //=============================================================================
187 //=============================================================================
189 bool SMESH_subMesh::SubMeshesComputed()
191 //MESSAGE("SMESH_subMesh::SubMeshesComputed");
192 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
194 bool subMeshesComputed = true;
195 map < int, SMESH_subMesh * >::const_iterator itsub;
196 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
198 SMESH_subMesh *sm = (*itsub).second;
200 const TopoDS_Shape & ss = sm->GetSubShape();
201 int type = ss.ShapeType();
202 bool computeOk = (sm->GetComputeState() == COMPUTE_OK);
205 subMeshesComputed = false;
209 case TopAbs_COMPOUND:
211 MESSAGE("The not computed sub mesh is a COMPOUND");
214 case TopAbs_COMPSOLID:
216 MESSAGE("The not computed sub mesh is a COMPSOLID");
221 MESSAGE("The not computed sub mesh is a SHEL");
226 MESSAGE("The not computed sub mesh is a WIRE");
231 MESSAGE("The not computed sub mesh is a SOLID");
236 MESSAGE("The not computed sub mesh is a FACE");
241 MESSAGE("The not computed sub mesh is a EDGE");
246 MESSAGE("The not computed sub mesh is of unknown type");
254 return subMeshesComputed;
257 //=============================================================================
261 //=============================================================================
263 bool SMESH_subMesh::SubMeshesReady()
265 MESSAGE("SMESH_subMesh::SubMeshesReady");
266 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
268 bool subMeshesReady = true;
269 map < int, SMESH_subMesh * >::const_iterator itsub;
270 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
272 SMESH_subMesh *sm = (*itsub).second;
273 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK)
274 || (sm->GetComputeState() == READY_TO_COMPUTE));
277 subMeshesReady = false;
282 return subMeshesReady;
285 //=============================================================================
287 * Construct dependence on first level subMeshes. complex shapes (compsolid,
288 * shell, wire) are not analysed the same way as simple shapes (solid, face,
290 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
291 * with possible multiples occurences. Multiples occurences corresponds to
292 * internal frontiers within shapes of the collection and must not be keeped.
293 * See FinalizeDependence.
295 //=============================================================================
297 const map < int, SMESH_subMesh * >&SMESH_subMesh::DependsOn()
299 if (_dependenceAnalysed)
302 //MESSAGE("SMESH_subMesh::DependsOn");
304 int type = _subShape.ShapeType();
308 case TopAbs_COMPOUND:
310 //MESSAGE("compound");
311 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
314 InsertDependence(exp.Current());
316 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
319 InsertDependence(exp.Current()); //only shell not in solid
321 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
324 InsertDependence(exp.Current());
326 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
329 InsertDependence(exp.Current());
333 case TopAbs_COMPSOLID:
335 //MESSAGE("compsolid");
336 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
339 InsertDependence(exp.Current());
346 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
349 InsertDependence(exp.Current());
356 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
359 InsertDependence(exp.Current());
366 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
369 InsertDependence(exp.Current());
376 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
379 InsertDependence(exp.Current());
386 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
389 InsertDependence(exp.Current());
402 _dependenceAnalysed = true;
406 //=============================================================================
408 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
410 //=============================================================================
412 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
414 //MESSAGE("SMESH_subMesh::InsertDependence");
415 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
416 int type = aSubShape.ShapeType();
417 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
418 int cle = aSubMesh->GetId();
419 cle += 10000000 * ordType; // sort map by ordType then index
420 if (_mapDepend.find(cle) == _mapDepend.end())
422 _mapDepend[cle] = aSubMesh;
423 const map < int, SMESH_subMesh * >&subMap = aSubMesh->DependsOn();
424 map < int, SMESH_subMesh * >::const_iterator im;
425 for (im = subMap.begin(); im != subMap.end(); im++)
427 int clesub = (*im).first;
428 SMESH_subMesh *sm = (*im).second;
429 if (_mapDepend.find(clesub) == _mapDepend.end())
430 _mapDepend[clesub] = sm;
436 //=============================================================================
440 //=============================================================================
442 const TopoDS_Shape & SMESH_subMesh::GetSubShape()
444 //MESSAGE("SMESH_subMesh::GetSubShape");
449 //=======================================================================
450 //function : CanAddHypothesis
451 //purpose : return true if theHypothesis can be attached to me:
452 // its dimention is checked
453 //=======================================================================
455 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
457 int aHypDim = theHypothesis->GetDim();
458 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
459 if ( aHypDim <= aShapeDim )
461 // if ( aHypDim < aShapeDim )
462 // return ( _father->IsMainShape( _subShape ));
467 //=======================================================================
468 //function : IsApplicableHypotesis
470 //=======================================================================
472 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
473 const TopAbs_ShapeEnum theShapeType)
475 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
477 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
481 switch ( theShapeType ) {
482 case TopAbs_EDGE: aShapeDim = 1; break;
483 case TopAbs_FACE: aShapeDim = 2; break;
484 case TopAbs_SHELL:aShapeDim = 3; break;
485 case TopAbs_SOLID:aShapeDim = 3; break;
486 // case TopAbs_VERTEX:
488 // case TopAbs_COMPSOLID:
489 // case TopAbs_COMPOUND:
490 default: return false;
493 return ( theHypothesis->GetDim() == aShapeDim );
496 //=============================================================================
500 //=============================================================================
502 SMESH_Hypothesis::Hypothesis_Status
503 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
505 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
506 //SCRUTE(_algoState);
509 // **** les retour des evenement shape sont significatifs
510 // (add ou remove fait ou non)
511 // le retour des evenement father n'indiquent pas que add ou remove fait
513 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
515 int dim = SMESH_Gen::GetShapeDim(_subShape);
520 if (event == ADD_HYP || event == ADD_ALGO)
521 return SMESH_Hypothesis::HYP_BAD_DIM; // do not allow to assign any hyp
523 return SMESH_Hypothesis::HYP_OK;
526 SMESH_Gen* gen =_father->GetGen();
528 int oldAlgoState = _algoState;
529 bool modifiedHyp = false; // if set to true, force event MODIF_ALGO_STATE
530 // in ComputeStateEngine
532 // ----------------------
533 // check mesh conformity
534 // ----------------------
535 if (event == ADD_ALGO)
537 if (IsApplicableHypotesis( anHyp ) &&
538 !_father->IsNotConformAllowed() &&
539 !IsConform( static_cast< SMESH_Algo* >( anHyp )))
540 return SMESH_Hypothesis::HYP_NOTCONFORM;
543 // ----------------------------------
544 // add a hypothesis to DS if possible
545 // ----------------------------------
546 if (event == ADD_HYP || event == ADD_ALGO)
548 if ( ! CanAddHypothesis( anHyp ))
549 return SMESH_Hypothesis::HYP_BAD_DIM;
551 if ( GetSimilarAttached( _subShape, anHyp ) )
552 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
554 if ( !_meshDS->AddHypothesis(_subShape, anHyp))
555 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
557 // Serve Propagation of 1D hypothesis
558 if (event == ADD_HYP) {
559 bool isPropagationOk = true;
560 string hypName = anHyp->GetName();
562 if (hypName == "Propagation") {
563 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
564 TopTools_MapOfShape aMap;
565 for (; exp.More(); exp.Next()) {
566 if (aMap.Add(exp.Current())) {
567 if (!_father->BuildPropagationChain(exp.Current())) {
568 isPropagationOk = false;
573 else if (anHyp->GetDim() == 1) { // Only 1D hypothesis can be propagated
574 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
575 TopTools_MapOfShape aMap;
576 for (; exp.More(); exp.Next()) {
577 if (aMap.Add(exp.Current())) {
578 TopoDS_Shape aMainEdge;
579 if (_father->IsPropagatedHypothesis(exp.Current(), aMainEdge)) {
580 isPropagationOk = _father->RebuildPropagationChains();
581 } else if (_father->IsPropagationHypothesis(exp.Current())) {
582 isPropagationOk = _father->BuildPropagationChain(exp.Current());
590 if (!isPropagationOk && ret < SMESH_Hypothesis::HYP_CONCURENT) {
591 ret = SMESH_Hypothesis::HYP_CONCURENT;
593 } // Serve Propagation of 1D hypothesis
596 // --------------------------
597 // remove a hypothesis from DS
598 // --------------------------
599 if (event == REMOVE_HYP || event == REMOVE_ALGO)
601 if (!_meshDS->RemoveHypothesis(_subShape, anHyp))
602 return SMESH_Hypothesis::HYP_OK; // nothing changes
604 // Serve Propagation of 1D hypothesis
605 if (event == REMOVE_HYP)
607 bool isPropagationOk = true;
608 SMESH_HypoFilter propagFilter( SMESH_HypoFilter::HasName( "Propagation" ));
609 if ( propagFilter.IsOk( anHyp, _subShape ))
611 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
612 TopTools_MapOfShape aMap;
613 for (; exp.More(); exp.Next()) {
614 if (aMap.Add(exp.Current()) &&
615 !_father->GetHypothesis( exp.Current(), propagFilter, true )) {
616 // no more Propagation on the current edge
617 if (!_father->RemovePropagationChain(exp.Current())) {
618 return SMESH_Hypothesis::HYP_UNKNOWN_FATAL;
622 // rebuild propagation chains, because removing one
623 // chain can resolve concurention, existing before
624 isPropagationOk = _father->RebuildPropagationChains();
626 else if (anHyp->GetDim() == 1) // Only 1D hypothesis can be propagated
628 isPropagationOk = _father->RebuildPropagationChains();
631 if (!isPropagationOk && ret < SMESH_Hypothesis::HYP_CONCURENT) {
632 ret = SMESH_Hypothesis::HYP_CONCURENT;
634 } // Serve Propagation of 1D hypothesis
637 // ------------------
638 // analyse algo state
639 // ------------------
640 if (!IsApplicableHypotesis( anHyp ))
641 return ret; // not applicable hypotheses do not change algo state
646 // ----------------------------------------------------------------------
653 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
655 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
656 SetAlgoState(HYP_OK);
658 SetAlgoState(MISSING_HYP);
667 case ADD_FATHER_ALGO: { // Algo just added in father
668 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
670 if ( algo == anHyp ) {
671 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
672 SetAlgoState(HYP_OK);
674 SetAlgoState(MISSING_HYP);
678 case REMOVE_FATHER_HYP:
680 case REMOVE_FATHER_ALGO: {
681 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
684 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
685 SetAlgoState(HYP_OK);
687 SetAlgoState(MISSING_HYP);
697 // ----------------------------------------------------------------------
703 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
705 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
706 SetAlgoState(HYP_OK);
707 if (SMESH_Hypothesis::IsStatusFatal( ret ))
708 _meshDS->RemoveHypothesis(_subShape, anHyp);
709 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
711 _meshDS->RemoveHypothesis(_subShape, anHyp);
712 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
716 case ADD_ALGO: { //already existing algo : on father ?
717 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
719 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
720 SetAlgoState(HYP_OK);
722 SetAlgoState(MISSING_HYP);
727 case REMOVE_ALGO: { // perhaps a father algo applies ?
728 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
729 if (algo == NULL) // no more algo applying on subShape...
731 SetAlgoState(NO_ALGO);
735 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
736 SetAlgoState(HYP_OK);
738 SetAlgoState(MISSING_HYP);
742 case ADD_FATHER_HYP: {
743 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
745 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
746 SetAlgoState(HYP_OK);
748 SetAlgoState(MISSING_HYP);
751 case ADD_FATHER_ALGO: { // new father algo
752 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
754 if ( algo == anHyp ) {
755 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
756 SetAlgoState(HYP_OK);
758 SetAlgoState(MISSING_HYP);
762 case REMOVE_FATHER_HYP: // nothing to do
764 case REMOVE_FATHER_ALGO: {
765 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
766 if (algo == NULL) // no more applying algo on father
768 SetAlgoState(NO_ALGO);
772 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
773 SetAlgoState(HYP_OK);
775 SetAlgoState(MISSING_HYP);
785 // ----------------------------------------------------------------------
791 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
793 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
795 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
796 // ret should be fatal: anHyp was not added
797 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
799 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
800 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
802 if (SMESH_Hypothesis::IsStatusFatal( ret ))
804 MESSAGE("do not add extra hypothesis");
805 _meshDS->RemoveHypothesis(_subShape, anHyp);
813 case ADD_ALGO: { //already existing algo : on father ?
814 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
815 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
816 // check if algo changes
818 f.Init( SMESH_HypoFilter::IsAlgo() );
819 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
820 f.AndNot( SMESH_HypoFilter::Is( algo ));
821 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
823 string(algo->GetName()) != string(prevAlgo->GetName()) )
827 SetAlgoState(MISSING_HYP);
831 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
833 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
834 SetAlgoState(HYP_OK);
836 SetAlgoState(MISSING_HYP);
840 case REMOVE_ALGO: { // perhaps a father algo applies ?
841 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
842 if (algo == NULL) // no more algo applying on subShape...
844 SetAlgoState(NO_ALGO);
848 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
849 // check if algo remains
850 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
854 SetAlgoState(MISSING_HYP);
858 case ADD_FATHER_HYP: { // new father hypothesis ?
859 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
861 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
863 if (_father->IsUsedHypothesis( anHyp, _subShape )) // new Hyp
867 SetAlgoState(MISSING_HYP);
870 case ADD_FATHER_ALGO: {
871 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
872 if ( algo == anHyp ) { // a new algo on father
873 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
874 // check if algo changes
876 f.Init( SMESH_HypoFilter::IsAlgo() );
877 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
878 f.AndNot( SMESH_HypoFilter::Is( algo ));
879 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
881 string(algo->GetName()) != string(prevAlgo->GetName()) )
885 SetAlgoState(MISSING_HYP);
889 case REMOVE_FATHER_HYP: {
890 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
892 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
893 // is there the same local hyp or maybe a new father algo applied?
894 if ( !GetSimilarAttached( _subShape, anHyp ) )
898 SetAlgoState(MISSING_HYP);
901 case REMOVE_FATHER_ALGO: {
902 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
903 if (algo == NULL) // no more applying algo on father
905 SetAlgoState(NO_ALGO);
909 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
910 // check if algo changes
911 if ( string(algo->GetName()) != string( anHyp->GetName()) )
915 SetAlgoState(MISSING_HYP);
925 // ----------------------------------------------------------------------
932 if ((_algoState != oldAlgoState) || modifiedHyp)
933 ComputeStateEngine(MODIF_ALGO_STATE);
939 //=======================================================================
940 //function : IsConform
941 //purpose : check if a conform mesh will be produced by the Algo
942 //=======================================================================
944 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
946 // MESSAGE( "SMESH_subMesh::IsConform" );
948 if ( !theAlgo ) return false;
950 // check only algo that doesn't NeedDescretBoundary(): because mesh made
951 // on a sub-shape will be ignored by theAlgo
952 if ( theAlgo->NeedDescretBoundary() )
955 SMESH_Gen* gen =_father->GetGen();
957 // only local algo is to be checked
958 if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
961 // check algo attached to adjacent shapes
963 // loop on one level down sub-meshes
964 TopoDS_Iterator itsub( _subShape );
965 for (; itsub.More(); itsub.Next())
967 // loop on adjacent subShapes
968 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
969 for (; it.More(); it.Next())
971 const TopoDS_Shape& adjacent = it.Value();
972 if ( _subShape.IsSame( adjacent )) continue;
973 if ( adjacent.ShapeType() != _subShape.ShapeType())
976 // check algo attached to smAdjacent
977 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
980 !algo->NeedDescretBoundary() /*&&
981 !gen->IsGlobalHypothesis( algo, *_father )*/)
982 return false; // NOT CONFORM MESH WILL BE PRODUCED
989 //=============================================================================
993 //=============================================================================
995 void SMESH_subMesh::SetAlgoState(int state)
997 // if (state != _oldAlgoState)
998 // int retc = ComputeStateEngine(MODIF_ALGO_STATE);
1002 //=============================================================================
1006 //=============================================================================
1007 SMESH_Hypothesis::Hypothesis_Status
1008 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1009 SMESH_Hypothesis * anHyp)
1011 //MESSAGE("SMESH_subMesh::SubMeshesAlgoStateEngine");
1012 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1013 //EAP: a wire (dim==1) should notify edges (dim==1)
1014 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1015 if (/*EAP:dim > 1*/ _subShape.ShapeType() < TopAbs_EDGE )
1017 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1019 map < int, SMESH_subMesh * >::const_iterator itsub;
1020 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1022 SMESH_subMesh *sm = (*itsub).second;
1023 SMESH_Hypothesis::Hypothesis_Status ret2 =
1024 sm->AlgoStateEngine(event, anHyp);
1032 //=============================================================================
1036 //=============================================================================
1038 void SMESH_subMesh::CleanDependsOn()
1040 MESSAGE("SMESH_subMesh::CleanDependsOn");
1041 // **** parcourir les ancetres dans l'ordre de dépendance
1043 ComputeStateEngine(CLEAN);
1045 const map < int, SMESH_subMesh * >&dependson = DependsOn();
1046 map < int, SMESH_subMesh * >::const_iterator its;
1047 for (its = dependson.begin(); its != dependson.end(); its++)
1049 SMESH_subMesh *sm = (*its).second;
1050 SCRUTE((*its).first);
1051 sm->ComputeStateEngine(CLEAN);
1055 //=============================================================================
1059 //=============================================================================
1061 void SMESH_subMesh::DumpAlgoState(bool isMain)
1063 int dim = SMESH_Gen::GetShapeDim(_subShape);
1064 // if (dim < 1) return;
1067 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1069 map < int, SMESH_subMesh * >::const_iterator itsub;
1070 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1072 SMESH_subMesh *sm = (*itsub).second;
1073 sm->DumpAlgoState(false);
1076 int type = _subShape.ShapeType();
1077 MESSAGE("dim = " << dim << " type of shape " << type);
1081 MESSAGE(" AlgoState = NO_ALGO");
1084 MESSAGE(" AlgoState = MISSING_HYP");
1087 MESSAGE(" AlgoState = HYP_OK");
1090 switch (_computeState)
1093 MESSAGE(" ComputeState = NOT_READY");
1095 case READY_TO_COMPUTE:
1096 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1099 MESSAGE(" ComputeState = COMPUTE_OK");
1101 case FAILED_TO_COMPUTE:
1102 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1107 //=============================================================================
1111 //=============================================================================
1113 bool SMESH_subMesh::ComputeStateEngine(int event)
1115 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1116 //SCRUTE(_computeState);
1119 int dim = SMESH_Gen::GetShapeDim(_subShape);
1123 if ( IsMeshComputed() )
1124 _computeState = COMPUTE_OK;
1126 _computeState = READY_TO_COMPUTE;
1129 SMESH_Gen *gen = _father->GetGen();
1130 SMESH_Algo *algo = 0;
1132 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1134 switch (_computeState)
1137 // ----------------------------------------------------------------------
1142 case MODIF_HYP: // nothing to do
1144 case MODIF_ALGO_STATE:
1145 if (_algoState == HYP_OK)
1147 _computeState = READY_TO_COMPUTE;
1150 case COMPUTE: // nothing to do
1153 RemoveSubMeshElementsAndNodes();
1158 case SUBMESH_COMPUTED: // nothing to do
1160 case SUBMESH_RESTORED:
1161 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1163 case MESH_ENTITY_REMOVED:
1165 case CHECK_COMPUTE_STATE:
1166 if ( IsMeshComputed() )
1167 _computeState = COMPUTE_OK;
1175 // ----------------------------------------------------------------------
1177 case READY_TO_COMPUTE:
1180 case MODIF_HYP: // nothing to do
1182 case MODIF_ALGO_STATE:
1183 _computeState = NOT_READY;
1184 algo = gen->GetAlgo((*_father), _subShape);
1187 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1189 _computeState = READY_TO_COMPUTE;
1194 algo = gen->GetAlgo((*_father), _subShape);
1196 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1199 MESSAGE("***** verify compute state *****");
1200 _computeState = NOT_READY;
1203 // check submeshes needed
1204 if (algo->NeedDescretBoundary())
1205 ret = SubMeshesComputed();
1208 MESSAGE("Some SubMeshes not computed");
1209 _computeState = FAILED_TO_COMPUTE;
1212 RemoveSubMeshElementsAndNodes();
1214 if (!algo->NeedDescretBoundary() && !algo->OnlyUnaryInput())
1215 ret = ApplyToCollection( algo, GetCollection( gen, algo ) );
1217 ret = algo->Compute((*_father), _subShape);
1221 MESSAGE("problem in algo execution: failed to compute");
1222 _computeState = FAILED_TO_COMPUTE;
1223 if (!algo->NeedDescretBoundary())
1224 UpdateSubMeshState( FAILED_TO_COMPUTE );
1227 // Show vertices location of a failed shape
1228 TopExp_Explorer exp( _subShape, TopAbs_VERTEX);
1229 for ( ; exp.More(); exp.Next() ) {
1230 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( exp.Current() )));
1231 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1238 _computeState = COMPUTE_OK;
1239 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1240 if (!algo->NeedDescretBoundary())
1241 UpdateSubMeshState( COMPUTE_OK );
1246 RemoveSubMeshElementsAndNodes();
1247 _computeState = NOT_READY;
1248 algo = gen->GetAlgo((*_father), _subShape);
1251 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1253 _computeState = READY_TO_COMPUTE;
1259 case SUBMESH_COMPUTED: // nothing to do
1261 case SUBMESH_RESTORED:
1262 // check if a mesh is already computed that may
1263 // happen after retrieval from a file
1264 ComputeStateEngine( CHECK_COMPUTE_STATE );
1265 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1267 case MESH_ENTITY_REMOVED:
1269 case CHECK_COMPUTE_STATE:
1270 if ( IsMeshComputed() )
1271 _computeState = COMPUTE_OK;
1279 // ----------------------------------------------------------------------
1285 CleanDependants(); // recursive recall with event CLEANDEP
1286 algo = gen->GetAlgo((*_father), _subShape);
1287 if (algo && !algo->NeedDescretBoundary())
1288 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1290 case MODIF_ALGO_STATE:
1291 CleanDependants(); // recursive recall with event CLEANDEP
1292 algo = gen->GetAlgo((*_father), _subShape);
1293 if (algo && !algo->NeedDescretBoundary())
1294 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1296 case COMPUTE: // nothing to do
1299 RemoveSubMeshElementsAndNodes();
1300 _computeState = NOT_READY;
1301 algo = gen->GetAlgo((*_father), _subShape);
1304 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1306 _computeState = READY_TO_COMPUTE;
1310 CleanDependants(); // recursive recall with event CLEANDEP
1312 case SUBMESH_COMPUTED: // nothing to do
1314 case SUBMESH_RESTORED:
1315 ComputeStateEngine( CHECK_COMPUTE_STATE );
1316 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1318 case MESH_ENTITY_REMOVED:
1319 UpdateDependantsState( CHECK_COMPUTE_STATE );
1320 ComputeStateEngine( CHECK_COMPUTE_STATE );
1321 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1323 case CHECK_COMPUTE_STATE:
1324 if ( !IsMeshComputed() )
1325 if (_algoState == HYP_OK)
1326 _computeState = READY_TO_COMPUTE;
1328 _computeState = NOT_READY;
1336 // ----------------------------------------------------------------------
1338 case FAILED_TO_COMPUTE:
1342 if (_algoState == HYP_OK)
1343 _computeState = READY_TO_COMPUTE;
1345 _computeState = NOT_READY;
1347 case MODIF_ALGO_STATE:
1348 if (_algoState == HYP_OK)
1349 _computeState = READY_TO_COMPUTE;
1351 _computeState = NOT_READY;
1353 case COMPUTE: // nothing to do
1356 RemoveSubMeshElementsAndNodes();
1357 if (_algoState == HYP_OK)
1358 _computeState = READY_TO_COMPUTE;
1360 _computeState = NOT_READY;
1365 case SUBMESH_COMPUTED: // allow retry compute
1366 if (_algoState == HYP_OK)
1367 _computeState = READY_TO_COMPUTE;
1369 _computeState = NOT_READY;
1371 case SUBMESH_RESTORED:
1372 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1374 case MESH_ENTITY_REMOVED:
1376 case CHECK_COMPUTE_STATE:
1377 if ( IsMeshComputed() )
1378 _computeState = COMPUTE_OK;
1380 if (_algoState == HYP_OK)
1381 _computeState = READY_TO_COMPUTE;
1383 _computeState = NOT_READY;
1391 // ----------------------------------------------------------------------
1397 //SCRUTE(_computeState);
1401 //=======================================================================
1402 //function : ApplyToCollection
1403 //purpose : Apply theAlgo to all subshapes in theCollection
1404 //=======================================================================
1406 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1407 const TopoDS_Shape& theCollection)
1409 MESSAGE("SMESH_subMesh::ApplyToCollection");
1410 ASSERT ( !theAlgo->NeedDescretBoundary() );
1415 ret = theAlgo->Compute( *_father, theCollection );
1417 // set _computeState of subshapes
1418 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1419 for ( ; anExplorer.More(); anExplorer.Next() )
1421 const TopoDS_Shape& aSubShape = anExplorer.Current();
1422 SMESH_subMesh* subMesh = _father->GetSubMeshContaining( aSubShape );
1427 subMesh->_computeState = COMPUTE_OK;
1428 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1429 subMesh->UpdateSubMeshState( COMPUTE_OK );
1433 subMesh->_computeState = FAILED_TO_COMPUTE;
1441 //=======================================================================
1442 //function : UpdateSubMeshState
1444 //=======================================================================
1446 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1448 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1449 map<int, SMESH_subMesh*>::const_iterator itsub;
1450 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1452 SMESH_subMesh* sm = (*itsub).second;
1453 sm->_computeState = theState;
1457 //=======================================================================
1458 //function : ComputeSubMeshStateEngine
1460 //=======================================================================
1462 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1464 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1465 map<int, SMESH_subMesh*>::const_iterator itsub;
1466 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1468 SMESH_subMesh* sm = (*itsub).second;
1469 sm->ComputeStateEngine(event);
1473 //=======================================================================
1474 //function : UpdateDependantsState
1476 //=======================================================================
1478 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1480 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1481 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1482 for (; it.More(); it.Next())
1484 const TopoDS_Shape& ancestor = it.Value();
1485 SMESH_subMesh *aSubMesh =
1486 _father->GetSubMeshContaining(ancestor);
1488 aSubMesh->ComputeStateEngine( theEvent );
1492 //=============================================================================
1496 //=============================================================================
1498 void SMESH_subMesh::CleanDependants()
1500 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1501 for (; it.More(); it.Next())
1503 const TopoDS_Shape& ancestor = it.Value();
1504 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEANDEP)
1505 // will erase mesh on other shapes in a compound
1506 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1507 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1509 aSubMesh->ComputeStateEngine(CLEANDEP);
1512 ComputeStateEngine(CLEAN);
1515 //=============================================================================
1519 //=============================================================================
1521 static void removeSubMesh( SMESHDS_Mesh * meshDS, const TopoDS_Shape& subShape)
1523 SMESHDS_SubMesh * subMeshDS = meshDS->MeshElements(subShape);
1524 if (subMeshDS!=NULL)
1526 SMDS_ElemIteratorPtr ite=subMeshDS->GetElements();
1529 const SMDS_MeshElement * elt = ite->next();
1530 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1531 meshDS->RemoveElement(elt);
1534 SMDS_NodeIteratorPtr itn=subMeshDS->GetNodes();
1537 const SMDS_MeshNode * node = itn->next();
1538 //MESSAGE( " RM node: "<<node->GetID());
1539 meshDS->RemoveNode(node);
1544 //=============================================================================
1548 //=============================================================================
1550 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1552 //SCRUTE(_subShape.ShapeType());
1554 removeSubMesh( _meshDS, _subShape );
1556 // algo may bind a submesh not to _subShape, eg 3D algo
1557 // sets nodes on SHELL while _subShape may be SOLID
1559 int dim = SMESH_Gen::GetShapeDim( _subShape );
1560 int type = _subShape.ShapeType() + 1;
1561 for ( ; type <= TopAbs_EDGE; type++)
1562 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1564 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1565 for ( ; exp.More(); exp.Next() )
1566 removeSubMesh( _meshDS, exp.Current() );
1572 //=======================================================================
1573 //function : IsMeshComputed
1574 //purpose : check if _subMeshDS contains mesh elements
1575 //=======================================================================
1577 bool SMESH_subMesh::IsMeshComputed() const
1579 // algo may bind a submesh not to _subShape, eg 3D algo
1580 // sets nodes on SHELL while _subShape may be SOLID
1582 int dim = SMESH_Gen::GetShapeDim( _subShape );
1583 int type = _subShape.ShapeType();
1584 for ( ; type <= TopAbs_VERTEX; type++) {
1585 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1587 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1588 for ( ; exp.More(); exp.Next() )
1590 SMESHDS_SubMesh * subMeshDS = _meshDS->MeshElements( exp.Current() );
1591 if ( subMeshDS != NULL &&
1592 (subMeshDS->GetElements()->more() || subMeshDS->GetNodes()->more())) {
1605 //=======================================================================
1606 //function : GetCollection
1607 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1608 // meshed at once along with _subShape
1609 //=======================================================================
1611 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1613 MESSAGE("SMESH_subMesh::GetCollection");
1614 ASSERT (!theAlgo->NeedDescretBoundary());
1616 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1618 if ( mainShape.IsSame( _subShape ))
1621 list<const SMESHDS_Hypothesis*> aUsedHyp =
1622 theAlgo->GetUsedHypothesis( *_father, _subShape ); // copy
1624 // put in a compound all shapes with the same hypothesis assigned
1625 // and a good ComputState
1627 TopoDS_Compound aCompound;
1628 BRep_Builder aBuilder;
1629 aBuilder.MakeCompound( aCompound );
1631 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1632 for ( ; anExplorer.More(); anExplorer.Next() )
1634 const TopoDS_Shape& S = anExplorer.Current();
1635 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1636 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1638 if (subMesh->GetComputeState() == READY_TO_COMPUTE &&
1639 anAlgo == theAlgo &&
1640 anAlgo->GetUsedHypothesis( *_father, S ) == aUsedHyp)
1642 aBuilder.Add( aCompound, S );
1649 //=======================================================================
1650 //function : GetSimilarAttached
1651 //purpose : return nb of hypotheses attached to theShape.
1652 // If theHyp is provided, similar but not same hypotheses
1653 // are countered; else only applicable ones having theHypType
1655 //=======================================================================
1657 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1658 const SMESH_Hypothesis * theHyp,
1659 const int theHypType)
1661 SMESH_HypoFilter filter;
1662 filter.Init( SMESH_HypoFilter::HasType( theHyp ? theHyp->GetType() : theHypType ));
1664 filter.And( SMESH_HypoFilter::HasDim( theHyp->GetDim() ));
1665 filter.AndNot( SMESH_HypoFilter::Is( theHyp ));
1668 filter.And( SMESH_HypoFilter::IsApplicableTo( theShape ));
1670 return _father->GetHypothesis( theShape, filter, false );
1673 //=======================================================================
1674 //function : CheckConcurentHypothesis
1675 //purpose : check if there are several applicable hypothesis attached to
1677 //=======================================================================
1679 SMESH_Hypothesis::Hypothesis_Status
1680 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1682 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1684 // is there local hypothesis on me?
1685 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1686 return SMESH_Hypothesis::HYP_OK;
1689 TopoDS_Shape aPrevWithHyp;
1690 const SMESH_Hypothesis* aPrevHyp = 0;
1691 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1692 for (; it.More(); it.Next())
1694 const TopoDS_Shape& ancestor = it.Value();
1695 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1698 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1700 aPrevWithHyp = ancestor;
1703 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1704 return SMESH_Hypothesis::HYP_CONCURENT;
1706 return SMESH_Hypothesis::HYP_OK;
1709 return SMESH_Hypothesis::HYP_OK;