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.

Tips for writing reusable code

Evan_ReeseEvan_Reese Member Posts: 500 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 Fractal
Website: fractalmade.com
Instagram: @evan.reese.designs

Answers

  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 1,887 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
    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 - I make FeatureScripts: View FeatureScripts
  • Evan_ReeseEvan_Reese Member Posts: 500 PRO
    @MBartlett21 thanks, I'll look at this tonight if I have time and try some tests!

    Evan Reese / Principal and Industrial Designer with Fractal
    Website: fractalmade.com
    Instagram: @evan.reese.designs
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 1,467 PRO
    edited July 15
    @MBartlett21 can you please give us access, I too would like to see how you're breaking up your programs.



    thanks
  • Evan_ReeseEvan_Reese Member Posts: 500 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 Fractal
    Website: fractalmade.com
    Instagram: @evan.reese.designs
  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 1,887 PRO
    @Evan_Reese
    @billy2

    Done. It is public now.
    MB - I make FeatureScripts: View FeatureScripts
Sign In or Register to comment.