Select Entity in Drawing View Example (VBA)
This example shows how to select any of these entities in a drawing
view: model face, edge, or vertex.
'----------------------------------------------------------------------------
' Problem:
' Selection of a model geometry in the
context of a drawing
' view can be quite problematic. To address
this, use
' IView::SelectEntity
'
' Thus, given an entity in the context of
the model, this
' method selects the entity in the context
of the drawing
' view.
'
' This code shows how to use this method to
assist in
' adding a dimension to a drawing view.
'
' Preconditions:
' 1. Part or assembly is open.
' 2. Assembly is fully resolved.
' 3. Specified template exists.
' 4. Face, edge or vertex is selected.
'
' Postconditions:
' 1. New drawing is created with three views.
' 2. If possible, the previously selected face, edge or vertex
' is dimensioned in the first drawing view.
'
' NOTE: The dimension is not guaranteed to be created if, for
' example, a face is selected.
'----------------------------------------------------------------------------
Option Explicit
Sub main()
Const sPathToTemplate
As String = "C:\Program Files\SolidWorks\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)
Set swDraw = swApp.NewDrawing2(swDwgTemplateCustom,
sPathToTemplate, swDwgPaperA1size, 0#, 0#)
Set swDrawModel = swDraw
bRet = swDraw.Create3rdAngleViews2(swModel.GetPathName)
Debug.Assert bRet
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView
bRet = swView.SelectEntity(swEnt,
False)
Debug.Assert bRet
' 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
' This depends on the orientation of the
entity in the drawing view.
' Thus, could be NULL.
'
' Will also create the dimension even if the entity is not
' visible in the drawing view
Set swDispDim = swDrawModel.AddDimension2(nXpos, nYpos,
0#)
End Sub