1 // VISU OBJECT : interactive object for VISU entities implementation
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : VISUConfig.cc
24 // Author : Alexey PETROV
27 #include "VISUConfig.hh"
29 #include "SUIT_ResourceMgr.h"
30 #include "SUIT_Session.h"
32 #include <SALOMEDSClient_GenericAttribute.hxx>
33 #include <SALOMEDSClient_AttributeIOR.hxx>
35 #include <SalomeApp_Application.h>
40 static int MYDEBUG = 0;
42 static int MYDEBUG = 0;
50 return SUIT_Session::session();
56 return GetSession()->resourceMgr();
59 //===========================================================================
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;
72 char* Base_i::GetID(){
74 CORBA::Object_var anObject = _this();
75 CORBA::String_var anIOR = myOrb->object_to_string(anObject);
78 return CORBA::string_dup(myID.c_str());
81 //===========================================================================
82 static int mySCnt = 0;
83 static QMutex aMutex(TRUE);
85 Mutex::Mutex(QMutex* theMutex): myMutex(&aMutex){
86 if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<!mySCnt);
94 if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<!mySCnt);
98 //===========================================================================
99 static Storable::TCallbackMap VisuStoreMap;
100 string Storable::ToString(){
101 ostringstream strOut;
102 Storable::DataToStream( strOut, "myComment", GetComment() );
105 if(MYDEBUG) MESSAGE("Storable::ToString - "<<strOut.str());
109 void Storable::Registry(const char* theComment, TStorableEngine theEngine)
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 !!!");
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;
125 aName = next.stripWhiteSpace();
129 aName = next.left( pos ).stripWhiteSpace();
130 aValue = next.mid( pos+1 );
131 if ( aValue.isNull() )
134 if ( !aName.isEmpty() )
135 theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );
139 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const QString& theVal) {
140 QString output = ( !theName.isNull() ? theName : QString("") )
142 + ( !theVal.isNull() ? theVal : QString("") );
143 theStr<<output.latin1()<<";";
146 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const int theVal) {
147 QString output = ( !theName.isNull() ? theName : QString("") )
149 + QString::number( theVal );
150 theStr<<output.latin1()<<";";
153 void Storable::DataToStream(ostringstream& theStr, const QString& theName, const double theVal) {
154 QString output = ( !theName.isNull() ? theName : QString("") )
156 + QString::number( theVal );
157 theStr<<output.latin1()<<";";
159 Storable* Storable::Create(SALOMEDS::SObject_ptr theSObject,
160 const string& thePrefix, const string& theLocalPersistentID)
163 QString strIn( theLocalPersistentID.c_str() );
165 StrToMap(strIn,aMap);
167 QString aComment = VISU::Storable::FindValue(aMap,"myComment",&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);
174 }catch(std::logic_error& exc){
175 INFOS("Follow exception was occured :\n"<<exc.what());
177 INFOS("Unknown exception was occured!");
182 QString Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
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");
192 if(isFind != NULL) *isFind = true;
197 QString Storable::FindValue(const TRestoringMap& theMap, const string& theArg, const QString& theDefaultValue)
199 bool anIsFound = false;
200 QString aValue = FindValue(theMap,theArg,&anIsFound);
203 return theDefaultValue;
207 //===========================================================================
208 PortableServer::ServantBase_var GetServant(CORBA::Object_ptr theObject){
209 if(CORBA::is_nil(theObject)) return NULL;
211 PortableServer::POA_ptr aPOA = Base_i::GetPOA();
212 PortableServer::Servant aServant = aPOA->reference_to_servant(theObject);
215 INFOS("GetServant - Unknown exception was occured!!!");
221 //===========================================================================
222 CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
223 SALOMEDS::GenericAttribute_var anAttr;
224 CORBA::Object_var anObj;
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);
234 INFOS("SObjectToObject - Unknown exception was occured!!!");
239 //===========================================================================
240 CORBA::Object_var ClientSObjectToObject(_PTR(SObject) theSObject){
241 _PTR(GenericAttribute) anAttr;
242 CORBA::Object_var anObj;
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);
252 INFOS("ClientSObjectToObject - Unknown exception was occured!!!");
258 //===========================================================================
259 string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
260 const char* theComment, int IsAllLevels)
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();
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)
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);
300 if(strcmp(theName,"") != 0){
301 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
302 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
303 aName->SetValue(theName);
305 if(strcmp(thePersistentRef,"") != 0){
306 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
307 SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
308 aPRef->SetValue(thePersistentRef);
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);
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);
321 CORBA::String_var anEntry = newObj->GetID();
322 string aRet(anEntry);
324 INFOS("CreateAttributes - StudyId = "<<theStudyDocument->StudyId()<<"; anEntry = "<<aRet<<"; IOR = '"<<theIOR<<"'");
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)
333 _PTR(StudyBuilder) aStudyBuilder = theStudyDocument->NewBuilder();
334 _PTR(SObject) aFather = theStudyDocument->FindObjectID(theFatherEntry);
335 _PTR(SObject) newObj;
337 newObj = aStudyBuilder->NewObject(aFather);
341 _PTR(GenericAttribute) anAttr;
342 if (strcmp(theIOR, "") != 0) {
343 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
344 _PTR(AttributeIOR) anIOR (anAttr);
345 anIOR->SetValue(theIOR);
347 if (strcmp(theName, "") != 0) {
348 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
349 _PTR(AttributeName) aName (anAttr);
350 aName->SetValue(theName);
352 if (strcmp(thePersistentRef, "") != 0) {
353 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
354 _PTR(AttributePersistentRef) aPRef (anAttr);
355 aPRef->SetValue(thePersistentRef);
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);
363 if (strcmp(theRefFatherEntry, "") != 0) {
364 _PTR(SObject) aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
365 _PTR(SObject) anObj = aStudyBuilder->NewObject(aRefFather);
366 aStudyBuilder->Addreference(anObj, newObj);
368 string aRet = newObj->GetID();
370 INFOS("CreateAttributes - StudyId = " << theStudyDocument->StudyId()
371 << "; anEntry = " << aRet << "; IOR = '" << theIOR << "'");
375 QString GenerateName(const string& theFmt, int theId){
376 static QString aName;
378 aName.sprintf("%s:%d",theFmt.c_str(),theId);
380 aName.sprintf("%s",theFmt.c_str());
385 SALOMEDS::StudyManager_var GetStudyManager()
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);
393 return aStudyManager;
397 SALOMEDS::Study_var GetDSStudy(_PTR(Study) theStudy)
399 //std::string aStudyName = theStudy->Name();
400 //return GetStudyManager()->GetStudyByName(aStudyName.c_str());
401 int aStudyID = theStudy->StudyId();
402 return GetStudyManager()->GetStudyByID(aStudyID);
405 SALOMEDS::SObject_var GetSObject( _PTR(SObject) obj )
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() );
413 _PTR(SObject) GetClientSObject( SALOMEDS::SObject_var obj,
416 return study->FindObjectID( obj->GetID() );
421 void RemoveFromStudy (SALOMEDS::SObject_ptr theSObject,
423 bool theDestroySubObjects)
425 if (theSObject->_is_nil()) return;
427 SALOMEDS::Study_var aStudyDocument = theSObject->GetStudy();
428 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudyDocument->NewBuilder();
430 aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
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;
441 VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
442 if (CORBA::is_nil(aRemovableObject)) continue;
444 aRemovableObject->RemoveFromStudy();
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++ )
451 SALOMEDS::SObject_var o = (*aRefs)[i];
452 if( o->GetFatherComponent()->ComponentDataType()==theSObject->GetFatherComponent()->ComponentDataType() )
454 aStudyBuilder->RemoveReference( o );
455 aStudyBuilder->RemoveObjectWithChildren( o );
459 // Remove the SObject itself
460 aStudyBuilder->RemoveObjectWithChildren(theSObject);
463 void RemoveFromStudy (_PTR(SObject) theSObject,
465 bool theDestroySubObjects)
467 if (!theSObject) return;
469 _PTR(Study) aStudyDocument = theSObject->GetStudy();
470 _PTR(StudyBuilder) aStudyBuilder = aStudyDocument->NewBuilder();
472 aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
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))
484 VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
485 if (CORBA::is_nil(aRemovableObject))
488 aRemovableObject->RemoveFromStudy();
491 // Remove the SObject itself
492 aStudyBuilder->RemoveObjectWithChildren(theSObject);