프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / Util / SPPIDUtil.cs @ 7c3cdd0b

이력 | 보기 | 이력해설 | 다운로드 (9.99 KB)

1 23eb98bf gaqhf
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using Newtonsoft.Json;
7
using System.IO;
8 b8e2644e gaqhf
using Converter.SPPID.DB;
9 23eb98bf gaqhf
using Converter.BaseModel;
10
using System.Windows.Forms;
11 bca81f4c gaqhf
using Converter.SPPID.Model;
12 5dfb8a24 gaqhf
using System.Drawing;
13 23eb98bf gaqhf
14 b8e2644e gaqhf
namespace Converter.SPPID.Util
15 23eb98bf gaqhf
{
16 5dfb8a24 gaqhf
    public enum SlopeType
17
    {
18
        Slope,
19
        HORIZONTAL,
20
        VERTICAL
21
    }
22 23eb98bf gaqhf
    public class SPPIDUtil
23
    {
24 fab4f207 gaqhf
        public static bool ConvertToSPPIDInfo(string jsonString)
25 23eb98bf gaqhf
        {
26 2e1e3c12 gaqhf
            SPPID_DBInfo _SPPIDInfo = SPPID_DBInfo.GetInstance();
27 23eb98bf gaqhf
            try
28
            {
29 fab4f207 gaqhf
                SPPID_DBInfo jsonSPPIDInfo = JsonConvert.DeserializeObject<SPPID_DBInfo>(jsonString);
30 23eb98bf gaqhf
31
                _SPPIDInfo.DBType = jsonSPPIDInfo.DBType;
32
                _SPPIDInfo.Service = jsonSPPIDInfo.Service;
33
                _SPPIDInfo.Site = jsonSPPIDInfo.Site;
34
                _SPPIDInfo.ServerIP = jsonSPPIDInfo.ServerIP;
35
                _SPPIDInfo.Port = jsonSPPIDInfo.Port;
36
                _SPPIDInfo.DBUser = jsonSPPIDInfo.DBUser;
37
                _SPPIDInfo.DBPassword = jsonSPPIDInfo.DBPassword;
38
                _SPPIDInfo.PlantPath = jsonSPPIDInfo.PlantPath;
39
                _SPPIDInfo.PlantDic = jsonSPPIDInfo.PlantDic;
40
                _SPPIDInfo.PlantPID = jsonSPPIDInfo.PlantPID;
41
                _SPPIDInfo.PlantPIDDic = jsonSPPIDInfo.PlantPIDDic;
42
                _SPPIDInfo.Plant = jsonSPPIDInfo.Plant;
43
                _SPPIDInfo.Enable = jsonSPPIDInfo.Enable;
44
                _SPPIDInfo.SelectedPlant = jsonSPPIDInfo.SelectedPlant;
45
                _SPPIDInfo.PlantList = jsonSPPIDInfo.PlantList;
46
47
            }
48
            catch (Exception ex)
49
            {
50 fab4f207 gaqhf
                _SPPIDInfo.Enable = false;
51 23eb98bf gaqhf
                return false;
52
            }
53
            return true;
54
        }
55 5dfb8a24 gaqhf
56 e00e891d gaqhf
        public static bool ConvertToETCSetting(string jsonString)
57
        {
58
            ETCSetting _ETCSetting = ETCSetting.GetInstance();
59
            try
60
            {
61
                ETCSetting jsonETCSetting = JsonConvert.DeserializeObject<ETCSetting>(jsonString);
62
63
                _ETCSetting.NoteSymbolPath = jsonETCSetting.NoteSymbolPath;
64
                _ETCSetting.TextSymbolPath = jsonETCSetting.TextSymbolPath;
65
                _ETCSetting.DrainValveSize = jsonETCSetting.DrainValveSize;
66 1a3a74a8 gaqhf
                _ETCSetting.TextLocation = jsonETCSetting.TextLocation;
67
                _ETCSetting.NoteLocation = jsonETCSetting.NoteLocation;
68
                _ETCSetting.LineNumberLocation = jsonETCSetting.LineNumberLocation;
69 e00e891d gaqhf
70
            }
71
            catch (Exception ex)
72
            {
73
                return false;
74
            }
75
            return true;
76
        }
77 bca81f4c gaqhf
78 7cbb1038 gaqhf
        public static bool ConvertToGridSetting(string jsonString)
79
        {
80
            GridSetting _GridSetting = GridSetting.GetInstance();
81
            try
82
            {
83
                GridSetting jsonGridSetting = JsonConvert.DeserializeObject<GridSetting>(jsonString);
84
85
                _GridSetting.UseSnapGrid = jsonGridSetting.UseSnapGrid;
86
                _GridSetting.Density = jsonGridSetting.Density;
87
                _GridSetting.Unit = jsonGridSetting.Unit;
88
            }
89
            catch (Exception ex)
90
            {
91
                return false;
92
            }
93
            return true;
94
        }
95
96 39a2a688 gaqhf
        public static bool ConvertPointBystring(string sPoint, ref double dX, ref double dY)
97
        {
98
            try
99
            {
100
                string[] pointArr = sPoint.Split(new char[] { ',' });
101
                if (pointArr.Length == 2)
102
                {
103
                    dX = Convert.ToDouble(pointArr[0]);
104
                    dY = Convert.ToDouble(pointArr[1]);
105
                }
106
            }
107
            catch (Exception)
108
            {
109
                dX = 0;
110
                dY = 0;
111
                return false;
112
            }
113
114
            return true;
115
        }
116 bca81f4c gaqhf
117 39a2a688 gaqhf
        public static void ConvertSPPIDPoint(ref double dX, ref double dY, double dDwgX, double dDwgY, double SPPID_Width, double SPPID_Height)
118
        {
119 1b261371 gaqhf
            decimal calcX = 0;
120
            decimal calcY = 0;
121
            decimal tempX = Convert.ToDecimal(dX);
122
            decimal tempY = Convert.ToDecimal(dY);
123
            decimal tempWidth = Convert.ToDecimal(SPPID_Width);
124
            decimal tempHeight = Convert.ToDecimal(SPPID_Height);
125
            decimal tempDwgX = Convert.ToDecimal(dDwgX);
126
            decimal tempDwgY = Convert.ToDecimal(dDwgY);
127
128 c3d2e266 gaqhf
            //calcX = (tempX * tempWidth) / tempDwgX;
129
            //calcX = Math.Truncate(calcX * 1000) / 1000;
130
            //calcY = tempHeight - ((tempY * tempHeight) / tempDwgY);
131
            //calcY = Math.Truncate(calcY * 1000) / 1000;
132
            //dX = Math.Round(Convert.ToDouble(calcX), 10);
133
            //dY = Math.Round(Convert.ToDouble(calcY), 10);
134
135 1b261371 gaqhf
            calcX = (tempX * tempWidth) / tempDwgX;
136
            calcY = tempHeight - ((tempY * tempHeight) / tempDwgY);
137 c3d2e266 gaqhf
            dX = Convert.ToDouble(calcX);
138
            dY = Convert.ToDouble(calcY);
139 39a2a688 gaqhf
        }
140 bca81f4c gaqhf
141 06b40010 gaqhf
        public static void ConvertGridPoint(ref double x, ref double y)
142
        {
143
            GridSetting _GridSetting = GridSetting.GetInstance();
144
            if (_GridSetting.UseSnapGrid)
145
            {
146
                if (_GridSetting.Unit == GridUnit.Inch)
147
                {
148
                    double length = _GridSetting.Density * 0.0254;
149
                    double tempX = x;
150
                    double tempY = y;
151
                    x = Math.Round(tempX / length) * length;
152
                    y = Math.Round(tempY / length) * length;
153
                }
154
            }
155
        }
156
157 5dfb8a24 gaqhf
        public static SlopeType CalcSlope(double x1, double y1, double x2, double y2)
158
        {
159
            if (x1 - x2 == 0)
160
            {
161
                return SlopeType.VERTICAL;
162
            }
163
            else
164
            {
165
                double angle = Math.Atan(Math.Abs(y2 - y1) / Math.Abs(x2 - x1)) * 180 / Math.PI;
166 c3d2e266 gaqhf
                if (angle <= 2)
167 5dfb8a24 gaqhf
                    return SlopeType.HORIZONTAL;
168 c3d2e266 gaqhf
                else if (angle >= 88)
169 5dfb8a24 gaqhf
                    return SlopeType.VERTICAL;
170
                else
171
                    return SlopeType.Slope;
172
            }
173
        }
174
175 5e6ecf05 gaqhf
        public static double CalcLineToPointDistance(double lineX1, double lineY1, double lineX2, double lineY2, double x, double y)
176
        {
177
178
            double distance = 0;
179
            if (lineX1 == lineX2)
180
                distance = Math.Abs(x - lineX1);
181
            else
182
            {
183
                double a;
184
                double b;
185
                double c;
186
187
                a = (lineY2 - lineY1) / (lineX2 - lineX1);
188
                b = -1;
189
                c = -a * lineX1 + lineY1;
190
191
                distance = Math.Abs(a * x + b * y + c) / Math.Pow(a * a + b * b, 0.5);
192
            }
193
            return distance;
194
        }
195
196 56bc67e1 gaqhf
        public static double[] CalcLineCrossingPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
197
        {
198
            x1 = Math.Round(x1, 10);
199
            x2 = Math.Round(x2, 10);
200
            x3 = Math.Round(x3, 10);
201
            x4 = Math.Round(x4, 10);
202
203
            y1 = Math.Round(y1, 10);
204
            y2 = Math.Round(y2, 10);
205
            y3 = Math.Round(y3, 10);
206
            y4 = Math.Round(y4, 10);
207
208
            double a1;
209
            double a2;
210
            double b1;
211
            double b2;
212
213
            if (x1 == x2)
214
                a1 = 0;
215
            else
216
                a1 = (y2 - y1) / (x2 - x1);
217
            if (x3 == x4)
218
                a2 = 0;
219
            else
220
                a2 = (y4 - y3) / (x4 - x3);
221
222
            b1 = -a1 * x1 + y1;
223
            b2 = -a2 * x3 + y3;
224
225
            if ((x1 == x2 && x3 == x4) ||
226
                (y1 == y2 && y3 == y4))
227
            {
228
                return null;
229
            }
230
            else if (x1 == x2)
231
            {
232
                return new double[] { x1, a2*x1 + b2 };
233
            }
234
            else if (x3 == x4)
235
            {
236
                return new double[] { x3, a1 * x3 + b1 };
237
            }
238
            else
239
            {
240
                return new double[] { -(b1 - b2) / (a1 - a2), a1 * (-(b1 - b2) / (a1 - a2) + b1) };
241
            }
242
        }
243 30a9ffce gaqhf
        public static double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
244
        {
245
            return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
246
        }
247
248 bca81f4c gaqhf
        #region
249
        public static bool IsBranchLine(string UID, Line connectedLine)
250
        {
251
            try
252
            {
253
                if (connectedLine.CONNECTORS[0].CONNECTEDITEM != UID && connectedLine.CONNECTORS[1].CONNECTEDITEM != UID)
254
                    return true;
255
            }
256
            catch (Exception ex)
257
            {
258
                
259
            }
260
261
            return false;
262
        }
263
        public static object FindObjectByUID(Document document, string UID)
264
        {
265
            foreach (Symbol item in document.SYMBOLS)
266
            {
267
                if (item.UID == UID)
268
                    return item;
269
            }
270
271
            foreach (Line item in document.LINES)
272
            {
273
                if (item.UID == UID)
274
                    return item;
275
            }
276
277
            foreach (Text item in document.TEXTINFOS)
278
            {
279
                if (item.UID == UID)
280
                    return item;
281
            }
282
283
            foreach (Note item in document.NOTES)
284
            {
285
                if (item.UID == UID)
286
                    return item;
287
            }
288
289
            foreach (LineNumber item in document.LINENUMBERS)
290
            {
291
                if (item.UID == UID)
292
                    return item;
293
            }
294
295 809a7640 gaqhf
            foreach (Equipment item in document.Equipments)
296
            {
297
                if (item.UID == UID)
298
                    return item;
299
            }
300
301 bca81f4c gaqhf
            return null;
302
        }
303 335b7a24 gaqhf
        public static List<Line> FindLinesByModelId(Document document, string ModelItemId)
304
        {
305
            List<Line> lines = new List<Line>();
306
            foreach (Line item in document.LINES)
307
            {
308
                if (item.SPPID.ModelItemId == ModelItemId)
309
                    lines.Add(item);
310
            }
311
312
            return lines;
313
        }
314 bca81f4c gaqhf
        #endregion
315 23eb98bf gaqhf
    }
316
}