Skip to content

Tightest Tolerance

Imagine you are developing a pricing or quoting engine, and you need to know the tightest tolerance on a Technical Drawing. This may occur because you want to add a price premium if the part is difficult to manufacture, or perhaps you wish to offer a discount if the part is simpler to manufacture.

First, simply send the Technical Drawing to WERK24! We will run the Technical Drawing through our Artificial Intelligence, and several seconds later tell you what tolerances we found on the drawing.

All that remains for you to do is to define what "tightest" tolerance means and filter for it.

Define Query Function

If you already had a look at at the Thumbnail Tutorial, this snippet should look extremely familiar:

from werk24 import Hook, W24TechreadClient, W24AskVariantMeasures

async def get_measures(drawing_bytes):
    hooks = [Hook(ask=W24AskVariantMeasures(), function=handle_measures)]
    async with W24TechreadClient.make_from_env() as session:
        await session.read_drawing_with_hooks(drawing_bytes,hooks)

Define the Response Handler

Great! Now, the handle_measures / handleMeasures is being called once for each sectional that we found on the document you sent us. Let us be resource-hungry and store all of the responses.

measures = []
async def handle_measures(cur_measures):
    measures += cur_measures

Define "tightest tolerance"

What "tightest tolerance" really means depends on your application and the number of design choices you have already made in your code. For the sake of simplicity, we are using the easiest of definitions.

from werk24.models.size_tolerance import W24SizeToleranceType

# define the set of tolerance types that define tolerance
# intervals. Other tolerance types are references to the
# general tolerances, contain no toleration info, or only
# define an upper/lower limit, but no interval.
TOLERANCE_TYPES_ACCEPT = {
    W24SizeToleranceType.FIT_SIZE_ISO,
    W24SizeToleranceType.OFF_SIZE}

def calc_tolerance_tightness(tolerance):

    # only two toleration types actually define
    # toleration intervals: OFF_SIZE and FIT_SIZE_ISO
    if tolerance.toleration_type not in TOLERANCE_TYPES_ACCEPT:
        return float("inf")

    # then we calculate the actual tightness
    return tolerance.deviation_upper - toleration.deviation_lower

Find the "tightest tolerance"

At this stage, we have (i) all measures including their tolerances, and (ii) a function that calculated the "tightness" of the tolerance. All that is missing is a small min() function call and some simple structuring.

# get the tightest tolerance. If the measures list
# empty, default to infinity
tightest_tolerance = min(
    (calc_tolerance_tightness(m.size_tolerance)
     for m in measures),
    default = float("inf"))

That's it. Now you end up with a variable describing that is set to infinity if the drawing does not contain any relevant toleration information, or you receive a float value that describes the tightness.