Salome HOME
Copyright update 2022
[modules/geom.git] / src / GEOM / GEOM_Engine.cxx
index dd4bc10126af391032cf63bed182c38951086002..46426c6fdeeca00566a6644d33fc924fffe3de2b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  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
@@ -37,6 +37,7 @@
 #include "utilities.h"
 
 #include <Basics_Utils.hxx>
+#include <Basics_OCCTVersion.hxx>
 
 #include <TDF_Tool.hxx>
 #include <TDF_Data.hxx>
@@ -62,7 +63,9 @@
 
 #include <TColStd_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
 
+#if OCC_VERSION_LARGE < 0x07050000
 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
+#endif
 
 #include <BinDrivers.hxx>
 #include <StdDrivers_DocumentRetrievalDriver.hxx>
@@ -82,6 +85,7 @@
 
 #ifdef _DEBUG_
 static int MYDEBUG = 0;
+using namespace std;
 #else
 static int MYDEBUG = 0;
 #endif
@@ -104,31 +108,16 @@ static GEOM_Engine* TheEngine = NULL;
 
 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_BaseObject)& theObject)
 {
-  TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
+  TCollection_AsciiString anEntry;
   TDF_Tool::Entry(theObject->GetEntry(), anEntry);
-  anID+=(TCollection_AsciiString("_")+anEntry);
-  return anID;
-}
-
-static TCollection_AsciiString BuildID(Standard_Integer theDocID, const char* theEntry)
-{
-  TCollection_AsciiString anID(theDocID);
-  anID+=(TCollection_AsciiString("_")+theEntry);
-  return anID;
-}
-
-static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
-{
-  TCollection_AsciiString aDocID = theID.Token("_");
-  if(aDocID.Length() < 1) return -1;
-  return aDocID.IntegerValue();
+  return anEntry;
 }
 
 bool ProcessFunction(Handle(GEOM_Function)&             theFunction,
                      TCollection_AsciiString&           theScript,
                      TCollection_AsciiString&           theAfterScript,
                      const TVariablesList&              theVariables,
-                     const bool                         theIsPublished,
+                     const bool                         /*theIsPublished*/,
                      TDF_LabelMap&                      theProcessed,
                      std::set<TCollection_AsciiString>& theIgnoreObjs,
                      bool&                              theIsDumpCollected);
@@ -150,11 +139,10 @@ void ReplaceEntriesByNames (TCollection_AsciiString&                  theScript,
                             Standard_Integer&                         objectCounter,
                             Resource_DataMapOfAsciiStringAsciiString& aNameToEntry);
 
-void AddObjectColors (int                      theDocID,
-                      TCollection_AsciiString& theScript,
+void AddObjectColors (TCollection_AsciiString& theScript,
                       const TSting2ObjDataMap& theEntry2ObjData);
 
-void AddTextures (int theDocID, TCollection_AsciiString& theScript);
+void AddTextures (TCollection_AsciiString& theScript);
 
 void PublishObject (TObjectData&                              theObjectData,
                     TSting2ObjDataMap&                        theEntry2ObjData,
@@ -256,14 +244,8 @@ GEOM_Engine::~GEOM_Engine()
   for(objit = objs.begin(); objit != objs.end(); ++objit)
     RemoveObject(*objit);
 
-  //Close all documents not closed
-  TColStd_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
-  for (; anItr.More(); anItr.Next())
-  {
-    Close(anItr.Key());
-    anItr.Initialize( _mapIDDocument ); // anItr becomes invalid at _mapIDDocument.UnBind(docId)
-  }
-  _mapIDDocument.Clear();
+  //Close document
+  Close();
   _objects.Clear();
 }
 
@@ -272,53 +254,35 @@ GEOM_Engine::~GEOM_Engine()
  *  GetDocument
  */
 //=============================================================================
-Handle(TDocStd_Document) GEOM_Engine::GetDocument(int theDocID, bool force)
+Handle(TDocStd_Document) GEOM_Engine::GetDocument(bool force)
 {
   Handle(TDocStd_Document) aDoc;
-  if(_mapIDDocument.IsBound(theDocID)) {
-    aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
+  if (_document) {
+    aDoc = _document;
   }
   else if (force) {
     _OCAFApp->NewDocument("BinOcaf", aDoc);
     aDoc->SetUndoLimit(_UndoLimit);
-    _mapIDDocument.Bind(theDocID, aDoc);
-    TDataStd_Integer::Set(aDoc->Main(), theDocID);
+    _document = aDoc;
   }
   return aDoc;
 }
 
-//=============================================================================
-/*!
- *  GetDocID
- */
-//=============================================================================
-int GEOM_Engine::GetDocID(Handle(TDocStd_Document) theDocument)
-{
-  if (theDocument.IsNull()) return -1;
-  TColStd_DataMapIteratorOfDataMapOfIntegerTransient anItr (_mapIDDocument);
-  for (; anItr.More(); anItr.Next())
-    if (anItr.Value() == theDocument) return anItr.Key();
-
-  return -1;
-}
-
 //=============================================================================
 /*!
  *  GetObject
  */
 //=============================================================================
 
-Handle(GEOM_BaseObject) GEOM_Engine::GetObject(int theDocID, const char* theEntry, bool force)
+Handle(GEOM_BaseObject) GEOM_Engine::GetObject(const char* theEntry, bool force)
 {
   Handle(GEOM_BaseObject) anObject;
 
-  TCollection_AsciiString anID = BuildID(theDocID, theEntry);
-
-  if (_objects.IsBound(anID)) {
-    anObject = Handle(GEOM_BaseObject)::DownCast(_objects(anID));
+  if (_objects.IsBound(theEntry)) {
+    anObject = Handle(GEOM_BaseObject)::DownCast(_objects(theEntry));
   }
   else if (force) {
-    Handle(TDocStd_Document) aDoc = GetDocument(theDocID, force);
+    Handle(TDocStd_Document) aDoc = GetDocument(force);
     if ( !aDoc.IsNull()) {
       TDF_Label aLabel;
       TDF_Tool::Label(aDoc->Main().Data(), theEntry, aLabel, Standard_True);
@@ -329,7 +293,7 @@ Handle(GEOM_BaseObject) GEOM_Engine::GetObject(int theDocID, const char* theEntr
         case GEOM_FIELD_STEP_OBJTYPE: anObject = new GEOM_FieldStep(aLabel); break;
         default:                      anObject = new GEOM_Object   (aLabel);
         }
-        _objects.Bind(anID, anObject);
+        _objects.Bind(theEntry, anObject);
       }
     }
   }
@@ -343,22 +307,19 @@ Handle(GEOM_BaseObject) GEOM_Engine::GetObject(int theDocID, const char* theEntr
  */
 //=============================================================================
 
-Handle(GEOM_BaseObject) GEOM_Engine::AddBaseObject(int theDocID, int theType)
+Handle(GEOM_BaseObject) GEOM_Engine::AddBaseObject(int theType)
 {
-  Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
+  Handle(TDocStd_Document) aDoc = GetDocument();
   Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
 
   // NPAL18604: use existing label to decrease memory usage,
   //            if this label has been freed (object deleted)
   bool useExisting = false;
   TDF_Label aChild;
-  if (_freeLabels.find(theDocID) != _freeLabels.end()) {
-    std::list<TDF_Label>& aFreeLabels = _freeLabels[theDocID];
-    if (!aFreeLabels.empty()) {
-      useExisting = true;
-      aChild = aFreeLabels.front();
-      aFreeLabels.pop_front();
-    }
+  if (!_freeLabels.empty()) {
+    useExisting = true;
+    aChild = _freeLabels.front();
+    _freeLabels.pop_front();
   }
   if (!useExisting) {
     // create new label
@@ -386,9 +347,9 @@ Handle(GEOM_BaseObject) GEOM_Engine::AddBaseObject(int theDocID, int theType)
  */
 //================================================================================
 
-Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
+Handle(GEOM_Object) GEOM_Engine::AddObject(int theType)
 {
-  return Handle(GEOM_Object)::DownCast( AddBaseObject(theDocID, theType) );
+  return Handle(GEOM_Object)::DownCast( AddBaseObject(theType) );
 }
 
 //=============================================================================
@@ -403,21 +364,17 @@ Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object)              th
 {
   if (theMainShape.IsNull() || theIndices.IsNull()) return NULL;
 
-  Handle(TDocStd_Document) aDoc = GetDocument(theMainShape->GetDocID());
+  Handle(TDocStd_Document) aDoc = GetDocument();
   Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
 
   // NPAL18604: use existing label to decrease memory usage,
   //            if this label has been freed (object deleted)
   bool useExisting = false;
-  TDF_Label aChild;
-  int aDocID = theMainShape->GetDocID();
-  if (_freeLabels.find(aDocID) != _freeLabels.end()) {
-    std::list<TDF_Label>& aFreeLabels = _freeLabels[aDocID];
-    if (!aFreeLabels.empty()) {
-      useExisting = true;
-      aChild = aFreeLabels.front();
-      aFreeLabels.pop_front();
-    }
+  TDF_Label aChild;;
+  if (!_freeLabels.empty()) {
+    useExisting = true;
+    aChild = _freeLabels.front();
+    _freeLabels.pop_front();
   }
   if (!useExisting) {
     // create new label
@@ -478,10 +435,13 @@ bool GEOM_Engine::RemoveObject(Handle(GEOM_BaseObject)& theObject)
 {
   if (theObject.IsNull()) return false;
 
-  int aDocID = theObject->GetDocID();
-  if(!_mapIDDocument.IsBound(aDocID))
+  if(!_document)
     return false;  // document is closed...
 
+  TDF_Label aLabel = theObject->GetEntry();
+  if ( aLabel == aLabel.Root() )
+    return false; // already removed object
+
   //Remove an object from the map of available objects
   TCollection_AsciiString anID = BuildIDFromObject(theObject);
   if (_objects.IsBound(anID)) {
@@ -510,20 +470,17 @@ bool GEOM_Engine::RemoveObject(Handle(GEOM_BaseObject)& theObject)
       aNode->Remove();
   }
 
-  TDF_Label aLabel = theObject->GetEntry();
   aLabel.ForgetAllAttributes(Standard_True);
 
   // Remember the label to reuse it then
-  std::list<TDF_Label>& aFreeLabels = _freeLabels[aDocID];
-  if ( aFreeLabels.empty() || aFreeLabels.back() != aLabel )
-    aFreeLabels.push_back(aLabel);
+  if ( _freeLabels.empty() || _freeLabels.back() != aLabel )
+    _freeLabels.push_back(aLabel);
 
   // we can't explicitly delete theObject. At least prevent its functioning
   // as an alive object when aLabel is reused for a new object
   theObject->_label = aLabel.Root();
   theObject->_ior.Clear();
-  theObject->_parameters.Clear();
-  theObject->_docID = -1;
+  theObject->_parameters.Clear();;
 
   theObject.Nullify();
 
@@ -535,9 +492,9 @@ bool GEOM_Engine::RemoveObject(Handle(GEOM_BaseObject)& theObject)
  *  Undo
  */
 //=============================================================================
-void GEOM_Engine::Undo(int theDocID)
+void GEOM_Engine::Undo()
 {
-  GetDocument(theDocID)->Undo();
+  GetDocument()->Undo();
 }
 
 //=============================================================================
@@ -545,9 +502,9 @@ void GEOM_Engine::Undo(int theDocID)
  *  Redo
  */
 //=============================================================================
-void GEOM_Engine::Redo(int theDocID)
+void GEOM_Engine::Redo()
 {
-  GetDocument(theDocID)->Redo();
+  GetDocument()->Redo();
 }
 
 //=============================================================================
@@ -555,14 +512,17 @@ void GEOM_Engine::Redo(int theDocID)
  *  Save
  */
 //=============================================================================
-bool GEOM_Engine::Save(int theDocID, const char* theFileName)
+bool GEOM_Engine::Save(const char* theFileName)
 {
-  if(!_mapIDDocument.IsBound(theDocID)) return false;
-  Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
+  if(!_document) return false;
 
-  _OCAFApp->SaveAs(aDoc, theFileName);
+#if defined(WIN32) && defined(UNICODE)
+  std::wstring aFileName = Kernel_Utils::utf8_decode_s(theFileName);
+#else
+  std::string aFileName = theFileName;
+#endif
 
-  return true;
+  return _OCAFApp->SaveAs( _document, aFileName.c_str() ) == PCDM_SS_OK;
 }
 
 //=============================================================================
@@ -570,10 +530,15 @@ bool GEOM_Engine::Save(int theDocID, const char* theFileName)
  *  Load
  */
 //=============================================================================
-bool GEOM_Engine::Load(int theDocID, const char* theFileName)
+bool GEOM_Engine::Load(const char* theFileName)
 {
+#if defined(WIN32) && defined(UNICODE)
+       std::wstring aFileName = Kernel_Utils::utf8_decode_s(theFileName);
+#else
+       std::string aFileName = theFileName;
+#endif
   Handle(TDocStd_Document) aDoc;
-  if (_OCAFApp->Open(theFileName, aDoc) != PCDM_RS_OK) {
+  if (_OCAFApp->Open(aFileName.c_str(), aDoc) != PCDM_RS_OK) {
     return false;
   }
 
@@ -584,10 +549,7 @@ bool GEOM_Engine::Load(int theDocID, const char* theFileName)
 
   aDoc->SetUndoLimit(_UndoLimit);
 
-  if(_mapIDDocument.IsBound(theDocID)) _mapIDDocument.UnBind(theDocID);
-  _mapIDDocument.Bind(theDocID, aDoc);
-
-  TDataStd_Integer::Set(aDoc->Main(), theDocID);
+  _document = aDoc;
 
   return true;
 }
@@ -597,30 +559,24 @@ bool GEOM_Engine::Load(int theDocID, const char* theFileName)
  *  Close
  */
 //=============================================================================
-void GEOM_Engine::Close(int theDocID)
+void GEOM_Engine::Close()
 {
-  if (_mapIDDocument.IsBound(theDocID)) {
-    Handle(TDocStd_Document) aDoc = Handle(TDocStd_Document)::DownCast(_mapIDDocument(theDocID));
-
-    //Remove all GEOM Objects associated to the given document
+  if (_document) {
+    //Remove all GEOM Objects associated to the document
     TColStd_SequenceOfAsciiString aSeq;
     GEOM_DataMapIteratorOfDataMapOfAsciiStringTransient It (_objects);
     for (; It.More(); It.Next()) {
-      TCollection_AsciiString anObjID (It.Key());
-      Standard_Integer anID = ExtractDocID(anObjID);
-      if (theDocID == anID) aSeq.Append(It.Key());
+      aSeq.Append(It.Key());
     }
-    for (Standard_Integer i=1; i<=aSeq.Length(); i++) _objects.UnBind(aSeq.Value(i));
-
-    // Forget free labels for this document
-    TFreeLabelsList::iterator anIt = _freeLabels.find(theDocID);
-    if (anIt != _freeLabels.end()) {
-      _freeLabels.erase(anIt);
+    for (Standard_Integer i=1; i<=aSeq.Length(); i++) {
+      _objects.UnBind(aSeq.Value(i));
     }
 
-    _mapIDDocument.UnBind(theDocID);
-    _OCAFApp->Close(aDoc);
-    aDoc.Nullify();
+    // Forget free labels for document
+    _freeLabels.clear();
+
+    _OCAFApp->Close(_document);
+    _document.Nullify();
   }
 }
 
@@ -629,8 +585,7 @@ void GEOM_Engine::Close(int theDocID)
  *  DumpPython
  */
 //=============================================================================
-TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
-                                                std::vector<TObjectData>& theObjectData,
+TCollection_AsciiString GEOM_Engine::DumpPython(std::vector<TObjectData>& theObjectData,
                                                 TVariablesList theVariables,
                                                 bool isPublished,
                                                 bool isMultiFile, 
@@ -640,26 +595,29 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
   Kernel_Utils::Localizer loc;
 
   TCollection_AsciiString aScript;
-  Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
+  Handle(TDocStd_Document) aDoc = GetDocument();
 
   if (aDoc.IsNull())
   {
     TCollection_AsciiString anEmptyScript;
     if( isMultiFile )
-      anEmptyScript = "def RebuildData(theStudy): pass\n";
+      anEmptyScript = "def RebuildData(): pass\n";
     return anEmptyScript;
   }
+  
+  if( isMultiFile )
+    aScript  = "import salome\n";
 
-  aScript  = "import GEOM\n";
+  aScript += "import GEOM\n";
   aScript += "from salome.geom import geomBuilder\n";
   aScript += "import math\n";
   aScript += "import SALOMEDS\n\n";
   if( isMultiFile )
-    aScript += "def RebuildData(theStudy):";
+    aScript += "def RebuildData():";
 
-  aScript += "\n\tgeompy = geomBuilder.New(theStudy)\n";
+  aScript += "\n\tgeompy = geomBuilder.New()\n";
 
-  AddTextures(theDocID, aScript);
+  AddTextures(aScript);
 
   Standard_Integer posToInsertGlobalVars = aScript.Length() + 1;
 
@@ -795,7 +753,7 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
   aScript += aFuncScript;
 
   // ouv : NPAL12872
-  AddObjectColors( theDocID, aScript, aEntry2ObjData );
+  AddObjectColors(aScript, aEntry2ObjData );
 
   // Make script to publish in study
   TSting2ObjDataPtrMap::iterator aStEntry2ObjDataPtrIt;
@@ -910,24 +868,21 @@ Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
 #define TEXTURE_LABEL_HEIGHT   4
 #define TEXTURE_LABEL_DATA     5
 
-int GEOM_Engine::addTexture(int theDocID, int theWidth, int theHeight,
+int GEOM_Engine::addTexture(int theWidth, int theHeight,
                             const Handle(TColStd_HArray1OfByte)& theTexture,
                             const TCollection_AsciiString& theFileName)
 {
-  Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
+  Handle(TDocStd_Document) aDoc = GetDocument();
   Handle(TDataStd_TreeNode) aRoot = TDataStd_TreeNode::Set(aDoc->Main());
 
   // NPAL18604: use existing label to decrease memory usage,
   //            if this label has been freed (object deleted)
   bool useExisting = false;
   TDF_Label aChild;
-  if (_freeLabels.find(theDocID) != _freeLabels.end()) {
-    std::list<TDF_Label>& aFreeLabels = _freeLabels[theDocID];
-    if (!aFreeLabels.empty()) {
-      useExisting = true;
-      aChild = aFreeLabels.front();
-      aFreeLabels.pop_front();
-    }
+  if (!_freeLabels.empty()) {
+    useExisting = true;
+    aChild = _freeLabels.front();
+    _freeLabels.pop_front();
   }
   if (!useExisting) {
     // create new label
@@ -956,14 +911,14 @@ int GEOM_Engine::addTexture(int theDocID, int theWidth, int theHeight,
   return aTextureID;
 }
 
-Handle(TColStd_HArray1OfByte) GEOM_Engine::getTexture(int theDocID, int theTextureID,
+Handle(TColStd_HArray1OfByte) GEOM_Engine::getTexture(int theTextureID,
                                                       int& theWidth, int& theHeight,
                                                       TCollection_AsciiString& theFileName)
 {
   Handle(TColStd_HArray1OfByte) anArray;
   theWidth = theHeight = 0;
 
-  Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
+  Handle(TDocStd_Document) aDoc = GetDocument();
 
   TDF_ChildIterator anIterator(aDoc->Main(), Standard_True);
   bool found = false;
@@ -997,11 +952,11 @@ Handle(TColStd_HArray1OfByte) GEOM_Engine::getTexture(int theDocID, int theTextu
   return anArray;
 }
 
-std::list<int> GEOM_Engine::getAllTextures(int theDocID)
+std::list<int> GEOM_Engine::getAllTextures()
 {
   std::list<int> id_list;
 
-  Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
+  Handle(TDocStd_Document) aDoc = GetDocument();
 
   TDF_ChildIterator anIterator(aDoc->Main(), Standard_True);
   for (; anIterator.More(); anIterator.Next()) {
@@ -1016,17 +971,6 @@ std::list<int> GEOM_Engine::getAllTextures(int theDocID)
   return id_list;
 }
 
-void GEOM_Engine::DocumentModified(const int theDocId, const bool isModified)
-{
-  if (isModified) _mapModifiedDocs.Add(theDocId);
-  else _mapModifiedDocs.Remove(theDocId);
-}
-bool GEOM_Engine::DocumentModified(const int theDocId)
-{
-  return _mapModifiedDocs.Contains(theDocId);
-}
-
 //===========================================================================
 //                     Internal functions
 //===========================================================================
@@ -1337,7 +1281,7 @@ Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theStrin
         if(c == 58) isFound = Standard_True;
       }
 
-      if(isFound && arr[j-2] != 58) { // last char should be a diggit
+      if(isFound && arr[j-2] != 58) { // last char should be a digit
         aSeq->Append(i+1); // +1 because AsciiString starts from 1
         aSeq->Append(j-1);
       }
@@ -1359,13 +1303,13 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
                       const TVariablesList&    theVariables)
 {
   if (MYDEBUG)
-    cout<<"Command : "<<theCommand<<endl;
+    std::cout<<"Command : "<<theCommand<<std::endl;
 
   if (MYDEBUG) {
-    cout<<"All Entries:"<<endl;
+    std::cout<<"All Entries:"<<std::endl;
     TVariablesList::const_iterator it = theVariables.begin();
     for(;it != theVariables.end();it++)
-      cout<<"\t'"<<(*it).first<<"'"<<endl;
+      std::cout<<"\t'"<<(*it).first<<"'"<<std::endl;
   }
 
   //Additional case - multi-row commands
@@ -1376,7 +1320,7 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
       break;
 
     if (MYDEBUG)
-      cout<<"Sub-command : "<<aCommand<<endl;
+      std::cout<<"Sub-command : "<<aCommand<<std::endl;
 
     Standard_Integer aStartCommandPos = theCommand.Location(aCommand,1,theCommand.Length());
     Standard_Integer aEndCommandPos = aStartCommandPos + aCommand.Length();
@@ -1394,7 +1338,7 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
     anEntry.RightAdjust();
     anEntry.LeftAdjust();
     if(MYDEBUG)
-      cout<<"Result entry : '" <<anEntry<<"'"<<endl;
+      std::cout<<"Result entry : '" <<anEntry<<"'"<<std::endl;
 
     if ( anEntry.IsEmpty() ) {
       aCommandIndex++;
@@ -1412,7 +1356,7 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
       anEntry.RightAdjust();
       anEntry.LeftAdjust();
       if(MYDEBUG)
-        cout<<"Sub-entry : '" <<anEntry<<"'"<<endl;
+               std::cout<<"Sub-entry : '" <<anEntry<<"'"<<std::endl;
     }
 
     //Find variables used for object construction
@@ -1423,7 +1367,7 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
 
     if(!aStates) {
       if(MYDEBUG)
-        cout<<"Valiables list empty!!!"<<endl;
+               std::cout<<"Valiables list empty!!!"<<std::endl;
       aCommandIndex++;
       continue;
     }
@@ -1431,9 +1375,9 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
     TState aVariables = aStates->GetCurrectState();
 
     if(MYDEBUG) {
-      cout<<"Variables from SObject:"<<endl;
-      for (int i = 0; i < aVariables.size();i++)
-        cout<<"\t Variable["<<i<<"] = "<<aVariables[i].myVariable<<endl;
+          std::cout<<"Variables from SObject:"<<std::endl;
+      for (size_t i = 0; i < aVariables.size();i++)
+        std::cout<<"\t Variable["<<i<<"] = "<<aVariables[i].myVariable<<std::endl;
     }
 
     //Calculate total number of parameters
@@ -1442,14 +1386,14 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
       aTotalNbParams++;
 
     if(MYDEBUG)
-      cout<<"aTotalNbParams = "<<aTotalNbParams<<endl;
+         std::cout<<"aTotalNbParams = "<<aTotalNbParams<<std::endl;
 
     Standard_Integer aFirstParam = aNbEntries;
 
     //Replace parameters by variables
     Standard_Integer aStartPos = 0;
     Standard_Integer aEndPos = 0;
-    int iVar = 0;
+    size_t iVar = 0;
     TCollection_AsciiString aVar, aReplacedVar;
     for(Standard_Integer i=aFirstParam;i <= aTotalNbParams;i++) {
       //Replace first parameter (bettwen '(' character and first ',' character)
@@ -1473,6 +1417,8 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
         aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
         aEndPos = aCommand.Location(i, COMMA, 1, aCommand.Length());
       }
+      if (aStartPos == 0 || aEndPos == 0)
+        continue;
 
       if( aCommand.Value( aStartPos ) == O_SQR_BRACKET )
         aStartPos++;
@@ -1482,14 +1428,14 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
         continue; // PAL20889: for "[]"
 
       if(MYDEBUG)
-        cout<<"aStartPos = "<<aStartPos<<", aEndPos = "<<aEndPos<<endl;
+        std::cout<<"aStartPos = "<<aStartPos<<", aEndPos = "<<aEndPos<<std::endl;
 
       aVar = aCommand.SubString(aStartPos, aEndPos-1);
       aVar.RightAdjust();
       aVar.LeftAdjust();
 
       if(MYDEBUG)
-        cout<<"Variable: '"<< aVar <<"'"<<endl;
+        std::cout<<"Variable: '"<< aVar <<"'"<<std::endl;
 
       // specific case for sketcher
       if(aVar.Location( TCollection_AsciiString("Sketcher:"), 1, aVar.Length() ) != 0) {
@@ -1509,7 +1455,7 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
 
           aSection = aVar.SubString(aStartSectionPos, aEndSectionPos-1);
           if(MYDEBUG)
-            cout<<"aSection: "<<aSection<<endl;
+                       std::cout<<"aSection: "<<aSection<<std::endl;
 
           Standard_Integer aNbParams = 1;
           while( aSection.Location( aNbParams, ' ', 1, aSection.Length() ) )
@@ -1526,14 +1472,14 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
               aEndParamPos = aSection.Length() + 1;
 
             if(MYDEBUG)
-              cout<<"aParamIndex: "<<aParamIndex<<" aStartParamPos: " <<aStartParamPos<<" aEndParamPos: "<<aEndParamPos<<endl;
+              std::cout<<"aParamIndex: "<<aParamIndex<<" aStartParamPos: " <<aStartParamPos<<" aEndParamPos: "<<aEndParamPos<<std::endl;
 
             if ( aStartParamPos == aEndParamPos)
               continue;
 
             aParameter = aSection.SubString(aStartParamPos, aEndParamPos-1);
             if(MYDEBUG)
-              cout<<"aParameter: "<<aParameter<<endl;
+                         std::cout<<"aParameter: "<<aParameter<<std::endl;
 
             if(iVar >= aVariables.size())
               continue;
@@ -1550,27 +1496,27 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
             }
 
             if(MYDEBUG)
-              cout<<"aSection before : "<<aSection<<endl;
+                         std::cout<<"aSection before : "<<aSection<< std::endl;
             aSection.Remove(aStartParamPos, aEndParamPos - aStartParamPos);
             aSection.Insert(aStartParamPos, aReplacedParameter);
             if(MYDEBUG)
-              cout<<"aSection after  : "<<aSection<<endl<<endl;
+              std::cout<<"aSection after  : "<<aSection<<std::endl<<std::endl;
             iVar++;
           }
           if(MYDEBUG)
-            cout<<"aVar before : "<<aVar<<endl;
+            std::cout<<"aVar before : "<<aVar<<std::endl;
           aVar.Remove(aStartSectionPos, aEndSectionPos - aStartSectionPos);
           aVar.Insert(aStartSectionPos, aSection);
           if(MYDEBUG)
-            cout<<"aVar after  : "<<aVar<<endl<<endl;
+            std::cout<<"aVar after  : "<<aVar<<std::endl<<std::endl;
         }
 
         if(MYDEBUG)
-          cout<<"aCommand before : "<<aCommand<<endl;
+          std::cout<<"aCommand before : "<<aCommand<<std::endl;
         aCommand.Remove(aStartPos, aEndPos - aStartPos);
         aCommand.Insert(aStartPos, aVar);
         if(MYDEBUG)
-          cout<<"aCommand after  : "<<aCommand<<endl;
+          std::cout<<"aCommand after  : "<<aCommand<<std::endl;
 
         break;
       } // end of specific case for sketcher
@@ -1607,7 +1553,7 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
   }
 
   if (MYDEBUG)
-    cout<<"Command : "<<theCommand<<endl;
+    std::cout<<"Command : "<<theCommand<<std::endl;
 }
 
 //=============================================================================
@@ -1617,7 +1563,7 @@ void ReplaceVariables(TCollection_AsciiString& theCommand,
 //=============================================================================
 void ReplaceEntriesByNames (TCollection_AsciiString&                  theScript,
                             TSting2ObjDataMap&                        aEntry2ObjData,
-                            const bool                                theIsPublished,
+                            const bool                                /*theIsPublished*/,
                             TColStd_SequenceOfAsciiString&            theObjListToPublish,
                             Standard_Integer&                         objectCounter,
                             Resource_DataMapOfAsciiStringAsciiString& aNameToEntry)
@@ -1637,7 +1583,7 @@ void ReplaceEntriesByNames (TCollection_AsciiString&                  theScript,
     theObjListToPublish.Append( anEntry );
     
     TObjectData& data = aEntry2ObjData[ anEntry ];
-    if ( data._pyName.IsEmpty() ) { // encounted for the 1st time
+    if ( data._pyName.IsEmpty() ) { // encountered for the 1st time
       if ( !data._name.IsEmpty() ) { // published object
         data._pyName = data._name;
         engine->healPyName( data._pyName, anEntry, aNameToEntry);
@@ -1667,12 +1613,11 @@ void ReplaceEntriesByNames (TCollection_AsciiString&                  theScript,
  *  AddObjectColors: Add color to objects
  */
 //=============================================================================
-void AddObjectColors (int                      theDocID,
-                      TCollection_AsciiString& theScript,
+void AddObjectColors (TCollection_AsciiString& theScript,
                       const TSting2ObjDataMap& theEntry2ObjData)
 {
   GEOM_Engine* engine = GEOM_Engine::GetEngine();
-  Handle(TDocStd_Document) aDoc = engine->GetDocument(theDocID);
+  Handle(TDocStd_Document) aDoc = engine->GetDocument();
 
   TSting2ObjDataMap::const_iterator anEntryToNameIt;
   for (anEntryToNameIt = theEntry2ObjData.begin();
@@ -1776,10 +1721,10 @@ static TCollection_AsciiString pack_data (const Handle(TColStd_HArray1OfByte)& a
   return stream;
 }
 
-void AddTextures (int theDocID, TCollection_AsciiString& theScript)
+void AddTextures (TCollection_AsciiString& theScript)
 {
   GEOM_Engine* engine = GEOM_Engine::GetEngine();
-  std::list<int> allTextures = engine->getAllTextures(theDocID);
+  std::list<int> allTextures = engine->getAllTextures();
   std::list<int>::const_iterator it;
 
   if (allTextures.size() > 0) {
@@ -1790,7 +1735,7 @@ void AddTextures (int theDocID, TCollection_AsciiString& theScript)
       Standard_Integer aWidth, aHeight;
       TCollection_AsciiString aFileName;
       Handle(TColStd_HArray1OfByte) aTexture =
-        engine->getTexture(theDocID, *it, aWidth, aHeight, aFileName);
+        engine->getTexture(*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 += "] = ";
@@ -1891,6 +1836,8 @@ TCollection_AsciiString GetPublishCommands
 
   if (!thePublished.count(theTag)) {
     // This object is not published yet.
+    thePublished.insert(theTag);
+
     std::map< int, TCollection_AsciiString >::const_iterator anIt =
       theEntryToCmdMap.find(theTag);
 
@@ -1899,7 +1846,7 @@ TCollection_AsciiString GetPublishCommands
       TIntToListIntMap::const_iterator aRefIt = theMapRefs.find(theTag);
 
       if (aRefIt != theMapRefs.end()) {
-        // Recursively publish all references.
+        // Recursively publish all references.         
         std::list< int >::const_iterator aRefTagIt = aRefIt->second.begin();
 
         for(; aRefTagIt != aRefIt->second.end(); ++aRefTagIt) {
@@ -1913,8 +1860,6 @@ TCollection_AsciiString GetPublishCommands
       // Add the object command.
       aResult += anIt->second;
     }
-
-    thePublished.insert(theTag);
   }
 
   return aResult;
@@ -1984,7 +1929,7 @@ ObjectStates::~ObjectStates()
 //================================================================================
 TState ObjectStates::GetCurrectState() const
 {
-  if(_states.size() > _dumpstate)
+  if((int)_states.size() > _dumpstate)
     return _states[_dumpstate];
   return TState();
 }