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