Salome HOME
Update version to 3.2.0a1
[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   //===========================================================================
198   PortableServer::ServantBase_var GetServant(CORBA::Object_ptr theObject){
199     if(CORBA::is_nil(theObject))  return NULL;
200     try{
201       PortableServer::POA_ptr aPOA = Base_i::GetPOA();
202       PortableServer::Servant aServant = aPOA->reference_to_servant(theObject);
203       return aServant;
204     } catch (...) {
205       INFOS("GetServant - Unknown exception was occured!!!");
206       return NULL;
207     }
208   }
209
210
211   //===========================================================================
212   CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
213     SALOMEDS::GenericAttribute_var anAttr;
214     CORBA::Object_var anObj;
215     try{
216       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
217         SALOMEDS::AttributeIOR_var anIOR  = SALOMEDS::AttributeIOR::_narrow(anAttr);
218         CORBA::String_var aValue = anIOR->Value();
219         CORBA::ORB_ptr anORB = Base_i::GetORB();
220         if(strcmp(aValue,"") != 0)
221           anObj = anORB->string_to_object(aValue);
222       }
223     }catch(...){
224       INFOS("SObjectToObject - Unknown exception was occured!!!");
225     }
226     return anObj;
227   }
228
229   //===========================================================================
230   CORBA::Object_var ClientSObjectToObject(_PTR(SObject) theSObject){
231     _PTR(GenericAttribute) anAttr;
232     CORBA::Object_var anObj;
233     try{
234       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
235         _PTR(AttributeIOR) anIOR  = anAttr;
236         CORBA::String_var aValue = anIOR->Value().c_str();
237         CORBA::ORB_ptr anORB = Base_i::GetORB();
238         if(strcmp(aValue,"") != 0)
239           anObj = anORB->string_to_object(aValue);
240       }
241     }catch(...){
242       INFOS("ClientSObjectToObject - Unknown exception was occured!!!");
243     }
244     return anObj;
245   }
246
247
248   //===========================================================================
249   string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
250                               const char* theComment, int IsAllLevels)
251   {
252     SALOMEDS::ChildIterator_var anIter =
253       theStudyDocument->NewChildIterator(theStudyDocument->FindObjectID(theStartEntry));
254     anIter->InitEx(IsAllLevels);
255     SALOMEDS::SObject_var aFieldSO;
256     for(;anIter->More();anIter->Next()) {
257       SALOMEDS::GenericAttribute_var anAttr;
258       if (anIter->Value()->FindAttribute(anAttr,"AttributeComment")) {
259         SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
260         CORBA::String_var aString = aCmnt->Value();
261         string aValue(aString);
262         if (aValue == theComment) {
263           aFieldSO = anIter->Value();
264           aString = aFieldSO->GetID();
265           aValue = aString;
266           return aValue;
267         }
268       }
269     }
270     return "";
271   }
272   //===========================================================================
273   string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument,
274                           const char* theFatherEntry, const char* theRefFatherEntry,
275                           const char* theIOR, const char* theName,
276                           const char* thePersistentRef, const char* theComment,
277                           CORBA::Boolean theCreateNew)
278   {
279     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
280     SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry);
281     SALOMEDS::SObject_var newObj;
282     if (theCreateNew) newObj = aStudyBuilder->NewObject(aFather);
283     else newObj = aFather;
284     SALOMEDS::GenericAttribute_var anAttr;
285     if(strcmp(theIOR,"") != 0){
286       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
287       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
288       anIOR->SetValue(theIOR);
289     }
290     if(strcmp(theName,"") != 0){
291       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
292       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
293       aName->SetValue(theName);
294     }
295     if(strcmp(thePersistentRef,"") != 0){
296       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
297       SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
298       aPRef->SetValue(thePersistentRef);
299     }
300     if(strcmp(theComment,"") != 0){
301       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
302       SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
303       aCmnt->SetValue(theComment);
304       if(MYDEBUG) INFOS("CreateAttributes - Comment = "<<theComment);
305     }
306     if(strcmp(theRefFatherEntry,"") != 0){
307       SALOMEDS::SObject_var aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
308       SALOMEDS::SObject_var anObj = aStudyBuilder->NewObject(aRefFather);
309       aStudyBuilder->Addreference(anObj,newObj);
310     }
311     CORBA::String_var anEntry = newObj->GetID();
312     string aRet(anEntry);
313     if(MYDEBUG)
314       INFOS("CreateAttributes - StudyId = "<<theStudyDocument->StudyId()<<"; anEntry = "<<aRet<<"; IOR = '"<<theIOR<<"'");
315     return aRet;
316   }
317   string CreateAttributes(_PTR(Study) theStudyDocument,
318                           const char* theFatherEntry, const char* theRefFatherEntry,
319                           const char* theIOR, const char* theName,
320                           const char* thePersistentRef, const char* theComment,
321                           CORBA::Boolean theCreateNew)
322   {
323     _PTR(StudyBuilder) aStudyBuilder = theStudyDocument->NewBuilder();
324     _PTR(SObject) aFather = theStudyDocument->FindObjectID(theFatherEntry);
325     _PTR(SObject) newObj;
326     if (theCreateNew)
327       newObj = aStudyBuilder->NewObject(aFather);
328     else
329       newObj = aFather;
330
331     _PTR(GenericAttribute) anAttr;
332     if (strcmp(theIOR, "") != 0) {
333       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
334       _PTR(AttributeIOR) anIOR (anAttr);
335       anIOR->SetValue(theIOR);
336     }
337     if (strcmp(theName, "") != 0) {
338       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
339       _PTR(AttributeName) aName (anAttr);
340       aName->SetValue(theName);
341     }
342     if (strcmp(thePersistentRef, "") != 0) {
343       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
344       _PTR(AttributePersistentRef) aPRef (anAttr);
345       aPRef->SetValue(thePersistentRef);
346     }
347     if (strcmp(theComment, "") != 0) {
348       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
349       _PTR(AttributeComment) aCmnt (anAttr);
350       aCmnt->SetValue(theComment);
351       if (MYDEBUG) INFOS("CreateAttributes - Comment = " << theComment);
352     }
353     if (strcmp(theRefFatherEntry, "") != 0) {
354       _PTR(SObject) aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
355       _PTR(SObject) anObj = aStudyBuilder->NewObject(aRefFather);
356       aStudyBuilder->Addreference(anObj, newObj);
357     }
358     string aRet = newObj->GetID();
359     if (MYDEBUG)
360       INFOS("CreateAttributes - StudyId = " << theStudyDocument->StudyId()
361             << "; anEntry = " << aRet << "; IOR = '" << theIOR << "'");
362     return aRet;
363   }
364
365   QString GenerateName(const string& theFmt, int theId){
366     static QString aName;
367     if(theId > 0)
368       aName.sprintf("%s:%d",theFmt.c_str(),theId);
369     else
370       aName.sprintf("%s",theFmt.c_str());
371     return aName;
372   }
373
374
375   SALOMEDS::StudyManager_var GetStudyManager()
376   {
377     static SALOMEDS::StudyManager_var aStudyManager;
378     if(CORBA::is_nil(aStudyManager)){
379       SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
380       CORBA::Object_var anObject = aNamingService->Resolve("/myStudyManager");
381       aStudyManager = SALOMEDS::StudyManager::_narrow(anObject);
382     }
383     return aStudyManager;
384   }
385
386
387   SALOMEDS::Study_var GetDSStudy(_PTR(Study) theStudy)
388   {
389     //std::string aStudyName = theStudy->Name();
390     //return GetStudyManager()->GetStudyByName(aStudyName.c_str());
391     int aStudyID = theStudy->StudyId();
392     return GetStudyManager()->GetStudyByID(aStudyID);
393   }
394
395   SALOMEDS::SObject_var GetSObject( _PTR(SObject) obj )
396   {
397     _PTR(Study) aStudy = obj->GetStudy();
398     SALOMEDS::Study_var aSalomeDSStudy = GetDSStudy( aStudy );
399     std::string id = obj->GetID();
400     return aSalomeDSStudy->FindObjectID( id.c_str() );
401   }
402
403   _PTR(SObject) GetClientSObject( SALOMEDS::SObject_var obj,
404                                   _PTR(Study) study )
405   {
406     return study->FindObjectID( obj->GetID() );
407
408   }
409
410
411   void RemoveFromStudy (SALOMEDS::SObject_ptr theSObject,
412                         bool theIsAttrOnly,
413                         bool theDestroySubObjects)
414   {
415     if (theSObject->_is_nil()) return;
416
417     SALOMEDS::Study_var aStudyDocument = theSObject->GetStudy();
418     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudyDocument->NewBuilder();
419     if (theIsAttrOnly) {
420       aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
421       return;
422     }
423
424     // Remove possible sub-objects
425     SALOMEDS::ChildIterator_var aChildIter = aStudyDocument->NewChildIterator(theSObject);
426     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
427       SALOMEDS::SObject_var aChildSObject = aChildIter->Value();
428       CORBA::Object_var aChildObj = VISU::SObjectToObject(aChildSObject);
429       if (CORBA::is_nil(aChildObj)) continue;
430
431       VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
432       if (CORBA::is_nil(aRemovableObject)) continue;
433
434       aRemovableObject->RemoveFromStudy();
435     }
436
437     // asl, fix for PAL10455: Remove references to SObject
438     SALOMEDS::Study::ListOfSObject* aRefs = aStudyDocument->FindDependances( theSObject );
439     for( int i=0, n=aRefs->length(); i<n; i++ )
440     {
441       SALOMEDS::SObject_var o = (*aRefs)[i];
442       if( o->GetFatherComponent()->ComponentDataType()==theSObject->GetFatherComponent()->ComponentDataType() )
443       {
444         aStudyBuilder->RemoveReference( o );
445         aStudyBuilder->RemoveObjectWithChildren( o );
446       }
447     }
448
449     // Remove the SObject itself
450     aStudyBuilder->RemoveObjectWithChildren(theSObject);
451   }
452
453   void RemoveFromStudy (_PTR(SObject) theSObject,
454                         bool theIsAttrOnly,
455                         bool theDestroySubObjects)
456   {
457     if (!theSObject) return;
458
459     _PTR(Study) aStudyDocument = theSObject->GetStudy();
460     _PTR(StudyBuilder) aStudyBuilder = aStudyDocument->NewBuilder();
461     if (theIsAttrOnly) {
462       aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
463       return;
464     }
465
466     // Remove possible sub-objects
467     _PTR(ChildIterator) aChildIter = aStudyDocument->NewChildIterator(theSObject);
468     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
469       _PTR(SObject) aChildSObject = aChildIter->Value();
470       CORBA::Object_var aChildObj = VISU::ClientSObjectToObject(aChildSObject);
471       if (CORBA::is_nil(aChildObj)) 
472         continue;
473
474       VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
475       if (CORBA::is_nil(aRemovableObject))
476         continue;
477
478       aRemovableObject->RemoveFromStudy();
479     }
480
481     // Remove the SObject itself
482     aStudyBuilder->RemoveObjectWithChildren(theSObject);
483   }
484 }