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 "utilities.h"
39 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
40 #include <TopTools_ListOfShape.hxx>
41 #include <TopTools_ListIteratorOfListOfShape.hxx>
42 #include <TColStd_ListIteratorOfListOfInteger.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <BRep_Builder.hxx>
48 #include <BRep_Tool.hxx>
52 //=============================================================================
54 * default constructor:
56 //=============================================================================
58 SMESH_subMesh::SMESH_subMesh(int Id, SMESH_Mesh * father, SMESHDS_Mesh * meshDS,
59 const TopoDS_Shape & aSubShape)
61 _subShape = aSubShape;
63 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
66 _dependenceAnalysed = false;
68 if (_subShape.ShapeType() == TopAbs_VERTEX)
71 _computeState = READY_TO_COMPUTE;
76 _computeState = NOT_READY;
80 //=============================================================================
84 //=============================================================================
86 SMESH_subMesh::~SMESH_subMesh()
88 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
92 //=============================================================================
96 //=============================================================================
98 int SMESH_subMesh::GetId() const
100 //MESSAGE("SMESH_subMesh::GetId");
104 //=============================================================================
108 //=============================================================================
110 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
112 //MESSAGE("SMESH_subMesh::GetSubMeshDS");
113 if (_subMeshDS==NULL)
115 //MESSAGE("subMesh pointer still null, trying to get it...");
116 _subMeshDS = _meshDS->MeshElements(_subShape); // may be null ...
117 if (_subMeshDS==NULL)
119 MESSAGE("problem... subMesh still empty");
121 //NRI throw SALOME_Exception(LOCALIZED(subMesh still empty));
127 //=============================================================================
131 //=============================================================================
133 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
135 if ( !GetSubMeshDS() )
136 _meshDS->NewSubMesh( _meshDS->ShapeToIndex( _subShape ) );
138 return GetSubMeshDS();
141 //=============================================================================
145 //=============================================================================
147 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
149 //MESSAGE("SMESH_subMesh::GetFirstToCompute");
150 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
151 SMESH_subMesh *firstToCompute = 0;
153 map < int, SMESH_subMesh * >::const_iterator itsub;
154 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
156 SMESH_subMesh *sm = (*itsub).second;
157 // SCRUTE(sm->GetId());
158 // SCRUTE(sm->GetComputeState());
159 bool readyToCompute = (sm->GetComputeState() == READY_TO_COMPUTE);
163 //SCRUTE(sm->GetId());
169 return firstToCompute; // a subMesh of this
171 if (_computeState == READY_TO_COMPUTE)
175 return 0; // nothing to compute
178 //=============================================================================
182 //=============================================================================
184 bool SMESH_subMesh::SubMeshesComputed()
186 //MESSAGE("SMESH_subMesh::SubMeshesComputed");
187 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
189 bool subMeshesComputed = true;
190 map < int, SMESH_subMesh * >::const_iterator itsub;
191 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
193 SMESH_subMesh *sm = (*itsub).second;
195 const TopoDS_Shape & ss = sm->GetSubShape();
196 int type = ss.ShapeType();
197 bool computeOk = (sm->GetComputeState() == COMPUTE_OK);
200 subMeshesComputed = false;
204 case TopAbs_COMPOUND:
206 MESSAGE("The not computed sub mesh is a COMPOUND");
209 case TopAbs_COMPSOLID:
211 MESSAGE("The not computed sub mesh is a COMPSOLID");
216 MESSAGE("The not computed sub mesh is a SHEL");
221 MESSAGE("The not computed sub mesh is a WIRE");
226 MESSAGE("The not computed sub mesh is a SOLID");
231 MESSAGE("The not computed sub mesh is a FACE");
236 MESSAGE("The not computed sub mesh is a EDGE");
241 MESSAGE("The not computed sub mesh is of unknown type");
249 return subMeshesComputed;
252 //=============================================================================
256 //=============================================================================
258 bool SMESH_subMesh::SubMeshesReady()
260 MESSAGE("SMESH_subMesh::SubMeshesReady");
261 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
263 bool subMeshesReady = true;
264 map < int, SMESH_subMesh * >::const_iterator itsub;
265 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
267 SMESH_subMesh *sm = (*itsub).second;
268 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK)
269 || (sm->GetComputeState() == READY_TO_COMPUTE));
272 subMeshesReady = false;
277 return subMeshesReady;
280 //=============================================================================
282 * Construct dependence on first level subMeshes. complex shapes (compsolid,
283 * shell, wire) are not analysed the same way as simple shapes (solid, face,
285 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
286 * with possible multiples occurences. Multiples occurences corresponds to
287 * internal frontiers within shapes of the collection and must not be keeped.
288 * See FinalizeDependence.
290 //=============================================================================
292 const map < int, SMESH_subMesh * >&SMESH_subMesh::DependsOn()
294 if (_dependenceAnalysed)
297 //MESSAGE("SMESH_subMesh::DependsOn");
299 int type = _subShape.ShapeType();
303 case TopAbs_COMPOUND:
305 //MESSAGE("compound");
306 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
309 InsertDependence(exp.Current());
311 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
314 InsertDependence(exp.Current()); //only shell not in solid
316 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
319 InsertDependence(exp.Current());
321 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
324 InsertDependence(exp.Current());
328 case TopAbs_COMPSOLID:
330 //MESSAGE("compsolid");
331 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
334 InsertDependence(exp.Current());
341 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
344 InsertDependence(exp.Current());
351 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
354 InsertDependence(exp.Current());
361 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
364 InsertDependence(exp.Current());
371 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
374 InsertDependence(exp.Current());
381 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
384 InsertDependence(exp.Current());
397 _dependenceAnalysed = true;
401 //=============================================================================
403 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
405 //=============================================================================
407 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
409 //MESSAGE("SMESH_subMesh::InsertDependence");
410 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
411 int type = aSubShape.ShapeType();
412 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
413 int cle = aSubMesh->GetId();
414 cle += 10000000 * ordType; // sort map by ordType then index
415 if (_mapDepend.find(cle) == _mapDepend.end())
417 _mapDepend[cle] = aSubMesh;
418 const map < int, SMESH_subMesh * >&subMap = aSubMesh->DependsOn();
419 map < int, SMESH_subMesh * >::const_iterator im;
420 for (im = subMap.begin(); im != subMap.end(); im++)
422 int clesub = (*im).first;
423 SMESH_subMesh *sm = (*im).second;
424 if (_mapDepend.find(clesub) == _mapDepend.end())
425 _mapDepend[clesub] = sm;
431 //=============================================================================
435 //=============================================================================
437 const TopoDS_Shape & SMESH_subMesh::GetSubShape()
439 //MESSAGE("SMESH_subMesh::GetSubShape");
444 //=======================================================================
445 //function : CanAddHypothesis
446 //purpose : return true if theHypothesis can be attached to me:
447 // its dimention is checked
448 //=======================================================================
450 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
452 int aHypDim = theHypothesis->GetDim();
453 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
454 if ( aHypDim <= aShapeDim )
456 // if ( aHypDim < aShapeDim )
457 // return ( _father->IsMainShape( _subShape ));
462 //=======================================================================
463 //function : IsApplicableHypotesis
464 //purpose : return true if theHypothesis can be used to mesh me:
465 // its shape type is checked
466 //=======================================================================
468 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis) const
470 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
472 return ( theHypothesis->GetShapeType() & (1<< _subShape.ShapeType()));
475 switch ( _subShape.ShapeType() ) {
480 int aHypDim = theHypothesis->GetDim();
481 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
482 return ( aHypDim == aShapeDim );
484 // case TopAbs_VERTEX:
486 // case TopAbs_COMPSOLID:
487 // case TopAbs_COMPOUND:
494 //=============================================================================
498 //=============================================================================
500 SMESH_Hypothesis::Hypothesis_Status
501 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
503 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
504 //SCRUTE(_algoState);
507 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
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
512 int dim = SMESH_Gen::GetShapeDim(_subShape);
517 if (event == ADD_HYP || event == ADD_ALGO)
518 return SMESH_Hypothesis::HYP_BAD_DIM; // do not allow to assign any hyp
520 return SMESH_Hypothesis::HYP_OK;
523 SMESH_Gen* gen =_father->GetGen();
525 int oldAlgoState = _algoState;
526 bool modifiedHyp = false; // if set to true, force event MODIF_ALGO_STATE
527 // in ComputeStateEngine
529 // ----------------------
530 // check mesh conformity
531 // ----------------------
532 if (event == ADD_ALGO)
534 if (IsApplicableHypotesis( anHyp ) &&
535 !_father->IsNotConformAllowed() &&
536 !IsConform( static_cast< SMESH_Algo* >( anHyp )))
537 return SMESH_Hypothesis::HYP_NOTCONFORM;
540 // ----------------------------------
541 // add a hypothesis to DS if possible
542 // ----------------------------------
543 if (event == ADD_HYP || event == ADD_ALGO)
545 if ( ! CanAddHypothesis( anHyp ))
546 return SMESH_Hypothesis::HYP_BAD_DIM;
548 if ( GetSimilarAttached( _subShape, anHyp ) )
549 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
551 if ( !_meshDS->AddHypothesis(_subShape, anHyp))
552 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
555 // --------------------------
556 // remove a hypothesis from DS
557 // --------------------------
558 if (event == REMOVE_HYP || event == REMOVE_ALGO)
560 if (!_meshDS->RemoveHypothesis(_subShape, anHyp))
561 return SMESH_Hypothesis::HYP_OK; // nothing changes
564 // ------------------
565 // analyse algo state
566 // ------------------
567 if (!IsApplicableHypotesis( anHyp ))
568 return ret; // not applicable hypotheses do not change algo state
573 // ----------------------------------------------------------------------
580 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
582 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
583 SetAlgoState(HYP_OK);
585 SetAlgoState(MISSING_HYP);
594 case ADD_FATHER_ALGO: { // Algo just added in father
595 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
597 if ( algo == anHyp ) {
598 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
599 SetAlgoState(HYP_OK);
601 SetAlgoState(MISSING_HYP);
605 case REMOVE_FATHER_HYP:
607 case REMOVE_FATHER_ALGO: {
608 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
611 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
612 SetAlgoState(HYP_OK);
614 SetAlgoState(MISSING_HYP);
624 // ----------------------------------------------------------------------
630 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
632 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
633 SetAlgoState(HYP_OK);
634 if (SMESH_Hypothesis::IsStatusFatal( ret ))
635 _meshDS->RemoveHypothesis(_subShape, anHyp);
636 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
638 _meshDS->RemoveHypothesis(_subShape, anHyp);
639 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
643 case ADD_ALGO: { //already existing algo : on father ?
644 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
646 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
647 SetAlgoState(HYP_OK);
649 SetAlgoState(MISSING_HYP);
654 case REMOVE_ALGO: { // perhaps a father algo applies ?
655 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
656 if (algo == NULL) // no more algo applying on subShape...
658 SetAlgoState(NO_ALGO);
662 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
663 SetAlgoState(HYP_OK);
665 SetAlgoState(MISSING_HYP);
669 case ADD_FATHER_HYP: {
670 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
672 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
673 SetAlgoState(HYP_OK);
675 SetAlgoState(MISSING_HYP);
678 case ADD_FATHER_ALGO: { // new father algo
679 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
681 if ( algo == anHyp ) {
682 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
683 SetAlgoState(HYP_OK);
685 SetAlgoState(MISSING_HYP);
689 case REMOVE_FATHER_HYP: // nothing to do
691 case REMOVE_FATHER_ALGO: {
692 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
693 if (algo == NULL) // no more applying algo on father
695 SetAlgoState(NO_ALGO);
699 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
700 SetAlgoState(HYP_OK);
702 SetAlgoState(MISSING_HYP);
712 // ----------------------------------------------------------------------
718 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
720 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
722 MESSAGE("two applying algo on the same shape not allowed");
723 _meshDS->RemoveHypothesis(_subShape, anHyp);
724 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
725 // ret should be fatal: anHyp was not added
726 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
728 else if (SMESH_Hypothesis::IsStatusFatal( ret ))
730 _meshDS->RemoveHypothesis(_subShape, anHyp);
732 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
734 _meshDS->RemoveHypothesis(_subShape, anHyp);
735 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
743 case ADD_ALGO: { //already existing algo : on father ?
744 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
745 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
746 SetAlgoState(HYP_OK);
748 SetAlgoState(MISSING_HYP);
753 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
755 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
756 SetAlgoState(HYP_OK);
758 SetAlgoState(MISSING_HYP);
762 case REMOVE_ALGO: { // perhaps a father algo applies ?
763 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
764 if (algo == NULL) // no more algo applying on subShape...
766 SetAlgoState(NO_ALGO);
770 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
771 SetAlgoState(HYP_OK);
773 SetAlgoState(MISSING_HYP);
774 // check if same algo remains
775 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
780 case ADD_FATHER_HYP: { // new father hypothesis ?
781 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
783 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
785 SetAlgoState(HYP_OK);
786 if (_father->IsUsedHypothesis( anHyp, _subShape )) // new Hyp
790 SetAlgoState(MISSING_HYP);
793 case ADD_FATHER_ALGO: { // a new algo on father
794 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
795 if ( algo == anHyp ) {
796 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
797 SetAlgoState(HYP_OK);
799 SetAlgoState(MISSING_HYP);
804 case REMOVE_FATHER_HYP: {
805 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
807 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
808 SetAlgoState(HYP_OK);
810 SetAlgoState(MISSING_HYP);
811 // is there the same local hyp or maybe a new father algo applied?
812 if ( !GetSimilarAttached( _subShape, anHyp ) )
816 case REMOVE_FATHER_ALGO: {
817 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
818 if (algo == NULL) // no more applying algo on father
820 SetAlgoState(NO_ALGO);
824 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
825 SetAlgoState(HYP_OK);
827 SetAlgoState(MISSING_HYP);
828 // is there the same local algo or maybe a new father algo applied?
829 if ( !GetSimilarAttached( _subShape, anHyp ))
840 // ----------------------------------------------------------------------
846 // ----------------------------------------
847 // check concurent hypotheses on ansestors
848 // ----------------------------------------
849 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
850 (event == ADD_FATHER_HYP ||
851 event == ADD_FATHER_ALGO ||
852 event == REMOVE_FATHER_HYP ||
853 event == REMOVE_FATHER_ALGO ||
854 event == REMOVE_ALGO ||
855 event == REMOVE_HYP))
857 ret = CheckConcurentHypothesis( anHyp->GetType() );
860 if ((_algoState != oldAlgoState) || modifiedHyp)
861 int retc = ComputeStateEngine(MODIF_ALGO_STATE);
867 //=======================================================================
868 //function : IsConform
869 //purpose : check if a conform mesh will be produced by the Algo
870 //=======================================================================
872 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
874 // MESSAGE( "SMESH_subMesh::IsConform" );
876 if ( !theAlgo ) return false;
878 // check only algo that doesn't NeedDescretBoundary(): because mesh made
879 // on a sub-shape will be ignored by theAlgo
880 if ( theAlgo->NeedDescretBoundary() )
883 SMESH_Gen* gen =_father->GetGen();
885 // only local algo is to be checked
886 if ( gen->IsGlobalAlgo( theAlgo, *_father ))
889 // check algo attached to adjacent shapes
891 // loop on one level down sub-meshes
892 TopoDS_Iterator itsub( _subShape );
893 for (; itsub.More(); itsub.Next())
895 // loop on adjacent subShapes
896 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
897 for (; it.More(); it.Next())
899 const TopoDS_Shape& adjacent = it.Value();
900 if ( _subShape.IsSame( adjacent )) continue;
901 if ( adjacent.ShapeType() != _subShape.ShapeType())
904 // check algo attached to smAdjacent
905 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
908 !algo->NeedDescretBoundary() /*&&
909 !gen->IsGlobalAlgo( algo, *_father )*/)
910 return false; // NOT CONFORM MESH WILL BE PRODUCED
917 //=============================================================================
921 //=============================================================================
923 void SMESH_subMesh::SetAlgoState(int state)
925 // if (state != _oldAlgoState)
926 // int retc = ComputeStateEngine(MODIF_ALGO_STATE);
930 //=============================================================================
934 //=============================================================================
935 SMESH_Hypothesis::Hypothesis_Status
936 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
937 SMESH_Hypothesis * anHyp)
939 //MESSAGE("SMESH_subMesh::SubMeshesAlgoStateEngine");
940 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
941 //EAP: a wire (dim==1) should notify edges (dim==1)
942 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
943 if (/*EAP:dim > 1*/ _subShape.ShapeType() < TopAbs_EDGE )
945 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
947 map < int, SMESH_subMesh * >::const_iterator itsub;
948 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
950 SMESH_subMesh *sm = (*itsub).second;
951 SMESH_Hypothesis::Hypothesis_Status ret2 =
952 sm->AlgoStateEngine(event, anHyp);
960 //=============================================================================
964 //=============================================================================
966 void SMESH_subMesh::CleanDependsOn()
968 MESSAGE("SMESH_subMesh::CleanDependsOn");
969 // **** parcourir les ancetres dans l'ordre de dépendance
971 ComputeStateEngine(CLEAN);
973 const map < int, SMESH_subMesh * >&dependson = DependsOn();
974 map < int, SMESH_subMesh * >::const_iterator its;
975 for (its = dependson.begin(); its != dependson.end(); its++)
977 SMESH_subMesh *sm = (*its).second;
978 SCRUTE((*its).first);
979 sm->ComputeStateEngine(CLEAN);
983 //=============================================================================
987 //=============================================================================
989 void SMESH_subMesh::DumpAlgoState(bool isMain)
991 int dim = SMESH_Gen::GetShapeDim(_subShape);
992 // if (dim < 1) return;
995 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
997 map < int, SMESH_subMesh * >::const_iterator itsub;
998 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1000 SMESH_subMesh *sm = (*itsub).second;
1001 sm->DumpAlgoState(false);
1004 int type = _subShape.ShapeType();
1005 MESSAGE("dim = " << dim << " type of shape " << type);
1009 MESSAGE(" AlgoState = NO_ALGO");
1012 MESSAGE(" AlgoState = MISSING_HYP");
1015 MESSAGE(" AlgoState = HYP_OK");
1018 switch (_computeState)
1021 MESSAGE(" ComputeState = NOT_READY");
1023 case READY_TO_COMPUTE:
1024 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1027 MESSAGE(" ComputeState = COMPUTE_OK");
1029 case FAILED_TO_COMPUTE:
1030 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1035 //=============================================================================
1039 //=============================================================================
1041 bool SMESH_subMesh::ComputeStateEngine(int event)
1043 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1044 //SCRUTE(_computeState);
1047 int dim = SMESH_Gen::GetShapeDim(_subShape);
1051 if ( IsMeshComputed() )
1052 _computeState = COMPUTE_OK;
1054 _computeState = READY_TO_COMPUTE;
1057 SMESH_Gen *gen = _father->GetGen();
1058 SMESH_Algo *algo = 0;
1060 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1062 switch (_computeState)
1065 // ----------------------------------------------------------------------
1070 case MODIF_HYP: // nothing to do
1072 case MODIF_ALGO_STATE:
1073 if (_algoState == HYP_OK)
1075 _computeState = READY_TO_COMPUTE;
1078 case COMPUTE: // nothing to do
1081 RemoveSubMeshElementsAndNodes();
1086 case SUBMESH_COMPUTED: // nothing to do
1088 case SUBMESH_RESTORED:
1089 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1091 case MESH_ENTITY_REMOVED:
1093 case CHECK_COMPUTE_STATE:
1094 if ( IsMeshComputed() )
1095 _computeState = COMPUTE_OK;
1103 // ----------------------------------------------------------------------
1105 case READY_TO_COMPUTE:
1108 case MODIF_HYP: // nothing to do
1110 case MODIF_ALGO_STATE:
1111 _computeState = NOT_READY;
1112 algo = gen->GetAlgo((*_father), _subShape);
1115 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1117 _computeState = READY_TO_COMPUTE;
1122 algo = gen->GetAlgo((*_father), _subShape);
1124 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1127 MESSAGE("***** verify compute state *****");
1128 _computeState = NOT_READY;
1131 // check submeshes needed
1132 if (algo->NeedDescretBoundary())
1133 ret = SubMeshesComputed();
1136 MESSAGE("Some SubMeshes not computed");
1137 _computeState = FAILED_TO_COMPUTE;
1140 RemoveSubMeshElementsAndNodes();
1142 if (!algo->NeedDescretBoundary() && !algo->OnlyUnaryInput())
1143 ret = ApplyToCollection( algo, GetCollection( gen, algo ) );
1145 ret = algo->Compute((*_father), _subShape);
1149 MESSAGE("problem in algo execution: failed to compute");
1150 _computeState = FAILED_TO_COMPUTE;
1151 if (!algo->NeedDescretBoundary())
1152 UpdateSubMeshState( FAILED_TO_COMPUTE );
1155 // Show vertices location of a failed shape
1156 TopExp_Explorer exp( _subShape, TopAbs_VERTEX);
1157 for ( ; exp.More(); exp.Next() ) {
1158 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( exp.Current() )));
1159 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1166 _computeState = COMPUTE_OK;
1167 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1168 if (!algo->NeedDescretBoundary())
1169 UpdateSubMeshState( COMPUTE_OK );
1174 RemoveSubMeshElementsAndNodes();
1175 _computeState = NOT_READY;
1176 algo = gen->GetAlgo((*_father), _subShape);
1179 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1181 _computeState = READY_TO_COMPUTE;
1187 case SUBMESH_COMPUTED: // nothing to do
1189 case SUBMESH_RESTORED:
1190 // check if a mesh is already computed that may
1191 // happen after retrieval from a file
1192 ComputeStateEngine( CHECK_COMPUTE_STATE );
1193 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1195 case MESH_ENTITY_REMOVED:
1197 case CHECK_COMPUTE_STATE:
1198 if ( IsMeshComputed() )
1199 _computeState = COMPUTE_OK;
1207 // ----------------------------------------------------------------------
1213 CleanDependants(); // recursive recall with event CLEANDEP
1214 algo = gen->GetAlgo((*_father), _subShape);
1215 if (algo && !algo->NeedDescretBoundary())
1216 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1218 case MODIF_ALGO_STATE:
1219 CleanDependants(); // recursive recall with event CLEANDEP
1220 algo = gen->GetAlgo((*_father), _subShape);
1221 if (algo && !algo->NeedDescretBoundary())
1222 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1224 case COMPUTE: // nothing to do
1227 RemoveSubMeshElementsAndNodes();
1228 _computeState = NOT_READY;
1229 algo = gen->GetAlgo((*_father), _subShape);
1232 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1234 _computeState = READY_TO_COMPUTE;
1238 CleanDependants(); // recursive recall with event CLEANDEP
1240 case SUBMESH_COMPUTED: // nothing to do
1242 case SUBMESH_RESTORED:
1243 ComputeStateEngine( CHECK_COMPUTE_STATE );
1244 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1246 case MESH_ENTITY_REMOVED:
1247 UpdateDependantsState( CHECK_COMPUTE_STATE );
1248 ComputeStateEngine( CHECK_COMPUTE_STATE );
1249 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1251 case CHECK_COMPUTE_STATE:
1252 if ( !IsMeshComputed() )
1253 if (_algoState == HYP_OK)
1254 _computeState = READY_TO_COMPUTE;
1256 _computeState = NOT_READY;
1264 // ----------------------------------------------------------------------
1266 case FAILED_TO_COMPUTE:
1270 if (_algoState == HYP_OK)
1271 _computeState = READY_TO_COMPUTE;
1273 _computeState = NOT_READY;
1275 case MODIF_ALGO_STATE:
1276 if (_algoState == HYP_OK)
1277 _computeState = READY_TO_COMPUTE;
1279 _computeState = NOT_READY;
1281 case COMPUTE: // nothing to do
1284 RemoveSubMeshElementsAndNodes();
1285 if (_algoState == HYP_OK)
1286 _computeState = READY_TO_COMPUTE;
1288 _computeState = NOT_READY;
1293 case SUBMESH_COMPUTED: // allow retry compute
1294 if (_algoState == HYP_OK)
1295 _computeState = READY_TO_COMPUTE;
1297 _computeState = NOT_READY;
1299 case SUBMESH_RESTORED:
1300 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1302 case MESH_ENTITY_REMOVED:
1304 case CHECK_COMPUTE_STATE:
1305 if ( IsMeshComputed() )
1306 _computeState = COMPUTE_OK;
1308 if (_algoState == HYP_OK)
1309 _computeState = READY_TO_COMPUTE;
1311 _computeState = NOT_READY;
1319 // ----------------------------------------------------------------------
1325 //SCRUTE(_computeState);
1329 //=======================================================================
1330 //function : ApplyToCollection
1331 //purpose : Apply theAlgo to all subshapes in theCollection
1332 //=======================================================================
1334 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1335 const TopoDS_Shape& theCollection)
1337 MESSAGE("SMESH_subMesh::ApplyToCollection");
1338 ASSERT ( !theAlgo->NeedDescretBoundary() );
1343 ret = theAlgo->Compute( *_father, theCollection );
1345 // set _computeState of subshapes
1346 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1347 for ( ; anExplorer.More(); anExplorer.Next() )
1349 const TopoDS_Shape& aSubShape = anExplorer.Current();
1350 SMESH_subMesh* subMesh = _father->GetSubMeshContaining( aSubShape );
1355 subMesh->_computeState = COMPUTE_OK;
1356 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1357 subMesh->UpdateSubMeshState( COMPUTE_OK );
1361 subMesh->_computeState = FAILED_TO_COMPUTE;
1369 //=======================================================================
1370 //function : UpdateSubMeshState
1372 //=======================================================================
1374 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1376 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1377 map<int, SMESH_subMesh*>::const_iterator itsub;
1378 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1380 SMESH_subMesh* sm = (*itsub).second;
1381 sm->_computeState = theState;
1385 //=======================================================================
1386 //function : ComputeSubMeshStateEngine
1388 //=======================================================================
1390 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1392 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1393 map<int, SMESH_subMesh*>::const_iterator itsub;
1394 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1396 SMESH_subMesh* sm = (*itsub).second;
1397 sm->ComputeStateEngine(event);
1401 //=======================================================================
1402 //function : UpdateDependantsState
1404 //=======================================================================
1406 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1408 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1409 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1410 for (; it.More(); it.Next())
1412 const TopoDS_Shape& ancestor = it.Value();
1413 SMESH_subMesh *aSubMesh =
1414 _father->GetSubMeshContaining(ancestor);
1416 aSubMesh->ComputeStateEngine( theEvent );
1420 //=============================================================================
1424 //=============================================================================
1426 void SMESH_subMesh::CleanDependants()
1428 //MESSAGE("SMESH_subMesh::CleanDependants: shape type " << _subShape.ShapeType() );
1430 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1431 for (; it.More(); it.Next())
1433 const TopoDS_Shape& ancestor = it.Value();
1434 //MESSAGE("ancestor shape type " << ancestor.ShapeType() );
1435 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1437 aSubMesh->ComputeStateEngine(CLEANDEP);
1439 ComputeStateEngine(CLEAN);
1443 //=============================================================================
1447 //=============================================================================
1449 static void removeSubMesh( SMESHDS_Mesh * meshDS, const TopoDS_Shape& subShape)
1451 SMESHDS_SubMesh * subMeshDS = meshDS->MeshElements(subShape);
1452 if (subMeshDS!=NULL)
1454 SMDS_ElemIteratorPtr ite=subMeshDS->GetElements();
1457 const SMDS_MeshElement * elt = ite->next();
1458 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1459 meshDS->RemoveElement(elt);
1462 SMDS_NodeIteratorPtr itn=subMeshDS->GetNodes();
1465 const SMDS_MeshNode * node = itn->next();
1466 //MESSAGE( " RM node: "<<node->GetID());
1467 meshDS->RemoveNode(node);
1472 //=============================================================================
1476 //=============================================================================
1478 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1480 SCRUTE(_subShape.ShapeType());
1482 removeSubMesh( _meshDS, _subShape );
1484 // algo may bind a submesh not to _subShape, eg 3D algo
1485 // sets nodes on SHELL while _subShape may be SOLID
1487 int dim = SMESH_Gen::GetShapeDim( _subShape );
1488 int type = _subShape.ShapeType() + 1;
1489 for ( ; type <= TopAbs_EDGE; type++)
1490 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1492 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1493 for ( ; exp.More(); exp.Next() )
1494 removeSubMesh( _meshDS, exp.Current() );
1500 //=======================================================================
1501 //function : IsMeshComputed
1502 //purpose : check if _subMeshDS contains mesh elements
1503 //=======================================================================
1505 bool SMESH_subMesh::IsMeshComputed() const
1507 // algo may bind a submesh not to _subShape, eg 3D algo
1508 // sets nodes on SHELL while _subShape may be SOLID
1510 int dim = SMESH_Gen::GetShapeDim( _subShape );
1511 int type = _subShape.ShapeType();
1512 for ( ; type <= TopAbs_VERTEX; type++) {
1513 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1515 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1516 for ( ; exp.More(); exp.Next() )
1518 SMESHDS_SubMesh * subMeshDS = _meshDS->MeshElements( exp.Current() );
1519 if ( subMeshDS != NULL &&
1520 (subMeshDS->GetElements()->more() || subMeshDS->GetNodes()->more())) {
1533 //=======================================================================
1534 //function : GetCollection
1535 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1536 // meshed at once along with _subShape
1537 //=======================================================================
1539 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1541 MESSAGE("SMESH_subMesh::GetCollection");
1542 ASSERT (!theAlgo->NeedDescretBoundary());
1544 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1546 if ( mainShape.IsSame( _subShape ))
1549 list<const SMESHDS_Hypothesis*> aUsedHyp =
1550 theAlgo->GetUsedHypothesis( *_father, _subShape ); // copy
1552 // put in a compound all shapes with the same hypothesis assigned
1553 // and a good ComputState
1555 TopoDS_Compound aCompound;
1556 BRep_Builder aBuilder;
1557 aBuilder.MakeCompound( aCompound );
1559 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1560 for ( ; anExplorer.More(); anExplorer.Next() )
1562 const TopoDS_Shape& S = anExplorer.Current();
1563 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1564 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1566 if (subMesh->GetComputeState() == READY_TO_COMPUTE &&
1567 anAlgo == theAlgo &&
1568 anAlgo->GetUsedHypothesis( *_father, S ) == aUsedHyp)
1570 aBuilder.Add( aCompound, S );
1577 //=======================================================================
1578 //function : GetSimilarAttached
1579 //purpose : return nb of hypotheses attached to theShape.
1580 // If theHyp is provided, similar but not same hypotheses
1581 // are countered; else only applicable ones having theHypType
1583 //=======================================================================
1585 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1586 const SMESH_Hypothesis * theHyp,
1587 const int theHypType)
1589 const list<const SMESHDS_Hypothesis*>& aHypList =
1590 _father->GetHypothesisList( theShape );
1591 list<const SMESHDS_Hypothesis*>::const_iterator it = aHypList.begin();
1592 for ( ; it != aHypList.end(); it++ )
1594 const SMESH_Hypothesis* hyp = static_cast< const SMESH_Hypothesis *>( *it );
1598 if (hyp != theHyp &&
1599 hyp->GetType() == theHyp->GetType() &&
1600 hyp->GetDim() == theHyp->GetDim())
1605 if ( hyp->GetType() == theHypType && IsApplicableHypotesis( hyp ))
1613 //=======================================================================
1614 //function : CheckConcurentHypothesis
1615 //purpose : check if there are several applicable hypothesis attached to
1617 //=======================================================================
1619 SMESH_Hypothesis::Hypothesis_Status
1620 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1622 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1624 // is there local hypothesis on me?
1625 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1626 return SMESH_Hypothesis::HYP_OK;
1629 TopoDS_Shape aPrevWithHyp;
1630 const SMESH_Hypothesis* aPrevHyp = 0;
1631 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1632 for (; it.More(); it.Next())
1634 const TopoDS_Shape& ancestor = it.Value();
1635 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1638 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1640 aPrevWithHyp = ancestor;
1643 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1644 return SMESH_Hypothesis::HYP_CONCURENT;
1646 return SMESH_Hypothesis::HYP_OK;
1649 return SMESH_Hypothesis::HYP_OK;