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.
Problem using opExtrude in functions
frank_halma
Member, Developers Posts: 2 ✭
I am trying to make some functions for creating extrusions. Unfortunately I cannot get things work.
When I use only 1 function, it works prefectly. Also when I disable the opExtrude, the sketches are made as I expected.
When I don't use functions I don't have any problems.
I am pretty new in FeatureScript, so probably I miss something easy...
Below the (simplified) script I tried.
Thanks a lot for helping me out!
Frank
When I use only 1 function, it works prefectly. Also when I disable the opExtrude, the sketches are made as I expected.
When I don't use functions I don't have any problems.
I am pretty new in FeatureScript, so probably I miss something easy...
Below the (simplified) script I tried.
Thanks a lot for helping me out!
Frank
FeatureScript 531;
import(path : "onshape/std/geometry.fs", version : "531.0");
const __BOOLEAN = "boolean";
const __DELETE = "delete";
const __EXTRUDE = "extrude";
const __PLANE = "plane";
const __POLYLINE = "polyline";
const __RECTANGLE = "rectangle";
const __REVOLVE = "revolve";
const __SKETCH = "sketch";
const __ZEROMM = 0 * millimeter;
annotation { "Feature Type Name" : "PARO002" }
export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
// Define the parameters of the feature type
}
{
var XMax = 300 * millimeter;
var YMax = 200 * millimeter;
var ZMax = 100 * millimeter;
var index = 0;
// Disable MB = makes CC = work
var MB = createMainBody(context, id, index, XMax, YMax, ZMax);
index = index + 1;
// Disanle CC = makes MB = work
var CC = createCutOut(context, id, index, XMax, YMax, ZMax, 2, 0 * millimeter, 0 * millimeter, 30 * millimeter, 20 * millimeter, 0.75 * YMax);
index = index + 1;
});
function createsketchPlane(context is Context, XMax, YMax, ZMax is ValueWithUnits, Face is number)
{
if (Face == 1)
{
return plane(vector(__ZEROMM, __ZEROMM, ZMax), vector(0, 0, 1), vector(1, 0, 0));
}
if (Face == 2)
{
return plane(vector(__ZEROMM, __ZEROMM, ZMax), vector(0, -1, 0), vector(1, 0, 0));
}
}
function createMainBody(context is Context, id is Id, index is number, XMax, YMax, ZMax is ValueWithUnits)
{
var sketchID = getID(id, __SKETCH, index);
var extrudeID = getID(id, __EXTRUDE, index);
var sketchplane = createsketchPlane(context, XMax, YMax, ZMax, 1);
var sketch = newSketchOnPlane(context, sketchID, {
"sketchPlane" : sketchplane
});
skRectangle(sketch, getID(__RECTANGLE, index), {
"firstCorner" : vector(0, 0) * millimeter,
"secondCorner" : vector(XMax, YMax)
});
skSolve(sketch);
// Without extrude sketches are made
opExtrude(context, extrudeID, {
"entities" : qSketchRegion(sketchID),
"direction" : sketchplane.normal * -1,
"endBound" : BoundingType.BLIND,
"endDepth" : ZMax
});
return qCreatedBy(extrudeID, EntityType.BODY);
}
function createCutOut(context is Context, id is Id, index is number, XMax, YMax, ZMax is ValueWithUnits, Face is number, XPos, YPos, Width, Height, Depth is ValueWithUnits)
{
var sketchID = getID(id, __SKETCH, index);
var extrudeID = getID(id, __EXTRUDE, index);
var sketchplane = createsketchPlane(context, XMax, YMax, ZMax, Face);
var sketch = newSketchOnPlane(context, sketchID, {
"sketchPlane" : sketchplane
});
skRectangle(sketch, getID(__RECTANGLE, index), {
"firstCorner" : vector(-XPos, -YPos),
"secondCorner" : vector(Width, Height)
});
skSolve(sketch);
// Without extrude sketches are made
opExtrude(context, extrudeID, {
"entities" : qSketchRegion(sketchID),
"direction" : sketchplane.normal * -1,
"endBound" : BoundingType.BLIND,
"endDepth" : Depth
});
return qCreatedBy(extrudeID, EntityType.BODY);
}
function getID(id is Id, name is string, index is number)
{
return id + name + index;
}
function getID(name is string, index is number)
{
return name ~ index;
}
0
Best Answer
-
kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565Hi Frank,
Looks like your issue is "revisiting" the sub-id named id + "sketch". That sub-id needs to refer to a contiguous region of history. I suggest reading the documentation for the Id type, which describes this issue.
A good fix is probable changing your getId() function to return id + index + name, or perhaps id + (name ~ index).
The error should be described in the notices flyout when you visit the Part Studio. When debugging it's also helpful to click "Monitor Part Studio 1" in the top right of your Feature Studio so you get feedback from runtime errors like this shown directly in your code whenever you commit a change (in this case, a warning underline on skSolve with the error described)
Hope that helps!
PS – In the future, linking to public document is more helpful than posting code, since this lets others immediately see the error state (and, as your FeatureScript gets more complex, shows us how you're using the feature in a Part Studio and handles imports correctly). If it's a work in progress, linking to a version is even better!
5
Answers
Looks like your issue is "revisiting" the sub-id named id + "sketch". That sub-id needs to refer to a contiguous region of history. I suggest reading the documentation for the Id type, which describes this issue.
A good fix is probable changing your getId() function to return id + index + name, or perhaps id + (name ~ index).
The error should be described in the notices flyout when you visit the Part Studio. When debugging it's also helpful to click "Monitor Part Studio 1" in the top right of your Feature Studio so you get feedback from runtime errors like this shown directly in your code whenever you commit a change (in this case, a warning underline on skSolve with the error described)
Hope that helps!
PS – In the future, linking to public document is more helpful than posting code, since this lets others immediately see the error state (and, as your FeatureScript gets more complex, shows us how you're using the feature in a Part Studio and handles imports correctly). If it's a work in progress, linking to a version is even better!
I changed the getID() function, that solves it (your link was very helpful).
I keep your PS in mind in case I have other questions.