#3 Create an FB

Unlicense
nobody
None
2020-02-08
2020-02-07
Ingo
No

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")

Discussion

  • Ingo

    Ingo - 2020-02-07
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -42,5 +42,5 @@
     if found and len(found) == 1:
        found[0].remove()
     else:
    -   print("POU 'MyPou' was not found"
    +   print("POU 'MyPou' was not found")
     ~~~
    
     
  • Ingo

    Ingo - 2020-02-08
    • status: Unassessed --> Unlicense
     

Log in to post a comment.