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.

Standalone Functions

SauerSauer Member Posts: 7
Hello,

Is it possible to write a standalone function that does not create new features but only returns a value to be used for other inputs in the UI? I'm creating a number of containers that will be 3D printed and want the thin walls to be an ideal thickness for printing. I know the formula used by my slicer to calculate recommended wall thicknesses and want to write a function that will take the layer height, extrusion width, and desired number of perimeters and return the required wall thickness for use in sketches and Shells.

Plugging the following formula into dimension fields in my sketches and other operations (with the placeholders filled with proper values) works just fine, but I'm copying this same formula all over the place and would rather have one function to maintain rather than dozens of separate dimensions fields scattered throughout my model.

number_of_perimeters * extrusion_width - layer_height * (1 - PI/4) * (number_of_perimeters - 1)

Best Answer

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 4,228
    Answer ✓
    Hi, just create a variable with that formula in it and use the result of that in your dimensions.
    Director, Technical Services, EMEAI

Answers

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 4,228
    Answer ✓
    Hi, just create a variable with that formula in it and use the result of that in your dimensions.
    Director, Technical Services, EMEAI
  • SauerSauer Member Posts: 7
    That makes perfect sense. Thank you very much.
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 1,779 PRO
    @Sauer

    The command line evaluation is powerful like @NeilCooke is suggesting, but, one step further, put that single line into a featurescript and it's easier to maintain. You can have a ternary operator on the command line, but why?

    Featurescript is so easy to learn and that long expression will be easier to handle using the OS editing space vs. scrolling back & forth along the command line. 

    One of the things that is really great about OS and is overlooked is the featurescript CLI, it's really good. After you get the expression working on the command line don't stop there and take it into the featurescript environment, you be glad you did.


  • christian_pettychristian_petty Member Posts: 39 PRO
    billy2 said:

    One of the things that is really great about OS and is overlooked is the featurescript CLI, it's really good. After you get the expression working on the command line don't stop there and take it into the featurescript environment, you be glad you did.
    Where can I access a CLI?
  • SauerSauer Member Posts: 7
    That was originally my intent as I planned to use this formula across many documents and storing it in one placer that is globally accessible would be ideal versus copying it into every document's variable table. I went through Feature Script's documentation and could not figure out a way to create a function that can accept a number of parameters and output directly into a dimension field.
  • SauerSauer Member Posts: 7
    It looks like I have managed to accomplished what I wanted. A closer look at the documentation for the part studio variable tables shows that functions can be embedded in them by using the "Any" type; effectively allowing standalone functions.

    A bit more searching turned up the following post by ilya_baran was extremely helpful explaining how to move the function into a feature script I can now use across my projects.

    A small example document showing it in action.

    Thank you for your assistance folks.
Sign In or Register to comment.