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