Added CubeBindings / CubeScriptBuilder

This commit is contained in:
Michael Smith 2016-03-09 23:23:29 -08:00
parent 16d59a1a19
commit 977f7d4b79
5 changed files with 130 additions and 59 deletions

View File

@ -18,6 +18,11 @@ namespace OSCADSharp.Bindings
public BindableVector(Vector3 vector, Dictionary<string, string> synonyms = null) : this(vector.X, vector.Y, vector.Z)
{
this.X = vector.X;
this.Y = vector.Y;
this.Z = vector.Z;
this.setSynonyms(synonyms);
}
public BindableVector(double x = 0, double y = 0, double z = 0, Dictionary<string, string> synonyms = null)

View File

@ -0,0 +1,75 @@
using OSCADSharp.Bindings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Bindings.Solids
{
internal class CubeBindings : ICloneable<CubeBindings>, IBindings
{
private Bindings bindings = new Bindings();
private static readonly Dictionary<string, string> sizeSynonyms = new Dictionary<string, string>()
{
{"size.x", "x" },
{"size.y", "y" },
{"size.z", "z" },
{"length", "x" },
{"width", "y" },
{"height", "z" }
};
public BindableVector SizeBinding = new BindableVector(new Vector3(), sizeSynonyms);
public BindableBoolean CenterBinding = new BindableBoolean("center");
public void Bind<T>(T obj, string property, Variable variable)
{
string prop = property.ToLower();
Cube cube = obj as Cube;
if (sizeSynonyms.ContainsKey(prop))
{
this.SizeBinding.X = cube.Size.X;
this.SizeBinding.Y = cube.Size.Y;
this.SizeBinding.Z = cube.Size.Z;
this.SizeBinding.Bind(prop, variable);
cube.Size = new Vector3(SizeBinding.X, SizeBinding.Y, SizeBinding.Z);
}
else if (prop == "center")
{
this.CenterBinding.Bind(prop, variable);
cube.Center = Convert.ToBoolean(variable.Value);
}
else
{
throw new KeyNotFoundException(String.Format("No bindable property matching the name {0} was found", prop));
}
}
public bool Contains(string openScadFieldName)
{
return this.bindings.Contains(openScadFieldName);
}
public Binding Get(string propertyName)
{
return this.bindings.Get(propertyName);
}
public void Synonym(string propertyName, string alternateName)
{
this.bindings.Synonym(propertyName, alternateName);
}
public CubeBindings Clone()
{
return new CubeBindings()
{
bindings = bindings.Clone(),
CenterBinding = this.CenterBinding,
SizeBinding = this.SizeBinding
};
}
}
}

View File

@ -0,0 +1,31 @@
using OSCADSharp.Bindings;
using OSCADSharp.Bindings.Solids;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Scripting.Solids
{
internal class CubeScriptBuilder
{
private CubeBindings bindings;
private Cube cube;
public CubeScriptBuilder(CubeBindings bindings, Cube cube)
{
this.bindings = bindings;
this.cube = cube;
}
internal string GetScript()
{
return String.Format("cube(size = {0}, center = {1}); {2}",
this.bindings.SizeBinding.ToString(),
this.bindings.CenterBinding.IsBound ? this.bindings.CenterBinding.ToString() :
this.cube.Center.ToString().ToLower(),
Environment.NewLine);
}
}
}

View File

@ -44,8 +44,10 @@
<ItemGroup>
<Compile Include="Internal\Bindings\BindableBoolean.cs" />
<Compile Include="Internal\Bindings\IBindings.cs" />
<Compile Include="Internal\Bindings\Solids\CubeBindings.cs" />
<Compile Include="Internal\Bindings\Solids\SphereBindings.cs" />
<Compile Include="Internal\ICloneable.cs" />
<Compile Include="Internal\Scripting\Solids\CubeScriptBuilder.cs" />
<Compile Include="Internal\Scripting\Solids\SphereScriptBuilder.cs" />
<Compile Include="Internal\Scripting\VariableCalculator.cs" />
<Compile Include="Internal\Scripting\CompoundVariable.cs" />

View File

@ -6,6 +6,8 @@ using System.Threading.Tasks;
using OSCADSharp.Spatial;
using OSCADSharp.Bindings;
using OSCADSharp.Scripting;
using OSCADSharp.Bindings.Solids;
using OSCADSharp.Scripting.Solids;
namespace OSCADSharp
{
@ -15,33 +17,17 @@ namespace OSCADSharp
public class Cube : OSCADObject
{
#region Attributes
private Vector3 size = new BindableVector(1, 1, 1, sizeSynonyms);
private bool center = false;
private BindableBoolean centerBinding = new BindableBoolean("center");
/// <summary>
/// The Size of the cube in terms of X/Y/Z units
/// </summary>
public Vector3 Size
{
get { return this.size; }
set { this.size = new BindableVector(value, sizeSynonyms); }
}
public Vector3 Size { get; set; } = new Vector3(1, 1, 1);
/// <summary>
/// If True, the center of the cube will be at 0, 0, 0
///
/// If False (default) one corner will be centered at 0,0, 0, with the cube extending into the positive octant (positive X/Y/Z)
/// </summary>
public bool Center
{
get { return this.center; }
set
{
this.center = value;
this.centerBinding.InnerValue = this.center.ToString().ToLower();
}
}
public bool Center { get; set; } = false;
#endregion
#region Constructors
@ -59,8 +45,14 @@ namespace OSCADSharp
/// <param name="center">Indicates whether the cube should be centered on the origin</param>
public Cube(Vector3 size = null, bool center = false)
{
this.Size = new BindableVector(size, sizeSynonyms) ?? new BindableVector(1, 1, 1, sizeSynonyms);
this.Size = size;
this.Center = center;
this.bindings.SizeBinding.X = size.X;
this.bindings.SizeBinding.Y = size.Y;
this.bindings.SizeBinding.Z = size.Z;
this.bindings.CenterBinding.InnerValue = this.Center.ToString().ToLower();
}
/// <summary>
@ -102,10 +94,8 @@ namespace OSCADSharp
/// <returns>Script for this object</returns>
public override string ToString()
{
return String.Format("cube(size = {0}, center = {1}); {2}",
this.Size.ToString(),
this.centerBinding.IsBound ? this.centerBinding.ToString() : this.center.ToString().ToLower(),
Environment.NewLine); ;
var scriptBuilder = new CubeScriptBuilder(this.bindings, this);
return scriptBuilder.GetScript();
}
/// <summary>
@ -114,15 +104,11 @@ namespace OSCADSharp
/// <returns></returns>
public override OSCADObject Clone()
{
var size = this.size as BindableVector;
var center = this.centerBinding.Clone();
var clone = new Cube()
{
Name = this.Name,
size = size.Clone(),
center = this.Center,
centerBinding = center,
Size = this.Size.Clone(),
Center = this.Center,
bindings = this.bindings.Clone()
};
@ -166,17 +152,7 @@ namespace OSCADSharp
}
}
private Bindings.Bindings bindings = new Bindings.Bindings();
private static readonly Dictionary<string, string> sizeSynonyms = new Dictionary<string, string>()
{
{"size.x", "x" },
{"size.y", "y" },
{"size.z", "z" },
{"length", "x" },
{"width", "y" },
{"height", "z" }
};
private CubeBindings bindings = new CubeBindings();
/// <summary>
/// Binds a a variable to a property on this object
/// </summary>
@ -185,25 +161,7 @@ namespace OSCADSharp
/// literal value of the property</param>
public override void Bind(string property, Variable 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);
}
else if(property.ToLower() == "center")
{
this.centerBinding.Bind(property, variable);
this.center = Convert.ToBoolean(variable.Value);
}
else
{
throw new KeyNotFoundException(String.Format("No bindable property matching the name {0} was found", property));
}
bindings.Bind<Cube>(this, property, variable);
}
#endregion
}