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 int myDim = SMESH_Gen::GetShapeDim( _subShape );
190 int dimToCheck = myDim - 1;
191 bool subMeshesComputed = true;
192 map < int, SMESH_subMesh * >::const_iterator itsub;
193 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
195 SMESH_subMesh *sm = (*itsub).second;
196 const TopoDS_Shape & ss = sm->GetSubShape();
197 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
198 // in checking of existence of edges if the algo needs only faces. Moreover,
199 // degenerated edges may have no submesh, as after computing NETGEN_2D.
200 int dim = SMESH_Gen::GetShapeDim( ss );
201 if (dim < dimToCheck)
203 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
205 // There are some tricks with compute states, e.g. Penta_3D leaves
206 // one face with READY_TO_COMPUTE state in order to be able to
207 // recompute 3D when a locale triangle hypo changes (see PAL7428).
208 // So we check if mesh is really present
209 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
210 (ds && ( ds->GetNodes()->more() || ds->GetElements()->more() )));
213 int type = ss.ShapeType();
215 subMeshesComputed = false;
219 case TopAbs_COMPOUND:
221 MESSAGE("The not computed sub mesh is a COMPOUND");
224 case TopAbs_COMPSOLID:
226 MESSAGE("The not computed sub mesh is a COMPSOLID");
231 MESSAGE("The not computed sub mesh is a SHEL");
236 MESSAGE("The not computed sub mesh is a WIRE");
241 MESSAGE("The not computed sub mesh is a SOLID");
246 MESSAGE("The not computed sub mesh is a FACE");
251 MESSAGE("The not computed sub mesh is a EDGE");
256 MESSAGE("The not computed sub mesh is of unknown type");
264 return subMeshesComputed;
267 //=============================================================================
271 //=============================================================================
273 bool SMESH_subMesh::SubMeshesReady()
275 MESSAGE("SMESH_subMesh::SubMeshesReady");
276 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
278 bool subMeshesReady = true;
279 map < int, SMESH_subMesh * >::const_iterator itsub;
280 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
282 SMESH_subMesh *sm = (*itsub).second;
283 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK)
284 || (sm->GetComputeState() == READY_TO_COMPUTE));
287 subMeshesReady = false;
292 return subMeshesReady;
295 //=============================================================================
297 * Construct dependence on first level subMeshes. complex shapes (compsolid,
298 * shell, wire) are not analysed the same way as simple shapes (solid, face,
300 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
301 * with possible multiples occurences. Multiples occurences corresponds to
302 * internal frontiers within shapes of the collection and must not be keeped.
303 * See FinalizeDependence.
305 //=============================================================================
307 const map < int, SMESH_subMesh * >&SMESH_subMesh::DependsOn()
309 if (_dependenceAnalysed)
312 //MESSAGE("SMESH_subMesh::DependsOn");
314 int type = _subShape.ShapeType();
318 case TopAbs_COMPOUND:
320 //MESSAGE("compound");
321 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
324 InsertDependence(exp.Current());
326 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
329 InsertDependence(exp.Current()); //only shell not in solid
331 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
334 InsertDependence(exp.Current());
336 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
339 InsertDependence(exp.Current());
343 case TopAbs_COMPSOLID:
345 //MESSAGE("compsolid");
346 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
349 InsertDependence(exp.Current());
356 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
359 InsertDependence(exp.Current());
366 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
369 InsertDependence(exp.Current());
376 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
379 InsertDependence(exp.Current());
386 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
389 InsertDependence(exp.Current());
396 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
399 InsertDependence(exp.Current());
412 _dependenceAnalysed = true;
416 //=============================================================================
418 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
420 //=============================================================================
422 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
424 //MESSAGE("SMESH_subMesh::InsertDependence");
425 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
426 int type = aSubShape.ShapeType();
427 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
428 int cle = aSubMesh->GetId();
429 cle += 10000000 * ordType; // sort map by ordType then index
430 if ( _mapDepend.find( cle ) == _mapDepend.end())
432 _mapDepend[cle] = aSubMesh;
433 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
434 _mapDepend.insert( subMap.begin(), subMap.end() );
438 //=============================================================================
442 //=============================================================================
444 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
446 //MESSAGE("SMESH_subMesh::GetSubShape");
451 //=======================================================================
452 //function : CanAddHypothesis
453 //purpose : return true if theHypothesis can be attached to me:
454 // its dimention is checked
455 //=======================================================================
457 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
459 int aHypDim = theHypothesis->GetDim();
460 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
461 if ( aHypDim <= aShapeDim )
463 // if ( aHypDim < aShapeDim )
464 // return ( _father->IsMainShape( _subShape ));
469 //=======================================================================
470 //function : IsApplicableHypotesis
472 //=======================================================================
474 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
475 const TopAbs_ShapeEnum theShapeType)
477 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
479 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
482 switch ( theShapeType ) {
487 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
489 // case TopAbs_COMPSOLID:
490 // case TopAbs_COMPOUND:
496 //=============================================================================
500 //=============================================================================
502 SMESH_Hypothesis::Hypothesis_Status
503 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
505 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
506 //SCRUTE(_algoState);
509 // **** les retour des evenement shape sont significatifs
510 // (add ou remove fait ou non)
511 // le retour des evenement father n'indiquent pas que add ou remove fait
513 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
515 int dim = SMESH_Gen::GetShapeDim(_subShape);
520 if (event == ADD_HYP || event == ADD_ALGO)
521 return SMESH_Hypothesis::HYP_BAD_DIM; // do not allow to assign any hyp
523 return SMESH_Hypothesis::HYP_OK;
526 SMESH_Gen* gen =_father->GetGen();
528 int oldAlgoState = _algoState;
529 bool modifiedHyp = false; // if set to true, force event MODIF_ALGO_STATE
530 // in ComputeStateEngine
532 // ----------------------
533 // check mesh conformity
534 // ----------------------
535 if (event == ADD_ALGO)
537 if (IsApplicableHypotesis( anHyp ) &&
538 !_father->IsNotConformAllowed() &&
539 !IsConform( static_cast< SMESH_Algo* >( anHyp )))
540 return SMESH_Hypothesis::HYP_NOTCONFORM;
543 // ----------------------------------
544 // add a hypothesis to DS if possible
545 // ----------------------------------
546 if (event == ADD_HYP || event == ADD_ALGO)
548 if ( ! CanAddHypothesis( anHyp ))
549 return SMESH_Hypothesis::HYP_BAD_DIM;
551 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
552 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
554 if ( !_meshDS->AddHypothesis(_subShape, anHyp))
555 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
557 // Serve Propagation of 1D hypothesis
558 if (event == ADD_HYP) {
559 bool isPropagationOk = true;
560 bool isPropagationHyp = ( strcmp( "Propagation", anHyp->GetName() ) == 0 );
562 if ( isPropagationHyp ) {
563 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
564 TopTools_MapOfShape aMap;
565 for (; exp.More(); exp.Next()) {
566 if (aMap.Add(exp.Current())) {
567 if (!_father->BuildPropagationChain(exp.Current())) {
568 isPropagationOk = false;
573 else if (anHyp->GetDim() == 1) { // Only 1D hypothesis can be propagated
574 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
575 TopTools_MapOfShape aMap;
576 for (; exp.More(); exp.Next()) {
577 if (aMap.Add(exp.Current())) {
578 TopoDS_Shape aMainEdge;
579 if (_father->IsPropagatedHypothesis(exp.Current(), aMainEdge)) {
580 isPropagationOk = _father->RebuildPropagationChains();
581 } else if (_father->IsPropagationHypothesis(exp.Current())) {
582 isPropagationOk = _father->BuildPropagationChain(exp.Current());
590 if ( isPropagationOk ) {
591 if ( isPropagationHyp )
592 return ret; // nothing more to do for "Propagation" hypothesis
594 else if ( ret < SMESH_Hypothesis::HYP_CONCURENT) {
595 ret = SMESH_Hypothesis::HYP_CONCURENT;
597 } // Serve Propagation of 1D hypothesis
600 // --------------------------
601 // remove a hypothesis from DS
602 // --------------------------
603 if (event == REMOVE_HYP || event == REMOVE_ALGO)
605 if (!_meshDS->RemoveHypothesis(_subShape, anHyp))
606 return SMESH_Hypothesis::HYP_OK; // nothing changes
608 // Serve Propagation of 1D hypothesis
609 if (event == REMOVE_HYP)
611 bool isPropagationOk = true;
612 SMESH_HypoFilter propagFilter( SMESH_HypoFilter::HasName( "Propagation" ));
613 bool isPropagationHyp = propagFilter.IsOk( anHyp, _subShape );
615 if ( isPropagationHyp )
617 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
618 TopTools_MapOfShape aMap;
619 for (; exp.More(); exp.Next()) {
620 if (aMap.Add(exp.Current()) &&
621 !_father->GetHypothesis( exp.Current(), propagFilter, true )) {
622 // no more Propagation on the current edge
623 if (!_father->RemovePropagationChain(exp.Current())) {
624 return SMESH_Hypothesis::HYP_UNKNOWN_FATAL;
628 // rebuild propagation chains, because removing one
629 // chain can resolve concurention, existing before
630 isPropagationOk = _father->RebuildPropagationChains();
632 else if (anHyp->GetDim() == 1) // Only 1D hypothesis can be propagated
634 isPropagationOk = _father->RebuildPropagationChains();
637 if ( isPropagationOk ) {
638 if ( isPropagationHyp )
639 return ret; // nothing more to do for "Propagation" hypothesis
641 else if ( ret < SMESH_Hypothesis::HYP_CONCURENT) {
642 ret = SMESH_Hypothesis::HYP_CONCURENT;
644 } // Serve Propagation of 1D hypothesis
645 else // event == REMOVE_ALGO
647 SMESH_Algo* algo = dynamic_cast<SMESH_Algo*> (anHyp);
648 if (!algo->NeedDescretBoundary())
650 // clean all mesh in the tree of the current submesh;
651 // we must perform it now because later
652 // we will have no information about the type of the removed algo
654 ComputeStateEngine( CLEAN );
660 // ------------------
661 // analyse algo state
662 // ------------------
663 if (!IsApplicableHypotesis( anHyp ))
664 return ret; // not applicable hypotheses do not change algo state
669 // ----------------------------------------------------------------------
676 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
678 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
679 SetAlgoState(HYP_OK);
681 SetAlgoState(MISSING_HYP);
690 case ADD_FATHER_ALGO: { // Algo just added in father
691 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
693 if ( algo == anHyp ) {
694 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
695 SetAlgoState(HYP_OK);
697 SetAlgoState(MISSING_HYP);
701 case REMOVE_FATHER_HYP:
703 case REMOVE_FATHER_ALGO: {
704 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
707 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
708 SetAlgoState(HYP_OK);
710 SetAlgoState(MISSING_HYP);
720 // ----------------------------------------------------------------------
726 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
728 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
729 SetAlgoState(HYP_OK);
730 if (SMESH_Hypothesis::IsStatusFatal( ret ))
731 _meshDS->RemoveHypothesis(_subShape, anHyp);
732 else if (!_father->IsUsedHypothesis( anHyp, this ))
734 _meshDS->RemoveHypothesis(_subShape, anHyp);
735 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
739 case ADD_ALGO: { //already existing algo : on father ?
740 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
742 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
743 SetAlgoState(HYP_OK);
745 SetAlgoState(MISSING_HYP);
750 case REMOVE_ALGO: { // perhaps a father algo applies ?
751 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
752 if (algo == NULL) // no more algo applying on subShape...
754 SetAlgoState(NO_ALGO);
758 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
759 SetAlgoState(HYP_OK);
761 SetAlgoState(MISSING_HYP);
765 case ADD_FATHER_HYP: {
766 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
768 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
769 SetAlgoState(HYP_OK);
771 SetAlgoState(MISSING_HYP);
774 case ADD_FATHER_ALGO: { // new father algo
775 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
777 if ( algo == anHyp ) {
778 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
779 SetAlgoState(HYP_OK);
781 SetAlgoState(MISSING_HYP);
785 case REMOVE_FATHER_HYP: // nothing to do
787 case REMOVE_FATHER_ALGO: {
788 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
789 if (algo == NULL) // no more applying algo on father
791 SetAlgoState(NO_ALGO);
795 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
796 SetAlgoState(HYP_OK);
798 SetAlgoState(MISSING_HYP);
808 // ----------------------------------------------------------------------
814 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
816 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
818 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
819 // ret should be fatal: anHyp was not added
820 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
822 else if (!_father->IsUsedHypothesis( anHyp, this ))
823 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
825 if (SMESH_Hypothesis::IsStatusFatal( ret ))
827 MESSAGE("do not add extra hypothesis");
828 _meshDS->RemoveHypothesis(_subShape, anHyp);
836 case ADD_ALGO: { //already existing algo : on father ?
837 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
838 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
839 // check if algo changes
841 f.Init( SMESH_HypoFilter::IsAlgo() );
842 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
843 f.AndNot( SMESH_HypoFilter::Is( algo ));
844 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
846 string(algo->GetName()) != string(prevAlgo->GetName()) )
850 SetAlgoState(MISSING_HYP);
854 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
856 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
857 SetAlgoState(HYP_OK);
859 SetAlgoState(MISSING_HYP);
863 case REMOVE_ALGO: { // perhaps a father algo applies ?
864 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
865 if (algo == NULL) // no more algo applying on subShape...
867 SetAlgoState(NO_ALGO);
871 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
872 // check if algo remains
873 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
877 SetAlgoState(MISSING_HYP);
881 case ADD_FATHER_HYP: { // new father hypothesis ?
882 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
884 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
886 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
890 SetAlgoState(MISSING_HYP);
893 case ADD_FATHER_ALGO: {
894 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
895 if ( algo == anHyp ) { // a new algo on father
896 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
897 // check if algo changes
899 f.Init( SMESH_HypoFilter::IsAlgo() );
900 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
901 f.AndNot( SMESH_HypoFilter::Is( algo ));
902 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
904 string(algo->GetName()) != string(prevAlgo->GetName()) )
908 SetAlgoState(MISSING_HYP);
912 case REMOVE_FATHER_HYP: {
913 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
915 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
916 // is there the same local hyp or maybe a new father algo applied?
917 if ( !GetSimilarAttached( _subShape, anHyp ) )
921 SetAlgoState(MISSING_HYP);
924 case REMOVE_FATHER_ALGO: {
925 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
926 if (algo == NULL) // no more applying algo on father
928 SetAlgoState(NO_ALGO);
932 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
933 // check if algo changes
934 if ( string(algo->GetName()) != string( anHyp->GetName()) )
938 SetAlgoState(MISSING_HYP);
948 // ----------------------------------------------------------------------
955 if ((_algoState != oldAlgoState) || modifiedHyp)
956 ComputeStateEngine(MODIF_ALGO_STATE);
962 //=======================================================================
963 //function : IsConform
964 //purpose : check if a conform mesh will be produced by the Algo
965 //=======================================================================
967 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
969 // MESSAGE( "SMESH_subMesh::IsConform" );
971 if ( !theAlgo ) return false;
973 // check only algo that doesn't NeedDescretBoundary(): because mesh made
974 // on a sub-shape will be ignored by theAlgo
975 if ( theAlgo->NeedDescretBoundary() )
978 SMESH_Gen* gen =_father->GetGen();
980 // only local algo is to be checked
981 if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
984 // check algo attached to adjacent shapes
986 // loop on one level down sub-meshes
987 TopoDS_Iterator itsub( _subShape );
988 for (; itsub.More(); itsub.Next())
990 // loop on adjacent subShapes
991 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
992 for (; it.More(); it.Next())
994 const TopoDS_Shape& adjacent = it.Value();
995 if ( _subShape.IsSame( adjacent )) continue;
996 if ( adjacent.ShapeType() != _subShape.ShapeType())
999 // check algo attached to smAdjacent
1000 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1002 //algo != theAlgo &&
1003 !algo->NeedDescretBoundary() /*&&
1004 !gen->IsGlobalHypothesis( algo, *_father )*/)
1005 return false; // NOT CONFORM MESH WILL BE PRODUCED
1012 //=============================================================================
1016 //=============================================================================
1018 void SMESH_subMesh::SetAlgoState(int state)
1023 //=============================================================================
1027 //=============================================================================
1028 SMESH_Hypothesis::Hypothesis_Status
1029 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1030 SMESH_Hypothesis * anHyp)
1032 //MESSAGE("SMESH_subMesh::SubMeshesAlgoStateEngine");
1033 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1034 //EAP: a wire (dim==1) should notify edges (dim==1)
1035 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1036 if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1038 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1040 map < int, SMESH_subMesh * >::const_iterator itsub;
1041 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1043 SMESH_subMesh *sm = (*itsub).second;
1044 SMESH_Hypothesis::Hypothesis_Status ret2 =
1045 sm->AlgoStateEngine(event, anHyp);
1053 //=============================================================================
1057 //=============================================================================
1059 void SMESH_subMesh::CleanDependsOn()
1061 //MESSAGE("SMESH_subMesh::CleanDependsOn");
1063 const map < int, SMESH_subMesh * >&dependson = DependsOn();
1064 map < int, SMESH_subMesh * >::const_iterator its;
1065 for (its = dependson.begin(); its != dependson.end(); its++)
1067 SMESH_subMesh *sm = (*its).second;
1068 sm->ComputeStateEngine(CLEAN);
1072 //=============================================================================
1076 //=============================================================================
1078 void SMESH_subMesh::DumpAlgoState(bool isMain)
1080 int dim = SMESH_Gen::GetShapeDim(_subShape);
1081 // if (dim < 1) return;
1084 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1086 map < int, SMESH_subMesh * >::const_iterator itsub;
1087 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1089 SMESH_subMesh *sm = (*itsub).second;
1090 sm->DumpAlgoState(false);
1093 int type = _subShape.ShapeType();
1094 MESSAGE("dim = " << dim << " type of shape " << type);
1098 MESSAGE(" AlgoState = NO_ALGO");
1101 MESSAGE(" AlgoState = MISSING_HYP");
1104 MESSAGE(" AlgoState = HYP_OK");
1107 switch (_computeState)
1110 MESSAGE(" ComputeState = NOT_READY");
1112 case READY_TO_COMPUTE:
1113 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1116 MESSAGE(" ComputeState = COMPUTE_OK");
1118 case FAILED_TO_COMPUTE:
1119 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1124 //================================================================================
1126 * \brief Remove nodes and elements bound to submesh
1127 * \param subMesh - submesh containing nodes and elements
1129 //================================================================================
1131 static void cleanSubMesh( SMESH_subMesh * subMesh )
1134 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1135 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1136 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1137 while (ite->more()) {
1138 const SMDS_MeshElement * elt = ite->next();
1139 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1140 //meshDS->RemoveElement(elt);
1141 meshDS->RemoveFreeElement(elt, subMeshDS);
1144 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1145 while (itn->more()) {
1146 const SMDS_MeshNode * node = itn->next();
1147 //MESSAGE( " RM node: "<<node->GetID());
1148 //meshDS->RemoveNode(node);
1149 meshDS->RemoveFreeNode(node, subMeshDS);
1155 //=============================================================================
1159 //=============================================================================
1161 bool SMESH_subMesh::ComputeStateEngine(int event)
1163 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1164 //SCRUTE(_computeState);
1167 int dim = SMESH_Gen::GetShapeDim(_subShape);
1171 if ( IsMeshComputed() )
1172 _computeState = COMPUTE_OK;
1174 _computeState = READY_TO_COMPUTE;
1177 SMESH_Gen *gen = _father->GetGen();
1178 SMESH_Algo *algo = 0;
1180 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1182 switch (_computeState)
1185 // ----------------------------------------------------------------------
1191 case MODIF_ALGO_STATE:
1192 algo = gen->GetAlgo((*_father), _subShape);
1193 if (algo && !algo->NeedDescretBoundary())
1194 CleanDependsOn(); // clean sub-meshes with event CLEAN
1195 if (event == MODIF_ALGO_STATE && _algoState == HYP_OK)
1197 _computeState = READY_TO_COMPUTE;
1200 case COMPUTE: // nothing to do
1204 RemoveSubMeshElementsAndNodes();
1206 case SUBMESH_COMPUTED: // nothing to do
1208 case SUBMESH_RESTORED:
1209 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1211 case MESH_ENTITY_REMOVED:
1213 case CHECK_COMPUTE_STATE:
1214 if ( IsMeshComputed() )
1215 _computeState = COMPUTE_OK;
1223 // ----------------------------------------------------------------------
1225 case READY_TO_COMPUTE:
1229 case MODIF_ALGO_STATE:
1230 algo = gen->GetAlgo((*_father), _subShape);
1231 if (algo && !algo->NeedDescretBoundary())
1232 CleanDependsOn(); // clean sub-meshes with event CLEAN
1233 if (event == MODIF_HYP)
1234 break; // nothing else to do when MODIF_HYP
1235 _computeState = NOT_READY;
1238 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1240 _computeState = READY_TO_COMPUTE;
1245 algo = gen->GetAlgo((*_father), _subShape);
1247 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1250 MESSAGE("***** verify compute state *****");
1251 _computeState = NOT_READY;
1254 // check submeshes needed
1255 if (algo->NeedDescretBoundary())
1256 ret = SubMeshesComputed();
1259 MESSAGE("Some SubMeshes not computed");
1260 _computeState = FAILED_TO_COMPUTE;
1265 RemoveSubMeshElementsAndNodes();
1268 if (!algo->NeedDescretBoundary() && !algo->OnlyUnaryInput())
1269 ret = ApplyToCollection( algo, GetCollection( gen, algo ) );
1271 ret = algo->Compute((*_father), _subShape);
1273 catch (Standard_Failure) {
1274 MESSAGE( "Exception in algo->Compute() ");
1280 MESSAGE("problem in algo execution: failed to compute");
1281 _computeState = FAILED_TO_COMPUTE;
1282 if (!algo->NeedDescretBoundary())
1283 UpdateSubMeshState( FAILED_TO_COMPUTE );
1286 // Show vertices location of a failed shape
1287 cout << algo->GetName() << " failed on shape with the following vertices:" << endl;
1288 TopTools_IndexedMapOfShape vMap;
1289 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1290 for ( int iv = 1; iv <= vMap.Extent(); ++iv ) {
1291 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1292 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1299 _computeState = COMPUTE_OK;
1300 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1301 if (!algo->NeedDescretBoundary())
1302 UpdateSubMeshState( COMPUTE_OK );
1308 RemoveSubMeshElementsAndNodes();
1309 _computeState = NOT_READY;
1310 algo = gen->GetAlgo((*_father), _subShape);
1313 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1315 _computeState = READY_TO_COMPUTE;
1318 case SUBMESH_COMPUTED: // nothing to do
1320 case SUBMESH_RESTORED:
1321 // check if a mesh is already computed that may
1322 // happen after retrieval from a file
1323 ComputeStateEngine( CHECK_COMPUTE_STATE );
1324 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1326 case MESH_ENTITY_REMOVED:
1328 case CHECK_COMPUTE_STATE:
1329 if ( IsMeshComputed() )
1330 _computeState = COMPUTE_OK;
1338 // ----------------------------------------------------------------------
1344 case MODIF_ALGO_STATE:
1345 ComputeStateEngine( CLEAN );
1346 algo = gen->GetAlgo((*_father), _subShape);
1347 if (algo && !algo->NeedDescretBoundary())
1348 CleanDependsOn(); // clean sub-meshes with event CLEAN
1350 case COMPUTE: // nothing to do
1353 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1354 RemoveSubMeshElementsAndNodes();
1355 _computeState = NOT_READY;
1356 algo = gen->GetAlgo((*_father), _subShape);
1359 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1361 _computeState = READY_TO_COMPUTE;
1364 case SUBMESH_COMPUTED: // nothing to do
1366 case SUBMESH_RESTORED:
1367 ComputeStateEngine( CHECK_COMPUTE_STATE );
1368 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1370 case MESH_ENTITY_REMOVED:
1371 UpdateDependantsState( CHECK_COMPUTE_STATE );
1372 ComputeStateEngine( CHECK_COMPUTE_STATE );
1373 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1375 case CHECK_COMPUTE_STATE:
1376 if ( !IsMeshComputed() )
1377 if (_algoState == HYP_OK)
1378 _computeState = READY_TO_COMPUTE;
1380 _computeState = NOT_READY;
1388 // ----------------------------------------------------------------------
1390 case FAILED_TO_COMPUTE:
1394 if (_algoState == HYP_OK)
1395 _computeState = READY_TO_COMPUTE;
1397 _computeState = NOT_READY;
1399 case MODIF_ALGO_STATE:
1400 if (_algoState == HYP_OK)
1401 _computeState = READY_TO_COMPUTE;
1403 _computeState = NOT_READY;
1405 case COMPUTE: // nothing to do
1408 CleanDependants(); // submeshes dependent on me should be cleaned as well
1409 RemoveSubMeshElementsAndNodes();
1410 if (_algoState == HYP_OK)
1411 _computeState = READY_TO_COMPUTE;
1413 _computeState = NOT_READY;
1415 case SUBMESH_COMPUTED: // allow retry compute
1416 if (_algoState == HYP_OK)
1417 _computeState = READY_TO_COMPUTE;
1419 _computeState = NOT_READY;
1421 case SUBMESH_RESTORED:
1422 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1424 case MESH_ENTITY_REMOVED:
1426 case CHECK_COMPUTE_STATE:
1427 if ( IsMeshComputed() )
1428 _computeState = COMPUTE_OK;
1430 if (_algoState == HYP_OK)
1431 _computeState = READY_TO_COMPUTE;
1433 _computeState = NOT_READY;
1441 // ----------------------------------------------------------------------
1447 //SCRUTE(_computeState);
1451 //=======================================================================
1452 //function : ApplyToCollection
1453 //purpose : Apply theAlgo to all subshapes in theCollection
1454 //=======================================================================
1456 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1457 const TopoDS_Shape& theCollection)
1459 MESSAGE("SMESH_subMesh::ApplyToCollection");
1460 ASSERT ( !theAlgo->NeedDescretBoundary() );
1465 ret = theAlgo->Compute( *_father, theCollection );
1467 // set _computeState of subshapes
1468 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1469 for ( ; anExplorer.More(); anExplorer.Next() )
1471 const TopoDS_Shape& aSubShape = anExplorer.Current();
1472 SMESH_subMesh* subMesh = _father->GetSubMeshContaining( aSubShape );
1477 subMesh->_computeState = COMPUTE_OK;
1478 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1479 subMesh->UpdateSubMeshState( COMPUTE_OK );
1483 subMesh->_computeState = FAILED_TO_COMPUTE;
1491 //=======================================================================
1492 //function : UpdateSubMeshState
1494 //=======================================================================
1496 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1498 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1499 map<int, SMESH_subMesh*>::const_iterator itsub;
1500 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1502 SMESH_subMesh* sm = (*itsub).second;
1503 sm->_computeState = theState;
1507 //=======================================================================
1508 //function : ComputeSubMeshStateEngine
1510 //=======================================================================
1512 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1514 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1515 map<int, SMESH_subMesh*>::const_iterator itsub;
1516 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1518 SMESH_subMesh* sm = (*itsub).second;
1519 sm->ComputeStateEngine(event);
1523 //=======================================================================
1524 //function : UpdateDependantsState
1526 //=======================================================================
1528 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1530 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1531 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1532 for (; it.More(); it.Next())
1534 const TopoDS_Shape& ancestor = it.Value();
1535 SMESH_subMesh *aSubMesh =
1536 _father->GetSubMeshContaining(ancestor);
1538 aSubMesh->ComputeStateEngine( theEvent );
1542 //=============================================================================
1546 //=============================================================================
1548 void SMESH_subMesh::CleanDependants()
1550 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1552 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1553 for (; it.More(); it.Next())
1555 const TopoDS_Shape& ancestor = it.Value();
1556 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1557 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1558 // will erase mesh on other shapes in a compound
1559 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1560 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1562 aSubMesh->ComputeStateEngine(CLEAN);
1568 //=============================================================================
1572 //=============================================================================
1574 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1576 //SCRUTE(_subShape.ShapeType());
1578 cleanSubMesh( this );
1580 // algo may bind a submesh not to _subShape, eg 3D algo
1581 // sets nodes on SHELL while _subShape may be SOLID
1583 int dim = SMESH_Gen::GetShapeDim( _subShape );
1584 int type = _subShape.ShapeType() + 1;
1585 for ( ; type <= TopAbs_EDGE; type++) {
1586 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1588 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1589 for ( ; exp.More(); exp.Next() )
1590 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1597 //=======================================================================
1598 //function : IsMeshComputed
1599 //purpose : check if _subMeshDS contains mesh elements
1600 //=======================================================================
1602 bool SMESH_subMesh::IsMeshComputed() const
1604 // algo may bind a submesh not to _subShape, eg 3D algo
1605 // sets nodes on SHELL while _subShape may be SOLID
1607 int dim = SMESH_Gen::GetShapeDim( _subShape );
1608 int type = _subShape.ShapeType();
1609 for ( ; type <= TopAbs_VERTEX; type++) {
1610 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1612 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1613 for ( ; exp.More(); exp.Next() )
1615 SMESHDS_SubMesh * subMeshDS = _meshDS->MeshElements( exp.Current() );
1616 if ( subMeshDS != NULL &&
1617 (subMeshDS->GetElements()->more() || subMeshDS->GetNodes()->more())) {
1630 //=======================================================================
1631 //function : GetCollection
1632 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1633 // meshed at once along with _subShape
1634 //=======================================================================
1636 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1638 MESSAGE("SMESH_subMesh::GetCollection");
1639 ASSERT (!theAlgo->NeedDescretBoundary());
1641 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1643 if ( mainShape.IsSame( _subShape ))
1646 const bool ignoreAuxiliaryHyps = false;
1647 list<const SMESHDS_Hypothesis*> aUsedHyp =
1648 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1650 // put in a compound all shapes with the same hypothesis assigned
1651 // and a good ComputState
1653 TopoDS_Compound aCompound;
1654 BRep_Builder aBuilder;
1655 aBuilder.MakeCompound( aCompound );
1657 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1658 for ( ; anExplorer.More(); anExplorer.Next() )
1660 const TopoDS_Shape& S = anExplorer.Current();
1661 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1662 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1664 if (subMesh->GetComputeState() == READY_TO_COMPUTE &&
1665 anAlgo == theAlgo &&
1666 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp)
1668 aBuilder.Add( aCompound, S );
1675 //=======================================================================
1676 //function : GetSimilarAttached
1677 //purpose : return a hypothesis attached to theShape.
1678 // If theHyp is provided, similar but not same hypotheses
1679 // is returned; else only applicable ones having theHypType
1681 //=======================================================================
1683 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1684 const SMESH_Hypothesis * theHyp,
1685 const int theHypType)
1687 SMESH_HypoFilter hypoKind;
1688 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1690 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1691 hypoKind.AndNot( hypoKind.Is( theHyp ));
1692 if ( theHyp->IsAuxiliary() )
1693 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1695 hypoKind.AndNot( hypoKind.IsAuxiliary());
1698 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1701 return _father->GetHypothesis( theShape, hypoKind, false );
1704 //=======================================================================
1705 //function : CheckConcurentHypothesis
1706 //purpose : check if there are several applicable hypothesis attached to
1708 //=======================================================================
1710 SMESH_Hypothesis::Hypothesis_Status
1711 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1713 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1715 // is there local hypothesis on me?
1716 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1717 return SMESH_Hypothesis::HYP_OK;
1720 TopoDS_Shape aPrevWithHyp;
1721 const SMESH_Hypothesis* aPrevHyp = 0;
1722 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1723 for (; it.More(); it.Next())
1725 const TopoDS_Shape& ancestor = it.Value();
1726 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1729 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1731 aPrevWithHyp = ancestor;
1734 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1735 return SMESH_Hypothesis::HYP_CONCURENT;
1737 return SMESH_Hypothesis::HYP_OK;
1740 return SMESH_Hypothesis::HYP_OK;