123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SAGA.RevitUtils
- {
- /// <summary>
- /// 图节点信息
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class GraphNode<T> where T: GraphNode<T>
- {
- public GraphNode()
- {
- Children = new GraphNodeCollection<T>(this as T);
- }
- /// <summary>
- /// 节点Id
- /// </summary>
- public string Id { get; set; }
- /// <summary>
- /// 节点名称
- /// </summary>
- public string Name { get; set; }
- #region 父子关联元素处理
- public GraphNodeCollection<T> Children { get; private set; }
- public T Parent { get; private set; }
- #endregion
- /// <summary>
- /// 关联相关元素
- /// </summary>
- public object Tag { get; set; }
- /// <summary>
- /// 获取根节点
- /// </summary>
- /// <returns></returns>
- public T GetRoot()
- {
- T root = this as T;
- if (root == null)
- return root;
- while ((root.Parent)!= null)
- {
- root = root.Parent;
- }
- return root;
- }
- /// <summary>
- /// 节点深度
- /// </summary>
- public int Deep
- {
- get
- {
- int deep = 1;
- T root = this as T;
- if (root == null)
- return deep;
- while ((root.Parent) != null)
- {
- root = root.Parent;
- deep++;
- }
- return deep;
- }
- }
- /// <summary>
- /// 获取所有的叶子节点
- /// </summary>
- /// <returns></returns>
- public List<T> GetLeaves()
- {
- List<T> result=new List<T>();
- Queue<T> queue=new Queue<T>();
- queue.Enqueue(this as T);
- while (queue.Any())
- {
- var current = queue.Dequeue();
- if (current != null)
- {
- if (current.Children.Count == 0)
- {
- result.Add(current);
- }
- else
- {
- current.Children.ToList().ForEach(c=>queue.Enqueue(c));
- }
- }
- }
- return result;
- }
- #region 扩展数据模块
- protected Dictionary<string, object> m_DicRef = new Dictionary<string, object>();
- private const string CLASS = "{8576A703-2562-4613-896D-C97E362AA238}";
- /// <summary>
- /// 注册关联值
- /// </summary>
- public void SetRef<V>(string key, V refObject)
- {
- m_DicRef[key] = refObject;
- }
- /// <summary>
- /// 获取关联对象
- /// </summary>
- /// <typeparam name="V"></typeparam>
- /// <param name="key"></param>
- /// <returns></returns>
- public V GetRef<V>(string key)
- {
- if (!m_DicRef.ContainsKey(key))
- return default(V);
- return (V)m_DicRef[key];
- }
-
- /// <summary>
- /// 设置相关联分类信息
- /// </summary>
- /// <param name="className"></param>
- public void SetClass<V>(V className)
- {
- SetRef(CLASS, className);
- }
- public V GetClass<V>()
- {
- return GetRef<V>(CLASS);
- }
- #endregion
- #region 节点集合类
- public class GraphNodeCollection<N> : ObservableCollection<N> where N : GraphNode<N>
- {
- private N m_Parent;
- internal GraphNodeCollection(N parent)
- {
- m_Parent = parent;
-
- }
- protected override void ClearItems()
- {
- foreach (var item in this)
- {
- if (item != null)
- {
- item.Parent = null;
- }
- }
- base.ClearItems();
- }
- protected override void InsertItem(int index, N item)
- {
- base.InsertItem(index, item);
- item.Parent = m_Parent;
- }
- protected override void RemoveItem(int index)
- {
- if (this[index] != null)
- {
- this[index].Parent = null;
- }
- base.RemoveItem(index);
- }
- protected override void SetItem(int index, N item)
- {
- base.SetItem(index, item);
- item.Parent = m_Parent;
- }
- public void AddRange(IEnumerable<N> items)
- {
- foreach (var item in items)
- {
- //item.Parent = m_Parent;
- this.Add(item);
- }
- }
- public void Reverse()
- {
- var list = this.Items as List<T>;
- if (list != null)
- {
- list.Reverse();
- }
- }
-
- }
- #endregion
- }
- }
|