/// <summary> /// Adds new leaves into the treeView and contructs the branches to get there by recursion /// </summary> /// <param name="elementIds"></param> /// <param name="treeNodes"></param> /// <param name="tree"></param> /// <param name="depth"></param> /// <param name="lowestDepth"></param> private void AddNodesToTree( List <ElementId> elementIds, TreeNodeCollection treeNodes, TreeStructure tree, Depth depth, Depth lowestDepth = Depth.Instance) { // If the method currently is at the lowestDepth... if (depth == lowestDepth) { // For every elementId... TreeNode node; foreach (ElementId id in elementIds) { // Get a clone of the tree's cached node node = CloneTreeNode(tree.ElementIdNodes[id]); // Add the node into the treeNode treeNodes.Add(node); // Record the elementId and node to keep track of it and for easy access this.curElementIds.Add(id); this.leafNodes.Add(id, node); } // Exit out immediately return; } Depth nextDepth; SortedDictionary <string, List <ElementId> > grouping; // Get the next depth nextDepth = GetNextDepth(depth, lowestDepth); // Get the next grouping grouping = GetNextGrouping(elementIds, tree.ElementIdNodes, depth.ToString()); // Sort the dictionary in alphebetical order grouping.OrderBy(key => key.Key); List <TreeNode> branches; foreach (KeyValuePair <string, List <ElementId> > kvp in grouping) { branches = treeNodes.Find(kvp.Key, false).ToList(); if (branches.Count == 0) { TreeNode brnch = new TreeNode(); brnch.Name = kvp.Key; brnch.Text = kvp.Key; // Put it into treeView treeNodes.Add(brnch); // Add it into branches branches.Add(brnch); } foreach (TreeNode brnch in branches) { AddNodesToTree(kvp.Value, brnch.Nodes, tree, nextDepth); } } }
private void RemoveFromTree(List <NodeData> nodes, ElementSet set, Depth depth) { Depth newDepth; Dictionary <string, List <NodeData> > grouping; // Retrieve the next depth if (depth == Depth.Invalid) { // If current depth is invalid, then throw an exception, the invalid depth indicates that something has gone wrong. throw new ArgumentException(); } else if (depth == Depth.Instance) { // current depth is the last depth, set nextDepth to depth.Invalid newDepth = Depth.Invalid; foreach (NodeData data in nodes) { set.Set.Remove(data.Id); } return; } else { // Get next depth down newDepth = (Depth)((int)depth + 1); } // Get the grouping on a paramName for all the nodes (CategoryType, Category, Family, ElementType) grouping = GetGroupingByNodeData(nodes, depth.ToString()); // For each KeyValuePair in grouping... ElementSet newSet; foreach (KeyValuePair <string, List <NodeData> > kvp in grouping) { // Get a new ElementSet corresponding to kvp.Key newSet = set.GetElementSet(kvp.Key); // If newSet doesn't exist, then add a branch and set newSet to the newly created branch if (newSet == null) { throw new NullReferenceException(); } //----- Debug ------ /* * StringBuilder sb = new StringBuilder(); * sb.AppendLine(depth.ToString()); * sb.AppendLine(kvp.Key); * foreach (NodeData d in kvp.Value) * { * sb.AppendLine(d.Id.ToString()); * } * MessageBox.Show(sb.ToString()); */ //----- Debug ------ // Recurse down RemoveFromTree(kvp.Value, newSet, newDepth); if (newSet.Set.Count == 0) { // When returning from the recursion, if the branch's contents are empty, // then remove the branch from the current ElementSet set.RemoveBranch(kvp.Key); } // When returning from the recursion, // remove the elementIds found within kvp.Value foreach (NodeData data in kvp.Value) { set.Set.Remove(data.Id); } } }
private void AddToTree(List <NodeData> nodes, ElementSet set, Depth depth) { Depth newDepth; Dictionary <string, List <NodeData> > grouping; // Retrieve the next depth if (depth == Depth.Invalid) { // If current depth is invalid, then throw an exception, the invalid depth indicates that something has gone wrong. throw new ArgumentException(); } else if (depth == Depth.Instance) { // current depth is the last depth, set nextDepth to depth.Invalid newDepth = Depth.Invalid; foreach (NodeData data in nodes) { set.Set.Add(data.Id); } return; } else { // Get next depth down newDepth = (Depth)((int)depth + 1); } // Get the grouping on a paramName for all the nodes (CategoryType, Category, Family, ElementType) grouping = GetGroupingByNodeData(nodes, depth.ToString()); // For each KeyValuePair in grouping... ElementSet newSet; foreach (KeyValuePair <string, List <NodeData> > kvp in grouping) { // Get a new ElementSet corresponding to kvp.Key newSet = set.GetElementSet(kvp.Key); // If newSet doesn't exist, then add a branch and set newSet to the newly created branch if (newSet == null) { newSet = set.AddBranch(kvp.Key); set.Name = depth.ToString(); } //----- Debug ------ /* * StringBuilder sb = new StringBuilder(); * sb.AppendLine(depth.ToString()); * sb.AppendLine(kvp.Key); * foreach (NodeData d in kvp.Value) * { * sb.AppendLine(d.Id.ToString()); * } * MessageBox.Show(sb.ToString()); */ //----- Debug ------ // Recurse down AddToTree(kvp.Value, newSet, newDepth); // When returning from the recursion, add the newSet's set // onto set.Set, as it should be recently updated set.Set.UnionWith(newSet.Set); } }
/// <summary> /// Deletes existing leaves in the treeView and removes the branches if not needed by recursion /// </summary> /// <param name="elementIds"></param> /// <param name="treeNodes"></param> /// <param name="nodeDict"></param> /// <param name="depth"></param> /// <param name="lowestDepth"></param> private void DelNodesInTree( List <ElementId> elementIds, TreeNodeCollection treeNodes, Dictionary <ElementId, TreeNode> nodeDict, Depth depth, Depth lowestDepth = Depth.Instance) { // If the method currently is at the lowestDepth... if (depth == lowestDepth) { // For every elementId... TreeNode node; foreach (ElementId id in elementIds) { // Get the node from the node mapping node = nodeDict[id]; // Remove the node from treeView treeNodes.Remove(node); // Remove the node and elementId from the records this.curElementIds.Remove(id); this.leafNodes.Remove(id); } // Exit out immediately return; } Depth nextDepth; SortedDictionary <string, List <ElementId> > grouping; // Get next depth nextDepth = GetNextDepth(depth, lowestDepth); // Get next grouping grouping = GetNextGrouping(elementIds, nodeDict, depth.ToString()); // Sort the elements in alphebetical order grouping.OrderBy(key => key.Key); List <TreeNode> branches; foreach (KeyValuePair <string, List <ElementId> > kvp in grouping) { branches = treeNodes.Find(kvp.Key, false).ToList(); if (branches.Count == 0) { foreach (ElementId id in kvp.Value) { this.curElementIds.Remove(id); this.leafNodes.Remove(id); } continue; } foreach (TreeNode brnch in branches) { DelNodesInTree(kvp.Value, brnch.Nodes, nodeDict, nextDepth); if (brnch.Nodes.Count == 0) { brnch.Remove(); } } } }