Multi-Hook Tutorial¶
This tutorial guides you through sending multiple asks to the Werk24 API (i.e., asking the Werk24 Services to extract several different things) and processing the responses.
Prerequisites¶
- A Werk24 account and API key.
- Python 3 with the
werk24
library installed (pip install werk24
). - A Technical Drawing. You can download an example from here: DRAWING_SUCCESS.png
Steps¶
Import necessary libraries¶
from werk24 import Hook, W24AskTitleBlock, W24AskVariantMeasures
from werk24.models.measure import W24Measure
from werk24.models.techread import W24TechreadMessage
from werk24.utils import w24_read_sync
Define a function to read the drawing data¶
def get_drawing_bytes(path: str = "DRAWING_SUCCESS.png") -> bytes:
""" Reads the contents of the file as bytes. """
with open(path, "rb") as drawing_file:
return drawing_file.read()
Define callback functions for received data:¶
These functions will be called when the Werk24 API sends information back.
receive_measures
: This function processes the received measures (dimensions) from the API. It validates them using the W24Measure.model_validate method and stores them in a list.receive_title_block
: This function handles receiving the title block information from the API. You can customize this function for your specific needs.
Read the drawing data and define hooks¶
get_drawing_bytes
: This function is called to read the drawing data from the file.hooks
: This list defines what information you want to retrieve from the Werk24 API and which callback function should handle the received data. Here, we request measures (W24AskVariantMeasures) and define the receive_measures function to process them. You can add more hooks for other data types.
# Read the drawing data from the file
drawing_data = get_drawing_bytes()
# Specify the information to be retrieved from the Werk24 API
# and the functions to handle the received data.
hooks = [
Hook(ask=W24AskTitleBlock(), function=receive_title_block),
Hook(ask=W24AskVariantMeasures(), function=receive_measures),
]
Submit the request to the Werk24 API¶
The w24_read_sync function sends the drawing data and the defined hooks to the Werk24 API for processing.
# Submit the request to the Werk24 API with the drawing data and hooks
w24_read_sync(drawing_data, hooks)
Print the extracted measures¶
This section iterates through the extracted_measures list (populated by the receive_measures function) and prints the blurb attribute of each measure (assuming it has one). You can modify this to print other relevant information from the measures. You can extend this to also print the information from the Title Block.
# Print the extracted measures (assuming they have a "blurb" attribute)
print("Extracted Measures:")
for measure in extracted_measures:
print(measure.label.blurb)