diff --git a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs index 35e02f9..349d8d5 100644 --- a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs +++ b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs @@ -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(); } diff --git a/OSCADSharp/OSCADSharp.UnitTests/Solids/Text3DTests.cs b/OSCADSharp/OSCADSharp.UnitTests/Solids/Text3DTests.cs index a9116ac..6e59477 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/Solids/Text3DTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/Solids/Text3DTests.cs @@ -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))); + } } } diff --git a/OSCADSharp/OSCADSharp/Solids/Text3D.cs b/OSCADSharp/OSCADSharp/Solids/Text3D.cs index c5a82c7..9a6dd78 100644 --- a/OSCADSharp/OSCADSharp/Solids/Text3D.cs +++ b/OSCADSharp/OSCADSharp/Solids/Text3D.cs @@ -5,13 +5,14 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using OSCADSharp.Spatial; +using OSCADSharp.Bindings; namespace OSCADSharp.Solids { /// /// Create text using fonts installed on the local system or provided as separate font file. /// - public class Text3D : OSCADObject + public class Text3D : OSCADObject, IBindable { #region Attributes /// @@ -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. /// - public uint? Size { get; set; } = null; + public int? Size { get; set; } = null; /// /// 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 /// /// 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. /// - public uint? Spacing { get; set; } = null; + public int? Spacing { get; set; } = null; /// /// 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 /// /// Text to display /// Font size for the text - 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 /// Script for this object 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() + { + { "text", "text" }, + { "size", "size" }, + { "font", "font" }, + { "spacing", "spacing" }, + { "textdirection", "direction" }, + { "language", "language" } + }); + + /// + /// Binds a a variable to a property on this object + /// + /// A string specifying the property such as "Diameter" or "Radius" + /// The variable to bind the to. This variable will appear in script output in lieu of the + /// literal value of the property + public void Bind(string property, Variable variable) + { + this.bindings.Add(this, property, variable); + } #endregion } }