public static bool SaveUncompressed(IObject3D item, string fileName, MeshOutputSettings outputInfo = null) { using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { return(Save(item, file, outputInfo)); } }
static public void SaveToLibraryFolder(PrintItemWrapper printItemWrapper, List <MeshGroup> meshGroups, bool AbsolutePositioned) { string[] metaData = { "Created By", "MatterControl" }; if (AbsolutePositioned) { metaData = new string[] { "Created By", "MatterControl", "BedPosition", "Absolute" }; } if (printItemWrapper.FileLocation.Contains(ApplicationDataStorage.Instance.ApplicationLibraryDataPath)) { MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, metaData); MeshFileIo.Save(meshGroups, printItemWrapper.FileLocation, outputInfo); } else // save a copy to the library and update this to point at it { string fileName = Path.ChangeExtension(Path.GetRandomFileName(), ".amf"); printItemWrapper.FileLocation = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, fileName); MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, metaData); MeshFileIo.Save(meshGroups, printItemWrapper.FileLocation, outputInfo); printItemWrapper.PrintItem.Commit(); // let the queue know that the item has changed so it load the correct part QueueData.Instance.SaveDefaultQueue(); } printItemWrapper.OnFileHasChanged(); }
/// <summary> /// Writes the mesh to disk in a zip container /// </summary> /// <param name="item">The object to save</param> /// <param name="fileName">The location to save to</param> /// <param name="outputInfo">Extra meta data to store in the file</param> /// <returns>The results of the save operation</returns> public static bool Save(IObject3D item, string fileName, MeshOutputSettings outputInfo = null) { try { var forceAscii = false; if (forceAscii || outputInfo?.OutputTypeSetting == MeshOutputSettings.OutputType.Ascii) { SaveUncompressed(item, fileName, outputInfo); return(true); } else { using (Stream stream = File.OpenWrite(fileName)) { using (var archive = new ZipArchive(stream, ZipArchiveMode.Create)) { ZipArchiveEntry zipEntry = archive.CreateEntry(Path.GetFileName(fileName)); using (var entryStream = zipEntry.Open()) { Save(item, entryStream, outputInfo); } } } return(true); } } catch (Exception e) { Debug.Print(e.Message); BreakInDebugger(); return(false); } }
private static string SaveAndGetFilenameForMaterial(MeshGroup extruderMeshGroup, List <int> materialIndexsToSaveInThisSTL) { string fileName = Path.ChangeExtension(Path.GetRandomFileName(), ".stl"); string applicationUserDataPath = ApplicationDataStorage.Instance.ApplicationUserDataPath; string folderToSaveStlsTo = Path.Combine(applicationUserDataPath, "data", "temp", "amf_to_stl"); if (!Directory.Exists(folderToSaveStlsTo)) { Directory.CreateDirectory(folderToSaveStlsTo); } MeshOutputSettings settings = new MeshOutputSettings(); settings.MaterialIndexsToSave = materialIndexsToSaveInThisSTL; string extruder1StlFileToSlice = Path.Combine(folderToSaveStlsTo, fileName); MeshFileIo.Save(extruderMeshGroup, extruder1StlFileToSlice, settings); return(extruder1StlFileToSlice); }
protected static void SaveToLibraryFolder(PrintItemWrapper printItemWrapper, List <MeshGroup> meshGroups, bool AbsolutePositioned) { string[] metaData = { "Created By", "MatterControl" }; if (AbsolutePositioned) { metaData = new string[] { "Created By", "MatterControl", "BedPosition", "Absolute" }; } // if it is not already in the right location if (!printItemWrapper.FileLocation.Contains(ApplicationDataStorage.Instance.ApplicationLibraryDataPath)) { // save a copy to the library and update this to point at it string fileName = Path.ChangeExtension(Path.GetRandomFileName(), ".amf"); printItemWrapper.FileLocation = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, fileName); MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, metaData); MeshFileIo.Save(meshGroups, printItemWrapper.FileLocation, outputInfo); } }
static public void SaveToLibraryFolder(PrintItemWrapper printItemWrapper, List <MeshGroup> meshGroups) { if (printItemWrapper.FileLocation.Contains(ApplicationDataStorage.Instance.ApplicationLibraryDataPath)) { MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, new string[] { "Created By", "MatterControl" }); MeshFileIo.Save(meshGroups, printItemWrapper.FileLocation, outputInfo); } else // save a copy to the library and update this to point at it { string fileName = Path.ChangeExtension(Path.GetRandomFileName(), ".amf"); printItemWrapper.FileLocation = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, fileName); MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, new string[] { "Created By", "MatterControl" }); MeshFileIo.Save(meshGroups, printItemWrapper.FileLocation, outputInfo); printItemWrapper.PrintItem.Commit(); } printItemWrapper.OnFileHasChanged(); }
/// <summary> /// Writes the mesh to disk in a zip container /// </summary> /// <param name="item">The mesh to save</param> /// <param name="fileName">The file path to save at</param> /// <param name="outputInfo">Extra meta data to store in the file</param> /// <returns>The results of the save operation</returns> public static bool Save(IObject3D item, string fileName, MeshOutputSettings outputInfo = null) { try { using (Stream stream = File.OpenWrite(fileName)) using (var archive = new ZipArchive(stream, ZipArchiveMode.Create)) { ZipArchiveEntry zipEntry = archive.CreateEntry(Path.GetFileName(fileName)); using (var entryStream = zipEntry.Open()) { return(Save(item, entryStream, outputInfo)); } } } catch (Exception e) { Debug.Print(e.Message); BreakInDebugger(); return(false); } }
public static bool Save(IObject3D item, Stream stream, MeshOutputSettings outputInfo) { throw new NotImplementedException(); }
public override IEnumerable <PrintItemAction> GetMenuItems() { return(new List <PrintItemAction>() { new PrintItemAction() { SingleItemOnly = false, Title = "Merge".Localize() + "...", Action = (items, queueDataWidget) => { List <QueueRowItem> allRowItems = new List <QueueRowItem>(items); if (allRowItems.Count > 1) { RenameItemWindow renameItemWindow = new RenameItemWindow(allRowItems[0].PrintItemWrapper.Name, (returnInfo) => { Task.Run(() => { List <MeshGroup> combinedMeshes = new List <MeshGroup>(); // Load up all the parts and merge them together foreach (QueueRowItem item in allRowItems) { List <MeshGroup> loadedMeshGroups = MeshFileIo.Load(item.PrintItemWrapper.FileLocation); combinedMeshes.AddRange(loadedMeshGroups); } // save them out string[] metaData = { "Created By", "MatterControl", "BedPosition", "Absolute" }; MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, metaData); string libraryDataPath = ApplicationDataStorage.Instance.ApplicationLibraryDataPath; if (!Directory.Exists(libraryDataPath)) { Directory.CreateDirectory(libraryDataPath); } string tempFileNameToSaveTo = Path.Combine(libraryDataPath, Path.ChangeExtension(Path.GetRandomFileName(), "amf")); bool savedSuccessfully = MeshFileIo.Save(combinedMeshes, tempFileNameToSaveTo, outputInfo); // Swap out the files if the save operation completed successfully if (savedSuccessfully && File.Exists(tempFileNameToSaveTo)) { // create a new print item // add it to the queue PrintItemWrapper newPrintItem = new PrintItemWrapper(new PrintItem(returnInfo.newName, tempFileNameToSaveTo)); QueueData.Instance.AddItem(newPrintItem, 0); // select the part we added, if possible QueueData.Instance.SelectedIndex = 0; queueDataWidget.LeaveEditMode(); } }); }, "Set Name".Localize()) { Title = "MatterHackers - Set Name".Localize(), ElementHeader = "Set Name".Localize(), }; } } } }); }
public static void Save(IObject3D itemToSave, Stream stream, MeshOutputSettings outputInfo) { TextWriter amfFile = new StreamWriter(stream); amfFile.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); amfFile.WriteLine("<amf unit=\"millimeter\" version=\"1.1\">"); if (outputInfo != null) { foreach (KeyValuePair <string, string> metaData in outputInfo.MetaDataKeyValue) { amfFile.WriteLine(Indent(1) + "<metadata type=\"{0}\">{1}</metadata>".FormatWith(metaData.Key, metaData.Value)); } } { var visibleMeshes = itemToSave.VisibleMeshes(); int totalMeshes = visibleMeshes.Count(); double ratioPerMesh = 1d / totalMeshes; double currentRatio = 0; var groupedByNameColorMaterial = visibleMeshes .GroupBy(i => (i.Name, i.Color, i.WorldMaterialIndex(), i.WorldOutputType())); int objectId = 0; foreach (var group in groupedByNameColorMaterial) { objectId++; amfFile.WriteLine(Indent(1) + "<object id=\"{0}\">".FormatWith(objectId)); { int vertexCount = 0; var meshVertexStart = new List <int>(); amfFile.WriteLine(Indent(2) + "<mesh>"); { amfFile.WriteLine(Indent(3) + "<vertices>"); { foreach (var item in group) { var matrix = item.WorldMatrix(); var mesh = item.Mesh; double meshVertCount = (double)mesh.Vertices.Count; meshVertexStart.Add(vertexCount); for (int vertexIndex = 0; vertexIndex < meshVertCount; vertexIndex++) { var position = mesh.Vertices[vertexIndex].Transform(matrix); outputInfo?.ReportProgress?.Invoke(currentRatio + vertexIndex / meshVertCount * ratioPerMesh * .5, ""); amfFile.WriteLine(Indent(4) + "<vertex>"); { amfFile.WriteLine(Indent(5) + "<coordinates>"); amfFile.WriteLine(Indent(6) + "<x>{0}</x>".FormatWith(position.X)); amfFile.WriteLine(Indent(6) + "<y>{0}</y>".FormatWith(position.Y)); amfFile.WriteLine(Indent(6) + "<z>{0}</z>".FormatWith(position.Z)); amfFile.WriteLine(Indent(5) + "</coordinates>"); } amfFile.WriteLine(Indent(4) + "</vertex>"); vertexCount++; } currentRatio += ratioPerMesh * .5; } } int meshIndex = 0; amfFile.WriteLine(Indent(3) + "</vertices>"); foreach (var item in group) { var mesh = item.Mesh; int firstVertexIndex = meshVertexStart[meshIndex++]; amfFile.WriteLine(Indent(3) + "<volume materialid=\"{0}\">".FormatWith(objectId)); double faceCount = (double)mesh.Faces.Count; for (int faceIndex = 0; faceIndex < faceCount; faceIndex++) { outputInfo?.ReportProgress?.Invoke(currentRatio + faceIndex / faceCount * ratioPerMesh * .5, ""); Face face = mesh.Faces[faceIndex]; amfFile.WriteLine(Indent(4) + "<triangle>"); amfFile.WriteLine(Indent(5) + $"<v1>{firstVertexIndex + face.v0}</v1>"); amfFile.WriteLine(Indent(5) + $"<v2>{firstVertexIndex + face.v1}</v2>"); amfFile.WriteLine(Indent(5) + $"<v3>{firstVertexIndex + face.v2}</v3>"); amfFile.WriteLine(Indent(4) + "</triangle>"); } currentRatio += ratioPerMesh * .5; amfFile.WriteLine(Indent(3) + "</volume>"); } } amfFile.WriteLine(Indent(2) + "</mesh>"); } amfFile.WriteLine(Indent(1) + "</object>"); } var nameColorMaterials = new HashSet <(string name, Color c, int material, PrintOutputTypes output)>(); foreach (var group in groupedByNameColorMaterial) { foreach (var item in group) { nameColorMaterials.Add((item.Name, item.WorldColor(), item.WorldMaterialIndex(), item.WorldOutputType())); } } int id = 1; foreach (var ncm in nameColorMaterials) { amfFile.WriteLine(Indent(1) + "<material id=\"{0}\">".FormatWith(id++)); amfFile.WriteLine(Indent(2) + $"<metadata type=\"Name\">{ncm.name}</metadata>"); amfFile.WriteLine(Indent(2) + $"<metadata type=\"MaterialIndex\">{ncm.material}</metadata>"); amfFile.WriteLine(Indent(2) + $"<color><r>{ncm.c.Red0To1}</r><g>{ncm.c.Green0To1}</g><b>{ncm.c.Blue0To1}</b></color>"); amfFile.WriteLine(Indent(2) + $"<metadata type=\"OutputType\">{ncm.output}</metadata>"); amfFile.WriteLine(Indent(1) + "</material>"); } } amfFile.WriteLine("</amf>"); amfFile.Flush(); }
/// <summary> /// Creates a database PrintItem entity, if forceAMF is set, converts to AMF otherwise just copies /// the source file to a new library path and updates the PrintItem to point at the new target /// </summary> private void AddItem(Stream stream, string extension, string displayName, bool forceAMF = true) { // Create a new entity in the database PrintItem printItem = new PrintItem(); printItem.Name = displayName; printItem.PrintItemCollectionID = this.baseLibraryCollection.Id; printItem.Commit(); // Special load processing for mesh data, simple copy below for non-mesh if (forceAMF && (extension != "" && MeshFileIo.ValidFileExtensions().Contains(extension.ToUpper()))) { try { // Load mesh List <MeshGroup> meshToConvertAndSave = MeshFileIo.Load(stream, extension); // Create a new PrintItemWrapper if (!printItem.FileLocation.Contains(ApplicationDataStorage.Instance.ApplicationLibraryDataPath)) { string[] metaData = { "Created By", "MatterControl" }; if (false) //AbsolutePositioned { metaData = new string[] { "Created By", "MatterControl", "BedPosition", "Absolute" }; } // save a copy to the library and update this to point at it printItem.FileLocation = CreateLibraryPath(".amf"); var outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, metaData); MeshFileIo.Save(meshToConvertAndSave, printItem.FileLocation, outputInfo); printItem.Commit(); } } catch (System.UnauthorizedAccessException) { UiThread.RunOnIdle(() => { //Do something special when unauthorized? StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes, unauthorized access", "Unable to save"); }); } catch { UiThread.RunOnIdle(() => { StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.", "Unable to save"); }); } } else // it is not a mesh so just add it { // Non-mesh content - copy stream to new Library path printItem.FileLocation = CreateLibraryPath(extension); using (var outStream = File.Create(printItem.FileLocation)) { stream.CopyTo(outStream); } printItem.Commit(); } }
public static bool Save(IObject3D itemToSave, Stream stream, MeshOutputSettings outputInfo) { TextWriter amfFile = new StreamWriter(stream); amfFile.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); amfFile.WriteLine("<amf unit=\"millimeter\" version=\"1.1\">"); if (outputInfo != null) { foreach (KeyValuePair <string, string> metaData in outputInfo.MetaDataKeyValue) { amfFile.WriteLine(Indent(1) + "<metadata type=\"{0}\">{1}</metadata>".FormatWith(metaData.Key, metaData.Value)); } } { int objectId = 1; var visibleMeshes = itemToSave.VisibleMeshes(); int totalMeshes = visibleMeshes.Count(); double ratioPerMesh = 1d / totalMeshes; double currentRation = 0; var groupedByExtruder = from item in visibleMeshes group item.Mesh by item.WorldMaterialIndex() into g select new { Extruder = g.Key, Meshes = g.ToList() }; foreach (var meshForExtruder in groupedByExtruder) { amfFile.WriteLine(Indent(1) + "<object id=\"{0}\">".FormatWith(objectId++)); { int vertexCount = 0; List <int> meshVertexStart = new List <int>(); amfFile.WriteLine(Indent(2) + "<mesh>"); { amfFile.WriteLine(Indent(3) + "<vertices>"); { foreach (var mesh in meshForExtruder.Meshes) { double vertCount = (double)mesh.Vertices.Count; meshVertexStart.Add(vertexCount); for (int vertexIndex = 0; vertexIndex < mesh.Vertices.Count; vertexIndex++) { IVertex vertex = mesh.Vertices[vertexIndex]; outputInfo.ReportProgress?.Invoke(currentRation + vertexIndex / vertCount * ratioPerMesh * .5, ""); Vector3 position = vertex.Position; amfFile.WriteLine(Indent(4) + "<vertex>"); { amfFile.WriteLine(Indent(5) + "<coordinates>"); amfFile.WriteLine(Indent(6) + "<x>{0}</x>".FormatWith(position.X)); amfFile.WriteLine(Indent(6) + "<y>{0}</y>".FormatWith(position.Y)); amfFile.WriteLine(Indent(6) + "<z>{0}</z>".FormatWith(position.Z)); amfFile.WriteLine(Indent(5) + "</coordinates>"); } amfFile.WriteLine(Indent(4) + "</vertex>"); vertexCount++; } currentRation += ratioPerMesh * .5; } } int meshIndex = 0; amfFile.WriteLine(Indent(3) + "</vertices>"); foreach (var mesh in meshForExtruder.Meshes) { int firstVertexIndex = meshVertexStart[meshIndex++]; amfFile.WriteLine(Indent(3) + "<volume>"); double faceCount = (double)mesh.Faces.Count; for (int faceIndex = 0; faceIndex < mesh.Faces.Count; faceIndex++) { outputInfo.ReportProgress?.Invoke(currentRation + faceIndex / faceCount * ratioPerMesh * .5, ""); Face face = mesh.Faces[faceIndex]; List <IVertex> positionsCCW = new List <IVertex>(); foreach (FaceEdge faceEdge in face.FaceEdges()) { positionsCCW.Add(faceEdge.FirstVertex); } int numPolys = positionsCCW.Count - 2; int secondIndex = 1; int thirdIndex = 2; for (int polyIndex = 0; polyIndex < numPolys; polyIndex++) { amfFile.WriteLine(Indent(4) + "<triangle>"); amfFile.WriteLine(Indent(5) + "<v1>{0}</v1>".FormatWith(firstVertexIndex + mesh.Vertices.IndexOf(positionsCCW[0]))); amfFile.WriteLine(Indent(5) + "<v2>{0}</v2>".FormatWith(firstVertexIndex + mesh.Vertices.IndexOf(positionsCCW[secondIndex]))); amfFile.WriteLine(Indent(5) + "<v3>{0}</v3>".FormatWith(firstVertexIndex + mesh.Vertices.IndexOf(positionsCCW[thirdIndex]))); amfFile.WriteLine(Indent(4) + "</triangle>"); secondIndex = thirdIndex; thirdIndex++; } } currentRation += ratioPerMesh * .5; amfFile.WriteLine(Indent(3) + "</volume>"); } } amfFile.WriteLine(Indent(2) + "</mesh>"); } amfFile.WriteLine(Indent(1) + "</object>"); } } amfFile.WriteLine("</amf>"); amfFile.Flush(); return(true); }