<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>Ticket search results</title><link>https://forge.codesys.com/tol/scripting/snippets/</link><description>You searched for assigned_to:"codesys-com"</description><language>en</language><lastBuildDate>Wed, 09 Aug 2023 17:04:52 -0000</lastBuildDate><item><title>CreateProperties from variable declaration in FB</title><link>https://forge.codesys.com/tol/scripting/snippets/23/</link><description>See original posting at: https://forge.codesys.com/forge/talk/Engineering/thread/678b24f53f/#d16e
By CODESYS and Ben Newman


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*")</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">hermsen</dc:creator><pubDate>Wed, 09 Aug 2023 17:04:52 -0000</pubDate><guid isPermaLink="false">https://forge.codesys.com/tol/scripting/snippets/23/</guid></item></channel></rss>