Added 'mimic' method to copy identical transforms to other objects

This commit is contained in:
Mike Smith 2016-02-14 01:07:18 -08:00
parent e8c42deb30
commit 1746190165
11 changed files with 99 additions and 16 deletions

View File

@ -13,16 +13,11 @@ namespace OSCADSharp.ConsoleTests
{
static void Main(string[] args)
{
OSCADObject cube = new Cube(new Vector3(15, 15, 15));
var cube = new Cube(null, true).Translate(10, 0, 5).Scale(1, 1, 5);
var sphere = new Sphere().Mimic(cube).Translate(0, 0, 5);
string script = cube.Union(sphere).ToString();
for (int i = 0; i < 1000; i++)
{
cube = cube.Translate(1, 0, 0);
}
string script = cube.ToString();
//File.WriteAllLines("test.scad", new string[] { script.ToString() });
File.WriteAllLines("test.scad", new string[] { script.ToString() });
Console.ReadKey();
}
}

View File

@ -42,5 +42,16 @@ namespace OSCADSharp.UnitTests
//But the child should be a different instance
Assert.IsFalse(clone.Children().FirstOrDefault() == text.Children().FirstOrDefault());
}
[TestMethod]
public void OSCADObject_MimickedObjectHasSameTransform()
{
var cube = new Cube(null, true).Translate(10, 0, 5);
var sphere = new Sphere().Mimic(cube);
Assert.IsTrue(sphere.GetType() == cube.GetType());
Assert.IsTrue(cube.ToString().StartsWith("translate("));
Assert.IsTrue(sphere.ToString().StartsWith("translate("));
}
}
}

View File

@ -229,6 +229,39 @@ namespace OSCADSharp
return allChildren;
}
/// <summary>
/// Copies the transforms that have been applied to another OSCADObject, and applies
/// the same transforms to this object. (Only transforms)
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public OSCADObject Mimic(OSCADObject other)
{
IEnumerable<OSCADObject> children = other.Children();
Stack<OSCADObject> stack = new Stack<OSCADObject>();
OSCADObject finalObject = this;
stack.Push(other);
foreach (var child in children)
{
stack.Push(child);
}
while(stack.Count > 0)
{
var current = stack.Pop();
if(!(current is IMimicer))
{
continue;
}
else
{
finalObject = ((IMimicer)current).MimicObject(finalObject);
}
}
return finalObject;
}
}
}

View File

@ -41,6 +41,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Sizes.cs" />
<Compile Include="Transforms\IMimicer.cs" />
<Compile Include="Transforms\LinearExtrudedObject.cs" />
<Compile Include="Scripting\BlockFormatter.cs" />
<Compile Include="Scripting\BlockStatementObject.cs" />

View File

@ -10,7 +10,7 @@ namespace OSCADSharp.Transforms
/// <summary>
/// An object that has color and/or opacity applied to it
/// </summary>
internal class ColoredObject : OSCADObject
internal class ColoredObject : OSCADObject, IMimicer
{
#region Attributes
internal string ColorName { get; set; } = "Yellow";
@ -45,5 +45,10 @@ namespace OSCADSharp.Transforms
{
return new ColoredObject(this.obj.Clone(), this.ColorName, this.Opacity);
}
public OSCADObject MimicObject(OSCADObject obj)
{
return new ColoredObject(obj, this.ColorName, this.Opacity);
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Transforms
{
internal interface IMimicer
{
OSCADObject MimicObject(OSCADObject obj);
}
}

View File

@ -10,7 +10,7 @@ namespace OSCADSharp.Transforms
/// <summary>
/// An object that's mirrored on a plane
/// </summary>
internal class MirroredObject : OSCADObject
internal class MirroredObject : OSCADObject, IMimicer
{
/// <summary>
/// The normal vector of a plane intersecting the origin of the object,
@ -44,5 +44,10 @@ namespace OSCADSharp.Transforms
{
return new MirroredObject(this.obj.Clone(), this.Normal);
}
public OSCADObject MimicObject(OSCADObject obj)
{
return new MirroredObject(obj, this.Normal);
}
}
}

View File

@ -10,7 +10,7 @@ namespace OSCADSharp.Transforms
/// <summary>
/// An object that's been resized to a specified set of X/Y/Z dimensions
/// </summary>
internal class ResizedObject : OSCADObject
internal class ResizedObject : OSCADObject, IMimicer
{
/// <summary>
/// Size of the object in terms of X/Y/Z
@ -43,5 +43,10 @@ namespace OSCADSharp.Transforms
{
return new ResizedObject(this.obj.Clone(), this.Size);
}
public OSCADObject MimicObject(OSCADObject obj)
{
return new ResizedObject(obj, this.Size);
}
}
}

View File

@ -10,7 +10,7 @@ namespace OSCADSharp.Transforms
/// <summary>
/// An object with rotation applied
/// </summary>
internal class RotatedObject : OSCADObject
internal class RotatedObject : OSCADObject, IMimicer
{
/// <summary>
/// The angle to rotate, in terms of X/Y/Z euler angles
@ -43,5 +43,10 @@ namespace OSCADSharp.Transforms
{
return new RotatedObject(this.obj.Clone(), this.Angle);
}
public OSCADObject MimicObject(OSCADObject obj)
{
return new RotatedObject(obj, this.Angle);
}
}
}

View File

@ -10,7 +10,7 @@ namespace OSCADSharp.Transforms
/// <summary>
/// An object that's been rescaled
/// </summary>
internal class ScaledObject : OSCADObject
internal class ScaledObject : OSCADObject, IMimicer
{
/// <summary>
/// The scale factor to be applied
@ -43,5 +43,10 @@ namespace OSCADSharp.Transforms
{
return new ScaledObject(this.obj.Clone(), this.ScaleFactor);
}
public OSCADObject MimicObject(OSCADObject obj)
{
return new ScaledObject(obj, this.ScaleFactor);
}
}
}

View File

@ -10,7 +10,7 @@ namespace OSCADSharp.Transforms
/// <summary>
/// An object or objects that have been moved along the specified vector
/// </summary>
internal class TranslatedObject : OSCADObject
internal class TranslatedObject : OSCADObject, IMimicer
{
internal Vector3 Vector { get; set; }
private OSCADObject obj;
@ -40,5 +40,10 @@ namespace OSCADSharp.Transforms
{
return new TranslatedObject(this.obj.Clone(), this.Vector);
}
public OSCADObject MimicObject(OSCADObject obj)
{
return new TranslatedObject(obj, this.Vector);
}
}
}