]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDS/SALOMEDS_Study_i.cxx
Salome HOME
Fixed SIGSEGV after recall python command "myStudy.<command>"
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Study_i.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : SALOMEDS_Study_i.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "utilities.h"
28 #include <sstream>
29 #include "SALOMEDS_Study_i.hxx"
30 #include "SALOMEDS_UseCaseIterator_i.hxx"
31 #include "SALOMEDS_GenericAttribute_i.hxx"
32 #include "SALOMEDS_AttributeStudyProperties_i.hxx"
33 #include "SALOMEDS_AttributeParameter_i.hxx"
34 #include "SALOMEDS_ChildIterator_i.hxx"
35 #include "SALOMEDS_Driver_i.hxx"
36 #include "SALOMEDS.hxx"
37
38 #include "SALOMEDSImpl_SObject.hxx"
39 #include "SALOMEDSImpl_SComponent.hxx"
40 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
41 #include "SALOMEDSImpl_AttributeStudyProperties.hxx"
42 #include "SALOMEDSImpl_AttributeParameter.hxx"
43 #include "SALOMEDSImpl_ChildIterator.hxx"
44 #include "SALOMEDSImpl_IParameters.hxx"
45 #include "SALOMEDSImpl_Callback.hxx"
46
47 #include "DF_Label.hxx"
48 #include "DF_Attribute.hxx"
49
50 #include "Utils_ExceptHandlers.hxx"
51
52 #include "Basics_Utils.hxx"
53 #include "SALOME_KernelServices.hxx"
54
55 #ifdef WIN32
56 #include <process.h>
57 #else
58 #include <sys/types.h>
59 #include <unistd.h>
60 #endif
61
62 UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
63 UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
64
65 static SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
66
67 namespace SALOMEDS
68 {
69   class Notifier: public SALOMEDSImpl_AbstractCallback
70   {
71   public:
72     Notifier(CORBA::ORB_ptr orb)
73     {
74       _orb = CORBA::ORB::_duplicate(orb);
75     }
76
77     //============================================================================
78     /*! Function : addSO_Notification
79      *  Purpose  : This function tells all the observers that a SO has been added
80      */
81     //============================================================================
82
83     virtual bool addSO_Notification(const SALOMEDSImpl_SObject& theSObject)
84     {
85       std::string anID=theSObject.GetID();
86       const char* cID=anID.c_str();
87       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
88       {
89         it->first->notifyObserverID(cID,1);
90       }
91       return true; // NGE return always true but can be modified if needed
92     }
93
94     //============================================================================
95     /*! Function : removeSO_Notification
96      *  Purpose  : This function tells all the observers that a SO has been removed
97      */
98     //============================================================================
99
100     virtual bool removeSO_Notification(const SALOMEDSImpl_SObject& theSObject)
101     {
102       std::string anID=theSObject.GetID();
103       const char* cID=anID.c_str();
104       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
105       {
106         it->first->notifyObserverID(cID,2);
107       }
108       return true; // NGE return always true but can be modified if needed
109     }
110
111     //============================================================================
112     /*! Function : modifySO_Notification
113      *  Purpose  : This function tells all the observers that a SO has been modified
114      */
115     //============================================================================
116
117     virtual bool modifySO_Notification(const SALOMEDSImpl_SObject& theSObject, int reason)
118     {
119       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
120       {
121         if(it->second)
122         {
123           std::string anID=theSObject.GetID();
124           const char* cID=anID.c_str();
125           it->first->notifyObserverID(cID,reason);
126         }
127       }
128       return true; // NGE return always true but can be modified if needed
129     }
130
131     //============================================================================
132     /*! Function : modifyNB_Notification
133      *  Purpose  : This function tells all the observers that 
134      *             a NoteBook variable has been added/modified/removed.
135      */
136     //============================================================================
137     
138     virtual bool modifyNB_Notification(const char* theVarName)
139     {
140       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
141         {
142           it->first->notifyObserverID(theVarName,6);
143         }
144       return true; // NGE return always true but can be modified if needed
145     }
146
147     //============================================================================
148     /*! Function : attach
149      *  Purpose  : register an Observer
150      */
151     //============================================================================
152
153     virtual void attach(SALOMEDS::Observer_ptr theObs, bool modify)
154     {
155       myObservers.push_back(std::make_pair(SALOMEDS::Observer::_duplicate(theObs),modify));
156     }
157
158     //============================================================================
159     /*! Function : detach
160      *  Purpose  : unregister an Observer
161      */
162     //============================================================================
163
164     virtual void detach(SALOMEDS::Observer_ptr theObs)
165     {
166       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
167       {
168         if ( it->first->_is_equivalent(theObs) ) {
169           myObservers.erase( it );
170           break;
171         }
172       }
173     }
174
175   private:
176     typedef std::list< std::pair< SALOMEDS::Observer_var, bool > > ObsList;
177     typedef ObsList::iterator ObsListIter;
178     ObsList myObservers;
179     CORBA::ORB_var                    _orb;
180   };
181
182   class GenObjRegister: public SALOMEDSImpl_AbstractCallback
183   {
184   public:
185     GenObjRegister(CORBA::ORB_ptr orb)
186     {
187       _orb = CORBA::ORB::_duplicate(orb);
188     }
189     virtual void RegisterGenObj  (const std::string& theIOR)
190     {
191       try
192       {
193         CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
194         if ( obj->_non_existent() ) return;
195         SALOME::GenericObj_var gobj = SALOME::GenericObj::_narrow(obj);
196         if(! CORBA::is_nil(gobj) )
197         {
198           gobj->Register();
199         }
200       }
201       catch(const CORBA::Exception& e)
202       {
203       }
204     }
205     virtual void UnRegisterGenObj(const std::string& theIOR)
206     {
207       try
208       {
209         CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
210         if ( obj->_non_existent() ) return;
211         SALOME::GenericObj_var gobj = SALOME::GenericObj::_narrow(obj);
212         if(! CORBA::is_nil(gobj) )
213         {
214           gobj->UnRegister();
215         }
216       }
217       catch(const CORBA::Exception& e)
218       {
219       }
220     }
221
222   private:
223     CORBA::ORB_var _orb;
224   };
225
226 } // namespace SALOMEDS
227
228 //============================================================================
229 /*! Function : SALOMEDS_Study_i
230  *  Purpose  : SALOMEDS_Study_i constructor
231  */
232 //============================================================================
233 SALOMEDS_Study_i::SALOMEDS_Study_i(CORBA::ORB_ptr orb)
234 {
235   _orb     = CORBA::ORB::_duplicate(orb);
236   _impl    = new SALOMEDSImpl_Study();
237   _factory = new SALOMEDS_DriverFactory_i(_orb);
238   _closed  = true;
239
240   Init();
241 }
242
243 //============================================================================
244 /*! Function : ~SALOMEDS_Study_i
245  *  Purpose  : SALOMEDS_Study_i destructor
246  */
247 //============================================================================
248 SALOMEDS_Study_i::~SALOMEDS_Study_i()
249 {
250   Clear();
251   delete _factory;
252   delete _impl;
253 }
254
255 //============================================================================
256 /*! Function : Init
257  *  Purpose  : Initialize study components
258  */
259 //============================================================================
260 void SALOMEDS_Study_i::Init()
261 {
262   if ( !_impl->GetDocument() )
263     _impl->Init();
264
265   if (!_closed)
266     throw SALOMEDS::Study::StudyInvalidReference();
267
268   _builder        = new SALOMEDS_StudyBuilder_i(_impl->NewBuilder(), _orb);  
269   _notifier       = new SALOMEDS::Notifier(_orb);
270   _genObjRegister = new SALOMEDS::GenObjRegister(_orb);
271   _closed         = false;
272
273   _impl->setNotifier(_notifier);
274   _impl->setGenObjRegister( _genObjRegister );
275
276   // update desktop title with new study name
277   NameChanged();
278
279   // Notify GUI that study was created
280   SALOME_NamingService *aNamingService = KERNEL::getNamingService();
281   CORBA::Object_var obj = aNamingService->Resolve("/Kernel/Session");
282   SALOME::Session_var aSession = SALOME::Session::_narrow(obj);
283   if ( !CORBA::is_nil(aSession) ) {
284     std::stringstream ss;
285     ss << "studyCreated";
286     std::string str = ss.str();
287     SALOMEDS::unlock();
288     aSession->emitMessageOneWay(str.c_str());
289     SALOMEDS::lock();
290   }
291 }
292
293 //============================================================================
294 /*! Function : Clear
295  *  Purpose  : Clear study components
296  */
297 //============================================================================
298 void SALOMEDS_Study_i::Clear()
299 {
300   SALOMEDS::Locker lock;
301   if (_closed)
302     throw SALOMEDS::Study::StudyInvalidReference();
303   //delete the builder servant
304   PortableServer::POA_var poa=_builder->_default_POA();
305   PortableServer::ObjectId_var anObjectId = poa->servant_to_id(_builder);
306   poa->deactivate_object(anObjectId.in());
307   _builder->_remove_ref();
308
309   RemovePostponed(-1);
310
311   if (_impl->GetDocument()) {
312     SALOMEDS::SComponentIterator_var itcomponent = NewComponentIterator();
313     for (; itcomponent->More(); itcomponent->Next()) {
314       SALOMEDS::SComponent_var sco = itcomponent->Value();
315       CORBA::String_var compodatatype=sco->ComponentDataType();
316       MESSAGE ( "Look for an engine for data type :"<< compodatatype);
317       // if there is an associated Engine call its method for closing
318       CORBA::String_var IOREngine;
319       if (sco->ComponentIOR(IOREngine)) {
320         // we have found the associated engine to write the data
321         MESSAGE ( "We have found an engine for data type :"<< compodatatype);
322         //_narrow can throw a corba exception
323         try {
324               CORBA::Object_var obj = _orb->string_to_object(IOREngine);
325               if (!CORBA::is_nil(obj)) {
326                 SALOMEDS::Driver_var anEngine = SALOMEDS::Driver::_narrow(obj) ;
327                 if (!anEngine->_is_nil())  {
328                   SALOMEDS::unlock();
329                   anEngine->Close(sco);
330                   SALOMEDS::lock();
331                 }
332               }
333         }
334         catch (CORBA::Exception&) {
335         }
336       }
337       sco->UnRegister();
338     }
339
340     //Does not need any more this iterator
341     itcomponent->UnRegister();
342   }
343
344   // Notify GUI that study is cleared
345   SALOME_NamingService *aNamingService = KERNEL::getNamingService();
346   CORBA::Object_ptr obj = aNamingService->Resolve("/Kernel/Session");
347   SALOME::Session_var aSession = SALOME::Session::_narrow(obj);
348   if ( !CORBA::is_nil(aSession) ) {
349     std::stringstream ss;
350     ss << "studyCleared";
351     std::string str = ss.str();
352     SALOMEDS::unlock();
353     aSession->emitMessageOneWay(str.c_str());
354     SALOMEDS::lock();
355   }
356
357   _impl->Clear();
358   _impl->setNotifier(0);
359   delete _notifier;
360   delete _genObjRegister;
361
362   _closed = true;
363 }
364
365 //============================================================================
366 /*! Function : Open
367  *  Purpose  : Open a Study from it's persistent reference
368  */
369 //============================================================================
370 bool SALOMEDS_Study_i::Open(const char* aUrl)
371   throw(SALOME::SALOME_Exception)
372 {
373   SALOMEDS::Locker lock;
374
375   Unexpect aCatch(SalomeException);
376   MESSAGE("Begin of SALOMEDS_Study_i::Open");
377
378   bool res = _impl->Open(std::string(aUrl));
379
380   // update desktop title with new study name
381   NameChanged();
382
383   if ( !res )
384     THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
385   return res;
386 }
387
388 //============================================================================
389 /*! Function : Save
390  *  Purpose  : Save a Study to it's persistent reference
391  */
392 //============================================================================
393 CORBA::Boolean SALOMEDS_Study_i::Save(CORBA::Boolean theMultiFile)
394 {
395   SALOMEDS::Locker lock;
396   if (_closed)
397     throw SALOMEDS::Study::StudyInvalidReference();
398   return _impl->Save(_factory, theMultiFile);
399 }
400
401 CORBA::Boolean SALOMEDS_Study_i::SaveASCII(CORBA::Boolean theMultiFile)
402 {
403   SALOMEDS::Locker lock;
404   if (_closed)
405     throw SALOMEDS::Study::StudyInvalidReference();
406   return _impl->SaveASCII(_factory, theMultiFile);
407 }
408
409 //=============================================================================
410 /*! Function : SaveAs
411  *  Purpose  : Save a study to the persistent reference aUrl
412  */
413 //============================================================================
414 CORBA::Boolean SALOMEDS_Study_i::SaveAs(const char* aUrl, CORBA::Boolean theMultiFile)
415 {
416   SALOMEDS::Locker lock;
417   if (_closed)
418     throw SALOMEDS::Study::StudyInvalidReference();
419   return _impl->SaveAs(std::string(aUrl), _factory, theMultiFile);
420 }
421
422 CORBA::Boolean SALOMEDS_Study_i::SaveAsASCII(const char* aUrl, CORBA::Boolean theMultiFile)
423 {
424   SALOMEDS::Locker lock;
425   if (_closed)
426     throw SALOMEDS::Study::StudyInvalidReference();
427   return _impl->SaveAsASCII(std::string(aUrl), _factory, theMultiFile);
428 }
429
430 //============================================================================
431 /*! Function : CanCopy
432  *  Purpose  :
433  */
434 //============================================================================
435 CORBA::Boolean SALOMEDS_Study_i::CanCopy(SALOMEDS::SObject_ptr theObject)
436 {
437   SALOMEDS::Locker lock;
438   if (_closed)
439     throw SALOMEDS::Study::StudyInvalidReference();
440
441   CORBA::String_var anID = theObject->GetID();
442   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
443
444   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
445   bool ret = _impl->CanCopy(anObject, aDriver);
446   delete aDriver;
447   return ret;
448 }
449
450 //============================================================================
451 /*! Function : Copy
452  *  Purpose  :
453  */
454 //============================================================================
455 CORBA::Boolean SALOMEDS_Study_i::Copy(SALOMEDS::SObject_ptr theObject)
456 {
457   SALOMEDS::Locker lock;
458   if (_closed)
459     throw SALOMEDS::Study::StudyInvalidReference();
460
461   CORBA::String_var anID = theObject->GetID();
462   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
463
464   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
465   bool ret = _impl->Copy(anObject, aDriver);
466   delete aDriver;
467   return ret;
468 }
469
470 //============================================================================
471 /*! Function : CanPaste
472  *  Purpose  :
473  */
474 //============================================================================
475 CORBA::Boolean SALOMEDS_Study_i::CanPaste(SALOMEDS::SObject_ptr theObject)
476 {
477   SALOMEDS::Locker lock;
478   if (_closed)
479     throw SALOMEDS::Study::StudyInvalidReference();
480
481   CORBA::String_var anID = theObject->GetID();
482   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
483
484   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
485   bool ret = _impl->CanPaste(anObject, aDriver);
486   delete aDriver;
487   return ret;
488 }
489
490 //============================================================================
491 /*! Function : Paste
492  *  Purpose  :
493  */
494 //============================================================================
495 SALOMEDS::SObject_ptr SALOMEDS_Study_i::Paste(SALOMEDS::SObject_ptr theObject)
496      throw(SALOMEDS::StudyBuilder::LockProtection)
497 {
498   SALOMEDS::Locker lock;
499
500   Unexpect aCatch(LockProtection);
501
502   CORBA::String_var anID = theObject->GetID();
503   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
504   SALOMEDSImpl_SObject aNewSO;
505
506   try {
507     SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
508     aNewSO =  _impl->Paste(anObject, aDriver);
509     delete aDriver;
510   }
511   catch (...) {
512     throw SALOMEDS::StudyBuilder::LockProtection();
513   }
514
515   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
516   return so._retn();
517 }
518
519 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
520 {
521   SALOMEDS_Driver_i* driver = NULL;
522
523   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
524   if(!aSCO.IsNull()) {
525     std::string IOREngine = aSCO.GetIOR();
526     if(!IOREngine.empty()) {
527       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
528       Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
529       driver = new SALOMEDS_Driver_i(Engine, orb);
530     }
531   }
532
533   return driver;
534 }
535
536 //============================================================================
537 /*! Function : GetPersistentReference
538  *  Purpose  : Get persistent reference of study (idem URL())
539  */
540 //============================================================================
541 char* SALOMEDS_Study_i::GetPersistentReference()
542 {
543   SALOMEDS::Locker lock; 
544   if (_closed)
545     throw SALOMEDS::Study::StudyInvalidReference();  
546   return CORBA::string_dup(_impl->GetPersistentReference().c_str());
547 }
548 //============================================================================
549 /*! Function : GetTransientReference
550  *  Purpose  : Get IOR of the Study (registred in OCAF document in doc->Root)
551  */
552 //============================================================================
553 char* SALOMEDS_Study_i::GetTransientReference()
554 {
555   SALOMEDS::Locker lock; 
556   if (_closed)
557     throw SALOMEDS::Study::StudyInvalidReference();  
558   return CORBA::string_dup(_impl->GetTransientReference().c_str());
559 }
560
561 //============================================================================
562 /*! Function : IsEmpty
563  *  Purpose  : Detect if study is empty
564  */
565 //============================================================================
566 CORBA::Boolean SALOMEDS_Study_i::IsEmpty()
567 {
568   SALOMEDS::Locker lock; 
569   if (_closed)
570     throw SALOMEDS::Study::StudyInvalidReference();  
571   return _impl->IsEmpty();
572 }
573
574 //============================================================================
575 /*! Function : FindComponent
576  *  Purpose  : Find a Component with ComponentDataType = aComponentName
577  */
578 //============================================================================
579 SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponent (const char* aComponentName)
580 {
581   SALOMEDS::Locker lock; 
582   
583   if (_closed)
584     throw SALOMEDS::Study::StudyInvalidReference();  
585
586   SALOMEDS::SComponent_var sco;
587
588   SALOMEDSImpl_SComponent aCompImpl = _impl->FindComponent(std::string(aComponentName));
589   if (!aCompImpl.IsNull())
590     sco = SALOMEDS_SComponent_i::New(aCompImpl, _orb);
591
592   return sco._retn();
593 }
594
595 //============================================================================
596 /*! Function : FindComponentID
597  *  Purpose  : Find a Component from it's ID
598  */
599 //============================================================================
600 SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponentID(const char* aComponentID)
601 {
602   SALOMEDS::Locker lock; 
603   
604   if (_closed)
605     throw SALOMEDS::Study::StudyInvalidReference();  
606
607   SALOMEDS::SComponent_var sco;
608
609   SALOMEDSImpl_SComponent aCompImpl = _impl->FindComponentID(std::string((char*)aComponentID));
610   if (!aCompImpl.IsNull())
611     sco = SALOMEDS_SComponent_i::New(aCompImpl, _orb);
612
613   return sco._retn();
614 }
615
616 //============================================================================
617 /*! Function : FindObject
618  *  Purpose  : Find an Object with SALOMEDS::Name = anObjectName
619  */
620 //============================================================================
621 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObject(const char* anObjectName)
622 {
623   SALOMEDS::Locker lock; 
624
625   if (_closed)
626     throw SALOMEDS::Study::StudyInvalidReference();  
627
628   SALOMEDS::SObject_var so;
629
630   SALOMEDSImpl_SObject aSO = _impl->FindObject(std::string((char*)anObjectName));
631   if (!aSO.IsNull()) {
632     if (aSO.IsComponent()) {
633       SALOMEDSImpl_SComponent aSCO = aSO;
634       so = SALOMEDS_SComponent_i::New(aSCO, _orb);
635     }
636     else {
637       so = SALOMEDS_SObject_i::New(aSO, _orb);
638     }
639   }
640
641   return so._retn();
642 }
643
644 //============================================================================
645 /*! Function : FindObjectID
646  *  Purpose  : Find an Object with ID = anObjectID
647  */
648 //============================================================================
649 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectID(const char* anObjectID)
650 {
651   SALOMEDS::Locker lock; 
652
653   if (_closed)
654     throw SALOMEDS::Study::StudyInvalidReference();  
655
656   SALOMEDS::SObject_var so;
657
658   SALOMEDSImpl_SObject aSO = _impl->FindObjectID(std::string((char*)anObjectID));
659   if (!aSO.IsNull())
660     so = SALOMEDS_SObject_i::New(aSO, _orb);
661
662   return so._retn();
663 }
664
665 //============================================================================
666 /*! Function : CreateObjectID
667  *  Purpose  : Creates an Object with ID = anObjectID
668  */
669 //============================================================================
670 SALOMEDS::SObject_ptr SALOMEDS_Study_i::CreateObjectID(const char* anObjectID)
671 {
672   SALOMEDS::Locker lock; 
673
674   if (_closed)
675     throw SALOMEDS::Study::StudyInvalidReference();  
676
677   SALOMEDS::SObject_var so;
678
679   if (anObjectID && strlen(anObjectID) > 0) {
680     SALOMEDSImpl_SObject aSO = _impl->CreateObjectID((char*)anObjectID);
681     if (!aSO.IsNull())
682       so = SALOMEDS_SObject_i::New(aSO, _orb);
683   }
684
685   return so._retn();
686 }
687
688 //============================================================================
689 /*! Function : FindObjectByName
690  *  Purpose  : Find Objects with SALOMEDS::Name = anObjectName in a Component
691  *           : with ComponentDataType = aComponentName
692  */
693 //============================================================================
694 SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindObjectByName( const char* anObjectName,
695                                                                     const char* aComponentName )
696 {
697   SALOMEDS::Locker lock; 
698
699   if (_closed)
700     throw SALOMEDS::Study::StudyInvalidReference();  
701
702   std::vector<SALOMEDSImpl_SObject> aSeq = _impl->FindObjectByName(std::string((char*)anObjectName),
703                                                                    std::string((char*)aComponentName));
704
705   SALOMEDS::Study::ListOfSObject_var listSO = new SALOMEDS::Study::ListOfSObject;
706   int aLength = aSeq.size();
707   listSO->length(aLength);
708   for (int i = 0; i < aLength; i++) {
709     SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New(aSeq[i], _orb);
710     listSO[i] = so;
711   }
712   
713   return listSO._retn();
714 }
715
716 //============================================================================
717 /*! Function : FindObjectIOR
718  *  Purpose  : Find an Object with IOR = anObjectIOR
719  */
720 //============================================================================
721 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectIOR(const char* anObjectIOR)
722 {
723   SALOMEDS::Locker lock; 
724
725   if (_closed)
726     throw SALOMEDS::Study::StudyInvalidReference();  
727
728   SALOMEDS::SObject_var so;
729
730   SALOMEDSImpl_SObject aSO = _impl->FindObjectIOR(std::string((char*)anObjectIOR));
731   if (!aSO.IsNull())
732     so = SALOMEDS_SObject_i::New(aSO, _orb);
733
734   return so._retn();
735 }
736
737 //============================================================================
738 /*! Function : FindObjectByPath
739  *  Purpose  : Find an Object by its path = thePath
740  */
741 //============================================================================
742 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectByPath(const char* thePath)
743 {
744   SALOMEDS::Locker lock; 
745
746   if (_closed)
747     throw SALOMEDS::Study::StudyInvalidReference();  
748
749   SALOMEDS::SObject_var so;
750
751   SALOMEDSImpl_SObject aSO = _impl->FindObjectByPath(std::string((char*)thePath));
752   if (!aSO.IsNull())
753     so = SALOMEDS_SObject_i::New (aSO, _orb);
754
755   return so._retn();
756 }
757
758 //============================================================================
759 /*! Function : GetObjectPath
760  *  Purpose  : 
761  */
762 //============================================================================
763 char* SALOMEDS_Study_i::GetObjectPath(CORBA::Object_ptr theObject)
764 {
765   SALOMEDS::Locker lock; 
766
767   if (_closed)
768     throw SALOMEDS::Study::StudyInvalidReference();  
769
770   std::string aPath = "";
771
772   if (!CORBA::is_nil(theObject)) {
773     SALOMEDS::SObject_var aSObj = SALOMEDS::SObject::_narrow(theObject);
774     SALOMEDSImpl_SObject aSO;
775
776     if (!CORBA::is_nil(aSObj)) {
777       aSO = _impl->FindObjectID(aSObj->GetID());
778     }
779     else {
780       aSO = _impl->FindObjectIOR(_orb->object_to_string(theObject));
781     }
782     
783     if (!aSO.IsNull()) {    
784       aPath = _impl->GetObjectPath(aSO);
785     }
786   }
787
788   return CORBA::string_dup(aPath.c_str());
789 }
790
791
792 //============================================================================
793 /*! Function : SetContext
794  *  Purpose  : Sets the current context
795  */
796 //============================================================================
797 void SALOMEDS_Study_i::SetContext(const char* thePath) 
798 {
799   SALOMEDS::Locker lock; 
800
801   if (_closed)
802     throw SALOMEDS::Study::StudyInvalidReference();  
803
804   _impl->SetContext(std::string((char*)thePath));
805   if (_impl->IsError() && _impl->GetErrorCode() == "InvalidContext") 
806     throw SALOMEDS::Study::StudyInvalidContext();  
807 }
808
809 //============================================================================
810 /*! Function : GetContext
811  *  Purpose  : Gets the current context
812  */
813 //============================================================================
814 char* SALOMEDS_Study_i::GetContext() 
815 {
816   SALOMEDS::Locker lock; 
817   
818   if (_closed)
819     throw SALOMEDS::Study::StudyInvalidReference();  
820
821   if (!_impl->HasCurrentContext()) throw SALOMEDS::Study::StudyInvalidContext();
822
823   return CORBA::string_dup(_impl->GetContext().c_str());
824 }
825
826 //============================================================================
827 /*! Function : GetObjectNames
828  *  Purpose  : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
829  */
830 //============================================================================
831 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetObjectNames(const char* theContext) 
832 {
833   SALOMEDS::Locker lock; 
834
835   if (_closed)
836     throw SALOMEDS::Study::StudyInvalidReference();  
837
838   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
839
840   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
841     throw SALOMEDS::Study::StudyInvalidContext();
842   
843   std::vector<std::string> aSeq = _impl->GetObjectNames(std::string((char*)theContext));
844   if (_impl->GetErrorCode() == "InvalidContext")
845     throw SALOMEDS::Study::StudyInvalidContext();
846
847   int aLength = aSeq.size();
848   aResult->length(aLength);
849   for (int anIndex = 0; anIndex < aLength; anIndex++) {
850     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
851   }
852
853   return aResult._retn();
854 }
855
856 //============================================================================
857 /*! Function : GetDirectoryNames
858  *  Purpose  : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
859  */
860 //============================================================================
861 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetDirectoryNames(const char* theContext) 
862 {
863   SALOMEDS::Locker lock; 
864
865   if (_closed)
866     throw SALOMEDS::Study::StudyInvalidReference();  
867
868   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
869
870   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
871     throw SALOMEDS::Study::StudyInvalidContext();
872   
873   std::vector<std::string> aSeq = _impl->GetDirectoryNames(std::string((char*)theContext));
874   if (_impl->GetErrorCode() == "InvalidContext")
875     throw SALOMEDS::Study::StudyInvalidContext();
876
877   int aLength = aSeq.size();
878   aResult->length(aLength);
879   for (int anIndex = 0; anIndex < aLength; anIndex++) {
880     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
881   }
882   
883   return aResult._retn();
884 }
885
886 //============================================================================
887 /*! Function : GetFileNames
888  *  Purpose  : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
889  */
890 //============================================================================
891 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetFileNames(const char* theContext) 
892 {
893   SALOMEDS::Locker lock; 
894
895   if (_closed)
896     throw SALOMEDS::Study::StudyInvalidReference();  
897
898   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
899
900   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
901     throw SALOMEDS::Study::StudyInvalidContext();
902   
903   std::vector<std::string> aSeq = _impl->GetFileNames(std::string((char*)theContext));
904   if (_impl->GetErrorCode() == "InvalidContext")
905     throw SALOMEDS::Study::StudyInvalidContext();
906
907   int aLength = aSeq.size();
908   aResult->length(aLength);
909   for (int anIndex = 0; anIndex < aLength; anIndex++) {
910     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
911   }
912
913   return aResult._retn();
914 }
915
916 //============================================================================
917 /*! Function : GetComponentNames
918  *  Purpose  : method to get all components names
919  *  SRN:       Note, theContext can be any, it doesn't matter
920  */
921 //============================================================================
922 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetComponentNames(const char* theContext) 
923 {
924   SALOMEDS::Locker lock; 
925
926   if (_closed)
927     throw SALOMEDS::Study::StudyInvalidReference();  
928
929   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
930
931   std::vector<std::string> aSeq = _impl->GetComponentNames(std::string((char*)theContext));
932
933   int aLength = aSeq.size();
934   aResult->length(aLength);
935   for(int anIndex = 0; anIndex < aLength; anIndex++) {
936     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
937   }
938
939   return aResult._retn();
940 }
941
942 //============================================================================
943 /*! Function : NewChildIterator
944  *  Purpose  : Create a ChildIterator from an SObject
945  */
946 //============================================================================
947 SALOMEDS::ChildIterator_ptr SALOMEDS_Study_i::NewChildIterator(SALOMEDS::SObject_ptr theSO)
948 {
949   SALOMEDS::Locker lock; 
950
951   if (_closed)
952     throw SALOMEDS::Study::StudyInvalidReference();  
953
954   CORBA::String_var anID = theSO->GetID();
955   SALOMEDSImpl_SObject aSO = _impl->GetSObject(anID.in());
956   SALOMEDSImpl_ChildIterator anItr(aSO);
957   SALOMEDS_ChildIterator_i* it_servant = new SALOMEDS_ChildIterator_i(anItr, _orb);
958   SALOMEDS::ChildIterator_var it = it_servant->_this();
959
960   return it._retn();
961 }
962
963
964 //============================================================================
965 /*! Function : NewComponentIterator
966  *  Purpose  : Create a SComponentIterator
967  */
968 //============================================================================
969 SALOMEDS::SComponentIterator_ptr SALOMEDS_Study_i::NewComponentIterator()
970 {
971   SALOMEDS::Locker lock; 
972
973   if (_closed)
974     throw SALOMEDS::Study::StudyInvalidReference();  
975
976   SALOMEDS_SComponentIterator_i* it_servant = new SALOMEDS_SComponentIterator_i(_impl->NewComponentIterator(), _orb);
977   it_servant->Init();
978   SALOMEDS::SComponentIterator_var it = it_servant->_this();
979
980   return it._retn();
981 }
982
983
984 //============================================================================
985 /*! Function : NewBuilder
986  *  Purpose  : Create a StudyBuilder
987  */
988 //============================================================================
989 SALOMEDS::StudyBuilder_ptr SALOMEDS_Study_i::NewBuilder()
990 {
991   SALOMEDS::Locker lock; 
992
993   if (_closed)
994     throw SALOMEDS::Study::StudyInvalidReference();  
995
996   SALOMEDS::StudyBuilder_var sb = SALOMEDS::StudyBuilder::_duplicate(_builder->_this());
997
998   return sb._retn();
999 }
1000  
1001 //============================================================================
1002 /*! Function : Name
1003  *  Purpose  : get study name
1004  */
1005 //============================================================================
1006 char* SALOMEDS_Study_i::Name()
1007 {
1008   SALOMEDS::Locker lock; 
1009   // Name is specified as IDL attribute: user exception cannot be raised
1010   return CORBA::string_dup(_impl->Name().c_str());
1011 }
1012
1013 //============================================================================
1014 /*! Function : IsSaved
1015  *  Purpose  : get if study has been saved
1016  */
1017 //============================================================================
1018 CORBA::Boolean SALOMEDS_Study_i::IsSaved()
1019 {
1020   SALOMEDS::Locker lock; 
1021   // IsSaved is specified as IDL attribute: user exception cannot be raised
1022   return (!_closed) ? _impl->IsSaved() : false;
1023 }
1024
1025 //============================================================================
1026 /*! Function : IsSaved
1027  *  Purpose  : set if study has been saved
1028  */
1029 //============================================================================
1030 void SALOMEDS_Study_i::IsSaved(CORBA::Boolean save)
1031 {
1032   SALOMEDS::Locker lock; 
1033   // IsSaved is specified as IDL attribute: user exception cannot be raised
1034   if (!_closed)
1035     _impl->IsSaved(save);
1036 }
1037
1038 //============================================================================
1039 /*! Function : IsModified
1040  *  Purpose  : Detect if a Study has been modified since it has been saved
1041  */
1042 //============================================================================
1043 CORBA::Boolean SALOMEDS_Study_i::IsModified()
1044 {
1045   SALOMEDS::Locker lock; 
1046
1047   if (_closed)
1048     throw SALOMEDS::Study::StudyInvalidReference();  
1049
1050   return _impl->IsModified();
1051 }
1052
1053 //============================================================================
1054 /*! Function : Modified
1055  *  Purpose  : Sets a Modified flag of a Study to True
1056  */
1057 //============================================================================
1058 void SALOMEDS_Study_i::Modified()
1059 {
1060   SALOMEDS::Locker lock; 
1061
1062   if (_closed)
1063     throw SALOMEDS::Study::StudyInvalidReference();  
1064
1065   _impl->Modify();
1066 }
1067
1068 //============================================================================
1069 /*! Function : URL
1070  *  Purpose  : get URL of the study (persistent reference of the study)
1071  */
1072 //============================================================================
1073 char* SALOMEDS_Study_i::URL()
1074 {
1075   SALOMEDS::Locker lock; 
1076   // URL is specified as IDL attribute: user exception cannot be raised
1077   return CORBA::string_dup(_impl->URL().c_str());
1078 }
1079
1080 //============================================================================
1081 /*! Function : URL
1082  *  Purpose  : set URL of the study (persistent reference of the study)
1083  */
1084 //============================================================================
1085 void SALOMEDS_Study_i::URL(const char* url)
1086 {
1087   SALOMEDS::Locker lock; 
1088   // URL is specified as IDL attribute: user exception cannot be raised
1089   _impl->URL(std::string((char*)url));
1090
1091   // update desktop title with new study name
1092   NameChanged();
1093 }
1094
1095 void SALOMEDS_Study_i::UpdateIORLabelMap(const char* anIOR, const char* anEntry) 
1096 {
1097   SALOMEDS::Locker lock; 
1098
1099   if (_closed)
1100     throw SALOMEDS::Study::StudyInvalidReference();  
1101
1102   _impl->UpdateIORLabelMap(std::string((char*)anIOR), std::string((char*)anEntry));
1103 }
1104
1105 SALOMEDS::Study_ptr SALOMEDS_Study_i::GetStudy(const DF_Label& theLabel, CORBA::ORB_ptr orb)
1106 {
1107   SALOMEDS::Locker lock;
1108
1109   SALOMEDSImpl_AttributeIOR* Att = NULL;
1110   if ((Att=(SALOMEDSImpl_AttributeIOR*)theLabel.Root().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))){
1111     char* IOR = CORBA::string_dup(Att->Value().c_str());
1112     CORBA::Object_var obj = orb->string_to_object(IOR);
1113     SALOMEDS::Study_ptr aStudy = SALOMEDS::Study::_narrow(obj) ;
1114     ASSERT(!CORBA::is_nil(aStudy));
1115     return SALOMEDS::Study::_duplicate(aStudy);
1116   } else {
1117     MESSAGE("GetStudy: Problem to get study");
1118   }
1119   return SALOMEDS::Study::_nil();
1120 }
1121
1122 void SALOMEDS_Study_i::IORUpdated(SALOMEDSImpl_AttributeIOR* theAttribute) 
1123 {
1124   SALOMEDS::Locker lock; 
1125   SALOMEDSImpl_Study::IORUpdated(theAttribute);
1126 }
1127
1128 SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindDependances(SALOMEDS::SObject_ptr anObject) 
1129 {
1130   SALOMEDS::Locker lock; 
1131
1132   if (_closed)
1133     throw SALOMEDS::Study::StudyInvalidReference();  
1134
1135   SALOMEDS::GenericAttribute_ptr aTarget;
1136   if (anObject->FindAttribute(aTarget,"AttributeTarget")) {
1137     return SALOMEDS::AttributeTarget::_narrow(aTarget)->Get();
1138   }
1139   SALOMEDS::Study::ListOfSObject* aList = new SALOMEDS::Study::ListOfSObject;
1140   aList->length(0);
1141   return aList;
1142 }
1143
1144
1145 SALOMEDS::AttributeStudyProperties_ptr SALOMEDS_Study_i::GetProperties() 
1146 {
1147   SALOMEDS::Locker lock; 
1148   
1149   if (_closed)
1150     throw SALOMEDS::Study::StudyInvalidReference();  
1151
1152   SALOMEDSImpl_AttributeStudyProperties* anAttr = _impl->GetProperties();
1153   SALOMEDS_AttributeStudyProperties_i* SP = new SALOMEDS_AttributeStudyProperties_i(anAttr, _orb);
1154   SALOMEDS::AttributeStudyProperties_var aProp = SP->_this();
1155   return aProp._retn();
1156 }
1157
1158 char* SALOMEDS_Study_i::GetLastModificationDate() 
1159 {
1160   SALOMEDS::Locker lock; 
1161
1162   if (_closed)
1163     throw SALOMEDS::Study::StudyInvalidReference();  
1164
1165   return CORBA::string_dup(_impl->GetLastModificationDate().c_str());
1166 }
1167
1168 SALOMEDS::ListOfDates* SALOMEDS_Study_i::GetModificationsDate() 
1169 {
1170   SALOMEDS::Locker lock; 
1171   
1172   if (_closed)
1173     throw SALOMEDS::Study::StudyInvalidReference();  
1174
1175   SALOMEDS::ListOfDates_var aDates = new SALOMEDS::ListOfDates;
1176
1177   std::vector<std::string> aSeq = _impl->GetModificationsDate();
1178
1179   int aLength = aSeq.size();
1180   aDates->length(aLength);
1181   for (int anIndex = 0; anIndex < aLength; anIndex++) {
1182     aDates[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
1183   }
1184
1185   return aDates._retn();
1186 }
1187
1188 //============================================================================
1189 /*! Function : GetUseCaseBuilder
1190  *  Purpose  : Returns a UseCase builder
1191  */
1192 //============================================================================
1193 SALOMEDS::UseCaseBuilder_ptr SALOMEDS_Study_i::GetUseCaseBuilder() 
1194 {
1195   SALOMEDS::Locker lock; 
1196
1197   if (_closed)
1198     throw SALOMEDS::Study::StudyInvalidReference();  
1199
1200   SALOMEDS_UseCaseBuilder_i* UCBuilder = new SALOMEDS_UseCaseBuilder_i(_impl->GetUseCaseBuilder(), _orb);
1201   SALOMEDS::UseCaseBuilder_var uc = UCBuilder->_this();
1202   return uc._retn();
1203 }
1204
1205 //============================================================================
1206 /*! Function : AddPostponed
1207  *  Purpose  : 
1208  */
1209  //============================================================================
1210 void SALOMEDS_Study_i::AddPostponed(const char* theIOR) 
1211 {
1212   SALOMEDS::Locker lock; 
1213   //Not implemented
1214 }
1215
1216 void SALOMEDS_Study_i::AddCreatedPostponed(const char* theIOR) 
1217 {
1218   SALOMEDS::Locker lock; 
1219   //Not implemented
1220 }
1221
1222 //============================================================================
1223 /*! Function : RemovePostponed
1224  *  Purpose  : 
1225  */
1226 //============================================================================
1227 void SALOMEDS_Study_i::RemovePostponed(CORBA::Long /*theUndoLimit*/) 
1228 {  
1229   SALOMEDS::Locker lock; 
1230
1231   if (_closed)
1232     throw SALOMEDS::Study::StudyInvalidReference();  
1233
1234   std::vector<std::string> anIORs = _impl->GetIORs();
1235   int i, aSize = (int)anIORs.size();
1236   
1237   for (i = 0; i < aSize; i++) {
1238     try {
1239       CORBA::Object_var obj = _orb->string_to_object(anIORs[i].c_str());
1240       SALOME::GenericObj_var aGeneric = SALOME::GenericObj::_narrow(obj);
1241       //rnv: To avoid double deletion of the Salome Generic Objects:
1242       //rnv: 1. First decrement of the reference count in the SALOMEDSImpl_AttributeIOR::~SALOMEDSImpl_AttributeIOR();
1243       //rnv: 2. Second decrement of the reference count in the next string : aGeneric->UnRegister();
1244       //if (!CORBA::is_nil(aGeneric)) aGeneric->UnRegister();
1245     } catch (...) {}
1246   }
1247
1248   //Not implemented
1249 }
1250
1251 //============================================================================
1252 /*! Function : UndoPostponed
1253  *  Purpose  : 
1254  */
1255 //============================================================================
1256 void SALOMEDS_Study_i::UndoPostponed(CORBA::Long theWay) 
1257 {
1258   SALOMEDS::Locker lock; 
1259   //Not implemented
1260 }
1261
1262
1263 //============================================================================
1264 /*! Function : DumpStudy
1265  *  Purpose  : 
1266  */
1267 //============================================================================
1268 CORBA::Boolean SALOMEDS_Study_i::DumpStudy(const char* thePath, 
1269                                            const char* theBaseName, 
1270                                            CORBA::Boolean isPublished,
1271                                            CORBA::Boolean isMultiFile)
1272 {
1273   SALOMEDS::Locker lock; 
1274
1275   if (_closed)
1276     throw SALOMEDS::Study::StudyInvalidReference();  
1277
1278   std::string aPath((char*)thePath), aBaseName((char*)theBaseName);
1279   SALOMEDS_DriverFactory_i* factory = new SALOMEDS_DriverFactory_i(_orb);
1280   bool ret = _impl->DumpStudy(aPath, aBaseName, isPublished, isMultiFile, factory);
1281   delete factory;
1282
1283   return ret;
1284 }
1285
1286 //============================================================================
1287 /*! Function : GetCommonParameters
1288  *  Purpose  : 
1289  */
1290 //============================================================================
1291 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetCommonParameters(const char* theID, CORBA::Long theSavePoint)
1292 {
1293   SALOMEDS::Locker lock; 
1294   
1295   if (_closed)
1296     throw SALOMEDS::Study::StudyInvalidReference();  
1297
1298   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetCommonParameters(theID, theSavePoint);
1299   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
1300   SALOMEDS::AttributeParameter_var aParam = SP->_this();
1301
1302   return aParam._retn();
1303 }
1304  
1305 //============================================================================
1306 /*! Function : GetCommonModuleParameters
1307  *  Purpose  : 
1308  */
1309 //============================================================================
1310 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetModuleParameters(const char* theID, 
1311                                                                        const char* theModuleName, 
1312                                                                        CORBA::Long theSavePoint)
1313 {
1314   SALOMEDS::Locker lock; 
1315   
1316   if (_closed)
1317     throw SALOMEDS::Study::StudyInvalidReference();  
1318
1319   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetModuleParameters(theID, theModuleName, theSavePoint);
1320   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
1321   SALOMEDS::AttributeParameter_var aParam = SP->_this();
1322
1323   return aParam._retn();
1324 }
1325
1326 //============================================================================
1327 /*! Function : SetStudyLock
1328  *  Purpose  : 
1329  */
1330 //============================================================================
1331 void SALOMEDS_Study_i::SetStudyLock(const char* theLockerID)
1332 {
1333   SALOMEDS::Locker lock; 
1334
1335   if (_closed)
1336     throw SALOMEDS::Study::StudyInvalidReference();  
1337
1338   _impl->SetStudyLock(theLockerID);
1339 }
1340
1341 //============================================================================
1342 /*! Function : IsStudyLocked
1343  *  Purpose  : 
1344  */
1345 //============================================================================
1346 bool SALOMEDS_Study_i::IsStudyLocked()
1347 {
1348   SALOMEDS::Locker lock; 
1349
1350   if (_closed)
1351     throw SALOMEDS::Study::StudyInvalidReference();  
1352
1353   return _impl->IsStudyLocked();
1354 }
1355
1356 //============================================================================
1357 /*! Function : UnLockStudy
1358  *  Purpose  : 
1359  */
1360 //============================================================================
1361 void SALOMEDS_Study_i::UnLockStudy(const char* theLockerID)
1362 {
1363   SALOMEDS::Locker lock; 
1364
1365   if (_closed)
1366     throw SALOMEDS::Study::StudyInvalidReference();  
1367
1368   _impl->UnLockStudy(theLockerID);
1369 }
1370
1371 //============================================================================
1372 /*! Function : GetLockerID
1373  *  Purpose  : 
1374  */
1375 //============================================================================
1376 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetLockerID()
1377 {
1378   SALOMEDS::Locker lock; 
1379
1380   if (_closed)
1381     throw SALOMEDS::Study::StudyInvalidReference();  
1382
1383   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
1384
1385   std::vector<std::string> aSeq = _impl->GetLockerID();
1386
1387   int aLength = aSeq.size();
1388   aResult->length(aLength);
1389   for(int anIndex = 0; anIndex < aLength; anIndex++) {
1390     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
1391   }
1392
1393   return aResult._retn();
1394 }
1395 //============================================================================
1396 /*! Function : SetReal
1397  *  Purpose  : 
1398  */
1399 //============================================================================
1400 void SALOMEDS_Study_i::SetReal(const char* theVarName, CORBA::Double theValue)
1401 {
1402   if (_closed)
1403     throw SALOMEDS::Study::StudyInvalidReference();  
1404
1405
1406   _impl->SetVariable(std::string(theVarName), 
1407                      theValue,
1408                      SALOMEDSImpl_GenericVariable::REAL_VAR);
1409   if (_notifier)
1410     _notifier->modifyNB_Notification(theVarName);
1411 }
1412
1413 //============================================================================
1414 /*! Function : SetInteger
1415  *  Purpose  : 
1416  */
1417 //============================================================================
1418 void SALOMEDS_Study_i::SetInteger(const char* theVarName, CORBA::Long theValue)
1419 {
1420   if (_closed)
1421     throw SALOMEDS::Study::StudyInvalidReference();  
1422
1423   _impl->SetVariable(std::string(theVarName), 
1424                      theValue,
1425                      SALOMEDSImpl_GenericVariable::INTEGER_VAR);
1426   if (_notifier)
1427     _notifier->modifyNB_Notification(theVarName);
1428 }
1429
1430 //============================================================================
1431 /*! Function : SetBoolean
1432  *  Purpose  : 
1433  */
1434 //============================================================================
1435 void SALOMEDS_Study_i::SetBoolean(const char* theVarName, CORBA::Boolean theValue)
1436 {
1437   if (_closed)
1438     throw SALOMEDS::Study::StudyInvalidReference();  
1439
1440   _impl->SetVariable(std::string(theVarName), 
1441                      theValue,
1442                      SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
1443   if (_notifier)
1444     _notifier->modifyNB_Notification(theVarName);
1445 }
1446
1447 //============================================================================
1448 /*! Function : SetString
1449  *  Purpose  : 
1450  */
1451 //============================================================================
1452 void SALOMEDS_Study_i::SetString(const char* theVarName, const char* theValue)
1453 {
1454   if (_closed)
1455     throw SALOMEDS::Study::StudyInvalidReference();  
1456
1457   _impl->SetStringVariable(std::string(theVarName), 
1458                            theValue,
1459                            SALOMEDSImpl_GenericVariable::STRING_VAR);
1460   if (_notifier)
1461     _notifier->modifyNB_Notification(theVarName);
1462 }
1463
1464 //============================================================================
1465 /*! Function : SetStringAsDouble
1466  *  Purpose  : 
1467  */
1468 //============================================================================
1469 void SALOMEDS_Study_i::SetStringAsDouble(const char* theVarName, CORBA::Double theValue)
1470 {
1471   if (_closed)
1472     throw SALOMEDS::Study::StudyInvalidReference();  
1473
1474   _impl->SetStringVariableAsDouble(std::string(theVarName), 
1475                                    theValue,
1476                                    SALOMEDSImpl_GenericVariable::STRING_VAR);
1477 }
1478
1479 //============================================================================
1480 /*! Function : GetReal
1481  *  Purpose  : 
1482  */
1483 //============================================================================
1484 CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName)
1485 {
1486   if (_closed)
1487     throw SALOMEDS::Study::StudyInvalidReference();  
1488
1489   return _impl->GetVariableValue(std::string(theVarName));
1490 }
1491
1492 //============================================================================
1493 /*! Function : GetInteger
1494  *  Purpose  : 
1495  */
1496 //============================================================================
1497 CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName)
1498 {
1499   if (_closed)
1500     throw SALOMEDS::Study::StudyInvalidReference();  
1501
1502   return (long)_impl->GetVariableValue(std::string(theVarName));
1503 }
1504
1505 //============================================================================
1506 /*! Function : GetBoolean
1507  *  Purpose  : 
1508  */
1509 //============================================================================
1510 CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName)
1511 {
1512   if (_closed)
1513     throw SALOMEDS::Study::StudyInvalidReference();  
1514
1515   return (bool)_impl->GetVariableValue(std::string(theVarName));
1516 }
1517
1518 //============================================================================
1519 /*! Function : GetString
1520  *  Purpose  : 
1521  */
1522 //============================================================================
1523 char* SALOMEDS_Study_i::GetString(const char* theVarName)
1524 {
1525   if (_closed)
1526     throw SALOMEDS::Study::StudyInvalidReference();  
1527
1528   return CORBA::string_dup(_impl->GetStringVariableValue(std::string(theVarName)).c_str());
1529 }
1530
1531 //============================================================================
1532 /*! Function : IsReal
1533  *  Purpose  : 
1534  */
1535 //============================================================================
1536 CORBA::Boolean SALOMEDS_Study_i::IsReal(const char* theVarName)
1537 {
1538   if (_closed)
1539     throw SALOMEDS::Study::StudyInvalidReference();  
1540
1541   return _impl->IsTypeOf(std::string(theVarName),
1542                          SALOMEDSImpl_GenericVariable::REAL_VAR);
1543 }
1544
1545 //============================================================================
1546 /*! Function : IsInteger
1547  *  Purpose  : 
1548  */
1549 //============================================================================
1550 CORBA::Boolean SALOMEDS_Study_i::IsInteger(const char* theVarName)
1551 {
1552   if (_closed)
1553     throw SALOMEDS::Study::StudyInvalidReference();  
1554
1555   return _impl->IsTypeOf(std::string(theVarName),
1556                          SALOMEDSImpl_GenericVariable::INTEGER_VAR);
1557 }
1558
1559 //============================================================================
1560 /*! Function : IsBoolean
1561  *  Purpose  : 
1562  */
1563 //============================================================================
1564 CORBA::Boolean SALOMEDS_Study_i::IsBoolean(const char* theVarName)
1565 {
1566   if (_closed)
1567     throw SALOMEDS::Study::StudyInvalidReference();  
1568
1569   return _impl->IsTypeOf(std::string(theVarName),
1570                          SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
1571 }
1572
1573 //============================================================================
1574 /*! Function : IsString
1575  *  Purpose  : 
1576  */
1577 //============================================================================
1578 CORBA::Boolean SALOMEDS_Study_i::IsString(const char* theVarName)
1579 {
1580   if (_closed)
1581     throw SALOMEDS::Study::StudyInvalidReference();  
1582
1583   return _impl->IsTypeOf(std::string(theVarName),
1584                          SALOMEDSImpl_GenericVariable::STRING_VAR);
1585 }
1586
1587 //============================================================================
1588 /*! Function : IsVariable
1589  *  Purpose  : 
1590  */
1591 //============================================================================
1592 CORBA::Boolean SALOMEDS_Study_i::IsVariable(const char* theVarName)
1593 {
1594   if (_closed)
1595     throw SALOMEDS::Study::StudyInvalidReference();  
1596
1597   return _impl->IsVariable(std::string(theVarName));
1598 }
1599
1600 //============================================================================
1601 /*! Function : GetVariableNames
1602  *  Purpose  : 
1603  */
1604 //============================================================================
1605 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetVariableNames()
1606 {
1607   if (_closed)
1608     throw SALOMEDS::Study::StudyInvalidReference();  
1609
1610   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
1611
1612   std::vector<std::string> aVarNames = _impl->GetVariableNames();
1613
1614   int aLen = aVarNames.size();
1615   aResult->length(aLen);
1616   for (int anInd = 0; anInd < aLen; anInd++)
1617     aResult[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1618   
1619   return aResult._retn();
1620 }
1621
1622 //============================================================================
1623 /*! Function : RemoveVariable
1624  *  Purpose  : 
1625  */
1626 //============================================================================
1627 CORBA::Boolean SALOMEDS_Study_i::RemoveVariable(const char* theVarName)
1628 {
1629   if (_closed)
1630     throw SALOMEDS::Study::StudyInvalidReference();  
1631
1632   bool res = _impl->RemoveVariable(std::string(theVarName));
1633   if (res && _notifier)
1634     _notifier->modifyNB_Notification(theVarName);
1635
1636   return res;
1637 }
1638
1639 //============================================================================
1640 /*! Function : RenameVariable
1641  *  Purpose  : 
1642  */
1643 //============================================================================
1644 CORBA::Boolean SALOMEDS_Study_i::RenameVariable(const char* theVarName, const char* theNewVarName)
1645 {
1646   if (_closed)
1647     throw SALOMEDS::Study::StudyInvalidReference();  
1648
1649   bool res = _impl->RenameVariable(std::string(theVarName), std::string(theNewVarName));
1650   if (res && _notifier)
1651     _notifier->modifyNB_Notification(theVarName);
1652
1653   return res;
1654 }
1655
1656 //============================================================================
1657 /*! Function : IsVariableUsed
1658  *  Purpose  : 
1659  */
1660 //============================================================================
1661 CORBA::Boolean SALOMEDS_Study_i::IsVariableUsed(const char* theVarName)
1662 {
1663   if (_closed)
1664     throw SALOMEDS::Study::StudyInvalidReference();  
1665
1666   return _impl->IsVariableUsed(std::string(theVarName));
1667 }
1668
1669
1670 //============================================================================
1671 /*! Function : ParseVariables
1672  *  Purpose  : 
1673  */
1674 //============================================================================
1675 SALOMEDS::ListOfListOfStrings* SALOMEDS_Study_i::ParseVariables(const char* theVarName)
1676 {
1677   if (_closed)
1678     throw SALOMEDS::Study::StudyInvalidReference();  
1679
1680   SALOMEDS::ListOfListOfStrings_var aResult = new SALOMEDS::ListOfListOfStrings;
1681
1682   std::vector< std::vector<std::string> > aSections = _impl->ParseVariables(std::string(theVarName));
1683
1684   int aSectionsLen = aSections.size();
1685   aResult->length(aSectionsLen);
1686
1687   for (int aSectionInd = 0; aSectionInd < aSectionsLen; aSectionInd++) {
1688     std::vector<std::string> aVarNames = aSections[aSectionInd];
1689
1690     SALOMEDS::ListOfStrings_var aList = new SALOMEDS::ListOfStrings;
1691
1692     int aLen = aVarNames.size();
1693     aList->length(aLen);
1694
1695     for (int anInd = 0; anInd < aLen; anInd++)
1696       aList[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1697
1698     aResult[aSectionInd] = aList;
1699   }
1700
1701   return aResult._retn();
1702 }
1703
1704 //============================================================================
1705 /*! Function : GetDefaultScript
1706  *  Purpose  : 
1707  */
1708 //============================================================================
1709 char* SALOMEDS_Study_i::GetDefaultScript(const char* theModuleName, const char* theShift)
1710 {
1711   SALOMEDS::Locker lock; 
1712
1713   if (_closed)
1714     throw SALOMEDS::Study::StudyInvalidReference();  
1715
1716   std::string script = SALOMEDSImpl_IParameters::getDefaultScript(_impl, theModuleName, theShift);
1717   return CORBA::string_dup(script.c_str());
1718 }
1719
1720 //============================================================================
1721 /*! Function : EnableUseCaseAutoFilling
1722  *  Purpose  : 
1723  */
1724 //============================================================================
1725 void SALOMEDS_Study_i::EnableUseCaseAutoFilling(CORBA::Boolean isEnabled) 
1726
1727   if (_closed)
1728     throw SALOMEDS::Study::StudyInvalidReference();  
1729
1730   _impl->EnableUseCaseAutoFilling(isEnabled); 
1731   SALOMEDSImpl_StudyBuilder* builder = _builder->GetImpl();
1732   if (builder) {
1733     if (isEnabled) {
1734       builder->SetOnAddSObject(_impl->GetCallback());
1735       builder->SetOnRemoveSObject(_impl->GetCallback());
1736     }
1737     else {
1738       builder->SetOnAddSObject(NULL);
1739       builder->SetOnRemoveSObject(NULL);
1740     }
1741   }
1742 }
1743
1744
1745 CORBA::Long SALOMEDS_Study_i::getPID()
1746 {
1747 #ifdef WIN32
1748   return (CORBA::Long)_getpid();
1749 #else
1750   return (CORBA::Long)getpid();
1751 #endif
1752 }
1753
1754 void SALOMEDS_Study_i::ShutdownWithExit()
1755 {
1756   exit( EXIT_SUCCESS );
1757 }
1758
1759 void SALOMEDS_Study_i::Shutdown()
1760 {
1761   if(!CORBA::is_nil(_orb))
1762     _orb->shutdown(0);
1763 }
1764
1765 //============================================================================
1766 /*! Function : attach
1767  *  Purpose  : This function attach an observer to the study
1768  */
1769 //============================================================================
1770 void SALOMEDS_Study_i::attach(SALOMEDS::Observer_ptr theObs, CORBA::Boolean modify)
1771 {
1772   if (_notifier)
1773     static_cast<SALOMEDS::Notifier*>(_notifier)->attach(theObs, modify);
1774 }
1775
1776
1777 //============================================================================
1778 /*! Function : detach
1779  *  Purpose  : This function detaches an observer from the study
1780  */
1781 //============================================================================
1782 void SALOMEDS_Study_i::detach(SALOMEDS::Observer_ptr theObs)
1783 {
1784   if (_notifier)
1785     static_cast<SALOMEDS::Notifier*>(_notifier)->detach(theObs);
1786 }
1787
1788 //===========================================================================
1789 //   PRIVATE FUNCTIONS
1790 //===========================================================================
1791 CORBA::LongLong SALOMEDS_Study_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
1792 {
1793 #ifdef WIN32
1794   long pid = (long)_getpid();
1795 #else
1796   long pid = (long)getpid();
1797 #endif  
1798   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
1799   return reinterpret_cast<CORBA::LongLong>(_impl);
1800 }
1801
1802 void SALOMEDS_Study_i::NameChanged()
1803 {
1804   // Notify GUI that the name of study was changed
1805   SALOME_NamingService *aNamingService = KERNEL::getNamingService();
1806   CORBA::Object_var obj = aNamingService->Resolve("/Kernel/Session");
1807   SALOME::Session_var aSession = SALOME::Session::_narrow(obj);
1808   if ( !CORBA::is_nil(aSession) ) {
1809     std::stringstream ss;
1810     ss << "studyNameChanged";
1811     std::string str = ss.str();
1812     SALOMEDS::unlock();
1813     aSession->emitMessageOneWay(str.c_str());
1814     SALOMEDS::lock();
1815   }
1816 }