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.
Why do op* functions require explicit id
brad_phelan
Member Posts: 89 ✭✭
Why is it that op* functions all take an explicit unique id instead of just taking the parent id and returnIng a new unique id.
After writing a few featurescript functions now I find myself repeating the construction of ids and thinking up new names that really have no purpose other than being unique.
Am I missing some subtlety that gives an advantage to manually constructing the ids?
After writing a few featurescript functions now I find myself repeating the construction of ids and thinking up new names that really have no purpose other than being unique.
Am I missing some subtlety that gives an advantage to manually constructing the ids?
0
Best Answer
-
kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565Great question. The answer is, it's all about the queries!
The most obvious example of this is inside your feature, where you can use ids in a query like qCreatedBy for a very robust way of getting at the geometry you created.
But an even more important reason is that outside your feature, downstream features are also going to reference the geometry from your feature. When they do so, these references should be robust and parametric when a user changes things upstream (something Onshape frankly excels at). When this happens, the queries generated are often going to involve the ids you provided in your code.
Consider these two features (Which I've put in a public document here: https://cad.onshape.com/documents/473a6f032091ec947b930ab2/w/f668e1c6c409278ddd5451ba/e/eb4ebbf09f80c92678e002eb )for the first feature, and
id+"FLq6FZ7GzZcIJwA_0.cuboidForMainThing.extrude.opExtrude"
for the second. The reason the first part studio fails is that the id "1" (from our global counter) changed as soon as the if statement resolved the other way. Here it failed to resolve, but you could also imagine it successfully resolving to the wrong thing. This is the opposite of robust parametric references, and not something we want proliferating through the Part Studios of anyone using a custom feature.
So yeah, it is annoying to keep thinking of ids like "cuboidForMainThing". But the upside is really robust parametric references to those things you create!
</code>FeatureScript 660; import(path : "onshape/std/geometry.fs", version : "660.0"); annotation { "Feature Type Name" : "Arbitrary ids" } export const arbitraryIds = defineFeature(function(context is Context, id is Id, definition is map) precondition { annotation { "Name" : "Do optional thingy" } definition.doOptionalThingy is boolean; } { var globalCount = 0; if (definition.doOptionalThingy) { fCuboid(context, id + globalCount, { "corner1" : vector(0, 0, 0) * inch, "corner2" : vector(1, 1, 1) * inch }); globalCount += 1; } fCuboid(context, id + globalCount, { "corner1" : vector(3, 0, 0) * inch, "corner2" : vector(4, 1, 1) * inch }); }); annotation { "Feature Type Name" : "Semantic ids" } export const semanticIds = defineFeature(function(context is Context, id is Id, definition is map) precondition { annotation { "Name" : "Do optional thingy" } definition.doOptionalThingy is boolean; } { if (definition.doOptionalThingy) { fCuboid(context, id + "cuboidForOptionalThing", { "corner1" : vector(0, 0, 0) * inch, "corner2" : vector(1, 1, 1) * inch }); } fCuboid(context, id + "cuboidForMainThing", { "corner1" : vector(3, 0, 0) * inch, "corner2" : vector(4, 1, 1) * inch }); }); </pre><div>Each feature has an if statement where different geometry is created based on some condition (in this case, it's a user input, but you can imagine any check of inputs, surrounding geometry, etc.)<br><br></div>In the first feature, I've imitated an Id system similar to what you've proposed: taking the parent id and returnIng a new unique id (In this case, I've used a simple global counter to make it unique).<br><br>In the second feature, I've kept the id consistent for each piece of code (where "piece of code", in practice, corresponds pretty well with "I'm making the same thing here").<br><br>Then, I built each feature in a Part Studio with the doOptionalThingy checkbox checked, and I filleted one of the edges of the main cuboid.<br><br>Now here's the test: When I go back and uncheck the doOptionalThingy box, does my fillet still work? With the first feature, my fillet breaks. With the second feature, it remains intact.<br><br>To find out exactly why you can right-click to "show code" on each Part Studio, where you'll see that the query used for the fillet involves the ids used in your feature: <code>id+"FB64yUZr1UQrhdJ_0.1.extrude.opExtrude"2
Answers
The most obvious example of this is inside your feature, where you can use ids in a query like qCreatedBy for a very robust way of getting at the geometry you created.
But an even more important reason is that outside your feature, downstream features are also going to reference the geometry from your feature. When they do so, these references should be robust and parametric when a user changes things upstream (something Onshape frankly excels at). When this happens, the queries generated are often going to involve the ids you provided in your code.
Consider these two features (Which I've put in a public document here: https://cad.onshape.com/documents/473a6f032091ec947b930ab2/w/f668e1c6c409278ddd5451ba/e/eb4ebbf09f80c92678e002eb )