Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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
37 using namespace std;
38
39 #ifdef _DEBUG_
40 static int MYDEBUG = 0;
41 #else
42 static int MYDEBUG = 0;
43 #endif
44
45 namespace VISU{
46
47   SUIT_Session*
48   GetSession()
49   {
50     return SUIT_Session::session();
51   }
52
53   SUIT_ResourceMgr*
54   GetResourceMgr()
55   {
56     return GetSession()->resourceMgr();
57   }
58
59   //===========================================================================
60
61   QMutex* Base_i::myMutex = NULL; //apo - &VISUMutex;
62   CORBA::ORB_var Base_i::myOrb;
63   PortableServer::POA_var Base_i::myPoa;
64   SALOME_NamingService* Base_i::myNamingService;
65   SALOME_LifeCycleCORBA* Base_i::myEnginesLifeCycle;
66   VISU_Gen_i* Base_i::myVisuGenImpl;
67
68   Base_i::Base_i() {}
69
70   Base_i::~Base_i() {}
71
72   char* Base_i::GetID(){
73     if(myID == ""){
74       CORBA::Object_var anObject = _this();
75       CORBA::String_var anIOR = myOrb->object_to_string(anObject);
76       myID = anIOR.in();
77     }
78     return CORBA::string_dup(myID.c_str());
79   }
80
81   //===========================================================================
82   static int mySCnt = 0;
83   static QMutex aMutex(TRUE);
84
85   Mutex::Mutex(QMutex* theMutex): myMutex(&aMutex){
86     if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<!mySCnt);
87     if(!mySCnt++)
88       myMutex->lock();
89   }
90
91   Mutex::~Mutex(){
92     if(!(--mySCnt))
93       myMutex->unlock();
94     if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<!mySCnt);
95   }
96
97
98   //===========================================================================
99   static Storable::TCallbackMap VisuStoreMap;
100   string Storable::ToString(){
101     ostringstream strOut;
102     Storable::DataToStream( strOut, "myComment", GetComment() );
103     ToStream(strOut);
104     strOut<<ends;
105     if(MYDEBUG) MESSAGE("Storable::ToString - "<<strOut.str());
106     return strOut.str();
107   }
108
109   void Storable::Registry(const char* theComment, TStorableEngine theEngine)
110   {
111     if(!VisuStoreMap.insert(TCallbackMap::value_type(theComment,theEngine)).second){
112       if(MYDEBUG) MESSAGE("Storable::Registry >> dupliacte registring !!!");
113       throw std::logic_error("Storable::Registry >> dupliacte registring !!!");
114     }
115   }
116
117   void Storable::StrToMap(const QString& theStr, VISU::Storable::TRestoringMap& theMap){
118     if(0 && MYDEBUG) MESSAGE("Storable::StrToMap : string="<<theStr);
119     QStringList strList = QStringList::split( ";", theStr, false );
120     for ( int i = 0; i < strList.count(); i++ ) {
121       QString next = strList[ i ];
122       int pos = next.find("=");
123       QString aName, aValue;
124       if ( pos < 0 ) {
125         aName = next.stripWhiteSpace();
126         aValue = "";
127       }
128       else {
129         aName = next.left( pos ).stripWhiteSpace();
130         aValue = next.mid( pos+1 );
131         if ( aValue.isNull() )
132           aValue = "";
133       }
134       if ( !aName.isEmpty() )
135         theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );
136     }
137   }
138
139   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const QString& theVal) {
140     QString output = ( !theName.isNull() ? theName : QString("") )
141                    + QString( "=" )
142                    + ( !theVal.isNull()  ? theVal  : QString("") );
143     theStr<<output.latin1()<<";";
144   }
145
146   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const int theVal) {
147     QString output = ( !theName.isNull() ? theName : QString("") )
148                    + QString( "=" )
149                    + QString::number( theVal );
150     theStr<<output.latin1()<<";";
151   }
152
153   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const double theVal) {
154     QString output = ( !theName.isNull() ? theName : QString("") )
155                    + QString( "=" )
156                    + QString::number( theVal );
157     theStr<<output.latin1()<<";";
158   }
159   Storable* Storable::Create(SALOMEDS::SObject_ptr theSObject,
160                              const string& thePrefix, const string& theLocalPersistentID)
161   {
162     try{
163       QString strIn( theLocalPersistentID.c_str() );
164       TRestoringMap aMap;
165       StrToMap(strIn,aMap);
166       bool isExist;
167       QString aComment = VISU::Storable::FindValue(aMap,"myComment",&isExist);
168       if ( isExist ) {
169         TCallbackMap::const_iterator i = VisuStoreMap.find(aComment.latin1());
170         if(MYDEBUG) MESSAGE("Storable::Create - "<<aComment.latin1()<<" "<<(i != VisuStoreMap.end()));
171         if(i == VisuStoreMap.end()) return NULL;
172         return (i->second)(theSObject,thePrefix,aMap);
173       }
174     }catch(std::logic_error& exc){
175       INFOS("Follow exception was occured :\n"<<exc.what());
176     }catch(...){
177       INFOS("Unknown exception was occured!");
178     }
179     return NULL;
180   }
181
182   QString Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
183   {
184     TRestoringMap::const_iterator i = theMap.find(theArg);
185     if(i == theMap.end()) {
186       if(MYDEBUG) MESSAGE("Storable::Init >> there is no value for "<<theArg);
187       if(isFind != NULL) *isFind = false;
188       //throw std::logic_error(string("Storable::Init >> there is no value for ") + theArg);
189       static QString BAD_VALUE("NULL");
190       return BAD_VALUE;
191     }
192     if(isFind != NULL) *isFind = true;
193     return i->second;
194   }
195
196
197   QString Storable::FindValue(const TRestoringMap& theMap, const string& theArg, const QString& theDefaultValue)
198   {
199     bool anIsFound = false;
200     QString aValue = FindValue(theMap,theArg,&anIsFound);
201     if(anIsFound)
202       return aValue;
203     return theDefaultValue;
204   }
205
206
207   //===========================================================================
208   PortableServer::ServantBase_var GetServant(CORBA::Object_ptr theObject){
209     if(CORBA::is_nil(theObject))  return NULL;
210     try{
211       PortableServer::POA_ptr aPOA = Base_i::GetPOA();
212       PortableServer::Servant aServant = aPOA->reference_to_servant(theObject);
213       return aServant;
214     } catch (...) {
215       INFOS("GetServant - Unknown exception was occured!!!");
216       return NULL;
217     }
218   }
219
220
221   //===========================================================================
222   CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
223     SALOMEDS::GenericAttribute_var anAttr;
224     CORBA::Object_var anObj;
225     try{
226       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
227         SALOMEDS::AttributeIOR_var anIOR  = SALOMEDS::AttributeIOR::_narrow(anAttr);
228         CORBA::String_var aValue = anIOR->Value();
229         CORBA::ORB_ptr anORB = Base_i::GetORB();
230         if(strcmp(aValue,"") != 0)
231           anObj = anORB->string_to_object(aValue);
232       }
233     }catch(...){
234       INFOS("SObjectToObject - Unknown exception was occured!!!");
235     }
236     return anObj;
237   }
238
239   //===========================================================================
240   CORBA::Object_var ClientSObjectToObject(_PTR(SObject) theSObject){
241     _PTR(GenericAttribute) anAttr;
242     CORBA::Object_var anObj;
243     try{
244       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
245         _PTR(AttributeIOR) anIOR  = anAttr;
246         CORBA::String_var aValue = anIOR->Value().c_str();
247         CORBA::ORB_ptr anORB = Base_i::GetORB();
248         if(strcmp(aValue,"") != 0)
249           anObj = anORB->string_to_object(aValue);
250       }
251     }catch(...){
252       INFOS("ClientSObjectToObject - Unknown exception was occured!!!");
253     }
254     return anObj;
255   }
256
257
258   //===========================================================================
259   string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
260                               const char* theComment, int IsAllLevels)
261   {
262     SALOMEDS::ChildIterator_var anIter =
263       theStudyDocument->NewChildIterator(theStudyDocument->FindObjectID(theStartEntry));
264     anIter->InitEx(IsAllLevels);
265     SALOMEDS::SObject_var aFieldSO;
266     for(;anIter->More();anIter->Next()) {
267       SALOMEDS::GenericAttribute_var anAttr;
268       if (anIter->Value()->FindAttribute(anAttr,"AttributeComment")) {
269         SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
270         CORBA::String_var aString = aCmnt->Value();
271         string aValue(aString);
272         if (aValue == theComment) {
273           aFieldSO = anIter->Value();
274           aString = aFieldSO->GetID();
275           aValue = aString;
276           return aValue;
277         }
278       }
279     }
280     return "";
281   }
282   //===========================================================================
283   string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument,
284                           const char* theFatherEntry, const char* theRefFatherEntry,
285                           const char* theIOR, const char* theName,
286                           const char* thePersistentRef, const char* theComment,
287                           CORBA::Boolean theCreateNew)
288   {
289     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
290     SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry);
291     SALOMEDS::SObject_var newObj;
292     if (theCreateNew) newObj = aStudyBuilder->NewObject(aFather);
293     else newObj = aFather;
294     SALOMEDS::GenericAttribute_var anAttr;
295     if(strcmp(theIOR,"") != 0){
296       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
297       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
298       anIOR->SetValue(theIOR);
299     }
300     if(strcmp(theName,"") != 0){
301       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
302       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
303       aName->SetValue(theName);
304     }
305     if(strcmp(thePersistentRef,"") != 0){
306       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
307       SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
308       aPRef->SetValue(thePersistentRef);
309     }
310     if(strcmp(theComment,"") != 0){
311       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
312       SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
313       aCmnt->SetValue(theComment);
314       if(MYDEBUG) INFOS("CreateAttributes - Comment = "<<theComment);
315     }
316     if(strcmp(theRefFatherEntry,"") != 0){
317       SALOMEDS::SObject_var aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
318       SALOMEDS::SObject_var anObj = aStudyBuilder->NewObject(aRefFather);
319       aStudyBuilder->Addreference(anObj,newObj);
320     }
321     CORBA::String_var anEntry = newObj->GetID();
322     string aRet(anEntry);
323     if(MYDEBUG)
324       INFOS("CreateAttributes - StudyId = "<<theStudyDocument->StudyId()<<"; anEntry = "<<aRet<<"; IOR = '"<<theIOR<<"'");
325     return aRet;
326   }
327   string CreateAttributes(_PTR(Study) theStudyDocument,
328                           const char* theFatherEntry, const char* theRefFatherEntry,
329                           const char* theIOR, const char* theName,
330                           const char* thePersistentRef, const char* theComment,
331                           CORBA::Boolean theCreateNew)
332   {
333     _PTR(StudyBuilder) aStudyBuilder = theStudyDocument->NewBuilder();
334     _PTR(SObject) aFather = theStudyDocument->FindObjectID(theFatherEntry);
335     _PTR(SObject) newObj;
336     if (theCreateNew)
337       newObj = aStudyBuilder->NewObject(aFather);
338     else
339       newObj = aFather;
340
341     _PTR(GenericAttribute) anAttr;
342     if (strcmp(theIOR, "") != 0) {
343       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
344       _PTR(AttributeIOR) anIOR (anAttr);
345       anIOR->SetValue(theIOR);
346     }
347     if (strcmp(theName, "") != 0) {
348       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
349       _PTR(AttributeName) aName (anAttr);
350       aName->SetValue(theName);
351     }
352     if (strcmp(thePersistentRef, "") != 0) {
353       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
354       _PTR(AttributePersistentRef) aPRef (anAttr);
355       aPRef->SetValue(thePersistentRef);
356     }
357     if (strcmp(theComment, "") != 0) {
358       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
359       _PTR(AttributeComment) aCmnt (anAttr);
360       aCmnt->SetValue(theComment);
361       if (MYDEBUG) INFOS("CreateAttributes - Comment = " << theComment);
362     }
363     if (strcmp(theRefFatherEntry, "") != 0) {
364       _PTR(SObject) aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
365       _PTR(SObject) anObj = aStudyBuilder->NewObject(aRefFather);
366       aStudyBuilder->Addreference(anObj, newObj);
367     }
368     string aRet = newObj->GetID();
369     if (MYDEBUG)
370       INFOS("CreateAttributes - StudyId = " << theStudyDocument->StudyId()
371             << "; anEntry = " << aRet << "; IOR = '" << theIOR << "'");
372     return aRet;
373   }
374
375   QString GenerateName(const string& theFmt, int theId){
376     static QString aName;
377     if(theId > 0)
378       aName.sprintf("%s:%d",theFmt.c_str(),theId);
379     else
380       aName.sprintf("%s",theFmt.c_str());
381     return aName;
382   }
383
384
385   SALOMEDS::StudyManager_var GetStudyManager()
386   {
387     static SALOMEDS::StudyManager_var aStudyManager;
388     if(CORBA::is_nil(aStudyManager)){
389       SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
390       CORBA::Object_var anObject = aNamingService->Resolve("/myStudyManager");
391       aStudyManager = SALOMEDS::StudyManager::_narrow(anObject);
392     }
393     return aStudyManager;
394   }
395
396
397   SALOMEDS::Study_var GetDSStudy(_PTR(Study) theStudy)
398   {
399     //std::string aStudyName = theStudy->Name();
400     //return GetStudyManager()->GetStudyByName(aStudyName.c_str());
401     int aStudyID = theStudy->StudyId();
402     return GetStudyManager()->GetStudyByID(aStudyID);
403   }
404
405   SALOMEDS::SObject_var GetSObject( _PTR(SObject) obj )
406   {
407     _PTR(Study) aStudy = obj->GetStudy();
408     SALOMEDS::Study_var aSalomeDSStudy = GetDSStudy( aStudy );
409     std::string id = obj->GetID();
410     return aSalomeDSStudy->FindObjectID( id.c_str() );
411   }
412
413   _PTR(SObject) GetClientSObject( SALOMEDS::SObject_var obj,
414                                   _PTR(Study) study )
415   {
416     return study->FindObjectID( obj->GetID() );
417
418   }
419
420
421   void RemoveFromStudy (SALOMEDS::SObject_ptr theSObject,
422                         bool theIsAttrOnly,
423                         bool theDestroySubObjects)
424   {
425     if (theSObject->_is_nil()) return;
426
427     SALOMEDS::Study_var aStudyDocument = theSObject->GetStudy();
428     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudyDocument->NewBuilder();
429     if (theIsAttrOnly) {
430       aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
431       return;
432     }
433
434     // Remove possible sub-objects
435     SALOMEDS::ChildIterator_var aChildIter = aStudyDocument->NewChildIterator(theSObject);
436     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
437       SALOMEDS::SObject_var aChildSObject = aChildIter->Value();
438       CORBA::Object_var aChildObj = VISU::SObjectToObject(aChildSObject);
439       if (CORBA::is_nil(aChildObj)) continue;
440
441       VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
442       if (CORBA::is_nil(aRemovableObject)) continue;
443
444       aRemovableObject->RemoveFromStudy();
445     }
446
447     // asl, fix for PAL10455: Remove references to SObject
448     SALOMEDS::Study::ListOfSObject* aRefs = aStudyDocument->FindDependances( theSObject );
449     for( int i=0, n=aRefs->length(); i<n; i++ )
450     {
451       SALOMEDS::SObject_var o = (*aRefs)[i];
452       if( o->GetFatherComponent()->ComponentDataType()==theSObject->GetFatherComponent()->ComponentDataType() )
453       {
454         aStudyBuilder->RemoveReference( o );
455         aStudyBuilder->RemoveObjectWithChildren( o );
456       }
457     }
458
459     // Remove the SObject itself
460     aStudyBuilder->RemoveObjectWithChildren(theSObject);
461   }
462
463   void RemoveFromStudy (_PTR(SObject) theSObject,
464                         bool theIsAttrOnly,
465                         bool theDestroySubObjects)
466   {
467     if (!theSObject) return;
468
469     _PTR(Study) aStudyDocument = theSObject->GetStudy();
470     _PTR(StudyBuilder) aStudyBuilder = aStudyDocument->NewBuilder();
471     if (theIsAttrOnly) {
472       aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
473       return;
474     }
475
476     // Remove possible sub-objects
477     _PTR(ChildIterator) aChildIter = aStudyDocument->NewChildIterator(theSObject);
478     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
479       _PTR(SObject) aChildSObject = aChildIter->Value();
480       CORBA::Object_var aChildObj = VISU::ClientSObjectToObject(aChildSObject);
481       if (CORBA::is_nil(aChildObj)) 
482         continue;
483
484       VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
485       if (CORBA::is_nil(aRemovableObject))
486         continue;
487
488       aRemovableObject->RemoveFromStudy();
489     }
490
491     // Remove the SObject itself
492     aStudyBuilder->RemoveObjectWithChildren(theSObject);
493   }
494 }