Salome HOME
Dump Python
[modules/geom.git] / src / GEOM / GEOM_Engine.cxx
index 46726dceae50e4fe1aa259f96269528c3c775f30..e945d9853170833aec45d8b68ccb97754fe872ec 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <TDF_Tool.hxx>
 #include <TDF_Data.hxx>
+#include <TDF_LabelSequence.hxx>
 #include <TDataStd_Integer.hxx>
 #include <TDataStd_ChildNodeIterator.hxx>
 #include <TFunction_Driver.hxx>
 #include <TCollection_AsciiString.hxx>
 #include <TCollection_ExtendedString.hxx>
 #include <TColStd_SequenceOfAsciiString.hxx>
+#include <TColStd_SequenceOfTransient.hxx>
+#include <TColStd_HSequenceOfTransient.hxx>
+#include <TColStd_ListOfTransient.hxx>
+#include <TColStd_MapOfTransient.hxx>
+#include <TColStd_HSequenceOfInteger.hxx>
 
 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
 
@@ -50,6 +56,12 @@ static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
   return aDocID.IntegerValue();
 }
 
+void ProcessFunction(Handle(GEOM_Function)& theFunction, 
+                    TCollection_AsciiString& theScript,
+                    TColStd_MapOfTransient& theProcessed);
+
+Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
+
 //=============================================================================
 /*!
  *  GetEngine
@@ -157,7 +169,9 @@ Handle(GEOM_Object) GEOM_Engine::AddObject(int theDocID, int theType)
  *  AddSubShape
  */
 //=============================================================================
-Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape, Handle(TColStd_HArray1OfInteger) theIndices)
+Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape, 
+                                            Handle(TColStd_HArray1OfInteger) theIndices,
+                                            bool isStandaloneOperation)
 {
   if(theMainShape.IsNull() || theIndices.IsNull()) return NULL;
 
@@ -192,18 +206,25 @@ Handle(GEOM_Object) GEOM_Engine::AddSubShape(Handle(GEOM_Object) theMainShape, H
   if(_objects.IsBound(anID)) _objects.UnBind(anID);
   _objects.Bind(anID, anObject);
 
-  TCollection_AsciiString anEntry, aDescr("");
-  TDF_Tool::Entry(anObject->GetEntry(), anEntry);
-  aDescr += anEntry;
-  aDescr += " = geom.AddSubShape(";
-  TDF_Tool::Entry(theMainShape->GetEntry(), anEntry);
-  aDescr += (anEntry+", ");
-  aDescr += (", [");
-  for(Standard_Integer i=theIndices->Lower(); i<=theIndices->Upper(); i++) {
-    aDescr += (TCollection_AsciiString(theIndices->Value(i))+", ");
+  TCollection_AsciiString aDescr("");
+  if(isStandaloneOperation) {
+    TCollection_AsciiString anEntry;
+    TDF_Tool::Entry(anObject->GetEntry(), anEntry);
+    aDescr += anEntry;
+    aDescr += " = geom.AddSubShape(";
+    TDF_Tool::Entry(theMainShape->GetEntry(), anEntry);
+    aDescr += (anEntry+", ");
+    aDescr += (", [");
+    for(Standard_Integer i=theIndices->Lower(); i<=theIndices->Upper(); i++) {
+      aDescr += (TCollection_AsciiString(theIndices->Value(i))+", ");
+    }
+    aDescr.Trunc(aDescr.Length()-1);
+    aDescr += "])";  
   }
-  aDescr.Trunc(aDescr.Length()-1);
-  aDescr += "])";
+  else 
+    TCollection_AsciiString aDescr("None");
   aFunction->SetDescription(aDescr);
 
   return anObject;
@@ -222,6 +243,14 @@ bool GEOM_Engine::RemoveObject(Handle(GEOM_Object) theObject)
   TCollection_AsciiString anID = BuildIDFromObject(theObject);
   if(_objects.IsBound(anID)) _objects.UnBind(anID);
 
+  int nb = theObject->GetNbFunctions();
+  Handle(TDataStd_TreeNode) aNode;
+  for(int i = 1; i<=nb; i++) {
+    Handle(GEOM_Function) aFunction = theObject->GetFunction(i);
+    if(aFunction->GetEntry().FindAttribute(GEOM_Function::GetFunctionTreeID(), aNode)) 
+      aNode->Remove();
+  }
+
   TDF_Label aLabel = theObject->GetEntry();
   aLabel.ForgetAllAttributes(Standard_True);
 
@@ -273,7 +302,9 @@ 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) return false;
+  if(_OCAFApp->Open(theFileName, aDoc) != CDF_RS_OK) {
+    return false;
+  }
 
   aDoc->SetUndoLimit(_UndoLimit);
 
@@ -282,6 +313,8 @@ bool GEOM_Engine::Load(int theDocID, char* theFileName)
 
   TDataStd_Integer::Set(aDoc->Main(), theDocID);
 
+  _OCAFApp->SaveAs(aDoc, "/dn05/salome/srn/Test.sdg");
+
   return true;
 }
 
@@ -310,3 +343,145 @@ void GEOM_Engine::Close(int theDocID)
     aDoc.Nullify();
   }
 }
+
+//=============================================================================
+/*!
+ *  DumpPython
+ */
+//=============================================================================
+TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID, 
+                                               Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
+                                               bool isPublished, 
+                                               bool& aValidScript)
+{
+  TCollection_AsciiString aScript;
+  Handle(TDocStd_Document) aDoc = GetDocument(theDocID);
+  
+  if(aDoc.IsNull()) return TCollection_AsciiString("def RebuildData(theStudy): pass\n");
+  aScript = "import geompy\n\n";
+  aScript += "def RebuildData(theStudy):";
+  
+  Handle(TDataStd_TreeNode) aNode, aRoot;
+  Handle(GEOM_Function) aFunction;
+  TColStd_MapOfTransient aMap;
+
+  if(aDoc->Main().FindAttribute(GEOM_Function::GetFunctionTreeID(), aRoot)) {
+    TDataStd_ChildNodeIterator Itr(aRoot);
+    for(; Itr.More(); Itr.Next()) {
+      aNode = Itr.Value();
+      aFunction = GEOM_Function::GetFunction(aNode->Label());
+      if(aFunction.IsNull()) {
+       cout << "Null function !!!!" << endl;
+       continue;
+      }
+      ProcessFunction(aFunction, aScript, aMap);
+    }
+  }
+
+  aScript += "\n\tpass\n";
+  aValidScript = true;
+
+  
+  Handle(TColStd_HSequenceOfInteger) aSeq = FindEntries(aScript);
+  Standard_Integer aLen = aSeq->Length(), objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
+  Resource_DataMapOfAsciiStringAsciiString aNames;
+
+  //Replace entries by the names
+  TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("geomObj_");
+  if(aLen == 0) anUpdatedScript = aScript;
+
+  for(Standard_Integer i = 1; i <= aLen; i+=2) {
+    anUpdatedScript += aScript.SubString(aStart, aSeq->Value(i)-1);
+    anEntry = aScript.SubString(aSeq->Value(i), aSeq->Value(i+1));
+    if(theObjectNames.IsBound(anEntry)) {
+      aName = theObjectNames.Find(anEntry); 
+    }
+    else {
+      aName = aBaseName + TCollection_AsciiString(++objectCounter);
+      while(theObjectNames.IsBound(aName)) aName = aBaseName + TCollection_AsciiString(++objectCounter);
+    }
+
+    anUpdatedScript += aName;
+    aNames.Bind(aName, "1");
+    aStart = aSeq->Value(i+1) + 1;
+  }
+  //Add final part of the script
+  if(aSeq->Value(aLen) < aScriptLength)  anUpdatedScript += aScript.SubString(aSeq->Value(aLen)+1, aScriptLength);
+  return anUpdatedScript;
+}
+
+
+//===========================================================================
+//                     Internal functions
+//===========================================================================
+void ProcessFunction(Handle(GEOM_Function)& theFunction, 
+                    TCollection_AsciiString& theScript,
+                    TColStd_MapOfTransient& theProcessed)
+{
+  if(theFunction.IsNull() || theProcessed.Contains(theFunction)) return;
+
+/*
+  TDF_LabelSequence aSeq;
+  theFunction->GetDependency(aSeq);
+  Standard_Integer aLen = aSeq.Length();
+  for(Standard_Integer i = 1; i<= aLen; i++) {
+    Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(aSeq.Value(i));
+    if(aFunction.IsNull()) continue;
+    ProcessFunction(aFunction, theScript, theProcessed);
+  }
+*/
+
+  TCollection_AsciiString aDescr = theFunction->GetDescription();
+  if(aDescr.Length() == 0) {
+    //cout << "Warning: the function has no description" << endl;
+    return;
+  }
+  //Check if its internal function which doesn't requires dumping
+  if(aDescr == "None") return;
+
+  theScript += "\n\t";
+  theScript += aDescr;
+  theProcessed.Add(theFunction);
+  return;
+}
+
+//=============================================================================
+/*!
+ *  FindEntries: Returns a sequence of start/end positions of entries in the string
+ */
+//=============================================================================
+Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString)
+{
+  Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
+  Standard_Integer aLen = theString.Length();
+  Standard_Boolean isFound = Standard_False;
+
+  char* arr = theString.ToCString();
+  Standard_Integer i = 0, j;
+
+  while(i < aLen) {
+    int c = (int)arr[i];
+    j = i+1;
+    if(c >= 48 && c <= 57) { //Is digit?
+      isFound = Standard_False;
+      while((j < aLen) && ((c >= 48 && c <= 57) || c == 58) ) { //Check if it is an entry
+       c = (int)arr[j++];  
+       if(c == 58) isFound = Standard_True;
+      }
+      
+      if(isFound) {
+       aSeq->Append(i+1); // +1 because AsciiString starts from 1
+       aSeq->Append(j-1);
+      }
+    }
+     
+    i = j;
+  }
+
+  return aSeq;
+}