Variable "Measure Angle" Feature Script Code001

warren_pegley
warren_pegley Member Posts: 14 ✭
// This is the first time I've touched this code - so a point in the right direction would be helpful, I think this script would be a helpful asset also so if there joy in punching out new features - please do.

FeatureScript 1271;
import(path : "onshape/std/geometry.fs", version : "1271.0");

/** Measure the Angle between two sets of entities and set the result to a variable */
annotation { "Feature Type Name" : "Measure Angle", "Feature Name Template" : "###name = #value", "UIHint" : "NO_PREVIEW_PROVIDED" }
export const measureAngle = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        annotation { "Name" : "Name", "Default" : "Angle" }
        definition.name is string;

        annotation { "Name" : "First entities", "Filter" : EntityType.FACE || EntityType.EDGE || EntityType.VERTEX || EntityType.BODY }
        definition.first is Query;

        annotation { "Name" : "Second entities", "Filter" : EntityType.FACE || EntityType.EDGE || EntityType.VERTEX || EntityType.BODY }
        definition.second is Query;

        annotation { "Name" : "Maximum" }
        definition.maximum is boolean;
    }
    {
        if (!match(definition.name, '[a-zA-Z_][a-zA-Z_0-9]*').hasMatch)
            throw regenError(ErrorStringEnum.VARIABLE_NAME_INVALID);

        const angleResult is AngleResult = evAngle(context, {
                    "side0" : definition.first,
                    "side1" : definition.second,
                    "maximum" : definition.maximum
                });

        const p0 = angleBetween(vector1, vector2)Result.sides[0].point;
        const p1 = angleBetween(vector1, vector2)Result.sides[1].point;
        if (!tolerantEquals(p0, p1))
        {
            debug(context, line(p0, p1 - p0));
            debug(context, line(p1, p0 - p1));
        }
        else // If it's the same point, just draw the point
        {
            debug(context, p0);
        }

        setFeatureComputedParameter(context, id, {
                "name" : "value",
                "value" : angleBetween(vector1, vector2)Result.angle
        });
        setVariable(context, definition.name, angleResult.angle);
    });

Comments