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