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