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