1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
30 #include "SMESH_subMesh.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_Hypothesis.hxx"
34 #include "SMESH_Algo.hxx"
35 #include "SMESH_HypoFilter.hxx"
37 #include "utilities.h"
40 #include <BRep_Builder.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <TopTools_MapOfShape.hxx>
45 #include <TopTools_ListOfShape.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
51 #include <BRep_Tool.hxx>
53 #include <TopTools_IndexedMapOfShape.hxx>
56 //=============================================================================
58 * default constructor:
60 //=============================================================================
62 SMESH_subMesh::SMESH_subMesh(int Id, SMESH_Mesh * father, SMESHDS_Mesh * meshDS,
63 const TopoDS_Shape & aSubShape)
65 _subShape = aSubShape;
67 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
70 _dependenceAnalysed = false;
72 if (_subShape.ShapeType() == TopAbs_VERTEX)
75 _computeState = READY_TO_COMPUTE;
80 _computeState = NOT_READY;
84 //=============================================================================
88 //=============================================================================
90 SMESH_subMesh::~SMESH_subMesh()
92 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
96 //=============================================================================
100 //=============================================================================
102 int SMESH_subMesh::GetId() const
104 //MESSAGE("SMESH_subMesh::GetId");
108 //=============================================================================
112 //=============================================================================
114 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
116 //MESSAGE("SMESH_subMesh::GetSubMeshDS");
117 if (_subMeshDS==NULL)
119 //MESSAGE("subMesh pointer still null, trying to get it...");
120 _subMeshDS = _meshDS->MeshElements(_subShape); // may be null ...
121 if (_subMeshDS==NULL)
123 MESSAGE("problem... subMesh still empty");
125 //NRI throw SALOME_Exception(LOCALIZED(subMesh still empty));
131 //=============================================================================
135 //=============================================================================
137 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
139 if ( !GetSubMeshDS() )
140 _meshDS->NewSubMesh( _meshDS->ShapeToIndex( _subShape ) );
142 return GetSubMeshDS();
145 //=============================================================================
149 //=============================================================================
151 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
153 //MESSAGE("SMESH_subMesh::GetFirstToCompute");
154 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
155 SMESH_subMesh *firstToCompute = 0;
157 map < int, SMESH_subMesh * >::const_iterator itsub;
158 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
160 SMESH_subMesh *sm = (*itsub).second;
161 // SCRUTE(sm->GetId());
162 // SCRUTE(sm->GetComputeState());
163 bool readyToCompute = (sm->GetComputeState() == READY_TO_COMPUTE);
167 //SCRUTE(sm->GetId());
173 return firstToCompute; // a subMesh of this
175 if (_computeState == READY_TO_COMPUTE)
179 return 0; // nothing to compute
182 //=============================================================================
186 //=============================================================================
188 bool SMESH_subMesh::SubMeshesComputed()
190 //MESSAGE("SMESH_subMesh::SubMeshesComputed");
191 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
193 bool subMeshesComputed = true;
194 map < int, SMESH_subMesh * >::const_iterator itsub;
195 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
197 SMESH_subMesh *sm = (*itsub).second;
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
469 //=======================================================================
471 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
472 const TopAbs_ShapeEnum theShapeType)
474 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
476 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
480 switch ( theShapeType ) {
481 case TopAbs_EDGE: aShapeDim = 1; break;
482 case TopAbs_FACE: aShapeDim = 2; break;
483 case TopAbs_SHELL:aShapeDim = 3; break;
484 case TopAbs_SOLID:aShapeDim = 3; break;
485 // case TopAbs_VERTEX:
487 // case TopAbs_COMPSOLID:
488 // case TopAbs_COMPOUND:
489 default: return false;
492 return ( theHypothesis->GetDim() == aShapeDim );
495 //=============================================================================
499 //=============================================================================
501 SMESH_Hypothesis::Hypothesis_Status
502 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
504 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
505 //SCRUTE(_algoState);
508 // **** les retour des evenement shape sont significatifs
509 // (add ou remove fait ou non)
510 // le retour des evenement father n'indiquent pas que add ou remove fait
512 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
514 int dim = SMESH_Gen::GetShapeDim(_subShape);
519 if (event == ADD_HYP || event == ADD_ALGO)
520 return SMESH_Hypothesis::HYP_BAD_DIM; // do not allow to assign any hyp
522 return SMESH_Hypothesis::HYP_OK;
525 SMESH_Gen* gen =_father->GetGen();
527 int oldAlgoState = _algoState;
528 bool modifiedHyp = false; // if set to true, force event MODIF_ALGO_STATE
529 // in ComputeStateEngine
531 // ----------------------
532 // check mesh conformity
533 // ----------------------
534 if (event == ADD_ALGO)
536 if (IsApplicableHypotesis( anHyp ) &&
537 !_father->IsNotConformAllowed() &&
538 !IsConform( static_cast< SMESH_Algo* >( anHyp )))
539 return SMESH_Hypothesis::HYP_NOTCONFORM;
542 // ----------------------------------
543 // add a hypothesis to DS if possible
544 // ----------------------------------
545 if (event == ADD_HYP || event == ADD_ALGO)
547 if ( ! CanAddHypothesis( anHyp ))
548 return SMESH_Hypothesis::HYP_BAD_DIM;
550 if ( GetSimilarAttached( _subShape, anHyp ) )
551 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
553 if ( !_meshDS->AddHypothesis(_subShape, anHyp))
554 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
556 // Serve Propagation of 1D hypothesis
557 if (event == ADD_HYP) {
558 bool isPropagationOk = true;
559 string hypName = anHyp->GetName();
561 if (hypName == "Propagation") {
562 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
563 TopTools_MapOfShape aMap;
564 for (; exp.More(); exp.Next()) {
565 if (aMap.Add(exp.Current())) {
566 if (!_father->BuildPropagationChain(exp.Current())) {
567 isPropagationOk = false;
572 else if (anHyp->GetDim() == 1) { // Only 1D hypothesis can be propagated
573 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
574 TopTools_MapOfShape aMap;
575 for (; exp.More(); exp.Next()) {
576 if (aMap.Add(exp.Current())) {
577 TopoDS_Shape aMainEdge;
578 if (_father->IsPropagatedHypothesis(exp.Current(), aMainEdge)) {
579 isPropagationOk = _father->RebuildPropagationChains();
580 } else if (_father->IsPropagationHypothesis(exp.Current())) {
581 isPropagationOk = _father->BuildPropagationChain(exp.Current());
589 if (!isPropagationOk && ret < SMESH_Hypothesis::HYP_CONCURENT) {
590 ret = SMESH_Hypothesis::HYP_CONCURENT;
592 } // Serve Propagation of 1D hypothesis
595 // --------------------------
596 // remove a hypothesis from DS
597 // --------------------------
598 if (event == REMOVE_HYP || event == REMOVE_ALGO)
600 if (!_meshDS->RemoveHypothesis(_subShape, anHyp))
601 return SMESH_Hypothesis::HYP_OK; // nothing changes
603 // Serve Propagation of 1D hypothesis
604 if (event == REMOVE_HYP)
606 bool isPropagationOk = true;
607 SMESH_HypoFilter propagFilter( SMESH_HypoFilter::HasName( "Propagation" ));
608 if ( propagFilter.IsOk( anHyp, _subShape ))
610 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
611 TopTools_MapOfShape aMap;
612 for (; exp.More(); exp.Next()) {
613 if (aMap.Add(exp.Current()) &&
614 !_father->GetHypothesis( exp.Current(), propagFilter, true )) {
615 // no more Propagation on the current edge
616 if (!_father->RemovePropagationChain(exp.Current())) {
617 return SMESH_Hypothesis::HYP_UNKNOWN_FATAL;
621 // rebuild propagation chains, because removing one
622 // chain can resolve concurention, existing before
623 isPropagationOk = _father->RebuildPropagationChains();
625 else if (anHyp->GetDim() == 1) // Only 1D hypothesis can be propagated
627 isPropagationOk = _father->RebuildPropagationChains();
630 if (!isPropagationOk && ret < SMESH_Hypothesis::HYP_CONCURENT) {
631 ret = SMESH_Hypothesis::HYP_CONCURENT;
633 } // Serve Propagation of 1D hypothesis
636 // ------------------
637 // analyse algo state
638 // ------------------
639 if (!IsApplicableHypotesis( anHyp ))
640 return ret; // not applicable hypotheses do not change algo state
645 // ----------------------------------------------------------------------
652 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
654 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
655 SetAlgoState(HYP_OK);
657 SetAlgoState(MISSING_HYP);
666 case ADD_FATHER_ALGO: { // Algo just added in father
667 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
669 if ( algo == anHyp ) {
670 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
671 SetAlgoState(HYP_OK);
673 SetAlgoState(MISSING_HYP);
677 case REMOVE_FATHER_HYP:
679 case REMOVE_FATHER_ALGO: {
680 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
683 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
684 SetAlgoState(HYP_OK);
686 SetAlgoState(MISSING_HYP);
696 // ----------------------------------------------------------------------
702 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
704 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
705 SetAlgoState(HYP_OK);
706 if (SMESH_Hypothesis::IsStatusFatal( ret ))
707 _meshDS->RemoveHypothesis(_subShape, anHyp);
708 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
710 _meshDS->RemoveHypothesis(_subShape, anHyp);
711 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
715 case ADD_ALGO: { //already existing algo : on father ?
716 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
718 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
719 SetAlgoState(HYP_OK);
721 SetAlgoState(MISSING_HYP);
726 case REMOVE_ALGO: { // perhaps a father algo applies ?
727 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
728 if (algo == NULL) // no more algo applying on subShape...
730 SetAlgoState(NO_ALGO);
734 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
735 SetAlgoState(HYP_OK);
737 SetAlgoState(MISSING_HYP);
741 case ADD_FATHER_HYP: {
742 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
744 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
745 SetAlgoState(HYP_OK);
747 SetAlgoState(MISSING_HYP);
750 case ADD_FATHER_ALGO: { // new father algo
751 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
753 if ( algo == anHyp ) {
754 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
755 SetAlgoState(HYP_OK);
757 SetAlgoState(MISSING_HYP);
761 case REMOVE_FATHER_HYP: // nothing to do
763 case REMOVE_FATHER_ALGO: {
764 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
765 if (algo == NULL) // no more applying algo on father
767 SetAlgoState(NO_ALGO);
771 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
772 SetAlgoState(HYP_OK);
774 SetAlgoState(MISSING_HYP);
784 // ----------------------------------------------------------------------
790 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
792 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
794 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
795 // ret should be fatal: anHyp was not added
796 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
798 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
799 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
801 if (SMESH_Hypothesis::IsStatusFatal( ret ))
803 MESSAGE("do not add extra hypothesis");
804 _meshDS->RemoveHypothesis(_subShape, anHyp);
812 case ADD_ALGO: { //already existing algo : on father ?
813 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
814 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
815 // check if algo changes
817 f.Init( SMESH_HypoFilter::IsAlgo() );
818 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
819 f.AndNot( SMESH_HypoFilter::Is( algo ));
820 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
822 string(algo->GetName()) != string(prevAlgo->GetName()) )
826 SetAlgoState(MISSING_HYP);
830 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
832 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
833 SetAlgoState(HYP_OK);
835 SetAlgoState(MISSING_HYP);
839 case REMOVE_ALGO: { // perhaps a father algo applies ?
840 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
841 if (algo == NULL) // no more algo applying on subShape...
843 SetAlgoState(NO_ALGO);
847 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
848 // check if algo remains
849 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
853 SetAlgoState(MISSING_HYP);
857 case ADD_FATHER_HYP: { // new father hypothesis ?
858 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
860 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
862 if (_father->IsUsedHypothesis( anHyp, _subShape )) // new Hyp
866 SetAlgoState(MISSING_HYP);
869 case ADD_FATHER_ALGO: {
870 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
871 if ( algo == anHyp ) { // a new algo on father
872 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
873 // check if algo changes
875 f.Init( SMESH_HypoFilter::IsAlgo() );
876 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
877 f.AndNot( SMESH_HypoFilter::Is( algo ));
878 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
880 string(algo->GetName()) != string(prevAlgo->GetName()) )
884 SetAlgoState(MISSING_HYP);
888 case REMOVE_FATHER_HYP: {
889 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
891 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
892 // is there the same local hyp or maybe a new father algo applied?
893 if ( !GetSimilarAttached( _subShape, anHyp ) )
897 SetAlgoState(MISSING_HYP);
900 case REMOVE_FATHER_ALGO: {
901 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
902 if (algo == NULL) // no more applying algo on father
904 SetAlgoState(NO_ALGO);
908 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
909 // check if algo changes
910 if ( string(algo->GetName()) != string( anHyp->GetName()) )
914 SetAlgoState(MISSING_HYP);
924 // ----------------------------------------------------------------------
931 if ((_algoState != oldAlgoState) || modifiedHyp)
932 ComputeStateEngine(MODIF_ALGO_STATE);
938 //=======================================================================
939 //function : IsConform
940 //purpose : check if a conform mesh will be produced by the Algo
941 //=======================================================================
943 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
945 // MESSAGE( "SMESH_subMesh::IsConform" );
947 if ( !theAlgo ) return false;
949 // check only algo that doesn't NeedDescretBoundary(): because mesh made
950 // on a sub-shape will be ignored by theAlgo
951 if ( theAlgo->NeedDescretBoundary() )
954 SMESH_Gen* gen =_father->GetGen();
956 // only local algo is to be checked
957 if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
960 // check algo attached to adjacent shapes
962 // loop on one level down sub-meshes
963 TopoDS_Iterator itsub( _subShape );
964 for (; itsub.More(); itsub.Next())
966 // loop on adjacent subShapes
967 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
968 for (; it.More(); it.Next())
970 const TopoDS_Shape& adjacent = it.Value();
971 if ( _subShape.IsSame( adjacent )) continue;
972 if ( adjacent.ShapeType() != _subShape.ShapeType())
975 // check algo attached to smAdjacent
976 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
979 !algo->NeedDescretBoundary() /*&&
980 !gen->IsGlobalHypothesis( algo, *_father )*/)
981 return false; // NOT CONFORM MESH WILL BE PRODUCED
988 //=============================================================================
992 //=============================================================================
994 void SMESH_subMesh::SetAlgoState(int state)
996 // if (state != _oldAlgoState)
997 // int retc = ComputeStateEngine(MODIF_ALGO_STATE);
1001 //=============================================================================
1005 //=============================================================================
1006 SMESH_Hypothesis::Hypothesis_Status
1007 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1008 SMESH_Hypothesis * anHyp)
1010 //MESSAGE("SMESH_subMesh::SubMeshesAlgoStateEngine");
1011 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1012 //EAP: a wire (dim==1) should notify edges (dim==1)
1013 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1014 if (/*EAP:dim > 1*/ _subShape.ShapeType() < TopAbs_EDGE )
1016 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1018 map < int, SMESH_subMesh * >::const_iterator itsub;
1019 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1021 SMESH_subMesh *sm = (*itsub).second;
1022 SMESH_Hypothesis::Hypothesis_Status ret2 =
1023 sm->AlgoStateEngine(event, anHyp);
1031 //=============================================================================
1035 //=============================================================================
1037 void SMESH_subMesh::CleanDependsOn()
1039 MESSAGE("SMESH_subMesh::CleanDependsOn");
1040 // **** parcourir les ancetres dans l'ordre de dépendance
1042 ComputeStateEngine(CLEAN);
1044 const map < int, SMESH_subMesh * >&dependson = DependsOn();
1045 map < int, SMESH_subMesh * >::const_iterator its;
1046 for (its = dependson.begin(); its != dependson.end(); its++)
1048 SMESH_subMesh *sm = (*its).second;
1049 SCRUTE((*its).first);
1050 sm->ComputeStateEngine(CLEAN);
1054 //=============================================================================
1058 //=============================================================================
1060 void SMESH_subMesh::DumpAlgoState(bool isMain)
1062 int dim = SMESH_Gen::GetShapeDim(_subShape);
1063 // if (dim < 1) return;
1066 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1068 map < int, SMESH_subMesh * >::const_iterator itsub;
1069 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1071 SMESH_subMesh *sm = (*itsub).second;
1072 sm->DumpAlgoState(false);
1075 int type = _subShape.ShapeType();
1076 MESSAGE("dim = " << dim << " type of shape " << type);
1080 MESSAGE(" AlgoState = NO_ALGO");
1083 MESSAGE(" AlgoState = MISSING_HYP");
1086 MESSAGE(" AlgoState = HYP_OK");
1089 switch (_computeState)
1092 MESSAGE(" ComputeState = NOT_READY");
1094 case READY_TO_COMPUTE:
1095 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1098 MESSAGE(" ComputeState = COMPUTE_OK");
1100 case FAILED_TO_COMPUTE:
1101 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1106 //=============================================================================
1110 //=============================================================================
1112 static void removeSubMesh( SMESHDS_Mesh * meshDS, const TopoDS_Shape& subShape)
1114 SMESHDS_SubMesh * subMeshDS = meshDS->MeshElements(subShape);
1115 if (subMeshDS!=NULL)
1117 SMDS_ElemIteratorPtr ite=subMeshDS->GetElements();
1120 const SMDS_MeshElement * elt = ite->next();
1121 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1122 meshDS->RemoveElement(elt);
1125 SMDS_NodeIteratorPtr itn=subMeshDS->GetNodes();
1128 const SMDS_MeshNode * node = itn->next();
1129 //MESSAGE( " RM node: "<<node->GetID());
1130 meshDS->RemoveNode(node);
1135 //=============================================================================
1139 //=============================================================================
1141 bool SMESH_subMesh::ComputeStateEngine(int event)
1143 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1144 //SCRUTE(_computeState);
1147 int dim = SMESH_Gen::GetShapeDim(_subShape);
1151 if ( IsMeshComputed() )
1152 _computeState = COMPUTE_OK;
1154 _computeState = READY_TO_COMPUTE;
1157 SMESH_Gen *gen = _father->GetGen();
1158 SMESH_Algo *algo = 0;
1160 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1162 switch (_computeState)
1165 // ----------------------------------------------------------------------
1170 case MODIF_HYP: // nothing to do
1172 case MODIF_ALGO_STATE:
1173 if (_algoState == HYP_OK)
1175 _computeState = READY_TO_COMPUTE;
1178 case COMPUTE: // nothing to do
1181 RemoveSubMeshElementsAndNodes();
1186 case SUBMESH_COMPUTED: // nothing to do
1188 case SUBMESH_RESTORED:
1189 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1191 case MESH_ENTITY_REMOVED:
1193 case CHECK_COMPUTE_STATE:
1194 if ( IsMeshComputed() )
1195 _computeState = COMPUTE_OK;
1203 // ----------------------------------------------------------------------
1205 case READY_TO_COMPUTE:
1208 case MODIF_HYP: // nothing to do
1210 case MODIF_ALGO_STATE:
1211 _computeState = NOT_READY;
1212 algo = gen->GetAlgo((*_father), _subShape);
1215 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1217 _computeState = READY_TO_COMPUTE;
1222 algo = gen->GetAlgo((*_father), _subShape);
1224 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1227 MESSAGE("***** verify compute state *****");
1228 _computeState = NOT_READY;
1231 // check submeshes needed
1232 if (algo->NeedDescretBoundary())
1233 ret = SubMeshesComputed();
1236 MESSAGE("Some SubMeshes not computed");
1237 _computeState = FAILED_TO_COMPUTE;
1241 removeSubMesh( _meshDS, _subShape );
1242 if (!algo->NeedDescretBoundary() && !algo->OnlyUnaryInput())
1243 ret = ApplyToCollection( algo, GetCollection( gen, algo ) );
1245 ret = algo->Compute((*_father), _subShape);
1249 MESSAGE("problem in algo execution: failed to compute");
1250 _computeState = FAILED_TO_COMPUTE;
1251 if (!algo->NeedDescretBoundary())
1252 UpdateSubMeshState( FAILED_TO_COMPUTE );
1255 // Show vertices location of a failed shape
1256 TopTools_IndexedMapOfShape vMap;
1257 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1258 for ( int iv = 1; iv <= vMap.Extent(); ++iv ) {
1259 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1260 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1267 _computeState = COMPUTE_OK;
1268 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1269 if (!algo->NeedDescretBoundary())
1270 UpdateSubMeshState( COMPUTE_OK );
1275 RemoveSubMeshElementsAndNodes();
1276 _computeState = NOT_READY;
1277 algo = gen->GetAlgo((*_father), _subShape);
1280 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1282 _computeState = READY_TO_COMPUTE;
1288 case SUBMESH_COMPUTED: // nothing to do
1290 case SUBMESH_RESTORED:
1291 // check if a mesh is already computed that may
1292 // happen after retrieval from a file
1293 ComputeStateEngine( CHECK_COMPUTE_STATE );
1294 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1296 case MESH_ENTITY_REMOVED:
1298 case CHECK_COMPUTE_STATE:
1299 if ( IsMeshComputed() )
1300 _computeState = COMPUTE_OK;
1308 // ----------------------------------------------------------------------
1314 CleanDependants(); // recursive recall with event CLEANDEP
1315 algo = gen->GetAlgo((*_father), _subShape);
1316 if (algo && !algo->NeedDescretBoundary())
1317 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1319 case MODIF_ALGO_STATE:
1320 CleanDependants(); // recursive recall with event CLEANDEP
1321 algo = gen->GetAlgo((*_father), _subShape);
1322 if (algo && !algo->NeedDescretBoundary())
1323 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1325 case COMPUTE: // nothing to do
1328 RemoveSubMeshElementsAndNodes();
1329 _computeState = NOT_READY;
1330 algo = gen->GetAlgo((*_father), _subShape);
1333 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1335 _computeState = READY_TO_COMPUTE;
1339 CleanDependants(); // recursive recall with event CLEANDEP
1341 case SUBMESH_COMPUTED: // nothing to do
1343 case SUBMESH_RESTORED:
1344 ComputeStateEngine( CHECK_COMPUTE_STATE );
1345 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1347 case MESH_ENTITY_REMOVED:
1348 UpdateDependantsState( CHECK_COMPUTE_STATE );
1349 ComputeStateEngine( CHECK_COMPUTE_STATE );
1350 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1352 case CHECK_COMPUTE_STATE:
1353 if ( !IsMeshComputed() )
1354 if (_algoState == HYP_OK)
1355 _computeState = READY_TO_COMPUTE;
1357 _computeState = NOT_READY;
1365 // ----------------------------------------------------------------------
1367 case FAILED_TO_COMPUTE:
1371 if (_algoState == HYP_OK)
1372 _computeState = READY_TO_COMPUTE;
1374 _computeState = NOT_READY;
1376 case MODIF_ALGO_STATE:
1377 if (_algoState == HYP_OK)
1378 _computeState = READY_TO_COMPUTE;
1380 _computeState = NOT_READY;
1382 case COMPUTE: // nothing to do
1385 RemoveSubMeshElementsAndNodes();
1386 if (_algoState == HYP_OK)
1387 _computeState = READY_TO_COMPUTE;
1389 _computeState = NOT_READY;
1394 case SUBMESH_COMPUTED: // allow retry compute
1395 if (_algoState == HYP_OK)
1396 _computeState = READY_TO_COMPUTE;
1398 _computeState = NOT_READY;
1400 case SUBMESH_RESTORED:
1401 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1403 case MESH_ENTITY_REMOVED:
1405 case CHECK_COMPUTE_STATE:
1406 if ( IsMeshComputed() )
1407 _computeState = COMPUTE_OK;
1409 if (_algoState == HYP_OK)
1410 _computeState = READY_TO_COMPUTE;
1412 _computeState = NOT_READY;
1420 // ----------------------------------------------------------------------
1426 //SCRUTE(_computeState);
1430 //=======================================================================
1431 //function : ApplyToCollection
1432 //purpose : Apply theAlgo to all subshapes in theCollection
1433 //=======================================================================
1435 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1436 const TopoDS_Shape& theCollection)
1438 MESSAGE("SMESH_subMesh::ApplyToCollection");
1439 ASSERT ( !theAlgo->NeedDescretBoundary() );
1444 ret = theAlgo->Compute( *_father, theCollection );
1446 // set _computeState of subshapes
1447 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1448 for ( ; anExplorer.More(); anExplorer.Next() )
1450 const TopoDS_Shape& aSubShape = anExplorer.Current();
1451 SMESH_subMesh* subMesh = _father->GetSubMeshContaining( aSubShape );
1456 subMesh->_computeState = COMPUTE_OK;
1457 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1458 subMesh->UpdateSubMeshState( COMPUTE_OK );
1462 subMesh->_computeState = FAILED_TO_COMPUTE;
1470 //=======================================================================
1471 //function : UpdateSubMeshState
1473 //=======================================================================
1475 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1477 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1478 map<int, SMESH_subMesh*>::const_iterator itsub;
1479 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1481 SMESH_subMesh* sm = (*itsub).second;
1482 sm->_computeState = theState;
1486 //=======================================================================
1487 //function : ComputeSubMeshStateEngine
1489 //=======================================================================
1491 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1493 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1494 map<int, SMESH_subMesh*>::const_iterator itsub;
1495 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1497 SMESH_subMesh* sm = (*itsub).second;
1498 sm->ComputeStateEngine(event);
1502 //=======================================================================
1503 //function : UpdateDependantsState
1505 //=======================================================================
1507 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1509 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1510 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1511 for (; it.More(); it.Next())
1513 const TopoDS_Shape& ancestor = it.Value();
1514 SMESH_subMesh *aSubMesh =
1515 _father->GetSubMeshContaining(ancestor);
1517 aSubMesh->ComputeStateEngine( theEvent );
1521 //=============================================================================
1525 //=============================================================================
1527 void SMESH_subMesh::CleanDependants()
1529 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1530 for (; it.More(); it.Next())
1532 const TopoDS_Shape& ancestor = it.Value();
1533 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEANDEP)
1534 // will erase mesh on other shapes in a compound
1535 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1536 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1538 aSubMesh->ComputeStateEngine(CLEANDEP);
1541 ComputeStateEngine(CLEAN);
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;