mirror of
https://github.com/eliasstepanik/OSCADSharpDotnet7.git
synced 2026-01-11 21:48:34 +00:00
90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using OSCADSharp.DataBinding;
|
|
using OSCADSharp.Spatial;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace OSCADSharp
|
|
{
|
|
/// <summary>
|
|
/// An object that's been rescaled
|
|
/// </summary>
|
|
internal class ScaledObject : SingleStatementObject
|
|
{
|
|
/// <summary>
|
|
/// The scale factor to be applied
|
|
/// </summary>
|
|
internal Vector3 ScaleFactor { get; set; } = new BindableVector(1, 1, 1);
|
|
|
|
/// <summary>
|
|
/// Creates a scaled object
|
|
/// </summary>
|
|
/// <param name="obj">Object(s) to be scaled</param>
|
|
/// <param name="scale">Scale factor in x/y/z components</param>
|
|
internal ScaledObject(OSCADObject obj, Vector3 scale) : base(obj)
|
|
{
|
|
this.ScaleFactor = new BindableVector(scale);
|
|
}
|
|
|
|
internal ScaledObject(OSCADObject obj, Variable normal) : base(obj)
|
|
{
|
|
this.Bind("scalefactor", normal);
|
|
}
|
|
|
|
internal ScaledObject(OSCADObject obj, Vector3 scale, Variable x, Variable y, Variable z) : base(obj)
|
|
{
|
|
this.ScaleFactor = new BindableVector(scale);
|
|
|
|
this.BindIfVariableNotNull("x", x);
|
|
this.BindIfVariableNotNull("y", y);
|
|
this.BindIfVariableNotNull("z", z);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
string scale = this.bindings.Contains("scalefactor") ? this.bindings.Get("scalefactor").BoundVariable.Text : this.ScaleFactor.ToString();
|
|
|
|
string scaleCommand = String.Format("scale(v = {0})", scale);
|
|
var formatter = new SingleBlockFormatter(scaleCommand, this.obj.ToString());
|
|
return formatter.ToString();
|
|
}
|
|
|
|
public override OSCADObject Clone()
|
|
{
|
|
return new ScaledObject(this.obj.Clone(), this.ScaleFactor)
|
|
{
|
|
Name = this.Name,
|
|
bindings = this.bindings.Clone()
|
|
};
|
|
}
|
|
|
|
public override Vector3 Position()
|
|
{
|
|
return obj.Position() * this.ScaleFactor;
|
|
}
|
|
|
|
public override Bounds Bounds()
|
|
{
|
|
var oldBounds = obj.Bounds();
|
|
return new Bounds(oldBounds.BottomLeft * this.ScaleFactor, oldBounds.TopRight * this.ScaleFactor);
|
|
}
|
|
|
|
private Bindings bindings = new Bindings(new Dictionary<string, string>() {
|
|
{ "scalefactor", "scalefactor" }
|
|
});
|
|
public override void Bind(string property, Variable variable)
|
|
{
|
|
var bindableVec = this.ScaleFactor as BindableVector;
|
|
property = property == "scale" ? "scalefactor" : property;
|
|
|
|
if (bindableVec != null && property == "x" || property == "y" || property == "z")
|
|
{
|
|
bindableVec.Bind(property, variable);
|
|
}
|
|
else
|
|
{
|
|
this.bindings.Add<ScaledObject>(this, property, variable);
|
|
}
|
|
}
|
|
}
|
|
}
|