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.
FeatureScript defineFeatures
graham_lock
Member Posts: 148 PRO
Hi,
Can somebody explain what the third code block is used for in defineFetaure please?
An example from the draft feature:
Can somebody explain what the third code block is used for in defineFetaure please?
An example from the draft feature:
export const draft = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
// inputs
// inputs
}
{
// main code block
// main code block
},
{
// what is this block used for?
// what is this block used for?
});
Thank you.
Thank you.
0
Best Answer
-
jnewth_onshape Member, Onshape Employees Posts: 89Yes, and I can explain to you also how to find out more!
Short version:
They are defaults that are provided to the draft function call when draft is called from FeatureScript. When draft is invoked via ui, the ui sets all these defaults and (afaik - could be wrong about that) these defaults are not used.
Now the long version:
First, look in the std library at feature.fs for the definition for defineFeature. Navigating to this is easy to do: On a mac I hold down the command-key then hover over the defineFeature keyword and it underlines and then it takes me where I want to go. The signature for this overload of the function is:export function defineFeature(feature is function, defaults is map) returns function<br>
So if we highlight everything before the "," we see that first two "blocks" are the precondition and the function body, respectively. Taken together, those two blocks are the "feature" passed in to defineFeature. The "third block" is the "defaults" map. If you look at the defineFeature code, it takes the defaults map and merges the definition map and the defaults map together before invoking the function.
Stepping back out to the top-level: What does this mean for the draft function in your example? It means that the keyword "draft" in our system is a function that takes three parameters (a context, an id, and a definition) but _also_ has this defaults map that will be merged in to the definition. This means when you, a FeatureScript programmer, decide to invoke draft as a function in your custom FeatureScript (and not via the UI like a regular CAD user) then anything not specified in the definition map you provide to your function call will get a default value: the values defined in that "third block".
Please add your questions to this thread as well: https://forum.onshape.com/discussion/23283/hey-featurescripters-what-would-you-like-to-see-in-an-advanced-featurescript-tutorial#latest
I'm planning on writing some more advanced tutorials when I get a chance and this is a good question not well covered in our current documentation.2
Answers
Short version:
They are defaults that are provided to the draft function call when draft is called from FeatureScript. When draft is invoked via ui, the ui sets all these defaults and (afaik - could be wrong about that) these defaults are not used.
Now the long version:
First, look in the std library at feature.fs for the definition for defineFeature. Navigating to this is easy to do: On a mac I hold down the command-key then hover over the defineFeature keyword and it underlines and then it takes me where I want to go. The signature for this overload of the function is:
So if we highlight everything before the "," we see that first two "blocks" are the precondition and the function body, respectively. Taken together, those two blocks are the "feature" passed in to defineFeature. The "third block" is the "defaults" map. If you look at the defineFeature code, it takes the defaults map and merges the definition map and the defaults map together before invoking the function.
Stepping back out to the top-level: What does this mean for the draft function in your example? It means that the keyword "draft" in our system is a function that takes three parameters (a context, an id, and a definition) but _also_ has this defaults map that will be merged in to the definition. This means when you, a FeatureScript programmer, decide to invoke draft as a function in your custom FeatureScript (and not via the UI like a regular CAD user) then anything not specified in the definition map you provide to your function call will get a default value: the values defined in that "third block".
Please add your questions to this thread as well: https://forum.onshape.com/discussion/23283/hey-featurescripters-what-would-you-like-to-see-in-an-advanced-featurescript-tutorial#latest
I'm planning on writing some more advanced tutorials when I get a chance and this is a good question not well covered in our current documentation.