Use Presentation Transforms to Move Component Example (VBA)
This example shows how to use presentation transforms.
'-----------------------------------------------
'
' To
move an assembly component, you can use
' the
IDragOperator object. This allows you to
' apply
a transform to an assembly component
' or
set of assembly components.
'
' The
IDragOperator object is a powerful and functional
' API.
It has the ability to detect
' collisions
and monitor dynamic clearances.
' This
functionality is the user-interface equivalent of
' interactively
dragging assembly components.
' As
such, this API honors all mates in the
' assembly.
'
' In
some cases, this may be too much functionality.
' For
example, to animate exploding an assembly,
' you
want to set each component transform and
' ignore
all assembly mates.
'
' A
presentation transform is a graphical representation.
' This
blindly applies a transformation to the
' graphical
display of an assembly component. The
' underlying
transform of the model data is not affected.
'
' Preconditions:
' (1)
Assembly is open.
' (2)
Assembly is fully resolved.
' (3)
Component is selected.
'
' Postconditions: Component is moved.
'
' NOTES:
' (1)
When the graphics area is redrawn,
' the
display reverts to how it was at
' the
start, that is, the selected component
' appears
to move back to its original
' position.
This is by design because
' presentation
transforms are disabled
' at
the end of the macro. If presentation transforms
' are
not disabled, then the component would
' remain
in its final position.
' (2)
When presentation transforms are enabled,
' access
to most of the menus is blocked.
' This
is by design because selections cannot
' be
made because the graphics area is not the
' same
as the model data.
'
'-----------------------------------------------
Option Explicit
Sub main()
Const
MaxSteps As
Long = 100
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swAssy As
SldWorks.AssemblyDoc
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swComp As
SldWorks.Component2
Dim
vXform As
Variant
Dim
arr(15) As
Double
Dim
swMathUtil As
SldWorks.MathUtility
Dim
swMathXform As
SldWorks.MathTransform
Dim
i As
Long
Set
swApp = Application.SldWorks
Set
swMathUtil = swApp.GetMathUtility()
Set
swModel = swApp.ActiveDoc
Set
swAssy = swModel
Set
swSelMgr = swModel.SelectionManager
Set
swComp = swSelMgr.GetSelectedObjectsComponent(1)
'
Blocks access to menus
swAssy.EnablePresentation = True
'
Set up unit matrix-no rotation, translation, or scaling
'
Unit rotation matrix
arr(0)
= 1#: arr(1)
= 0#: arr(2)
= 0#
arr(3)
= 0#: arr(4)
= 1#: arr(5)
= 0#
arr(6)
= 0#: arr(7)
= 0#: arr(8)
= 1#
'
No translation
arr(9)
= 0#: arr(10)
= 0#: arr(11)
= 0#
'
Unit scaling
arr(12)
= 1#
'
No used so pad with zeros
arr(13)
= 0#: arr(14)
= 0#: arr(15)
= 0#
For
i = 1 To MaxSteps
'
change rotation
arr(2)
= 1.2 * (i / MaxSteps)
arr(3)
= 1.2 * (i / MaxSteps): arr(4)
= 0.8 * (i / MaxSteps): arr(5)
= 0.9 * (i / MaxSteps)
arr(6)
= 0.8 * (i / MaxSteps)
'
Change translation
arr(9)
= 0.1 * (i / MaxSteps): arr(10)
= 0.2 * (i / MaxSteps)
'
Should really check that matrix is valid,
'
especially rotation matrix
vXform
= arr
Set
swMathXform = swMathUtil.CreateTransform((vXform))
swComp.RemovePresentationTransform
swComp.PresentationTransform = swMathXform
'
Redraw so it is shown immediately
swModel.GraphicsRedraw2
Next
i
'
Re-enable access to menus
'
comment out to leave component in its final position
swAssy.EnablePresentation = False
End Sub
'------------------------------------------