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.
Best practices for making non-trivial sketches in a script.
vladimir_shapranov
Member Posts: 7 ✭
Hello,
I'm trying out writing scripts for Onshape and I can't figure out how to make a sketch that is even slightly more complex than in the example, all while keeping the code nice, readable and maintainable.
For some context, here is the sketch I'm trying to implement. Most of the dimensions are to be computed dynamically by the script.
Naturally, the first thought I had is to draw the sketch in the UI and copypaste it to the script. It does run, and later one can replace the hardcoded dimensions with the actual values, remove all the extras (like precise label positions, etc), etc. However maintaining the code later seems impractical: it is still pretty redundant, uses some undocumented APIs (e.g. what does "endSnap0" mean and where can I read about it?), and relies on some autogenerated segment IDs to define the constraints.
Next idea is to make a helper library with some simpler API for specific usecases (e.g. "turtle interpreter"-like, "left 5mm, up 10mm", etc). Making it myself with the constraints imposed by Onshape's DSL (can't run it locally, can't make unittests, have to use the editor in the browser, no version control, etc.) is way too much effort. Maybe someone already made it?
And the final one is to have something that allows defining the sketch in the general purpose language of choice and then generates the code in Onshape's DSL. Again, quite a feat (although should be easier than the previous idea).
What am I missing? How do you make sketches in a script?
0
Comments
You should not copy/paste code from the "show code" panel, all you need for that sketch is a single call to skPolyline and there is no need to use contraints.
This is simple stuff and calling Onshape's IDE "too much effort" is a bit of stretch. You're making your life harder for no reason or benefit.
We do have built-in version control. You can write unit tests, it just takes a couple of steps (make a feature that uses your code, put an instance of it in a Part Studio, monitor the Part Studio to see the output live). Here's an example: https://cad.onshape.com/documents/ba693d54861383c93d7bc497/w/d9c286b3d3e847bbc43e846a/e/c3a477d552fbc10eb0a4c5ec
I didn't call the "IDE" too much effort, let's not misinterpret. And don't get me wrong, it's an impressive text editor.
What I said is that writing certain things for Onshape (which, of course, necessitates the use of the IDE) is too much effort compared to doing same things in a general purpose language using the tools available for it.
- create (in this case) a sketch in a separate part studio