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.

Rounding Calculated Variables to Pattern number of Instances

3D3D OS Professional Posts: 52 ✭✭
Can anyone offer any tips for how to round off or convert calculated variables to integers so that the calculated variable works for patterns?
(number of instances patterned?)

Surely this should be fairly simple in FeatureScript but not sure how to approach it.

Comments

  • lemon1324lemon1324 Member, Developers Posts: 225 EDU
    If it's in FS, this is pretty easy; check the ceil(), floor(), and round() functions in the math module documentation. As per standard definitions of these functions, round() is to nearest integer, floor() rounds down, and ceil() rounds up.  Typically, if you're calculating, you'll probably want floor() or ceil() depending on whether you calculated a minimum or maximum value that you need to round.

    I use these in my Laser Joint feature (lines 565-586) for adaptively setting the number of pins in the joints, if you want to look at some code.
    Arul Suresh
    PhD, Mechanical Engineering, Stanford University
  • 3D3D OS Professional Posts: 52 ✭✭
    I do not really understand the debugging tips. Can anyone interpret please? or direct to appropriate help?
    https://cad.onshape.com/documents/fe787db77801eacc744b9da0/w/0317627ef816cb97646636d5/e/98d1243380284b60b57a0710

    OS FS probably gulping @ green coder...
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,671
    You don't need FeatureScript, simply put the expression in the instance count field in the pattern dialog:

    e.g. Variable #count = 3.45

    Instance count: ceil(#count) 

    Would give you 4 instances.
    Senior Director, Technical Services, EMEAI
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,210
    One thing to beware of is if your variable has length units, floor and ceil won't work (because it is not clear what length to round to).  You have to do meter * floor(#myLength / meter) for example if you want to round down to the nearest meter.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • 3D3D OS Professional Posts: 52 ✭✭
    edited July 2016
    Thank you @NeilCooke  - you are correct - FS is not necessary - you can just do the following as @lemon1324 suggested in the (x) variable 

    If the calculated variable is z = 3.142857142857143, reference another variable: 

    floor(#z) ; value = 3
    ceil(#z) ; value = 4
    round(#z) ; value = 3 rounds to nearest integer as in example above

    and thanks @ilya_baran - while not necessary in this case - is good to know for FS in general.
  • 3D3D OS Professional Posts: 52 ✭✭
    The above worked for me before, but seems to be broken now. Perhaps @ilya_baran or @NeilCooke might have some insights into this?
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,210
    @3D
    We didn't change anything so people need more to go on than "it doesn't work" to help -- a shared document with a broken feature or at least a copy-pasted expression.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • 3D3D OS Professional Posts: 52 ✭✭
    Dilemma Solved: 

    I didn't think it would be likely OS would have changed anything, however the above wouldn't work - until I realised it was due to the Units included in the Varable by FS MD (Measure Distance) tool.

    Divide by mm & it's solved!
Sign In or Register to comment.