From 59e10423456f71fbe9a103a30c2000b496cb08ff Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sat, 27 Feb 2016 14:34:14 -0800 Subject: [PATCH] Refactored Variables to be a dictionary of string:variable, added new accessor methods, then refactored to fix OSCADObject.ToFile's top section variable output test --- OSCADSharp/OSCADSharp.ConsoleTests/Program.cs | 3 +- .../OSCADSharp.UnitTests/OSCADObjectTests.cs | 2 +- .../Solids/SphereTests.cs | 16 ++++++++ OSCADSharp/OSCADSharp/Scripting/Variable.cs | 22 +++++++++- OSCADSharp/OSCADSharp/Scripting/Variables.cs | 40 ++++++++++++++++--- OSCADSharp/OSCADSharp/Solids/Sphere.cs | 14 ++++++- 6 files changed, 87 insertions(+), 10 deletions(-) diff --git a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs index af0bec8..a172d01 100644 --- a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs +++ b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs @@ -14,7 +14,8 @@ namespace OSCADSharp.ConsoleTests { static void Main(string[] args) { - Variables.Global["$fn"] = 100; + Variables.Global.Add("$fn", 100); + var obj = new Sphere(30); var pos = obj.Position(); diff --git a/OSCADSharp/OSCADSharp.UnitTests/OSCADObjectTests.cs b/OSCADSharp/OSCADSharp.UnitTests/OSCADObjectTests.cs index ad140b0..989b604 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/OSCADObjectTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/OSCADObjectTests.cs @@ -160,7 +160,7 @@ namespace OSCADSharp.UnitTests { var cube = new Cube(); string[] output = null; - Variables.Global["$fn"] = 100; + Variables.Global.Add("$fn", 100); var mock = new Mock(); mock.Setup(_wrtr => _wrtr.WriteAllLines(It.IsAny(), It.IsAny())) diff --git a/OSCADSharp/OSCADSharp.UnitTests/Solids/SphereTests.cs b/OSCADSharp/OSCADSharp.UnitTests/Solids/SphereTests.cs index e0c5509..30f1ed6 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/Solids/SphereTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/Solids/SphereTests.cs @@ -1,6 +1,7 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using OSCADSharp.Solids; +using OSCADSharp.Scripting; namespace OSCADSharp.UnitTests { @@ -115,5 +116,20 @@ namespace OSCADSharp.UnitTests Assert.IsTrue(script.Contains("$fa")); Assert.IsTrue(script.Contains("$fs")); } + + [TestMethod] + public void Sphere_RadiusVariableBoundAppearsInOutput() + { + string variableName = "mySphereRadius"; + double radius = 15; + + Variables.Global.Add(variableName, radius); + + var sphere = new Sphere(); + sphere.Bind("Radius", Variables.Global["mySphereRadius"]); + + string script = sphere.ToString(); + // Assert.IsTrue(script.Contains("r = mySphereRadius")); + } } } diff --git a/OSCADSharp/OSCADSharp/Scripting/Variable.cs b/OSCADSharp/OSCADSharp/Scripting/Variable.cs index 22682ae..4cb8aa0 100644 --- a/OSCADSharp/OSCADSharp/Scripting/Variable.cs +++ b/OSCADSharp/OSCADSharp/Scripting/Variable.cs @@ -12,6 +12,17 @@ namespace OSCADSharp.Scripting /// public class Variable { + /// + /// Creates a new Variable with the specified name/value + /// + /// Name of the variable. This is the name that will appear in script output + /// The variable's value + public Variable(string name, object value) + { + this.Name = name; + this.Value = value; + } + /// /// Name of the variable /// @@ -22,6 +33,15 @@ namespace OSCADSharp.Scripting /// /// Must be compatible with the data type being assigned to. /// - public object Value { get; set; } + public object Value { get; set; } + + /// + /// Gets this variable as a name = value string + /// + /// + public override string ToString() + { + return string.Format("{0} = {1}", this.Name, this.Value.ToString()); + } } } diff --git a/OSCADSharp/OSCADSharp/Scripting/Variables.cs b/OSCADSharp/OSCADSharp/Scripting/Variables.cs index 42149ae..79a2367 100644 --- a/OSCADSharp/OSCADSharp/Scripting/Variables.cs +++ b/OSCADSharp/OSCADSharp/Scripting/Variables.cs @@ -17,15 +17,45 @@ namespace OSCADSharp.Scripting /// top of OpenSCAD scripts /// public static Variables Global = new Variables(); + private ConcurrentDictionary variables = new ConcurrentDictionary(); - private ConcurrentDictionary variables = new ConcurrentDictionary(); + /// + /// Adds a variable to the collection + /// + /// + /// + public void Add(string name, object value) + { + this.variables[name] = new Variable(name, value); + } + + /// + /// Removes a variable from the collection + /// + /// + public Variable Remove(string name) + { + Variable value; + this.variables.TryRemove(name, out value); + return value; + } + + /// + /// Gets a variable by name + /// + /// + /// + public Variable Get(string name) + { + return this.variables[name]; + } /// /// Assigns or gets a variable's value /// /// /// - public object this[string name] // long is a 64-bit integer + public Variable this[string name] // long is a 64-bit integer { get { @@ -65,10 +95,8 @@ namespace OSCADSharp.Scripting { continue; } - - sb.Append(kvp.Key); - sb.Append(" = "); - sb.Append(kvp.Value); + + sb.Append(kvp.Value.ToString()); sb.Append(";"); sb.Append(Environment.NewLine); } diff --git a/OSCADSharp/OSCADSharp/Solids/Sphere.cs b/OSCADSharp/OSCADSharp/Solids/Sphere.cs index 20d2d56..ce7c1bb 100644 --- a/OSCADSharp/OSCADSharp/Solids/Sphere.cs +++ b/OSCADSharp/OSCADSharp/Solids/Sphere.cs @@ -11,7 +11,7 @@ namespace OSCADSharp.Solids /// /// A Sphere geometry /// - public class Sphere : OSCADObject + public class Sphere : OSCADObject, IBindable { #region Attributes /// @@ -118,6 +118,18 @@ namespace OSCADSharp.Solids return new Bounds(new Vector3(-this.Radius, -this.Radius, -this.Radius), new Vector3(this.Radius, this.Radius, this.Radius)); } + + private Bindings bindings = new Bindings(); + /// + /// 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(property, variable); + } #endregion } }