Diagram / Diagram DOM / Drawings / Pages / Layers
In This Topic
    Layers
    In This Topic

    Layers provide a powerful way to organize and manage the visibility and editing of different page items within a page. They allow you to segregate page items into different categories, which is particularly useful for handling complex diagrams. Each layer can be independently shown or hidden and locked or unlocked, giving you granular control over the page items you want to work with and enabling you to focus on specific parts of your page without affecting others.

     Creating Layers

    Layers are represented by the NLayer class. The layers of a page are contained inside the NPage Layers collections. One thing to have in mind is that you cannot add layers without a name inside this collection and also the name of the layer must be unique within the collection. The following code example creates two layers and adds them in the drawing active page:

    Creating Layers
    Copy Code
    // create redLayer
    NLayer redLayer = new NLayer();
    redLayer.Color = NColor.Red;
    redLayer.Name = "Red";
    drawing.ActivePage.Layers.Add(redLayer);
    
    // create blueLayer
    NLayer blueLayer = new NLayer();
    blueLayer.Name = "Blue";
    blueLayer.Color = NColor.Blue;
    drawing.ActivePage.Layers.Add(blueLayer);
    
     Assigning Page Items To Layers

    Each page item can be belong to multiple layers. Because of that, layers do not control the Z-Order of the page items. You can assign page items to layers by calling the NPageItem AssignToLayers method (NPageItem is a base class for NShape and NGuideline).

    You can get the layers to which a page item is assigned to via the GetLayers() method. You can get the page items that belong to a layer by calling the NLayer GetPageItems method.

    The following code example demonstrates how to assign page items to layers.

    Assigning Page Items To Layers
    Copy Code
                    
    // create a shape and assign it to the red layer
    NShape redShape = shapeFactory.CreateShape(ENBasicShape.Rectangle);
    redShape.SetBounds(10, 10, 100, 100);
    redShape.AssignToLayers(redLayer);
    drawing.ActivePage.Items.Add(redShape);
    
    // create a shape and assign it to the blue layer
    NShape blueShape = shapeFactory.CreateShape(ENBasicShape.Rectangle);
    blueShape.SetBounds(120, 10, 100, 100);
    blueShape.AssignToLayers(blueLayer);
    drawing.ActivePage.Items.Add(blueShape);
    
    // get the layers to which the blue shape is assigned
    NList<NLayer> layers = blueShape.GetLayers();
    
    // get page items assigned to the blue layer
    NList<NPageItem> pageItems = blueLayer.GetPageItems();
    
     Layer Properties

    The following table summarizes the properties of a layer:

    Property Description
    Name Controls the name of the layer. Must be a non-empty string that is unique within the NPage Layers collection.
    Visible Controls the visibility of the page items assigned to the layer. In order for a page item to be displayed it must belong to at least one visible layer.
    Print Controls the visibility of the page items when it is printed or exported. In order for a page item to be printed or exported it must belong to at least one printable layer.
    Active Determines whether layer is considered active. Active layers are automatically assigned to shapes that are dropped on the page, created by tools, or pasted.
    Locked Determines whether layer is considered locked. Locked layers prevent modification and selection of the items belonging to them. A page item is considered locked (non-editable), if it belongs to at least one locked layer.
    Snap Gets or sets whether moved objects can be snapped to page items belonging to this layer. You can for example split the guidelines of a page to several layers and dynamically control to which set of guidelines the moved objects are snapped to.
    Glue Gets or sets whether the user can glue to page items belonging to this layer.
    Highlight Controls whether the page items belonging to the layer must be highlighted. If set to true, the associated page items are rendered in a layer specific color, so that the user can easily identify them.
    Color Gets or sets the color used to highlight the page items belonging to the layer. By default set to DarkGray.