RobotStudio event

Alignment of targets created by vision (Dispensing)

Hello guys :

I'm doing a dispensing application. And all targets are created by 3D vision system, now robot could follow the path correctly.
But now I want robot could run this path smoothly, and found that the alignment function in RobotStudio is very helpful and it saves the time for robot to rewind and make the path smoothly.

I guess the mechanism of alignment is to rotate Z-asix(Z is the direction of the gun, tool coordination) and find the angle that maximum projection of X'(X-axis after rotated) occurred on X(original X-axis of target).
Now comes the problem : Could I use some RAPID instructions(or math calculation) to do this? Because all targets are created by vision system from the customer, I want the alignment could be done automatically.

Do you have any idea on this?.

Thank you!
BR/ Jerry