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.
conditional logic based on "EntityType"
EvanReese
Member, Mentor Posts: 2,135 ✭✭✭✭✭
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
0
Best Answers
-
konstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭There are no way to find entity type or geometry type until you try to evaluate it (which is not good i think), so:try silent{var vertex = evVertex(selection)//vertex handling code}try silent{var somethingElse = evSomethingElse(selection)//some other specific code}5
-
ilya_baran Onshape Employees, Developers, HDM Posts: 1,212There 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.Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc5
Answers
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?
in my case there are only 2 choices (vertex or mate connector) so I'll have a look at that. Thanks.
If I want to check if anything in myQuery resolves to a vertex, I would do:
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.