diff --git a/Elements/src/extension.dib b/Elements/src/extension.dib index a9794fbc3..9a0e53d41 100644 --- a/Elements/src/extension.dib +++ b/Elements/src/extension.dib @@ -18,6 +18,7 @@ using Microsoft.DotNet.Interactive; using Microsoft.DotNet.Interactive.Formatting; using Elements; using Elements.Geometry; +using Elements.Geometry.Solids; using Elements.Serialization.glTF; using Newtonsoft.Json; using System; @@ -134,6 +135,7 @@ if (KernelInvocationContext.Current is { } currentContext) double DEFAULT_MODEL_WIDTH = 600; double DEFAULT_MODEL_HEIGHT = 400; Material DEFAULT_CURVE_MATERIAL = BuiltInMaterials.XAxis; +Material DEFAULT_SOLID_MATERIAL = BuiltInMaterials.Default; string GetModelViewerSrc(Model model, double? width=null, double? height=null) { var gltf = model.ToGlTF(); @@ -193,6 +195,45 @@ Formatter.Register((model, writer) => { writer.Write(src); }, "text/html"); +Formatter.Register((solidOp, writer) => { + var model = new Model(); + model.AddElement(new GeometricElement { Representation = solidOp, Material= DEFAULT_SOLID_MATERIAL}); + var src = GetModelViewerSrc(model); + writer.Write(src); + +}, "text/html"); +Formatter.Register((representation, writer) => { + var model = new Model(); + model.AddElement(new GeometricElement { Representation = representation, Material= DEFAULT_SOLID_MATERIAL}); + var src = GetModelViewerSrc(model); + writer.Write(src); + +}, "text/html"); +Formatter.Register>((solidOps, writer) => { + var model = new Model(); + foreach(var solidOp in solidOps) { + model.AddElement(new GeometricElement { Representation = solidOp, Material= DEFAULT_SOLID_MATERIAL}); + } + var src = GetModelViewerSrc(model); + writer.Write(src); + +}, "text/html"); +Formatter.Register((xform, writer) => { + var model = new Model(); + model.AddElements(xform.ToModelCurves()); + var src = GetModelViewerSrc(model); + writer.Write(src); +}, "text/html"); + +Formatter.Register>((xforms, writer) => { + var model = new Model(); + foreach(var xform in xforms) { + model.AddElements(xform.ToModelCurves()); + } + var src = GetModelViewerSrc(model); + writer.Write(src); +}, "text/html"); + void DisplayModel(Model model, double? width=null, double? height=null) { var src = GetModelViewerSrc(model, width ?? DEFAULT_MODEL_WIDTH, height ?? DEFAULT_MODEL_HEIGHT); KernelInvocationContext.Current.DisplayAs(src, "text/html"); @@ -206,3 +247,7 @@ void SetDefaultDisplaySize(double width, double height) { void SetDefaultCurveMaterial(Material mat) { DEFAULT_CURVE_MATERIAL = mat; } + +void SetDefaultSolidMaterial(Material mat) { + DEFAULT_SOLID_MATERIAL = mat; +} diff --git a/scratch.dib b/scratch.dib index dc59a09fa..278cbf2a6 100644 --- a/scratch.dib +++ b/scratch.dib @@ -4,7 +4,7 @@ #!csharp -#r "/Users/iankeough/dev/Hypar/Elements/Elements/src/bin/Debug/netstandard2.0/Hypar.Elements.dll" +#r "./Elements/src/bin/Debug/netstandard2.0/Hypar.Elements.dll" #r "nuget:glTF2Loader, 1.1.3-alpha" #r "nuget:Unofficial.LibTessDotNet, 2.0.0" #r "nuget:SixLabors.Fonts, 1.0.0-beta19" @@ -13,200 +13,7 @@ #!csharp -using System.CommandLine; -using System.CommandLine.Invocation; -using Microsoft.DotNet.Interactive; -using Microsoft.DotNet.Interactive.Formatting; -using Elements; -using Elements.Geometry; -using Elements.Serialization.glTF; -using Newtonsoft.Json; -using System; -using System.IO; - -var viewerSrc = @" -
- - -"; - -if (KernelInvocationContext.Current is { } currentContext) -{ - currentContext.DisplayAs("Add `return model;` at the end of a cell or call `DisplayModel(model, width, height);` to display an Elements model. You can also return individual elements, curves, profiles, or lists of elements, curves, or profiles to automatically populate a model.", "text/markdown"); -} - -double DEFAULT_MODEL_WIDTH = 600; -double DEFAULT_MODEL_HEIGHT = 400; -Material DEFAULT_CURVE_MATERIAL = BuiltInMaterials.XAxis; - -string GetModelViewerSrc(Model model, double? width=null, double? height=null) { - var gltf = model.ToGlTF(); - var gltfString = Convert.ToBase64String(gltf, 0, gltf.Length); - return viewerSrc - .Replace("MODEL_BYTES_HERE", gltfString) - .Replace("WIDTH_VAR", (width ?? DEFAULT_MODEL_WIDTH).ToString()) - .Replace("HEIGHT_VAR", (height ?? DEFAULT_MODEL_HEIGHT).ToString()) - .Replace("DIV_ID", Guid.NewGuid().ToString()); -} - -Formatter.Register((crv, writer) => { - var model = new Model(); - model.AddElement(new ModelCurve(crv, DEFAULT_CURVE_MATERIAL)); - var src = GetModelViewerSrc(model); - writer.Write(src); - -}, "text/html"); - -Formatter.Register((p, writer) => { - var model = new Model(); - model.AddElements(p.ToModelCurves(null, DEFAULT_CURVE_MATERIAL)); - var src = GetModelViewerSrc(model); - writer.Write(src); -}, "text/html"); - -Formatter.Register((e, writer) => { - var model = new Model(); - model.AddElement(e); - var src = GetModelViewerSrc(model); - writer.Write(src); -}, "text/html"); - -Formatter.Register>((elements, writer) => { - var model = new Model(); - model.AddElements(elements); - var src = GetModelViewerSrc(model); - writer.Write(src); -}, "text/html"); - -Formatter.Register>((crvs, writer) => { - var model = new Model(); - model.AddElements(crvs.Select(crv => new ModelCurve(crv, DEFAULT_CURVE_MATERIAL))); - var src = GetModelViewerSrc(model); - writer.Write(src); -}, "text/html"); - -Formatter.Register>((profiles, writer) => { - var model = new Model(); - model.AddElements(profiles.SelectMany(p => p.ToModelCurves(null, DEFAULT_CURVE_MATERIAL))); - var src = GetModelViewerSrc(model); - writer.Write(src); -}, "text/html"); - -Formatter.Register((model, writer) => { - var src = GetModelViewerSrc(model); - writer.Write(src); -}, "text/html"); - -void DisplayModel(Model model, double? width=null, double? height=null) { - var src = GetModelViewerSrc(model, width ?? DEFAULT_MODEL_WIDTH, height ?? DEFAULT_MODEL_HEIGHT); - KernelInvocationContext.Current.DisplayAs(src, "text/html"); -} - -void SetDefaultDisplaySize(double width, double height) { - DEFAULT_MODEL_WIDTH = width; - DEFAULT_MODEL_HEIGHT = height; -} - -void SetDefaultCurveMaterial(Material mat) { - DEFAULT_CURVE_MATERIAL = mat; -} +#!import "./Elements/src/extension.dib" #!csharp