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

This commit is contained in:
Michael Smith 2016-02-27 14:34:14 -08:00
parent 6b628d9bca
commit 59e1042345
6 changed files with 87 additions and 10 deletions

View File

@ -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();

View File

@ -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<IFileWriter>();
mock.Setup(_wrtr => _wrtr.WriteAllLines(It.IsAny<string>(), It.IsAny<string[]>()))

View File

@ -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"));
}
}
}

View File

@ -12,6 +12,17 @@ namespace OSCADSharp.Scripting
/// </summary>
public class Variable
{
/// <summary>
/// Creates a new Variable with the specified name/value
/// </summary>
/// <param name="name">Name of the variable. This is the name that will appear in script output</param>
/// <param name="value">The variable's value</param>
public Variable(string name, object value)
{
this.Name = name;
this.Value = value;
}
/// <summary>
/// Name of the variable
/// </summary>
@ -22,6 +33,15 @@ namespace OSCADSharp.Scripting
///
/// Must be compatible with the data type being assigned to.
/// </summary>
public object Value { get; set; }
public object Value { get; set; }
/// <summary>
/// Gets this variable as a name = value string
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format("{0} = {1}", this.Name, this.Value.ToString());
}
}
}

View File

@ -17,15 +17,45 @@ namespace OSCADSharp.Scripting
/// top of OpenSCAD scripts
/// </summary>
public static Variables Global = new Variables();
private ConcurrentDictionary<string, Variable> variables = new ConcurrentDictionary<string, Variable>();
private ConcurrentDictionary<string, object> variables = new ConcurrentDictionary<string, object>();
/// <summary>
/// Adds a variable to the collection
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
public void Add(string name, object value)
{
this.variables[name] = new Variable(name, value);
}
/// <summary>
/// Removes a variable from the collection
/// </summary>
/// <param name="name"></param>
public Variable Remove(string name)
{
Variable value;
this.variables.TryRemove(name, out value);
return value;
}
/// <summary>
/// Gets a variable by name
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Variable Get(string name)
{
return this.variables[name];
}
/// <summary>
/// Assigns or gets a variable's value
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
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);
}

View File

@ -11,7 +11,7 @@ namespace OSCADSharp.Solids
/// <summary>
/// A Sphere geometry
/// </summary>
public class Sphere : OSCADObject
public class Sphere : OSCADObject, IBindable
{
#region Attributes
/// <summary>
@ -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();
/// <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(property, variable);
}
#endregion
}
}