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"
36 #include "utilities.h"
39 #include <BRep_Builder.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TopTools_MapOfShape.hxx>
44 #include <TopTools_ListOfShape.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
48 #include <TColStd_ListIteratorOfListOfInteger.hxx>
52 #include <BRep_Tool.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;
199 const TopoDS_Shape & ss = sm->GetSubShape();
200 int type = ss.ShapeType();
201 bool computeOk = (sm->GetComputeState() == COMPUTE_OK);
204 subMeshesComputed = false;
208 case TopAbs_COMPOUND:
210 MESSAGE("The not computed sub mesh is a COMPOUND");
213 case TopAbs_COMPSOLID:
215 MESSAGE("The not computed sub mesh is a COMPSOLID");
220 MESSAGE("The not computed sub mesh is a SHEL");
225 MESSAGE("The not computed sub mesh is a WIRE");
230 MESSAGE("The not computed sub mesh is a SOLID");
235 MESSAGE("The not computed sub mesh is a FACE");
240 MESSAGE("The not computed sub mesh is a EDGE");
245 MESSAGE("The not computed sub mesh is of unknown type");
253 return subMeshesComputed;
256 //=============================================================================
260 //=============================================================================
262 bool SMESH_subMesh::SubMeshesReady()
264 MESSAGE("SMESH_subMesh::SubMeshesReady");
265 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
267 bool subMeshesReady = true;
268 map < int, SMESH_subMesh * >::const_iterator itsub;
269 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
271 SMESH_subMesh *sm = (*itsub).second;
272 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK)
273 || (sm->GetComputeState() == READY_TO_COMPUTE));
276 subMeshesReady = false;
281 return subMeshesReady;
284 //=============================================================================
286 * Construct dependence on first level subMeshes. complex shapes (compsolid,
287 * shell, wire) are not analysed the same way as simple shapes (solid, face,
289 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
290 * with possible multiples occurences. Multiples occurences corresponds to
291 * internal frontiers within shapes of the collection and must not be keeped.
292 * See FinalizeDependence.
294 //=============================================================================
296 const map < int, SMESH_subMesh * >&SMESH_subMesh::DependsOn()
298 if (_dependenceAnalysed)
301 //MESSAGE("SMESH_subMesh::DependsOn");
303 int type = _subShape.ShapeType();
307 case TopAbs_COMPOUND:
309 //MESSAGE("compound");
310 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
313 InsertDependence(exp.Current());
315 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
318 InsertDependence(exp.Current()); //only shell not in solid
320 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
323 InsertDependence(exp.Current());
325 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
328 InsertDependence(exp.Current());
332 case TopAbs_COMPSOLID:
334 //MESSAGE("compsolid");
335 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
338 InsertDependence(exp.Current());
345 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
348 InsertDependence(exp.Current());
355 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
358 InsertDependence(exp.Current());
365 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
368 InsertDependence(exp.Current());
375 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
378 InsertDependence(exp.Current());
385 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
388 InsertDependence(exp.Current());
401 _dependenceAnalysed = true;
405 //=============================================================================
407 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
409 //=============================================================================
411 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
413 //MESSAGE("SMESH_subMesh::InsertDependence");
414 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
415 int type = aSubShape.ShapeType();
416 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
417 int cle = aSubMesh->GetId();
418 cle += 10000000 * ordType; // sort map by ordType then index
419 if (_mapDepend.find(cle) == _mapDepend.end())
421 _mapDepend[cle] = aSubMesh;
422 const map < int, SMESH_subMesh * >&subMap = aSubMesh->DependsOn();
423 map < int, SMESH_subMesh * >::const_iterator im;
424 for (im = subMap.begin(); im != subMap.end(); im++)
426 int clesub = (*im).first;
427 SMESH_subMesh *sm = (*im).second;
428 if (_mapDepend.find(clesub) == _mapDepend.end())
429 _mapDepend[clesub] = sm;
435 //=============================================================================
439 //=============================================================================
441 const TopoDS_Shape & SMESH_subMesh::GetSubShape()
443 //MESSAGE("SMESH_subMesh::GetSubShape");
448 //=======================================================================
449 //function : CanAddHypothesis
450 //purpose : return true if theHypothesis can be attached to me:
451 // its dimention is checked
452 //=======================================================================
454 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
456 int aHypDim = theHypothesis->GetDim();
457 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
458 if ( aHypDim <= aShapeDim )
460 // if ( aHypDim < aShapeDim )
461 // return ( _father->IsMainShape( _subShape ));
466 //=======================================================================
467 //function : IsApplicableHypotesis
468 //purpose : return true if theHypothesis can be used to mesh me:
469 // its shape type is checked
470 //=======================================================================
472 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis) const
474 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
476 return ( theHypothesis->GetShapeType() & (1<< _subShape.ShapeType()));
479 switch ( _subShape.ShapeType() ) {
484 int aHypDim = theHypothesis->GetDim();
485 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
486 return ( aHypDim == aShapeDim );
488 // case TopAbs_VERTEX:
490 // case TopAbs_COMPSOLID:
491 // case TopAbs_COMPOUND:
498 //=============================================================================
502 //=============================================================================
504 SMESH_Hypothesis::Hypothesis_Status
505 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
507 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
508 //SCRUTE(_algoState);
511 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
513 // **** les retour des evenement shape sont significatifs
514 // (add ou remove fait ou non)
515 // le retour des evenement father n'indiquent pas que add ou remove fait
516 int dim = SMESH_Gen::GetShapeDim(_subShape);
521 if (event == ADD_HYP || event == ADD_ALGO)
522 return SMESH_Hypothesis::HYP_BAD_DIM; // do not allow to assign any hyp
524 return SMESH_Hypothesis::HYP_OK;
527 SMESH_Gen* gen =_father->GetGen();
529 int oldAlgoState = _algoState;
530 bool modifiedHyp = false; // if set to true, force event MODIF_ALGO_STATE
531 // in ComputeStateEngine
533 // ----------------------
534 // check mesh conformity
535 // ----------------------
536 if (event == ADD_ALGO)
538 if (IsApplicableHypotesis( anHyp ) &&
539 !_father->IsNotConformAllowed() &&
540 !IsConform( static_cast< SMESH_Algo* >( anHyp )))
541 return SMESH_Hypothesis::HYP_NOTCONFORM;
544 // ----------------------------------
545 // add a hypothesis to DS if possible
546 // ----------------------------------
547 if (event == ADD_HYP || event == ADD_ALGO)
549 if ( ! CanAddHypothesis( anHyp ))
550 return SMESH_Hypothesis::HYP_BAD_DIM;
552 if ( GetSimilarAttached( _subShape, anHyp ) )
553 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
555 if ( !_meshDS->AddHypothesis(_subShape, anHyp))
556 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
558 // Serve Propagation of 1D hypothesis
559 if (event == ADD_HYP) {
560 bool isPropagationOk = true;
561 string hypName = anHyp->GetName();
563 if (hypName == "Propagation") {
564 if (_subShape.ShapeType() == TopAbs_EDGE) {
565 isPropagationOk = _father->BuildPropagationChain(_subShape);
567 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
568 TopTools_MapOfShape aMap;
569 for (; exp.More(); exp.Next()) {
570 if (aMap.Add(exp.Current())) {
571 if (!_father->BuildPropagationChain(exp.Current())) {
572 isPropagationOk = false;
577 } else if (anHyp->GetDim() == 1) { // Only 1D hypothesis can be propagated
578 if (_subShape.ShapeType() == TopAbs_EDGE) {
579 TopoDS_Shape aMainEdge;
580 if (_father->IsPropagatedHypothesis(_subShape, aMainEdge)) {
581 isPropagationOk = _father->RebuildPropagationChains();
582 } else if (_father->IsPropagationHypothesis(_subShape)) {
583 isPropagationOk = _father->BuildPropagationChain(_subShape);
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) {
606 bool isPropagationOk = true;
607 string hypName = anHyp->GetName();
609 if (hypName == "Propagation") {
610 if (_subShape.ShapeType() == TopAbs_EDGE) {
611 if (!_father->RemovePropagationChain(_subShape)) {
612 return SMESH_Hypothesis::HYP_UNKNOWN_FATAL;
614 // rebuild propagation chains, because removing one
615 // chain can resolve concurention, existing before
616 isPropagationOk = _father->RebuildPropagationChains();
618 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
619 TopTools_MapOfShape aMap;
620 for (; exp.More(); exp.Next()) {
621 if (aMap.Add(exp.Current())) {
622 if (!_father->RemovePropagationChain(exp.Current())) {
623 return SMESH_Hypothesis::HYP_UNKNOWN_FATAL;
627 // rebuild propagation chains, because removing one
628 // chain can resolve concurention, existing before
629 if (!_father->RebuildPropagationChains()) {
630 isPropagationOk = false;
633 } else { // if (hypName == "Propagation")
634 if (anHyp->GetDim() == 1) // Only 1D hypothesis can be propagated
636 if (_subShape.ShapeType() == TopAbs_EDGE) {
637 isPropagationOk = _father->RebuildPropagationChains();
638 if (!isPropagationOk && ret < SMESH_Hypothesis::HYP_CONCURENT)
639 ret = SMESH_Hypothesis::HYP_CONCURENT;
644 if (!isPropagationOk && ret < SMESH_Hypothesis::HYP_CONCURENT) {
645 ret = SMESH_Hypothesis::HYP_CONCURENT;
647 } // Serve Propagation of 1D hypothesis
650 // ------------------
651 // analyse algo state
652 // ------------------
653 if (!IsApplicableHypotesis( anHyp ))
654 return ret; // not applicable hypotheses do not change algo state
659 // ----------------------------------------------------------------------
666 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
668 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
669 SetAlgoState(HYP_OK);
671 SetAlgoState(MISSING_HYP);
680 case ADD_FATHER_ALGO: { // Algo just added in father
681 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
683 if ( algo == anHyp ) {
684 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
685 SetAlgoState(HYP_OK);
687 SetAlgoState(MISSING_HYP);
691 case REMOVE_FATHER_HYP:
693 case REMOVE_FATHER_ALGO: {
694 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
697 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
698 SetAlgoState(HYP_OK);
700 SetAlgoState(MISSING_HYP);
710 // ----------------------------------------------------------------------
716 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
718 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
719 SetAlgoState(HYP_OK);
720 if (SMESH_Hypothesis::IsStatusFatal( ret ))
721 _meshDS->RemoveHypothesis(_subShape, anHyp);
722 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
724 _meshDS->RemoveHypothesis(_subShape, anHyp);
725 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
729 case ADD_ALGO: { //already existing algo : on father ?
730 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
732 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
733 SetAlgoState(HYP_OK);
735 SetAlgoState(MISSING_HYP);
740 case REMOVE_ALGO: { // perhaps a father algo applies ?
741 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
742 if (algo == NULL) // no more algo applying on subShape...
744 SetAlgoState(NO_ALGO);
748 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
749 SetAlgoState(HYP_OK);
751 SetAlgoState(MISSING_HYP);
755 case ADD_FATHER_HYP: {
756 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
758 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
759 SetAlgoState(HYP_OK);
761 SetAlgoState(MISSING_HYP);
764 case ADD_FATHER_ALGO: { // new father algo
765 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
767 if ( algo == anHyp ) {
768 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
769 SetAlgoState(HYP_OK);
771 SetAlgoState(MISSING_HYP);
775 case REMOVE_FATHER_HYP: // nothing to do
777 case REMOVE_FATHER_ALGO: {
778 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
779 if (algo == NULL) // no more applying algo on father
781 SetAlgoState(NO_ALGO);
785 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
786 SetAlgoState(HYP_OK);
788 SetAlgoState(MISSING_HYP);
798 // ----------------------------------------------------------------------
804 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
806 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
808 MESSAGE("two applying algo on the same shape not allowed");
809 _meshDS->RemoveHypothesis(_subShape, anHyp);
810 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
811 // ret should be fatal: anHyp was not added
812 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
814 else if (SMESH_Hypothesis::IsStatusFatal( ret ))
816 _meshDS->RemoveHypothesis(_subShape, anHyp);
818 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
820 _meshDS->RemoveHypothesis(_subShape, anHyp);
821 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
829 case ADD_ALGO: { //already existing algo : on father ?
830 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
831 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
832 SetAlgoState(HYP_OK);
834 SetAlgoState(MISSING_HYP);
839 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
841 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
842 SetAlgoState(HYP_OK);
844 SetAlgoState(MISSING_HYP);
848 case REMOVE_ALGO: { // perhaps a father algo applies ?
849 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
850 if (algo == NULL) // no more algo applying on subShape...
852 SetAlgoState(NO_ALGO);
856 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
857 SetAlgoState(HYP_OK);
859 SetAlgoState(MISSING_HYP);
860 // check if same algo remains
861 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
866 case ADD_FATHER_HYP: { // new father hypothesis ?
867 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
869 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
871 SetAlgoState(HYP_OK);
872 if (_father->IsUsedHypothesis( anHyp, _subShape )) // new Hyp
876 SetAlgoState(MISSING_HYP);
879 case ADD_FATHER_ALGO: { // a new algo on father
880 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
881 if ( algo == anHyp ) {
882 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
883 SetAlgoState(HYP_OK);
885 SetAlgoState(MISSING_HYP);
890 case REMOVE_FATHER_HYP: {
891 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
893 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
894 SetAlgoState(HYP_OK);
896 SetAlgoState(MISSING_HYP);
897 // is there the same local hyp or maybe a new father algo applied?
898 if ( !GetSimilarAttached( _subShape, anHyp ) )
902 case REMOVE_FATHER_ALGO: {
903 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
904 if (algo == NULL) // no more applying algo on father
906 SetAlgoState(NO_ALGO);
910 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
911 SetAlgoState(HYP_OK);
913 SetAlgoState(MISSING_HYP);
914 // is there the same local algo or maybe a new father algo applied?
915 if ( !GetSimilarAttached( _subShape, anHyp ))
926 // ----------------------------------------------------------------------
933 if ((_algoState != oldAlgoState) || modifiedHyp)
934 ComputeStateEngine(MODIF_ALGO_STATE);
940 //=======================================================================
941 //function : IsConform
942 //purpose : check if a conform mesh will be produced by the Algo
943 //=======================================================================
945 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
947 // MESSAGE( "SMESH_subMesh::IsConform" );
949 if ( !theAlgo ) return false;
951 // check only algo that doesn't NeedDescretBoundary(): because mesh made
952 // on a sub-shape will be ignored by theAlgo
953 if ( theAlgo->NeedDescretBoundary() )
956 SMESH_Gen* gen =_father->GetGen();
958 // only local algo is to be checked
959 if ( gen->IsGlobalAlgo( theAlgo, *_father ))
962 // check algo attached to adjacent shapes
964 // loop on one level down sub-meshes
965 TopoDS_Iterator itsub( _subShape );
966 for (; itsub.More(); itsub.Next())
968 // loop on adjacent subShapes
969 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
970 for (; it.More(); it.Next())
972 const TopoDS_Shape& adjacent = it.Value();
973 if ( _subShape.IsSame( adjacent )) continue;
974 if ( adjacent.ShapeType() != _subShape.ShapeType())
977 // check algo attached to smAdjacent
978 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
981 !algo->NeedDescretBoundary() /*&&
982 !gen->IsGlobalAlgo( algo, *_father )*/)
983 return false; // NOT CONFORM MESH WILL BE PRODUCED
990 //=============================================================================
994 //=============================================================================
996 void SMESH_subMesh::SetAlgoState(int state)
998 // if (state != _oldAlgoState)
999 // int retc = ComputeStateEngine(MODIF_ALGO_STATE);
1003 //=============================================================================
1007 //=============================================================================
1008 SMESH_Hypothesis::Hypothesis_Status
1009 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1010 SMESH_Hypothesis * anHyp)
1012 //MESSAGE("SMESH_subMesh::SubMeshesAlgoStateEngine");
1013 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1014 //EAP: a wire (dim==1) should notify edges (dim==1)
1015 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1016 if (/*EAP:dim > 1*/ _subShape.ShapeType() < TopAbs_EDGE )
1018 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1020 map < int, SMESH_subMesh * >::const_iterator itsub;
1021 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1023 SMESH_subMesh *sm = (*itsub).second;
1024 SMESH_Hypothesis::Hypothesis_Status ret2 =
1025 sm->AlgoStateEngine(event, anHyp);
1033 //=============================================================================
1037 //=============================================================================
1039 void SMESH_subMesh::CleanDependsOn()
1041 MESSAGE("SMESH_subMesh::CleanDependsOn");
1042 // **** parcourir les ancetres dans l'ordre de dépendance
1044 ComputeStateEngine(CLEAN);
1046 const map < int, SMESH_subMesh * >&dependson = DependsOn();
1047 map < int, SMESH_subMesh * >::const_iterator its;
1048 for (its = dependson.begin(); its != dependson.end(); its++)
1050 SMESH_subMesh *sm = (*its).second;
1051 SCRUTE((*its).first);
1052 sm->ComputeStateEngine(CLEAN);
1056 //=============================================================================
1060 //=============================================================================
1062 void SMESH_subMesh::DumpAlgoState(bool isMain)
1064 int dim = SMESH_Gen::GetShapeDim(_subShape);
1065 // if (dim < 1) return;
1068 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1070 map < int, SMESH_subMesh * >::const_iterator itsub;
1071 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1073 SMESH_subMesh *sm = (*itsub).second;
1074 sm->DumpAlgoState(false);
1077 int type = _subShape.ShapeType();
1078 MESSAGE("dim = " << dim << " type of shape " << type);
1082 MESSAGE(" AlgoState = NO_ALGO");
1085 MESSAGE(" AlgoState = MISSING_HYP");
1088 MESSAGE(" AlgoState = HYP_OK");
1091 switch (_computeState)
1094 MESSAGE(" ComputeState = NOT_READY");
1096 case READY_TO_COMPUTE:
1097 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1100 MESSAGE(" ComputeState = COMPUTE_OK");
1102 case FAILED_TO_COMPUTE:
1103 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1108 //=============================================================================
1112 //=============================================================================
1114 bool SMESH_subMesh::ComputeStateEngine(int event)
1116 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1117 //SCRUTE(_computeState);
1120 int dim = SMESH_Gen::GetShapeDim(_subShape);
1124 if ( IsMeshComputed() )
1125 _computeState = COMPUTE_OK;
1127 _computeState = READY_TO_COMPUTE;
1130 SMESH_Gen *gen = _father->GetGen();
1131 SMESH_Algo *algo = 0;
1133 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1135 switch (_computeState)
1138 // ----------------------------------------------------------------------
1143 case MODIF_HYP: // nothing to do
1145 case MODIF_ALGO_STATE:
1146 if (_algoState == HYP_OK)
1148 _computeState = READY_TO_COMPUTE;
1151 case COMPUTE: // nothing to do
1154 RemoveSubMeshElementsAndNodes();
1159 case SUBMESH_COMPUTED: // nothing to do
1161 case SUBMESH_RESTORED:
1162 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1164 case MESH_ENTITY_REMOVED:
1166 case CHECK_COMPUTE_STATE:
1167 if ( IsMeshComputed() )
1168 _computeState = COMPUTE_OK;
1176 // ----------------------------------------------------------------------
1178 case READY_TO_COMPUTE:
1181 case MODIF_HYP: // nothing to do
1183 case MODIF_ALGO_STATE:
1184 _computeState = NOT_READY;
1185 algo = gen->GetAlgo((*_father), _subShape);
1188 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1190 _computeState = READY_TO_COMPUTE;
1195 algo = gen->GetAlgo((*_father), _subShape);
1197 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1200 MESSAGE("***** verify compute state *****");
1201 _computeState = NOT_READY;
1204 // check submeshes needed
1205 if (algo->NeedDescretBoundary())
1206 ret = SubMeshesComputed();
1209 MESSAGE("Some SubMeshes not computed");
1210 _computeState = FAILED_TO_COMPUTE;
1213 RemoveSubMeshElementsAndNodes();
1215 if (!algo->NeedDescretBoundary() && !algo->OnlyUnaryInput())
1216 ret = ApplyToCollection( algo, GetCollection( gen, algo ) );
1218 ret = algo->Compute((*_father), _subShape);
1222 MESSAGE("problem in algo execution: failed to compute");
1223 _computeState = FAILED_TO_COMPUTE;
1224 if (!algo->NeedDescretBoundary())
1225 UpdateSubMeshState( FAILED_TO_COMPUTE );
1228 // Show vertices location of a failed shape
1229 TopExp_Explorer exp( _subShape, TopAbs_VERTEX);
1230 for ( ; exp.More(); exp.Next() ) {
1231 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( exp.Current() )));
1232 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1239 _computeState = COMPUTE_OK;
1240 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1241 if (!algo->NeedDescretBoundary())
1242 UpdateSubMeshState( COMPUTE_OK );
1247 RemoveSubMeshElementsAndNodes();
1248 _computeState = NOT_READY;
1249 algo = gen->GetAlgo((*_father), _subShape);
1252 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1254 _computeState = READY_TO_COMPUTE;
1260 case SUBMESH_COMPUTED: // nothing to do
1262 case SUBMESH_RESTORED:
1263 // check if a mesh is already computed that may
1264 // happen after retrieval from a file
1265 ComputeStateEngine( CHECK_COMPUTE_STATE );
1266 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1268 case MESH_ENTITY_REMOVED:
1270 case CHECK_COMPUTE_STATE:
1271 if ( IsMeshComputed() )
1272 _computeState = COMPUTE_OK;
1280 // ----------------------------------------------------------------------
1286 CleanDependants(); // recursive recall with event CLEANDEP
1287 algo = gen->GetAlgo((*_father), _subShape);
1288 if (algo && !algo->NeedDescretBoundary())
1289 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1291 case MODIF_ALGO_STATE:
1292 CleanDependants(); // recursive recall with event CLEANDEP
1293 algo = gen->GetAlgo((*_father), _subShape);
1294 if (algo && !algo->NeedDescretBoundary())
1295 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1297 case COMPUTE: // nothing to do
1300 RemoveSubMeshElementsAndNodes();
1301 _computeState = NOT_READY;
1302 algo = gen->GetAlgo((*_father), _subShape);
1305 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1307 _computeState = READY_TO_COMPUTE;
1311 CleanDependants(); // recursive recall with event CLEANDEP
1313 case SUBMESH_COMPUTED: // nothing to do
1315 case SUBMESH_RESTORED:
1316 ComputeStateEngine( CHECK_COMPUTE_STATE );
1317 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1319 case MESH_ENTITY_REMOVED:
1320 UpdateDependantsState( CHECK_COMPUTE_STATE );
1321 ComputeStateEngine( CHECK_COMPUTE_STATE );
1322 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1324 case CHECK_COMPUTE_STATE:
1325 if ( !IsMeshComputed() )
1326 if (_algoState == HYP_OK)
1327 _computeState = READY_TO_COMPUTE;
1329 _computeState = NOT_READY;
1337 // ----------------------------------------------------------------------
1339 case FAILED_TO_COMPUTE:
1343 if (_algoState == HYP_OK)
1344 _computeState = READY_TO_COMPUTE;
1346 _computeState = NOT_READY;
1348 case MODIF_ALGO_STATE:
1349 if (_algoState == HYP_OK)
1350 _computeState = READY_TO_COMPUTE;
1352 _computeState = NOT_READY;
1354 case COMPUTE: // nothing to do
1357 RemoveSubMeshElementsAndNodes();
1358 if (_algoState == HYP_OK)
1359 _computeState = READY_TO_COMPUTE;
1361 _computeState = NOT_READY;
1366 case SUBMESH_COMPUTED: // allow retry compute
1367 if (_algoState == HYP_OK)
1368 _computeState = READY_TO_COMPUTE;
1370 _computeState = NOT_READY;
1372 case SUBMESH_RESTORED:
1373 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1375 case MESH_ENTITY_REMOVED:
1377 case CHECK_COMPUTE_STATE:
1378 if ( IsMeshComputed() )
1379 _computeState = COMPUTE_OK;
1381 if (_algoState == HYP_OK)
1382 _computeState = READY_TO_COMPUTE;
1384 _computeState = NOT_READY;
1392 // ----------------------------------------------------------------------
1398 //SCRUTE(_computeState);
1402 //=======================================================================
1403 //function : ApplyToCollection
1404 //purpose : Apply theAlgo to all subshapes in theCollection
1405 //=======================================================================
1407 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1408 const TopoDS_Shape& theCollection)
1410 MESSAGE("SMESH_subMesh::ApplyToCollection");
1411 ASSERT ( !theAlgo->NeedDescretBoundary() );
1416 ret = theAlgo->Compute( *_father, theCollection );
1418 // set _computeState of subshapes
1419 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1420 for ( ; anExplorer.More(); anExplorer.Next() )
1422 const TopoDS_Shape& aSubShape = anExplorer.Current();
1423 SMESH_subMesh* subMesh = _father->GetSubMeshContaining( aSubShape );
1428 subMesh->_computeState = COMPUTE_OK;
1429 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1430 subMesh->UpdateSubMeshState( COMPUTE_OK );
1434 subMesh->_computeState = FAILED_TO_COMPUTE;
1442 //=======================================================================
1443 //function : UpdateSubMeshState
1445 //=======================================================================
1447 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1449 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1450 map<int, SMESH_subMesh*>::const_iterator itsub;
1451 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1453 SMESH_subMesh* sm = (*itsub).second;
1454 sm->_computeState = theState;
1458 //=======================================================================
1459 //function : ComputeSubMeshStateEngine
1461 //=======================================================================
1463 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1465 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1466 map<int, SMESH_subMesh*>::const_iterator itsub;
1467 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1469 SMESH_subMesh* sm = (*itsub).second;
1470 sm->ComputeStateEngine(event);
1474 //=======================================================================
1475 //function : UpdateDependantsState
1477 //=======================================================================
1479 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1481 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1482 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1483 for (; it.More(); it.Next())
1485 const TopoDS_Shape& ancestor = it.Value();
1486 SMESH_subMesh *aSubMesh =
1487 _father->GetSubMeshContaining(ancestor);
1489 aSubMesh->ComputeStateEngine( theEvent );
1493 //=============================================================================
1497 //=============================================================================
1499 void SMESH_subMesh::CleanDependants()
1501 //MESSAGE("SMESH_subMesh::CleanDependants: shape type " << _subShape.ShapeType() );
1503 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1504 for (; it.More(); it.Next())
1506 const TopoDS_Shape& ancestor = it.Value();
1507 //MESSAGE("ancestor shape type " << ancestor.ShapeType() );
1508 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1510 aSubMesh->ComputeStateEngine(CLEANDEP);
1512 ComputeStateEngine(CLEAN);
1516 //=============================================================================
1520 //=============================================================================
1522 static void removeSubMesh( SMESHDS_Mesh * meshDS, const TopoDS_Shape& subShape)
1524 SMESHDS_SubMesh * subMeshDS = meshDS->MeshElements(subShape);
1525 if (subMeshDS!=NULL)
1527 SMDS_ElemIteratorPtr ite=subMeshDS->GetElements();
1530 const SMDS_MeshElement * elt = ite->next();
1531 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1532 meshDS->RemoveElement(elt);
1535 SMDS_NodeIteratorPtr itn=subMeshDS->GetNodes();
1538 const SMDS_MeshNode * node = itn->next();
1539 //MESSAGE( " RM node: "<<node->GetID());
1540 meshDS->RemoveNode(node);
1545 //=============================================================================
1549 //=============================================================================
1551 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1553 SCRUTE(_subShape.ShapeType());
1555 removeSubMesh( _meshDS, _subShape );
1557 // algo may bind a submesh not to _subShape, eg 3D algo
1558 // sets nodes on SHELL while _subShape may be SOLID
1560 int dim = SMESH_Gen::GetShapeDim( _subShape );
1561 int type = _subShape.ShapeType() + 1;
1562 for ( ; type <= TopAbs_EDGE; type++)
1563 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1565 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1566 for ( ; exp.More(); exp.Next() )
1567 removeSubMesh( _meshDS, exp.Current() );
1573 //=======================================================================
1574 //function : IsMeshComputed
1575 //purpose : check if _subMeshDS contains mesh elements
1576 //=======================================================================
1578 bool SMESH_subMesh::IsMeshComputed() const
1580 // algo may bind a submesh not to _subShape, eg 3D algo
1581 // sets nodes on SHELL while _subShape may be SOLID
1583 int dim = SMESH_Gen::GetShapeDim( _subShape );
1584 int type = _subShape.ShapeType();
1585 for ( ; type <= TopAbs_VERTEX; type++) {
1586 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1588 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1589 for ( ; exp.More(); exp.Next() )
1591 SMESHDS_SubMesh * subMeshDS = _meshDS->MeshElements( exp.Current() );
1592 if ( subMeshDS != NULL &&
1593 (subMeshDS->GetElements()->more() || subMeshDS->GetNodes()->more())) {
1606 //=======================================================================
1607 //function : GetCollection
1608 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1609 // meshed at once along with _subShape
1610 //=======================================================================
1612 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1614 MESSAGE("SMESH_subMesh::GetCollection");
1615 ASSERT (!theAlgo->NeedDescretBoundary());
1617 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1619 if ( mainShape.IsSame( _subShape ))
1622 list<const SMESHDS_Hypothesis*> aUsedHyp =
1623 theAlgo->GetUsedHypothesis( *_father, _subShape ); // copy
1625 // put in a compound all shapes with the same hypothesis assigned
1626 // and a good ComputState
1628 TopoDS_Compound aCompound;
1629 BRep_Builder aBuilder;
1630 aBuilder.MakeCompound( aCompound );
1632 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1633 for ( ; anExplorer.More(); anExplorer.Next() )
1635 const TopoDS_Shape& S = anExplorer.Current();
1636 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1637 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1639 if (subMesh->GetComputeState() == READY_TO_COMPUTE &&
1640 anAlgo == theAlgo &&
1641 anAlgo->GetUsedHypothesis( *_father, S ) == aUsedHyp)
1643 aBuilder.Add( aCompound, S );
1650 //=======================================================================
1651 //function : GetSimilarAttached
1652 //purpose : return nb of hypotheses attached to theShape.
1653 // If theHyp is provided, similar but not same hypotheses
1654 // are countered; else only applicable ones having theHypType
1656 //=======================================================================
1658 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1659 const SMESH_Hypothesis * theHyp,
1660 const int theHypType)
1662 const list<const SMESHDS_Hypothesis*>& aHypList =
1663 _father->GetHypothesisList( theShape );
1664 list<const SMESHDS_Hypothesis*>::const_iterator it = aHypList.begin();
1665 for ( ; it != aHypList.end(); it++ )
1667 const SMESH_Hypothesis* hyp = static_cast< const SMESH_Hypothesis *>( *it );
1671 if (hyp != theHyp &&
1672 hyp->GetType() == theHyp->GetType() &&
1673 hyp->GetDim() == theHyp->GetDim())
1678 if ( hyp->GetType() == theHypType && IsApplicableHypotesis( hyp ))
1686 //=======================================================================
1687 //function : CheckConcurentHypothesis
1688 //purpose : check if there are several applicable hypothesis attached to
1690 //=======================================================================
1692 SMESH_Hypothesis::Hypothesis_Status
1693 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1695 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1697 // is there local hypothesis on me?
1698 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1699 return SMESH_Hypothesis::HYP_OK;
1702 TopoDS_Shape aPrevWithHyp;
1703 const SMESH_Hypothesis* aPrevHyp = 0;
1704 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1705 for (; it.More(); it.Next())
1707 const TopoDS_Shape& ancestor = it.Value();
1708 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1711 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1713 aPrevWithHyp = ancestor;
1716 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1717 return SMESH_Hypothesis::HYP_CONCURENT;
1719 return SMESH_Hypothesis::HYP_OK;
1722 return SMESH_Hypothesis::HYP_OK;