Link Image Position

As we said earlier, we need to place an GoObject over the link. so we coaded it this way. but we are not able to assign the GoObject into instance of this class. Our GoObject is an GoImage…

using System;
using System.Drawing;
using Northwoods.Go;

namespace Processor {
public interface IGoLinkImage {
///


/// Gets the point on the parent link where this image connects
///

///
///
///
PointF LinkImagePoint { get; }

/// <summary>
/// Gets or sets the offset of this image relative to the
/// <see cref="LinkImagePoint"/>.
/// </summary>
/// <seealso cref="Segment"/>
/// <seealso cref="SegmentPercentage"/>
SizeF Offset { get; set; }

/// <summary>
/// Gets or sets the index of the segment where the <see cref="LinkImagePoint"/> should be.
/// </summary>
/// <seealso cref="SegmentPercentage"/>
/// <seealso cref="Offset"/>
int Segment { get; set; }

/// <summary>
/// Gets or sets the percentage along the segment where the <see cref="LinkImagePoint"/> should be.
/// </summary>
/// <seealso cref="Segment"/>
/// <seealso cref="Offset"/>
float SegmentPercentage { get; set; }

}

[Serializable]
public class LinkImage : GoObject, IGoLinkImage
{
    public LinkImage()
    {
        this.Copyable = false;
        this.Deletable = false;
        this.Editable = false;
        this.Selectable = true;
        this.Movable = true;
    }

    /// <summary>
    /// A Convenience property, for getting the parent link as a GoLabeledLink.
    /// </summary>
    public GoLabeledLink Link
    {
        get { return this.Parent as GoLabeledLink; }
    }

    /// <summary>
    /// Gets the point on the parent link where this image connects
    /// </summary>
    /// <seealso cref="Segment"/>
    /// <seealso cref="SegmentPercentage"/>
    public PointF LinkImagePoint
    {
        get
        {
            GoLabeledLink l = this.Link;
            if (l != null && l.RealLink != null)
            {
                GoLink rl = l.RealLink;
                int numpts = rl.PointsCount;
                if (numpts < 2) return this.Center;
                int s = this.Segment;
                if (s >= numpts - 1)
                    s = numpts - 2;
                if (s < 0)
                    s = 0;
                PointF A = rl.GetPoint(s);
                PointF B = rl.GetPoint(s + 1);
                float segdst = this.SegmentPercentage;
                return new PointF(A.X + ((B.X - A.X) * segdst) / 100,
                                  A.Y + ((B.Y - A.Y) * segdst) / 100);
            }
            else
            {
                return this.Center;
            }
        }
    }

    /// <summary>
    /// Gets or sets the offset of the center of this label relative to the
    /// <see cref="LinkImagePoint"/>.
    /// </summary>
    /// <seealso cref="Segment"/>
    /// <seealso cref="SegmentPercentage"/>
    public SizeF Offset
    {
        get { return myOffset; }
        set
        {
            SizeF old = myOffset;
            if (old != value)
            {
                myOffset = value;
                Changed(ChangedOffset, 0, null, MakeRect(old), 0, null, MakeRect(value));
            }
        }
    }

    /// <summary>
    /// Gets or sets the index of the segment where the <see cref="LinkImagePoint"/> should be.
    /// </summary>
    /// <seealso cref="SegmentPercentage"/>
    /// <seealso cref="Offset"/>
    public int Segment
    {
        get { return mySegment; }
        set
        {
            int old = mySegment;
            if (old != value)
            {
                mySegment = value;
                Changed(ChangedSegment, old, null, NullRect, value, null, NullRect);
            }
        }
    }

    /// <summary>
    /// Gets or sets the percentage along the segment where the <see cref="LinkImagePoint"/> should be.
    /// </summary>
    /// <seealso cref="Segment"/>
    /// <seealso cref="Offset"/>
    public float SegmentPercentage
    {
        get { return mySegmentPercentage; }
        set
        {
            float old = mySegmentPercentage;
            if (old != value)
            {
                mySegmentPercentage = value;
                Changed(ChangedSegmentPercentage, 0, null, MakeRect(old), 0, null, MakeRect(value));
            }
        }
    }

    public override void ChangeValue(GoChangedEventArgs e, bool undo)
    {
        switch (e.SubHint)
        {
            case ChangedOffset:
                this.Offset = e.GetSize(undo);
                return;
            case ChangedSegment:
                this.Segment = e.GetInt(undo);
                return;
            case ChangedSegmentPercentage:
                this.SegmentPercentage = e.GetFloat(undo);
                return;
            default:
                base.ChangeValue(e, undo);
                return;
        }
    }

    public const int ChangedOffset = LastChangedHint + 3214;
    public const int ChangedSegment = LastChangedHint + 3215;
    public const int ChangedSegmentPercentage = LastChangedHint + 3216;
    public const int ChangedConnectionColor = LastChangedHint + 3217;

    private SizeF myOffset = new SizeF(0, 0);
    private int mySegment = 3;
    private float mySegmentPercentage = 50;
}

}

Following Code is in Another Class GraphLink.cs which is having the instance of LinkImage Class

LinkImage myLinkFigure = new LinkImage();

and assigning the GoObject using :

[TypeConverter(typeof(LinkTypes))]
public string LinkType
{
get
{
return myLinkType;
}
set
{
string old = myLinkType;
if (old != value)
{
myLinkType = value;
if (!String.IsNullOrEmpty(this.Text))
this.LinkFigure = (new GraphFigure()).GetLinkFigure(value);

                if (MdiProvisioning.App.PropertyChanged == "LinkType")
                {
                    if (!String.IsNullOrEmpty(this.Text))
                    {
                        if (!String.IsNullOrEmpty(value))
                        {
                            int typeId = int.Parse(DataUtils.GetValue("SELECT dbo.LinkTypeId('" + value + "')"));
                            DataUtils.ExecuteQuery("EXEC Sp_LinkEditType " + typeId + ",'" + this.Text + "'");
                        }
                        else
                        {
                            DataUtils.ExecuteQuery("EXEC Sp_LinkEditType null,'" + this.Text + "'");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Text property should be assigned first.", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        MdiProvisioning.App.GetProperts.Focus();
                        myLinkType = old;
                    }
                }
                MdiProvisioning.App.PropertyChanged = null;
            }
        }
    }

private GoObject LinkFigure
{
set
{

            if (value != null)
            {
                myLinkFigure= (LinkImage)value;
                myLinkFigure.Size = new SizeF(20.0F, 20.0F);
                Add(myLinkFigure);
            }
        }
    }

Error Occured:

Unable to cast object of type ‘Provisioning.GrpahImage’
to type ‘Provisioning.LinkImage’.

error is “Unable to cast object of type 'Provisioning.GrpahImage’
to type ‘Provisioning.LinkImage’.”

well, I don't see any "GrpahImage" here, so I'm guessing you're using that instead of "LinkImage" somewhere.

Dear Jake,

GraphImage is a drived class which returns the image in the form of GoObject.

LinkImage Class here you are seeing is also drived from GoObject.

So it quite logical to assign the GoObject to LinkImage…

This is the Line of code i m doing this:

GoObject gObj= (new GraphFigure()).GetLinkFigure(value));

this.LinkFigure =gObj as LinkImage;

the Code of property “LinkFigure” you can see above post

Man you just look into the code of class “LinkImage” and tell me why it is giving error when i m assigning any GoObject to it’s instance.

for Exapmle

GoObject gObj; //it’s initialized with GoImage

LinkImage linkImage=new LinkImage();

linkImage=gObj as LinkImage;

I don’t see the code here for GetLinkFigure(value)… but given the error I’m assuming it’s returning an object of type ‘Provisioning.GrpahImage’.

The error is that you can't cast that to a type of 'Provisioning.LinkImage'.
This is not a GoDiagram problem.

Dear Jake,

As you suggested earlier, to look into the Processor sample that has Label on link which is movable by user. So what we want is to place an GoImage inplace of GoText.

from that sample we tried to convert LinkLabel class to LinkImage but the concept is not working. Please have a look at the code

using System;
using System.Drawing;
using Northwoods.Go;

namespace Processor {
public interface IGoLinkImage {
///


/// Gets the point on the parent link where this image connects
///

///
///
///
PointF LinkImagePoint { get; }

/// <summary>
/// Gets or sets the offset of this image relative to the
/// <see cref="LinkImagePoint"/>.
/// </summary>
/// <seealso cref="Segment"/>
/// <seealso cref="SegmentPercentage"/>
SizeF Offset { get; set; }

/// <summary>
/// Gets or sets the index of the segment where the <see cref="LinkImagePoint"/> should be.
/// </summary>
/// <seealso cref="SegmentPercentage"/>
/// <seealso cref="Offset"/>
int Segment { get; set; }

/// <summary>
/// Gets or sets the percentage along the segment where the <see cref="LinkImagePoint"/> should be.
/// </summary>
/// <seealso cref="Segment"/>
/// <seealso cref="Offset"/>
float SegmentPercentage { get; set; }

}

[Serializable]
public class LinkImage : GoObject, IGoLinkImage
{
    public LinkImage()
    {
        this.Copyable = false;
        this.Deletable = false;
        this.Editable = false;
        this.Selectable = true;
        this.Movable = true;
    }

    /// <summary>
    /// A Convenience property, for getting the parent link as a GoLabeledLink.
    /// </summary>
    public GoLabeledLink Link
    {
        get { return this.Parent as GoLabeledLink; }
    }

    /// <summary>
    /// Gets the point on the parent link where this image connects
    /// </summary>
    /// <seealso cref="Segment"/>
    /// <seealso cref="SegmentPercentage"/>
    public PointF LinkImagePoint
    {
        get
        {
            GoLabeledLink l = this.Link;
            if (l != null && l.RealLink != null)
            {
                GoLink rl = l.RealLink;
                int numpts = rl.PointsCount;
                if (numpts < 2) return this.Center;
                int s = this.Segment;
                if (s >= numpts - 1)
                    s = numpts - 2;
                if (s < 0)
                    s = 0;
                PointF A = rl.GetPoint(s);
                PointF B = rl.GetPoint(s + 1);
                float segdst = this.SegmentPercentage;
                return new PointF(A.X + ((B.X - A.X) * segdst) / 100,
                                  A.Y + ((B.Y - A.Y) * segdst) / 100);
            }
            else
            {
                return this.Center;
            }
        }
    }

    /// <summary>
    /// Gets or sets the offset of the center of this label relative to the
    /// <see cref="LinkImagePoint"/>.
    /// </summary>
    /// <seealso cref="Segment"/>
    /// <seealso cref="SegmentPercentage"/>
    public SizeF Offset
    {
        get { return myOffset; }
        set
        {
            SizeF old = myOffset;
            if (old != value)
            {
                myOffset = value;
                Changed(ChangedOffset, 0, null, MakeRect(old), 0, null, MakeRect(value));
            }
        }
    }

    /// <summary>
    /// Gets or sets the index of the segment where the <see cref="LinkImagePoint"/> should be.
    /// </summary>
    /// <seealso cref="SegmentPercentage"/>
    /// <seealso cref="Offset"/>
    public int Segment
    {
        get { return mySegment; }
        set
        {
            int old = mySegment;
            if (old != value)
            {
                mySegment = value;
                Changed(ChangedSegment, old, null, NullRect, value, null, NullRect);
            }
        }
    }

    /// <summary>
    /// Gets or sets the percentage along the segment where the <see cref="LinkImagePoint"/> should be.
    /// </summary>
    /// <seealso cref="Segment"/>
    /// <seealso cref="Offset"/>
    public float SegmentPercentage
    {
        get { return mySegmentPercentage; }
        set
        {
            float old = mySegmentPercentage;
            if (old != value)
            {
                mySegmentPercentage = value;
                Changed(ChangedSegmentPercentage, 0, null, MakeRect(old), 0, null, MakeRect(value));
            }
        }
    }

    public override void ChangeValue(GoChangedEventArgs e, bool undo)
    {
        switch (e.SubHint)
        {
            case ChangedOffset:
                this.Offset = e.GetSize(undo);
                return;
            case ChangedSegment:
                this.Segment = e.GetInt(undo);
                return;
            case ChangedSegmentPercentage:
                this.SegmentPercentage = e.GetFloat(undo);
                return;
            default:
                base.ChangeValue(e, undo);
                return;
        }
    }

    public const int ChangedOffset = LastChangedHint + 3214;
    public const int ChangedSegment = LastChangedHint + 3215;
    public const int ChangedSegmentPercentage = LastChangedHint + 3216;
    public const int ChangedConnectionColor = LastChangedHint + 3217;

    private SizeF myOffset = new SizeF(0, 0);
    private int mySegment = 3;
    private float mySegmentPercentage = 50;
}

}