cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Access application node with python script

Access application node with python script

TobiasFr
New Poster

How do I access the application node of a project with a python script.
Tested with sample script from 3S.

The intention to access the application from script is to test whether it would be possible to generate PLC projects from script, based on an EPLAN export as well as some additional parameter files or not. Generating PLC-projects via script is currently part of our workflow.

 

Script:

# encoding:utf-8
# We enable the new python 3 print syntax
from __future__ import print_function

# Prints out all devices in the currently open project.

print("--- Printing the devices of the project: ---")

# Define the printing function. This function starts with the
# so called "docstring" which is the recommended way to document
# functions in python.
def print_tree(treeobj, depth=0):
""" Print a device and all its children

Arguments:
treeobj -- the object to print
depth -- The current depth within the tree (default 0).

The argument 'depth' is used by recursive call and
should not be supplied by the user.
"""

# if the current object is a device, we print the name and device identification.
if treeobj.is_device:
name = treeobj.get_name(False)
deviceid = treeobj.get_device_identification()
print("{0}- {1} {2}".format("--"*depth, name, deviceid))

# we recursively call the print_tree function for the child objects.
for child in treeobj.get_children(False):
print_tree(child, depth+1)

# We iterate over all top level objects and call the print_tree function for them.
for obj in projects.primary.get_children():
print_tree(obj)

print("--- Script finished. ---")

 

Project:

Output:BoschPlcTree.PNG

BoschPlcOutput.PNG

 

4 REPLIES 4

HmiGuide
Community Moderator
Community Moderator

I didn't use that very interesting and powerful CoDeSys interface up to now. My expection is that this info must be available in the CoDeSys documentation.

georkueh
Community Moderator
Community Moderator

We provide APIs for ctrlX PLC Engineering and ctrlX IO Engineering based on a REST API for automated creation of PLC programs and IO configurations. Maybe this is an alternate approach.

HmiGuide
Community Moderator
Community Moderator

Here you find the CoDeSys Scripting Engine docu: https://help.codesys.com/webapp/ScriptProject;product=ScriptEngine;version=3.5.16.0

Here an example to get applicaion node of active application.

# encoding:utf-8
from __future__ import print_function

proj = projects.primary             # get loaded project
app  = proj.active_application      # get active application
print("App Name=", app.get_name() ) # print application name

HmiGuide
Community Moderator
Community Moderator
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist