Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic State Diagram (Machine) Using NetDiagram (Read 4568 times)
jlj30
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 183
Joined: Sep 4th, 2011
State Diagram (Machine) Using NetDiagram
May 28th, 2012 at 6:48pm
Print Post  
Hi,

Has anyone used NetDiagram to create a State Diagram (may be called a State Machine)?
If so, I'd be interested to know what layout you used, or whether it was created without a built-in layout.  I will be creating the diagram from a database; the user will not be able to interact with it.

Thanks in advance for any ideas or examples.

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


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: State Diagram (Machine) Using NetDiagram
Reply #1 - May 29th, 2012 at 7:14am
Print Post  
Hi,

Try the code here:
http://mindfusion.eu/Forum/YaBB.pl?num=1259588106/4#4

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


I love YaBB 1G - SP1!

Posts: 183
Joined: Sep 4th, 2011
Re: State Diagram (Machine) Using NetDiagram
Reply #2 - Jun 5th, 2012 at 12:13am
Print Post  
Hi Stoyan,

Thanks for the tip.  It gave me some ideas.
Actually, I've produced a decent looking diagram using the OrthogonalLayout followed by the OrthogonalRouter layout.  I just have 2 issues.

First, the text on the links is not center'd on the links.
Second, the diagram is somewhat reversed (vertically) in that the BPMN start shape is at the bottom and the terminate/end shape is at the top.  I still find the diagram readable, but would like it if I could get the start shape at the top.

For my first issue, here's some relevant code snippets:

protected void createLink(ShapeNode sourceNode, ShapeNode targetNode, string trigger, Diagram diagram)
    {
        Factory factory = diagram.Factory;
        DiagramLink link = factory.CreateDiagramLink(sourceNode, targetNode);
        link.Font = new Font("Arial", 8, System.Drawing.FontStyle.Regular);
        link.ShadowOffsetX = 0;
        link.ShadowOffsetY = 0;
        link.Text = trigger;
        link.HeadBrush = transitionLineColour;
        link.TextStyle = LinkTextStyle.Rotate;
        link.Style = LinkStyle.Polyline;
        link.TextAlignment = StringAlignment.Center; // <<<<<<----- Text Is NOT center'd on diagram!!!!
    }

case "OrthogonalRouter":
                {
                    OrthogonalLayout layout = new OrthogonalLayout();
                    layout.Padding = 40;
                    layout.Arrange(diagram);
                    OrthogonalRouter layout2 = new OrthogonalRouter();
                    layout2.Arrange(diagram);
                    break;
                }

A sample image is attached, along with the complete class.

Any thoughts or suggestions on either issue?

Thanks in advance

Jim
  

OrthogonalRouter_Layout_-_Link_Text_Issue_-_04jun2012.zip (Attachment deleted)
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: State Diagram (Machine) Using NetDiagram
Reply #3 - Jun 5th, 2012 at 8:22am
Print Post  
Hi Jim,

The Rotate text style aligns text to a long-enough segment rather than the full length of a link, so I suppose this will happen if the links have more than two control points. After applying OrthogonalRouter, you could reset the links to a single segment so that text appears centered, e.g.

Code
Select All
if (link.SegmentCount == 2 &&
	link.ControlPoints[0].X == link.ControlPoints[1].X &&
	link.ControlPoints[1].X == link.ControlPoints[2].X)
{
	var p1 = link.ControlPoints[0];
	var p2 = link.ControlPoints[1];
	link.Style = LinkStyle.Polyline;
	link.SegmentCount = 1;
	link.ControlPoints[0] = p1;
	link.ControlPoints[1] = p2;
	link.UpdateFromPoints();
} 



Alternatively, use the Follow text style so that text follows the path defined by all link segments - it won't be centered but it won't overlap with nodes.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: State Diagram (Machine) Using NetDiagram
Reply #4 - Jun 5th, 2012 at 12:40pm
Print Post  
At this time you can't specify fixed positions for nodes in OrthogonalLayout. If you detect that your start node is closer to the bottom of the diagram, you could mirror the arranged diagram vertically to bring that node to the top:

Code
Select All
void MirrorVertically(Diagram diagram)
{
	var bounds = diagram.GetContentBounds(false, true);
	var y = bounds.Y + bounds.Height / 2;
	foreach (DiagramNode node in diagram.Nodes)
	{
		var ny = node.GetCenter().Y;
		var dy = ny - y;
		MoveNode(node, y - dy);
	}
	foreach (DiagramLink link in diagram.Links)
	{
		for (int i = 0; i < link.ControlPoints.Count; i++)
		{
			var p = link.ControlPoints[i];
			var dy = p.Y - y;
			p.Y = y - dy;
			link.ControlPoints[i] = p;
		}
		link.UpdateFromPoints(false, false);
	}
	diagram.Invalidate();
}

void MoveNode(DiagramNode node, float newY)
{
	var bounds = node.Bounds;
	bounds.Y = newY - bounds.Height / 2;
	node.SetBounds(bounds, false, false);
} 



You could also consider using LayeredLayout - it provides StartNode and EndNode properties.

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


I love YaBB 1G - SP1!

Posts: 183
Joined: Sep 4th, 2011
Re: State Diagram (Machine) Using NetDiagram
Reply #5 - Jun 5th, 2012 at 1:05pm
Print Post  
Hi Stoyan,

I have yet to try your suggestions, but they sound promising.
Thanks for the prompt reply.

Jim
  
Back to top
 
IP Logged
 
jlj30
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 183
Joined: Sep 4th, 2011
Re: State Diagram (Machine) Using NetDiagram
Reply #6 - Jun 5th, 2012 at 5:40pm
Print Post  
Stoyan,

Your code to mirror the diagram worked perfectly and accomplished exactly what I needed.

However, the other code snippet to deal with the center'd text issue does not work - the if condition is never true. Any thoughts?

Thanks

Jim
  
Back to top
 
IP Logged
 
jlj30
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 183
Joined: Sep 4th, 2011
Re: State Diagram (Machine) Using NetDiagram
Reply #7 - Jun 5th, 2012 at 6:32pm
Print Post  
Hi again,

Looks like there are actually 3 segments in the horizontal and vertical links, so I just needed to check for 3 as well as use 4 control points.  Code is now working great.

Thanks

Jim
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint