aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/Helpers/SegmentsCsvHelper.cs
blob: ebc466bb796a4f5ae206b6b9abfc8d51a5aa85a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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<Segment> Segments { get; set; }

            public SegmentsCsvResult()
            {
                Segments = new List<Segment>();
            }
        }

        public static Task<SegmentsCsvResult> FromFile(String filePath, Machine machine, ObservablesContext context)
        {
            return Task.Factory.StartNew(() =>
            {
                var csv = new CsvDynamicReader(filePath);

                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();
                }

                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<Segment> 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);
            });
        }
    }
}