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: 1,999 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;import(path : "onshape/std/geometry.fs", version : "1053.0");
export enum SM{    annotation {"Name" : "1/4\""}    _25,    annotation {"Name" : "7 Guage  (.1793)"}    _7,    annotation {"Name" : "10 Guage (.1345)"}    _10,    annotation {"Name" : "11 Guage (.1196)"}    _11,    annotation {"Name" : "12 Guage (.1046)"}    _12,    annotation {"Name" : "14 Guage (.0747)"}    _14}
export const SM_Table ={     "_25" : { Size : .25, Bend : .262 }, // .25"    "_7" : { Size : .1793, Bend : .262 }, // 7 gauge    "_10" : { Size : .1345, Bend : .164 }, // 10 gauge    "_11" : { Size : .1196, Bend : .164 }, // 11 gauge    "_12" : { Size : .1046, Bend : .164 }, // 12 gauge    "_14" : { 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" : definition.SMSize.Name   // ERROR HERE ?        });    });

Best Answers

Answers

  • john_mcclaryjohn_mcclary Member, Developers Posts: 1,999 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: 1,999 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: 449
    edited July 23
    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: 1,999 PRO
    edited July 23
    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: 449
    Yep, makes sense. File the improvement request for use of enum names in templates!
Sign In or Register to comment.