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