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.

Options

Best practices for making non-trivial sketches in a script.

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?

Comments

  • Options
    NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,416
    Everything you need to know about sketches is here: https://cad.onshape.com/FsDoc/library.html#module-sketch.fs

    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.
    Senior Director, Technical Services, EMEAI
  • Options
    _anton_anton Member, Onshape Employees Posts: 279
    (The Lindenmayer system sounds pretty fun to implement, but it's not necessary.)

    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
  • Options
    vladimir_shapranovvladimir_shapranov Member Posts: 7
    NeilCooke said:
    Everything you need to know about sketches is here: https://cad.onshape.com/FsDoc/library.html#module-sketch.fs

    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.
    Indeed, for this case `skPolyline` should do the trick here, didn't notice it, thanks. Although it works if the sketch contains only straight lines.

    > 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.

    I'm *starting* with this simple stuff to see if it is a good idea to commit to this technology. Checking if things I'm used to see as trivial are going to "make life harder". Ultimately, choosing between the awesome UI and visualization of Onshape and more conventional extension development available in some other tools.

    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.


  • Options
    Konst_ShKonst_Sh Member Posts: 42 PRO
    edited February 27
    I would create the profile manually in the template part studio, configure it with a number of parameters that you need to access from feature code, and use instantiator() function, which derives the profile to the given coordinate system.
  • Options
    vladimir_shapranovvladimir_shapranov Member Posts: 7
    Konst_Sh said:
    I would create the profile manually in the template part studio, configure it with a number of parameters that you need to access from feature code, and use instantiator() function, which derives the profile to the given coordinate system.

    This looks like the real answer, thanks!

    So, do I understand correctly, that I have to:
     - create (in this case) a sketch in a separate part studio
     - make the variables I need in that part studio (and use them in the sketch accordingly)
     - go to "configuration panel" of the part studio, add all those variables to configuration
     - use variable names as keys in the map passed as the "configuration" parameter of `addInstance`

    bonus question: does this (undocumented in both cases) "configuration" parameter work the same way for the "/parts/d/{did}/{wvm}/{wvmid}/e/{eid}/partid/{partid}/stl" api call?

  • Options
    Konst_ShKonst_Sh Member Posts: 42 PRO
    edited February 29
    You will need to add part studio configuration variables first, and then use them in the features that you want to parametrize, be it sketches or 3d features.
    For using with api you need to pass something called configurationString as query parameter for the endpoint that you linked, I suppose that to get this configurationString you will need to tackle another endpoint: https://cad.onshape.com/glassworks/explorer/#/Element/encodeConfigurationMap
    that will encode it from from the collection of configuration parameters. To find which format of those configuration parameters it really expecting in case when you have there values with units, enums, booleans you can get this endpoint: https://cad.onshape.com/glassworks/explorer/#/Element/getConfiguration , and decode the content it using this endpoint https://cad.onshape.com/glassworks/explorer/#/Element/decodeConfiguration
    It might not be exactly accurate, but it should work something like that
Sign In or Register to comment.