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,
135 const ::MeshDimension aDim,
136 TSetOfInt* aShapesId)
138 MESSAGE("SMESH_Gen::Compute");
142 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
144 const bool includeSelf = true;
145 const bool complexShapeFirst = true;
147 SMESH_subMeshIteratorPtr smIt;
149 if ( anUpward ) // is called from below code here
151 // -----------------------------------------------
152 // mesh all the subshapes starting from vertices
153 // -----------------------------------------------
154 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
155 while ( smIt->more() )
157 SMESH_subMesh* smToCompute = smIt->next();
159 // do not mesh vertices of a pseudo shape
160 const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
161 if ( !aMesh.HasShapeToMesh() && aShType == TopAbs_VERTEX )
164 // check for preview dimension limitations
165 if ( aShapesId && GetShapeDim( aShType ) > (int)aDim )
168 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
169 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
171 // we check all the submeshes here and detect if any of them failed to compute
172 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
174 else if ( aShapesId )
175 aShapesId->insert( smToCompute->GetId() );
181 // -----------------------------------------------------------------
182 // apply algos that DO NOT require descretized boundaries and DO NOT
183 // support submeshes, starting from the most complex shapes
184 // and collect submeshes with algos that DO support submeshes
185 // -----------------------------------------------------------------
186 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
187 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
188 while ( smIt->more() )
190 SMESH_subMesh* smToCompute = smIt->next();
191 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
194 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
195 const int aShapeDim = GetShapeDim( aSubShape );
196 if ( aShapeDim < 1 ) break;
198 // check for preview dimension limitations
199 if ( aShapesId && aShapeDim > (int)aDim )
202 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
203 if ( algo && !algo->NeedDescretBoundary() )
205 if ( algo->SupportSubmeshes() )
206 smWithAlgoSupportingSubmeshes.push_back( smToCompute );
209 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
211 aShapesId->insert( smToCompute->GetId() );
215 // ------------------------------------------------------------
216 // compute submeshes under shapes with algos that DO NOT require
217 // descretized boundaries and DO support submeshes
218 // ------------------------------------------------------------
219 list< SMESH_subMesh* >::reverse_iterator subIt, subEnd;
220 subIt = smWithAlgoSupportingSubmeshes.rbegin();
221 subEnd = smWithAlgoSupportingSubmeshes.rend();
222 // start from lower shapes
223 for ( ; subIt != subEnd; ++subIt )
227 // get a shape the algo is assigned to
228 TopoDS_Shape algoShape;
229 if ( !GetAlgo( aMesh, sm->GetSubShape(), & algoShape ))
230 continue; // strange...
232 // look for more local algos
233 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
234 while ( smIt->more() )
236 SMESH_subMesh* smToCompute = smIt->next();
238 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
239 const int aShapeDim = GetShapeDim( aSubShape );
240 //if ( aSubShape.ShapeType() == TopAbs_VERTEX ) continue;
241 if ( aShapeDim < 1 ) continue;
243 // check for preview dimension limitations
244 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
247 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
249 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
250 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape ));
252 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( aSubShape, filter, true )) {
253 SMESH_Hypothesis::Hypothesis_Status status;
254 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
255 // mesh a lower smToCompute starting from vertices
256 Compute( aMesh, aSubShape, /*anUpward=*/true, aDim, aShapesId );
260 // ----------------------------------------------------------
261 // apply the algos that do not require descretized boundaries
262 // ----------------------------------------------------------
263 for ( subIt = smWithAlgoSupportingSubmeshes.rbegin(); subIt != subEnd; ++subIt )
264 if ( sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
266 const TopAbs_ShapeEnum aShType = sm->GetSubShape().ShapeType();
267 // check for preview dimension limitations
268 if ( aShapesId && GetShapeDim( aShType ) > (int)aDim )
271 sm->ComputeStateEngine( SMESH_subMesh::COMPUTE );
273 aShapesId->insert( sm->GetId() );
276 // -----------------------------------------------
277 // mesh the rest subshapes starting from vertices
278 // -----------------------------------------------
279 ret = Compute( aMesh, aShape, /*anUpward=*/true, aDim, aShapesId );
282 MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
286 //=======================================================================
287 //function : checkConformIgnoredAlgos
289 //=======================================================================
291 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
292 SMESH_subMesh* aSubMesh,
293 const SMESH_Algo* aGlobIgnoAlgo,
294 const SMESH_Algo* aLocIgnoAlgo,
296 map<int, SMESH_subMesh*>& aCheckedMap,
297 list< SMESH_Gen::TAlgoStateError > & theErrors)
300 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
306 const list<const SMESHDS_Hypothesis*>& listHyp =
307 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
308 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
309 for ( ; it != listHyp.end(); it++)
311 const SMESHDS_Hypothesis * aHyp = *it;
312 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
315 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
318 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
320 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
321 << aLocIgnoAlgo->GetName() << ">");
325 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
326 int dim = algo->GetDim();
327 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
329 if ( dim < aMaxGlobIgnoDim )
331 // algo is hidden by a global algo
332 INFOS( ( isGlobal ? "Global" : "Local" )
333 << " <" << algo->GetName() << "> is hidden by global <"
334 << aGlobIgnoAlgo->GetName() << ">");
336 else if ( !algo->NeedDescretBoundary() && !isGlobal)
338 // local algo is not hidden and hides algos on sub-shapes
339 if (checkConform && !aSubMesh->IsConform( algo ))
342 checkConform = false; // no more check conformity
343 INFOS( "ERROR: Local <" << algo->GetName() <<
344 "> would produce not conform mesh: "
345 "<Not Conform Mesh Allowed> hypotesis is missing");
346 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
347 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
350 // sub-algos will be hidden by a local <algo>
351 const map<int, SMESH_subMesh*>& smMap = aSubMesh->DependsOn();
352 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub;
353 bool checkConform2 = false;
354 for ( revItSub = smMap.rbegin(); revItSub != smMap.rend(); revItSub++)
356 checkConformIgnoredAlgos (aMesh, (*revItSub).second, aGlobIgnoAlgo,
357 algo, checkConform2, aCheckedMap, theErrors);
358 int key = (*revItSub).first;
359 SMESH_subMesh* sm = (*revItSub).second;
360 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
362 aCheckedMap[ key ] = sm;
372 //=======================================================================
373 //function : checkMissing
374 //purpose : notify on missing hypothesis
375 // Return false if algo or hipothesis is missing
376 //=======================================================================
378 static bool checkMissing(SMESH_Gen* aGen,
380 SMESH_subMesh* aSubMesh,
381 const int aTopAlgoDim,
383 const bool checkNoAlgo,
384 map<int, SMESH_subMesh*>& aCheckedMap,
385 list< SMESH_Gen::TAlgoStateError > & theErrors)
387 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
390 //MESSAGE("=====checkMissing");
393 SMESH_Algo* algo = 0;
395 switch (aSubMesh->GetAlgoState())
397 case SMESH_subMesh::NO_ALGO: {
400 // should there be any algo?
401 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
402 if (aTopAlgoDim > shapeDim)
404 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
406 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
407 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
412 case SMESH_subMesh::MISSING_HYP: {
413 // notify if an algo missing hyp is attached to aSubMesh
414 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
416 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
417 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
419 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
420 SMESH_Hypothesis::Hypothesis_Status status;
421 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
422 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
423 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
424 << "<" << algo->GetName() << "> has a bad parameter value");
426 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
427 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
428 << "<" << algo->GetName() << "> assigned to mismatching geometry");
431 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
432 << "<" << algo->GetName() << "> misses some hypothesis");
434 if (IsGlobalHypothesis)
435 globalChecked[ algo->GetDim() ] = true;
436 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
437 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
442 case SMESH_subMesh::HYP_OK:
443 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
449 // do not check under algo that hides sub-algos or
450 // re-start checking NO_ALGO state
452 bool isTopLocalAlgo =
453 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
454 if (!algo->NeedDescretBoundary() || isTopLocalAlgo)
456 bool checkNoAlgo2 = ( algo->NeedDescretBoundary() );
457 const map<int, SMESH_subMesh*>& subMeshes = aSubMesh->DependsOn();
458 map<int, SMESH_subMesh*>::const_iterator itsub;
459 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
461 // sub-meshes should not be checked further more
462 int key = (*itsub).first;
463 SMESH_subMesh* sm = (*itsub).second;
464 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
465 aCheckedMap[ key ] = sm;
469 //check algo on sub-meshes
470 int aTopAlgoDim2 = algo->GetDim();
471 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
472 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
475 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
476 checkNoAlgo2 = false;
484 //=======================================================================
485 //function : CheckAlgoState
486 //purpose : notify on bad state of attached algos, return false
487 // if Compute() would fail because of some algo bad state
488 //=======================================================================
490 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
492 list< TAlgoStateError > errors;
493 return GetAlgoState( aMesh, aShape, errors );
496 //=======================================================================
497 //function : GetAlgoState
498 //purpose : notify on bad state of attached algos, return false
499 // if Compute() would fail because of some algo bad state
500 // theErrors list contains problems description
501 //=======================================================================
503 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
504 const TopoDS_Shape& theShape,
505 list< TAlgoStateError > & theErrors)
507 //MESSAGE("SMESH_Gen::CheckAlgoState");
510 bool hasAlgo = false;
512 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
513 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
514 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
520 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
522 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
523 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
524 for ( ; it != listHyp.end(); it++)
526 const SMESHDS_Hypothesis * aHyp = *it;
527 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
530 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
533 int dim = algo->GetDim();
534 aGlobAlgoArr[ dim ] = algo;
539 // --------------------------------------------------------
540 // info on algos that will be ignored because of ones that
541 // don't NeedDescretBoundary() attached to super-shapes,
542 // check that a conform mesh will be produced
543 // --------------------------------------------------------
546 // find a global algo possibly hiding sub-algos
548 const SMESH_Algo* aGlobIgnoAlgo = 0;
549 for (dim = 3; dim > 0; dim--)
551 if (aGlobAlgoArr[ dim ] &&
552 !aGlobAlgoArr[ dim ]->NeedDescretBoundary())
554 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
559 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
560 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
561 map<int, SMESH_subMesh*> aCheckedMap;
562 bool checkConform = ( !theMesh.IsNotConformAllowed() );
564 SMESH_subMesh* smToCheck = sm;
566 // loop on theShape and its sub-shapes
569 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
572 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
573 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
574 0, checkConform, aCheckedMap, theErrors))
577 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
581 if (revItSub != smMap.rend())
583 aKey = (*revItSub).first;
584 smToCheck = (*revItSub).second;
594 // ----------------------------------------------------------------
595 // info on missing hypothesis and find out if all needed algos are
597 // ----------------------------------------------------------------
599 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
601 // find max dim of global algo
603 for (dim = 3; dim > 0; dim--)
605 if (aGlobAlgoArr[ dim ])
613 revItSub = smMap.rbegin();
614 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
615 bool globalChecked[] = { false, false, false, false };
617 // loop on theShape and its sub-shapes
620 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
623 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
624 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
625 globalChecked, checkNoAlgo, aCheckedMap, theErrors))
628 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
633 if (revItSub != smMap.rend())
635 aKey = (*revItSub).first;
636 smToCheck = (*revItSub).second;
645 INFOS( "None algorithm attached" );
646 theErrors.push_back( TAlgoStateError() );
647 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, 1, true );
653 //=======================================================================
654 //function : IsGlobalHypothesis
655 //purpose : check if theAlgo is attached to the main shape
656 //=======================================================================
658 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
660 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
661 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
664 //=============================================================================
668 //=============================================================================
670 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
671 const TopoDS_Shape & aShape,
672 TopoDS_Shape* assignedTo)
675 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
676 filter.And( filter.IsApplicableTo( aShape ));
678 return (SMESH_Algo*) aMesh.GetHypothesis( aShape, filter, true, assignedTo );
681 //=============================================================================
685 //=============================================================================
687 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
689 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
691 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
693 _mapStudyContext[studyId] = new StudyContextStruct;
694 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
696 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
697 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
698 return myStudyContext;
701 //=============================================================================
705 //=============================================================================
707 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
711 //=============================================================================
715 //=============================================================================
717 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
721 //=============================================================================
725 //=============================================================================
727 void SMESH_Gen::Close(int studyId)
731 //=============================================================================
735 //=============================================================================
737 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
739 static vector<int> dim;
742 dim.resize( TopAbs_SHAPE, -1 );
743 dim[ TopAbs_COMPOUND ] = MeshDim_3D;
744 dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
745 dim[ TopAbs_SOLID ] = MeshDim_3D;
746 dim[ TopAbs_SHELL ] = MeshDim_3D;
747 dim[ TopAbs_FACE ] = MeshDim_2D;
748 dim[ TopAbs_WIRE ] = MeshDim_1D;
749 dim[ TopAbs_EDGE ] = MeshDim_1D;
750 dim[ TopAbs_VERTEX ] = MeshDim_0D;
752 return dim[ aShapeType ];
755 //=============================================================================
759 //=============================================================================
761 int SMESH_Gen::GetANewId()
763 //MESSAGE("SMESH_Gen::GetANewId");