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_Gen.cxx
25 // Author : Paul RASCLE, EDF
29 #include "SMESH_Gen.hxx"
30 #include "SMESH_subMesh.hxx"
31 #include "SMDS_MeshElement.hxx"
32 #include "SMDS_MeshNode.hxx"
34 #include "utilities.h"
36 #include "Utils_ExceptHandlers.hxx"
39 #include <BRep_Tool.hxx>
40 #include <TopTools_ListOfShape.hxx>
41 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 //=============================================================================
47 * default constructor:
49 //=============================================================================
51 SMESH_Gen::SMESH_Gen()
53 MESSAGE("SMESH_Gen::SMESH_Gen");
58 //=============================================================================
62 //=============================================================================
64 SMESH_Gen::~SMESH_Gen()
66 MESSAGE("SMESH_Gen::~SMESH_Gen");
69 //=============================================================================
73 //=============================================================================
75 /*SMESH_Hypothesis *SMESH_Gen::CreateHypothesis(const char *anHyp, int studyId)
76 throw(SALOME_Exception)
79 MESSAGE("CreateHypothesis("<<anHyp<<","<<studyId<<")");
80 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
82 StudyContextStruct *myStudyContext = GetStudyContext(studyId);
84 // create a new hypothesis object, store its ref. in studyContext
86 SMESH_Hypothesis *myHypothesis = _hypothesisFactory.Create(anHyp, studyId);
87 int hypId = myHypothesis->GetID();
88 myStudyContext->mapHypothesis[hypId] = myHypothesis;
92 // store hypothesis in SMESHDS document
94 myStudyContext->myDocument->AddHypothesis(myHypothesis);
98 //=============================================================================
102 //=============================================================================
104 SMESH_Mesh* SMESH_Gen::CreateMesh(int studyId)
105 throw(SALOME_Exception)
107 Unexpect aCatch(SalomeException);
108 MESSAGE("SMESH_Gen::CreateMesh");
109 // if (aShape.ShapeType() == TopAbs_COMPOUND)
111 // INFOS("Mesh Compound not yet implemented!");
112 // throw(SALOME_Exception(LOCALIZED("Mesh Compound not yet implemented!")));
115 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
117 StudyContextStruct *myStudyContext = GetStudyContext(studyId);
119 // create a new SMESH_mesh object
121 SMESH_Mesh *mesh = new SMESH_Mesh(_localId++,
124 myStudyContext->myDocument);
125 myStudyContext->mapMesh[_localId] = mesh;
127 // associate a TopoDS_Shape to the mesh
129 //mesh->ShapeToMesh(aShape);
133 //=============================================================================
137 //=============================================================================
139 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
141 MESSAGE("SMESH_Gen::Compute");
142 // bool isDone = false;
144 Algo : s'appuie ou non sur une geometrie
146 Vertex : rien à faire (range le point)
147 Edge, Wire, collection d'edge et wire : 1D
148 Face, Shell, collection de Face et Shells : 2D
149 Solid, Collection de Solid : 3D
151 // *** corriger commentaires
152 // check hypothesis associated to the mesh :
153 // - only one algo : type compatible with the type of the shape
154 // - hypothesis = compatible with algo
155 // - check if hypothesis are applicable to this algo
156 // - check contradictions within hypothesis
157 // (test if enough hypothesis is done further)
161 // if ( !CheckAlgoState( aMesh, aShape ))
163 // INFOS( "ABORT MESHING: some algos or hypothesis are missing");
167 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
169 // -----------------------------------------------------------------
170 // apply algos that do not require descretized boundaries, starting
171 // from the most complex shapes
172 // -----------------------------------------------------------------
174 // map containing all subshapes in the order: vertices, edges, faces...
175 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
176 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
178 SMESH_subMesh* smToCompute = sm;
179 while ( smToCompute )
181 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
182 if ( GetShapeDim( aSubShape ) < 1 ) break;
184 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
185 if (algo && !algo->NeedDescretBoundary()) {
186 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE) {
187 ret = smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
188 } else if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE) {
198 if (revItSub != smMap.rend())
200 smToCompute = (*revItSub).second;
207 // -----------------------------------------------
208 // mesh the rest subshapes starting from vertices
209 // -----------------------------------------------
211 int i, nbSub = smMap.size();
212 map<int, SMESH_subMesh*>::const_iterator itSub = smMap.begin();
213 for ( i = 0; i <= nbSub; ++i ) // loop on the whole map plus <sm>
215 if ( itSub == smMap.end() )
218 smToCompute = (itSub++)->second;
219 if (smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE) {
220 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
224 TopoDS_Shape subShape = smToCompute->GetSubShape();
225 if ( subShape.ShapeType() != TopAbs_VERTEX )
227 if ( !smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE) )
232 TopoDS_Vertex V1 = TopoDS::Vertex(subShape);
233 gp_Pnt P1 = BRep_Tool::Pnt(V1);
234 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
235 SMDS_MeshNode * node = meshDS->AddNode(P1.X(), P1.Y(), P1.Z());
236 if ( node ) { // san - increase robustness
237 meshDS->SetNodeOnVertex(node, V1);
238 smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
243 MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
248 //=======================================================================
249 //function : checkConformIgnoredAlgos
251 //=======================================================================
253 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
254 SMESH_subMesh* aSubMesh,
255 const SMESH_Algo* aGlobIgnoAlgo,
256 const SMESH_Algo* aLocIgnoAlgo,
258 map<int, SMESH_subMesh*>& aCheckedMap)
261 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
267 const list<const SMESHDS_Hypothesis*>& listHyp =
268 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
269 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
270 for ( ; it != listHyp.end(); it++)
272 const SMESHDS_Hypothesis * aHyp = *it;
273 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
276 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
279 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
281 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
282 << aLocIgnoAlgo->GetName() << ">");
286 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
287 int dim = algo->GetDim();
288 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
290 if ( dim < aMaxGlobIgnoDim )
292 // algo is hidden by a global algo
293 INFOS( ( isGlobal ? "Global" : "Local" )
294 << " <" << algo->GetName() << "> is hidden by global <"
295 << aGlobIgnoAlgo->GetName() << ">");
297 else if ( !algo->NeedDescretBoundary() && !isGlobal)
299 // local algo is not hidden and hides algos on sub-shapes
300 if (checkConform && !aSubMesh->IsConform( algo ))
303 checkConform = false; // no more check conformity
304 INFOS( "ERROR: Local <" << algo->GetName() <<
305 "> would produce not conform mesh: "
306 "<Not Conform Mesh Allowed> hypotesis is missing");
309 // sub-algos will be hidden by a local <algo>
310 const map<int, SMESH_subMesh*>& smMap = aSubMesh->DependsOn();
311 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub;
312 bool checkConform2 = false;
313 for ( revItSub = smMap.rbegin(); revItSub != smMap.rend(); revItSub++)
315 checkConformIgnoredAlgos (aMesh, (*revItSub).second, aGlobIgnoAlgo,
316 algo, checkConform2, aCheckedMap);
317 int key = (*revItSub).first;
318 SMESH_subMesh* sm = (*revItSub).second;
319 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
321 aCheckedMap[ key ] = sm;
331 //=======================================================================
332 //function : checkMissing
333 //purpose : notify on missing hypothesis
334 // Return false if algo or hipothesis is missing
335 //=======================================================================
337 static bool checkMissing(SMESH_Gen* aGen,
339 SMESH_subMesh* aSubMesh,
340 const int aTopAlgoDim,
342 const bool checkNoAlgo,
343 map<int, SMESH_subMesh*>& aCheckedMap)
345 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
348 //MESSAGE("=====checkMissing");
351 SMESH_Algo* algo = 0;
353 switch (aSubMesh->GetAlgoState())
355 case SMESH_subMesh::NO_ALGO: {
358 // should there be any algo?
359 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
360 if (aTopAlgoDim > shapeDim)
362 INFOS( "ERROR: " << shapeDim << "D algorithm is missing" );
368 case SMESH_subMesh::MISSING_HYP: {
369 // notify if an algo missing hyp is attached to aSubMesh
370 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
372 bool isGlobalAlgo = aGen->IsGlobalAlgo( algo, aMesh );
373 if (!isGlobalAlgo || !globalChecked[ algo->GetDim() ])
375 INFOS( "ERROR: " << (isGlobalAlgo ? "Global " : "Local ")
376 << "<" << algo->GetName() << "> misses some hypothesis");
378 globalChecked[ algo->GetDim() ] = true;
383 case SMESH_subMesh::HYP_OK:
384 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
390 // do not check under algo that hides sub-algos or
391 // re-start checking NO_ALGO state
393 bool isTopLocalAlgo =
394 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalAlgo( algo, aMesh ));
395 if (!algo->NeedDescretBoundary() || isTopLocalAlgo)
397 bool checkNoAlgo2 = ( algo->NeedDescretBoundary() );
398 const map<int, SMESH_subMesh*>& subMeshes = aSubMesh->DependsOn();
399 map<int, SMESH_subMesh*>::const_iterator itsub;
400 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
402 // sub-meshes should not be checked further more
403 int key = (*itsub).first;
404 SMESH_subMesh* sm = (*itsub).second;
405 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
406 aCheckedMap[ key ] = sm;
410 //check algo on sub-meshes
411 int aTopAlgoDim2 = algo->GetDim();
412 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
413 globalChecked, checkNoAlgo2, aCheckedMap))
416 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
417 checkNoAlgo2 = false;
425 //=======================================================================
426 //function : CheckAlgoState
427 //purpose : notify on bad state of attached algos, return false
428 // if Compute() would fail because of some algo bad state
429 //=======================================================================
431 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
433 //MESSAGE("SMESH_Gen::CheckAlgoState");
436 bool hasAlgo = false;
438 SMESH_subMesh* sm = aMesh.GetSubMesh(aShape);
439 const SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
440 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
446 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
448 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
449 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
450 for ( ; it != listHyp.end(); it++)
452 const SMESHDS_Hypothesis * aHyp = *it;
453 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
456 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
459 int dim = algo->GetDim();
460 aGlobAlgoArr[ dim ] = algo;
465 // --------------------------------------------------------
466 // info on algos that will be ignored because of ones that
467 // don't NeedDescretBoundary() attached to super-shapes,
468 // check that a conform mesh will be produced
469 // --------------------------------------------------------
472 // find a global algo possibly hidding sub-algos
474 const SMESH_Algo* aGlobIgnoAlgo = 0;
475 for (dim = 3; dim > 0; dim--)
477 if (aGlobAlgoArr[ dim ] &&
478 !aGlobAlgoArr[ dim ]->NeedDescretBoundary())
480 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
485 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
486 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
487 map<int, SMESH_subMesh*> aCheckedMap;
488 bool checkConform = ( !aMesh.IsNotConformAllowed() );
490 SMESH_subMesh* smToCheck = sm;
492 // loop on aShape and its sub-shapes
495 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
498 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
499 if (!checkConformIgnoredAlgos (aMesh, smToCheck, aGlobIgnoAlgo,
500 0, checkConform, aCheckedMap))
503 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
507 if (revItSub != smMap.rend())
509 aKey = (*revItSub).first;
510 smToCheck = (*revItSub).second;
520 // ----------------------------------------------------------------
521 // info on missing hypothesis and find out if all needed algos are
523 // ----------------------------------------------------------------
525 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
527 // find max dim of global algo
529 for (dim = 3; dim > 0; dim--)
531 if (aGlobAlgoArr[ dim ])
539 revItSub = smMap.rbegin();
540 bool checkNoAlgo = (bool) aTopAlgoDim;
541 bool globalChecked[] = { false, false, false, false };
543 // loop on aShape and its sub-shapes
546 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
549 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
550 if (!checkMissing (this, aMesh, smToCheck, aTopAlgoDim,
551 globalChecked, checkNoAlgo, aCheckedMap))
554 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
559 if (revItSub != smMap.rend())
561 aKey = (*revItSub).first;
562 smToCheck = (*revItSub).second;
570 INFOS( "None algorithm attached" );
572 return ( ret && hasAlgo );
575 //=======================================================================
576 //function : IsGlobalAlgo
577 //purpose : check if theAlgo is attached to the main shape
578 //=======================================================================
580 bool SMESH_Gen::IsGlobalAlgo(const SMESH_Algo* theAlgo, SMESH_Mesh& aMesh)
582 const SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
583 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
584 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
585 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
586 for ( ; it != listHyp.end(); it++)
587 if ( *it == theAlgo )
594 //=======================================================================
595 //function : getAlgoId
596 //purpose : return algo ID or -1 if not found
597 //=======================================================================
599 static int getAlgo(const list<const SMESHDS_Hypothesis*>& theHypList,
600 const int theAlgoDim,
601 const int theAlgoShapeType)
603 list<const SMESHDS_Hypothesis*>::const_iterator it = theHypList.begin();
608 while (it!=theHypList.end())
610 const SMESH_Hypothesis *anHyp = static_cast< const SMESH_Hypothesis *>( *it );
611 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO &&
612 anHyp->GetDim() == theAlgoDim &&
613 ((anHyp->GetShapeType()) & (1 << theAlgoShapeType)))
616 algo_id = anHyp->GetID();
620 //if (nb_algo > 1) return -1; // more than one algo
627 //=============================================================================
631 //=============================================================================
633 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
635 // MESSAGE("SMESH_Gen::GetAlgo");
637 const SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
638 int dim = GetShapeDim( aShape );
639 int shape_type = aShape.ShapeType();
642 algo_id = getAlgo( meshDS->GetHypothesis( aShape ), dim, shape_type );
647 TopTools_ListIteratorOfListOfShape ancIt( aMesh.GetAncestors( aShape ));
648 for (; ancIt.More(); ancIt.Next())
650 const TopoDS_Shape& ancestor = ancIt.Value();
651 algo_id = getAlgo( meshDS->GetHypothesis( ancestor ), dim, shape_type );
655 if (algo_id < 0) return NULL;
658 ASSERT(_mapAlgo.find(algo_id) != _mapAlgo.end());
660 return _mapAlgo[algo_id];
662 // const SMESHDS_Hypothesis *theHyp = NULL;
663 // SMESH_Algo *algo = NULL;
664 // const SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
669 // // try shape first, then main shape
671 // TopoDS_Shape mainShape = meshDS->ShapeToMesh();
672 // const TopoDS_Shape *shapeToTry[2] = { &aShape, &mainShape };
674 // for (int iShape = 0; iShape < 2; iShape++)
676 // TopoDS_Shape tryShape = (*shapeToTry[iShape]);
678 // const list<const SMESHDS_Hypothesis*>& listHyp =
679 // meshDS->GetHypothesis(tryShape);
680 // list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
683 // int shapeDim = GetShapeDim(aShape);
684 // int typeOfShape = aShape.ShapeType();
686 // while (it!=listHyp.end())
688 // const SMESHDS_Hypothesis *anHyp = *it;
689 // hypType = anHyp->GetType();
690 // //SCRUTE(hypType);
691 // if (hypType > SMESHDS_Hypothesis::PARAM_ALGO)
695 // case SMESHDS_Hypothesis::ALGO_1D:
698 // case SMESHDS_Hypothesis::ALGO_2D:
701 // case SMESHDS_Hypothesis::ALGO_3D:
708 // //SCRUTE(algoDim);
709 // //SCRUTE(shapeDim);
710 // //SCRUTE(typeOfShape);
711 // if (shapeDim == algoDim) // count only algos of shape dim.
712 // { // discard algos for subshapes
713 // hypId = anHyp->GetID(); // (of lower dim.)
714 // ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
715 // SMESH_Algo *anAlgo = _mapAlgo[hypId];
716 // //SCRUTE(anAlgo->GetShapeType());
717 // //if (anAlgo->GetShapeType() == typeOfShape)
718 // if ((anAlgo->GetShapeType()) & (1 << typeOfShape))
719 // { // only specific TopoDS_Shape
725 // if (nb_algo > 1) return NULL; // more than one algo
728 // if (nb_algo == 1) // one algo found : OK
729 // break; // do not try a parent shape
733 // return NULL; // no algo found
735 // hypType = theHyp->GetType();
736 // hypId = theHyp->GetID();
738 // ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
739 // algo = _mapAlgo[hypId];
740 // //MESSAGE("Algo found " << algo->GetName() << " Id " << hypId);
744 //=============================================================================
748 //=============================================================================
750 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
752 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
754 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
756 _mapStudyContext[studyId] = new StudyContextStruct;
757 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
759 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
760 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
761 return myStudyContext;
764 //=============================================================================
768 //=============================================================================
770 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
774 //=============================================================================
778 //=============================================================================
780 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
784 //=============================================================================
788 //=============================================================================
790 void SMESH_Gen::Close(int studyId)
794 //=============================================================================
798 //=============================================================================
800 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
802 int shapeDim = -1; // Shape dimension: 0D, 1D, 2D, 3D
803 int type = aShapeType;//.ShapeType();
806 case TopAbs_COMPOUND:
807 case TopAbs_COMPSOLID:
814 // case TopAbs_SHELL:
835 //=============================================================================
839 //=============================================================================
841 int SMESH_Gen::GetANewId()
843 //MESSAGE("SMESH_Gen::GetANewId");