]> SALOME platform Git repositories - modules/geom.git/commitdiff
Salome HOME
Implementation of the "python" persistance in GEOM.
authorrnv <rnv@opencascade.com>
Fri, 7 Nov 2008 15:51:55 +0000 (15:51 +0000)
committerrnv <rnv@opencascade.com>
Fri, 7 Nov 2008 15:51:55 +0000 (15:51 +0000)
src/GEOM/GEOM_Engine.cxx
src/GEOM/GEOM_Engine.hxx
src/GEOM_I/GEOM_DumpPython.cc
src/GEOM_SWIG/geompyDC.py

index 5693abcebe0a1d7ba7d755acff8f016605beb9bc..ba4976e0049a7111f18ce4f1675bb61f341e1bc6 100644 (file)
@@ -49,6 +49,7 @@
 #include <TColStd_MapOfTransient.hxx>
 #include <TColStd_HSequenceOfInteger.hxx>
 
+
 #include <Interface_DataMapIteratorOfDataMapOfIntegerTransient.hxx>
 #include <Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString.hxx>
 
 #include <Standard_Failure.hxx>
 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
 
+#define COMMA ','
+#define O_BRACKET '('
+#define C_BRACKET ')'
+
+#ifdef _DEBUG_
+static int MYDEBUG = 0;
+#else
+static int MYDEBUG = 0;
+#endif
+
 static GEOM_Engine* TheEngine = NULL;
 
 static TCollection_AsciiString BuildIDFromObject(Handle(GEOM_Object)& theObject)
@@ -84,10 +95,17 @@ static Standard_Integer ExtractDocID(TCollection_AsciiString& theID)
 
 void ProcessFunction(Handle(GEOM_Function)& theFunction, 
                     TCollection_AsciiString& theScript,
+                     Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
                     TColStd_MapOfTransient& theProcessed);
 
+void ReplaceVariables(TCollection_AsciiString& theCommand, 
+                      Resource_DataMapOfAsciiStringAsciiString& theVariableNames);
+
+
+
 Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theString);
 
+
 //=============================================================================
 /*!
  *  GetEngine
@@ -421,6 +439,7 @@ void GEOM_Engine::Close(int theDocID)
 //=============================================================================
 TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
                                                Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
+                                               Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
                                                bool isPublished,
                                                bool& aValidScript)
 {
@@ -450,7 +469,7 @@ TCollection_AsciiString GEOM_Engine::DumpPython(int theDocID,
        MESSAGE ( "Null function !!!!" );
        continue;
       }
-      ProcessFunction(aFunction, aScript, aMap);
+      ProcessFunction(aFunction, aScript,theVariableNames,aMap);
     }
   }
 
@@ -675,6 +694,7 @@ Handle(TColStd_HSequenceOfAsciiString) GEOM_Engine::GetAllDumpNames() const
 //===========================================================================
 void ProcessFunction(Handle(GEOM_Function)& theFunction, 
                     TCollection_AsciiString& theScript,
+                     Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
                     TColStd_MapOfTransient& theProcessed)
 {
   if(theFunction.IsNull() || theProcessed.Contains(theFunction)) return;
@@ -698,8 +718,11 @@ void ProcessFunction(Handle(GEOM_Function)& theFunction,
   //Check if its internal function which doesn't requires dumping
   if(aDescr == "None") return;
 
+  //Replace parameter by notebook variables
+  ReplaceVariables(aDescr,theVariableNames);
   theScript += "\n\t";
   theScript += aDescr;
+
  
   theProcessed.Add(theFunction);
   return;
@@ -741,3 +764,140 @@ Handle(TColStd_HSequenceOfInteger) FindEntries(TCollection_AsciiString& theStrin
 
   return aSeq;
 }
+
+//=============================================================================
+/*!
+ *  ReplaceVariables: Replace parameters of the function by variales from 
+ *                    Notebook if need
+ */
+//=============================================================================
+void ReplaceVariables(TCollection_AsciiString& theCommand, 
+                      Resource_DataMapOfAsciiStringAsciiString& theVariableNames)
+{
+  //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<<"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++;
+  }
+
+  if(MYDEBUG)
+    cout<<"Number of replaced variables : " <<aNbReplacedParams<<endl;
+
+  if(MYDEBUG)
+    {
+      for(Standard_Integer i = 1;i<=aPositions.Length();i++)
+        cout<<"Positions ["<<i<<"] = "<<aPositions.Value(i)<<endl;
+    }
+
+  //Calculate total number of parameter
+  Standard_Integer aTotalNbParams = 1;
+  while(theCommand.Location(aTotalNbParams,COMMA,1,theCommand.Length()))
+    aTotalNbParams++;
+
+
+  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){
+    
+    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);
+    }
+    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<<"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) 
+      {
+        aStartPos = theCommand.Location(O_BRACKET, 1, theCommand.Length()) + 1;
+        aEndPos = theCommand.Location(COMMA, 1, theCommand.Length());
+      }
+    //Replace last parameter (bettwen ',' character and ')' character)
+    else if(i == aVarSeq.Length())
+      {
+        aStartPos = theCommand.Location(i-1, COMMA, 1, theCommand.Length()) + 2;
+        aEndPos = theCommand.Location(C_BRACKET, 1, theCommand.Length());
+      }
+    //Replace other parameters (bettwen two ',' characters)
+    else if(i != 1 && i != aVarSeq.Length())
+      {
+        aStartPos = theCommand.Location(i-1, COMMA, 1, theCommand.Length()) + 2;
+        aEndPos = theCommand.Location(i, COMMA, 1, theCommand.Length());
+      }
+
+    TCollection_AsciiString aParameter = theCommand.SubString(aStartPos, aStartPos);
+
+    if(MYDEBUG)
+      cout<<"Current parameter "<<aParameter<<endl;
+
+    aVar = aVarSeq.Value(iVar);
+    
+    //If parameter is entry, skip it
+    aVar.RightAdjust();
+    aVar.LeftAdjust();
+    if(theVariableNames.IsBound(aVar))
+      continue;
+
+      
+    if(aVar.IsEmpty()) {
+      iVar++;
+      continue;
+    }
+    
+    aVar.InsertBefore(1,"\"");
+    aVar.InsertAfter(aVar.Length(),"\"");
+
+    theCommand.Remove(aStartPos, aEndPos - aStartPos);
+    theCommand.Insert(aStartPos,aVar);    
+    iVar++;
+  }
+}
index ab6b8282117288344670e1d9dfc0a58292c0fe2c..f5f475cb580a0a477914620c8786d2b5f1d73016 100644 (file)
@@ -84,6 +84,7 @@ class GEOM_Engine
 
   Standard_EXPORT TCollection_AsciiString DumpPython(int theDocID, 
                                     Resource_DataMapOfAsciiStringAsciiString& theObjectNames,
+                                     Resource_DataMapOfAsciiStringAsciiString& theVariableNames,
                                     bool isPublished, 
                                     bool& aValidScript);
 
index 8f545f63ec88fa9c80e8b74a76c4080e25c9316c..c8fe6918a1c4fd854bb1ae31f8bfe730b86f554e 100644 (file)
@@ -46,7 +46,7 @@ Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
   if(CORBA::is_nil(aSO))
     return new Engines::TMPFile(0);  
 
-  Resource_DataMapOfAsciiStringAsciiString aMap;
+  Resource_DataMapOfAsciiStringAsciiString aMap, aVariableMap;
 
   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
@@ -59,6 +59,16 @@ Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
         CORBA::String_var aName = aValue->GetName();
         CORBA::String_var anEntry = GO->GetEntry();
        aMap.Bind( (char*)anEntry.in(), (char*)aName.in() );
+       
+       //Find attribute with list of used notebook variables
+       SALOMEDS::GenericAttribute_var anAttr;
+       SALOMEDS::AttributeString_var anAttrStr;
+       TCollection_AsciiString aParameters;
+       if(aValue->FindAttribute(anAttr,"AttributeString")){
+         anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
+         aParameters = TCollection_AsciiString(anAttrStr->Value());
+       }
+       aVariableMap.Bind((char*)anEntry.in(),aParameters);
       }
     }
   }
@@ -66,7 +76,7 @@ Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
   TCollection_AsciiString aScript =
     "### This file is generated by SALOME automatically by dump python functionality\n"
       "### of GEOM component\n\n";
-  aScript += _impl->DumpPython(aStudy->StudyId(), aMap, isPublished, isValidScript);
+  aScript += _impl->DumpPython(aStudy->StudyId(), aMap, aVariableMap, isPublished, isValidScript);
 
   if (isPublished)
   {
index d3477a9a3853e1f341764b090ff7036af397a6d1..2a883e4251fc06490d08fd1bedce3fca6ec5af35 100644 (file)
@@ -80,6 +80,8 @@ import salome
 salome.salome_init()
 from salome import *
 
+from salome_notebook import *
+
 import GEOM
 import math
 
@@ -92,6 +94,29 @@ ShapeType = {"COMPOUND":0, "COMPSOLID":1, "SOLID":2, "SHELL":3, "FACE":4, "WIRE"
 def RaiseIfFailed (Method_name, Operation):
     if Operation.IsDone() == 0 and Operation.GetErrorCode() != "NOT_FOUND_ANY":
         raise RuntimeError, Method_name + " : " + Operation.GetErrorCode()
+    
+## Return list of variables value from salome notebook
+## @ingroup l1_geompy_auxiliary    
+def ParseParameters(*parameters):
+    Result = []
+    StringResult = ""
+    for parameter in parameters:
+        if isinstance(parameter,str):
+            if notebook.isVariable(parameter):
+                Result.append(notebook.get(parameter))
+                pass
+            pass
+        else:
+            Result.append(parameter)
+            pass
+        
+        StringResult = StringResult + str(parameter)
+        StringResult = StringResult + ":"
+        pass
+    StringResult = StringResult[:len(StringResult)-1]
+    Result.append(StringResult)
+    return Result
+    
 
 ## Kinds of shape enumeration
 #  @ingroup l1_geompy_auxiliary
@@ -684,6 +709,8 @@ class geompyDC(GEOM._objref_GEOM_Gen):
         #  @ref tui_creation_box "Example"
         def MakeBoxDXDYDZ(self,theDX, theDY, theDZ):
             # Example: see GEOM_TestAll.py
+            theDX,theDY,theDZ,Parameters = ParseParameters(theDX, theDY, theDZ)
+            self.PrimOp.SetParameters(Parameters)
             anObj = self.PrimOp.MakeBoxDXDYDZ(theDX, theDY, theDZ)
             RaiseIfFailed("MakeBoxDXDYDZ", self.PrimOp)
             return anObj