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 "SMESHDS_Document.hxx"
33 #include "SMDS_MeshElement.hxx"
34 #include "SMDS_MeshNode.hxx"
36 #include "utilities.h"
38 #include "Utils_ExceptHandlers.hxx"
41 #include <BRep_Tool.hxx>
42 #include <TopTools_ListOfShape.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 //=============================================================================
49 * default constructor:
51 //=============================================================================
53 SMESH_Gen::SMESH_Gen()
55 MESSAGE("SMESH_Gen::SMESH_Gen");
60 //=============================================================================
64 //=============================================================================
66 SMESH_Gen::~SMESH_Gen()
68 MESSAGE("SMESH_Gen::~SMESH_Gen");
71 //=============================================================================
75 //=============================================================================
77 /*SMESH_Hypothesis *SMESH_Gen::CreateHypothesis(const char *anHyp, int studyId)
78 throw(SALOME_Exception)
81 MESSAGE("CreateHypothesis("<<anHyp<<","<<studyId<<")");
82 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
84 StudyContextStruct *myStudyContext = GetStudyContext(studyId);
86 // create a new hypothesis object, store its ref. in studyContext
88 SMESH_Hypothesis *myHypothesis = _hypothesisFactory.Create(anHyp, studyId);
89 int hypId = myHypothesis->GetID();
90 myStudyContext->mapHypothesis[hypId] = myHypothesis;
94 // store hypothesis in SMESHDS document
96 myStudyContext->myDocument->AddHypothesis(myHypothesis);
100 //=============================================================================
104 //=============================================================================
106 SMESH_Mesh* SMESH_Gen::CreateMesh(int theStudyId, bool theIsEmbeddedMode)
107 throw(SALOME_Exception)
109 Unexpect aCatch(SalomeException);
110 MESSAGE("SMESH_Gen::CreateMesh");
112 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
113 StudyContextStruct *aStudyContext = GetStudyContext(theStudyId);
115 // create a new SMESH_mesh object
116 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
120 aStudyContext->myDocument);
121 aStudyContext->mapMesh[_localId] = aMesh;
126 //=============================================================================
130 //=============================================================================
132 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
134 MESSAGE("SMESH_Gen::Compute");
138 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
140 // -----------------------------------------------------------------
141 // apply algos that do not require descretized boundaries, starting
142 // from the most complex shapes
143 // -----------------------------------------------------------------
145 const bool includeSelf = true;
146 const bool complexShapeFirst = true;
148 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(includeSelf,
150 while ( smIt->more() )
152 SMESH_subMesh* smToCompute = smIt->next();
154 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
155 if ( GetShapeDim( aSubShape ) < 1 ) break;
157 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
158 if (algo && !algo->NeedDescretBoundary())
160 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
161 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
163 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
168 // -----------------------------------------------
169 // mesh the rest subshapes starting from vertices
170 // -----------------------------------------------
171 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
172 while ( smIt->more() )
174 SMESH_subMesh* smToCompute = smIt->next();
176 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
177 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
179 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
183 MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
187 //=======================================================================
188 //function : checkConformIgnoredAlgos
190 //=======================================================================
192 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
193 SMESH_subMesh* aSubMesh,
194 const SMESH_Algo* aGlobIgnoAlgo,
195 const SMESH_Algo* aLocIgnoAlgo,
197 map<int, SMESH_subMesh*>& aCheckedMap,
198 list< SMESH_Gen::TAlgoStateError > & theErrors)
201 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
207 const list<const SMESHDS_Hypothesis*>& listHyp =
208 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
209 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
210 for ( ; it != listHyp.end(); it++)
212 const SMESHDS_Hypothesis * aHyp = *it;
213 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
216 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
219 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
221 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
222 << aLocIgnoAlgo->GetName() << ">");
226 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
227 int dim = algo->GetDim();
228 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
230 if ( dim < aMaxGlobIgnoDim )
232 // algo is hidden by a global algo
233 INFOS( ( isGlobal ? "Global" : "Local" )
234 << " <" << algo->GetName() << "> is hidden by global <"
235 << aGlobIgnoAlgo->GetName() << ">");
237 else if ( !algo->NeedDescretBoundary() && !isGlobal)
239 // local algo is not hidden and hides algos on sub-shapes
240 if (checkConform && !aSubMesh->IsConform( algo ))
243 checkConform = false; // no more check conformity
244 INFOS( "ERROR: Local <" << algo->GetName() <<
245 "> would produce not conform mesh: "
246 "<Not Conform Mesh Allowed> hypotesis is missing");
247 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
248 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
251 // sub-algos will be hidden by a local <algo>
252 const map<int, SMESH_subMesh*>& smMap = aSubMesh->DependsOn();
253 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub;
254 bool checkConform2 = false;
255 for ( revItSub = smMap.rbegin(); revItSub != smMap.rend(); revItSub++)
257 checkConformIgnoredAlgos (aMesh, (*revItSub).second, aGlobIgnoAlgo,
258 algo, checkConform2, aCheckedMap, theErrors);
259 int key = (*revItSub).first;
260 SMESH_subMesh* sm = (*revItSub).second;
261 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
263 aCheckedMap[ key ] = sm;
273 //=======================================================================
274 //function : checkMissing
275 //purpose : notify on missing hypothesis
276 // Return false if algo or hipothesis is missing
277 //=======================================================================
279 static bool checkMissing(SMESH_Gen* aGen,
281 SMESH_subMesh* aSubMesh,
282 const int aTopAlgoDim,
284 const bool checkNoAlgo,
285 map<int, SMESH_subMesh*>& aCheckedMap,
286 list< SMESH_Gen::TAlgoStateError > & theErrors)
288 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
291 //MESSAGE("=====checkMissing");
294 SMESH_Algo* algo = 0;
296 switch (aSubMesh->GetAlgoState())
298 case SMESH_subMesh::NO_ALGO: {
301 // should there be any algo?
302 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
303 if (aTopAlgoDim > shapeDim)
305 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
307 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
308 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
313 case SMESH_subMesh::MISSING_HYP: {
314 // notify if an algo missing hyp is attached to aSubMesh
315 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
317 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
318 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
320 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
321 SMESH_Hypothesis::Hypothesis_Status status;
322 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
323 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
324 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
325 << "<" << algo->GetName() << "> has a bad parameter value");
327 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
328 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
329 << "<" << algo->GetName() << "> assigned to mismatching geometry");
332 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
333 << "<" << algo->GetName() << "> misses some hypothesis");
335 if (IsGlobalHypothesis)
336 globalChecked[ algo->GetDim() ] = true;
337 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
338 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
343 case SMESH_subMesh::HYP_OK:
344 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
350 // do not check under algo that hides sub-algos or
351 // re-start checking NO_ALGO state
353 bool isTopLocalAlgo =
354 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
355 if (!algo->NeedDescretBoundary() || isTopLocalAlgo)
357 bool checkNoAlgo2 = ( algo->NeedDescretBoundary() );
358 const map<int, SMESH_subMesh*>& subMeshes = aSubMesh->DependsOn();
359 map<int, SMESH_subMesh*>::const_iterator itsub;
360 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
362 // sub-meshes should not be checked further more
363 int key = (*itsub).first;
364 SMESH_subMesh* sm = (*itsub).second;
365 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
366 aCheckedMap[ key ] = sm;
370 //check algo on sub-meshes
371 int aTopAlgoDim2 = algo->GetDim();
372 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
373 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
376 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
377 checkNoAlgo2 = false;
385 //=======================================================================
386 //function : CheckAlgoState
387 //purpose : notify on bad state of attached algos, return false
388 // if Compute() would fail because of some algo bad state
389 //=======================================================================
391 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
393 list< TAlgoStateError > errors;
394 return GetAlgoState( aMesh, aShape, errors );
397 //=======================================================================
398 //function : GetAlgoState
399 //purpose : notify on bad state of attached algos, return false
400 // if Compute() would fail because of some algo bad state
401 // theErrors list contains problems description
402 //=======================================================================
404 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
405 const TopoDS_Shape& theShape,
406 list< TAlgoStateError > & theErrors)
408 //MESSAGE("SMESH_Gen::CheckAlgoState");
411 bool hasAlgo = false;
413 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
414 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
415 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
421 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
423 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
424 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
425 for ( ; it != listHyp.end(); it++)
427 const SMESHDS_Hypothesis * aHyp = *it;
428 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
431 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
434 int dim = algo->GetDim();
435 aGlobAlgoArr[ dim ] = algo;
440 // --------------------------------------------------------
441 // info on algos that will be ignored because of ones that
442 // don't NeedDescretBoundary() attached to super-shapes,
443 // check that a conform mesh will be produced
444 // --------------------------------------------------------
447 // find a global algo possibly hiding sub-algos
449 const SMESH_Algo* aGlobIgnoAlgo = 0;
450 for (dim = 3; dim > 0; dim--)
452 if (aGlobAlgoArr[ dim ] &&
453 !aGlobAlgoArr[ dim ]->NeedDescretBoundary())
455 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
460 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
461 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
462 map<int, SMESH_subMesh*> aCheckedMap;
463 bool checkConform = ( !theMesh.IsNotConformAllowed() );
465 SMESH_subMesh* smToCheck = sm;
467 // loop on theShape and its sub-shapes
470 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
473 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
474 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
475 0, checkConform, aCheckedMap, theErrors))
478 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
482 if (revItSub != smMap.rend())
484 aKey = (*revItSub).first;
485 smToCheck = (*revItSub).second;
495 // ----------------------------------------------------------------
496 // info on missing hypothesis and find out if all needed algos are
498 // ----------------------------------------------------------------
500 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
502 // find max dim of global algo
504 for (dim = 3; dim > 0; dim--)
506 if (aGlobAlgoArr[ dim ])
514 revItSub = smMap.rbegin();
515 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
516 bool globalChecked[] = { false, false, false, false };
518 // loop on theShape and its sub-shapes
521 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
524 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
525 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
526 globalChecked, checkNoAlgo, aCheckedMap, theErrors))
529 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
534 if (revItSub != smMap.rend())
536 aKey = (*revItSub).first;
537 smToCheck = (*revItSub).second;
546 INFOS( "None algorithm attached" );
547 theErrors.push_back( TAlgoStateError() );
548 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, 1, true );
554 //=======================================================================
555 //function : IsGlobalHypothesis
556 //purpose : check if theAlgo is attached to the main shape
557 //=======================================================================
559 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
561 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
562 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
565 //=============================================================================
569 //=============================================================================
571 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
574 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
575 filter.And( filter.IsApplicableTo( aShape ));
577 list <const SMESHDS_Hypothesis * > algoList;
578 aMesh.GetHypotheses( aShape, filter, algoList, true );
580 if ( algoList.empty() )
583 return const_cast<SMESH_Algo*> ( static_cast<const SMESH_Algo* >( algoList.front() ));
586 //=============================================================================
590 //=============================================================================
592 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
594 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
596 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
598 _mapStudyContext[studyId] = new StudyContextStruct;
599 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
601 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
602 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
603 return myStudyContext;
606 //=============================================================================
610 //=============================================================================
612 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
616 //=============================================================================
620 //=============================================================================
622 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
626 //=============================================================================
630 //=============================================================================
632 void SMESH_Gen::Close(int studyId)
636 //=============================================================================
640 //=============================================================================
642 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
644 static vector<int> dim;
647 dim.resize( TopAbs_SHAPE, -1 );
648 dim[ TopAbs_COMPOUND ] = 3;
649 dim[ TopAbs_COMPSOLID ] = 3;
650 dim[ TopAbs_SOLID ] = 3;
651 dim[ TopAbs_SHELL ] = 3;
652 dim[ TopAbs_FACE ] = 2;
653 dim[ TopAbs_WIRE ] = 1;
654 dim[ TopAbs_EDGE ] = 1;
655 dim[ TopAbs_VERTEX ] = 0;
657 return dim[ aShapeType ];
660 //=============================================================================
664 //=============================================================================
666 int SMESH_Gen::GetANewId()
668 //MESSAGE("SMESH_Gen::GetANewId");