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