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
31 #include "SMESH_Gen.hxx"
33 #include "SMESH_subMesh.hxx"
35 #include "SMESHDS_ListOfPtrHypothesis.hxx"
36 #include "SMESHDS_ListIteratorOfListOfPtrHypothesis.hxx"
37 #include "SMDS_MeshElement.hxx"
38 #include "SMDS_MeshNode.hxx"
41 #include <BRep_Tool.hxx>
43 #include "utilities.h"
46 //=============================================================================
48 * default constructor:
50 //=============================================================================
52 SMESH_Gen::SMESH_Gen()
54 MESSAGE("SMESH_Gen::SMESH_Gen");
56 _hypothesisFactory.SetGen(this);
59 //=============================================================================
63 //=============================================================================
65 SMESH_Gen::~SMESH_Gen()
67 MESSAGE("SMESH_Gen::~SMESH_Gen");
70 //=============================================================================
74 //=============================================================================
76 SMESH_Hypothesis* SMESH_Gen::CreateHypothesis(const char* anHyp,
78 throw (SALOME_Exception)
80 MESSAGE("SMESH_Gen::CreateHypothesis");
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);
99 //=============================================================================
103 //=============================================================================
105 SMESH_Mesh* SMESH_Gen::Init(int studyId, const TopoDS_Shape& aShape)
106 throw (SALOME_Exception)
108 MESSAGE("SMESH_Gen::Init");
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)
140 throw (SALOME_Exception)
142 MESSAGE("SMESH_Gen::Compute");
143 // bool isDone = false;
145 Algo : s'appuie ou non sur une geometrie
147 Vertex : rien à faire (range le point)
148 Edge, Wire, collection d'edge et wire : 1D
149 Face, Shell, collection de Face et Shells : 2D
150 Solid, Collection de Solid : 3D
152 // *** corriger commentaires
153 // check hypothesis associated to the mesh :
154 // - only one algo : type compatible with the type of the shape
155 // - hypothesis = compatible with algo
156 // - check if hypothesis are applicable to this algo
157 // - check contradictions within hypothesis
158 // (test if enough hypothesis is done further)
162 SMESH_subMesh* sm = aMesh.GetSubMesh(aShape);
164 SMESH_subMesh* smToCompute = sm->GetFirstToCompute();
167 TopoDS_Shape subShape = smToCompute->GetSubShape();
168 int dim = GetShapeDim(subShape);
172 bool ret1 = smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
178 ASSERT(smToCompute->_vertexSet == false);
179 TopoDS_Vertex V1 = TopoDS::Vertex(subShape);
180 gp_Pnt P1 = BRep_Tool::Pnt(V1);
181 const Handle(SMESHDS_Mesh)& meshDS = aMesh.GetMeshDS();
182 int nodeId = meshDS->AddNode(P1.X(), P1.Y(), P1.Z());
183 //MESSAGE("point "<<nodeId<<" "<<P1.X()<<" "<<P1.Y()<<" "<<P1.Z());
184 Handle (SMDS_MeshElement) elt = meshDS->FindNode(nodeId);
185 Handle (SMDS_MeshNode) node = meshDS->GetNode(1, elt);
186 meshDS->SetNodeOnVertex(node, V1);
187 const Handle(SMESHDS_SubMesh)& subMeshDS
188 = smToCompute->GetSubMeshDS();
189 smToCompute->_vertexSet = true;
190 bool ret1 = smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
192 smToCompute = sm->GetFirstToCompute();
198 //=============================================================================
202 //=============================================================================
204 SMESH_Algo* SMESH_Gen::GetAlgo(SMESH_Mesh& aMesh,
205 const TopoDS_Shape& aShape)
207 //MESSAGE("SMESH_Gen::GetAlgo");
209 SMESHDS_Hypothesis* theHyp = NULL;
210 SMESH_Algo* algo = NULL;
211 const Handle(SMESHDS_Mesh)& meshDS = aMesh.GetMeshDS();
216 // try shape first, then main shape
218 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
219 const TopoDS_Shape* shapeToTry[2] = {&aShape, &mainShape};
221 for (int iShape=0; iShape<2; iShape++)
223 TopoDS_Shape tryShape = (*shapeToTry[iShape]);
225 const SMESHDS_ListOfPtrHypothesis& listHyp
226 = meshDS->GetHypothesis(tryShape);
227 SMESHDS_ListIteratorOfListOfPtrHypothesis it(listHyp);
230 int shapeDim = GetShapeDim(aShape);
231 int typeOfShape = aShape.ShapeType();
235 SMESHDS_Hypothesis* anHyp = it.Value();
236 hypType = anHyp->GetType();
238 if (hypType > SMESHDS_Hypothesis::PARAM_ALGO)
242 case SMESHDS_Hypothesis::ALGO_1D: algoDim=1; break;
243 case SMESHDS_Hypothesis::ALGO_2D: algoDim=2; break;
244 case SMESHDS_Hypothesis::ALGO_3D: algoDim=3; break;
245 default: algoDim=0; break;
249 // SCRUTE(typeOfShape);
250 if (shapeDim == algoDim) // count only algos of shape dim.
251 { // discard algos for subshapes
252 hypId = anHyp->GetID(); // (of lower dim.)
253 ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
254 SMESH_Algo* anAlgo = _mapAlgo[hypId];
255 //SCRUTE(anAlgo->GetShapeType());
256 // if (anAlgo->GetShapeType() == typeOfShape)
257 if ((anAlgo->GetShapeType()) & (1 << typeOfShape))
258 { // only specific TopoDS_Shape
264 if (nb_algo > 1) return NULL; // more than one algo
267 if (nb_algo == 1) // one algo found : OK
268 break; // do not try a parent shape
271 if (!theHyp) return NULL; // no algo found
273 hypType = theHyp->GetType();
274 hypId = theHyp->GetID();
276 ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
277 algo = _mapAlgo[hypId];
278 const char* algoName = algo->GetName();
279 //MESSAGE("Algo found " << algoName << " Id " << hypId);
283 //=============================================================================
287 //=============================================================================
289 StudyContextStruct* SMESH_Gen::GetStudyContext(int studyId)
291 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
293 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
295 _mapStudyContext[studyId] = new StudyContextStruct;
296 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
298 StudyContextStruct* myStudyContext = _mapStudyContext[studyId];
299 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
300 return myStudyContext;
303 //=============================================================================
307 //=============================================================================
309 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
313 //=============================================================================
317 //=============================================================================
319 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
323 //=============================================================================
327 //=============================================================================
329 void SMESH_Gen::Close(int studyId)
333 //=============================================================================
337 //=============================================================================
339 const char* SMESH_Gen::ComponentDataType()
344 //=============================================================================
348 //=============================================================================
350 const char* SMESH_Gen::IORToLocalPersistentID(const char* IORString,
355 //=============================================================================
359 //=============================================================================
361 const char* SMESH_Gen::LocalPersistentIDToIOR(const char* aLocalPersistentID)
365 //=============================================================================
369 //=============================================================================
371 int SMESH_Gen::GetShapeDim(const TopoDS_Shape& aShape)
373 int shapeDim = -1; // Shape dimension: 0D, 1D, 2D, 3D
374 int type = aShape.ShapeType();
377 // case TopAbs_COMPOUND:
381 case TopAbs_COMPOUND:
382 case TopAbs_COMPSOLID:
389 // case TopAbs_SHELL: