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