defCreateAllProperties(fbname):#getcurrentprojectproject=projects.primary#findthefunctionblockfb=project.find(fbname,True)[0]#getthedeclarationtextdeclaration=fb.textual_declaration#iteratealllinesinthedeclarationforiinrange(declaration.linecount):line=declaration.get_line(i)#getthisdeclarationlinestripped=line.strip()#stripoutwhitespaceinthislineendofline=stripped.find(";")stripped=stripped[0:endofline]#discardsanythingpastthe";"(comments,etc)ifstripped.startswith("_"):var=stripped.split(":")#NOTE:thisalsoremovesinitialvaluedeclarations,orputsthemintothe[2]indexofvar,whichwecandiscardmembername=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*")
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
this is an improved version that doesn't throw an exception if the property already exists
defCreateAllProperties(fbname,shouldAddMonitor=False):#getcurrentprojectproject=projects.primary#findthefunctionblockfb=project.find(fbname,True)[0]#getthedeclarationtextdeclaration=fb.textual_declaration#iteratealllinesinthedeclarationforiinrange(declaration.linecount):line=declaration.get_line(i)#getthisdeclarationlinestripped=line.strip()#stripoutwhitespaceinthislineendofline=stripped.find(";")stripped=stripped[0:endofline]#discardsanythingpastthe";"(comments,etc)ifstripped.startswith("_"):var=stripped.split(":")#NOTE:thisalsoremovesinitialvaluedeclarations,orputsthemintothe[2]indexofvar,whichwecandiscardmembername=var[0].strip(" ")propname=membername.lstrip("_");proptype=var[1].strip(" ");ifnotfb.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]ifshouldAddMonitor: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*")
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
this is an improved version that doesn't throw an exception if the property already exists