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