]> SALOME platform Git repositories - modules/geom.git/blobdiff - src/GEOM/GEOM_Engine.cxx
Salome HOME
0021672: [CEA 565] Dump Study from script
[modules/geom.git] / src / GEOM / GEOM_Engine.cxx
index 368323d2230410b2bccd47673ea925ca0f4a7b11..cc284959899b3c6d8c5b285dbfd65abf269ba376 100644 (file)
@@ -1,23 +1,23 @@
-//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2012  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
 //
 
 #ifdef WNT
@@ -33,6 +33,8 @@
 #include "GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient.hxx"
 #include "GEOM_PythonDump.hxx"
 
+#include <Basics_OCCTVersion.hxx>
+
 #include "utilities.h"
 
 #include <Basics_Utils.hxx>
@@ -45,7 +47,6 @@
 #include <TDataStd_ChildNodeIterator.hxx>
 #include <TFunction_Driver.hxx>
 #include <TFunction_DriverTable.hxx>
-#include <TDataStd_HArray1OfByte.hxx>
 #include <TDataStd_ByteArray.hxx>
 #include <TDataStd_UAttribute.hxx>
 #include <TDF_ChildIterator.hxx>
 #include <TColStd_MapOfTransient.hxx>
 #include <TColStd_HSequenceOfInteger.hxx>
 
+#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
+#include <TColStd_HArray1OfByte.hxx>
+#include <TColStd_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
+#else
+#include <TDataStd_HArray1OfByte.hxx>
 #include <Interface_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
+#endif
+
 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
 
 #include <set>
@@ -141,7 +149,7 @@ void PublishObject (TObjectData&                              theObjectData,
                     TSting2ObjDataMap&                        theEntry2ObjData,
                     const TSting2ObjDataPtrMap&               theStEntry2ObjDataPtr,
                     Resource_DataMapOfAsciiStringAsciiString& theNameToEntry,
-                    TSting2StringMap&                         theEntryToCmdMap,
+                    std::map< int, TCollection_AsciiString >& theEntryToCmdMap,
                     std::set<TCollection_AsciiString>&        theMapOfPublished);
 
 namespace
@@ -211,7 +219,7 @@ void GEOM_Engine::SetEngine(GEOM_Engine* theEngine) { TheEngine = theEngine; }
 GEOM_Engine::GEOM_Engine()
 {
   TFunction_DriverTable::Get()->AddDriver(GEOM_Object::GetSubShapeID(), new GEOM_SubShapeDriver());
-
+  
   _OCAFApp = new GEOM_Application();
   _UndoLimit = 10;
 }
@@ -230,7 +238,12 @@ GEOM_Engine::~GEOM_Engine()
     RemoveObject(*objit);
 
   //Close all documents not closed
-  for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
+#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
+  TColStd_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
+#else
+  Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
+#endif
+  for (; anItr.More(); anItr.Next())
     Close(anItr.Key());
 
   _mapIDDocument.Clear();
@@ -264,12 +277,16 @@ Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID, bool force)
 //=============================================================================
 int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
 {
-  if(theDocument.IsNull()) return -1;
-  for(Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr(_mapIDDocument); anItr.More(); anItr.Next())
-    if(anItr.Value() == theDocument) return anItr.Key();
+  if (theDocument.IsNull()) return -1;
+#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
+  TColStd_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
+#else
+  Interface_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
+#endif
+  for (; anItr.More(); anItr.Next())
+    if (anItr.Value() == theDocument) return anItr.Key();
 
   return -1;
-
 }
 
 //=============================================================================
@@ -377,7 +394,7 @@ Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
   aSSI.SetIndices(theIndices);
 
   try {
-#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
+#if OCC_VERSION_LARGE > 0x06010000
     OCC_CATCH_SIGNALS;
 #endif
     GEOM_Solver aSolver (GEOM_Engine::GetEngine());
@@ -397,7 +414,7 @@ Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape,
   if (_objects.IsBound(anID)) _objects.UnBind(anID);
   _objects.Bind(anID, anObject);
 
-  // Put this subshape in the list of subshapes of theMainShape
+  // Put this sub-shape in the list of sub-shapes of theMainShape
   aMainShape->AddSubShapeReference(aFunction);
 
   GEOM::TPythonDump pd (aFunction);
@@ -433,7 +450,7 @@ bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
   TCollection_AsciiString anID = BuildIDFromObject(theObject);
   if (_objects.IsBound(anID)) _objects.UnBind(anID);
 
-  // If subshape, remove it from the list of subshapes of its main shape
+  // If sub-shape, remove it from the list of sub-shapes of its main shape
   if (!theObject->IsMainShape()) {
     Handle(GEOM_Function) aFunction = theObject->GetFunction(1);
     GEOM_ISubShape aSSI (aFunction);
@@ -506,7 +523,11 @@ bool GEOM_Engine::Save(int theDocID, char* theFileName)
 bool GEOM_Engine::Load(int theDocID, char* theFileName)
 {
   Handle(TDocStd_Document) aDoc;
-  if(_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
+#if OCC_VERSION_LARGE > 0x06050100 // For OCCT6.5.2 and higher
+  if (_OCAFApp->Open(theFileName, aDoc) != PCDM_RS_OK) {
+#else
+  if (_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
+#endif
     return false;
   }
 
@@ -584,8 +605,7 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
   aScript += "import SALOMEDS\n\n";
   if( isMultiFile )
     aScript += "def RebuildData(theStudy):";
-  else
-    aScript += "theStudy = salome.myStudy";
+
   aScript += "\n\tgeompy.init_geom(theStudy)\n";
 
   AddTextures(theDocID, aScript);
@@ -651,14 +671,22 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
         continue;
       // add function description before dump
       if (!aCurScript.IsEmpty())
+      {
+        if ( aFunction->GetDriverGUID() == GEOM_Object::GetSubShapeID() )
+          // avoid repeated SubShape...() command at the end
+          if (aFuncScript.Location( aCurScript,
+                                    aFuncScript.Length() - aCurScript.Length(),
+                                    aFuncScript.Length()))
+              continue;
         aFuncScript += aCurScript;
+      }
       if (isDumpCollected ) {
         // Replace entries by the names
         ReplaceEntriesByNames( aFuncScript, aEntry2ObjData, isPublished,
                                aObjListToPublish, objectCounter, aNameToEntry );
 
         // publish collected objects
-        TSting2StringMap anEntryToCmdMap; // sort publishing commands by study entry
+        std::map< int, TCollection_AsciiString > anEntryToCmdMap; // sort publishing commands by study entry
         int i = 1, n = aObjListToPublish.Length();
         for ( ; i <= n; i++ )
         {
@@ -667,7 +695,7 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
                          aNameToEntry, anEntryToCmdMap, anIgnoreObjMap );
         }
         // add publishing commands to the script
-        TSting2StringMap::iterator anEntryToCmd = anEntryToCmdMap.begin();
+        std::map< int, TCollection_AsciiString >::iterator anEntryToCmd = anEntryToCmdMap.begin();
         for ( ; anEntryToCmd != anEntryToCmdMap.end(); ++anEntryToCmd )
           aFuncScript += anEntryToCmd->second;
 
@@ -709,7 +737,7 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
   TSting2ObjDataPtrMap::iterator aStEntry2ObjDataPtrIt;
   if ( isPublished )
   {
-    TSting2StringMap anEntryToCmdMap; // sort publishing commands by object entry
+    std::map< int, TCollection_AsciiString > anEntryToCmdMap; // sort publishing commands by object entry
 
     for (aStEntry2ObjDataPtrIt  = aStEntry2ObjDataPtr.begin();
          aStEntry2ObjDataPtrIt != aStEntry2ObjDataPtr.end();
@@ -722,11 +750,28 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
                      aNameToEntry, anEntryToCmdMap, anIgnoreObjMap );
     }
     // add publishing commands to the script
-    TSting2StringMap::iterator anEntryToCmd = anEntryToCmdMap.begin();
+    std::map< int, TCollection_AsciiString >::iterator anEntryToCmd = anEntryToCmdMap.begin();
     for ( ; anEntryToCmd != anEntryToCmdMap.end(); ++anEntryToCmd )
       aScript += anEntryToCmd->second;
   }
 
+  //RNV: issue 16219: EDF PAL 469: "RemoveFromStudy" Function
+  //Add unpublish command if need
+  TCollection_AsciiString unpublishCmd("\n");
+  if(isMultiFile)
+    unpublishCmd += "\t";
+  unpublishCmd += "geompy.hideInStudy(";
+  
+  for (aStEntry2ObjDataPtrIt  = aStEntry2ObjDataPtr.begin();
+       aStEntry2ObjDataPtrIt != aStEntry2ObjDataPtr.end();
+       ++aStEntry2ObjDataPtrIt)
+    {
+      TObjectData* data = aStEntry2ObjDataPtrIt->second;      
+      if ( data->_unpublished && !data->_pyName.IsEmpty() ) {
+       aScript +=  unpublishCmd + data->_pyName + ")";
+      }
+    }    
+  
   //aScript += "\n\tpass\n";
   aScript += "\n";
   aValidScript = true;
@@ -745,7 +790,7 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
       globalVars += ", ";
     globalVars += data->_pyName;
   }
-  if ( !globalVars.IsEmpty() ) {
+  if ( isMultiFile && !globalVars.IsEmpty() ) {
     globalVars.Insert( 1, "\n\tglobal " );
     aScript.Insert( posToInsertGlobalVars, globalVars );
   }
@@ -790,7 +835,11 @@ Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
 #define TEXTURE_LABEL_DATA     5
 
 int GEOM_Engine::addTexture(int theDocID, int theWidth, int theHeight,
+#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
+                            const Handle(TColStd_HArray1OfByte)& theTexture,
+#else
                             const Handle(TDataStd_HArray1OfByte)& theTexture,
+#endif
                             const TCollection_AsciiString& theFileName)
 {
   Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
@@ -835,11 +884,19 @@ int GEOM_Engine::addTexture(int theDocID, int theWidth, int theHeight,
   return aTextureID;
 }
 
+#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
+Handle(TColStd_HArray1OfByte) GEOM_Engine::getTexture(int theDocID, int theTextureID,
+#else
 Handle(TDataStd_HArray1OfByte) GEOM_Engine::getTexture(int theDocID, int theTextureID,
-                                                       int& theWidth, int& theHeight,
-                                                       TCollection_AsciiString& theFileName)
+#endif
+                                                      int& theWidth, int& theHeight,
+                                                      TCollection_AsciiString& theFileName)
 {
+#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
+  Handle(TColStd_HArray1OfByte) anArray;
+#else
   Handle(TDataStd_HArray1OfByte) anArray;
+#endif
   theWidth = theHeight = 0;
 
   Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
@@ -1150,7 +1207,7 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
       else if(i == aTotalNbParams)
       {
         aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
-        aEndPos = aCommand.Location(C_BRACKET, 1, aCommand.Length());
+        aEndPos = aCommand.Location(C_BRACKET, aStartPos , aCommand.Length());
       }
       //Replace other parameters (bettwen two ',' characters)
       else if(i != aFirstParam && i != aTotalNbParams )
@@ -1210,6 +1267,12 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
             else
               aEndParamPos = aSection.Length() + 1;
 
+            if(MYDEBUG)
+              cout<<"aParamIndex: "<<aParamIndex<<" aStartParamPos: " <<aStartParamPos<<" aEndParamPos: "<<aEndParamPos<<endl;
+
+           if ( aStartParamPos == aEndParamPos)
+             continue;
+
             aParameter = aSection.SubString(aStartParamPos, aEndParamPos-1);
             if(MYDEBUG)
               cout<<"aParameter: "<<aParameter<<endl;
@@ -1321,9 +1384,9 @@ void ReplaceEntriesByNames (TCollection_AsciiString&                  theScript,
         healPyName( data._pyName, anEntry, aNameToEntry);
       }
       else {
-       do {
-         data._pyName = aBaseName + TCollection_AsciiString(++objectCounter);
-       } while(aNameToEntry.IsBound(data._pyName));
+        do {
+          data._pyName = aBaseName + TCollection_AsciiString(++objectCounter);
+        } while(aNameToEntry.IsBound(data._pyName));
       }
     }
     
@@ -1439,7 +1502,11 @@ void AddObjectColors (int                      theDocID,
   }
 }
 
-static TCollection_AsciiString pack_data(const Handle(TDataStd_HArray1OfByte)& aData )
+#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
+static TCollection_AsciiString pack_data (const Handle(TColStd_HArray1OfByte)& aData)
+#else
+static TCollection_AsciiString pack_data (const Handle(TDataStd_HArray1OfByte)& aData)
+#endif
 {
   TCollection_AsciiString stream;
   if (!aData.IsNull()) {
@@ -1467,7 +1534,12 @@ void AddTextures (int theDocID, TCollection_AsciiString& theScript)
       if (*it <= 0) continue;
       Standard_Integer aWidth, aHeight;
       TCollection_AsciiString aFileName;
-      Handle(TDataStd_HArray1OfByte) aTexture = engine->getTexture(theDocID, *it, aWidth, aHeight, aFileName);
+#if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
+      Handle(TColStd_HArray1OfByte) aTexture =
+#else
+      Handle(TDataStd_HArray1OfByte) aTexture =
+#endif
+        engine->getTexture(theDocID, *it, aWidth, aHeight, aFileName);
       if (aWidth > 0 && aHeight > 0 && !aTexture.IsNull() && aTexture->Length() > 0 ) {
         TCollection_AsciiString aCommand = "\n\t";
         aCommand += "texture_map["; aCommand += *it; aCommand += "] = ";
@@ -1498,7 +1570,7 @@ void PublishObject (TObjectData&                              theObjectData,
                     TSting2ObjDataMap&                        theEntry2ObjData,
                     const TSting2ObjDataPtrMap&               theStEntry2ObjDataPtr,
                     Resource_DataMapOfAsciiStringAsciiString& theNameToEntry,
-                    TSting2StringMap&                         theEntryToCmdMap,
+                    std::map< int, TCollection_AsciiString >& theEntryToCmdMap,
                     std::set< TCollection_AsciiString>&       theIgnoreMap)
 {
   if ( theObjectData._studyEntry.IsEmpty() )
@@ -1517,6 +1589,8 @@ void PublishObject (TObjectData&                              theObjectData,
   if ( stEntry2DataPtr != theStEntry2ObjDataPtr.end() )
     aFatherData = stEntry2DataPtr->second;
 
+  const int geomObjDepth = 3;
+
   // treat multiply published object
   if ( theObjectData._pyName.IsEmpty() )
   {
@@ -1530,9 +1604,8 @@ void PublishObject (TObjectData&                              theObjectData,
     aCreationCommand += theObjectData._pyName + " = " + data0._pyName;
 
     // store aCreationCommand before publishing commands
-    TCollection_AsciiString mapKey(" ");
-    mapKey += theObjectData._studyEntry;
-    theEntryToCmdMap.insert( std::make_pair( mapKey, aCreationCommand ));
+    int tag = theObjectData._entry.Token( ":", geomObjDepth ).IntegerValue();
+    theEntryToCmdMap.insert( std::make_pair( tag + 2*theEntry2ObjData.size(), aCreationCommand ));
   }
 
   // make a command
@@ -1546,7 +1619,8 @@ void PublishObject (TObjectData&                              theObjectData,
   aCommand += theObjectData._pyName + ", '" + theObjectData._name + "' )";
 
   // bind a command to the study entry
-  theEntryToCmdMap.insert( std::make_pair( theObjectData._studyEntry, aCommand ));
+  int tag = theObjectData._entry.Token( ":", geomObjDepth ).IntegerValue();
+  theEntryToCmdMap.insert( std::make_pair( tag, aCommand ));
 
   theObjectData._studyEntry.Clear(); // not to publish any more
 }