1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using System.Drawing;
|
---|
5 | namespace Netron.Diagramming.Core.Layout
|
---|
6 | {
|
---|
7 | public static class Tree
|
---|
8 | {
|
---|
9 |
|
---|
10 | public static Bundle CreateRandomTree<S>(Size bounds, int amount) where S : SimpleShapeBase, new()
|
---|
11 | {
|
---|
12 | if (new S().Connectors.Count == 0)
|
---|
13 | throw new InconsistencyException("The shape type specified '" + typeof(S).Name + "' has no connectors and cannot be used to create a tree. Please choose another type based on the SimpleShapeBase class.");
|
---|
14 |
|
---|
15 | #region Preamble
|
---|
16 | int counter = 0;
|
---|
17 | Dictionary<int, S> labels = new Dictionary<int, S>();
|
---|
18 | Random rnd = new Random();
|
---|
19 | Bundle bundle = new Bundle();
|
---|
20 | Connection cn;
|
---|
21 | S shape;
|
---|
22 | #endregion
|
---|
23 |
|
---|
24 | #region Create the root
|
---|
25 | shape = new S();
|
---|
26 | shape.Text = "Root";
|
---|
27 | shape.Location = new Point(rnd.Next(10, bounds.Width - 30), rnd.Next(10, bounds.Height - 30));
|
---|
28 | labels.Add(0, shape);
|
---|
29 | bundle.Entities.Add(shape);
|
---|
30 | //this.diagramControl1.SetLayoutRoot(shape);
|
---|
31 | #endregion
|
---|
32 |
|
---|
33 | #region Add random nodes to the existing tree
|
---|
34 | for (int i = 0; i < amount; i++)
|
---|
35 | {
|
---|
36 | counter++;
|
---|
37 | shape = new S();
|
---|
38 | shape.Text = "Shape " + counter;
|
---|
39 | int s1;
|
---|
40 | shape.Location = new Point(rnd.Next(10, bounds.Width - 30), rnd.Next(10, bounds.Height - 30));
|
---|
41 |
|
---|
42 | labels.Add(counter, shape);
|
---|
43 | bundle.Entities.Add(shape);
|
---|
44 | //let's try to find a parent for this new node
|
---|
45 | s1 = rnd.Next(0, counter - 1);
|
---|
46 | while (labels[s1].Connectors[0].AttachedConnectors.Count >= rnd.Next(1, 9) && s1 <= counter - 5)
|
---|
47 | {
|
---|
48 | s1 = rnd.Next(0, counter - 1);
|
---|
49 | }
|
---|
50 | cn = new Connection(Point.Empty, Point.Empty);
|
---|
51 |
|
---|
52 | labels[s1].Connectors[0].AttachConnector(cn.From);
|
---|
53 | shape.Connectors[0].AttachConnector(cn.To);
|
---|
54 | bundle.Entities.Add(cn);
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 | #endregion
|
---|
59 |
|
---|
60 | return bundle;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// Only a utility function which returns a specific type of (handcrafted) tree.
|
---|
65 | /// </summary>
|
---|
66 | /// <returns></returns>
|
---|
67 | public static Bundle CreateSpecificTree()
|
---|
68 | {
|
---|
69 | Bundle bundle = new Bundle();
|
---|
70 |
|
---|
71 | SimpleRectangle rec1 = new SimpleRectangle();
|
---|
72 | rec1.Location = new Point(100, 30);
|
---|
73 |
|
---|
74 | SimpleRectangle rec2 = new SimpleRectangle();
|
---|
75 | rec2.Location = new Point(50, 80);
|
---|
76 |
|
---|
77 | SimpleRectangle rec3 = new SimpleRectangle();
|
---|
78 | rec3.Location = new Point(150, 80);
|
---|
79 |
|
---|
80 |
|
---|
81 | Connection cn1 = new Connection();
|
---|
82 | rec1.Connectors[2].AttachConnector(cn1.From);
|
---|
83 | rec2.Connectors[0].AttachConnector(cn1.To);
|
---|
84 |
|
---|
85 | Connection cn2 = new Connection();
|
---|
86 | rec1.Connectors[2].AttachConnector(cn2.From);
|
---|
87 | rec3.Connectors[0].AttachConnector(cn2.To);
|
---|
88 |
|
---|
89 | bundle.Entities.Add(rec1);
|
---|
90 | bundle.Entities.Add(rec2);
|
---|
91 | bundle.Entities.Add(rec3);
|
---|
92 | bundle.Entities.Add(cn1);
|
---|
93 | bundle.Entities.Add(cn2);
|
---|
94 |
|
---|
95 | return bundle;
|
---|
96 |
|
---|
97 |
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|