diff --git a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs index cd114d8..682f3c5 100644 --- a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs +++ b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs @@ -56,7 +56,9 @@ namespace OSCADSharp.ConsoleTests static void Main(string[] args) { - makePeg(); + var model = new ImportedModel("seahawkImport.stl", new Bounds(new Vector3(1, 1, 1), new Vector3(-1, -1, -1))); + model.ToFile("modelTest").Open(); + //makePeg(); //var diam = new Variable("mainColumn", Inches.Half); //var height = new Variable("overallHeight", Inches.Quarter); diff --git a/OSCADSharp/OSCADSharp/OSCADSharp.csproj b/OSCADSharp/OSCADSharp/OSCADSharp.csproj index e79aeb4..16d3bd5 100644 --- a/OSCADSharp/OSCADSharp/OSCADSharp.csproj +++ b/OSCADSharp/OSCADSharp/OSCADSharp.csproj @@ -47,6 +47,7 @@ + diff --git a/OSCADSharp/OSCADSharp/Solids/ImportedModel.cs b/OSCADSharp/OSCADSharp/Solids/ImportedModel.cs new file mode 100644 index 0000000..365cbae --- /dev/null +++ b/OSCADSharp/OSCADSharp/Solids/ImportedModel.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using OSCADSharp.DataBinding; +using OSCADSharp.Spatial; +using System.IO; + +namespace OSCADSharp.Solids +{ + /// + /// An imported 3D model, supported types are limited. + /// + public class ImportedModel : OSCADObject + { + #region Fields + private string filePath; + private Bounds objectBounds; + #endregion + + #region Constructors/Initialization + /// + /// Imports a model located at the specified file path. + /// + /// If this is an ASCII-based STL model, the bounds and position will automatically be initialized. + /// + /// + public ImportedModel(string filePath) + { + this.filePath = filePath; + this.correctPath(); + //TODO: Attempt to compute bounds + } + + /// + /// Imports a model located at the specified file path. + /// + /// + /// + public ImportedModel(string filePath, Bounds bounds) + { + this.filePath = filePath; + this.correctPath(); + + this.objectBounds = bounds; + } + + private void correctPath() + { + string currentDir = Directory.GetCurrentDirectory(); + + if (!this.filePath.Contains(currentDir)) + { + this.filePath = currentDir + "\\" + this.filePath; + } + } + #endregion + + #region OSCADObject Overrides + /// + /// Binds the specified property to a variable + /// + /// + /// + public override void Bind(string property, Variable variable) + { + //No bindable properties on an imported model + } + + /// + /// Returns the approximate boundaries of this OpenSCAD object + /// + /// + public override Bounds Bounds() + { + if(this.objectBounds == null) + { + throw new InvalidOperationException("Could not compute bounds for an imported model where bounds were neither specified nor could be derived."); + } + + return this.objectBounds; + } + + /// + /// Gets a copy of this object that is a new instance + /// + /// + public override OSCADObject Clone() + { + return new ImportedModel(this.filePath, this.objectBounds); + } + + /// + /// Gets the position of this object's center (origin) in + /// world space + /// + /// + public override Vector3 Position() + { + if(this.objectBounds == null) + { + throw new InvalidOperationException("Cannot compute position on an imported model with no bounds"); + } + + return Vector3.Average(this.objectBounds.TopRight, this.objectBounds.BottomLeft); + } + + /// + /// Converts this object to an OpenSCAD script + /// + /// Script for this object + public override string ToString() + { + return String.Format("import(\"{0}\");", this.filePath.Replace("\\", @"\\")); + } + #endregion + } +}