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