From 3a7875ee510c72cf90144d5a53ff4c47d2356029 Mon Sep 17 00:00:00 2001 From: Michael L Smith Date: Mon, 22 Feb 2016 20:36:15 -0800 Subject: [PATCH] + Reworked Cylinder so that if $f* values are omitted, they don't appear in output. --- .../Solids/CylinderTests.cs | 29 +++++++++++++++++++ .../OSCADSharp/Scripting/StatementBuilder.cs | 2 +- OSCADSharp/OSCADSharp/Solids/Cylinder.cs | 22 ++++++++++---- OSCADSharp/OSCADSharp/Solids/Text3D.cs | 1 + 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/OSCADSharp/OSCADSharp.UnitTests/Solids/CylinderTests.cs b/OSCADSharp/OSCADSharp.UnitTests/Solids/CylinderTests.cs index b1acbce..5e441f5 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/Solids/CylinderTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/Solids/CylinderTests.cs @@ -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")); + } } } diff --git a/OSCADSharp/OSCADSharp/Scripting/StatementBuilder.cs b/OSCADSharp/OSCADSharp/Scripting/StatementBuilder.cs index 3498a90..30171ad 100644 --- a/OSCADSharp/OSCADSharp/Scripting/StatementBuilder.cs +++ b/OSCADSharp/OSCADSharp/Scripting/StatementBuilder.cs @@ -29,7 +29,7 @@ namespace OSCADSharp.Scripting } SB.Append(name); - SB.Append("="); + SB.Append(" = "); SB.Append(value); } } diff --git a/OSCADSharp/OSCADSharp/Solids/Cylinder.cs b/OSCADSharp/OSCADSharp/Solids/Cylinder.cs index 5fe08b8..a70fc47 100644 --- a/OSCADSharp/OSCADSharp/Solids/Cylinder.cs +++ b/OSCADSharp/OSCADSharp/Solids/Cylinder.cs @@ -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) /// - public int MinimumAngle { get; set; } = 12; + public int? MinimumAngle { get; set; } /// /// Minimum circumferential length of each fragment. /// ($fs in OpenSCAD) /// - public int MinimumCircumferentialLength { get; set; } = 2; + public int? MinimumCircumferentialLength { get; set; } /// /// Number of fragments in 360 degrees. Values of 3 or more override MinimumAngle and MinimumCircumferentialLength /// ($fn in OpenSCAD) /// - public int Resolution { get; set; } = 0; + public int? Resolution { get; set; } #endregion #region Constructors @@ -125,9 +126,18 @@ namespace OSCADSharp.Solids /// Script for this object 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(); } /// diff --git a/OSCADSharp/OSCADSharp/Solids/Text3D.cs b/OSCADSharp/OSCADSharp/Solids/Text3D.cs index a41a9dc..40045cd 100644 --- a/OSCADSharp/OSCADSharp/Solids/Text3D.cs +++ b/OSCADSharp/OSCADSharp/Solids/Text3D.cs @@ -145,6 +145,7 @@ namespace OSCADSharp.Solids /// /// Returns the approximate boundaries of this OpenSCAD object + /// /// public override Bounds Bounds() {