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