Salome HOME
Integration of PAL/SALOME V2.1.0c from OCC
[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 <TColStd_MapOfInteger.hxx>
45 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
46 #include <TColStd_SequenceOfInteger.hxx>
47 #include "SMESHDS_Command.hxx"
48 #include "SMESHDS_CommandType.hxx"
49 #include "SMESH_MeshEditor_i.hxx"
50 #include "SMESH_Gen_i.hxx"
51 #include "DriverMED_R_SMESHDS_Mesh.h"
52
53 #include <string>
54 #include <iostream>
55 // _CS_gbo_050504 Ajout explicite du sstream pour ostringstream 
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_Group_ptr>::iterator it;
96   for ( it = _mapGroups.begin(); it != _mapGroups.end(); it++ ) {
97     SMESH_Group_i* aGroup = dynamic_cast<SMESH_Group_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     setShape( theShapeObject );
125   }
126   catch(SALOME_Exception & S_ex) {
127     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
128   }  
129
130   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
131   if ( aStudy->_is_nil() ) 
132     return;
133
134   // Create a reference to <theShape> 
135   SALOMEDS::SObject_var aMeshSO  = SALOMEDS::SObject::_narrow( aStudy->FindObjectIOR( ( SMESH_Gen_i::GetORB()->object_to_string( _this() ) ) ) );
136   SALOMEDS::SObject_var aShapeSO = aStudy->FindObjectIOR( SMESH_Gen_i::GetORB()->object_to_string( theShapeObject ) );
137   
138   SALOMEDS::SObject_var          anObj, aRef;
139   SALOMEDS::GenericAttribute_var anAttr;
140   SALOMEDS::AttributeIOR_var     anIOR;
141   SALOMEDS::StudyBuilder_var     aBuilder = aStudy->NewBuilder();
142   long                           aTag = SMESH_Gen_i::GetRefOnShapeTag();      
143   
144   if ( aMeshSO->FindSubObject( aTag, anObj ) ) {
145     if ( anObj->ReferencedObject( aRef ) ) {
146       if ( strcmp( aRef->GetID(), aShapeSO->GetID() ) == 0 ) {
147         // Setting the same shape twice forbidden
148         return;
149       }
150     }
151   }
152   else {
153     anObj = aBuilder->NewObjectToTag( aMeshSO, aTag );
154   }
155   aBuilder->Addreference( anObj, aShapeSO );
156 }
157
158 //=============================================================================
159 /*!
160  *  setShape
161  *
162  *  Sets shape to the mesh implementation
163  */
164 //=============================================================================
165
166 bool SMESH_Mesh_i::setShape( GEOM::GEOM_Object_ptr theShapeObject )
167 {
168   if ( theShapeObject->_is_nil() )
169     return false;
170
171   TopoDS_Shape aLocShape  = _gen_i->GetShapeReader()->GetShape( SMESH_Gen_i::GetGeomEngine(), theShapeObject );
172   _impl->ShapeToMesh( aLocShape );
173   return true;
174 }
175
176 //=============================================================================
177 /*!
178  *  
179  */
180 //=============================================================================
181
182 static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
183 {
184   SMESH::DriverMED_ReadStatus res;
185   switch (theStatus)
186   {
187   case DriverMED_R_SMESHDS_Mesh::DRS_OK:
188     res = SMESH::DRS_OK; break;
189   case DriverMED_R_SMESHDS_Mesh::DRS_EMPTY:
190     res = SMESH::DRS_EMPTY; break;
191   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_RENUMBER:
192     res = SMESH::DRS_WARN_RENUMBER; break;
193   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_SKIP_ELEM:
194     res = SMESH::DRS_WARN_SKIP_ELEM; break;
195   case DriverMED_R_SMESHDS_Mesh::DRS_FAIL:
196   default:
197     res = SMESH::DRS_FAIL; break;
198   }
199   return res;
200 }
201
202 //=============================================================================
203 /*!
204  *  ImportMEDFile
205  *
206  *  Imports mesh data from MED file
207  */
208 //=============================================================================
209
210 SMESH::DriverMED_ReadStatus
211 SMESH_Mesh_i::ImportMEDFile( const char* theFileName, const char* theMeshName )
212   throw ( SALOME::SALOME_Exception )
213 {
214   Unexpect aCatch(SALOME_SalomeException);
215   int status;
216   try {
217     status = importMEDFile( theFileName, theMeshName );
218   }
219   catch( SALOME_Exception& S_ex ) {
220     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
221   }  
222   catch ( ... ) {
223     THROW_SALOME_CORBA_EXCEPTION("ImportMEDFile(): unknown exception", SALOME::BAD_PARAM);
224   }
225
226   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
227   if ( aStudy->_is_nil() ) 
228     return ConvertDriverMEDReadStatus(status);
229   
230   // publishing of the groups in the study (sub-meshes are out of scope of MED import)
231   map<int, SMESH::SMESH_Group_ptr>::iterator it = _mapGroups.begin();
232   for (; it != _mapGroups.end(); it++ ) {
233     SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_duplicate( it->second );
234     if ( _gen_i->CanPublishInStudy( aGroup ) )
235       _gen_i->PublishInStudy( aStudy, 
236                               SALOMEDS::SObject::_nil(),
237                               aGroup,
238                               aGroup->GetName() );
239   }
240   return ConvertDriverMEDReadStatus(status);
241 }
242
243 //=============================================================================
244 /*!
245  *  ImportUNVFile
246  *
247  *  Imports mesh data from MED file
248  */
249 //=============================================================================
250
251 int SMESH_Mesh_i::ImportUNVFile( const char* theFileName )
252   throw ( SALOME::SALOME_Exception )
253 {
254   // Read mesh with name = <theMeshName> into SMESH_Mesh
255   _impl->UNVToMesh( theFileName );
256
257   return 1;
258 }
259
260 //=============================================================================
261 /*!
262  *  ImportSTLFile
263  *
264  *  Imports mesh data from STL file
265  */
266 //=============================================================================
267 int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
268   throw ( SALOME::SALOME_Exception )
269 {
270   // Read mesh with name = <theMeshName> into SMESH_Mesh
271   _impl->STLToMesh( theFileName );
272
273   return 1;
274 }
275
276 //=============================================================================
277 /*!
278  *  importMEDFile
279  *
280  *  Imports mesh data from MED file
281  */
282 //=============================================================================
283
284 int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshName )
285 {
286   // Read mesh with name = <theMeshName> and all its groups into SMESH_Mesh
287   int status = _impl->MEDToMesh( theFileName, theMeshName );
288
289   // Create group servants, if any groups were imported
290   list<int> aGroupIds = _impl->GetGroupIds();
291   for ( list<int>::iterator it = aGroupIds.begin(); it != aGroupIds.end(); it++ ) {
292     SMESH_Group_i* aGroupImpl     = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, *it );
293     SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
294     _mapGroups[*it]               = SMESH::SMESH_Group::_duplicate( aGroup );
295
296     // register CORBA object for persistence
297     StudyContext* myStudyContext = _gen_i->GetCurrentStudyContext();
298     string iorString = SMESH_Gen_i::GetORB()->object_to_string( aGroup );
299     int nextId = myStudyContext->addObject( iorString );
300     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId << " and IOR = " << iorString.c_str() );
301   }
302
303   return status;
304 }
305
306 //=============================================================================
307 /*!
308  *  
309  */
310 //=============================================================================
311
312 static SMESH::Hypothesis_Status ConvertHypothesisStatus
313                          (SMESH_Hypothesis::Hypothesis_Status theStatus)
314 {
315   SMESH::Hypothesis_Status res;
316   switch (theStatus)
317   {
318   case SMESH_Hypothesis::HYP_OK:
319     res = SMESH::HYP_OK; break;
320   case SMESH_Hypothesis::HYP_MISSING:
321     res = SMESH::HYP_MISSING; break;
322   case SMESH_Hypothesis::HYP_CONCURENT:
323     res = SMESH::HYP_CONCURENT; break;
324   case SMESH_Hypothesis::HYP_BAD_PARAMETER:
325     res = SMESH::HYP_BAD_PARAMETER; break;
326   case SMESH_Hypothesis::HYP_INCOMPATIBLE:
327     res = SMESH::HYP_INCOMPATIBLE; break;
328   case SMESH_Hypothesis::HYP_NOTCONFORM:
329     res = SMESH::HYP_NOTCONFORM; break;
330   case SMESH_Hypothesis::HYP_ALREADY_EXIST:
331     res = SMESH::HYP_ALREADY_EXIST; break;
332   case SMESH_Hypothesis::HYP_BAD_DIM:
333     res = SMESH::HYP_BAD_DIM; break;
334   default:
335     res = SMESH::HYP_UNKNOWN_FATAL;
336   }
337   return res;
338 }
339
340 //=============================================================================
341 /*!
342  *  AddHypothesis
343  *
344  *  calls internal addHypothesis() and then adds a reference to <anHyp> under 
345  *  the SObject actually having a reference to <aSubShape>.
346  *  NB: For this method to work, it is necessary to add a reference to sub-shape first.
347  */
348 //=============================================================================
349
350 SMESH::Hypothesis_Status SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
351                                                      SMESH::SMESH_Hypothesis_ptr anHyp)
352   throw(SALOME::SALOME_Exception)
353 {
354   Unexpect aCatch(SALOME_SalomeException);
355   SMESH_Hypothesis::Hypothesis_Status status = addHypothesis( aSubShapeObject, anHyp );
356
357   if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
358     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
359
360     if ( !aStudy->_is_nil() ) {
361       // Detect whether <aSubShape> refers to this mesh or its sub-mesh
362       SALOMEDS::GenericAttribute_var anAttr;
363       SALOMEDS::AttributeIOR_var     anIOR;
364       SALOMEDS::SObject_var aMeshSO  = SALOMEDS::SObject::_narrow( aStudy->FindObjectIOR( ( SMESH_Gen_i::GetORB()->object_to_string( _this() ) ) ) );
365       if ( aMeshSO->_is_nil() ) {
366         SCRUTE( SMESH_Gen_i::GetORB()->object_to_string( _this() ));
367         removeHypothesis( aSubShapeObject, anHyp );
368         return SMESH::HYP_UNKNOWN_FATAL;
369       }
370       SALOMEDS::SObject_var aMorSM, aRef;
371       CORBA::String_var aShapeIOR    = CORBA::string_dup( SMESH_Gen_i::GetORB()->object_to_string( aSubShapeObject ) );
372       SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator( aMeshSO );
373
374       for ( it->InitEx( true ); it->More(); it->Next() ) {
375         SALOMEDS::SObject_var anObj = it->Value();
376         if ( anObj->ReferencedObject( aRef ) ) {
377           if ( aRef->FindAttribute( anAttr, "AttributeIOR" ) ) {
378             anIOR = SALOMEDS::AttributeIOR::_narrow( anAttr );
379             if ( strcmp( anIOR->Value(), aShapeIOR ) == 0 ) {
380               aMorSM = anObj->GetFather();
381               break;
382             }
383           }
384         }
385       }
386
387       bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( anHyp )->_is_nil();
388       SALOMEDS::SObject_var aHypSO  = SALOMEDS::SObject::_narrow( aStudy->FindObjectIOR( ( SMESH_Gen_i::GetORB()->object_to_string( anHyp   ) ) ) );
389       if ( !aMorSM->_is_nil() && !aHypSO->_is_nil() ) {
390         //Find or Create Applied Hypothesis root
391         SALOMEDS::SObject_var             AHR;
392         SALOMEDS::AttributeName_var       aName;
393         SALOMEDS::AttributeSelectable_var aSelAttr;
394         SALOMEDS::AttributePixMap_var     aPixmap;
395         SALOMEDS::StudyBuilder_var        aBuilder = aStudy->NewBuilder();
396         long                              aTag = aIsAlgo ? SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag() : SMESH_Gen_i::GetRefOnAppliedHypothesisTag();
397
398         if ( !aMorSM->FindSubObject( aTag, AHR ) ) {
399           AHR      = aBuilder->NewObjectToTag( aMorSM, aTag );
400           anAttr   = aBuilder->FindOrCreateAttribute( AHR, "AttributeName" );
401           aName    = SALOMEDS::AttributeName::_narrow( anAttr );
402           aName    ->SetValue( aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
403           anAttr   = aBuilder->FindOrCreateAttribute( AHR, "AttributeSelectable" );
404           aSelAttr = SALOMEDS::AttributeSelectable::_narrow( anAttr );
405           aSelAttr ->SetSelectable( false );
406           anAttr   = aBuilder->FindOrCreateAttribute( AHR, "AttributePixMap" );
407           aPixmap  = SALOMEDS::AttributePixMap::_narrow( anAttr );
408           aPixmap  ->SetPixMap( aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO" );
409         }
410
411         SALOMEDS::SObject_var SO = aBuilder->NewObject( AHR );
412         aBuilder->Addreference( SO, aHypSO );
413       }
414     }
415   }
416   if(MYDEBUG) MESSAGE( " AddHypothesis(): status = " << status );
417
418   return ConvertHypothesisStatus(status);
419 }
420
421 //=============================================================================
422 /*!
423  *  
424  */
425 //=============================================================================
426
427 SMESH_Hypothesis::Hypothesis_Status
428   SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
429                               SMESH::SMESH_Hypothesis_ptr anHyp)
430 {
431         if(MYDEBUG) MESSAGE("addHypothesis");
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 =
447                         _gen_i->GetShapeReader()->GetShape(SMESH_Gen_i::GetGeomEngine(), aSubShapeObject);
448                 int hypId = myHyp->GetId();
449                 status = _impl->AddHypothesis(myLocSubShape, hypId);
450                 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
451                   _mapHypo[hypId] = myHyp;
452         }
453         catch(SALOME_Exception & S_ex)
454         {
455                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
456         }
457         return status;
458 }
459
460 //=============================================================================
461 /*!
462  *  
463  */
464 //=============================================================================
465
466 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
467                                                         SMESH::SMESH_Hypothesis_ptr anHyp)
468      throw(SALOME::SALOME_Exception)
469 {
470   Unexpect aCatch(SALOME_SalomeException);
471   SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
472
473   if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
474     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
475
476     if ( !aStudy->_is_nil() ) {
477       // Detect whether <aSubShape> refers to this mesh or its sub-mesh
478       SALOMEDS::GenericAttribute_var anAttr;
479       SALOMEDS::AttributeIOR_var     anIOR;
480       SALOMEDS::SObject_var aMeshSO  = SALOMEDS::SObject::_narrow( aStudy->FindObjectIOR( ( SMESH_Gen_i::GetORB()->object_to_string( _this() ) ) ) );
481       if ( aMeshSO->_is_nil() ) {
482         SCRUTE( SMESH_Gen_i::GetORB()->object_to_string( _this() ));
483         addHypothesis( aSubShapeObject, anHyp );
484         return SMESH::HYP_UNKNOWN_FATAL;
485       }
486       SALOMEDS::SObject_var aMorSM, aRef;
487       CORBA::String_var aShapeIOR    = CORBA::string_dup( SMESH_Gen_i::GetORB()->object_to_string( aSubShapeObject ) );
488       SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator( aMeshSO );
489
490       for ( it->InitEx( true ); it->More(); it->Next() ) {
491         SALOMEDS::SObject_var anObj = it->Value();
492         if ( anObj->ReferencedObject( aRef ) ) {
493           if ( aRef->FindAttribute( anAttr, "AttributeIOR" ) ) {
494             anIOR = SALOMEDS::AttributeIOR::_narrow( anAttr );
495             if ( strcmp( anIOR->Value(), aShapeIOR ) == 0 ) {
496               aMorSM = anObj->GetFather();
497               break;
498             }
499           }
500         }
501       }
502
503       bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( anHyp )->_is_nil();
504       SALOMEDS::SObject_var aHypSO  = SALOMEDS::SObject::_narrow( aStudy->FindObjectIOR( ( SMESH_Gen_i::GetORB()->object_to_string( anHyp ) ) ) );
505       if ( !aMorSM->_is_nil() && !aHypSO->_is_nil() ) {
506         // Remove a refernce to hypothesis or algorithm
507         SALOMEDS::SObject_var             AHR;
508         SALOMEDS::AttributeName_var       aName;
509         SALOMEDS::AttributeSelectable_var aSelAttr;
510         SALOMEDS::AttributePixMap_var     aPixmap;
511         SALOMEDS::StudyBuilder_var        aBuilder = aStudy->NewBuilder();
512         CORBA::String_var                 aHypIOR  = CORBA::string_dup( SMESH_Gen_i::GetORB()->object_to_string( anHyp ) );
513         long                              aTag     = aIsAlgo ? SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag() : SMESH_Gen_i::GetRefOnAppliedHypothesisTag();
514
515         if ( aMorSM->FindSubObject( aTag, AHR ) ) {
516           SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator( AHR );
517           for ( ; it->More(); it->Next() ) {
518             SALOMEDS::SObject_var anObj = it->Value();
519             if ( anObj->ReferencedObject( aRef ) ) {
520               if ( aRef->FindAttribute( anAttr, "AttributeIOR" ) ) {
521                 anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
522                 if ( strcmp( anIOR->Value(), aHypIOR ) == 0 ) {
523                   aBuilder->RemoveObject( anObj );
524                   break;
525                 }
526               }
527             }
528           }
529         }
530       }
531     }
532   }
533
534   return ConvertHypothesisStatus(status);
535 }
536
537
538 //=============================================================================
539 /*!
540  *  
541  */
542 //=============================================================================
543
544 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
545                                  SMESH::SMESH_Hypothesis_ptr anHyp)
546 {
547         if(MYDEBUG) MESSAGE("removeHypothesis()");
548         // **** proposer liste de subShape (selection multiple)
549
550         if (CORBA::is_nil(aSubShapeObject))
551                 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
552                         SALOME::BAD_PARAM);
553
554         SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
555         if (CORBA::is_nil(myHyp))
556           THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
557                         SALOME::BAD_PARAM);
558
559         SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
560         try
561         {
562                 TopoDS_Shape myLocSubShape =
563                         _gen_i->GetShapeReader()->GetShape(SMESH_Gen_i::GetGeomEngine(), aSubShapeObject);
564                 int hypId = myHyp->GetId();
565                 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
566                 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
567                   _mapHypo.erase( hypId );
568         }
569         catch(SALOME_Exception & S_ex)
570         {
571                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
572         }
573         return status;
574 }
575
576 //=============================================================================
577 /*!
578  *  
579  */
580 //=============================================================================
581
582 SMESH::ListOfHypothesis *
583         SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
584 throw(SALOME::SALOME_Exception)
585 {
586   Unexpect aCatch(SALOME_SalomeException);
587   MESSAGE("GetHypothesisList");
588   if (CORBA::is_nil(aSubShapeObject))
589     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
590                                  SALOME::BAD_PARAM);
591   
592   SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
593
594   try {
595     TopoDS_Shape myLocSubShape
596       = _gen_i->GetShapeReader()->GetShape(SMESH_Gen_i::GetGeomEngine(), aSubShapeObject);
597     
598     const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
599     int i = 0, n = aLocalList.size();
600     aList->length( n );
601
602     for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
603       SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
604       if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
605         aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
606     }
607
608     aList->length( i );
609   }
610   catch(SALOME_Exception & S_ex) {
611     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
612   }
613   
614   return aList._retn();
615 }
616
617 //=============================================================================
618 /*!
619  *  
620  */
621 //=============================================================================
622 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
623                                                   const char*          theName ) 
624      throw(SALOME::SALOME_Exception)
625 {
626   Unexpect aCatch(SALOME_SalomeException);
627   MESSAGE("SMESH_Mesh_i::GetSubMesh");
628   if (CORBA::is_nil(aSubShapeObject))
629     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
630                                  SALOME::BAD_PARAM);
631   
632   int subMeshId = 0;
633   try {
634     TopoDS_Shape myLocSubShape
635       = _gen_i->GetShapeReader()->GetShape(SMESH_Gen_i::GetGeomEngine(), aSubShapeObject);
636     
637     //Get or Create the SMESH_subMesh object implementation
638     
639     ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
640     subMeshId = mySubMesh->GetId();
641     
642     // create a new subMesh object servant if there is none for the shape
643     
644     if (_mapSubMesh.find(subMeshId) == _mapSubMesh.end()) {
645       SMESH::SMESH_subMesh_var subMesh = createSubMesh( aSubShapeObject );
646       if ( _gen_i->CanPublishInStudy( subMesh ) ) {
647         SALOMEDS::SObject_var aSubmeshSO = _gen_i->PublishInStudy( _gen_i->GetCurrentStudy(), 
648                                                                    SALOMEDS::SObject::_nil(),
649                                                                    subMesh,
650                                                                    theName );
651           
652         // Add reference to <aSubShape> to the study
653         SALOMEDS::Study_var aStudy = _gen_i->GetCurrentStudy();
654         SALOMEDS::SObject_var aShapeSO = aStudy->FindObjectIOR( SMESH_Gen_i::GetORB()->object_to_string( aSubShapeObject ) );
655         if ( !aSubmeshSO->_is_nil() && !aShapeSO->_is_nil() ) {
656           if(MYDEBUG) MESSAGE( "********** SMESH_Mesh_i::GetSubMesh(): adding shape reference..." )
657           SALOMEDS::StudyBuilder_var aBuilder = aStudy->NewBuilder();
658           SALOMEDS::SObject_var SO = aBuilder->NewObjectToTag( aSubmeshSO, SMESH_Gen_i::GetRefOnShapeTag() );
659           aBuilder->Addreference( SO, aShapeSO );
660           if(MYDEBUG) MESSAGE( "********** SMESH_Mesh_i::GetSubMesh(): shape reference added" )
661         }
662       }
663     }
664   }
665   catch(SALOME_Exception & S_ex) {
666     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
667   }
668     
669   ASSERT(_mapSubMeshIor.find(subMeshId) != _mapSubMeshIor.end());
670   return SMESH::SMESH_subMesh::_duplicate(_mapSubMeshIor[subMeshId]);
671 }
672
673
674 //=============================================================================
675 /*!
676  *  
677  */
678 //=============================================================================
679
680 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
681      throw (SALOME::SALOME_Exception)
682 {
683   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
684   if ( theSubMesh->_is_nil() )
685     return;
686
687   GEOM::GEOM_Object_var aSubShapeObject;
688   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
689   if ( !aStudy->_is_nil() )  {
690     // Remove submesh's SObject
691     SALOMEDS::SObject_var anSO = SALOMEDS::SObject::_narrow( aStudy->FindObjectIOR( ( SMESH_Gen_i::GetORB()->object_to_string( theSubMesh ) ) ) );
692     if ( !anSO->_is_nil() ) {
693       long aTag = SMESH_Gen_i::GetRefOnShapeTag(); 
694       SALOMEDS::SObject_var anObj, aRef;
695       if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
696         aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
697
698       aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
699     }
700   }
701
702   removeSubMesh( theSubMesh, aSubShapeObject.in() );
703 }
704
705
706 //=============================================================================
707 /*!
708  *  
709  */
710 //=============================================================================
711
712 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
713                                               const char* theName )
714      throw(SALOME::SALOME_Exception)
715 {
716   Unexpect aCatch(SALOME_SalomeException);
717   SMESH::SMESH_Group_var aNewGroup = createGroup( theElemType, theName );
718
719   // Groups should be put under separate roots according to their type (nodes, edges, faces, volumes)
720   if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
721     SALOMEDS::SObject_var aGroupSO = _gen_i->PublishInStudy( _gen_i->GetCurrentStudy(), 
722                                                              SALOMEDS::SObject::_nil(),
723                                                              aNewGroup,
724                                                              theName );
725   }
726
727   return aNewGroup._retn();
728 }
729
730
731 //=============================================================================
732 /*!
733  *  
734  */
735 //=============================================================================
736 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroupFromGEOM( SMESH::ElementType theElemType,
737                                                           const char* theName,
738                                                           GEOM::GEOM_Object_ptr theGEOMGroup)
739      throw(SALOME::SALOME_Exception)
740 {
741   Unexpect aCatch(SALOME_SalomeException);
742   
743   SMESH::SMESH_Group_var aNewGroup = createGroup( theElemType, theName );
744   
745   if ( CORBA::is_nil(theGEOMGroup) || theGEOMGroup->GetType() != 37 || CORBA::is_nil(aNewGroup))
746     return aNewGroup._retn();
747   
748   GEOM::GEOM_IGroupOperations_var aGroupOp = SMESH_Gen_i::GetGeomEngine()->GetIGroupOperations(_studyId);
749   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
750   
751   // Check if group constructed on the same shape as a mesh or on its child:
752   GEOM::GEOM_Object_var aGroupMainShape = aGroupOp->GetMainShape( theGEOMGroup );
753   SALOMEDS::SObject_var aGroupMainShapeSO =
754     SALOMEDS::SObject::_narrow( aStudy->FindObjectIOR( SMESH_Gen_i::GetORB()->object_to_string(aGroupMainShape) ) );
755   SALOMEDS::SObject_var aMeshSO  = 
756     SALOMEDS::SObject::_narrow( aStudy->FindObjectIOR( ( SMESH_Gen_i::GetORB()->object_to_string( _this() ) ) ) );
757   
758   SALOMEDS::SObject_var anObj, aRef;
759   bool isRefOrSubShape = false;
760   
761   if ( aMeshSO->FindSubObject( 1, anObj ) &&  anObj->ReferencedObject( aRef )) {
762     if ( strcmp( aRef->GetID(), aGroupMainShapeSO->GetID() ) == 0 )
763       isRefOrSubShape = true;
764     else
765       {
766         SALOMEDS::SObject_var aFather = aGroupMainShapeSO->GetFather();
767         SALOMEDS::SComponent_var aComponent = aGroupMainShapeSO->GetFatherComponent();
768         while ( !isRefOrSubShape && strcmp( aFather->GetID(), aComponent->GetID() ) != 0 )
769           {
770             if (strcmp( aRef->GetID(), aFather->GetID() ) == 0)
771               isRefOrSubShape = true;
772             else
773               aFather = aFather->GetFather();
774           }
775       }
776     if ( !isRefOrSubShape ) 
777       return aNewGroup._retn();
778   }
779   
780   // Detect type of the geometry group
781   SMESH::ElementType aGEOMGroupType;
782   
783   SMESH::ElementType aGroupType = SMESH::ALL;
784   switch(aGroupOp->GetType(theGEOMGroup)) 
785     {
786     case 7: aGEOMGroupType = SMESH::NODE; break;
787     case 6: aGEOMGroupType = SMESH::EDGE; break;
788     case 4: aGEOMGroupType = SMESH::FACE; break;
789     case 2: aGEOMGroupType = SMESH::VOLUME; break;
790     }
791     
792   if ( aGEOMGroupType == theElemType )
793     {
794       if ( !aStudy->_is_nil() ) 
795         {
796           SALOMEDS::SObject_var aGEOMGroupSO =
797             SALOMEDS::SObject::_narrow( aStudy->FindObjectIOR( SMESH_Gen_i::GetORB()->object_to_string(theGEOMGroup) ) );
798           
799           if ( !aGEOMGroupSO->_is_nil() ) {
800             // Construct filter
801             SMESH::FilterManager_var aFilterMgr = _gen_i->CreateFilterManager();
802             SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
803             SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();
804             aBelongToGeom->SetGeom( theGEOMGroup );
805             aBelongToGeom->SetShapeName( aGEOMGroupSO->GetName() );
806             aBelongToGeom->SetElementType( theElemType );
807             aFilter->SetPredicate( aBelongToGeom );
808             SMESH::long_array_var anElements = aFilter->GetElementsId( _this() );
809             aNewGroup->Add( anElements );
810             
811             // Groups should be put under separate roots according to their type (nodes, edges, faces, volumes)
812             if ( _gen_i->CanPublishInStudy( aNewGroup ) )
813               {
814                 SALOMEDS::SObject_var aGroupSO = _gen_i->PublishInStudy( _gen_i->GetCurrentStudy(), 
815                                                                          SALOMEDS::SObject::_nil(),
816                                                                          aNewGroup,
817                                                                          theName );
818                 if ( !aGroupSO->_is_nil() )
819                   {
820                     //Add reference to geometry group
821                     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
822                     SALOMEDS::SObject_var aReference = aStudyBuilder->NewObject(aGroupSO);
823                     aStudyBuilder->Addreference(aReference, aGEOMGroupSO);
824                   }
825               }
826           }
827         }
828     }
829   
830   return aNewGroup._retn();
831 }
832 //=============================================================================
833 /*!
834  *  
835  */
836 //=============================================================================
837
838 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_Group_ptr theGroup )
839     throw (SALOME::SALOME_Exception)
840 {
841   if ( theGroup->_is_nil() )
842     return;
843
844   SMESH_Group_i* aGroup = dynamic_cast<SMESH_Group_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
845   if ( !aGroup )
846     return;
847
848   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
849   if ( !aStudy->_is_nil() )  {
850     // Remove group's SObject
851     SALOMEDS::SObject_var aGroupSO = SALOMEDS::SObject::_narrow( aStudy->FindObjectIOR( ( SMESH_Gen_i::GetORB()->object_to_string( theGroup ) ) ) );
852     if ( !aGroupSO->_is_nil() )
853       aStudy->NewBuilder()->RemoveObject( aGroupSO );
854   }
855
856   // Remove the group from SMESH data structures
857   removeGroup( aGroup->GetLocalID() );
858 }
859
860 //=============================================================================
861 /*! RemoveGroupWithContents
862  *  Remove group with its contents
863  */ 
864 //=============================================================================
865 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_Group_ptr theGroup )
866   throw (SALOME::SALOME_Exception)
867 {
868   if ( theGroup->_is_nil() )
869     return;
870
871   SMESH_Group_i* aGroup = dynamic_cast<SMESH_Group_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
872   if ( !aGroup )
873     return;
874   
875   SMESH::long_array_var anIds = aGroup->GetListOfID();
876   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
877     
878   if ( aGroup->GetType() == SMESH::NODE )
879     aMeshEditor->RemoveNodes( anIds );
880   else
881     aMeshEditor->RemoveElements( anIds );
882   
883   RemoveGroup( theGroup );
884 }
885
886 //=============================================================================
887 /*! UnionGroups
888  *  New group is created. All mesh elements that are 
889  *  present in initial groups are added to the new one
890  */
891 //=============================================================================
892 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_Group_ptr theGroup1, 
893                                                   SMESH::SMESH_Group_ptr theGroup2, 
894                                                   const char* theName )
895   throw (SALOME::SALOME_Exception)
896 {
897   try
898   {
899     SMESH::SMESH_Group_var aResGrp;
900
901     if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
902          theGroup1->GetType() != theGroup2->GetType() )
903       return SMESH::SMESH_Group::_nil();
904
905     aResGrp = CreateGroup( theGroup1->GetType(), theName );
906     if ( aResGrp->_is_nil() )
907       return SMESH::SMESH_Group::_nil();
908
909     SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
910     SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
911
912     TColStd_MapOfInteger aResMap;
913
914     for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
915       aResMap.Add( anIds1[ i1 ] );
916
917     for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
918       aResMap.Add( anIds2[ i2 ] );
919
920     SMESH::long_array_var aResIds = new SMESH::long_array;
921     aResIds->length( aResMap.Extent() );
922
923     int resI = 0;
924     TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
925     for( ; anIter.More(); anIter.Next() )
926       aResIds[ resI++ ] = anIter.Key();
927
928     aResGrp->Add( aResIds );
929
930     return aResGrp._retn();
931   }
932   catch( ... )
933   {
934     return SMESH::SMESH_Group::_nil();
935   }
936 }
937   
938 //=============================================================================
939 /*! IntersectGroups
940  *  New group is created. All mesh elements that are 
941  *  present in both initial groups are added to the new one.
942  */
943 //=============================================================================
944 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_Group_ptr theGroup1, 
945                                                       SMESH::SMESH_Group_ptr theGroup2, 
946                                                       const char* theName )
947   throw (SALOME::SALOME_Exception)
948 {
949   SMESH::SMESH_Group_var aResGrp;
950   
951   if ( theGroup1->_is_nil() || theGroup2->_is_nil() || 
952        theGroup1->GetType() != theGroup2->GetType() )
953     return aResGrp;
954   
955   aResGrp = CreateGroup( theGroup1->GetType(), theName );
956   if ( aResGrp->_is_nil() )
957     return aResGrp;
958   
959   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
960   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
961   
962   TColStd_MapOfInteger aMap1;
963   
964   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
965     aMap1.Add( anIds1[ i1 ] );
966
967   TColStd_SequenceOfInteger aSeq;
968
969   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
970     if ( aMap1.Contains( anIds2[ i2 ] ) )
971       aSeq.Append( anIds2[ i2 ] );
972   
973   SMESH::long_array_var aResIds = new SMESH::long_array;
974   aResIds->length( aSeq.Length() );
975   
976   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
977     aResIds[ resI ] = aSeq( resI + 1 );
978   
979   aResGrp->Add( aResIds );
980   
981   return aResGrp._retn();
982 }
983
984 //=============================================================================
985 /*! CutGroups
986  *  New group is created. All mesh elements that are present in 
987  *  main group but do not present in tool group are added to the new one 
988  */
989 //=============================================================================
990 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_Group_ptr theGroup1, 
991                                                 SMESH::SMESH_Group_ptr theGroup2, 
992                                                 const char* theName )
993   throw (SALOME::SALOME_Exception)
994 {
995   SMESH::SMESH_Group_var aResGrp;
996   
997   if ( theGroup1->_is_nil() || theGroup2->_is_nil() || 
998        theGroup1->GetType() != theGroup2->GetType() )
999     return aResGrp;
1000   
1001   aResGrp = CreateGroup( theGroup1->GetType(), theName );
1002   if ( aResGrp->_is_nil() )
1003     return aResGrp;
1004   
1005   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
1006   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
1007   
1008   TColStd_MapOfInteger aMap2;
1009   
1010   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
1011     aMap2.Add( anIds2[ i2 ] );
1012
1013
1014   TColStd_SequenceOfInteger aSeq;
1015   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
1016     if ( !aMap2.Contains( anIds1[ i1 ] ) )
1017       aSeq.Append( anIds1[ i1 ] );
1018
1019   SMESH::long_array_var aResIds = new SMESH::long_array;
1020   aResIds->length( aSeq.Length() );
1021
1022   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
1023     aResIds[ resI ] = aSeq( resI + 1 );  
1024   
1025   aResGrp->Add( aResIds );
1026   
1027   return aResGrp._retn();
1028 }
1029
1030 //=============================================================================
1031 /*!
1032  *  
1033  */
1034 //=============================================================================
1035
1036 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
1037 {
1038   TopoDS_Shape myLocSubShape = _gen_i->GetShapeReader()->GetShape(SMESH_Gen_i::GetGeomEngine(), theSubShapeObject);
1039
1040   ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
1041   int subMeshId = mySubMesh->GetId();
1042   SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
1043   SMESH::SMESH_subMesh_var subMesh
1044     = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
1045
1046   _mapSubMesh[subMeshId] = mySubMesh;
1047   _mapSubMesh_i[subMeshId] = subMeshServant;
1048   _mapSubMeshIor[subMeshId]
1049     = SMESH::SMESH_subMesh::_duplicate(subMesh);
1050
1051   // register CORBA object for persistence
1052   StudyContext* myStudyContext = _gen_i->GetCurrentStudyContext();
1053   string iorString = SMESH_Gen_i::GetORB()->object_to_string( subMesh );
1054   int nextId = myStudyContext->addObject( iorString );
1055   if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId << " and IOR = " << iorString.c_str() );
1056
1057   return subMesh._retn(); 
1058 }
1059
1060
1061 //=============================================================================
1062 /*!
1063  *  
1064  */
1065 //=============================================================================
1066
1067 void SMESH_Mesh_i::removeSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh, GEOM::GEOM_Object_ptr theSubShapeObject )
1068 {
1069   MESSAGE("SMESH_Mesh_i::removeSubMesh()");
1070   if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
1071     return;
1072
1073   try {
1074     SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
1075     for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
1076       removeHypothesis( theSubShapeObject, aHypList[i] );
1077     }
1078   }
1079   catch( const SALOME::SALOME_Exception& ) {
1080     INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
1081   }
1082
1083   int subMeshId = theSubMesh->GetId();
1084
1085   _mapSubMesh.erase(subMeshId);
1086   _mapSubMesh_i.erase(subMeshId);
1087   _mapSubMeshIor.erase(subMeshId);
1088   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
1089 }
1090
1091 //=============================================================================
1092 /*!
1093  *  
1094  */
1095 //=============================================================================
1096
1097 SMESH::SMESH_Group_ptr SMESH_Mesh_i::createGroup( SMESH::ElementType theElemType, const char* theName )
1098 {
1099   int anId;
1100   SMESH::SMESH_Group_var aGroup;
1101   if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId ) ) {
1102     SMESH_Group_i* aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
1103     aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
1104     _mapGroups[anId] = SMESH::SMESH_Group::_duplicate( aGroup );
1105
1106     // register CORBA object for persistence
1107     StudyContext* myStudyContext = _gen_i->GetCurrentStudyContext();
1108     string iorString = SMESH_Gen_i::GetORB()->object_to_string( aGroup );
1109     int nextId = myStudyContext->addObject( iorString );
1110     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId << " and IOR = " << iorString.c_str() );
1111   }
1112   return aGroup._retn();
1113 }
1114
1115
1116 //=============================================================================
1117 /*!
1118  * SMESH_Mesh_i::removeGroup
1119  *
1120  * Should be called by ~SMESH_Group_i() 
1121  */
1122 //=============================================================================
1123
1124 void SMESH_Mesh_i::removeGroup( const int theId )
1125 {
1126   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );  
1127   if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
1128     _mapGroups.erase( theId );
1129     _impl->RemoveGroup( theId );
1130   }
1131 }
1132
1133
1134 //=============================================================================
1135 /*!
1136  *  
1137  */
1138 //=============================================================================
1139
1140 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
1141 throw(SALOME::SALOME_Exception)
1142 {
1143   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
1144   
1145   SMESH::log_array_var aLog;
1146   try{
1147     list < SMESHDS_Command * >logDS = _impl->GetLog();
1148     aLog = new SMESH::log_array;
1149     int indexLog = 0;
1150     int lg = logDS.size();
1151     SCRUTE(lg);
1152     aLog->length(lg);
1153     list < SMESHDS_Command * >::iterator its = logDS.begin();
1154     while(its != logDS.end()){
1155       SMESHDS_Command *com = *its;
1156       int comType = com->GetType();
1157       //SCRUTE(comType);
1158       int lgcom = com->GetNumber();
1159       //SCRUTE(lgcom);
1160       const list < int >&intList = com->GetIndexes();
1161       int inum = intList.size();
1162       //SCRUTE(inum);
1163       list < int >::const_iterator ii = intList.begin();
1164       const list < double >&coordList = com->GetCoords();
1165       int rnum = coordList.size();
1166       //SCRUTE(rnum);
1167       list < double >::const_iterator ir = coordList.begin();
1168       aLog[indexLog].commandType = comType;
1169       aLog[indexLog].number = lgcom;
1170       aLog[indexLog].coords.length(rnum);
1171       aLog[indexLog].indexes.length(inum);
1172       for(int i = 0; i < rnum; i++){
1173         aLog[indexLog].coords[i] = *ir;
1174         //MESSAGE(" "<<i<<" "<<ir.Value());
1175         ir++;
1176       }
1177       for(int i = 0; i < inum; i++){
1178         aLog[indexLog].indexes[i] = *ii;
1179         //MESSAGE(" "<<i<<" "<<ii.Value());
1180         ii++;
1181       }
1182       indexLog++;
1183       its++;
1184     }
1185     if(clearAfterGet)
1186       _impl->ClearLog();
1187   }
1188   catch(SALOME_Exception & S_ex){
1189     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1190   }
1191   return aLog._retn();
1192 }
1193
1194
1195 //=============================================================================
1196 /*!
1197  *  
1198  */
1199 //=============================================================================
1200
1201 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
1202 {
1203   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
1204   // ****
1205 }
1206
1207 //=============================================================================
1208 /*!
1209  *  
1210  */
1211 //=============================================================================
1212
1213 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1214 {
1215   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1216   return _id;
1217 }
1218
1219 //=============================================================================
1220 /*!
1221  *  
1222  */
1223 //=============================================================================
1224
1225 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1226 {
1227   return _studyId;
1228 }
1229
1230 //=============================================================================
1231 /*!
1232  *  
1233  */
1234 //=============================================================================
1235
1236 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1237 {
1238   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1239   _impl = impl;
1240 }
1241
1242 //=============================================================================
1243 /*!
1244  *  
1245  */
1246 //=============================================================================
1247
1248 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1249 {
1250   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1251   return *_impl;
1252 }
1253
1254
1255 //=============================================================================
1256 /*!
1257  *  
1258  */
1259 //=============================================================================
1260
1261 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1262 {
1263         SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl );
1264         SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1265         return aMesh._retn();
1266 }
1267
1268 //=============================================================================
1269 /*!
1270  *  
1271  */
1272 //=============================================================================
1273
1274 void SMESH_Mesh_i::ExportMED(const char *file, CORBA::Boolean auto_groups) throw(SALOME::SALOME_Exception)
1275 {
1276   Unexpect aCatch(SALOME_SalomeException);
1277   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1278   if ( aStudy->_is_nil() ) 
1279     return;
1280
1281   char* aMeshName = NULL;
1282   SALOMEDS::SObject_var aMeshSO = SALOMEDS::SObject::_narrow( aStudy->FindObjectIOR( ( SMESH_Gen_i::GetORB()->object_to_string( _this() ) ) ) );
1283   if ( !aMeshSO->_is_nil() )
1284     {
1285       aMeshName = aMeshSO->GetName();
1286       //SCRUTE(file);
1287       //SCRUTE(aMeshName);
1288       //SCRUTE(aMeshSO->GetID());
1289       SALOMEDS::GenericAttribute_var anAttr;
1290       SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1291       SALOMEDS::AttributeExternalFileDef_var aFileName;
1292       anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1293       aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1294       ASSERT(!aFileName->_is_nil());
1295       aFileName->SetValue(file);
1296       SALOMEDS::AttributeFileType_var aFileType;
1297       anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1298       aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1299       ASSERT(!aFileType->_is_nil());
1300       aFileType->SetValue("FICHIERMED");
1301     }
1302   _impl->ExportMED( file, aMeshName, auto_groups );
1303 }
1304
1305 void SMESH_Mesh_i::ExportDAT(const char *file) throw(SALOME::SALOME_Exception)
1306 {
1307   Unexpect aCatch(SALOME_SalomeException);
1308   _impl->ExportDAT(file);
1309 }
1310 void SMESH_Mesh_i::ExportUNV(const char *file) throw(SALOME::SALOME_Exception)
1311 {
1312   Unexpect aCatch(SALOME_SalomeException);
1313   _impl->ExportUNV(file);
1314 }
1315
1316 void SMESH_Mesh_i::ExportSTL(const char *file, const bool isascii) throw(SALOME::SALOME_Exception)
1317 {
1318   Unexpect aCatch(SALOME_SalomeException);
1319   _impl->ExportSTL(file, isascii);
1320 }
1321
1322 //=============================================================================
1323 /*!
1324  *  
1325  */
1326 //=============================================================================
1327
1328 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1329 {
1330   Unexpect aCatch(SALOME_SalomeException);
1331   SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1332   SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1333   return aMesh._retn();
1334 }
1335
1336 //=============================================================================
1337 /*!
1338  *  
1339  */
1340 //=============================================================================
1341 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1342 {
1343   Unexpect aCatch(SALOME_SalomeException);
1344   return _impl->NbNodes();
1345 }
1346
1347 //=============================================================================
1348 /*!
1349  *  
1350  */
1351 //=============================================================================
1352 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1353 {
1354   Unexpect aCatch(SALOME_SalomeException);
1355   return _impl->NbEdges();
1356 }
1357
1358 //=============================================================================
1359 /*!
1360  *  
1361  */
1362 //=============================================================================
1363 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1364 {
1365   Unexpect aCatch(SALOME_SalomeException);
1366   return _impl->NbFaces();
1367 }
1368
1369 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1370 {
1371   Unexpect aCatch(SALOME_SalomeException);
1372   return _impl->NbTriangles();
1373 }
1374
1375 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1376 {
1377   Unexpect aCatch(SALOME_SalomeException);
1378   return _impl->NbQuadrangles();
1379 }
1380
1381 //=============================================================================
1382 /*!
1383  *  
1384  */
1385 //=============================================================================
1386 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1387 {
1388   Unexpect aCatch(SALOME_SalomeException);
1389   return _impl->NbVolumes();
1390 }
1391
1392 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1393 {
1394   Unexpect aCatch(SALOME_SalomeException);
1395   return _impl->NbTetras();
1396 }
1397
1398 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1399 {
1400   Unexpect aCatch(SALOME_SalomeException);
1401   return _impl->NbHexas();
1402 }
1403
1404 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1405 {
1406   Unexpect aCatch(SALOME_SalomeException);
1407   return _impl->NbPyramids();
1408 }
1409
1410 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1411 {
1412   Unexpect aCatch(SALOME_SalomeException);
1413   return _impl->NbPrisms();
1414 }
1415
1416 //=============================================================================
1417 /*!
1418  *  
1419  */
1420 //=============================================================================
1421 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1422 {
1423   Unexpect aCatch(SALOME_SalomeException);
1424   return _impl->NbSubMesh();
1425 }
1426
1427 //=============================================================================
1428 /*!
1429  *  
1430  */
1431 //=============================================================================
1432 char* SMESH_Mesh_i::Dump()
1433 {
1434   std::ostringstream os;
1435   _impl->Dump( os );
1436   return CORBA::string_dup( os.str().c_str() );
1437 }