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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESH_Gen.cxx
25 // Author : Paul RASCLE, EDF
29 #include "SMESH_Gen.hxx"
30 #include "SMESH_subMesh.hxx"
31 #include "SMDS_MeshElement.hxx"
32 #include "SMDS_MeshNode.hxx"
34 #include "utilities.h"
36 #include "Utils_ExceptHandlers.hxx"
39 #include <BRep_Tool.hxx>
40 #include <TopTools_ListOfShape.hxx>
41 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 //=============================================================================
47 * default constructor:
49 //=============================================================================
51 SMESH_Gen::SMESH_Gen()
53 MESSAGE("SMESH_Gen::SMESH_Gen");
58 //=============================================================================
62 //=============================================================================
64 SMESH_Gen::~SMESH_Gen()
66 MESSAGE("SMESH_Gen::~SMESH_Gen");
69 //=============================================================================
73 //=============================================================================
75 /*SMESH_Hypothesis *SMESH_Gen::CreateHypothesis(const char *anHyp, int studyId)
76 throw(SALOME_Exception)
79 MESSAGE("CreateHypothesis("<<anHyp<<","<<studyId<<")");
80 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
82 StudyContextStruct *myStudyContext = GetStudyContext(studyId);
84 // create a new hypothesis object, store its ref. in studyContext
86 SMESH_Hypothesis *myHypothesis = _hypothesisFactory.Create(anHyp, studyId);
87 int hypId = myHypothesis->GetID();
88 myStudyContext->mapHypothesis[hypId] = myHypothesis;
92 // store hypothesis in SMESHDS document
94 myStudyContext->myDocument->AddHypothesis(myHypothesis);
98 //=============================================================================
102 //=============================================================================
104 SMESH_Mesh* SMESH_Gen::CreateMesh(int studyId)
105 throw(SALOME_Exception)
107 Unexpect aCatch(SalomeException);
108 MESSAGE("SMESH_Gen::CreateMesh");
109 // if (aShape.ShapeType() == TopAbs_COMPOUND)
111 // INFOS("Mesh Compound not yet implemented!");
112 // throw(SALOME_Exception(LOCALIZED("Mesh Compound not yet implemented!")));
115 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
117 StudyContextStruct *myStudyContext = GetStudyContext(studyId);
119 // create a new SMESH_mesh object
121 SMESH_Mesh *mesh = new SMESH_Mesh(_localId++,
124 myStudyContext->myDocument);
125 myStudyContext->mapMesh[_localId] = mesh;
127 // associate a TopoDS_Shape to the mesh
129 //mesh->ShapeToMesh(aShape);
133 //=============================================================================
137 //=============================================================================
139 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
141 MESSAGE("SMESH_Gen::Compute");
142 // bool isDone = false;
144 Algo : s'appuie ou non sur une geometrie
146 Vertex : rien à faire (range le point)
147 Edge, Wire, collection d'edge et wire : 1D
148 Face, Shell, collection de Face et Shells : 2D
149 Solid, Collection de Solid : 3D
151 // *** corriger commentaires
152 // check hypothesis associated to the mesh :
153 // - only one algo : type compatible with the type of the shape
154 // - hypothesis = compatible with algo
155 // - check if hypothesis are applicable to this algo
156 // - check contradictions within hypothesis
157 // (test if enough hypothesis is done further)
161 // if ( !CheckAlgoState( aMesh, aShape ))
163 // INFOS( "ABORT MESHING: some algos or hypothesis are missing");
167 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
169 // -----------------------------------------------------------------
170 // apply algos that do not require descretized boundaries, starting
171 // from the most complex shapes
172 // -----------------------------------------------------------------
174 // map containing all subshapes in the order: vertices, edges, faces...
175 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
176 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
178 SMESH_subMesh* smToCompute = sm;
179 while ( smToCompute )
181 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
182 if ( GetShapeDim( aSubShape ) < 1 ) break;
184 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
185 if (algo && !algo->NeedDescretBoundary()) {
186 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE) {
187 ret = smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
188 } else if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE) {
198 if (revItSub != smMap.rend())
200 smToCompute = (*revItSub).second;
207 // -----------------------------------------------
208 // mesh the rest subshapes starting from vertices
209 // -----------------------------------------------
211 smToCompute = sm->GetFirstToCompute();
214 TopoDS_Shape subShape = smToCompute->GetSubShape();
215 int dim = GetShapeDim(subShape);
218 if ( !smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE) )
223 TopoDS_Vertex V1 = TopoDS::Vertex(subShape);
224 gp_Pnt P1 = BRep_Tool::Pnt(V1);
225 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
226 SMDS_MeshNode * node = meshDS->AddNode(P1.X(), P1.Y(), P1.Z());
227 if ( node ) { // san - increase robustness
228 meshDS->SetNodeOnVertex(node, V1);
229 smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
232 smToCompute = sm->GetFirstToCompute();
235 if (!ret) return false;
237 // JFA for PAL6524: if there are failed sub-meshes, return Standard_False
238 const map < int, SMESH_subMesh * >&subMeshes = sm->DependsOn();
239 map < int, SMESH_subMesh * >::const_iterator itsub;
240 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++) {
241 SMESH_subMesh *smi = (*itsub).second;
242 if (smi->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE) return false;
244 if (sm->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE) return false;
246 MESSAGE( "VSR - SMESH_Gen::Compute() finished" );
251 //=======================================================================
252 //function : checkConformIgnoredAlgos
254 //=======================================================================
256 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
257 SMESH_subMesh* aSubMesh,
258 const SMESH_Algo* aGlobIgnoAlgo,
259 const SMESH_Algo* aLocIgnoAlgo,
261 map<int, SMESH_subMesh*>& aCheckedMap)
264 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
270 const list<const SMESHDS_Hypothesis*>& listHyp =
271 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
272 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
273 for ( ; it != listHyp.end(); it++)
275 const SMESHDS_Hypothesis * aHyp = *it;
276 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
279 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
282 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
284 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
285 << aLocIgnoAlgo->GetName() << ">");
289 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
290 int dim = algo->GetDim();
291 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
293 if ( dim < aMaxGlobIgnoDim )
295 // algo is hidden by a global algo
296 INFOS( ( isGlobal ? "Global" : "Local" )
297 << " <" << algo->GetName() << "> is hidden by global <"
298 << aGlobIgnoAlgo->GetName() << ">");
300 else if ( !algo->NeedDescretBoundary() && !isGlobal)
302 // local algo is not hidden and hides algos on sub-shapes
303 if (checkConform && !aSubMesh->IsConform( algo ))
306 checkConform = false; // no more check conformity
307 INFOS( "ERROR: Local <" << algo->GetName() <<
308 "> would produce not conform mesh: "
309 "<Not Conform Mesh Allowed> hypotesis is missing");
312 // sub-algos will be hidden by a local <algo>
313 const map<int, SMESH_subMesh*>& smMap = aSubMesh->DependsOn();
314 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub;
315 bool checkConform2 = false;
316 for ( revItSub = smMap.rbegin(); revItSub != smMap.rend(); revItSub++)
318 checkConformIgnoredAlgos (aMesh, (*revItSub).second, aGlobIgnoAlgo,
319 algo, checkConform2, aCheckedMap);
320 int key = (*revItSub).first;
321 SMESH_subMesh* sm = (*revItSub).second;
322 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
324 aCheckedMap[ key ] = sm;
334 //=======================================================================
335 //function : checkMissing
336 //purpose : notify on missing hypothesis
337 // Return false if algo or hipothesis is missing
338 //=======================================================================
340 static bool checkMissing(SMESH_Gen* aGen,
342 SMESH_subMesh* aSubMesh,
343 const int aTopAlgoDim,
345 const bool checkNoAlgo,
346 map<int, SMESH_subMesh*>& aCheckedMap)
348 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
351 //MESSAGE("=====checkMissing");
354 SMESH_Algo* algo = 0;
356 switch (aSubMesh->GetAlgoState())
358 case SMESH_subMesh::NO_ALGO: {
361 // should there be any algo?
362 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
363 if (aTopAlgoDim > shapeDim)
365 INFOS( "ERROR: " << shapeDim << "D algorithm is missing" );
371 case SMESH_subMesh::MISSING_HYP: {
372 // notify if an algo missing hyp is attached to aSubMesh
373 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
375 bool isGlobalAlgo = aGen->IsGlobalAlgo( algo, aMesh );
376 if (!isGlobalAlgo || !globalChecked[ algo->GetDim() ])
378 INFOS( "ERROR: " << (isGlobalAlgo ? "Global " : "Local ")
379 << "<" << algo->GetName() << "> misses some hypothesis");
381 globalChecked[ algo->GetDim() ] = true;
386 case SMESH_subMesh::HYP_OK:
387 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
393 // do not check under algo that hides sub-algos or
394 // re-start checking NO_ALGO state
396 bool isTopLocalAlgo =
397 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalAlgo( algo, aMesh ));
398 if (!algo->NeedDescretBoundary() || isTopLocalAlgo)
400 bool checkNoAlgo2 = ( algo->NeedDescretBoundary() );
401 const map<int, SMESH_subMesh*>& subMeshes = aSubMesh->DependsOn();
402 map<int, SMESH_subMesh*>::const_iterator itsub;
403 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
405 // sub-meshes should not be checked further more
406 int key = (*itsub).first;
407 SMESH_subMesh* sm = (*itsub).second;
408 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
409 aCheckedMap[ key ] = sm;
413 //check algo on sub-meshes
414 int aTopAlgoDim2 = algo->GetDim();
415 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
416 globalChecked, checkNoAlgo2, aCheckedMap))
419 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
420 checkNoAlgo2 = false;
428 //=======================================================================
429 //function : CheckAlgoState
430 //purpose : notify on bad state of attached algos, return false
431 // if Compute() would fail because of some algo bad state
432 //=======================================================================
434 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
436 //MESSAGE("SMESH_Gen::CheckAlgoState");
439 bool hasAlgo = false;
441 SMESH_subMesh* sm = aMesh.GetSubMesh(aShape);
442 const SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
443 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
449 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
451 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
452 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
453 for ( ; it != listHyp.end(); it++)
455 const SMESHDS_Hypothesis * aHyp = *it;
456 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
459 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
462 int dim = algo->GetDim();
463 aGlobAlgoArr[ dim ] = algo;
468 // --------------------------------------------------------
469 // info on algos that will be ignored because of ones that
470 // don't NeedDescretBoundary() attached to super-shapes,
471 // check that a conform mesh will be produced
472 // --------------------------------------------------------
475 // find a global algo possibly hidding sub-algos
477 const SMESH_Algo* aGlobIgnoAlgo = 0;
478 for (dim = 3; dim > 0; dim--)
480 if (aGlobAlgoArr[ dim ] &&
481 !aGlobAlgoArr[ dim ]->NeedDescretBoundary())
483 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
488 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
489 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
490 map<int, SMESH_subMesh*> aCheckedMap;
491 bool checkConform = ( !aMesh.IsNotConformAllowed() );
493 SMESH_subMesh* smToCheck = sm;
495 // loop on aShape and its sub-shapes
498 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
501 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
502 if (!checkConformIgnoredAlgos (aMesh, smToCheck, aGlobIgnoAlgo,
503 0, checkConform, aCheckedMap))
506 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
510 if (revItSub != smMap.rend())
512 aKey = (*revItSub).first;
513 smToCheck = (*revItSub).second;
523 // ----------------------------------------------------------------
524 // info on missing hypothesis and find out if all needed algos are
526 // ----------------------------------------------------------------
528 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
530 // find max dim of global algo
532 for (dim = 3; dim > 0; dim--)
534 if (aGlobAlgoArr[ dim ])
542 revItSub = smMap.rbegin();
543 bool checkNoAlgo = (bool) aTopAlgoDim;
544 bool globalChecked[] = { false, false, false, false };
546 // loop on aShape and its sub-shapes
549 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
552 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
553 if (!checkMissing (this, aMesh, smToCheck, aTopAlgoDim,
554 globalChecked, checkNoAlgo, aCheckedMap))
557 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
562 if (revItSub != smMap.rend())
564 aKey = (*revItSub).first;
565 smToCheck = (*revItSub).second;
573 INFOS( "None algorithm attached" );
575 return ( ret && hasAlgo );
578 //=======================================================================
579 //function : IsGlobalAlgo
580 //purpose : check if theAlgo is attached to the main shape
581 //=======================================================================
583 bool SMESH_Gen::IsGlobalAlgo(const SMESH_Algo* theAlgo, SMESH_Mesh& aMesh)
585 const SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
586 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
587 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
588 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
589 for ( ; it != listHyp.end(); it++)
590 if ( *it == theAlgo )
597 //=======================================================================
598 //function : getAlgoId
599 //purpose : return algo ID or -1 if not found
600 //=======================================================================
602 static int getAlgo(const list<const SMESHDS_Hypothesis*>& theHypList,
603 const int theAlgoDim,
604 const int theAlgoShapeType)
606 list<const SMESHDS_Hypothesis*>::const_iterator it = theHypList.begin();
611 while (it!=theHypList.end())
613 const SMESH_Hypothesis *anHyp = static_cast< const SMESH_Hypothesis *>( *it );
614 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO &&
615 anHyp->GetDim() == theAlgoDim &&
616 ((anHyp->GetShapeType()) & (1 << theAlgoShapeType)))
619 algo_id = anHyp->GetID();
623 //if (nb_algo > 1) return -1; // more than one algo
630 //=============================================================================
634 //=============================================================================
636 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
638 // MESSAGE("SMESH_Gen::GetAlgo");
640 const SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
641 int dim = GetShapeDim( aShape );
642 int shape_type = aShape.ShapeType();
645 algo_id = getAlgo( meshDS->GetHypothesis( aShape ), dim, shape_type );
650 TopTools_ListIteratorOfListOfShape ancIt( aMesh.GetAncestors( aShape ));
651 for (; ancIt.More(); ancIt.Next())
653 const TopoDS_Shape& ancestor = ancIt.Value();
654 algo_id = getAlgo( meshDS->GetHypothesis( ancestor ), dim, shape_type );
658 if (algo_id < 0) return NULL;
661 ASSERT(_mapAlgo.find(algo_id) != _mapAlgo.end());
663 return _mapAlgo[algo_id];
665 // const SMESHDS_Hypothesis *theHyp = NULL;
666 // SMESH_Algo *algo = NULL;
667 // const SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
672 // // try shape first, then main shape
674 // TopoDS_Shape mainShape = meshDS->ShapeToMesh();
675 // const TopoDS_Shape *shapeToTry[2] = { &aShape, &mainShape };
677 // for (int iShape = 0; iShape < 2; iShape++)
679 // TopoDS_Shape tryShape = (*shapeToTry[iShape]);
681 // const list<const SMESHDS_Hypothesis*>& listHyp =
682 // meshDS->GetHypothesis(tryShape);
683 // list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
686 // int shapeDim = GetShapeDim(aShape);
687 // int typeOfShape = aShape.ShapeType();
689 // while (it!=listHyp.end())
691 // const SMESHDS_Hypothesis *anHyp = *it;
692 // hypType = anHyp->GetType();
693 // //SCRUTE(hypType);
694 // if (hypType > SMESHDS_Hypothesis::PARAM_ALGO)
698 // case SMESHDS_Hypothesis::ALGO_1D:
701 // case SMESHDS_Hypothesis::ALGO_2D:
704 // case SMESHDS_Hypothesis::ALGO_3D:
711 // //SCRUTE(algoDim);
712 // //SCRUTE(shapeDim);
713 // //SCRUTE(typeOfShape);
714 // if (shapeDim == algoDim) // count only algos of shape dim.
715 // { // discard algos for subshapes
716 // hypId = anHyp->GetID(); // (of lower dim.)
717 // ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
718 // SMESH_Algo *anAlgo = _mapAlgo[hypId];
719 // //SCRUTE(anAlgo->GetShapeType());
720 // //if (anAlgo->GetShapeType() == typeOfShape)
721 // if ((anAlgo->GetShapeType()) & (1 << typeOfShape))
722 // { // only specific TopoDS_Shape
728 // if (nb_algo > 1) return NULL; // more than one algo
731 // if (nb_algo == 1) // one algo found : OK
732 // break; // do not try a parent shape
736 // return NULL; // no algo found
738 // hypType = theHyp->GetType();
739 // hypId = theHyp->GetID();
741 // ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
742 // algo = _mapAlgo[hypId];
743 // //MESSAGE("Algo found " << algo->GetName() << " Id " << hypId);
747 //=============================================================================
751 //=============================================================================
753 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
755 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
757 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
759 _mapStudyContext[studyId] = new StudyContextStruct;
760 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
762 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
763 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
764 return myStudyContext;
767 //=============================================================================
771 //=============================================================================
773 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
777 //=============================================================================
781 //=============================================================================
783 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
787 //=============================================================================
791 //=============================================================================
793 void SMESH_Gen::Close(int studyId)
797 //=============================================================================
801 //=============================================================================
803 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
805 int shapeDim = -1; // Shape dimension: 0D, 1D, 2D, 3D
806 int type = aShapeType;//.ShapeType();
809 case TopAbs_COMPOUND:
810 case TopAbs_COMPSOLID:
817 // case TopAbs_SHELL:
838 //=============================================================================
842 //=============================================================================
844 int SMESH_Gen::GetANewId()
846 //MESSAGE("SMESH_Gen::GetANewId");