Hide Table of Contents

Get Components in Drawing View Example (VBA)

This example shows how to get the components in a drawing view and how to change their line font styles.

' Preconditions:
' 1. Drawing document opened by the macro exists.
' 2. Drawing view is selected.
' 3. Open the Immediate window.
' Postconditions:
' 1. Specified drawing document is opened.
' 2. Drawing View1 is selected.
' 3. Gets the root and children components for Drawing  
'    View1.
' 4. For each component:
'    a. Prints whether a drawing component is selected, the 
'       name of the component, and the name of the configuration 
'       to the Immediate window.
'    b. Disables the use of the document defaults for the
'       the component's line font style.
'    c. Sets new line style and line thickness for the component's
'       visible edges and prints the new settings and values to
'       the Immediate window.
'    d. Prints the file name of the component to the Immediate window.
Option Explicit
Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swDraw                      As SldWorks.DrawingDoc
    Dim swSelMgr                    As SldWorks.SelectionMgr
    Dim swSelData                   As SldWorks.SelectData
    Dim swModelDocExt               As SldWorks.ModelDocExtension
    Dim swView                      As SldWorks.View
    Dim swRootDrawComp              As SldWorks.DrawingComponent
    Dim vDrawChildCompArr           As Variant
    Dim vDrawChildComp              As Variant
    Dim swDrawComp                  As SldWorks.DrawingComponent
    Dim swComp                      As SldWorks.Component2
    Dim swCompModel                 As SldWorks.ModelDoc2
    Dim assemblyDrawing             As String
    Dim status                      As Boolean
    Dim errors                      As Long
    Dim warnings                    As Long
    Dim lineWeight                  As Long
    Dim lineThickness               As Double
    Set swApp = Application.SldWorks
    assemblyDrawing = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2017\tutorial\driveworksxpress\mobile gantry.slddrw"
    Set swModel = swApp.OpenDoc6(assemblyDrawing, swDocDRAWING, swOpenDocOptions_Silent, "", errors, warnings)
    Set swDraw = swModel
    Set swModelDocExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager
    Set swSelData = swSelMgr.CreateSelectData
    status = swDraw.ActivateView("Drawing View4")
    status = swModelDocExt.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.104008832128, 0.1163870710783, 0, False, 0, Nothing, 0)
    Set swView = swSelMgr.GetSelectedObject6(1, -1)
    Set swRootDrawComp = swView.RootDrawingComponent
    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  View = " & swView.Name
    vDrawChildCompArr = swRootDrawComp.GetChildren
    For Each vDrawChildComp In vDrawChildCompArr
        Set swDrawComp = vDrawChildComp

        ' Drawing component selected?
        Debug.Print " Drawing component selected = " & swDrawComp.Select(True, Nothing)
        ' Returns NULL if underlying model is open in a different configuration
        Set swComp = swDrawComp.Component
        If Not Nothing Is swComp Then
            ' Returns NULL if drawing is lightweight
            Set swCompModel = swComp.GetModelDoc2
            Debug.Print "      Component                            = " & swComp.Name2
            Debug.Print "      Configuration                        = " & swComp.ReferencedConfiguration            
            ' Turn off using document default settings for component's line font style
            swDrawComp.UseDocumentDefaults = False
            Debug.Print "      Default component line font in use   = " & swDrawComp.UseDocumentDefaults
            ' Set new line style for visible edges
            swDrawComp.SetLineStyle swDrawingComponentLineFontVisible, swLineCHAIN
            Debug.Print "        Line style for visible edges                      = " & swDrawComp.GetLineStyle(swDrawingComponentLineFontVisible)
            ' Set new line thickness for visible edges
            swDrawComp.SetLineThickness swDrawingComponentLineFontVisible, swLW_CUSTOM, 0.0003
            lineWeight = swDrawComp.GetLineThickness(swDrawingComponentLineFontVisible, lineThickness)
            Debug.Print "        Line weight style and thickness for visible edges = " & lineWeight & ", " & lineThickness * 1000 & " mm"
            If Not Nothing Is swCompModel Then
                Debug.Print "      File                                 = " & swCompModel.GetPathName
                Debug.Print " "
            End If
        End If
End Sub

Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Get Components in Drawing View Example (VBA)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2017 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.