+ Implemented Cube size bindings and synonyms for them

This commit is contained in:
Michael L Smith 2016-02-29 21:16:21 -08:00
parent 0d578131d8
commit 0989c4660c
4 changed files with 142 additions and 26 deletions

View File

@ -3,6 +3,7 @@ using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OSCADSharp.Solids;
using OSCADSharp.Scripting;
namespace OSCADSharp.UnitTests
{
@ -103,5 +104,45 @@ namespace OSCADSharp.UnitTests
Assert.AreEqual(new Vector3(2.5, 2.5, 10), obj.Bounds().TopRight);
Assert.AreEqual(new Vector3(-2.5, -2.5, -10), obj.Bounds().BottomLeft);
}
[TestMethod]
public void Cube_Size_XYZBindingsAppearInOutput()
{
Variable xValue = new Variable("xVal", 10.125);
Variable yValue = new Variable("yVal", 15.5);
Variable zValue = new Variable("zVal", 25);
var obj = new Cube();
obj.Bind("Size.X", xValue);
obj.Bind("Size.Y", yValue);
obj.Bind("Size.Z", zValue);
string script = obj.ToString();
Assert.AreEqual(Convert.ToDouble(xValue.Value), obj.Size.X);
Assert.AreEqual(Convert.ToDouble(yValue.Value), obj.Size.Y);
Assert.AreEqual(Convert.ToDouble(zValue.Value), obj.Size.Z);
Assert.IsTrue(script.Contains("size = [xVal, yVal, zVal]"));
}
[TestMethod]
public void Cube_Size_LengthWidthHeightindingsAppearInOutput()
{
Variable xValue = new Variable("xVal", 10.125);
Variable yValue = new Variable("yVal", 15.5);
Variable zValue = new Variable("zVal", 25);
var obj = new Cube();
obj.Bind("Length", xValue);
obj.Bind("Width", yValue);
obj.Bind("Height", zValue);
string script = obj.ToString();
Assert.AreEqual(Convert.ToDouble(xValue.Value), obj.Size.X);
Assert.AreEqual(Convert.ToDouble(yValue.Value), obj.Size.Y);
Assert.AreEqual(Convert.ToDouble(zValue.Value), obj.Size.Z);
Assert.IsTrue(script.Contains("size = [xVal, yVal, zVal]"));
}
}
}

View File

@ -16,6 +16,30 @@ namespace OSCADSharp.Bindings
{ "z", "z" }
});
public BindableVector(Vector3 vector, Dictionary<string, string> synonyms = null) : this(vector.X, vector.Y, vector.Z)
{
}
public BindableVector(double x = 0, double y = 0, double z = 0, Dictionary<string, string> synonyms = null)
{
this.X = x;
this.Y = y;
this.Z = z;
this.setSynonyms(synonyms);
}
private void setSynonyms(Dictionary<string, string> synonyms)
{
if (synonyms == null)
return;
foreach (KeyValuePair<string, string> item in synonyms)
{
this.bindings.Synonym(item.Value, item.Key);
}
}
public void Bind(string property, Variable variable)
{
this.bindings.Add<BindableVector>(this, property, variable);

View File

@ -13,9 +13,15 @@ namespace OSCADSharp.Bindings
#region Fields
private Dictionary<string, Binding> bindings = new Dictionary<string, Binding>();
private Dictionary<string, string> propertyNametoOpenSCADFieldMappings = new Dictionary<string, string>();
private Dictionary<string, string> synonyms = new Dictionary<string, string>();
#endregion
#region Constructors
public Bindings()
{
this.propertyNametoOpenSCADFieldMappings = new Dictionary<string, string>();
}
public Bindings(Dictionary<string, string> mappings)
{
this.propertyNametoOpenSCADFieldMappings = mappings;
@ -23,7 +29,7 @@ namespace OSCADSharp.Bindings
#endregion
#region Private Methods
private void SetProperty<T>(T instance, string property, Variable variable)
private void setProperty<T>(T instance, string property, Variable variable)
{
PropertyInfo[] properties;
properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
@ -31,7 +37,11 @@ namespace OSCADSharp.Bindings
for (int i = properties.Length - 1; i >= 0; i--)
{
var prop = properties[i];
if (prop.Name.ToLower() == property.ToLower())
string lProperty = property.ToLower();
if (this.hasMatchingSynonym(lProperty))
lProperty = this.synonyms[lProperty];
if (prop.Name.ToLower() == lProperty)
{
prop.SetValue(instance, variable.Value);
}
@ -43,9 +53,10 @@ namespace OSCADSharp.Bindings
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
private bool HasMapping(string propertyName)
private bool hasMapping(string propertyName)
{
return this.propertyNametoOpenSCADFieldMappings.ContainsKey(propertyName.ToLower());
return this.propertyNametoOpenSCADFieldMappings.ContainsKey(propertyName.ToLower())
|| this.hasMatchingSynonym(propertyName.ToLower());
}
/// <summary>
@ -54,21 +65,38 @@ namespace OSCADSharp.Bindings
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
private string PropertyToOpenSCADField(string propertyName)
private string propertyToOpenSCADField(string propertyName)
{
return this.propertyNametoOpenSCADFieldMappings[propertyName.ToLower()];
string lpropertyName = propertyName.ToLower();
if (this.hasMatchingSynonym(lpropertyName))
{
return this.synonymToOpenScadField(lpropertyName);
}
return this.propertyNametoOpenSCADFieldMappings[lpropertyName];
}
private void Add(Binding binding)
private void add(Binding binding)
{
bindings[binding.OpenSCADFieldName] = binding;
}
private bool hasMatchingSynonym(string synonymName)
{
return this.synonyms.ContainsKey(synonymName);
}
private string synonymToOpenScadField(string synonymName)
{
return this.propertyToOpenSCADField(this.synonyms[synonymName]);
}
#endregion
#region Internal API
internal void Add<T>(T instance, string propertyName, Variable variable)
{
if (!this.HasMapping(propertyName))
if (!this.hasMapping(propertyName))
{
throw new KeyNotFoundException(String.Format("No bindable property matching the name {0} was found"));
}
@ -76,24 +104,29 @@ namespace OSCADSharp.Bindings
//Assign mapping r -> radius -> variable
var binding = new Binding()
{
OpenSCADFieldName = this.PropertyToOpenSCADField(propertyName),
OpenSCADFieldName = this.propertyToOpenSCADField(propertyName),
BoundVariable = variable
};
//Set value of property to variable value
this.SetProperty<T>(instance, propertyName, variable);
this.Add(binding);
this.setProperty<T>(instance, propertyName, variable);
this.add(binding);
}
internal bool Contains(string propertyName)
internal bool Contains(string openScadFieldName)
{
return bindings.ContainsKey(propertyName);
return bindings.ContainsKey(openScadFieldName);
}
internal Binding Get(string propertyName)
{
return bindings[propertyName];
}
}
internal void Synonym(string propertyName, string alternateName)
{
this.synonyms[alternateName] = propertyName;
}
#endregion
}
}

View File

@ -15,10 +15,16 @@ namespace OSCADSharp.Solids
public class Cube : OSCADObject, IBindable
{
#region Attributes
private Vector3 size = new BindableVector(1, 1, 1, sizeSynonyms);
/// <summary>
/// The Size of the cube in terms of X/Y/Z units
/// </summary>
public Vector3 Size { get; set; } = new Vector3(1, 1, 1);
public Vector3 Size
{
get { return this.size; }
set { this.size = new BindableVector(value, sizeSynonyms); }
}
/// <summary>
/// If True, the center of the cube will be at 0, 0, 0
@ -43,7 +49,7 @@ namespace OSCADSharp.Solids
/// <param name="center">Indicates whether the cube should be centered on the origin</param>
public Cube(Vector3 size = null, bool center = false)
{
this.Size = size ?? new Vector3(1, 1, 1);
this.Size = new BindableVector(size, sizeSynonyms) ?? new BindableVector(1, 1, 1, sizeSynonyms);
this.Center = center;
}
@ -71,9 +77,8 @@ namespace OSCADSharp.Solids
/// <returns>Script for this object</returns>
public override string ToString()
{
return String.Format("cube(size = [{0}, {1}, {2}], center = {3}); {4}",
this.Size.X.ToString(), this.Size.Y.ToString(), this.Size.Z.ToString(),
this.Center.ToString().ToLower(), Environment.NewLine); ;
return String.Format("cube(size = {0}, center = {1}); {2}",
this.Size.ToString(), this.Center.ToString().ToLower(), Environment.NewLine); ;
}
/// <summary>
@ -127,11 +132,16 @@ namespace OSCADSharp.Solids
}
}
private Bindings.Bindings bindings = new Bindings.Bindings(new Dictionary<string, string>()
private Bindings.Bindings bindings = new Bindings.Bindings();
private static readonly Dictionary<string, string> sizeSynonyms = new Dictionary<string, string>()
{
{ "center", "center" },
{ "size", "size" }
});
{"size.x", "x" },
{"size.y", "y" },
{"size.z", "z" },
{"length", "x" },
{"width", "y" },
{"height", "z" }
};
/// <summary>
/// Binds a a variable to a property on this object
@ -141,8 +151,16 @@ namespace OSCADSharp.Solids
/// literal value of the property</param>
public void Bind(string property, Variable variable)
{
throw new NotImplementedException();
//this.bindings.Add<Cube>(this, property, variable);
if (sizeSynonyms.ContainsKey(property.ToLower()))
{
BindableVector vec;
if (this.size is BindableVector)
vec = this.Size as BindableVector;
else
vec = new BindableVector(this.size);
vec.Bind(property, variable);
}
}
#endregion
}