Only Structured Text can be edited directly!
For the graphical languages you have to import the POU from PLCopenXML or the codesys native format.
See IScriptTextualObjectMarker, IScriptObjectWithTextualDeclaration, IScriptObjectWithTextualImplementation and IScriptTextDocument for the textual language and the methods importxml() and importnative() from IScriptObject2 and IScriptProject2 for the import. Find the Scripting object, for example withfind(), and use the method remove() to delete an object.
proj = projects.primary found = proj.find("Application", True) app = found[0] # Create FB mypou = app.create_pou("MyPou") # Change declaration of the FB implementation = mypou.textual_declaration.replace("""FUNCTION_BLOCK MyPou VAR_INPUT iValue : INT; END_VAR VAR_OUTPUT END_VAR VAR END_VAR""") # 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 VAR_INPUT iVal1 : INT; iVal2 : INT; END_VAR""") # 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: found[0].remove() else: print("POU 'MyPou' was not found")
Diff: