Added Variables class, Settings class, settings/header output in OSCADObject.ToFile

This commit is contained in:
Michael L Smith 2016-02-23 19:14:50 -08:00
parent 8506459939
commit 4532e7b20d
5 changed files with 103 additions and 1 deletions

View File

@ -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();

View File

@ -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

View File

@ -44,6 +44,7 @@
<ItemGroup>
<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" />
@ -68,6 +69,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,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,27 @@
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();
}
}