Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

User created Refactoring functions?

  • thecolonel26

    thecolonel26 - 2022-08-15

    Is there any way to extend Codesys Development System with C# or Python to add your own refactoring functions?

    I would very much like to automate FB properties to automatically add a backing VAR. It is vary tedious doing that by hand.

  • tvm

    tvm - 2022-09-19

    Try this:

    This script will find any variable that starts with an underscore and create a property for it

    def CreateAllProperties(fbname):
        # get current project
        project = projects.primary 
        # find the function block
        fb = project.find(fbname, True)[0]
        # get the declaration text
        declaration = fb.textual_declaration
        # iterate all lines in the declaration
        for i in range(declaration.linecount):
            line = declaration.get_line(i)  # get this declaration line
            stripped = line.strip() #strip out white space in this line
            endofline = stripped.find(";")
            stripped = stripped[0:endofline]    #discards anything past the ";" (comments, etc)
            if stripped.startswith("_"):
                var = stripped.split(":")   #NOTE: this also removes initial value declarations, or puts them into the [2] index of var, which we can discard
                propname = var[0][1:]
                proptype = var[1]
                print("Creating property " + propname + " with type " + proptype)
                fb.create_property(propname, proptype)
                newprop = fb.find(propname)[0]
                getter = newprop.find("Get")[0]
                setter = newprop.find("Set")[0]            
                newprop.textual_declaration.insert(0,0,"{attribute 'monitoring':='call'}\r\n")
                getter.textual_implementation.replace(propname + ":= _" + propname + ";")
                setter.textual_implementation.replace("_" + propname + ":= " + propname + ";")

Log in to post a comment.