12 Replies Latest reply on Oct 4, 2016 1:26 PM by Todd Bennett

    Bounding box with API?

    Todd Bennett

      I've seen a lot of posts about bounding boxes.  I have tried a dozen (or more) macros.  Does anyone have a macro that can actually get an accurate bounding box, even if the part was drawn at some goofy orientation?

       

      We import files created in other programs by other companies.  I need to collect accurate bounding box dimensions with the a macro so I can: open all the files, get the info, then export the part on new coordinate system based on the bounding box.

       

      You would think all that other stuff is the hard part, but the bounding box has been the real challenge.  I have attached a simple part that should prove somewhat difficult.

       

      Thank you.

        • Re: Bounding box with API?
          John Stoltzfus

          I think those are hard to come by, the one I use isn't 100% correct and I do only use it as a note in the drawing, so if a part is modeled "Catty Waumpoous"  it'll give it a weird dimension.

            • Re: Bounding box with API?
              Todd Bennett

              The bounding box made by the Weldment feature is perfect, but I have a few problems:

               

              1) I don't know how to turn a part into a weldment programmatically.

              2) I don't know how to make the weldment bounding box programmatically.

              3) I don't know how to use the weldment bounding box's lines progammatically.

                • Re: Bounding box with API?
                  Todd Bennett

                  This will basically take care of 1 and 2 of my list.  I think you have to make sure your Document Properties have "Automatically create cut lists" and "Automatically update cut lists..." selected under Weldments.

                   

                   

                  Dim swApp As Object

                  Dim Part As Object

                  Dim boolstatus As Boolean

                  Dim longstatus As Long, longwarnings As Long

                   

                  Sub main()

                   

                      Dim myModelView As Object

                      Dim myFeature As Object

                    

                      Set swApp = Application.SldWorks

                      Set Part = swApp.ActiveDoc

                      Set myModelView = Part.ActiveView

                      myModelView.FrameState = swWindowState_e.swWindowMaximized

                      Set myFeature = Part.FeatureManager.InsertWeldmentFeature()

                      boolstatus = Part.Extension.SelectByID2("Solid Bodies", "BDYFOLDER", 0, 0, 0, False, 0, Nothing, 0)

                      Part.ClearSelection2 True

                      boolstatus = Part.ForceRebuild3(True)

                      boolstatus = Part.Extension.SelectByID2("Solid Bodies", "BDYFOLDER", 0, 0, 0, False, 0, Nothing, 0)

                      Part.Extension.Create3DBoundingBox

                      boolstatus = Part.Extension.SelectByID2("Bounding-Box_Cut-List-Item1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

                      Part.UnblankSketch

                    

                  End Sub

                    • Re: Bounding box with API?
                      Todd Bennett

                      Although I created this from an imported "dumb" solid, the cut list does have accurate length, width, and thickness.

                      • Re: Bounding box with API?
                        Todd Bennett

                        I added the following directly after "myModelView.FrameState = swWindowState_e.swWindowMaximized" to make sure the Document Properties were set.

                         

                        boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swWeldmentEnableAutomaticCutList, 0, True)

                        boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swWeldmentEnableAutomaticUpdate, 0, True)

                        boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swWeldmentRenameCutlistDescriptionPropertyValue, 0, False)

                  • Re: Bounding box with API?
                    JOHN GEORGE

                    Todd,

                    Check this discussion

                    There is a macro by Matt Fisher

                    Hope this works for you

                    • Re: Bounding box with API?
                      JOHN GEORGE

                      With the above macro, you can pick any 2 edges and it create the box

                      see this

                      or these edges

                       

                      • Re: Bounding box with API?
                        Todd Bennett

                        Yeah, that is cool, but I'm not much of a programmer and I need to get the lines without any input from the user.  For me that is trading one thing I can't do for different thing I can't do.

                        • Re: Bounding box with API?
                          Keith Rice

                          There are an infinite number of bounding boxes for any given body depending on the reference. Please specify the axes of the bounding box you want.

                           

                          If one of the faces is parallel to one of the bounding box planes, then you can get the normals of each plane and use them in conjunction with IBody2::GetExtremePoint to calculate the bounding box relative to that direction.

                           

                          The weldment LENGTH is based on the sketch used in the structural member feature, AFAIK. Since this isn't created from a structural member you won't be able to use the weldment LENGTH property.

                           

                          Keith

                          SolidWorks API Training and Services

                            • Re: Bounding box with API?
                              Todd Bennett

                              Good point, Keith.  I think there would usually be at least one face parallel to a bounding box plane.  We don't deal with a lot of free form surfaces.

                              I'm not sure how SolidWorks calculates their weldment bounding box, but it sure does a good job at minimizing the volume, while keeping a face parallel to at least one bounding box plane.  The problem is using the bounding box after it creates it.

                            • Re: Bounding box with API?
                              Todd Bennett

                              I guess I basically answered my original question.  I will explore question three (above) in a separate post.