mirror of
https://github.com/eliasstepanik/OSCADSharpDotnet7.git
synced 2026-01-24 19:48:28 +00:00
Added variable operators + one test so far
This commit is contained in:
parent
627569e6ed
commit
5afc0ed517
@ -47,5 +47,20 @@ namespace OSCADSharp.UnitTests.Transforms
|
|||||||
string script = cube.ToString();
|
string script = cube.ToString();
|
||||||
Assert.IsTrue(script.Contains("v = [-5, yAmt, zAmt]"));
|
Assert.IsTrue(script.Contains("v = [-5, yAmt, zAmt]"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Translate_AddingTwoVariablesYieldsInlineOperation()
|
||||||
|
{
|
||||||
|
var cylinDiameter = new Variable("cylinDiam", Inches.One);
|
||||||
|
var wallThickness = new Variable("wallThickness", Inches.Eigth);
|
||||||
|
|
||||||
|
var cylin = new Cylinder(cylinDiameter, cylinDiameter);
|
||||||
|
cylin.Height = 15;
|
||||||
|
|
||||||
|
var obj = cylin.Translate(cylinDiameter + wallThickness, 0, 0);
|
||||||
|
|
||||||
|
string script = obj.ToString();
|
||||||
|
Assert.IsTrue(script.Contains("v = [cylinDiam + wallThickness, 0, 0]"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,6 +43,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Bindings\BindableBoolean.cs" />
|
<Compile Include="Bindings\BindableBoolean.cs" />
|
||||||
|
<Compile Include="Scripting\VariableCalculator.cs" />
|
||||||
<Compile Include="Settings\Dependencies.cs" />
|
<Compile Include="Settings\Dependencies.cs" />
|
||||||
<Compile Include="Files\DefaultFileInvoker.cs" />
|
<Compile Include="Files\DefaultFileInvoker.cs" />
|
||||||
<Compile Include="Files\DefaultFileWriter.cs" />
|
<Compile Include="Files\DefaultFileWriter.cs" />
|
||||||
|
|||||||
@ -42,6 +42,52 @@ namespace OSCADSharp.Scripting
|
|||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return string.Format("{0} = {1}", this.Name, this.Value.ToString());
|
return string.Format("{0} = {1}", this.Name, this.Value.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Operators
|
||||||
|
/// <summary>
|
||||||
|
/// Adds two variables together
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="left"></param>
|
||||||
|
/// <param name="right"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static Variable operator +(Variable left, Variable right)
|
||||||
|
{
|
||||||
|
return new Variable(String.Format("{0} + {1}", left.Name, right.Name), VariableCalculator.Add(left.Value, right.Value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Subtracts two variables
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="left"></param>
|
||||||
|
/// <param name="right"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static Variable operator -(Variable left, Variable right)
|
||||||
|
{
|
||||||
|
return new Variable(String.Format("{0} - {1}", left.Name, right.Name), VariableCalculator.Subtract(left.Value, right.Value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Multiplies two variables
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="left"></param>
|
||||||
|
/// <param name="right"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static Variable operator *(Variable left, Variable right)
|
||||||
|
{
|
||||||
|
return new Variable(String.Format("{0} * {1}", left.Name, right.Name), VariableCalculator.Multiply(left.Value, right.Value));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Divides two variables
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="left"></param>
|
||||||
|
/// <param name="right"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static Variable operator /(Variable left, Variable right)
|
||||||
|
{
|
||||||
|
return new Variable(String.Format("{0} / {1}", left.Name, right.Name), VariableCalculator.Divide(left.Value, right.Value));
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
73
OSCADSharp/OSCADSharp/Scripting/VariableCalculator.cs
Normal file
73
OSCADSharp/OSCADSharp/Scripting/VariableCalculator.cs
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
using OSCADSharp.Bindings;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace OSCADSharp.Scripting
|
||||||
|
{
|
||||||
|
internal static class VariableCalculator
|
||||||
|
{
|
||||||
|
private static bool isNumeric(object value)
|
||||||
|
{
|
||||||
|
return value is int || value is double || value is float || value is decimal;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool isVector(object value)
|
||||||
|
{
|
||||||
|
return value is Vector3 || value is BindableVector;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object computeExpression(BinaryExpression expr, object left, object right)
|
||||||
|
{
|
||||||
|
object result = null;
|
||||||
|
|
||||||
|
if (isNumeric(left) && isNumeric(right))
|
||||||
|
result = Expression.Lambda<Func<double>>(expr).Compile()();
|
||||||
|
if (isVector(left) || isVector(right))
|
||||||
|
result = Expression.Lambda<Func<Vector3>>(expr).Compile()();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static BinaryExpression makeExpression(Func<ConstantExpression, ConstantExpression, BinaryExpression> methodToUse,
|
||||||
|
object left, object right)
|
||||||
|
{
|
||||||
|
if (isNumeric(left))
|
||||||
|
left = Convert.ToDouble(left);
|
||||||
|
if (isNumeric(right))
|
||||||
|
right = Convert.ToDouble(right);
|
||||||
|
|
||||||
|
var leftExpr = Expression.Constant(left, left.GetType());
|
||||||
|
var rightExpr = Expression.Constant(right, right.GetType());
|
||||||
|
BinaryExpression expr = methodToUse(leftExpr, rightExpr);
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static object Add(object left, object right)
|
||||||
|
{
|
||||||
|
BinaryExpression expr = makeExpression(Expression.Add, left, right);
|
||||||
|
return computeExpression(expr, left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static object Subtract(object left, object right)
|
||||||
|
{
|
||||||
|
BinaryExpression expr = makeExpression(Expression.Subtract, left, right);
|
||||||
|
return computeExpression(expr, left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static object Multiply(object left, object right)
|
||||||
|
{
|
||||||
|
BinaryExpression expr = makeExpression(Expression.Multiply, left, right);
|
||||||
|
return computeExpression(expr, left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static object Divide(object left, object right)
|
||||||
|
{
|
||||||
|
BinaryExpression expr = makeExpression(Expression.Divide, left, right);
|
||||||
|
return computeExpression(expr, left, right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -212,6 +212,28 @@ namespace OSCADSharp
|
|||||||
{
|
{
|
||||||
return new Vector3(left * right.X, left * right.Y, left * right.Z);
|
return new Vector3(left * right.X, left * right.Y, left * right.Z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Divides a vector by a double
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="left"></param>
|
||||||
|
/// <param name="right"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static Vector3 operator /(Vector3 left, double right)
|
||||||
|
{
|
||||||
|
return new Vector3(left.X / right, left.Y / right, left.Z / right);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Divides a vector by a double
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="left"></param>
|
||||||
|
/// <param name="right"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static Vector3 operator /(double left, Vector3 right)
|
||||||
|
{
|
||||||
|
return new Vector3(left / right.X, left / right.Y, left / right.Z);
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
internal Matrix ToMatrix()
|
internal Matrix ToMatrix()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user