Get Display Dimension Type - API

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

SolidworksApi macros
Answers 7
KR
KR 2012-09-23 Best Answer

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

JG
JG 2012-09-23

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


KR
KR 2012-09-23

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

JG
JG 2012-09-23

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?

AT
AT 2012-09-24

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:

<p style="margin-bottom: 0.0001pt;">Dim swApp                   As SldWorks.SldWorks</p><p style="margin-bottom: 0.0001pt;">Dim swModel                 As SldWorks.ModelDoc2</p><p style="margin-bottom: 0.0001pt;">Dim swDraw                  As SldWorks.DrawingDoc</p><p style="margin-bottom: 0.0001pt;">Dim swView                  As SldWorks.View</p><p style="margin-bottom: 0.0001pt;">Dim swDispDim               As SldWorks.DisplayDimension</p><p style="margin-bottom: 0.0001pt;">Dim swDim                   As SldWorks.Dimension</p><p style="margin-bottom: 0.0001pt;">Dim swAnn                   As SldWorks.Annotation</p><p style="margin-bottom: 0.0001pt;">Dim swDisplayDimension      As SldWorks.DisplayDimension</p><p></p><p style="margin-bottom: 0.0001pt;">Sub main()</p><p style="margin-bottom: 0.0001pt;">    Set swApp = Application.SldWorks</p><p style="margin-bottom: 0.0001pt;">    Set swModel = swApp.ActiveDoc</p><p style="margin-bottom: 0.0001pt;">    Set swDraw = swModel</p><p style="margin-bottom: 0.0001pt;">    Set swView = swDraw.GetFirstView</p><p style="margin-bottom: 0.0001pt;">    While Not swView Is Nothing</p><p style="margin-bottom: 0.0001pt;">        Set swAnn = swView.GetFirstAnnotation3</p><p style="margin-bottom: 0.0001pt;">        While Not swAnn Is Nothing</p><p style="margin-bottom: 0.0001pt;">            Debug.Print " "</p><p style="margin-bottom: 0.0001pt;">            Debug.Print "Annotation name = " & swAnn.GetName</p><p style="margin-bottom: 0.0001pt;">            If swAnn.GetType = swAnnotationType_e.swDisplayDimension Then</p><p style="margin-bottom: 0.0001pt;">                Debug.Print "  Is a display dimension? True"</p><p style="margin-bottom: 0.0001pt;">                Set swDisplayDimension = swAnn.GetSpecificAnnotation</p><p style="margin-bottom: 0.0001pt;">                Select Case (swDisplayDimension.Type2)</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swOrdinateDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = " & GetOrientation(swDisplayDimension) & " ordinate and its subordinates"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swLinearDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = " & GetOrientation(swDisplayDimension) & " linear"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swAngularDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type  = angular"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swArcLengthDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = arc length"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swRadialDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = radial"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swDiameterDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = diameter"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swHorOrdinateDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = horizontal ordinate"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swVertOrdinateDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = vertical ordinate"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swZAxisDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = z-axis"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swChamferDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = chamfer dimension"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swHorLinearDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = horizontal linear"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swVertLinearDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = vertical linear"</p><p style="margin-bottom: 0.0001pt;">                   Case swDimensionType_e.swScalarDimension</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = scalar"</p><p style="margin-bottom: 0.0001pt;">                   Case Else</p><p style="margin-bottom: 0.0001pt;"> Debug.Print "  Display dimension type = unknown"</p><p style="margin-bottom: 0.0001pt;">                End Select</p><p style="margin-bottom: 0.0001pt;">            End If</p><p style="margin-bottom: 0.0001pt;">            Set swAnn = swAnn.GetNext3</p><p style="margin-bottom: 0.0001pt;">        Wend</p><p style="margin-bottom: 0.0001pt;">        Set swView = swView.GetNextView</p><p style="margin-bottom: 0.0001pt;">    Wend</p><p style="margin-bottom: 0.0001pt;">End Sub</p><p></p><p style="margin-bottom: 0.0001pt;">Function GetOrientation(swDisplayDimension As DisplayDimension) As String</p><p style="margin-bottom: 0.0001pt;">    Dim swDim As SldWorks.Dimension</p><p style="margin-bottom: 0.0001pt;">    Set swDim = swDisplayDimension.GetDimension2(0)</p><p style="margin-bottom: 0.0001pt;">    Dim swMathVec As SldWorks.MathVector</p><p style="margin-bottom: 0.0001pt;">    Dim vDir As Variant</p><p style="margin-bottom: 0.0001pt;">    </p><p style="margin-bottom: 0.0001pt;">    GetOrientation = ""</p><p style="margin-bottom: 0.0001pt;">    </p><p style="margin-bottom: 0.0001pt;">    Set swMathVec = swDim.DimensionLineDirection</p><p style="margin-bottom: 0.0001pt;">    vDir = swMathVec.ArrayData</p><p style="margin-bottom: 0.0001pt;">    </p><p style="margin-bottom: 0.0001pt;">    If IsCollinear(vDir(0), 1) And IsCollinear(vDir(1), 0) Then</p><p style="margin-bottom: 0.0001pt;">        GetOrientation = "horizontal"</p><p style="margin-bottom: 0.0001pt;">    ElseIf IsCollinear(vDir(1), 1) And IsCollinear(vDir(0), 0) Then</p><p style="margin-bottom: 0.0001pt;">        GetOrientation = "vertical"</p><p style="margin-bottom: 0.0001pt;">    End If</p><p style="margin-bottom: 0.0001pt;">End Function</p><p></p><p style="margin-bottom: 0.0001pt;">Function IsCollinear(val As Variant, vector As Double) As Boolean</p><p style="margin-bottom: 0.0001pt;">    Const TOL = 0.00000001</p><p style="margin-bottom: 0.0001pt;">    IsCollinear = Abs(Abs(val) - Abs(vector)) < TOL</p><p style="margin-bottom: 0.0001pt;">End Function</p>

______________________

Regards,

Artem Taturevych

Application Engineer at Intercad

http://intercad.com.au/

Tel: +61 2 9454 4444

JG
JG 2012-09-24

Artem,

That function did the trick

Now it identifiy horizontal and vertical

Thanks

JOHN

JP
JP 2014-12-31

How Can you utilize swDisplayDimension.Type2 , as I cannot differentiate between a linear dimension and a diameter. Also returning swDisplayDimension.Type2 gives a value....