5 Replies Latest reply on Jan 14, 2018 6:19 AM by Korbi Anis

    get the 2D oriented bounding box (OBB) for 2D sketch

    Korbi Anis

      Hi friends , I need a VBA code that allows to get the 2D oriented bounding box for a 2D sketech.Thanks

        • Re: get the 2D oriented bounding box (OBB) for 2D sketch
          Matthew Cempa

          Oriented in what way?  That is the key question.  I wrote one that creates a bounding volume oriented along the axes defined by the moment of inertia of the selected body(s).  I get that from the API mass properties object but that requires a body.  The other issue is the question of whether the sketch is closed.  If it is closed you could extrude the sketch to get a body and then use that for the orientation of the Ixx & Iyy axes.  I have found that the moment of inertia gives a pretty decent minimum area bound.

          • Re: get the 2D oriented bounding box (OBB) for 2D sketch
            Rob Edwards

            Hi Korbi

            Im only just starting out with the API, so Im pretty interested in learning..

            Someones guarenteed to know better but by my reckoning a sketch is just a feature, so you can use 2017 SOLIDWORKS API Help - GetBox Method (IFeature)

             

            This may not be so helpful if your sketch is not on a default plane, I guess that's where the maths starts..

             

            I'll have a look into it and see if I can come up with something.  What exactly are you looking for?

             

            Option Explicit
            Dim swApp As SldWorks.SldWorks
            Dim swModel As SldWorks.ModelDoc2
            Dim swPart As SldWorks.PartDoc
            Dim swSelMgr As SldWorks.SelectionMgr
            Dim swFeature As SldWorks.Feature
            Dim box As Variant
            Dim bs As Boolean
            
            Sub Main()
                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc
                Set swSelMgr = swModel.SelectionManager
                Set swFeature = swSelMgr.GetSelectedObject6(index:=1, Mark:=-1)
                
                bs = swFeature.GetBox(box)
                
                Debug.Print swFeature.Name
                Debug.Print "Get Box: " & bs
                Debug.Print "Point", "x", "y", "z"
                Debug.Print "1", box(0) * 1000, box(1) * 1000, box(2) * 1000
                Debug.Print "2", box(3) * 1000, box(4) * 1000, box(5) * 1000
                
            End Sub
            

            Running the macro on this sketch gives

            Sketch1

            Get Box: True

            Point        x            y            z

            1            -2.77555756156289E-14        0            0

            2              100          50            0

             

            Sketch2

            Get Box: True

            Point        x            y            z

            1              0            -45          -45

            2              0            45            45

             

            Sketch3

            Get Box: True

            Point        x            y            z

            1            -123          0            -75

            2            -36.3974596215561            0            0

             

            edit:

             

            found this in the help file...  not sure what to do with it yet

             

                Set swSketch = swFeature.GetSpecificFeature2
                
                Set swMathTrans = swSketch.ModelToSketchTransform
                
                Debug.Print "    Origin = (" & -1# * swMathTrans.ArrayData(9) * 1000# & ", " & -1# * swMathTrans.ArrayData(10) * 1000# & ", " & -1# * swMathTrans.ArrayData(11) * 1000# & ") mm"
                Debug.Print "    Rot1   = (" & swMathTrans.ArrayData(0) & ", " & swMathTrans.ArrayData(1) & ", " & swMathTrans.ArrayData(2) & ")"
                Debug.Print "    Rot2   = (" & swMathTrans.ArrayData(3) & ", " & swMathTrans.ArrayData(4) & ", " & swMathTrans.ArrayData(5) & ")"
                Debug.Print "    Rot3   = (" & swMathTrans.ArrayData(6) & ", " & swMathTrans.ArrayData(7) & ", " & swMathTrans.ArrayData(8) & ")"
                Debug.Print "    Trans  = (" & swMathTrans.ArrayData(9) * 1000# & ", " & swMathTrans.ArrayData(10) * 1000# & ", " & swMathTrans.ArrayData(11) * 1000# & ") mm"
                Debug.Print "    Scale  = " & swMathTrans.ArrayData(12)
            

             

            but looking useful

             

            Sketch3

            Get Box: True

            Point         x             y             z

            1             -123           0            -75

            2             -36.3974596215561            0             0

                Origin = (-0, -0, -0) mm

                Rot1   = (1, 0, 0)

                Rot2   = (0, 0, 1)

                Rot3   = (0, -1, 0)

                Trans  = (0, 0, 0) mm

                Scale  = 1

             

            Think Ive got it now.  This help page does the opposite of what we want so easy enough to adapt

            2012 SOLIDWORKS API Help - Transform Coordinates from Sketch to Model Space Example (VBA)

             

            ... continuing from prev code
                box_pt1 = Array(box(0), box(1), box(2))
                box_pt2 = Array(box(3), box(4), box(5))
              
                For i = 0 To 2
                    Debug.Print "box_pt1:", i, box_pt1(i)
                Next
                For i = 0 To 2
                    Debug.Print "box_pt2:", i, box_pt2(i)
                Next
                sketch_pt1 = GetSketchCoordinates(box_pt1)
                sketch_pt2 = GetSketchCoordinates(box_pt2)
                
                For i = 0 To 2
                    Debug.Print "sketch_pt1:", i, sketch_pt1(i)
                Next
                For i = 0 To 2
                    Debug.Print "sketch_pt2:", i, sketch_pt2(i)
                Next
              
            End Sub
            
            Private Function GetSketchCoordinates(PtArr As Variant) As Variant
                Dim swMathPt As SldWorks.MathPoint
                Dim swMathUtil As SldWorks.MathUtility
                Set swMathUtil = swApp.GetMathUtility
                Set swMathPt = swMathUtil.CreatePoint(PtArr)
                Set swMathPt = swMathPt.MultiplyTransform(swMathTrans)
                GetSketchCoordinates = swMathPt.ArrayData
            End Function
            

             

            I'll clean up the code and post when I do

            • Re: get the 2D oriented bounding box (OBB) for 2D sketch
              Rob Edwards

              Hi Korbi

               

              I reworked the code, but am sorry to say it only works in simple cases

              doesn't seem to like ellipses or splines

               

              didn't mind mid planes

              but these slots on a plane defined with a line and point, produced nothing!

               

               

              I've either got a mistake in my code or as the help file says, it doesn't always work!

               

              Anyway I've had fun and learned a bit more, my code is below.  Must be a way, you just need someone who knows what they doin

               

              'by 369

              Option Explicit

               

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swPart As SldWorks.PartDoc

              Dim swSelMgr As SldWorks.SelectionMgr

              Dim swFeature As SldWorks.Feature

              Dim box As Variant

              Dim swSketch As SldWorks.Sketch

              Dim swMathTrans As SldWorks.MathTransform

              Dim swMathPt As SldWorks.MathPoint

              Dim swMathUtil As SldWorks.MathUtility

              Dim ptArr(2) As Double

              Dim i As Integer

              Dim bs As Boolean

               

              Sub Main()

               

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swSelMgr = swModel.SelectionManager

                  Set swFeature = swSelMgr.GetSelectedObject6(index:=1, Mark:=-1)

                  Set swSketch = swFeature.GetSpecificFeature2

                  Debug.Print swSketch.Name

                  Call swSketch.GetBox(box)

                  

                  Debug.Print "model space"

                  For i = 0 To 5

                      Debug.Print FormatNumber(box(i) * 1000#, 3)

                  Next

                

                  Set swMathUtil = swApp.GetMathUtility

                  Set swMathTrans = swSketch.ModelToSketchTransform

                

                  Debug.Print

                  Debug.Print "Origin = (" & -1# * swMathTrans.ArrayData(9) * 1000# & ", " & -1# * swMathTrans.ArrayData(10) * 1000# & ", " & -1# * swMathTrans.ArrayData(11) * 1000# & ") mm"

                  Debug.Print "Rot1  = (" & swMathTrans.ArrayData(0) & ", " & swMathTrans.ArrayData(1) & ", " & swMathTrans.ArrayData(2) & ")"

                  Debug.Print "Rot2  = (" & swMathTrans.ArrayData(3) & ", " & swMathTrans.ArrayData(4) & ", " & swMathTrans.ArrayData(5) & ")"

                  Debug.Print "Rot3  = (" & swMathTrans.ArrayData(6) & ", " & swMathTrans.ArrayData(7) & ", " & swMathTrans.ArrayData(8) & ")"

                  Debug.Print "Trans  = (" & swMathTrans.ArrayData(9) * 1000# & ", " & swMathTrans.ArrayData(10) * 1000# & ", " & swMathTrans.ArrayData(11) * 1000# & ") mm"

                  Debug.Print "Scale  = " & swMathTrans.ArrayData(12)

                

                  ptArr(0) = box(0)

                  ptArr(1) = box(1)

                  ptArr(2) = box(2)

               

                  Set swMathPt = swMathUtil.CreatePoint((ptArr))

                  Set swMathPt = swMathPt.MultiplyTransform(swMathTrans)

                

                  box(0) = swMathPt.ArrayData(0)

                  box(1) = swMathPt.ArrayData(1)

                  box(2) = swMathPt.ArrayData(2)

                

                  ptArr(0) = box(3)

                  ptArr(1) = box(4)

                  ptArr(2) = box(5)

                

                  Set swMathPt = swMathUtil.CreatePoint((ptArr))

                  Set swMathPt = swMathPt.MultiplyTransform(swMathTrans)

                

                  box(3) = swMathPt.ArrayData(0)

                  box(4) = swMathPt.ArrayData(1)

                  box(5) = swMathPt.ArrayData(2)

                

                  Debug.Print

                  Debug.Print "sketch space"

                  For i = 0 To 5

                  Debug.Print FormatNumber(box(i) * 1000#, 3)

                  Next

               

                  With swModel

                      .EditSketch

                      .SketchManager.CreateCornerRectangle box(0), box(1), 0, box(3), box(4), 0

                      .SketchManager.CreateConstructionGeometry

                      '.SketchManager.InsertSketch True

                  End With

              End Sub