Autodimension a Sketch Example (VBA)
This example shows how to autodimension a sketch.
'----------------------------------------------------
' Preconditions:
' (1)
Part or assembly is open.
' (2)
Sketch is being edited.
' (3)
Sketch contains some sketch segments or geometry.
' (4)
At least two sketch entities are selected.
' (5)
First selected sketch entity is used for horizontal datum.
' (6)
Second selected sketch entity is used for vertical datum.
'
' Postconditions: If successful, then dimensions are added
to sketch.
'
' NOTES: Return code from ISketch::AutoDimension2 is output
to
' debug window and you should examine the code in the
case of failure.
'
'----------------------------------------------------
Option Explicit
Public Enum swAutodimEntities_e
swAutodimEntitiesAll
= 1
swAutodimEntitiesSelected
= 2
End Enum
Public Enum swAutodimMark_e
swAutodimMarkEntities
= &H1
swAutodimMarkHorizontalDatum
= &H2
swAutodimMarkVerticalDatum
= &H4
End Enum
Public Enum swAutodimScheme_e
swAutodimSchemeBaseline
= 1
swAutodimSchemeOrdinate
= 2
swAutodimSchemeChain
= 3
swAutodimSchemeCenterline
= 4
End Enum
Public Enum swAutodimHorizontalPlacement_e
swAutodimHorizontalPlacementBelow
= -1
swAutodimHorizontalPlacementAbove
= 1
End Enum
Public Enum swAutodimVerticalPlacement_e
swAutodimVerticalPlacementLeft
= -1
swAutodimVerticalPlacementRight
= 1
End Enum
Public Enum swAutodimStatus_e
swAutodimStatusSuccess
= 0
swAutodimStatusBadOptionValue
= 1
swAutodimStatusNoActiveDoc
= 2
swAutodimStatusDocTypeNotSupported
= 3
swAutodimStatusNoActiveSketch
= 4
swAutodimStatus3DSketchNotSupported
= 5
swAutodimStatusSketchIsEmpty
= 6
swAutodimStatusSketchIsOverDefined
= 7
swAutodimStatusNoEntities
= 8
swAutodimStatusEntitiesNotValid
= 9
swAutodimStatusCenterlineNotAllowed
= 10
swAutodimStatusDatumNotSupplied
= 11
swAutodimStatusDatumNotUnique
= 12
swAutodimStatusDatumNotValidType
= 13
swAutodimStatusDatumLineNotCenterline
= 14
swAutodimStatusDatumLineNotVertical
= 15
swAutodimStatusDatumLineNotHorizontal
= 16
swAutodimStatusAlgorithmFailed
= 17
End Enum
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swSketch As
SldWorks.sketch
Dim
swSketchSegHoriz As
SldWorks.SketchSegment
Dim
swSketchSegVert As
SldWorks.SketchSegment
Dim
nRetVal As
Long
Dim
i As
Long
Dim
bRet As
Boolean
Set
swApp = CreateObject("SldWorks.Application")
Set
swModel = swApp.ActiveDoc
Set
swSketch = swModel.GetActiveSketch2
Set
swSelMgr = swModel.SelectionManager
Set
swSketchSegHoriz = swSelMgr.GetSelectedObject5(1)
Set
swSketchSegVert = swSelMgr.GetSelectedObject5(2)
swModel.ClearSelection2 True
'
Reselect sketch segments with correct marks
'
for auto-dimensioning
bRet
= swSketchSegHoriz.Select3(True,
swAutodimMarkHorizontalDatum, Nothing)
bRet
= swSketchSegVert.Select3(True,
swAutodimMarkVerticalDatum, Nothing)
nRetVal
= swSketch.AutoDimension2( _
swAutodimEntitiesAll,
_
swAutodimSchemeBaseline,
_
swAutodimHorizontalPlacementBelow,
_
swAutodimSchemeBaseline,
_
swAutodimVerticalPlacementLeft)
Debug.Print
" AutoDim
= " & nRetVal
'
Redraw so dimensions are displayed immediately
swModel.GraphicsRedraw2
End Sub