(no subject)
Scripting
snippets
(Thread)
Last updated: 2023-08-09
Post by bnewman on #23 CreateProperties from variable declaration in FB
Scripting
snippets
(Post)
this is an improved version that doesn't throw an exception if the property already exists def CreateAllProperties(fbname, shouldAddMonitor = False): # 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(" "); if not fb.find(propname): print("Creating property \"" + propname + "\" with type " + proptype) fb.create_property(propname, return_type=proptype, language=None) newprop = fb.find(propname)[0] getter = newprop.find("Get")[0] setter = newprop.find("Set")[0] if shouldAddMonitor: newprop.textual_declaration.insert(0,0,"{attribute 'monitoring':='call'}\r\n") getter.textual_implementation.replace(propname + ":= " + membername + ";") setter.textual_implementation.replace(membername + ":= " + propname + ";") print("Property " + propname + " with type " + proptype + "was created!") print("Property " + propname + " Get and Set were implemented") else: print("Property \'" + propname + "\' already exists. Skipping"); CreateAllProperties("*NameOfFunctionBlock*")
Last updated: 2023-08-09
Ticket #23: CreateProperties from variable declaration in FB
Scripting
snippets
(Ticket)
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")2023-08-09 17:04:52.676000 Ticket #23: CreateProperties from variable declaration in FB Scripting scripting tickets snippets False /tol/scripting/snippets/23/ Ticket NOTE https://forge.codesys.com/forge/talk/Engineering/thread/678b24f53f/#d16e newprop CreateProperties from variable declaration in FB False 1 2023-08-09 09:37:00.711000 23 CreateProperties from variable declaration in FB Unlicense 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") False False 0 0 0 None hermsen codesys-com
Last updated: 2023-08-09
wiki Discussion
eatec
wiki
(Discussion)
Forum for wiki comments
Last updated: 2023-08-10
blog Discussion
eatec
blog
(Discussion)
Forum for blog comments
Last updated: 2023-08-10
(no subject)
eatec
wiki
(Thread)
Last updated: 2023-08-10
Home
eatec
wiki
(WikiPage)
This is the personal project of eatec. This project is created automatically during user registration as an easy place to store personal data that doesn't need its own project such as cloned repositories.
Last updated: 2023-08-10
Home
merar
wiki
(WikiPage)
This is the personal project of merar. This project is created automatically during user registration as an easy place to store personal data that doesn't need its own project such as cloned repositories.
Last updated: 2023-08-10
wiki Discussion
merar
wiki
(Discussion)
Forum for wiki comments
Last updated: 2023-08-10
blog Discussion
merar
blog
(Discussion)
Forum for blog comments
Last updated: 2023-08-10
blog Discussion
pjvnc
blog
(Discussion)
Forum for blog comments
Last updated: 2023-08-10
(no subject)
merar
wiki
(Thread)
Last updated: 2023-08-10
Home
pjvnc
wiki
(WikiPage)
This is the personal project of pjvnc. This project is created automatically during user registration as an easy place to store personal data that doesn't need its own project such as cloned repositories.
Last updated: 2023-08-10
wiki Discussion
pjvnc
wiki
(Discussion)
Forum for wiki comments
Last updated: 2023-08-10
(no subject)
pjvnc
wiki
(Thread)
Last updated: 2023-08-10
wiki Discussion
samf
wiki
(Discussion)
Forum for wiki comments
Last updated: 2023-08-10