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

Tips for writing reusable code

Evan_ReeseEvan_Reese Member Posts: 2,064 PRO
I'm new to code of any kind, so perhaps this is obvious, but is there a clean way to reuse feature scripts and update them all from a central place? I'd love to be able to write an "edge selection" module that can just be imported into new custom features to create parts of the UI and selection logic. I'd like to have the option to update all features using the module from a central place. I see things in the standard Onshape library that seem to do this but I don't fully get how, and what the limitations are. 
Evan Reese / Principal and Industrial Designer with Ovyl
Website: ovyl.io

Answers

  • Options
    MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,034 EDU
    I'm new to code of any kind, so perhaps this is obvious, but is there a clean way to reuse feature scripts and update them all from a central place? I'd love to be able to write an "edge selection" module that can just be imported into new custom features to create parts of the UI and selection logic. I'd like to have the option to update all features using the module from a central place. I see things in the standard Onshape library that seem to do this but I don't fully get how, and what the limitations are. 
    @Evan_Reese
    How the standard library works is that it is all in one workspace.

    I ran into this problem a few times.
    I had created a simple library to make creating a Cutout FeatureScript easier, by providing predicates for the definition and a simple library of functions (https://cad.onshape.com/documents/2ad339efe44128f5171291fa)
    I also had created a library to evaluate expressions, by tokenising them, then converting them to RPN notation, then finally evaluating it, returning an error code if a variable wasn't found. (https://cad.onshape.com/documents/429fd7dda5f0fdb93833a34a)

    I find that the easiest way is to just have a simple document with the main code and test cases. You can then create versions of this and import it into other documents. The disadvantage of this approach is that it isn't easy to see what documents use that library.
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • Options
    Evan_ReeseEvan_Reese Member Posts: 2,064 PRO
    @MBartlett21 thanks, I'll look at this tonight if I have time and try some tests!

    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • Options
    billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,014 PRO
    edited July 2020
    @MBartlett21 can you please give us access, I too would like to see how you're breaking up your programs.



    thanks
  • Options
    Evan_ReeseEvan_Reese Member Posts: 2,064 PRO
    @MBartlett21
    I'm getting the same error on the first link (https://cad.onshape.com/documents/2ad339efe44128f5171291fa)
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • Options
    MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,034 EDU
    @Evan_Reese
    @billy2

    Done. It is public now.
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
Sign In or Register to comment.