]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
1. Dump notebook variables in the Study script. TG_DumpPython_Extension_1
authorrnv <rnv@opencascade.com>
Wed, 5 Nov 2008 13:16:40 +0000 (13:16 +0000)
committerrnv <rnv@opencascade.com>
Wed, 5 Nov 2008 13:16:40 +0000 (13:16 +0000)
2. Use a std::vector for storage notebook vasiables instead of a std::map.

src/SALOMEDS/SALOMEDS_Study.cxx
src/SALOMEDS/SALOMEDS_Study_i.cxx
src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.cxx
src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.hxx
src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.cxx
src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.hxx
src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx
src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx
src/SALOMEDSImpl/SALOMEDSImpl_StudyManager.cxx

index 14797c541de912b4b4283a154ecfeb4e744f376b..ade49db95984d304bdeee3f2d2f663f7b4102998 100644 (file)
@@ -722,7 +722,7 @@ double SALOMEDS_Study::GetReal(const string& theVarName)
   double aResult;
   if (_isLocal) {
     SALOMEDS::Locker lock;
-    aResult = _local_impl->GetVariable(theVarName);
+    aResult = _local_impl->GetVariableValue(theVarName);
   }
   else 
     aResult = _corba_impl->GetReal((char*)theVarName.c_str());
@@ -734,7 +734,7 @@ int SALOMEDS_Study::GetInteger(const string& theVarName)
   int aResult;  
   if (_isLocal) {
     SALOMEDS::Locker lock;
-    aResult = (int) _local_impl->GetVariable(theVarName);
+    aResult = (int) _local_impl->GetVariableValue(theVarName);
   }
   else 
     aResult = _corba_impl->GetInteger((char*)theVarName.c_str());
@@ -746,7 +746,7 @@ bool SALOMEDS_Study::GetBoolean(const string& theVarName)
   bool aResult;
   if (_isLocal) {
     SALOMEDS::Locker lock;
-    aResult = (bool) _local_impl->GetVariable(theVarName);
+    aResult = (bool) _local_impl->GetVariableValue(theVarName);
   }
   else 
     aResult = _corba_impl->GetBoolean((char*)theVarName.c_str());
index 3f161946c04628f70647e40d940289971733864a..6d8ef34e53ed0aaf33925536b86dc738da0c3ff1 100644 (file)
@@ -886,7 +886,7 @@ void SALOMEDS_Study_i::SetBoolean(const char* theVarName, CORBA::Boolean theValu
 //============================================================================
 CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName)
 {
-  return _impl->GetVariable(string(theVarName));
+  return _impl->GetVariableValue(string(theVarName));
 }
 
 //============================================================================
@@ -896,7 +896,7 @@ CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName)
 //============================================================================
 CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName)
 {
-  return (int)_impl->GetVariable(string(theVarName));
+  return (int)_impl->GetVariableValue(string(theVarName));
 }
 
 //============================================================================
@@ -906,7 +906,7 @@ CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName)
 //============================================================================
 CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName)
 {
-  return (bool)_impl->GetVariable(string(theVarName));
+  return (bool)_impl->GetVariableValue(string(theVarName));
 }
 
 //============================================================================
index 5c6840b722160607f96b2960ab34a4d1a17a1098..529620ef02b449001551f9ffc116e5f584db3f65 100644 (file)
@@ -36,8 +36,10 @@ using namespace std;
  */
 //============================================================================
 SALOMEDSImpl_GenericVariable::
-SALOMEDSImpl_GenericVariable(SALOMEDSImpl_GenericVariable::VariableTypes theType): 
-  _type(theType)
+SALOMEDSImpl_GenericVariable(SALOMEDSImpl_GenericVariable::VariableTypes theType,
+                             const string& theName): 
+  _type(theType),
+  _name(theName)
 {}
 
 //============================================================================
@@ -58,6 +60,16 @@ SALOMEDSImpl_GenericVariable::VariableTypes SALOMEDSImpl_GenericVariable::Type()
   return _type;
 }
 
+//============================================================================
+/*! Function : Name
+ *  Purpose  : 
+ */
+//============================================================================
+string SALOMEDSImpl_GenericVariable::Name() const
+{
+  return _name;
+}
+
 //============================================================================
 /*! Function : setType
  *  Purpose  : 
@@ -121,6 +133,16 @@ string SALOMEDSImpl_GenericVariable::Save() const
   return string();
 }
 
+
+//============================================================================
+/*! Function : SaveToScript
+ *  Purpose  : 
+ */
+//============================================================================
+string SALOMEDSImpl_GenericVariable::SaveToScript() const
+{
+  return string();
+}
 //============================================================================
 /*! Function : SaveType
  *  Purpose  : 
index d40d5cd534cfe4c28e463c982098bbf4457f44e0..5e4d6c66901b680ff35010c226fe0075cd232f08 100644 (file)
@@ -37,11 +37,13 @@ class SALOMEDSIMPL_EXPORT SALOMEDSImpl_GenericVariable
   //Supported types of the nootebook variables
   enum VariableTypes{REAL_VAR, INTEGER_VAR, BOOLEAN_VAR};
 
-  SALOMEDSImpl_GenericVariable(VariableTypes theType);
+  SALOMEDSImpl_GenericVariable(VariableTypes theType, const std::string& theName);
   ~SALOMEDSImpl_GenericVariable();
 
   VariableTypes Type() const;
 
+  std::string Name() const;
+
   static VariableTypes String2VariableType(const std::string& theStrType);
   
   void setType(const VariableTypes theType);
@@ -51,12 +53,14 @@ class SALOMEDSIMPL_EXPORT SALOMEDSImpl_GenericVariable
   virtual void SetModifyFlag();
 
   virtual std::string Save() const;
+  virtual std::string SaveToScript() const;
   virtual std::string SaveType() const;
 
   virtual void Load(const std::string& theStrValue);
   
  private:
   VariableTypes _type;
+  std::string _name;
 };
 
 #endif //_GENERICIMPL_VARIABLE_HXX_
index 036c5c0aeb058ce8f585a3f43f01ea1ee592cf00..683dbbf16932fac061ab3d6ed32e2fca8ad02ca5 100644 (file)
@@ -32,8 +32,9 @@ using namespace std;
  */
 //============================================================================
 SALOMEDSImpl_ScalarVariable::
-SALOMEDSImpl_ScalarVariable(SALOMEDSImpl_GenericVariable::VariableTypes type):
-  SALOMEDSImpl_GenericVariable(type)
+SALOMEDSImpl_ScalarVariable(SALOMEDSImpl_GenericVariable::VariableTypes type,
+                            const string& theName):
+  SALOMEDSImpl_GenericVariable(type,theName)
 {}
 
 //============================================================================
@@ -88,12 +89,45 @@ string SALOMEDSImpl_ScalarVariable::Save() const{
     case SALOMEDSImpl_GenericVariable::INTEGER_VAR:
       {
         sprintf(buffer, "%d", (int)myValue);
+        break;
       }
     default:break;
     }
   return string(buffer);
 }
 
+//============================================================================
+/*! Function : SaveToScript()
+ *  Purpose  : 
+ */
+//============================================================================
+string SALOMEDSImpl_ScalarVariable::SaveToScript() const
+{
+  char buffer[255];
+  switch(Type())
+    {
+    case SALOMEDSImpl_GenericVariable::REAL_VAR:
+      {
+        sprintf(buffer, "%f", myValue);
+        break;
+      }
+    case SALOMEDSImpl_GenericVariable::INTEGER_VAR:
+      {
+        sprintf(buffer, "%d", (int)myValue);
+        break;
+      }
+    case SALOMEDSImpl_GenericVariable::BOOLEAN_VAR:
+      {
+        if((bool)myValue)
+          sprintf(buffer, "%s", "True");
+        else
+          sprintf(buffer, "%s", "False");
+        break;
+      }
+    default:break;
+    }
+  return string(buffer);
+}
 
 //============================================================================
 /*! Function : SaveType()
index 94316a47c74ac6733f02a82f857aa45ec477e00e..b13ddeed2348221e5eac4060e3aadbe6fd3a2541 100644 (file)
@@ -31,13 +31,15 @@ class SALOMEDSIMPL_EXPORT SALOMEDSImpl_ScalarVariable :
   public SALOMEDSImpl_GenericVariable 
 {
 public:
-  SALOMEDSImpl_ScalarVariable(SALOMEDSImpl_GenericVariable::VariableTypes theType);
+  SALOMEDSImpl_ScalarVariable(SALOMEDSImpl_GenericVariable::VariableTypes theType,
+                              const std::string& theName);
   ~SALOMEDSImpl_ScalarVariable();
   
   void setValue(const double theValue);
   double getValue() const;
 
   virtual std::string Save() const;
+  virtual std::string SaveToScript() const;
   virtual std::string SaveType() const;
 
   virtual void Load(const std::string& theStrValue);
index 5b1b02411a6652b6f8f3517ae05234c0d596752c..c74298441499918e3c837f4cddc84d34f54b7b20 100644 (file)
@@ -877,6 +877,60 @@ SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO,
   return RefSO;
 }
 
+//============================================================================
+/*! Function : _GetNoteBookAccessor
+ *  Purpose  : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
+ */
+//============================================================================
+string SALOMEDSImpl_Study::_GetNoteBookAccessor(){
+  return string("notebook");
+}
+
+//============================================================================
+/*! Function : _GetStudyVariablesScript
+ *  Purpose  : 
+ */
+//============================================================================
+string SALOMEDSImpl_Study::_GetStudyVariablesScript()
+{
+  string dump("");
+  
+  if(myNoteBookVars.empty())
+    return dump;
+  
+  dump += "####################################################\n";
+  dump += "##       Begin of NoteBook variables section      ##\n";
+  dump += "####################################################\n";
+
+  string set_method = _GetNoteBookAccessor()+".set(";
+  string varName;
+  string varValue;
+  for(int i = 0 ; i < myNoteBookVars.size();i++ ) {
+    varName = myNoteBookVars[i]->Name();
+    varValue = myNoteBookVars[i]->SaveToScript();
+    dump+=set_method+"\""+varName+"\", "+varValue+")\n";
+  }
+  
+  dump += "####################################################\n";
+  dump += "##        End of NoteBook variables section       ##\n";
+  dump += "####################################################\n";
+
+  return dump;
+}
+
+//============================================================================
+/*! Function : _GetNoteBookAccess
+ *  Purpose  :
+ */
+//============================================================================
+string SALOMEDSImpl_Study::_GetNoteBookAccess()
+{
+  string accessor = _GetNoteBookAccessor();
+  string notebook = "import salome_notebook\n";
+         notebook += accessor+" = salome_notebook."+accessor + "\n";
+  return notebook;       
+}
+
 bool SALOMEDSImpl_Study::IsLocked()
 {
   _errorCode = "";
@@ -1156,8 +1210,12 @@ bool SALOMEDSImpl_Study::DumpStudy(const string& thePath,
 
   fp << aBatchModeScript << ".salome_init()" << endl << endl;
 
+  fp << _GetNoteBookAccess();
+
   fp << "sys.path.insert( 0, \'" << thePath << "\')" << endl << endl;
 
+  //Dump NoteBook Variables
+  fp << _GetStudyVariablesScript();
 
   //Check if it's necessary to dump visual parameters
   bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
@@ -1495,22 +1553,21 @@ void SALOMEDSImpl_Study::SetVariable(const string& theVarName,
                                      const double theValue,
                                      const SALOMEDSImpl_GenericVariable::VariableTypes theType)
 {
-  std::map<std::string, SALOMEDSImpl_GenericVariable*>::iterator it 
-    = myNoteBookVars.find(theVarName);
+  SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
   
-  SALOMEDSImpl_ScalarVariable* aVar = NULL;
-  if( it == myNoteBookVars.end() ) {
+  if( aGVar == NULL ) {
 
-    aVar = new SALOMEDSImpl_ScalarVariable(theType);
+    SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
 
-    aVar->setValue(theValue);
-    myNoteBookVars[theVarName] = aVar;
+    aSVar->setValue(theValue);
+    myNoteBookVars.push_back(aSVar);
   }
   else {
-    if(aVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>((*it).second)) {
-      aVar->setValue(theValue);
-      if(aVar->Type() != theType)
-        aVar->setType(theType);
+    if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
+      aSVar->setValue(theValue);
+      if(aSVar->Type() != theType)
+        aSVar->setType(theType);
     }
   }
 }
@@ -1520,14 +1577,13 @@ void SALOMEDSImpl_Study::SetVariable(const string& theVarName,
  *  Purpose  :
  */
 //============================================================================
-double SALOMEDSImpl_Study::GetVariable(const string& theVarName)
+double SALOMEDSImpl_Study::GetVariableValue(const string& theVarName)
 {
-  std::map<std::string, SALOMEDSImpl_GenericVariable*>::const_iterator it = 
-    myNoteBookVars.find(theVarName);
+  SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
   
-  if(it != myNoteBookVars.end())
-    if(SALOMEDSImpl_ScalarVariable* aVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>((*it).second))
-      return aVar->getValue();
+  if(aGVar != NULL )
+    if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
+      return aSVar->getValue();
 }
 
 //============================================================================
@@ -1539,11 +1595,10 @@ bool SALOMEDSImpl_Study::IsTypeOf(const string& theVarName,
                                   SALOMEDSImpl_GenericVariable::
                                   VariableTypes theType) const
 {
-  std::map<std::string, SALOMEDSImpl_GenericVariable*>::const_iterator it = 
-    myNoteBookVars.find(theVarName);
+  SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
   
-  if(it != myNoteBookVars.end())
-    return (*it).second->Type() == theType;
+  if(aGVar != NULL )
+    return aGVar->Type() == theType;
   
   return false;
 }
@@ -1555,7 +1610,8 @@ bool SALOMEDSImpl_Study::IsTypeOf(const string& theVarName,
 //============================================================================
 bool SALOMEDSImpl_Study::IsVariable(const string& theVarName) const
 {
-  return myNoteBookVars.find(theVarName) != myNoteBookVars.end();
+  SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
+  return (aGVar != NULL);
 }
 
 //============================================================================
@@ -1566,12 +1622,10 @@ bool SALOMEDSImpl_Study::IsVariable(const string& theVarName) const
 vector<string> SALOMEDSImpl_Study::GetVariableNames() const
 {
   vector<string> aResult;
-  std::map<std::string, SALOMEDSImpl_GenericVariable*>::const_iterator it = 
-    myNoteBookVars.begin();
-
-  for(; it != myNoteBookVars.end(); it++)
-    aResult.push_back((*it).first);
 
+  for(int i = 0; i < myNoteBookVars.size(); i++)
+    aResult.push_back(myNoteBookVars[i]->Name());
+  
   return aResult;
 }
 
@@ -1580,10 +1634,26 @@ vector<string> SALOMEDSImpl_Study::GetVariableNames() const
  *  Purpose  :
  */
 //============================================================================
-void SALOMEDSImpl_Study::AddVariable(const string& theVarName,
-                                     SALOMEDSImpl_GenericVariable* theVariable)
+void SALOMEDSImpl_Study::AddVariable(SALOMEDSImpl_GenericVariable* theVariable)
 {
-  myNoteBookVars[theVarName] = theVariable;
+  myNoteBookVars.push_back(theVariable);
+}
+
+//============================================================================
+/*! Function : AddVariable
+ *  Purpose  :
+ */
+//============================================================================
+SALOMEDSImpl_GenericVariable* SALOMEDSImpl_Study::GetVariable(const std::string& theName) const
+{
+  SALOMEDSImpl_GenericVariable* aResult = NULL;
+  for(int i = 0; i < myNoteBookVars.size();i++) {
+    if(theName.compare(myNoteBookVars[i]->Name()) == 0) {
+      aResult = myNoteBookVars[i];
+      break;
+    }  
+  }
+  return aResult;
 }
 
 //============================================================================
index ed79820847ed8a2c0bd31ef583107d63fcd0dd18..98bb6c52a906c04bce344e8b76f3dfa9038c0f86 100644 (file)
@@ -70,7 +70,7 @@ private:
   std::map<std::string, SALOMEDSImpl_SObject> _mapOfSO;
   std::map<std::string, SALOMEDSImpl_SComponent> _mapOfSCO;
   std::map<std::string, DF_Label> myIORLabels;
-  std::map<std::string, SALOMEDSImpl_GenericVariable*> myNoteBookVars;
+  std::vector<SALOMEDSImpl_GenericVariable*> myNoteBookVars;
 
   SALOMEDSImpl_SObject   _FindObject(const SALOMEDSImpl_SObject& SO,
     const std::string& anObjectName,
@@ -80,6 +80,10 @@ private:
     const std::string& anObjectIOR,
     bool& _find);
 
+  std::string _GetStudyVariablesScript();
+  std::string _GetNoteBookAccessor();
+  std::string _GetNoteBookAccess();
+
 public:
 
   static SALOMEDSImpl_Study* GetStudy(const DF_Label& theLabel);
@@ -258,18 +262,19 @@ public:
                    const double theValue, 
                    const SALOMEDSImpl_GenericVariable::VariableTypes);
 
-  double GetVariable(const std::string& theVarName);
+  double GetVariableValue(const std::string& theVarName);
 
   bool IsTypeOf(const std::string& theVarName,
                 SALOMEDSImpl_GenericVariable::VariableTypes theType) const;
         
   bool IsVariable(const std::string& theVarName) const;
 
+
   std::vector<std::string> GetVariableNames() const;
 
-  void AddVariable(const std::string& theVarName,
-                   SALOMEDSImpl_GenericVariable* theVariable);
-  
+  void AddVariable(SALOMEDSImpl_GenericVariable* theVariable);
+
+  SALOMEDSImpl_GenericVariable* GetVariable(const std::string& theName) const;
 
   //Returns a callback 
   SALOMEDSImpl_Callback* GetCallback() { return _cb; }
index 9fb0cbefe476d49bc9a9b2425610dff315fa4e1a..95d1de8912a65e45adb7bd8e55853595d881791b 100644 (file)
@@ -652,18 +652,16 @@ bool SALOMEDSImpl_StudyManager::Impl_SaveAs(const string& aUrl,
       hdf_notebook_vars = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file);
       hdf_notebook_vars->CreateOnDisk();
       
-      map <std::string, SALOMEDSImpl_GenericVariable*>::const_iterator it = 
-        aStudy->myNoteBookVars.begin();
       string varValue;
       string varType;
 
-      for( ;it != aStudy->myNoteBookVars.end(); it++ ){
+      for(int i=0 ;i < aStudy->myNoteBookVars.size(); i++ ){
         // For each variable create HDF group
-        hdf_notebook_var = new HDFgroup((char*)(*it).first.c_str(),hdf_notebook_vars);
+        hdf_notebook_var = new HDFgroup((char*)aStudy->myNoteBookVars[i]->Name().c_str(),hdf_notebook_vars);
         hdf_notebook_var->CreateOnDisk();
 
         // Save Variable type
-        varType = (*it).second->SaveType();
+        varType = aStudy->myNoteBookVars[i]->SaveType();
         name_len = (hdf_int32) varType.length();
         size[0] = name_len +1 ;
         hdf_dataset = new HDFdataset("VARIABLE_TYPE",hdf_notebook_var,HDF_STRING,size,1);
@@ -673,7 +671,7 @@ bool SALOMEDSImpl_StudyManager::Impl_SaveAs(const string& aUrl,
         hdf_dataset=0; //will be deleted by hdf_sco_group destructor
         
         // Save Variable value
-        varValue = (*it).second->Save();
+        varValue = aStudy->myNoteBookVars[i]->Save();
         name_len = (hdf_int32) varValue.length();
         size[0] = name_len +1 ;
         hdf_dataset = new HDFdataset("VARIABLE_VALUE",hdf_notebook_var,HDF_STRING,size,1);
@@ -1315,11 +1313,11 @@ void ReadNoteBookVariables(SALOMEDSImpl_Study* theStudy, HDFgroup* theGroup)
 
       //Create variable and add it in the study
       SALOMEDSImpl_GenericVariable* aVariable = 
-        new SALOMEDSImpl_ScalarVariable(aVarType);
+        new SALOMEDSImpl_ScalarVariable(aVarType,string(aVarName));
       aVariable->Load(string(currentVarValue));
       delete currentVarValue;
       
-      theStudy->AddVariable(string(aVarName),aVariable);
+      theStudy->AddVariable(aVariable);
     }
   }
   theGroup->CloseOnDisk();