Transforming GraphicComponent
This example provides information on transforming graphic components with respect to local reference coordinate system, world reference coordinate system, user reference coordinate system and parent reference coordinate system.
You need a station with a GraphicComponent object for this tutorial.
Solution
First, obtain the initial GraphicComponent from the station, assuming there is at least one available. This step sets the target for subsequent transformations.
// Gets the first GraphicComponent of the station // Assumes there is at least one GraphicComponent in the station GraphicComponent sourceComponent = Station.ActiveStation.GraphicComponents[0];Start by creating Vector3 objects for both translation and orientation. These will determine how the GraphicComponent is transformed. Combine these vectors into a Matrix4, which represents the complete transformation.
// Define new translation and orientation for transforming the GraphicComponent Vector3 newTranslation = new Vector3(1, 1, 1); Vector3 newOrientation = new Vector3(Globals.DegToRad(30), Globals.DegToRad(40), Globals.DegToRad(60)); Matrix4 newMatrix = new Matrix4(newTranslation, newOrientation);Get the current matrix of the GraphicComponent and apply the transformation relative to its local coordinate system.
// Transform GraphicComponent relative to its local coordinate system Matrix4 refLocalMatrix = sourceComponent.Transform.Matrix; sourceComponent.Transform.SetRelativeTransform(refLocalMatrix, newMatrix);Transform the GraphicComponent relative to the world coordinate system by defining a world matrix and applying the transformation.
// Transform GraphicComponent relative to the world coordinate system Matrix4 refWorldMatrix = Matrix4.Identity; sourceComponent.Transform.SetRelativeTransform(refWorldMatrix, newMatrix);Define a matrix based on user-defined translation and orientation, then transform the GraphicComponent accordingly.
// Transform GraphicComponent relative to User reference coordinate Vector3 userDefTranslation = new Vector3(2, 2, 2); Vector3 userDefOrientation = new Vector3(Globals.DegToRad(15), Globals.DegToRad(15), Globals.DegToRad(15)); Matrix4 userDefMatrix = new Matrix4(userDefTranslation, userDefOrientation); sourceComponent.Transform.SetRelativeTransform(userDefMatrix, newMatrix);Finally, transform the GraphicComponent relative to its parent's coordinate system by retrieving the parent's global matrix and applying the transformation.
// Transform GraphicComponent with respect to Parent reference coordinate Matrix4 parentRefMatrix = new Matrix4(userDefTranslation, userDefOrientation); ProjectObject parent = sourceComponent.Parent; if (parent is IHasTransform parentTransform) { parentRefMatrix = parentTransform.Transform.GlobalMatrix; } sourceComponent.Transform.SetRelativeTransform(parentRefMatrix, newMatrix);
Example
This example provides information on transforming graphic components with respect to different reference coordinate system.
// Begin UndoStep
Project.UndoContext.BeginUndoStep("TransformGraphics");
try
{
// Gets the first GraphicComponent of the station
// Assumes there is at least one GraphicComponent in the station
GraphicComponent sourceComponent = Station.ActiveStation.GraphicComponents[0];
// Define new translation and orientation for transforming the GraphicComponent
Vector3 newTranslation = new Vector3(1, 1, 1);
Vector3 newOrientation = new Vector3(Globals.DegToRad(30),
Globals.DegToRad(40),
Globals.DegToRad(60));
Matrix4 newMatrix = new Matrix4(newTranslation, newOrientation);
// Transform GraphicComponent relative to its local coordinate system
Matrix4 refLocalMatrix = sourceComponent.Transform.Matrix;
sourceComponent.Transform.SetRelativeTransform(refLocalMatrix, newMatrix);
// Transform GraphicComponent relative to the world coordinate system
Matrix4 refWorldMatrix = Matrix4.Identity;
sourceComponent.Transform.SetRelativeTransform(refWorldMatrix, newMatrix);
// Transform GraphicComponent relative to User reference coordinate
Vector3 userDefTranslation = new Vector3(2, 2, 2);
Vector3 userDefOrientation = new Vector3(Globals.DegToRad(15),
Globals.DegToRad(15),
Globals.DegToRad(15));
Matrix4 userDefMatrix = new Matrix4(userDefTranslation, userDefOrientation);
sourceComponent.Transform.SetRelativeTransform(userDefMatrix, newMatrix);
// Transform GraphicComponent with respect to Parent reference coordinate
Matrix4 parentRefMatrix = new Matrix4(userDefTranslation, userDefOrientation);
ProjectObject parent = sourceComponent.Parent;
if (parent is IHasTransform parentTransform)
{
parentRefMatrix = parentTransform.Transform.GlobalMatrix;
}
sourceComponent.Transform.SetRelativeTransform(parentRefMatrix, newMatrix);
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}
Required Namespaces
ABB.Robotics.RobotStudio.Stations