private void SaveAmf(SaveFileDialogParams saveParams) { try { if (saveParams.FileName != null) { string filePathToSave = saveParams.FileName; if (filePathToSave != null && filePathToSave != "") { string extension = Path.GetExtension(filePathToSave); if (extension == "") { File.Delete(filePathToSave); filePathToSave += ".amf"; } if (Path.GetExtension(printItemWrapper.FileLocation).ToUpper() == Path.GetExtension(filePathToSave).ToUpper()) { File.Copy(printItemWrapper.FileLocation, filePathToSave, true); } else { List <MeshGroup> meshGroups = MeshFileIo.Load(printItemWrapper.FileLocation); MeshFileIo.Save(meshGroups, filePathToSave); } ShowFileIfRequested(filePathToSave); } } } catch { } }
static public void SaveToLibraryFolder2(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(); }
private void MergeAndSavePartsDoWork(string filePath) { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; try { // push all the transforms into the meshes for (int i = 0; i < asyncMeshGroups.Count; i++) { asyncMeshGroups[i].Transform(MeshGroupTransforms[i].TotalTransform); processingProgressControl.RatioComplete = (double)i / asyncMeshGroups.Count * .1; } MeshFileIo.Save(asyncMeshGroups, filePath); } catch (System.UnauthorizedAccessException) { //Do something special when unauthorized? StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.".Localize(), "Unable to save".Localize()); } catch { StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.".Localize(), "Unable to save".Localize()); } }
private void stlSaveWorker_DoWork(object sender, DoWorkEventArgs e) { SaveFileDialogParams saveParams = e.Argument as SaveFileDialogParams; if (saveParams.FileName != null) { string filePathToSave = saveParams.FileName; if (filePathToSave != null && filePathToSave != "") { string extension = Path.GetExtension(filePathToSave); if (extension == "") { File.Delete(filePathToSave); filePathToSave += ".stl"; } if (Path.GetExtension(printItemWrapper.FileLocation).ToUpper() == Path.GetExtension(filePathToSave).ToUpper()) { File.Copy(printItemWrapper.FileLocation, filePathToSave, true); } else { List <MeshGroup> meshGroups = MeshFileIo.Load(printItemWrapper.FileLocation); MeshFileIo.Save(meshGroups, filePathToSave); } ShowFileIfRequested(filePathToSave); } } }
internal void MeshCopy() { { Mesh testMesh = new Mesh(); Vertex left = testMesh.CreateVertex(-1, -1, 0); Vertex middle = testMesh.CreateVertex(0, 1, 0); Vertex right = testMesh.CreateVertex(1, -1, 0); Vertex top = testMesh.CreateVertex(0, 0, 1); testMesh.CreateFace(new Vertex[] { left, top, middle }); testMesh.CreateFace(new Vertex[] { left, right, top }); testMesh.CreateFace(new Vertex[] { right, middle, top }); testMesh.CreateFace(new Vertex[] { left, middle, right }); testMesh.MergeVertices(); MeshFileIo.Save(testMesh, "control.stl", new MeshOutputSettings(MeshOutputSettings.OutputType.Ascii)); SaveDebugInfo(testMesh, "MeshCopy (orig)"); Mesh copyMesh = Mesh.Copy(testMesh); MeshFileIo.Save(testMesh, "test.stl", new MeshOutputSettings(MeshOutputSettings.OutputType.Ascii)); SaveDebugInfo(copyMesh, "MeshCopy (copy)"); Mesh copyMesh2 = Mesh.Copy(copyMesh); MeshFileIo.Save(copyMesh2, "test2.stl", new MeshOutputSettings(MeshOutputSettings.OutputType.Ascii)); SaveDebugInfo(copyMesh, "MeshCopy (copy2)"); } }
void onExportStlFileSelected(SaveFileDialogParams saveParams) { Close(); if (saveParams.FileName != null) { string filePathToSave = saveParams.FileName; if (filePathToSave != null && filePathToSave != "") { string extension = Path.GetExtension(filePathToSave); if (extension == "") { File.Delete(filePathToSave); filePathToSave += ".stl"; } if (Path.GetExtension(printItemWrapper.FileLocation).ToUpper() == Path.GetExtension(filePathToSave).ToUpper()) { File.Copy(printItemWrapper.FileLocation, filePathToSave, true); } else { List <MeshGroup> meshGroups = MeshFileIo.Load(printItemWrapper.FileLocation); MeshFileIo.Save(meshGroups, filePathToSave); } ShowFileIfRequested(filePathToSave); } } }
private static string SaveAndGetFilenameForMesh(Mesh meshToSave) { string fileName = Path.ChangeExtension(Path.GetRandomFileName(), ".stl"); string applicationUserDataPath = ApplicationDataStorage.ApplicationUserDataPath; string folderToSaveStlsTo = Path.Combine(applicationUserDataPath, "data", "temp", "amf_to_stl"); if (!Directory.Exists(folderToSaveStlsTo)) { Directory.CreateDirectory(folderToSaveStlsTo); } string saveStlFileName = Path.Combine(folderToSaveStlsTo, fileName); MeshFileIo.Save(meshToSave, saveStlFileName); return(saveStlFileName); }
public static async Task <bool> ExportMesh(ILibraryItem source, string filePathToSave) { try { if (source is ILibraryObject3D contentItem) { // If the content is an IObject3D, the we need to load it and MeshFileIO save to the target path var content = await contentItem.GetObject3D(null); return(MeshFileIo.Save(content, filePathToSave, CancellationToken.None)); } else if (source is ILibraryAssetStream streamContent) { if (!string.IsNullOrEmpty(filePathToSave)) { // If the file is already AMF, it just needs copied to the target path if (Path.GetExtension(streamContent.FileName).ToUpper() == Path.GetExtension(filePathToSave).ToUpper()) { using (var result = await streamContent.GetStream(null)) using (var fileStream = File.Create(filePathToSave)) { result.Stream.CopyTo(fileStream); } return(true); } else { // Otherwise we need to load the content and MeshFileIO save to the target path using (var result = await streamContent.GetStream(null)) { IObject3D item = Object3D.Load(result.Stream, Path.GetExtension(streamContent.FileName), CancellationToken.None); return(MeshFileIo.Save(item, filePathToSave, CancellationToken.None)); } } } } } catch (Exception ex) { Trace.WriteLine("Error exporting file: " + ex.Message); } 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); }
private void mergeAndSavePartsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; BackgroundWorker backgroundWorker = (BackgroundWorker)sender; try { // push all the transforms into the meshes for (int i = 0; i < asynchMeshGroups.Count; i++) { asynchMeshGroups[i].Transform(MeshGroupTransforms[i].TotalTransform); int nextPercent = (i + 1) * 40 / asynchMeshGroups.Count; backgroundWorker.ReportProgress(nextPercent); } string fileName = "TextCreator_{0}".FormatWith(Path.ChangeExtension(Path.GetRandomFileName(), ".amf")); string filePath = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, fileName); List <MeshGroup> mergResults = new List <MeshGroup>(); mergResults.Add(new MeshGroup()); mergResults[0].Meshes.Add(new Mesh()); foreach (MeshGroup meshGroup in asynchMeshGroups) { foreach (Mesh mesh in meshGroup.Meshes) { mergResults[0].Meshes[0] = CsgOperations.Union(mergResults[0].Meshes[0], mesh); } } MeshFileIo.Save(mergResults, filePath); e.Result = filePath; } catch (System.UnauthorizedAccessException) { //Do something special when unauthorized? StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.".Localize(), "Unable to save".Localize()); } catch { StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.".Localize(), "Unable to save".Localize()); } }
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(); }
private void SaveStl(SaveFileDialogParams saveParams) { try { if (!string.IsNullOrEmpty(saveParams.FileName)) { string filePathToSave = saveParams.FileName; if (filePathToSave != null && filePathToSave != "") { string extension = Path.GetExtension(filePathToSave); if (extension == "") { File.Delete(filePathToSave); filePathToSave += ".stl"; } if (Path.GetExtension(printItemWrapper.FileLocation).ToUpper() == Path.GetExtension(filePathToSave).ToUpper()) { File.Copy(printItemWrapper.FileLocation, filePathToSave, true); } else { List <MeshGroup> meshGroups = MeshFileIo.Load(printItemWrapper.FileLocation); if (!MeshFileIo.Save(meshGroups, filePathToSave)) { UiThread.RunOnIdle(() => { StyledMessageBox.ShowMessageBox(null, "AMF to STL conversion failed", "Couldn't save file".Localize()); }); } } ShowFileIfRequested(filePathToSave); } } } catch (Exception e) { UiThread.RunOnIdle(() => { StyledMessageBox.ShowMessageBox(null, e.Message, "Couldn't save file".Localize()); }); } }
private void mergeAndSavePartsBackgroundWorker_DoWork(string filePath) { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; try { // push all the transforms into the meshes for (int i = 0; i < asynchMeshGroups.Count; i++) { asynchMeshGroups[i].Transform(MeshGroupTransforms[i].TotalTransform); processingProgressControl.RatioComplete = (double)i / asynchMeshGroups.Count * .1; } List <MeshGroup> mergResults = new List <MeshGroup>(); mergResults.Add(new MeshGroup()); mergResults[0].Meshes.Add(new Mesh()); double meshGroupIndex = 0; foreach (MeshGroup meshGroup in asynchMeshGroups) { foreach (Mesh mesh in meshGroup.Meshes) { processingProgressControl.RatioComplete = .1 + (double)meshGroupIndex / asynchMeshGroups.Count; mergResults[0].Meshes[0] = CsgOperations.Union(mergResults[0].Meshes[0], mesh); } meshGroupIndex++; } MeshFileIo.Save(mergResults, filePath); } catch (System.UnauthorizedAccessException) { //Do something special when unauthorized? StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.".Localize(), "Unable to save".Localize()); } catch { StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.".Localize(), "Unable to save".Localize()); } }
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(), }; } } } }); }
/// <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(); } }