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.

Switch statement

Member, Developers Posts: 3,943 PRO
Is there an equivalent way of doing switch statements in FS?

instead of a bunch of if-else statements

Best Answers

  • Onshape Employees, Developers, HDM Posts: 1,230
    Answer ✓
    There are indeed cases when a switch statement makes things simpler, but in this case, you could get a similar effect by doing:
    const table = {<br>&nbsp;...<br>&nbsp; &nbsp;3/4 : { KeySeat : .644, KeyWay : .837},<br>&nbsp;13/16 : { KeySeat : .708, KeyWay : .900},<br>&nbsp;...<br>};<br>var entry = table[ShaftDia];<br>if (entry == undefined)<br>&nbsp; &nbsp;throw "No match for shaft diameter: " ~ ShaftDia;<br>var KeySeat = entry.KeySeat;<br>var KeyWay&nbsp;= entry.KeyWay;<br>

    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc

Answers

  • Moderator, Onshape Employees, Developers Posts: 1,646
    @john_mcclary @konstantin_shiriazdanov

    @ilya_baran informs me that there is a switch-expression in FS that we may document for public use in the future.  It is slightly different from a switch-statement in that it does not switch on an expression and then execute a block of code (a statement), but rather switches on an expression and then return another specified expression.  This can be made to act like a traditional switch statement using lambdas if desired.

    Here is an example:
    https://cad.onshape.com/documents/f6780441494046f4439a8219/w/71e7c5c95c521d8716fa75fa/e/631535138fdaba3ef7928ea2
    Jake Rosenfeld - Modeling Team
  • Member Posts: 1,221 ✭✭✭✭✭
    @Jake_Rosenfeld so this switch is a function, I beleve Ilya wrote it half an hour ago)
  • Member, Developers Posts: 3,943 PRO
    edited August 2018
    @Jake_Rosenfeld well I suppose that is similar to a switch statement, but it doesn't allow flow through multiple cases, and it still requires many lines of code.

    y = 3;<br>switch(x)<br>{<br>case 0: y++;<br>case 1: y++;<br>case 2: y++; break;<br>case 3: y = 10; break;<br>default: y = 0;<br>}<br><br>X = 0 -> Y = 6;
    X = 1 -> Y = 5;
    X = 2 -> Y = 4;
    X = 3 -> Y = 10;
    X = 4 -> Y = 0;

    Not that I end up using that method very often, but it is a traditional syntax for switch which I have taken advantage of before.

    What brought this on was I had a long list of if-else statements to fill in all the keyway sizes given a shaft diameter.
    it ended up being 500 lines of code that is ugly to read.
    if they were switch statements it would only be around 100 or so lines of code and resemble a readable table.



    Vrs:

    case 7/8: KeySeat = .771; KeyWay = .900; break;
    case 15/16: KeySeat = .796; KeyWay = 1.051; break;
    case 1: KeySeat = .859; KeyWay = 1.114; break;
  • Onshape Employees, Developers, HDM Posts: 1,230
    Answer ✓
    There are indeed cases when a switch statement makes things simpler, but in this case, you could get a similar effect by doing:
    const table = {<br>&nbsp;...<br>&nbsp; &nbsp;3/4 : { KeySeat : .644, KeyWay : .837},<br>&nbsp;13/16 : { KeySeat : .708, KeyWay : .900},<br>&nbsp;...<br>};<br>var entry = table[ShaftDia];<br>if (entry == undefined)<br>&nbsp; &nbsp;throw "No match for shaft diameter: " ~ ShaftDia;<br>var KeySeat = entry.KeySeat;<br>var KeyWay&nbsp;= entry.KeyWay;<br>

    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Member, Developers Posts: 3,943 PRO
    Thanks @ilya_baran That's what I'm looking for for this use case
  • Member, Developers Posts: 3,943 PRO
    Went from 465 lines of code down to 189 with that

    I'll know better next time :)
  • Member, OS Professional, Developers Posts: 2,055 ✭✭✭✭✭
    @Jake_Rosenfeld
    Can you specify a default with that switch statement?
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • Member, Developers Posts: 3,943 PRO
    think that is what the if(entry == undefined) is doing
  • Member, csevp Posts: 111 PRO

    Switch no longer seems to work unfortunitally, look forward to seeing it in the future tho

  • Member, OS Professional, Developers Posts: 2,055 ✭✭✭✭✭

    @Chris_D_Mentes_001

    What do you mean? Onshape uses it many times in their own code and we can create code just like it:

    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • Member, csevp Posts: 111 PRO

    Ah okay thanks for this I've been using it wrong.

Sign In or Register to comment.