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.
setFeatureComputedParameter() Generates error, but the broken code is the only code executing...
john_mcclary
Member, Developers Posts: 3,936 PRO
https://cad.onshape.com/documents/ad3a9824a02b9066da33b369/w/bb566cc2750ad4ccc29f90ce/e/4ed605af55a5d19fd46cfaec
So I am trying to create a selectable Sheet metal table that creates a variable that can be pushed to the sheet-metal base feature.
Everything works, but I wanted to go step further and have the selected size set to the feature name in long form (same name that shows in the enum)
It actually works how I want as you can see:
But it breaks the rest of the code...
If I comment out setFeatureComputerParameter() then it works except the name of the feature is "_7"... which doesn't do anyone any favors.
I really am trying to use the value I already have stored in the enum, rather than doing a for loop and checking which one was selected, and manually outputting a string that says "7 Gauge (.1793)".
Is the syntax for reading the enum's "Name" parameter incorrect?
I tried it definition.SMSize["Name"] but it does the exact same thing.
One would think it would be correct, seeings how it is returning the correct string and setting it to the feature name...
After Commenting out setFeature....
So I am trying to create a selectable Sheet metal table that creates a variable that can be pushed to the sheet-metal base feature.
Everything works, but I wanted to go step further and have the selected size set to the feature name in long form (same name that shows in the enum)
It actually works how I want as you can see:
But it breaks the rest of the code...
If I comment out setFeatureComputerParameter() then it works except the name of the feature is "_7"... which doesn't do anyone any favors.
I really am trying to use the value I already have stored in the enum, rather than doing a for loop and checking which one was selected, and manually outputting a string that says "7 Gauge (.1793)".
Is the syntax for reading the enum's "Name" parameter incorrect?
I tried it definition.SMSize["Name"] but it does the exact same thing.
One would think it would be correct, seeings how it is returning the correct string and setting it to the feature name...
After Commenting out setFeature....
FeatureScript 1053;<div>import(path : "onshape/std/geometry.fs", version : "1053.0");</div><div><br></div><div>export enum SM</div><div>{</div><div> annotation {"Name" : "1/4\""}</div><div> _25,</div><div> annotation {"Name" : "7 Guage (.1793)"}</div><div> _7,</div><div> annotation {"Name" : "10 Guage (.1345)"}</div><div> _10,</div><div> annotation {"Name" : "11 Guage (.1196)"}</div><div> _11,</div><div> annotation {"Name" : "12 Guage (.1046)"}</div><div> _12,</div><div> annotation {"Name" : "14 Guage (.0747)"}</div><div> _14</div><div>}</div><div><br></div><div>export const SM_Table =</div><div>{ </div><div> "_25" : { Size : .25, Bend : .262 }, // .25"</div><div> "_7" : { Size : .1793, Bend : .262 }, // 7 gauge</div><div> "_10" : { Size : .1345, Bend : .164 }, // 10 gauge</div><div> "_11" : { Size : .1196, Bend : .164 }, // 11 gauge</div><div> "_12" : { Size : .1046, Bend : .164 }, // 12 gauge</div><div> "_14" : { Size : .0747, Bend : .079 } // 14 gauge</div><div>};</div><div><br></div><div> </div><div>annotation { "Feature Type Name" : "Sheet Metal Selection", "Feature Name Template" : "#SMSize" }</div><div>export const SM_Select = defineFeature(function(context is Context, id is Id, definition is map)</div><div> precondition</div><div> {</div><div> annotation { "Name" : "Size" }</div><div> definition.SMSize is SM;</div><div> }</div><div> {</div><div> const Size = toString(definition.SMSize);</div><div> setVariable(context, "thickness", SM_Table[Size].Size);</div><div> setVariable(context, "bendRadius", SM_Table[Size].Bend);</div><div> </div><div> setFeatureComputedParameter(context, id, {</div><div> "name" : "SMSize",</div><div> "value" : definition.SMSize.Name // ERROR HERE ?</div><div> });</div> });
0
Best Answers
-
kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565There's no "Name" field on an enum. Those annotation are not stored on the enum value, since the enum value is just a string. In fact, annotations never effect runtime execution of FeatureScript.
If you want to encode those names, you'll need to make a separate table of names which is usable at runtime. Something like:... "name" : { SM._25 : "1/4\"" SM._7 : "7 Guage (.1793)" ... }[definition.SMSize]
(For codeblocks, I just click the paragraph button > "Code", then paste into the yellow block)5 -
kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565In your shoes, I'd also file an improvement request to simply display enum values as their annotated names without setFeatureComputedParameter! This would mean less code and also would get rid of the quotation marks in the final name (which I don't know of a workaround to avoid)6
-
john_mcclary Member, Developers Posts: 3,936 PRO
Answers
If you want to encode those names, you'll need to make a separate table of names which is usable at runtime. Something like:
(For codeblocks, I just click the paragraph button > "Code", then paste into the yellow block)
and for "not having" a name field, oh boy does it still read it correctly
but why must it break the entire script for the part that is obviously working ?!
Thanks for the tip on the code format, never noticed the paragraph button ...
If expect an error to be thrown, you can handle that error in a try-catch block, to do something sane in that error case (see https://cad.onshape.com/FsDoc/exceptions.html), possibly skipping that step, trying another method, etc. If you catch all errors the feature will not turn red and feature changes will be kept. Otherwise, Onshape assumes the error is unexpected and aborts the entire feature to be safe.
IR Made
I found that copy&paste out of vscode doesn't format properly using the css code style.
To get around the issue I:
1. cut & paste the code into a web editor https://www.editpad.org/
2. then download it locally
3. copy the clean code into the forum notes using the css styling "code"
I guess the moral to this story is, stop using microsoft.
This isn't the correct place to post this comment, I searched "codeblock" and this post was the #1 returned. This post has nothing to do with codeblock formatting.
FYI code copy&paste from OS featurescript editor works fine in this forum using the css style.