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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
|
using ColorMine.ColorSpaces;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using Tango.Core;
using Tango.BL;
using Tango.BL.Enumerations;
using Tango.PMR.ColorLab;
using Google.Protobuf;
using System.Runtime.InteropServices;
using Tango.PMR;
using System.Diagnostics;
using System.Windows.Threading;
using System.Timers;
using Tango.Core.Threading;
namespace Tango.BL.Entities
{
public partial class BrushStop : BrushStopBase
{
private bool _ignorePropChanged;
private ActionTimer _syncTimer;
private static List<String> _colorPropertyNames;
public const double MAX_TOTAL_LIQUID_VOLUME = 200;
#region Enums
/// <summary>
/// Represents brush stops color space synchronization mode.
/// </summary>
public enum ColorSynchronizationModes
{
/// <summary>
/// No synchronization will be performed.
/// </summary>
None,
/// <summary>
/// Synchronizes changes immediately.
/// </summary>
Immediate,
/// <summary>
/// Throttles changes within 10 milliseconds.
/// </summary>
Throttle,
}
#endregion
#region Constructors
/// <summary>
/// Initializes the <see cref="BrushStop"/> class.
/// </summary>
static BrushStop()
{
_colorPropertyNames = new List<string>();
_colorPropertyNames.Add(nameof(Red));
_colorPropertyNames.Add(nameof(Green));
_colorPropertyNames.Add(nameof(Blue));
_colorPropertyNames.Add(nameof(L));
_colorPropertyNames.Add(nameof(A));
_colorPropertyNames.Add(nameof(B));
_colorPropertyNames.Add(nameof(Cyan));
_colorPropertyNames.Add(nameof(Magenta));
_colorPropertyNames.Add(nameof(Yellow));
_colorPropertyNames.Add(nameof(Black));
_colorPropertyNames.Add(nameof(Color));
_colorPropertyNames.Add(nameof(ColorCatalogsItem));
}
/// <summary>
/// Initializes a new instance of the <see cref="BrushStop" /> class.
/// </summary>
public BrushStop() : base()
{
_syncTimer = new ActionTimer(TimeSpan.FromMilliseconds(10));
}
#endregion
#region Static Properties
/// <summary>
/// Gets or sets the color synchronization mode for all brush stops.
/// </summary>
public static ColorSynchronizationModes ColorSynchronizationMode { get; set; } = ColorSynchronizationModes.Throttle;
#endregion
#region Properties
/// <summary>
/// Gets or sets a value indicating whether the total value of liquid volumes has exceeded the maximum range of <see cref="MAX_TOTAL_LIQUID_VOLUME"/>.
/// </summary>
[NotMapped]
[JsonIgnore]
public bool IsLiquidVolumesOutOfRange
{
get
{
return LiquidVolumes != null ? LiquidVolumes.Where(x => x.IdsPack.IdsPackFormula.Code == IdsPackFormulas.StandardColor.ToInt32()).Sum(x => x.Volume) > MAX_TOTAL_LIQUID_VOLUME : false;
}
}
private ObservableCollection<LiquidVolume> _liquidVolumes;
/// <summary>
/// Gets or sets the collection of this liquid volumes.
/// </summary>
[NotMapped]
[JsonIgnore]
public ObservableCollection<LiquidVolume> LiquidVolumes
{
get { return _liquidVolumes; }
set
{
_liquidVolumes = value;
RaisePropertyChangedAuto();
RaisePropertyChanged(nameof(LiquidVolumesOrdered));
RaisePropertyChanged(nameof(LiquidVolumesOrderedPigmented));
}
}
/// <summary>
/// Gets the collection of liquid volumes ordered by their liquid type preferred index.
/// </summary>
[NotMapped]
[JsonIgnore]
public ObservableCollection<LiquidVolume> LiquidVolumesOrdered
{
get
{
return LiquidVolumes != null ? LiquidVolumes.OrderBy(x => x.IdsPack.LiquidType.PreferredIndex).ToObservableCollection() : null;
}
}
/// <summary>
/// Gets the collection of liquid volumes with pigmented (color) liquid types ordered by their liquid type preferred index.
/// </summary>
[NotMapped]
[JsonIgnore]
public ObservableCollection<LiquidVolume> LiquidVolumesOrderedPigmented
{
get { return LiquidVolumes != null ? LiquidVolumes.Where(x => x.IdsPack.LiquidType.HasPigment).OrderBy(x => x.IdsPack.LiquidType.PreferredIndex).ToObservableCollection() : null; }
}
/// <summary>
/// Gets or sets the brush stop color.
/// </summary>
[NotMapped]
[JsonIgnore]
public Color Color
{
get { return Color.FromRgb((byte)_red, (byte)_green, (byte)_blue); }
set
{
Rgb rgb = new Rgb(value.R, value.G, value.B);
_red = (byte)rgb.R;
_green = (byte)rgb.G;
_blue = (byte)rgb.B;
Cmyk cmyk = rgb.To<Cmyk>();
_cyan = cmyk.C;
_magenta = cmyk.M;
_yellow = cmyk.Y;
_black = cmyk.K;
Lab lab = rgb.To<Lab>();
_l = lab.L;
_a = lab.A;
_b = lab.B;
RaisePropertyChanged(nameof(Color));
}
}
/// <summary>
/// Returns a solid brush from <see cref="Color"/>.
/// </summary>
[NotMapped]
[JsonIgnore]
public SolidColorBrush Brush
{
get { return new SolidColorBrush(Color); }
}
/// <summary>
/// Gets a value indicating whether this brush stop is the first one within its segment brush stops.
/// </summary>
[NotMapped]
[JsonIgnore]
public bool IsFirst
{
get
{
if (Segment != null && Segment.BrushStops.Count > 0)
{
return StopIndex == Segment.BrushStops.Min(x => x.StopIndex);
}
else
{
return true;
}
}
}
/// <summary>
/// Gets a value indicating whether this brush stop is the last one within its segment brush stops.
/// </summary>
[NotMapped]
[JsonIgnore]
public bool IsLast
{
get
{
if (Segment != null && Segment.BrushStops.Count > 0)
{
return StopIndex == Segment.BrushStops.Max(x => x.StopIndex);
}
else
{
return true;
}
}
}
/// <summary>
/// Gets a value indicating whether this brush stop is not the first nor last within its segment brush stops.
/// </summary>
[NotMapped]
[JsonIgnore]
public bool IsMiddle
{
get { return !IsFirst && !IsLast; }
}
/// <summary>
/// Gets this brush stop offset in meters.
/// </summary>
[NotMapped]
[JsonIgnore]
public double OffsetMeters
{
get
{
if (Segment != null)
{
var a = Segment.Length * (OffsetPercent / 100d);
return a;
}
else
{
return 0;
}
}
set
{
if (Segment != null)
{
OffsetPercent = (value / Segment.Length) * 100d;
RaisePropertyChangedAuto();
}
}
}
private bool _isOutOfGamut;
/// <summary>
/// Gets or sets a value indicating whether this instance is out of gamut.
/// </summary>
[NotMapped]
[JsonIgnore]
public bool IsOutOfGamut
{
get { return _isOutOfGamut; }
set
{
if (_isOutOfGamut != value)
{
_isOutOfGamut = value;
RaisePropertyChangedAuto();
if (Segment != null)
{
Segment.RaiseHasOutOfGamutBrushStop();
}
}
}
}
private bool _outOfGamutChecked;
/// <summary>
/// Gets or sets a value indicating whether out of gamut has been checked.
/// </summary>
[NotMapped]
[JsonIgnore]
public bool OutOfGamutChecked
{
get { return _outOfGamutChecked; }
set { _outOfGamutChecked = value; RaisePropertyChangedAuto(); }
}
/// <summary>
/// Gets the total liquid volume.
/// </summary>
[NotMapped]
[JsonIgnore]
public double TotalLiquidVolume
{
get
{
return LiquidVolumes != null ? LiquidVolumes.Where(x => x.IdsPack.IdsPackFormula.Code != IdsPackFormulas.Lubricant.ToInt32() && x.IdsPack.IdsPackFormula.Code != IdsPackFormulas.CleanerLiquid.ToInt32()).Sum(x => x.Volume) : 0;
}
}
/// <summary>
/// Gets the total liquid nanoliter per centimeter.
/// </summary>
[NotMapped]
[JsonIgnore]
public double TotalLiquidNanoliterPerCentimeter
{
get
{
return LiquidVolumes != null ? LiquidVolumes.Where(x => x.IdsPack.IdsPackFormula.Code != IdsPackFormulas.Lubricant.ToInt32() && x.IdsPack.IdsPackFormula.Code != IdsPackFormulas.CleanerLiquid.ToInt32()).Sum(x => x.NanoliterPerCentimeter) : 0;
}
}
/// <summary>
/// Gets the brush color space as enum.
/// </summary>
[NotMapped]
[JsonIgnore]
public ColorSpaces BrushColorSpace
{
get { return (ColorSpaces)ColorSpace.Code; }
}
#endregion
#region Public Methods
/// <summary>
/// Notifies about the offset percentage and offset meters changes.
/// </summary>
public void RaiseOffsetChanged()
{
RaisePropertyChanged(nameof(OffsetPercent));
RaisePropertyChanged(nameof(OffsetMeters));
RaisePropertyChanged(nameof(IsFirst));
RaisePropertyChanged(nameof(IsLast));
RaisePropertyChanged(nameof(IsMiddle));
}
/// <summary>
/// Sets the brush stop index without raising change event.
/// </summary>
/// <param name="index">The index.</param>
public void SetStopIndexNoRaise(int index)
{
_stopindex = index;
}
/// <summary>
/// Raises a change notification for <see cref="BrushStopBase.StopIndex"/>.
/// </summary>
public void RaiseStopIndex()
{
StopIndex = _stopindex;
RaisePropertyChanged(nameof(StopIndex));
}
#endregion
#region Liquid Volumes Methods
/// <summary>
/// Sets this brush stop liquid volumes.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="rml">The RML.</param>
/// <param name="processParametersTable">The process parameters table.</param>
public void SetLiquidVolumes(Configuration configuration, Rml rml, ProcessParametersTable processParametersTable)
{
LiquidVolumes = new ObservableCollection<LiquidVolume>();
foreach (var idsPack in configuration.GetSupportedIdsPacks(rml))
{
var liquidVolume = new LiquidVolume(configuration, idsPack, rml, processParametersTable, this);
liquidVolume.VolumeChanged += LiquidVolume_VolumeChanged;
LiquidVolumes.Add(liquidVolume);
}
foreach (var volume in LiquidVolumes.ToList())
{
volume.Invalidate();
}
RaisePropertyChanged(nameof(LiquidVolumes));
RaisePropertyChanged(nameof(LiquidVolumesOrdered));
RaisePropertyChanged(nameof(LiquidVolumesOrderedPigmented));
}
/// <summary>
/// Gets the liquid volume value by the specified IDS pack index.
/// </summary>
/// <param name="packIndex">Index of the pack.</param>
/// <returns></returns>
public double GetVolume(int packIndex)
{
return (double)typeof(BrushStop).GetProperty("V" + packIndex).GetValue(this);
}
/// <summary>
/// Gets the liquid volume value by the specified IDS pack liquid type.
/// </summary>
/// <param name="liquidType">Type of the liquid.</param>
/// <returns></returns>
public double GetVolume(LiquidTypes liquidType)
{
return GetVolume(Segment.Job.Machine.Configuration.NoneEmptyIdsPacks.SingleOrDefault(x => x.LiquidType.Code == liquidType.ToInt32()).PackIndex);
}
/// <summary>
/// Sets the liquid volume value by the specified IDS pack index.
/// </summary>
/// <param name="packIndex">Index of the pack.</param>
/// <param name="volume">The volume.</param>
public void SetVolume(int packIndex, double volume)
{
typeof(BrushStop).GetProperty("V" + packIndex).SetValue(this, Math.Max(0, volume));
}
/// <summary>
/// Sets the liquid volume value by the specified IDS pack liquid type.
/// </summary>
/// <param name="liquidType">Type of the liquid.</param>
/// <param name="volume">The volume.</param>
public void SetVolume(LiquidTypes liquidType, double volume)
{
SetVolume(Segment.Job.Machine.Configuration.NoneEmptyIdsPacks.SingleOrDefault(x => x.LiquidType.Code == liquidType.ToInt32()).PackIndex, volume);
}
/// <summary>
/// Gets the dispensing step division by the specified IDS pack index.
/// </summary>
/// <param name="packIndex">Index of the pack.</param>
/// <returns></returns>
public int GetDispensingDivision(int packIndex)
{
return (int)typeof(BrushStop).GetProperty("V" + packIndex + "Div").GetValue(this);
}
/// <summary>
/// Sets the dispensing step division by the specified IDS pack index.
/// </summary>
/// <param name="packIndex">Index of the pack.</param>
/// <param name="division">The division.</param>
public void SetDispensingDivision(int packIndex, int division)
{
typeof(BrushStop).GetProperty("V" + packIndex + "Div").SetValue(this, division);
}
/// <summary>
/// Sets all dispensing step divisions.
/// </summary>
/// <param name="division">The division.</param>
public void SetAllDispensingStepDivisions(Dispensing.DispenserStepDivisions division)
{
for (int i = 0; i < 8; i++)
{
typeof(BrushStop).GetProperty("V" + i + "Div").SetValue(this, (int)division);
}
}
#endregion
#region Color Synchronization
/// <summary>
/// Synchronizes between the different brush stop color spaces.
/// </summary>
public void SynchronizeColorSpaces()
{
if (ColorSpace != null)
{
Rgb rgb = new Rgb(Red, Green, Blue);
Cmyk cmyk = new Cmyk(Cyan, Magenta, Yellow, Black);
Lab lab = new Lab(L, A, B);
Rgb rgb_catalog = new Rgb(255, 255, 255);
if (ColorCatalogsItem != null)
{
rgb_catalog = new Rgb(ColorCatalogsItem.Red, ColorCatalogsItem.Green, ColorCatalogsItem.Blue);
}
switch ((ColorSpaces)ColorSpace.Code)
{
case ColorSpaces.RGB:
cmyk = rgb.To<Cmyk>();
lab = rgb.To<Lab>();
break;
case ColorSpaces.CMYK:
rgb = cmyk.To<Rgb>();
lab = cmyk.To<Lab>();
break;
case ColorSpaces.LAB:
rgb = lab.To<Rgb>();
cmyk = lab.To<Cmyk>();
break;
case ColorSpaces.Catalog:
cmyk = rgb_catalog.To<Cmyk>();
lab = rgb_catalog.To<Lab>();
rgb = rgb_catalog;
Validate(null);
break;
}
_red = (int)rgb.R;
_green = (int)rgb.G;
_blue = (int)rgb.B;
_cyan = cmyk.C;
_magenta = cmyk.M;
_yellow = cmyk.Y;
_black = cmyk.K;
_l = lab.L;
_a = lab.A;
_b = lab.B;
}
}
/// <summary>
/// Handles color spaces synchronization when properties changes.
/// </summary>
/// <param name="propName">Name of the property.</param>
private void PerformColorSynchronization(String propName)
{
if (ColorSynchronizationMode != ColorSynchronizationModes.None && !_ignorePropChanged && propName != nameof(ColorSpace) && ColorSpace != null)
{
if (_colorPropertyNames.Contains(propName))
{
if (ColorSynchronizationMode == ColorSynchronizationModes.Throttle)
{
_syncTimer.ResetReplace(() =>
{
SynchronizeColorSpaces();
_ignorePropChanged = true;
foreach (var prop in _colorPropertyNames)
{
RaisePropertyChanged(prop);
}
if (Segment != null)
{
Segment.RaiseSegmentBrushChanged();
}
_ignorePropChanged = false;
});
}
else
{
SynchronizeColorSpaces();
_ignorePropChanged = true;
foreach (var prop in _colorPropertyNames)
{
RaisePropertyChanged(prop);
}
if (Segment != null)
{
Segment.RaiseSegmentBrushChanged();
}
}
}
}
}
#endregion
#region Event Handlers
/// <summary>
/// Handles the liquid volumes <see cref="LiquidVolume.VolumeChanged"/> event.
/// </summary>
private void LiquidVolume_VolumeChanged()
{
RaisePropertyChanged(nameof(IsLiquidVolumesOutOfRange));
RaisePropertyChanged(nameof(TotalLiquidVolume));
RaisePropertyChanged(nameof(TotalLiquidNanoliterPerCentimeter));
}
#endregion
#region Override Methods
/// <summary>
/// Raises the property changed event.
/// </summary>
/// <param name="propName">Name of the property.</param>
protected override void RaisePropertyChanged(string propName)
{
base.RaisePropertyChanged(propName);
PerformColorSynchronization(propName);
}
#endregion
#region Properties Changed
/// <summary>
/// Called when the OffsetPercent has changed.
/// </summary>
/// <param name="offsetpercent"></param>
protected override void OnOffsetPercentChanged(double offsetpercent)
{
base.OnOffsetPercentChanged(offsetpercent);
RaisePropertyChanged(nameof(OffsetMeters));
if (Segment != null)
{
Segment.RaiseSegmentBrushChanged();
}
}
/// <summary>
/// Called when the ColorSpace has changed.
/// </summary>
/// <param name="colorspace"></param>
protected override void OnColorSpaceChanged(ColorSpace colorspace)
{
base.OnColorSpaceChanged(colorspace);
if (ColorSpace != null && ColorSpace.Code == ColorSpaces.Catalog.ToInt32())
{
Corrected = false;
IsOutOfGamut = false;
}
}
#endregion
#region Cloning
/// <summary>
/// Clones this entity.
/// </summary>
/// <returns></returns>
public override BrushStop Clone()
{
BrushStop cloned = base.Clone();
return cloned;
}
/// <summary>
/// Creates a cloned version of this brush stop including all its liquid volumes.
/// </summary>
/// <param name="segment">The segment.</param>
/// <returns></returns>
public BrushStop Clone(Segment segment)
{
BrushStop cloned = base.Clone();
cloned.Segment = segment;
cloned.ColorCatalogsItem = ColorCatalogsItem;
cloned.ColorSpace = ColorSpace;
cloned.SegmentGuid = segment.Guid;
if (LiquidVolumes != null && LiquidVolumes.Count > 0)
{
cloned.LiquidVolumes = LiquidVolumes.Select(x => x.Clone(cloned)).ToObservableCollection();
}
return cloned;
}
#endregion
#region Validation
/// <summary>
/// Called when entity is validating.
/// </summary>
/// <param name="context"></param>
protected override void OnValidating(ObservablesContext context)
{
base.OnValidating(context);
if (BrushColorSpace == ColorSpaces.Catalog && ColorCatalogsItem == null)
{
InsertError(nameof(ColorCatalogsItem), "Please specify a color code.");
}
}
#endregion
}
}
|