Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Layout/Tree.cs @ 11072

Last change on this file since 11072 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 3.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4namespace Netron.Diagramming.Core.Layout {
5  public static class Tree {
6
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.");
10
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
19
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
28
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));
36
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);
45
46        labels[s1].Connectors[0].AttachConnector(cn.From);
47        shape.Connectors[0].AttachConnector(cn.To);
48        bundle.Entities.Add(cn);
49
50      }
51
52      #endregion
53
54      return bundle;
55    }
56
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();
63
64      SimpleRectangle rec1 = new SimpleRectangle();
65      rec1.Location = new Point(100, 30);
66
67      SimpleRectangle rec2 = new SimpleRectangle();
68      rec2.Location = new Point(50, 80);
69
70      SimpleRectangle rec3 = new SimpleRectangle();
71      rec3.Location = new Point(150, 80);
72
73
74      Connection cn1 = new Connection();
75      rec1.Connectors[2].AttachConnector(cn1.From);
76      rec2.Connectors[0].AttachConnector(cn1.To);
77
78      Connection cn2 = new Connection();
79      rec1.Connectors[2].AttachConnector(cn2.From);
80      rec3.Connectors[0].AttachConnector(cn2.To);
81
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);
87
88      return bundle;
89
90
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.