Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

How to import the RoboDK API & toolbox?

#1
Hi, I think some months ago the default code when adding a new Python program in the RoboDK tree has been changed in how RoboDK packages are loaded and 'RDK' is assigned. (Post edit: as I read here now: Python API for RoboDK 5.4.0)

The 'old' lines of code are as below, and I could use e.g. ITEM_TYPE_PROGRAM to search for a robot program:

Code:
from robolink import *    # RoboDK API
from robodk import *      # Robot toolbox
RDK = Robolink()

prog = RDK.Item('myProgram',ITEM_TYPE_PROGRAM)
print('Program name: ' + prog.Name())
raise Exception('Eof exception.')

This still works fine, the program is found and its name gets printed. The version with the 'new' preamble is however:

Code:
from robodk import robolink    # RoboDK API
from robodk import robomath    # Robot toolbox
RDK = robolink.Robolink()

prog = RDK.Item('myProgram',ITEM_TYPE_PROGRAM)
print('Program name: ' + prog.Name())
raise Exception('Eof exception.')

The latter results in an error: NameError: name 'ITEM_TYPE_PROGRAM' is not defined.
When I use: prog = RDK.Item('myProgram',8) using an integer 8 instead of the ITEM_TYPE_PROGRAM string, the item does get found.

Why do I get this error? And as it seems to have something to do with the loading of packages, how do you recommend to load the required packages?

Best regards,

Maarten
#2
Hi Maarten,

You need to use robolink.ITEM_TYPE_PROGRAM.
Please read the Forum Guidelines before posting!
Find useful information about RoboDK by visiting our Online Documentation.
#3
Hi Sam, thanks, that does the trick. So with the new package updates I can either use 'robolink.ITEM_TYPE_PROGRAM' or the corresponding integer ('8' in this case) when searching an item?
#4
No, there is no guarantee that the corresponding integer will not change in the future.

This has more to do with the way the python modules are imported. More info here: https://robodk.com/doc/en/PythonAPI/robo...dk-package
Please read the Forum Guidelines before posting!
Find useful information about RoboDK by visiting our Online Documentation.
#5
Hi Sam, in follow up to your last reply: When using Type() to determine what type an item is, it returns the integer value, not ITEM_TYPE_*. If there is no guarantee that the integer values will not change in the future, how can these be avoided when determining the type of an item?
#6
You need to compare the return from Item.Type() with a robolink.ITEM_TYPE_* in your code.

Code:
if item.Type() == robolink.ITEM_TYPE_ROBOT:
 print("this is a robot")
Please read the Forum Guidelines before posting!
Find useful information about RoboDK by visiting our Online Documentation.
  




Users browsing this thread:
1 Guest(s)