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