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)
166 if ((algo && !aMesh.HasShapeToMesh()))
168 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
169 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
171 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
176 // -----------------------------------------------
177 // mesh the rest subshapes starting from vertices
178 // -----------------------------------------------
179 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
180 while ( smIt->more() )
182 SMESH_subMesh* smToCompute = smIt->next();
184 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
185 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
187 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
191 MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
195 //=======================================================================
196 //function : checkConformIgnoredAlgos
198 //=======================================================================
200 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
201 SMESH_subMesh* aSubMesh,
202 const SMESH_Algo* aGlobIgnoAlgo,
203 const SMESH_Algo* aLocIgnoAlgo,
205 map<int, SMESH_subMesh*>& aCheckedMap,
206 list< SMESH_Gen::TAlgoStateError > & theErrors)
209 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
215 const list<const SMESHDS_Hypothesis*>& listHyp =
216 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
217 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
218 for ( ; it != listHyp.end(); it++)
220 const SMESHDS_Hypothesis * aHyp = *it;
221 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
224 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
227 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
229 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
230 << aLocIgnoAlgo->GetName() << ">");
234 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
235 int dim = algo->GetDim();
236 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
238 if ( dim < aMaxGlobIgnoDim )
240 // algo is hidden by a global algo
241 INFOS( ( isGlobal ? "Global" : "Local" )
242 << " <" << algo->GetName() << "> is hidden by global <"
243 << aGlobIgnoAlgo->GetName() << ">");
245 else if ( !algo->NeedDescretBoundary() && !isGlobal)
247 // local algo is not hidden and hides algos on sub-shapes
248 if (checkConform && !aSubMesh->IsConform( algo ))
251 checkConform = false; // no more check conformity
252 INFOS( "ERROR: Local <" << algo->GetName() <<
253 "> would produce not conform mesh: "
254 "<Not Conform Mesh Allowed> hypotesis is missing");
255 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
256 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
259 // sub-algos will be hidden by a local <algo>
260 const map<int, SMESH_subMesh*>& smMap = aSubMesh->DependsOn();
261 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub;
262 bool checkConform2 = false;
263 for ( revItSub = smMap.rbegin(); revItSub != smMap.rend(); revItSub++)
265 checkConformIgnoredAlgos (aMesh, (*revItSub).second, aGlobIgnoAlgo,
266 algo, checkConform2, aCheckedMap, theErrors);
267 int key = (*revItSub).first;
268 SMESH_subMesh* sm = (*revItSub).second;
269 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
271 aCheckedMap[ key ] = sm;
281 //=======================================================================
282 //function : checkMissing
283 //purpose : notify on missing hypothesis
284 // Return false if algo or hipothesis is missing
285 //=======================================================================
287 static bool checkMissing(SMESH_Gen* aGen,
289 SMESH_subMesh* aSubMesh,
290 const int aTopAlgoDim,
292 const bool checkNoAlgo,
293 map<int, SMESH_subMesh*>& aCheckedMap,
294 list< SMESH_Gen::TAlgoStateError > & theErrors)
296 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
299 //MESSAGE("=====checkMissing");
302 SMESH_Algo* algo = 0;
304 switch (aSubMesh->GetAlgoState())
306 case SMESH_subMesh::NO_ALGO: {
309 // should there be any algo?
310 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
311 if (aTopAlgoDim > shapeDim)
313 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
315 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
316 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
321 case SMESH_subMesh::MISSING_HYP: {
322 // notify if an algo missing hyp is attached to aSubMesh
323 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
325 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
326 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
328 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
329 SMESH_Hypothesis::Hypothesis_Status status;
330 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
331 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
332 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
333 << "<" << algo->GetName() << "> has a bad parameter value");
335 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
336 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
337 << "<" << algo->GetName() << "> assigned to mismatching geometry");
340 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
341 << "<" << algo->GetName() << "> misses some hypothesis");
343 if (IsGlobalHypothesis)
344 globalChecked[ algo->GetDim() ] = true;
345 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
346 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
351 case SMESH_subMesh::HYP_OK:
352 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
358 // do not check under algo that hides sub-algos or
359 // re-start checking NO_ALGO state
361 bool isTopLocalAlgo =
362 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
363 if (!algo->NeedDescretBoundary() || isTopLocalAlgo)
365 bool checkNoAlgo2 = ( algo->NeedDescretBoundary() );
366 const map<int, SMESH_subMesh*>& subMeshes = aSubMesh->DependsOn();
367 map<int, SMESH_subMesh*>::const_iterator itsub;
368 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
370 // sub-meshes should not be checked further more
371 int key = (*itsub).first;
372 SMESH_subMesh* sm = (*itsub).second;
373 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
374 aCheckedMap[ key ] = sm;
378 //check algo on sub-meshes
379 int aTopAlgoDim2 = algo->GetDim();
380 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
381 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
384 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
385 checkNoAlgo2 = false;
393 //=======================================================================
394 //function : CheckAlgoState
395 //purpose : notify on bad state of attached algos, return false
396 // if Compute() would fail because of some algo bad state
397 //=======================================================================
399 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
401 list< TAlgoStateError > errors;
402 return GetAlgoState( aMesh, aShape, errors );
405 //=======================================================================
406 //function : GetAlgoState
407 //purpose : notify on bad state of attached algos, return false
408 // if Compute() would fail because of some algo bad state
409 // theErrors list contains problems description
410 //=======================================================================
412 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
413 const TopoDS_Shape& theShape,
414 list< TAlgoStateError > & theErrors)
416 //MESSAGE("SMESH_Gen::CheckAlgoState");
419 bool hasAlgo = false;
421 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
422 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
423 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
429 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
431 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
432 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
433 for ( ; it != listHyp.end(); it++)
435 const SMESHDS_Hypothesis * aHyp = *it;
436 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
439 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
442 int dim = algo->GetDim();
443 aGlobAlgoArr[ dim ] = algo;
448 // --------------------------------------------------------
449 // info on algos that will be ignored because of ones that
450 // don't NeedDescretBoundary() attached to super-shapes,
451 // check that a conform mesh will be produced
452 // --------------------------------------------------------
455 // find a global algo possibly hiding sub-algos
457 const SMESH_Algo* aGlobIgnoAlgo = 0;
458 for (dim = 3; dim > 0; dim--)
460 if (aGlobAlgoArr[ dim ] &&
461 !aGlobAlgoArr[ dim ]->NeedDescretBoundary())
463 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
468 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
469 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
470 map<int, SMESH_subMesh*> aCheckedMap;
471 bool checkConform = ( !theMesh.IsNotConformAllowed() );
473 SMESH_subMesh* smToCheck = sm;
475 // loop on theShape and its sub-shapes
478 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
481 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
482 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
483 0, checkConform, aCheckedMap, theErrors))
486 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
490 if (revItSub != smMap.rend())
492 aKey = (*revItSub).first;
493 smToCheck = (*revItSub).second;
503 // ----------------------------------------------------------------
504 // info on missing hypothesis and find out if all needed algos are
506 // ----------------------------------------------------------------
508 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
510 // find max dim of global algo
512 for (dim = 3; dim > 0; dim--)
514 if (aGlobAlgoArr[ dim ])
522 revItSub = smMap.rbegin();
523 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
524 bool globalChecked[] = { false, false, false, false };
526 // loop on theShape and its sub-shapes
529 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
532 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
533 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
534 globalChecked, checkNoAlgo, aCheckedMap, theErrors))
537 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
542 if (revItSub != smMap.rend())
544 aKey = (*revItSub).first;
545 smToCheck = (*revItSub).second;
554 INFOS( "None algorithm attached" );
555 theErrors.push_back( TAlgoStateError() );
556 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, 1, true );
562 //=======================================================================
563 //function : IsGlobalHypothesis
564 //purpose : check if theAlgo is attached to the main shape
565 //=======================================================================
567 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
569 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
570 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
573 //=============================================================================
577 //=============================================================================
579 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
582 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
583 filter.And( filter.IsApplicableTo( aShape ));
585 list <const SMESHDS_Hypothesis * > algoList;
586 aMesh.GetHypotheses( aShape, filter, algoList, true );
588 if ( algoList.empty() )
591 return const_cast<SMESH_Algo*> ( static_cast<const SMESH_Algo* >( algoList.front() ));
594 //=============================================================================
598 //=============================================================================
600 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
602 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
604 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
606 _mapStudyContext[studyId] = new StudyContextStruct;
607 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
609 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
610 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
611 return myStudyContext;
614 //=============================================================================
618 //=============================================================================
620 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
624 //=============================================================================
628 //=============================================================================
630 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
634 //=============================================================================
638 //=============================================================================
640 void SMESH_Gen::Close(int studyId)
644 //=============================================================================
648 //=============================================================================
650 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
652 static vector<int> dim;
655 dim.resize( TopAbs_SHAPE, -1 );
656 dim[ TopAbs_COMPOUND ] = 3;
657 dim[ TopAbs_COMPSOLID ] = 3;
658 dim[ TopAbs_SOLID ] = 3;
659 dim[ TopAbs_SHELL ] = 3;
660 dim[ TopAbs_FACE ] = 2;
661 dim[ TopAbs_WIRE ] = 1;
662 dim[ TopAbs_EDGE ] = 1;
663 dim[ TopAbs_VERTEX ] = 0;
665 return dim[ aShapeType ];
668 //=============================================================================
672 //=============================================================================
674 int SMESH_Gen::GetANewId()
676 //MESSAGE("SMESH_Gen::GetANewId");