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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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 //===========================================================================
198 PortableServer::ServantBase_var GetServant(CORBA::Object_ptr theObject){
199 if(CORBA::is_nil(theObject)) return NULL;
201 PortableServer::POA_ptr aPOA = Base_i::GetPOA();
202 PortableServer::Servant aServant = aPOA->reference_to_servant(theObject);
205 INFOS("GetServant - Unknown exception was occured!!!");
211 //===========================================================================
212 CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
213 SALOMEDS::GenericAttribute_var anAttr;
214 CORBA::Object_var anObj;
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);
224 INFOS("SObjectToObject - Unknown exception was occured!!!");
229 //===========================================================================
230 CORBA::Object_var ClientSObjectToObject(_PTR(SObject) theSObject){
231 _PTR(GenericAttribute) anAttr;
232 CORBA::Object_var anObj;
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);
242 INFOS("ClientSObjectToObject - Unknown exception was occured!!!");
248 //===========================================================================
249 string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
250 const char* theComment, int IsAllLevels)
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();
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)
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);
290 if(strcmp(theName,"") != 0){
291 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
292 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
293 aName->SetValue(theName);
295 if(strcmp(thePersistentRef,"") != 0){
296 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
297 SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
298 aPRef->SetValue(thePersistentRef);
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);
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);
311 CORBA::String_var anEntry = newObj->GetID();
312 string aRet(anEntry);
314 INFOS("CreateAttributes - StudyId = "<<theStudyDocument->StudyId()<<"; anEntry = "<<aRet<<"; IOR = '"<<theIOR<<"'");
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)
323 _PTR(StudyBuilder) aStudyBuilder = theStudyDocument->NewBuilder();
324 _PTR(SObject) aFather = theStudyDocument->FindObjectID(theFatherEntry);
325 _PTR(SObject) newObj;
327 newObj = aStudyBuilder->NewObject(aFather);
331 _PTR(GenericAttribute) anAttr;
332 if (strcmp(theIOR, "") != 0) {
333 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
334 _PTR(AttributeIOR) anIOR (anAttr);
335 anIOR->SetValue(theIOR);
337 if (strcmp(theName, "") != 0) {
338 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
339 _PTR(AttributeName) aName (anAttr);
340 aName->SetValue(theName);
342 if (strcmp(thePersistentRef, "") != 0) {
343 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
344 _PTR(AttributePersistentRef) aPRef (anAttr);
345 aPRef->SetValue(thePersistentRef);
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);
353 if (strcmp(theRefFatherEntry, "") != 0) {
354 _PTR(SObject) aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
355 _PTR(SObject) anObj = aStudyBuilder->NewObject(aRefFather);
356 aStudyBuilder->Addreference(anObj, newObj);
358 string aRet = newObj->GetID();
360 INFOS("CreateAttributes - StudyId = " << theStudyDocument->StudyId()
361 << "; anEntry = " << aRet << "; IOR = '" << theIOR << "'");
365 QString GenerateName(const string& theFmt, int theId){
366 static QString aName;
368 aName.sprintf("%s:%d",theFmt.c_str(),theId);
370 aName.sprintf("%s",theFmt.c_str());
375 SALOMEDS::StudyManager_var GetStudyManager()
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);
383 return aStudyManager;
387 SALOMEDS::Study_var GetDSStudy(_PTR(Study) theStudy)
389 //std::string aStudyName = theStudy->Name();
390 //return GetStudyManager()->GetStudyByName(aStudyName.c_str());
391 int aStudyID = theStudy->StudyId();
392 return GetStudyManager()->GetStudyByID(aStudyID);
395 SALOMEDS::SObject_var GetSObject( _PTR(SObject) obj )
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() );
403 _PTR(SObject) GetClientSObject( SALOMEDS::SObject_var obj,
406 return study->FindObjectID( obj->GetID() );
411 void RemoveFromStudy (SALOMEDS::SObject_ptr theSObject,
413 bool theDestroySubObjects)
415 if (theSObject->_is_nil()) return;
417 SALOMEDS::Study_var aStudyDocument = theSObject->GetStudy();
418 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudyDocument->NewBuilder();
420 aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
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;
431 VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
432 if (CORBA::is_nil(aRemovableObject)) continue;
434 aRemovableObject->RemoveFromStudy();
437 if (theDestroySubObjects) {
438 VISU::Prs3d_var aPrs3d = VISU::Prs3d::_narrow(aRemovableObject);
439 if (CORBA::is_nil(aPrs3d)) continue;
444 // Remove the SObject itself
445 aStudyBuilder->RemoveObjectWithChildren(theSObject);
448 void RemoveFromStudy (_PTR(SObject) theSObject,
450 bool theDestroySubObjects)
452 if (!theSObject) return;
454 _PTR(Study) aStudyDocument = theSObject->GetStudy();
455 _PTR(StudyBuilder) aStudyBuilder = aStudyDocument->NewBuilder();
457 aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
461 // Remove possible sub-objects
462 _PTR(ChildIterator) aChildIter = aStudyDocument->NewChildIterator(theSObject);
463 for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
464 _PTR(SObject) aChildSObject = aChildIter->Value();
465 CORBA::Object_var aChildObj = VISU::ClientSObjectToObject(aChildSObject);
466 if (CORBA::is_nil(aChildObj)) continue;
468 VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
469 if (CORBA::is_nil(aRemovableObject)) continue;
471 aRemovableObject->RemoveFromStudy();
474 if (theDestroySubObjects) {
475 VISU::Prs3d_var aPrs3d = VISU::Prs3d::_narrow(aRemovableObject);
476 if (CORBA::is_nil(aPrs3d)) continue;
481 // Remove the SObject itself
482 aStudyBuilder->RemoveObjectWithChildren(theSObject);