Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Programmatically creating and moving links (Read 10067 times)
PetrOs
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: May 26th, 2011
Programmatically creating and moving links
Dec 6th, 2011 at 10:42am
Print Post  
Hey,
I have a following task to do, and I am not quite sure how to do that...

I have two templated nodes. Between these, I need several links, which should be placed proportional to some numeric property. This proportion is changing in the runtime, so I need to move the AnchorPoints (and eventually change their count). Every link creation is steered from my diagram subclass.

So far, I just used the extra shapes being placed accordingly, but i dont like this solution, and want to use links...

I did not found an extensive tutorial or an example how to do this...

So:
1) How do I add AnchorPoints on a templated node programmatically?
2) How do I move or remove these
3) How do I add a link using some very specific anchor point?
4) Can I style the link at all? Meaning adding a caption, line style and color?

Thanks, Petr
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Programmatically creating and moving links
Reply #1 - Dec 6th, 2011 at 12:01pm
Print Post  
Hi,

1, 2) Unfortunately we haven't designed the AnchorPattern objects to be very dynamic. Better create an entirely new AnchorPattern instance with more or less points and assign it to node.AnchorPattern. Then reset the links' OriginAnchor and DestinationAnchor properties so link ends realign to the current anchor point positions:

// force link to update its start point from anchor point
int o = link.OriginAnchor;
link.OriginAnchor = -1;
link.OriginAnchor = o;
int d = link.DestinationAnchor;
....

3) Either use the Factory.CreateDiagramLink(node, anchor, node, anchor) overloaded method, or set the OriginAnchor and DestinationAnchor properties after creating a link.

4) Set the Stroke* and Text properties derived from DiagramItem.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
PetrOs
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: May 26th, 2011
Re: Programmatically creating and moving links
Reply #2 - Dec 7th, 2011 at 8:57am
Print Post  
Hey

It seems to be working for inter-object links.

However, I tried to create the link to self, from one anchor point to another, and that does not work.
For that, my node1 has 2 anchor points, but independently how i set the link, origin works well (sticking out of an anchor point 0) but destination runs to the mid-top of the component instead to an anchor point 1

The code is this:


[code]
           var link = new DiagramLink(this);
           link.Origin = node1;
           link.Destination = node1;
           link.OriginAnchor = 0;
           link.DestinationAnchor = 1;
           this.Links.Add(link);
[/code]

Any idea?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Programmatically creating and moving links
Reply #3 - Dec 7th, 2011 at 9:28am
Print Post  
Hi,

That seems to connect the correct anchor points in my test, with everything at default values. What property values are you using for diagram's link related properties, such as RouteLinks, DynamicLinks, RoutingOptions, etc?

Stoyan
  
Back to top
 
IP Logged
 
PetrOs
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: May 26th, 2011
Re: Programmatically creating and moving links
Reply #4 - Dec 7th, 2011 at 1:33pm
Print Post  
I did not change any, all is set to default..
However, if I use both same origin and destination, and originanchor and destinationanchor - then i get a loop...
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Programmatically creating and moving links
Reply #5 - Dec 8th, 2011 at 8:23am
Print Post  
Ok, I've managed to reproduce this with a different AnchorPattern. Apparently the link's OriginAnchor and DestinationAnchor are set to the index of the anchor point closest to the node's top middle point, while the loop shape set for self-loop links ignores the anchor position. Then the OriginAnchor or DestinationAnchor setter just returns if the property already has the same value, without changing the actual position of the link end point. So you can use the same work-around as the one for dynamically changing anchor point positions - reset the Origin/DestinationAnchor property values to -1 and back to the actual value:

Code
Select All
var link = new DiagramLink(diagram);
link.Origin = node;
link.Destination = node;
link.OriginAnchor = -1;
link.DestinationAnchor = -1;
link.OriginAnchor = 0;
link.DestinationAnchor = 1;
diagram.Links.Add(link); 



If using a custom node class, you could also override the DiagramNode.SetReflexive method and directly set the link's CotnrolPoints to coordinates that correspond to the anchor positions.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
PetrOs
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: May 26th, 2011
Re: Programmatically creating and moving links
Reply #6 - Dec 8th, 2011 at 11:30am
Print Post  
Ok, that works Wink thanks!

However, that brings another question - how can I control the way the loop is built? Now it is always going up from the origin anchor point, and only then reverses and goes to destination. I would like to have a loop completely on the left (or right) of the anchor points..

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


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Programmatically creating and moving links
Reply #7 - Dec 8th, 2011 at 12:03pm
Print Post  
You can do that by assigning new coordinates to link ControlPoints, for example you could do this if both anchor points are at the top of a node:

Code
Select All
var p = link.ControlPoints;
p[1] = new Point(p[0].X, p[1].Y);
p[2] = new Point(p[3].X, p[2].Y);
link.UpdateFromPoints(); 



You will have to implement similar code for a lot of cases if the anchor points can be on different sides of the node.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
PetrOs
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: May 26th, 2011
Re: Programmatically creating and moving links
Reply #8 - Dec 8th, 2011 at 2:55pm
Print Post  
Ok, it works too, thanks!
  
Back to top
 
IP Logged
 
PetrOs
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: May 26th, 2011
Re: Programmatically creating and moving links
Reply #9 - Dec 14th, 2011 at 11:03am
Print Post  
Hey

I found some strange behaviour for the links...

I create my links, add these to Links collection of a diagram, and then start adjusting them.  (and later doing remove/add from the links to see the effect)
If I first set the destination and destinationanchor, and then the origin and originanchor, all works well.

If I set the Origin and OriginAnchor first, and then the destination ones, OriginAnchor field is getting botched!

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


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Programmatically creating and moving links
Reply #10 - Dec 15th, 2011 at 8:32am
Print Post  
Hi,

In what manner is it getting botched? It might be reassigned to a new anchor point if some properties are enabled, e.g. Dynamic or AutoRoute + RoutingOptions.Anchoring.Reassign. I suppose in such case you should set OriginAnchor and DestinationAnchor only after both Origin and Destination are set.

Stoyan
  
Back to top
 
IP Logged
 
PetrOs
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: May 26th, 2011
Re: Programmatically creating and moving links
Reply #11 - Dec 15th, 2011 at 9:50am
Print Post  
The value in the OriginAnchor is getting changed. (i.E. was 2 before, after setting the Destination it becomes 1 suddenly).

I had to restructure my algorithm for that, so I collect all pending changes and then setting the origin, destination, etc all in one block for each link. Costs extra performance and extra memory however ;(
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Programmatically creating and moving links
Reply #12 - Dec 15th, 2011 at 12:56pm
Print Post  
Do you think anyone will notice a difference Smiley Anyway this code:

link = diagram.Factory.CreateLink(origin, orgAnchor, dest, destAnchor);

should be actually faster than

link.Origin = origin;
link.OriginAnchor = oa;

...

link.Destination = dest;
link.DestinationAnchor = da;

E.g. the latter code could run the link routing method four times, and repeat some geometrical calculations.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint