mirror of
https://github.com/eliasstepanik/OSCADSharpDotnet7.git
synced 2026-01-22 10:48:27 +00:00
Added CubeBindings / CubeScriptBuilder
This commit is contained in:
parent
16d59a1a19
commit
977f7d4b79
@ -18,6 +18,11 @@ namespace OSCADSharp.Bindings
|
|||||||
|
|
||||||
public BindableVector(Vector3 vector, Dictionary<string, string> synonyms = null) : this(vector.X, vector.Y, vector.Z)
|
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)
|
public BindableVector(double x = 0, double y = 0, double z = 0, Dictionary<string, string> synonyms = null)
|
||||||
|
|||||||
@ -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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -44,8 +44,10 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Internal\Bindings\BindableBoolean.cs" />
|
<Compile Include="Internal\Bindings\BindableBoolean.cs" />
|
||||||
<Compile Include="Internal\Bindings\IBindings.cs" />
|
<Compile Include="Internal\Bindings\IBindings.cs" />
|
||||||
|
<Compile Include="Internal\Bindings\Solids\CubeBindings.cs" />
|
||||||
<Compile Include="Internal\Bindings\Solids\SphereBindings.cs" />
|
<Compile Include="Internal\Bindings\Solids\SphereBindings.cs" />
|
||||||
<Compile Include="Internal\ICloneable.cs" />
|
<Compile Include="Internal\ICloneable.cs" />
|
||||||
|
<Compile Include="Internal\Scripting\Solids\CubeScriptBuilder.cs" />
|
||||||
<Compile Include="Internal\Scripting\Solids\SphereScriptBuilder.cs" />
|
<Compile Include="Internal\Scripting\Solids\SphereScriptBuilder.cs" />
|
||||||
<Compile Include="Internal\Scripting\VariableCalculator.cs" />
|
<Compile Include="Internal\Scripting\VariableCalculator.cs" />
|
||||||
<Compile Include="Internal\Scripting\CompoundVariable.cs" />
|
<Compile Include="Internal\Scripting\CompoundVariable.cs" />
|
||||||
|
|||||||
@ -6,6 +6,8 @@ using System.Threading.Tasks;
|
|||||||
using OSCADSharp.Spatial;
|
using OSCADSharp.Spatial;
|
||||||
using OSCADSharp.Bindings;
|
using OSCADSharp.Bindings;
|
||||||
using OSCADSharp.Scripting;
|
using OSCADSharp.Scripting;
|
||||||
|
using OSCADSharp.Bindings.Solids;
|
||||||
|
using OSCADSharp.Scripting.Solids;
|
||||||
|
|
||||||
namespace OSCADSharp
|
namespace OSCADSharp
|
||||||
{
|
{
|
||||||
@ -15,33 +17,17 @@ namespace OSCADSharp
|
|||||||
public class Cube : OSCADObject
|
public class Cube : OSCADObject
|
||||||
{
|
{
|
||||||
#region Attributes
|
#region Attributes
|
||||||
private Vector3 size = new BindableVector(1, 1, 1, sizeSynonyms);
|
|
||||||
private bool center = false;
|
|
||||||
private BindableBoolean centerBinding = new BindableBoolean("center");
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The Size of the cube in terms of X/Y/Z units
|
/// The Size of the cube in terms of X/Y/Z units
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Vector3 Size
|
public Vector3 Size { get; set; } = new Vector3(1, 1, 1);
|
||||||
{
|
|
||||||
get { return this.size; }
|
|
||||||
set { this.size = new BindableVector(value, sizeSynonyms); }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If True, the center of the cube will be at 0, 0, 0
|
/// 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)
|
/// 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>
|
/// </summary>
|
||||||
public bool Center
|
public bool Center { get; set; } = false;
|
||||||
{
|
|
||||||
get { return this.center; }
|
|
||||||
set
|
|
||||||
{
|
|
||||||
this.center = value;
|
|
||||||
this.centerBinding.InnerValue = this.center.ToString().ToLower();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
@ -59,8 +45,14 @@ namespace OSCADSharp
|
|||||||
/// <param name="center">Indicates whether the cube should be centered on the origin</param>
|
/// <param name="center">Indicates whether the cube should be centered on the origin</param>
|
||||||
public Cube(Vector3 size = null, bool center = false)
|
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.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>
|
/// <summary>
|
||||||
@ -102,10 +94,8 @@ namespace OSCADSharp
|
|||||||
/// <returns>Script for this object</returns>
|
/// <returns>Script for this object</returns>
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return String.Format("cube(size = {0}, center = {1}); {2}",
|
var scriptBuilder = new CubeScriptBuilder(this.bindings, this);
|
||||||
this.Size.ToString(),
|
return scriptBuilder.GetScript();
|
||||||
this.centerBinding.IsBound ? this.centerBinding.ToString() : this.center.ToString().ToLower(),
|
|
||||||
Environment.NewLine); ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -114,15 +104,11 @@ namespace OSCADSharp
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override OSCADObject Clone()
|
public override OSCADObject Clone()
|
||||||
{
|
{
|
||||||
var size = this.size as BindableVector;
|
|
||||||
var center = this.centerBinding.Clone();
|
|
||||||
|
|
||||||
var clone = new Cube()
|
var clone = new Cube()
|
||||||
{
|
{
|
||||||
Name = this.Name,
|
Name = this.Name,
|
||||||
size = size.Clone(),
|
Size = this.Size.Clone(),
|
||||||
center = this.Center,
|
Center = this.Center,
|
||||||
centerBinding = center,
|
|
||||||
bindings = this.bindings.Clone()
|
bindings = this.bindings.Clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -166,17 +152,7 @@ namespace OSCADSharp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Bindings.Bindings bindings = new Bindings.Bindings();
|
private CubeBindings bindings = new CubeBindings();
|
||||||
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" }
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Binds a a variable to a property on this object
|
/// Binds a a variable to a property on this object
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -185,25 +161,7 @@ namespace OSCADSharp
|
|||||||
/// literal value of the property</param>
|
/// literal value of the property</param>
|
||||||
public override void Bind(string property, Variable variable)
|
public override void Bind(string property, Variable variable)
|
||||||
{
|
{
|
||||||
if (sizeSynonyms.ContainsKey(property.ToLower()))
|
bindings.Bind<Cube>(this, property, variable);
|
||||||
{
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user