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.

Bounding Box Help

Nath_McCNath_McC Member Posts: 37 PRO
Hi Forum,

I was wondering if a feature script smart person could give me a helping hand. I am trying to create a bounding box around my part. I have had a look at some other feature scripts but they included sheet metal parts (mine does not) and I got confused.

What I am looking for is to below:

Line 51 - Add a script that will get the  X,Y, Z lengths of the part, My thinking is that I can then use these to find the longest of the three sides. These values will link to the variables xLength, yLength and zLength.

Line 60 - Add code which will take the longest axis and set it as longestAxis variable from the array.

Line 106 - If the 'create bound box' checkbox is ticked build a bounding box around the part, I guess this will end up being a new body so it would be great if that could be auto named 'Part # - Bounding Box'. That the name of the select body and then adding the extra to the end.

It has taken me quite a while to get to this point so if anybody could help, it would be greatly appreciated.

https://cad.onshape.com/documents/5da157e99bb455d905d327d2/w/d44f3937f3715cfeea184d91/e/cc11569037b08d0bcc741424
Tagged:

Comments

  • S1monS1mon Member Posts: 883 PRO
    I haven't looked at your code, but have you seen this featurescript which handles parts, points and sheet metal:
    https://cad.onshape.com/documents/2ce3e64026df1ac7e63b98bd/w/7a2029e15b241c80c591a492/e/ee8919ad0a28cec3b0740617

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,120
    The above post is a good example, but it's a bit complex.  To get the bounding box variables, you can use evBox3d like this:

            var bbox = evBox3d(context, {
                    "topology" : definition.SelectedBody,
                    "tight" : true
            });
            
            var xLength = bbox.maxCorner[0] - bbox.minCorner[0];
            var yLength = bbox.maxCorner[1] - bbox.minCorner[1];
            var zLength = bbox.maxCorner[2] - bbox.minCorner[2];
    
    For line 60, the max function can take an array:
    var longestAxis = max(axisLenghts);
    To create the bounding box part, you can use fCuboid:
            fCuboid(context, id + "cuboid", {
                    "corner1" : bbox.minCorner,
                    "corner2" : bbox.maxCorner
            });
    With naming, there's a bit of a snag since you don't know the part name yet at this point during regen.  You can give it a static name, but not a name that depends on the part:
            setProperty(context, {
                    "entities" : qCreatedBy(id + "cuboid", EntityType.BODY),
                    "propertyType" : PropertyType.NAME,
                    "value" : "Bounding box"
            });
    Hope this helps.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • Nath_McCNath_McC Member Posts: 37 PRO
    edited November 2021
    S1mon said:
    I haven't looked at your code, but have you seen this featurescript which handles parts, points and sheet metal:
    https://cad.onshape.com/documents/2ce3e64026df1ac7e63b98bd/w/7a2029e15b241c80c591a492/e/ee8919ad0a28cec3b0740617

    @S1mon your code was the one what confused by. it worked great but as my parts will only be solid bodies I wanted to simplify it down a little.

    @ilya_baran Thank you, that break down give me a great starting point for updating my code.
  • S1monS1mon Member Posts: 883 PRO
    @N_McCluskey
    To be clear, I didn’t write the feature script I linked to.
  • Nath_McCNath_McC Member Posts: 37 PRO
    @S1mon Sorry, I thought you had. its a great script, just to complex for my basic knowledge of feature script. 
Sign In or Register to comment.