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.

conditional logic based on "EntityType"

Evan_ReeseEvan_Reese Member Posts: 2,060 PRO
I'm basically trying to get a feature that says
if (my selection is a vertex)<br>{handle it one way}<br>else<br>{handle it another way}
I tried
if (definition.point == EntityType.VERTEX)
but it didn't like that. I'm new to this and I sense it's some mismatch of data types. How can I ask what EntityType a certain entity is? do I need to evaluate my query or something similar?
Evan Reese / Principal and Industrial Designer with Ovyl
Website: ovyl.io

Best Answers

Answers

  • Evan_ReeseEvan_Reese Member Posts: 2,060 PRO
    @konstantin_shiriazdanov
      
    that worked! thanks. Didn't know about the try function. Let me make sure I understand it. It just tries something, and if there's an error, then it just ignores the code between the curly braces?

    I searched the Standard Library Documentation and there is no use of the word "silent" there anywhere. Does that just make the try function not throw an error?
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,307
    You can also "catch" the try block which is especially useful if there are only 2 choices.
    Senior Director, Technical Services, EMEAI
  • Evan_ReeseEvan_Reese Member Posts: 2,060 PRO
    @NeilCooke
    in my case there are only 2 choices (vertex or mate connector) so I'll have a look at that. Thanks.
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    this operator is described in exception handling article: https://cad.onshape.com/FsDoc/exceptions.html
    it exactly what you said - allows you to handle errors if you expect them during some code execution, in this case you should use such ev functions that the one and only correct evaluation should happen.
  • Evan_ReeseEvan_Reese Member Posts: 2,060 PRO
    There are a few cases (like evAxis) when you currently need exception handling for flow control, but that's generally not the best way to do it.  try silent is especially dangerous, as you may miss real errors.

    If I want to check if anything in myQuery resolves to a vertex, I would do:

    if (evaluateQuery(context, qEntityFilter(myQuery, EntityType.VERTEX)) != [])
    {
       ...
    }
    If I want to check the first thing my query resolves to (what evVertex would use), I'd put a qNthElement around myQuery also.

    It's not too pretty, so if I found myself doing this frequently, I'd factor it out into a predicate.
    ooh! I like this way too. I'll add it to my toolkit. Thanks!
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
Sign In or Register to comment.