Table of Contents



Complete Examples

This chapter combines the ideas from the previous tutorials into complete scripts.

The examples use:

Use these examples as patterns for your own scripts.


Tool Radius and Area

This example calculates tool radius and cross-section area from a diameter.

function ToolRadius(diameter)
{
    if(!diameter.is_num_notnan() || diameter <= 0)
    {
        error('Tool diameter must be a positive number');
    };
 
    return diameter / 2;
}
 
function ToolArea(diameter)
{
    set radius = ToolRadius(diameter);
    return pi() * radius ** 2;
}
 
diameter = 6;
radius = ToolRadius(diameter);
area = ToolArea(diameter);
 
'Tool radius=' + radius + ', area=' + area;

What this shows:


Probe Status

This example converts a probe error value into a status string.

function ProbeStatus(errorValue)
{
    if(errorValue.is_none())
    {
        return 'missing';
    };
 
    if(!errorValue.is_num_notnan())
    {
        return 'invalid';
    };
 
    if(errorValue <= 0.01)
    {
        return 'excellent';
    }
    else if(errorValue <= 0.05)
    {
        return 'acceptable';
    }
    else if(errorValue <= 0.10)
    {
        return 'warning';
    };
 
    return 'failed';
}
 
probeError = 0.03;
status = ProbeStatus(probeError);
 
status;

The final value is `'acceptable'`.

What this shows:


Safe Division

This example returns `none()` instead of failing when division cannot be done.

function SafeDivide(a, b)
{
    if(!a.is_num_notnan() || !b.is_num_notnan())
    {
        return none();
    };
 
    if(b == 0)
    {
        return none();
    };
 
    return a / b;
}
 
result = SafeDivide(10, 2);
 
if(result.is_none())
{
    'division failed';
}
else
{
    'result=' + result;
};

The final value is `'result=5'`.

What this shows:


Counter With loop

This example counts how many times a loop runs.

count = 0;
 
loop(5)
{
    count += 1;
};
 
count;

The final value is `5`.

The same pattern can be used for repeated calculations, repeated checks, or simple fixed-count automation steps.


Sum With for

This example adds numbers from `1` to `5`.

sum = 0;
 
for(i = 1; i <= 5; i += 1)
{
    sum += i;
};
 
sum;

The final value is `15`.

What this shows:


Skip Values With continue

This example adds only odd numbers.

sum = 0;
 
for(i = 1; i <= 6; i += 1)
{
    if(i % 2 == 0)
    {
        continue;
    };
 
    sum += i;
};
 
sum;

The final value is `9` because the script adds `1 + 3 + 5`.

What this shows:


Find First Matching Value

This example searches for the first value larger than a limit.

limit = 10;
found = none();
 
for(i = 1; i <= 20; i += 1)
{
    value = i * 3;
 
    if(value > limit)
    {
        found = value;
        break;
    };
};
 
found;

The final value is `12`.

What this shows:


Tool Object

This example uses a class to keep tool data and tool calculations together.

class ToolInfo(number, diameter)
{
    Number = number;
    Diameter = diameter;
 
    function Radius()
    {
        return Diameter / 2;
    }
 
    function Description()
    {
        return 'T' + Number + ' D=' + Diameter + ' R=' + Radius();
    }
}
 
tool = ToolInfo(3, 8);
tool.Description();

The final value is `'T3 D=8 R=4'`.

What this shows:


Probe Result Object

This example stores a probe result and provides helper methods for status and display text.

class ProbeResult(x, y, z, errorValue)
{
    X = x;
    Y = y;
    Z = z;
    Error = errorValue;
 
    function IsValid()
    {
        return Error.is_num_notnan() && Error <= 0.05;
    }
 
    function Status()
    {
        if(Error.is_none())
        {
            return 'missing';
        };
 
        if(!Error.is_num_notnan())
        {
            return 'invalid';
        };
 
        if(Error <= 0.05)
        {
            return 'ok';
        };
 
        return 'too large';
    }
 
    function Summary()
    {
        return 'X=' + X + ', Y=' + Y + ', Z=' + Z + ', status=' + Status();
    }
}
 
probe = ProbeResult(10, 20, -1.5, 0.03);
probe.Summary();

The final value is `'X=10, Y=20, Z=-1.5, status=ok'`.

What this shows:


Average Calculator Object

This example accumulates values and calculates an average.

class Average()
{
    Count = 0;
    Sum = 0;
 
    function Add(value)
    {
        if(!value.is_num_notnan())
        {
            return false;
        };
 
        Count += 1;
        Sum += value;
        return true;
    }
 
    function Value()
    {
        if(Count == 0)
        {
            return none();
        };
 
        return Sum / Count;
    }
}
 
avg = Average();
avg.Add(10);
avg.Add(20);
avg.Add(30);
 
avg.Value();

The final value is `20`.

What this shows:


String Cleanup

This example normalizes text input.

function NormalizeToolName(name)
{
    if(!name.is_string())
    {
        return none();
    };
 
    return name.trim().upper();
}
 
NormalizeToolName('  tool 3  ');

The final value is `'TOOL 3'`.

What this shows:


Simple Validation Before Workflow

This example validates several values before a workflow continues.

function RequireNumber(value, name)
{
    if(!value.is_num_notnan())
    {
        error(name + ' must be a valid number');
    };
 
    return value;
}
 
function RequirePositive(value, name)
{
    RequireNumber(value, name);
 
    if(value <= 0)
    {
        error(name + ' must be positive');
    };
 
    return value;
}
 
toolDiameter = RequirePositive(6, 'Tool diameter');
probeStep = RequirePositive(0.2, 'Probe step');
 
'Tool diameter=' + toolDiameter + ', probe step=' + probeStep;

What this shows:


For scripts that affect machine behavior:


Where To Go Next

You have finished the beginner tutorial path.

Next useful sections: