aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/Entities/Rml.cs
blob: 679226fd2c6a7a52cff8fad936ce054ef4f9e90e (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
using ColorMine.ColorSpaces;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using Tango.BL.Builders;
using Tango.BL.Enumerations;
using Tango.BL.Serialization;

namespace Tango.BL.Entities
{
    public partial class Rml : RmlBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Rml" /> class.
        /// </summary>
        public Rml() : base()
        {
            WhitePointL = 92.1815;
            WhitePointA = 2.2555;
            WhitePointB = -10.9325;
        }

        protected override void OnWhitePointLChanged(double whitepointl)
        {
            base.OnWhitePointLChanged(whitepointl);
            UpdateColor();
        }

        protected override void OnWhitePointAChanged(double whitepointa)
        {
            base.OnWhitePointAChanged(whitepointa);
            UpdateColor();
        }

        protected override void OnWhitePointBChanged(double whitepointb)
        {
            base.OnWhitePointBChanged(whitepointb);
            UpdateColor();
        }

        private void UpdateColor()
        {
            RaisePropertyChanged(nameof(Color));
        }

        [NotMapped]
        [JsonIgnore]
        public Color Color
        {
            get
            {
                Lab lab = new Lab(WhitePointA, WhitePointA, WhitePointB);
                Rgb rgb = lab.To<Rgb>();
                return Color.FromRgb((byte)rgb.R, (byte)rgb.G, (byte)rgb.B);
            }
        }

        /// <summary>
        /// Gets or sets the thread head type.
        /// </summary>
        [NotMapped]
        [JsonIgnore]
        public HeadTypes RmlHeadType
        {
            get
            {
                return (HeadTypes)HeadType;
            }
            set
            {
                HeadType = value.ToInt32();
            }
        }

        /// <summary>
        /// Gets or sets the thread qualification level.
        /// </summary>
        [NotMapped]
        [JsonIgnore]
        public RmlQualifications RmlQualificationLevel
        {
            get
            {
                return (RmlQualifications)QualificationLevel;
            }
            set
            {
                QualificationLevel = value.ToInt32();
            }
        }

        public ProcessParametersTablesGroup GetActiveProcessGroup()
        {
            return ProcessParametersTablesGroups.FirstOrDefault(x => x.Active);
        }

        public Task<String> ToRmlFile(ObservablesContext context)
        {
            return Task.Factory.StartNew<String>(() =>
            {
                var rml = new RmlBuilder(context).Set(Guid).WithActiveParametersGroup().WithCCT().WithLiquidFactors().WithSpools().Build();

                String result = rml.ToJson(new EntitySerializationStrategy()
                    .Include(() => rml.Cct)
                    .Include(() => rml.LiquidTypesRmls)
                    .Include(() => rml.ProcessParametersTablesGroups)
                    .Include(() => rml.RmlsSpools)
                    .Include(typeof(ProcessParametersTablesGroup).GetProperty(nameof(ProcessParametersTablesGroup.ProcessParametersTables))),
                    EntitySerializationFlags.IgnoreCollections | EntitySerializationFlags.IgnoreReferenceTypes | EntitySerializationFlags.Indented);

                return result;
            });
        }

        public static Task<Rml> FromRmlFile(ObservablesContext context, String json)
        {
            return Task.Factory.StartNew<Rml>(() =>
            {
                Rml rml = Rml.FromJson(json,new EntitySerializationStrategy(),EntitySerializationFlags.Indented);

                if (context.Rmls.Any(x => x.Guid == rml.Guid || x.Name == rml.Name))
                {
                    throw new InvalidOperationException($"Thread {rml.Name} already exist.");
                }

                if (rml.Cct != null)
                {
                    if (context.Ccts.Any(x => x.Guid == rml.Cct.Guid))
                    {
                        rml.Cct = null;
                    }
                }

                rml.LastUpdated = DateTime.UtcNow;
                rml.Code = context.Rmls.Max(x => x.Code) + 1;

                return rml;
            });
        }
    }
}