aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/Helpers
diff options
context:
space:
mode:
authorVictoria Plitt <Victoria.Plitt@twine-s.com>2020-12-27 12:07:57 +0200
committerVictoria Plitt <Victoria.Plitt@twine-s.com>2020-12-27 12:07:57 +0200
commit36928d513d8091c20b248e065ef7b6a0e3bc61ee (patch)
tree52b6dde5e097c637d3510868edbb31ef41026da0 /Software/Visual_Studio/Tango.BL/Helpers
parent953cb38772543e941c9b115a787bc2bec2187ee1 (diff)
downloadTango-36928d513d8091c20b248e065ef7b6a0e3bc61ee.tar.gz
Tango-36928d513d8091c20b248e065ef7b6a0e3bc61ee.zip
to load/save a segments on PPC from/to an csv file
Diffstat (limited to 'Software/Visual_Studio/Tango.BL/Helpers')
-rw-r--r--Software/Visual_Studio/Tango.BL/Helpers/SegmentsCsvHelper.cs327
1 files changed, 327 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.BL/Helpers/SegmentsCsvHelper.cs b/Software/Visual_Studio/Tango.BL/Helpers/SegmentsCsvHelper.cs
new file mode 100644
index 000000000..e2bffcc03
--- /dev/null
+++ b/Software/Visual_Studio/Tango.BL/Helpers/SegmentsCsvHelper.cs
@@ -0,0 +1,327 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tango.BL.Builders;
+using Tango.BL.DTO;
+using Tango.BL.Entities;
+using Tango.BL.Enumerations;
+using Tango.CSV;
+using Tango.PMR.Exports;
+
+namespace Tango.BL.Helpers
+{
+ public static class SegmentsCsvHelper
+ {
+ public class SegmentCsvModel
+ {
+
+ public String Index { get; set; }
+
+ public String ColorSpace { get; set; }
+
+ public String Length { get; set; }
+
+ public String CatalogName1 { get; set; }
+ public String CatalogItem1 { get; set; }
+
+ public String Red1 { get; set; }
+ public String Green1 { get; set; }
+ public String Blue1 { get; set; }
+ public String Red2 { get; set; }
+ public String Green2 { get; set; }
+ public String Blue2 { get; set; }
+
+ public String Cyan1 { get; set; }
+ public String Magenta1 { get; set; }
+ public String Yellow1 { get; set; }
+ public String Black1 { get; set; }
+
+ public String Cyan2{ get; set; }
+ public String Magenta2 { get; set; }
+ public String Yellow2 { get; set; }
+ public String Black2 { get; set; }
+
+ public String L1 { get; set; }
+ public String A1 { get; set; }
+ public String B1 { get; set; }
+ public String L2 { get; set; }
+ public String A2 { get; set; }
+ public String B2 { get; set; }
+
+ public String CatalogName2 { get; set; }
+ public String CatalogItem2 { get; set; }
+
+ public bool IsRgb2NullOrEqual()
+ {
+ if (Red2 == null && Green2 == null && Blue2 == null) return true;
+ if (Red1 == Red2 && Green1 == Green2 && Blue1 == Blue2) return true;
+ return false;
+ }
+
+ internal bool IsVolumeNullOrEqual()
+ {
+ if (Cyan2 == null && Magenta2 == null && Yellow2 == null && Black2 == null) return true;
+ if (Cyan1 == Cyan2 && Magenta1 == Magenta2 && Yellow1 == Yellow2 && Black1 == Black2) return true;
+ return false;
+ }
+
+ internal bool IsCatalogNameNullOrEqual()
+ {
+ if (String.IsNullOrEmpty(CatalogName2) && String.IsNullOrEmpty(CatalogItem2)) return true;
+ if (CatalogName1 == CatalogName2 && CatalogItem1 == CatalogItem2) return true;
+ return false;
+ }
+
+ internal bool IsLab2NullOrEqual()
+ {
+ if (L2 == null && A2 == null && B2 == null) return true;
+ if (L1 == L2 && A1 == A2 && B1 == B2) return true;
+ return false;
+ }
+ }
+
+ static public Task<List<Segment>> FromFile(String filePath, Machine machine, ObservablesContext context)
+ {
+ return Task.Factory.StartNew(() =>
+ {
+ List<SegmentCsvModel> rows = CsvFile.Read<SegmentCsvModel>(new CsvSource(filePath)).ToList();
+
+ List<Segment> segments = new List<Segment>();
+
+ var catalogs = new CatalogsCollectionBuilder(context).SetAll().WithGroups().WithItems().BuildList();
+ var colorSpaces = context.ColorSpaces.ToList();
+
+ if (machine.Configuration == null || machine.Configuration.IdsPacks == null || machine.Configuration.IdsPacks.Count == 0)
+ {
+ machine = new MachineBuilder(context).Set(machine.Guid).WithConfiguration().Build();
+ }
+
+ var idsPacks = machine.Configuration.NoneEmptyIdsPacks.ToList();
+
+ var cyanIdsPack = idsPacks.FirstOrDefault(x => x.LiquidType.Type == LiquidTypes.Cyan);
+ var magentaIdsPack = idsPacks.FirstOrDefault(x => x.LiquidType.Type == LiquidTypes.Magenta);
+ var yellowIdsPack = idsPacks.FirstOrDefault(x => x.LiquidType.Type == LiquidTypes.Yellow);
+ var blackIdsPack = idsPacks.FirstOrDefault(x => x.LiquidType.Type == LiquidTypes.Black);
+
+ foreach (var row in rows)
+ {
+ Segment segment = new Segment();
+ segment.Length = double.Parse(row.Length);
+ segment.SegmentIndex = int.Parse(row.Index);
+
+ ColorSpace colorSpace = colorSpaces.SingleOrDefault(x => x.Name == row.ColorSpace);
+
+ if (colorSpace == null) throw new InvalidOperationException($"Color space '{row.ColorSpace}' not found!");
+
+ BrushStop stop1 = new BrushStop();
+ stop1.ColorSpace = colorSpace;
+ stop1.OffsetPercent = 0;
+ segment.BrushStops.Add(stop1);
+
+ if (colorSpace.Space == ColorSpaces.RGB)
+ {
+ stop1.Red = int.Parse(row.Red1);
+ if (stop1.Red < 0 || stop1.Red > 255) throw new InvalidOperationException($"Value red1 '{row.Red1}' is out of bounds!");
+ stop1.Green = int.Parse(row.Green1);
+ if (stop1.Green < 0 || stop1.Green > 255) throw new InvalidOperationException($"Value green1 '{row.Green1}' is out of bounds!");
+ stop1.Blue = int.Parse(row.Blue1);
+ if (stop1.Blue < 0 || stop1.Blue > 255) throw new InvalidOperationException($"Value blue1 '{row.Blue1}' is out of bounds!");
+
+ if (!row.IsRgb2NullOrEqual())
+ {
+ BrushStop stop2 = new BrushStop();
+ stop2.ColorSpace = stop1.ColorSpace;
+ stop2.OffsetPercent = 100;
+
+ stop2.Red = int.Parse(row.Red2);
+ if (stop2.Red < 0 || stop2.Red > 255) throw new InvalidOperationException($"Value red2 '{row.Red2}' is out of bounds!");
+ stop2.Green = int.Parse(row.Green2);
+ if (stop2.Green < 0 || stop2.Green > 255) throw new InvalidOperationException($"Value green2 '{row.Green2}' is out of bounds!");
+ stop2.Blue = int.Parse(row.Blue2);
+ if (stop2.Blue < 0 || stop2.Blue > 255) throw new InvalidOperationException($"Value blue2 '{row.Blue2}' is out of bounds!");
+
+ segment.BrushStops.Add(stop2);
+ }
+ }
+ else if (colorSpace.Space == ColorSpaces.Volume)
+ {
+ double cyan1 = double.Parse(row.Cyan1);
+ stop1.SetVolume(cyanIdsPack.PackIndex, cyan1);
+
+ double magenta1 = double.Parse(row.Magenta1);
+ stop1.SetVolume(magentaIdsPack.PackIndex, magenta1);
+
+ double yellow1 = double.Parse(row.Yellow1);
+ stop1.SetVolume(yellowIdsPack.PackIndex, yellow1);
+
+ double black1 = double.Parse(row.Black1);
+ stop1.SetVolume(cyanIdsPack.PackIndex, cyan1);
+
+ if (!row.IsVolumeNullOrEqual())
+ {
+ BrushStop stop2 = new BrushStop();
+ stop2.ColorSpace = stop1.ColorSpace;
+ stop2.OffsetPercent = 100;
+
+ double cyan2 = double.Parse(row.Cyan2);
+ stop2.SetVolume(cyanIdsPack.PackIndex, cyan2);
+
+ double magenta2 = double.Parse(row.Magenta2);
+ stop2.SetVolume(magentaIdsPack.PackIndex, magenta2);
+
+ double yellow2 = double.Parse(row.Yellow2);
+ stop2.SetVolume(yellowIdsPack.PackIndex, yellow2);
+
+ double black2 = double.Parse(row.Black2);
+ stop2.SetVolume(cyanIdsPack.PackIndex, cyan2);
+
+ segment.BrushStops.Add(stop2);
+ }
+ }
+ else if (colorSpace.Space == ColorSpaces.Catalog)
+ {
+ var catalog = catalogs.FirstOrDefault(x => x.Name == row.CatalogName1);
+ if(catalog == null)
+ {
+ throw new InvalidOperationException($"Catalog name: '{row.CatalogName1}' not found!");
+ }
+
+ var item = catalog.ColorCatalogsGroups.SelectMany(x => x.ColorCatalogsItems).FirstOrDefault(x => x.Name == row.CatalogItem1);
+ if(item == null)
+ {
+ throw new InvalidOperationException($"Catalog item: '{row.CatalogItem1}' not found in ColorCatalogsGroups!");
+ }
+
+ stop1.ColorCatalog = catalog;
+ stop1.ColorCatalogsItem = item;
+ if(!row.IsCatalogNameNullOrEqual())
+ {
+ var catalog2 = catalogs.FirstOrDefault(x => x.Name == row.CatalogName2);
+ if (catalog == null)
+ {
+ throw new InvalidOperationException($"Catalog name: '{row.CatalogName2}' not found!");
+ }
+
+ var item2 = catalog.ColorCatalogsGroups.SelectMany(x => x.ColorCatalogsItems).FirstOrDefault(x => x.Name == row.CatalogItem2);
+ if (item == null)
+ {
+ throw new InvalidOperationException($"Catalog item: '{row.CatalogItem2}' not found in ColorCatalogsGroups!");
+ }
+ BrushStop stop2 = new BrushStop();
+ stop2.ColorCatalog = catalog2;
+ stop2.ColorCatalogsItem = item2;
+ segment.BrushStops.Add(stop2);
+ }
+ }
+ else if(colorSpace.Space == ColorSpaces.LAB)
+ {
+ stop1.L = int.Parse(row.L1);
+ if (stop1.L < 0 || stop1.L > 100) throw new InvalidOperationException($"Value L1 '{row.L1}' is out of bounds!");
+ stop1.A = int.Parse(row.A1);
+ if (stop1.A < -128 || stop1.A > 128) throw new InvalidOperationException($"Value A1 '{row.A1}' is out of bounds!");
+ stop1.B = int.Parse(row.B1);
+ if (stop1.B < -128 || stop1.B > 128) throw new InvalidOperationException($"Value B1 '{row.B1}' is out of bounds!");
+ if (!row.IsLab2NullOrEqual())
+ {
+ BrushStop stop2 = new BrushStop();
+ stop2.ColorSpace = stop1.ColorSpace;
+ stop2.OffsetPercent = 100;
+
+ stop2.L = int.Parse(row.L2);
+ if (stop2.L < 0 || stop2.L > 100) throw new InvalidOperationException($"Value L2 '{row.L2}' is out of bounds!");
+ stop2.A = int.Parse(row.A2);
+ if (stop2.A < -128 || stop2.A > 128) throw new InvalidOperationException($"Value A2 '{row.A2}' is out of bounds!");
+ stop2.B = int.Parse(row.B2);
+ if (stop2.B < -128 || stop2.B > 128) throw new InvalidOperationException($"Value B2 '{row.B2}' is out of bounds!");
+
+ segment.BrushStops.Add(stop2);
+ }
+ }
+ }
+
+ return segments;
+ });
+ }
+
+ static async void ToFile(String filePath, List<Segment> segments)
+ {
+ await Task.Factory.StartNew(() =>
+ {
+ SegmentCsvModel model = new SegmentCsvModel();
+ List<string> columnNames = model.GetType().GetProperties().Select(x => x.Name).ToList();
+ CsvFile<SegmentCsvModel> csvFile = new CsvFile<SegmentCsvModel>(new CsvDestination(filePath), new CsvDefinition() {
+ Columns = columnNames
+ });
+ foreach(var segment in segments)
+ {
+ SegmentCsvModel csvmodel = new SegmentCsvModel();
+ csvmodel.Index = segment.SegmentIndex.ToString();
+ csvmodel.Length = segment.Length.ToString();
+ if(segment.BrushStops.Count > 2 ) throw new InvalidOperationException($"Cannot save more than two brush stops!");
+ //save first brush stop
+ if (segment.BrushStops.Count > 0)
+ {
+ var brushStop1 = segment.BrushStops[0];
+ csvmodel.ColorSpace = brushStop1.ColorSpace.Name;
+ if (csvmodel.ColorSpace == ColorSpaces.RGB.ToDescription())
+ {
+ csvmodel.Red1 = brushStop1.Red.ToString();
+ csvmodel.Green1 = brushStop1.Green.ToString();
+ csvmodel.Blue1 = brushStop1.Blue.ToString();
+ }
+ else if (csvmodel.ColorSpace == ColorSpaces.Volume.ToDescription())
+ {
+ csvmodel.Cyan1 = brushStop1.GetVolume(LiquidTypes.Cyan).ToString();
+ csvmodel.Magenta1 = brushStop1.GetVolume(LiquidTypes.Magenta).ToString();
+ csvmodel.Yellow1 = brushStop1.GetVolume(LiquidTypes.Yellow).ToString();
+ csvmodel.Black1 = brushStop1.GetVolume(LiquidTypes.Black).ToString();
+ }
+ else if (csvmodel.ColorSpace == ColorSpaces.Catalog.ToDescription())
+ {
+ csvmodel.CatalogName1 = brushStop1.ColorCatalog.Name;
+ csvmodel.CatalogItem1 = brushStop1.ColorCatalogsItem.Name;
+ }
+ else if (csvmodel.ColorSpace == ColorSpaces.LAB.ToDescription())
+ {
+ csvmodel.L1 = brushStop1.L.ToString();
+ csvmodel.A1 = brushStop1.A.ToString();
+ csvmodel.B1 = brushStop1.B.ToString();
+ }
+ }
+ if (segment.BrushStops.Count > 1)
+ {
+ var brushStop2 = segment.BrushStops[1];
+ if (csvmodel.ColorSpace == ColorSpaces.RGB.ToDescription())
+ {
+ csvmodel.Red2 = brushStop2.Red.ToString();
+ csvmodel.Green2 = brushStop2.Green.ToString();
+ csvmodel.Blue2 = brushStop2.Blue.ToString();
+ }
+ else if (csvmodel.ColorSpace == ColorSpaces.Volume.ToDescription())
+ {
+ csvmodel.Cyan2 = brushStop2.GetVolume(LiquidTypes.Cyan).ToString();
+ csvmodel.Magenta2 = brushStop2.GetVolume(LiquidTypes.Magenta).ToString();
+ csvmodel.Yellow2 = brushStop2.GetVolume(LiquidTypes.Yellow).ToString();
+ csvmodel.Black2 = brushStop2.GetVolume(LiquidTypes.Black).ToString();
+ }
+ else if (csvmodel.ColorSpace == ColorSpaces.Catalog.ToDescription())
+ {
+ csvmodel.CatalogName2 = brushStop2.ColorCatalog.Name;
+ csvmodel.CatalogItem2 = brushStop2.ColorCatalogsItem.Name;
+ }
+ else if (csvmodel.ColorSpace == ColorSpaces.LAB.ToDescription())
+ {
+ csvmodel.L2 = brushStop2.L.ToString();
+ csvmodel.A2 = brushStop2.A.ToString();
+ csvmodel.B2 = brushStop2.B.ToString();
+ }
+ }
+
+ csvFile.Append(csvmodel);
+ }
+ });
+ }
+ }
+}