This example shows how to traverse the Project Tree for a specific item. This example uses the IProjectTree61 (FindItem, GetItemName, HiddenGroupName) property and methods.
privatevoid ScanTree()
{
try
{
var sgworld = new SGWorld61();
MessageBox.Show("Click ok to find Vermont item by its path in the tree");
var id = sgworld.ProjectTree.FindItem("New England\\States\\Vermont");
if(id > 0)
MessageBox.Show("Found Vermont with id=" + id);
else
MessageBox.Show("New England\\States\\Vermont does not exist in tree");
// root is the first visible item in tree.
var root = sgworld.ProjectTree.GetNextItem(0, ItemCode.ROOT);
var tree = BuildTreeRecursive(root, 1);
MessageBox.Show(tree);
}
catch (Exception ex)
{
MessageBox.Show("Unexpected error: " + ex.Message);
}
}
private string BuildTreeRecursive(int current, int indent)
{
var sgworld = new SGWorld61();
// build padding
var padding = new string('-', indent * 3);
var result = string.Empty;
// iterate over all siblings of current node
while (current > 0)
{
// append node name to the tree string
var currentName = sgworld.ProjectTree.GetItemName(current);
result += padding + currentName + Environment.NewLine;
// if current node is group, recursively build tree from its first child;
if (sgworld.ProjectTree.IsGroup(current))
{
var child = sgworld.ProjectTree.GetNextItem(current, ItemCode.CHILD);
result += BuildTreeRecursive(child, indent + 1);
}
// move to next sibling
current = sgworld.ProjectTree.GetNextItem(current, ItemCode.NEXT);
}
return result;