Files
UVtools/UVtools.Core/Helpers.cs
T
Tiago Conceição 097bbb867d v2.3.0
* **PrusaSlicer:**
   * **In this release is recommended to discard your printer and refresh it with uvtools updated printer or replace notes over**
   * (Add) FILEFORMAT_XXX variable to auto-convert to that file format once open in UVtools
   * (Update) Print profiles fields with new PrusaSlicer version
   * (Remove) LayerOffDelay from printer notes and use only the LightOffDelay variable instead, both were being used, to avoid redundacy LayerOffDelay was dropped. Please update your printer accordingly!
   * (Remove) FLIP_XY compability from printers
   * (Remove) AntiAlias variable from printers
* **(Add) Settings - Automations:**
   * Auto save the file after apply any automation(s)
   * Auto convert SL1 files to the target format when possible and load it back
   * Auto set the extra 'light-off delay' based on lift height and speed.
* (Add) Allow all and future formats to convert between them without knowing each other (Abstraction)
* (Add) XYResolution and XYResolutionUm property to file formats
* (Add) Calculator - Optimal model tilt: Calculates the optimal model tilt angle for printing and to minimize the visual layer effect
* (Add) Bottom layer count to the status bar
* **(Add) FileFormat propertiers:**
    * MirrorDisplay: If images need to be mirrored on lcd to print on the correct orientation (If available)
    * MaxPrintHeight: The maximum Z build volume of the printer (If available)
* (Add) ZCodex: Print paramenter light-off delay"
* (Add) SL1: Implement missing keys: host_type, physical_printer_settings_id and support_small_pillar_diameter_percent
* (Change) File formats: Round all setters floats to 2 decimals
* (Change) Island Repair: "Remove Islands Below Equal Pixels" limit from 255 to 65535 (#124)
* (Change) LightOffTime variables to LayerOffDelay
* (Fix) Files with upper case extensions doesn't load in
* **(Fix) SL1:**
    * Prevent error when bottle volume is 0
    * bool values were incorrectly parsed
* (Fix) **ZIP:**
    * Material volume was set to grams
    * Bed Y was not being set
2021-01-13 23:19:23 +00:00

114 lines
4.0 KiB
C#

/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using BinarySerialization;
using Newtonsoft.Json;
using UVtools.Core.Extensions;
namespace UVtools.Core
{
/// <summary>
/// A helper class with utilities
/// </summary>
public static class Helpers
{
/// <summary>
/// Gets the <see cref="BinarySerializer"/> instance
/// </summary>
public static BinarySerializer Serializer { get; } = new BinarySerializer {Endianness = Endianness.Little };
public static MemoryStream Serialize(object value)
{
MemoryStream stream = new MemoryStream();
Serializer.Serialize(stream, value);
return stream;
}
public static T Deserialize<T>(Stream stream)
{
return Serializer.Deserialize<T>(stream);
}
public static uint SerializeWriteFileStream(FileStream fs, object value, int offset = 0)
{
using (MemoryStream stream = Serialize(value))
{
return fs.WriteStream(stream, offset);
}
}
public static T JsonDeserializeObject<T>(Stream stream)
{
using (TextReader tr = new StreamReader(stream))
{
return JsonConvert.DeserializeObject<T>(tr.ReadToEnd());
}
}
public static SHA1CryptoServiceProvider SHA1 { get; } = new SHA1CryptoServiceProvider();
public static string ComputeSHA1Hash(byte[] input)
{
return Convert.ToBase64String(SHA1.ComputeHash(input));
}
public static bool SetPropertyValue(PropertyInfo attribute, object obj, string value)
{
var name = attribute.PropertyType.Name.ToLower();
switch (name)
{
case "string":
attribute.SetValue(obj, value.Convert<string>());
return true;
case "boolean":
if(char.IsDigit(value[0]))
attribute.SetValue(obj, !value.Equals("0"));
else
attribute.SetValue(obj, value.Equals("True", StringComparison.InvariantCultureIgnoreCase));
return true;
case "byte":
attribute.SetValue(obj, value.Convert<byte>());
return true;
case "uint16":
attribute.SetValue(obj, value.Convert<ushort>());
return true;
case "uint32":
attribute.SetValue(obj, value.Convert<uint>());
return true;
case "single":
attribute.SetValue(obj, (float)Math.Round(float.Parse(value, CultureInfo.InvariantCulture.NumberFormat), 3));
return true;
case "double":
attribute.SetValue(obj, Math.Round(double.Parse(value, CultureInfo.InvariantCulture.NumberFormat), 3));
return true;
case "decimal":
attribute.SetValue(obj, Math.Round(decimal.Parse(value, CultureInfo.InvariantCulture.NumberFormat), 3));
return true;
default:
throw new Exception($"Data type '{name}' not recognized, contact developer.");
}
}
public static int GetPixelPosition(int width, int x, int y)
{
return width * y + x;
}
public static void SwapVariables<T>(ref T var1, ref T var2)
{
var backup = var1;
var1 = var2;
var2 = backup;
}
}
}