Hi there,
I have developed a grasshopper component with Visual Studio in C# to filter nodes in a XML file.
I have managed to create a data tree, where nodes are grouped in lists in the data tree.
But the list-numbers are not ordered.
How to write code, so the list-numbers will appear as 0, 1, 2, 3, 4, 5 … and not 5, 6, 7, 9, 15, 16 … ?
Here is my C# Code:
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddTextParameter(“Path”, “Path”, “Path to XML file”, GH_ParamAccess.item);
}
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddTextParameter("PointsTree", "PointsTree", "Temporary output", GH_ParamAccess.tree);
protected override void SolveInstance(IGH_DataAccess DA)
{
String path = System.String.Empty;
DA.GetData(0, ref path);
XmlDocument osmFile = new XmlDocument();
osmFile.Load(path);
List<List<string>> pointsDataTree = new List<List<string>>();
XmlNodeList wayNodes = osmFile.SelectNodes("osm/way");
foreach (XmlNode way in wayNodes)
{
List<string> wayPoints = new List<string>();
XmlNodeList refPoints = way.SelectNodes("nd");
foreach (XmlNode refPoint in refPoints)
{
string reference = refPoint.Attributes["ref"].Value;
var wayNode = new OSMNode();
wayNode.Node = reference;
XmlNodeList tags = way.SelectNodes("tag");
foreach (XmlNode tag in tags)
{
string key = tag.Attributes["k"].Value;
var wayKey = new OSMWay();
wayKey.Key = key;
if (key == "cycleway")
{
wayPoints.Add(reference);
}
}
}
pointsDataTree.Add(wayPoints);
}
DataTree<String> cyclewayPoints = new DataTree<String>();
for (int i = 0; i < pointsDataTree.Count; i++)
{
List<string> way = pointsDataTree[i];
for (int j = 0; j < way.Count; j++)
{
string point = way[j];
cyclewayPoints.Add(point, new GH_Path(i));
}
}
DA.SetDataTree(0, cyclewayPoints);