Salome HOME
sources v1.2
[modules/smesh.git] / src / SMESH / SMESH_Gen.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESH_Gen.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 using namespace std;
31 #include "SMESH_Gen.hxx"
32
33 #include "SMESH_subMesh.hxx"
34
35 #include "SMESHDS_ListOfPtrHypothesis.hxx"
36 #include "SMESHDS_ListIteratorOfListOfPtrHypothesis.hxx"
37 #include "SMDS_MeshElement.hxx"
38 #include "SMDS_MeshNode.hxx"
39
40 #include <gp_Pnt.hxx>
41 #include <BRep_Tool.hxx>
42
43 #include "utilities.h"
44 #include "OpUtil.hxx"
45
46 //=============================================================================
47 /*!
48  *  default constructor:
49  */
50 //=============================================================================
51
52 SMESH_Gen::SMESH_Gen()
53 {
54   MESSAGE("SMESH_Gen::SMESH_Gen");
55   _localId = 0;
56   _hypothesisFactory.SetGen(this);
57 }
58
59 //=============================================================================
60 /*!
61  * 
62  */
63 //=============================================================================
64
65 SMESH_Gen::~SMESH_Gen()
66 {
67   MESSAGE("SMESH_Gen::~SMESH_Gen");
68 }
69
70 //=============================================================================
71 /*!
72  * 
73  */
74 //=============================================================================
75
76 SMESH_Hypothesis* SMESH_Gen::CreateHypothesis(const char* anHyp,
77                                               int studyId)
78   throw (SALOME_Exception)
79 {
80   MESSAGE("SMESH_Gen::CreateHypothesis");
81
82   // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
83
84   StudyContextStruct* myStudyContext = GetStudyContext(studyId);
85
86   // create a new hypothesis object, store its ref. in studyContext
87
88   SMESH_Hypothesis* myHypothesis = _hypothesisFactory.Create(anHyp, studyId);
89   int hypId = myHypothesis->GetID();
90   myStudyContext->mapHypothesis[hypId] = myHypothesis;
91   SCRUTE(studyId);
92   SCRUTE(hypId);
93
94   // store hypothesis in SMESHDS document
95
96   myStudyContext->myDocument->AddHypothesis(myHypothesis);
97 }
98
99 //=============================================================================
100 /*!
101  * 
102  */
103 //=============================================================================
104
105 SMESH_Mesh* SMESH_Gen::Init(int studyId, const TopoDS_Shape& aShape)
106   throw (SALOME_Exception)
107 {
108   MESSAGE("SMESH_Gen::Init");
109 //   if (aShape.ShapeType() == TopAbs_COMPOUND)
110 //     {
111 //       INFOS("Mesh Compound not yet implemented!");
112 //       throw(SALOME_Exception(LOCALIZED("Mesh Compound not yet implemented!")));
113 //     }
114
115   // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
116
117   StudyContextStruct* myStudyContext = GetStudyContext(studyId);
118
119   // create a new SMESH_mesh object 
120
121   SMESH_Mesh* mesh = new SMESH_Mesh(_localId++,
122                                     studyId,
123                                     this,
124                                     myStudyContext->myDocument);
125   myStudyContext->mapMesh[_localId] = mesh;
126
127   // associate a TopoDS_Shape to the mesh
128
129   mesh->ShapeToMesh(aShape);
130   return mesh;
131 }
132
133 //=============================================================================
134 /*!
135  * 
136  */
137 //=============================================================================
138
139 bool SMESH_Gen::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
140   throw (SALOME_Exception)
141 {
142   MESSAGE("SMESH_Gen::Compute");
143 //   bool isDone = false;
144 /* 
145 Algo : s'appuie ou non sur une geometrie
146 Si 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
151 */
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)
159
160   bool ret = true;
161
162   SMESH_subMesh* sm = aMesh.GetSubMesh(aShape);
163 //   SCRUTE(sm);
164   SMESH_subMesh* smToCompute = sm->GetFirstToCompute();
165   while (smToCompute)
166     {
167       TopoDS_Shape subShape = smToCompute->GetSubShape();
168       int dim = GetShapeDim(subShape);
169       //SCRUTE(dim);
170       if (dim > 0)
171         {
172           bool ret1 = smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
173           ret = ret && ret1;
174         }
175       else
176         {
177           ASSERT(dim == 0);
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);
191         }
192       smToCompute = sm->GetFirstToCompute();
193     }
194
195   return ret;
196 }
197
198 //=============================================================================
199 /*!
200  * 
201  */
202 //=============================================================================
203
204 SMESH_Algo* SMESH_Gen::GetAlgo(SMESH_Mesh& aMesh,
205                                const TopoDS_Shape& aShape)
206 {
207   //MESSAGE("SMESH_Gen::GetAlgo");
208
209   SMESHDS_Hypothesis* theHyp = NULL;
210   SMESH_Algo* algo = NULL;
211   const Handle(SMESHDS_Mesh)& meshDS = aMesh.GetMeshDS();
212   int hypType;
213   int hypId;
214   int algoDim;
215
216   // try shape first, then main shape
217
218   TopoDS_Shape mainShape = meshDS->ShapeToMesh();
219   const TopoDS_Shape* shapeToTry[2] = {&aShape, &mainShape};
220
221   for (int iShape=0; iShape<2; iShape++)
222     {
223       TopoDS_Shape tryShape = (*shapeToTry[iShape]);
224
225       const SMESHDS_ListOfPtrHypothesis& listHyp
226         = meshDS->GetHypothesis(tryShape);
227       SMESHDS_ListIteratorOfListOfPtrHypothesis it(listHyp);
228
229       int nb_algo = 0;
230       int shapeDim = GetShapeDim(aShape);
231       int typeOfShape = aShape.ShapeType();
232
233       while (it.More())
234         {
235           SMESHDS_Hypothesis* anHyp = it.Value();
236           hypType = anHyp->GetType();
237 //        SCRUTE(hypType);
238           if (hypType > SMESHDS_Hypothesis::PARAM_ALGO)
239             {
240               switch (hypType)
241                 {
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;
246                 }
247 //            SCRUTE(algoDim);
248 //            SCRUTE(shapeDim);
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
259                       nb_algo++;                  
260                       theHyp = anHyp;
261                     }
262                 }
263             }
264           if (nb_algo > 1) return NULL;  // more than one algo
265           it.Next();
266         }
267       if (nb_algo == 1)                  // one algo found : OK
268         break;                           // do not try a parent shape
269     }
270
271   if (!theHyp) return NULL;              // no algo found
272
273   hypType = theHyp->GetType();
274   hypId = theHyp->GetID();
275
276   ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
277   algo = _mapAlgo[hypId];
278   const char* algoName = algo->GetName();
279   //MESSAGE("Algo found " << algoName << " Id " << hypId);
280   return algo;
281 }
282
283 //=============================================================================
284 /*!
285  * 
286  */
287 //=============================================================================
288
289 StudyContextStruct* SMESH_Gen::GetStudyContext(int studyId)
290 {
291   // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
292
293   if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
294     {
295       _mapStudyContext[studyId] = new StudyContextStruct;
296       _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
297     }
298   StudyContextStruct* myStudyContext = _mapStudyContext[studyId];
299 //   ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
300   return myStudyContext;
301 }
302
303 //=============================================================================
304 /*!
305  * 
306  */
307 //=============================================================================
308
309 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
310 {
311 }
312
313 //=============================================================================
314 /*!
315  * 
316  */
317 //=============================================================================
318
319 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
320 {
321
322
323 //=============================================================================
324 /*!
325  * 
326  */
327 //=============================================================================
328
329 void SMESH_Gen::Close(int studyId)
330 {
331 }
332  
333 //=============================================================================
334 /*!
335  * 
336  */
337 //=============================================================================
338
339 const char* SMESH_Gen::ComponentDataType()
340 {
341 }
342
343
344 //=============================================================================
345 /*!
346  * 
347  */
348 //=============================================================================
349
350 const char* SMESH_Gen::IORToLocalPersistentID(const char* IORString,
351                                               bool& IsAFile)
352 {
353 }
354
355 //=============================================================================
356 /*!
357  * 
358  */
359 //=============================================================================
360
361 const char* SMESH_Gen::LocalPersistentIDToIOR(const char* aLocalPersistentID)
362 {
363 }
364
365 //=============================================================================
366 /*!
367  * 
368  */
369 //=============================================================================
370
371 int SMESH_Gen::GetShapeDim(const TopoDS_Shape& aShape)
372 {
373   int shapeDim = -1;              // Shape dimension: 0D, 1D, 2D, 3D
374   int type = aShape.ShapeType();
375   switch (type)
376     {
377 //     case TopAbs_COMPOUND:
378 //       {
379 //      break;
380 //       }
381     case TopAbs_COMPOUND:
382     case TopAbs_COMPSOLID:
383     case TopAbs_SOLID:
384     case TopAbs_SHELL:
385       {
386         shapeDim = 3;
387         break;
388       }
389       //    case TopAbs_SHELL:
390     case TopAbs_FACE:
391       {
392         shapeDim = 2;
393         break;
394       }
395     case TopAbs_WIRE:
396     case TopAbs_EDGE:
397       {
398         shapeDim = 1;
399         break;
400       }
401     case TopAbs_VERTEX:
402       {
403         shapeDim = 0;
404         break;
405       }
406     }
407 //   SCRUTE(shapeDim);
408   return shapeDim;
409 }