During function development, it is necessary to use attribute structures to make the Revit data structure itself clearer. When writing code, the MVVM pattern was adopted, and data collection and organization were completed in the backend. Recursive methods were used to classify the entire hierarchy.
This article references: https://www.cnblogs.com/yeyunfei/p/5408931.html
https://www.jianshu.com/p/908070cca109
https://www.cnblogs.com/larissa-0464/p/10227483.html
cases.
xaml code:
1 2 3 4 5 6 7
| <TreeView Name="TreeView" ItemsSource="{Binding TreeNodes}" HorizontalAlignment="Left" Height="325" Margin="25,60,0,0" VerticalAlignment="Top" Width="220"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Path= Nodes}"> <TextBlock Text="{Binding ElementName}"></TextBlock> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>
|
xaml.cs code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public MainWindow(List<Command.TreeNode> fds) { InitializeComponent(); Datas = fds; TreeView.DataContext = new WindowData(); } public class WindowData : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private List<Command.TreeNode> treeNodes = new List<Command.TreeNode>(); public List<Command.TreeNode> TreeNodes { get { return treeNodes; } set { treeNodes = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("TreeNodes")); } } public WindowData() { TreeNodes = GetFamilyData(0, Datas); } private List<Command.TreeNode> GetFamilyData(double parenId, List<Command.TreeNode> datas) { List<Command.TreeNode> mainDatas = datas.Where(t => t.ParentId == parenId).ToList<Command.TreeNode>(); List<Command.TreeNode> secondDatas = datas.Where(t => t.ParentId != parenId).ToList<Command.TreeNode>(); foreach (Command.TreeNode fd in mainDatas) { fd.Nodes = GetFamilyData(fd.Id, secondDatas); } return mainDatas; } }
|
Backend data processing code, feel automatic processing code is a bit complex, will optimize later:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| private List<TreeNode> GetTreeNodes(List<FamilyData> fds) { int v = 1; List<TreeNode> treeNodes = new List<TreeNode>(); for (int i = 0; i < fds.Count; i++) { TreeNode tn = new TreeNode(); tn.Id = v; tn.ParentId = 0; tn.ElementName = fds[i].Category.Name; v++; treeNodes.Add(tn); foreach (var dic in fds[i].FamilyAndSymbol) { TreeNode tn2 = new TreeNode(); tn2.ParentId = tn.Id; tn2.Id = v; v++; tn2.ElementName = dic.Key.Name; treeNodes.Add(tn2); foreach (FamilySymbol sym in dic.Value) { TreeNode tn3 = new TreeNode(); tn3.ParentId = tn2.Id; tn3.Id = v; tn3.ElementName = sym.Name; v++; treeNodes.Add(tn3); } } }
return treeNodes; }
|
Data Structure:
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class TreeNode { public double ParentId { get; set; } public string ElementName { get; set; } public double Id { get; set; } public ElementId ElementId { get; set; } public List<TreeNode> Nodes { get; set; } public TreeNode() { Nodes = new List<TreeNode>(); } }
|