Merge pull request #2 from Exolun/SettingsOutput

Settings output
This commit is contained in:
Michael Smith 2016-02-24 11:46:03 -08:00
commit dc9db3b8ef
24 changed files with 377 additions and 27 deletions

View File

@ -14,8 +14,8 @@ namespace OSCADSharp.ConsoleTests
static void Main(string[] args)
{
var obj = new Cube(5, 5, 20)
.Translate(30, 0, 0).Rotate(0, 90, 0).Resize(2, 2, 2);
Settings.Globals["$fn"] = 100;
var obj = new Sphere(30);
var pos = obj.Position();
var cyl1 = new Cylinder(1, 100, true).Translate(pos);
@ -23,10 +23,10 @@ namespace OSCADSharp.ConsoleTests
var cyl3 = new Cylinder(1, 100, true).Rotate(90, 0, 0).Translate(pos);
var axisHelper = cyl1.Union(cyl2, cyl3).Color("Red");
var topCorner = new Sphere().Translate(obj.Bounds().TopRight);
var botCorner = new Sphere().Translate(obj.Bounds().BottomLeft);
//var topCorner = new Sphere().Translate(obj.Bounds().TopRight);
//var botCorner = new Sphere().Translate(obj.Bounds().BottomLeft);
(obj + topCorner + botCorner + axisHelper).ToFile("test.scad");
(obj + axisHelper).ToFile("test.scad");
//Console.ReadKey();
}

View File

@ -1,4 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using OSCADSharp.Scripting;
using OSCADSharp.Solids;
using System;
using System.Collections.Concurrent;
@ -135,6 +137,39 @@ namespace OSCADSharp.UnitTests
Assert.AreEqual("Text", children[0].Name);
Assert.AreEqual("Union", children[1].Name);
}
[TestMethod]
public void OSCADObject_ToFileIncludesOSCADSharpGeneratedHeader()
{
var cube = new Cube();
string[] output = null;
var mock = new Mock<IFileWriter>();
mock.Setup(_wrtr => _wrtr.WriteAllLines(It.IsAny<string>(), It.IsAny<string[]>()))
.Callback<string, string[]>((path, contents) => { output = contents; });
Settings.FileWriter = mock.Object;
cube.ToFile("myFile");
Assert.AreEqual(Settings.OSCADSharpHeader, output[0]);
}
[TestMethod]
public void OSCADObject_ToFileIncludesGlobalVariablesDefinedInSettings()
{
var cube = new Cube();
string[] output = null;
Settings.Globals["$fn"] = 100;
var mock = new Mock<IFileWriter>();
mock.Setup(_wrtr => _wrtr.WriteAllLines(It.IsAny<string>(), It.IsAny<string[]>()))
.Callback<string, string[]>((path, contents) => { output = contents; });
Settings.FileWriter = mock.Object;
cube.ToFile("myFile");
Assert.AreEqual("$fn = 100;\r\n", output[1]);
}
}
}

View File

@ -35,6 +35,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Moq, Version=4.2.1510.2205, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
</ItemGroup>
<Choose>
@ -75,6 +79,9 @@
<Name>OSCADSharp</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>

View File

@ -57,5 +57,34 @@ namespace OSCADSharp.UnitTests
Assert.AreEqual(new Vector3(2.5, 2.5, 10), obj.Bounds().TopRight);
Assert.AreEqual(new Vector3(-2.5, -2.5, -10), obj.Bounds().BottomLeft);
}
[TestMethod]
public void Cylinder_ScriptOutputDoesNotContainResolutionValuesIfNotSpecified()
{
var cylinder = new Cylinder();
string script = cylinder.ToString();
Assert.IsTrue(!script.Contains("$fn"));
Assert.IsTrue(!script.Contains("$fa"));
Assert.IsTrue(!script.Contains("$fs"));
}
[TestMethod]
public void Cylinder_ScriptOutpuHasResolutionValuesIfSpecified()
{
var cylinder = new Cylinder()
{
Resolution = 40,
MinimumAngle = 5,
MinimumCircumferentialLength = 2
};
string script = cylinder.ToString();
Assert.IsTrue(script.Contains("$fn"));
Assert.IsTrue(script.Contains("$fa"));
Assert.IsTrue(script.Contains("$fs"));
}
}
}

View File

@ -86,5 +86,34 @@ namespace OSCADSharp.UnitTests
Assert.AreEqual(new Vector3(15, 15, 15), obj.Bounds().TopRight);
Assert.AreEqual(new Vector3(-15, -15, -15), obj.Bounds().BottomLeft);
}
[TestMethod]
public void Sphere_ScriptOutputDoesNotContainResolutionValuesIfNotSpecified()
{
var sphere = new Sphere();
string script = sphere.ToString();
Assert.IsTrue(!script.Contains("$fn"));
Assert.IsTrue(!script.Contains("$fa"));
Assert.IsTrue(!script.Contains("$fs"));
}
[TestMethod]
public void Sphere_ScriptOutpuHasResolutionValuesIfSpecified()
{
var sphere = new Sphere()
{
Resolution = 40,
MinimumAngle = 5,
MinimumFragmentSize = 2
};
string script = sphere.ToString();
Assert.IsTrue(script.Contains("$fn"));
Assert.IsTrue(script.Contains("$fa"));
Assert.IsTrue(script.Contains("$fs"));
}
}
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Moq" version="4.2.1510.2205" targetFramework="net452" />
</packages>

View File

@ -262,6 +262,11 @@ namespace OSCADSharp
return this.ToString() == other.ToString();
}
/// <summary>
/// The parent of this object in its OSCADObject tree
/// </summary>
internal OSCADObject Parent { get; set; }
/// <summary>
/// Internal collection of children for this object
/// </summary>
@ -323,8 +328,13 @@ namespace OSCADSharp
{
path += ".scad";
}
File.WriteAllLines(path, new string[] { this.ToString() });
Settings.FileWriter.WriteAllLines(path, new string[]
{
Settings.OSCADSharpHeader,
Settings.Globals.ToString(),
this.ToString()
});
}
#endregion

View File

@ -42,7 +42,11 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Scripting\DefaultFileWriter.cs" />
<Compile Include="Scripting\IFileWriter.cs" />
<Compile Include="Ids.cs" />
<Compile Include="Scripting\StatementBuilder.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Sizes.cs" />
<Compile Include="Spatial\Bounds.cs" />
<Compile Include="Spatial\Matrix.cs" />
@ -67,6 +71,7 @@
<Compile Include="Transforms\ScaledObject.cs" />
<Compile Include="Transforms\TranslatedObject.cs" />
<Compile Include="Spatial\Vector3.cs" />
<Compile Include="Scripting\Variables.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Scripting
{
internal class DefaultFileWriter : IFileWriter
{
public void WriteAllLines(string path, string[] contents)
{
File.WriteAllLines(path, contents);
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Scripting
{
/// <summary>
/// A class that takes text and writes to file
/// </summary>
public interface IFileWriter
{
/// <summary>
/// Writes lines of text to a file at the path specified
/// </summary>
/// <param name="path"></param>
/// <param name="contents"></param>
void WriteAllLines(string path, string[] contents);
}
}

View File

@ -20,6 +20,10 @@ namespace OSCADSharp.Scripting
{
this.outerStatement = outerStatement;
this.children = children.ToList();
foreach (var child in children)
{
child.Parent = this;
}
}
public override string ToString()

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Scripting
{
/// <summary>
/// Extends the capabilities of StringBuilder with domain-specific behavior
/// </summary>
internal class StatementBuilder
{
private StringBuilder SB { get; set; } = new StringBuilder();
/// <summary>
/// Special append method for conditionally adding value-pairs
/// </summary>
/// <param name="name">The Name of the value-pair</param>
/// <param name="value">The value - if null this method does nothing</param>
/// <param name="prefixWithComma">(optional) Flag indicating whether a comma should be added before the value-pair</param>
public void AppendValuePairIfExists(string name, object value, bool prefixWithComma = false)
{
if (!String.IsNullOrEmpty(value?.ToString()))
{
if (prefixWithComma)
{
SB.Append(", ");
}
SB.Append(name);
SB.Append(" = ");
SB.Append(value);
}
}
/// <summary>
/// Pass-through for StringBuilder.Append
/// </summary>
/// <param name="text"></param>
public void Append(string text)
{
SB.Append(text);
}
/// <summary>
/// Pass-through for StringBuilder.AppendLine
/// </summary>
/// <param name="text"></param>
public void AppendLine(string text)
{
SB.AppendLine(text);
}
/// <summary>
/// Gets this builder's full string
/// </summary>
/// <returns></returns>
public override string ToString()
{
return SB.ToString();
}
}
}

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.Scripting
{
/// <summary>
/// A collection of names/values for variables
/// </summary>
public sealed class Variables
{
private Dictionary<string, object> variables = new Dictionary<string, object>();
/// <summary>
/// Assigns or gets a variable's value
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public object this[string name] // long is a 64-bit integer
{
get
{
if (variables.ContainsKey(name))
{
return variables[name];
}
else
{
return null;
}
}
set
{
variables[name] = value;
}
}
/// <summary>
/// This class is intended for use in other externally-exposed classes,
/// as such its constructor is not public.
/// </summary>
internal Variables()
{
}
/// <summary>
/// Gets the string representation for all variables
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
foreach (var kvp in this.variables)
{
sb.Append(kvp.Key);
sb.Append(" = ");
sb.Append(kvp.Value);
sb.Append(";");
sb.Append(Environment.NewLine);
}
return sb.ToString();
}
}
}

View File

@ -0,0 +1,32 @@
using OSCADSharp.Scripting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp
{
/// <summary>
/// Settings for OpenSCAD scripts
/// </summary>
public static class Settings
{
/// <summary>
/// Code-gen header
/// </summary>
public static readonly string OSCADSharpHeader = String.Format("/*Code Generated using OSCADSharp on {0}. {1}{2}For more information, please visit https://github.com/Exolun/OSCADSharp */{3}",
DateTime.Now.ToString(), Environment.NewLine, Environment.NewLine, Environment.NewLine);
/// <summary>
/// Global variables that can be assigned for output at the
/// top of OpenSCAD scripts
/// </summary>
public static Variables Globals = new Variables();
/// <summary>
/// Used to write scripts to file
/// </summary>
public static IFileWriter FileWriter = new DefaultFileWriter();
}
}

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OSCADSharp.Spatial;
using OSCADSharp.Scripting;
namespace OSCADSharp.Solids
{
@ -81,19 +82,19 @@ namespace OSCADSharp.Solids
/// Minimum angle (in degrees) of each cylinder fragment.
/// ($fa in OpenSCAD)
/// </summary>
public int MinimumAngle { get; set; } = 12;
public int? MinimumAngle { get; set; }
/// <summary>
/// Minimum circumferential length of each fragment.
/// ($fs in OpenSCAD)
/// </summary>
public int MinimumCircumferentialLength { get; set; } = 2;
public int? MinimumCircumferentialLength { get; set; }
/// <summary>
/// Number of fragments in 360 degrees. Values of 3 or more override MinimumAngle and MinimumCircumferentialLength
/// ($fn in OpenSCAD)
/// </summary>
public int Resolution { get; set; } = 0;
public int? Resolution { get; set; }
#endregion
#region Constructors
@ -125,9 +126,18 @@ namespace OSCADSharp.Solids
/// <returns>Script for this object</returns>
public override string ToString()
{
return String.Format("cylinder($fn = {0}, $fa = {1}, $fs = {2}, h = {3}, r1 = {4}, r2 = {5}, center = {6}); {7}",
Resolution.ToString(), MinimumAngle.ToString(), MinimumCircumferentialLength.ToString(),
Height.ToString(), Radius1.ToString(), Radius2.ToString(), Center.ToString().ToLower(), Environment.NewLine);
var sb = new StatementBuilder();
sb.Append("cylinder(");
sb.AppendValuePairIfExists("center", this.Center.ToString().ToLower());
sb.AppendValuePairIfExists("r1", this.Radius1, true);
sb.AppendValuePairIfExists("r2", this.Radius2, true);
sb.AppendValuePairIfExists("h", this.Height, true);
sb.AppendValuePairIfExists("$fn", this.Resolution, true);
sb.AppendValuePairIfExists("$fa", this.MinimumAngle, true);
sb.AppendValuePairIfExists("$fs", this.MinimumCircumferentialLength, true);
sb.Append(");");
sb.Append(Environment.NewLine);
return sb.ToString();
}
/// <summary>

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OSCADSharp.Spatial;
using OSCADSharp.Scripting;
namespace OSCADSharp.Solids
{
@ -30,19 +31,19 @@ namespace OSCADSharp.Solids
/// Minimum angle (in degrees) of each cylinder fragment.
/// ($fa in OpenSCAD)
/// </summary>
public int MinimumAngle { get; set; } = 12;
public int? MinimumAngle { get; set; }
/// <summary>
/// Fragment size in mm
/// ($fs in OpenSCAD)
/// </summary>
public int MinimumFragmentSize { get; set; } = 2;
public int? MinimumFragmentSize { get; set; }
/// <summary>
/// Number of fragments in 360 degrees. Values of 3 or more override MinimumAngle and MinimumCircumferentialLength
/// ($fn in OpenSCAD)
/// </summary>
public int Resolution { get; set; } = 0;
public int? Resolution { get; set; }
#endregion
#region Constructors
@ -70,9 +71,16 @@ namespace OSCADSharp.Solids
/// <returns>Script for this object</returns>
public override string ToString()
{
return String.Format("sphere($fn = {0}, $fa = {1}, $fs = {2}, r = {3});{4}",
this.Resolution.ToString(), this.MinimumAngle.ToString(),
this.MinimumFragmentSize.ToString(), this.Radius.ToString(), Environment.NewLine);
StatementBuilder sb = new StatementBuilder();
sb.Append("sphere(");
sb.AppendValuePairIfExists("r", this.Radius);
sb.AppendValuePairIfExists("$fn", this.Resolution, true);
sb.AppendValuePairIfExists("$fa", this.MinimumAngle, true);
sb.AppendValuePairIfExists("$fs", this.MinimumFragmentSize, true);
sb.Append(");");
sb.Append(Environment.NewLine);
return sb.ToString();
}
/// <summary>

View File

@ -107,22 +107,22 @@ namespace OSCADSharp.Solids
/// <returns>Script for this object</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
StatementBuilder sb = new StatementBuilder();
sb.Append("text(");
sb.Append("\"");
sb.Append(this.Text);
sb.Append("\"");
appendIfValueNotNullOrEmpty("size", this.Size?.ToString(), sb);
sb.AppendValuePairIfExists("size", this.Size?.ToString(), true);
// Text is always centered in OSCADSharp to ensure correctness of
// position interpolation
appendIfValueNotNullOrEmpty("halign", "\"center\"", sb);
appendIfValueNotNullOrEmpty("valign", "\"center\"", sb);
sb.AppendValuePairIfExists("halign", "\"center\"", true);
sb.AppendValuePairIfExists("valign", "\"center\"", true);
appendIfValueNotNullOrEmpty("font", this.Font, sb);
appendIfValueNotNullOrEmpty("spacing", this.Spacing?.ToString(), sb);
appendIfValueNotNullOrEmpty("direction", this.TextDirection?.ToString(), sb);
appendIfValueNotNullOrEmpty("language", this.Language?.ToString(), sb);
sb.AppendValuePairIfExists("font", this.Font, true);
sb.AppendValuePairIfExists("spacing", this.Spacing?.ToString(), true);
sb.AppendValuePairIfExists("direction", this.TextDirection?.ToString(), true);
sb.AppendValuePairIfExists("language", this.Language?.ToString(), true);
sb.Append(");");
sb.Append(Environment.NewLine);
@ -145,6 +145,7 @@ namespace OSCADSharp.Solids
/// <summary>
/// Returns the approximate boundaries of this OpenSCAD object
/// </summary>
/// <returns></returns>
public override Bounds Bounds()
{

View File

@ -33,6 +33,7 @@ namespace OSCADSharp.Transforms
this.Opacity = opacity;
this.children.Add(obj);
obj.Parent = this;
}
public override string ToString()

View File

@ -35,6 +35,7 @@ namespace OSCADSharp.Transforms
this.Height = height;
this.children.Add(obj);
obj.Parent = this;
}
public override OSCADObject Clone()

View File

@ -32,6 +32,7 @@ namespace OSCADSharp.Transforms
this.Normal = normal;
this.children.Add(obj);
obj.Parent = this;
}
public override string ToString()

View File

@ -30,6 +30,7 @@ namespace OSCADSharp.Transforms
this.Size = size;
this.children.Add(obj);
obj.Parent = this;
}
public override string ToString()

View File

@ -30,6 +30,7 @@ namespace OSCADSharp.Transforms
this.Angle = angle;
this.children.Add(obj);
obj.Parent = this;
}
public override string ToString()

View File

@ -30,6 +30,7 @@ namespace OSCADSharp.Transforms
this.ScaleFactor = scale;
this.children.Add(obj);
obj.Parent = this;
}
public override string ToString()

View File

@ -27,6 +27,7 @@ namespace OSCADSharp.Transforms
this.Vector = vector;
this.children.Add(obj);
obj.Parent = this;
}
public override string ToString()