Part Information
This example provides information on creating parts, and their properties.
Consider the following scenario. Create a Part , containing two bodies, a box and a cylinder. The color of the part is set to orange, and moved a bit. Then the part is copied, and myPart2 is created. myPart2 is added to a library, which is saved on your computer.
Then myPart3 is created, which is a new instance of myPart1. The example removes the geometry from myPart3, which removes the geometry from myPart1, since they use the same definition. The example prints some information about myPart2 in the logger (the output window in RobotStudio).
Solution
Use this procedure to cover the scenario mentioned above.
Create a part myPart1.
Part myPart1 = new Part(); myPart1.Name = "MyPart_1";
Add the part to the graphics components collection of the station.
station.GraphicComponents.Add(myPart1);
Create a box and add it to myPart1.
Matrix4 box_origin = new Matrix4(Vector3.XVector, 0.0); Vector3 box_size = new Vector3(0.1, 0.1, 0.1); Body box = Body.CreateSolidBox(box_origin, box_size); box.Name = "MyBox"; myPart1.Bodies.Add(box);
Create a cylinder and add it to myPart1.
Matrix4 origin = new Matrix4(new Vector3(Axis.X), 0.0); double radius = 0.2; double height = 1.0; Body myCylinder = Body.CreateSolidCylinder(origin, radius, height); myCylinder.Name = "MyCylinder"; myPart1.Bodies.Add(myCylinder);
Make all the geometries in myPart orange.
myPart1.Color = Color.Orange;
Make myPart1 visible in graphical view.
myPart1.Visible = true;
Move myPart1 and all the bodies in it 100 mm along the X-axis and 100 mm along the Y-axis.
myPart1.Transform.X = myPart1.Transform.X + 0.1; myPart1.Transform.Y = myPart1.Transform.Y + 0.1;
Create a copy of myPart1.
Part myPart2 = (Part)myPart1.Copy(); myPart2.Name = "MyPart_2";
Add myPart2 to the station.
station.GraphicComponents.Add(myPart2);
Create Graphic
Component and save a copy of the library.Library GraphicComponentLibrary myLib = myPart2.MoveDefinitionToLibrary(); Logger.AddMessage( new LogMessage("The RootComponent of the Lib is: " + myLib.RootComponent.Name)); // Save a copy of the lib. // Get the path to the UserProjectsFolder. string userProjPath = (string)Options.GetValue("RobotStudio", "Directories.UserProjects"); if (userProjPath != null) { myLib.SaveAs(userProjPath + "\\Libraries\\myLib.rslib"); } else { // If there is no UserprojPath, save it in user documents. myLib.SaveAs(Path.Combine (System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "myLib.rslib")); }
Create a new instance of myPart say myPart3.
Part myPart3 = (Part)myPart1.CopyInstance();
Delete the geometry of myPart3, this causes the geometry of myPart1 to be deleted to, since they use the same definition.
myPart3.DeleteGeometry();
Disconnect myPart2 from its library.
myPart2.DisconnectFromLibrary();
Get the normal of myPart2.
BoundingBox bbox = myPart2.GetBoundingBox(true); Vector3 firstCorner = bbox.min; Vector3 secondCorner = bbox.max; Logger.AddMessage (new LogMessage("The first corner of the bounding box is ( " + firstCorner.x + "; " + firstCorner.y + "; " + firstCorner.z + ")")); Logger.AddMessage (new LogMessage("The second corner of the bounding box is ( " + secondCorner.x + "; " + secondCorner.y + "; " + secondCorner.z + ")")); Vector3 testPoint = new Vector3(0.0, 0.0, 0.0); Vector3 hitPoint; Vector3 hitPointNormal; Face hitFace; myPart2.GetNormalToSurface(testPoint, out hitPoint, out hitPointNormal, out hitFace);
Example
This example provides information on creating parts, and their properties.
public void PartProperties()
{
NewStation();
Project.UndoContext.BeginUndoStep("PartProperties");
try
{
Station station = Station.ActiveStation;
// Create a part.
Part myPart1 = new Part();
myPart1.Name = "MyPart_1";
// Add the part to the graphics componenets collection of the station.
station.GraphicComponents.Add(myPart1);
// Create a box and add it to myPart.
Matrix4 box_origin = new Matrix4(Vector3.XVector, 0.0);
Vector3 box_size = new Vector3(0.1, 0.1, 0.1);
Body box = Body.CreateSolidBox(box_origin, box_size);
box.Name = "MyBox";
myPart1.Bodies.Add(box);
// Create a cylinder and add it to myPart.
Matrix4 origin = new Matrix4(new Vector3(Axis.X), 0.0);
double radius = 0.2;
double height = 1.0;
Body myCylinder = Body.CreateSolidCylinder(origin, radius, height);
myCylinder.Name = "MyCylinder";
myPart1.Bodies.Add(myCylinder);
// Make all the geometries in myPart orange.
// When using VSTA, change to these lines instead:
// Byte R1 = 255; Byte G1 = 165; Byte B1 = 0;
// VSTABridge.SetColor(myPart, R1, G1, B1);
myPart1.Color = Color.Orange;
myPart1.Visible = true;
myPart1.PickingEnabled = true;
// Move myPart and all the bodies in it 100 mm along the
//X-axis and 100 mm along the Y-axis.
myPart1.Transform.X = myPart1.Transform.X + 0.1;
myPart1.Transform.Y = myPart1.Transform.Y + 0.1;
// Create a copy of myPart.
Part myPart2 = (Part)myPart1.Copy();
myPart2.Name = "MyPart_2";
// Add myPart2 to the station.
station.GraphicComponents.Add(myPart2);
GraphicComponentLibrary myLib = myPart2.MoveDefinitionToLibrary();
Logger.AddMessage(
new LogMessage("The RootComponent of the Lib is: " + myLib.RootComponent.Name));
// Save a copy of the lib.
// Get the path to the UserProjectsFolder.
string userProjPath =
(string)Options.GetValue("RobotStudio", "Directories.UserProjects");
if (userProjPath != null)
{
myLib.SaveAs(userProjPath + "\\Libraries\\myLib.rslib");
}
else
{
// If there is no UserprojPath, save it in user documents.
myLib.SaveAs(Path.Combine
(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "myLib.rslib"));
}
// Create a new instance of myPart.
Part myPart3 = (Part)myPart1.CopyInstance();
// Add myPart3 to the station.
station.GraphicComponents.Add(myPart3);
// Delete the geometry of myPart3, this causes
// the geometry of myPart1 to be deleted to,
// since they use the same definition.
myPart3.DeleteGeometry();
// Disconnect myPart2 from its library.
myPart2.DisconnectFromLibrary();
// Close the library
myLib.Close();
// Get the normal of myPart2
BoundingBox bbox = myPart2.GetBoundingBox(true);
Vector3 firstCorner = bbox.min;
Vector3 secondCorner = bbox.max;
Logger.AddMessage
(new LogMessage("The first corner of the bounding box is ( "
+ firstCorner.x + "; " + firstCorner.y + "; " + firstCorner.z + ")"));
Logger.AddMessage
(new LogMessage("The second corner of the bounding box is ( "
+ secondCorner.x + "; " + secondCorner.y + "; " + secondCorner.z + ")"));
Vector3 testPoint = new Vector3(0.0, 0.0, 0.0);
Vector3 hitPoint;
Vector3 hitPointNormal;
Face hitFace;
myPart2.GetNormalToSurface(testPoint, out hitPoint, out hitPointNormal, out hitFace);
// Make the hitFace green.
// When using VSTA, change to these lines instead:
// Byte R = 0; Byte G = 255; Byte B = 0;
// VSTABridge.SetColor(hitFace, R, G, B);
hitFace.Color = Color.Green;
Logger.AddMessage
(new LogMessage("The hit point from GetNormalToSurface at test point (0,0,0) is: ( "
+ hitPoint.x + "; " + hitPoint.y + "; " + hitPoint.z + ")"));
Logger.AddMessage
(new LogMessage("The hit point normal from GetNormalToSurface at test point (0,0,0) is: ( "
+ hitPointNormal.x + "; " + hitPointNormal.y + "; " + hitPointNormal.z + ")"));
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}
}
Required Namespaces
ABB.