Salome HOME
88773c1afa2bf223ada83def174d3383052b6af9
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 #include "SMESH_PythonDump.hxx"
35
36 #include "Utils_CorbaException.hxx"
37 #include "Utils_ExceptHandlers.hxx"
38 #include "utilities.h"
39
40 #include "SALOME_NamingService.hxx"
41 #include "Utils_SINGLETON.hxx"
42 #include "OpUtil.hxx"
43
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 //#include "SMDS_ElemIterator.hxx"
50 #include "SMDS_VolumeTool.hxx"
51 #include "SMESH_MesherHelper.hxx"
52 #include "SMESH_MeshEditor.hxx"
53
54 // OCCT Includes
55 #include <OSD_Path.hxx>
56 #include <OSD_File.hxx>
57 #include <OSD_Directory.hxx>
58 #include <OSD_Protection.hxx>
59 #include <TColStd_MapOfInteger.hxx>
60 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
61 #include <TColStd_SequenceOfInteger.hxx>
62 #include "TCollection_AsciiString.hxx"
63
64 // STL Includes
65 #include <string>
66 #include <iostream>
67 #include <sstream>
68
69 #ifdef _DEBUG_
70 static int MYDEBUG = 0;
71 #else
72 static int MYDEBUG = 0;
73 #endif
74
75 using namespace std;
76 using SMESH::TPythonDump;
77
78 int SMESH_Mesh_i::myIdGenerator = 0;
79
80
81
82 //=============================================================================
83 /*!
84  *  Constructor
85  */
86 //=============================================================================
87
88 SMESH_Mesh_i::SMESH_Mesh_i( PortableServer::POA_ptr thePOA,
89                             SMESH_Gen_i*            gen_i,
90                             CORBA::Long studyId )
91 : SALOME::GenericObj_i( thePOA )
92 {
93   MESSAGE("SMESH_Mesh_i");
94   _impl = NULL;
95   _gen_i = gen_i;
96   _id = myIdGenerator++;
97   _studyId = studyId;
98   thePOA->activate_object( this );
99 }
100
101 //=============================================================================
102 /*!
103  *  Destructor
104  */
105 //=============================================================================
106
107 SMESH_Mesh_i::~SMESH_Mesh_i()
108 {
109   INFOS("~SMESH_Mesh_i");
110   map<int, SMESH::SMESH_GroupBase_ptr>::iterator it;
111   for ( it = _mapGroups.begin(); it != _mapGroups.end(); it++ ) {
112     SMESH_GroupBase_i* aGroup = dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( it->second ).in() );
113     if ( aGroup ) {
114
115       // this method is colled from destructor of group (PAL6331)
116       //_impl->RemoveGroup( aGroup->GetLocalID() );
117
118       aGroup->Destroy();
119     }
120   }
121   _mapGroups.clear();
122 }
123
124 //=============================================================================
125 /*!
126  *  SetShape
127  *
128  *  Associates <this> mesh with <theShape> and puts a reference
129  *  to <theShape> into the current study;
130  *  the previous shape is substituted by the new one.
131  */
132 //=============================================================================
133
134 void SMESH_Mesh_i::SetShape( GEOM::GEOM_Object_ptr theShapeObject )
135     throw (SALOME::SALOME_Exception)
136 {
137   Unexpect aCatch(SALOME_SalomeException);
138   try {
139     _impl->ShapeToMesh( _gen_i->GeomObjectToShape( theShapeObject ));
140   }
141   catch(SALOME_Exception & S_ex) {
142     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
143   }
144 }
145
146 //================================================================================
147 /*!
148  * \brief return true if mesh has a shape to build a shape on
149  */
150 //================================================================================
151
152 CORBA::Boolean SMESH_Mesh_i::HasShapeToMesh()
153   throw (SALOME::SALOME_Exception)
154 {
155   Unexpect aCatch(SALOME_SalomeException);
156   bool res = false;
157   try {
158     res = _impl->HasShapeToMesh();
159   }
160   catch(SALOME_Exception & S_ex) {
161     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
162   }
163   return res;
164 }
165
166 //=======================================================================
167 //function : GetShapeToMesh
168 //purpose  :
169 //=======================================================================
170
171 GEOM::GEOM_Object_ptr SMESH_Mesh_i::GetShapeToMesh()
172     throw (SALOME::SALOME_Exception)
173 {
174   Unexpect aCatch(SALOME_SalomeException);
175   GEOM::GEOM_Object_var aShapeObj;
176   try {
177     TopoDS_Shape S = _impl->GetMeshDS()->ShapeToMesh();
178     if ( !S.IsNull() )
179       aShapeObj = _gen_i->ShapeToGeomObject( S );
180   }
181   catch(SALOME_Exception & S_ex) {
182     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
183   }
184   return aShapeObj._retn();
185 }
186
187 //=============================================================================
188 /*!
189  *
190  */
191 //=============================================================================
192
193 static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
194 {
195   SMESH::DriverMED_ReadStatus res;
196   switch (theStatus)
197   {
198   case DriverMED_R_SMESHDS_Mesh::DRS_OK:
199     res = SMESH::DRS_OK; break;
200   case DriverMED_R_SMESHDS_Mesh::DRS_EMPTY:
201     res = SMESH::DRS_EMPTY; break;
202   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_RENUMBER:
203     res = SMESH::DRS_WARN_RENUMBER; break;
204   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_SKIP_ELEM:
205     res = SMESH::DRS_WARN_SKIP_ELEM; break;
206   case DriverMED_R_SMESHDS_Mesh::DRS_FAIL:
207   default:
208     res = SMESH::DRS_FAIL; break;
209   }
210   return res;
211 }
212
213 //=============================================================================
214 /*!
215  *  ImportMEDFile
216  *
217  *  Imports mesh data from MED file
218  */
219 //=============================================================================
220
221 SMESH::DriverMED_ReadStatus
222 SMESH_Mesh_i::ImportMEDFile( const char* theFileName, const char* theMeshName )
223   throw ( SALOME::SALOME_Exception )
224 {
225   Unexpect aCatch(SALOME_SalomeException);
226   int status;
227   try {
228     status = importMEDFile( theFileName, theMeshName );
229   }
230   catch( SALOME_Exception& S_ex ) {
231     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
232   }
233   catch ( ... ) {
234     THROW_SALOME_CORBA_EXCEPTION("ImportMEDFile(): unknown exception", SALOME::BAD_PARAM);
235   }
236
237   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
238   if ( !aStudy->_is_nil() ) {
239     // publishing of the groups in the study (sub-meshes are out of scope of MED import)
240     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
241     for (; it != _mapGroups.end(); it++ ) {
242       SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_duplicate( it->second );
243       _gen_i->PublishGroup( aStudy, _this(), aGroup,
244                            GEOM::GEOM_Object::_nil(), aGroup->GetName());
245     }
246   }
247   return ConvertDriverMEDReadStatus(status);
248 }
249
250 //=============================================================================
251 /*!
252  *  ImportUNVFile
253  *
254  *  Imports mesh data from MED file
255  */
256 //=============================================================================
257
258 int SMESH_Mesh_i::ImportUNVFile( const char* theFileName )
259   throw ( SALOME::SALOME_Exception )
260 {
261   // Read mesh with name = <theMeshName> into SMESH_Mesh
262   _impl->UNVToMesh( theFileName );
263
264   CreateGroupServants();
265
266   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
267   if ( !aStudy->_is_nil() ) {
268     // publishing of the groups in the study (sub-meshes are out of scope of UNV import)
269     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
270     for (; it != _mapGroups.end(); it++ ) {
271       SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_duplicate( it->second );
272       _gen_i->PublishGroup( aStudy, _this(), aGroup,
273                            GEOM::GEOM_Object::_nil(), aGroup->GetName());
274     }
275   }
276   return 1;
277 }
278
279 //=============================================================================
280 /*!
281  *  ImportSTLFile
282  *
283  *  Imports mesh data from STL file
284  */
285 //=============================================================================
286 int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
287   throw ( SALOME::SALOME_Exception )
288 {
289   // Read mesh with name = <theMeshName> into SMESH_Mesh
290   _impl->STLToMesh( theFileName );
291
292   return 1;
293 }
294
295 //=============================================================================
296 /*!
297  *  importMEDFile
298  *
299  *  Imports mesh data from MED file
300  */
301 //=============================================================================
302
303 int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshName )
304 {
305   // Read mesh with name = <theMeshName> and all its groups into SMESH_Mesh
306   int status = _impl->MEDToMesh( theFileName, theMeshName );
307   CreateGroupServants();
308
309   return status;
310 }
311
312 //=============================================================================
313 /*!
314  *
315  */
316 //=============================================================================
317
318 #define RETURNCASE(hyp_stat) case SMESH_Hypothesis::hyp_stat: return SMESH::hyp_stat;
319
320 static SMESH::Hypothesis_Status ConvertHypothesisStatus
321                          (SMESH_Hypothesis::Hypothesis_Status theStatus)
322 {
323   switch (theStatus) {
324   RETURNCASE( HYP_OK            );
325   RETURNCASE( HYP_MISSING       );
326   RETURNCASE( HYP_CONCURENT     );
327   RETURNCASE( HYP_BAD_PARAMETER );
328   RETURNCASE( HYP_HIDDEN_ALGO   );
329   RETURNCASE( HYP_HIDING_ALGO   );
330   RETURNCASE( HYP_UNKNOWN_FATAL );
331   RETURNCASE( HYP_INCOMPATIBLE  );
332   RETURNCASE( HYP_NOTCONFORM    );
333   RETURNCASE( HYP_ALREADY_EXIST );
334   RETURNCASE( HYP_BAD_DIM       );
335   RETURNCASE( HYP_BAD_SUBSHAPE  );
336   RETURNCASE( HYP_BAD_GEOMETRY  );
337   default:;
338   }
339   return SMESH::HYP_UNKNOWN_FATAL;
340 }
341
342 //=============================================================================
343 /*!
344  *  AddHypothesis
345  *
346  *  calls internal addHypothesis() and then adds a reference to <anHyp> under
347  *  the SObject actually having a reference to <aSubShape>.
348  *  NB: For this method to work, it is necessary to add a reference to sub-shape first.
349  */
350 //=============================================================================
351
352 SMESH::Hypothesis_Status SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
353                                                      SMESH::SMESH_Hypothesis_ptr anHyp)
354   throw(SALOME::SALOME_Exception)
355 {
356   Unexpect aCatch(SALOME_SalomeException);
357   SMESH_Hypothesis::Hypothesis_Status status = addHypothesis( aSubShapeObject, anHyp );
358
359   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
360     _gen_i->AddHypothesisToShape(_gen_i->GetCurrentStudy(), _this(),
361                                  aSubShapeObject, anHyp );
362
363   if(MYDEBUG) MESSAGE( " AddHypothesis(): status = " << status );
364
365   // Update Python script
366   TPythonDump() << "status = " << _this() << ".AddHypothesis( "
367                 << aSubShapeObject << ", " << anHyp << " )";
368
369   return ConvertHypothesisStatus(status);
370 }
371
372 //=============================================================================
373 /*!
374  *
375  */
376 //=============================================================================
377
378 SMESH_Hypothesis::Hypothesis_Status
379   SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr       aSubShapeObject,
380                               SMESH::SMESH_Hypothesis_ptr anHyp)
381 {
382   if(MYDEBUG) MESSAGE("addHypothesis");
383
384   if (CORBA::is_nil(aSubShapeObject))
385     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
386                                  SALOME::BAD_PARAM);
387
388   SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
389   if (CORBA::is_nil(myHyp))
390     THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
391                                  SALOME::BAD_PARAM);
392
393   SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
394   try
395   {
396     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
397     int hypId = myHyp->GetId();
398     status = _impl->AddHypothesis(myLocSubShape, hypId);
399     if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
400       _mapHypo[hypId] = SMESH::SMESH_Hypothesis::_duplicate( myHyp );
401       // assure there is a corresponding submesh
402       if ( !_impl->IsMainShape( myLocSubShape )) {
403         int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
404         if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
405           createSubMesh( aSubShapeObject );
406       }
407     }
408   }
409   catch(SALOME_Exception & S_ex)
410   {
411     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
412   }
413   return status;
414 }
415
416 //=============================================================================
417 /*!
418  *
419  */
420 //=============================================================================
421
422 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
423                                                         SMESH::SMESH_Hypothesis_ptr anHyp)
424      throw(SALOME::SALOME_Exception)
425 {
426   Unexpect aCatch(SALOME_SalomeException);
427   SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
428
429   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
430     _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
431                                       aSubShapeObject, anHyp );
432
433   // Update Python script
434   TPythonDump() << "status = " << _this() << ".RemoveHypothesis( "
435                 << aSubShapeObject << ", " << anHyp << " )";
436
437   return ConvertHypothesisStatus(status);
438 }
439
440 //=============================================================================
441 /*!
442  *
443  */
444 //=============================================================================
445
446 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
447                                  SMESH::SMESH_Hypothesis_ptr anHyp)
448 {
449         if(MYDEBUG) MESSAGE("removeHypothesis()");
450         // **** proposer liste de subShape (selection multiple)
451
452         if (CORBA::is_nil(aSubShapeObject))
453                 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
454                         SALOME::BAD_PARAM);
455
456         SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
457         if (CORBA::is_nil(myHyp))
458           THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
459                         SALOME::BAD_PARAM);
460
461         SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
462         try
463         {
464                 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
465                 int hypId = myHyp->GetId();
466                 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
467                 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
468                   _mapHypo.erase( hypId );
469         }
470         catch(SALOME_Exception & S_ex)
471         {
472                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
473         }
474         return status;
475 }
476
477 //=============================================================================
478 /*!
479  *
480  */
481 //=============================================================================
482
483 SMESH::ListOfHypothesis *
484         SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
485 throw(SALOME::SALOME_Exception)
486 {
487   Unexpect aCatch(SALOME_SalomeException);
488   if (MYDEBUG) MESSAGE("GetHypothesisList");
489   if (CORBA::is_nil(aSubShapeObject))
490     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
491                                  SALOME::BAD_PARAM);
492
493   SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
494
495   try {
496     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
497     const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
498     int i = 0, n = aLocalList.size();
499     aList->length( n );
500
501     for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
502       SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
503       if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
504         aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
505     }
506
507     aList->length( i );
508   }
509   catch(SALOME_Exception & S_ex) {
510     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
511   }
512
513   return aList._retn();
514 }
515
516 //=============================================================================
517 /*!
518  *
519  */
520 //=============================================================================
521 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
522                                                   const char*           theName )
523      throw(SALOME::SALOME_Exception)
524 {
525   Unexpect aCatch(SALOME_SalomeException);
526   MESSAGE("SMESH_Mesh_i::GetSubMesh");
527   if (CORBA::is_nil(aSubShapeObject))
528     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
529                                  SALOME::BAD_PARAM);
530
531   SMESH::SMESH_subMesh_var subMesh;
532   SMESH::SMESH_Mesh_var    aMesh = SMESH::SMESH_Mesh::_narrow(_this());
533   try {
534     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
535
536     //Get or Create the SMESH_subMesh object implementation
537
538     int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
539     subMesh = getSubMesh( subMeshId );
540
541     // create a new subMesh object servant if there is none for the shape
542     if ( subMesh->_is_nil() )
543       subMesh = createSubMesh( aSubShapeObject );
544
545     if ( _gen_i->CanPublishInStudy( subMesh )) {
546       SALOMEDS::SObject_var aSO =
547         _gen_i->PublishSubMesh(_gen_i->GetCurrentStudy(), aMesh,
548                                subMesh, aSubShapeObject, theName );
549       if ( !aSO->_is_nil()) {
550         // Update Python script
551         TPythonDump() << aSO << " = " << _this() << ".GetSubMesh( "
552                       << aSubShapeObject << ", '" << theName << "' )";
553       }
554     }
555   }
556   catch(SALOME_Exception & S_ex) {
557     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
558   }
559   return subMesh._retn();
560 }
561
562 //=============================================================================
563 /*!
564  *
565  */
566 //=============================================================================
567
568 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
569      throw (SALOME::SALOME_Exception)
570 {
571   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
572   if ( theSubMesh->_is_nil() )
573     return;
574
575   GEOM::GEOM_Object_var aSubShapeObject;
576   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
577   if ( !aStudy->_is_nil() )  {
578     // Remove submesh's SObject
579     SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
580     if ( !anSO->_is_nil() ) {
581       long aTag = SMESH_Gen_i::GetRefOnShapeTag();
582       SALOMEDS::SObject_var anObj, aRef;
583       if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
584         aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
585
586       aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
587
588       // Update Python script
589       TPythonDump() << _this() << ".RemoveSubMesh( " << anSO << " )";
590     }
591   }
592
593   removeSubMesh( theSubMesh, aSubShapeObject.in() );
594 }
595
596 //=============================================================================
597 /*!
598  *  ElementTypeString
599  */
600 //=============================================================================
601 #define CASE2STRING(enum) case SMESH::enum: return "SMESH."#enum;
602 inline TCollection_AsciiString ElementTypeString (SMESH::ElementType theElemType)
603 {
604   switch (theElemType) {
605     CASE2STRING( ALL );
606     CASE2STRING( NODE );
607     CASE2STRING( EDGE );
608     CASE2STRING( FACE );
609     CASE2STRING( VOLUME );
610   default:;
611   }
612   return "";
613 }
614
615 //=============================================================================
616 /*!
617  *
618  */
619 //=============================================================================
620
621 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
622                                                  const char*         theName )
623      throw(SALOME::SALOME_Exception)
624 {
625   Unexpect aCatch(SALOME_SalomeException);
626   SMESH::SMESH_Group_var aNewGroup =
627     SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
628
629   if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
630     SALOMEDS::SObject_var aSO =
631       _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(),
632                            aNewGroup, GEOM::GEOM_Object::_nil(), theName);
633     if ( !aSO->_is_nil()) {
634       // Update Python script
635       TPythonDump() << aSO << " = " << _this() << ".CreateGroup( "
636                     << ElementTypeString(theElemType) << ", '" << theName << "' )";
637     }
638   }
639   return aNewGroup._retn();
640 }
641
642
643 //=============================================================================
644 /*!
645  *
646  */
647 //=============================================================================
648 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM (SMESH::ElementType    theElemType,
649                                                                 const char*           theName,
650                                                                 GEOM::GEOM_Object_ptr theGeomObj)
651      throw(SALOME::SALOME_Exception)
652 {
653   Unexpect aCatch(SALOME_SalomeException);
654   SMESH::SMESH_GroupOnGeom_var aNewGroup;
655
656   TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
657   if ( !aShape.IsNull() ) {
658     aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
659       ( createGroup( theElemType, theName, aShape ));
660     if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
661       SALOMEDS::SObject_var aSO =
662         _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(),
663                              aNewGroup, theGeomObj, theName);
664       if ( !aSO->_is_nil()) {
665         // Update Python script
666         TPythonDump() << aSO << " = " << _this() << ".CreateGroupFromGEOM("
667                       << ElementTypeString(theElemType) << ", '" << theName << "', "
668                       << theGeomObj << " )";
669       }
670     }
671   }
672
673   return aNewGroup._retn();
674 }
675
676 //=============================================================================
677 /*!
678  *
679  */
680 //=============================================================================
681
682 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
683      throw (SALOME::SALOME_Exception)
684 {
685   if ( theGroup->_is_nil() )
686     return;
687
688   SMESH_GroupBase_i* aGroup =
689     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
690   if ( !aGroup )
691     return;
692
693   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
694   if ( !aStudy->_is_nil() )  {
695     SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
696
697     if ( !aGroupSO->_is_nil() ) {
698       // Update Python script
699       TPythonDump() << _this() << ".RemoveGroup( " << aGroupSO << " )";
700
701       // Remove group's SObject
702       aStudy->NewBuilder()->RemoveObject( aGroupSO );
703     }
704   }
705
706   // Remove the group from SMESH data structures
707   removeGroup( aGroup->GetLocalID() );
708 }
709
710 //=============================================================================
711 /*! RemoveGroupWithContents
712  *  Remove group with its contents
713  */
714 //=============================================================================
715 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
716   throw (SALOME::SALOME_Exception)
717 {
718   if ( theGroup->_is_nil() )
719     return;
720
721   SMESH_GroupBase_i* aGroup =
722     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
723   if ( !aGroup )
724     return;
725
726   SMESH::long_array_var anIds = aGroup->GetListOfID();
727   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
728
729   // Update Python script
730   TPythonDump() << _this() << ".RemoveGroupWithContents( " << theGroup << " )";
731
732   // Remove contents
733   if ( aGroup->GetType() == SMESH::NODE )
734     aMeshEditor->RemoveNodes( anIds );
735   else
736     aMeshEditor->RemoveElements( anIds );
737
738   // Remove group
739   RemoveGroup( theGroup );
740
741   // Clear python lines, created by RemoveNodes/Elements() and RemoveGroup()
742   _gen_i->RemoveLastFromPythonScript(_gen_i->GetCurrentStudy()->StudyId());
743   _gen_i->RemoveLastFromPythonScript(_gen_i->GetCurrentStudy()->StudyId());
744 }
745
746
747 //================================================================================
748 /*!
749  * \brief Get the list of groups existing in the mesh
750   * \retval SMESH::ListOfGroups * - list of groups
751  */
752 //================================================================================
753
754 SMESH::ListOfGroups * SMESH_Mesh_i::GetGroups() throw(SALOME::SALOME_Exception)
755 {
756   Unexpect aCatch(SALOME_SalomeException);
757   if (MYDEBUG) MESSAGE("GetGroups");
758
759   SMESH::ListOfGroups_var aList = new SMESH::ListOfGroups();
760   // Python Dump
761   TPythonDump aPythonDump;
762   aPythonDump << "[ ";
763
764   try {
765     aList->length( _mapGroups.size() );
766     int i = 0;
767     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
768     for ( ; it != _mapGroups.end(); it++ ) {
769       if ( CORBA::is_nil( it->second )) continue;
770       aList[i++] = SMESH::SMESH_GroupBase::_duplicate( it->second );
771       // Python Dump
772       if (i > 1) aPythonDump << ", ";
773       aPythonDump << it->second;
774     }
775     aList->length( i );
776   }
777   catch(SALOME_Exception & S_ex) {
778     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
779   }
780
781   // Update Python script
782   aPythonDump << " ] = " << _this() << ".GetGroups()";
783
784   return aList._retn();
785 }
786
787 //=============================================================================
788 /*! UnionGroups
789  *  New group is created. All mesh elements that are
790  *  present in initial groups are added to the new one
791  */
792 //=============================================================================
793 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
794                                                   SMESH::SMESH_GroupBase_ptr theGroup2,
795                                                   const char* theName )
796   throw (SALOME::SALOME_Exception)
797 {
798   try
799   {
800     if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
801          theGroup1->GetType() != theGroup2->GetType() )
802       return SMESH::SMESH_Group::_nil();
803
804     // Create Union
805     SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
806     if ( aResGrp->_is_nil() )
807       return SMESH::SMESH_Group::_nil();
808
809     SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
810     SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
811
812     TColStd_MapOfInteger aResMap;
813
814     for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
815       aResMap.Add( anIds1[ i1 ] );
816
817     for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
818       aResMap.Add( anIds2[ i2 ] );
819
820     SMESH::long_array_var aResIds = new SMESH::long_array;
821     aResIds->length( aResMap.Extent() );
822
823     int resI = 0;
824     TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
825     for( ; anIter.More(); anIter.Next() )
826       aResIds[ resI++ ] = anIter.Key();
827
828     aResGrp->Add( aResIds );
829
830     // Clear python lines, created by CreateGroup() and Add()
831     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
832     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
833     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
834
835     // Update Python script
836     TPythonDump() << aResGrp << " = " << _this() << ".UnionGroups( "
837                   << theGroup1 << ", " << theGroup2 << ", '"
838                   << theName << "' )";
839
840     return aResGrp._retn();
841   }
842   catch( ... )
843   {
844     return SMESH::SMESH_Group::_nil();
845   }
846 }
847
848 //=============================================================================
849 /*! IntersectGroups
850  *  New group is created. All mesh elements that are
851  *  present in both initial groups are added to the new one.
852  */
853 //=============================================================================
854 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
855                                                       SMESH::SMESH_GroupBase_ptr theGroup2,
856                                                       const char* theName )
857   throw (SALOME::SALOME_Exception)
858 {
859   if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
860        theGroup1->GetType() != theGroup2->GetType() )
861     return SMESH::SMESH_Group::_nil();
862
863   // Create Intersection
864   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
865   if ( aResGrp->_is_nil() )
866     return aResGrp;
867
868   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
869   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
870
871   TColStd_MapOfInteger aMap1;
872
873   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
874     aMap1.Add( anIds1[ i1 ] );
875
876   TColStd_SequenceOfInteger aSeq;
877
878   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
879     if ( aMap1.Contains( anIds2[ i2 ] ) )
880       aSeq.Append( anIds2[ i2 ] );
881
882   SMESH::long_array_var aResIds = new SMESH::long_array;
883   aResIds->length( aSeq.Length() );
884
885   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
886     aResIds[ resI ] = aSeq( resI + 1 );
887
888   aResGrp->Add( aResIds );
889
890   // Clear python lines, created by CreateGroup() and Add()
891   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
892   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
893   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
894
895   // Update Python script
896   TPythonDump() << aResGrp << " = " << _this() << ".IntersectGroups( "
897                 << theGroup1 << ", " << theGroup2 << ", '" << theName << "')";
898
899   return aResGrp._retn();
900 }
901
902 //=============================================================================
903 /*! CutGroups
904  *  New group is created. All mesh elements that are present in
905  *  main group but do not present in tool group are added to the new one
906  */
907 //=============================================================================
908 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
909                                                 SMESH::SMESH_GroupBase_ptr theGroup2,
910                                                 const char* theName )
911   throw (SALOME::SALOME_Exception)
912 {
913   if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
914        theGroup1->GetType() != theGroup2->GetType() )
915     return SMESH::SMESH_Group::_nil();
916
917   // Perform Cutting
918   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
919   if ( aResGrp->_is_nil() )
920     return aResGrp;
921
922   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
923   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
924
925   TColStd_MapOfInteger aMap2;
926
927   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
928     aMap2.Add( anIds2[ i2 ] );
929
930
931   TColStd_SequenceOfInteger aSeq;
932   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
933     if ( !aMap2.Contains( anIds1[ i1 ] ) )
934       aSeq.Append( anIds1[ i1 ] );
935
936   SMESH::long_array_var aResIds = new SMESH::long_array;
937   aResIds->length( aSeq.Length() );
938
939   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
940     aResIds[ resI ] = aSeq( resI + 1 );
941
942   aResGrp->Add( aResIds );
943
944   // Clear python lines, created by CreateGroup() and Add()
945   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
946   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
947   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
948
949   // Update Python script
950   TPythonDump() << aResGrp << " = " << _this() << ".CutGroups( "
951                 << theGroup1 << ", " << theGroup2 << ", '"
952                 << theName << "' )";
953
954   return aResGrp._retn();
955 }
956
957 //=============================================================================
958 /*!
959  *
960  */
961 //=============================================================================
962
963 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
964 {
965   if(MYDEBUG) MESSAGE( "createSubMesh" );
966   TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
967
968   ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
969   int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
970   SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
971   SMESH::SMESH_subMesh_var subMesh
972     = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
973
974   _mapSubMesh[subMeshId] = mySubMesh;
975   _mapSubMesh_i[subMeshId] = subMeshServant;
976   _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
977
978   // register CORBA object for persistence
979   int nextId = _gen_i->RegisterObject( subMesh );
980   if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
981
982   return subMesh._retn();
983 }
984
985 //=======================================================================
986 //function : getSubMesh
987 //purpose  :
988 //=======================================================================
989
990 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
991 {
992   map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
993   if ( it == _mapSubMeshIor.end() )
994     return SMESH::SMESH_subMesh::_nil();
995
996   return SMESH::SMESH_subMesh::_duplicate( (*it).second );
997 }
998
999
1000 //=============================================================================
1001 /*!
1002  *
1003  */
1004 //=============================================================================
1005
1006 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
1007                                   GEOM::GEOM_Object_ptr    theSubShapeObject )
1008 {
1009   MESSAGE("SMESH_Mesh_i::removeSubMesh()");
1010   if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
1011     return;
1012
1013   try {
1014     SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
1015     for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
1016       removeHypothesis( theSubShapeObject, aHypList[i] );
1017     }
1018   }
1019   catch( const SALOME::SALOME_Exception& ) {
1020     INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
1021   }
1022
1023   int subMeshId = theSubMesh->GetId();
1024
1025   _mapSubMesh.erase(subMeshId);
1026   _mapSubMesh_i.erase(subMeshId);
1027   _mapSubMeshIor.erase(subMeshId);
1028   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
1029 }
1030
1031 //=============================================================================
1032 /*!
1033  *
1034  */
1035 //=============================================================================
1036
1037 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
1038                                                       const char*         theName,
1039                                                       const TopoDS_Shape& theShape )
1040 {
1041   int anId;
1042   SMESH::SMESH_GroupBase_var aGroup;
1043   if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
1044     SMESH_GroupBase_i* aGroupImpl;
1045     if ( !theShape.IsNull() )
1046       aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
1047     else
1048       aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
1049
1050     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
1051     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
1052     aGroupImpl->Register();
1053     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
1054
1055     aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
1056     _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
1057
1058     // register CORBA object for persistence
1059     int nextId = _gen_i->RegisterObject( aGroup );
1060     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
1061   }
1062   return aGroup._retn();
1063 }
1064
1065 //=============================================================================
1066 /*!
1067  * SMESH_Mesh_i::removeGroup
1068  *
1069  * Should be called by ~SMESH_Group_i()
1070  */
1071 //=============================================================================
1072
1073 void SMESH_Mesh_i::removeGroup( const int theId )
1074 {
1075   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );
1076   if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
1077     _mapGroups.erase( theId );
1078     _impl->RemoveGroup( theId );
1079   }
1080 }
1081
1082
1083 //=============================================================================
1084 /*!
1085  *
1086  */
1087 //=============================================================================
1088
1089 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
1090 throw(SALOME::SALOME_Exception)
1091 {
1092   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
1093
1094   SMESH::log_array_var aLog;
1095   try{
1096     list < SMESHDS_Command * >logDS = _impl->GetLog();
1097     aLog = new SMESH::log_array;
1098     int indexLog = 0;
1099     int lg = logDS.size();
1100     SCRUTE(lg);
1101     aLog->length(lg);
1102     list < SMESHDS_Command * >::iterator its = logDS.begin();
1103     while(its != logDS.end()){
1104       SMESHDS_Command *com = *its;
1105       int comType = com->GetType();
1106       //SCRUTE(comType);
1107       int lgcom = com->GetNumber();
1108       //SCRUTE(lgcom);
1109       const list < int >&intList = com->GetIndexes();
1110       int inum = intList.size();
1111       //SCRUTE(inum);
1112       list < int >::const_iterator ii = intList.begin();
1113       const list < double >&coordList = com->GetCoords();
1114       int rnum = coordList.size();
1115       //SCRUTE(rnum);
1116       list < double >::const_iterator ir = coordList.begin();
1117       aLog[indexLog].commandType = comType;
1118       aLog[indexLog].number = lgcom;
1119       aLog[indexLog].coords.length(rnum);
1120       aLog[indexLog].indexes.length(inum);
1121       for(int i = 0; i < rnum; i++){
1122         aLog[indexLog].coords[i] = *ir;
1123         //MESSAGE(" "<<i<<" "<<ir.Value());
1124         ir++;
1125       }
1126       for(int i = 0; i < inum; i++){
1127         aLog[indexLog].indexes[i] = *ii;
1128         //MESSAGE(" "<<i<<" "<<ii.Value());
1129         ii++;
1130       }
1131       indexLog++;
1132       its++;
1133     }
1134     if(clearAfterGet)
1135       _impl->ClearLog();
1136   }
1137   catch(SALOME_Exception & S_ex){
1138     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1139   }
1140   return aLog._retn();
1141 }
1142
1143
1144 //=============================================================================
1145 /*!
1146  *
1147  */
1148 //=============================================================================
1149
1150 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
1151 {
1152   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
1153   // ****
1154 }
1155
1156 //=============================================================================
1157 /*!
1158  *
1159  */
1160 //=============================================================================
1161
1162 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1163 {
1164   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1165   return _id;
1166 }
1167
1168 //=============================================================================
1169 /*!
1170  *
1171  */
1172 //=============================================================================
1173
1174 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1175 {
1176   return _studyId;
1177 }
1178
1179 //=============================================================================
1180 /*!
1181  *
1182  */
1183 //=============================================================================
1184
1185 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1186 {
1187   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1188   _impl = impl;
1189 }
1190
1191 //=============================================================================
1192 /*!
1193  *
1194  */
1195 //=============================================================================
1196
1197 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1198 {
1199   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1200   return *_impl;
1201 }
1202
1203 //=============================================================================
1204 /*!
1205  * Return mesh editor
1206  */
1207 //=============================================================================
1208
1209 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1210 {
1211   // Create MeshEditor
1212   SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl, false );
1213   SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1214
1215   // Update Python script
1216   TPythonDump() << aMeshEditor << " = " << _this() << ".GetMeshEditor()";
1217
1218   return aMesh._retn();
1219 }
1220
1221 //=============================================================================
1222 /*!
1223  * Return mesh edition previewer
1224  */
1225 //=============================================================================
1226
1227 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditPreviewer()
1228 {
1229   SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl, true );
1230   SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1231   return aMesh._retn();
1232 }
1233
1234 //=============================================================================
1235 /*!
1236  *  Export in different formats
1237  */
1238 //=============================================================================
1239
1240 CORBA::Boolean SMESH_Mesh_i::HasDuplicatedGroupNamesMED()
1241 {
1242   return _impl->HasDuplicatedGroupNamesMED();
1243 }
1244
1245 static void PrepareForWriting (const char* file)
1246 {
1247   TCollection_AsciiString aFullName ((char*)file);
1248   OSD_Path aPath (aFullName);
1249   OSD_File aFile (aPath);
1250   if (aFile.Exists()) {
1251     // existing filesystem node
1252     if (aFile.KindOfFile() == OSD_FILE) {
1253       if (aFile.IsWriteable()) {
1254         aFile.Reset();
1255         aFile.Remove();
1256         if (aFile.Failed()) {
1257           TCollection_AsciiString msg ("File ");
1258           msg += aFullName + " cannot be replaced.";
1259           THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1260         }
1261       } else {
1262         TCollection_AsciiString msg ("File ");
1263         msg += aFullName + " cannot be overwritten.";
1264         THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1265       }
1266     } else {
1267       TCollection_AsciiString msg ("Location ");
1268       msg += aFullName + " is not a file.";
1269       THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1270     }
1271   } else {
1272     // nonexisting file; check if it can be created
1273     aFile.Reset();
1274     aFile.Build(OSD_WriteOnly, OSD_Protection());
1275     if (aFile.Failed()) {
1276       TCollection_AsciiString msg ("You cannot create the file ");
1277       msg += aFullName + ". Check the directory existance and access rights.";
1278       THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1279     } else {
1280       aFile.Close();
1281       aFile.Remove();
1282     }
1283   }
1284 }
1285
1286 void SMESH_Mesh_i::ExportToMED (const char* file,
1287                                 CORBA::Boolean auto_groups,
1288                                 SMESH::MED_VERSION theVersion)
1289   throw(SALOME::SALOME_Exception)
1290 {
1291   Unexpect aCatch(SALOME_SalomeException);
1292
1293   // Update Python script
1294   TPythonDump() << _this() << ".ExportToMED( '"
1295                 << file << "', " << auto_groups << ", " << theVersion << " )";
1296
1297   // Perform Export
1298   PrepareForWriting(file);
1299   char* aMeshName = "Mesh";
1300   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1301   if ( !aStudy->_is_nil() ) {
1302     SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
1303     if ( !aMeshSO->_is_nil() ) {
1304       aMeshName = aMeshSO->GetName();
1305       //SCRUTE(file);
1306       //SCRUTE(aMeshName);
1307       //SCRUTE(aMeshSO->GetID());
1308
1309       // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes
1310       if ( !aStudy->GetProperties()->IsLocked() )
1311         {
1312         SALOMEDS::GenericAttribute_var anAttr;
1313         SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1314         SALOMEDS::AttributeExternalFileDef_var aFileName;
1315         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1316         aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1317         ASSERT(!aFileName->_is_nil());
1318         aFileName->SetValue(file);
1319         SALOMEDS::AttributeFileType_var aFileType;
1320         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1321         aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1322         ASSERT(!aFileType->_is_nil());
1323         aFileType->SetValue("FICHIERMED");
1324         }
1325     }
1326   }
1327   _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
1328 }
1329
1330 void SMESH_Mesh_i::ExportMED (const char* file,
1331                               CORBA::Boolean auto_groups)
1332   throw(SALOME::SALOME_Exception)
1333 {
1334   ExportToMED(file,auto_groups,SMESH::MED_V2_1);
1335 }
1336
1337 void SMESH_Mesh_i::ExportDAT (const char *file)
1338   throw(SALOME::SALOME_Exception)
1339 {
1340   Unexpect aCatch(SALOME_SalomeException);
1341
1342   // Update Python script
1343   TPythonDump() << _this() << ".ExportDAT( '" << file << "' )";
1344
1345   // Perform Export
1346   PrepareForWriting(file);
1347   _impl->ExportDAT(file);
1348 }
1349
1350 void SMESH_Mesh_i::ExportUNV (const char *file)
1351   throw(SALOME::SALOME_Exception)
1352 {
1353   Unexpect aCatch(SALOME_SalomeException);
1354
1355   // Update Python script
1356   TPythonDump() << _this() << ".ExportUNV( '" << file << "' )";
1357
1358   // Perform Export
1359   PrepareForWriting(file);
1360   _impl->ExportUNV(file);
1361 }
1362
1363 void SMESH_Mesh_i::ExportSTL (const char *file, const bool isascii)
1364   throw(SALOME::SALOME_Exception)
1365 {
1366   Unexpect aCatch(SALOME_SalomeException);
1367
1368   // Update Python script
1369   TPythonDump() << _this() << ".ExportSTL( '" << file << "', " << isascii << " )";
1370
1371   // Perform Export
1372   PrepareForWriting(file);
1373   _impl->ExportSTL(file, isascii);
1374 }
1375
1376 //=============================================================================
1377 /*!
1378  *
1379  */
1380 //=============================================================================
1381
1382 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1383 {
1384   Unexpect aCatch(SALOME_SalomeException);
1385   SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1386   SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1387   return aMesh._retn();
1388 }
1389
1390 //=============================================================================
1391 /*!
1392  *
1393  */
1394 //=============================================================================
1395 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1396 {
1397   Unexpect aCatch(SALOME_SalomeException);
1398   return _impl->NbNodes();
1399 }
1400
1401 //=============================================================================
1402 /*!
1403  *
1404  */
1405 //=============================================================================
1406 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
1407 {
1408   Unexpect aCatch(SALOME_SalomeException);
1409   return NbEdges() + NbFaces() + NbVolumes();
1410 }
1411
1412 //=============================================================================
1413 /*!
1414  *
1415  */
1416 //=============================================================================
1417 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1418 {
1419   Unexpect aCatch(SALOME_SalomeException);
1420   return _impl->NbEdges();
1421 }
1422
1423 CORBA::Long SMESH_Mesh_i::NbEdgesOfOrder(SMESH::ElementOrder order)
1424   throw(SALOME::SALOME_Exception)
1425 {
1426   Unexpect aCatch(SALOME_SalomeException);
1427   return _impl->NbEdges( (::SMESH_Mesh::ElementOrder) order);
1428 }
1429
1430 //=============================================================================
1431 /*!
1432  *
1433  */
1434 //=============================================================================
1435 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1436 {
1437   Unexpect aCatch(SALOME_SalomeException);
1438   return _impl->NbFaces();
1439 }
1440
1441 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1442 {
1443   Unexpect aCatch(SALOME_SalomeException);
1444   return _impl->NbTriangles();
1445 }
1446
1447 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1448 {
1449   Unexpect aCatch(SALOME_SalomeException);
1450   return _impl->NbQuadrangles();
1451 }
1452
1453 CORBA::Long SMESH_Mesh_i::NbPolygons()throw(SALOME::SALOME_Exception)
1454 {
1455   Unexpect aCatch(SALOME_SalomeException);
1456   return _impl->NbPolygons();
1457 }
1458
1459 CORBA::Long SMESH_Mesh_i::NbFacesOfOrder(SMESH::ElementOrder order)
1460   throw(SALOME::SALOME_Exception)
1461 {
1462   Unexpect aCatch(SALOME_SalomeException);
1463   return _impl->NbFaces( (::SMESH_Mesh::ElementOrder) order);
1464 }
1465
1466 CORBA::Long SMESH_Mesh_i::NbTrianglesOfOrder(SMESH::ElementOrder order)
1467   throw(SALOME::SALOME_Exception)
1468 {
1469   Unexpect aCatch(SALOME_SalomeException);
1470   return _impl->NbTriangles( (::SMESH_Mesh::ElementOrder) order);
1471 }
1472
1473 CORBA::Long SMESH_Mesh_i::NbQuadranglesOfOrder(SMESH::ElementOrder order)
1474   throw(SALOME::SALOME_Exception)
1475 {
1476   Unexpect aCatch(SALOME_SalomeException);
1477   return _impl->NbQuadrangles( (::SMESH_Mesh::ElementOrder) order);
1478 }
1479
1480 //=============================================================================
1481 /*!
1482  *
1483  */
1484 //=============================================================================
1485 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1486 {
1487   Unexpect aCatch(SALOME_SalomeException);
1488   return _impl->NbVolumes();
1489 }
1490
1491 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1492 {
1493   Unexpect aCatch(SALOME_SalomeException);
1494   return _impl->NbTetras();
1495 }
1496
1497 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1498 {
1499   Unexpect aCatch(SALOME_SalomeException);
1500   return _impl->NbHexas();
1501 }
1502
1503 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1504 {
1505   Unexpect aCatch(SALOME_SalomeException);
1506   return _impl->NbPyramids();
1507 }
1508
1509 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1510 {
1511   Unexpect aCatch(SALOME_SalomeException);
1512   return _impl->NbPrisms();
1513 }
1514
1515 CORBA::Long SMESH_Mesh_i::NbPolyhedrons()throw(SALOME::SALOME_Exception)
1516 {
1517   Unexpect aCatch(SALOME_SalomeException);
1518   return _impl->NbPolyhedrons();
1519 }
1520
1521 CORBA::Long SMESH_Mesh_i::NbVolumesOfOrder(SMESH::ElementOrder order)
1522   throw(SALOME::SALOME_Exception)
1523 {
1524   Unexpect aCatch(SALOME_SalomeException);
1525   return _impl->NbVolumes( (::SMESH_Mesh::ElementOrder) order);
1526 }
1527
1528 CORBA::Long SMESH_Mesh_i::NbTetrasOfOrder(SMESH::ElementOrder order)
1529   throw(SALOME::SALOME_Exception)
1530 {
1531   Unexpect aCatch(SALOME_SalomeException);
1532   return _impl->NbTetras( (::SMESH_Mesh::ElementOrder) order);
1533 }
1534
1535 CORBA::Long SMESH_Mesh_i::NbHexasOfOrder(SMESH::ElementOrder order)
1536   throw(SALOME::SALOME_Exception)
1537 {
1538   Unexpect aCatch(SALOME_SalomeException);
1539   return _impl->NbHexas( (::SMESH_Mesh::ElementOrder) order);
1540 }
1541
1542 CORBA::Long SMESH_Mesh_i::NbPyramidsOfOrder(SMESH::ElementOrder order)
1543   throw(SALOME::SALOME_Exception)
1544 {
1545   Unexpect aCatch(SALOME_SalomeException);
1546   return _impl->NbPyramids( (::SMESH_Mesh::ElementOrder) order);
1547 }
1548
1549 CORBA::Long SMESH_Mesh_i::NbPrismsOfOrder(SMESH::ElementOrder order)
1550   throw(SALOME::SALOME_Exception)
1551 {
1552   Unexpect aCatch(SALOME_SalomeException);
1553   return _impl->NbPrisms( (::SMESH_Mesh::ElementOrder) order);
1554 }
1555
1556 //=============================================================================
1557 /*!
1558  *
1559  */
1560 //=============================================================================
1561 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1562 {
1563   Unexpect aCatch(SALOME_SalomeException);
1564   return _impl->NbSubMesh();
1565 }
1566
1567 //=============================================================================
1568 /*!
1569  *
1570  */
1571 //=============================================================================
1572 char* SMESH_Mesh_i::Dump()
1573 {
1574   std::ostringstream os;
1575   _impl->Dump( os );
1576   return CORBA::string_dup( os.str().c_str() );
1577 }
1578
1579 //=============================================================================
1580 /*!
1581  *
1582  */
1583 //=============================================================================
1584 SMESH::long_array* SMESH_Mesh_i::GetIDs()
1585 {
1586 //   SMESH::long_array_var aResult = new SMESH::long_array();
1587 //   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1588 //   int aMinId = aSMESHDS_Mesh->MinElementID();
1589 //   int aMaxId =  aSMESHDS_Mesh->MaxElementID();
1590
1591 //   aResult->length(aMaxId - aMinId + 1);
1592
1593 //   for (int i = 0, id = aMinId; id <= aMaxId; id++  )
1594 //     aResult[i++] = id;
1595
1596 //   return aResult._retn();
1597   // PAL12398
1598   return GetElementsId();
1599 }
1600
1601 //=============================================================================
1602 /*!
1603  *
1604  */
1605 //=============================================================================
1606
1607 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
1608      throw (SALOME::SALOME_Exception)
1609 {
1610   Unexpect aCatch(SALOME_SalomeException);
1611   MESSAGE("SMESH_Mesh_i::GetElementsId");
1612   SMESH::long_array_var aResult = new SMESH::long_array();
1613   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1614
1615   if ( aSMESHDS_Mesh == NULL )
1616     return aResult._retn();
1617
1618   long nbElements = NbElements();
1619   aResult->length( nbElements );
1620   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1621   for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
1622     aResult[i] = anIt->next()->GetID();
1623
1624   return aResult._retn();
1625 }
1626
1627
1628 //=============================================================================
1629 /*!
1630  *
1631  */
1632 //=============================================================================
1633
1634 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
1635     throw (SALOME::SALOME_Exception)
1636 {
1637   Unexpect aCatch(SALOME_SalomeException);
1638   MESSAGE("SMESH_subMesh_i::GetElementsByType");
1639   SMESH::long_array_var aResult = new SMESH::long_array();
1640   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1641
1642   if ( aSMESHDS_Mesh == NULL )
1643     return aResult._retn();
1644
1645   long nbElements = NbElements();
1646
1647   // No sense in returning ids of elements along with ids of nodes:
1648   // when theElemType == SMESH::ALL, return node ids only if
1649   // there are no elements
1650   if ( theElemType == SMESH::NODE || theElemType == SMESH::ALL && nbElements == 0 )
1651     return GetNodesId();
1652
1653   aResult->length( nbElements );
1654
1655   int i = 0;
1656
1657   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1658   while ( i < nbElements && anIt->more() ) {
1659     const SMDS_MeshElement* anElem = anIt->next();
1660     if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
1661       aResult[i++] = anElem->GetID();
1662   }
1663
1664   aResult->length( i );
1665
1666   return aResult._retn();
1667 }
1668
1669 //=============================================================================
1670 /*!
1671  *
1672  */
1673 //=============================================================================
1674
1675 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
1676   throw (SALOME::SALOME_Exception)
1677 {
1678   Unexpect aCatch(SALOME_SalomeException);
1679   MESSAGE("SMESH_subMesh_i::GetNodesId");
1680   SMESH::long_array_var aResult = new SMESH::long_array();
1681   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1682
1683   if ( aSMESHDS_Mesh == NULL )
1684     return aResult._retn();
1685
1686   long nbNodes = NbNodes();
1687   aResult->length( nbNodes );
1688   SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator();
1689   for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
1690     aResult[i] = anIt->next()->GetID();
1691
1692   return aResult._retn();
1693 }
1694
1695 //=============================================================================
1696 /*!
1697  *
1698  */
1699 //=============================================================================
1700
1701 SMESH::ElementType SMESH_Mesh_i::GetElementType( const CORBA::Long id, const bool iselem )
1702   throw (SALOME::SALOME_Exception)
1703 {
1704   return ( SMESH::ElementType )_impl->GetElementType( id, iselem );
1705 }
1706
1707
1708 //=============================================================================
1709 /*!
1710  * Returns ID of elements for given submesh
1711  */
1712 //=============================================================================
1713 SMESH::long_array* SMESH_Mesh_i::GetSubMeshElementsId(const CORBA::Long ShapeID)
1714      throw (SALOME::SALOME_Exception)
1715 {
1716   SMESH::long_array_var aResult = new SMESH::long_array();
1717
1718   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
1719   if(!SM) return aResult._retn();
1720
1721   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
1722   if(!SDSM) return aResult._retn();
1723
1724   aResult->length(SDSM->NbElements());
1725
1726   SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
1727   int i = 0;
1728   while ( eIt->more() ) {
1729     aResult[i++] = eIt->next()->GetID();
1730   }
1731
1732   return aResult._retn();
1733 }
1734
1735
1736 //=============================================================================
1737 /*!
1738  * Returns ID of nodes for given submesh
1739  * If param all==true - returns all nodes, else -
1740  * returns only nodes on shapes.
1741  */
1742 //=============================================================================
1743 SMESH::long_array* SMESH_Mesh_i::GetSubMeshNodesId(const CORBA::Long ShapeID, CORBA::Boolean all)
1744      throw (SALOME::SALOME_Exception)
1745 {
1746   SMESH::long_array_var aResult = new SMESH::long_array();
1747
1748   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
1749   if(!SM) return aResult._retn();
1750
1751   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
1752   if(!SDSM) return aResult._retn();
1753
1754   map<int,const SMDS_MeshElement*> theElems;
1755   if( !all || (SDSM->NbElements()==0 && SDSM->NbNodes()==1) ) {
1756     SMDS_NodeIteratorPtr nIt = SDSM->GetNodes();
1757     while ( nIt->more() ) {
1758       const SMDS_MeshNode* elem = nIt->next();
1759       theElems.insert( make_pair(elem->GetID(),elem) );
1760     }
1761   }
1762   else { // all nodes of submesh elements
1763     SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
1764     while ( eIt->more() ) {
1765       const SMDS_MeshElement* anElem = eIt->next();
1766       SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
1767       while ( nIt->more() ) {
1768         const SMDS_MeshElement* elem = nIt->next();
1769         theElems.insert( make_pair(elem->GetID(),elem) );
1770       }
1771     }
1772   }
1773
1774   aResult->length(theElems.size());
1775   map<int, const SMDS_MeshElement * >::iterator itElem;
1776   int i = 0;
1777   for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
1778     aResult[i++] = (*itElem).first;
1779
1780   return aResult._retn();
1781 }
1782   
1783
1784 //=============================================================================
1785 /*!
1786  * Returns type of elements for given submesh
1787  */
1788 //=============================================================================
1789 SMESH::ElementType SMESH_Mesh_i::GetSubMeshElementType(const CORBA::Long ShapeID)
1790      throw (SALOME::SALOME_Exception)
1791 {
1792   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
1793   if(!SM) return SMESH::ALL;
1794
1795   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
1796   if(!SDSM) return SMESH::ALL;
1797
1798   if(SDSM->NbElements()==0)
1799     return (SM->GetSubShape().ShapeType() == TopAbs_VERTEX) ? SMESH::NODE : SMESH::ALL;
1800
1801   SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
1802   const SMDS_MeshElement* anElem = eIt->next();
1803   return ( SMESH::ElementType ) anElem->GetType();
1804 }
1805   
1806
1807 //=============================================================================
1808 /*!
1809  *
1810  */
1811 //=============================================================================
1812
1813 CORBA::Long SMESH_Mesh_i::GetMeshPtr()
1814 {
1815   return CORBA::Long(size_t(_impl));
1816 }
1817
1818
1819 //=============================================================================
1820 /*!
1821  * Get XYZ coordinates of node as list of double
1822  * If there is not node for given ID - returns empty list
1823  */
1824 //=============================================================================
1825
1826 SMESH::double_array* SMESH_Mesh_i::GetNodeXYZ(const CORBA::Long id)
1827 {
1828   SMESH::double_array_var aResult = new SMESH::double_array();
1829   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1830   if ( aSMESHDS_Mesh == NULL )
1831     return aResult._retn();
1832
1833   // find node
1834   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
1835   if(!aNode)
1836     return aResult._retn();
1837
1838   // add coordinates
1839   aResult->length(3);
1840   aResult[0] = aNode->X();
1841   aResult[1] = aNode->Y();
1842   aResult[2] = aNode->Z();
1843   return aResult._retn();
1844 }
1845
1846
1847 //=============================================================================
1848 /*!
1849  * For given node returns list of IDs of inverse elements
1850  * If there is not node for given ID - returns empty list
1851  */
1852 //=============================================================================
1853
1854 SMESH::long_array* SMESH_Mesh_i::GetNodeInverseElements(const CORBA::Long id)
1855 {
1856   SMESH::long_array_var aResult = new SMESH::long_array();
1857   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1858   if ( aSMESHDS_Mesh == NULL )
1859     return aResult._retn();
1860
1861   // find node
1862   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
1863   if(!aNode)
1864     return aResult._retn();
1865
1866   // find inverse elements
1867   SMDS_ElemIteratorPtr eIt = aNode->GetInverseElementIterator();
1868   TColStd_SequenceOfInteger IDs;
1869   while(eIt->more()) {
1870     const SMDS_MeshElement* elem = eIt->next();
1871     IDs.Append(elem->GetID());
1872   }
1873   if(IDs.Length()>0) {
1874     aResult->length(IDs.Length());
1875     int i = 1;
1876     for(; i<=IDs.Length(); i++) {
1877       aResult[i-1] = IDs.Value(i);
1878     }
1879   }
1880   return aResult._retn();
1881 }
1882
1883
1884 //=============================================================================
1885 /*!
1886  * If given element is node returns IDs of shape from position
1887  * If there is not node for given ID - returns -1
1888  */
1889 //=============================================================================
1890
1891 CORBA::Long SMESH_Mesh_i::GetShapeID(const CORBA::Long id)
1892 {
1893   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1894   if ( aSMESHDS_Mesh == NULL )
1895     return -1;
1896
1897   // try to find node
1898   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
1899   if(aNode) {
1900     SMDS_PositionPtr pos = aNode->GetPosition();
1901     if(!pos)
1902       return -1;
1903     else
1904       return pos->GetShapeId();
1905   }
1906
1907   return -1;
1908 }
1909
1910
1911 //=============================================================================
1912 /*!
1913  * For given element returns ID of result shape after 
1914  * ::FindShape() from SMESH_MeshEditor
1915  * If there is not element for given ID - returns -1
1916  */
1917 //=============================================================================
1918
1919 CORBA::Long SMESH_Mesh_i::GetShapeIDForElem(const CORBA::Long id)
1920 {
1921   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1922   if ( aSMESHDS_Mesh == NULL )
1923     return -1;
1924
1925   // try to find element
1926   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
1927   if(!elem)
1928     return -1;
1929
1930   //SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
1931   ::SMESH_MeshEditor aMeshEditor(_impl);
1932   int index = aMeshEditor.FindShape( elem );
1933   if(index>0)
1934     return index;
1935
1936   return -1;
1937 }
1938
1939
1940 //=============================================================================
1941 /*!
1942  * Returns number of nodes for given element
1943  * If there is not element for given ID - returns -1
1944  */
1945 //=============================================================================
1946
1947 CORBA::Long SMESH_Mesh_i::GetElemNbNodes(const CORBA::Long id)
1948 {
1949   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1950   if ( aSMESHDS_Mesh == NULL ) return -1;
1951   // try to find element
1952   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
1953   if(!elem) return -1;
1954   return elem->NbNodes();
1955 }
1956
1957
1958 //=============================================================================
1959 /*!
1960  * Returns ID of node by given index for given element
1961  * If there is not element for given ID - returns -1
1962  * If there is not node for given index - returns -2
1963  */
1964 //=============================================================================
1965
1966 CORBA::Long SMESH_Mesh_i::GetElemNode(const CORBA::Long id, const CORBA::Long index)
1967 {
1968   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1969   if ( aSMESHDS_Mesh == NULL ) return -1;
1970   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
1971   if(!elem) return -1;
1972   if( index>=elem->NbNodes() || index<0 ) return -1;
1973   return elem->GetNode(index)->GetID();
1974 }
1975
1976 //=============================================================================
1977 /*!
1978  * Returns IDs of nodes of given element
1979  */
1980 //=============================================================================
1981
1982 SMESH::long_array* SMESH_Mesh_i::GetElemNodes(const CORBA::Long id)
1983 {
1984   SMESH::long_array_var aResult = new SMESH::long_array();
1985   if ( SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS() )
1986   {
1987     if ( const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id) )
1988     {
1989       aResult->length( elem->NbNodes() );
1990       for ( int i = 0; i < elem->NbNodes(); ++i )
1991         aResult[ i ] = elem->GetNode( i )->GetID();
1992     }
1993   }
1994   return aResult._retn();
1995 }
1996
1997 //=============================================================================
1998 /*!
1999  * Returns true if given node is medium node
2000  * in given quadratic element
2001  */
2002 //=============================================================================
2003
2004 CORBA::Boolean SMESH_Mesh_i::IsMediumNode(const CORBA::Long ide, const CORBA::Long idn)
2005 {
2006   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2007   if ( aSMESHDS_Mesh == NULL ) return false;
2008   // try to find node
2009   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(idn);
2010   if(!aNode) return false;
2011   // try to find element
2012   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(ide);
2013   if(!elem) return false;
2014
2015   return elem->IsMediumNode(aNode);
2016 }
2017
2018
2019 //=============================================================================
2020 /*!
2021  * Returns true if given node is medium node
2022  * in one of quadratic elements
2023  */
2024 //=============================================================================
2025
2026 CORBA::Boolean SMESH_Mesh_i::IsMediumNodeOfAnyElem(const CORBA::Long idn,
2027                                                    SMESH::ElementType theElemType)
2028 {
2029   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2030   if ( aSMESHDS_Mesh == NULL ) return false;
2031
2032   // try to find node
2033   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(idn);
2034   if(!aNode) return false;
2035
2036   SMESH_MesherHelper aHelper( *(_impl) );
2037
2038   SMDSAbs_ElementType aType;
2039   if(theElemType==SMESH::EDGE) aType = SMDSAbs_Edge;
2040   else if(theElemType==SMESH::FACE) aType = SMDSAbs_Face;
2041   else if(theElemType==SMESH::VOLUME) aType = SMDSAbs_Volume;
2042   else aType = SMDSAbs_All;
2043
2044   return aHelper.IsMedium(aNode,aType);
2045 }
2046
2047
2048 //=============================================================================
2049 /*!
2050  * Returns number of edges for given element
2051  */
2052 //=============================================================================
2053
2054 CORBA::Long SMESH_Mesh_i::ElemNbEdges(const CORBA::Long id)
2055 {
2056   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2057   if ( aSMESHDS_Mesh == NULL ) return -1;
2058   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2059   if(!elem) return -1;
2060   return elem->NbEdges();
2061 }
2062
2063
2064 //=============================================================================
2065 /*!
2066  * Returns number of faces for given element
2067  */
2068 //=============================================================================
2069
2070 CORBA::Long SMESH_Mesh_i::ElemNbFaces(const CORBA::Long id)
2071 {
2072   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2073   if ( aSMESHDS_Mesh == NULL ) return -1;
2074   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2075   if(!elem) return -1;
2076   return elem->NbFaces();
2077 }
2078
2079
2080 //=============================================================================
2081 /*!
2082  * Returns true if given element is polygon
2083  */
2084 //=============================================================================
2085
2086 CORBA::Boolean SMESH_Mesh_i::IsPoly(const CORBA::Long id)
2087 {
2088   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2089   if ( aSMESHDS_Mesh == NULL ) return false;
2090   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2091   if(!elem) return false;
2092   return elem->IsPoly();
2093 }
2094
2095
2096 //=============================================================================
2097 /*!
2098  * Returns true if given element is quadratic
2099  */
2100 //=============================================================================
2101
2102 CORBA::Boolean SMESH_Mesh_i::IsQuadratic(const CORBA::Long id)
2103 {
2104   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2105   if ( aSMESHDS_Mesh == NULL ) return false;
2106   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2107   if(!elem) return false;
2108   return elem->IsQuadratic();
2109 }
2110
2111
2112 //=============================================================================
2113 /*!
2114  * Returns bary center for given element
2115  */
2116 //=============================================================================
2117
2118 SMESH::double_array* SMESH_Mesh_i::BaryCenter(const CORBA::Long id)
2119 {
2120   SMESH::double_array_var aResult = new SMESH::double_array();
2121   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2122   if ( aSMESHDS_Mesh == NULL )
2123     return aResult._retn();
2124
2125   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2126   if(!elem)
2127     return aResult._retn();
2128
2129   if(elem->GetType()==SMDSAbs_Volume) {
2130     // use SMDS_VolumeTool
2131     SMDS_VolumeTool aTool;
2132     if(aTool.Set(elem)) {
2133       double x=0., y=0., z=0.;
2134       if(aTool.GetBaryCenter(x,y,z)) {
2135         // add coordinates
2136         aResult->length(3);
2137         aResult[0] = x;
2138         aResult[1] = y;
2139         aResult[2] = z;
2140       }
2141     }
2142   }
2143   else {
2144     SMDS_ElemIteratorPtr anIt = elem->nodesIterator();
2145     int nbn = 0;
2146     double x=0., y=0., z=0.;
2147     for(; anIt->more(); ) {
2148       nbn++;
2149       const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(anIt->next());
2150       x += aNode->X();
2151       y += aNode->Y();
2152       z += aNode->Z();
2153     }
2154     if(nbn>0) {
2155       // add coordinates
2156       aResult->length(3);
2157       aResult[0] = x/nbn;
2158       aResult[1] = y/nbn;
2159       aResult[2] = z/nbn;
2160     }
2161   }
2162
2163   return aResult._retn();
2164 }
2165
2166
2167 //=============================================================================
2168 /*!
2169  *
2170  */
2171 //=============================================================================
2172 void SMESH_Mesh_i::CreateGroupServants() 
2173 {
2174   // Create group servants, if any groups were imported
2175   list<int> aGroupIds = _impl->GetGroupIds();
2176   for ( list<int>::iterator it = aGroupIds.begin(); it != aGroupIds.end(); it++ ) {
2177     SMESH_Group_i* aGroupImpl     = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, *it );
2178
2179     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
2180     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
2181     aGroupImpl->Register();
2182     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
2183
2184     SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
2185     _mapGroups[*it]               = SMESH::SMESH_Group::_duplicate( aGroup );
2186
2187     // register CORBA object for persistence
2188     int nextId = _gen_i->RegisterObject( aGroup );
2189     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
2190   }
2191 }
2192