Salome HOME
Merge from OCC_development_generic_2006
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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
50 // OCCT Includes
51 #include <OSD_Path.hxx>
52 #include <OSD_File.hxx>
53 #include <OSD_Directory.hxx>
54 #include <OSD_Protection.hxx>
55 #include <TColStd_MapOfInteger.hxx>
56 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
57 #include <TColStd_SequenceOfInteger.hxx>
58 #include "TCollection_AsciiString.hxx"
59
60 // STL Includes
61 #include <string>
62 #include <iostream>
63 #include <sstream>
64
65 #ifdef _DEBUG_
66 static int MYDEBUG = 0;
67 #else
68 static int MYDEBUG = 0;
69 #endif
70
71 using namespace std;
72 using SMESH::TPythonDump;
73
74 int SMESH_Mesh_i::myIdGenerator = 0;
75
76 //=============================================================================
77 /*!
78  *  Constructor
79  */
80 //=============================================================================
81
82 SMESH_Mesh_i::SMESH_Mesh_i( PortableServer::POA_ptr thePOA,
83                             SMESH_Gen_i*            gen_i,
84                             CORBA::Long studyId )
85 : SALOME::GenericObj_i( thePOA )
86 {
87   INFOS("SMESH_Mesh_i");
88   _impl = NULL;
89   _gen_i = gen_i;
90   _id = myIdGenerator++;
91   _studyId = studyId;
92   thePOA->activate_object( this );
93 }
94
95 //=============================================================================
96 /*!
97  *  Destructor
98  */
99 //=============================================================================
100
101 SMESH_Mesh_i::~SMESH_Mesh_i()
102 {
103   INFOS("~SMESH_Mesh_i");
104   map<int, SMESH::SMESH_GroupBase_ptr>::iterator it;
105   for ( it = _mapGroups.begin(); it != _mapGroups.end(); it++ ) {
106     SMESH_GroupBase_i* aGroup = dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( it->second ).in() );
107     if ( aGroup ) {
108
109       // this method is colled from destructor of group (PAL6331)
110       //_impl->RemoveGroup( aGroup->GetLocalID() );
111
112       aGroup->Destroy();
113     }
114   }
115   _mapGroups.clear();
116 }
117
118 //=============================================================================
119 /*!
120  *  SetShape
121  *
122  *  Associates <this> mesh with <theShape> and puts a reference
123  *  to <theShape> into the current study;
124  *  the previous shape is substituted by the new one.
125  */
126 //=============================================================================
127
128 void SMESH_Mesh_i::SetShape( GEOM::GEOM_Object_ptr theShapeObject )
129     throw (SALOME::SALOME_Exception)
130 {
131   Unexpect aCatch(SALOME_SalomeException);
132   try {
133     _impl->ShapeToMesh( _gen_i->GeomObjectToShape( theShapeObject ));
134   }
135   catch(SALOME_Exception & S_ex) {
136     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
137   }
138 }
139
140 //=======================================================================
141 //function : GetShapeToMesh
142 //purpose  :
143 //=======================================================================
144
145 GEOM::GEOM_Object_ptr SMESH_Mesh_i::GetShapeToMesh()
146     throw (SALOME::SALOME_Exception)
147 {
148   Unexpect aCatch(SALOME_SalomeException);
149   GEOM::GEOM_Object_var aShapeObj;
150   try {
151     TopoDS_Shape S = _impl->GetMeshDS()->ShapeToMesh();
152     if ( !S.IsNull() )
153       aShapeObj = _gen_i->ShapeToGeomObject( S );
154   }
155   catch(SALOME_Exception & S_ex) {
156     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
157   }
158   return aShapeObj._retn();
159 }
160
161 //=============================================================================
162 /*!
163  *
164  */
165 //=============================================================================
166
167 static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
168 {
169   SMESH::DriverMED_ReadStatus res;
170   switch (theStatus)
171   {
172   case DriverMED_R_SMESHDS_Mesh::DRS_OK:
173     res = SMESH::DRS_OK; break;
174   case DriverMED_R_SMESHDS_Mesh::DRS_EMPTY:
175     res = SMESH::DRS_EMPTY; break;
176   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_RENUMBER:
177     res = SMESH::DRS_WARN_RENUMBER; break;
178   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_SKIP_ELEM:
179     res = SMESH::DRS_WARN_SKIP_ELEM; break;
180   case DriverMED_R_SMESHDS_Mesh::DRS_FAIL:
181   default:
182     res = SMESH::DRS_FAIL; break;
183   }
184   return res;
185 }
186
187 //=============================================================================
188 /*!
189  *  ImportMEDFile
190  *
191  *  Imports mesh data from MED file
192  */
193 //=============================================================================
194
195 SMESH::DriverMED_ReadStatus
196 SMESH_Mesh_i::ImportMEDFile( const char* theFileName, const char* theMeshName )
197   throw ( SALOME::SALOME_Exception )
198 {
199   Unexpect aCatch(SALOME_SalomeException);
200   int status;
201   try {
202     status = importMEDFile( theFileName, theMeshName );
203   }
204   catch( SALOME_Exception& S_ex ) {
205     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
206   }
207   catch ( ... ) {
208     THROW_SALOME_CORBA_EXCEPTION("ImportMEDFile(): unknown exception", SALOME::BAD_PARAM);
209   }
210
211   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
212   if ( !aStudy->_is_nil() ) {
213     // publishing of the groups in the study (sub-meshes are out of scope of MED import)
214     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
215     for (; it != _mapGroups.end(); it++ ) {
216       SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_duplicate( it->second );
217       _gen_i->PublishGroup( aStudy, _this(), aGroup,
218                            GEOM::GEOM_Object::_nil(), aGroup->GetName());
219     }
220   }
221   return ConvertDriverMEDReadStatus(status);
222 }
223
224 //=============================================================================
225 /*!
226  *  ImportUNVFile
227  *
228  *  Imports mesh data from MED file
229  */
230 //=============================================================================
231
232 int SMESH_Mesh_i::ImportUNVFile( const char* theFileName )
233   throw ( SALOME::SALOME_Exception )
234 {
235   // Read mesh with name = <theMeshName> into SMESH_Mesh
236   _impl->UNVToMesh( theFileName );
237
238   return 1;
239 }
240
241 //=============================================================================
242 /*!
243  *  ImportSTLFile
244  *
245  *  Imports mesh data from STL file
246  */
247 //=============================================================================
248 int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
249   throw ( SALOME::SALOME_Exception )
250 {
251   // Read mesh with name = <theMeshName> into SMESH_Mesh
252   _impl->STLToMesh( theFileName );
253
254   return 1;
255 }
256
257 //=============================================================================
258 /*!
259  *  importMEDFile
260  *
261  *  Imports mesh data from MED file
262  */
263 //=============================================================================
264
265 int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshName )
266 {
267   // Read mesh with name = <theMeshName> and all its groups into SMESH_Mesh
268   int status = _impl->MEDToMesh( theFileName, theMeshName );
269
270   // Create group servants, if any groups were imported
271   list<int> aGroupIds = _impl->GetGroupIds();
272   for ( list<int>::iterator it = aGroupIds.begin(); it != aGroupIds.end(); it++ ) {
273     SMESH_Group_i* aGroupImpl     = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, *it );
274
275     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
276     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
277     aGroupImpl->Register();
278     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
279
280     SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
281     _mapGroups[*it]               = SMESH::SMESH_Group::_duplicate( aGroup );
282
283     // register CORBA object for persistence
284     int nextId = _gen_i->RegisterObject( aGroup );
285     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
286   }
287
288   return status;
289 }
290
291 //=============================================================================
292 /*!
293  *
294  */
295 //=============================================================================
296
297 static SMESH::Hypothesis_Status ConvertHypothesisStatus
298                          (SMESH_Hypothesis::Hypothesis_Status theStatus)
299 {
300   SMESH::Hypothesis_Status res;
301   switch (theStatus)
302   {
303   case SMESH_Hypothesis::HYP_OK:
304     res = SMESH::HYP_OK; break;
305   case SMESH_Hypothesis::HYP_MISSING:
306     res = SMESH::HYP_MISSING; break;
307   case SMESH_Hypothesis::HYP_CONCURENT:
308     res = SMESH::HYP_CONCURENT; break;
309   case SMESH_Hypothesis::HYP_BAD_PARAMETER:
310     res = SMESH::HYP_BAD_PARAMETER; break;
311   case SMESH_Hypothesis::HYP_INCOMPATIBLE:
312     res = SMESH::HYP_INCOMPATIBLE; break;
313   case SMESH_Hypothesis::HYP_NOTCONFORM:
314     res = SMESH::HYP_NOTCONFORM; break;
315   case SMESH_Hypothesis::HYP_ALREADY_EXIST:
316     res = SMESH::HYP_ALREADY_EXIST; break;
317   case SMESH_Hypothesis::HYP_BAD_DIM:
318     res = SMESH::HYP_BAD_DIM; break;
319   default:
320     res = SMESH::HYP_UNKNOWN_FATAL;
321   }
322   return res;
323 }
324
325 //=============================================================================
326 /*!
327  *  AddHypothesis
328  *
329  *  calls internal addHypothesis() and then adds a reference to <anHyp> under
330  *  the SObject actually having a reference to <aSubShape>.
331  *  NB: For this method to work, it is necessary to add a reference to sub-shape first.
332  */
333 //=============================================================================
334
335 SMESH::Hypothesis_Status SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
336                                                      SMESH::SMESH_Hypothesis_ptr anHyp)
337   throw(SALOME::SALOME_Exception)
338 {
339   Unexpect aCatch(SALOME_SalomeException);
340   SMESH_Hypothesis::Hypothesis_Status status = addHypothesis( aSubShapeObject, anHyp );
341
342   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
343     _gen_i->AddHypothesisToShape(_gen_i->GetCurrentStudy(), _this(),
344                                  aSubShapeObject, anHyp );
345
346   if(MYDEBUG) MESSAGE( " AddHypothesis(): status = " << status );
347
348   // Update Python script
349   TPythonDump() << "status = " << _this() << ".AddHypothesis( "
350                 << aSubShapeObject << ", " << anHyp << " )";
351
352   return ConvertHypothesisStatus(status);
353 }
354
355 //=============================================================================
356 /*!
357  *
358  */
359 //=============================================================================
360
361 SMESH_Hypothesis::Hypothesis_Status
362   SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr       aSubShapeObject,
363                               SMESH::SMESH_Hypothesis_ptr anHyp)
364 {
365   if(MYDEBUG) MESSAGE("addHypothesis");
366
367   if (CORBA::is_nil(aSubShapeObject))
368     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
369                                  SALOME::BAD_PARAM);
370
371   SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
372   if (CORBA::is_nil(myHyp))
373     THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
374                                  SALOME::BAD_PARAM);
375
376   SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
377   try
378   {
379     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
380     int hypId = myHyp->GetId();
381     status = _impl->AddHypothesis(myLocSubShape, hypId);
382     if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
383       _mapHypo[hypId] = SMESH::SMESH_Hypothesis::_duplicate( myHyp );
384       // assure there is a corresponding submesh
385       if ( !_impl->IsMainShape( myLocSubShape )) {
386         int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
387         if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
388           createSubMesh( aSubShapeObject );
389       }
390     }
391   }
392   catch(SALOME_Exception & S_ex)
393   {
394     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
395   }
396   return status;
397 }
398
399 //=============================================================================
400 /*!
401  *
402  */
403 //=============================================================================
404
405 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
406                                                         SMESH::SMESH_Hypothesis_ptr anHyp)
407      throw(SALOME::SALOME_Exception)
408 {
409   Unexpect aCatch(SALOME_SalomeException);
410   SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
411
412   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
413     _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
414                                       aSubShapeObject, anHyp );
415
416   // Update Python script
417   TPythonDump() << "status = " << _this() << ".RemoveHypothesis( "
418                 << aSubShapeObject << ", " << anHyp << " )";
419
420   return ConvertHypothesisStatus(status);
421 }
422
423 //=============================================================================
424 /*!
425  *
426  */
427 //=============================================================================
428
429 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
430                                  SMESH::SMESH_Hypothesis_ptr anHyp)
431 {
432         if(MYDEBUG) MESSAGE("removeHypothesis()");
433         // **** proposer liste de subShape (selection multiple)
434
435         if (CORBA::is_nil(aSubShapeObject))
436                 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
437                         SALOME::BAD_PARAM);
438
439         SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
440         if (CORBA::is_nil(myHyp))
441           THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
442                         SALOME::BAD_PARAM);
443
444         SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
445         try
446         {
447                 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
448                 int hypId = myHyp->GetId();
449                 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
450                 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
451                   _mapHypo.erase( hypId );
452         }
453         catch(SALOME_Exception & S_ex)
454         {
455                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
456         }
457         return status;
458 }
459
460 //=============================================================================
461 /*!
462  *
463  */
464 //=============================================================================
465
466 SMESH::ListOfHypothesis *
467         SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
468 throw(SALOME::SALOME_Exception)
469 {
470   Unexpect aCatch(SALOME_SalomeException);
471   if (MYDEBUG) MESSAGE("GetHypothesisList");
472   if (CORBA::is_nil(aSubShapeObject))
473     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
474                                  SALOME::BAD_PARAM);
475
476   SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
477
478   try {
479     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
480     const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
481     int i = 0, n = aLocalList.size();
482     aList->length( n );
483
484     for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
485       SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
486       if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
487         aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
488     }
489
490     aList->length( i );
491   }
492   catch(SALOME_Exception & S_ex) {
493     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
494   }
495
496   return aList._retn();
497 }
498
499 //=============================================================================
500 /*!
501  *
502  */
503 //=============================================================================
504 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
505                                                   const char*           theName )
506      throw(SALOME::SALOME_Exception)
507 {
508   Unexpect aCatch(SALOME_SalomeException);
509   MESSAGE("SMESH_Mesh_i::GetSubMesh");
510   if (CORBA::is_nil(aSubShapeObject))
511     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
512                                  SALOME::BAD_PARAM);
513
514   SMESH::SMESH_subMesh_var subMesh;
515   SMESH::SMESH_Mesh_var    aMesh = SMESH::SMESH_Mesh::_narrow(_this());
516   try {
517     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
518
519     //Get or Create the SMESH_subMesh object implementation
520
521     int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
522     subMesh = getSubMesh( subMeshId );
523
524     // create a new subMesh object servant if there is none for the shape
525     if ( subMesh->_is_nil() )
526       subMesh = createSubMesh( aSubShapeObject );
527
528     if ( _gen_i->CanPublishInStudy( subMesh )) {
529       SALOMEDS::SObject_var aSO =
530         _gen_i->PublishSubMesh(_gen_i->GetCurrentStudy(), aMesh,
531                                subMesh, aSubShapeObject, theName );
532       if ( !aSO->_is_nil()) {
533         // Update Python script
534         TPythonDump() << aSO << " = " << _this() << ".GetSubMesh( "
535                       << aSubShapeObject << ", '" << theName << "' )";
536       }
537     }
538   }
539   catch(SALOME_Exception & S_ex) {
540     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
541   }
542   return subMesh._retn();
543 }
544
545 //=============================================================================
546 /*!
547  *
548  */
549 //=============================================================================
550
551 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
552      throw (SALOME::SALOME_Exception)
553 {
554   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
555   if ( theSubMesh->_is_nil() )
556     return;
557
558   GEOM::GEOM_Object_var aSubShapeObject;
559   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
560   if ( !aStudy->_is_nil() )  {
561     // Remove submesh's SObject
562     SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
563     if ( !anSO->_is_nil() ) {
564       long aTag = SMESH_Gen_i::GetRefOnShapeTag();
565       SALOMEDS::SObject_var anObj, aRef;
566       if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
567         aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
568
569       aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
570
571       // Update Python script
572       TPythonDump() << _this() << ".RemoveSubMesh( " << anSO << " )";
573     }
574   }
575
576   removeSubMesh( theSubMesh, aSubShapeObject.in() );
577 }
578
579 //=============================================================================
580 /*!
581  *  ElementTypeString
582  */
583 //=============================================================================
584 #define CASE2STRING(enum) case SMESH::enum: return "SMESH."#enum;
585 inline TCollection_AsciiString ElementTypeString (SMESH::ElementType theElemType)
586 {
587   switch (theElemType) {
588     CASE2STRING( ALL );
589     CASE2STRING( NODE );
590     CASE2STRING( EDGE );
591     CASE2STRING( FACE );
592     CASE2STRING( VOLUME );
593   default:;
594   }
595   return "";
596 }
597
598 //=============================================================================
599 /*!
600  *
601  */
602 //=============================================================================
603
604 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
605                                                  const char*         theName )
606      throw(SALOME::SALOME_Exception)
607 {
608   Unexpect aCatch(SALOME_SalomeException);
609   SMESH::SMESH_Group_var aNewGroup =
610     SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
611
612   if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
613     SALOMEDS::SObject_var aSO =
614       _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(),
615                            aNewGroup, GEOM::GEOM_Object::_nil(), theName);
616     if ( !aSO->_is_nil()) {
617       // Update Python script
618       TPythonDump() << aSO << " = " << _this() << ".CreateGroup( "
619                     << ElementTypeString(theElemType) << ", '" << theName << "' )";
620     }
621   }
622   return aNewGroup._retn();
623 }
624
625
626 //=============================================================================
627 /*!
628  *
629  */
630 //=============================================================================
631 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM (SMESH::ElementType    theElemType,
632                                                                 const char*           theName,
633                                                                 GEOM::GEOM_Object_ptr theGeomObj)
634      throw(SALOME::SALOME_Exception)
635 {
636   Unexpect aCatch(SALOME_SalomeException);
637   SMESH::SMESH_GroupOnGeom_var aNewGroup;
638
639   TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
640   if ( !aShape.IsNull() ) {
641     aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
642       ( createGroup( theElemType, theName, aShape ));
643     if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
644       SALOMEDS::SObject_var aSO =
645         _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(),
646                              aNewGroup, theGeomObj, theName);
647       if ( !aSO->_is_nil()) {
648         // Update Python script
649         TPythonDump() << aSO << " = " << _this() << ".CreateGroupFromGEOM("
650                       << ElementTypeString(theElemType) << ", '" << theName << "', "
651                       << theGeomObj << " )";
652       }
653     }
654   }
655
656   return aNewGroup._retn();
657 }
658
659 //=============================================================================
660 /*!
661  *
662  */
663 //=============================================================================
664
665 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
666      throw (SALOME::SALOME_Exception)
667 {
668   if ( theGroup->_is_nil() )
669     return;
670
671   SMESH_GroupBase_i* aGroup =
672     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
673   if ( !aGroup )
674     return;
675
676   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
677   if ( !aStudy->_is_nil() )  {
678     SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
679
680     if ( !aGroupSO->_is_nil() ) {
681       // Update Python script
682       TPythonDump() << _this() << ".RemoveGroup( " << aGroupSO << " )";
683
684       // Remove group's SObject
685       aStudy->NewBuilder()->RemoveObject( aGroupSO );
686     }
687   }
688
689   // Remove the group from SMESH data structures
690   removeGroup( aGroup->GetLocalID() );
691 }
692
693 //=============================================================================
694 /*! RemoveGroupWithContents
695  *  Remove group with its contents
696  */
697 //=============================================================================
698 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
699   throw (SALOME::SALOME_Exception)
700 {
701   if ( theGroup->_is_nil() )
702     return;
703
704   SMESH_GroupBase_i* aGroup =
705     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
706   if ( !aGroup )
707     return;
708
709   SMESH::long_array_var anIds = aGroup->GetListOfID();
710   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
711
712   // Update Python script
713   TPythonDump() << _this() << ".RemoveGroupWithContents( " << theGroup << " )";
714
715   // Remove contents
716   if ( aGroup->GetType() == SMESH::NODE )
717     aMeshEditor->RemoveNodes( anIds );
718   else
719     aMeshEditor->RemoveElements( anIds );
720
721   // Remove group
722   RemoveGroup( theGroup );
723
724   // Clear python lines, created by RemoveNodes/Elements() and RemoveGroup()
725   _gen_i->RemoveLastFromPythonScript(_gen_i->GetCurrentStudy()->StudyId());
726   _gen_i->RemoveLastFromPythonScript(_gen_i->GetCurrentStudy()->StudyId());
727 }
728
729 //=============================================================================
730 /*! UnionGroups
731  *  New group is created. All mesh elements that are
732  *  present in initial groups are added to the new one
733  */
734 //=============================================================================
735 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
736                                                   SMESH::SMESH_GroupBase_ptr theGroup2,
737                                                   const char* theName )
738   throw (SALOME::SALOME_Exception)
739 {
740   try
741   {
742     if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
743          theGroup1->GetType() != theGroup2->GetType() )
744       return SMESH::SMESH_Group::_nil();
745
746     // Create Union
747     SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
748     if ( aResGrp->_is_nil() )
749       return SMESH::SMESH_Group::_nil();
750
751     SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
752     SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
753
754     TColStd_MapOfInteger aResMap;
755
756     for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
757       aResMap.Add( anIds1[ i1 ] );
758
759     for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
760       aResMap.Add( anIds2[ i2 ] );
761
762     SMESH::long_array_var aResIds = new SMESH::long_array;
763     aResIds->length( aResMap.Extent() );
764
765     int resI = 0;
766     TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
767     for( ; anIter.More(); anIter.Next() )
768       aResIds[ resI++ ] = anIter.Key();
769
770     aResGrp->Add( aResIds );
771
772     // Clear python lines, created by CreateGroup() and Add()
773     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
774     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
775     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
776
777     // Update Python script
778     TPythonDump() << aResGrp << " = " << _this() << ".UnionGroups( "
779                   << theGroup1 << ", " << theGroup2 << ", '"
780                   << theName << "' )";
781
782     return aResGrp._retn();
783   }
784   catch( ... )
785   {
786     return SMESH::SMESH_Group::_nil();
787   }
788 }
789
790 //=============================================================================
791 /*! IntersectGroups
792  *  New group is created. All mesh elements that are
793  *  present in both initial groups are added to the new one.
794  */
795 //=============================================================================
796 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
797                                                       SMESH::SMESH_GroupBase_ptr theGroup2,
798                                                       const char* theName )
799   throw (SALOME::SALOME_Exception)
800 {
801   if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
802        theGroup1->GetType() != theGroup2->GetType() )
803     return SMESH::SMESH_Group::_nil();
804
805   // Create Intersection
806   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
807   if ( aResGrp->_is_nil() )
808     return aResGrp;
809
810   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
811   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
812
813   TColStd_MapOfInteger aMap1;
814
815   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
816     aMap1.Add( anIds1[ i1 ] );
817
818   TColStd_SequenceOfInteger aSeq;
819
820   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
821     if ( aMap1.Contains( anIds2[ i2 ] ) )
822       aSeq.Append( anIds2[ i2 ] );
823
824   SMESH::long_array_var aResIds = new SMESH::long_array;
825   aResIds->length( aSeq.Length() );
826
827   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
828     aResIds[ resI ] = aSeq( resI + 1 );
829
830   aResGrp->Add( aResIds );
831
832   // Clear python lines, created by CreateGroup() and Add()
833   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
834   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
835   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
836
837   // Update Python script
838   TPythonDump() << aResGrp << " = " << _this() << ".IntersectGroups( "
839                 << theGroup1 << ", " << theGroup2 << ", '" << theName << "')";
840
841   return aResGrp._retn();
842 }
843
844 //=============================================================================
845 /*! CutGroups
846  *  New group is created. All mesh elements that are present in
847  *  main group but do not present in tool group are added to the new one
848  */
849 //=============================================================================
850 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
851                                                 SMESH::SMESH_GroupBase_ptr theGroup2,
852                                                 const char* theName )
853   throw (SALOME::SALOME_Exception)
854 {
855   if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
856        theGroup1->GetType() != theGroup2->GetType() )
857     return SMESH::SMESH_Group::_nil();
858
859   // Perform Cutting
860   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
861   if ( aResGrp->_is_nil() )
862     return aResGrp;
863
864   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
865   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
866
867   TColStd_MapOfInteger aMap2;
868
869   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
870     aMap2.Add( anIds2[ i2 ] );
871
872
873   TColStd_SequenceOfInteger aSeq;
874   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
875     if ( !aMap2.Contains( anIds1[ i1 ] ) )
876       aSeq.Append( anIds1[ i1 ] );
877
878   SMESH::long_array_var aResIds = new SMESH::long_array;
879   aResIds->length( aSeq.Length() );
880
881   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
882     aResIds[ resI ] = aSeq( resI + 1 );
883
884   aResGrp->Add( aResIds );
885
886   // Clear python lines, created by CreateGroup() and Add()
887   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
888   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
889   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
890
891   // Update Python script
892   TPythonDump() << aResGrp << " = " << _this() << ".CutGroups( "
893                 << theGroup1 << ", " << theGroup2 << ", '"
894                 << theName << "' )";
895
896   return aResGrp._retn();
897 }
898
899 //=============================================================================
900 /*!
901  *
902  */
903 //=============================================================================
904
905 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
906 {
907   if(MYDEBUG) MESSAGE( "createSubMesh" );
908   TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
909
910   ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
911   int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
912   SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
913   SMESH::SMESH_subMesh_var subMesh
914     = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
915
916   _mapSubMesh[subMeshId] = mySubMesh;
917   _mapSubMesh_i[subMeshId] = subMeshServant;
918   _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
919
920   // register CORBA object for persistence
921   int nextId = _gen_i->RegisterObject( subMesh );
922   if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
923
924   return subMesh._retn();
925 }
926
927 //=======================================================================
928 //function : getSubMesh
929 //purpose  :
930 //=======================================================================
931
932 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
933 {
934   map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
935   if ( it == _mapSubMeshIor.end() )
936     return SMESH::SMESH_subMesh::_nil();
937
938   return SMESH::SMESH_subMesh::_duplicate( (*it).second );
939 }
940
941
942 //=============================================================================
943 /*!
944  *
945  */
946 //=============================================================================
947
948 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
949                                   GEOM::GEOM_Object_ptr    theSubShapeObject )
950 {
951   MESSAGE("SMESH_Mesh_i::removeSubMesh()");
952   if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
953     return;
954
955   try {
956     SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
957     for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
958       removeHypothesis( theSubShapeObject, aHypList[i] );
959     }
960   }
961   catch( const SALOME::SALOME_Exception& ) {
962     INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
963   }
964
965   int subMeshId = theSubMesh->GetId();
966
967   _mapSubMesh.erase(subMeshId);
968   _mapSubMesh_i.erase(subMeshId);
969   _mapSubMeshIor.erase(subMeshId);
970   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
971 }
972
973 //=============================================================================
974 /*!
975  *
976  */
977 //=============================================================================
978
979 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
980                                                       const char*         theName,
981                                                       const TopoDS_Shape& theShape )
982 {
983   int anId;
984   SMESH::SMESH_GroupBase_var aGroup;
985   if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
986     SMESH_GroupBase_i* aGroupImpl;
987     if ( !theShape.IsNull() )
988       aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
989     else
990       aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
991
992     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
993     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
994     aGroupImpl->Register();
995     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
996
997     aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
998     _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
999
1000     // register CORBA object for persistence
1001     int nextId = _gen_i->RegisterObject( aGroup );
1002     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
1003   }
1004   return aGroup._retn();
1005 }
1006
1007 //=============================================================================
1008 /*!
1009  * SMESH_Mesh_i::removeGroup
1010  *
1011  * Should be called by ~SMESH_Group_i()
1012  */
1013 //=============================================================================
1014
1015 void SMESH_Mesh_i::removeGroup( const int theId )
1016 {
1017   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );
1018   if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
1019     _mapGroups.erase( theId );
1020     _impl->RemoveGroup( theId );
1021   }
1022 }
1023
1024
1025 //=============================================================================
1026 /*!
1027  *
1028  */
1029 //=============================================================================
1030
1031 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
1032 throw(SALOME::SALOME_Exception)
1033 {
1034   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
1035
1036   SMESH::log_array_var aLog;
1037   try{
1038     list < SMESHDS_Command * >logDS = _impl->GetLog();
1039     aLog = new SMESH::log_array;
1040     int indexLog = 0;
1041     int lg = logDS.size();
1042     SCRUTE(lg);
1043     aLog->length(lg);
1044     list < SMESHDS_Command * >::iterator its = logDS.begin();
1045     while(its != logDS.end()){
1046       SMESHDS_Command *com = *its;
1047       int comType = com->GetType();
1048       //SCRUTE(comType);
1049       int lgcom = com->GetNumber();
1050       //SCRUTE(lgcom);
1051       const list < int >&intList = com->GetIndexes();
1052       int inum = intList.size();
1053       //SCRUTE(inum);
1054       list < int >::const_iterator ii = intList.begin();
1055       const list < double >&coordList = com->GetCoords();
1056       int rnum = coordList.size();
1057       //SCRUTE(rnum);
1058       list < double >::const_iterator ir = coordList.begin();
1059       aLog[indexLog].commandType = comType;
1060       aLog[indexLog].number = lgcom;
1061       aLog[indexLog].coords.length(rnum);
1062       aLog[indexLog].indexes.length(inum);
1063       for(int i = 0; i < rnum; i++){
1064         aLog[indexLog].coords[i] = *ir;
1065         //MESSAGE(" "<<i<<" "<<ir.Value());
1066         ir++;
1067       }
1068       for(int i = 0; i < inum; i++){
1069         aLog[indexLog].indexes[i] = *ii;
1070         //MESSAGE(" "<<i<<" "<<ii.Value());
1071         ii++;
1072       }
1073       indexLog++;
1074       its++;
1075     }
1076     if(clearAfterGet)
1077       _impl->ClearLog();
1078   }
1079   catch(SALOME_Exception & S_ex){
1080     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1081   }
1082   return aLog._retn();
1083 }
1084
1085
1086 //=============================================================================
1087 /*!
1088  *
1089  */
1090 //=============================================================================
1091
1092 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
1093 {
1094   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
1095   // ****
1096 }
1097
1098 //=============================================================================
1099 /*!
1100  *
1101  */
1102 //=============================================================================
1103
1104 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1105 {
1106   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1107   return _id;
1108 }
1109
1110 //=============================================================================
1111 /*!
1112  *
1113  */
1114 //=============================================================================
1115
1116 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1117 {
1118   return _studyId;
1119 }
1120
1121 //=============================================================================
1122 /*!
1123  *
1124  */
1125 //=============================================================================
1126
1127 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1128 {
1129   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1130   _impl = impl;
1131 }
1132
1133 //=============================================================================
1134 /*!
1135  *
1136  */
1137 //=============================================================================
1138
1139 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1140 {
1141   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1142   return *_impl;
1143 }
1144
1145
1146 //=============================================================================
1147 /*!
1148  *
1149  */
1150 //=============================================================================
1151
1152 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1153 {
1154   // Create MeshEditor
1155   SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl );
1156   SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1157
1158   // Update Python script
1159   TPythonDump() << aMeshEditor << " = " << _this() << ".GetMeshEditor()";
1160
1161   return aMesh._retn();
1162 }
1163
1164 //=============================================================================
1165 /*!
1166  *  Export in different formats
1167  */
1168 //=============================================================================
1169
1170 static void PrepareForWriting (const char* file)
1171 {
1172   TCollection_AsciiString aFullName ((char*)file);
1173   OSD_Path aPath (aFullName);
1174   OSD_File aFile (aPath);
1175   if (aFile.Exists()) {
1176     // existing filesystem node
1177     if (aFile.KindOfFile() == OSD_FILE) {
1178       if (aFile.IsWriteable()) {
1179         aFile.Reset();
1180         aFile.Remove();
1181         if (aFile.Failed()) {
1182           TCollection_AsciiString msg ("File ");
1183           msg += aFullName + " cannot be replaced.";
1184           THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1185         }
1186       } else {
1187         TCollection_AsciiString msg ("File ");
1188         msg += aFullName + " cannot be overwritten.";
1189         THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1190       }
1191     } else {
1192       TCollection_AsciiString msg ("Location ");
1193       msg += aFullName + " is not a file.";
1194       THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1195     }
1196   } else {
1197     // nonexisting file; check if it can be created
1198     aFile.Reset();
1199     aFile.Build(OSD_WriteOnly, OSD_Protection());
1200     if (aFile.Failed()) {
1201       TCollection_AsciiString msg ("You cannot create the file ");
1202       msg += aFullName + ". Check the directory existance and access rights.";
1203       THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1204     } else {
1205       aFile.Close();
1206       aFile.Remove();
1207     }
1208   }
1209 }
1210
1211 void SMESH_Mesh_i::ExportToMED (const char* file,
1212                                 CORBA::Boolean auto_groups,
1213                                 SMESH::MED_VERSION theVersion)
1214   throw(SALOME::SALOME_Exception)
1215 {
1216   Unexpect aCatch(SALOME_SalomeException);
1217
1218   // Update Python script
1219   TPythonDump() << _this() << ".ExportToMED( '"
1220                 << file << "', " << auto_groups << ", " << theVersion << " )";
1221
1222   // Perform Export
1223   PrepareForWriting(file);
1224   char* aMeshName = "Mesh";
1225   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1226   if ( !aStudy->_is_nil() ) {
1227     SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
1228     if ( !aMeshSO->_is_nil() ) {
1229       aMeshName = aMeshSO->GetName();
1230       //SCRUTE(file);
1231       //SCRUTE(aMeshName);
1232       //SCRUTE(aMeshSO->GetID());
1233
1234       // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes
1235       if ( !aStudy->GetProperties()->IsLocked() )
1236         {
1237         SALOMEDS::GenericAttribute_var anAttr;
1238         SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1239         SALOMEDS::AttributeExternalFileDef_var aFileName;
1240         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1241         aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1242         ASSERT(!aFileName->_is_nil());
1243         aFileName->SetValue(file);
1244         SALOMEDS::AttributeFileType_var aFileType;
1245         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1246         aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1247         ASSERT(!aFileType->_is_nil());
1248         aFileType->SetValue("FICHIERMED");
1249         }
1250     }
1251   }
1252   _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
1253 }
1254
1255 void SMESH_Mesh_i::ExportMED (const char* file,
1256                               CORBA::Boolean auto_groups)
1257   throw(SALOME::SALOME_Exception)
1258 {
1259   ExportToMED(file,auto_groups,SMESH::MED_V2_1);
1260 }
1261
1262 void SMESH_Mesh_i::ExportDAT (const char *file)
1263   throw(SALOME::SALOME_Exception)
1264 {
1265   Unexpect aCatch(SALOME_SalomeException);
1266
1267   // Update Python script
1268   TPythonDump() << _this() << ".ExportDAT( '" << file << "' )";
1269
1270   // Perform Export
1271   PrepareForWriting(file);
1272   _impl->ExportDAT(file);
1273 }
1274
1275 void SMESH_Mesh_i::ExportUNV (const char *file)
1276   throw(SALOME::SALOME_Exception)
1277 {
1278   Unexpect aCatch(SALOME_SalomeException);
1279
1280   // Update Python script
1281   TPythonDump() << _this() << ".ExportUNV( '" << file << "' )";
1282
1283   // Perform Export
1284   PrepareForWriting(file);
1285   _impl->ExportUNV(file);
1286 }
1287
1288 void SMESH_Mesh_i::ExportSTL (const char *file, const bool isascii)
1289   throw(SALOME::SALOME_Exception)
1290 {
1291   Unexpect aCatch(SALOME_SalomeException);
1292
1293   // Update Python script
1294   TPythonDump() << _this() << ".ExportSTL( '" << file << "', " << isascii << " )";
1295
1296   // Perform Export
1297   PrepareForWriting(file);
1298   _impl->ExportSTL(file, isascii);
1299 }
1300
1301 //=============================================================================
1302 /*!
1303  *
1304  */
1305 //=============================================================================
1306
1307 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1308 {
1309   Unexpect aCatch(SALOME_SalomeException);
1310   SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1311   SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1312   return aMesh._retn();
1313 }
1314
1315 //=============================================================================
1316 /*!
1317  *
1318  */
1319 //=============================================================================
1320 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1321 {
1322   Unexpect aCatch(SALOME_SalomeException);
1323   return _impl->NbNodes();
1324 }
1325
1326 //=============================================================================
1327 /*!
1328  *
1329  */
1330 //=============================================================================
1331 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
1332 {
1333   Unexpect aCatch(SALOME_SalomeException);
1334   return NbEdges() + NbFaces() + NbVolumes();
1335 }
1336
1337 //=============================================================================
1338 /*!
1339  *
1340  */
1341 //=============================================================================
1342 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1343 {
1344   Unexpect aCatch(SALOME_SalomeException);
1345   return _impl->NbEdges();
1346 }
1347
1348 //=============================================================================
1349 /*!
1350  *
1351  */
1352 //=============================================================================
1353 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1354 {
1355   Unexpect aCatch(SALOME_SalomeException);
1356   return _impl->NbFaces();
1357 }
1358
1359 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1360 {
1361   Unexpect aCatch(SALOME_SalomeException);
1362   return _impl->NbTriangles();
1363 }
1364
1365 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1366 {
1367   Unexpect aCatch(SALOME_SalomeException);
1368   return _impl->NbQuadrangles();
1369 }
1370
1371 CORBA::Long SMESH_Mesh_i::NbPolygons()throw(SALOME::SALOME_Exception)
1372 {
1373   Unexpect aCatch(SALOME_SalomeException);
1374   return _impl->NbPolygons();
1375 }
1376
1377 //=============================================================================
1378 /*!
1379  *
1380  */
1381 //=============================================================================
1382 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1383 {
1384   Unexpect aCatch(SALOME_SalomeException);
1385   return _impl->NbVolumes();
1386 }
1387
1388 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1389 {
1390   Unexpect aCatch(SALOME_SalomeException);
1391   return _impl->NbTetras();
1392 }
1393
1394 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1395 {
1396   Unexpect aCatch(SALOME_SalomeException);
1397   return _impl->NbHexas();
1398 }
1399
1400 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1401 {
1402   Unexpect aCatch(SALOME_SalomeException);
1403   return _impl->NbPyramids();
1404 }
1405
1406 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1407 {
1408   Unexpect aCatch(SALOME_SalomeException);
1409   return _impl->NbPrisms();
1410 }
1411
1412 CORBA::Long SMESH_Mesh_i::NbPolyhedrons()throw(SALOME::SALOME_Exception)
1413 {
1414   Unexpect aCatch(SALOME_SalomeException);
1415   return _impl->NbPolyhedrons();
1416 }
1417
1418 //=============================================================================
1419 /*!
1420  *
1421  */
1422 //=============================================================================
1423 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1424 {
1425   Unexpect aCatch(SALOME_SalomeException);
1426   return _impl->NbSubMesh();
1427 }
1428
1429 //=============================================================================
1430 /*!
1431  *
1432  */
1433 //=============================================================================
1434 char* SMESH_Mesh_i::Dump()
1435 {
1436   std::ostringstream os;
1437   _impl->Dump( os );
1438   return CORBA::string_dup( os.str().c_str() );
1439 }
1440
1441 //=============================================================================
1442 /*!
1443  *
1444  */
1445 //=============================================================================
1446 SMESH::long_array* SMESH_Mesh_i::GetIDs()
1447 {
1448   SMESH::long_array_var aResult = new SMESH::long_array();
1449   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1450   int aMinId = aSMESHDS_Mesh->MinElementID();
1451   int aMaxId =  aSMESHDS_Mesh->MaxElementID();
1452
1453   aResult->length(aMaxId - aMinId + 1);
1454
1455   for (int i = 0, id = aMinId; id <= aMaxId; id++  )
1456     aResult[i++] = id;
1457
1458   return aResult._retn();
1459 }
1460
1461 //=============================================================================
1462 /*!
1463  *
1464  */
1465 //=============================================================================
1466
1467 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
1468      throw (SALOME::SALOME_Exception)
1469 {
1470   Unexpect aCatch(SALOME_SalomeException);
1471   MESSAGE("SMESH_Mesh_i::GetElementsId");
1472   SMESH::long_array_var aResult = new SMESH::long_array();
1473   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1474
1475   if ( aSMESHDS_Mesh == NULL )
1476     return aResult._retn();
1477
1478   long nbElements = NbElements();
1479   aResult->length( nbElements );
1480   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1481   for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
1482     aResult[i] = anIt->next()->GetID();
1483
1484   return aResult._retn();
1485 }
1486
1487
1488 //=============================================================================
1489 /*!
1490  *
1491  */
1492 //=============================================================================
1493
1494 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
1495     throw (SALOME::SALOME_Exception)
1496 {
1497   Unexpect aCatch(SALOME_SalomeException);
1498   MESSAGE("SMESH_subMesh_i::GetElementsByType");
1499   SMESH::long_array_var aResult = new SMESH::long_array();
1500   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1501
1502   if ( aSMESHDS_Mesh == NULL )
1503     return aResult._retn();
1504
1505   long nbElements = NbElements();
1506
1507   // No sense in returning ids of elements along with ids of nodes:
1508   // when theElemType == SMESH::ALL, return node ids only if
1509   // there are no elements
1510   if ( theElemType == SMESH::NODE || theElemType == SMESH::ALL && nbElements == 0 )
1511     return GetNodesId();
1512
1513   aResult->length( nbElements );
1514
1515   int i = 0;
1516
1517   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1518   while ( i < nbElements && anIt->more() ) {
1519     const SMDS_MeshElement* anElem = anIt->next();
1520     if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
1521       aResult[i++] = anElem->GetID();
1522   }
1523
1524   aResult->length( i );
1525
1526   return aResult._retn();
1527 }
1528
1529 //=============================================================================
1530 /*!
1531  *
1532  */
1533 //=============================================================================
1534
1535 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
1536   throw (SALOME::SALOME_Exception)
1537 {
1538   Unexpect aCatch(SALOME_SalomeException);
1539   MESSAGE("SMESH_subMesh_i::GetNodesId");
1540   SMESH::long_array_var aResult = new SMESH::long_array();
1541   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1542
1543   if ( aSMESHDS_Mesh == NULL )
1544     return aResult._retn();
1545
1546   long nbNodes = NbNodes();
1547   aResult->length( nbNodes );
1548   SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator();
1549   for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
1550     aResult[i] = anIt->next()->GetID();
1551
1552   return aResult._retn();
1553 }
1554
1555 //=============================================================================
1556 /*!
1557  *
1558  */
1559 //=============================================================================
1560
1561 SMESH::ElementType SMESH_Mesh_i::GetElementType( const CORBA::Long id, const bool iselem )
1562   throw (SALOME::SALOME_Exception)
1563 {
1564   return ( SMESH::ElementType )_impl->GetElementType( id, iselem );
1565 }