mirror of
https://github.com/eliasstepanik/OSCADSharpDotnet7.git
synced 2026-01-13 06:28:35 +00:00
Added optional recursive flag to Children() to allow selection of immediate children instead of all descendants.
This commit is contained in:
parent
511cb45b6f
commit
96693e062b
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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>());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user