Bounding Box Help

Nath_McC
Nath_McC Member Posts: 136 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

  • S1mon
    S1mon Member Posts: 4,643 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

    Simon Gatrall | Product Development, Engineering, Design, Onshape | Ex- IDEO, PCH, Unagi, Carbon | LinkedIn

  • ilya_baran
    ilya_baran Onshape Employees, Developers, HDM Posts: 1,278 image
    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_McC
    Nath_McC Member Posts: 136 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.
  • S1mon
    S1mon Member Posts: 4,643 PRO
    @N_McCluskey
    To be clear, I didn’t write the feature script I linked to.

    Simon Gatrall | Product Development, Engineering, Design, Onshape | Ex- IDEO, PCH, Unagi, Carbon | LinkedIn

  • Nath_McC
    Nath_McC Member Posts: 136 PRO
    @S1mon Sorry, I thought you had. its a great script, just to complex for my basic knowledge of feature script.