How can i force the placement of a port on its parent ?

I would force the position of a port on the bottom edge on its parent.
Is it possible to implement this feature

There is a PortPlacementHelper property but it's a non overridable static property. Is it a way

Thanks,

 



Answer this question

How can i force the placement of a port on its parent ?

  • ScooterBrown

    Hi Malain,

    You need to write a BoundsRule for the Port shape. BoundsRules can be applied to any shape, and are used (1) to restrict how the user can resize the shape (not applicable to Ports) and (2) to restrict how the user can place the shape.

    using System;
    using Microsoft.VisualStudio.Modeling;
    using Microsoft.VisualStudio.Modeling.Diagrams;

    public partial class InPortShape

    {
    // For a port shape, overrides the normal port positioning.
    public override BoundsRules BoundsRules#
    {
    get
    {
    return new InPortBoundsRules();#

    }}}

    public class InPortBoundsRules : BoundsRules
    {
    /// <summary>
    /// Called whenever the user tries to move or resize the shape.
    /// </summary>
    /// <param name="shape"></param>
    /// <param name="proposedBounds"></param>
    /// <returns></returns>

    public override RectangleD GetCompliantBounds(ShapeElement shape, RectangleD proposedBounds)
    {
    InPortShape portShape = shape as InPortShape;
    ComponentShape parentShape = portShape.ParentShape as ComponentShape;
    // on initial creation, there is no parent shape
    if (parentShape == null) return proposedBounds;
    double x = Math.Min(Math.Max(proposedBounds.Left, proposedBounds.Width * 0.5),
    parentShape.AbsoluteBoundingBox.Width - proposedBounds.Width * 1.5);
    double y = parentShape.AbsoluteBoundingBox.Height - proposedBounds.Height * 0.5;
    return new RectangleD(x, y, proposedBounds.Width, proposedBounds.Height);
    }
    }

    Note that RectangleD's dimensions are doubles, so should not be compared for equality without some rounding.



  • Lejing

    Thanks, Alan.
  • How can i force the placement of a port on its parent ?