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