Transform part relative to UCS

Hello,

I want to position a part relative to an UCS in VBA. I Do the folowing:

Sets UCS

Define the Transformation

Then I would like to use The WCSToUCS, but this gives me troubles.

Regards

Michael

Hi Michael

Here comes a example how to position relative

This example will set part p1 to ucs and then position part p2 relativ ucs 100mm in X and 100 mm in Y

Sub RelativeUCS()
Dim p1 As Part
Dim p2 As Part
Dim t As New Transform

t.x = 0.1
t.y = 0.1
t.z = 0

t.Rx = 0
t.Ry = 0
t.Rz = 0

Set p1 = ActiveStation.Parts(1)
Set p2 = ActiveStation.Parts(2)

ActiveStation.UCS = p1

p2.Transform = UCSToWCS(t)

End Sub

Or here is another example:

Sub RelativeMove()

Dim tfmT As New Transform
tfmT.x = 0
tfmT.y = 0
tfmT.z = 1
tfmT.Rx = 0
tfmT.Ry = 0.785398163397448 '45?
tfmT.Rz = 0.785398163397448 '45?

Dim prt1 As Part
Set prt1 = ActiveStation.Parts.Add
prt1.Transform = tfmT
prt1.ShowCoordinateSystem = True 'displays the parts coord in the gfx
MsgBox prt1.Name & " created."

Dim prt2 As Part
Set prt2 = ActiveStation.Parts.Add
prt2.ShowCoordinateSystem = True 'displays the parts coord in the gfx
MsgBox prt2.Name & " created."

ActiveStation.UCS = prt1
MsgBox prt1.Name & " set as UCS."

tfmT.x = 0
tfmT.y = 0
tfmT.z = 0.5
tfmT.Rx = 0
tfmT.Ry = 0
tfmT.Rz = 0

prt2.Transform = UCSToWCS(tfmT)
MsgBox prt2.Name & " placed 500 mm relative to direction of " & prt1.Name & “.”

MsgBox “Cleanup.”
ActiveStation.UCS = ActiveStation 'resets the UCS
prt1.Delete
prt2.Delete
MsgBox “Cleanup finished.”

End Sub

:stuck_out_tongue_winking_eye:

Another example that use the RobotStudio Math Util (no need to set UCS). You need to add a reference to the RobotStudio Math util (tools eferences)

This example only handles parts , but I think you get the idea, and it moves part2(p2) 100mm in part1(p1) x direction.

If you have for example a lot of stuff in your station using the Math Util is much more faster than setting the UCS all the time.

Sub MoveRelativeTo(MoveObject As Part, RelativeObject As Part, DirectionValues As Variant)
Dim tmpTransform As Transform
Dim tmpVector As RsMathVector
Dim tmpMatrix As RsMathMatrix

On Error Resume Next

Set tmpTransform = RelativeObject.Transform

tmpMatrix = RsMath_TransformToMatrix(tmpTransform)

tmpVector.x = UnitToAPI(rsQuantityLength, DirectionValues(0))
tmpVector.y = UnitToAPI(rsQuantityLength, DirectionValues(1))
tmpVector.z = UnitToAPI(rsQuantityLength, DirectionValues(2))

tmpMatrix.pos = RsMath_mult_mat_vec(tmpMatrix, tmpVector)

MoveObject.Transform.Position = RsMath_MatrixToTransform(tmpMatrix).Position

ActiveStation.Refresh

End Sub

Sub TestRelative()
Dim p1 As Part
Dim p2 As Part
Dim Values(0 To 2) As Variant

'x,y,z directions
Values(0) = 100
Values(1) = 0
Values(2) = 0

Set p1 = ActiveStation.Parts(1)
Set p2 = ActiveStation.Parts(2)

MoveRelativeTo p2, p1, Values
End Sub