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");
137 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
139 // -----------------------------------------------------------------
140 // apply algos that do not require descretized boundaries, starting
141 // from the most complex shapes
142 // -----------------------------------------------------------------
144 const bool includeSelf = true;
145 const bool complexShapeFirst = true;
147 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(includeSelf,
149 while ( smIt->more() )
151 SMESH_subMesh* smToCompute = smIt->next();
153 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
154 if ( GetShapeDim( aSubShape ) < 1 ) break;
156 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
157 if (algo && !algo->NeedDescretBoundary())
159 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
160 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
162 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
167 // -----------------------------------------------
168 // mesh the rest subshapes starting from vertices
169 // -----------------------------------------------
170 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
171 while ( smIt->more() )
173 SMESH_subMesh* smToCompute = smIt->next();
175 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
176 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
178 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
182 MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
186 //=======================================================================
187 //function : checkConformIgnoredAlgos
189 //=======================================================================
191 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
192 SMESH_subMesh* aSubMesh,
193 const SMESH_Algo* aGlobIgnoAlgo,
194 const SMESH_Algo* aLocIgnoAlgo,
196 map<int, SMESH_subMesh*>& aCheckedMap,
197 list< SMESH_Gen::TAlgoStateError > & theErrors)
200 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
206 const list<const SMESHDS_Hypothesis*>& listHyp =
207 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
208 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
209 for ( ; it != listHyp.end(); it++)
211 const SMESHDS_Hypothesis * aHyp = *it;
212 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
215 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
218 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
220 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
221 << aLocIgnoAlgo->GetName() << ">");
225 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
226 int dim = algo->GetDim();
227 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
229 if ( dim < aMaxGlobIgnoDim )
231 // algo is hidden by a global algo
232 INFOS( ( isGlobal ? "Global" : "Local" )
233 << " <" << algo->GetName() << "> is hidden by global <"
234 << aGlobIgnoAlgo->GetName() << ">");
236 else if ( !algo->NeedDescretBoundary() && !isGlobal)
238 // local algo is not hidden and hides algos on sub-shapes
239 if (checkConform && !aSubMesh->IsConform( algo ))
242 checkConform = false; // no more check conformity
243 INFOS( "ERROR: Local <" << algo->GetName() <<
244 "> would produce not conform mesh: "
245 "<Not Conform Mesh Allowed> hypotesis is missing");
246 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
247 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
250 // sub-algos will be hidden by a local <algo>
251 const map<int, SMESH_subMesh*>& smMap = aSubMesh->DependsOn();
252 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub;
253 bool checkConform2 = false;
254 for ( revItSub = smMap.rbegin(); revItSub != smMap.rend(); revItSub++)
256 checkConformIgnoredAlgos (aMesh, (*revItSub).second, aGlobIgnoAlgo,
257 algo, checkConform2, aCheckedMap, theErrors);
258 int key = (*revItSub).first;
259 SMESH_subMesh* sm = (*revItSub).second;
260 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
262 aCheckedMap[ key ] = sm;
272 //=======================================================================
273 //function : checkMissing
274 //purpose : notify on missing hypothesis
275 // Return false if algo or hipothesis is missing
276 //=======================================================================
278 static bool checkMissing(SMESH_Gen* aGen,
280 SMESH_subMesh* aSubMesh,
281 const int aTopAlgoDim,
283 const bool checkNoAlgo,
284 map<int, SMESH_subMesh*>& aCheckedMap,
285 list< SMESH_Gen::TAlgoStateError > & theErrors)
287 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
290 //MESSAGE("=====checkMissing");
293 SMESH_Algo* algo = 0;
295 switch (aSubMesh->GetAlgoState())
297 case SMESH_subMesh::NO_ALGO: {
300 // should there be any algo?
301 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
302 if (aTopAlgoDim > shapeDim)
304 INFOS( "ERROR: " << shapeDim << "D algorithm is missing" );
306 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
307 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
312 case SMESH_subMesh::MISSING_HYP: {
313 // notify if an algo missing hyp is attached to aSubMesh
314 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
316 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
317 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
319 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
320 SMESH_Hypothesis::Hypothesis_Status status;
321 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
322 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
323 INFOS( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
324 << "<" << algo->GetName() << "> has a bad parameter value");
326 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
327 INFOS( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
328 << "<" << algo->GetName() << "> assigned to mismatching geometry");
331 INFOS( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
332 << "<" << algo->GetName() << "> misses some hypothesis");
334 if (IsGlobalHypothesis)
335 globalChecked[ algo->GetDim() ] = true;
336 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
337 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
342 case SMESH_subMesh::HYP_OK:
343 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
349 // do not check under algo that hides sub-algos or
350 // re-start checking NO_ALGO state
352 bool isTopLocalAlgo =
353 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
354 if (!algo->NeedDescretBoundary() || isTopLocalAlgo)
356 bool checkNoAlgo2 = ( algo->NeedDescretBoundary() );
357 const map<int, SMESH_subMesh*>& subMeshes = aSubMesh->DependsOn();
358 map<int, SMESH_subMesh*>::const_iterator itsub;
359 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
361 // sub-meshes should not be checked further more
362 int key = (*itsub).first;
363 SMESH_subMesh* sm = (*itsub).second;
364 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
365 aCheckedMap[ key ] = sm;
369 //check algo on sub-meshes
370 int aTopAlgoDim2 = algo->GetDim();
371 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
372 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
375 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
376 checkNoAlgo2 = false;
384 //=======================================================================
385 //function : CheckAlgoState
386 //purpose : notify on bad state of attached algos, return false
387 // if Compute() would fail because of some algo bad state
388 //=======================================================================
390 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
392 list< TAlgoStateError > errors;
393 return GetAlgoState( aMesh, aShape, errors );
396 //=======================================================================
397 //function : GetAlgoState
398 //purpose : notify on bad state of attached algos, return false
399 // if Compute() would fail because of some algo bad state
400 // theErrors list contains problems description
401 //=======================================================================
403 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
404 const TopoDS_Shape& theShape,
405 list< TAlgoStateError > & theErrors)
407 //MESSAGE("SMESH_Gen::CheckAlgoState");
410 bool hasAlgo = false;
412 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
413 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
414 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
420 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
422 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
423 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
424 for ( ; it != listHyp.end(); it++)
426 const SMESHDS_Hypothesis * aHyp = *it;
427 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
430 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
433 int dim = algo->GetDim();
434 aGlobAlgoArr[ dim ] = algo;
439 // --------------------------------------------------------
440 // info on algos that will be ignored because of ones that
441 // don't NeedDescretBoundary() attached to super-shapes,
442 // check that a conform mesh will be produced
443 // --------------------------------------------------------
446 // find a global algo possibly hiding sub-algos
448 const SMESH_Algo* aGlobIgnoAlgo = 0;
449 for (dim = 3; dim > 0; dim--)
451 if (aGlobAlgoArr[ dim ] &&
452 !aGlobAlgoArr[ dim ]->NeedDescretBoundary())
454 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
459 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
460 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
461 map<int, SMESH_subMesh*> aCheckedMap;
462 bool checkConform = ( !theMesh.IsNotConformAllowed() );
464 SMESH_subMesh* smToCheck = sm;
466 // loop on theShape and its sub-shapes
469 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
472 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
473 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
474 0, checkConform, aCheckedMap, theErrors))
477 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
481 if (revItSub != smMap.rend())
483 aKey = (*revItSub).first;
484 smToCheck = (*revItSub).second;
494 // ----------------------------------------------------------------
495 // info on missing hypothesis and find out if all needed algos are
497 // ----------------------------------------------------------------
499 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
501 // find max dim of global algo
503 for (dim = 3; dim > 0; dim--)
505 if (aGlobAlgoArr[ dim ])
513 revItSub = smMap.rbegin();
514 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
515 bool globalChecked[] = { false, false, false, false };
517 // loop on theShape and its sub-shapes
520 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
523 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
524 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
525 globalChecked, checkNoAlgo, aCheckedMap, theErrors))
528 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
533 if (revItSub != smMap.rend())
535 aKey = (*revItSub).first;
536 smToCheck = (*revItSub).second;
545 INFOS( "None algorithm attached" );
546 theErrors.push_back( TAlgoStateError() );
547 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, 1, true );
553 //=======================================================================
554 //function : IsGlobalHypothesis
555 //purpose : check if theAlgo is attached to the main shape
556 //=======================================================================
558 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
560 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
561 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
564 //=============================================================================
568 //=============================================================================
570 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
573 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
574 filter.And( filter.IsApplicableTo( aShape ));
576 list <const SMESHDS_Hypothesis * > algoList;
577 aMesh.GetHypotheses( aShape, filter, algoList, true );
579 if ( algoList.empty() )
582 return const_cast<SMESH_Algo*> ( static_cast<const SMESH_Algo* >( algoList.front() ));
585 //=============================================================================
589 //=============================================================================
591 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
593 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
595 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
597 _mapStudyContext[studyId] = new StudyContextStruct;
598 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
600 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
601 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
602 return myStudyContext;
605 //=============================================================================
609 //=============================================================================
611 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
615 //=============================================================================
619 //=============================================================================
621 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
625 //=============================================================================
629 //=============================================================================
631 void SMESH_Gen::Close(int studyId)
635 //=============================================================================
639 //=============================================================================
641 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
643 static vector<int> dim;
646 dim.resize( TopAbs_SHAPE, -1 );
647 dim[ TopAbs_COMPOUND ] = 3;
648 dim[ TopAbs_COMPSOLID ] = 3;
649 dim[ TopAbs_SOLID ] = 3;
650 dim[ TopAbs_SHELL ] = 3;
651 dim[ TopAbs_FACE ] = 2;
652 dim[ TopAbs_WIRE ] = 1;
653 dim[ TopAbs_EDGE ] = 1;
654 dim[ TopAbs_VERTEX ] = 0;
656 return dim[ aShapeType ];
659 //=============================================================================
663 //=============================================================================
665 int SMESH_Gen::GetANewId()
667 //MESSAGE("SMESH_Gen::GetANewId");