diff --git a/OSCADSharp/OSCADSharp/Internal/Bindings/BindableVector.cs b/OSCADSharp/OSCADSharp/Internal/Bindings/BindableVector.cs index 46ebc2b..3ff6274 100644 --- a/OSCADSharp/OSCADSharp/Internal/Bindings/BindableVector.cs +++ b/OSCADSharp/OSCADSharp/Internal/Bindings/BindableVector.cs @@ -18,6 +18,11 @@ namespace OSCADSharp.Bindings public BindableVector(Vector3 vector, Dictionary 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 synonyms = null) diff --git a/OSCADSharp/OSCADSharp/Internal/Bindings/Solids/CubeBindings.cs b/OSCADSharp/OSCADSharp/Internal/Bindings/Solids/CubeBindings.cs new file mode 100644 index 0000000..e95f677 --- /dev/null +++ b/OSCADSharp/OSCADSharp/Internal/Bindings/Solids/CubeBindings.cs @@ -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, IBindings + { + private Bindings bindings = new Bindings(); + private static readonly Dictionary sizeSynonyms = new Dictionary() + { + {"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 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 + }; + } + } +} diff --git a/OSCADSharp/OSCADSharp/Internal/Scripting/Solids/CubeScriptBuilder.cs b/OSCADSharp/OSCADSharp/Internal/Scripting/Solids/CubeScriptBuilder.cs new file mode 100644 index 0000000..eb6f1b2 --- /dev/null +++ b/OSCADSharp/OSCADSharp/Internal/Scripting/Solids/CubeScriptBuilder.cs @@ -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); + } + } +} diff --git a/OSCADSharp/OSCADSharp/OSCADSharp.csproj b/OSCADSharp/OSCADSharp/OSCADSharp.csproj index 89f7fad..fe551bf 100644 --- a/OSCADSharp/OSCADSharp/OSCADSharp.csproj +++ b/OSCADSharp/OSCADSharp/OSCADSharp.csproj @@ -44,8 +44,10 @@ + + diff --git a/OSCADSharp/OSCADSharp/Public/Solids/Cube.cs b/OSCADSharp/OSCADSharp/Public/Solids/Cube.cs index f426e47..9266b83 100644 --- a/OSCADSharp/OSCADSharp/Public/Solids/Cube.cs +++ b/OSCADSharp/OSCADSharp/Public/Solids/Cube.cs @@ -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"); - /// /// The Size of the cube in terms of X/Y/Z units /// - public Vector3 Size - { - get { return this.size; } - set { this.size = new BindableVector(value, sizeSynonyms); } - } + public Vector3 Size { get; set; } = new Vector3(1, 1, 1); /// /// 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) /// - 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 /// Indicates whether the cube should be centered on the origin 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(); } /// @@ -102,10 +94,8 @@ namespace OSCADSharp /// Script for this object 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(); } /// @@ -114,15 +104,11 @@ namespace OSCADSharp /// 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 sizeSynonyms = new Dictionary() - { - {"size.x", "x" }, - {"size.y", "y" }, - {"size.z", "z" }, - {"length", "x" }, - {"width", "y" }, - {"height", "z" } - }; - + private CubeBindings bindings = new CubeBindings(); /// /// Binds a a variable to a property on this object /// @@ -185,25 +161,7 @@ namespace OSCADSharp /// literal value of the property 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(this, property, variable); } #endregion }