+ Text3D bindings

+ Text3D happy path binding tests
This commit is contained in:
Michael Smith 2016-02-29 23:52:25 -08:00
parent 53be8a6a1b
commit 54b8e6a686
3 changed files with 105 additions and 9 deletions

View File

@ -15,9 +15,17 @@ namespace OSCADSharp.ConsoleTests
static void Main(string[] args)
{
Variables.Global.Add("sphereRadius", 15);
Variables.Global.Add("cubeWidth", 10);
var obj = new Sphere();
obj.Bind("Radius", Variables.Global["sphereRadius"]);
OSCADObject obj = new Sphere();
((Sphere)obj).Bind("Radius", Variables.Global["sphereRadius"]);
var cube = new Cube();
cube.Bind("Width", Variables.Global["cubeWidth"]);
cube.Bind("Height", Variables.Global["sphereRadius"]);
cube.Size.X = 30;
obj = obj + cube;
var pos = obj.Position();
var cyl1 = new Cylinder(1, 100, true).Translate(pos);
@ -28,7 +36,7 @@ namespace OSCADSharp.ConsoleTests
//var topCorner = new Sphere().Translate(obj.Bounds().TopRight);
//var botCorner = new Sphere().Translate(obj.Bounds().BottomLeft);
(obj + axisHelper).ToFile("test.scad").Open();
(obj + axisHelper).ToFile("test.scad").Open();
//Console.ReadKey();
}

View File

@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OSCADSharp.Scripting;
using OSCADSharp.Solids;
using System;
using System.Collections.Generic;
@ -25,5 +26,62 @@ namespace OSCADSharp.UnitTests
{
var obj = new Text3D("BBBB", 16).Bounds();
}
[TestMethod]
public void Text_TextAndSizeBindingAffectsOutput()
{
var text = new Variable("txt", "yo");
var size = new Variable("txtSize", 34);
var obj = new Text3D();
obj.Bind("Text", text);
obj.Bind("Size", size);
string script = obj.ToString();
Assert.AreEqual(text.Value, obj.Text);
Assert.IsTrue(size.Value.ToString() == obj.Size.ToString());
Assert.IsTrue(script.Contains(String.Format("text(\"{0}\"", text.Name)));
Assert.IsTrue(script.Contains(String.Format("size = {0}", size.Name)));
}
[TestMethod]
public void Text_FontSpacingBindingAffectsOutput()
{
var font = new Variable("font", "wingdings");
var spacing = new Variable("spacing", 34);
var obj = new Text3D();
obj.Bind("font", font);
obj.Bind("SpacinG", spacing);
string script = obj.ToString();
Assert.AreEqual(font.Value, obj.Font);
Assert.IsTrue(spacing.Value.ToString() == obj.Spacing.ToString());
Assert.IsTrue(script.Contains(String.Format("font = {0}", font.Name)));
Assert.IsTrue(script.Contains(String.Format("spacing = {0}", spacing.Name)));
}
[TestMethod]
public void Text_TextDirectionLanguageBindingAffectsOutput()
{
var direction = new Variable("direction", "ltr");
var language = new Variable("language", "en");
var obj = new Text3D();
obj.Bind("textdirection", direction);
obj.Bind("language", language);
string script = obj.ToString();
Assert.AreEqual(direction.Value, obj.TextDirection);
Assert.AreEqual(language.Value, obj.Language);
Assert.IsTrue(script.Contains(String.Format("direction = {0}", direction.Name)));
Assert.IsTrue(script.Contains(String.Format("language = {0}", language.Name)));
}
}
}

View File

@ -5,13 +5,14 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OSCADSharp.Spatial;
using OSCADSharp.Bindings;
namespace OSCADSharp.Solids
{
/// <summary>
/// Create text using fonts installed on the local system or provided as separate font file.
/// </summary>
public class Text3D : OSCADObject
public class Text3D : OSCADObject, IBindable
{
#region Attributes
/// <summary>
@ -23,7 +24,7 @@ namespace OSCADSharp.Solids
/// The generated text will have approximately an ascent of the given value (height above the baseline). Default is 10.
/// Note that specific fonts will vary somewhat and may not fill the size specified exactly, usually slightly smaller.
/// </summary>
public uint? Size { get; set; } = null;
public int? Size { get; set; } = null;
/// <summary>
/// The name of the font that should be used. This is not the name of the font file,
@ -35,7 +36,7 @@ namespace OSCADSharp.Solids
/// <summary>
/// Factor to increase/decrease the character spacing. The default value of 1 will result in the normal spacing for the font, giving a value greater than 1 will cause the letters to be spaced further apart.
/// </summary>
public uint? Spacing { get; set; } = null;
public int? Spacing { get; set; } = null;
/// <summary>
/// Direction of the text flow. Possible values are "ltr" (left-to-right), "rtl" (right-to-left), "ttb" (top-to-bottom) and "btt" (bottom-to-top). Default is "ltr".
@ -63,7 +64,7 @@ namespace OSCADSharp.Solids
/// </summary>
/// <param name="text">Text to display</param>
/// <param name="size">Font size for the text</param>
public Text3D(string text, uint? size = null)
public Text3D(string text, int? size = null)
{
this.Text = text;
this.Size = size;
@ -95,10 +96,18 @@ namespace OSCADSharp.Solids
/// <returns>Script for this object</returns>
public override string ToString()
{
StatementBuilder sb = new StatementBuilder();
StatementBuilder sb = new StatementBuilder(this.bindings);
sb.Append("text(");
sb.Append("\"");
sb.Append(this.Text);
if (this.bindings.Contains("text"))
{
sb.Append(this.bindings.Get("text").BoundVariable.Name);
}
else
{
sb.Append(this.Text);
}
sb.Append("\"");
sb.AppendValuePairIfExists("size", this.Size?.ToString(), true);
@ -139,6 +148,27 @@ namespace OSCADSharp.Solids
{
throw new NotSupportedException("Bounds are not supported for objects using Text3D");
}
private Bindings.Bindings bindings = new Bindings.Bindings(new Dictionary<string, string>()
{
{ "text", "text" },
{ "size", "size" },
{ "font", "font" },
{ "spacing", "spacing" },
{ "textdirection", "direction" },
{ "language", "language" }
});
/// <summary>
/// Binds a a variable to a property on this object
/// </summary>
/// <param name="property">A string specifying the property such as "Diameter" or "Radius"</param>
/// <param name="variable">The variable to bind the to. This variable will appear in script output in lieu of the
/// literal value of the property</param>
public void Bind(string property, Variable variable)
{
this.bindings.Add<Text3D>(this, property, variable);
}
#endregion
}
}