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.

setFeatureComputedParameter() Generates error, but the broken code is the only code executing...

john_mcclaryjohn_mcclary Member, Developers Posts: 3,890 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....


FeatureScript 1053;<div>import(path : "onshape/std/geometry.fs", version : "1053.0");</div><div><br></div><div>export enum SM</div><div>{</div><div>&nbsp; &nbsp; annotation {"Name" : "1/4\""}</div><div>&nbsp; &nbsp; _25,</div><div>&nbsp; &nbsp; annotation {"Name" : "7 Guage&nbsp; (.1793)"}</div><div>&nbsp; &nbsp; _7,</div><div>&nbsp; &nbsp; annotation {"Name" : "10 Guage (.1345)"}</div><div>&nbsp; &nbsp; _10,</div><div>&nbsp; &nbsp; annotation {"Name" : "11 Guage (.1196)"}</div><div>&nbsp; &nbsp; _11,</div><div>&nbsp; &nbsp; annotation {"Name" : "12 Guage (.1046)"}</div><div>&nbsp; &nbsp; _12,</div><div>&nbsp; &nbsp; annotation {"Name" : "14 Guage (.0747)"}</div><div>&nbsp; &nbsp; _14</div><div>}</div><div><br></div><div>export const SM_Table =</div><div>{&nbsp;</div><div>&nbsp; &nbsp; "_25" : { Size : .25, Bend : .262 }, // .25"</div><div>&nbsp; &nbsp; "_7" : { Size : .1793, Bend : .262 }, // 7 gauge</div><div>&nbsp; &nbsp; "_10" : { Size : .1345, Bend : .164 }, // 10 gauge</div><div>&nbsp; &nbsp; "_11" : { Size : .1196, Bend : .164 }, // 11 gauge</div><div>&nbsp; &nbsp; "_12" : { Size : .1046, Bend : .164 }, // 12 gauge</div><div>&nbsp; &nbsp; "_14" : { Size : .0747, Bend : .079 }&nbsp; // 14 gauge</div><div>};</div><div><br></div><div>&nbsp; &nbsp;&nbsp;</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>&nbsp; &nbsp; precondition</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; annotation { "Name" : "Size" }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; definition.SMSize is SM;</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; const Size = toString(definition.SMSize);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; setVariable(context, "thickness", SM_Table[Size].Size);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; setVariable(context, "bendRadius", SM_Table[Size].Bend);</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; setFeatureComputedParameter(context, id, {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name" : "SMSize",</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : definition.SMSize.Name&nbsp; &nbsp;// ERROR HERE ?</div><div>&nbsp; &nbsp; &nbsp; &nbsp; });</div>&nbsp; &nbsp; });

Best Answers

Answers

  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,890 PRO
    by the way, what is the best way to post a codeblock ?  <code> </code> apparently isn't good enough
  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,890 PRO
    I was hoping to  not have to enter the name string in yet another place.

    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 ...  :s
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    edited July 2019
    In general, any error you throw in FS will abort the entire feature, as if nothing ever ran. This is to avoid leaving the model in a partially-built state. Any time the feature is red, it does nothing to change the model, and can be safely deleted without changing anything or risking more breakages downstream.

    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.
  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,890 PRO
    edited July 2019
    I just mashed a "Name" into the bend table, I knew that would work, but I was looking for a method that didn't involve duplication of code.

    FeatureScript 1053;
    import(path : "onshape/std/geometry.fs", version : "1053.0");
    
    export enum SM
    {
        annotation {"Name" : "1/4\" (.25)"}
        _25,
        annotation {"Name" : "7 Gauge  (.1793)"}
        _7,
        annotation {"Name" : "10 Gauge (.1345)"}
        _10,
        annotation {"Name" : "11 Gauge (.1196)"}
        _11,
        annotation {"Name" : "12 Gauge (.1046)"}
        _12,
        annotation {"Name" : "14 Gauge (.0747)"}
        _14
    }
    
    export const SM_Table =
    { 
        "_25" : { Name : "1/4\" (.25)", Size : .25, Bend : .262 }, // .25"
        "_7" : { Name : "7 Gauge  (.1793)", Size : .1793, Bend : .262 }, // 7 gauge
        "_10" : { Name : "10 Gauge (.1345)", Size : .1345, Bend : .164 }, // 10 gauge
        "_11" : { Name : "11 Gauge (.1196)", Size : .1196, Bend : .164 }, // 11 gauge
        "_12" : { Name : "12 Gauge (.1046)", Size : .1046, Bend : .164 }, // 12 gauge
        "_14" : { Name : "14 Gauge (.0747)", Size : .0747, Bend : .079 }  // 14 gauge
    };
    
        
    annotation { "Feature Type Name" : "Sheet Metal Selection", "Feature Name Template" : "#SMSize" }
    export const SM_Select = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Size" }
            definition.SMSize is SM;
        }
        {
            const Size = toString(definition.SMSize);
            setVariable(context, "thickness", SM_Table[Size].Size);
            setVariable(context, "bendRadius", SM_Table[Size].Bend);
            
            setFeatureComputedParameter(context, id, {
                    "name" : "SMSize",
                    "value" : SM_Table[Size].Name
            });
        });

  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    Yep, makes sense. File the improvement request for use of enum names in templates!
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,014 PRO
    edited November 2021
    notes for codeblock formatting.....

    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.


Sign In or Register to comment.