2018-11-13 18:43:27 -08:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2018, 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 System.IO;
|
|
|
|
|
|
using MatterControl.Printing;
|
2021-03-23 08:12:52 -07:00
|
|
|
|
using MatterHackers.Agg;
|
|
|
|
|
|
using MatterHackers.DataConverters3D;
|
|
|
|
|
|
using MatterHackers.MatterControl.DataStorage;
|
|
|
|
|
|
using MatterHackers.MatterControl.DesignTools;
|
|
|
|
|
|
using MatterHackers.MatterControl.Library;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2022-02-02 17:31:44 -08:00
|
|
|
|
using System;
|
2022-02-26 22:21:29 -08:00
|
|
|
|
using System.Collections.Generic;
|
2018-11-13 18:43:27 -08:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public class EditContext
|
|
|
|
|
|
{
|
2018-12-06 12:14:47 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The object responsible for item persistence
|
|
|
|
|
|
/// </summary>
|
2018-11-13 18:43:27 -08:00
|
|
|
|
public IContentStore ContentStore { get; set; }
|
|
|
|
|
|
|
2021-03-23 08:12:52 -07:00
|
|
|
|
public string SourceFilePath
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
2021-03-23 08:12:52 -07:00
|
|
|
|
get
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
2021-03-23 08:12:52 -07:00
|
|
|
|
if (SourceItem is ILibraryAsset fileItem)
|
2018-11-13 18:43:27 -08:00
|
|
|
|
{
|
2021-03-23 08:12:52 -07:00
|
|
|
|
return fileItem.AssetPath;
|
2018-11-13 18:43:27 -08:00
|
|
|
|
}
|
2021-03-23 08:12:52 -07:00
|
|
|
|
|
|
|
|
|
|
return null;
|
2018-11-13 18:43:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-23 08:12:52 -07:00
|
|
|
|
public bool FreezeGCode { get; set; }
|
|
|
|
|
|
|
2022-02-02 17:31:44 -08:00
|
|
|
|
public event EventHandler SourceItemChanged;
|
|
|
|
|
|
|
|
|
|
|
|
private ILibraryItem _sourceItem;
|
2021-03-23 08:12:52 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The library item to load and persist
|
|
|
|
|
|
/// </summary>
|
2022-02-02 17:31:44 -08:00
|
|
|
|
public ILibraryItem SourceItem
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _sourceItem;
|
|
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (value != _sourceItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
_sourceItem = value;
|
|
|
|
|
|
SourceItemChanged?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-03-23 08:12:52 -07:00
|
|
|
|
|
2019-06-14 14:13:23 -07:00
|
|
|
|
public bool IsGGCodeSource => (this.SourceItem as ILibraryAsset)?.ContentType == "gcode";
|
|
|
|
|
|
|
2018-12-06 12:14:47 -08:00
|
|
|
|
// Override or natural path
|
2020-07-29 14:29:59 -07:00
|
|
|
|
public async Task<string> GCodeFilePath(PrinterConfig printer)
|
2018-12-06 12:14:47 -08:00
|
|
|
|
{
|
2020-07-29 14:29:59 -07:00
|
|
|
|
var path = await this.GCodeOverridePath(printer);
|
|
|
|
|
|
if (File.Exists(path))
|
2018-12-06 12:14:47 -08:00
|
|
|
|
{
|
2020-07-29 14:29:59 -07:00
|
|
|
|
return path;
|
2018-12-06 12:14:47 -08:00
|
|
|
|
}
|
2019-06-14 14:13:23 -07:00
|
|
|
|
|
2020-07-29 14:29:59 -07:00
|
|
|
|
return await GCodePath(printer);
|
2018-12-06 12:14:47 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-22 13:12:24 -07:00
|
|
|
|
public static string GCodeFilePath(PrinterConfig printer, IObject3D object3D)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
// Write JSON
|
|
|
|
|
|
object3D.SaveTo(memoryStream);
|
|
|
|
|
|
|
|
|
|
|
|
// Reposition
|
|
|
|
|
|
memoryStream.Position = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate
|
|
|
|
|
|
string fileHashCode = HashGenerator.ComputeSHA1(memoryStream);
|
|
|
|
|
|
|
|
|
|
|
|
ulong settingsHashCode = printer.Settings.GetGCodeCacheKey();
|
|
|
|
|
|
|
|
|
|
|
|
return Path.Combine(
|
|
|
|
|
|
ApplicationDataStorage.Instance.GCodeOutputPath,
|
|
|
|
|
|
$"{fileHashCode}_{ settingsHashCode}.gcode");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-04 16:26:28 -07:00
|
|
|
|
public async Task Save(IObject3D scene)
|
2018-12-06 12:14:47 -08:00
|
|
|
|
{
|
|
|
|
|
|
if (!this.FreezeGCode)
|
|
|
|
|
|
{
|
2022-02-21 08:52:49 -08:00
|
|
|
|
if (this.SourceItem != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ApplicationController.Instance.Thumbnails.DeleteCache(this.SourceItem);
|
|
|
|
|
|
}
|
2018-12-06 12:14:47 -08:00
|
|
|
|
|
2022-02-12 09:56:24 -08:00
|
|
|
|
if (scene is InteractiveScene interactiveScene)
|
|
|
|
|
|
{
|
2022-02-26 22:49:12 -08:00
|
|
|
|
using (new SelectionMaintainer(interactiveScene))
|
2022-02-26 22:21:29 -08:00
|
|
|
|
{
|
|
|
|
|
|
// Call save on the provider
|
2022-04-04 16:26:28 -07:00
|
|
|
|
await this.ContentStore?.Save(this.SourceItem, scene);
|
2022-02-26 22:21:29 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-12 09:56:24 -08:00
|
|
|
|
interactiveScene.MarkSavePoint();
|
|
|
|
|
|
}
|
2022-02-26 22:21:29 -08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Call save on the provider
|
2022-04-04 16:26:28 -07:00
|
|
|
|
await this.ContentStore?.Save(this.SourceItem, scene);
|
2022-02-26 22:21:29 -08:00
|
|
|
|
}
|
2018-12-06 12:14:47 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-04-06 17:53:41 -07:00
|
|
|
|
|
|
|
|
|
|
// Natural path
|
|
|
|
|
|
private async Task<string> GCodePath(PrinterConfig printer)
|
2018-11-14 14:28:29 -08:00
|
|
|
|
{
|
2018-12-05 17:07:55 -08:00
|
|
|
|
if (File.Exists(this.SourceFilePath))
|
2018-11-14 14:28:29 -08:00
|
|
|
|
{
|
2020-07-29 14:29:59 -07:00
|
|
|
|
return await this.GetGCodePath(printer, this.SourceFilePath);
|
2018-11-14 14:28:29 -08:00
|
|
|
|
}
|
2018-12-05 16:48:04 -08:00
|
|
|
|
|
2018-11-14 14:28:29 -08:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2018-11-13 18:43:27 -08:00
|
|
|
|
|
2020-07-29 14:29:59 -07:00
|
|
|
|
public static string[] GetFileNamesFromMcx(string mcxFileName)
|
|
|
|
|
|
{
|
|
|
|
|
|
// add in the cache path
|
|
|
|
|
|
mcxFileName = Path.Combine(ApplicationDataStorage.Instance.PlatingDirectory, mcxFileName);
|
|
|
|
|
|
if (File.Exists(mcxFileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
var document = JsonConvert.DeserializeObject<McxDocument.McxNode>(File.ReadAllText(mcxFileName));
|
|
|
|
|
|
var names = document.AllVisibleMeshFileNames();
|
|
|
|
|
|
return names.GroupBy(n => n)
|
|
|
|
|
|
.Select(g => g.Key)
|
|
|
|
|
|
.OrderBy(n => n)
|
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-12-05 18:31:58 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns the computed GCode path given a content file path and considering current settings
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="printer">The associated printer</param>
|
|
|
|
|
|
/// <param name="fileLocation">The source file</param>
|
|
|
|
|
|
/// <returns>The target GCode path</returns>
|
2020-07-29 14:29:59 -07:00
|
|
|
|
private async Task<string> GetGCodePath(PrinterConfig printer, string fileLocation)
|
2018-12-05 18:31:58 -08:00
|
|
|
|
{
|
|
|
|
|
|
if (fileLocation.Trim() != "")
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Path.GetExtension(fileLocation).ToUpper() == ".GCODE")
|
|
|
|
|
|
{
|
|
|
|
|
|
return fileLocation;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string fileHashCode = HashGenerator.ComputeFileSHA1(fileLocation);
|
2020-07-29 14:29:59 -07:00
|
|
|
|
|
|
|
|
|
|
var fileExtension = Path.GetExtension(fileLocation).ToUpper();
|
|
|
|
|
|
if (fileExtension == ".MCX")
|
|
|
|
|
|
{
|
|
|
|
|
|
var hashCode = fileHashCode.GetLongHashCode();
|
|
|
|
|
|
var fileNames = GetFileNamesFromMcx(fileLocation);
|
|
|
|
|
|
foreach (var file in fileNames)
|
|
|
|
|
|
{
|
2022-02-15 11:20:05 -08:00
|
|
|
|
var fullPath = Object3DExtensions.ResolveFilePath(file, null, CancellationToken.None);
|
2020-07-29 14:29:59 -07:00
|
|
|
|
if (File.Exists(fullPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
hashCode = File.GetLastWriteTime(fullPath).ToString().GetLongHashCode(hashCode);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fileHashCode = hashCode.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-21 15:07:34 -07:00
|
|
|
|
ulong settingsHashCode = printer.Settings.GetGCodeCacheKey();
|
2018-12-05 18:31:58 -08:00
|
|
|
|
|
|
|
|
|
|
return Path.Combine(
|
|
|
|
|
|
ApplicationDataStorage.Instance.GCodeOutputPath,
|
|
|
|
|
|
$"{fileHashCode}_{ settingsHashCode}.gcode");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-13 18:43:27 -08:00
|
|
|
|
// Override path
|
2020-07-29 14:29:59 -07:00
|
|
|
|
private async Task<string> GCodeOverridePath(PrinterConfig printer)
|
2018-11-14 14:28:29 -08:00
|
|
|
|
{
|
2020-07-29 14:29:59 -07:00
|
|
|
|
return Path.ChangeExtension(await GCodePath(printer), GCodeFile.PostProcessedExtension);
|
2018-11-14 14:28:29 -08:00
|
|
|
|
}
|
2018-11-13 18:43:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|