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