From 4532e7b20d41a4e6c17ef9a0dfaa1f172c21b90f Mon Sep 17 00:00:00 2001 From: Michael L Smith Date: Tue, 23 Feb 2016 19:14:50 -0800 Subject: [PATCH] Added Variables class, Settings class, settings/header output in OSCADObject.ToFile --- OSCADSharp/OSCADSharp.ConsoleTests/Program.cs | 1 + OSCADSharp/OSCADSharp/OSCADObject.cs | 7 +- OSCADSharp/OSCADSharp/OSCADSharp.csproj | 2 + OSCADSharp/OSCADSharp/Scripting/Variables.cs | 67 +++++++++++++++++++ OSCADSharp/OSCADSharp/Settings.cs | 27 ++++++++ 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 OSCADSharp/OSCADSharp/Scripting/Variables.cs create mode 100644 OSCADSharp/OSCADSharp/Settings.cs diff --git a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs index b6a77fa..c1b74b7 100644 --- a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs +++ b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs @@ -14,6 +14,7 @@ namespace OSCADSharp.ConsoleTests static void Main(string[] args) { + Settings.Globals["$fn"] = 100; var obj = new Sphere(30); var pos = obj.Position(); diff --git a/OSCADSharp/OSCADSharp/OSCADObject.cs b/OSCADSharp/OSCADSharp/OSCADObject.cs index 5201a46..67d2ab6 100644 --- a/OSCADSharp/OSCADSharp/OSCADObject.cs +++ b/OSCADSharp/OSCADSharp/OSCADObject.cs @@ -329,7 +329,12 @@ namespace OSCADSharp path += ".scad"; } - File.WriteAllLines(path, new string[] { this.ToString() }); + File.WriteAllLines(path, new string[] + { + Settings.OSCADSharpHeader, + Settings.Globals.ToString(), + this.ToString() + }); } #endregion diff --git a/OSCADSharp/OSCADSharp/OSCADSharp.csproj b/OSCADSharp/OSCADSharp/OSCADSharp.csproj index 6a7f166..ae770d1 100644 --- a/OSCADSharp/OSCADSharp/OSCADSharp.csproj +++ b/OSCADSharp/OSCADSharp/OSCADSharp.csproj @@ -44,6 +44,7 @@ + @@ -68,6 +69,7 @@ + diff --git a/OSCADSharp/OSCADSharp/Scripting/Variables.cs b/OSCADSharp/OSCADSharp/Scripting/Variables.cs new file mode 100644 index 0000000..1a47561 --- /dev/null +++ b/OSCADSharp/OSCADSharp/Scripting/Variables.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OSCADSharp.Scripting +{ + /// + /// A collection of names/values for variables + /// + public sealed class Variables + { + private Dictionary variables = new Dictionary(); + + /// + /// Assigns or gets a variable's value + /// + /// + /// + 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; + } + } + + /// + /// This class is intended for use in other externally-exposed classes, + /// as such its constructor is not public. + /// + internal Variables() + { + } + + /// + /// Gets the string representation for all variables + /// + /// + 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(); + } + } +} diff --git a/OSCADSharp/OSCADSharp/Settings.cs b/OSCADSharp/OSCADSharp/Settings.cs new file mode 100644 index 0000000..3886f99 --- /dev/null +++ b/OSCADSharp/OSCADSharp/Settings.cs @@ -0,0 +1,27 @@ +using OSCADSharp.Scripting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OSCADSharp +{ + /// + /// Settings for OpenSCAD scripts + /// + public static class Settings + { + /// + /// Code-gen header + /// + 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); + + /// + /// Global variables that can be assigned for output at the + /// top of OpenSCAD scripts + /// + public static Variables Globals = new Variables(); + } +}