[RuleOn(typeof(ParentShapeContainsNestedChildShapes), FireTime = TimeToFire.TopLevelCommit)]
public class ShapeAddedToDiagramRule : AddRule
{
private double offset = 0.25;
private PointD location = new PointD(0.25, 0.25);
public override void ElementAdded(ElementAddedEventArgs e)
{
Shape shape = null;
ParentShapeContainsNestedChildShapes nestedLink = e.ModelElement as ParentShapeContainsNestedChildShapes;
if (nestedLink != null)
{
shape = nestedLink.NestedChildShapes as Shape;
}
if (shape != null && shape.Diagram != null)
{
// Expand the shape and move it to its new position
shape.IsExpanded = true;
shape.Location = new PointD(location.X, location.Y + offset);
// Adjust the height offset for the size of the shape
// (I'm assuming that the DefaultContainerMargin
// provides for a decent spacing between the shapes)
offset += shape.Size.Height + shape.Diagram.DefaultContainerMargin.Height;
}
}
}