Save to compressed mcx is working

Don't call save twice
This commit is contained in:
Lars Brubaker 2022-02-02 10:52:42 -08:00
parent c351af843b
commit a2aaaf6715
9 changed files with 109 additions and 56 deletions

View file

@ -2167,11 +2167,13 @@ namespace MatterHackers.MatterControl
if (settingsFilePath != null)
{
using (var file = File.OpenWrite(archivePath))
using (var zip = new ZipArchive(file, ZipArchiveMode.Create))
{
zip.CreateEntryFromFile(sourcePath, "PrinterPlate.mcx");
zip.CreateEntryFromFile(settingsFilePath, printer.PrinterName + ".printer");
zip.CreateEntryFromFile(gcodeFilePath, "sliced.gcode");
using (var zip = new ZipArchive(file, ZipArchiveMode.Create))
{
zip.CreateEntryFromFile(sourcePath, "PrinterPlate.mcx");
zip.CreateEntryFromFile(settingsFilePath, printer.PrinterName + ".printer");
zip.CreateEntryFromFile(gcodeFilePath, "sliced.gcode");
}
}
}
}

View file

@ -194,7 +194,7 @@ namespace MatterHackers.MatterControl.Library.Export
{
status.Progress0To1 = percentComplete;
progress.Report(status);
}, publishAssets: false);
}, forceIntoCache: false);
}
else if (assetStream != null)
{

View file

@ -30,6 +30,7 @@ either expressed or implied, of the FreeBSD Project.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using MatterHackers.Agg;
@ -37,6 +38,7 @@ using MatterHackers.Agg.Image;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.DataStorage;
namespace MatterHackers.MatterControl.Library
@ -84,18 +86,83 @@ namespace MatterHackers.MatterControl.Library
{
if (item is FileSystemFileItem fileItem)
{
if (fileItem.FilePath.Contains(ApplicationDataStorage.Instance.LibraryAssetsPath))
{
if (fileItem.FilePath.Contains(ApplicationDataStorage.Instance.ApplicationLibraryDataPath))
{
// save using the normal uncompressed mcx file
base.Save(item, content);
}
}
else
{
// save to a binary mcx file (a zip with a scene.mcx and an assets folder)
{
ApplicationController.Instance.Tasks.Execute(
"Saving Changes".Localize(),
null,
async (reporter, cancellationTokenSource) =>
{
var status = new ProgressStatus()
{
Status = "Saving Asset".Localize()
};
// Serialize the scene to disk using a modified Json.net pipeline with custom ContractResolvers and JsonConverters
File.WriteAllText(fileItem.FilePath, content.ToJson());
this.OnItemContentChanged(new LibraryItemChangedEventArgs(fileItem));
// make sure we have all the mesh items in the cache for saving to the archive
await content.PersistAssets((percentComplete, text) =>
{
status.Progress0To1 = percentComplete * .9;
reporter.Report(status);
}, forceIntoCache: true);
var backupName = "";
// rename any existing file
if (File.Exists(fileItem.FilePath))
{
var directory = Path.GetDirectoryName(fileItem.FilePath);
var filename = Path.GetFileNameWithoutExtension(fileItem.FilePath);
backupName = Path.Combine(directory, Path.ChangeExtension(filename + "_bak", ".mcx"));
File.Move(fileItem.FilePath, backupName);
}
var persistableItems = content.GetPersistable(true);
var persistCount = persistableItems.Count();
var savedCount = 0;
// save to a binary mcx file (a zip with a scene.mcx and an assets folder)
using (var file = File.OpenWrite(fileItem.FilePath))
{
using (var zip = new ZipArchive(file, ZipArchiveMode.Create))
{
using (var writer = new StreamWriter(zip.CreateEntry("scene.mcx").Open()))
{
writer.Write(content.ToJson());
}
foreach (var persistMeshItem in persistableItems)
{
var sourcePath = persistMeshItem.MeshPath;
if (!File.Exists(sourcePath))
{
sourcePath = Path.Combine(ApplicationDataStorage.Instance.LibraryAssetsPath, Path.GetFileName(persistMeshItem.MeshPath));
}
if (File.Exists(sourcePath))
{
var assetName = Path.Combine("Assets", Path.GetFileName(sourcePath));
zip.CreateEntryFromFile(sourcePath, assetName);
}
savedCount++;
status.Progress0To1 = .9 + .1 * (savedCount / persistCount);
reporter.Report(status);
}
}
}
// Serialize the scene to disk using a modified Json.net pipeline with custom ContractResolvers and JsonConverters
this.OnItemContentChanged(new LibraryItemChangedEventArgs(fileItem));
// remove the existing file after a successfull save
if (!string.IsNullOrEmpty(backupName))
{
File.Delete(backupName);
}
});
}
}
}

View file

@ -550,7 +550,12 @@ namespace MatterHackers.MatterControl.CustomWidgets
}
else
{
ApplicationController.Instance.OpenIntoNewTab(new[] { itemModel });
void OpenNewTab()
{
_ = ApplicationController.Instance.OpenIntoNewTab(new[] { itemModel });
}
OpenNewTab();
}
}
else

View file

@ -70,6 +70,8 @@ namespace MatterHackers.MatterControl
if (librarySelectorWidget.ActiveContainer is ILibraryWritableContainer)
{
acceptButton.InvokeClick();
// And disable it so there are not multiple fires. No need to re-enable, the dialog is going to close.
this.AcceptButton.Enabled = false;
}
};
contentRow.AddChild(itemNameWidget);

View file

@ -486,7 +486,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
{
this.CloseClicked?.Invoke(this, null);
// Must be called after CloseClicked otherwise listeners are cleared before event is invoked
this.parentTabControl.CloseTab(this);
this.parentTabControl?.CloseTab(this);
});
break;
}

View file

@ -603,8 +603,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
mergeString += ",";
}
// TODO: Use existing AssetsPath property
string assetsDirectory = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, "Assets");
var itemWorldMatrix = item.WorldMatrix();
if (item is GeneratedSupportObject3D generatedSupportObject3D
&& item.Mesh != null)
@ -616,7 +614,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
itemWorldMatrix = itemWorldMatrix.ApplyAtPosition(aabbForCenter.Center.Transform(itemWorldMatrix), Matrix4X4.CreateScale(xyScale, xyScale, 1));
}
outputItems.Add((itemWorldMatrix, Path.Combine(assetsDirectory, item.MeshPath)));
outputItems.Add((itemWorldMatrix, Path.Combine(ApplicationDataStorage.Instance.LibraryAssetsPath, item.MeshPath)));
mergeString += $"({savedStlCount++}";
first = false;
}