Skip to content

Tightest Tolerance

Imaging you are developing a pricing or quoting engine and you need to know the tightest tolerance on the Technical Drawing, so that you can add a premium if the part is difficult to manufacture, or add a discount if the part is very simple to manufacture.

Plainly 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.

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(document_bytes,hooks)
async function getMeasures(drawingBytes) {
    const askLib = await werk24.loadAsks();
    const hooks = [new werk24.Hook(new askLib.W24AskVariantMeasures(), handleMeasures)];

    let client = werk24.W24TechreadClient.makeFromEnv();
    try { await client.getSectionalThumbnails(drawingBytes, hooks);}
    catch (e) { console.error(e); } finally { client.close(); }
}

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 repsonses.

measures = []
async def handle_measures(cur_measures):
    measures += cur_measures
var measures = [];
async function handleMeasures(curMeasures) {
    measures = measures.concat(curMeasures);
}

Define "tightest tolerance"

What the "tightest tolerance" really means depends on your application and your a number of design choice that you have already made in your code. For simplicity reasons, we are using the easiest of all defintions

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
// define the list 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.
const TOLERANCE_TYPES_ACCEPT = ["FIT_SIZE_ISO","OFF_SIZE"];

function calcToleranceTightness(tolerance) {
    // only two toleration types actually define
    // toleration intervals: OFF_SIZE and FIT_SIZE_ISO
    if (!TOLERANCE_TYPES_ACCEPT.includes(tolerance.toleration_type)){
        return Infinity
    }

    // then calcualte 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"))
// get the tightest tolerance. If the measures list 
// empty, default to infinity
toleranceTightnesses = [];
for (curMeasure in measures){
    curTightness = calcToleranceTightness(curMeasure.size_tolerance)
    toleranceTightnesses.push(curTightness)
}

tightestTolerance = Math.min(toleranceTightnesses)

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