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.

Script error i can't resolve please help. It's driving me nuts

markus_coxmarkus_cox Member Posts: 6

here is my code.

import(path:"onshape/std/standard.fs",version:"2581.0");

// Offset Skin FeatureScript for Xbox Controller (with Multiple Grip Patterns)
// Creates an offset skin for an Xbox controller, allowing interactive
// definition of the front face cut-out region and multiple grip patterns.

export const xboxSkin = defineFeature(function(context is Context, id is Id, definition is map)
annotation { "Feature Name" = "Xbox Series Controller Skin"; }
precondition
{
// Configuration Parameters

// Offset distance from the controller surface.
annotation { "Name" : "Offset Distance", "Filter" : LENGTH_BOUND_FILTER }
definition.offsetDistance is Length;

// Thickness of the skin material.
annotation { "Name" : "Skin Thickness", "Filter" : LENGTH_BOUND_FILTER }
definition.skinThickness is Length;

// Front cut plane (User selects this plane)
annotation { "Name" : "Front Cut Plane", "Filter" : EntityType.PLANE }
definition.frontCutPlane is Query;

// Bounding box for defining the area where the skin is applied (excluding the front)
annotation { "Name" : "Cut Box Max X", "Filter" : LENGTH_BOUND_FILTER }
definition.cutBoxMaxX is Length;
annotation { "Name" : "Cut Box Min X", "Filter" : LENGTH_BOUND_FILTER }
definition.cutBoxMinX is Length;
annotation { "Name" : "Cut Box Max Y", "Filter" : LENGTH_BOUND_FILTER }
definition.cutBoxMaxY is Length;
annotation { "Name" : "Cut Box Min Y", "Filter" : LENGTH_BOUND_FILTER }
definition.cutBoxMinY is Length;
annotation { "Name" : "Cut Box Max Z", "Filter" : LENGTH_BOUND_FILTER }
definition.cutBoxMaxZ is Length;
annotation { "Name" : "Cut Box Min Z", "Filter" : LENGTH_BOUND_FILTER }
definition.cutBoxMinZ is Length;

// Grip Pattern Parameters
annotation { "Name" : "Grip Pattern Type", "Filter" : ENUM_BOUND_FILTER, "Enum Name" : "GripType" }
definition.gripPatternType is string;

annotation { "Name" : "Grip Pattern Spacing", "Filter" : LENGTH_BOUND_FILTER }
definition.gripPatternSpacing is Length;

annotation { "Name" : "Grip Pattern Size", "Filter" : LENGTH_BOUND_FILTER }
definition.gripPatternSize is Length;

// Letter Pattern Specific Parameters (example - add more as needed)
annotation { "Name" : "Grip Text", "Filter" : STRING_BOUND_FILTER }
definition.gripText is string;

annotation { "Name" : "Grip Text Spacing", "Filter" : LENGTH_BOUND_FILTER }
definition.gripTextSpacing is Length;

//Predicate
isDefined(definition.frontCutPlane);
}
{
// --- Main Logic ---
//Set default values
definition.offsetDistance = 1 * millimeter;
definition.skinThickness = 2 * millimeter;
definition.cutBoxMaxX = 100 * millimeter;
definition.cutBoxMinX = -100 * millimeter;
definition.cutBoxMaxY = 50 * millimeter;
definition.cutBoxMinY = -50 * millimeter;
definition.cutBoxMaxZ = 50 * millimeter;
definition.cutBoxMinZ = -50 * millimeter;
definition.gripPatternType = "Circles";
definition.gripPatternSpacing = 2 * millimeter;
definition.gripPatternSize = 1 * millimeter;
definition.gripText = "GRIP";
definition.gripTextSpacing = 1 * millimeter;

// 1. Get the base Xbox controller body.
const controllerBody = qOwnedBy(Context.defaultPartStudio(), EntityType.BODY);

// 2. Create an offset surface from the controller body.
const offsetSurface = opThicken(context, id + "offsetSurface", {
"entities" : controllerBody,
"direction" : Vector3d.Z, // Offset outwards
"distance" : definition.offsetDistance,
"oppositeDirection" : false, // Thicken inwards, *towards* the original body
"type" : ThickenType.ADD // Create a *new* surface, don't modify the original
});

// 3. Create a Bounding Box for the cut
const cutBox = opCreateBox(context, id + "cutBox", {
"corner1" : vector(definition.cutBoxMinX, definition.cutBoxMinY, definition.cutBoxMinZ),
"corner2" : vector(definition.cutBoxMaxX, definition.cutBoxMaxY, definition.cutBoxMaxZ),
"coordinateSystem" : worldCoordinateSystem
});

// 4. Create an extruded cut using the user-selected plane.
const frontCut = opExtrude(context, id + "frontCut", {
"entities" : definition.frontCutPlane,
"direction" : evPlaneNormal(context, {"plane" : definition.frontCutPlane}), // Extrude along the plane's normal
"endBound" : BoundingType.THROUGH_ALL,
"endDepth" : undefined,
"startBound": BoundingType.BLIND,
"startDepth": 0 * millimeter,
"oppositeDirection" : true, // Cut in the opposite direction of the normal
"remove" : true, // Remove material
"surface" : false
});

// 5. Boolean Subtract: Remove the cutBox from the offsetSurface.
const skinWithoutFront = opBoolean(context, id + "skinWithoutFront", {
"tools" : cutBox,
"targets" : offsetSurface,
"operationType" : BooleanOperationType.SUBTRACT,
"keepTools" : false
});

// 6. Boolean Subtract: Remove the frontCut from the skinWithoutFront.
const skinWithoutFront2 = opBoolean(context, id + "skinWithoutFront2", {
"tools" : frontCut,
"targets" : skinWithoutFront,
"operationType" : BooleanOperationType.SUBTRACT,
"keepTools" : false
});

// --- Grip Pattern Generation (Conditional based on gripPatternType) ---

if (definition.gripPatternType == "Circles") {
// Generate an array of circles
var circles = [];
for (var i = 0; i < 10; i += 1) // Example: 10 rows
{
for (var j = 0; j < 10; j += 1) // Example: 10 columns
{
var circle = opCreateCircle(context, id + "circle" + i + "_" + j, {
"center" : vector(definition.cutBoxMinX + j * definition.gripPatternSpacing, definition.cutBoxMinY + i * definition.gripPatternSpacing, 0),
"radius" : definition.gripPatternSize / 2,
"plane" : worldCoordinateSystem.xYPlane // Project onto a plane for now
});
circles = append(circles, circle);
}
}

// Extrude the circles as cuts.
opExtrude(context, id + "circleCuts", {
"entities" : circles,
"direction" : vector(0, 0, 1),
"endBound" : BoundingType.BLIND,
"endDepth" : definition.skinThickness,
"oppositeDirection" : true,
"remove" : true,
"surface" : false
});

} else if (definition.gripPatternType == "Squares") {
// Generate an array of sketches, then extrude.
var squares = [];
for (var i = 0; i < 10; i += 1)
{
for (var j = 0; j < 10; j += 1)
{
var square = opCreateBox(context, id + "square" + i + "_" + j, {
"corner1" : vector(definition.cutBoxMinX + j * definition.gripPatternSpacing , definition.cutBoxMinY + i * definition.gripPatternSpacing , 0),
"corner2" : vector(definition.cutBoxMinX + j * definition.gripPatternSpacing + definition.gripPatternSize, definition.cutBoxMinY + i * definition.gripPatternSpacing + definition.gripPatternSize, 0),
"coordinateSystem" : worldCoordinateSystem
});
squares = append(squares, square);
}
}

// Extrude the squares as cuts.
opExtrude(context, id + "squareCuts", {
"entities" : squares,
"direction" : vector(0, 0, 1),
"endBound" : BoundingType.BLIND,
"endDepth" : definition.skinThickness,
"oppositeDirection" : true,
"remove" : true,
"surface" : false
});

} else if (definition.gripPatternType == "Hexagons") {
// TODO: Implement hexagon pattern generation
println("Hexagon pattern not yet implemented.");

} else if (definition.gripPatternType == "Letters") {
// TODO: Implement letter pattern generation (using opText)
println("Letter pattern not yet implemented.");

} else if (definition.gripPatternType == "Custom") {
// TODO: Provide a mechanism for custom pattern input (e.g., a sketch)
println("Custom pattern not yet implemented.");
}

// 7. Thicken the remaining surface to create the final solid skin.
opThicken(context, id + "finalSkin", {
"entities" : skinWithoutFront2, // Use the result after the boolean
"direction" : Vector3d.Z,
"distance" : definition.skinThickness,
"oppositeDirection" : false, // Thicken *outwards* from the offset surface
"type" : ThickenType.ADD // Add material to create a solid
});
}

im getting the problem in the first annotation word and a symbol on the same line. I cant solve it even using ai to check it.

Answers

  • Jed_YeiserJed_Yeiser Member Posts: 44 PRO

    I'd be helpful to share your document, as well as describe what you're trying to achieve. That being said, it looks as if you've put your annotation for your feature in the feature definition, not above the feature declaration, as it should be. There's also a semicolon in the annotation statement that shouldn't be there. I'd encourage you to look into the intro to featurescript course. I have not tried many LLM's outside of ChatGPT for featurescript, but I've not fount cgpt4 to be borderline useless.

    export const xboxSkin = defineFeature(function(context is Context, id is Id, definition is map)

    annotation { "Feature Name" = "Xbox Series Controller Skin"; }

    precondition

    {

    annotation { "Feature Type Name" = "Xbox Series Controller Skin", "Feature Type Description" : "" }

    export const xboxSkin = defineFeature(function(context is Context, id is Id, definition is map)

    precondition

    {

  • markus_coxmarkus_cox Member Posts: 6

    I've given the ai all of onshapes documents to teach itself as the ai on its own is poor at this stuff.

  • markus_coxmarkus_cox Member Posts: 6

    finally got it to work. Took me hours to retrain the ai. Just cabt seem to find my features once I've hit comicommit, not showing up when I search for them

Sign In or Register to comment.