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) 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++) File.WriteAllLines("test.scad", new string[] { script.ToString() });
{
cube = cube.Translate(1, 0, 0);
}
string script = cube.ToString();
//File.WriteAllLines("test.scad", new string[] { script.ToString() });
Console.ReadKey(); Console.ReadKey();
} }
} }

View File

@ -42,5 +42,16 @@ namespace OSCADSharp.UnitTests
//But the child should be a different instance //But the child should be a different instance
Assert.IsFalse(clone.Children().FirstOrDefault() == text.Children().FirstOrDefault()); 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

@ -230,5 +230,38 @@ namespace OSCADSharp
return allChildren; 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>
<ItemGroup> <ItemGroup>
<Compile Include="Sizes.cs" /> <Compile Include="Sizes.cs" />
<Compile Include="Transforms\IMimicer.cs" />
<Compile Include="Transforms\LinearExtrudedObject.cs" /> <Compile Include="Transforms\LinearExtrudedObject.cs" />
<Compile Include="Scripting\BlockFormatter.cs" /> <Compile Include="Scripting\BlockFormatter.cs" />
<Compile Include="Scripting\BlockStatementObject.cs" /> <Compile Include="Scripting\BlockStatementObject.cs" />

View File

@ -10,7 +10,7 @@ namespace OSCADSharp.Transforms
/// <summary> /// <summary>
/// An object that has color and/or opacity applied to it /// An object that has color and/or opacity applied to it
/// </summary> /// </summary>
internal class ColoredObject : OSCADObject internal class ColoredObject : OSCADObject, IMimicer
{ {
#region Attributes #region Attributes
internal string ColorName { get; set; } = "Yellow"; internal string ColorName { get; set; } = "Yellow";
@ -45,5 +45,10 @@ namespace OSCADSharp.Transforms
{ {
return new ColoredObject(this.obj.Clone(), this.ColorName, this.Opacity); 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> /// <summary>
/// An object that's mirrored on a plane /// An object that's mirrored on a plane
/// </summary> /// </summary>
internal class MirroredObject : OSCADObject internal class MirroredObject : OSCADObject, IMimicer
{ {
/// <summary> /// <summary>
/// The normal vector of a plane intersecting the origin of the object, /// 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); 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> /// <summary>
/// An object that's been resized to a specified set of X/Y/Z dimensions /// An object that's been resized to a specified set of X/Y/Z dimensions
/// </summary> /// </summary>
internal class ResizedObject : OSCADObject internal class ResizedObject : OSCADObject, IMimicer
{ {
/// <summary> /// <summary>
/// Size of the object in terms of X/Y/Z /// 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); 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> /// <summary>
/// An object with rotation applied /// An object with rotation applied
/// </summary> /// </summary>
internal class RotatedObject : OSCADObject internal class RotatedObject : OSCADObject, IMimicer
{ {
/// <summary> /// <summary>
/// The angle to rotate, in terms of X/Y/Z euler angles /// 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); 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> /// <summary>
/// An object that's been rescaled /// An object that's been rescaled
/// </summary> /// </summary>
internal class ScaledObject : OSCADObject internal class ScaledObject : OSCADObject, IMimicer
{ {
/// <summary> /// <summary>
/// The scale factor to be applied /// The scale factor to be applied
@ -43,5 +43,10 @@ namespace OSCADSharp.Transforms
{ {
return new ScaledObject(this.obj.Clone(), this.ScaleFactor); 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> /// <summary>
/// An object or objects that have been moved along the specified vector /// An object or objects that have been moved along the specified vector
/// </summary> /// </summary>
internal class TranslatedObject : OSCADObject internal class TranslatedObject : OSCADObject, IMimicer
{ {
internal Vector3 Vector { get; set; } internal Vector3 Vector { get; set; }
private OSCADObject obj; private OSCADObject obj;
@ -40,5 +40,10 @@ namespace OSCADSharp.Transforms
{ {
return new TranslatedObject(this.obj.Clone(), this.Vector); return new TranslatedObject(this.obj.Clone(), this.Vector);
} }
public OSCADObject MimicObject(OSCADObject obj)
{
return new TranslatedObject(obj, this.Vector);
}
} }
} }