Salome HOME
ADD a end user module (services.py) to help the manipulation of SALOME KERNEL service...
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_Study.cxx
index a3450207ef287298ba7542de6bd8b204ea26dc42..76c9d28478b9321e7ca7a7f83725f685997deb9b 100644 (file)
@@ -1,23 +1,23 @@
-//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  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 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.
+// 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
+// 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 //  File   : SALOMEDSImpl_Study.cxx
@@ -45,6 +45,9 @@
 #include <fstream>
 #include <sstream>
 
+// comment out the following define to enable \t symbols in in the python dump files
+#define WITHOUT_TABS
+
 #define DIRECTORYID       16661
 #define FILELOCALID       26662
 #define FILEID            "FILE: "
@@ -1170,6 +1173,31 @@ DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const std::string& theEntry,
   return anAttr;
 }
 
+//! number of spaces for indentation in Python dump files (to replace \t symbols)
+static const int indent_size = 2;
+
+static std::string replace_tabs( const std::string& in )
+{
+  std::string out = in;
+#ifdef WITHOUT_TABS
+  size_t pos = out.find( '\t' );
+  while ( pos != std::string::npos ) {
+    out.replace( pos, 1, indent_size, ' ' );
+    pos = out.find( '\t' );
+  }
+#endif
+  return out;
+}
+
+static std::string GetComponentHeader(const char* theComponentName)
+{
+  std::stringstream txt;
+  txt << "###" << std::endl;
+  txt << "### " << theComponentName << " component" << std::endl;
+  txt << "###" << std::endl;
+  return txt.str();
+}
+
 //============================================================================
 /*! Function : DumpStudy
  *  Purpose  :
@@ -1178,6 +1206,7 @@ DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const std::string& theEntry,
 bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath,
                                    const std::string& theBaseName,
                                    bool isPublished,
+                                   bool isMultiFile,
                                    SALOMEDSImpl_DriverFactory* theFactory)
 {
   _errorCode = "";
@@ -1226,29 +1255,33 @@ bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath,
     return false;
   }
 
+  std::stringstream sfp;
+
   std::string aBatchModeScript = "salome";
 
   //Output to the main Study script required Python modules import,
   //set sys.path and add a creation of the study.
 
   // dump header
-  fp << GetDumpStudyComment() << std::endl;
+  sfp << GetDumpStudyComment() << std::endl;
 
   // global imports
-  fp << "import sys" << std::endl;
-  fp << "import " << aBatchModeScript << std::endl << std::endl;
+  sfp << "import sys" << std::endl;
+  sfp << "import " << aBatchModeScript << std::endl << std::endl;
 
   // initialization function
-  fp << aBatchModeScript << ".salome_init()" << std::endl << std::endl;
+  sfp << aBatchModeScript << ".salome_init()" << std::endl;
+  if ( !isMultiFile )
+    sfp << "theStudy = salome.myStudy" <<std::endl << std::endl;
 
   // notebook initialization
-  fp << _GetNoteBookAccess();
+  sfp << _GetNoteBookAccess();
 
   // extend sys.path with the directory where the script is being dumped to
-  fp << "sys.path.insert( 0, r\'" << thePath << "\')" << std::endl << std::endl;
+  sfp << "sys.path.insert( 0, r\'" << thePath << "\')" << std::endl << std::endl;
 
   // dump NoteBook variables
-  fp << _GetStudyVariablesScript();
+  sfp << _GetStudyVariablesScript();
 
   // dump visual parameters if necessary
   bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
@@ -1256,7 +1289,7 @@ bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath,
   if(isDumpVisuals) {
     lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
     if(lastSavePoint > 0) {
-      fp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << std::endl << std::endl;
+      sfp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << std::endl << std::endl;
     }
   }
 
@@ -1300,61 +1333,77 @@ bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath,
 
     bool isValidScript;
     long aStreamLength  = 0;
-    SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(this, isPublished, isValidScript, aStreamLength);
+    SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(this, isPublished, isMultiFile, isValidScript, aStreamLength);
     if ( !isValidScript )
       isOk = false;
 
-    //Create a file that will contain the component specific script
-    std::fstream fp2;
+    std::stringstream sfp2;
+    
+    //Output the Python script generated by the component in the newly created file.
+    if ( isMultiFile )
+      sfp2 << GetDumpStudyComment( aCompType.c_str() ) << std::endl;
+    else
+      sfp2 << GetComponentHeader( aCompType.c_str() ) << std::endl;
+    sfp2 << aStream->Data();
+    
+    if ( isMultiFile ) {
+      //Create a file that will contain the component specific script
+      std::fstream fp2;
 #ifdef WIN32
-    aFileName=thePath+std::string("\\");
+      aFileName=thePath+std::string("\\");
 #else
-    aFileName=thePath+std::string("/");
+      aFileName=thePath+std::string("/");
 #endif
-    std::string aScriptName;
-    aScriptName += theBaseName;
-    aScriptName += "_";
-    aScriptName += aCompType;
-
-    aFileName += aScriptName+ std::string(".py");
-    aSeqOfFileNames.push_back(aFileName);
-
-    fp2.open(aFileName.c_str(), std::ios::out);
-
+      std::string aScriptName;
+      aScriptName += theBaseName;
+      aScriptName += "_";
+      aScriptName += aCompType;
+      
+      aFileName += aScriptName+ std::string(".py");
+      aSeqOfFileNames.push_back(aFileName);
+      
+      fp2.open(aFileName.c_str(), std::ios::out);
+      
 #ifdef WIN32
-    isOpened = fp2.is_open();
+      isOpened = fp2.is_open();
 #else
-    isOpened = fp2.rdbuf()->is_open();
+      isOpened = fp2.rdbuf()->is_open();
 #endif
+      
+      if(!isOpened) {
+       _errorCode = std::string("Can't create a file ")+aFileName;
+       SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
+       return false;
+      }
+     
+      // replace '\t' symbols
+      fp2 << replace_tabs( sfp2.str() );
 
-    if(!isOpened) {
-      _errorCode = std::string("Can't create a file ")+aFileName;
-      SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
-      return false;
-    }
+      fp2.close();
 
-    //Output the Python script generated by the component in the newly created file.
-    fp2 << GetDumpStudyComment( aCompType.c_str() ) << std::endl;
-    fp2 << aStream->Data();
-    fp2.close();
+      //Add to the main script a call to RebuildData of the generated by the component the Python script
+      sfp << "import " << aScriptName << std::endl;
+      sfp << aScriptName << ".RebuildData(" << aBatchModeScript << ".myStudy)" << std::endl;
+    }
+    else
+      sfp << sfp2.str();
 
     if(aStream) delete aStream;
-
-    //Add to the main script a call to RebuildData of the generated by the component the Python script
-    fp << "import " << aScriptName << std::endl;
-    fp << aScriptName << ".RebuildData(" << aBatchModeScript << ".myStudy)" << std::endl;
   }
 
-  fp << std::endl;
-  fp << "if salome.sg.hasDesktop():" << std::endl;
-  fp << "\tsalome.sg.updateObjBrowser(1)" << std::endl;
+  sfp << std::endl;
+  sfp << "if salome.sg.hasDesktop():" << std::endl;
+  sfp << "\tsalome.sg.updateObjBrowser(1)" << std::endl;
 
   if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
-    fp << "\tiparameters.getSession().restoreVisualState(1)" << std::endl;
+    sfp << "\tiparameters.getSession().restoreVisualState(1)" << std::endl;
   }
 
-
+  // replace '\t' symbols
+  fp << replace_tabs( sfp.str() );
+  
   fp.close();
+
   return isOk;
 }
 
@@ -1477,12 +1526,24 @@ void SALOMEDSImpl_Study::Modify()
 //============================================================================
 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
 {
-  if (theSavePoint < 0) return NULL;
+  if (theSavePoint < -1) return NULL;
   SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
   SALOMEDSImpl_SObject so = FindComponent((char*)theID);
   if (!so) so = builder->NewComponent((char*)theID);
   SALOMEDSImpl_AttributeParameter* attParam = NULL;
 
+  if (theSavePoint == -1) {
+    int ctag = 1;
+    DF_Label savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
+    DF_Label prevPointLabel;
+    while ( !savePointLabel.IsNull() ) {
+      ctag++;
+      prevPointLabel = savePointLabel;
+      savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
+    }
+    if ( !prevPointLabel.IsNull() )
+      so = GetSObject( prevPointLabel );
+  }
   if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
     DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
     if ( !savePointLabel.IsNull() )
@@ -1511,7 +1572,7 @@ SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const c
                                                                          const char* theModuleName,
                                                                          int theSavePoint)
 {
-  if(theSavePoint <= 0) return NULL;
+  if(theSavePoint < -1) return NULL;
   SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
   SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
   SALOMEDSImpl_AttributeParameter* par = NULL;