Temporary Graphics
Temporary Graphic objects are used when there is a need to insert volatile 3D objects into a station. When the station is saved, the persistence of these objects is merely temporary, hence their name.
This example provides information on how to add a Temporary Graphic to your station. We create a box, and make a copy
of it using a method from the Temporary
Last part of the example shows how to create a cylinder with the TemporaryGraphic class, and there are many more geometries that can be created this way.
The text labels over the boxes are also created with TemporaryGraphic. TemporaryGraphic objects created are only visible in the graphical view. They do not appear in the browser and they cannot be selected.
Note
You can easily try out this example using the RobotStudio Empty Add-in template from Visual Studio.
The steps that will be performed in this topic are:
Create a solid box.
Add a temporary text for the user.
Copy the box and another text.
Delete the box.
Create a temporary cylinder.
Solution
First we will start by creating a solid box with its origin centered in the station.
Station station = Project.ActiveProject as Station; // Create a box and add it to our station. Part part = new Part(); Body box = Body.CreateSolidBox(new Matrix4(new Vector3(0, 0, 0)), new Vector3(0.5, 0.5, 0.5)); part.Bodies.Add(box); station.GraphicComponents.Add(part);
Add a temporary graphic that contains some text telling the user about the recently created box.
TemporaryGraphic tgTextOverBox = station.TemporaryGraphics.DrawText(new Vector3(0, 0, 0.6), "The box.");
We will now create two temporary graphics: One contains a copy of the Box, using the Draw
Part(Matrix4, Part, double) method of the class, and the other one contains another display text.// Create a copy of the box as a TemporaryGraphics. Set the opacity of the copy to 0.5. TemporaryGraphic tgCopyOfBox = station.TemporaryGraphics.DrawPart(new Matrix4(new Vector3(1, 1, 0)), part, 0.5); // Add a text to the copy of the box. TemporaryGraphic tgTextOverCopyOfBox = station.TemporaryGraphics.DrawText(new Vector3(1, 1, 0.6), "TemporaryGraphics copy of the box, with opacity = 0.5");
One of the main advantages of using temporary graphics is how easy they can be removed Create TemporaryGraphic and remove it.
// Create another copy of the box as a TemporaryGraphics. TemporaryGraphic tgRemovedBox = station.TemporaryGraphics.DrawPart(new Matrix4(new Vector3(0, 1, 0)), part); // Remove the TemporaryGraphics copy. station.TemporaryGraphics.Remove(tgRemovedBox); // Add a text where the removed box was. TemporaryGraphic tgTextOverRemovedBox = station.TemporaryGraphics.DrawText (new Vector3(0, 1, 0.6), "The deleted TemporaryGraphics box was here.");
Draw
Cylinder(Matrix4, double, double, Color) , another member of the TemporaryGraphic class, allows the developer to draw a cylinder. Many other 3D objects can be created as a temporary graphic.// Create a TemporaryGraphics cylinder. TemporaryGraphic tgCylinder = station.TemporaryGraphics.DrawCylinder (new Matrix4(new Vector3(-1, -1, 0)), 0.3, 1, Color.Gold);
Example
The complete code example is shown below:
private static void TemporaryGraphics()
{
Project.UndoContext.BeginUndoStep("TemporaryGraphics");
try
{
Station station = Project.ActiveProject as Station;
// Create a box and add it to our station.
Part part = new Part();
Body box = Body.CreateSolidBox(new Matrix4(new Vector3(0, 0, 0)), new Vector3(0.5, 0.5, 0.5));
part.Bodies.Add(box);
station.GraphicComponents.Add(part);
// Create a text displaying that this is the box.
TemporaryGraphic tgTextOverBox = station.TemporaryGraphics.DrawText(new Vector3(0, 0, 0.6), "The box.");
// Create a copy of the box as a TemporaryGraphics. Set the opacity of the copy to 0.5.
TemporaryGraphic tgCopyOfBox = station.TemporaryGraphics.DrawPart(new Matrix4(new Vector3(1, 1, 0)), part, 0.5);
// Add a text to the copy of the box.
TemporaryGraphic tgTextOverCopyOfBox = station.TemporaryGraphics.DrawText
(new Vector3(1, 1, 0.6), "TemporaryGraphics copy of the box, with opacity = 0.5");
// Get the temporary graphics bounding box.
Vector3 bbMin, bbMax;
tgCopyOfBox.GetBoundingBox(true, out bbMin, out bbMax);
// Highlights the temporary graphic in the specified color.
tgCopyOfBox.Highlight(true, Color.Green);
// Get a copy of the temporary graphic.
TemporaryGraphic copyOfTempGraph = tgCopyOfBox.Copy();
// Create another copy of the box as a TemporaryGraphics.
TemporaryGraphic tgRemovedBox = station.TemporaryGraphics.DrawPart(new Matrix4(new Vector3(0, 1, 0)), part);
// Remove the TemporaryGraphics copy.
station.TemporaryGraphics.Remove(tgRemovedBox);
// Add a text where the removed box was.
TemporaryGraphic tgTextOverRemovedBox = station.TemporaryGraphics.DrawText
(new Vector3(0, 1, 0.6), "The deleted TemporaryGraphics box was here.");
// Create a TemporaryGraphics cylinder.
TemporaryGraphic tgCylinder = station.TemporaryGraphics.DrawCylinder
(new Matrix4(new Vector3(-1, -1, 0)), 0.3, 1, Color.Gold);
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}
}
Required Namespaces
ABB.
ABB.