Salome HOME
NRI : First integration.
[modules/smesh.git] / src / SMESH / SMESH_Mesh.cxx
1 using namespace std;
2 //=============================================================================
3 // File      : SMESH_Mesh.cxx
4 // Created   : sam mai 18 08:08:43 CEST 2002
5 // Author    : Paul RASCLE, EDF
6 // Project   : SALOME
7 // Copyright : EDF 2002
8 // $Header$
9 //=============================================================================
10 using namespace std;
11
12 #include "SMESH_Mesh.hxx"
13 #include "SMESH_subMesh.hxx"
14 #include "SMESH_Gen.hxx"
15 #include "SMESH_Hypothesis.hxx"
16 #include "SMESHDS_Script.hxx"
17 //#include "SMESHDS_ListOfAsciiString.hxx"
18 //#include "SMESHDS_ListIteratorOfListOfAsciiString.hxx"
19 #include "SMESHDS_ListOfPtrHypothesis.hxx"
20 #include "SMESHDS_ListIteratorOfListOfPtrHypothesis.hxx"
21 #include "SMDS_MeshElement.hxx"
22 #include "SMDS_MeshFacesIterator.hxx"
23 #include "SMDS_MeshVolumesIterator.hxx"
24 #include "TCollection_AsciiString.hxx"
25
26 #include "utilities.h"
27
28 #include "Mesh_Writer.h"
29 #include "DriverMED_W_SMESHDS_Mesh.h"
30 #include "DriverDAT_W_SMESHDS_Mesh.h"
31 #include "DriverUNV_W_SMESHDS_Mesh.h"
32
33 //=============================================================================
34 /*!
35  * 
36  */
37 //=============================================================================
38
39 SMESH_Mesh::SMESH_Mesh()
40 {
41   MESSAGE("SMESH_Mesh::SMESH_Mesh");
42   _id = -1;
43   ASSERT(0);
44 }
45
46 //=============================================================================
47 /*!
48  * 
49  */
50 //=============================================================================
51
52 SMESH_Mesh::SMESH_Mesh(int localId,
53                        int studyId,
54                        SMESH_Gen* gen,
55                        const Handle(SMESHDS_Document)& myDocument)
56 {
57   MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
58   _id = localId;
59   _studyId = studyId;
60   _gen = gen;
61   _myDocument = myDocument;
62   _idDoc = _myDocument->NewMesh();
63   _myMeshDS = _myDocument->GetMesh(_idDoc);
64   _isShapeToMesh = false;
65 }
66
67 //=============================================================================
68 /*!
69  * 
70  */
71 //=============================================================================
72
73 SMESH_Mesh::~SMESH_Mesh()
74 {
75   MESSAGE("SMESH_Mesh::~SMESH_Mesh");
76 }
77
78 //=============================================================================
79 /*!
80  * 
81  */
82 //=============================================================================
83
84 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape& aShape)
85   throw (SALOME_Exception)
86 {
87   MESSAGE("SMESH_Mesh::ShapeToMesh");
88   if (_isShapeToMesh)
89     throw SALOME_Exception(LOCALIZED("a shape to mesh as already been defined"));
90   _isShapeToMesh = true;
91   _myMeshDS->ShapeToMesh(aShape);
92
93   // NRI : 24/02/03
94   TopExp::MapShapes(aShape,_subShapes);
95 }
96
97 //=============================================================================
98 /*!
99  * 
100  */
101 //=============================================================================
102
103 bool SMESH_Mesh::AddHypothesis(const TopoDS_Shape& aSubShape,
104                                int anHypId)
105   throw (SALOME_Exception)
106 {
107   MESSAGE("SMESH_Mesh::AddHypothesis");
108
109   StudyContextStruct* sc = _gen->GetStudyContext(_studyId);
110   if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
111     {
112       MESSAGE("Hypothesis ID does not give an hypothesis");
113       SCRUTE(_studyId);
114       SCRUTE(anHypId);
115       throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
116     }
117
118   SMESH_subMesh* subMesh = GetSubMesh(aSubShape);
119   SMESH_Hypothesis* anHyp = sc->mapHypothesis[anHypId];
120   int event;
121
122   // shape 
123
124   if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
125     event = SMESH_subMesh::ADD_HYP;
126   else
127     event = SMESH_subMesh::ADD_ALGO;
128   int ret = subMesh->AlgoStateEngine(event, anHyp);
129
130   // subShapes (only when shape is mainShape)
131   TopoDS_Shape mainShape = _myMeshDS->ShapeToMesh();
132   if (aSubShape.IsSame(mainShape))
133     {
134       if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
135         event = SMESH_subMesh::ADD_FATHER_HYP;
136       else
137         event = SMESH_subMesh::ADD_FATHER_ALGO;
138       subMesh->SubMeshesAlgoStateEngine(event, anHyp);
139     }
140
141   subMesh->DumpAlgoState(true);
142   //SCRUTE(ret);
143   return ret;
144 }
145
146 //=============================================================================
147 /*!
148  * 
149  */
150 //=============================================================================
151
152 bool SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape& aSubShape,
153                                   int anHypId)
154   throw (SALOME_Exception)
155 {
156   MESSAGE("SMESH_Mesh::RemoveHypothesis");
157
158   StudyContextStruct* sc = _gen->GetStudyContext(_studyId);
159   if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
160     throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
161
162   SMESH_subMesh* subMesh = GetSubMesh(aSubShape);
163   SMESH_Hypothesis* anHyp = sc->mapHypothesis[anHypId];
164   int hypType = anHyp->GetType();
165   SCRUTE(hypType);
166   int event;
167
168   // shape 
169
170   if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
171     event = SMESH_subMesh::REMOVE_HYP;
172   else
173     event = SMESH_subMesh::REMOVE_ALGO;
174   int ret = subMesh->AlgoStateEngine(event, anHyp);
175
176   // subShapes (only when shape is mainShape)
177
178   TopoDS_Shape mainShape = _myMeshDS->ShapeToMesh();
179   if (aSubShape.IsSame(mainShape))
180     {
181       if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
182         event = SMESH_subMesh::REMOVE_FATHER_HYP;
183       else
184         event = SMESH_subMesh::REMOVE_FATHER_ALGO;
185       subMesh->SubMeshesAlgoStateEngine(event, anHyp);
186     }
187
188   subMesh->DumpAlgoState(true);
189   SCRUTE(ret);
190   return ret;
191 }
192
193 //=============================================================================
194 /*!
195  * 
196  */
197 //=============================================================================
198
199 const Handle(SMESHDS_Mesh)& SMESH_Mesh::GetMeshDS()
200 {
201   return _myMeshDS;
202 }
203
204
205 //=============================================================================
206 /*!
207  * 
208  */
209 //=============================================================================
210
211 const list<SMESHDS_Hypothesis*>&
212 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape& aSubShape)
213   throw (SALOME_Exception)
214 {
215   MESSAGE("SMESH_Mesh::GetHypothesisList");
216   _subShapeHypothesisList.clear();
217   const SMESHDS_ListOfPtrHypothesis& listHyp
218     = _myMeshDS->GetHypothesis(aSubShape);
219   SMESHDS_ListIteratorOfListOfPtrHypothesis it(listHyp);
220   while (it.More())
221     {
222       SMESHDS_Hypothesis* anHyp = it.Value();
223       _subShapeHypothesisList.push_back(anHyp);
224       it.Next();
225     }
226   return _subShapeHypothesisList;
227 }
228
229 //=============================================================================
230 /*!
231  * 
232  */
233 //=============================================================================
234
235 const SMESHDS_ListOfCommand& SMESH_Mesh::GetLog()
236   throw (SALOME_Exception)
237 {
238   MESSAGE("SMESH_Mesh::GetLog");
239   Handle (SMESHDS_Script) scriptDS = _myMeshDS->GetScript();
240   const SMESHDS_ListOfCommand& logDS = scriptDS->GetCommands();
241 //   SMESHDS_ListIteratorOfListOfCommand its;
242 //   const SMESHDS_ListOfAsciiString& logDS = scriptDS->GetCommands();
243 //   SMESHDS_ListIteratorOfListOfAsciiString its;
244 //   for (its.Initialize(logDS); its.More(); its.Next())
245 //     {
246 //       SCRUTE(its.Value().ToCString());
247 //     }
248   return logDS;
249 }
250
251 //=============================================================================
252 /*!
253  * 
254  */
255 //=============================================================================
256 void SMESH_Mesh::ClearLog()
257   throw (SALOME_Exception)
258 {
259   MESSAGE("SMESH_Mesh::ClearLog");
260   Handle (SMESHDS_Script) scriptDS = _myMeshDS->GetScript();
261   scriptDS->Clear();
262 }
263
264 //=============================================================================
265 /*!
266  * 
267  */
268 //=============================================================================
269
270 int SMESH_Mesh::GetId()
271 {
272   MESSAGE("SMESH_Mesh::GetId");
273   return _id;
274 }
275
276 //=============================================================================
277 /*!
278  * 
279  */
280 //=============================================================================
281
282 SMESH_Gen* SMESH_Mesh::GetGen()
283 {
284   return _gen;
285 }
286
287 //=============================================================================
288 /*!
289  * Get or Create the SMESH_subMesh object implementation
290  */
291 //=============================================================================
292
293 SMESH_subMesh* SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
294   throw (SALOME_Exception)
295 {
296   //MESSAGE("SMESH_Mesh::GetSubMesh");
297   SMESH_subMesh* aSubMesh;
298   int index = _subShapes.FindIndex(aSubShape);
299   if ( _mapSubMesh.find(index) != _mapSubMesh.end() ) {
300     aSubMesh = _mapSubMesh[index];
301   } else {
302     aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
303     _mapSubMesh[index] = aSubMesh;
304   }
305
306   /* NRI 24/02/2003
307   int index = -1;
308   if (_subShapes.Contains(aSubShape))
309     {
310       index = _subShapes.FindIndex(aSubShape);
311       ASSERT(_mapSubMesh.find(index) != _mapSubMesh.end());
312       aSubMesh = _mapSubMesh[index];
313       //MESSAGE("found submesh " << index);
314     }
315   else
316     {
317       index = _subShapes.Add(aSubShape);
318       aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
319       _mapSubMesh[index] = aSubMesh;
320       //MESSAGE("created submesh " << index);
321     }
322   */
323   return aSubMesh;
324 }
325
326 //=============================================================================
327 /*!
328  * Get the SMESH_subMesh object implementation. Dont create it, return null
329  * if it does not exist.
330  */
331 //=============================================================================
332 //
333 //  * Given a subShape, find if there is a subMesh associated to this subShape
334 //  * or to a collection of shapes containing this subShape. Collection =
335 //  * compsolid, shell, wire.
336 //  *
337 //  * WARNING : with arg = compsolid, shell or wire returns always NULL.
338 //  * with a face inside a shell, and submesh created for both, if arg is face,
339 //  * returns first created submesh of the two. 
340 //  * subMesh is not created, return may be NULL.
341
342 SMESH_subMesh* SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape)
343   throw (SALOME_Exception)
344 {
345   //MESSAGE("SMESH_Mesh::GetSubMeshContaining");
346   bool isFound = false;
347   SMESH_subMesh* aSubMesh = NULL;
348
349   int index = _subShapes.FindIndex(aSubShape);
350   if ( _mapSubMesh.find(index) != _mapSubMesh.end() ) {
351     aSubMesh = _mapSubMesh[index];
352     isFound = true;
353   } 
354
355   /* NRI 24/02/2003
356   int index = -1;
357   if (_subShapes.Contains(aSubShape))
358     {
359       index = _subShapes.FindIndex(aSubShape);
360       ASSERT(_mapSubMesh.find(index) != _mapSubMesh.end());
361       aSubMesh = _mapSubMesh[index];
362       isFound = true;
363       //MESSAGE("found submesh " << index);
364     }
365   */
366
367 //   map<int, SMESH_subMesh*>::iterator itsm;
368 //   for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
369 //     {
370 //       aSubMesh = (*itsm).second;
371 //       isFound = aSubMesh->Contains(aSubShape);
372 //       if (isFound) break;
373 //     }
374
375   if (! isFound) aSubMesh = NULL;
376   return aSubMesh;
377 }
378
379 //=============================================================================
380 /*!
381  *
382  */
383 //=============================================================================
384
385 const list <SMESH_subMesh*>&
386 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis* anHyp)
387   throw (SALOME_Exception)
388 {
389   MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
390   map<int, SMESH_subMesh*>::iterator itsm;
391   _subMeshesUsingHypothesisList.clear();
392   for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
393     {
394       SMESH_subMesh* aSubMesh = (*itsm).second;
395       bool usesHyp = false;
396       SMESH_Algo* algo = _gen->GetAlgo(*this, aSubMesh->GetSubShape());
397       if (algo != NULL)
398         {
399           const list<SMESHDS_Hypothesis*>& usedHyps
400             = algo->GetUsedHypothesis(*this, aSubMesh->GetSubShape());
401           list<SMESHDS_Hypothesis*>::const_iterator itl;
402           for(itl=usedHyps.begin(); itl != usedHyps.end(); itl++)
403             if (anHyp == (*itl))
404               {
405                 usesHyp = true;
406                 break;
407               }     
408         }
409       if (usesHyp) _subMeshesUsingHypothesisList.push_back(aSubMesh);
410     }
411   return _subMeshesUsingHypothesisList;
412 }
413
414 //=============================================================================
415 /*!
416  *
417  */
418 //=============================================================================
419
420 void SMESH_Mesh::ExportMED( const char* file )
421   throw (SALOME_Exception)
422 {
423   Mesh_Writer* myWriter = new DriverMED_W_SMESHDS_Mesh;
424   myWriter->SetFile( string(file) );
425   myWriter->SetMesh( _myMeshDS );
426   MESSAGE ( " _idDoc " << _idDoc )
427   myWriter->SetMeshId( _idDoc );
428   myWriter->Add();
429 }
430
431 void SMESH_Mesh::ExportDAT( const char* file )
432   throw (SALOME_Exception)
433 {
434   Mesh_Writer* myWriter = new DriverDAT_W_SMESHDS_Mesh;
435   myWriter->SetFile( string(file) );
436   myWriter->SetMesh( _myMeshDS );
437   myWriter->SetMeshId( _idDoc );
438   myWriter->Add();
439 }
440
441 void SMESH_Mesh::ExportUNV( const char* file )
442   throw (SALOME_Exception)
443 {
444   Mesh_Writer* myWriter = new DriverUNV_W_SMESHDS_Mesh;
445   myWriter->SetFile( string(file) );
446   myWriter->SetMesh( _myMeshDS );
447   myWriter->SetMeshId( _idDoc );
448   myWriter->Add();
449 }
450
451 //=============================================================================
452 /*!
453  *  
454  */
455 //=============================================================================
456 int SMESH_Mesh::NbNodes()
457   throw (SALOME_Exception)
458 {
459   return _myMeshDS->NbNodes();
460 }
461
462 //=============================================================================
463 /*!
464  *  
465  */
466 //=============================================================================
467 int SMESH_Mesh::NbEdges()
468   throw (SALOME_Exception)
469 {
470   return _myMeshDS->NbEdges();
471 }
472
473 //=============================================================================
474 /*!
475  *  
476  */
477 //=============================================================================
478 int SMESH_Mesh::NbFaces()
479   throw (SALOME_Exception)
480 {
481   return _myMeshDS->NbFaces();
482 }
483 int SMESH_Mesh::NbTriangles()
484   throw (SALOME_Exception)
485 {
486   SMDS_MeshFacesIterator itFaces(_myMeshDS);
487   int Nb = 0;
488   for (;itFaces.More();itFaces.Next()) {
489     const Handle(SMDS_MeshElement)& elem = itFaces.Value();
490
491     switch (elem->NbNodes()) {
492     case 3 : {
493       Nb++;
494       break;
495     }
496     }
497   }
498   return Nb;
499 }
500 int SMESH_Mesh::NbQuadrangles()
501   throw (SALOME_Exception)
502 {
503   SMDS_MeshFacesIterator itFaces(_myMeshDS);
504   int Nb = 0;
505   for (;itFaces.More();itFaces.Next()) {
506     const Handle(SMDS_MeshElement)& elem = itFaces.Value();
507     
508     switch (elem->NbNodes()) {
509     case 4 : {
510       Nb++;
511       break;
512     }
513     }
514   }
515   return Nb;
516 }
517
518 //=============================================================================
519 /*!
520  *  
521  */
522 //=============================================================================
523 int SMESH_Mesh::NbVolumes()
524   throw (SALOME_Exception)
525 {
526   return _myMeshDS->NbVolumes();
527 }
528 int SMESH_Mesh::NbTetras()
529   throw (SALOME_Exception)
530 {
531   int Nb = 0;
532   SMDS_MeshVolumesIterator itVolumes(_myMeshDS);
533   for (;itVolumes.More();itVolumes.Next()) {
534     const Handle(SMDS_MeshElement)& elem = itVolumes.Value();
535
536     switch (elem->NbNodes()) {
537     case 4 : {
538       Nb++;
539       break;
540     }
541     }
542   }
543   return Nb;
544 }
545 int SMESH_Mesh::NbHexas()
546   throw (SALOME_Exception)
547 {
548   int Nb = 0;
549   SMDS_MeshVolumesIterator itVolumes(_myMeshDS);
550   for (;itVolumes.More();itVolumes.Next()) {
551     const Handle(SMDS_MeshElement)& elem = itVolumes.Value();
552
553     switch (elem->NbNodes()) {
554     case 8 : {
555       Nb++;
556       break;
557     }
558     }
559   }
560   return Nb;
561 }
562
563 //=============================================================================
564 /*!
565  *  
566  */
567 //=============================================================================
568 int SMESH_Mesh::NbSubMesh()
569   throw (SALOME_Exception)
570 {
571   return _myMeshDS->NbSubMesh();
572 }