Salome HOME
31d8a6af97315e0c67fc47892020256aeb53053e
[modules/visu.git] / src / VISU_I / VISUConfig.cc
1 //  VISU OBJECT : interactive object for VISU entities implementation
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //  File   : VISUConfig.cc
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISUConfig.hh"
28
29 #include "SUIT_ResourceMgr.h"
30 #include "SUIT_Session.h"
31
32 #include "SALOMEDSClient_GenericAttribute.hxx"
33 #include "SALOMEDSClient_AttributeIOR.hxx"
34
35 #include "SalomeApp_Application.h"
36 #include "SalomeApp_Study.h"
37
38 #include "SALOME_Event.hxx"
39
40 #include <qregexp.h>
41
42 #include <fstream>
43
44 #ifdef _DEBUG_
45 static int MYDEBUG = 0;
46 #else
47 static int MYDEBUG = 0;
48 #endif
49
50
51 namespace VISU
52 {
53
54   SUIT_Session*
55   GetSession()
56   {
57     return SUIT_Session::session();
58   }
59
60   SUIT_ResourceMgr*
61   GetResourceMgr()
62   {
63     return GetSession()->resourceMgr();
64   }
65
66   //---------------------------------------------------------------------------
67
68   QMutex* Base_i::myMutex = NULL; //apo - &VISUMutex;
69   CORBA::ORB_var Base_i::myOrb;
70   PortableServer::POA_var Base_i::myPoa;
71   SALOME_NamingService* Base_i::myNamingService;
72   SALOME_LifeCycleCORBA* Base_i::myEnginesLifeCycle;
73   VISU_Gen_i* Base_i::myVisuGenImpl;
74
75   Base_i::Base_i() {}
76
77   Base_i::~Base_i() {}
78
79   char* Base_i::GetID(){
80     if(myID == ""){
81       CORBA::Object_var anObject = _this();
82       CORBA::String_var anIOR = myOrb->object_to_string(anObject);
83       myID = anIOR.in();
84     }
85     return CORBA::string_dup(myID.c_str());
86   }
87
88   //---------------------------------------------------------------------------
89   static int mySCnt = 0;
90   static QMutex aMutex(TRUE);
91
92   Mutex::Mutex(QMutex* theMutex): myMutex(&aMutex)
93   {
94     if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<!mySCnt);
95     if(!mySCnt++)
96       myMutex->lock();
97   }
98
99   Mutex::~Mutex()
100   {
101     if(!(--mySCnt))
102       myMutex->unlock();
103     if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<!mySCnt);
104   }
105
106
107   //---------------------------------------------------------------------------
108   Storable::TStorableEngineMap Storable::myStorableEngineMap;
109
110   std::string 
111   Storable
112   ::ToString()
113   {
114     ostringstream anOutputStream;
115     Storable::DataToStream( anOutputStream, "myComment", GetComment() );
116     ToStream(anOutputStream);
117     anOutputStream<<ends;
118     if(MYDEBUG) MESSAGE("Storable::ToString - "<<anOutputStream.str());
119     return anOutputStream.str();
120   }
121
122
123   //---------------------------------------------------------------
124   CORBA::Boolean 
125   Storable
126   ::CanCopy(SALOMEDS::SObject_ptr theObject) 
127   {
128     return false;
129   }
130
131
132   //---------------------------------------------------------------
133   bool 
134   Storable
135   ::CopyFrom(SALOMEDS::SObject_ptr theObject, 
136              CORBA::Long& theObjectID,
137              const std::string& theTmpDir,
138              TFileNames& theFileNames)
139   {
140     TFileName aFileName("copy_persistent");
141     std::string aCopyPersist =  theTmpDir + aFileName;
142     std::ofstream anOutputFileStream(aCopyPersist.c_str(), ios::out);
143     anOutputFileStream<<ToString()<<endl;
144
145     theFileNames.push_back(aFileName);
146     theObjectID = 1;
147
148     return true;
149   }
150
151
152   //---------------------------------------------------------------
153   void 
154   Storable
155   ::RegistryStorableEngine(const std::string& theComment, 
156                            TStorableEngine theEngine)
157   {
158     if(!myStorableEngineMap.insert(TStorableEngineMap::value_type(theComment, theEngine)).second)
159       throw std::logic_error("Storable::Registry >> dupliacte registring !!!");
160   }
161
162
163   //---------------------------------------------------------------
164   Storable* 
165   Storable
166   ::Create(SALOMEDS::SObject_ptr theSObject,
167            const std::string& theLocalPersistentID, 
168            const std::string& thePrefix,
169            CORBA::Boolean theIsMultiFile)
170   {
171     try{
172       QString aString( CorrectPersistentString( theLocalPersistentID ) );
173       TRestoringMap aRestoringMap;
174       StringToMap(aString, aRestoringMap);
175       bool anIsExists;
176       QString aComment = VISU::Storable::FindValue(aRestoringMap, "myComment", &anIsExists);
177       if ( anIsExists ) {
178         TStorableEngineMap::const_iterator anIter = myStorableEngineMap.find(aComment.latin1());
179         if(MYDEBUG) MESSAGE("Storable::Create - "<<aComment.latin1()<<" "<<(anIter != myStorableEngineMap.end()));
180         if(anIter == myStorableEngineMap.end()) 
181           return NULL;
182         TStorableEngine aStorableEngine = anIter->second;
183         return aStorableEngine(theSObject, aRestoringMap, thePrefix, theIsMultiFile);
184       }
185     }catch(std::exception& exc){
186       INFOS("Follow exception was occured :\n"<<exc.what());
187     }catch(...){
188       INFOS("Unknown exception was occured!");
189     }
190     return NULL;
191   }
192
193
194   //---------------------------------------------------------------
195   VISU::VISUType
196   Storable
197   ::Comment2Type(const std::string& theComment)
198   {
199     if ( theComment == "CURVE" ) 
200       return VISU::TCURVE;
201     if ( theComment == "TABLE" ) 
202       return VISU::TTABLE;
203     if ( theComment == "CONTAINER" ) 
204       return VISU::TCONTAINER;
205     if ( theComment == "MESH" ) 
206       return VISU::TMESH;
207     if ( theComment == "SCALARMAP" ) 
208       return VISU::TSCALARMAP;
209     if ( theComment == "PRSMERGER" ) 
210       return VISU::TSCALARMAP;
211     if ( theComment == "ISOSURFACE" ) 
212       return VISU::TISOSURFACES;
213     if ( theComment == "DEFORMEDSHAPE" ) 
214       return VISU::TDEFORMEDSHAPE;
215     if ( theComment == "SCALARMAPONDEFORMEDSHAPE" ) 
216       return VISU::TSCALARMAPONDEFORMEDSHAPE;
217     if ( theComment == "GAUSSPOINTS" ) 
218       return VISU::TGAUSSPOINTS;
219     if ( theComment == "PLOT3D" ) 
220       return VISU::TPLOT3D;
221     if ( theComment == "CUTPLANES" ) 
222       return VISU::TCUTPLANES;
223     if ( theComment == "CUTLINES" ) 
224       return VISU::TCUTLINES;
225     if ( theComment == "VECTORS" ) 
226       return VISU::TVECTORS;
227     if ( theComment == "STREAMLINES" ) 
228       return VISU::TSTREAMLINES;
229     if ( theComment == "VISUGEN" ) 
230       return VISU::TVISUGEN;
231     if ( theComment == "VIEWMANAGER" ) 
232       return VISU::TVIEWMANAGER;
233     if ( theComment == "RESULT" ) 
234       return VISU::TRESULT;
235     if ( theComment == "XYPLOT" ) 
236       return VISU::TXYPLOT;
237     if ( theComment == "TABLEVIEW" ) 
238       return VISU::TTABLEVIEW;
239     if ( theComment == "VIEW3D" ) 
240       return VISU::TVIEW3D;
241     if ( theComment == "GAUSSVIEW" ) 
242       return VISU::TGAUSSVIEW;
243     if ( theComment == "ENTITY" ) 
244       return VISU::TENTITY;
245     if ( theComment == "FAMILY" ) 
246       return VISU::TFAMILY;
247     if ( theComment == "PART" ) 
248       return VISU::TPART; // MULTIPR
249     if ( theComment == "GROUP" ) 
250       return VISU::TGROUP;
251     if ( theComment == "FIELD" ) 
252       return VISU::TFIELD;
253     if ( theComment == "TIMESTAMP" ) 
254       return VISU::TTIMESTAMP;
255     if ( theComment == "ANIMATION" ) 
256       return VISU::TANIMATION;
257     if ( theComment == "ALL" ) 
258       return VISU::TALL;
259     
260     return VISU::TNONE;
261   }
262   
263
264   //---------------------------------------------------------------
265   VISU::VISUType
266   Storable
267   ::RestoringMap2Type(const TRestoringMap& theRestoringMap)
268   {
269     QString aComment = Storable::FindValue( theRestoringMap, "myComment", "" );
270     return Comment2Type(aComment.latin1());
271   }
272
273
274   //---------------------------------------------------------------
275   VISU::VISUType
276   Storable
277   ::Stream2Type(const std::string& thePersistentString)
278   {
279     QString aString(thePersistentString.c_str());
280     if(aString.isEmpty())
281       return VISU::TNONE;
282
283     VISU::Storable::TRestoringMap aRestoringMap;
284     Storable::StringToMap( aString, aRestoringMap );
285     return RestoringMap2Type( aRestoringMap );    
286   }
287
288
289   //---------------------------------------------------------------
290   VISU::VISUType
291   Storable
292   ::SObject2Type(const _PTR(SObject)& theSObject)
293   {
294     _PTR(GenericAttribute) anAttr;
295     if (theSObject->FindAttribute(anAttr, "AttributeString")) {
296       _PTR(AttributeString) aDataAttr(anAttr);
297       std::string aData = aDataAttr->Value();
298       return Stream2Type(aData);
299     }
300
301     return VISU::TNONE;
302   }
303
304
305   //---------------------------------------------------------------
306   std::string 
307   Storable
308   ::CorrectPersistentString(const std::string& thePersistentString)
309   {
310     QString aString(thePersistentString.c_str());
311     if(aString.isEmpty())
312       return aString.latin1();
313
314     VISU::Storable::TRestoringMap aRestoringMap;
315     Storable::StringToMap( aString, aRestoringMap );
316           
317     bool anIsFound = false;
318     QString aComment = Storable::FindValue( aRestoringMap, "myComment", &anIsFound );
319     if ( anIsFound ) {
320       if ( aComment == "PRSMERGER" )
321         aString.replace( "myComment=PRSMERGER", "myComment=SCALARMAP" );
322     }
323
324     return aString.latin1();
325   }
326
327
328   //---------------------------------------------------------------
329   std::string
330   Storable
331   ::FindEntry(SALOMEDS::Study_ptr theStudyDocument, 
332               const std::string& theStartEntry,
333               const TRestoringMap& theRestoringMap, 
334               int IsAllLevels)
335   {
336     SALOMEDS::SObject_var aSObject = theStudyDocument->FindObjectID(theStartEntry.c_str());
337     SALOMEDS::ChildIterator_var anIter = theStudyDocument->NewChildIterator(aSObject);
338     anIter->InitEx(IsAllLevels);
339     for(; anIter->More(); anIter->Next()) {
340       aSObject = anIter->Value();
341       SALOMEDS::GenericAttribute_var anAttr;
342       if (aSObject->FindAttribute(anAttr,"AttributeString")) {
343         SALOMEDS::AttributeString_var aDataAttr = SALOMEDS::AttributeString::_narrow(anAttr);
344         CORBA::String_var aString = aDataAttr->Value();
345         VISU::Storable::TRestoringMap aRestoringMap;
346         Storable::StringToMap( aString.in(), aRestoringMap );
347         bool anIsSame = true;
348         TRestoringMap::const_iterator anIter = theRestoringMap.begin();
349         for(; anIter != theRestoringMap.end(); anIter++){
350           const TRestoringMap::key_type& aKey = anIter->first;
351           TRestoringMap::const_iterator anIter2 = aRestoringMap.find(aKey);
352           if(anIter2 != aRestoringMap.end()){
353             if (anIter->second == anIter2->second)
354               continue;
355           }
356           anIsSame = false;
357           break;
358         }
359         if(anIsSame){
360           CORBA::String_var anEntry = aSObject->GetID();
361           return anEntry.in();
362         }
363       }
364     }
365     return "";
366   }
367
368
369   //----------------------------------------------------------------------------
370   void 
371   Storable
372   ::StringToMap(const QString& theString, 
373                 TRestoringMap& theMap)
374   {
375     if(0 && MYDEBUG) MESSAGE("Storable::StringToMap : string="<<theString);
376     QStringList strList = QStringList::split( ";", theString, false );
377     for ( int i = 0; i < strList.count(); i++ ) {
378       QString next = strList[ i ];
379       int pos = next.find("=");
380       QString aName, aValue;
381       if ( pos < 0 ) {
382         aName = next.stripWhiteSpace();
383         aValue = "";
384       }
385       else {
386         aName = next.left( pos ).stripWhiteSpace();
387         aValue = next.mid( pos+1 );
388         if ( aValue.isNull() )
389           aValue = "";
390       }
391       if ( !aName.isEmpty() )
392         theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );
393     }
394   }
395
396
397   //----------------------------------------------------------------------------
398   Storable::TRestoringMap
399   Storable
400   ::GetStorableMap(_PTR(SObject) theSObject)
401   {
402     Storable::TRestoringMap aRestoringMap;
403     if(theSObject){
404       _PTR(GenericAttribute) anAttr;
405       if(theSObject->FindAttribute(anAttr,"AttributeString")){
406         _PTR(AttributeString) aComment (anAttr);
407         std::string aValue = aComment->Value();
408         VISU::Storable::StringToMap(aValue.c_str(), aRestoringMap);
409       }
410     }
411     return aRestoringMap;
412   }
413
414
415   //----------------------------------------------------------------------------
416   void
417   Storable
418   ::DataToStream(std::ostringstream& theStr, 
419                  const QString& theName, 
420                  const QString& theVal) 
421   {
422     QString output = ( !theName.isNull() ? theName : QString("") )
423                    + QString( "=" )
424                    + ( !theVal.isNull()  ? theVal  : QString("") );
425     theStr<<output.latin1()<<";";
426   }
427
428
429   //---------------------------------------------------------------
430   void 
431   Storable
432   ::DataToStream(std::ostringstream& theStr, 
433                  const QString& theName, 
434                  int theVal) 
435   {
436     QString output = ( !theName.isNull() ? theName : QString("") )
437                    + QString( "=" )
438                    + QString::number( theVal );
439     theStr<<output.latin1()<<";";
440   }
441
442
443   //---------------------------------------------------------------
444   void 
445   Storable
446   ::DataToStream(std::ostringstream& theStr, 
447                  const QString& theName, 
448                  long theVal) 
449   {
450     QString output = ( !theName.isNull() ? theName : QString("") )
451                    + QString( "=" )
452                    + QString::number( theVal );
453     theStr<<output.latin1()<<";";
454   }
455
456
457   //---------------------------------------------------------------
458   void
459   Storable
460   ::DataToStream(std::ostringstream& theStr, 
461                  const QString& theName, 
462                  double theVal) 
463   {
464     QString output = ( !theName.isNull() ? theName : QString("") )
465                    + QString( "=" )
466                    + QString::number( theVal );
467     theStr<<output.latin1()<<";";
468   }
469
470
471   //---------------------------------------------------------------
472   QString
473   Storable
474   ::FindValue(const TRestoringMap& theMap, 
475               const std::string& theArg, 
476               bool* theIsFind)
477   {
478     TRestoringMap::const_iterator anIter = theMap.find(theArg);
479     if(anIter == theMap.end()) {
480       if(MYDEBUG) MESSAGE("Storable::Init >> there is no value for "<<theArg);
481       if(theIsFind != NULL) 
482         *theIsFind = false;
483       //throw std::logic_error(string("Storable::Init >> there is no value for ") + theArg);
484       static QString BAD_VALUE("NULL");
485       return BAD_VALUE;
486     }
487     if(theIsFind != NULL) 
488       *theIsFind = true;
489     return anIter->second;
490   }
491
492
493   //---------------------------------------------------------------
494   QString
495   Storable
496   ::FindValue(const TRestoringMap& theMap, 
497               const std::string& theArg, 
498               const QString& theDefaultValue)
499   {
500     bool anIsFound = false;
501     QString aValue = FindValue(theMap,theArg,&anIsFound);
502     if(anIsFound)
503       return aValue;
504     return theDefaultValue;
505   }
506
507
508   //---------------------------------------------------------------------------
509   PortableServer::ServantBase_var
510   GetServant(CORBA::Object_ptr theObject)
511   {
512     if(CORBA::is_nil(theObject))  
513       return NULL;
514     try{
515       PortableServer::POA_ptr aPOA = Base_i::GetPOA();
516       PortableServer::Servant aServant = aPOA->reference_to_servant(theObject);
517       return aServant;
518     } catch (...) {
519       INFOS("GetServant - Unknown exception was occured!!!");
520       return NULL;
521     }
522   }
523
524
525   //---------------------------------------------------------------------------
526   CORBA::Object_var 
527   SObjectToObject(SALOMEDS::SObject_ptr theSObject)
528   {
529     SALOMEDS::GenericAttribute_var anAttr;
530     CORBA::Object_var anObject;
531     if(CORBA::is_nil(theSObject))
532       return anObject;
533     try{
534       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
535         SALOMEDS::AttributeIOR_var anIOR  = SALOMEDS::AttributeIOR::_narrow(anAttr);
536         CORBA::String_var aValue = anIOR->Value();
537         CORBA::ORB_ptr anORB = Base_i::GetORB();
538         if(strcmp(aValue,"") != 0)
539           anObject = anORB->string_to_object(aValue);
540       }
541     }catch(...){
542       INFOS("SObjectToObject - Unknown exception was occured!!!");
543     }
544     return anObject;
545   }
546
547   //---------------------------------------------------------------------------
548   CORBA::Object_var
549   ClientSObjectToObject(_PTR(SObject) theSObject)
550   {
551     _PTR(GenericAttribute) anAttr;
552     CORBA::Object_var anObject;
553     if(!theSObject)
554       return anObject;
555     try{
556       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
557         _PTR(AttributeIOR) anIOR  = anAttr;
558         CORBA::String_var aValue = anIOR->Value().c_str();
559         CORBA::ORB_ptr anORB = Base_i::GetORB();
560         if(strcmp(aValue,"") != 0)
561           anObject = anORB->string_to_object(aValue);
562       }
563     }catch(...){
564       INFOS("ClientSObjectToObject - Unknown exception was occured!!!");
565     }
566     return anObject;
567   }
568
569
570   //---------------------------------------------------------------------------
571   std::string
572   CreateAttributes(SALOMEDS::Study_ptr theStudyDocument,
573                    const std::string& theFatherEntry, 
574                    const std::string& theIconName,
575                    const std::string& theIOR, 
576                    const std::string& theName,
577                    const std::string& thePersistentRef, 
578                    const std::string& theComment,
579                    CORBA::Boolean theCreateNew)
580   {
581     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
582     SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry.c_str());
583     SALOMEDS::SObject_var aNewSObject;
584     if (theCreateNew) 
585       aNewSObject = aStudyBuilder->NewObject(aFather);
586     else
587       aNewSObject = aFather;
588     SALOMEDS::GenericAttribute_var anAttr;
589     if(theIOR != NO_IOR){
590       anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSObject, "AttributeIOR");
591       SALOMEDS::AttributeIOR_var aCustomAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
592       aCustomAttr->SetValue(theIOR.c_str());
593     }
594     if(theName != NO_NAME){
595       anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSObject, "AttributeName");
596       SALOMEDS::AttributeName_var aCustomAttr = SALOMEDS::AttributeName::_narrow(anAttr);
597       aCustomAttr->SetValue(theName.c_str());
598     }
599     if(thePersistentRef != NO_PERFSITENT_REF){
600       anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSObject, "AttributePersistentRef");
601       SALOMEDS::AttributePersistentRef_var aCustomAttr = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
602       aCustomAttr->SetValue(thePersistentRef.c_str());
603     }
604     if(theComment != NO_COMMENT){
605       anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSObject, "AttributeString");
606       SALOMEDS::AttributeString_var aCustomAttr = SALOMEDS::AttributeString::_narrow(anAttr);
607       aCustomAttr->SetValue(theComment.c_str());
608     }
609     if(theIconName != NO_ICON){
610       anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSObject, "AttributePixMap");
611       SALOMEDS::AttributePixMap_var aCustomAttr = SALOMEDS::AttributePixMap::_narrow(anAttr);
612       aCustomAttr->SetPixMap(theIconName.c_str());
613     }
614     CORBA::String_var anEntry = aNewSObject->GetID();
615     std::string aRet(anEntry);
616     if(MYDEBUG)
617       INFOS("CreateAttributes - StudyId = "<<theStudyDocument->StudyId()<<"; anEntry = "<<aRet<<"; IOR = '"<<theIOR<<"'");
618     return aRet;
619   }
620   
621   std::string
622   CreateAttributes(_PTR(Study) theStudyDocument,
623                    const std::string& theFatherEntry, 
624                    const std::string& theIconName,
625                    const std::string& theIOR, 
626                    const std::string& theName,
627                    const std::string& thePersistentRef, 
628                    const std::string& theComment,
629                    CORBA::Boolean theCreateNew)
630   {
631     _PTR(StudyBuilder) aStudyBuilder = theStudyDocument->NewBuilder();
632     _PTR(SObject) aFather = theStudyDocument->FindObjectID(theFatherEntry);
633     _PTR(SObject) aNewSObject;
634     if (theCreateNew)
635     {
636       aNewSObject = aStudyBuilder->NewObject(aFather);
637     }
638     else
639       aNewSObject = aFather;
640
641     _PTR(GenericAttribute) anAttr;
642     if (theIOR != NO_IOR) {
643       anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSObject, "AttributeIOR");
644       _PTR(AttributeIOR) aCustomAttr (anAttr);
645       aCustomAttr->SetValue(theIOR);
646     }
647     if (theName != NO_NAME) {
648       anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSObject, "AttributeName");
649       _PTR(AttributeName) aCustomAttr (anAttr);
650       aCustomAttr->SetValue(theName);
651     }
652     if (thePersistentRef != NO_PERFSITENT_REF) {
653       anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSObject, "AttributePersistentRef");
654       _PTR(AttributePersistentRef) aCustomAttr (anAttr);
655       aCustomAttr->SetValue(thePersistentRef);
656     }
657     if (theComment != NO_COMMENT) {
658       anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSObject, "AttributeString");
659       _PTR(AttributeString) aCustomAttr (anAttr);
660       aCustomAttr->SetValue(theComment);
661     }
662     if (theIconName != NO_ICON) {
663       anAttr = aStudyBuilder->FindOrCreateAttribute(aNewSObject, "AttributePixMap");
664       _PTR(AttributePixMap) aCustomAttr (anAttr);
665       aCustomAttr->SetPixMap(theIconName);
666     }
667     std::string aRet = aNewSObject->GetID();
668     if (MYDEBUG)
669       INFOS("CreateAttributes - StudyId = " << theStudyDocument->StudyId()
670             << "; anEntry = " << aRet << "; IOR = '" << theIOR << "'");
671     return aRet;
672   }
673
674
675   //---------------------------------------------------------------
676   QString 
677   GenerateName(const std::string& theFmt, int theId)
678   {
679     QString aName;
680     if(theId > 0)
681       aName.sprintf("%s:%d", theFmt.c_str(), theId);
682     else
683       aName.sprintf("%s", theFmt.c_str());
684     return aName;
685   }
686
687
688   //---------------------------------------------------------------
689   SALOMEDS::StudyManager_var 
690   GetStudyManager()
691   {
692     static SALOMEDS::StudyManager_var aStudyManager;
693     if(CORBA::is_nil(aStudyManager)){
694       SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
695       CORBA::Object_var anObject = aNamingService->Resolve("/myStudyManager");
696       aStudyManager = SALOMEDS::StudyManager::_narrow(anObject);
697     }
698     return aStudyManager;
699   }
700
701
702   //---------------------------------------------------------------
703   SALOMEDS::Study_var 
704   GetDSStudy(_PTR(Study) theStudy)
705   {
706     int aStudyID = theStudy->StudyId();
707     return GetStudyManager()->GetStudyByID(aStudyID);
708   }
709
710
711   //---------------------------------------------------------------
712   struct TGetStudyEvent: public SALOME_Event
713   {
714     SALOMEDS::Study_var myStudy;
715     
716     typedef SalomeApp_Study* TResult;
717     TResult myResult;
718     
719     TGetStudyEvent(SALOMEDS::Study_ptr theStudy):
720       myStudy(SALOMEDS::Study::_duplicate(theStudy)),
721       myResult(NULL)
722     {}
723     
724     virtual
725     void
726     Execute()
727     {
728       if(CORBA::is_nil(myStudy))
729         return;
730       
731       int aStudyId = myStudy->StudyId();
732       SUIT_Session* aSession = SUIT_Session::session();
733       QPtrList<SUIT_Application> anApplications = aSession->applications();
734       QPtrListIterator<SUIT_Application> anIter (anApplications);
735       while (SUIT_Application* anApp = anIter.current()) {
736         ++anIter;
737         if (SUIT_Study* aSStudy = anApp->activeStudy()) {
738           if (SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(aSStudy)) {
739             if (_PTR(Study) aCStudy = aStudy->studyDS()) {
740               if (aStudyId == aCStudy->StudyId()) {
741                 myResult = aStudy;
742                 break;
743               }
744             }
745           }
746         }
747       }
748     }
749   };
750   
751
752   //---------------------------------------------------------------
753   SalomeApp_Study* 
754   GetGUIStudy(SALOMEDS::Study_ptr theStudy)
755   {
756     return ProcessEvent(new TGetStudyEvent(theStudy));
757   }
758
759
760   //---------------------------------------------------------------
761   _PTR(Study) 
762   GetStudy(SALOMEDS::Study_ptr theStudy)
763   {
764     if(SalomeApp_Study* aStudy = ProcessEvent(new TGetStudyEvent(theStudy)))
765       return aStudy->studyDS();
766     
767     return _PTR(Study)();
768   }
769
770
771   //---------------------------------------------------------------
772   SALOMEDS::SObject_var 
773   GetSObject( _PTR(SObject) obj )
774   {
775     _PTR(Study) aStudy = obj->GetStudy();
776     SALOMEDS::Study_var aSalomeDSStudy = GetDSStudy( aStudy );
777     std::string id = obj->GetID();
778     return aSalomeDSStudy->FindObjectID( id.c_str() );
779   }
780
781   //---------------------------------------------------------------
782   _PTR(SObject) 
783   GetClientSObject(SALOMEDS::SObject_ptr theSObject,
784                    _PTR(Study) theStudy)
785   {
786     CORBA::String_var anEntry = theSObject->GetID();
787     return theStudy->FindObjectID( anEntry.in() );
788   }
789
790
791   //---------------------------------------------------------------
792   void
793   RemoveFromStudy (SALOMEDS::SObject_ptr theSObject,
794                    bool theIsAttrOnly,
795                    bool theDestroySubObjects)
796   {
797     if (theSObject->_is_nil()) return;
798
799     SALOMEDS::Study_var aStudyDocument = theSObject->GetStudy();
800     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudyDocument->NewBuilder();
801     if (theIsAttrOnly) {
802       aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
803       return;
804     }
805
806     // Remove possible sub-objects
807     SALOMEDS::ChildIterator_var aChildIter = aStudyDocument->NewChildIterator(theSObject);
808     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
809       SALOMEDS::SObject_var aChildSObject = aChildIter->Value();
810       CORBA::Object_var aChildObj = VISU::SObjectToObject(aChildSObject);
811       if (CORBA::is_nil(aChildObj)) continue;
812
813       VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
814       if (CORBA::is_nil(aRemovableObject)) continue;
815
816       aRemovableObject->RemoveFromStudy();
817     }
818
819     // asl, fix for PAL10455: Remove references to SObject
820     SALOMEDS::Study::ListOfSObject_var aRefs = aStudyDocument->FindDependances( theSObject );
821     for( int i=0, n=aRefs->length(); i<n; i++ )
822     {
823       SALOMEDS::SObject_var o = aRefs[i];
824       if( o->GetFatherComponent()->ComponentDataType()==theSObject->GetFatherComponent()->ComponentDataType() )
825       {
826         aStudyBuilder->RemoveReference( o );
827         aStudyBuilder->RemoveObjectWithChildren( o );
828       }
829     }
830
831     // Remove the SObject itself
832     aStudyBuilder->RemoveObjectWithChildren(theSObject);
833   }
834
835
836   //---------------------------------------------------------------
837   void
838   RemoveFromStudy (_PTR(SObject) theSObject,
839                    bool theIsAttrOnly,
840                    bool theDestroySubObjects)
841   {
842     if (!theSObject) return;
843
844     _PTR(Study) aStudyDocument = theSObject->GetStudy();
845     _PTR(StudyBuilder) aStudyBuilder = aStudyDocument->NewBuilder();
846     if (theIsAttrOnly) {
847       aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
848       return;
849     }
850
851     // Remove possible sub-objects
852     _PTR(ChildIterator) aChildIter = aStudyDocument->NewChildIterator(theSObject);
853     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
854       _PTR(SObject) aChildSObject = aChildIter->Value();
855       CORBA::Object_var aChildObj = VISU::ClientSObjectToObject(aChildSObject);
856       if (CORBA::is_nil(aChildObj)) 
857         continue;
858
859       VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
860       if (CORBA::is_nil(aRemovableObject))
861         continue;
862
863       aRemovableObject->RemoveFromStudy();
864     }
865
866     // Remove the SObject itself
867     aStudyBuilder->RemoveObjectWithChildren(theSObject);
868   }
869 }