Skip to main content
Skip table of contents

Pykechain (Python SDK for KE-chain)

Pykechain is a python library developed by KE-works with the sole purpose to interact with the KE-chain datamodel, explorer, tasks and the scripts environment. Basically, an experienced user in pykechain, can extract any information stored in any project and use it to build scripts that would ultimately produce the desired outputs. Furthermore, he can also automatically create or extend the data model, add part instances, activities and even configure and customize said activities. Below, you will find some video examples of simple scripts ran in a KE-chain project, together with the code itself:

Update property values

This script introduces the user to the one of the most important functionalities of pykechain: retrieving and storing values. Property values are accessed, used in computations and the results are then stored in other property values. Iteration can be performed again and again, based on new inputs.

CODE
# Retrieve the project where this script is ran
project = get_project()

# Retrieve the wheel part model
wheel_model = project.model(name='Wheel')

# Retrieve all the part instances created based on the wheel model
wheel_parts = project.parts(model=wheel_model)

# Loop through the list of part instances
for wheel_part in wheel_parts:
    # Retrieve the value of the 'Diameter' property belonging to the wheel part instance
    wheel_diameter = wheel_part.property(name='Diameter').value

    # Calculate the circumference based on the diameter (C = pi*d), rounded to 2 decimals
    circumference = round(wheel_diameter * math.pi, 2)

    # Store the circumference in the value of the 'Circumference' property
    wheel_part.property(name='Circumference').value = circumference

Add parts and properties

In this script, you can get an idea how new models and new properties can be automatically created. 

CODE
# Make the needed imports from pykechain
from pykechain import get_project
from pykechain.enums import PropertyType


# Retrieve the project where this script is ran
project = get_project()

# Retrieve the bike part model
bike_model = project.model(name='Bicycle')

# Create a new 'Exactly 1' model under 'Bicycle' and call it 'Saddle'
saddle_model = bike_model.add_model(name='Saddle', multiplicity='ONE')

# Add some properties to it
saddle_model.add_property(name='Material', property_type=PropertyType.CHAR_VALUE, default_value='Nylon')
saddle_model.add_property(name='Saddle tilt', property_type=PropertyType.FLOAT_VALUE,
                          description='Angle of saddle compared to ground (degrees)')


Add tasks OUTDATED

The third script presents some actions that can be performed on the work breakdown.

Tasks have been replaced by forms since the 2023-04 Release Forms app 4.0

CODE
# Make the needed imports from pykechain
from pykechain import get_project
from pykechain.enums import ActivityType


# Retrieve the project where this script is ran
project = get_project()

# Retrieve the root process of the work breakdown
workflow_root = project.activity(name='WORKFLOW_ROOT')

# Create a new process called 'Design saddle'
design_saddle = workflow_root.create(name='Design saddle', activity_type=ActivityType.PROCESS)

# Create a new task under it called 'Define saddle properties'
design_saddle.create(name='Define saddle properties', activity_type=ActivityType.TASK)

Add widgets OUTDATED

Finally, this script will show how easy it is to add widgets to an activity.

Tasks have been replaced by forms since the 2023-04 Release Forms app 4.0

PY
# Retrieve the project where this script is ran
project = get_project()

# Retrieve the 'Define saddle properties' activity in a variable
define_saddle_properties = project.activity(name='Define saddle properties')

# Retrieve the 'Saddle' part model in a variable
saddle_model = project.model(name='Saddle')

# Retrieve the two properties of 'Saddle'
saddle_material = saddle_model.property(name='Material')
saddle_tilt = saddle_model.property(name='Saddle tilt')

# Add a Form widget to the activity with the two properties as writable inputs
widget_manager = define_saddle_properties.widgets()
widget_manager.add_propertygrid_widget(part_instance=saddle_model.instance(),
                                       custom_title='Saddle overview',
                                       readable_models=[],
                                       writable_models=[saddle_material, saddle_tilt])

Final remarks

This article is not meant to be a Pykechain tutorial, but rather an introduction to the potential this package has in respect to KE-chain. If you desire to follow a Pykechain tutorial, please contact the Service Desk.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.