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