6 Replies Latest reply on Nov 7, 2018 1:59 AM by Fifi Riri

    VBA - Get actual "Displayed" Dimension

    Cad Admin

      I'm working on a macro that processes dimensions in a drawing.  I'm trying to retrieve the actual dimension shown on the drawing with the least manipulation.  I know i can use GetSystemValue to get the value of the dim, but its not necessarily what is shown on the drawing.

       

      Example:

      Sheet dim is 0.5000

      GetSystemValue returns: 0.0127

      i can manipulate the value with XXX * 1000# * (1 / 25.4)

      But after manipulation the new value is 0.5

       

      I just want to retrieve the "Actual displayed dimension" ...in this example is 0.5000

        • Re: VBA - Get actual "Displayed" Dimension
          Alex Burnett

          It's hard to say exactly what you're looking for but here are a couple approaches I played around with. I'm not sure how to get the secondary dual dimension but I'm sure there's a way with the index of GetDimension2 or something.

           

          DisplayDimension dd = (DisplayDimension)swAnno.GetSpecificAnnotation();

          ((Dimension)dd.GetDimension2(0)).Value   // output (25.4)

          ((Dimension)dd.GetDimension2(0)).SystemValue // output (0.0254)

           

          Edit: Nevermind, the index is only used with Chamfer dimensions. I'd have to dig more to see if the dual dimension can be pulled directly.

          • Re: VBA - Get actual "Displayed" Dimension
            Josh Brady

            The DisplayDimension object is what you want.  It has a GetText method.

             

            Sorry... Doesn't get the right text.

              • Re: VBA - Get actual "Displayed" Dimension
                Alex Burnett

                Josh,

                 

                I took a look at all of the GetText options and it appears to return everything but the dimensions. For example, see my output below for that same 25.4 [1.000] linear value I tested above.

                 

                GetText(0):

                GetText(3):

                GetText(7):

                GetText(4):

                GetText(8):

                GetText(1):

                GetText(5):

                GetText(2):

                GetText(6):

                GetDimension2.Value: (25.4)

                GetDimension2.SystemValue: (0.0254)

                 

                 

                 

                Again for this value:

                GetText(0):

                GetText(3): above

                GetText(7): above

                GetText(4): below

                GetText(8): below

                GetText(1): front<MOD-DIAM>

                GetText(5): front<MOD-DIAM>

                GetText(2):  back

                GetText(6):  back

                GetDimension2.Value: (14.757192160023)

                GetDimension2.SystemValue: (0.014757192160023)

                • Re: VBA - Get actual "Displayed" Dimension
                  Josh Brady

                  If you want to get the actual displayed value, you may have to figure it out yourself...

                  As Alex mentioned, you can get the value in document units rather than using SystemValue.

                  Then, you can look at the PrimaryPrecision of the DisplayDimension to determine what to round it to.  Of course, you also need to check if the value is overridden with displayDimension.GetOverride.

                  Another possible approach would be to compare the Value with the pieces of text returned from the underlying Annotation's DisplayData object using GetTextAtIndex.  It returns the actual pieces of text that make up what's displayed on screen. However, it is a bit unpredictable at which index the desired value falls, so that's why you still have to compare Value with the displayed text.  It gets tricky if the displayed text is, say,  8.94, but the actual value is 8.9378 or something... You'd have to compare a rounded version of the dimension to each text piece from the DisplayData.  Doesn't sound fun.

                    • Re: VBA - Get actual "Displayed" Dimension
                      Cad Admin

                      Josh, thanks for your thoroughness and detailed explanation/options.  I will try to work through and see if i can come up with the appropriate output using your suggestions.  & you are right ...it doesn't sound fun...lol

                        • Re: VBA - Get actual "Displayed" Dimension
                          Fifi Riri

                          Hello,

                          Try this

                           

                          Option Explicit

                          Dim swApp As SldWorks.SldWorks

                          Dim swDraw As SldWorks.DrawingDoc

                          Dim swDispDim As SldWorks.DisplayDimension

                          Dim boolstatus As Boolean

                          Sub main()

                          Set swApp = Application.SldWorks

                          Set swDraw = swApp.ActiveDoc

                          'boolstatus = swDraw.Extension.SelectByID2("RD1@Drawing View1", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)

                          If swDraw.SelectionManager.GetSelectedObjectType3(1, -1) = 0 Or swDraw.SelectionManager.GetSelectedObjectType2(1) <> swSelectType_e.swSelDIMENSIONS Then

                              MsgBox ("Select a dimension")

                              Exit Sub

                          End If

                          Set swDispDim = swDraw.SelectionManager.GetSelectedObject6(1, -1)

                          Dim myValue As String

                          myValue = swDispDim.GetDimension2(0).Value

                          Dim myPrecision As Long

                          myPrecision = swDispDim.GetPrimaryPrecision

                          myValue = Round(myValue, myPrecision)

                          Do While myPrecision > Len(myValue) - InStr(myValue, ".")

                              myValue = myValue & "0"

                          Loop

                          MsgBox (myValue)

                          End Sub