Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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
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
|
---|
|
|
|
|
|
|
|
|
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
Remove the brace before precondition, remove the closing brace.
Thanks Neil
so that seemed to help a little now all i have is
Error in initializer function arguments
@harold_lasswell I'm not quite sure what you are trying to do towards the end, so it has been commented out. But here, I cleaned it up a bit for you so that it will run up to the end of the code where it has been commented out. I also added a drop down enum and moved your constant map outside of the feature's function:
https://cad.onshape.com/documents/5e328e3ddf165fc0f55149c6/w/ab41ebe8d64667171eacd0d1/e/f29d2e3bec509d4e64478a19
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴
Thanks Micheal
I'm still learning. Mistakes were made. I'll try it out tonight and see how it runs!
Nice! Keep it up. 😎
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴