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,
133 const TopoDS_Shape & aShape,
136 MESSAGE("SMESH_Gen::Compute");
140 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
142 const bool includeSelf = true;
143 const bool complexShapeFirst = true;
145 SMESH_subMeshIteratorPtr smIt;
147 if ( anUpward ) // is called from below code here
149 // -----------------------------------------------
150 // mesh all the subshapes starting from vertices
151 // -----------------------------------------------
152 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
153 while ( smIt->more() )
155 SMESH_subMesh* smToCompute = smIt->next();
157 // do not mesh vertices of a pseudo shape
158 if ( !aMesh.HasShapeToMesh() &&
159 smToCompute->GetSubShape().ShapeType() == TopAbs_VERTEX )
162 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
163 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
165 // we check all the submeshes here and detect if any of them failed to compute
166 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
173 // -----------------------------------------------------------------
174 // apply algos that DO NOT require descretized boundaries and DO NOT
175 // support submeshes, starting from the most complex shapes
176 // and collect submeshes with algos that DO support submeshes
177 // -----------------------------------------------------------------
178 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
179 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
180 while ( smIt->more() )
182 SMESH_subMesh* smToCompute = smIt->next();
183 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
186 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
187 if ( GetShapeDim( aSubShape ) < 1 ) break;
189 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
190 if ( algo && !algo->NeedDescretBoundary() )
192 if ( algo->SupportSubmeshes() )
193 smWithAlgoSupportingSubmeshes.push_back( smToCompute );
195 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
198 // ------------------------------------------------------------
199 // compute submeshes under shapes with algos that DO NOT require
200 // descretized boundaries and DO support submeshes
201 // ------------------------------------------------------------
202 list< SMESH_subMesh* >::reverse_iterator subIt, subEnd;
203 subIt = smWithAlgoSupportingSubmeshes.rbegin();
204 subEnd = smWithAlgoSupportingSubmeshes.rend();
205 // start from lower shapes
206 for ( ; subIt != subEnd; ++subIt )
210 // get a shape the algo is assigned to
211 TopoDS_Shape algoShape;
212 if ( !GetAlgo( aMesh, sm->GetSubShape(), & algoShape ))
213 continue; // strange...
215 // look for more local algos
216 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
217 while ( smIt->more() )
219 SMESH_subMesh* smToCompute = smIt->next();
221 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
222 if ( aSubShape.ShapeType() == TopAbs_VERTEX ) continue;
224 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
226 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
227 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape ));
229 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( aSubShape, filter, true )) {
230 SMESH_Hypothesis::Hypothesis_Status status;
231 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
232 // mesh a lower smToCompute
233 Compute( aMesh, aSubShape );
237 // ----------------------------------------------------------
238 // apply the algos that do not require descretized boundaries
239 // ----------------------------------------------------------
240 for ( subIt = smWithAlgoSupportingSubmeshes.rbegin(); subIt != subEnd; ++subIt )
241 if ( sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
242 sm->ComputeStateEngine( SMESH_subMesh::COMPUTE );
244 // -----------------------------------------------
245 // mesh the rest subshapes starting from vertices
246 // -----------------------------------------------
247 ret = Compute( aMesh, aShape, /*anUpward=*/true );
250 MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
254 //=======================================================================
255 //function : checkConformIgnoredAlgos
257 //=======================================================================
259 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
260 SMESH_subMesh* aSubMesh,
261 const SMESH_Algo* aGlobIgnoAlgo,
262 const SMESH_Algo* aLocIgnoAlgo,
264 map<int, SMESH_subMesh*>& aCheckedMap,
265 list< SMESH_Gen::TAlgoStateError > & theErrors)
268 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
274 const list<const SMESHDS_Hypothesis*>& listHyp =
275 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
276 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
277 for ( ; it != listHyp.end(); it++)
279 const SMESHDS_Hypothesis * aHyp = *it;
280 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
283 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
286 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
288 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
289 << aLocIgnoAlgo->GetName() << ">");
293 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
294 int dim = algo->GetDim();
295 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
297 if ( dim < aMaxGlobIgnoDim )
299 // algo is hidden by a global algo
300 INFOS( ( isGlobal ? "Global" : "Local" )
301 << " <" << algo->GetName() << "> is hidden by global <"
302 << aGlobIgnoAlgo->GetName() << ">");
304 else if ( !algo->NeedDescretBoundary() && !isGlobal)
306 // local algo is not hidden and hides algos on sub-shapes
307 if (checkConform && !aSubMesh->IsConform( algo ))
310 checkConform = false; // no more check conformity
311 INFOS( "ERROR: Local <" << algo->GetName() <<
312 "> would produce not conform mesh: "
313 "<Not Conform Mesh Allowed> hypotesis is missing");
314 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
315 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
318 // sub-algos will be hidden by a local <algo>
319 const map<int, SMESH_subMesh*>& smMap = aSubMesh->DependsOn();
320 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub;
321 bool checkConform2 = false;
322 for ( revItSub = smMap.rbegin(); revItSub != smMap.rend(); revItSub++)
324 checkConformIgnoredAlgos (aMesh, (*revItSub).second, aGlobIgnoAlgo,
325 algo, checkConform2, aCheckedMap, theErrors);
326 int key = (*revItSub).first;
327 SMESH_subMesh* sm = (*revItSub).second;
328 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
330 aCheckedMap[ key ] = sm;
340 //=======================================================================
341 //function : checkMissing
342 //purpose : notify on missing hypothesis
343 // Return false if algo or hipothesis is missing
344 //=======================================================================
346 static bool checkMissing(SMESH_Gen* aGen,
348 SMESH_subMesh* aSubMesh,
349 const int aTopAlgoDim,
351 const bool checkNoAlgo,
352 map<int, SMESH_subMesh*>& aCheckedMap,
353 list< SMESH_Gen::TAlgoStateError > & theErrors)
355 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
358 //MESSAGE("=====checkMissing");
361 SMESH_Algo* algo = 0;
363 switch (aSubMesh->GetAlgoState())
365 case SMESH_subMesh::NO_ALGO: {
368 // should there be any algo?
369 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
370 if (aTopAlgoDim > shapeDim)
372 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
374 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
375 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
380 case SMESH_subMesh::MISSING_HYP: {
381 // notify if an algo missing hyp is attached to aSubMesh
382 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
384 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
385 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
387 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
388 SMESH_Hypothesis::Hypothesis_Status status;
389 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
390 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
391 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
392 << "<" << algo->GetName() << "> has a bad parameter value");
394 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
395 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
396 << "<" << algo->GetName() << "> assigned to mismatching geometry");
399 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
400 << "<" << algo->GetName() << "> misses some hypothesis");
402 if (IsGlobalHypothesis)
403 globalChecked[ algo->GetDim() ] = true;
404 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
405 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
410 case SMESH_subMesh::HYP_OK:
411 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
417 // do not check under algo that hides sub-algos or
418 // re-start checking NO_ALGO state
420 bool isTopLocalAlgo =
421 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
422 if (!algo->NeedDescretBoundary() || isTopLocalAlgo)
424 bool checkNoAlgo2 = ( algo->NeedDescretBoundary() );
425 const map<int, SMESH_subMesh*>& subMeshes = aSubMesh->DependsOn();
426 map<int, SMESH_subMesh*>::const_iterator itsub;
427 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
429 // sub-meshes should not be checked further more
430 int key = (*itsub).first;
431 SMESH_subMesh* sm = (*itsub).second;
432 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
433 aCheckedMap[ key ] = sm;
437 //check algo on sub-meshes
438 int aTopAlgoDim2 = algo->GetDim();
439 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
440 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
443 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
444 checkNoAlgo2 = false;
452 //=======================================================================
453 //function : CheckAlgoState
454 //purpose : notify on bad state of attached algos, return false
455 // if Compute() would fail because of some algo bad state
456 //=======================================================================
458 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
460 list< TAlgoStateError > errors;
461 return GetAlgoState( aMesh, aShape, errors );
464 //=======================================================================
465 //function : GetAlgoState
466 //purpose : notify on bad state of attached algos, return false
467 // if Compute() would fail because of some algo bad state
468 // theErrors list contains problems description
469 //=======================================================================
471 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
472 const TopoDS_Shape& theShape,
473 list< TAlgoStateError > & theErrors)
475 //MESSAGE("SMESH_Gen::CheckAlgoState");
478 bool hasAlgo = false;
480 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
481 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
482 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
488 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
490 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
491 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
492 for ( ; it != listHyp.end(); it++)
494 const SMESHDS_Hypothesis * aHyp = *it;
495 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
498 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
501 int dim = algo->GetDim();
502 aGlobAlgoArr[ dim ] = algo;
507 // --------------------------------------------------------
508 // info on algos that will be ignored because of ones that
509 // don't NeedDescretBoundary() attached to super-shapes,
510 // check that a conform mesh will be produced
511 // --------------------------------------------------------
514 // find a global algo possibly hiding sub-algos
516 const SMESH_Algo* aGlobIgnoAlgo = 0;
517 for (dim = 3; dim > 0; dim--)
519 if (aGlobAlgoArr[ dim ] &&
520 !aGlobAlgoArr[ dim ]->NeedDescretBoundary())
522 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
527 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
528 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
529 map<int, SMESH_subMesh*> aCheckedMap;
530 bool checkConform = ( !theMesh.IsNotConformAllowed() );
532 SMESH_subMesh* smToCheck = sm;
534 // loop on theShape and its sub-shapes
537 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
540 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
541 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
542 0, checkConform, aCheckedMap, theErrors))
545 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
549 if (revItSub != smMap.rend())
551 aKey = (*revItSub).first;
552 smToCheck = (*revItSub).second;
562 // ----------------------------------------------------------------
563 // info on missing hypothesis and find out if all needed algos are
565 // ----------------------------------------------------------------
567 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
569 // find max dim of global algo
571 for (dim = 3; dim > 0; dim--)
573 if (aGlobAlgoArr[ dim ])
581 revItSub = smMap.rbegin();
582 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
583 bool globalChecked[] = { false, false, false, false };
585 // loop on theShape and its sub-shapes
588 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
591 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
592 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
593 globalChecked, checkNoAlgo, aCheckedMap, theErrors))
596 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
601 if (revItSub != smMap.rend())
603 aKey = (*revItSub).first;
604 smToCheck = (*revItSub).second;
613 INFOS( "None algorithm attached" );
614 theErrors.push_back( TAlgoStateError() );
615 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, 1, true );
621 //=======================================================================
622 //function : IsGlobalHypothesis
623 //purpose : check if theAlgo is attached to the main shape
624 //=======================================================================
626 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
628 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
629 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
632 //=============================================================================
636 //=============================================================================
638 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
639 const TopoDS_Shape & aShape,
640 TopoDS_Shape* assignedTo)
643 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
644 filter.And( filter.IsApplicableTo( aShape ));
646 return (SMESH_Algo*) aMesh.GetHypothesis( aShape, filter, true, assignedTo );
649 //=============================================================================
653 //=============================================================================
655 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
657 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
659 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
661 _mapStudyContext[studyId] = new StudyContextStruct;
662 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
664 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
665 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
666 return myStudyContext;
669 //=============================================================================
673 //=============================================================================
675 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
679 //=============================================================================
683 //=============================================================================
685 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
689 //=============================================================================
693 //=============================================================================
695 void SMESH_Gen::Close(int studyId)
699 //=============================================================================
703 //=============================================================================
705 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
707 static vector<int> dim;
710 dim.resize( TopAbs_SHAPE, -1 );
711 dim[ TopAbs_COMPOUND ] = 3;
712 dim[ TopAbs_COMPSOLID ] = 3;
713 dim[ TopAbs_SOLID ] = 3;
714 dim[ TopAbs_SHELL ] = 3;
715 dim[ TopAbs_FACE ] = 2;
716 dim[ TopAbs_WIRE ] = 1;
717 dim[ TopAbs_EDGE ] = 1;
718 dim[ TopAbs_VERTEX ] = 0;
720 return dim[ aShapeType ];
723 //=============================================================================
727 //=============================================================================
729 int SMESH_Gen::GetANewId()
731 //MESSAGE("SMESH_Gen::GetANewId");