Salome HOME
Creating and importing 2 meshes from the same engine was not working. Mesh ID managem...
[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 #include "SMESH_Gen.hxx"
30 #include "SMESH_subMesh.hxx"
31 #include "SMDS_MeshElement.hxx"
32 #include "SMDS_MeshNode.hxx"
33 #include "SMESHDriver.h"
34
35 #include <gp_Pnt.hxx>
36 #include <BRep_Tool.hxx>
37
38 #include "utilities.h"
39 #include "OpUtil.hxx"
40
41 //=============================================================================
42 /*!
43  *  default constructor:
44  */
45 //=============================================================================
46
47 SMESH_Gen::SMESH_Gen()
48 {
49         MESSAGE("SMESH_Gen::SMESH_Gen");
50         _localId = 0;
51         _hypothesisFactory.SetGen(this);
52 }
53
54 //=============================================================================
55 /*!
56  * 
57  */
58 //=============================================================================
59
60 SMESH_Gen::~SMESH_Gen()
61 {
62         MESSAGE("SMESH_Gen::~SMESH_Gen");
63 }
64
65 //=============================================================================
66 /*!
67  * 
68  */
69 //=============================================================================
70
71 SMESH_Hypothesis *SMESH_Gen::CreateHypothesis(const char *anHyp, int studyId)
72         throw(SALOME_Exception)
73 {
74
75         MESSAGE("CreateHypothesis("<<anHyp<<","<<studyId<<")");
76         // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
77
78         StudyContextStruct *myStudyContext = GetStudyContext(studyId);
79
80         // create a new hypothesis object, store its ref. in studyContext
81
82         SMESH_Hypothesis *myHypothesis = _hypothesisFactory.Create(anHyp, studyId);
83         int hypId = myHypothesis->GetID();
84         myStudyContext->mapHypothesis[hypId] = myHypothesis;
85         SCRUTE(studyId);
86         SCRUTE(hypId);
87
88         // store hypothesis in SMESHDS document
89
90         myStudyContext->myDocument->AddHypothesis(myHypothesis);
91         return myHypothesis;
92 }
93
94 //=============================================================================
95 /*!
96  * 
97  */
98 //=============================================================================
99
100 SMESH_Mesh *SMESH_Gen::Init(int studyId, const TopoDS_Shape & aShape, int meshID)
101         throw(SALOME_Exception)
102 {
103         MESSAGE("SMESH_Gen::Init");
104 //   if (aShape.ShapeType() == TopAbs_COMPOUND)
105 //     {
106 //       INFOS("Mesh Compound not yet implemented!");
107 //       throw(SALOME_Exception(LOCALIZED("Mesh Compound not yet implemented!")));
108 //     }
109
110         // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
111
112         StudyContextStruct *myStudyContext = GetStudyContext(studyId);
113
114         // create a new SMESH_mesh object 
115
116         if(meshID == -1)
117                 meshID=_localId++;
118         else if(_localId<=meshID)
119                 _localId=meshID+1;
120
121         SMESH_Mesh *mesh = new SMESH_Mesh(meshID,
122                 studyId,
123                 this,
124                 myStudyContext->myDocument);
125         myStudyContext->mapMesh[meshID] = mesh;
126
127         // associate a TopoDS_Shape to the mesh
128
129         mesh->ShapeToMesh(aShape);
130         return mesh;
131 }
132
133 //=============================================================================
134 /*!
135  * @TODO Doing a full update after computation is not optimal when doing a local
136  * remeshing.
137  */
138 //=============================================================================
139
140 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
141 throw(SALOME_Exception)
142 {
143         MESSAGE("SMESH_Gen::Compute");
144 //   bool isDone = false;
145 /* 
146 Algo : s'appuie ou non sur une geometrie
147 Si geometrie:
148 Vertex : rien �faire (range le point)
149 Edge, Wire, collection d'edge et wire : 1D
150 Face, Shell, collection de Face et Shells : 2D
151 Solid, Collection de Solid : 3D
152 */
153 // *** corriger commentaires
154         // check hypothesis associated to the mesh :
155         // - only one algo : type compatible with the type of the shape
156         // - hypothesis = compatible with algo
157         //    - check if hypothesis are applicable to this algo
158         //    - check contradictions within hypothesis
159         //    (test if enough hypothesis is done further)
160
161         bool ret = true;
162
163         SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
164 //   SCRUTE(sm);
165         SMESH_subMesh *smToCompute = sm->GetFirstToCompute();
166         while (smToCompute)
167         {
168                 TopoDS_Shape subShape = smToCompute->GetSubShape();
169                 int dim = GetShapeDim(subShape);
170                 //SCRUTE(dim);
171                 if (dim > 0)
172                 {
173                         bool ret1 = smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
174                         ret = ret && ret1;
175                 }
176                 else
177                 {
178                         ASSERT(dim == 0);
179                         ASSERT(smToCompute->_vertexSet == false);
180                         TopoDS_Vertex V1 = TopoDS::Vertex(subShape);
181                         gp_Pnt P1 = BRep_Tool::Pnt(V1);
182                         SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
183                         //MESSAGE("point "<<nodeId<<" "<<P1.X()<<" "<<P1.Y()<<" "<<P1.Z());
184                         SMDS_MeshNode * node = meshDS->AddNode(P1.X(), P1.Y(), P1.Z());
185                         meshDS->SetNodeOnVertex(node, V1);
186                         smToCompute->GetSubMeshDS();
187                         smToCompute->_vertexSet = true;
188                         smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
189                 }
190                 smToCompute = sm->GetFirstToCompute();
191         }
192
193         aMesh.GetMeshDS()->logFullUpdate();
194
195         return ret;
196 }
197
198 //=============================================================================
199 /*!
200  * 
201  */
202 //=============================================================================
203
204 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
205 {
206         //MESSAGE("SMESH_Gen::GetAlgo");
207
208         const SMESHDS_Hypothesis *theHyp = NULL;
209         SMESH_Algo *algo = NULL;
210         const SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
211         int hypType;
212         int hypId;
213         int algoDim;
214
215         // try shape first, then main shape
216
217         TopoDS_Shape mainShape = meshDS->ShapeToMesh();
218         const TopoDS_Shape *shapeToTry[2] = { &aShape, &mainShape };
219
220         for (int iShape = 0; iShape < 2; iShape++)
221         {
222                 TopoDS_Shape tryShape = (*shapeToTry[iShape]);
223
224                 const list<const SMESHDS_Hypothesis*>& listHyp =
225                         meshDS->GetHypothesis(tryShape);
226                 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
227                 
228                 int nb_algo = 0;
229                 int shapeDim = GetShapeDim(aShape);
230                 int typeOfShape = aShape.ShapeType();
231
232                 while (it!=listHyp.end())
233                 {
234                         const SMESHDS_Hypothesis *anHyp = *it;
235                         hypType = anHyp->GetType();
236                         //SCRUTE(hypType);
237                         if (hypType > SMESHDS_Hypothesis::PARAM_ALGO)
238                         {
239                                 switch (hypType)
240                                 {
241                                 case SMESHDS_Hypothesis::ALGO_1D:
242                                         algoDim = 1;
243                                         break;
244                                 case SMESHDS_Hypothesis::ALGO_2D:
245                                         algoDim = 2;
246                                         break;
247                                 case SMESHDS_Hypothesis::ALGO_3D:
248                                         algoDim = 3;
249                                         break;
250                                 default:
251                                         algoDim = 0;
252                                         break;
253                                 }
254                                 //SCRUTE(algoDim);
255                                 //SCRUTE(shapeDim);
256                                 //SCRUTE(typeOfShape);
257                                 if (shapeDim == algoDim)        // count only algos of shape dim.
258                                 {                               // discard algos for subshapes
259                                         hypId = anHyp->GetID(); // (of lower dim.)
260                                         ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
261                                         SMESH_Algo *anAlgo = _mapAlgo[hypId];
262                                         //SCRUTE(anAlgo->GetShapeType());
263                                         //if (anAlgo->GetShapeType() == typeOfShape)
264                                         if ((anAlgo->GetShapeType()) & (1 << typeOfShape))
265                                         {                       // only specific TopoDS_Shape
266                                                 nb_algo++;
267                                                 theHyp = anHyp;
268                                         }
269                                 }
270                         }
271                         if (nb_algo > 1) return NULL;   // more than one algo
272                         it++;
273                 }
274                 if (nb_algo == 1)               // one algo found : OK
275                         break;                          // do not try a parent shape
276         }
277
278         if (!theHyp)
279                 return NULL;                    // no algo found
280
281         hypType = theHyp->GetType();
282         hypId = theHyp->GetID();
283
284         ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
285         algo = _mapAlgo[hypId];
286         //MESSAGE("Algo found " << algo->GetName() << " Id " << hypId);
287         return algo;
288 }
289
290 //=============================================================================
291 /*!
292  * 
293  */
294 //=============================================================================
295
296 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
297 {
298         // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
299
300         if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
301         {
302                 _mapStudyContext[studyId] = new StudyContextStruct;
303                 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
304         }
305         StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
306 //   ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
307         return myStudyContext;
308 }
309
310 //=============================================================================
311 /*!
312  * 
313  */
314 //=============================================================================
315
316 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
317 {
318 }
319
320 //=============================================================================
321 /*!
322  * 
323  */
324 //=============================================================================
325
326 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
327 {
328 }
329
330 //=============================================================================
331 /*!
332  * 
333  */
334 //=============================================================================
335
336 void SMESH_Gen::Close(int studyId)
337 {
338 }
339
340 //=============================================================================
341 /*!
342  * 
343  */
344 //=============================================================================
345
346 const char *SMESH_Gen::ComponentDataType()
347 {
348 }
349
350 //=============================================================================
351 /*!
352  * 
353  */
354 //=============================================================================
355
356 const char *SMESH_Gen::IORToLocalPersistentID(const char *IORString,
357         bool & IsAFile)
358 {
359 }
360
361 //=============================================================================
362 /*!
363  * 
364  */
365 //=============================================================================
366
367 const char *SMESH_Gen::LocalPersistentIDToIOR(const char *aLocalPersistentID)
368 {
369 }
370
371 //=============================================================================
372 /*!
373  * 
374  */
375 //=============================================================================
376
377 int SMESH_Gen::GetShapeDim(const TopoDS_Shape & aShape)
378 {
379         int shapeDim = -1;                      // Shape dimension: 0D, 1D, 2D, 3D
380         int type = aShape.ShapeType();
381         switch (type)
382         {
383 //     case TopAbs_COMPOUND:
384 //       {
385 //  break;
386 //       }
387         case TopAbs_COMPOUND:
388         case TopAbs_COMPSOLID:
389         case TopAbs_SOLID:
390         case TopAbs_SHELL:
391         {
392                 shapeDim = 3;
393                 break;
394         }
395                 //    case TopAbs_SHELL:
396         case TopAbs_FACE:
397         {
398                 shapeDim = 2;
399                 break;
400         }
401         case TopAbs_WIRE:
402         case TopAbs_EDGE:
403         {
404                 shapeDim = 1;
405                 break;
406         }
407         case TopAbs_VERTEX:
408         {
409                 shapeDim = 0;
410                 break;
411         }
412         }
413 //   SCRUTE(shapeDim);
414         return shapeDim;
415 }
416
417 /**
418  * Import a mesh from a file
419  * @param fileName file name to be imported
420  * @param fileType Currently it could be either "DAT", "UNV" or "MED".
421  * @todo
422  */
423 SMESH_Mesh * SMESH_Gen::Import(int studyId, const char * fileName,
424         const char * fileType)
425 {
426         MESSAGE("SMESH_Gen::Import("<<studyId<<","<<fileName<<","<<fileType<<")");
427
428         // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
429         StudyContextStruct *myStudyContext = GetStudyContext(studyId);
430
431         // will be used with document
432         /*Document_Reader * reader = SMESHDriver::GetDocumentReader(string(fileType));
433         reader->SetDocument(myStudyContext->myDocument);
434         reader->SetFile(string(fileName));
435         reader->Read();*/
436         // currently we only read one mesh from a file (limitation on MED files).
437
438         // create a new SMESH_mesh object 
439         SMESH_Mesh *mesh = new SMESH_Mesh(_localId++, studyId, this,
440                 myStudyContext->myDocument);
441         myStudyContext->mapMesh[_localId] = mesh;
442         
443         Mesh_Reader * reader = SMESHDriver::GetMeshReader(string(fileType));
444         reader->SetMesh(mesh->GetMeshDS());
445         reader->SetFile(string(fileName));
446         reader->Read();
447         
448         mesh->GetMeshDS()->logFullUpdate();
449         
450         return mesh;
451 }
452