Salome HOME
IPAL9626: Incorrect default values for Arrange presentations (from Animation db,...
[modules/visu.git] / src / VISU_I / VISUConfig.cc
index e2229e199f405e8d644881d3278618b75b87c0da..30cb7c4fa68c29b22e3b9b1ec12dd8b97233abf5 100644 (file)
@@ -1,23 +1,23 @@
 //  VISU OBJECT : interactive object for VISU entities implementation
 //
 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
-// 
-//  This library is free software; you can redistribute it and/or 
-//  modify it under the terms of the GNU Lesser General Public 
-//  License as published by the Free Software Foundation; either 
-//  version 2.1 of the License. 
-// 
-//  This library is distributed in the hope that it will be useful, 
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
-//  Lesser General Public License for more details. 
-// 
-//  You should have received a copy of the GNU Lesser General Public 
-//  License along with this library; if not, write to the Free Software 
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
-// 
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+//  This library is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU Lesser General Public
+//  License as published by the Free Software Foundation; either
+//  version 2.1 of the License.
+//
+//  This library is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+//  Lesser General Public License for more details.
+//
+//  You should have received a copy of the GNU Lesser General Public
+//  License along with this library; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
 //
 //
 //  File   : VISUConfig.cc
 
 #include "VISUConfig.hh"
 
+#include "SUIT_ResourceMgr.h"
+#include "SUIT_Session.h"
+
+#include <SALOMEDSClient_GenericAttribute.hxx>
+#include <SALOMEDSClient_AttributeIOR.hxx>
+
+#include <SalomeApp_Application.h>
+
 using namespace std;
 
 #ifdef _DEBUG_
@@ -35,7 +43,21 @@ static int MYDEBUG = 0;
 #endif
 
 namespace VISU{
+
+  SUIT_Session*
+  GetSession()
+  {
+    return SUIT_Session::session();
+  }
+
+  SUIT_ResourceMgr*
+  GetResourceMgr()
+  {
+    return GetSession()->resourceMgr();
+  }
+
   //===========================================================================
+
   QMutex* Base_i::myMutex = NULL; //apo - &VISUMutex;
   CORBA::ORB_var Base_i::myOrb;
   PortableServer::POA_var Base_i::myPoa;
@@ -47,7 +69,7 @@ namespace VISU{
 
   Base_i::~Base_i() {}
 
-  char* Base_i::GetID(){ 
+  char* Base_i::GetID(){
     if(myID == ""){
       CORBA::Object_var anObject = _this();
       CORBA::String_var anIOR = myOrb->object_to_string(anObject);
@@ -58,32 +80,18 @@ namespace VISU{
 
   //===========================================================================
   static int mySCnt = 0;
-  static int myQCnt = 0;
-  static int myIsBatchMode = 0;
+  static QMutex aMutex(TRUE);
 
-  Mutex::Mutex(QMutex* theMutex, QApplication* theQApp, int theDelay) :
-    myQApp(theQApp), isQAppLocked(theQApp->locked()), myDelay(theDelay),
-    myMutex(theMutex), isSessionLocked(theMutex->locked())
-  {
-    if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<(!isQAppLocked && !myQCnt)<<" "<<(!isSessionLocked && !mySCnt));
-    if(!myIsBatchMode && isQAppLocked) myIsBatchMode++;
-    if(!isSessionLocked && !mySCnt) { myMutex->lock();}; mySCnt++;
-    if(!isQAppLocked && !myQCnt) { 
-      myQApp->lock(); 
-      myQApp->syncX();
-    };
-    myQCnt++;
+  Mutex::Mutex(QMutex* theMutex): myMutex(&aMutex){
+    if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<!mySCnt);
+    if(!mySCnt++)
+      myMutex->lock();
   }
+
   Mutex::~Mutex(){
-    myQCnt--; 
-    if(!isQAppLocked && !myQCnt) { 
-      myQApp->flushX(); 
-      //if(myDelay > 0)
-       myQApp->processEvents(myDelay+3);
-      myQApp->unlock();
-    }
-    mySCnt--; if(!isSessionLocked && !mySCnt) { myMutex->unlock();}
-    if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<(!isQAppLocked && !myQCnt)<<" "<<(!isSessionLocked && !mySCnt));
+    if(!(--mySCnt))
+      myMutex->unlock();
+    if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<!mySCnt);
   }
 
 
@@ -95,18 +103,17 @@ namespace VISU{
     ToStream(strOut);
     strOut<<ends;
     if(MYDEBUG) MESSAGE("Storable::ToString - "<<strOut.str());
-    return strOut.str(); 
+    return strOut.str();
   }
 
   void Storable::Registry(const char* theComment, TStorableEngine theEngine)
-    throw(std::logic_error&)
   {
     if(!VisuStoreMap.insert(TCallbackMap::value_type(theComment,theEngine)).second){
       if(MYDEBUG) MESSAGE("Storable::Registry >> dupliacte registring !!!");
       throw std::logic_error("Storable::Registry >> dupliacte registring !!!");
     }
   }
-  
+
   void Storable::StrToMap(const QString& theStr, VISU::Storable::TRestoringMap& theMap){
     if(0 && MYDEBUG) MESSAGE("Storable::StrToMap : string="<<theStr);
     QStringList strList = QStringList::split( ";", theStr, false );
@@ -125,37 +132,36 @@ namespace VISU{
          aValue = "";
       }
       if ( !aName.isEmpty() )
-       theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );   
+       theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );
     }
   }
 
   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const QString& theVal) {
-    QString output = ( !theName.isNull() ? theName : QString("") ) 
-                   + QString( "=" ) 
+    QString output = ( !theName.isNull() ? theName : QString("") )
+                   + QString( "=" )
                   + ( !theVal.isNull()  ? theVal  : QString("") );
     theStr<<output.latin1()<<";";
   }
 
   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const int theVal) {
-    QString output = ( !theName.isNull() ? theName : QString("") ) 
-                   + QString( "=" ) 
+    QString output = ( !theName.isNull() ? theName : QString("") )
+                   + QString( "=" )
                   + QString::number( theVal );
     theStr<<output.latin1()<<";";
   }
 
   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const double theVal) {
-    QString output = ( !theName.isNull() ? theName : QString("") ) 
-                   + QString( "=" ) 
+    QString output = ( !theName.isNull() ? theName : QString("") )
+                   + QString( "=" )
                   + QString::number( theVal );
     theStr<<output.latin1()<<";";
   }
   Storable* Storable::Create(SALOMEDS::SObject_ptr theSObject,
-                            const string& thePrefix, const string& theLocalPersistentID) 
-    throw (std::runtime_error&)
+                            const string& thePrefix, const string& theLocalPersistentID)
   {
     try{
       QString strIn( theLocalPersistentID.c_str() );
-      TRestoringMap aMap;        
+      TRestoringMap aMap;
       StrToMap(strIn,aMap);
       bool isExist;
       QString aComment = VISU::Storable::FindValue(aMap,"myComment",&isExist);
@@ -166,15 +172,14 @@ namespace VISU{
        return (i->second)(theSObject,thePrefix,aMap);
       }
     }catch(std::logic_error& exc){
-      INFOS("Follow exception was accured :\n"<<exc.what());
+      INFOS("Follow exception was occured :\n"<<exc.what());
     }catch(...){
-      INFOS("Unknown exception was accured!");
+      INFOS("Unknown exception was occured!");
     }
     return NULL;
   }
-  
-  const QString& Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
-    throw(std::logic_error&)
+
+  QString Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
   {
     TRestoringMap::const_iterator i = theMap.find(theArg);
     if(i == theMap.end()) {
@@ -187,7 +192,7 @@ namespace VISU{
     if(isFind != NULL) *isFind = true;
     return i->second;
   }
-  
+
 
   //===========================================================================
   PortableServer::ServantBase_var GetServant(CORBA::Object_ptr theObject){
@@ -197,7 +202,7 @@ namespace VISU{
       PortableServer::Servant aServant = aPOA->reference_to_servant(theObject);
       return aServant;
     } catch (...) {
-      INFOS("GetServant - Unknown exception was accured!!!"); 
+      INFOS("GetServant - Unknown exception was occured!!!");
       return NULL;
     }
   }
@@ -216,7 +221,25 @@ namespace VISU{
          anObj = anORB->string_to_object(aValue);
       }
     }catch(...){
-      INFOS("SObjectToObject - Unknown exception was accured!!!");
+      INFOS("SObjectToObject - Unknown exception was occured!!!");
+    }
+    return anObj;
+  }
+
+  //===========================================================================
+  CORBA::Object_var ClientSObjectToObject(_PTR(SObject) theSObject){
+    _PTR(GenericAttribute) anAttr;
+    CORBA::Object_var anObj;
+    try{
+      if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
+       _PTR(AttributeIOR) anIOR  = anAttr;
+       CORBA::String_var aValue = anIOR->Value().c_str();
+       CORBA::ORB_ptr anORB = Base_i::GetORB();
+       if(strcmp(aValue,"") != 0)
+         anObj = anORB->string_to_object(aValue);
+      }
+    }catch(...){
+      INFOS("ClientSObjectToObject - Unknown exception was occured!!!");
     }
     return anObj;
   }
@@ -226,7 +249,7 @@ namespace VISU{
   string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
                              const char* theComment, int IsAllLevels)
   {
-    SALOMEDS::ChildIterator_var anIter = 
+    SALOMEDS::ChildIterator_var anIter =
       theStudyDocument->NewChildIterator(theStudyDocument->FindObjectID(theStartEntry));
     anIter->InitEx(IsAllLevels);
     SALOMEDS::SObject_var aFieldSO;
@@ -247,9 +270,9 @@ namespace VISU{
     return "";
   }
   //===========================================================================
-  string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument, 
+  string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument,
                          const char* theFatherEntry, const char* theRefFatherEntry,
-                         const char* theIOR, const char* theName, 
+                         const char* theIOR, const char* theName,
                          const char* thePersistentRef, const char* theComment,
                          CORBA::Boolean theCreateNew)
   {
@@ -262,23 +285,23 @@ namespace VISU{
     if(strcmp(theIOR,"") != 0){
       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
-      anIOR->SetValue(theIOR); 
+      anIOR->SetValue(theIOR);
     }
     if(strcmp(theName,"") != 0){
       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
-      aName->SetValue(theName); 
+      aName->SetValue(theName);
     }
     if(strcmp(thePersistentRef,"") != 0){
       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
       SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
-      aPRef->SetValue(thePersistentRef); 
+      aPRef->SetValue(thePersistentRef);
     }
     if(strcmp(theComment,"") != 0){
       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
       SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
       aCmnt->SetValue(theComment);
-      INFOS("CreateAttributes - Comment = "<<theComment);
+      if(MYDEBUG) INFOS("CreateAttributes - Comment = "<<theComment);
     }
     if(strcmp(theRefFatherEntry,"") != 0){
       SALOMEDS::SObject_var aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
@@ -287,18 +310,175 @@ namespace VISU{
     }
     CORBA::String_var anEntry = newObj->GetID();
     string aRet(anEntry);
-    if(MYDEBUG) 
+    if(MYDEBUG)
       INFOS("CreateAttributes - StudyId = "<<theStudyDocument->StudyId()<<"; anEntry = "<<aRet<<"; IOR = '"<<theIOR<<"'");
     return aRet;
   }
-}
+  string CreateAttributes(_PTR(Study) theStudyDocument,
+                         const char* theFatherEntry, const char* theRefFatherEntry,
+                         const char* theIOR, const char* theName,
+                         const char* thePersistentRef, const char* theComment,
+                         CORBA::Boolean theCreateNew)
+  {
+    _PTR(StudyBuilder) aStudyBuilder = theStudyDocument->NewBuilder();
+    _PTR(SObject) aFather = theStudyDocument->FindObjectID(theFatherEntry);
+    _PTR(SObject) newObj;
+    if (theCreateNew)
+      newObj = aStudyBuilder->NewObject(aFather);
+    else
+      newObj = aFather;
+
+    _PTR(GenericAttribute) anAttr;
+    if (strcmp(theIOR, "") != 0) {
+      anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
+      _PTR(AttributeIOR) anIOR (anAttr);
+      anIOR->SetValue(theIOR);
+    }
+    if (strcmp(theName, "") != 0) {
+      anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
+      _PTR(AttributeName) aName (anAttr);
+      aName->SetValue(theName);
+    }
+    if (strcmp(thePersistentRef, "") != 0) {
+      anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
+      _PTR(AttributePersistentRef) aPRef (anAttr);
+      aPRef->SetValue(thePersistentRef);
+    }
+    if (strcmp(theComment, "") != 0) {
+      anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
+      _PTR(AttributeComment) aCmnt (anAttr);
+      aCmnt->SetValue(theComment);
+      if (MYDEBUG) INFOS("CreateAttributes - Comment = " << theComment);
+    }
+    if (strcmp(theRefFatherEntry, "") != 0) {
+      _PTR(SObject) aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
+      _PTR(SObject) anObj = aStudyBuilder->NewObject(aRefFather);
+      aStudyBuilder->Addreference(anObj, newObj);
+    }
+    string aRet = newObj->GetID();
+    if (MYDEBUG)
+      INFOS("CreateAttributes - StudyId = " << theStudyDocument->StudyId()
+           << "; anEntry = " << aRet << "; IOR = '" << theIOR << "'");
+    return aRet;
+  }
+
+  QString GenerateName(const string& theFmt, int theId){
+    static QString aName;
+    if(theId > 0)
+      aName.sprintf("%s:%d",theFmt.c_str(),theId);
+    else
+      aName.sprintf("%s",theFmt.c_str());
+    return aName;
+  }
+
+
+  SALOMEDS::StudyManager_var GetStudyManager()
+  {
+    static SALOMEDS::StudyManager_var aStudyManager;
+    if(CORBA::is_nil(aStudyManager)){
+      SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
+      CORBA::Object_var anObject = aNamingService->Resolve("/myStudyManager");
+      aStudyManager = SALOMEDS::StudyManager::_narrow(anObject);
+    }
+    return aStudyManager;
+  }
+
+
+  SALOMEDS::Study_var GetDSStudy(_PTR(Study) theStudy)
+  {
+    //std::string aStudyName = theStudy->Name();
+    //return GetStudyManager()->GetStudyByName(aStudyName.c_str());
+    int aStudyID = theStudy->StudyId();
+    return GetStudyManager()->GetStudyByID(aStudyID);
+  }
+
+  SALOMEDS::SObject_var GetSObject( _PTR(SObject) obj )
+  {
+    _PTR(Study) aStudy = obj->GetStudy();
+    SALOMEDS::Study_var aSalomeDSStudy = GetDSStudy( aStudy );
+    std::string id = obj->GetID();
+    return aSalomeDSStudy->FindObjectID( id.c_str() );
+  }
+
+  _PTR(SObject) GetClientSObject( SALOMEDS::SObject_var obj,
+                                 _PTR(Study) study )
+  {
+    return study->FindObjectID( obj->GetID() );
+
+  }
 
 
-QString VISU::GenerateName(const string& theFmt, int theId){
-  static QString aName;
-  if(theId > 0)
-    aName.sprintf("%s:%d",theFmt.c_str(),theId);
-  else
-    aName.sprintf("%s",theFmt.c_str());
-  return aName;
+  void RemoveFromStudy (SALOMEDS::SObject_ptr theSObject,
+                       bool theIsAttrOnly,
+                       bool theDestroySubObjects)
+  {
+    if (theSObject->_is_nil()) return;
+
+    SALOMEDS::Study_var aStudyDocument = theSObject->GetStudy();
+    SALOMEDS::StudyBuilder_var aStudyBuilder = aStudyDocument->NewBuilder();
+    if (theIsAttrOnly) {
+      aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
+      return;
+    }
+
+    // Remove possible sub-objects
+    SALOMEDS::ChildIterator_var aChildIter = aStudyDocument->NewChildIterator(theSObject);
+    for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
+      SALOMEDS::SObject_var aChildSObject = aChildIter->Value();
+      CORBA::Object_var aChildObj = VISU::SObjectToObject(aChildSObject);
+      if (CORBA::is_nil(aChildObj)) continue;
+
+      VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
+      if (CORBA::is_nil(aRemovableObject)) continue;
+
+      aRemovableObject->RemoveFromStudy();
+
+      // Destroy
+      if (theDestroySubObjects) {
+       VISU::Prs3d_var aPrs3d = VISU::Prs3d::_narrow(aRemovableObject);
+       if (CORBA::is_nil(aPrs3d)) continue;
+       aPrs3d->Destroy();
+      }
+    }
+
+    // Remove the SObject itself
+    aStudyBuilder->RemoveObjectWithChildren(theSObject);
+  }
+
+  void RemoveFromStudy (_PTR(SObject) theSObject,
+                       bool theIsAttrOnly,
+                       bool theDestroySubObjects)
+  {
+    if (!theSObject) return;
+
+    _PTR(Study) aStudyDocument = theSObject->GetStudy();
+    _PTR(StudyBuilder) aStudyBuilder = aStudyDocument->NewBuilder();
+    if (theIsAttrOnly) {
+      aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
+      return;
+    }
+
+    // Remove possible sub-objects
+    _PTR(ChildIterator) aChildIter = aStudyDocument->NewChildIterator(theSObject);
+    for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
+      _PTR(SObject) aChildSObject = aChildIter->Value();
+      CORBA::Object_var aChildObj = VISU::ClientSObjectToObject(aChildSObject);
+      if (CORBA::is_nil(aChildObj)) continue;
+
+      VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(aChildObj);
+      if (CORBA::is_nil(aRemovableObject)) continue;
+
+      aRemovableObject->RemoveFromStudy();
+
+      // Destroy
+      if (theDestroySubObjects) {
+       VISU::Prs3d_var aPrs3d = VISU::Prs3d::_narrow(aRemovableObject);
+       if (CORBA::is_nil(aPrs3d)) continue;
+       aPrs3d->Destroy();
+      }
+    }
+
+    // Remove the SObject itself
+    aStudyBuilder->RemoveObjectWithChildren(theSObject);
+  }
 }