I need a script to insert a POU below of Application object.
The Scripting API was extended with the necessary features with CODESYS V3.5 SP9. Look for IScriptIecLanguageObjectContainer and IScriptIecLanguageMemberContainer in the Scripting documentation.
I need too a script to edit a specific POU (MainPrg, for example), i.e., add new variables and modify code of POU.
We can only edit Structure Text directly. For the graphical languages you have to import the POU from PLCopenXML or our native format. See IScriptTextualObjectMarker, IScriptObjectWithTextualDeclaration, IScriptObjectWithTextualImplementation and IScriptTextDocument for the textual language and the methods import_xml() and import_native() from IScriptObject2 and IScriptProject2 for the import.
Finally is necessary deletar a specific POU.
Find the Scripting object, for example with find(), and use the method remove() to delete the object.
Here some example:
proj = projects.primary
found = proj.find("Application", True)
app = found
# Create FB
mypou = app.create_pou("MyPou")
# Change declaration of the FB
implementation = mypou.textual_declaration.replace("""FUNCTION_BLOCK MyPou
iValue : INT;
# Change implementation of the FB
mypou.textual_implementation.replace("""iValue := iValue + 1;""")
# Add method to FB
dosomething = mypou.create_method("DoSomething", "INT")
# Change declaration of the method
dosomething.textual_declaration.replace("""METHOD DoSomething : INT
iVal1 : INT;
iVal2 : INT;
# Change implementation of the method
dosomething.textual_implementation.replace("""DoSomething := iVal1 + iVal2;""")
# Find the pou and delete it
found = app.find("MyPou")
if found and len(found) == 1:
print("POU 'MyPou' was not found"
Check out the CODESYS store: http://store.codesys.com/CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions