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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
55 #include <Standard_Failure.hxx>
56 #include <Standard_ErrorHandler.hxx>
58 //=============================================================================
60 * default constructor:
62 //=============================================================================
64 SMESH_subMesh::SMESH_subMesh(int Id, SMESH_Mesh * father, SMESHDS_Mesh * meshDS,
65 const TopoDS_Shape & aSubShape)
67 _subShape = aSubShape;
69 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
72 _dependenceAnalysed = false;
74 if (_subShape.ShapeType() == TopAbs_VERTEX)
77 _computeState = READY_TO_COMPUTE;
82 _computeState = NOT_READY;
86 //=============================================================================
90 //=============================================================================
92 SMESH_subMesh::~SMESH_subMesh()
94 MESSAGE("SMESH_subMesh::~SMESH_subMesh");
98 //=============================================================================
102 //=============================================================================
104 int SMESH_subMesh::GetId() const
106 //MESSAGE("SMESH_subMesh::GetId");
110 //=============================================================================
114 //=============================================================================
116 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
118 // submesh appears in DS only when a mesher set nodes and elements on it
119 if (_subMeshDS==NULL)
121 _subMeshDS = _meshDS->MeshElements(_subShape); // may be null ...
122 // if (_subMeshDS==NULL)
124 // MESSAGE("problem... subMesh still empty");
130 //=============================================================================
134 //=============================================================================
136 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
138 if ( !GetSubMeshDS() )
139 _meshDS->NewSubMesh( _meshDS->ShapeToIndex( _subShape ) );
141 return GetSubMeshDS();
144 //=============================================================================
148 //=============================================================================
150 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
152 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
153 SMESH_subMesh *firstToCompute = 0;
155 map < int, SMESH_subMesh * >::const_iterator itsub;
156 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
158 SMESH_subMesh *sm = (*itsub).second;
159 bool readyToCompute = (sm->GetComputeState() == READY_TO_COMPUTE);
168 return firstToCompute; // a subMesh of this
170 if (_computeState == READY_TO_COMPUTE)
174 return 0; // nothing to compute
177 //=============================================================================
181 //=============================================================================
183 bool SMESH_subMesh::SubMeshesComputed()
185 //MESSAGE("SMESH_subMesh::SubMeshesComputed");
186 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
188 int myDim = SMESH_Gen::GetShapeDim( _subShape );
189 int dimToCheck = myDim - 1;
190 bool subMeshesComputed = true;
191 map < int, SMESH_subMesh * >::const_iterator itsub;
192 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
194 SMESH_subMesh *sm = (*itsub).second;
195 const TopoDS_Shape & ss = sm->GetSubShape();
196 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
197 // in checking of existence of edges if the algo needs only faces. Moreover,
198 // degenerated edges may have no submesh, as after computing NETGEN_2D.
199 int dim = SMESH_Gen::GetShapeDim( ss );
200 if (dim < dimToCheck)
202 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
204 // There are some tricks with compute states, e.g. Penta_3D leaves
205 // one face with READY_TO_COMPUTE state in order to be able to
206 // recompute 3D when a locale triangle hypo changes (see PAL7428).
207 // So we check if mesh is really present
208 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
209 (ds && ( ds->GetNodes()->more() || ds->GetElements()->more() )));
212 int type = ss.ShapeType();
214 subMeshesComputed = false;
218 case TopAbs_COMPOUND:
220 MESSAGE("The not computed sub mesh is a COMPOUND");
223 case TopAbs_COMPSOLID:
225 MESSAGE("The not computed sub mesh is a COMPSOLID");
230 MESSAGE("The not computed sub mesh is a SHEL");
235 MESSAGE("The not computed sub mesh is a WIRE");
240 MESSAGE("The not computed sub mesh is a SOLID");
245 MESSAGE("The not computed sub mesh is a FACE");
250 MESSAGE("The not computed sub mesh is a EDGE");
255 MESSAGE("The not computed sub mesh is of unknown type");
263 return subMeshesComputed;
266 //=============================================================================
270 //=============================================================================
272 bool SMESH_subMesh::SubMeshesReady()
274 MESSAGE("SMESH_subMesh::SubMeshesReady");
275 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
277 bool subMeshesReady = true;
278 map < int, SMESH_subMesh * >::const_iterator itsub;
279 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
281 SMESH_subMesh *sm = (*itsub).second;
282 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK)
283 || (sm->GetComputeState() == READY_TO_COMPUTE));
286 subMeshesReady = false;
291 return subMeshesReady;
294 //=============================================================================
296 * Construct dependence on first level subMeshes. complex shapes (compsolid,
297 * shell, wire) are not analysed the same way as simple shapes (solid, face,
299 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
300 * with possible multiples occurences. Multiples occurences corresponds to
301 * internal frontiers within shapes of the collection and must not be keeped.
302 * See FinalizeDependence.
304 //=============================================================================
306 const map < int, SMESH_subMesh * >&SMESH_subMesh::DependsOn()
308 if (_dependenceAnalysed)
311 //MESSAGE("SMESH_subMesh::DependsOn");
313 int type = _subShape.ShapeType();
317 case TopAbs_COMPOUND:
319 //MESSAGE("compound");
320 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
323 InsertDependence(exp.Current());
325 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More();
328 InsertDependence(exp.Current()); //only shell not in solid
330 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();
333 InsertDependence(exp.Current());
335 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();
338 InsertDependence(exp.Current());
342 case TopAbs_COMPSOLID:
344 //MESSAGE("compsolid");
345 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();
348 InsertDependence(exp.Current());
355 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
358 InsertDependence(exp.Current());
365 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
368 InsertDependence(exp.Current());
375 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();
378 InsertDependence(exp.Current());
385 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();
388 InsertDependence(exp.Current());
395 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More();
398 InsertDependence(exp.Current());
411 _dependenceAnalysed = true;
415 //=============================================================================
417 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
419 //=============================================================================
421 void SMESH_subMesh::InsertDependence(const TopoDS_Shape aSubShape)
423 //MESSAGE("SMESH_subMesh::InsertDependence");
424 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
425 int type = aSubShape.ShapeType();
426 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
427 int cle = aSubMesh->GetId();
428 cle += 10000000 * ordType; // sort map by ordType then index
429 if ( _mapDepend.find( cle ) == _mapDepend.end())
431 _mapDepend[cle] = aSubMesh;
432 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
433 _mapDepend.insert( subMap.begin(), subMap.end() );
437 //=============================================================================
441 //=============================================================================
443 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
445 //MESSAGE("SMESH_subMesh::GetSubShape");
450 //=======================================================================
451 //function : CanAddHypothesis
452 //purpose : return true if theHypothesis can be attached to me:
453 // its dimention is checked
454 //=======================================================================
456 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
458 int aHypDim = theHypothesis->GetDim();
459 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
460 if ( aHypDim <= aShapeDim )
462 // if ( aHypDim < aShapeDim )
463 // return ( _father->IsMainShape( _subShape ));
468 //=======================================================================
469 //function : IsApplicableHypotesis
471 //=======================================================================
473 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
474 const TopAbs_ShapeEnum theShapeType)
476 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
478 return ( theHypothesis->GetShapeType() & (1<< theShapeType));
481 switch ( theShapeType ) {
486 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
488 // case TopAbs_COMPSOLID:
489 // case TopAbs_COMPOUND:
495 //=============================================================================
499 //=============================================================================
501 SMESH_Hypothesis::Hypothesis_Status
502 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
504 // MESSAGE("SMESH_subMesh::AlgoStateEngine");
505 //SCRUTE(_algoState);
508 // **** les retour des evenement shape sont significatifs
509 // (add ou remove fait ou non)
510 // le retour des evenement father n'indiquent pas que add ou remove fait
512 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
514 int dim = SMESH_Gen::GetShapeDim(_subShape);
519 if (event == ADD_HYP || event == ADD_ALGO)
520 return SMESH_Hypothesis::HYP_BAD_DIM; // do not allow to assign any hyp
522 return SMESH_Hypothesis::HYP_OK;
525 SMESH_Gen* gen =_father->GetGen();
527 int oldAlgoState = _algoState;
528 bool modifiedHyp = false; // if set to true, force event MODIF_ALGO_STATE
529 // in ComputeStateEngine
531 // ----------------------
532 // check mesh conformity
533 // ----------------------
534 if (event == ADD_ALGO)
536 if (IsApplicableHypotesis( anHyp ) &&
537 !_father->IsNotConformAllowed() &&
538 !IsConform( static_cast< SMESH_Algo* >( anHyp )))
539 return SMESH_Hypothesis::HYP_NOTCONFORM;
542 // ----------------------------------
543 // add a hypothesis to DS if possible
544 // ----------------------------------
545 if (event == ADD_HYP || event == ADD_ALGO)
547 if ( ! CanAddHypothesis( anHyp ))
548 return SMESH_Hypothesis::HYP_BAD_DIM;
550 if ( /*!anHyp->IsAuxiliary() &&*/ GetSimilarAttached( _subShape, anHyp ) )
551 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
553 if ( !_meshDS->AddHypothesis(_subShape, anHyp))
554 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
556 // Serve Propagation of 1D hypothesis
557 if (event == ADD_HYP) {
558 bool isPropagationOk = true;
559 bool isPropagationHyp = ( strcmp( "Propagation", anHyp->GetName() ) == 0 );
561 if ( isPropagationHyp ) {
562 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
563 TopTools_MapOfShape aMap;
564 for (; exp.More(); exp.Next()) {
565 if (aMap.Add(exp.Current())) {
566 if (!_father->BuildPropagationChain(exp.Current())) {
567 isPropagationOk = false;
572 else if (anHyp->GetDim() == 1) { // Only 1D hypothesis can be propagated
573 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
574 TopTools_MapOfShape aMap;
575 for (; exp.More(); exp.Next()) {
576 if (aMap.Add(exp.Current())) {
577 TopoDS_Shape aMainEdge;
578 if (_father->IsPropagatedHypothesis(exp.Current(), aMainEdge)) {
579 isPropagationOk = _father->RebuildPropagationChains();
580 } else if (_father->IsPropagationHypothesis(exp.Current())) {
581 isPropagationOk = _father->BuildPropagationChain(exp.Current());
589 if ( isPropagationOk ) {
590 if ( isPropagationHyp )
591 return ret; // nothing more to do for "Propagation" hypothesis
593 else if ( ret < SMESH_Hypothesis::HYP_CONCURENT) {
594 ret = SMESH_Hypothesis::HYP_CONCURENT;
596 } // Serve Propagation of 1D hypothesis
599 // --------------------------
600 // remove a hypothesis from DS
601 // --------------------------
602 if (event == REMOVE_HYP || event == REMOVE_ALGO)
604 if (!_meshDS->RemoveHypothesis(_subShape, anHyp))
605 return SMESH_Hypothesis::HYP_OK; // nothing changes
607 // Serve Propagation of 1D hypothesis
608 if (event == REMOVE_HYP)
610 bool isPropagationOk = true;
611 SMESH_HypoFilter propagFilter( SMESH_HypoFilter::HasName( "Propagation" ));
612 bool isPropagationHyp = propagFilter.IsOk( anHyp, _subShape );
614 if ( isPropagationHyp )
616 TopExp_Explorer exp (_subShape, TopAbs_EDGE);
617 TopTools_MapOfShape aMap;
618 for (; exp.More(); exp.Next()) {
619 if (aMap.Add(exp.Current()) &&
620 !_father->GetHypothesis( exp.Current(), propagFilter, true )) {
621 // no more Propagation on the current edge
622 if (!_father->RemovePropagationChain(exp.Current())) {
623 return SMESH_Hypothesis::HYP_UNKNOWN_FATAL;
627 // rebuild propagation chains, because removing one
628 // chain can resolve concurention, existing before
629 isPropagationOk = _father->RebuildPropagationChains();
631 else if (anHyp->GetDim() == 1) // Only 1D hypothesis can be propagated
633 isPropagationOk = _father->RebuildPropagationChains();
636 if ( isPropagationOk ) {
637 if ( isPropagationHyp )
638 return ret; // nothing more to do for "Propagation" hypothesis
640 else if ( ret < SMESH_Hypothesis::HYP_CONCURENT) {
641 ret = SMESH_Hypothesis::HYP_CONCURENT;
643 } // Serve Propagation of 1D hypothesis
644 else // event == REMOVE_ALGO
646 SMESH_Algo* algo = dynamic_cast<SMESH_Algo*> (anHyp);
647 if (!algo->NeedDescretBoundary())
649 // clean all mesh in the tree of the current submesh;
650 // we must perform it now because later
651 // we will have no information about the type of the removed algo
653 ComputeStateEngine( CLEAN );
659 // ------------------
660 // analyse algo state
661 // ------------------
662 if (!IsApplicableHypotesis( anHyp ))
663 return ret; // not applicable hypotheses do not change algo state
668 // ----------------------------------------------------------------------
675 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
677 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
678 SetAlgoState(HYP_OK);
680 SetAlgoState(MISSING_HYP);
689 case ADD_FATHER_ALGO: { // Algo just added in father
690 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
692 if ( algo == anHyp ) {
693 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
694 SetAlgoState(HYP_OK);
696 SetAlgoState(MISSING_HYP);
700 case REMOVE_FATHER_HYP:
702 case REMOVE_FATHER_ALGO: {
703 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
706 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
707 SetAlgoState(HYP_OK);
709 SetAlgoState(MISSING_HYP);
719 // ----------------------------------------------------------------------
725 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
727 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
728 SetAlgoState(HYP_OK);
729 if (SMESH_Hypothesis::IsStatusFatal( ret ))
730 _meshDS->RemoveHypothesis(_subShape, anHyp);
731 else if (!_father->IsUsedHypothesis( anHyp, this ))
733 _meshDS->RemoveHypothesis(_subShape, anHyp);
734 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
738 case ADD_ALGO: { //already existing algo : on father ?
739 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
741 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
742 SetAlgoState(HYP_OK);
744 SetAlgoState(MISSING_HYP);
749 case REMOVE_ALGO: { // perhaps a father algo applies ?
750 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
751 if (algo == NULL) // no more algo applying on subShape...
753 SetAlgoState(NO_ALGO);
757 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
758 SetAlgoState(HYP_OK);
760 SetAlgoState(MISSING_HYP);
764 case ADD_FATHER_HYP: {
765 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
767 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
768 SetAlgoState(HYP_OK);
770 SetAlgoState(MISSING_HYP);
773 case ADD_FATHER_ALGO: { // new father algo
774 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
776 if ( algo == anHyp ) {
777 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
778 SetAlgoState(HYP_OK);
780 SetAlgoState(MISSING_HYP);
784 case REMOVE_FATHER_HYP: // nothing to do
786 case REMOVE_FATHER_ALGO: {
787 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
788 if (algo == NULL) // no more applying algo on father
790 SetAlgoState(NO_ALGO);
794 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
795 SetAlgoState(HYP_OK);
797 SetAlgoState(MISSING_HYP);
807 // ----------------------------------------------------------------------
813 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
815 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
817 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
818 // ret should be fatal: anHyp was not added
819 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
821 else if (!_father->IsUsedHypothesis( anHyp, this ))
822 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
824 if (SMESH_Hypothesis::IsStatusFatal( ret ))
826 MESSAGE("do not add extra hypothesis");
827 _meshDS->RemoveHypothesis(_subShape, anHyp);
835 case ADD_ALGO: { //already existing algo : on father ?
836 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
837 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
838 // check if algo changes
840 f.Init( SMESH_HypoFilter::IsAlgo() );
841 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
842 f.AndNot( SMESH_HypoFilter::Is( algo ));
843 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( _subShape, f, true );
845 string(algo->GetName()) != string(prevAlgo->GetName()) )
849 SetAlgoState(MISSING_HYP);
853 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
855 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
856 SetAlgoState(HYP_OK);
858 SetAlgoState(MISSING_HYP);
862 case REMOVE_ALGO: { // perhaps a father algo applies ?
863 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
864 if (algo == NULL) // no more algo applying on subShape...
866 SetAlgoState(NO_ALGO);
870 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
871 // check if algo remains
872 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
876 SetAlgoState(MISSING_HYP);
880 case ADD_FATHER_HYP: { // new father hypothesis ?
881 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
883 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
885 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
889 SetAlgoState(MISSING_HYP);
892 case ADD_FATHER_ALGO: {
893 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
894 if ( algo == anHyp ) { // a new algo on father
895 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
896 // check if algo changes
898 f.Init( SMESH_HypoFilter::IsAlgo() );
899 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
900 f.AndNot( SMESH_HypoFilter::Is( algo ));
901 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( _subShape, f, true );
903 string(algo->GetName()) != string(prevAlgo->GetName()) )
907 SetAlgoState(MISSING_HYP);
911 case REMOVE_FATHER_HYP: {
912 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
914 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
915 // is there the same local hyp or maybe a new father algo applied?
916 if ( !GetSimilarAttached( _subShape, anHyp ) )
920 SetAlgoState(MISSING_HYP);
923 case REMOVE_FATHER_ALGO: {
924 SMESH_Algo* algo = gen->GetAlgo((*_father), _subShape);
925 if (algo == NULL) // no more applying algo on father
927 SetAlgoState(NO_ALGO);
931 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
932 // check if algo changes
933 if ( string(algo->GetName()) != string( anHyp->GetName()) )
937 SetAlgoState(MISSING_HYP);
947 // ----------------------------------------------------------------------
954 if ((_algoState != oldAlgoState) || modifiedHyp)
955 ComputeStateEngine(MODIF_ALGO_STATE);
961 //=======================================================================
962 //function : IsConform
963 //purpose : check if a conform mesh will be produced by the Algo
964 //=======================================================================
966 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
968 // MESSAGE( "SMESH_subMesh::IsConform" );
970 if ( !theAlgo ) return false;
972 // check only algo that doesn't NeedDescretBoundary(): because mesh made
973 // on a sub-shape will be ignored by theAlgo
974 if ( theAlgo->NeedDescretBoundary() )
977 SMESH_Gen* gen =_father->GetGen();
979 // only local algo is to be checked
980 if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
983 // check algo attached to adjacent shapes
985 // loop on one level down sub-meshes
986 TopoDS_Iterator itsub( _subShape );
987 for (; itsub.More(); itsub.Next())
989 // loop on adjacent subShapes
990 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( itsub.Value() ));
991 for (; it.More(); it.Next())
993 const TopoDS_Shape& adjacent = it.Value();
994 if ( _subShape.IsSame( adjacent )) continue;
995 if ( adjacent.ShapeType() != _subShape.ShapeType())
998 // check algo attached to smAdjacent
999 SMESH_Algo * algo = gen->GetAlgo((*_father), adjacent);
1001 //algo != theAlgo &&
1002 !algo->NeedDescretBoundary() /*&&
1003 !gen->IsGlobalHypothesis( algo, *_father )*/)
1004 return false; // NOT CONFORM MESH WILL BE PRODUCED
1011 //=============================================================================
1015 //=============================================================================
1017 void SMESH_subMesh::SetAlgoState(int state)
1022 //=============================================================================
1026 //=============================================================================
1027 SMESH_Hypothesis::Hypothesis_Status
1028 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1029 SMESH_Hypothesis * anHyp)
1031 //MESSAGE("SMESH_subMesh::SubMeshesAlgoStateEngine");
1032 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1033 //EAP: a wire (dim==1) should notify edges (dim==1)
1034 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1035 if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1037 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1039 map < int, SMESH_subMesh * >::const_iterator itsub;
1040 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1042 SMESH_subMesh *sm = (*itsub).second;
1043 SMESH_Hypothesis::Hypothesis_Status ret2 =
1044 sm->AlgoStateEngine(event, anHyp);
1052 //=============================================================================
1056 //=============================================================================
1058 void SMESH_subMesh::CleanDependsOn()
1060 //MESSAGE("SMESH_subMesh::CleanDependsOn");
1062 const map < int, SMESH_subMesh * >&dependson = DependsOn();
1063 map < int, SMESH_subMesh * >::const_iterator its;
1064 for (its = dependson.begin(); its != dependson.end(); its++)
1066 SMESH_subMesh *sm = (*its).second;
1067 sm->ComputeStateEngine(CLEAN);
1071 //=============================================================================
1075 //=============================================================================
1077 void SMESH_subMesh::DumpAlgoState(bool isMain)
1079 int dim = SMESH_Gen::GetShapeDim(_subShape);
1080 // if (dim < 1) return;
1083 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1085 map < int, SMESH_subMesh * >::const_iterator itsub;
1086 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1088 SMESH_subMesh *sm = (*itsub).second;
1089 sm->DumpAlgoState(false);
1092 int type = _subShape.ShapeType();
1093 MESSAGE("dim = " << dim << " type of shape " << type);
1097 MESSAGE(" AlgoState = NO_ALGO");
1100 MESSAGE(" AlgoState = MISSING_HYP");
1103 MESSAGE(" AlgoState = HYP_OK");
1106 switch (_computeState)
1109 MESSAGE(" ComputeState = NOT_READY");
1111 case READY_TO_COMPUTE:
1112 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1115 MESSAGE(" ComputeState = COMPUTE_OK");
1117 case FAILED_TO_COMPUTE:
1118 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1123 //================================================================================
1125 * \brief Remove nodes and elements bound to submesh
1126 * \param subMesh - submesh containing nodes and elements
1128 //================================================================================
1130 static void cleanSubMesh( SMESH_subMesh * subMesh )
1133 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1134 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1135 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1136 while (ite->more()) {
1137 const SMDS_MeshElement * elt = ite->next();
1138 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1139 //meshDS->RemoveElement(elt);
1140 meshDS->RemoveFreeElement(elt, subMeshDS);
1143 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1144 while (itn->more()) {
1145 const SMDS_MeshNode * node = itn->next();
1146 //MESSAGE( " RM node: "<<node->GetID());
1147 //meshDS->RemoveNode(node);
1148 meshDS->RemoveFreeNode(node, subMeshDS);
1154 //=============================================================================
1158 //=============================================================================
1160 bool SMESH_subMesh::ComputeStateEngine(int event)
1162 //MESSAGE("SMESH_subMesh::ComputeStateEngine");
1163 //SCRUTE(_computeState);
1166 int dim = SMESH_Gen::GetShapeDim(_subShape);
1170 if ( IsMeshComputed() )
1171 _computeState = COMPUTE_OK;
1173 _computeState = READY_TO_COMPUTE;
1176 SMESH_Gen *gen = _father->GetGen();
1177 SMESH_Algo *algo = 0;
1179 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1181 switch (_computeState)
1184 // ----------------------------------------------------------------------
1190 case MODIF_ALGO_STATE:
1191 algo = gen->GetAlgo((*_father), _subShape);
1192 if (algo && !algo->NeedDescretBoundary())
1193 CleanDependsOn(); // clean sub-meshes with event CLEAN
1194 if (event == MODIF_ALGO_STATE && _algoState == HYP_OK)
1196 _computeState = READY_TO_COMPUTE;
1199 case COMPUTE: // nothing to do
1203 RemoveSubMeshElementsAndNodes();
1205 case SUBMESH_COMPUTED: // nothing to do
1207 case SUBMESH_RESTORED:
1208 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1210 case MESH_ENTITY_REMOVED:
1212 case CHECK_COMPUTE_STATE:
1213 if ( IsMeshComputed() )
1214 _computeState = COMPUTE_OK;
1222 // ----------------------------------------------------------------------
1224 case READY_TO_COMPUTE:
1228 case MODIF_ALGO_STATE:
1229 algo = gen->GetAlgo((*_father), _subShape);
1230 if (algo && !algo->NeedDescretBoundary())
1231 CleanDependsOn(); // clean sub-meshes with event CLEAN
1232 if (event == MODIF_HYP)
1233 break; // nothing else to do when MODIF_HYP
1234 _computeState = NOT_READY;
1237 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1239 _computeState = READY_TO_COMPUTE;
1244 algo = gen->GetAlgo((*_father), _subShape);
1246 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1249 MESSAGE("***** verify compute state *****");
1250 _computeState = NOT_READY;
1253 // check submeshes needed
1254 if (algo->NeedDescretBoundary())
1255 ret = SubMeshesComputed();
1258 MESSAGE("Some SubMeshes not computed");
1259 _computeState = FAILED_TO_COMPUTE;
1264 RemoveSubMeshElementsAndNodes();
1267 if (!algo->NeedDescretBoundary() && !algo->OnlyUnaryInput())
1268 ret = ApplyToCollection( algo, GetCollection( gen, algo ) );
1270 ret = algo->Compute((*_father), _subShape);
1272 catch (Standard_Failure) {
1273 MESSAGE( "Exception in algo->Compute() ");
1279 MESSAGE("problem in algo execution: failed to compute");
1280 _computeState = FAILED_TO_COMPUTE;
1281 if (!algo->NeedDescretBoundary())
1282 UpdateSubMeshState( FAILED_TO_COMPUTE );
1285 // Show vertices location of a failed shape
1286 cout << algo->GetName() << " failed on shape with the following vertices:" << endl;
1287 TopTools_IndexedMapOfShape vMap;
1288 TopExp::MapShapes( _subShape, TopAbs_VERTEX, vMap );
1289 for ( int iv = 1; iv <= vMap.Extent(); ++iv ) {
1290 gp_Pnt P( BRep_Tool::Pnt( TopoDS::Vertex( vMap( iv ) )));
1291 cout << P.X() << " " << P.Y() << " " << P.Z() << " " << endl;
1298 _computeState = COMPUTE_OK;
1299 UpdateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1300 if (!algo->NeedDescretBoundary())
1301 UpdateSubMeshState( COMPUTE_OK );
1307 RemoveSubMeshElementsAndNodes();
1308 _computeState = NOT_READY;
1309 algo = gen->GetAlgo((*_father), _subShape);
1312 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1314 _computeState = READY_TO_COMPUTE;
1317 case SUBMESH_COMPUTED: // nothing to do
1319 case SUBMESH_RESTORED:
1320 // check if a mesh is already computed that may
1321 // happen after retrieval from a file
1322 ComputeStateEngine( CHECK_COMPUTE_STATE );
1323 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1325 case MESH_ENTITY_REMOVED:
1327 case CHECK_COMPUTE_STATE:
1328 if ( IsMeshComputed() )
1329 _computeState = COMPUTE_OK;
1337 // ----------------------------------------------------------------------
1343 case MODIF_ALGO_STATE:
1344 ComputeStateEngine( CLEAN );
1345 algo = gen->GetAlgo((*_father), _subShape);
1346 if (algo && !algo->NeedDescretBoundary())
1347 CleanDependsOn(); // clean sub-meshes with event CLEAN
1349 case COMPUTE: // nothing to do
1352 CleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1353 RemoveSubMeshElementsAndNodes();
1354 _computeState = NOT_READY;
1355 algo = gen->GetAlgo((*_father), _subShape);
1358 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1360 _computeState = READY_TO_COMPUTE;
1363 case SUBMESH_COMPUTED: // nothing to do
1365 case SUBMESH_RESTORED:
1366 ComputeStateEngine( CHECK_COMPUTE_STATE );
1367 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1369 case MESH_ENTITY_REMOVED:
1370 UpdateDependantsState( CHECK_COMPUTE_STATE );
1371 ComputeStateEngine( CHECK_COMPUTE_STATE );
1372 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1374 case CHECK_COMPUTE_STATE:
1375 if ( !IsMeshComputed() )
1376 if (_algoState == HYP_OK)
1377 _computeState = READY_TO_COMPUTE;
1379 _computeState = NOT_READY;
1387 // ----------------------------------------------------------------------
1389 case FAILED_TO_COMPUTE:
1393 if (_algoState == HYP_OK)
1394 _computeState = READY_TO_COMPUTE;
1396 _computeState = NOT_READY;
1398 case MODIF_ALGO_STATE:
1399 if (_algoState == HYP_OK)
1400 _computeState = READY_TO_COMPUTE;
1402 _computeState = NOT_READY;
1404 case COMPUTE: // nothing to do
1407 CleanDependants(); // submeshes dependent on me should be cleaned as well
1408 RemoveSubMeshElementsAndNodes();
1409 if (_algoState == HYP_OK)
1410 _computeState = READY_TO_COMPUTE;
1412 _computeState = NOT_READY;
1414 case SUBMESH_COMPUTED: // allow retry compute
1415 if (_algoState == HYP_OK)
1416 _computeState = READY_TO_COMPUTE;
1418 _computeState = NOT_READY;
1420 case SUBMESH_RESTORED:
1421 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1423 case MESH_ENTITY_REMOVED:
1425 case CHECK_COMPUTE_STATE:
1426 if ( IsMeshComputed() )
1427 _computeState = COMPUTE_OK;
1429 if (_algoState == HYP_OK)
1430 _computeState = READY_TO_COMPUTE;
1432 _computeState = NOT_READY;
1440 // ----------------------------------------------------------------------
1446 //SCRUTE(_computeState);
1450 //=======================================================================
1451 //function : ApplyToCollection
1452 //purpose : Apply theAlgo to all subshapes in theCollection
1453 //=======================================================================
1455 bool SMESH_subMesh::ApplyToCollection (SMESH_Algo* theAlgo,
1456 const TopoDS_Shape& theCollection)
1458 MESSAGE("SMESH_subMesh::ApplyToCollection");
1459 ASSERT ( !theAlgo->NeedDescretBoundary() );
1464 ret = theAlgo->Compute( *_father, theCollection );
1466 // set _computeState of subshapes
1467 TopExp_Explorer anExplorer( theCollection, _subShape.ShapeType() );
1468 for ( ; anExplorer.More(); anExplorer.Next() )
1470 const TopoDS_Shape& aSubShape = anExplorer.Current();
1471 SMESH_subMesh* subMesh = _father->GetSubMeshContaining( aSubShape );
1476 subMesh->_computeState = COMPUTE_OK;
1477 subMesh->UpdateDependantsState( SUBMESH_COMPUTED );
1478 subMesh->UpdateSubMeshState( COMPUTE_OK );
1482 subMesh->_computeState = FAILED_TO_COMPUTE;
1490 //=======================================================================
1491 //function : UpdateSubMeshState
1493 //=======================================================================
1495 void SMESH_subMesh::UpdateSubMeshState(const compute_state theState)
1497 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1498 map<int, SMESH_subMesh*>::const_iterator itsub;
1499 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1501 SMESH_subMesh* sm = (*itsub).second;
1502 sm->_computeState = theState;
1506 //=======================================================================
1507 //function : ComputeSubMeshStateEngine
1509 //=======================================================================
1511 void SMESH_subMesh::ComputeSubMeshStateEngine(int event)
1513 const map<int, SMESH_subMesh*>& smMap = DependsOn();
1514 map<int, SMESH_subMesh*>::const_iterator itsub;
1515 for (itsub = smMap.begin(); itsub != smMap.end(); itsub++)
1517 SMESH_subMesh* sm = (*itsub).second;
1518 sm->ComputeStateEngine(event);
1522 //=======================================================================
1523 //function : UpdateDependantsState
1525 //=======================================================================
1527 void SMESH_subMesh::UpdateDependantsState(const compute_event theEvent)
1529 //MESSAGE("SMESH_subMesh::UpdateDependantsState");
1530 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1531 for (; it.More(); it.Next())
1533 const TopoDS_Shape& ancestor = it.Value();
1534 SMESH_subMesh *aSubMesh =
1535 _father->GetSubMeshContaining(ancestor);
1537 aSubMesh->ComputeStateEngine( theEvent );
1541 //=============================================================================
1545 //=============================================================================
1547 void SMESH_subMesh::CleanDependants()
1549 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1551 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1552 for (; it.More(); it.Next())
1554 const TopoDS_Shape& ancestor = it.Value();
1555 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean ) {
1556 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1557 // will erase mesh on other shapes in a compound
1558 if ( ancestor.ShapeType() >= TopAbs_SOLID ) {
1559 SMESH_subMesh *aSubMesh = _father->GetSubMeshContaining(ancestor);
1561 aSubMesh->ComputeStateEngine(CLEAN);
1567 //=============================================================================
1571 //=============================================================================
1573 void SMESH_subMesh::RemoveSubMeshElementsAndNodes()
1575 //SCRUTE(_subShape.ShapeType());
1577 cleanSubMesh( this );
1579 // algo may bind a submesh not to _subShape, eg 3D algo
1580 // sets nodes on SHELL while _subShape may be SOLID
1582 int dim = SMESH_Gen::GetShapeDim( _subShape );
1583 int type = _subShape.ShapeType() + 1;
1584 for ( ; type <= TopAbs_EDGE; type++) {
1585 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1587 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1588 for ( ; exp.More(); exp.Next() )
1589 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
1596 //=======================================================================
1597 //function : IsMeshComputed
1598 //purpose : check if _subMeshDS contains mesh elements
1599 //=======================================================================
1601 bool SMESH_subMesh::IsMeshComputed() const
1603 // algo may bind a submesh not to _subShape, eg 3D algo
1604 // sets nodes on SHELL while _subShape may be SOLID
1606 int dim = SMESH_Gen::GetShapeDim( _subShape );
1607 int type = _subShape.ShapeType();
1608 for ( ; type <= TopAbs_VERTEX; type++) {
1609 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
1611 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
1612 for ( ; exp.More(); exp.Next() )
1614 SMESHDS_SubMesh * subMeshDS = _meshDS->MeshElements( exp.Current() );
1615 if ( subMeshDS != NULL &&
1616 (subMeshDS->GetElements()->more() || subMeshDS->GetNodes()->more())) {
1629 //=======================================================================
1630 //function : GetCollection
1631 //purpose : return a shape containing all sub-shapes of the MainShape that can be
1632 // meshed at once along with _subShape
1633 //=======================================================================
1635 TopoDS_Shape SMESH_subMesh::GetCollection(SMESH_Gen * theGen, SMESH_Algo* theAlgo)
1637 MESSAGE("SMESH_subMesh::GetCollection");
1638 ASSERT (!theAlgo->NeedDescretBoundary());
1640 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
1642 if ( mainShape.IsSame( _subShape ))
1645 const bool ignoreAuxiliaryHyps = false;
1646 list<const SMESHDS_Hypothesis*> aUsedHyp =
1647 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
1649 // put in a compound all shapes with the same hypothesis assigned
1650 // and a good ComputState
1652 TopoDS_Compound aCompound;
1653 BRep_Builder aBuilder;
1654 aBuilder.MakeCompound( aCompound );
1656 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
1657 for ( ; anExplorer.More(); anExplorer.Next() )
1659 const TopoDS_Shape& S = anExplorer.Current();
1660 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
1661 SMESH_Algo* anAlgo = theGen->GetAlgo( *_father, S );
1663 if (subMesh->GetComputeState() == READY_TO_COMPUTE &&
1664 anAlgo == theAlgo &&
1665 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp)
1667 aBuilder.Add( aCompound, S );
1674 //=======================================================================
1675 //function : GetSimilarAttached
1676 //purpose : return a hypothesis attached to theShape.
1677 // If theHyp is provided, similar but not same hypotheses
1678 // is returned; else only applicable ones having theHypType
1680 //=======================================================================
1682 const SMESH_Hypothesis* SMESH_subMesh::GetSimilarAttached(const TopoDS_Shape& theShape,
1683 const SMESH_Hypothesis * theHyp,
1684 const int theHypType)
1686 SMESH_HypoFilter hypoKind;
1687 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
1689 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
1690 hypoKind.AndNot( hypoKind.Is( theHyp ));
1691 if ( theHyp->IsAuxiliary() )
1692 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
1694 hypoKind.AndNot( hypoKind.IsAuxiliary());
1697 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
1700 return _father->GetHypothesis( theShape, hypoKind, false );
1703 //=======================================================================
1704 //function : CheckConcurentHypothesis
1705 //purpose : check if there are several applicable hypothesis attached to
1707 //=======================================================================
1709 SMESH_Hypothesis::Hypothesis_Status
1710 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
1712 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
1714 // is there local hypothesis on me?
1715 if ( GetSimilarAttached( _subShape, 0, theHypType ) )
1716 return SMESH_Hypothesis::HYP_OK;
1719 TopoDS_Shape aPrevWithHyp;
1720 const SMESH_Hypothesis* aPrevHyp = 0;
1721 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
1722 for (; it.More(); it.Next())
1724 const TopoDS_Shape& ancestor = it.Value();
1725 const SMESH_Hypothesis* hyp = GetSimilarAttached( ancestor, 0, theHypType );
1728 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
1730 aPrevWithHyp = ancestor;
1733 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
1734 return SMESH_Hypothesis::HYP_CONCURENT;
1736 return SMESH_Hypothesis::HYP_OK;
1739 return SMESH_Hypothesis::HYP_OK;