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?

2022-08-15
2023-08-09
  • 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 - 2022-09-19

    Try this:
    https://help.codesys.com/webapp/_cds_struct_using_scripts;product=codesys;version=3.5.17.0

    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 + ";")
    
     

    Related

    Talk.ru: 1
    Talk.ru: 2

  • bnewman - 2023-08-08

    Here is an updated version that works in Codesys 3.5.19.2

    That old version didn't like the line

    fb.create_property(propname, proptype)
    
    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
                membername = var[0].strip(" ")
                propname = membername.lstrip("_");
                proptype = var[1].strip(" ");
                print("Creating property \"" + propname + "\" with type " + proptype)
                fb.create_property(propname, return_type=proptype, language=None)
                print("property " + propname + " with type " + proptype + "was created!")
    
                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")
                print("Inserting to getter")
                getter.textual_implementation.replace(propname + ":= " + membername + ";")
                print("Inserting to setter")
                setter.textual_implementation.replace(membername + ":= " + propname + ";")
    
    CreateAllProperties("*NameOfFunctionBlock*")
    
     
    πŸ‘
    1
  • hermsen

    hermsen - 2023-08-09

    Thank you for this snippet, I took the liberty to post repost it here;
    https://forge.codesys.com/tol/scripting/snippets/search/?q=status%3AUnlicense

     
    • bnewman - 2023-08-09

      That's fine with me
      I added a comment to that snippet with an improved version that doesn't throw an exception if the property already exists

       

      Last edit: bnewman 2023-08-10

Log in to post a comment.