Salome HOME
correct a small bug found by the EDF developpement team (PN and AT) :
[modules/smesh.git] / src / SMESH_I / SMESH_Mesh_i.cxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
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_Mesh_i.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 using namespace std;
30 #include "SMESH_Mesh_i.hxx"
31 #include "SMESH_subMesh_i.hxx"
32 #include "SMESH_MEDMesh_i.hxx"
33
34 #include "Utils_CorbaException.hxx"
35 #include "utilities.h"
36
37 #include "SALOME_NamingService.hxx"
38 #include "Utils_SINGLETON.hxx"
39 #include "OpUtil.hxx"
40
41 #include "TCollection_AsciiString.hxx"
42 #include "SMESHDS_Command.hxx"
43 #include "SMESHDS_CommandType.hxx"
44 #include "SMESH_MeshEditor_i.hxx"
45
46 #include <string>
47 #include <iostream>
48
49 //**** SMESHDS en champ
50
51 //=============================================================================
52 /*!
53  *  
54  */
55 //=============================================================================
56
57 SMESH_Mesh_i::SMESH_Mesh_i()
58 {
59         MESSAGE("SMESH_Mesh_i: default constructor, not for use");
60         ASSERT(0);
61 };
62
63 //=============================================================================
64 /*!
65  *  Constructor
66  */
67 //=============================================================================
68
69 SMESH_Mesh_i::SMESH_Mesh_i(SMESH_Gen_i * gen_i,
70         GEOM::GEOM_Gen_ptr geomEngine, CORBA::Long studyId, int localId)
71 {
72         MESSAGE("SMESH_Mesh_i");
73         _gen_i = gen_i;
74         _id = localId;
75         _geom = GEOM::GEOM_Gen::_narrow(geomEngine);
76         _studyId = studyId;
77 }
78
79 //=============================================================================
80 /*!
81  *  Destructor
82  */
83 //=============================================================================
84
85 SMESH_Mesh_i::~SMESH_Mesh_i()
86 {
87         MESSAGE("~SMESH_Mesh_i");
88         // ****
89 };
90
91 //=============================================================================
92 /*!
93  *  
94  */
95 //=============================================================================
96
97 CORBA::Boolean SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Shape_ptr aSubShape,
98         SMESH::SMESH_Hypothesis_ptr anHyp) throw(SALOME::SALOME_Exception)
99 {
100         MESSAGE("AddHypothesis");
101         // **** proposer liste de subShape (selection multiple)
102
103         GEOM::GEOM_Shape_var mySubShape = GEOM::GEOM_Shape::_narrow(aSubShape);
104         if (CORBA::is_nil(mySubShape))
105                 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
106                         SALOME::BAD_PARAM);
107
108         SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
109         if (CORBA::is_nil(myHyp))
110                 THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
111                         SALOME::BAD_PARAM);
112         bool ret = false;
113         try
114         {
115                 TopoDS_Shape myLocSubShape =
116                         _gen_i->_ShapeReader->GetShape(_geom, mySubShape);
117                 int hypId = myHyp->GetId();
118                 ret = _impl->AddHypothesis(myLocSubShape, hypId);
119         }
120         catch(SALOME_Exception & S_ex)
121         {
122                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
123         }
124         return ret;
125 };
126
127 //=============================================================================
128 /*!
129  *  
130  */
131 //=============================================================================
132
133 CORBA::Boolean
134         SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Shape_ptr aSubShape,
135         SMESH::SMESH_Hypothesis_ptr anHyp) throw(SALOME::SALOME_Exception)
136 {
137         MESSAGE("RemoveHypothesis");
138         // **** proposer liste de subShape (selection multiple)
139
140         GEOM::GEOM_Shape_var mySubShape = GEOM::GEOM_Shape::_narrow(aSubShape);
141         if (CORBA::is_nil(mySubShape))
142                 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
143                         SALOME::BAD_PARAM);
144
145         SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
146         if (CORBA::is_nil(myHyp))
147                 THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
148                         SALOME::BAD_PARAM);
149
150         bool ret = false;
151         try
152         {
153                 TopoDS_Shape myLocSubShape =
154                         _gen_i->_ShapeReader->GetShape(_geom, mySubShape);
155                 int hypId = myHyp->GetId();
156                 ret = _impl->RemoveHypothesis(myLocSubShape, hypId);
157         }
158         catch(SALOME_Exception & S_ex)
159         {
160                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
161         }
162         return ret;
163 };
164
165 //=============================================================================
166 /*!
167  *  
168  */
169 //=============================================================================
170
171 SMESH::ListOfHypothesis *
172         SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Shape_ptr aSubShape)
173 throw(SALOME::SALOME_Exception)
174 {
175         MESSAGE("GetHypothesisList");
176         // ****
177 };
178
179 //=============================================================================
180 /*!
181  *  
182  */
183 //=============================================================================
184 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetElementsOnShape(GEOM::
185         GEOM_Shape_ptr aSubShape) throw(SALOME::SALOME_Exception)
186 {
187         MESSAGE("SMESH_Mesh_i::GetElementsOnShape");
188         GEOM::GEOM_Shape_var mySubShape = GEOM::GEOM_Shape::_narrow(aSubShape);
189         if (CORBA::is_nil(mySubShape))
190                 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
191                         SALOME::BAD_PARAM);
192
193         int subMeshId = 0;
194         try
195         {
196                 SMESH_subMesh_i *subMeshServant;
197                 TopoDS_Shape myLocSubShape
198                         = _gen_i->_ShapeReader->GetShape(_geom, mySubShape);
199
200                 //Get or Create the SMESH_subMesh object implementation
201
202                 ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
203                 subMeshId = mySubMesh->GetId();
204
205                 // create a new subMesh object servant if there is none for the shape
206
207                 if (_mapSubMesh.find(subMeshId) != _mapSubMesh.end())
208                 {
209                         ASSERT(_mapSubMesh_i.find(subMeshId) != _mapSubMesh_i.end());
210                         subMeshServant = _mapSubMesh_i[subMeshId];
211                 }
212                 else
213                 {
214                         // create and activate the CORBA servant of Mesh
215                         subMeshServant = new SMESH_subMesh_i(_gen_i, this, subMeshId);
216                         SMESH::SMESH_subMesh_var subMesh
217                                 = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
218                         _mapSubMesh[subMeshId] = mySubMesh;
219                         _mapSubMesh_i[subMeshId] = subMeshServant;
220                         _mapSubMeshIor[subMeshId]
221                                 = SMESH::SMESH_subMesh::_duplicate(subMesh);
222                 }
223         }
224         catch(SALOME_Exception & S_ex)
225         {
226                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
227         }
228
229         ASSERT(_mapSubMeshIor.find(subMeshId) != _mapSubMeshIor.end());
230         return SMESH::SMESH_subMesh::_duplicate(_mapSubMeshIor[subMeshId]);
231 }
232
233 //=============================================================================
234 /*!
235  *  
236  */
237 //=============================================================================
238
239 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
240 throw(SALOME::SALOME_Exception)
241 {
242         MESSAGE("SMESH_Mesh_i::GetLog");
243
244         SMESH::log_array_var aLog;
245         try
246         {
247                 list < SMESHDS_Command * >logDS = _impl->GetLog();
248                 aLog = new SMESH::log_array;
249                 int indexLog = 0;
250                 int lg = logDS.size();
251                 SCRUTE(lg);
252                 aLog->length(lg);
253                 list < SMESHDS_Command * >::iterator its = logDS.begin();
254                 while (its != logDS.end())
255                 {
256                         SMESHDS_Command *com = *its;
257                         int comType = com->GetType();
258                         //SCRUTE(comType);
259                         int lgcom = com->GetNumber();
260                         //SCRUTE(lgcom);
261                         const list < int >&intList = com->GetIndexes();
262                         int inum = intList.size();
263                         //SCRUTE(inum);
264                         list < int >::const_iterator ii = intList.begin();
265                         const list < double >&coordList = com->GetCoords();
266                         int rnum = coordList.size();
267                         //SCRUTE(rnum);
268                         list < double >::const_iterator ir = coordList.begin();
269                         aLog[indexLog].commandType = comType;
270                         aLog[indexLog].number = lgcom;
271                         aLog[indexLog].coords.length(rnum);
272                         aLog[indexLog].indexes.length(inum);
273                         for (int i = 0; i < rnum; i++)
274                         {
275                                 aLog[indexLog].coords[i] = *ir;
276                                 //MESSAGE(" "<<i<<" "<<ir.Value());
277                                 ir++;
278                         }
279                         for (int i = 0; i < inum; i++)
280                         {
281                                 aLog[indexLog].indexes[i] = *ii;
282                                 //MESSAGE(" "<<i<<" "<<ii.Value());
283                                 ii++;
284                         }
285                         indexLog++;
286                         its++;
287                 }
288                 if (clearAfterGet)
289                         _impl->ClearLog();
290         }
291         catch(SALOME_Exception & S_ex)
292         {
293                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
294         }
295         return aLog._retn();
296 }
297
298 //  SMESH::string_array* SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
299 //    throw (SALOME::SALOME_Exception)
300 //  {
301 //    MESSAGE("SMESH_Mesh_i::GetLog");
302
303 //    SMESH::string_array_var aLog;
304 //    try
305 //      {
306 //        const SMESHDS_ListOfCommand& logDS =_impl->GetLog();
307 //        aLog = new SMESH::string_array;
308 //        int logSize = 0;
309 //        int indexLog = 0;
310 //        int lg = logDS.Extent();
311 //        SCRUTE(lg);
312 //        SMESHDS_ListIteratorOfListOfCommand its(logDS);
313 //        while(its.More())
314 //        {
315 //      Handle(SMESHDS_Command) com = its.Value();
316 //      int comType = com->GetType();
317 //      SCRUTE(comType);
318 //      int lgcom = com->GetNumber();
319 //      SCRUTE(lgcom);
320 //      logSize += lgcom;
321 //      aLog->length(logSize);
322 //      SCRUTE(logSize);
323 //      const TColStd_ListOfInteger& intList = com->GetIndexes();
324 //      TColStd_ListIteratorOfListOfInteger ii(intList);
325 //      const TColStd_ListOfReal& coordList = com->GetCoords();
326 //      TColStd_ListIteratorOfListOfReal ir(coordList);
327 //      for (int icom = 1; icom <= lgcom; icom++)
328 //        {
329 //          ostringstream S;
330 //          switch (comType)
331 //            {
332 //            case SMESHDS_AddNode:
333 //          S << "AddNode " << ii.Value(); ii.Next();
334 //          S << " " << ir.Value(); ir.Next(); 
335 //          S << " " << ir.Value(); ir.Next();
336 //          S << " " << ir.Value(); ir.Next();
337 //          break;
338 //            case SMESHDS_AddEdge:
339 //          S << "AddEdge " << ii.Value(); ii.Next();
340 //          S << " " << ii.Value(); ii.Next();
341 //          S << " " << ii.Value(); ii.Next();
342 //          break;
343 //            case SMESHDS_AddTriangle:
344 //          S << "AddFace " << ii.Value(); ii.Next();
345 //          S << " " << ii.Value(); ii.Next();
346 //          S << " " << ii.Value(); ii.Next();
347 //          S << " " << ii.Value(); ii.Next();
348 //          break;
349 //            case SMESHDS_AddQuadrangle:
350 //          S << "AddFace " << ii.Value(); ii.Next();
351 //          S << " " << ii.Value(); ii.Next();
352 //          S << " " << ii.Value(); ii.Next();
353 //          S << " " << ii.Value(); ii.Next();
354 //          S << " " << ii.Value(); ii.Next();
355 //          break;
356 //            case SMESHDS_AddTetrahedron:
357 //          S << "AddVolume " << ii.Value(); ii.Next();
358 //          S << " " << ii.Value(); ii.Next();
359 //          S << " " << ii.Value(); ii.Next();
360 //          S << " " << ii.Value(); ii.Next();
361 //          S << " " << ii.Value(); ii.Next();
362 //          break;
363 //            case SMESHDS_AddPyramid:
364 //          S << "AddVolume " << ii.Value(); ii.Next();
365 //          S << " " << ii.Value(); ii.Next();
366 //          S << " " << ii.Value(); ii.Next();
367 //          S << " " << ii.Value(); ii.Next();
368 //          S << " " << ii.Value(); ii.Next();
369 //          S << " " << ii.Value(); ii.Next();
370 //          break;
371 //            case SMESHDS_AddPrism:
372 //          S << "AddVolume " << ii.Value(); ii.Next();
373 //          S << " " << ii.Value(); ii.Next();
374 //          S << " " << ii.Value(); ii.Next();
375 //          S << " " << ii.Value(); ii.Next();
376 //          S << " " << ii.Value(); ii.Next();
377 //          S << " " << ii.Value(); ii.Next();
378 //          S << " " << ii.Value(); ii.Next();
379 //          break;
380 //            case SMESHDS_AddHexahedron:
381 //          S << "AddVolume " << ii.Value(); ii.Next();
382 //          S << " " << ii.Value(); ii.Next();
383 //          S << " " << ii.Value(); ii.Next();
384 //          S << " " << ii.Value(); ii.Next();
385 //          S << " " << ii.Value(); ii.Next();
386 //          S << " " << ii.Value(); ii.Next();
387 //          S << " " << ii.Value(); ii.Next();
388 //          S << " " << ii.Value(); ii.Next();
389 //          S << " " << ii.Value(); ii.Next();
390 //          break;
391 //            case SMESHDS_RemoveNode:
392 //          S << "RemoveNode " << ii.Value(); ii.Next();
393 //          break;
394 //            case SMESHDS_RemoveElement:
395 //          S << "RemoveElement " << ii.Value(); ii.Next();
396 //          break;
397 //            default:
398 //          ASSERT(0);
399 //          break;
400 //            }
401 //          string ch = S.str();
402 //          SCRUTE(ch);
403 //          aLog[indexLog++] = CORBA::string_dup(ch.c_str());
404 //        }
405 //      its.Next();
406 //        }
407 //        if (clearAfterGet) _impl->ClearLog();
408 //      }
409 //    catch (SALOME_Exception& S_ex)
410 //      {
411 //        THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
412 //      }
413 //    return aLog._retn();
414 //  }
415
416 //=============================================================================
417 /*!
418  *  
419  */
420 //=============================================================================
421
422 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
423 {
424         MESSAGE("SMESH_Mesh_i::ClearLog");
425         // ****
426 }
427
428 //=============================================================================
429 /*!
430  *  
431  */
432 //=============================================================================
433
434 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
435 {
436         MESSAGE("SMESH_Mesh_i::GetId");
437         return _id;
438 }
439
440 //=============================================================================
441 /*!
442  *  
443  */
444 //=============================================================================
445
446 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
447 {
448         return _studyId;
449 }
450
451 //=============================================================================
452 /*!
453  *  
454  */
455 //=============================================================================
456
457 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
458 {
459         MESSAGE("SMESH_Mesh_i::SetImpl");
460         _impl = impl;
461 }
462
463 //=============================================================================
464 /*!
465  *  
466  */
467 //=============================================================================
468
469 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
470 {
471         MESSAGE("SMESH_Mesh_i::GetImpl()");
472         return *_impl;
473 }
474
475 //=============================================================================
476 /*!
477  *  
478  */
479 //=============================================================================
480
481 GEOM::GEOM_Gen_ptr SMESH_Mesh_i::GetGeomEngine()
482 {
483         MESSAGE("SMESH_Mesh_i::GetGeomEngine");
484         return GEOM::GEOM_Gen::_duplicate(_geom);
485 }
486
487 //=============================================================================
488 /*!
489  *  
490  */
491 //=============================================================================
492
493 void SMESH_Mesh_i::SetIor(SMESH::SMESH_Mesh_ptr myIor)
494 {
495         MESSAGE("SMESH_Mesh_i::SetIor");
496         _myIor = SMESH::SMESH_Mesh::_duplicate(myIor);
497         ASSERT(!CORBA::is_nil(_myIor));
498 }
499
500 //=============================================================================
501 /*!
502  *  
503  */
504 //=============================================================================
505
506 SMESH::SMESH_Mesh_ptr SMESH_Mesh_i::GetIor()
507 {
508         MESSAGE("SMESH_Mesh_i::GetIor");
509         ASSERT(!CORBA::is_nil(_myIor));
510         return SMESH::SMESH_Mesh::_duplicate(_myIor);
511 }
512
513 //=============================================================================
514 /*!
515  *  
516  */
517 //=============================================================================
518
519 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
520 {
521         SMESH_MeshEditor_i *aMeshEditor =
522                 new SMESH_MeshEditor_i(_impl->GetMeshDS());
523         SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
524         return aMesh._retn();
525 }
526
527 //=============================================================================
528 /*!
529  *  
530  */
531 //=============================================================================
532
533 void SMESH_Mesh_i::ExportMED(const char *file) throw(SALOME::SALOME_Exception)
534 {
535         _impl->ExportMED(file);
536 }
537 void SMESH_Mesh_i::ExportDAT(const char *file) throw(SALOME::SALOME_Exception)
538 {
539         _impl->ExportDAT(file);
540 }
541 void SMESH_Mesh_i::ExportUNV(const char *file) throw(SALOME::SALOME_Exception)
542 {
543         _impl->ExportUNV(file);
544 }
545
546 //=============================================================================
547 /*!
548  *  
549  */
550 //=============================================================================
551
552 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
553 {
554         SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
555         SALOME_MED::MESH_var aMesh = aMedMesh->_this();
556         return aMesh._retn();
557 }
558
559 //=============================================================================
560 /*!
561  *  
562  */
563 //=============================================================================
564 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
565 {
566         return _impl->NbNodes();
567 }
568
569 //=============================================================================
570 /*!
571  *  
572  */
573 //=============================================================================
574 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
575 {
576         return _impl->NbEdges();
577 }
578
579 //=============================================================================
580 /*!
581  *  
582  */
583 //=============================================================================
584 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
585 {
586         return _impl->NbFaces();
587 }
588
589 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
590 {
591         return _impl->NbTriangles();
592 }
593
594 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
595 {
596         return _impl->NbQuadrangles();
597 }
598
599 //=============================================================================
600 /*!
601  *  
602  */
603 //=============================================================================
604 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
605 {
606         return _impl->NbVolumes();
607 }
608
609 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
610 {
611         return _impl->NbTetras();
612 }
613
614 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
615 {
616         return _impl->NbHexas();
617 }
618
619 //=============================================================================
620 /*!
621  *  
622  */
623 //=============================================================================
624 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
625 {
626         return _impl->NbSubMesh();
627 }