Added optional recursive flag to Children() to allow selection of immediate children instead of all descendants.

This commit is contained in:
Michael Smith 2016-02-21 20:27:56 -08:00
parent 511cb45b6f
commit 96693e062b
2 changed files with 23 additions and 3 deletions

View File

@ -122,5 +122,19 @@ namespace OSCADSharp.UnitTests
Assert.AreEqual("Cube", children[4].Name);
Assert.AreEqual("Sphere", children[1].Name);
}
[TestMethod]
public void OSCADObject_ChildrenWithRecursiveFalseReturnsOnlyDirectChildren()
{
var firstLevel = new Sphere().Union(new Cube(), new Sphere(), new Cylinder());
firstLevel.Name = "Union";
var secondLevel = new Text3D() { Name = "Text" }.Difference(firstLevel);
var children = secondLevel.Children(false).ToList();
Assert.AreEqual("Text", children[0].Name);
Assert.AreEqual("Union", children[1].Name);
}
}
}

View File

@ -266,13 +266,19 @@ namespace OSCADSharp
/// Internal collection of children for this object
/// </summary>
protected List<OSCADObject> children = new List<OSCADObject>();
/// <summary>
/// Returns all children of this OSCADObject
/// Returns all chidren of this OSCADObject
/// </summary>
/// <param name="recursive">If true, returns all descendants. If false, returns only direct children.</param>
/// <returns></returns>
public IEnumerable<OSCADObject> Children()
public IEnumerable<OSCADObject> Children(bool recursive = true)
{
if(recursive == false)
{
return new List<OSCADObject>(this.children);
}
// Initial children are reversed here because for objects with multiple children (such as boolean operations)
// the natural collection order would yield opposite the expected order in a stack (first child would be the last popped)
Stack<OSCADObject> toTraverse = new Stack<OSCADObject>(this.children.Reverse<OSCADObject>());