Hide Table of Contents

Select Entity in Drawing View Example (VBA)

This example shows how to select a model face, edge, or vertex in a drawing view and dimension it.

'----------------------------------------------------------------------------
' Preconditions:
' 1. Open a part or fully resolved assembly.
' 2. Verify that the specified template exists.
' 3. Select a face, edge, or vertex.
'
' Postconditions:
' 1. Creates a new drawing with three views.
' 2. Dimensions the selected face, edge, or vertex
'    in the first drawing view.
' 3. Examine the drawing.
'
' NOTE: The dimension is not guaranteed to be created if a face is selected.
'----------------------------------------------------------------------------
Option Explicit

Sub main()

    Const sPathToTemplate       As String = "C:\ProgramData\SolidWorks\SOLIDWORKS 2016\data\templates\drawing.drwdot"
    Const nYoffset              As Double = 0.01

    Dim swApp                   As SldWorks.SldWorks
    Dim swModel                 As SldWorks.ModelDoc2
    Dim swSelMgr                As SldWorks.SelectionMgr
    Dim swEnt                   As SldWorks.Entity

    Dim swDraw                  As SldWorks.DrawingDoc
    Dim swDrawModel             As SldWorks.ModelDoc2
    Dim swView                  As SldWorks.View
    Dim vOutline                As Variant
    Dim swDispDim               As SldWorks.DisplayDimension

    Dim nXpos                   As Double
    Dim nYpos                   As Double

    Dim bRet                    As Boolean

    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swEnt = swSelMgr.GetSelectedObject6(1, -1)
   

    If Not swEnt Is Nothing Then

        Set swDraw = swApp.NewDrawing2(swDwgTemplateCustom, sPathToTemplate, swDwgPaperA1size, 0#, 0#)
        Set swDrawModel = swDraw
   

        bRet = swDraw.Create3rdAngleViews2(swModel.GetPathName)
   

        Set swView = swDraw.GetFirstView
        Set swView = swView.GetNextView
   

        bRet = swView.SelectEntity(swEnt, False)
   

        ' Work out where to place dimension -
        ' midway across view and slightly above
        vOutline = swView.GetOutline
        nXpos = (vOutline(0) + vOutline(2)) / 2#
        nYpos = vOutline(3) + nYoffset
   

        ' Create the dimension, even if the entity is not
        ' visible in the drawing view
        Set swDispDim = swDrawModel.Extension.AddDimension(nXpos, nYpos, 0#, swSmartDimensionDirection_Left)
       

    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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Select Entity in Drawing View Example (VBA)
*Comment:  
*   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:

x

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
x

Web Help Content Version: API Help (English only) 2016 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.