Diagram / Diagram DOM / Drawings / Shapes / Finding Shapes in Page
In This Topic
    Finding Shapes in Page
    In This Topic

    NOV Diagram lets you easily find shapes in a page using shape filters. Shape filters are classes that implement the INFilter<NShape> interface.

     Commonly Used Shape Filters

    The most commonly used filters are placed in the static class NDiagramFilters:

    • ShapeType2D - filters all 2D shapes, i.e. shapes whose ShapeType is 2D.
    • ShapeType1D - filters all 1D shapes, i.e. shapes whose ShapeType is 1D.
    • ShapeRoutableConnector - filters all routable connectors shapes, i.e. shapes that are or inherit from NRoutableConnector.
    • ShapeConnector - filters all connectors, i.e. 1D shapes whose UserClass is set to "Connector".
    • ShapeGraphEdge - filters all 1D shapes whose GraphPart property is set to true.
    • ShapeGraphVertex - filters all 2D shapes whose GraphPart property is set to true.

    To find shapes in a page, use the GetShapes method of the page passing a shape filter like the ones mentioned above or an instance of any class that implements the INFilter<NShape> interface. For example to get all 2D shapes in the active page of a drawing use the following piece of code:

    Get all 2D shapes in the active page of a drawing
    Copy Code
    bool includeGroupedShapes = true;
    NList<NShape> shapes = drawingView.ActivePage.GetShapes(includeGroupedShapes, NDiagramFilters.ShapeType2D);
    
     Included Shape Filter Classes

    You can also use the following filter classes included in NOV Diagram:

    • NShapeNameFilter - filters shapes whose Name property is equal to a given value.
    • NShapeTextFilter - filters shapes whose Text property is equal to a given value.
    • NUserClassFilter - filters shapes whose UserClass property is equal to a given value.

    For example to find all shapes whose text is equal to "My Shape", use the following piece of code:

    Get all 2D shapes in the active page of a drawing
    Copy Code
    bool includeGroupedShapes = true;
    NList<NShape> shapes = drawingView.ActivePage.GetShapes(includeGroupedShapes, new NShapeTextFilter("My Shape"));
    
     Custom Shape Filters
    You can easily create a class that implements the INFilter<NShape> interface to make a custom search on the shapes in a page. For example, the class ShapeFillFilter below will find all shapes that are filled with a given color:
    Shape fill filter
    Copy Code
    class ShapeFillFilter : INFilter<NShape>
    {
        public ShapeFillFilter(NColor color)
        {
            m_Color = color;
        }
    
        public bool Filter(NShape shape)
        {
            if (shape.GeometryNoCreate == null)
                return false;
    
            NColorFill colorFill = shape.GeometryNoCreate.Fill as NColorFill;
            if (colorFill == null)
                return false;
    
            return colorFill.Color == m_Color;
        }
    
        private NColor m_Color;
    }
    

    To find all shapes filled with red, use the class above in the following way:

    Find all shapes filled with red
    Copy Code
    bool includeGroupedShapes = true;
    NList<NShape> shapes = drawingView.ActivePage.GetShapes(includeGroupedShapes, new ShapeFillFilter(NColor.Red));
    
    See Also

    DataStructures