Maroder1 / Openness_examples_python

27 stars 7 forks source link

SetAttribute Method #5

Closed Chops91 closed 3 years ago

Chops91 commented 3 years ago

Hi,

I don't really have an issue just wasn't sure how to contact you direct. Firstly thanks for the pointers and examples you have provided they have been of great help to get me as far as I have got so far, Python for me is much easier to get my head around than C# and all the visual studio overhead and so this is more me reaching out asking for some pointers.

I have a project where I can add a device however I want to change some of the default device configuration values for our application and doing this for 1000's of devices manually is very tedious so here goes scripting.

I can add the device and I have managed to get to the attributes that I want to update the issue I am facing now is how exactly the syntax should be for the values that I am trying to achieve.

From the openness API docs there is not much info regarding the SetAttribute function so I am using the library SetAttribute example as some guidance for what I want to achieve. Extract from page 142 of the Openness API docs below:

// Setting the name attribute
var type = project.ProjectLibrary.TypeFolder.Types.Find("SampleTypeName");
type.Name = "NewTypeName";
//Setting the name attribute dynamically
var type = project.ProjectLibrary.TypeFolder.Types.Find("SampleTypeName");
type.SetAttributes(new[] {new KeyValuePair<string,object>("Name", "NewTypeName")});

Below is an example for my application, where the device names and what it is looking for specifically have been substituted out for something a little more generic.

ungroupedDevices = myproject.UngroupedDevicesGroup.Devices
for hardware in ungroupedDevices:
    #print(f"Hardware | {hardware.Name}")
    if hardware.Name == "specific_device_for_testing":
        for items in hardware.DeviceItems:
            print(f"Items: {items.Name}")
            print(f"TypeIdentifier: {items.TypeIdentifier}")
            for lowLevel in items.DeviceItems:
                 if lowLevel.Name == "PN-IO":
                     lowLevel.SetAttribute("some_attribute", 2)

Running the above I get the error:

The type of the argument 'some_attribute' (System.Int32) is invalid.
   at Siemens.Engineering.Private.Session.Siemens.Engineering.Private.IObjectSession.SetAttribute[TC](LifetimeContractHandle`1 lifetimeContractHandle, String attributeName, Object attributeValue, String fullName) in c:\Users\User\AppData\Local\Temp\zqyf5piw\zqyf5piw.57.cs:line 2055
   at Siemens.Engineering.Private.InternalObject_Access`2.SetAttribute(String name, Object value) in c:\Users\User\AppData\Local\Temp\zqyf5piw\zqyf5piw.59.cs:line 458

The problem I appear to be having is that the '2' (attribute value) that I am trying to set is not of the type object. Looking at the docs this seems consistent as the set attribute is expecting a KeyValuePair<string, object>.

Do you by any chance have any insights into how I can do this so that '2' (attribute value) is an object? Or how I can create a KeyValuePair<string,object> in python?

Maroder1 commented 3 years ago

Hello.

You need to specify the type.

import System
for hardware in myproject.Devices:
    for item in hardware.DeviceItems:
        if item.Name == "PLC1":
            attribute = (item.GetAttribute("CycleCommunicationLoad"))
            print (attribute)
            print (type(attribute))
            item.SetAttribute("CycleCommunicationLoad", System.UInt64(60))
            attribute = (item.GetAttribute("CycleCommunicationLoad"))
            print (attribute)

I'm not sure how to properly find the correct types for all. I am not able to find it using openness explorer. Maybe in C# the GetAttributes will return the correct type? Either way I think its mosly Strings and UInt64 being used.

Chops91 commented 3 years ago

Thanks for the quick response. Now I am onto the next stumbling block.

One of the configuration values I want to change is part of the failsafe settings of a safety device and I now get this nice error:

A password was assigned for the safety program. Changes with Openness are therefore not permitted.

Anyway your suggestion does work for non failsafe configuration values.

Maroder1 commented 3 years ago

Which version of TIA and FW are you working on? I am not sure (just guessing), could it by any luck be something that has been added at a later SW/FW?

Chops91 commented 3 years ago

See below for my TIA version. Yes I am aware that V17 is already but you know how it goes in the corporate world. We have just got our V16 license so will be making the move to that shortly. image

You have answered the initial question so will close the issue.

Thank you for your assistance!

Chops91 commented 3 years ago

Just FYI from page 610 of the Openness API docs.

image