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 #include <Standard_Failure.hxx>
57 #include <Standard_ErrorHandler.hxx>
59 //=============================================================================
61 * default constructor:
63 //=============================================================================
65 SMESH_subMesh::SMESH_subMesh(int Id, SMESH_Mesh * father, SMESHDS_Mesh * meshDS,
66 const TopoDS_Shape & aSubShape)
68 _subShape = aSubShape;
70 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
73 _dependenceAnalysed = false;
75 if (_subShape.ShapeType() == TopAbs_VERTEX)
78 _computeState = READY_TO_COMPUTE;
83 _computeState = NOT_READY;
87 //=============================================================================
91 //=============================================================================
93 SMESH_subMesh::~SMESH_subMesh()
95 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
99 //=============================================================================
103 //=============================================================================
105 int SMESH_subMesh::GetId() const
107 //MESSAGE("SMESH_subMesh::GetId");
111 //=============================================================================
115 //=============================================================================
117 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
119 // submesh appears in DS only when a mesher set nodes and elements on it
120 if (_subMeshDS==NULL)
122 _subMeshDS = _meshDS->MeshElements(_subShape); // may be null ...
123 // if (_subMeshDS==NULL)
125 // MESSAGE("problem... 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 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
154 SMESH_subMesh *firstToCompute = 0;
156 map < int, SMESH_subMesh * >::const_iterator itsub;
157 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
159 SMESH_subMesh *sm = (*itsub).second;
160 bool readyToCompute = (sm->GetComputeState() == READY_TO_COMPUTE);
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;
194 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
196 // There are some tricks with compute states, e.g. Penta_3D leaves
197 // one face with READY_TO_COMPUTE state in order to be able to
198 // recompute 3D when a locale triangle hypo changes (see PAL7428).
199 // So we check if mesh is really present
200 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
201 (ds && ( ds->GetNodes()->more() || ds->GetElements()->more() )));
204 const TopoDS_Shape & ss = sm->GetSubShape();
205 int type = ss.ShapeType();
207 subMeshesComputed = false;
211 case TopAbs_COMPOUND:
213 MESSAGE("The not computed sub mesh is a COMPOUND");
216 case TopAbs_COMPSOLID:
218 MESSAGE("The not computed sub mesh is a COMPSOLID");
223 MESSAGE("The not computed sub mesh is a SHEL");
228 MESSAGE("The not computed sub mesh is a WIRE");
233 MESSAGE("The not computed sub mesh is a SOLID");
238 MESSAGE("The not computed sub mesh is a FACE");
243 MESSAGE("The not computed sub mesh is a EDGE");
248 MESSAGE("The not computed sub mesh is of unknown type");
256 return subMeshesComputed;
259 //=============================================================================
263 //=============================================================================
265 bool SMESH_subMesh::SubMeshesReady()
267 MESSAGE("SMESH_subMesh::SubMeshesReady");
268 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
270 bool subMeshesReady = true;
271 map < int, SMESH_subMesh * >::const_iterator itsub;
272 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
274 SMESH_subMesh *sm = (*itsub).second;
275 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK)
276 || (sm->GetComputeState() == READY_TO_COMPUTE));
279 subMeshesReady = false;
284 return subMeshesReady;
287 //=============================================================================
289 * Construct dependence on first level subMeshes. complex shapes (compsolid,
290 * shell, wire) are not analysed the same way as simple shapes (solid, face,
292 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
293 * with possible multiples occurences. Multiples occurences corresponds to
294 * internal frontiers within shapes of the collection and must not be keeped.
295 * See FinalizeDependence.
297 //=============================================================================
299 const map < int, SMESH_subMesh * >&SMESH_subMesh::DependsOn()
301 if (_dependenceAnalysed)
304 //MESSAGE("SMESH_subMesh::DependsOn");
306 int type = _subShape.ShapeType();
310 case TopAbs_COMPOUND:
312 //MESSAGE("compound");
313 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
316 InsertDependence(exp.Current());
318 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
321 InsertDependence(exp.Current()); //only shell not in solid
323 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
326 InsertDependence(exp.Current());
328 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
331 InsertDependence(exp.Current());
335 case TopAbs_COMPSOLID:
337 //MESSAGE("compsolid");
338 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
341 InsertDependence(exp.Current());
348 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
351 InsertDependence(exp.Current());
358 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
361 InsertDependence(exp.Current());
368 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
371 InsertDependence(exp.Current());
378 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
381 InsertDependence(exp.Current());
388 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
391 InsertDependence(exp.Current());
404 _dependenceAnalysed = true;
408 //=============================================================================
410 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
412 //=============================================================================
414 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
416 //MESSAGE("SMESH_subMesh::InsertDependence");
417 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
418 int type = aSubShape.ShapeType();
419 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
420 int cle = aSubMesh->GetId();
421 cle += 10000000 * ordType; // sort map by ordType then index
422 if ( _mapDepend.find( cle ) == _mapDepend.end())
424 _mapDepend[cle] = aSubMesh;
425 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
426 _mapDepend.insert( subMap.begin(), subMap.end() );
430 //=============================================================================
434 //=============================================================================
436 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
438 //MESSAGE("SMESH_subMesh::GetSubShape");
443 //=======================================================================
444 //function : CanAddHypothesis
445 //purpose : return true if theHypothesis can be attached to me:
446 // its dimention is checked
447 //=======================================================================
449 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
451 int aHypDim = theHypothesis->GetDim();
452 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
453 if ( aHypDim <= aShapeDim )
455 // if ( aHypDim < aShapeDim )
456 // return ( _father->IsMainShape( _subShape ));
461 //=======================================================================
462 //function : IsApplicableHypotesis
464 //=======================================================================
466 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
467 const TopAbs_ShapeEnum theShapeType)
469 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
471 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
474 switch ( theShapeType ) {
479 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
481 // case TopAbs_COMPSOLID:
482 // case TopAbs_COMPOUND:
488 //=============================================================================
492 //=============================================================================
494 SMESH_Hypothesis::Hypothesis_Status
495 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
497 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
498 //SCRUTE(_algoState);
501 // **** les retour des evenement shape sont significatifs
502 // (add ou remove fait ou non)
503 // le retour des evenement father n'indiquent pas que add ou remove fait
505 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
507 int dim = SMESH_Gen::GetShapeDim(_subShape);
512 if (event == ADD_HYP || event == ADD_ALGO)
513 return SMESH_Hypothesis::HYP_BAD_DIM; // do not allow to assign any hyp
515 return SMESH_Hypothesis::HYP_OK;
518 SMESH_Gen* gen =_father->GetGen();
520 int oldAlgoState = _algoState;
521 bool modifiedHyp = false; // if set to true, force event MODIF_ALGO_STATE
522 // in ComputeStateEngine
524 // ----------------------
525 // check mesh conformity
526 // ----------------------
527 if (event == ADD_ALGO)
529 if (IsApplicableHypotesis( anHyp ) &&
530 !_father->IsNotConformAllowed() &&
531 !IsConform( static_cast< SMESH_Algo* >( anHyp )))
532 return SMESH_Hypothesis::HYP_NOTCONFORM;
535 // ----------------------------------
536 // add a hypothesis to DS if possible
537 // ----------------------------------
538 if (event == ADD_HYP || event == ADD_ALGO)
540 if ( ! CanAddHypothesis( anHyp ))
541 return SMESH_Hypothesis::HYP_BAD_DIM;
543 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
544 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
546 if ( !_meshDS->AddHypothesis(_subShape, anHyp))
547 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
549 // Serve Propagation of 1D hypothesis
550 if (event == ADD_HYP) {
551 bool isPropagationOk = true;
552 bool isPropagationHyp = ( strcmp( "Propagation", anHyp->GetName() ) == 0 );
554 if ( isPropagationHyp ) {
555 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
556 TopTools_MapOfShape aMap;
557 for (; exp.More(); exp.Next()) {
558 if (aMap.Add(exp.Current())) {
559 if (!_father->BuildPropagationChain(exp.Current())) {
560 isPropagationOk = false;
565 else if (anHyp->GetDim() == 1) { // Only 1D hypothesis can be propagated
566 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
567 TopTools_MapOfShape aMap;
568 for (; exp.More(); exp.Next()) {
569 if (aMap.Add(exp.Current())) {
570 TopoDS_Shape aMainEdge;
571 if (_father->IsPropagatedHypothesis(exp.Current(), aMainEdge)) {
572 isPropagationOk = _father->RebuildPropagationChains();
573 } else if (_father->IsPropagationHypothesis(exp.Current())) {
574 isPropagationOk = _father->BuildPropagationChain(exp.Current());
582 if ( isPropagationOk ) {
583 if ( isPropagationHyp )
584 return ret; // nothing more to do for "Propagation" hypothesis
586 else if ( ret < SMESH_Hypothesis::HYP_CONCURENT) {
587 ret = SMESH_Hypothesis::HYP_CONCURENT;
589 } // Serve Propagation of 1D hypothesis
592 // --------------------------
593 // remove a hypothesis from DS
594 // --------------------------
595 if (event == REMOVE_HYP || event == REMOVE_ALGO)
597 if (!_meshDS->RemoveHypothesis(_subShape, anHyp))
598 return SMESH_Hypothesis::HYP_OK; // nothing changes
600 // Serve Propagation of 1D hypothesis
601 if (event == REMOVE_HYP)
603 bool isPropagationOk = true;
604 SMESH_HypoFilter propagFilter( SMESH_HypoFilter::HasName( "Propagation" ));
605 bool isPropagationHyp = propagFilter.IsOk( anHyp, _subShape );
607 if ( isPropagationHyp )
609 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
610 TopTools_MapOfShape aMap;
611 for (; exp.More(); exp.Next()) {
612 if (aMap.Add(exp.Current()) &&
613 !_father->GetHypothesis( exp.Current(), propagFilter, true )) {
614 // no more Propagation on the current edge
615 if (!_father->RemovePropagationChain(exp.Current())) {
616 return SMESH_Hypothesis::HYP_UNKNOWN_FATAL;
620 // rebuild propagation chains, because removing one
621 // chain can resolve concurention, existing before
622 isPropagationOk = _father->RebuildPropagationChains();
624 else if (anHyp->GetDim() == 1) // Only 1D hypothesis can be propagated
626 isPropagationOk = _father->RebuildPropagationChains();
629 if ( isPropagationOk ) {
630 if ( isPropagationHyp )
631 return ret; // nothing more to do for "Propagation" hypothesis
633 else if ( ret < SMESH_Hypothesis::HYP_CONCURENT) {
634 ret = SMESH_Hypothesis::HYP_CONCURENT;
636 } // Serve Propagation of 1D hypothesis
639 // ------------------
640 // analyse algo state
641 // ------------------
642 if (!IsApplicableHypotesis( anHyp ))
643 return ret; // not applicable hypotheses do not change algo state
648 // ----------------------------------------------------------------------
655 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
657 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
658 SetAlgoState(HYP_OK);
660 SetAlgoState(MISSING_HYP);
669 case ADD_FATHER_ALGO: { // Algo just added in father
670 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
672 if ( algo == anHyp ) {
673 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
674 SetAlgoState(HYP_OK);
676 SetAlgoState(MISSING_HYP);
680 case REMOVE_FATHER_HYP:
682 case REMOVE_FATHER_ALGO: {
683 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
686 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
687 SetAlgoState(HYP_OK);
689 SetAlgoState(MISSING_HYP);
699 // ----------------------------------------------------------------------
705 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
707 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
708 SetAlgoState(HYP_OK);
709 if (SMESH_Hypothesis::IsStatusFatal( ret ))
710 _meshDS->RemoveHypothesis(_subShape, anHyp);
711 else if (!_father->IsUsedHypothesis( anHyp, this ))
713 _meshDS->RemoveHypothesis(_subShape, anHyp);
714 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
718 case ADD_ALGO: { //already existing algo : on father ?
719 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
721 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
722 SetAlgoState(HYP_OK);
724 SetAlgoState(MISSING_HYP);
729 case REMOVE_ALGO: { // perhaps a father algo applies ?
730 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
731 if (algo == NULL) // no more algo applying on subShape...
733 SetAlgoState(NO_ALGO);
737 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
738 SetAlgoState(HYP_OK);
740 SetAlgoState(MISSING_HYP);
744 case ADD_FATHER_HYP: {
745 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
747 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
748 SetAlgoState(HYP_OK);
750 SetAlgoState(MISSING_HYP);
753 case ADD_FATHER_ALGO: { // new father algo
754 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
756 if ( algo == anHyp ) {
757 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
758 SetAlgoState(HYP_OK);
760 SetAlgoState(MISSING_HYP);
764 case REMOVE_FATHER_HYP: // nothing to do
766 case REMOVE_FATHER_ALGO: {
767 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
768 if (algo == NULL) // no more applying algo on father
770 SetAlgoState(NO_ALGO);
774 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
775 SetAlgoState(HYP_OK);
777 SetAlgoState(MISSING_HYP);
787 // ----------------------------------------------------------------------
793 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
795 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
797 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
798 // ret should be fatal: anHyp was not added
799 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
801 else if (!_father->IsUsedHypothesis( anHyp, this ))
802 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
804 if (SMESH_Hypothesis::IsStatusFatal( ret ))
806 MESSAGE("do not add extra hypothesis");
807 _meshDS->RemoveHypothesis(_subShape, anHyp);
815 case ADD_ALGO: { //already existing algo : on father ?
816 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
817 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
818 // check if algo changes
820 f.Init( SMESH_HypoFilter::IsAlgo() );
821 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
822 f.AndNot( SMESH_HypoFilter::Is( algo ));
823 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
825 string(algo->GetName()) != string(prevAlgo->GetName()) )
829 SetAlgoState(MISSING_HYP);
833 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
835 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
836 SetAlgoState(HYP_OK);
838 SetAlgoState(MISSING_HYP);
842 case REMOVE_ALGO: { // perhaps a father algo applies ?
843 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
844 if (algo == NULL) // no more algo applying on subShape...
846 SetAlgoState(NO_ALGO);
850 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
851 // check if algo remains
852 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
856 SetAlgoState(MISSING_HYP);
860 case ADD_FATHER_HYP: { // new father hypothesis ?
861 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
863 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
865 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
869 SetAlgoState(MISSING_HYP);
872 case ADD_FATHER_ALGO: {
873 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
874 if ( algo == anHyp ) { // a new algo on father
875 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
876 // check if algo changes
878 f.Init( SMESH_HypoFilter::IsAlgo() );
879 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
880 f.AndNot( SMESH_HypoFilter::Is( algo ));
881 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
883 string(algo->GetName()) != string(prevAlgo->GetName()) )
887 SetAlgoState(MISSING_HYP);
891 case REMOVE_FATHER_HYP: {
892 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
894 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
895 // is there the same local hyp or maybe a new father algo applied?
896 if ( !GetSimilarAttached( _subShape, anHyp ) )
900 SetAlgoState(MISSING_HYP);
903 case REMOVE_FATHER_ALGO: {
904 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
905 if (algo == NULL) // no more applying algo on father
907 SetAlgoState(NO_ALGO);
911 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
912 // check if algo changes
913 if ( string(algo->GetName()) != string( anHyp->GetName()) )
917 SetAlgoState(MISSING_HYP);
927 // ----------------------------------------------------------------------
934 if ((_algoState != oldAlgoState) || modifiedHyp)
935 ComputeStateEngine(MODIF_ALGO_STATE);
941 //=======================================================================
942 //function : IsConform
943 //purpose : check if a conform mesh will be produced by the Algo
944 //=======================================================================
946 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
948 // MESSAGE( "SMESH_subMesh::IsConform" );
950 if ( !theAlgo ) return false;
952 // check only algo that doesn't NeedDescretBoundary(): because mesh made
953 // on a sub-shape will be ignored by theAlgo
954 if ( theAlgo->NeedDescretBoundary() )
957 SMESH_Gen* gen =_father->GetGen();
959 // only local algo is to be checked
960 if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
963 // check algo attached to adjacent shapes
965 // loop on one level down sub-meshes
966 TopoDS_Iterator itsub( _subShape );
967 for (; itsub.More(); itsub.Next())
969 // loop on adjacent subShapes
970 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
971 for (; it.More(); it.Next())
973 const TopoDS_Shape& adjacent = it.Value();
974 if ( _subShape.IsSame( adjacent )) continue;
975 if ( adjacent.ShapeType() != _subShape.ShapeType())
978 // check algo attached to smAdjacent
979 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
982 !algo->NeedDescretBoundary() /*&&
983 !gen->IsGlobalHypothesis( algo, *_father )*/)
984 return false; // NOT CONFORM MESH WILL BE PRODUCED
991 //=============================================================================
995 //=============================================================================
997 void SMESH_subMesh::SetAlgoState(int state)
1002 //=============================================================================
1006 //=============================================================================
1007 SMESH_Hypothesis::Hypothesis_Status
1008 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1009 SMESH_Hypothesis * anHyp)
1011 //MESSAGE("SMESH_subMesh::SubMeshesAlgoStateEngine");
1012 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1013 //EAP: a wire (dim==1) should notify edges (dim==1)
1014 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1015 if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1017 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1019 map < int, SMESH_subMesh * >::const_iterator itsub;
1020 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1022 SMESH_subMesh *sm = (*itsub).second;
1023 SMESH_Hypothesis::Hypothesis_Status ret2 =
1024 sm->AlgoStateEngine(event, anHyp);
1032 //=============================================================================
1036 //=============================================================================
1038 void SMESH_subMesh::CleanDependsOn()
1040 //MESSAGE("SMESH_subMesh::CleanDependsOn");
1041 // **** parcourir les ancetres dans l'ordre de dépendance
1043 ComputeStateEngine(CLEAN);
1045 const map < int, SMESH_subMesh * >&dependson = DependsOn();
1046 map < int, SMESH_subMesh * >::const_iterator its;
1047 for (its = dependson.begin(); its != dependson.end(); its++)
1049 SMESH_subMesh *sm = (*its).second;
1050 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 //================================================================================
1108 * \brief Remove nodes and elements bound to submesh
1109 * \param subMesh - submesh containing nodes and elements
1111 //================================================================================
1113 static void cleanSubMesh( SMESH_subMesh * subMesh )
1116 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1117 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1118 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1119 while (ite->more()) {
1120 const SMDS_MeshElement * elt = ite->next();
1121 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1122 //meshDS->RemoveElement(elt);
1123 meshDS->RemoveFreeElement(elt, subMeshDS);
1126 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1127 while (itn->more()) {
1128 const SMDS_MeshNode * node = itn->next();
1129 //MESSAGE( " RM node: "<<node->GetID());
1130 //meshDS->RemoveNode(node);
1131 meshDS->RemoveFreeNode(node, subMeshDS);
1137 //=============================================================================
1141 //=============================================================================
1143 bool SMESH_subMesh::ComputeStateEngine(int event)
1145 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1146 //SCRUTE(_computeState);
1149 int dim = SMESH_Gen::GetShapeDim(_subShape);
1153 if ( IsMeshComputed() )
1154 _computeState = COMPUTE_OK;
1156 _computeState = READY_TO_COMPUTE;
1159 SMESH_Gen *gen = _father->GetGen();
1160 SMESH_Algo *algo = 0;
1162 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1164 switch (_computeState)
1167 // ----------------------------------------------------------------------
1172 case MODIF_HYP: // nothing to do
1174 case MODIF_ALGO_STATE:
1175 if (_algoState == HYP_OK)
1177 _computeState = READY_TO_COMPUTE;
1180 case COMPUTE: // nothing to do
1184 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;
1242 RemoveSubMeshElementsAndNodes();
1244 if (!algo->NeedDescretBoundary() && !algo->OnlyUnaryInput())
1245 ret = ApplyToCollection( algo, GetCollection( gen, algo ) );
1247 ret = algo->Compute((*_father), _subShape);
1249 catch (Standard_Failure) {
1250 MESSAGE( "Exception in algo->Compute() ");
1255 MESSAGE("problem in algo execution: failed to compute");
1256 _computeState = FAILED_TO_COMPUTE;
1257 if (!algo->NeedDescretBoundary())
1258 UpdateSubMeshState( FAILED_TO_COMPUTE );
1261 // Show vertices location of a failed shape
1262 TopTools_IndexedMapOfShape vMap;
1263 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1264 for ( int iv = 1; iv <= vMap.Extent(); ++iv ) {
1265 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1266 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1273 _computeState = COMPUTE_OK;
1274 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1275 if (!algo->NeedDescretBoundary())
1276 UpdateSubMeshState( COMPUTE_OK );
1282 RemoveSubMeshElementsAndNodes();
1283 _computeState = NOT_READY;
1284 algo = gen->GetAlgo((*_father), _subShape);
1287 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1289 _computeState = READY_TO_COMPUTE;
1292 case SUBMESH_COMPUTED: // nothing to do
1294 case SUBMESH_RESTORED:
1295 // check if a mesh is already computed that may
1296 // happen after retrieval from a file
1297 ComputeStateEngine( CHECK_COMPUTE_STATE );
1298 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1300 case MESH_ENTITY_REMOVED:
1302 case CHECK_COMPUTE_STATE:
1303 if ( IsMeshComputed() )
1304 _computeState = COMPUTE_OK;
1312 // ----------------------------------------------------------------------
1318 case MODIF_ALGO_STATE:
1319 ComputeStateEngine( CLEAN );
1320 algo = gen->GetAlgo((*_father), _subShape);
1321 if (algo && !algo->NeedDescretBoundary())
1322 CleanDependsOn(); // clean sub-meshes with event CLEAN
1324 case COMPUTE: // nothing to do
1327 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
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;
1338 case SUBMESH_COMPUTED: // nothing to do
1340 case SUBMESH_RESTORED:
1341 ComputeStateEngine( CHECK_COMPUTE_STATE );
1342 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1344 case MESH_ENTITY_REMOVED:
1345 UpdateDependantsState( CHECK_COMPUTE_STATE );
1346 ComputeStateEngine( CHECK_COMPUTE_STATE );
1347 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1349 case CHECK_COMPUTE_STATE:
1350 if ( !IsMeshComputed() )
1351 if (_algoState == HYP_OK)
1352 _computeState = READY_TO_COMPUTE;
1354 _computeState = NOT_READY;
1362 // ----------------------------------------------------------------------
1364 case FAILED_TO_COMPUTE:
1368 if (_algoState == HYP_OK)
1369 _computeState = READY_TO_COMPUTE;
1371 _computeState = NOT_READY;
1373 case MODIF_ALGO_STATE:
1374 if (_algoState == HYP_OK)
1375 _computeState = READY_TO_COMPUTE;
1377 _computeState = NOT_READY;
1379 case COMPUTE: // nothing to do
1382 CleanDependants(); // submeshes dependent on me should be cleaned as well
1383 RemoveSubMeshElementsAndNodes();
1384 if (_algoState == HYP_OK)
1385 _computeState = READY_TO_COMPUTE;
1387 _computeState = NOT_READY;
1389 case SUBMESH_COMPUTED: // allow retry compute
1390 if (_algoState == HYP_OK)
1391 _computeState = READY_TO_COMPUTE;
1393 _computeState = NOT_READY;
1395 case SUBMESH_RESTORED:
1396 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1398 case MESH_ENTITY_REMOVED:
1400 case CHECK_COMPUTE_STATE:
1401 if ( IsMeshComputed() )
1402 _computeState = COMPUTE_OK;
1404 if (_algoState == HYP_OK)
1405 _computeState = READY_TO_COMPUTE;
1407 _computeState = NOT_READY;
1415 // ----------------------------------------------------------------------
1421 //SCRUTE(_computeState);
1425 //=======================================================================
1426 //function : ApplyToCollection
1427 //purpose : Apply theAlgo to all subshapes in theCollection
1428 //=======================================================================
1430 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1431 const TopoDS_Shape& theCollection)
1433 MESSAGE("SMESH_subMesh::ApplyToCollection");
1434 ASSERT ( !theAlgo->NeedDescretBoundary() );
1439 ret = theAlgo->Compute( *_father, theCollection );
1441 // set _computeState of subshapes
1442 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1443 for ( ; anExplorer.More(); anExplorer.Next() )
1445 const TopoDS_Shape& aSubShape = anExplorer.Current();
1446 SMESH_subMesh* subMesh = _father->GetSubMeshContaining( aSubShape );
1451 subMesh->_computeState = COMPUTE_OK;
1452 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1453 subMesh->UpdateSubMeshState( COMPUTE_OK );
1457 subMesh->_computeState = FAILED_TO_COMPUTE;
1465 //=======================================================================
1466 //function : UpdateSubMeshState
1468 //=======================================================================
1470 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1472 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1473 map<int, SMESH_subMesh*>::const_iterator itsub;
1474 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1476 SMESH_subMesh* sm = (*itsub).second;
1477 sm->_computeState = theState;
1481 //=======================================================================
1482 //function : ComputeSubMeshStateEngine
1484 //=======================================================================
1486 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1488 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1489 map<int, SMESH_subMesh*>::const_iterator itsub;
1490 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1492 SMESH_subMesh* sm = (*itsub).second;
1493 sm->ComputeStateEngine(event);
1497 //=======================================================================
1498 //function : UpdateDependantsState
1500 //=======================================================================
1502 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1504 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1505 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1506 for (; it.More(); it.Next())
1508 const TopoDS_Shape& ancestor = it.Value();
1509 SMESH_subMesh *aSubMesh =
1510 _father->GetSubMeshContaining(ancestor);
1512 aSubMesh->ComputeStateEngine( theEvent );
1516 //=============================================================================
1520 //=============================================================================
1522 void SMESH_subMesh::CleanDependants()
1524 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1526 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1527 for (; it.More(); it.Next())
1529 const TopoDS_Shape& ancestor = it.Value();
1530 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1531 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1532 // will erase mesh on other shapes in a compound
1533 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1534 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1536 aSubMesh->ComputeStateEngine(CLEAN);
1542 //=============================================================================
1546 //=============================================================================
1548 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1550 //SCRUTE(_subShape.ShapeType());
1552 cleanSubMesh( this );
1554 // algo may bind a submesh not to _subShape, eg 3D algo
1555 // sets nodes on SHELL while _subShape may be SOLID
1557 int dim = SMESH_Gen::GetShapeDim( _subShape );
1558 int type = _subShape.ShapeType() + 1;
1559 for ( ; type <= TopAbs_EDGE; type++) {
1560 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1562 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1563 for ( ; exp.More(); exp.Next() )
1564 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1571 //=======================================================================
1572 //function : IsMeshComputed
1573 //purpose : check if _subMeshDS contains mesh elements
1574 //=======================================================================
1576 bool SMESH_subMesh::IsMeshComputed() const
1578 // algo may bind a submesh not to _subShape, eg 3D algo
1579 // sets nodes on SHELL while _subShape may be SOLID
1581 int dim = SMESH_Gen::GetShapeDim( _subShape );
1582 int type = _subShape.ShapeType();
1583 for ( ; type <= TopAbs_VERTEX; type++) {
1584 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1586 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1587 for ( ; exp.More(); exp.Next() )
1589 SMESHDS_SubMesh * subMeshDS = _meshDS->MeshElements( exp.Current() );
1590 if ( subMeshDS != NULL &&
1591 (subMeshDS->GetElements()->more() || subMeshDS->GetNodes()->more())) {
1604 //=======================================================================
1605 //function : GetCollection
1606 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1607 // meshed at once along with _subShape
1608 //=======================================================================
1610 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1612 MESSAGE("SMESH_subMesh::GetCollection");
1613 ASSERT (!theAlgo->NeedDescretBoundary());
1615 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1617 if ( mainShape.IsSame( _subShape ))
1620 const bool ignoreAuxiliaryHyps = false;
1621 list<const SMESHDS_Hypothesis*> aUsedHyp =
1622 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // 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, ignoreAuxiliaryHyps ) == aUsedHyp)
1642 aBuilder.Add( aCompound, S );
1649 //=======================================================================
1650 //function : GetSimilarAttached
1651 //purpose : return a hypothesis attached to theShape.
1652 // If theHyp is provided, similar but not same hypotheses
1653 // is returned; 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 hypoKind;
1662 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1664 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1665 hypoKind.AndNot( hypoKind.Is( theHyp ));
1666 if ( theHyp->IsAuxiliary() )
1667 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1669 hypoKind.AndNot( hypoKind.IsAuxiliary());
1672 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1675 return _father->GetHypothesis( theShape, hypoKind, false );
1678 //=======================================================================
1679 //function : CheckConcurentHypothesis
1680 //purpose : check if there are several applicable hypothesis attached to
1682 //=======================================================================
1684 SMESH_Hypothesis::Hypothesis_Status
1685 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1687 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1689 // is there local hypothesis on me?
1690 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1691 return SMESH_Hypothesis::HYP_OK;
1694 TopoDS_Shape aPrevWithHyp;
1695 const SMESH_Hypothesis* aPrevHyp = 0;
1696 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1697 for (; it.More(); it.Next())
1699 const TopoDS_Shape& ancestor = it.Value();
1700 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1703 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1705 aPrevWithHyp = ancestor;
1708 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1709 return SMESH_Hypothesis::HYP_CONCURENT;
1711 return SMESH_Hypothesis::HYP_OK;
1714 return SMESH_Hypothesis::HYP_OK;