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"
38 #include <BRep_Tool.hxx>
39 #include <TopTools_ListOfShape.hxx>
40 #include <TopTools_ListIteratorOfListOfShape.hxx>
41 #include "Utils_ExceptHandlers.hxx"
44 //=============================================================================
46 * default constructor:
48 //=============================================================================
50 SMESH_Gen::SMESH_Gen()
52 MESSAGE("SMESH_Gen::SMESH_Gen");
57 //=============================================================================
61 //=============================================================================
63 SMESH_Gen::~SMESH_Gen()
65 MESSAGE("SMESH_Gen::~SMESH_Gen");
68 //=============================================================================
72 //=============================================================================
74 /*SMESH_Hypothesis *SMESH_Gen::CreateHypothesis(const char *anHyp, int studyId)
75 throw(SALOME_Exception)
78 MESSAGE("CreateHypothesis("<<anHyp<<","<<studyId<<")");
79 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
81 StudyContextStruct *myStudyContext = GetStudyContext(studyId);
83 // create a new hypothesis object, store its ref. in studyContext
85 SMESH_Hypothesis *myHypothesis = _hypothesisFactory.Create(anHyp, studyId);
86 int hypId = myHypothesis->GetID();
87 myStudyContext->mapHypothesis[hypId] = myHypothesis;
91 // store hypothesis in SMESHDS document
93 myStudyContext->myDocument->AddHypothesis(myHypothesis);
97 //=============================================================================
101 //=============================================================================
103 SMESH_Mesh* SMESH_Gen::CreateMesh(int studyId)
104 throw(SALOME_Exception)
106 Unexpect aCatch(SalomeException);
107 MESSAGE("SMESH_Gen::CreateMesh");
108 // if (aShape.ShapeType() == TopAbs_COMPOUND)
110 // INFOS("Mesh Compound not yet implemented!");
111 // throw(SALOME_Exception(LOCALIZED("Mesh Compound not yet implemented!")));
114 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
116 StudyContextStruct *myStudyContext = GetStudyContext(studyId);
118 // create a new SMESH_mesh object
120 SMESH_Mesh *mesh = new SMESH_Mesh(_localId++,
123 myStudyContext->myDocument);
124 myStudyContext->mapMesh[_localId] = mesh;
126 // associate a TopoDS_Shape to the mesh
128 //mesh->ShapeToMesh(aShape);
132 //=============================================================================
136 //=============================================================================
138 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
140 MESSAGE("SMESH_Gen::Compute");
141 // bool isDone = false;
143 Algo : s'appuie ou non sur une geometrie
145 Vertex : rien à faire (range le point)
146 Edge, Wire, collection d'edge et wire : 1D
147 Face, Shell, collection de Face et Shells : 2D
148 Solid, Collection de Solid : 3D
150 // *** corriger commentaires
151 // check hypothesis associated to the mesh :
152 // - only one algo : type compatible with the type of the shape
153 // - hypothesis = compatible with algo
154 // - check if hypothesis are applicable to this algo
155 // - check contradictions within hypothesis
156 // (test if enough hypothesis is done further)
160 if ( !CheckAlgoState( aMesh, aShape ))
162 INFOS( "ABORT MESHING: some algos or hypothesis are missing");
166 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
168 // -----------------------------------------------------------------
169 // apply algos that do not require descretized boundaries, starting
170 // from the most complex shapes
171 // -----------------------------------------------------------------
173 // map containing all subshapes in the order: vertices, edges, faces...
174 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
175 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
177 SMESH_subMesh* smToCompute = sm;
178 while ( smToCompute )
180 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
181 if ( GetShapeDim( aSubShape ) < 1 ) break;
183 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
185 !algo->NeedDescretBoundary() &&
186 smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE )
188 ret = smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
194 if (revItSub != smMap.rend())
196 smToCompute = (*revItSub).second;
203 // -----------------------------------------------
204 // mesh the rest subshapes starting from vertices
205 // -----------------------------------------------
207 smToCompute = sm->GetFirstToCompute();
210 TopoDS_Shape subShape = smToCompute->GetSubShape();
211 int dim = GetShapeDim(subShape);
214 //MESSAGE ( "MESH shape id=" << smToCompute->GetId() <<
215 // " type=" << smToCompute->GetSubShape().ShapeType());
216 bool ret1 = smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
222 ASSERT(smToCompute->_vertexSet == false);
223 TopoDS_Vertex V1 = TopoDS::Vertex(subShape);
224 gp_Pnt P1 = BRep_Tool::Pnt(V1);
225 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
226 //MESSAGE("point "<<nodeId<<" "<<P1.X()<<" "<<P1.Y()<<" "<<P1.Z());
227 SMDS_MeshNode * node = meshDS->AddNode(P1.X(), P1.Y(), P1.Z());
228 if ( node ) { // san - increase robustness
229 meshDS->SetNodeOnVertex(node, V1);
230 smToCompute->GetSubMeshDS();
231 smToCompute->_vertexSet = true;
232 smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
235 smToCompute = sm->GetFirstToCompute();
238 MESSAGE( "VSR - SMESH_Gen::Compute() finished" );
243 //=======================================================================
244 //function : checkConformIgnoredAlgos
246 //=======================================================================
248 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
249 SMESH_subMesh* aSubMesh,
250 const SMESH_Algo* aGlobIgnoAlgo,
251 const SMESH_Algo* aLocIgnoAlgo,
253 map<int, SMESH_subMesh*>& aCheckedMap)
256 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
262 const list<const SMESHDS_Hypothesis*>& listHyp =
263 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
264 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
265 for ( ; it != listHyp.end(); it++)
267 const SMESHDS_Hypothesis * aHyp = *it;
268 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
271 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
274 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
276 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
277 << aLocIgnoAlgo->GetName() << ">");
281 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
282 int dim = algo->GetDim();
283 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
285 if ( dim < aMaxGlobIgnoDim )
287 // algo is hidden by a global algo
288 INFOS( ( isGlobal ? "Global" : "Local" )
289 << " <" << algo->GetName() << "> is hidden by global <"
290 << aGlobIgnoAlgo->GetName() << ">");
292 else if ( !algo->NeedDescretBoundary() && !isGlobal)
294 // local algo is not hidden and hides algos on sub-shapes
295 if (checkConform && !aSubMesh->IsConform( algo ))
298 checkConform = false; // no more check conformity
299 INFOS( "ERROR: Local <" << algo->GetName() <<
300 "> would produce not conform mesh: "
301 "<Not Conform Mesh Allowed> hypotesis is missing");
304 // sub-algos will be hidden by a local <algo>
305 const map<int, SMESH_subMesh*>& smMap = aSubMesh->DependsOn();
306 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub;
307 bool checkConform2 = false;
308 for ( revItSub = smMap.rbegin(); revItSub != smMap.rend(); revItSub++)
310 checkConformIgnoredAlgos (aMesh, (*revItSub).second, aGlobIgnoAlgo,
311 algo, checkConform2, aCheckedMap);
312 int key = (*revItSub).first;
313 SMESH_subMesh* sm = (*revItSub).second;
314 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
316 aCheckedMap[ key ] = sm;
326 //=======================================================================
327 //function : checkMissing
328 //purpose : notify on missing hypothesis
329 // Return false if algo or hipothesis is missing
330 //=======================================================================
332 static bool checkMissing(SMESH_Gen* aGen,
334 SMESH_subMesh* aSubMesh,
335 const int aTopAlgoDim,
337 const bool checkNoAlgo,
338 map<int, SMESH_subMesh*>& aCheckedMap)
340 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
343 //MESSAGE("=====checkMissing");
346 SMESH_Algo* algo = 0;
348 switch (aSubMesh->GetAlgoState())
350 case SMESH_subMesh::NO_ALGO: {
353 // should there be any algo?
354 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
355 if (aTopAlgoDim > shapeDim)
357 INFOS( "ERROR: " << shapeDim << "D algorithm is missing" );
363 case SMESH_subMesh::MISSING_HYP: {
364 // notify if an algo missing hyp is attached to aSubMesh
365 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
367 bool isGlobalAlgo = aGen->IsGlobalAlgo( algo, aMesh );
368 if (!isGlobalAlgo || !globalChecked[ algo->GetDim() ])
370 INFOS( "ERROR: " << (isGlobalAlgo ? "Global " : "Local ")
371 << "<" << algo->GetName() << "> misses some hypothesis");
373 globalChecked[ algo->GetDim() ] = true;
378 case SMESH_subMesh::HYP_OK:
379 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
385 // do not check under algo that hides sub-algos or
386 // re-start checking NO_ALGO state
388 bool isTopLocalAlgo =
389 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalAlgo( algo, aMesh ));
390 if (!algo->NeedDescretBoundary() || isTopLocalAlgo)
392 bool checkNoAlgo2 = ( algo->NeedDescretBoundary() );
393 const map<int, SMESH_subMesh*>& subMeshes = aSubMesh->DependsOn();
394 map<int, SMESH_subMesh*>::const_iterator itsub;
395 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
397 // sub-meshes should not be checked further more
398 int key = (*itsub).first;
399 SMESH_subMesh* sm = (*itsub).second;
400 if ( aCheckedMap.find( key ) == aCheckedMap.end() )
401 aCheckedMap[ key ] = sm;
405 //check algo on sub-meshes
406 int aTopAlgoDim2 = algo->GetDim();
407 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
408 globalChecked, checkNoAlgo2, aCheckedMap))
411 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
412 checkNoAlgo2 = false;
420 //=======================================================================
421 //function : CheckAlgoState
422 //purpose : notify on bad state of attached algos, return false
423 // if Compute() would fail because of some algo bad state
424 //=======================================================================
426 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
428 //MESSAGE("SMESH_Gen::CheckAlgoState");
431 bool hasAlgo = false;
433 SMESH_subMesh* sm = aMesh.GetSubMesh(aShape);
434 const SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
435 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
441 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
443 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
444 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
445 for ( ; it != listHyp.end(); it++)
447 const SMESHDS_Hypothesis * aHyp = *it;
448 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
451 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
454 int dim = algo->GetDim();
455 aGlobAlgoArr[ dim ] = algo;
460 // --------------------------------------------------------
461 // info on algos that will be ignored because of ones that
462 // don't NeedDescretBoundary() attached to super-shapes,
463 // check that a conform mesh will be produced
464 // --------------------------------------------------------
467 // find a global algo possibly hidding sub-algos
469 const SMESH_Algo* aGlobIgnoAlgo = 0;
470 for (dim = 3; dim > 0; dim--)
472 if (aGlobAlgoArr[ dim ] &&
473 !aGlobAlgoArr[ dim ]->NeedDescretBoundary())
475 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
480 const map<int, SMESH_subMesh*>& smMap = sm->DependsOn();
481 map<int, SMESH_subMesh*>::const_reverse_iterator revItSub = smMap.rbegin();
482 map<int, SMESH_subMesh*> aCheckedMap;
483 bool checkConform = ( !aMesh.IsNotConformAllowed() );
485 SMESH_subMesh* smToCheck = sm;
487 // loop on aShape and its sub-shapes
490 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
493 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
494 if (!checkConformIgnoredAlgos (aMesh, smToCheck, aGlobIgnoAlgo,
495 0, checkConform, aCheckedMap))
498 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
502 if (revItSub != smMap.rend())
504 aKey = (*revItSub).first;
505 smToCheck = (*revItSub).second;
515 // ----------------------------------------------------------------
516 // info on missing hypothesis and find out if all needed algos are
518 // ----------------------------------------------------------------
520 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
522 // find max dim of global algo
524 for (dim = 3; dim > 0; dim--)
526 if (aGlobAlgoArr[ dim ])
534 revItSub = smMap.rbegin();
535 bool checkNoAlgo = (bool) aTopAlgoDim;
536 bool globalChecked[] = { false, false, false, false };
538 // loop on aShape and its sub-shapes
541 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
544 if ( aCheckedMap.find( aKey ) == aCheckedMap.end() )
545 if (!checkMissing (this, aMesh, smToCheck, aTopAlgoDim,
546 globalChecked, checkNoAlgo, aCheckedMap))
549 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
554 if (revItSub != smMap.rend())
556 aKey = (*revItSub).first;
557 smToCheck = (*revItSub).second;
565 INFOS( "None algorithm attached" );
567 return ( ret && hasAlgo );
570 //=======================================================================
571 //function : IsGlobalAlgo
572 //purpose : check if theAlgo is attached to the main shape
573 //=======================================================================
575 bool SMESH_Gen::IsGlobalAlgo(const SMESH_Algo* theAlgo, SMESH_Mesh& aMesh)
577 const SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
578 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
579 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
580 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
581 for ( ; it != listHyp.end(); it++)
582 if ( *it == theAlgo )
589 //=======================================================================
590 //function : getAlgoId
591 //purpose : return algo ID or -1 if not found
592 //=======================================================================
594 static int getAlgo(const list<const SMESHDS_Hypothesis*>& theHypList,
595 const int theAlgoDim,
596 const int theAlgoShapeType)
598 list<const SMESHDS_Hypothesis*>::const_iterator it = theHypList.begin();
603 while (it!=theHypList.end())
605 const SMESH_Hypothesis *anHyp = static_cast< const SMESH_Hypothesis *>( *it );
606 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO &&
607 anHyp->GetDim() == theAlgoDim &&
608 ((anHyp->GetShapeType()) & (1 << theAlgoShapeType)))
611 algo_id = anHyp->GetID();
615 //if (nb_algo > 1) return -1; // more than one algo
622 //=============================================================================
626 //=============================================================================
628 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
630 // MESSAGE("SMESH_Gen::GetAlgo");
632 const SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
633 int dim = GetShapeDim( aShape );
634 int shape_type = aShape.ShapeType();
637 algo_id = getAlgo( meshDS->GetHypothesis( aShape ), dim, shape_type );
642 TopTools_ListIteratorOfListOfShape ancIt( aMesh.GetAncestors( aShape ));
643 for (; ancIt.More(); ancIt.Next())
645 const TopoDS_Shape& ancestor = ancIt.Value();
646 algo_id = getAlgo( meshDS->GetHypothesis( ancestor ), dim, shape_type );
650 if (algo_id < 0) return NULL;
653 ASSERT(_mapAlgo.find(algo_id) != _mapAlgo.end());
655 return _mapAlgo[algo_id];
657 // const SMESHDS_Hypothesis *theHyp = NULL;
658 // SMESH_Algo *algo = NULL;
659 // const SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
664 // // try shape first, then main shape
666 // TopoDS_Shape mainShape = meshDS->ShapeToMesh();
667 // const TopoDS_Shape *shapeToTry[2] = { &aShape, &mainShape };
669 // for (int iShape = 0; iShape < 2; iShape++)
671 // TopoDS_Shape tryShape = (*shapeToTry[iShape]);
673 // const list<const SMESHDS_Hypothesis*>& listHyp =
674 // meshDS->GetHypothesis(tryShape);
675 // list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
678 // int shapeDim = GetShapeDim(aShape);
679 // int typeOfShape = aShape.ShapeType();
681 // while (it!=listHyp.end())
683 // const SMESHDS_Hypothesis *anHyp = *it;
684 // hypType = anHyp->GetType();
685 // //SCRUTE(hypType);
686 // if (hypType > SMESHDS_Hypothesis::PARAM_ALGO)
690 // case SMESHDS_Hypothesis::ALGO_1D:
693 // case SMESHDS_Hypothesis::ALGO_2D:
696 // case SMESHDS_Hypothesis::ALGO_3D:
703 // //SCRUTE(algoDim);
704 // //SCRUTE(shapeDim);
705 // //SCRUTE(typeOfShape);
706 // if (shapeDim == algoDim) // count only algos of shape dim.
707 // { // discard algos for subshapes
708 // hypId = anHyp->GetID(); // (of lower dim.)
709 // ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
710 // SMESH_Algo *anAlgo = _mapAlgo[hypId];
711 // //SCRUTE(anAlgo->GetShapeType());
712 // //if (anAlgo->GetShapeType() == typeOfShape)
713 // if ((anAlgo->GetShapeType()) & (1 << typeOfShape))
714 // { // only specific TopoDS_Shape
720 // if (nb_algo > 1) return NULL; // more than one algo
723 // if (nb_algo == 1) // one algo found : OK
724 // break; // do not try a parent shape
728 // return NULL; // no algo found
730 // hypType = theHyp->GetType();
731 // hypId = theHyp->GetID();
733 // ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
734 // algo = _mapAlgo[hypId];
735 // //MESSAGE("Algo found " << algo->GetName() << " Id " << hypId);
739 //=============================================================================
743 //=============================================================================
745 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
747 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
749 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
751 _mapStudyContext[studyId] = new StudyContextStruct;
752 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
754 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
755 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
756 return myStudyContext;
759 //=============================================================================
763 //=============================================================================
765 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
769 //=============================================================================
773 //=============================================================================
775 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
779 //=============================================================================
783 //=============================================================================
785 void SMESH_Gen::Close(int studyId)
789 //=============================================================================
793 //=============================================================================
795 const char *SMESH_Gen::ComponentDataType()
799 //=============================================================================
803 //=============================================================================
805 const char *SMESH_Gen::IORToLocalPersistentID(const char *IORString,
810 //=============================================================================
814 //=============================================================================
816 const char *SMESH_Gen::LocalPersistentIDToIOR(const char *aLocalPersistentID)
820 //=============================================================================
824 //=============================================================================
826 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
828 int shapeDim = -1; // Shape dimension: 0D, 1D, 2D, 3D
829 int type = aShapeType;//.ShapeType();
832 case TopAbs_COMPOUND:
833 case TopAbs_COMPSOLID:
840 // case TopAbs_SHELL:
861 //=============================================================================
865 //=============================================================================
867 int SMESH_Gen::GetANewId()
869 //MESSAGE("SMESH_Gen::GetANewId");