diff --git a/OSCADSharp/OSCADSharp.UnitTests/Transforms/RotateTests.cs b/OSCADSharp/OSCADSharp.UnitTests/Transforms/RotateTests.cs index 892fbe6..e776aeb 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/Transforms/RotateTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/Transforms/RotateTests.cs @@ -35,5 +35,17 @@ namespace OSCADSharp.UnitTests.Transforms string script = cyl.ToString(); Assert.IsTrue(script.Contains("rotate([xAngle, yAngle, 120])")); } + + [TestMethod] + public void Rotate_CanMultiplyAVectorVariableInline() + { + var rotation = new Variable("cubeRot", 15); + + var cube = new Cube(20, 20, 80, true).Rotate(rotation * 1.5, 30, 30); + + string script = cube.ToString(); + + Assert.IsTrue(script.Contains("rotate([cubeRot * 1.5, 30, 30])")); + } } } diff --git a/OSCADSharp/OSCADSharp/Scripting/Variable.cs b/OSCADSharp/OSCADSharp/Scripting/Variable.cs index dd2100d..695c627 100644 --- a/OSCADSharp/OSCADSharp/Scripting/Variable.cs +++ b/OSCADSharp/OSCADSharp/Scripting/Variable.cs @@ -45,6 +45,30 @@ namespace OSCADSharp.Scripting } #region Operators + private static Variable applyMixedOperatorLeft(string oprtor, Variable left, object right, Func calcMethod) + { + if (VariableCalculator.IsNumeric(right)) + { + return new Variable(String.Format("{0} {1} {2}", left.Name, oprtor, right.ToString()), + calcMethod(left.Value, right)); + } + + throw new NotSupportedException(String.Format("Cannot use {0} operator on a variable with an object of type {1}", + oprtor, typeof(object).ToString())); + } + + private static Variable applyMixedOperatorRight(string oprtor, object left, Variable right, Func calcMethod) + { + if (VariableCalculator.IsNumeric(left)) + { + return new Variable(String.Format("{0} {1} {2}", left.ToString(), oprtor, right.Name), + calcMethod(left, right.Value)); + } + + throw new NotSupportedException(String.Format("Cannot use {0} operator on a variable with an object of type {1}", + oprtor, typeof(object).ToString())); + } + /// /// Adds two variables together /// @@ -56,6 +80,28 @@ namespace OSCADSharp.Scripting return new Variable(String.Format("{0} + {1}", left.Name, right.Name), VariableCalculator.Add(left.Value, right.Value)); } + /// + /// Adds a value to a variable + /// + /// + /// + /// + public static Variable operator +(Variable left, object right) + { + return applyMixedOperatorLeft("+", left, right, VariableCalculator.Add); + } + + /// + /// Adds a value to a variable + /// + /// + /// + /// + public static Variable operator +(object left, Variable right) + { + return applyMixedOperatorRight("+", left, right, VariableCalculator.Add); + } + /// /// Subtracts two variables /// @@ -67,6 +113,28 @@ namespace OSCADSharp.Scripting return new Variable(String.Format("{0} - {1}", left.Name, right.Name), VariableCalculator.Subtract(left.Value, right.Value)); } + /// + /// Subtracts a value from a variable + /// + /// + /// + /// + public static Variable operator -(Variable left, object right) + { + return applyMixedOperatorLeft("-", left, right, VariableCalculator.Subtract); + } + + /// + /// Subtracts a value from a variable + /// + /// + /// + /// + public static Variable operator -(object left, Variable right) + { + return applyMixedOperatorRight("-", left, right, VariableCalculator.Subtract); + } + /// /// Multiplies two variables /// @@ -78,6 +146,28 @@ namespace OSCADSharp.Scripting return new Variable(String.Format("{0} * {1}", left.Name, right.Name), VariableCalculator.Multiply(left.Value, right.Value)); } + /// + /// Multiplies a variable by a value + /// + /// + /// + /// + public static Variable operator *(Variable left, object right) + { + return applyMixedOperatorLeft("*", left, right, VariableCalculator.Multiply); + } + + /// + /// Multiplies a variable by a value + /// + /// + /// + /// + public static Variable operator *(object left, Variable right) + { + return applyMixedOperatorRight("*", left, right, VariableCalculator.Multiply); + } + /// /// Divides two variables /// @@ -88,6 +178,28 @@ namespace OSCADSharp.Scripting { return new Variable(String.Format("{0} / {1}", left.Name, right.Name), VariableCalculator.Divide(left.Value, right.Value)); } + + /// + /// Divides a variable by a value + /// + /// + /// + /// + public static Variable operator /(Variable left, object right) + { + return applyMixedOperatorLeft("/", left, right, VariableCalculator.Divide); + } + + /// + /// Divides a variable by a value + /// + /// + /// + /// + public static Variable operator /(object left, Variable right) + { + return applyMixedOperatorRight("/", left, right, VariableCalculator.Divide); + } #endregion } } diff --git a/OSCADSharp/OSCADSharp/Scripting/VariableCalculator.cs b/OSCADSharp/OSCADSharp/Scripting/VariableCalculator.cs index 0dc40b6..4c0976c 100644 --- a/OSCADSharp/OSCADSharp/Scripting/VariableCalculator.cs +++ b/OSCADSharp/OSCADSharp/Scripting/VariableCalculator.cs @@ -10,12 +10,12 @@ namespace OSCADSharp.Scripting { internal static class VariableCalculator { - private static bool isNumeric(object value) + internal static bool IsNumeric(object value) { return value is int || value is double || value is float || value is decimal; } - private static bool isVector(object value) + internal static bool IsVector(object value) { return value is Vector3 || value is BindableVector; } @@ -24,9 +24,9 @@ namespace OSCADSharp.Scripting { object result = null; - if (isNumeric(left) && isNumeric(right)) + if (IsNumeric(left) && IsNumeric(right)) result = Expression.Lambda>(expr).Compile()(); - if (isVector(left) || isVector(right)) + if (IsVector(left) || IsVector(right)) result = Expression.Lambda>(expr).Compile()(); return result; @@ -35,9 +35,9 @@ namespace OSCADSharp.Scripting private static BinaryExpression makeExpression(Func methodToUse, object left, object right) { - if (isNumeric(left)) + if (IsNumeric(left)) left = Convert.ToDouble(left); - if (isNumeric(right)) + if (IsNumeric(right)) right = Convert.ToDouble(right); var leftExpr = Expression.Constant(left, left.GetType());