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 "SMESH_HypoFilter.hxx"
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_MeshNode.hxx"
35 #include "utilities.h"
37 #include "Utils_ExceptHandlers.hxx"
40 #include <BRep_Tool.hxx>
41 #include <TopTools_ListOfShape.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 //=============================================================================
48 * default constructor:
50 //=============================================================================
52 SMESH_Gen::SMESH_Gen()
54 MESSAGE("SMESH_Gen::SMESH_Gen");
59 //=============================================================================
63 //=============================================================================
65 SMESH_Gen::~SMESH_Gen()
67 MESSAGE("SMESH_Gen::~SMESH_Gen");
70 //=============================================================================
74 //=============================================================================
76 /*SMESH_Hypothesis *SMESH_Gen::CreateHypothesis(const char *anHyp, int studyId)
77 throw(SALOME_Exception)
80 MESSAGE("CreateHypothesis("<<anHyp<<","<<studyId<<")");
81 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
83 StudyContextStruct *myStudyContext = GetStudyContext(studyId);
85 // create a new hypothesis object, store its ref. in studyContext
87 SMESH_Hypothesis *myHypothesis = _hypothesisFactory.Create(anHyp, studyId);
88 int hypId = myHypothesis->GetID();
89 myStudyContext->mapHypothesis[hypId] = myHypothesis;
93 // store hypothesis in SMESHDS document
95 myStudyContext->myDocument->AddHypothesis(myHypothesis);
99 //=============================================================================
103 //=============================================================================
105 SMESH_Mesh* SMESH_Gen::CreateMesh(int studyId)
106 throw(SALOME_Exception)
108 Unexpect aCatch(SalomeException);
109 MESSAGE("SMESH_Gen::CreateMesh");
110 // if (aShape.ShapeType() == TopAbs_COMPOUND)
112 // INFOS("Mesh Compound not yet implemented!");
113 // throw(SALOME_Exception(LOCALIZED("Mesh Compound not yet implemented!")));
116 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
118 StudyContextStruct *myStudyContext = GetStudyContext(studyId);
120 // create a new SMESH_mesh object
122 SMESH_Mesh *mesh = new SMESH_Mesh(_localId++,
125 myStudyContext->myDocument);
126 myStudyContext->mapMesh[_localId] = mesh;
128 // associate a TopoDS_Shape to the mesh
130 //mesh->ShapeToMesh(aShape);
134 //=============================================================================
138 //=============================================================================
140 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
142 MESSAGE("SMESH_Gen::Compute");
143 // bool isDone = false;
145 Algo : s'appuie ou non sur une geometrie
147 Vertex : rien à faire (range le point)
148 Edge, Wire, collection d'edge et wire : 1D
149 Face, Shell, collection de Face et Shells : 2D
150 Solid, Collection de Solid : 3D
152 // *** corriger commentaires
153 // check hypothesis associated to the mesh :
154 // - only one algo : type compatible with the type of the shape
155 // - hypothesis = compatible with algo
156 // - check if hypothesis are applicable to this algo
157 // - check contradictions within hypothesis
158 // (test if enough hypothesis is done further)
162 // if ( !CheckAlgoState( aMesh, aShape ))
164 // INFOS( "ABORT MESHING: some algos or hypothesis are missing");
168 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
170 if ( sm->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
171 return true; // already computed
173 // -----------------------------------------------------------------
174 // apply algos that do not require descretized boundaries, starting
175 // from the most complex shapes
176 // -----------------------------------------------------------------
178 // map containing all subshapes in the order: vertices, edges, faces...
179 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
180 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
182 SMESH_subMesh* smToCompute = sm;
183 while ( smToCompute )
185 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
186 if ( GetShapeDim( aSubShape ) < 1 ) break;
188 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
189 if (algo && !algo->NeedDescretBoundary()) {
190 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE) {
191 ret = smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
192 } else if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE) {
202 if (revItSub != smMap.rend())
204 smToCompute = (*revItSub).second;
211 // -----------------------------------------------
212 // mesh the rest subshapes starting from vertices
213 // -----------------------------------------------
215 int i, nbSub = smMap.size();
216 map<int, SMESH_subMesh*>::const_iterator itSub = smMap.begin();
217 for ( i = 0; i <= nbSub; ++i ) // loop on the whole map plus <sm>
219 if ( itSub == smMap.end() )
222 smToCompute = (itSub++)->second;
223 if (smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE) {
224 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
228 TopoDS_Shape subShape = smToCompute->GetSubShape();
229 if ( subShape.ShapeType() != TopAbs_VERTEX )
231 if ( !smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE) )
236 TopoDS_Vertex V1 = TopoDS::Vertex(subShape);
237 gp_Pnt P1 = BRep_Tool::Pnt(V1);
238 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
239 SMDS_MeshNode * node = meshDS->AddNode(P1.X(), P1.Y(), P1.Z());
240 if ( node ) { // san - increase robustness
241 meshDS->SetNodeOnVertex(node, V1);
242 smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
247 MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
252 //=======================================================================
253 //function : checkConformIgnoredAlgos
255 //=======================================================================
257 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
258 SMESH_subMesh* aSubMesh,
259 const SMESH_Algo* aGlobIgnoAlgo,
260 const SMESH_Algo* aLocIgnoAlgo,
262 map<int, SMESH_subMesh*>& aCheckedMap)
265 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
271 const list<const SMESHDS_Hypothesis*>& listHyp =
272 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
273 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
274 for ( ; it != listHyp.end(); it++)
276 const SMESHDS_Hypothesis * aHyp = *it;
277 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
280 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
283 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
285 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
286 << aLocIgnoAlgo->GetName() << ">");
290 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
291 int dim = algo->GetDim();
292 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
294 if ( dim < aMaxGlobIgnoDim )
296 // algo is hidden by a global algo
297 INFOS( ( isGlobal ? "Global" : "Local" )
298 << " <" << algo->GetName() << "> is hidden by global <"
299 << aGlobIgnoAlgo->GetName() << ">");
301 else if ( !algo->NeedDescretBoundary() && !isGlobal)
303 // local algo is not hidden and hides algos on sub-shapes
304 if (checkConform && !aSubMesh->IsConform( algo ))
307 checkConform = false; // no more check conformity
308 INFOS( "ERROR: Local <" << algo->GetName() <<
309 "> would produce not conform mesh: "
310 "<Not Conform Mesh Allowed> hypotesis is missing");
313 // sub-algos will be hidden by a local <algo>
314 const map<int, SMESH_subMesh*>& smMap = aSubMesh->DependsOn();
315 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub;
316 bool checkConform2 = false;
317 for ( revItSub = smMap.rbegin(); revItSub != smMap.rend(); revItSub++)
319 checkConformIgnoredAlgos (aMesh, (*revItSub).second, aGlobIgnoAlgo,
320 algo, checkConform2, aCheckedMap);
321 int key = (*revItSub).first;
322 SMESH_subMesh* sm = (*revItSub).second;
323 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
325 aCheckedMap[ key ] = sm;
335 //=======================================================================
336 //function : checkMissing
337 //purpose : notify on missing hypothesis
338 // Return false if algo or hipothesis is missing
339 //=======================================================================
341 static bool checkMissing(SMESH_Gen* aGen,
343 SMESH_subMesh* aSubMesh,
344 const int aTopAlgoDim,
346 const bool checkNoAlgo,
347 map<int, SMESH_subMesh*>& aCheckedMap)
349 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
352 //MESSAGE("=====checkMissing");
355 SMESH_Algo* algo = 0;
357 switch (aSubMesh->GetAlgoState())
359 case SMESH_subMesh::NO_ALGO: {
362 // should there be any algo?
363 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
364 if (aTopAlgoDim > shapeDim)
366 INFOS( "ERROR: " << shapeDim << "D algorithm is missing" );
372 case SMESH_subMesh::MISSING_HYP: {
373 // notify if an algo missing hyp is attached to aSubMesh
374 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
376 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
377 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
379 INFOS( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
380 << "<" << algo->GetName() << "> misses some hypothesis");
381 if (IsGlobalHypothesis)
382 globalChecked[ algo->GetDim() ] = true;
387 case SMESH_subMesh::HYP_OK:
388 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
394 // do not check under algo that hides sub-algos or
395 // re-start checking NO_ALGO state
397 bool isTopLocalAlgo =
398 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
399 if (!algo->NeedDescretBoundary() || isTopLocalAlgo)
401 bool checkNoAlgo2 = ( algo->NeedDescretBoundary() );
402 const map<int, SMESH_subMesh*>& subMeshes = aSubMesh->DependsOn();
403 map<int, SMESH_subMesh*>::const_iterator itsub;
404 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
406 // sub-meshes should not be checked further more
407 int key = (*itsub).first;
408 SMESH_subMesh* sm = (*itsub).second;
409 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
410 aCheckedMap[ key ] = sm;
414 //check algo on sub-meshes
415 int aTopAlgoDim2 = algo->GetDim();
416 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
417 globalChecked, checkNoAlgo2, aCheckedMap))
420 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
421 checkNoAlgo2 = false;
429 //=======================================================================
430 //function : CheckAlgoState
431 //purpose : notify on bad state of attached algos, return false
432 // if Compute() would fail because of some algo bad state
433 //=======================================================================
435 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
437 //MESSAGE("SMESH_Gen::CheckAlgoState");
440 bool hasAlgo = false;
442 SMESH_subMesh* sm = aMesh.GetSubMesh(aShape);
443 const SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
444 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
450 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
452 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
453 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
454 for ( ; it != listHyp.end(); it++)
456 const SMESHDS_Hypothesis * aHyp = *it;
457 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
460 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
463 int dim = algo->GetDim();
464 aGlobAlgoArr[ dim ] = algo;
469 // --------------------------------------------------------
470 // info on algos that will be ignored because of ones that
471 // don't NeedDescretBoundary() attached to super-shapes,
472 // check that a conform mesh will be produced
473 // --------------------------------------------------------
476 // find a global algo possibly hidding sub-algos
478 const SMESH_Algo* aGlobIgnoAlgo = 0;
479 for (dim = 3; dim > 0; dim--)
481 if (aGlobAlgoArr[ dim ] &&
482 !aGlobAlgoArr[ dim ]->NeedDescretBoundary())
484 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
489 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
490 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
491 map<int, SMESH_subMesh*> aCheckedMap;
492 bool checkConform = ( !aMesh.IsNotConformAllowed() );
494 SMESH_subMesh* smToCheck = sm;
496 // loop on aShape and its sub-shapes
499 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
502 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
503 if (!checkConformIgnoredAlgos (aMesh, smToCheck, aGlobIgnoAlgo,
504 0, checkConform, aCheckedMap))
507 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
511 if (revItSub != smMap.rend())
513 aKey = (*revItSub).first;
514 smToCheck = (*revItSub).second;
524 // ----------------------------------------------------------------
525 // info on missing hypothesis and find out if all needed algos are
527 // ----------------------------------------------------------------
529 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
531 // find max dim of global algo
533 for (dim = 3; dim > 0; dim--)
535 if (aGlobAlgoArr[ dim ])
543 revItSub = smMap.rbegin();
544 bool checkNoAlgo = (bool) aTopAlgoDim;
545 bool globalChecked[] = { false, false, false, false };
547 // loop on aShape and its sub-shapes
550 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
553 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
554 if (!checkMissing (this, aMesh, smToCheck, aTopAlgoDim,
555 globalChecked, checkNoAlgo, aCheckedMap))
558 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
563 if (revItSub != smMap.rend())
565 aKey = (*revItSub).first;
566 smToCheck = (*revItSub).second;
574 INFOS( "None algorithm attached" );
576 return ( ret && hasAlgo );
579 //=======================================================================
580 //function : IsGlobalHypothesis
581 //purpose : check if theAlgo is attached to the main shape
582 //=======================================================================
584 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
586 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
587 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
590 //=============================================================================
594 //=============================================================================
596 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
598 // MESSAGE("SMESH_Gen::GetAlgo");
600 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
601 filter.And( filter.IsApplicableTo( aShape ));
603 list <const SMESHDS_Hypothesis * > algoList;
604 aMesh.GetHypotheses( aShape, filter, algoList, true );
605 if (algoList.size() != 1 )
608 return const_cast<SMESH_Algo*> ( static_cast<const SMESH_Algo* >( algoList.front() ));
611 //=============================================================================
615 //=============================================================================
617 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
619 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
621 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
623 _mapStudyContext[studyId] = new StudyContextStruct;
624 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
626 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
627 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
628 return myStudyContext;
631 //=============================================================================
635 //=============================================================================
637 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
641 //=============================================================================
645 //=============================================================================
647 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
651 //=============================================================================
655 //=============================================================================
657 void SMESH_Gen::Close(int studyId)
661 //=============================================================================
665 //=============================================================================
667 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
669 int shapeDim = -1; // Shape dimension: 0D, 1D, 2D, 3D
670 int type = aShapeType;//.ShapeType();
673 case TopAbs_COMPOUND:
674 case TopAbs_COMPSOLID:
681 // case TopAbs_SHELL:
702 //=============================================================================
706 //=============================================================================
708 int SMESH_Gen::GetANewId()
710 //MESSAGE("SMESH_Gen::GetANewId");