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.

Options

Auto-number text on the same face of all parts

rashugerashuge Member Posts: 10

I am looking to try and make a FeatureScript that I can select a face, it will then place text with size and font as variables, on the face of every part. I have been trying to utilize Gemini and other AI tools to assist but the translation of how to actually use the code is limited and I am running into errors. What is the best recommended path? This is the code I got.

// FeatureScript 1.0;

import(path : "onshape/std/geometry.fs", version : "1.0");

// Define the user-facing name of the feature
annotation { "Feature Type Name" : "Sequential Part Numbers" }
export const sequentialPartNumbers = featureScript(3) {
// Define the UI parameters for the user
annotation {
"Name" : "Faces to Number",
"Description" : "Select faces in the order you want them numbered (1, 2, 3...).",
"Filter" : "Face",
"MaxNumberOfPicks" : -1 // Allow selecting an unlimited number of faces
}
definition.faces is array of Query;

annotation { "Name" : "Text Height", "Description" : "The height of the number text." }
definition.textHeight is Length;

annotation { "Name" : "Cut Depth", "Description" : "The total symmetric depth of the cut." }
definition.depth is Length;

annotation { "Name" : "Font Name", "Description" : "The font to use for the numbers (e.g., Roboto, Arial)." }
definition.font is string;

// The main logic of the FeatureScript
precondition
{
// Ensure the feature is used in a Part Studio
isAtLeast(evaluateQuery(context, qPartStudio), 1);
}

// The execution block where the geometry is created
execution
{
// Loop through each face selected by the user
for (var i = 0; i < size(definition.faces); i += 1)
{
// The part number is the loop index plus one
var partNumber = "" ~ (i + 1);
var faceQuery = definition.faces[i];

// Create a unique ID for this specific operation to avoid conflicts
var id = id + i;

// Get the body (part) that the selected face belongs to
var bodyToCut = evaluateQuery(context, qOwnerBody(faceQuery));

// Find the geometric center of the face to place the text
var planeOnFace = evFaceTangentPlane(context, { "face" : faceQuery, "parameter" : vector(0.5, 0.5) });

// Create a new sketch on the selected face
var newSketch = newSketchOnPlane(context, id + "sketch", { "sketchPlane" : planeOnFace });

// Add the number text to the sketch
opSketchText(context, id + "sketchText", {
"sketch" : newSketch,
"text" : partNumber,
"fontName" : definition.font,
"height" : definition.textHeight,
"transform" : fromPlane(planeOnFace) // Use the face's center and orientation
});

// Extrude the sketch text as a symmetric cut
opExtrude(context, id + "extrude", {
"entities" : newSketch,
"operationType" : OperationType.REMOVE,
"extrudeType" : ExtrudeType.SYMMETRIC,
"depth" : definition.depth,
"applyTo" : bodyToCut // Ensure the cut only affects the correct part
});
}
}

// Set default values for the parameters to make the feature easier to use
initialParameters
{
definition.textHeight = 0.25 * inch;
definition.depth = 0.05 * inch;
definition.font = "Roboto";
}

};

I know that I am missing something. I am more than willing to dig in and try to figure it out. But, at the very least if someone can point me in the right direction to get started, I would appreciate it.

Comments

  • Caden_ArmstrongCaden_Armstrong Member Posts: 291 PRO

    AI tools are pretty much useless for writing featurescripts. I don't think your script even has a single correct line of code in it.

    Have you considered using an existing feature such as the PP Surface text:
    https://cad.onshape.com/documents/cfec40e2b66bb4ddb2f3414b/w/24132f252a02825eb0606641/e/c8e6602fb08acac97a10a38e

    Adding text to a surface in a feature brings up a bunch of challenges, such as baseline orientation, how to handle non-planar surfaces, etc. Its not an easy first featurescript.
    If you want to learn featurescript, try the course onshape course:
    https://learn.onshape.com/courses/featurescript-fundamentals

    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
Sign In or Register to comment.