From 097b53649aa87c4547cf9bade9efc7549fbdefeb Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Mon, 20 Nov 2023 18:00:21 -0800 Subject: [PATCH] 3mf loading improvements (soon) Text wrapping --- .../Settings/ValidationError.cs | 3 +- .../ApplicationView/ContentStoreConverter.cs | 75 ++++------------ .../DesignTools/Primitives/TextObject3D.cs | 27 +++--- .../Library/Export/TheeMFExport.cs | 83 ++++++++++++++++++ StaticData/Icons/filetypes/3mf.png | Bin 0 -> 556 bytes StaticData/Translations/Master.txt | 3 + Submodules/agg-sharp | 2 +- 7 files changed, 120 insertions(+), 73 deletions(-) create mode 100644 MatterControlLib/Library/Export/TheeMFExport.cs create mode 100644 StaticData/Icons/filetypes/3mf.png diff --git a/MatterControl.Printing/Settings/ValidationError.cs b/MatterControl.Printing/Settings/ValidationError.cs index d5c303be4..1114fa766 100644 --- a/MatterControl.Printing/Settings/ValidationError.cs +++ b/MatterControl.Printing/Settings/ValidationError.cs @@ -65,7 +65,8 @@ namespace MatterHackers.MatterControl public static readonly string ExceptionDuringSliceSettingsValidation = nameof(ExceptionDuringSliceSettingsValidation); public static readonly string IncompatibleBedSurfaceAndMaterial = nameof(IncompatibleBedSurfaceAndMaterial); public static readonly string ItemCannotBeExported = nameof(ItemCannotBeExported); - public static readonly string ItemToAMFExportInvalid = nameof(ItemToAMFExportInvalid); + public static readonly string ItemToAMFExportInvalid = nameof(ItemToAMFExportInvalid); + public static readonly string ItemTo3MFExportInvalid = nameof(ItemTo3MFExportInvalid); public static readonly string ItemToSTLExportInvalid = nameof(ItemToSTLExportInvalid); public static readonly string MaterialNotSelected = nameof(MaterialNotSelected); public static readonly string BedTemperatureError = nameof(BedTemperatureError); diff --git a/MatterControlLib/ApplicationView/ContentStoreConverter.cs b/MatterControlLib/ApplicationView/ContentStoreConverter.cs index f554a8336..44f7f98a1 100644 --- a/MatterControlLib/ApplicationView/ContentStoreConverter.cs +++ b/MatterControlLib/ApplicationView/ContentStoreConverter.cs @@ -27,56 +27,12 @@ of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Diagnostics; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading; -using System.Threading.Tasks; -using global::MatterControl.Printing; -using Markdig.Agg; -using Markdig.Renderers.Agg; -using MatterHackers.Agg; -using MatterHackers.Agg.Font; -using MatterHackers.Agg.Image; -using MatterHackers.Agg.Platform; -using MatterHackers.Agg.UI; -using MatterHackers.Agg.VertexSource; -using MatterHackers.DataConverters3D; -using MatterHackers.DataConverters3D.UndoCommands; -using MatterHackers.Localizations; -using MatterHackers.MatterControl.CustomWidgets; -using MatterHackers.MatterControl.DataStorage; -using MatterHackers.MatterControl.DesignTools; -using MatterHackers.MatterControl.DesignTools.Operations; -using MatterHackers.MatterControl.Extensibility; using MatterHackers.MatterControl.Library; -using MatterHackers.MatterControl.PartPreviewWindow; -using MatterHackers.MatterControl.PartPreviewWindow.View3D; -using MatterHackers.MatterControl.Plugins; -using MatterHackers.MatterControl.PrinterCommunication; -using MatterHackers.MatterControl.PrinterControls.PrinterConnections; -using MatterHackers.MatterControl.PrintQueue; -using MatterHackers.MatterControl.SettingsManagement; -using MatterHackers.MatterControl.SlicerConfiguration; -using MatterHackers.MatterControl.Tour; -using MatterHackers.PolygonMesh; -using MatterHackers.PolygonMesh.Processors; -using MatterHackers.VectorMath; -using MatterHackers.VectorMath.TrackBall; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; +using System; +using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("MatterControl.Tests")] [assembly: InternalsVisibleTo("MatterControl.AutomationTests")] @@ -84,20 +40,19 @@ using Newtonsoft.Json.Linq; namespace MatterHackers.MatterControl { + public class ContentStoreConverter : JsonConverter + { + public override bool CanWrite => false; - public class ContentStoreConverter : JsonConverter - { - public override bool CanWrite => false; + public override IContentStore ReadJson(JsonReader reader, Type objectType, IContentStore existingValue, bool hasExistingValue, JsonSerializer serializer) + { + var token = JToken.Load(reader); - public override IContentStore ReadJson(JsonReader reader, Type objectType, IContentStore existingValue, bool hasExistingValue, JsonSerializer serializer) - { - var token = JToken.Load(reader); + return null; + } - return null; - } - - public override void WriteJson(JsonWriter writer, IContentStore value, JsonSerializer serializer) - { - } - } -} + public override void WriteJson(JsonWriter writer, IContentStore value, JsonSerializer serializer) + { + } + } +} \ No newline at end of file diff --git a/MatterControlLib/DesignTools/Primitives/TextObject3D.cs b/MatterControlLib/DesignTools/Primitives/TextObject3D.cs index 70e10b12d..7ae8f6259 100644 --- a/MatterControlLib/DesignTools/Primitives/TextObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/TextObject3D.cs @@ -191,12 +191,12 @@ namespace MatterHackers.MatterControl.DesignTools { bool valuesChanged = false; var height = Height.ClampIfNotCalculated(this, .01, 1000000, ref valuesChanged); - var wrappingWidth = WrappingWidth.Value(this); + var wrappingWidth_mm = WrappingWidth.Value(this); var wrappingIndent = WrappingIndent.ClampIfNotCalculated(this, 0, 100, ref valuesChanged); - if (wrappingWidth < 10) + if (wrappingWidth_mm < 10) { - wrappingWidth = 0; + wrappingWidth_mm = 0; } var textToWrite = MultiLine @@ -205,10 +205,16 @@ namespace MatterHackers.MatterControl.DesignTools var pointSize = PointSize.Value(this); - if (WrapLines && wrappingWidth > 0) + var mmPerInch = 25.4; + var mmPerPoint = mmPerInch / StyledTypeFace.PointsPerInch; + var pixelsPerPoint = StyledTypeFace.PixelsPerInch / StyledTypeFace.PointsPerInch; + + if (WrapLines && wrappingWidth_mm > 0) { var wrapper = new EnglishTextWrapping(pointSize); - textToWrite = wrapper.InsertCRs(textToWrite, wrappingWidth, MultiLine ? wrappingIndent : 0); + var wrappingPoints = wrappingWidth_mm / mmPerPoint; + var wrappingPixels = wrappingPoints * pixelsPerPoint; + textToWrite = wrapper.InsertCRs(textToWrite, wrappingPixels, MultiLine ? wrappingIndent : 0); } if (string.IsNullOrWhiteSpace(textToWrite)) @@ -224,7 +230,6 @@ namespace MatterHackers.MatterControl.DesignTools list.Clear(); var offset = Vector2.Zero; - double pointsToMm = 0.352778; var lineNumber = 1; var leterNumber = 1; var lineObject = new Object3D() @@ -240,14 +245,14 @@ namespace MatterHackers.MatterControl.DesignTools { ResolutionScale = 10 }; - var scaledLetterPrinter = new VertexSourceApplyTransform(letterPrinter, Affine.NewScaling(pointsToMm)); + var scaledLetterPrinter = new VertexSourceApplyTransform(letterPrinter, Affine.NewScaling(mmPerPoint)); if (letter == '\n') { leterNumber = 0; lineNumber++; offset.X = 0; - offset.Y -= style.EmSizeInPoints * pointsToMm * 1.4; + offset.Y -= style.EmSizeInPoints * mmPerPoint * 1.4; lineObject = new Object3D() { Matrix = Matrix4X4.CreateTranslation(0, offset.Y, 0), @@ -261,11 +266,11 @@ namespace MatterHackers.MatterControl.DesignTools switch (letter) { case ' ': - offset.X += letterPrinter.GetSize(" ").X * pointsToMm; + offset.X += letterPrinter.GetSize(" ").X * mmPerPoint; break; case '\t': - offset.X += letterPrinter.GetSize(" ").X * pointsToMm; + offset.X += letterPrinter.GetSize(" ").X * mmPerPoint; break; default: @@ -281,7 +286,7 @@ namespace MatterHackers.MatterControl.DesignTools new VertexSourceApplyTransform( new VertexStorage(scaledLetterPrinter), Affine.NewTranslation(offset.X, offset.Y))); } - offset.X += letterPrinter.GetSize(letter.ToString()).X * pointsToMm; + offset.X += letterPrinter.GetSize(letter.ToString()).X * mmPerPoint; break; } diff --git a/MatterControlLib/Library/Export/TheeMFExport.cs b/MatterControlLib/Library/Export/TheeMFExport.cs new file mode 100644 index 000000000..6bf42175e --- /dev/null +++ b/MatterControlLib/Library/Export/TheeMFExport.cs @@ -0,0 +1,83 @@ +/* +Copyright (c) 2023, Lars Brubaker, John Lewin +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation are those +of the authors and should not be interpreted as representing official policies, +either expressed or implied, of the FreeBSD Project. +*/ + +using MatterHackers.Agg.Image; +using MatterHackers.Agg.Platform; +using MatterHackers.Localizations; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace MatterHackers.MatterControl.Library.Export +{ +#if DEBUG + // This is not working yet. So it is only enabled in debug mode + public class TheeMFExport : IExportPlugin + { + public string ButtonText => "3MF File".Localize(); + public string DisabledReason => ""; + public bool Enabled => true; + public string ExtensionFilter => "Save as 3MF|*.3mf"; + public string FileExtension => ".3mf"; + public ImageBuffer Icon { get; } = StaticData.Instance.LoadIcon(Path.Combine("filetypes", "3mf.png")); + public int Priority => 3; + + public bool ExportPossible(ILibraryAsset libraryItem) => true; + + public async Task> Generate(IEnumerable libraryItems, string outputPath, Action progress, CancellationToken cancellationToken) + { + var firstItem = libraryItems.OfType().FirstOrDefault(); + if (firstItem is ILibraryAsset libraryItem) + { + bool exportSuccessful = await MeshExport.ExportMesh(libraryItem, outputPath, true, progress); + if (exportSuccessful) + { + return null; + } + } + + return new List() + { + new ValidationError(ValidationErrors.ItemTo3MFExportInvalid) + { + Error = "Item cannot be exported as 3MF".Localize(), + Details = firstItem?.ToString() ?? "" + } + }; + } + + public void Initialize(PrinterConfig printer) + { + } + } +#endif +} \ No newline at end of file diff --git a/StaticData/Icons/filetypes/3mf.png b/StaticData/Icons/filetypes/3mf.png new file mode 100644 index 0000000000000000000000000000000000000000..b9569e11efafeb544666d6f9e74715213fc96148 GIT binary patch literal 556 zcmeAS@N?(olHy`uVBq!ia0y~yV31^BU{K^>V_;y=Il!=(fq{Xg*vT`5gM)*kh9jke zfq{Xuz$3Dlfq`Qx2s2J&`EUiKtHd>;Bsf2(yEr+qAXP8FD1G)j z8z}|`#+9Bfjv*eMZ?F1#OFK#&`1oJ%*-)6*o}E^WDF z_n~vU>tTa++uPdJ#pX=f#LnOl9ed^3)k7DTNk59bvi4R?pzWOxqKq3_A~Zz!uGhLU zNKK2-S-JE5J|zaH8K+fEzw<6QtN%nWN>i+;ZAs=*YkjU;+urWb^L#Pyt;@7nf4gV5 z=h|c|I54Sz&>ZJk(XpSO*jH-C{1<+_?9k23c>x+uT63SSS`@fF%m404BQGQ8#dC|Lx0DpSHtwQp&DJ@|^Cs=0E*Z zUpG-whBmM2C1$Ik61=jIy!ten2}b;X@U(4Cu+92En5*cuefHz;p;P1 z^Cw2{?J5g%@%;8V{