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 { private const double MAX_VOLUME = 200; public class SegmentsCsvResult { public Rml Rml { get; set; } public List Segments { get; set; } public SegmentsCsvResult() { Segments = new List(); } } public static Task FromFile(String filePath, Machine machine, ObservablesContext context) { return Task.Factory.StartNew(() => { var csv = new CsvDynamicReader(filePath); List segments = new List(); 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(); } String threadName = null; if (csv.Rows.Count > 0) { if (csv.Rows.First().Exists("ThreadName")) { threadName = csv.Rows.First().Read("ThreadName", String.Empty); } else if (csv.Rows.First().Exists("Thread")) { threadName = csv.Rows.First().Read("Thread", String.Empty); } else if (csv.Rows.First().Exists("RML")) { threadName = csv.Rows.First().Read("RML", String.Empty); } } Rml rml = null; if (threadName.IsNotNullOrEmpty()) { rml = context.Rmls.FirstOrDefault(x => x.Name == threadName || x.DisplayName == threadName); if (rml == null) throw new InvalidOperationException($"Thread type '{threadName}' not found."); } if (machine.SiteGuid != null) { if (!context.SitesRmls.Any(x => x.SiteGuid == machine.SiteGuid && x.RmlGuid == rml.Guid)) { throw new InvalidOperationException($"Thread type '{threadName}' is not allowed for this machine's site."); } } int lineCount = 0; foreach (var row in csv.Rows) { lineCount++; try { //Name Segment segment = new Segment(); segment.Name = "Standard Segment"; //Length and Index segment.Length = row.Read("Length", 100); segment.SegmentIndex = row.Read("Index", lineCount); //Color Space ColorSpaces space = row.Read("ColorSpace", ColorSpaces.Volume); ColorSpace colorSpace = colorSpaces.SingleOrDefault(x => x.Space == space); BrushStop stop = new BrushStop(); stop.StopIndex = 1; stop.ColorSpace = colorSpace; stop.OffsetPercent = 0; stop.Segment = segment; segment.BrushStops.Add(stop); //RGB if (space == ColorSpaces.RGB) { stop.Red = row.Read("RGB R", 0); stop.Green = row.Read("RGB G", 0); stop.Blue = row.Read("RGB B", 0); } //LAB if (space == ColorSpaces.LAB) { stop.L = row.Read("L", 0); stop.A = row.Read("A", 0); stop.B = row.Read("B", 0); } //Volumes if (space == ColorSpaces.Volume) { foreach (var idsPack in machine.Configuration.NoneEmptyIdsPacks.Where(x => x.LiquidType.AvailableForStandardUser)) { var volume = row.Read(idsPack.LiquidType.DisplayName, 0); stop.SetVolume(idsPack.PackIndex, volume); } } //Catalog if (space == ColorSpaces.Catalog) { var catalogName = row.Read("Catalog", String.Empty); var catalogItemName = row.Read("Catalog Item", String.Empty); var catalog = catalogs.FirstOrDefault(x => x.Name == catalogName); if (catalog == null) { throw new InvalidOperationException($"Catalog '{catalogName}' not found on line '{lineCount}'."); } if (machine.SiteGuid != null) { if (!context.SitesCatalogs.Any(x => x.SiteGuid == machine.SiteGuid && x.ColorCatalogGuid == catalog.Guid)) { throw new InvalidOperationException($"Catalog '{catalog.Name}' is not assigned for this machine's site."); } } var item = catalog.ColorCatalogsGroups.SelectMany(x => x.ColorCatalogsItems).FirstOrDefault(x => x.Name == catalogItemName); if (item == null) { throw new InvalidOperationException($"Catalog item '{catalogItemName}' not found on catalog '{catalog.Name}' on line '{lineCount}'."); } stop.ColorCatalog = catalog; stop.ColorCatalogsItem = item; stop.Color = item.Color; } segments.Add(segment); } catch (Exception ex) { throw new InvalidOperationException($"Error parsing file on line {lineCount}.\n{ex.Message}"); } } return new SegmentsCsvResult() { Segments = segments, Rml = rml }; }); } public static async void ToFile(String filePath, List segments) { await Task.Factory.StartNew(() => { CsvDynamicWriter csv = new CsvDynamicWriter(); int cIndex = 0; foreach (var segment in segments) { csv.Write(segment.SegmentIndex, "Index", 0, cIndex++); csv.Write(segment.Length, "Length", 0, cIndex++); //save first brush stop var stop = segment.BrushStops[0]; csv.Write(stop.ColorSpace.Space, "ColorSpace", ColorSpaces.Volume, cIndex++); if (stop.BrushColorSpace == ColorSpaces.RGB) { csv.Write(stop.Red, "RGB R", 0, cIndex++); csv.Write(stop.Green, "RGB G", 0, cIndex++); csv.Write(stop.Blue, "RGB B", 0, cIndex++); } else if (stop.BrushColorSpace == ColorSpaces.LAB) { csv.Write(stop.L, "L", 0, cIndex++); csv.Write(stop.A, "A", 0, cIndex++); csv.Write(stop.B, "B", 0, cIndex++); } else if (stop.BrushColorSpace == ColorSpaces.Catalog) { csv.Write(stop.ColorCatalog.Name, "Catalog", String.Empty, cIndex++); csv.Write(stop.ColorCatalogsItem.Name, "Catalog Item", String.Empty, cIndex++); } else { foreach (var liquidVolume in stop.LiquidVolumesOrderedPigmentedForStandardUser) { csv.Write(liquidVolume.Volume, liquidVolume.IdsPack.LiquidType.DisplayName, 0, cIndex++); } } csv.Next(); } csv.Save(filePath); }); } } }