Salome HOME
Dump Python extension
[modules/geom.git] / src / GEOM / GEOM_Engine.cxx
index ba4976e0049a7111f18ce4f1675bb61f341e1bc6..14e81340877fa3440cebdd4940a7b76430b41706 100644 (file)
@@ -55,6 +55,7 @@
 
 #include <map>
 #include <string>
+#include <vector>
 
 #include <Standard_Failure.hxx>
 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
@@ -62,6 +63,9 @@
 #define COMMA ','
 #define O_BRACKET '('
 #define C_BRACKET ')'
+#define O_SQR_BRACKET '['
+#define C_SQR_BRACKET ']'
+#define PY_NULL "None"
 
 #ifdef _DEBUG_
 static int MYDEBUG = 0;
@@ -71,6 +75,8 @@ static int MYDEBUG = 0;
 
 static GEOM_Engine* TheEngine = NULL;
 
+using namespace std;
+
 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
 {
   TCollection_AsciiString anID(theObject->GetDocID()), anEntry;
@@ -95,11 +101,11 @@ static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
 
 void ProcessFunction(Handle(GEOM_Function)& theFunction, 
                     TCollection_AsciiString& theScript,
-                     Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
+                     TVariablesList theVariables,
                     TColStd_MapOfTransient& theProcessed);
 
 void ReplaceVariables(TCollection_AsciiString& theCommand, 
-                      Resource_DataMapOfAsciiStringAsciiString& theVariableNames);
+                      TVariablesList theVariables);
 
 
 
@@ -439,7 +445,7 @@ void GEOM_Engine::Close(int theDocID)
 //=============================================================================
 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
                                                Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
-                                               Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
+                                                TVariablesList theVariables,
                                                bool isPublished,
                                                bool& aValidScript)
 {
@@ -469,7 +475,7 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
        MESSAGE ( "Null function !!!!" );
        continue;
       }
-      ProcessFunction(aFunction, aScript,theVariableNames,aMap);
+      ProcessFunction(aFunction, aScript, theVariables,aMap);
     }
   }
 
@@ -694,7 +700,7 @@ Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
 //===========================================================================
 void ProcessFunction(Handle(GEOM_Function)& theFunction, 
                     TCollection_AsciiString& theScript,
-                     Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
+                     TVariablesList theVariables,
                     TColStd_MapOfTransient& theProcessed)
 {
   if(theFunction.IsNull() || theProcessed.Contains(theFunction)) return;
@@ -719,7 +725,7 @@ void ProcessFunction(Handle(GEOM_Function)& theFunction,
   if(aDescr == "None") return;
 
   //Replace parameter by notebook variables
-  ReplaceVariables(aDescr,theVariableNames);
+  ReplaceVariables(aDescr,theVariables);
   theScript += "\n\t";
   theScript += aDescr;
 
@@ -772,132 +778,233 @@ Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theStrin
  */
 //=============================================================================
 void ReplaceVariables(TCollection_AsciiString& theCommand, 
-                      Resource_DataMapOfAsciiStringAsciiString& theVariableNames)
+                      TVariablesList theVariables)
 {
-  //Get Entry of the result object
-  TCollection_AsciiString anEntry = theCommand.Token("=",1);
-  
-  //Remove white spaces
-  anEntry.RightAdjust();
-  anEntry.LeftAdjust();
-  if(MYDEBUG)
-    cout<<"Result entry : '" <<anEntry<<"'"<<endl;
-    
-  //Find variables used for object construction
-  TCollection_AsciiString aVariables;
-  if(theVariableNames.IsBound(anEntry)) 
-    aVariables = theVariableNames.Find(anEntry);
-
-  if(aVariables.IsEmpty()) {
-    if(MYDEBUG)
-      cout<<"Valiables list empty!!!"<<endl;
-    return;
+  if (MYDEBUG)
+    cout<<"Command : "<<theCommand<<endl;
+
+  if (MYDEBUG) {
+    cout<<"All Entries:"<<endl;
+    TVariablesList::const_iterator it = theVariables.begin();
+    for(;it != theVariables.end();it++)
+      cout<<"\t'"<<(*it).first<<"'"<<endl;
   }
 
-  if(MYDEBUG)
-    cout<<"Variables : '" <<aVariables<<"'"<<endl;
-
-  //Calculate number of variables, that mast be replaced
-  Standard_Integer aNbReplacedParams = 1,aPos;
-  TColStd_HSequenceOfInteger aPositions;
-  while(aPos = aVariables.Location(aNbReplacedParams,':',1,aVariables.Length())) {
-    aPositions.Append(aPos);
-    aNbReplacedParams++;
-  }
+  //Additional case - multi-row commands
+  int aCommandIndex = 1;
+  while( aCommandIndex < 10 ) { // tmp check
+    TCollection_AsciiString aCommand = theCommand.Token("\n",aCommandIndex);
+    if( aCommand.Length() == 0 )
+      break;
 
-  if(MYDEBUG)
-    cout<<"Number of replaced variables : " <<aNbReplacedParams<<endl;
+    if (MYDEBUG)
+      cout<<"Sub-command : "<<aCommand<<endl;
 
-  if(MYDEBUG)
-    {
-      for(Standard_Integer i = 1;i<=aPositions.Length();i++)
-        cout<<"Positions ["<<i<<"] = "<<aPositions.Value(i)<<endl;
-    }
+    Standard_Integer aStartCommandPos = theCommand.Location(aCommand,1,theCommand.Length());
+    Standard_Integer aEndCommandPos = aStartCommandPos + aCommand.Length();
 
-  //Calculate total number of parameter
-  Standard_Integer aTotalNbParams = 1;
-  while(theCommand.Location(aTotalNbParams,COMMA,1,theCommand.Length()))
-    aTotalNbParams++;
+    //Get Entry of the result object
+    TCollection_AsciiString anEntry = aCommand.Token("=",1);
 
-
-  if(MYDEBUG)
-    cout<<"Total Number of parameters : " <<aTotalNbParams<<endl;
-
-  //Get Variables names 
-  TColStd_SequenceOfAsciiString aVarSeq;
-  TCollection_AsciiString aVar;
-  Standard_Integer i = 1;
-  while(i <= aNbReplacedParams){
+    //Remove white spaces
+    anEntry.RightAdjust();
+    anEntry.LeftAdjust();
+    if(MYDEBUG)
+      cout<<"Result entry : '" <<anEntry<<"'"<<endl;
+
+    //Check if result is list of entries - enough to get the first entry in this case
+    int aNbEntries = 1;
+    if( anEntry.Value( 1 ) == O_SQR_BRACKET && anEntry.Value( anEntry.Length() ) == C_SQR_BRACKET ) {
+      while(anEntry.Location(aNbEntries,COMMA,1,anEntry.Length()))
+       aNbEntries++;
+      TCollection_AsciiString aSeparator(COMMA);
+      anEntry = anEntry.Token(aSeparator.ToCString(),1);
+      anEntry.Remove( 1, 1 );
+      anEntry.RightAdjust();
+      anEntry.LeftAdjust();
+      if(MYDEBUG)
+       cout<<"Sub-entry : '" <<anEntry<<"'"<<endl;
+    }
     
-    if(i == 1)
-      aVar = (aPositions.Value(i) == 1) ? TCollection_AsciiString() : aVariables.SubString(1, aPositions.Value(i)-1);
-    else if(i == aNbReplacedParams) {
-      Standard_Integer aLen = aVariables.Length();
-      Standard_Integer aPos = aPositions.Value(i-1);
-      aVar = (aPos == aLen) ? TCollection_AsciiString() : aVariables.SubString(aPos+1, aLen);
+    //Find variables used for object construction
+    vector<TVariable> aVariables;
+    TVariablesList::const_iterator it = theVariables.find(anEntry);
+    if( it != theVariables.end() ) 
+      aVariables = (*it).second;
+
+    if(aVariables.empty()) {
+      if(MYDEBUG)
+       cout<<"Valiables list empty!!!"<<endl;
+      aCommandIndex++;
+      continue;
     }
-    else {
-      Standard_Integer aPrevPos = aPositions.Value(i-1);
-      Standard_Integer aCurrentPos = aPositions.Value(i);
-      aVar = (aCurrentPos - aPrevPos == 1) ? TCollection_AsciiString() : aVariables.SubString(aPrevPos+1, aCurrentPos-1);
+  
+    if(MYDEBUG) {
+      cout<<"Variables from SObject:"<<endl;
+      for (int i = 0; i < aVariables.size();i++)
+       cout<<"\t Variable["<<i<<"] = "<<aVariables[i].myVariable<<endl;
     }
-    if(MYDEBUG)
-      cout<<"Variable ["<<i<<"] = '"<<aVar<<"'"<<endl;
-    
-    //Add current varibale
-    aVarSeq.Append(aVar);
-    i++;
-  }
 
-  //Replace parameters by variables
-  Standard_Integer aStartPos = 0;
-  Standard_Integer aEndPos = 0;
-  Standard_Integer iVar = 1;
-  for(Standard_Integer i=1;i <= aTotalNbParams;i++) {
-        
-    //Replace first parameter (bettwen '(' character and first ',' character)
-    if(i == 1) 
+    //Calculate total number of parameters
+    Standard_Integer aTotalNbParams = 1;
+    while(aCommand.Location(aTotalNbParams,COMMA,1,aCommand.Length()))
+      aTotalNbParams++;
+
+    if(MYDEBUG)
+      cout<<"aTotalNbParams = "<<aTotalNbParams<<endl;
+
+    Standard_Integer aFirstParam = aNbEntries;
+
+    //Replace parameters by variables
+    Standard_Integer aStartPos = 0;
+    Standard_Integer aEndPos = 0;
+    int iVar = 0;
+    TCollection_AsciiString aVar, aReplacedVar;
+    for(Standard_Integer i=aFirstParam;i <= aTotalNbParams;i++) {
+      //Replace first parameter (bettwen '(' character and first ',' character)
+      if(i == aFirstParam)
       {
-        aStartPos = theCommand.Location(O_BRACKET, 1, theCommand.Length()) + 1;
-        aEndPos = theCommand.Location(COMMA, 1, theCommand.Length());
+       aStartPos = aCommand.Location(O_BRACKET, 1, aCommand.Length()) + 1;
+       if(aTotalNbParams - aNbEntries > 0 )
+         aEndPos = aCommand.Location(aFirstParam, COMMA, 1, aCommand.Length());
+       else
+         aEndPos = aCommand.Location(C_BRACKET, 1, aCommand.Length()); 
       }
-    //Replace last parameter (bettwen ',' character and ')' character)
-    else if(i == aVarSeq.Length())
+      //Replace last parameter (bettwen ',' character and ')' character)
+      else if(i == aTotalNbParams)
       {
-        aStartPos = theCommand.Location(i-1, COMMA, 1, theCommand.Length()) + 2;
-        aEndPos = theCommand.Location(C_BRACKET, 1, theCommand.Length());
+       aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
+       aEndPos = aCommand.Location(C_BRACKET, 1, aCommand.Length());
       }
-    //Replace other parameters (bettwen two ',' characters)
-    else if(i != 1 && i != aVarSeq.Length())
+      //Replace other parameters (bettwen two ',' characters)
+      else if(i != aFirstParam && i != aTotalNbParams )
       {
-        aStartPos = theCommand.Location(i-1, COMMA, 1, theCommand.Length()) + 2;
-        aEndPos = theCommand.Location(i, COMMA, 1, theCommand.Length());
+       aStartPos = aCommand.Location(i-1, COMMA, 1, aCommand.Length()) + 2;
+       aEndPos = aCommand.Location(i, COMMA, 1, aCommand.Length());
       }
 
-    TCollection_AsciiString aParameter = theCommand.SubString(aStartPos, aStartPos);
+      if( aCommand.Value( aStartPos ) == O_SQR_BRACKET )
+       aStartPos++;
+      if( aCommand.Value( aEndPos-1 ) == C_SQR_BRACKET )
+       aEndPos--;
 
-    if(MYDEBUG)
-      cout<<"Current parameter "<<aParameter<<endl;
+      if(MYDEBUG) 
+       cout<<"aStartPos = "<<aStartPos<<", aEndPos = "<<aEndPos<<endl;
 
-    aVar = aVarSeq.Value(iVar);
+      aVar = aCommand.SubString(aStartPos, aEndPos-1);
+      aVar.RightAdjust();
+      aVar.LeftAdjust();
     
-    //If parameter is entry, skip it
-    aVar.RightAdjust();
-    aVar.LeftAdjust();
-    if(theVariableNames.IsBound(aVar))
-      continue;
+      if(MYDEBUG) 
+       cout<<"Variable: '"<< aVar <<"'"<<endl;
+
+      // specific case for sketcher
+      if(aVar.Location( TCollection_AsciiString("Sketcher:"), 1, aVar.Length() ) != 0) {
+       Standard_Integer aNbSections = 1;
+       while( aVar.Location( aNbSections, ':', 1, aVar.Length() ) )
+         aNbSections++;
+       aNbSections--;
+
+       int aStartSectionPos = 0, aEndSectionPos = 0;
+       TCollection_AsciiString aSection, aReplacedSection;
+       for(Standard_Integer aSectionIndex = 1; aSectionIndex <= aNbSections; aSectionIndex++) {
+         aStartSectionPos = aVar.Location( aSectionIndex, ':', 1, aVar.Length() ) + 1;
+         if( aSectionIndex != aNbSections )
+           aEndSectionPos = aVar.Location( aSectionIndex + 1, ':', 1, aVar.Length() );
+         else
+           aEndSectionPos = aVar.Length();
+
+         aSection = aVar.SubString(aStartSectionPos, aEndSectionPos-1);
+         if(MYDEBUG) 
+           cout<<"aSection: "<<aSection<<endl;
+
+         Standard_Integer aNbParams = 1;
+         while( aSection.Location( aNbParams, ' ', 1, aSection.Length() ) )
+           aNbParams++;
+         aNbParams--;
+
+         int aStartParamPos = 0, aEndParamPos = 0;
+         TCollection_AsciiString aParameter, aReplacedParameter;
+         for(Standard_Integer aParamIndex = 1; aParamIndex <= aNbParams; aParamIndex++) {
+           aStartParamPos = aSection.Location( aParamIndex, ' ', 1, aSection.Length() ) + 1;
+           if( aParamIndex != aNbParams )
+             aEndParamPos = aSection.Location( aParamIndex + 1, ' ', 1, aSection.Length() );
+           else
+             aEndParamPos = aSection.Length() + 1;
+
+           aParameter = aSection.SubString(aStartParamPos, aEndParamPos-1);
+           if(MYDEBUG) 
+             cout<<"aParameter: "<<aParameter<<endl;
+
+           if(iVar >= aVariables.size())
+             continue;
+
+           aReplacedParameter = aVariables[iVar].myVariable;
+           if(aReplacedParameter.IsEmpty()) {
+             iVar++;
+             continue;
+           }
+
+           if(aVariables[iVar].isVariable) {
+             aReplacedParameter.InsertBefore(1,"'");
+             aReplacedParameter.InsertAfter(aReplacedParameter.Length(),"'");
+           }
+
+           if(MYDEBUG) 
+             cout<<"aSection before : "<<aSection<<endl;
+           aSection.Remove(aStartParamPos, aEndParamPos - aStartParamPos);
+           aSection.Insert(aStartParamPos, aReplacedParameter);
+           if(MYDEBUG) 
+             cout<<"aSection after  : "<<aSection<<endl<<endl;
+           iVar++;
+         }
+         if(MYDEBUG) 
+           cout<<"aVar before : "<<aVar<<endl;
+         aVar.Remove(aStartSectionPos, aEndSectionPos - aStartSectionPos);
+         aVar.Insert(aStartSectionPos, aSection);
+         if(MYDEBUG) 
+           cout<<"aVar after  : "<<aVar<<endl<<endl;
+       }
+
+       if(MYDEBUG) 
+         cout<<"aCommand before : "<<aCommand<<endl;
+       aCommand.Remove(aStartPos, aEndPos - aStartPos);
+       aCommand.Insert(aStartPos, aVar);
+       if(MYDEBUG) 
+         cout<<"aCommand after  : "<<aCommand<<endl;
+
+       break;
+      } // end of specific case for sketcher
+
+      //If parameter is entry or 'None', skip it
+      if(theVariables.find(aVar) != theVariables.end() || aVar == PY_NULL)
+       continue;
 
-      
-    if(aVar.IsEmpty()) {
+      if(iVar >= aVariables.size())
+       continue;
+
+      aReplacedVar = aVariables[iVar].myVariable;
+      if(aReplacedVar.IsEmpty()) {
+       iVar++;
+       continue;
+      }
+
+      if(aVariables[iVar].isVariable) {
+       aReplacedVar.InsertBefore(1,"\"");
+       aReplacedVar.InsertAfter(aReplacedVar.Length(),"\"");
+      }
+
+      aCommand.Remove(aStartPos, aEndPos - aStartPos);
+      aCommand.Insert(aStartPos, aReplacedVar);
       iVar++;
-      continue;
     }
-    
-    aVar.InsertBefore(1,"\"");
-    aVar.InsertAfter(aVar.Length(),"\"");
 
-    theCommand.Remove(aStartPos, aEndPos - aStartPos);
-    theCommand.Insert(aStartPos,aVar);    
-    iVar++;
+    theCommand.Remove(aStartCommandPos, aEndCommandPos - aStartCommandPos);
+    theCommand.Insert(aStartCommandPos, aCommand);
+
+    aCommandIndex++;
   }
+
+  if (MYDEBUG)
+    cout<<"Command : "<<theCommand<<endl;
 }