Salome HOME
PAL8267: two new API methods added: ExtrusionSweepObject1(2)D()
[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 #include "SMESH_Mesh_i.hxx"
30 #include "SMESH_subMesh_i.hxx"
31 #include "SMESH_MEDMesh_i.hxx"
32 #include "SMESH_Group_i.hxx"
33 #include "SMESH_Filter_i.hxx"
34
35 #include "Utils_CorbaException.hxx"
36 #include "Utils_ExceptHandlers.hxx"
37 #include "utilities.h"
38
39 #include "SALOME_NamingService.hxx"
40 #include "Utils_SINGLETON.hxx"
41 #include "OpUtil.hxx"
42
43 #include "TCollection_AsciiString.hxx"
44 #include "SMESHDS_Command.hxx"
45 #include "SMESHDS_CommandType.hxx"
46 #include "SMESH_MeshEditor_i.hxx"
47 #include "SMESH_Gen_i.hxx"
48 #include "DriverMED_R_SMESHDS_Mesh.h"
49
50 #include <TColStd_MapOfInteger.hxx>
51 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
52 #include <TColStd_SequenceOfInteger.hxx>
53
54 #include <string>
55 #include <iostream>
56 #include <sstream>
57
58 #ifdef _DEBUG_
59 static int MYDEBUG = 0;
60 #else
61 static int MYDEBUG = 0;
62 #endif
63
64 using namespace std;
65
66 int SMESH_Mesh_i::myIdGenerator = 0;
67
68 //=============================================================================
69 /*!
70  *  Constructor
71  */
72 //=============================================================================
73
74 SMESH_Mesh_i::SMESH_Mesh_i( PortableServer::POA_ptr thePOA,
75                             SMESH_Gen_i*            gen_i,
76                             CORBA::Long studyId )
77 : SALOME::GenericObj_i( thePOA )
78 {
79   INFOS("SMESH_Mesh_i");
80   _gen_i = gen_i;
81   _id = myIdGenerator++;
82   _studyId = studyId;
83   thePOA->activate_object( this );
84 }
85
86 //=============================================================================
87 /*!
88  *  Destructor
89  */
90 //=============================================================================
91
92 SMESH_Mesh_i::~SMESH_Mesh_i()
93 {
94   INFOS("~SMESH_Mesh_i");
95   map<int, SMESH::SMESH_GroupBase_ptr>::iterator it;
96   for ( it = _mapGroups.begin(); it != _mapGroups.end(); it++ ) {
97     SMESH_GroupBase_i* aGroup = dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( it->second ).in() );
98     if ( aGroup ) {
99
100       // this method is colled from destructor of group (PAL6331)
101       //_impl->RemoveGroup( aGroup->GetLocalID() );
102       
103       aGroup->Destroy();
104     }
105   }
106   _mapGroups.clear();
107 }
108
109 //=============================================================================
110 /*!
111  *  SetShape
112  *
113  *  Associates <this> mesh with <theShape> and puts a reference  
114  *  to <theShape> into the current study; 
115  *  the previous shape is substituted by the new one.
116  */
117 //=============================================================================
118
119 void SMESH_Mesh_i::SetShape( GEOM::GEOM_Object_ptr theShapeObject )
120     throw (SALOME::SALOME_Exception)
121 {
122   Unexpect aCatch(SALOME_SalomeException);
123   try {
124     _impl->ShapeToMesh( _gen_i->GeomObjectToShape( theShapeObject ));
125   }
126   catch(SALOME_Exception & S_ex) {
127     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
128   }
129 }
130
131 //=======================================================================
132 //function : GetShapeToMesh
133 //purpose  : 
134 //=======================================================================
135
136 GEOM::GEOM_Object_ptr SMESH_Mesh_i::GetShapeToMesh()
137     throw (SALOME::SALOME_Exception)
138 {
139   Unexpect aCatch(SALOME_SalomeException);
140   GEOM::GEOM_Object_var aShapeObj;
141   try {
142     TopoDS_Shape S = _impl->GetMeshDS()->ShapeToMesh();
143     if ( !S.IsNull() )
144       aShapeObj = _gen_i->ShapeToGeomObject( S );
145   }
146   catch(SALOME_Exception & S_ex) {
147     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
148   }
149   return aShapeObj._retn();
150 }
151
152 //=============================================================================
153 /*!
154  *  
155  */
156 //=============================================================================
157
158 static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
159 {
160   SMESH::DriverMED_ReadStatus res;
161   switch (theStatus)
162   {
163   case DriverMED_R_SMESHDS_Mesh::DRS_OK:
164     res = SMESH::DRS_OK; break;
165   case DriverMED_R_SMESHDS_Mesh::DRS_EMPTY:
166     res = SMESH::DRS_EMPTY; break;
167   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_RENUMBER:
168     res = SMESH::DRS_WARN_RENUMBER; break;
169   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_SKIP_ELEM:
170     res = SMESH::DRS_WARN_SKIP_ELEM; break;
171   case DriverMED_R_SMESHDS_Mesh::DRS_FAIL:
172   default:
173     res = SMESH::DRS_FAIL; break;
174   }
175   return res;
176 }
177
178 //=============================================================================
179 /*!
180  *  ImportMEDFile
181  *
182  *  Imports mesh data from MED file
183  */
184 //=============================================================================
185
186 SMESH::DriverMED_ReadStatus
187 SMESH_Mesh_i::ImportMEDFile( const char* theFileName, const char* theMeshName )
188   throw ( SALOME::SALOME_Exception )
189 {
190   Unexpect aCatch(SALOME_SalomeException);
191   int status;
192   try {
193     status = importMEDFile( theFileName, theMeshName );
194   }
195   catch( SALOME_Exception& S_ex ) {
196     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
197   }  
198   catch ( ... ) {
199     THROW_SALOME_CORBA_EXCEPTION("ImportMEDFile(): unknown exception", SALOME::BAD_PARAM);
200   }
201
202   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
203   if ( !aStudy->_is_nil() ) {
204     // publishing of the groups in the study (sub-meshes are out of scope of MED import)
205     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
206     for (; it != _mapGroups.end(); it++ ) {
207       SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_duplicate( it->second );
208       _gen_i->PublishGroup( aStudy, _this(), aGroup,
209                            GEOM::GEOM_Object::_nil(), aGroup->GetName());
210     }
211   }
212   return ConvertDriverMEDReadStatus(status);
213 }
214
215 //=============================================================================
216 /*!
217  *  ImportUNVFile
218  *
219  *  Imports mesh data from MED file
220  */
221 //=============================================================================
222
223 int SMESH_Mesh_i::ImportUNVFile( const char* theFileName )
224   throw ( SALOME::SALOME_Exception )
225 {
226   // Read mesh with name = <theMeshName> into SMESH_Mesh
227   _impl->UNVToMesh( theFileName );
228
229   return 1;
230 }
231
232 //=============================================================================
233 /*!
234  *  ImportSTLFile
235  *
236  *  Imports mesh data from STL file
237  */
238 //=============================================================================
239 int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
240   throw ( SALOME::SALOME_Exception )
241 {
242   // Read mesh with name = <theMeshName> into SMESH_Mesh
243   _impl->STLToMesh( theFileName );
244
245   return 1;
246 }
247
248 //=============================================================================
249 /*!
250  *  importMEDFile
251  *
252  *  Imports mesh data from MED file
253  */
254 //=============================================================================
255
256 int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshName )
257 {
258   // Read mesh with name = <theMeshName> and all its groups into SMESH_Mesh
259   int status = _impl->MEDToMesh( theFileName, theMeshName );
260
261   // Create group servants, if any groups were imported
262   list<int> aGroupIds = _impl->GetGroupIds();
263   for ( list<int>::iterator it = aGroupIds.begin(); it != aGroupIds.end(); it++ ) {
264     SMESH_Group_i* aGroupImpl     = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, *it );
265
266     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
267     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
268     aGroupImpl->Register();
269     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
270
271     SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
272     _mapGroups[*it]               = SMESH::SMESH_Group::_duplicate( aGroup );
273
274     // register CORBA object for persistence
275     int nextId = _gen_i->RegisterObject( aGroup );
276     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
277   }
278
279   return status;
280 }
281
282 //=============================================================================
283 /*!
284  *  
285  */
286 //=============================================================================
287
288 static SMESH::Hypothesis_Status ConvertHypothesisStatus
289                          (SMESH_Hypothesis::Hypothesis_Status theStatus)
290 {
291   SMESH::Hypothesis_Status res;
292   switch (theStatus)
293   {
294   case SMESH_Hypothesis::HYP_OK:
295     res = SMESH::HYP_OK; break;
296   case SMESH_Hypothesis::HYP_MISSING:
297     res = SMESH::HYP_MISSING; break;
298   case SMESH_Hypothesis::HYP_CONCURENT:
299     res = SMESH::HYP_CONCURENT; break;
300   case SMESH_Hypothesis::HYP_BAD_PARAMETER:
301     res = SMESH::HYP_BAD_PARAMETER; break;
302   case SMESH_Hypothesis::HYP_INCOMPATIBLE:
303     res = SMESH::HYP_INCOMPATIBLE; break;
304   case SMESH_Hypothesis::HYP_NOTCONFORM:
305     res = SMESH::HYP_NOTCONFORM; break;
306   case SMESH_Hypothesis::HYP_ALREADY_EXIST:
307     res = SMESH::HYP_ALREADY_EXIST; break;
308   case SMESH_Hypothesis::HYP_BAD_DIM:
309     res = SMESH::HYP_BAD_DIM; break;
310   default:
311     res = SMESH::HYP_UNKNOWN_FATAL;
312   }
313   return res;
314 }
315
316 //=============================================================================
317 /*!
318  *  AddHypothesis
319  *
320  *  calls internal addHypothesis() and then adds a reference to <anHyp> under 
321  *  the SObject actually having a reference to <aSubShape>.
322  *  NB: For this method to work, it is necessary to add a reference to sub-shape first.
323  */
324 //=============================================================================
325
326 SMESH::Hypothesis_Status SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
327                                                      SMESH::SMESH_Hypothesis_ptr anHyp)
328   throw(SALOME::SALOME_Exception)
329 {
330   Unexpect aCatch(SALOME_SalomeException);
331   SMESH_Hypothesis::Hypothesis_Status status = addHypothesis( aSubShapeObject, anHyp );
332
333   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
334     _gen_i->AddHypothesisToShape(_gen_i->GetCurrentStudy(), _this(),
335                                  aSubShapeObject, anHyp );
336
337   if(MYDEBUG) MESSAGE( " AddHypothesis(): status = " << status );
338
339   return ConvertHypothesisStatus(status);
340 }
341
342 //=============================================================================
343 /*!
344  *  
345  */
346 //=============================================================================
347
348 SMESH_Hypothesis::Hypothesis_Status
349   SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr       aSubShapeObject,
350                               SMESH::SMESH_Hypothesis_ptr anHyp)
351 {
352   if(MYDEBUG) MESSAGE("addHypothesis");
353
354   if (CORBA::is_nil(aSubShapeObject))
355     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
356                                  SALOME::BAD_PARAM);
357
358   SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
359   if (CORBA::is_nil(myHyp))
360     THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
361                                  SALOME::BAD_PARAM);
362
363   SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
364   try
365   {
366     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
367     int hypId = myHyp->GetId();
368     status = _impl->AddHypothesis(myLocSubShape, hypId);
369     if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
370       _mapHypo[hypId] = myHyp;
371       // assure there is a corresponding submesh
372       if ( !_impl->IsMainShape( myLocSubShape )) {
373         int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
374         if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
375           createSubMesh( aSubShapeObject );
376       }
377     }
378   }
379   catch(SALOME_Exception & S_ex)
380   {
381     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
382   }
383   return status;
384 }
385
386 //=============================================================================
387 /*!
388  *  
389  */
390 //=============================================================================
391
392 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
393                                                         SMESH::SMESH_Hypothesis_ptr anHyp)
394      throw(SALOME::SALOME_Exception)
395 {
396   Unexpect aCatch(SALOME_SalomeException);
397   SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
398
399   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
400     _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
401                                       aSubShapeObject, anHyp );
402
403   return ConvertHypothesisStatus(status);
404 }
405
406 //=============================================================================
407 /*!
408  *  
409  */
410 //=============================================================================
411
412 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
413                                  SMESH::SMESH_Hypothesis_ptr anHyp)
414 {
415         if(MYDEBUG) MESSAGE("removeHypothesis()");
416         // **** proposer liste de subShape (selection multiple)
417
418         if (CORBA::is_nil(aSubShapeObject))
419                 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
420                         SALOME::BAD_PARAM);
421
422         SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
423         if (CORBA::is_nil(myHyp))
424           THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
425                         SALOME::BAD_PARAM);
426
427         SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
428         try
429         {
430                 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
431                 int hypId = myHyp->GetId();
432                 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
433                 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
434                   _mapHypo.erase( hypId );
435         }
436         catch(SALOME_Exception & S_ex)
437         {
438                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
439         }
440         return status;
441 }
442
443 //=============================================================================
444 /*!
445  *  
446  */
447 //=============================================================================
448
449 SMESH::ListOfHypothesis *
450         SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
451 throw(SALOME::SALOME_Exception)
452 {
453   Unexpect aCatch(SALOME_SalomeException);
454   if (MYDEBUG) MESSAGE("GetHypothesisList");
455   if (CORBA::is_nil(aSubShapeObject))
456     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
457                                  SALOME::BAD_PARAM);
458   
459   SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
460
461   try {
462     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
463     const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
464     int i = 0, n = aLocalList.size();
465     aList->length( n );
466
467     for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
468       SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
469       if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
470         aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
471     }
472
473     aList->length( i );
474   }
475   catch(SALOME_Exception & S_ex) {
476     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
477   }
478   
479   return aList._retn();
480 }
481
482 //=============================================================================
483 /*!
484  *  
485  */
486 //=============================================================================
487 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
488                                                   const char*           theName ) 
489      throw(SALOME::SALOME_Exception)
490 {
491   Unexpect aCatch(SALOME_SalomeException);
492   MESSAGE("SMESH_Mesh_i::GetSubMesh");
493   if (CORBA::is_nil(aSubShapeObject))
494     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
495                                  SALOME::BAD_PARAM);
496   
497   SMESH::SMESH_subMesh_var subMesh;
498   SMESH::SMESH_Mesh_var    aMesh = SMESH::SMESH_Mesh::_narrow(_this());
499   try {
500     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
501     
502     //Get or Create the SMESH_subMesh object implementation
503     
504     int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
505     subMesh = getSubMesh( subMeshId );
506
507     // create a new subMesh object servant if there is none for the shape
508     if ( subMesh->_is_nil() )
509       subMesh = createSubMesh( aSubShapeObject );
510
511     if ( _gen_i->CanPublishInStudy( subMesh ))
512       _gen_i->PublishSubMesh (_gen_i->GetCurrentStudy(), aMesh,
513                               subMesh, aSubShapeObject, theName );
514   }
515   catch(SALOME_Exception & S_ex) {
516     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
517   }
518   return subMesh._retn();
519 }
520
521 //=============================================================================
522 /*!
523  *  
524  */
525 //=============================================================================
526
527 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
528      throw (SALOME::SALOME_Exception)
529 {
530   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
531   if ( theSubMesh->_is_nil() )
532     return;
533
534   GEOM::GEOM_Object_var aSubShapeObject;
535   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
536   if ( !aStudy->_is_nil() )  {
537     // Remove submesh's SObject
538     SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
539     if ( !anSO->_is_nil() ) {
540       long aTag = SMESH_Gen_i::GetRefOnShapeTag(); 
541       SALOMEDS::SObject_var anObj, aRef;
542       if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
543         aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
544
545       aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
546     }
547   }
548
549   removeSubMesh( theSubMesh, aSubShapeObject.in() );
550 }
551
552
553 //=============================================================================
554 /*!
555  *  
556  */
557 //=============================================================================
558
559 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
560                                                  const char*         theName )
561      throw(SALOME::SALOME_Exception)
562 {
563   Unexpect aCatch(SALOME_SalomeException);
564   SMESH::SMESH_Group_var aNewGroup =
565     SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
566
567   _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(),
568                        aNewGroup, GEOM::GEOM_Object::_nil(), theName);
569
570   return aNewGroup._retn();
571 }
572
573
574 //=============================================================================
575 /*!
576  *  
577  */
578 //=============================================================================
579 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM( SMESH::ElementType    theElemType,
580                                                                const char*           theName,
581                                                                GEOM::GEOM_Object_ptr theGeomObj)
582      throw(SALOME::SALOME_Exception)
583 {
584   Unexpect aCatch(SALOME_SalomeException);
585   SMESH::SMESH_GroupOnGeom_var aNewGroup;
586
587   TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
588   if ( !aShape.IsNull() ) {
589     aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
590       ( createGroup( theElemType, theName, aShape ));
591     if ( _gen_i->CanPublishInStudy( aNewGroup ) )
592       _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(), 
593                            aNewGroup, theGeomObj, theName );
594   }
595
596   return aNewGroup._retn();
597 }
598 //=============================================================================
599 /*!
600  *  
601  */
602 //=============================================================================
603
604 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
605      throw (SALOME::SALOME_Exception)
606 {
607   if ( theGroup->_is_nil() )
608     return;
609
610   SMESH_GroupBase_i* aGroup =
611     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
612   if ( !aGroup )
613     return;
614
615   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
616   if ( !aStudy->_is_nil() )  {
617     // Remove group's SObject
618     SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
619     if ( !aGroupSO->_is_nil() )
620       aStudy->NewBuilder()->RemoveObject( aGroupSO );
621   }
622
623   // Remove the group from SMESH data structures
624   removeGroup( aGroup->GetLocalID() );
625 }
626
627 //=============================================================================
628 /*! RemoveGroupWithContents
629  *  Remove group with its contents
630  */ 
631 //=============================================================================
632 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
633   throw (SALOME::SALOME_Exception)
634 {
635   if ( theGroup->_is_nil() )
636     return;
637
638   SMESH_GroupBase_i* aGroup =
639     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
640   if ( !aGroup )
641     return;
642   
643   SMESH::long_array_var anIds = aGroup->GetListOfID();
644   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
645     
646   if ( aGroup->GetType() == SMESH::NODE )
647     aMeshEditor->RemoveNodes( anIds );
648   else
649     aMeshEditor->RemoveElements( anIds );
650   
651   RemoveGroup( theGroup );
652 }
653
654 //=============================================================================
655 /*! UnionGroups
656  *  New group is created. All mesh elements that are 
657  *  present in initial groups are added to the new one
658  */
659 //=============================================================================
660 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1, 
661                                                   SMESH::SMESH_GroupBase_ptr theGroup2, 
662                                                   const char* theName )
663   throw (SALOME::SALOME_Exception)
664 {
665   try
666   {
667     SMESH::SMESH_Group_var aResGrp;
668
669     if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
670          theGroup1->GetType() != theGroup2->GetType() )
671       return SMESH::SMESH_Group::_nil();
672
673     aResGrp = CreateGroup( theGroup1->GetType(), theName );
674     if ( aResGrp->_is_nil() )
675       return SMESH::SMESH_Group::_nil();
676
677     SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
678     SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
679
680     TColStd_MapOfInteger aResMap;
681
682     for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
683       aResMap.Add( anIds1[ i1 ] );
684
685     for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
686       aResMap.Add( anIds2[ i2 ] );
687
688     SMESH::long_array_var aResIds = new SMESH::long_array;
689     aResIds->length( aResMap.Extent() );
690
691     int resI = 0;
692     TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
693     for( ; anIter.More(); anIter.Next() )
694       aResIds[ resI++ ] = anIter.Key();
695
696     aResGrp->Add( aResIds );
697
698     return aResGrp._retn();
699   }
700   catch( ... )
701   {
702     return SMESH::SMESH_Group::_nil();
703   }
704 }
705   
706 //=============================================================================
707 /*! IntersectGroups
708  *  New group is created. All mesh elements that are 
709  *  present in both initial groups are added to the new one.
710  */
711 //=============================================================================
712 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1, 
713                                                       SMESH::SMESH_GroupBase_ptr theGroup2, 
714                                                       const char* theName )
715   throw (SALOME::SALOME_Exception)
716 {
717   SMESH::SMESH_Group_var aResGrp;
718   
719   if ( theGroup1->_is_nil() || theGroup2->_is_nil() || 
720        theGroup1->GetType() != theGroup2->GetType() )
721     return aResGrp;
722   
723   aResGrp = CreateGroup( theGroup1->GetType(), theName );
724   if ( aResGrp->_is_nil() )
725     return aResGrp;
726   
727   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
728   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
729   
730   TColStd_MapOfInteger aMap1;
731   
732   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
733     aMap1.Add( anIds1[ i1 ] );
734
735   TColStd_SequenceOfInteger aSeq;
736
737   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
738     if ( aMap1.Contains( anIds2[ i2 ] ) )
739       aSeq.Append( anIds2[ i2 ] );
740   
741   SMESH::long_array_var aResIds = new SMESH::long_array;
742   aResIds->length( aSeq.Length() );
743   
744   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
745     aResIds[ resI ] = aSeq( resI + 1 );
746   
747   aResGrp->Add( aResIds );
748   
749   return aResGrp._retn();
750 }
751
752 //=============================================================================
753 /*! CutGroups
754  *  New group is created. All mesh elements that are present in 
755  *  main group but do not present in tool group are added to the new one 
756  */
757 //=============================================================================
758 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1, 
759                                                 SMESH::SMESH_GroupBase_ptr theGroup2, 
760                                                 const char* theName )
761   throw (SALOME::SALOME_Exception)
762 {
763   SMESH::SMESH_Group_var aResGrp;
764   
765   if ( theGroup1->_is_nil() || theGroup2->_is_nil() || 
766        theGroup1->GetType() != theGroup2->GetType() )
767     return aResGrp;
768   
769   aResGrp = CreateGroup( theGroup1->GetType(), theName );
770   if ( aResGrp->_is_nil() )
771     return aResGrp;
772   
773   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
774   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
775   
776   TColStd_MapOfInteger aMap2;
777   
778   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
779     aMap2.Add( anIds2[ i2 ] );
780
781
782   TColStd_SequenceOfInteger aSeq;
783   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
784     if ( !aMap2.Contains( anIds1[ i1 ] ) )
785       aSeq.Append( anIds1[ i1 ] );
786
787   SMESH::long_array_var aResIds = new SMESH::long_array;
788   aResIds->length( aSeq.Length() );
789
790   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
791     aResIds[ resI ] = aSeq( resI + 1 );  
792   
793   aResGrp->Add( aResIds );
794   
795   return aResGrp._retn();
796 }
797
798 //=============================================================================
799 /*!
800  *  
801  */
802 //=============================================================================
803
804 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
805 {
806   if(MYDEBUG) MESSAGE( "createSubMesh" );
807   TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
808
809   ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
810   int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
811   SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
812   SMESH::SMESH_subMesh_var subMesh
813     = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
814
815   _mapSubMesh[subMeshId] = mySubMesh;
816   _mapSubMesh_i[subMeshId] = subMeshServant;
817   _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
818
819   // register CORBA object for persistence
820   int nextId = _gen_i->RegisterObject( subMesh );
821   if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
822
823   return subMesh._retn(); 
824 }
825
826 //=======================================================================
827 //function : getSubMesh
828 //purpose  : 
829 //=======================================================================
830
831 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
832 {
833   map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
834   if ( it == _mapSubMeshIor.end() )
835     return SMESH::SMESH_subMesh::_nil();
836
837   return SMESH::SMESH_subMesh::_duplicate( (*it).second );
838 }
839
840
841 //=============================================================================
842 /*!
843  *  
844  */
845 //=============================================================================
846
847 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
848                                   GEOM::GEOM_Object_ptr    theSubShapeObject )
849 {
850   MESSAGE("SMESH_Mesh_i::removeSubMesh()");
851   if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
852     return;
853
854   try {
855     SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
856     for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
857       removeHypothesis( theSubShapeObject, aHypList[i] );
858     }
859   }
860   catch( const SALOME::SALOME_Exception& ) {
861     INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
862   }
863
864   int subMeshId = theSubMesh->GetId();
865
866   _mapSubMesh.erase(subMeshId);
867   _mapSubMesh_i.erase(subMeshId);
868   _mapSubMeshIor.erase(subMeshId);
869   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
870 }
871
872 //=============================================================================
873 /*!
874  *  
875  */
876 //=============================================================================
877
878 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
879                                                       const char*         theName,
880                                                       const TopoDS_Shape& theShape )
881 {
882   int anId;
883   SMESH::SMESH_GroupBase_var aGroup;
884   if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
885     SMESH_GroupBase_i* aGroupImpl;
886     if ( !theShape.IsNull() )
887       aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
888     else
889       aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
890
891     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
892     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
893     aGroupImpl->Register();
894     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
895
896     aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
897     _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
898
899     // register CORBA object for persistence
900     int nextId = _gen_i->RegisterObject( aGroup );
901     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
902   }
903   return aGroup._retn();
904 }
905
906 //=============================================================================
907 /*!
908  * SMESH_Mesh_i::removeGroup
909  *
910  * Should be called by ~SMESH_Group_i() 
911  */
912 //=============================================================================
913
914 void SMESH_Mesh_i::removeGroup( const int theId )
915 {
916   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );  
917   if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
918     _mapGroups.erase( theId );
919     _impl->RemoveGroup( theId );
920   }
921 }
922
923
924 //=============================================================================
925 /*!
926  *  
927  */
928 //=============================================================================
929
930 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
931 throw(SALOME::SALOME_Exception)
932 {
933   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
934   
935   SMESH::log_array_var aLog;
936   try{
937     list < SMESHDS_Command * >logDS = _impl->GetLog();
938     aLog = new SMESH::log_array;
939     int indexLog = 0;
940     int lg = logDS.size();
941     SCRUTE(lg);
942     aLog->length(lg);
943     list < SMESHDS_Command * >::iterator its = logDS.begin();
944     while(its != logDS.end()){
945       SMESHDS_Command *com = *its;
946       int comType = com->GetType();
947       //SCRUTE(comType);
948       int lgcom = com->GetNumber();
949       //SCRUTE(lgcom);
950       const list < int >&intList = com->GetIndexes();
951       int inum = intList.size();
952       //SCRUTE(inum);
953       list < int >::const_iterator ii = intList.begin();
954       const list < double >&coordList = com->GetCoords();
955       int rnum = coordList.size();
956       //SCRUTE(rnum);
957       list < double >::const_iterator ir = coordList.begin();
958       aLog[indexLog].commandType = comType;
959       aLog[indexLog].number = lgcom;
960       aLog[indexLog].coords.length(rnum);
961       aLog[indexLog].indexes.length(inum);
962       for(int i = 0; i < rnum; i++){
963         aLog[indexLog].coords[i] = *ir;
964         //MESSAGE(" "<<i<<" "<<ir.Value());
965         ir++;
966       }
967       for(int i = 0; i < inum; i++){
968         aLog[indexLog].indexes[i] = *ii;
969         //MESSAGE(" "<<i<<" "<<ii.Value());
970         ii++;
971       }
972       indexLog++;
973       its++;
974     }
975     if(clearAfterGet)
976       _impl->ClearLog();
977   }
978   catch(SALOME_Exception & S_ex){
979     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
980   }
981   return aLog._retn();
982 }
983
984
985 //=============================================================================
986 /*!
987  *  
988  */
989 //=============================================================================
990
991 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
992 {
993   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
994   // ****
995 }
996
997 //=============================================================================
998 /*!
999  *  
1000  */
1001 //=============================================================================
1002
1003 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1004 {
1005   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1006   return _id;
1007 }
1008
1009 //=============================================================================
1010 /*!
1011  *  
1012  */
1013 //=============================================================================
1014
1015 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1016 {
1017   return _studyId;
1018 }
1019
1020 //=============================================================================
1021 /*!
1022  *  
1023  */
1024 //=============================================================================
1025
1026 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1027 {
1028   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1029   _impl = impl;
1030 }
1031
1032 //=============================================================================
1033 /*!
1034  *  
1035  */
1036 //=============================================================================
1037
1038 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1039 {
1040   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1041   return *_impl;
1042 }
1043
1044
1045 //=============================================================================
1046 /*!
1047  *  
1048  */
1049 //=============================================================================
1050
1051 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1052 {
1053         SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl );
1054         SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1055         return aMesh._retn();
1056 }
1057
1058 //=============================================================================
1059 /*!
1060  *  
1061  */
1062 //=============================================================================
1063
1064 void SMESH_Mesh_i::ExportToMED( const char* file, 
1065                                 CORBA::Boolean auto_groups, 
1066                                 SMESH::MED_VERSION theVersion )
1067   throw(SALOME::SALOME_Exception)
1068 {
1069   Unexpect aCatch(SALOME_SalomeException);
1070   
1071   char* aMeshName = "Mesh";
1072   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1073   if ( !aStudy->_is_nil() ) {
1074     SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
1075     if ( !aMeshSO->_is_nil() ) {
1076       aMeshName = aMeshSO->GetName();
1077       //SCRUTE(file);
1078       //SCRUTE(aMeshName);
1079       //SCRUTE(aMeshSO->GetID());
1080       
1081       // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes 
1082       if ( !aStudy->GetProperties()->IsLocked() ) 
1083         {
1084         SALOMEDS::GenericAttribute_var anAttr;
1085         SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1086         SALOMEDS::AttributeExternalFileDef_var aFileName;
1087         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1088         aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1089         ASSERT(!aFileName->_is_nil());
1090         aFileName->SetValue(file);
1091         SALOMEDS::AttributeFileType_var aFileType;
1092         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1093         aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1094         ASSERT(!aFileType->_is_nil());
1095         aFileType->SetValue("FICHIERMED");
1096         }
1097     }
1098   }
1099   _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
1100 }
1101
1102 void SMESH_Mesh_i::ExportMED( const char* file, 
1103                               CORBA::Boolean auto_groups)
1104   throw(SALOME::SALOME_Exception)
1105 {
1106   ExportToMED(file,auto_groups,SMESH::MED_V2_1);
1107 }
1108
1109 void SMESH_Mesh_i::ExportDAT(const char *file) throw(SALOME::SALOME_Exception)
1110 {
1111   Unexpect aCatch(SALOME_SalomeException);
1112   _impl->ExportDAT(file);
1113 }
1114 void SMESH_Mesh_i::ExportUNV(const char *file) throw(SALOME::SALOME_Exception)
1115 {
1116   Unexpect aCatch(SALOME_SalomeException);
1117   _impl->ExportUNV(file);
1118 }
1119
1120 void SMESH_Mesh_i::ExportSTL(const char *file, const bool isascii) throw(SALOME::SALOME_Exception)
1121 {
1122   Unexpect aCatch(SALOME_SalomeException);
1123   _impl->ExportSTL(file, isascii);
1124 }
1125
1126 //=============================================================================
1127 /*!
1128  *  
1129  */
1130 //=============================================================================
1131
1132 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1133 {
1134   Unexpect aCatch(SALOME_SalomeException);
1135   SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1136   SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1137   return aMesh._retn();
1138 }
1139
1140 //=============================================================================
1141 /*!
1142  *  
1143  */
1144 //=============================================================================
1145 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1146 {
1147   Unexpect aCatch(SALOME_SalomeException);
1148   return _impl->NbNodes();
1149 }
1150
1151 //=============================================================================
1152 /*!
1153  *  
1154  */
1155 //=============================================================================
1156 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
1157 {
1158   Unexpect aCatch(SALOME_SalomeException);
1159   return NbEdges() + NbFaces() + NbVolumes();
1160 }
1161   
1162 //=============================================================================
1163 /*!
1164  *  
1165  */
1166 //=============================================================================
1167 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1168 {
1169   Unexpect aCatch(SALOME_SalomeException);
1170   return _impl->NbEdges();
1171 }
1172
1173 //=============================================================================
1174 /*!
1175  *  
1176  */
1177 //=============================================================================
1178 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1179 {
1180   Unexpect aCatch(SALOME_SalomeException);
1181   return _impl->NbFaces();
1182 }
1183
1184 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1185 {
1186   Unexpect aCatch(SALOME_SalomeException);
1187   return _impl->NbTriangles();
1188 }
1189
1190 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1191 {
1192   Unexpect aCatch(SALOME_SalomeException);
1193   return _impl->NbQuadrangles();
1194 }
1195
1196 //=============================================================================
1197 /*!
1198  *  
1199  */
1200 //=============================================================================
1201 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1202 {
1203   Unexpect aCatch(SALOME_SalomeException);
1204   return _impl->NbVolumes();
1205 }
1206
1207 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1208 {
1209   Unexpect aCatch(SALOME_SalomeException);
1210   return _impl->NbTetras();
1211 }
1212
1213 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1214 {
1215   Unexpect aCatch(SALOME_SalomeException);
1216   return _impl->NbHexas();
1217 }
1218
1219 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1220 {
1221   Unexpect aCatch(SALOME_SalomeException);
1222   return _impl->NbPyramids();
1223 }
1224
1225 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1226 {
1227   Unexpect aCatch(SALOME_SalomeException);
1228   return _impl->NbPrisms();
1229 }
1230
1231 //=============================================================================
1232 /*!
1233  *  
1234  */
1235 //=============================================================================
1236 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1237 {
1238   Unexpect aCatch(SALOME_SalomeException);
1239   return _impl->NbSubMesh();
1240 }
1241
1242 //=============================================================================
1243 /*!
1244  *  
1245  */
1246 //=============================================================================
1247 char* SMESH_Mesh_i::Dump()
1248 {
1249   std::ostringstream os;
1250   _impl->Dump( os );
1251   return CORBA::string_dup( os.str().c_str() );
1252 }
1253
1254 //=============================================================================
1255 /*!
1256  *  
1257  */
1258 //=============================================================================
1259 SMESH::long_array* SMESH_Mesh_i::GetIDs()
1260 {
1261   SMESH::long_array_var aResult = new SMESH::long_array();
1262   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1263   int aMinId = aSMESHDS_Mesh->MinElementID();
1264   int aMaxId =  aSMESHDS_Mesh->MaxElementID();
1265
1266   aResult->length(aMaxId - aMinId + 1);
1267   
1268   for (int i = 0, id = aMinId; id <= aMaxId; id++  )
1269     aResult[i++] = id;
1270   
1271   return aResult._retn();
1272 }
1273
1274 //=============================================================================
1275 /*!
1276  *  
1277  */
1278 //=============================================================================
1279   
1280 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
1281      throw (SALOME::SALOME_Exception)
1282 {
1283   Unexpect aCatch(SALOME_SalomeException);
1284   MESSAGE("SMESH_Mesh_i::GetElementsId");
1285   SMESH::long_array_var aResult = new SMESH::long_array();
1286   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1287
1288   if ( aSMESHDS_Mesh == NULL )
1289     return aResult._retn();
1290
1291   long nbElements = NbElements();
1292   aResult->length( nbElements );
1293   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1294   for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
1295     aResult[i] = anIt->next()->GetID();
1296
1297   return aResult._retn();
1298 }
1299
1300
1301 //=============================================================================
1302 /*!
1303  *  
1304  */
1305 //=============================================================================
1306
1307 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
1308     throw (SALOME::SALOME_Exception)
1309 {
1310   Unexpect aCatch(SALOME_SalomeException);
1311   MESSAGE("SMESH_subMesh_i::GetElementsByType");
1312   SMESH::long_array_var aResult = new SMESH::long_array();
1313   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1314
1315   if ( aSMESHDS_Mesh == NULL )
1316     return aResult._retn();
1317
1318   long nbElements = NbElements();
1319
1320   // No sense in returning ids of elements along with ids of nodes:
1321   // when theElemType == SMESH::ALL, return node ids only if
1322   // there are no elements
1323   if ( theElemType == SMESH::NODE || theElemType == SMESH::ALL && nbElements == 0 )
1324     return GetNodesId();
1325
1326   aResult->length( nbElements );
1327
1328   int i = 0;
1329
1330   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1331   while ( i < nbElements && anIt->more() ) {
1332     const SMDS_MeshElement* anElem = anIt->next();
1333     if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
1334       aResult[i++] = anElem->GetID();
1335   }
1336
1337   aResult->length( i );
1338
1339   return aResult._retn();
1340 }
1341
1342 //=============================================================================
1343 /*!
1344  *  
1345  */
1346 //=============================================================================
1347   
1348 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
1349   throw (SALOME::SALOME_Exception)
1350 {
1351   Unexpect aCatch(SALOME_SalomeException);
1352   MESSAGE("SMESH_subMesh_i::GetNodesId");
1353   SMESH::long_array_var aResult = new SMESH::long_array();
1354   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1355
1356   if ( aSMESHDS_Mesh == NULL )
1357     return aResult._retn();
1358
1359   long nbNodes = NbNodes();
1360   aResult->length( nbNodes );
1361   SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator();
1362   for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
1363     aResult[i] = anIt->next()->GetID();
1364
1365   return aResult._retn();
1366 }
1367