2016-03-17 23:32:22 -07:00

103 lines
3.5 KiB
C#

using OSCADSharp.Spatial;
using System;
using System.Collections.Generic;
namespace OSCADSharp
{
/// <summary>
/// An object that's been resized to a specified set of X/Y/Z dimensions
/// </summary>
internal class ResizedObject : SingleStatementObject
{
/// <summary>
/// Size of the object in terms of X/Y/Z
/// </summary>
internal Vector3 Size { get; set; }
/// <summary>
/// Creates a resized object
/// </summary>
/// <param name="obj">The object(s) to be resized</param>
/// <param name="size">The size to resize to, in terms of x/y/z dimensions</param>
internal ResizedObject(OSCADObject obj, Vector3 size) : base(obj)
{
Size = new BindableVector(size);
}
internal ResizedObject(OSCADObject obj, Variable size) : base(obj)
{
this.Bind("size", size);
}
internal ResizedObject(OSCADObject obj, Vector3 size, Variable x, Variable y, Variable z) : base(obj)
{
this.Size = new BindableVector(size);
this.BindIfVariableNotNull("x", x);
this.BindIfVariableNotNull("y", y);
this.BindIfVariableNotNull("z", z);
}
internal ResizedObject(OSCADObject obj) : base(obj)
{
}
public override string ToString()
{
string size = this.bindings.Contains("size") ? this.bindings.Get("size").BoundVariable.Text : this.Size.ToString();
string resizeCommand = String.Format("resize({0})", size);
var formatter = new SingleBlockFormatter(resizeCommand, this.obj.ToString());
return formatter.ToString();
}
public override OSCADObject Clone()
{
var bndSize = this.Size as BindableVector;
return new ResizedObject(this.obj.Clone())
{
Name = this.Name,
bindings = this.bindings.Clone(),
Size = bndSize != null ? bndSize.Clone() : this.Size.Clone()
};
}
public override Vector3 Position()
{
var bounds = this.Bounds();
return Vector3.Average(bounds.BottomLeft, bounds.TopRight);
}
public override Bounds Bounds()
{
var oldBounds = obj.Bounds();
double xScaleFactor = this.Size.X > 0 ? this.Size.X / Math.Abs(oldBounds.XMax - oldBounds.XMin) : 1;
double yScaleFactor = this.Size.Y > 0 ? this.Size.Y / Math.Abs(oldBounds.YMax - oldBounds.YMin) : 1;
double zScaleFactor = this.Size.Z > 0 ? this.Size.Z / Math.Abs(oldBounds.ZMax - oldBounds.ZMin) : 1;
Vector3 scaleMultiplier = new Vector3(xScaleFactor, yScaleFactor, zScaleFactor);
return new Bounds(oldBounds.BottomLeft * scaleMultiplier, oldBounds.TopRight * scaleMultiplier);
}
private Bindings bindings = new Bindings(new Dictionary<string, string>() {
{ "size", "size" }
});
public override void Bind(string property, Variable variable)
{
var bindableVec = this.Size as BindableVector;
if(bindableVec != null && property == "x" || property == "y" || property == "z")
{
bindableVec.Bind(property, variable);
}
else
{
this.bindings.Add<ResizedObject>(this, property, variable);
}
}
}
}