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