This project has moved and is read-only. For the latest updates, please go here.

How to properly handle visiblity of a shape

Mar 27, 2014 at 3:14 PM
I'm using NShape to visualise my business objects. The also have property which allows them to be visible or not. If I want to display shapes that correspond to these objects, which is the right way to handle the "Visible" property?
Mar 27, 2014 at 3:29 PM
Edited Mar 27, 2014 at 3:36 PM
Shapes do not have a visible property as you already figured out.
What you can do is
  • Adding a layer for invisible shapes to the diagram:
    Layer invisibleLayer = new Layer("Invisible");
    display.Diagram.Layers.Add(invisibleLayer);
  • Adding shapes that should not be visible to this layer: Diagram.AddShapeToLayers(...) or DiagramSetController.AssignShapesToLayers(...)
    display.Diagram.AddShapeToLayers(shape, invisibleLayer);
    // ** or **
    diagramSetController.AddShapesToLayers(diagram, 
        SingleInstanceEnumerator<Shape>(shape).Create(), invisibleLayer);
  • Hide the layer for invisible shapes:
    Display.HiddenLayers = invisibleLayer.Id;
You can toggle the visibility of single shapes by adding/removing them from the layer of invisible shapes.
Hint: A shape is visible as long as at least one layer (it belongs to) is visible.
Mar 27, 2014 at 3:46 PM
Great, thank you very much.

I'm now slowly getting my bits and pieces together. :)
Mar 27, 2014 at 5:03 PM
Edited Mar 27, 2014 at 5:12 PM
Regarding your feature suggestion, here is an alternative way of hinding your shapes:

You could define line styles and/or fill styles that are (semi) transparent and assign them to your invisible shapes instead of hiding them with layers.
Example:
    ColorStyle colorStyle = new ColorStyle("SemiTransparentBlue", 
        project.Design.ColorStyles.Blue.Color, 50); // 50% transparency
    project.Design.AddStyle(colorStyle);
    // Save new style to repository 
    project.Repository.Insert(project.Design, colorStyle);

    LineStyle lineStyle = new LineStyle("Invisible");
    lineStyle.ColorStyle = project.Design.ColorStyles["SemiTransparentBlue"];
    // Save new style to repository 
    project.Repository.Insert(project.Design, lineStyle);

    FillStyle fillStyle = new FillStyle("Invisible");
    fillStyle.AdditionalColorStyle = project.Design.ColorStyles.Transparent;
    fillStyle.BaseColorStyle = project.Design.ColorStyles["SemiTransparentBlue"];
    // Save new style to repository 
    project.Repository.Insert(project.Design, fillStyle);
            
    project.Repository.Update(project.Design);
    // Assign the invisible styles to invisible shapes
    RectangleBase shape = null;//...
    shape.LineStyle = project.Design.LineStyles["Invisible"];
    shape.FillStyle = project.Design.FillStyles["Invisible"];