Files
Insert components at a specific location using transforms
Code
'Preconditions: have transform_test.sldasm open; specify a point on the pin (relative to the pin _
origin) and a point on the block (relative to the block origin)
'Postconditions: specific point of transform_test_pin.sldprt should be equal to specific point of _
transform_test_block.sldprt and the pin rotated in line with the block
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swMathUtil As SldWorks.MathUtility
Dim swBlockMathPt As SldWorks.MathPoint
Dim swPinMathPt As SldWorks.MathPoint
Dim swBlockTransform As SldWorks.MathTransform
Dim swPinTransform As SldWorks.MathTransform
Dim dblBlockPtParams(2) As Double
Dim dblPinPtParams(2) As Double
Dim vArrayData As Variant
Dim vBlockTransformData As Variant
Dim vPinTransformData As Variant
Dim i As Integer
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
Set swMathUtil = swApp.GetMathUtility
'1. State the desired location relative to the pin origin
dblPinPtParams(0) = 0
dblPinPtParams(1) = 0.01
dblPinPtParams(2) = 0
'2. State the desired location relative to the block origin (the following coordinates _
are for the block hole center)
dblBlockPtParams(0) = 0.025
dblBlockPtParams(1) = 0
dblBlockPtParams(2) = -0.025
'3. Modify param data to accompany other specified pt; IMPORTANT: the below operation needs _
to be addition or subtraction depending on whether the coordinate systems match. As an _
example, test with the pin extrude feature flipped and see the result.
dblBlockPtParams(0) = dblBlockPtParams(0) + dblPinPtParams(0)
dblBlockPtParams(1) = dblBlockPtParams(1) + dblPinPtParams(1)
dblBlockPtParams(2) = dblBlockPtParams(2) + dblPinPtParams(2)
'4. Turn into math point
Set swBlockMathPt = swMathUtil.CreatePoint(dblBlockPtParams)
'5. Get assy-centric math point
Set swComp = swAssy.GetComponentByName("transform_test_block-1")
Set swBlockTransform = swComp.Transform2
Set swBlockMathPt = swBlockMathPt.MultiplyTransform(swBlockTransform)
'6. Get array data from math point
vArrayData = swBlockMathPt.ArrayData
'7. Get pin transform
Set swComp = swAssy.GetComponentByName("transform_test_pin-1")
Set swPinTransform = swComp.Transform2
'8. Modify pin transform so that location data matches math point data from step 3
vPinTransformData = swPinTransform.ArrayData
For i = 9 To 11
vPinTransformData(i) = vArrayData(i - 9)
Next i
'9. Modify pin transform so that rotation data matches block transform rotation data
vBlockTransformData = swBlockTransform.ArrayData
For i = 0 To 8
vPinTransformData(i) = vBlockTransformData(i)
Next i
'10. Create a transform from the data in steps 7 and 8 and apply it to the pin
Set swBlockTransform = swMathUtil.CreateTransform(vPinTransformData)
swComp.Transform2 = swBlockTransform
swModel.ForceRebuild3 False
End Sub
'Other:
'The assy-centric location of the hole center could be obtained using IMathPoint::ArrayData
'You can align a comp to the assy origin by settings swComp.Transform2 equal to the transform you _
got from swMathUtil.CreateTransform(Nothing). It appears that "Nothing" returns the transform _
of the assy
'A presentation transform is purely a graphical representation. The underlying transform _
is not affected; hence this would be used with an explode.