Added numerical negation operator on variables

This commit is contained in:
Michael Smith 2016-03-06 15:24:25 -08:00
parent a048f8baa1
commit 61fc5cf227
2 changed files with 38 additions and 0 deletions

View File

@ -222,5 +222,22 @@ namespace OSCADSharp.UnitTests
Assert.IsTrue(script.Contains("d2 = mainColumn"));
Assert.IsTrue(script.Contains("h = overallHeight"));
}
[TestMethod]
public void Cylinder_NegationOnConstructorVariablesProvidesExpectedOutput()
{
Variable wheelThickness = new Variable("wheelThickness", Inches.Eigth + Inches.Sixteenth);
Variable wheelDiameter = new Variable("wheelDiameter", Inches.ToMillimeters(1.5));
Variable wheelHoleDiameter = new Variable("wheelHoleDiameter", Inches.Quarter);
OSCADObject cyl = new Cylinder(wheelHoleDiameter - 1, wheelHoleDiameter - 1, wheelThickness + 2)
.Translate(0, -wheelDiameter / 2 + wheelHoleDiameter / 2, 0);
string script = cyl.ToString();
Assert.IsTrue(script.Contains("translate(v = [0, -wheelDiameter / 2 + wheelHoleDiameter / 2, 0])"));
Assert.IsTrue(script.Contains("cylinder(center = false, d1 = wheelHoleDiameter - 1, d2 = wheelHoleDiameter - 1, h = wheelThickness + 2);"));
}
}
}

View File

@ -113,6 +113,27 @@ namespace OSCADSharp.Scripting
return new Variable(String.Format("{0} - {1}", left.Name, right.Name), VariableCalculator.Subtract(left.Value, right.Value));
}
/// <summary>
/// Numerical negation on a variable
/// </summary>
/// <param name="right"></param>
/// <returns></returns>
public static Variable operator -(Variable right)
{
object value = null;
if (VariableCalculator.IsNumeric(right.Value))
{
value = -Convert.ToDouble(right.Value);
}
else if(VariableCalculator.IsVector(right.Value))
{
value = ((Vector3)right.Value).Negate();
}
return new Variable(String.Format("-{0}", right.Name), value);
}
/// <summary>
/// Subtracts a value from a variable
/// </summary>