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