Traverse Annotations Example (VBA)
This example shows how to visit all display dimension annotations in
a selected sketch in an open model by using the IFeature::GetFirstDisplayDimension
and IFeature::GetNextDisplayDimension methods.
First open a SolidWorks model, and select a sketch containing one or
more Dimension objects. Use ISelectionMgr to access the selected sketch,
and call IFeature::GetFirstDisplayDimension to obtain the first IDisplayDimension
in the sketch. Then enter a loop that iterates while IDisplayDimension
is not Nothing.
Inside the loop, retrieve the annotation that contains the IDisplayDimension
with a call to IDisplayDimension::GetAnnotation, and get the position
of the annotation by calling IAnnotation::GetPosition. The VARIANT array
returned by IAnnotation::GetPosition can be modified and the position
of the annotation can be updated to reflect this change with a call to
IAnnotation::SetPosition. The next IDisplayDimension in the loop is obtained
with a call to IFeature::GetNextDisplayDimension.
The code to move all IDisplayDimension annotations 100 mm to the right
(in the x dimension) is as follows.
'---------------------------------------------
Option Explicit
Dim
swApp As SldWorks.SldWorks
Dim
Model As SldWorks.ModelDoc2
Dim
selMgr As SldWorks.SelectionMgr
Dim
CurAnnotation As SldWorks.Annotation
Dim
AnnotationPosition As Variant
Dim
CurFeature As SldWorks.feature
Dim
DispDim As SldWorks.DisplayDimension
Sub main()
'Attach
the program to SolidWorks application
Set
swApp = CreateObject("SldWorks.Application")
'Get
the model
Set
Model = swApp.ActiveDoc
Set
selMgr = Model.SelectionManager
'
Get the selected sketch
Set
CurFeature = selMgr.GetSelectedObject5(1)
'Get
the first DisplayDimension in the selected sketch
Set
DispDim = CurFeature.GetFirstDisplayDimension
'Iterate
through all of the display dimensions in the sketch
Do
While Not DispDim Is Nothing
'Retrieve
the annotation from display dimension
Set
CurAnnotation = DispDim.GetAnnotation
'Get
the position of the annotation
AnnotationPosition
= CurAnnotation.GetPosition
'If
there is no problem obtaining the position
If
Not IsNull(AnnotationPosition) Then
'Move
the annotation to the right
CurAnnotation.SetPosition AnnotationPosition(0) +
0.1, AnnotationPosition(1), AnnotationPosition(2)
End
If
'Get
the next display dimension
Set
DispDim = CurFeature.GetNextDisplayDimension(DispDim)
Loop
End Sub