Transform Point from Component Space to Assembly Space Example (VBA)
This example shows how to transform a point from component space to
assembly space.
'----------------------------------------
'
' Preconditions:
' (1)
Assembly document is open.
' (2)
Component is selected.
'
' Postconditions: Component's origin is transformed to
a point assembly space.
'
'-----------------------------------------
Option Explicit
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swMathUtil As
SldWorks.MathUtility
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swComp As
SldWorks.Component2
Dim
swXform As
SldWorks.MathTransform
Dim
nPt(2) As
Double
Dim
vPt As
Variant
Dim
swPt As
SldWorks.MathPoint
Dim
i As
Long
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swMathUtil = swApp.GetMathUtility
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swComp = swSelMgr.GetSelectedObjectsComponent(1)
Set
swXform = swComp.Transform2
'
Point at component origin
nPt(0)
= 0#
nPt(1)
= 0#
nPt(2)
= 0#
vPt
= nPt
Set
swPt = swMathUtil.CreatePoint(vPt)
Set
swPt = swPt.MultiplyTransform(swXform)
Debug.Print
"File = " & swModel.GetPathName
Debug.Print
" Comp
= " & swComp.Name2 & " [" & swComp.GetPathName
& "]"
Debug.Print
" Pt
in comp = (" & nPt(0) * 1000# & ", " & nPt(1)
* 1000# & ", " & nPt(2) * 1000# & ") mm"
Debug.Print
" Pt
in assy = (" & swPt.ArrayData(0)
* 1000# & ", " & swPt.ArrayData(1)
* 1000# & ", " & swPt.ArrayData(2)
* 1000# & ") mm"
End Sub