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.
stop a feature on a condition
EvanReese
Member, Mentor Posts: 2,135 ✭✭✭✭✭
What's the best way to stop a feature from processing and add a warning report? I assumed erroneously that just adding reportFeatureWarning would prevent the rest of the feature from processing and just display the warning, but it seems to fail because of downstream things instead. I could put everything after in a giant if/then, but something tells me that's not how the pros do it. Suggestions?
If it's relevant, the failure mode is an array index out of bounds.
If it's relevant, the failure mode is an array index out of bounds.
Evan Reese
0
Best Answers
-
MBartlett21 Member, OS Professional, Developers Posts: 2,050 ✭✭✭✭✭@Evan_Reese
You can wrap the top-level function in a try-catch block and then use `reportFeatureWarning` on the error at the end.
To do the error, you can the use `throw "This is the error that Jack threw"`
EDIT: Note: if the error propagates all the way past your function, and is not a regenError the user only gets a form error: Error regenerating.0 -
Alex_Kempen Member Posts: 248 EDUInformation on error handling can be found in the error handling section of the FS documentation. The short of it is that throw regenError is used to hit the brakes, since it stops your feature from regenerating, resulting in your feature producing nothing. You can also attach custom error messages to regenErrors and tag incorrect parameters and queries so they highlight in red, which is useful for accurately reporting what went wrong.
reportFeatureWarning is less severe than regenError, causing your feature to turn red but still regenerate (i.e. create and/or modify your part studio). An example usage is in my belt FeatureScript, where I use reportFeatureWarning to warn the user if the user's selected belt run doesn't match the distance between their selections. In this case, I want the user to know something is wrong, but the belt FS should also still create the belt and pulleys, so reportFeatureWarning works fine for that situation.
Try and catch statements can be used with all types of warnings to prevent unhelpful errors from appearing; here's a common usage pattern:try { opExtrude(context, id, extrudeDef); } catch { throw regenError("Error message goes here."); // can optionally append parameters and queries to tag in red }
Note that you could also avoid the use of the try/catch statement by preprocessing the extrudeDefinition using if statements, which could allow for even more accurate error warnings, depending on what you're doing.if (evaluateQuery(context, extrudeDef.entities) == []) throw regenError("Select entities to extrude.", ["entities"]); // where definition.entities is a parameter<br>
You can also use the verifyNonemptyQuery method to get the same effect.
Lastly, reportFeatureInfo can be used to simply display a message popup, without affecting the model state in any way. This is great for things like success messages.
Hopefully, that makes some sense and helps answer your questions.CS Student at UT DallasAlex.Kempen@utdallas.eduCheck out my FeatureScripts here:0
Answers
You can wrap the top-level function in a try-catch block and then use `reportFeatureWarning` on the error at the end.
To do the error, you can the use `throw "This is the error that Jack threw"`
EDIT: Note: if the error propagates all the way past your function, and is not a regenError the user only gets a form error: Error regenerating.
IR for AS/NZS 1100
reportFeatureWarning is less severe than regenError, causing your feature to turn red but still regenerate (i.e. create and/or modify your part studio). An example usage is in my belt FeatureScript, where I use reportFeatureWarning to warn the user if the user's selected belt run doesn't match the distance between their selections. In this case, I want the user to know something is wrong, but the belt FS should also still create the belt and pulleys, so reportFeatureWarning works fine for that situation.
Try and catch statements can be used with all types of warnings to prevent unhelpful errors from appearing; here's a common usage pattern: Note that you could also avoid the use of the try/catch statement by preprocessing the extrudeDefinition using if statements, which could allow for even more accurate error warnings, depending on what you're doing.
You can also use the verifyNonemptyQuery method to get the same effect.
Lastly, reportFeatureInfo can be used to simply display a message popup, without affecting the model state in any way. This is great for things like success messages.
Hopefully, that makes some sense and helps answer your questions.