Select Entity in Drawing View Example (VB.NET)
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.
'----------------------------------------------------------------------------
Imports
SolidWorks.Interop.sldworks
Imports
SolidWorks.Interop.swconst
Imports
System.Runtime.InteropServices
Imports
System
Partial
Class
SolidWorksMacro
Sub
main()
Const
sPathToTemplate As
String =
"C:\Program Files\SolidWorks\data\templates\drawing.drwdot"
Const
nYoffset As
Double =
0.01
Dim
swModel As
ModelDoc2
Dim
swSelMgr As
SelectionMgr
Dim
swEnt As
Entity
Dim
swDraw As
DrawingDoc
Dim
swDrawModel As
ModelDoc2
Dim
swView As
View
Dim
vOutline As
Object
Dim
swDispDim As
DisplayDimension
Dim
nXpos As
Double
Dim
nYpos As
Double
Dim
bRet As
Boolean
swModel = swApp.ActiveDoc
swSelMgr = swModel.SelectionManager
swEnt = swSelMgr.GetSelectedObject6(1, -1)
swDraw = swApp.NewDrawing2(swDwgTemplates_e.swDwgTemplateCustom,
sPathToTemplate, swDwgPaperSizes_e.swDwgPaperA1size, 0.0#, 0.0#)
swDrawModel = swDraw
bRet = swDraw.Create3rdAngleViews2(swModel.GetPathName)
swView = swDraw.GetFirstView
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.0#
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
swDispDim = swDrawModel.AddDimension2(nXpos,
nYpos, 0.0#)
End
Sub
Public
swApp As
SldWorks
End
Class