CODESYS - the IEC 61131-3 automation software
https://forum.codesys.com/

Execute custom object commands in python script
https://forum.codesys.com/viewtopic.php?f=18&t=5554
Page 1 of 1

Author:  dsDeveloper [ Wed Jan 08, 2014 11:51 am ]
Post subject:  Execute custom object commands in python script

Good day,

I am trying to call and execute custom objects using python script and hope someone could help me out with that.
Currently with OnDriverLoad() I add an object to ScriptEngine which I call companyCommand. This companyCommand object
implements the IScripExecutor interface (C#) and has the function "execute".

Now if I want to call that function in a pythong script which I passed to the .exe, how do I need to proceed? Is it enough to add

Code:
companyCommand.execute(...)


and also pass the GUID of the object or do I need to call it in a special way? Any imports necessary? I couldn't find a tutorial
where this is shown.

Best regards and Thank you,

dsDeveloper

P.S.

I will be able to provide more information on the implementation when I am back at the office tomorrow.

Author:  M.Schaber [ Wed Jan 08, 2014 12:12 pm ]
Post subject:  Re: Execute custom object commands in python script

Hi, dsDeveloper,

First, this seems to be an Automation Platform question, which is best directed to the developer contact you got during your Automation Platform training.

The developer network has an article How to make a scriptable plugin which explains the basics of creating scriptable plugins. (If you don't have an account yet, create one, and contact your 3S Sales Rep. to enable access to the AP section.)

The IScriptExecutor interface is implemented by the Script Engine, and other Automation Platform plugins use it to trigger the execution of scripts. It is not to be implemented by objects which are provided for the scripts - those object should just provide the interface to be used by the scripts.

In your IScriptDriver, during the OnDriverLoad() method, just pass in the objects you want to be accessible for the script:
Code:
[TypeGuid("{insert-your-guid-here}")]
public class ScriptDriverSystem : IScriptDriver
{
    public void OnDriverLoad(IScriptExecutor executor)
    {
        // The script can access an instance of our CompanyObject() class by the name
        // company_object and call all its methods:
        executor.ProvideObjectForScript("company_object", new CompanyObject());

        // Some of our methods need the MyEnum parameter, so we provide the enum to the script.
        // Enums and other type objects which need to be accessible by the script (e. G. for instantiation and
        // calling of static methods) need to be passed through PrepareType - it creates a python type out of
        // the .NET Type object.
        executor.ProvideObjectForScript("MyEnum", executor.Engine.PrepareType(typeof(MyEnum)));

        // You can also provide functions directly, by creating delegates:
        executor.ProvideObjectForScript("company_function", new System.Action<int>(SomeMethod));
    }

    public void SomeMethod(int i) { do something here... }
}

public enum MyEnum { ... }

public class CompanyObject { ... }

Author:  dsDeveloper [ Thu Jan 09, 2014 12:52 pm ]
Post subject:  Re: Execute custom object commands in python script

Thank you for your reply, Schaber. I checked the website and my implementation and it seemed to be correct. My issue was
the way I passed arguments to a function of the company object. Python uses lists and an array was expected. I was using
params in the C# code, so just passing the arguments one by one was the solution. Now it works as it should (at least this step).

Thank you again for your effort!

Best regards,

dsDeveloper

Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/