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
29 #include "SMESH_subMesh.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_Mesh.hxx"
32 #include "SMESH_Hypothesis.hxx"
33 #include "SMESH_Algo.hxx"
34 #include "SMESH_HypoFilter.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>
50 #include <BRep_Tool.hxx>
52 #include <TopTools_IndexedMapOfShape.hxx>
57 //=============================================================================
59 * default constructor:
61 //=============================================================================
63 SMESH_subMesh::SMESH_subMesh(int Id, SMESH_Mesh * father, SMESHDS_Mesh * meshDS,
64 const TopoDS_Shape & aSubShape)
66 _subShape = aSubShape;
68 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
71 _dependenceAnalysed = false;
73 if (_subShape.ShapeType() == TopAbs_VERTEX)
76 _computeState = READY_TO_COMPUTE;
81 _computeState = NOT_READY;
85 //=============================================================================
89 //=============================================================================
91 SMESH_subMesh::~SMESH_subMesh()
93 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
97 //=============================================================================
101 //=============================================================================
103 int SMESH_subMesh::GetId() const
105 //MESSAGE("SMESH_subMesh::GetId");
109 //=============================================================================
113 //=============================================================================
115 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
117 //MESSAGE("SMESH_subMesh::GetSubMeshDS");
118 if (_subMeshDS==NULL)
120 //MESSAGE("subMesh pointer still null, trying to get it...");
121 _subMeshDS = _meshDS->MeshElements(_subShape); // may be null ...
122 if (_subMeshDS==NULL)
124 MESSAGE("problem... subMesh still empty");
126 //NRI throw SALOME_Exception(LOCALIZED(subMesh still empty));
132 //=============================================================================
136 //=============================================================================
138 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
140 if ( !GetSubMeshDS() )
141 _meshDS->NewSubMesh( _meshDS->ShapeToIndex( _subShape ) );
143 return GetSubMeshDS();
146 //=============================================================================
150 //=============================================================================
152 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
154 //MESSAGE("SMESH_subMesh::GetFirstToCompute");
155 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
156 SMESH_subMesh *firstToCompute = 0;
158 map < int, SMESH_subMesh * >::const_iterator itsub;
159 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
161 SMESH_subMesh *sm = (*itsub).second;
162 // SCRUTE(sm->GetId());
163 // SCRUTE(sm->GetComputeState());
164 bool readyToCompute = (sm->GetComputeState() == READY_TO_COMPUTE);
168 //SCRUTE(sm->GetId());
174 return firstToCompute; // a subMesh of this
176 if (_computeState == READY_TO_COMPUTE)
180 return 0; // nothing to compute
183 //=============================================================================
187 //=============================================================================
189 bool SMESH_subMesh::SubMeshesComputed()
191 //MESSAGE("SMESH_subMesh::SubMeshesComputed");
192 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
194 bool subMeshesComputed = true;
195 map < int, SMESH_subMesh * >::const_iterator itsub;
196 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
198 SMESH_subMesh *sm = (*itsub).second;
199 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
201 // There are some tricks with compute states, e.g. Penta_3D leaves
202 // one face with READY_TO_COMPUTE state in order to be able to
203 // recompute 3D when a locale triangle hypo changes (see PAL7428).
204 // So we check if mesh is really present
205 //bool computeOk = (sm->GetComputeState() == COMPUTE_OK);
206 bool computeOk = ( ds && ( ds->GetNodes()->more() || ds->GetElements()->more() ));
209 const TopoDS_Shape & ss = sm->GetSubShape();
210 int type = ss.ShapeType();
212 subMeshesComputed = false;
216 case TopAbs_COMPOUND:
218 MESSAGE("The not computed sub mesh is a COMPOUND");
221 case TopAbs_COMPSOLID:
223 MESSAGE("The not computed sub mesh is a COMPSOLID");
228 MESSAGE("The not computed sub mesh is a SHEL");
233 MESSAGE("The not computed sub mesh is a WIRE");
238 MESSAGE("The not computed sub mesh is a SOLID");
243 MESSAGE("The not computed sub mesh is a FACE");
248 MESSAGE("The not computed sub mesh is a EDGE");
253 MESSAGE("The not computed sub mesh is of unknown type");
261 return subMeshesComputed;
264 //=============================================================================
268 //=============================================================================
270 bool SMESH_subMesh::SubMeshesReady()
272 MESSAGE("SMESH_subMesh::SubMeshesReady");
273 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
275 bool subMeshesReady = true;
276 map < int, SMESH_subMesh * >::const_iterator itsub;
277 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
279 SMESH_subMesh *sm = (*itsub).second;
280 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK)
281 || (sm->GetComputeState() == READY_TO_COMPUTE));
284 subMeshesReady = false;
289 return subMeshesReady;
292 //=============================================================================
294 * Construct dependence on first level subMeshes. complex shapes (compsolid,
295 * shell, wire) are not analysed the same way as simple shapes (solid, face,
297 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
298 * with possible multiples occurences. Multiples occurences corresponds to
299 * internal frontiers within shapes of the collection and must not be keeped.
300 * See FinalizeDependence.
302 //=============================================================================
304 const map < int, SMESH_subMesh * >&SMESH_subMesh::DependsOn()
306 if (_dependenceAnalysed)
309 //MESSAGE("SMESH_subMesh::DependsOn");
311 int type = _subShape.ShapeType();
315 case TopAbs_COMPOUND:
317 //MESSAGE("compound");
318 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
321 InsertDependence(exp.Current());
323 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
326 InsertDependence(exp.Current()); //only shell not in solid
328 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
331 InsertDependence(exp.Current());
333 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
336 InsertDependence(exp.Current());
340 case TopAbs_COMPSOLID:
342 //MESSAGE("compsolid");
343 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
346 InsertDependence(exp.Current());
353 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
356 InsertDependence(exp.Current());
363 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
366 InsertDependence(exp.Current());
373 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
376 InsertDependence(exp.Current());
383 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
386 InsertDependence(exp.Current());
393 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
396 InsertDependence(exp.Current());
409 _dependenceAnalysed = true;
413 //=============================================================================
415 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
417 //=============================================================================
419 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
421 //MESSAGE("SMESH_subMesh::InsertDependence");
422 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
423 int type = aSubShape.ShapeType();
424 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
425 int cle = aSubMesh->GetId();
426 cle += 10000000 * ordType; // sort map by ordType then index
427 if (_mapDepend.find(cle) == _mapDepend.end())
429 _mapDepend[cle] = aSubMesh;
430 const map < int, SMESH_subMesh * >&subMap = aSubMesh->DependsOn();
431 map < int, SMESH_subMesh * >::const_iterator im;
432 for (im = subMap.begin(); im != subMap.end(); im++)
434 int clesub = (*im).first;
435 SMESH_subMesh *sm = (*im).second;
436 if (_mapDepend.find(clesub) == _mapDepend.end())
437 _mapDepend[clesub] = sm;
443 //=============================================================================
447 //=============================================================================
449 const TopoDS_Shape & SMESH_subMesh::GetSubShape()
451 //MESSAGE("SMESH_subMesh::GetSubShape");
456 //=======================================================================
457 //function : CanAddHypothesis
458 //purpose : return true if theHypothesis can be attached to me:
459 // its dimention is checked
460 //=======================================================================
462 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
464 int aHypDim = theHypothesis->GetDim();
465 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
466 if ( aHypDim <= aShapeDim )
468 // if ( aHypDim < aShapeDim )
469 // return ( _father->IsMainShape( _subShape ));
474 //=======================================================================
475 //function : IsApplicableHypotesis
477 //=======================================================================
479 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
480 const TopAbs_ShapeEnum theShapeType)
482 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
484 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
488 switch ( theShapeType ) {
489 case TopAbs_EDGE: aShapeDim = 1; break;
490 case TopAbs_FACE: aShapeDim = 2; break;
491 case TopAbs_SHELL:aShapeDim = 3; break;
492 case TopAbs_SOLID:aShapeDim = 3; break;
493 // case TopAbs_VERTEX:
495 // case TopAbs_COMPSOLID:
496 // case TopAbs_COMPOUND:
497 default: return false;
500 return ( theHypothesis->GetDim() == aShapeDim );
503 //=============================================================================
507 //=============================================================================
509 SMESH_Hypothesis::Hypothesis_Status
510 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
512 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
513 //SCRUTE(_algoState);
516 // **** les retour des evenement shape sont significatifs
517 // (add ou remove fait ou non)
518 // le retour des evenement father n'indiquent pas que add ou remove fait
520 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
522 int dim = SMESH_Gen::GetShapeDim(_subShape);
527 if (event == ADD_HYP || event == ADD_ALGO)
528 return SMESH_Hypothesis::HYP_BAD_DIM; // do not allow to assign any hyp
530 return SMESH_Hypothesis::HYP_OK;
533 SMESH_Gen* gen =_father->GetGen();
535 int oldAlgoState = _algoState;
536 bool modifiedHyp = false; // if set to true, force event MODIF_ALGO_STATE
537 // in ComputeStateEngine
539 // ----------------------
540 // check mesh conformity
541 // ----------------------
542 if (event == ADD_ALGO)
544 if (IsApplicableHypotesis( anHyp ) &&
545 !_father->IsNotConformAllowed() &&
546 !IsConform( static_cast< SMESH_Algo* >( anHyp )))
547 return SMESH_Hypothesis::HYP_NOTCONFORM;
550 // ----------------------------------
551 // add a hypothesis to DS if possible
552 // ----------------------------------
553 if (event == ADD_HYP || event == ADD_ALGO)
555 if ( ! CanAddHypothesis( anHyp ))
556 return SMESH_Hypothesis::HYP_BAD_DIM;
558 if ( GetSimilarAttached( _subShape, anHyp ) )
559 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
561 if ( !_meshDS->AddHypothesis(_subShape, anHyp))
562 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
564 // Serve Propagation of 1D hypothesis
565 if (event == ADD_HYP) {
566 bool isPropagationOk = true;
567 string hypName = anHyp->GetName();
569 if (hypName == "Propagation") {
570 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
571 TopTools_MapOfShape aMap;
572 for (; exp.More(); exp.Next()) {
573 if (aMap.Add(exp.Current())) {
574 if (!_father->BuildPropagationChain(exp.Current())) {
575 isPropagationOk = false;
580 else if (anHyp->GetDim() == 1) { // Only 1D hypothesis can be propagated
581 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
582 TopTools_MapOfShape aMap;
583 for (; exp.More(); exp.Next()) {
584 if (aMap.Add(exp.Current())) {
585 TopoDS_Shape aMainEdge;
586 if (_father->IsPropagatedHypothesis(exp.Current(), aMainEdge)) {
587 isPropagationOk = _father->RebuildPropagationChains();
588 } else if (_father->IsPropagationHypothesis(exp.Current())) {
589 isPropagationOk = _father->BuildPropagationChain(exp.Current());
597 if (!isPropagationOk && ret < SMESH_Hypothesis::HYP_CONCURENT) {
598 ret = SMESH_Hypothesis::HYP_CONCURENT;
600 } // Serve Propagation of 1D hypothesis
603 // --------------------------
604 // remove a hypothesis from DS
605 // --------------------------
606 if (event == REMOVE_HYP || event == REMOVE_ALGO)
608 if (!_meshDS->RemoveHypothesis(_subShape, anHyp))
609 return SMESH_Hypothesis::HYP_OK; // nothing changes
611 // Serve Propagation of 1D hypothesis
612 if (event == REMOVE_HYP)
614 bool isPropagationOk = true;
615 SMESH_HypoFilter propagFilter( SMESH_HypoFilter::HasName( "Propagation" ));
616 if ( propagFilter.IsOk( anHyp, _subShape ))
618 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
619 TopTools_MapOfShape aMap;
620 for (; exp.More(); exp.Next()) {
621 if (aMap.Add(exp.Current()) &&
622 !_father->GetHypothesis( exp.Current(), propagFilter, true )) {
623 // no more Propagation on the current edge
624 if (!_father->RemovePropagationChain(exp.Current())) {
625 return SMESH_Hypothesis::HYP_UNKNOWN_FATAL;
629 // rebuild propagation chains, because removing one
630 // chain can resolve concurention, existing before
631 isPropagationOk = _father->RebuildPropagationChains();
633 else if (anHyp->GetDim() == 1) // Only 1D hypothesis can be propagated
635 isPropagationOk = _father->RebuildPropagationChains();
638 if (!isPropagationOk && ret < SMESH_Hypothesis::HYP_CONCURENT) {
639 ret = SMESH_Hypothesis::HYP_CONCURENT;
641 } // Serve Propagation of 1D hypothesis
644 // ------------------
645 // analyse algo state
646 // ------------------
647 if (!IsApplicableHypotesis( anHyp ))
648 return ret; // not applicable hypotheses do not change algo state
653 // ----------------------------------------------------------------------
660 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
662 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
663 SetAlgoState(HYP_OK);
665 SetAlgoState(MISSING_HYP);
674 case ADD_FATHER_ALGO: { // Algo just added in father
675 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
677 if ( algo == anHyp ) {
678 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
679 SetAlgoState(HYP_OK);
681 SetAlgoState(MISSING_HYP);
685 case REMOVE_FATHER_HYP:
687 case REMOVE_FATHER_ALGO: {
688 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
691 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
692 SetAlgoState(HYP_OK);
694 SetAlgoState(MISSING_HYP);
704 // ----------------------------------------------------------------------
710 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
712 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
713 SetAlgoState(HYP_OK);
714 if (SMESH_Hypothesis::IsStatusFatal( ret ))
715 _meshDS->RemoveHypothesis(_subShape, anHyp);
716 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
718 _meshDS->RemoveHypothesis(_subShape, anHyp);
719 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
723 case ADD_ALGO: { //already existing algo : on father ?
724 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
726 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
727 SetAlgoState(HYP_OK);
729 SetAlgoState(MISSING_HYP);
734 case REMOVE_ALGO: { // perhaps a father algo applies ?
735 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
736 if (algo == NULL) // no more algo applying on subShape...
738 SetAlgoState(NO_ALGO);
742 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
743 SetAlgoState(HYP_OK);
745 SetAlgoState(MISSING_HYP);
749 case ADD_FATHER_HYP: {
750 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
752 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
753 SetAlgoState(HYP_OK);
755 SetAlgoState(MISSING_HYP);
758 case ADD_FATHER_ALGO: { // new father algo
759 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
761 if ( algo == anHyp ) {
762 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
763 SetAlgoState(HYP_OK);
765 SetAlgoState(MISSING_HYP);
769 case REMOVE_FATHER_HYP: // nothing to do
771 case REMOVE_FATHER_ALGO: {
772 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
773 if (algo == NULL) // no more applying algo on father
775 SetAlgoState(NO_ALGO);
779 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
780 SetAlgoState(HYP_OK);
782 SetAlgoState(MISSING_HYP);
792 // ----------------------------------------------------------------------
798 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
800 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
802 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
803 // ret should be fatal: anHyp was not added
804 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
806 else if (!_father->IsUsedHypothesis( anHyp, _subShape ))
807 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
809 if (SMESH_Hypothesis::IsStatusFatal( ret ))
811 MESSAGE("do not add extra hypothesis");
812 _meshDS->RemoveHypothesis(_subShape, anHyp);
820 case ADD_ALGO: { //already existing algo : on father ?
821 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
822 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
823 // check if algo changes
825 f.Init( SMESH_HypoFilter::IsAlgo() );
826 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
827 f.AndNot( SMESH_HypoFilter::Is( algo ));
828 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
830 string(algo->GetName()) != string(prevAlgo->GetName()) )
834 SetAlgoState(MISSING_HYP);
838 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
840 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
841 SetAlgoState(HYP_OK);
843 SetAlgoState(MISSING_HYP);
847 case REMOVE_ALGO: { // perhaps a father algo applies ?
848 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
849 if (algo == NULL) // no more algo applying on subShape...
851 SetAlgoState(NO_ALGO);
855 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
856 // check if algo remains
857 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
861 SetAlgoState(MISSING_HYP);
865 case ADD_FATHER_HYP: { // new father hypothesis ?
866 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
868 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
870 if (_father->IsUsedHypothesis( anHyp, _subShape )) // new Hyp
874 SetAlgoState(MISSING_HYP);
877 case ADD_FATHER_ALGO: {
878 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
879 if ( algo == anHyp ) { // a new algo on father
880 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
881 // check if algo changes
883 f.Init( SMESH_HypoFilter::IsAlgo() );
884 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
885 f.AndNot( SMESH_HypoFilter::Is( algo ));
886 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
888 string(algo->GetName()) != string(prevAlgo->GetName()) )
892 SetAlgoState(MISSING_HYP);
896 case REMOVE_FATHER_HYP: {
897 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
899 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
900 // is there the same local hyp or maybe a new father algo applied?
901 if ( !GetSimilarAttached( _subShape, anHyp ) )
905 SetAlgoState(MISSING_HYP);
908 case REMOVE_FATHER_ALGO: {
909 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
910 if (algo == NULL) // no more applying algo on father
912 SetAlgoState(NO_ALGO);
916 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
917 // check if algo changes
918 if ( string(algo->GetName()) != string( anHyp->GetName()) )
922 SetAlgoState(MISSING_HYP);
932 // ----------------------------------------------------------------------
939 if ((_algoState != oldAlgoState) || modifiedHyp)
940 ComputeStateEngine(MODIF_ALGO_STATE);
946 //=======================================================================
947 //function : IsConform
948 //purpose : check if a conform mesh will be produced by the Algo
949 //=======================================================================
951 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
953 // MESSAGE( "SMESH_subMesh::IsConform" );
955 if ( !theAlgo ) return false;
957 // check only algo that doesn't NeedDescretBoundary(): because mesh made
958 // on a sub-shape will be ignored by theAlgo
959 if ( theAlgo->NeedDescretBoundary() )
962 SMESH_Gen* gen =_father->GetGen();
964 // only local algo is to be checked
965 if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
968 // check algo attached to adjacent shapes
970 // loop on one level down sub-meshes
971 TopoDS_Iterator itsub( _subShape );
972 for (; itsub.More(); itsub.Next())
974 // loop on adjacent subShapes
975 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
976 for (; it.More(); it.Next())
978 const TopoDS_Shape& adjacent = it.Value();
979 if ( _subShape.IsSame( adjacent )) continue;
980 if ( adjacent.ShapeType() != _subShape.ShapeType())
983 // check algo attached to smAdjacent
984 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
987 !algo->NeedDescretBoundary() /*&&
988 !gen->IsGlobalHypothesis( algo, *_father )*/)
989 return false; // NOT CONFORM MESH WILL BE PRODUCED
996 //=============================================================================
1000 //=============================================================================
1002 void SMESH_subMesh::SetAlgoState(int state)
1004 // if (state != _oldAlgoState)
1005 // int retc = ComputeStateEngine(MODIF_ALGO_STATE);
1009 //=============================================================================
1013 //=============================================================================
1014 SMESH_Hypothesis::Hypothesis_Status
1015 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1016 SMESH_Hypothesis * anHyp)
1018 //MESSAGE("SMESH_subMesh::SubMeshesAlgoStateEngine");
1019 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1020 //EAP: a wire (dim==1) should notify edges (dim==1)
1021 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1022 if (/*EAP:dim > 1*/ _subShape.ShapeType() < TopAbs_EDGE )
1024 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1026 map < int, SMESH_subMesh * >::const_iterator itsub;
1027 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1029 SMESH_subMesh *sm = (*itsub).second;
1030 SMESH_Hypothesis::Hypothesis_Status ret2 =
1031 sm->AlgoStateEngine(event, anHyp);
1039 //=============================================================================
1043 //=============================================================================
1045 void SMESH_subMesh::CleanDependsOn()
1047 MESSAGE("SMESH_subMesh::CleanDependsOn");
1048 // **** parcourir les ancetres dans l'ordre de dépendance
1050 ComputeStateEngine(CLEAN);
1052 const map < int, SMESH_subMesh * >&dependson = DependsOn();
1053 map < int, SMESH_subMesh * >::const_iterator its;
1054 for (its = dependson.begin(); its != dependson.end(); its++)
1056 SMESH_subMesh *sm = (*its).second;
1057 sm->ComputeStateEngine(CLEAN);
1061 //=============================================================================
1065 //=============================================================================
1067 void SMESH_subMesh::DumpAlgoState(bool isMain)
1069 int dim = SMESH_Gen::GetShapeDim(_subShape);
1070 // if (dim < 1) return;
1073 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1075 map < int, SMESH_subMesh * >::const_iterator itsub;
1076 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1078 SMESH_subMesh *sm = (*itsub).second;
1079 sm->DumpAlgoState(false);
1082 int type = _subShape.ShapeType();
1083 MESSAGE("dim = " << dim << " type of shape " << type);
1087 MESSAGE(" AlgoState = NO_ALGO");
1090 MESSAGE(" AlgoState = MISSING_HYP");
1093 MESSAGE(" AlgoState = HYP_OK");
1096 switch (_computeState)
1099 MESSAGE(" ComputeState = NOT_READY");
1101 case READY_TO_COMPUTE:
1102 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1105 MESSAGE(" ComputeState = COMPUTE_OK");
1107 case FAILED_TO_COMPUTE:
1108 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1113 //=============================================================================
1117 //=============================================================================
1119 static void removeSubMesh( SMESHDS_Mesh * meshDS, const TopoDS_Shape& subShape)
1121 SMESHDS_SubMesh * subMeshDS = meshDS->MeshElements(subShape);
1122 if (subMeshDS!=NULL)
1124 SMDS_ElemIteratorPtr ite=subMeshDS->GetElements();
1127 const SMDS_MeshElement * elt = ite->next();
1128 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1129 meshDS->RemoveElement(elt);
1132 SMDS_NodeIteratorPtr itn=subMeshDS->GetNodes();
1135 const SMDS_MeshNode * node = itn->next();
1136 //MESSAGE( " RM node: "<<node->GetID());
1137 meshDS->RemoveNode(node);
1142 //=============================================================================
1146 //=============================================================================
1148 bool SMESH_subMesh::ComputeStateEngine(int event)
1150 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1151 //SCRUTE(_computeState);
1154 int dim = SMESH_Gen::GetShapeDim(_subShape);
1158 if ( IsMeshComputed() )
1159 _computeState = COMPUTE_OK;
1161 _computeState = READY_TO_COMPUTE;
1164 SMESH_Gen *gen = _father->GetGen();
1165 SMESH_Algo *algo = 0;
1167 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1169 switch (_computeState)
1172 // ----------------------------------------------------------------------
1177 case MODIF_HYP: // nothing to do
1179 case MODIF_ALGO_STATE:
1180 if (_algoState == HYP_OK)
1182 _computeState = READY_TO_COMPUTE;
1185 case COMPUTE: // nothing to do
1188 RemoveSubMeshElementsAndNodes();
1193 case SUBMESH_COMPUTED: // nothing to do
1195 case SUBMESH_RESTORED:
1196 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1198 case MESH_ENTITY_REMOVED:
1200 case CHECK_COMPUTE_STATE:
1201 if ( IsMeshComputed() )
1202 _computeState = COMPUTE_OK;
1210 // ----------------------------------------------------------------------
1212 case READY_TO_COMPUTE:
1215 case MODIF_HYP: // nothing to do
1217 case MODIF_ALGO_STATE:
1218 _computeState = NOT_READY;
1219 algo = gen->GetAlgo((*_father), _subShape);
1222 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1224 _computeState = READY_TO_COMPUTE;
1229 algo = gen->GetAlgo((*_father), _subShape);
1231 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1234 MESSAGE("***** verify compute state *****");
1235 _computeState = NOT_READY;
1238 // check submeshes needed
1239 if (algo->NeedDescretBoundary())
1240 ret = SubMeshesComputed();
1243 MESSAGE("Some SubMeshes not computed");
1244 _computeState = FAILED_TO_COMPUTE;
1249 //RemoveSubMeshElementsAndNodes();
1250 //removeSubMesh( _meshDS, _subShape );
1251 if (!algo->NeedDescretBoundary() && !algo->OnlyUnaryInput())
1252 ret = ApplyToCollection( algo, GetCollection( gen, algo ) );
1254 ret = algo->Compute((*_father), _subShape);
1258 MESSAGE("problem in algo execution: failed to compute");
1259 _computeState = FAILED_TO_COMPUTE;
1260 if (!algo->NeedDescretBoundary())
1261 UpdateSubMeshState( FAILED_TO_COMPUTE );
1264 // Show vertices location of a failed shape
1265 TopTools_IndexedMapOfShape vMap;
1266 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1267 for ( int iv = 1; iv <= vMap.Extent(); ++iv ) {
1268 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1269 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1276 _computeState = COMPUTE_OK;
1277 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1278 if (!algo->NeedDescretBoundary())
1279 UpdateSubMeshState( COMPUTE_OK );
1284 RemoveSubMeshElementsAndNodes();
1285 _computeState = NOT_READY;
1286 algo = gen->GetAlgo((*_father), _subShape);
1289 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1291 _computeState = READY_TO_COMPUTE;
1297 case SUBMESH_COMPUTED: // nothing to do
1299 case SUBMESH_RESTORED:
1300 // check if a mesh is already computed that may
1301 // happen after retrieval from a file
1302 ComputeStateEngine( CHECK_COMPUTE_STATE );
1303 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1305 case MESH_ENTITY_REMOVED:
1307 case CHECK_COMPUTE_STATE:
1308 if ( IsMeshComputed() )
1309 _computeState = COMPUTE_OK;
1317 // ----------------------------------------------------------------------
1323 CleanDependants(); // recursive recall with event CLEANDEP
1324 algo = gen->GetAlgo((*_father), _subShape);
1325 if (algo && !algo->NeedDescretBoundary())
1326 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1328 case MODIF_ALGO_STATE:
1329 CleanDependants(); // recursive recall with event CLEANDEP
1330 algo = gen->GetAlgo((*_father), _subShape);
1331 if (algo && !algo->NeedDescretBoundary())
1332 CleanDependsOn(); // remove sub-mesh with event CLEANDEP
1334 case COMPUTE: // nothing to do
1337 RemoveSubMeshElementsAndNodes();
1338 _computeState = NOT_READY;
1339 algo = gen->GetAlgo((*_father), _subShape);
1342 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1344 _computeState = READY_TO_COMPUTE;
1348 CleanDependants(); // recursive recall with event CLEANDEP
1350 case SUBMESH_COMPUTED: // nothing to do
1352 case SUBMESH_RESTORED:
1353 ComputeStateEngine( CHECK_COMPUTE_STATE );
1354 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1356 case MESH_ENTITY_REMOVED:
1357 UpdateDependantsState( CHECK_COMPUTE_STATE );
1358 ComputeStateEngine( CHECK_COMPUTE_STATE );
1359 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1361 case CHECK_COMPUTE_STATE:
1362 if ( !IsMeshComputed() )
1363 if (_algoState == HYP_OK)
1364 _computeState = READY_TO_COMPUTE;
1366 _computeState = NOT_READY;
1374 // ----------------------------------------------------------------------
1376 case FAILED_TO_COMPUTE:
1380 if (_algoState == HYP_OK)
1381 _computeState = READY_TO_COMPUTE;
1383 _computeState = NOT_READY;
1385 case MODIF_ALGO_STATE:
1386 if (_algoState == HYP_OK)
1387 _computeState = READY_TO_COMPUTE;
1389 _computeState = NOT_READY;
1391 case COMPUTE: // nothing to do
1394 RemoveSubMeshElementsAndNodes();
1395 if (_algoState == HYP_OK)
1396 _computeState = READY_TO_COMPUTE;
1398 _computeState = NOT_READY;
1403 case SUBMESH_COMPUTED: // allow retry compute
1404 if (_algoState == HYP_OK)
1405 _computeState = READY_TO_COMPUTE;
1407 _computeState = NOT_READY;
1409 case SUBMESH_RESTORED:
1410 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1412 case MESH_ENTITY_REMOVED:
1414 case CHECK_COMPUTE_STATE:
1415 if ( IsMeshComputed() )
1416 _computeState = COMPUTE_OK;
1418 if (_algoState == HYP_OK)
1419 _computeState = READY_TO_COMPUTE;
1421 _computeState = NOT_READY;
1429 // ----------------------------------------------------------------------
1435 //SCRUTE(_computeState);
1439 //=======================================================================
1440 //function : ApplyToCollection
1441 //purpose : Apply theAlgo to all subshapes in theCollection
1442 //=======================================================================
1444 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1445 const TopoDS_Shape& theCollection)
1447 MESSAGE("SMESH_subMesh::ApplyToCollection");
1448 ASSERT ( !theAlgo->NeedDescretBoundary() );
1453 ret = theAlgo->Compute( *_father, theCollection );
1455 // set _computeState of subshapes
1456 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1457 for ( ; anExplorer.More(); anExplorer.Next() )
1459 const TopoDS_Shape& aSubShape = anExplorer.Current();
1460 SMESH_subMesh* subMesh = _father->GetSubMeshContaining( aSubShape );
1465 subMesh->_computeState = COMPUTE_OK;
1466 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1467 subMesh->UpdateSubMeshState( COMPUTE_OK );
1471 subMesh->_computeState = FAILED_TO_COMPUTE;
1479 //=======================================================================
1480 //function : UpdateSubMeshState
1482 //=======================================================================
1484 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1486 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1487 map<int, SMESH_subMesh*>::const_iterator itsub;
1488 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1490 SMESH_subMesh* sm = (*itsub).second;
1491 sm->_computeState = theState;
1495 //=======================================================================
1496 //function : ComputeSubMeshStateEngine
1498 //=======================================================================
1500 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1502 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1503 map<int, SMESH_subMesh*>::const_iterator itsub;
1504 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1506 SMESH_subMesh* sm = (*itsub).second;
1507 sm->ComputeStateEngine(event);
1511 //=======================================================================
1512 //function : UpdateDependantsState
1514 //=======================================================================
1516 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1518 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1519 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1520 for (; it.More(); it.Next())
1522 const TopoDS_Shape& ancestor = it.Value();
1523 SMESH_subMesh *aSubMesh =
1524 _father->GetSubMeshContaining(ancestor);
1526 aSubMesh->ComputeStateEngine( theEvent );
1530 //=============================================================================
1534 //=============================================================================
1536 void SMESH_subMesh::CleanDependants()
1538 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1539 for (; it.More(); it.Next())
1541 const TopoDS_Shape& ancestor = it.Value();
1542 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEANDEP)
1543 // will erase mesh on other shapes in a compound
1544 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1545 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1547 aSubMesh->ComputeStateEngine(CLEANDEP);
1550 ComputeStateEngine(CLEAN);
1553 //=============================================================================
1557 //=============================================================================
1559 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1561 //SCRUTE(_subShape.ShapeType());
1563 removeSubMesh( _meshDS, _subShape );
1565 // algo may bind a submesh not to _subShape, eg 3D algo
1566 // sets nodes on SHELL while _subShape may be SOLID
1568 int dim = SMESH_Gen::GetShapeDim( _subShape );
1569 int type = _subShape.ShapeType() + 1;
1570 for ( ; type <= TopAbs_EDGE; type++)
1571 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1573 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1574 for ( ; exp.More(); exp.Next() )
1575 removeSubMesh( _meshDS, exp.Current() );
1581 //=======================================================================
1582 //function : IsMeshComputed
1583 //purpose : check if _subMeshDS contains mesh elements
1584 //=======================================================================
1586 bool SMESH_subMesh::IsMeshComputed() const
1588 // algo may bind a submesh not to _subShape, eg 3D algo
1589 // sets nodes on SHELL while _subShape may be SOLID
1591 int dim = SMESH_Gen::GetShapeDim( _subShape );
1592 int type = _subShape.ShapeType();
1593 for ( ; type <= TopAbs_VERTEX; type++) {
1594 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1596 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1597 for ( ; exp.More(); exp.Next() )
1599 SMESHDS_SubMesh * subMeshDS = _meshDS->MeshElements( exp.Current() );
1600 if ( subMeshDS != NULL &&
1601 (subMeshDS->GetElements()->more() || subMeshDS->GetNodes()->more())) {
1614 //=======================================================================
1615 //function : GetCollection
1616 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1617 // meshed at once along with _subShape
1618 //=======================================================================
1620 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1622 MESSAGE("SMESH_subMesh::GetCollection");
1623 ASSERT (!theAlgo->NeedDescretBoundary());
1625 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1627 if ( mainShape.IsSame( _subShape ))
1630 list<const SMESHDS_Hypothesis*> aUsedHyp =
1631 theAlgo->GetUsedHypothesis( *_father, _subShape ); // copy
1633 // put in a compound all shapes with the same hypothesis assigned
1634 // and a good ComputState
1636 TopoDS_Compound aCompound;
1637 BRep_Builder aBuilder;
1638 aBuilder.MakeCompound( aCompound );
1640 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1641 for ( ; anExplorer.More(); anExplorer.Next() )
1643 const TopoDS_Shape& S = anExplorer.Current();
1644 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1645 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1647 if (subMesh->GetComputeState() == READY_TO_COMPUTE &&
1648 anAlgo == theAlgo &&
1649 anAlgo->GetUsedHypothesis( *_father, S ) == aUsedHyp)
1651 aBuilder.Add( aCompound, S );
1658 //=======================================================================
1659 //function : GetSimilarAttached
1660 //purpose : return nb of hypotheses attached to theShape.
1661 // If theHyp is provided, similar but not same hypotheses
1662 // are countered; else only applicable ones having theHypType
1664 //=======================================================================
1666 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1667 const SMESH_Hypothesis * theHyp,
1668 const int theHypType)
1670 SMESH_HypoFilter filter;
1671 filter.Init( SMESH_HypoFilter::HasType( theHyp ? theHyp->GetType() : theHypType ));
1673 filter.And( SMESH_HypoFilter::HasDim( theHyp->GetDim() ));
1674 filter.AndNot( SMESH_HypoFilter::Is( theHyp ));
1677 filter.And( SMESH_HypoFilter::IsApplicableTo( theShape ));
1679 return _father->GetHypothesis( theShape, filter, false );
1682 //=======================================================================
1683 //function : CheckConcurentHypothesis
1684 //purpose : check if there are several applicable hypothesis attached to
1686 //=======================================================================
1688 SMESH_Hypothesis::Hypothesis_Status
1689 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1691 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1693 // is there local hypothesis on me?
1694 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1695 return SMESH_Hypothesis::HYP_OK;
1698 TopoDS_Shape aPrevWithHyp;
1699 const SMESH_Hypothesis* aPrevHyp = 0;
1700 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1701 for (; it.More(); it.Next())
1703 const TopoDS_Shape& ancestor = it.Value();
1704 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1707 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1709 aPrevWithHyp = ancestor;
1712 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1713 return SMESH_Hypothesis::HYP_CONCURENT;
1715 return SMESH_Hypothesis::HYP_OK;
1718 return SMESH_Hypothesis::HYP_OK;