Added Union (first Boolean operation)

This commit is contained in:
Mike Smith 2016-02-07 21:07:58 -08:00
parent 584049346f
commit efc981579f
4 changed files with 64 additions and 2 deletions

View File

@ -25,7 +25,15 @@ namespace OSCADSharp.ConsoleTests
.Scale(1, 1, 2)
.Translate(10, 5, 2);
Console.WriteLine(cube.ToString());
OSCADObject cylinder = new Cylinder()
{
Diameter = 25.4,
Height = 50.8
}.Translate(10, 5, 2);
var combined = cube.Union(cylinder);
Console.WriteLine(combined.ToString());
Console.ReadKey();
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Booleans
{
/// <summary>
/// A union of child nodes. This is the sum of all children (logical or).
/// </summary>
internal class Union : OSCADObject
{
private IEnumerable<OSCADObject> children;
/// <summary>
/// Create a union that is the combination of all children
/// </summary>
/// <param name="children">OSCADObjects to combine</param>
internal Union(IEnumerable<OSCADObject> children)
{
this.children = children;
}
public override string ToString()
{
string unionCommand = "union()";
StringBuilder sb = new StringBuilder();
foreach (var child in this.children)
{
sb.AppendLine(child.ToString());
}
var formatter = new BlockFormatter(unionCommand, sb.ToString());
return formatter.ToString();
}
}
}

View File

@ -1,4 +1,5 @@
using OSCADSharp.Transforms;
using OSCADSharp.Booleans;
using OSCADSharp.Transforms;
using System;
using System.Collections.Generic;
using System.Linq;
@ -133,5 +134,19 @@ namespace OSCADSharp
return this.Translate(new Vector3(x, y, z));
}
#endregion
#region Boolean Operations
public OSCADObject Union(params OSCADObject[] objects)
{
if (objects == null || objects.Length < 1)
{
throw new ArgumentException("Union requires at least one non-null entities");
}
IEnumerable<OSCADObject> children = new List<OSCADObject>() { this };
children = children.Concat(objects);
return new Union(children);
}
#endregion
}
}

View File

@ -41,6 +41,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BlockFormatter.cs" />
<Compile Include="Booleans\Union.cs" />
<Compile Include="OSCADObject.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Solids\Cube.cs" />