Show / Hide Table of Contents

GraphicComponentLibrary Properties

This example shows you how to save and load a GraphicComponentLibrary. It uses the Options class to get the UserProjects directory to store the library in. If that is not available, it stores in on c:\ .

Example

Project.UndoContext.BeginUndoStep("GraphicComponentLibraryProperties");
try
{
    Station station = Station.ActiveStation;

    // Create a part.
    Part myPart = new Part();
    myPart.Name = "MyPart";

    // Add the part to the station.
    station.GraphicComponents.Add(myPart);

    // Create a box and add it to myPart.
    Body box = Body.CreateSolidBox(new Matrix4(Vector3.XVector, 0.0), new Vector3(0.1, 0.1, 0.1));
    box.Name = "MyBox";
    myPart.Bodies.Add(box);

    // Instance the library.
    GraphicComponentLibrary myLib = myPart.MoveDefinitionToLibrary();
    Logger.AddMessage(new LogMessage($"The RootComponent of the Lib is: {myLib.RootComponent.Name}"));

    // Get the path to the UserProjectsFolder.
    string userProjPath = (string)Options.GetValue("RobotStudio", "Directories.UserDocuments");

    // Save a copy of the lib.
    if (userProjPath != null)
    {
        myLib.SaveAs(userProjPath + "\\Libraries\\myBoxLib.rslib");
    }
    else
    {
        // If there is no userProjPath, save the library in the temporary directory
        myLib.SaveAs(Path.Combine(Path.GetTempPath(), "myBoxLib.rslib"));
    }

    // Close the library
    myLib.Close();
    myLib = null;

    // Load the library from the file, in readOnly mode.
    if (userProjPath != null)
    {
        myLib = GraphicComponentLibrary.Load(userProjPath + "\\Libraries\\myBoxLib.rslib", true);
    }
    else
    {
        myLib = GraphicComponentLibrary.Load(Path.Combine(Path.GetTempPath(), "myBoxLib.rslib"), true);
    }

    // Add an instance copy of the root component to the station.
    var newInstance = myLib.RootComponent.CopyInstance();
    station.GraphicComponents.Add(newInstance);

    // Disconnect the new instance from the library.
    newInstance.DisconnectFromLibrary();
    // Close the library
    myLib.Close();
}
catch
{
    Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
    throw;
}
finally
{
    Project.UndoContext.EndUndoStep();
}

See Also

  • GraphicComponentLibrary
  • Options
  • Part
  • Body
In this article
Back to top Copyright © 2025 ABB