GraphNode.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SAGA.RevitUtils
  8. {
  9. /// <summary>
  10. /// 图节点信息
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. public class GraphNode<T> where T: GraphNode<T>
  14. {
  15. public GraphNode()
  16. {
  17. Children = new GraphNodeCollection<T>(this as T);
  18. }
  19. /// <summary>
  20. /// 节点Id
  21. /// </summary>
  22. public string Id { get; set; }
  23. /// <summary>
  24. /// 节点名称
  25. /// </summary>
  26. public string Name { get; set; }
  27. #region 父子关联元素处理
  28. public GraphNodeCollection<T> Children { get; private set; }
  29. public T Parent { get; private set; }
  30. #endregion
  31. /// <summary>
  32. /// 关联相关元素
  33. /// </summary>
  34. public object Tag { get; set; }
  35. /// <summary>
  36. /// 获取根节点
  37. /// </summary>
  38. /// <returns></returns>
  39. public T GetRoot()
  40. {
  41. T root = this as T;
  42. if (root == null)
  43. return root;
  44. while ((root.Parent)!= null)
  45. {
  46. root = root.Parent;
  47. }
  48. return root;
  49. }
  50. /// <summary>
  51. /// 节点深度
  52. /// </summary>
  53. public int Deep
  54. {
  55. get
  56. {
  57. int deep = 1;
  58. T root = this as T;
  59. if (root == null)
  60. return deep;
  61. while ((root.Parent) != null)
  62. {
  63. root = root.Parent;
  64. deep++;
  65. }
  66. return deep;
  67. }
  68. }
  69. /// <summary>
  70. /// 获取所有的叶子节点
  71. /// </summary>
  72. /// <returns></returns>
  73. public List<T> GetLeaves()
  74. {
  75. List<T> result=new List<T>();
  76. Queue<T> queue=new Queue<T>();
  77. queue.Enqueue(this as T);
  78. while (queue.Any())
  79. {
  80. var current = queue.Dequeue();
  81. if (current != null)
  82. {
  83. if (current.Children.Count == 0)
  84. {
  85. result.Add(current);
  86. }
  87. else
  88. {
  89. current.Children.ToList().ForEach(c=>queue.Enqueue(c));
  90. }
  91. }
  92. }
  93. return result;
  94. }
  95. #region 扩展数据模块
  96. protected Dictionary<string, object> m_DicRef = new Dictionary<string, object>();
  97. private const string CLASS = "{8576A703-2562-4613-896D-C97E362AA238}";
  98. /// <summary>
  99. /// 注册关联值
  100. /// </summary>
  101. public void SetRef<V>(string key, V refObject)
  102. {
  103. m_DicRef[key] = refObject;
  104. }
  105. /// <summary>
  106. /// 获取关联对象
  107. /// </summary>
  108. /// <typeparam name="V"></typeparam>
  109. /// <param name="key"></param>
  110. /// <returns></returns>
  111. public V GetRef<V>(string key)
  112. {
  113. if (!m_DicRef.ContainsKey(key))
  114. return default(V);
  115. return (V)m_DicRef[key];
  116. }
  117. /// <summary>
  118. /// 设置相关联分类信息
  119. /// </summary>
  120. /// <param name="className"></param>
  121. public void SetClass<V>(V className)
  122. {
  123. SetRef(CLASS, className);
  124. }
  125. public V GetClass<V>()
  126. {
  127. return GetRef<V>(CLASS);
  128. }
  129. #endregion
  130. #region 节点集合类
  131. public class GraphNodeCollection<N> : ObservableCollection<N> where N : GraphNode<N>
  132. {
  133. private N m_Parent;
  134. internal GraphNodeCollection(N parent)
  135. {
  136. m_Parent = parent;
  137. }
  138. protected override void ClearItems()
  139. {
  140. foreach (var item in this)
  141. {
  142. if (item != null)
  143. {
  144. item.Parent = null;
  145. }
  146. }
  147. base.ClearItems();
  148. }
  149. protected override void InsertItem(int index, N item)
  150. {
  151. base.InsertItem(index, item);
  152. item.Parent = m_Parent;
  153. }
  154. protected override void RemoveItem(int index)
  155. {
  156. if (this[index] != null)
  157. {
  158. this[index].Parent = null;
  159. }
  160. base.RemoveItem(index);
  161. }
  162. protected override void SetItem(int index, N item)
  163. {
  164. base.SetItem(index, item);
  165. item.Parent = m_Parent;
  166. }
  167. public void AddRange(IEnumerable<N> items)
  168. {
  169. foreach (var item in items)
  170. {
  171. //item.Parent = m_Parent;
  172. this.Add(item);
  173. }
  174. }
  175. public void Reverse()
  176. {
  177. var list = this.Items as List<T>;
  178. if (list != null)
  179. {
  180. list.Reverse();
  181. }
  182. }
  183. }
  184. #endregion
  185. }
  186. }