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