Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) IndexOutOfRangeException updating links from point (Read 5714 times)
vtortola
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 104
Joined: Nov 26th, 2008
IndexOutOfRangeException updating links from point
Mar 9th, 2009 at 4:40pm
Print Post  
Hi,

I have some nodes that are linked with themselves (link source and target is the same). Looks fine, but when after save them I try to load them... a exception happens.

I get the follow exception:

System.IndexOutOfRangeException occurred
Message="Index was outside the bounds of the array."
Source="MindFusion.Diagramming.Wpf"
StackTrace:
at MindFusion.x09d6340d05b7af69.xd2546c282e6f199a(DrawingContext x41347a961b838962, Pen x9c79b5ad7b769b12, Point[] x6fa2570084b2ad39)
InnerException:

I'm trying to save and retrieve the links from a XML. It was something that I thought was working but seems like the links never remains in the same position, so I did some changes in the code following your recommendations in other posts about UpdateFromPoints and SegmentCount, but I get that exeption.

I save the links with this code:

Code
Select All
XElement linksXml = new XElement( "Links",
    from l in DatabaseDiagram.Links.OfType<DiagramLink>().Where(l=>l.Tag is DataDiagramRelation)
    select new XElement( "Link",
        new XAttribute( "FkColumn", ((DataDiagramRelation)l.Tag).FkColumn ),
        new XAttribute( "FromTableName", ((DataTable)l.Origin.Tag).TableName ),
        new XAttribute( "ToTableName", ((DataTable)l.Destination.Tag).TableName),
        new XAttribute( "LinkStyle", l.Style.ToString() ),
        new XAttribute( "Color", "Black" ),
        new XAttribute( "LinkHeader", l.HeadShape.ToString() ),
        new XElement( "ControlPoints",
            new XAttribute( "SegmentCount", l.SegmentCount ),
                     from p in l.ControlPoints.OfType<Point>()
                     select new XElement( "Point",
                        new XAttribute( "X", p.X ),
                        new XAttribute( "Y", p.Y ) ) )

 



And I obtain a XML, where one of the items is:

Code
Select All
<Link FkColumn="ParentTaskId" FromTableName="table1" ToTableName="table1" LinkStyle="Cascading" Color="Black" LinkHeader="Reversed">
  <ControlPoints SegmentCount="3">
    <Point X="360" Y="109.99999999999996" />
    <Point X="360" Y="110" />
    <Point X="360" Y="110" />
    <Point X="360" Y="109.99999999999996" />
  </ControlPoints>
</Link>
 



Then, when I open the diagram again, I try to apply that configuratio to the links using the following code:

Code
Select All
private void SetLinkProperties( XElement xlink, DiagramLink link )
{
    Object style = Enum.Parse( typeof( LinkStyle ), xlink.Attribute( "LinkStyle" ).Value );
    link.Style = style != null ? (LinkStyle)style : LinkStyle.Polyline;

    Object header = Enum.Parse( typeof( ArrowHead ), xlink.Attribute( "LinkHeader" ).Value );
    link.HeadShape = header != null ? (ArrowHead)header : ArrowHead.None;

    if ( xlink.Attribute( "Color" ) != null )
    {
        link.Brush = GetSolidColorBrush( (String)xlink.Attribute( "Color" ) );
        link.Pen = new Pen( link.Brush, 1 );
    }

    link.ControlPoints.Clear();

    link.SegmentCount = Int16.Parse( xlink.Element( "ControlPoints" ).Attribute( "SegmentCount" ).Value );

    foreach ( XElement controlPoint in xlink.Element( "ControlPoints" ).Descendants( "Point" ) )
        link.ControlPoints.Add( new Point( Double.Parse( controlPoint.Attribute( "X" ).Value ),
                                           Double.Parse( controlPoint.Attribute( "Y" ).Value ) ) );

    link.UpdateFromPoints();
    link.AutoRoute = true;
}
 




Then fails firstly on the "link.UpdateFromPoints();" line. The "link.ControlPoints" collection has 14 elements... although I only have added 4, I dunno if it's normal or not Tongue

Although I catch the exception, is raised again when I do "myForm.ShowDialog()" the the application crash.

Besides, the rest of the links are not keeping their exact locations.

What could be the problem?

Cheers.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: IndexOutOfRangeException updating links from p
Reply #1 - Mar 9th, 2009 at 5:02pm
Print Post  
What happens if you move the link.ControlPoints.Clear() call after the link.SegmentCount = Int16.Parse() assignment?
  
Back to top
 
IP Logged
 
vtortola
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 104
Joined: Nov 26th, 2008
Re: IndexOutOfRangeException updating links from p
Reply #2 - Mar 9th, 2009 at 5:31pm
Print Post  
Thanks, that solve the exception in selflinks and now remember the positions of the anchors correctly, but if you drag&drop some of the intermediate points... those positions are not saved.

Cheers.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: IndexOutOfRangeException updating links from p
Reply #3 - Mar 9th, 2009 at 8:43pm
Print Post  
Are you sure that saving code works? These coordinates from the generated XML look incorrect -

<Point X="360" Y="109.99999999999996" />
<Point X="360" Y="110" />
<Point X="360" Y="110" />
<Point X="360" Y="109.99999999999996" />

Stoyan
  
Back to top
 
IP Logged
 
vtortola
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 104
Joined: Nov 26th, 2008
Re: IndexOutOfRangeException updating links from p
Reply #4 - Mar 10th, 2009 at 10:33am
Print Post  
That link is a selflink, a link from one table to the same table Tongue

The rest of the links looks more normal:

<Link FkColumn="PredTaskId" FromTableName="t1" ToTableName="t2" LinkStyle="Cascading" Color="Black" LinkHeader="Reversed">
  <ControlPoints SegmentCount="2">
    <Point X="960" Y="605.33333333333337" />
    <Point X="1010" Y="605.33333333333337" />
    <Point X="1010" Y="605.33333333333337" />
  </ControlPoints>
</Link>
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: IndexOutOfRangeException updating links from p
Reply #5 - Mar 10th, 2009 at 11:04am
Print Post  
Are you sure that query gives you the point in the correct order? Setting link.AutoRoute = true might discard your points and set new ones; could you check if commenting it out will show the points where expected?
  
Back to top
 
IP Logged
 
vtortola
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 104
Joined: Nov 26th, 2008
Re: IndexOutOfRangeException updating links from p
Reply #6 - Mar 10th, 2009 at 1:41pm
Print Post  
When I comment the AutoRoute line the links are now in the exact position. Thank you!
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: IndexOutOfRangeException updating links from p
Reply #7 - Mar 10th, 2009 at 3:25pm
Print Post  
AutoRoute should not change the link shape if the link hasn't been added to the diagram yet. If you still want auto-routing enabled, try creating a DiagramLink using the (origin,destination) constructor, call SetLinkProperties on it, and call diagram.Links.Add() after that.
  
Back to top
 
IP Logged
 
vtortola
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 104
Joined: Nov 26th, 2008
Re: IndexOutOfRangeException updating links from p
Reply #8 - Mar 10th, 2009 at 4:00pm
Print Post  
Which is the difference with creating the links with the "Factory" methods?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: IndexOutOfRangeException updating links from p
Reply #9 - Mar 10th, 2009 at 4:19pm
Print Post  
The factory methods automatically add the link to the diagram and setting AutoRoute = true triggers the routing operation. This should not trigger it:

DiagramLink link = new DiagramLink();
SetLinkProperties(link);
diagram.Links.Add(link);

However, adding a node after this might still route the link and change the points you have assigned, so you'd have to load the links after all nodes.
  
Back to top
 
IP Logged
 
vtortola
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 104
Joined: Nov 26th, 2008
Re: IndexOutOfRangeException updating links from p
Reply #10 - Mar 10th, 2009 at 4:20pm
Print Post  
Thanks.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: IndexOutOfRangeException updating links from p
Reply #11 - Mar 13th, 2009 at 12:28pm
Print Post  
Check the version from the PM page. It adds the void UpdateFromPoints(bool updateGroups, bool updateSegmentCount) overloaded method. Now in your custom deserialization code you can restore just the Style and ControlPoints, and then call UpdateFromPoints(false, true). This should set SegmentCount internally, and will also work if AutoRoute is enabled.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
vtortola
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 104
Joined: Nov 26th, 2008
Re: IndexOutOfRangeException updating links from p
Reply #12 - Mar 16th, 2009 at 4:32pm
Print Post  
Nice work, thanks.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint