Create Python Script Behaviors in a .Net Add-on Programming

Because some commands are not available in .Net you sometimes need to create Python Scripts to run them via the workaround => Change a property on ISimComponent which triggers a Python Script.

Because in my use case it is not given that the Python script does exist on the component. That’s why I want to search for it and if it isn’t available create it by code. Below you can find my current code. Is there a way to create behaviors in VC?

        IProperty SaveBitmapProperty = component.GetProperty("saveBitmap");

        if (SaveBitmapProperty == null)
            component.CreateProperty(typeof(string), PropertyConstraintType.AllValuesAllowed, "saveBitmap");

        IBehavior SaveBitmapBehavior = component.FindBehavior("saveBitmap");

        if (SaveBitmapBehavior == null)
        {
            // Create Python Script Behavior Here
        }

To create behaviors see ISimNode.CreateBehavior(…) and ISimNode.CreateBehaviorByType(…)

I don’t know what exactly you want to do with “save bitmap”, but I’m pretty sure it is possible from the .NET API.

Thank you for the fast response.

So I want to make a screenshot and couldn’t figure out to create one within .Net:

Here is my Python Code. As you can see I’m using a saveBitmap Property to define the target path, which is triggering the script at the same time.

from vcScript import *
app = getApplication()
comp = getComponent()
bitmap_path = comp.getProperty("saveBitmap")

def saveBitmap(*args): 
  if bitmap_path.Value == "" : 
     return
  cmd = app.findCommand("saveBitmap")
  cmd.execute(bitmap_path.Value,'rpng',1920,1080)
  bitmap_path.Value = ""  

bitmap_path.OnChanged = saveBitmap

If you can show me how to do this in .Net I would be very glad, otherwise I will try to create a behavior with the ISimNode.

Thank you very much!

So it worked. Here is the result:

        IProperty SaveBitmapProperty = component.GetProperty("saveBitmap");

        if (SaveBitmapProperty == null)
            component.CreateProperty(typeof(string), PropertyConstraintType.AllValuesAllowed, "saveBitmap");

        IPythonScript SaveBitmapBehavior = (IPythonScript)component.FindBehavior("saveBitmap");

        if (SaveBitmapBehavior == null)
        {
            ISimNode simNode = component.Nodes[0];
            SaveBitmapBehavior = (IPythonScript)simNode.CreateBehaviorByType(BehaviorType.PythonScript);
            SaveBitmapBehavior.Name = "saveBitmap";
            SaveBitmapBehavior.Script = Properties.Resources.saveBitmap; // String with the python code
        }

Still, if you find a way on how to create a bitmap with the .NET API let me know! There is some functionallity in the PlotViewEx Class, but I couldn’t figure out how to fetch this object from the session.

See IApplication.SurfaceToImage(…) and IApplication.SurfaceToBuffer(…) methods, those have several overloads.

1 Like