Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

features script help

harold_lasswellharold_lasswell Member Posts: 7 ✭✭
edited September 18 in General

I've been trying to write a script to create NPT threads. i keep getting the same error from the precondition block and i can figure it out and its driving me nuts lol

these the errors

Error in initializer function arguments

7:54

NPT internal thread

extraneous input 'precondition' expecting {'annotation', '[', '+', '-', 'return', 'if', 'for', 'while', 'new', '!', '++', '--', '::', 'function', 'operator', 'undefined', 'break', 'continue', 'var', 'const', 'try', 'in', 'switch', 'throw', SEMI, '{', '(', '}', BOOLEAN, NUMBER, ID, BUILTIN, STRING}

10:5

NPT internal thread

extraneous input ')' expecting {<EOF>, 'annotation', 'export', 'enum', 'type', 'function', 'predicate', 'operator', 'typeconvert', 'const', TOP_SEMI}

107:2

NPT internal thread

This is the code

// FeatureScript version declaration (Required)
FeatureScript 2455;
import(path : "onshape/std/common.fs", version : "2455.0");

// Define the custom NPT internal thread feature
annotation { "Feature Type Name" : "NPT Internal Thread", "Feature Type Description" : "Creates an internal NPT thread on a selected face with multiple size options" }
export const nptInternalThreadFeature = defineFeature(function(context is Context, id is Id, definition is map)
{
// Precondition block to define inputs
precondition
{
// Input for selecting the face for threading
definition.face is Query;
annotation { "Name" : "Threaded Face", "Filter" : EntityType.FACE };

    // Enum input for selecting the NPT thread size
definition.threadSize is string;
annotation { "Name" : "NPT Thread Size", "Values" : ["1/8", "1/4", "3/8", "1/2", "3/4", "1"], "Default" : "1/4" };

// Input for setting the thread depth
definition.threadDepth is Length;
annotation { "Name" : "Thread Depth", "Default" : 1 * inch, "Min" : 0 * inch, "Max" : 10 * inch };
}

// Main execution block
{
// Define NPT thread sizes and dimensions
const nptDimensions = {
"1/8": { "pitch": 27, "diameter": 0.405 * inch },
"1/4": { "pitch": 18, "diameter": 0.5401 * inch },
"3/8": { "pitch": 18, "diameter": 0.675 * inch },
"1/2": { "pitch": 14, "diameter": 0.84 * inch },
"3/4": { "pitch": 14, "diameter": 1.05 * inch },
"1": { "pitch": 11.5, "diameter": 1.315 * inch }
};

// Fetch the selected thread size and corresponding dimensions
var selectedThread = nptDimensions[definition.threadSize];
var pitch = selectedThread.pitch;
var diameter = selectedThread.diameter;

// Get the face's position and normal
var facePlane = evFaceTangentPlane(context, {
"face": definition.face
});

// Thread depth from user input
var threadDepth = definition.threadDepth;

// Create the sketch for the cylindrical base of the internal thread
var sketch = newSketch(context, id + "sketch", {
"sketchPlane": facePlane
});

// Draw a circle for the base
skCircle(sketch, "circle", {
"center": vector(0, 0) * inch,
"radius": diameter / 2
});
skSolve(sketch);

// Extrude the sketch with a taper angle to form the thread base
opExtrude(context, id + "extrude", {
"entities": qSketchRegion(id + "sketch"),
"direction": facePlane.normal,
"endBound": BoundingType.BLIND,
"depth": threadDepth,
"taperAngle": -1.7899 * degree // Standard NPT taper for internal threads
});

// Create the thread profile (a small triangle)
var threadProfile = newSketchOnPlane(context, id + "threadProfile", {
"sketchPlane": plane(facePlane.origin, facePlane.normal)
});

// Define the thread height based on the pitch
var threadHeight = (0.8 / pitch) * inch;

// Draw the triangle for the thread profile
skLineSegment(threadProfile, "line1", {
"start": vector(0, 0),
"end": vector(threadHeight / tan(30 * degree), threadHeight)
});
skLineSegment(threadProfile, "line2", {
"start": vector(threadHeight / tan(30 * degree), threadHeight),
"end": vector(-threadHeight / tan(30 * degree), threadHeight)
});
skLineSegment(threadProfile, "line3", {
"start": vector(-threadHeight / tan(30 * degree), threadHeight),
"end": vector(0, 0)
});
skSolve(threadProfile);

// Sweep the profile along the cylindrical base to create the thread
opSweep(context, id + "sweep", {
"profiles": qSketchRegion(id + "threadProfile"),
"path": qCreatedBy(id + "extrude", EntityType.EDGE)
});

// Subtract the thread from the main body to create internal threads
opBoolean(context, id + "subtractThread", {
"tools": qCreatedBy(id + "sweep", EntityType.BODY),
"operationType": BooleanOperationType.SUBTRACTION,
"targets": qBody()
});
}

});

Comments

Sign In or Register to comment.