Showing results 1 to 8 of 8

Thread: Writing recipe values with C# AddIn

  1. #1
    Join Date
    11.02.2021
    Posts
    5

    Beitrag Writing recipe values with C# AddIn

    Hello
    i'm new to this forum and am currently working on a Zenon wizard. I would be very happy if someone can help.

    The problem arises when accessing data to recipe variables, the values are not saved permanently. I call the GetDynamicProperty () or SetDynamicProperty () methods via the AddIn Object Model (COM).
    The #Develop debugger generates debug messages that read and write took place as expected.
    However, no value changes are retained in the zenon editor (project manager: Project> -> Recipes-> RGM-> Edit recipe ...).

    C # source code:

    re">        object RcpValuePropValue = myRcpValue.GetDynamicProperty("Value");
    re">        context.DebugPrint("GetDynamicProperty("Value"): " + RcpValuePropValue.ToString(), DebugPrintStyle.Standard);

    re">        Type t = RcpValuePropValue.GetType();
    re">        context.DebugPrint("Type of object " + t.ToString(), DebugPrintStyle.Standard);

    re">        //set recipe value 
    re">        double Schwimmerlaenge = 440;re">                                
    re">        RcpValuePropValue = Schwimmerlaenge;

    re">        myRcpValue.SetDynamicProperty("Value", RcpValuePropValue);
    re">        context.DebugPrint("SetDynamicProperty("Value"...): " + Schwimmerlaenge.ToString(), DebugPrintStyle.Standard);

    re">        RcpValuePropValue = myRcpValue.GetDynamicProperty("Value");
    re">        context.DebugPrint("GetDynamicProperty("Value"): " + RcpValuePropValue.ToString(), DebugPrintStyle.Standard);
    re">                
                    if(!myRcp.SaveCurrentValuesAsRecipe())
    re">            context.DebugPrint("Error saving recipe!", DebugPrintStyle.Standard);
                                  
                    if(!myRcpGrp.Save())
    re">        context.DebugPrint("Error saving recipe group!", DebugPrintStyle.Standard);

    Debug-output:

    re">        Debug Ausgabe:GetDynamicProperty("Value"): 39
    re">        Debug Ausgabe:Type of object System.Double
    re">        Debug Ausgabe:SetDynamicProperty("Value"...): 440
    re">        Debug Ausgabe:GetDynamicProperty("Value"): 440

    Best regards
    seal

  2. #2
    Join Date
    03.11.2015
    Posts
    24

    Default Re: Writing recipe values with C# AddIn

    Hello Seal and welcome to the forum!

    reading you code it is a bit unclear what these variables are:

    'myRcpValue'
    'myRcp'
    '
    myRcpGrp'

    Are you accessing standard receipes? or receipes of the receipe group manager (RGM)?



     

  3. #3
    Join Date
    11.02.2021
    Posts
    5

    Default Re: Writing recipe values with C# AddIn

    Hi,
    thank you for your response.

    I'm accessing RGM receipes via the ObjectModel. The variables 'myRcpGrp', 'myRcp' and 'myRcpValue' are inhereted from the Interface class 'IRuntimeObject'.

    I have declared the variables myRcpGroup and myRcp in two loops.

    IRecipeGroup myRcpGrp = myProject.RecipeGroupCollection [i];
    'i' is the index of the outer loop. I go through all recipe groups of the RGM with the Index 'i'.

    IRecipe myRcp = myProject.RecipeGroupCollection [myProject.RecipeGroupCollection [i] .Name] [j];
    'j' is the index of the inner loop. 'j' runs through the recipes of the RGM recipe group.

    The variable 'myRcpValue' is the value of the RGM receipe variable that I want to write.

    I wrote the add-in as an editor Wizard in the Zenon Editor, as described in the Example:
    https://onlinehelp.copadata.com/help/800/addin/html/RecipeGroupManager-IRecipeValue-SetDynamicProperty(String,Object).htm

    Unfortunately, I did not find anything about converting the type 'object' into 'Double' in function 'SetDynamicProperty()'.

    Any idea how the values ​​are stored in the recipe?

    Kind regards
    Last edited by seal0706 : 16th February 2021 at 22:07

  4. #4
    Join Date
    11.02.2021
    Posts
    5

    Default Re: Writing recipe values with C# AddIn

    Hello,

    here is the whole Project Wizard:

    public void Run(IProject context, IBehavior behavior)
    {
    // enter your code which should be executed on triggering the function "Execute Project Wizard Extension" in the SCADA Runtime
    int i, j;

    if(context == null)
    {
    Console.WriteLine("No reference to an active zenon project");
    return;
    }

    IProject myProject = context;

    Console.WriteLine(myProject.RecipeGroupCollection.Count + " Recipe Groups");

    for (i = 0; i < myProject.RecipeGroupCollection.Count; i++)
    {
    IRecipeGroup myRcpGrp = myProject.RecipeGroupCollection[i];
    if(myProject.RecipeGroupCollection[ myProject.RecipeGroupCollection[i].Name ] == null)
    {
    Console.WriteLine("Recipe Group doesn't exist");
    return;
    }
    else
    {
    Console.WriteLine("myProject.RecipeGroupCollection[i].Name: " + myProject.RecipeGroupCollection[i].Name);

    for(j = 0; j < myProject.RecipeGroupCollection[ myProject.RecipeGroupCollection[i].Name ].Count; j++)
    {
    string name = myProject.RecipeGroupCollection[myProject.RecipeGroupCollection[i].Name][j].Name;

    Console.WriteLine("Recipe Info: " + name + " Schwimmerhöhe in mm: " + name.Substring(8,2));

    // get recipe
    IRecipe myRcp = myProject.RecipeGroupCollection[myProject.RecipeGroupCollection[i].Name][j];
    if (myRcp != null)
    {
    //get the last variable and value in the recipe
    int RcpCountVariable = myProject.RecipeGroupCollection[ myProject.RecipeGroupCollection[i].Name ].CountVariable;
    IRecipeVariable myRcpVar = myProject.RecipeGroupCollection[myProject.RecipeGroupCollection[i].Name].GetVariable(RcpCountVariable-1);
    IRecipeValue myRcpValue = myProject.RecipeGroupCollection[myProject.RecipeGroupCollection[i].Name][j].GetRecipeValue(RcpCountVariable-1);
    if (myRcpValue != null && myRcpVar != null)
    {
    Console.WriteLine("Variablename: " + myRcpVar.Name);

    object RcpValuePropValue = myRcpValue.GetDynamicProperty("Value");
    Console.WriteLine("GetDynamicProperty("Value"): " + RcpValuePropValue.ToString());

    //set recipe value
    double Schwimmerlaenge = Int32.Parse(name.Substring(8,2));
    RcpValuePropValue = Schwimmerlaenge;

    myRcpValue.SetDynamicProperty("Value", RcpValuePropValue);
    Console.WriteLine("SetDynamicProperty("Value"...): " + Schwimmerlaenge.ToString());

    RcpValuePropValue = myRcpValue.GetDynamicProperty("Value");
    Console.WriteLine("GetDynamicProperty("Value"): " + RcpValuePropValue.ToString());

    myRcp.SaveCurrentValuesAsRecipe();
    }
    }
    myRcpGrp.Save();
    }
    }
    }
    return;
    }

    I appreciate any help.

    Thxs
    Last edited by seal0706 : 23rd February 2021 at 17:29

  5. #5
    Join Date
    03.11.2015
    Posts
    24

    Default Re: Writing recipe values with C# AddIn

    However, no value changes are retained in the zenon editor
    Oh now I get It: you are trying to write a Project Wizard Extention to change something in the Editor?

    That's not possible. you should write a Editor Wizard Extention for the Editor. Project Wizards are for the Runtime
    Last edited by c.f. : 23rd February 2021 at 18:05

     

  6. #6
    Join Date
    11.02.2021
    Posts
    5

    Default Re: Writing recipe values with C# AddIn

    Hello,

    I've tried both options. No changes have been saved in the recipe with either the editor wizard or the runtime wizard.
    The value should be changed to 440. But In the recipe itself, the value remained unchanged.

    Here is the source code of the editor wizard:


    IProject myProject = context.Workspace.ActiveProject;

    context.DebugPrint(myProject.RecipeGroupCollection.Count + " Recipe Groups", DebugPrintStyle.Standard);

    for (i = 0; i < myProject.RecipeGroupCollection.Count; i++)
    {
    IRecipeGroup myRcpGrp = myProject.RecipeGroupCollection[i];

    if(myProject.RecipeGroupCollection[ myProject.RecipeGroupCollection[i].Name ] == null)
    {
    context.DebugPrint("Recipe Group doesn't exist", DebugPrintStyle.Error);
    return;
    }
    else
    {
    context.DebugPrint("myProject.RecipeGroupCollection[i].Name: " + myProject.RecipeGroupCollection[i].Name, DebugPrintStyle.Standard);

    for(j = 0; j < myProject.RecipeGroupCollection[ myProject.RecipeGroupCollection[i].Name ].Count; j++)
    {
    string name = myProject.RecipeGroupCollection[myProject.RecipeGroupCollection[i].Name][j].Name;
    context.DebugPrint("Recipe Info: " + name + " Schwimmerhöhe in mm: " + name.Substring(8,2), DebugPrintStyle.Standard);

    // get recipe
    IRecipe myRcp = myProject.RecipeGroupCollection[myProject.RecipeGroupCollection[i].Name][j];

    if (myRcp != null)
    {
    //get the last variable and value in the recipe
    int RcpCountVariable = myProject.RecipeGroupCollection[ myProject.RecipeGroupCollection[i].Name ].CountVariable;
    IRecipeVariable myRcpVar = myProject.RecipeGroupCollection[myProject.RecipeGroupCollection[i].Name].GetVariable(RcpCountVariable-1);
    IRecipeValue myRcpValue = myProject.RecipeGroupCollection[myProject.RecipeGroupCollection[i].Name][j].GetRecipeValue(RcpCountVariable-1);

    if (myRcpValue != null && myRcpVar != null)
    {
    context.DebugPrint("Variablename: " + myRcpVar.Name, DebugPrintStyle.Standard);
    object RcpValuePropValue = myRcpValue.GetDynamicProperty("Value");
    context.DebugPrint("GetDynamicProperty("Value"): " + RcpValuePropValue.ToString(), DebugPrintStyle.Standard);

    Type t = RcpValuePropValue.GetType();
    context.DebugPrint("Type of object " + t.ToString(), DebugPrintStyle.Standard);

    //set recipe value
    double Schwimmerlaenge = 440;
    RcpValuePropValue = Schwimmerlaenge;

    myRcpValue.SetDynamicProperty("Value", RcpValuePropValue);
    context.DebugPrint("SetDynamicProperty("Value"...): " + Schwimmerlaenge.ToString(), DebugPrintStyle.Standard);

    RcpValuePropValue = myRcpValue.GetDynamicProperty("Value");
    context.DebugPrint("GetDynamicProperty("Value"): " + RcpValuePropValue.ToString(), DebugPrintStyle.Standard);

    ItemSaved = myRcp.SaveCurrentValuesAsRecipe();

    if(!ItemSaved)
    {
    context.DebugPrint("Error saving recipe!", DebugPrintStyle.Standard);
    ItemSaved = false;
    }
    }
    }
    ItemSaved = myRcpGrp.Save();

    if(!ItemSaved)
    {
    context.DebugPrint("Error saving recipe!", DebugPrintStyle.Standard);
    ItemSaved = false;
    }
    }
    }
    }

    Here is the wizard as an image-file. it was not possible to format the sources

    Click image for larger version

Name:	Unbenannt.PNG
Views:	5
Size:	76.3 KB
ID:	3077
    Last edited by seal0706 : 24th February 2021 at 08:02

  7. #7
    Join Date
    03.11.2015
    Posts
    24

    Default Re: Writing recipe values with C# AddIn

    Hi,

    i modified the code a little and now it works (as Editor Wizard.)


    Code:
    public void Run(IEditorApplication context, IBehavior behavior)
        {
          IProject myProject = context.Workspace.ActiveProject;
    
    
          context.DebugPrint("Amount Recipe Groups" + myProject.RecipeGroupCollection.Count, DebugPrintStyle.Standard);
    
    
          for (var i = 0; i < myProject.RecipeGroupCollection.Count; i++)
          {
            IRecipeGroup currentRecipeGroup = myProject.RecipeGroupCollection[i];
            var currentRecipeGroupName = currentRecipeGroup.Name;
    
    
            if (myProject.RecipeGroupCollection[currentRecipeGroupName] == null)
            {
              context.DebugPrint("Recipe Group doesn't exist", DebugPrintStyle.Error);
              return;
            }
            else
            {
              context.DebugPrint($"Accessing Recipe Group myProject.RecipeGroupCollection[{i}].Name: " + currentRecipeGroupName, DebugPrintStyle.Standard);
    
    
              for (var j = 0; j < myProject.RecipeGroupCollection[currentRecipeGroupName].Count; j++)
              {
                // get recipe
                IRecipe currentRecipe = myProject.RecipeGroupCollection[currentRecipeGroupName][j];
    
    
                if (currentRecipe != null)
                {
                  string currentRecipeName = currentRecipe.Name;
    
    
                  //the following code line was altered because it seems very intimidating: the wizard is dependant on the name of a recipe. if the recipe name does not match string lenght criterias the wizard will fail.
                  //context.DebugPrint("Recipe Info: " + currentReicpeName + " Schwimmerhöhe in mm: " + currentReicpeName.Substring(8, 2), DebugPrintStyle.Standard);
                  context.DebugPrint("Current recipe Name: " + currentRecipeName, DebugPrintStyle.Standard);
    
    
    
    
                  //get the last variable and value in the recipe
                  int indexLastVariableInRecipe = myProject.RecipeGroupCollection[currentRecipeGroupName].CountVariable -1;
                  
                  IRecipeVariable myRcpVar = myProject.RecipeGroupCollection[currentRecipeGroupName].GetVariable(indexLastVariableInRecipe);
                  IRecipeValue myRcpValue = currentRecipe.GetRecipeValue(indexLastVariableInRecipe);
    
    
                  if (myRcpValue != null && myRcpVar != null)
                  {
                    context.DebugPrint("current variable name: " + myRcpVar.Name, DebugPrintStyle.Standard);
                    object RcpValuePropValue = myRcpValue.GetDynamicProperty("Value");
                    context.DebugPrint("GetDynamicProperty(\"Value\") is: " + RcpValuePropValue.ToString() + " of type: " + RcpValuePropValue.GetType() + " before change.", DebugPrintStyle.Standard);
    
    
                    //set recipe value
                    double Schwimmerlaenge = 45.0;
                    //RcpValuePropValue = Schwimmerlaenge;
    
    
                    myRcpValue.SetDynamicProperty("Value", Schwimmerlaenge);
                    context.DebugPrint("SetDynamicProperty(\"Value\"...) to new value: " + Schwimmerlaenge.ToString(), DebugPrintStyle.Standard);
    
    
                    RcpValuePropValue = myRcpValue.GetDynamicProperty("Value");
                    context.DebugPrint("GetDynamicProperty(\"Value\") is: " + RcpValuePropValue.ToString() + " after change.", DebugPrintStyle.Standard);
    
    
                    var recipeSaved = currentRecipe.SaveCurrentValuesAsRecipe();
    
    
                    if (!recipeSaved)
                    {
                      context.DebugPrint("Error saving recipe!", DebugPrintStyle.Standard);
                      recipeSaved = false;
                    }
                  }
                }
    
    
                var recipeGroupSaved = currentRecipeGroup.Save();
    
    
                if (!recipeGroupSaved)
                {
                  context.DebugPrint("Error saving recipe!", DebugPrintStyle.Standard);
                  recipeGroupSaved = false;
                }
              }
            }
          }
        }


    Code Formatting
    Last edited by c.f. : 24th February 2021 at 16:41 Reason: code formatting

     

  8. #8
    Join Date
    11.02.2021
    Posts
    5

    Default Re: Writing recipe values with C# AddIn

    Hi,

    many thanks for your answer.

    Meanwhile, I've found a simpler solution with graphical controls in the Zenon editor. However, i was very interested in whether it would work with a program and started your improved program again. It works!

    Best wishes

Similar Threads

  1. Replies: 2
    Last Post: 27th November 2019, 10:30
  2. Writing and reading bits from\to Word
    By masonpiotr in forum VSTA
    Replies: 1
    Last Post: 29th January 2015, 09:00
  3. Writing Cell values from Reports to Internal Variables
    By shikra in forum zenon Energy Edition
    Replies: 3
    Last Post: 24th February 2013, 00:55
  4. Writing a value using functions and redundancy
    By nmblanquart in forum zenon Service Engine Supervisor
    Replies: 3
    Last Post: 20th February 2012, 01:17
  5. Force synchronous writing for recipes
    By affoltep in forum zenon Service Engine Supervisor
    Replies: 3
    Last Post: 24th March 2010, 14:56

Tags for this Thread

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •