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_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 theStudyId, bool theIsEmbeddedMode)
106 throw(SALOME_Exception)
108 Unexpect aCatch(SalomeException);
109 MESSAGE("SMESH_Gen::CreateMesh");
111 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
112 StudyContextStruct *aStudyContext = GetStudyContext(theStudyId);
114 // create a new SMESH_mesh object
115 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
119 aStudyContext->myDocument);
120 aStudyContext->mapMesh[_localId] = aMesh;
125 //=============================================================================
129 //=============================================================================
131 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
133 MESSAGE("SMESH_Gen::Compute");
134 // bool isDone = false;
136 Algo : s'appuie ou non sur une geometrie
138 Vertex : rien à faire (range le point)
139 Edge, Wire, collection d'edge et wire : 1D
140 Face, Shell, collection de Face et Shells : 2D
141 Solid, Collection de Solid : 3D
143 // *** corriger commentaires
144 // check hypothesis associated to the mesh :
145 // - only one algo : type compatible with the type of the shape
146 // - hypothesis = compatible with algo
147 // - check if hypothesis are applicable to this algo
148 // - check contradictions within hypothesis
149 // (test if enough hypothesis is done further)
153 // if ( !CheckAlgoState( aMesh, aShape ))
155 // INFOS( "ABORT MESHING: some algos or hypothesis are missing");
159 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
161 if ( sm->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
162 return true; // already computed
164 // -----------------------------------------------------------------
165 // apply algos that do not require descretized boundaries, starting
166 // from the most complex shapes
167 // -----------------------------------------------------------------
169 // map containing all subshapes in the order: vertices, edges, faces...
170 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
171 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
173 SMESH_subMesh* smToCompute = sm;
174 while ( smToCompute )
176 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
177 if ( GetShapeDim( aSubShape ) < 1 ) break;
179 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
180 if (algo && !algo->NeedDescretBoundary()) {
181 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE) {
182 ret = smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
183 } else if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE) {
193 if (revItSub != smMap.rend())
195 smToCompute = (*revItSub).second;
202 // -----------------------------------------------
203 // mesh the rest subshapes starting from vertices
204 // -----------------------------------------------
206 int i, nbSub = smMap.size();
207 map<int, SMESH_subMesh*>::const_iterator itSub = smMap.begin();
208 for ( i = 0; i <= nbSub; ++i ) // loop on the whole map plus <sm>
210 if ( itSub == smMap.end() )
213 smToCompute = (itSub++)->second;
214 if (smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE) {
215 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
219 TopoDS_Shape subShape = smToCompute->GetSubShape();
220 if ( subShape.ShapeType() != TopAbs_VERTEX )
222 if ( !smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE) )
227 TopoDS_Vertex V1 = TopoDS::Vertex(subShape);
228 gp_Pnt P1 = BRep_Tool::Pnt(V1);
229 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
230 SMDS_MeshNode * node = meshDS->AddNode(P1.X(), P1.Y(), P1.Z());
231 if ( node ) { // san - increase robustness
232 meshDS->SetNodeOnVertex(node, V1);
233 smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
238 MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
243 //=======================================================================
244 //function : checkConformIgnoredAlgos
246 //=======================================================================
248 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
249 SMESH_subMesh* aSubMesh,
250 const SMESH_Algo* aGlobIgnoAlgo,
251 const SMESH_Algo* aLocIgnoAlgo,
253 map<int, SMESH_subMesh*>& aCheckedMap,
254 list< SMESH_Gen::TAlgoStateError > & theErrors)
257 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
263 const list<const SMESHDS_Hypothesis*>& listHyp =
264 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
265 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
266 for ( ; it != listHyp.end(); it++)
268 const SMESHDS_Hypothesis * aHyp = *it;
269 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
272 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
275 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
277 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
278 << aLocIgnoAlgo->GetName() << ">");
282 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
283 int dim = algo->GetDim();
284 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
286 if ( dim < aMaxGlobIgnoDim )
288 // algo is hidden by a global algo
289 INFOS( ( isGlobal ? "Global" : "Local" )
290 << " <" << algo->GetName() << "> is hidden by global <"
291 << aGlobIgnoAlgo->GetName() << ">");
293 else if ( !algo->NeedDescretBoundary() && !isGlobal)
295 // local algo is not hidden and hides algos on sub-shapes
296 if (checkConform && !aSubMesh->IsConform( algo ))
299 checkConform = false; // no more check conformity
300 INFOS( "ERROR: Local <" << algo->GetName() <<
301 "> would produce not conform mesh: "
302 "<Not Conform Mesh Allowed> hypotesis is missing");
303 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
304 theErrors.back().Set( SMESH_Gen::NOT_CONFORM_MESH, algo, false );
307 // sub-algos will be hidden by a local <algo>
308 const map<int, SMESH_subMesh*>& smMap = aSubMesh->DependsOn();
309 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub;
310 bool checkConform2 = false;
311 for ( revItSub = smMap.rbegin(); revItSub != smMap.rend(); revItSub++)
313 checkConformIgnoredAlgos (aMesh, (*revItSub).second, aGlobIgnoAlgo,
314 algo, checkConform2, aCheckedMap, theErrors);
315 int key = (*revItSub).first;
316 SMESH_subMesh* sm = (*revItSub).second;
317 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
319 aCheckedMap[ key ] = sm;
329 //=======================================================================
330 //function : checkMissing
331 //purpose : notify on missing hypothesis
332 // Return false if algo or hipothesis is missing
333 //=======================================================================
335 static bool checkMissing(SMESH_Gen* aGen,
337 SMESH_subMesh* aSubMesh,
338 const int aTopAlgoDim,
340 const bool checkNoAlgo,
341 map<int, SMESH_subMesh*>& aCheckedMap,
342 list< SMESH_Gen::TAlgoStateError > & theErrors)
344 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
347 //MESSAGE("=====checkMissing");
350 SMESH_Algo* algo = 0;
352 switch (aSubMesh->GetAlgoState())
354 case SMESH_subMesh::NO_ALGO: {
357 // should there be any algo?
358 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
359 if (aTopAlgoDim > shapeDim)
361 INFOS( "ERROR: " << shapeDim << "D algorithm is missing" );
363 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
364 theErrors.back().Set( SMESH_Gen::MISSING_ALGO, shapeDim, true );
369 case SMESH_subMesh::MISSING_HYP: {
370 // notify if an algo missing hyp is attached to aSubMesh
371 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
373 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
374 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
376 SMESH_Gen::TAlgoStateErrorName errName = SMESH_Gen::MISSING_HYPO;
377 SMESH_Hypothesis::Hypothesis_Status status;
378 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
379 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
380 INFOS( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
381 << "<" << algo->GetName() << "> has a bad parameter value");
382 errName = SMESH_Gen::BAD_PARAM_VALUE;
384 INFOS( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
385 << "<" << algo->GetName() << "> misses some hypothesis");
387 if (IsGlobalHypothesis)
388 globalChecked[ algo->GetDim() ] = true;
389 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
390 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
395 case SMESH_subMesh::HYP_OK:
396 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
402 // do not check under algo that hides sub-algos or
403 // re-start checking NO_ALGO state
405 bool isTopLocalAlgo =
406 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
407 if (!algo->NeedDescretBoundary() || isTopLocalAlgo)
409 bool checkNoAlgo2 = ( algo->NeedDescretBoundary() );
410 const map<int, SMESH_subMesh*>& subMeshes = aSubMesh->DependsOn();
411 map<int, SMESH_subMesh*>::const_iterator itsub;
412 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
414 // sub-meshes should not be checked further more
415 int key = (*itsub).first;
416 SMESH_subMesh* sm = (*itsub).second;
417 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
418 aCheckedMap[ key ] = sm;
422 //check algo on sub-meshes
423 int aTopAlgoDim2 = algo->GetDim();
424 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
425 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
428 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
429 checkNoAlgo2 = false;
437 //=======================================================================
438 //function : CheckAlgoState
439 //purpose : notify on bad state of attached algos, return false
440 // if Compute() would fail because of some algo bad state
441 //=======================================================================
443 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
445 list< TAlgoStateError > errors;
446 return GetAlgoState( aMesh, aShape, errors );
449 //=======================================================================
450 //function : GetAlgoState
451 //purpose : notify on bad state of attached algos, return false
452 // if Compute() would fail because of some algo bad state
453 // theErrors list contains problems description
454 //=======================================================================
456 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
457 const TopoDS_Shape& theShape,
458 list< TAlgoStateError > & theErrors)
460 //MESSAGE("SMESH_Gen::CheckAlgoState");
463 bool hasAlgo = false;
465 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
466 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
467 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
473 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
475 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
476 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
477 for ( ; it != listHyp.end(); it++)
479 const SMESHDS_Hypothesis * aHyp = *it;
480 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
483 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
486 int dim = algo->GetDim();
487 aGlobAlgoArr[ dim ] = algo;
492 // --------------------------------------------------------
493 // info on algos that will be ignored because of ones that
494 // don't NeedDescretBoundary() attached to super-shapes,
495 // check that a conform mesh will be produced
496 // --------------------------------------------------------
499 // find a global algo possibly hidding sub-algos
501 const SMESH_Algo* aGlobIgnoAlgo = 0;
502 for (dim = 3; dim > 0; dim--)
504 if (aGlobAlgoArr[ dim ] &&
505 !aGlobAlgoArr[ dim ]->NeedDescretBoundary())
507 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
512 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
513 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
514 map<int, SMESH_subMesh*> aCheckedMap;
515 bool checkConform = ( !theMesh.IsNotConformAllowed() );
517 SMESH_subMesh* smToCheck = sm;
519 // loop on theShape and its sub-shapes
522 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
525 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
526 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
527 0, checkConform, aCheckedMap, theErrors))
530 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
534 if (revItSub != smMap.rend())
536 aKey = (*revItSub).first;
537 smToCheck = (*revItSub).second;
547 // ----------------------------------------------------------------
548 // info on missing hypothesis and find out if all needed algos are
550 // ----------------------------------------------------------------
552 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
554 // find max dim of global algo
556 for (dim = 3; dim > 0; dim--)
558 if (aGlobAlgoArr[ dim ])
566 revItSub = smMap.rbegin();
567 bool checkNoAlgo = (bool) aTopAlgoDim;
568 bool globalChecked[] = { false, false, false, false };
570 // loop on theShape and its sub-shapes
573 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
576 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
577 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
578 globalChecked, checkNoAlgo, aCheckedMap, theErrors))
581 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
586 if (revItSub != smMap.rend())
588 aKey = (*revItSub).first;
589 smToCheck = (*revItSub).second;
598 INFOS( "None algorithm attached" );
599 theErrors.push_back( TAlgoStateError() );
600 theErrors.back().Set( MISSING_ALGO, 1, true );
606 //=======================================================================
607 //function : IsGlobalHypothesis
608 //purpose : check if theAlgo is attached to the main shape
609 //=======================================================================
611 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
613 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
614 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
617 //=============================================================================
621 //=============================================================================
623 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
626 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
627 filter.And( filter.IsApplicableTo( aShape ));
629 list <const SMESHDS_Hypothesis * > algoList;
630 aMesh.GetHypotheses( aShape, filter, algoList, true );
632 if ( algoList.empty() )
635 return const_cast<SMESH_Algo*> ( static_cast<const SMESH_Algo* >( algoList.front() ));
638 //=============================================================================
642 //=============================================================================
644 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
646 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
648 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
650 _mapStudyContext[studyId] = new StudyContextStruct;
651 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
653 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
654 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
655 return myStudyContext;
658 //=============================================================================
662 //=============================================================================
664 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
668 //=============================================================================
672 //=============================================================================
674 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
678 //=============================================================================
682 //=============================================================================
684 void SMESH_Gen::Close(int studyId)
688 //=============================================================================
692 //=============================================================================
694 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
696 static vector<int> dim;
699 dim.resize( TopAbs_SHAPE, -1 );
700 dim[ TopAbs_COMPOUND ] = 3;
701 dim[ TopAbs_COMPSOLID ] = 3;
702 dim[ TopAbs_SOLID ] = 3;
703 dim[ TopAbs_SHELL ] = 3;
704 dim[ TopAbs_FACE ] = 2;
705 dim[ TopAbs_WIRE ] = 1;
706 dim[ TopAbs_EDGE ] = 1;
707 dim[ TopAbs_VERTEX ] = 0;
709 return dim[ aShapeType ];
712 //=============================================================================
716 //=============================================================================
718 int SMESH_Gen::GetANewId()
720 //MESSAGE("SMESH_Gen::GetANewId");