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.

How to find smallest bounding box for part?

david_riedelldavid_riedell Member Posts: 23 ✭✭
edited June 2020 in General
I love the new custom tables feature.

I've been using OnShape for a while but I'm just getting into FeatureScript. The first one I'm working on is a way to generate a BOM that can be imported into Cutlist Optimizer (https://www.cutlistoptimizer.com/) when working with plywood or MDF sheet goods. I've tweaked the default "Part Bounds" table and created a "CutList Optimizer" table that reorders the columns and also consolidates parts of the same size into one row by adding a "Quantity" column.

What I want to do next is figure out a way to get the true dimensions of a panel when it is off axis. Right now if there is a part that is not strictly parallel to the XYZ planes, this FeatureScript mismeasures it because it uses the World Coordinate System. How can I make each part use its own coordinate system that minimizes its bounding box?

These pictures show what I mean. Each part of the assembly table on the left has faces that are parallel to the XYZ planes. The lone panel on the right does not have faces parallel to all 3 of the XYZ planes, and the CutList Optimizer featurescript table shows its calculated bounding box. I want this to show 24in x 12in.



This is the document I've been working on: https://cad.onshape.com/documents/fe89b7f320c073cd268fc79a/w/625cf4db4215519167e355db/e/7d4d64c704cf3c842f4060ce

Comments

  • lemon1324lemon1324 Member, Developers Posts: 225 EDU
    You can evaluate a bounding box with respect to a particular coordinate system.  The most natural approach for planar parts is to get the largest planar face and do an evPlane on it to get the part normal axis.  You then can either sweep candidate x-axes through 90 degrees to find the best orientation or check each linear edge in the part to see if it is a minimizing x-axis for the bounding box.

    You may find the Auto Layout feature I'm maintaining useful for either code examples (since it solves this actual problem; see the getOrientedFace, getUniqueVectors, and getInitialTransform functions) or to actually do the layout for you:
    https://cad.onshape.com/documents/576e01dbe4b0cc2e7f46a55d/w/b7cb6876d1121a6c249f59c7/e/887d6e2324589bfd2058c3e1
    Arul Suresh
    PhD, Mechanical Engineering, Stanford University
  • david_riedelldavid_riedell Member Posts: 23 ✭✭
    @lemon1324
    That sounds like it would work. I've experimented with your Auto Layout feature and it's very close to what I want. However I like using the Cutlist Optimizer website because it lists the cuts in order and does a good job displaying them visually.

    I'll take a look at the code in your feature. I think I'll probably be able to cobble together what I want by stealing bits and pieces of it :)

    Thanks!
  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,045 ✭✭✭✭✭
    @david_riedell
    @lemon1324
    You could also have a look at the algorithm behind Onshape's Calculate Bounds custom feature (It also includes some edits by me to calculate the minimum box).
    https://cad.onshape.com/documents/2ce3e64026df1ac7e63b98bd
    It uses another library that calculates the minimum 2d bounding box for a convex hull:
    https://cad.onshape.com/documents/d41ef62ff9b7c703c4e0afba
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • lemon1324lemon1324 Member, Developers Posts: 225 EDU
    @MBartlett21
    Ah nice, I may port AutoLayout to use that library then, I don't have a good guess on which approach would be faster for approximately-rectangular parts.
    Arul Suresh
    PhD, Mechanical Engineering, Stanford University
  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,045 ✭✭✭✭✭
    @lemon1324
    You should be able to just use the second document. (the one that does the convex hull and bounding box.) If you look at the source of Calculate Bounds, it takes a certain amount of samples for each non-linear edge, and converts them to 2d (see the getBoxPoints function)
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • david_riedelldavid_riedell Member Posts: 23 ✭✭
    Hey guys I appreciate the responses.

    @MBartlett21
    Calculate Bounds seems like a super useful feature, but I think I may have found a bug. It seems like it has trouble figuring out how to handle a transformed part. In the first image the transform is suppressed, in the second it is unsuppressed.







    Additionally, if I make a reference plane and create an off-axis part, the bounding box seems to get a little confused. It's definitely not the smallest possible box surrounding this object. Any idea why this is happening? What method are you using to determine how to create the box? It seems like @lemon1324
    idea of finding the largest planar face would work well in this instance.




    https://cad.onshape.com/documents/c6fe51c9efd6d4821b03fd44/w/8ab51650e92ad4aa4e21a5ba/e/a86d22af6987c379909a7073
  • david_riedelldavid_riedell Member Posts: 23 ✭✭
    @lemon1324 @MBartlett21

    Hey guys,

    Just wanted to let you know that I finished my CutList Optimizer Table FeatureScript. I'm super happy with it!

    https://cad.onshape.com/documents/51f6a6f5620fad627196859f/w/3c2f6950eda18c3e63ec881d/e/885748009bd1eb302e6939a8

    I used the technique that @lemon1324 suggested by finding the largest planar face, then testing each linear edge of that face to see which one yielded the smallest bounding box. I borrowed a few of your function definitions to accomplish this.

    I think my method is fairly efficient and it has worked well in my test cases so far. I think it may get bogged down if a body has tons of short linear edges, but I think that will be a pretty rare occurrence.

    I think the next step will be to distinguish between different material types so that it's not mixing 0.75" MDF panels with 0.75" Plywood panels. I'll have to look into how the material library works to figure this out.

  • lemon1324lemon1324 Member, Developers Posts: 225 EDU
    Nice! I'll leave it to @MBartlett21 to look into the possible arbitrary-orientation bug in the convex hull.

    Not that it really matters that much, but if you could credit me in the source code if you copied code I'd appreciate it (not that it's formally licensed or anything).

    As far as getting bogged down, I did do some profiling and the evBox3d takes significantly more time than evaluation of the direction of a linear edge, so if you're using the pre-filter for unique edge directions, then it should run in reasonable time even on large collections of parts.

    I was going to say I don't know if you can read materials from a FeatureScript, but it looks like in a custom table you can: https://cad.onshape.com/FsDoc/library.html#getProperty-Context-map
    Arul Suresh
    PhD, Mechanical Engineering, Stanford University
  • david_riedelldavid_riedell Member Posts: 23 ✭✭
    Sure, I'll be happy to credit you. I'll add your username and a link to the FeatureScripts I borrowed the code from.

    Yeah, I think your method of iterating through nonparallel edges on the largest face is pretty good heuristic. There may be some rare cases where there are tons of short edges but by and large I think this featurescript will be run on parts with <10 edges on the largest face. For my purposes this method definitely works better than sweeping through 90 degrees.

    I think you're right, the getProperty looks like what I need to pull in the materials information.

    I found it a little frustrating that FS doesn't let you pop or remove items from an array, but I was able to get the result I wanted by using a couple of for loops and populating a new array (see line 141).

    Thanks for all your pointers, I appreciate it!
  • kevin_ravenkevin_raven Member Posts: 3 PRO
    hi guys really new to this i use solidworks but i need a cutting list on the drawings as we manufacture large frames 
    i see you have done a script to do this how do i upload it so it will work for me 

    Thanks  
  • jacob_straszynskijacob_straszynski Member Posts: 2
    David, thanks for the that custom table. Saved me from messing around with OpenBom solely for this functionality.


Sign In or Register to comment.