7 Replies Latest reply on Dec 31, 2014 9:21 AM by Jon Proffitt

    Get Display Dimension Type - API

    JOHN GEORGE

      I am trying to create a macro from one of the API help example (modified) to show the dimension type

       

      When I run the code, I am getting the error (highlighted)

      Can someone please correct the code to get it working?. (I am not a VB expert and trying to learn thru the SW API Examples)

       

       

      Option Explicit

       

      Dim swApp                   As SldWorks.SldWorks

      Dim swModel                 As ModelDoc2

      Dim swDraw                  As DrawingDoc

      Dim swView                  As SldWorks.View

      Dim swDispDim               As SldWorks.DisplayDimension

      Dim swDim                   As SldWorks.Dimension

      Dim swAnnotation            As SldWorks.Annotation

      Dim swDisplayDimension      As SldWorks.DisplayDimension


      Sub main()

       

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

         Set swAnnotation = swDraw.GetFirstAnnotation2

          Do While (Not (swAnnotation Is Nothing))

              Debug.Print " "

              Debug.Print "Annotation name = " & swAnnotation.GetName

                  If swAnnotationType_e.swDisplayDimension = swAnnotation.GetType Then

                      Debug.Print "  Is a display dimension? True"

                      Set swDisplayDimension = swAnnotation.GetSpecificAnnotation

                      Select Case (swDisplayDimension.Type2)

                         Case swDimensionType_e.swOrdinateDimension

                             Debug.Print "  Display dimension type = base ordinate and its subordinates"

                         Case swDimensionType_e.swLinearDimension

                             Debug.Print "  Display dimension type = linear"

                         Case swDimensionType_e.swAngularDimension

                             Debug.Print "  Display dimension type  = angular"

                         Case swDimensionType_e.swArcLengthDimension

                             Debug.Print "  Display dimension type = arc length"

                         Case swDimensionType_e.swRadialDimension

                             Debug.Print "  Display dimension type = radial"

                         Case swDimensionType_e.swDiameterDimension

                             Debug.Print "  Display dimension type = diameter"

                         Case swDimensionType_e.swHorOrdinateDimension

                             Debug.Print "  Display dimension type = horizontal ordinate"

                         Case swDimensionType_e.swVertOrdinateDimension

                             Debug.Print "  Display dimension type = vertical ordinate"

                         Case swDimensionType_e.swZAxisDimension

                             Debug.Print "  Display dimension type = z-axis"

                         Case swDimensionType_e.swChamferDimension

                             Debug.Print "  Display dimension type = chamfer dimension"

                         Case swDimensionType_e.swHorLinearDimension

                             Debug.Print "  Display dimension type = horizontal linear"

                         Case swDimensionType_e.swVertLinearDimension

                             Debug.Print "  Display dimension type = vertical linear"

                         Case swDimensionType_e.swScalarDimension

                             Debug.Print "  Display dimension type = scalar"

                         Case Else

                             Debug.Print "  Display dimension type = unknown"

                      End Select

                      End If

              Loop

      End Sub

       

      Thanks in advance

      JOHN

        • Re: Get Display Dimension Type - API
          Keith Rice

          John,

           

          In the future you can just tell us what error message you are getting, that way we can help you faster.

           

          My guess is that you are getting the most common error, "object variable or with block not set", which means that on the erroneous line you are trying to use an object variable that you never set.  In your case, the culprit is swDraw.  This was never set equal to an IDrawingDoc object.  To get the IDrawingDoc object for the active document, you need this line AFTER "Set swModel = swApp.ActiveDoc":

           

          Set swDraw = swModel

           

          That will "cast" the IModelDoc2 pointer for the active document to your IDrawingDoc object variable.  In less technical jargon: IDrawingDoc is similar enough to IModelDoc2 that you can assign one to the other.

           

          Anyway, once swDraw is set then you can use it with IDrawingDoc::GetFirstAnnotation2.

           

          By the way, for future reference, consider download our "Debugging Tips for SolidWorks API Programmers" to understand what the most common error messages in SolidWorks API VBA error messages mean.

           

          Keith

          Video Tutorials for the SolidWorks API

            • Re: Get Display Dimension Type - API
              JOHN GEORGE

              Keith,

              Thanks for the reply

              Once I add the line  Set swDraw = swModel I don't get any result

              Still I am missing something

               

              I just have another question:

              What's the effect of  "Debug.print" and the difference between Debug.print and MsgBox


                • Re: Get Display Dimension Type - API
                  Keith Rice

                  John,

                   

                  The remarks for IModelDoc2::GetFirstAnnotation2 say, "For drawings, access the annotations using the IView::GetFirstAnnotation3 method."  So your code needs to look like this:

                   

                  =====

                   

                  Option Explicit

                   

                  Dim swApp                   As SldWorks.SldWorks

                  Dim swModel                 As SldWorks.ModelDoc2

                  Dim swDraw                  As SldWorks.DrawingDoc

                  Dim swView                  As SldWorks.View

                  Dim swDispDim               As SldWorks.DisplayDimension

                  Dim swDim                   As SldWorks.Dimension

                  Dim swAnn                   As SldWorks.Annotation

                  Dim swDisplayDimension      As SldWorks.DisplayDimension

                   

                  Sub main()

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc

                      Set swDraw = swModel

                      Set swView = swDraw.GetFirstView

                      While Not swView Is Nothing

                          Set swAnn = swView.GetFirstAnnotation3

                          While Not swAnn Is Nothing

                              Debug.Print " "

                              Debug.Print "Annotation name = " & swAnn.GetName

                              If swAnn.GetType = swAnnotationType_e.swDisplayDimension Then

                                  Debug.Print "  Is a display dimension? True"

                                  Set swDisplayDimension = swAnn.GetSpecificAnnotation

                                  Select Case (swDisplayDimension.Type2)

                                     Case swDimensionType_e.swOrdinateDimension

                                         Debug.Print "  Display dimension type = base ordinate and its subordinates"

                                     Case swDimensionType_e.swLinearDimension

                                         Debug.Print "  Display dimension type = linear"

                                     Case swDimensionType_e.swAngularDimension

                                         Debug.Print "  Display dimension type  = angular"

                                     Case swDimensionType_e.swArcLengthDimension

                                         Debug.Print "  Display dimension type = arc length"

                                     Case swDimensionType_e.swRadialDimension

                                         Debug.Print "  Display dimension type = radial"

                                     Case swDimensionType_e.swDiameterDimension

                                         Debug.Print "  Display dimension type = diameter"

                                     Case swDimensionType_e.swHorOrdinateDimension

                                         Debug.Print "  Display dimension type = horizontal ordinate"

                                     Case swDimensionType_e.swVertOrdinateDimension

                                         Debug.Print "  Display dimension type = vertical ordinate"

                                     Case swDimensionType_e.swZAxisDimension

                                         Debug.Print "  Display dimension type = z-axis"

                                     Case swDimensionType_e.swChamferDimension

                                         Debug.Print "  Display dimension type = chamfer dimension"

                                     Case swDimensionType_e.swHorLinearDimension

                                         Debug.Print "  Display dimension type = horizontal linear"

                                     Case swDimensionType_e.swVertLinearDimension

                                         Debug.Print "  Display dimension type = vertical linear"

                                     Case swDimensionType_e.swScalarDimension

                                         Debug.Print "  Display dimension type = scalar"

                                     Case Else

                                         Debug.Print "  Display dimension type = unknown"

                                  End Select

                              End If

                              Set swAnn = swAnn.GetNext3

                          Wend

                          Set swView = swView.GetNextView

                      Wend

                  End Sub

                   

                  =====

                   

                  Debug.print sends text to the immediate window (which you can view by going to View-->Immediate Window) and is generally used for text you don't want the end user to see, only the programmer.  Hence it is also called the debug window sometimes.  MsgBox creates a dialog box that the end user has to interact with.

                   

                  Keith

                  Video Tutorials for the SolidWorks API

                    • Re: Get Display Dimension Type - API
                      JOHN GEORGE

                      Keith,

                      This code shows the result.

                      Still it doesn't show the dimension type in detail


                      For example:

                      My drawing has Horizontal and Vertical Ordinate dimensions, Horizontal and Vertical Linear Dimensions

                       

                      All the ordinate dimensions (Either horizontal or vertical) display as base ordinate and its subordinates

                      Even linear dimensions, it doesn't display as Horizontal Linear or Vertical Linear

                       

                      Any idea?

                        • Re: Get Display Dimension Type - API
                          Artem Taturevych

                          Hi John,

                           

                          This macro will return the dimension type based on the command you have used. For example even if you insert horizontal dimension but used smart dimension command it will not recognize it as horizontal dimension.

                           

                          I have changed a macro so it recognizes the dimension type based on the position. Hope this is what are you looking for:

                           

                          Dim swApp                   As SldWorks.SldWorks

                          Dim swModel                 As SldWorks.ModelDoc2

                          Dim swDraw                  As SldWorks.DrawingDoc

                          Dim swView                  As SldWorks.View

                          Dim swDispDim               As SldWorks.DisplayDimension

                          Dim swDim                   As SldWorks.Dimension

                          Dim swAnn                   As SldWorks.Annotation

                          Dim swDisplayDimension      As SldWorks.DisplayDimension

                           

                          Sub main()

                              Set swApp = Application.SldWorks

                              Set swModel = swApp.ActiveDoc

                              Set swDraw = swModel

                              Set swView = swDraw.GetFirstView

                              While Not swView Is Nothing

                                  Set swAnn = swView.GetFirstAnnotation3

                                  While Not swAnn Is Nothing

                                      Debug.Print " "

                                      Debug.Print "Annotation name = " & swAnn.GetName

                                      If swAnn.GetType = swAnnotationType_e.swDisplayDimension Then

                                          Debug.Print "  Is a display dimension? True"

                                          Set swDisplayDimension = swAnn.GetSpecificAnnotation

                                          Select Case (swDisplayDimension.Type2)

                                             Case swDimensionType_e.swOrdinateDimension

                          Debug.Print "  Display dimension type = " & GetOrientation(swDisplayDimension) & " ordinate and its subordinates"

                                             Case swDimensionType_e.swLinearDimension

                          Debug.Print "  Display dimension type = " & GetOrientation(swDisplayDimension) & " linear"

                                             Case swDimensionType_e.swAngularDimension

                          Debug.Print "  Display dimension type  = angular"

                                             Case swDimensionType_e.swArcLengthDimension

                          Debug.Print "  Display dimension type = arc length"

                                             Case swDimensionType_e.swRadialDimension

                          Debug.Print "  Display dimension type = radial"

                                             Case swDimensionType_e.swDiameterDimension

                          Debug.Print "  Display dimension type = diameter"

                                             Case swDimensionType_e.swHorOrdinateDimension

                          Debug.Print "  Display dimension type = horizontal ordinate"

                                             Case swDimensionType_e.swVertOrdinateDimension

                          Debug.Print "  Display dimension type = vertical ordinate"

                                             Case swDimensionType_e.swZAxisDimension

                          Debug.Print "  Display dimension type = z-axis"

                                             Case swDimensionType_e.swChamferDimension

                          Debug.Print "  Display dimension type = chamfer dimension"

                                             Case swDimensionType_e.swHorLinearDimension

                          Debug.Print "  Display dimension type = horizontal linear"

                                             Case swDimensionType_e.swVertLinearDimension

                          Debug.Print "  Display dimension type = vertical linear"

                                             Case swDimensionType_e.swScalarDimension

                          Debug.Print "  Display dimension type = scalar"

                                             Case Else

                          Debug.Print "  Display dimension type = unknown"

                                          End Select

                                      End If

                                      Set swAnn = swAnn.GetNext3

                                  Wend

                                  Set swView = swView.GetNextView

                              Wend

                          End Sub

                           

                          Function GetOrientation(swDisplayDimension As DisplayDimension) As String

                              Dim swDim As SldWorks.Dimension

                              Set swDim = swDisplayDimension.GetDimension2(0)

                              Dim swMathVec As SldWorks.MathVector

                              Dim vDir As Variant

                             

                              GetOrientation = ""

                             

                              Set swMathVec = swDim.DimensionLineDirection

                              vDir = swMathVec.ArrayData

                             

                              If IsCollinear(vDir(0), 1) And IsCollinear(vDir(1), 0) Then

                                  GetOrientation = "horizontal"

                              ElseIf IsCollinear(vDir(1), 1) And IsCollinear(vDir(0), 0) Then

                                  GetOrientation = "vertical"

                              End If

                          End Function

                           

                          Function IsCollinear(val As Variant, vector As Double) As Boolean

                              Const TOL = 0.00000001

                              IsCollinear = Abs(Abs(val) - Abs(vector)) < TOL

                          End Function

                          ______________________

                          Regards,

                          Artem Taturevych

                          Application Engineer at Intercad

                          http://intercad.com.au/

                          Tel: +61 2 9454 4444