프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / Util / SPPIDUtil.cs @ 71ba1ca3

이력 | 보기 | 이력해설 | 다운로드 (14.9 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 d1eac84d gaqhf
        None,
19 5dfb8a24 gaqhf
        Slope,
20
        HORIZONTAL,
21
        VERTICAL
22
    }
23 23eb98bf gaqhf
    public class SPPIDUtil
24
    {
25 fab4f207 gaqhf
        public static bool ConvertToSPPIDInfo(string jsonString)
26 23eb98bf gaqhf
        {
27 2e1e3c12 gaqhf
            SPPID_DBInfo _SPPIDInfo = SPPID_DBInfo.GetInstance();
28 23eb98bf gaqhf
            try
29
            {
30 fab4f207 gaqhf
                SPPID_DBInfo jsonSPPIDInfo = JsonConvert.DeserializeObject<SPPID_DBInfo>(jsonString);
31 23eb98bf gaqhf
32
                _SPPIDInfo.DBType = jsonSPPIDInfo.DBType;
33
                _SPPIDInfo.Service = jsonSPPIDInfo.Service;
34
                _SPPIDInfo.Site = jsonSPPIDInfo.Site;
35
                _SPPIDInfo.ServerIP = jsonSPPIDInfo.ServerIP;
36
                _SPPIDInfo.Port = jsonSPPIDInfo.Port;
37
                _SPPIDInfo.DBUser = jsonSPPIDInfo.DBUser;
38
                _SPPIDInfo.DBPassword = jsonSPPIDInfo.DBPassword;
39
                _SPPIDInfo.PlantPath = jsonSPPIDInfo.PlantPath;
40
                _SPPIDInfo.PlantDic = jsonSPPIDInfo.PlantDic;
41
                _SPPIDInfo.PlantPID = jsonSPPIDInfo.PlantPID;
42
                _SPPIDInfo.PlantPIDDic = jsonSPPIDInfo.PlantPIDDic;
43
                _SPPIDInfo.Plant = jsonSPPIDInfo.Plant;
44
                _SPPIDInfo.Enable = jsonSPPIDInfo.Enable;
45
                _SPPIDInfo.SelectedPlant = jsonSPPIDInfo.SelectedPlant;
46
                _SPPIDInfo.PlantList = jsonSPPIDInfo.PlantList;
47
48
            }
49
            catch (Exception ex)
50
            {
51 fab4f207 gaqhf
                _SPPIDInfo.Enable = false;
52 23eb98bf gaqhf
                return false;
53
            }
54
            return true;
55
        }
56 5dfb8a24 gaqhf
57 e00e891d gaqhf
        public static bool ConvertToETCSetting(string jsonString)
58
        {
59
            ETCSetting _ETCSetting = ETCSetting.GetInstance();
60
            try
61
            {
62
                ETCSetting jsonETCSetting = JsonConvert.DeserializeObject<ETCSetting>(jsonString);
63
64
                _ETCSetting.NoteSymbolPath = jsonETCSetting.NoteSymbolPath;
65
                _ETCSetting.TextSymbolPath = jsonETCSetting.TextSymbolPath;
66
                _ETCSetting.DrainValveSize = jsonETCSetting.DrainValveSize;
67 1a3a74a8 gaqhf
                _ETCSetting.TextLocation = jsonETCSetting.TextLocation;
68
                _ETCSetting.NoteLocation = jsonETCSetting.NoteLocation;
69
                _ETCSetting.LineNumberLocation = jsonETCSetting.LineNumberLocation;
70 b2d1c1aa gaqhf
                _ETCSetting.FlowMarkSymbolPath = jsonETCSetting.FlowMarkSymbolPath;
71 e00e891d gaqhf
            }
72
            catch (Exception ex)
73
            {
74
                return false;
75
            }
76
            return true;
77
        }
78 bca81f4c gaqhf
79 7cbb1038 gaqhf
        public static bool ConvertToGridSetting(string jsonString)
80
        {
81
            GridSetting _GridSetting = GridSetting.GetInstance();
82
            try
83
            {
84
                GridSetting jsonGridSetting = JsonConvert.DeserializeObject<GridSetting>(jsonString);
85
86
                _GridSetting.UseSnapGrid = jsonGridSetting.UseSnapGrid;
87
                _GridSetting.Density = jsonGridSetting.Density;
88
                _GridSetting.Unit = jsonGridSetting.Unit;
89
            }
90
            catch (Exception ex)
91
            {
92
                return false;
93
            }
94
            return true;
95
        }
96
97 39a2a688 gaqhf
        public static bool ConvertPointBystring(string sPoint, ref double dX, ref double dY)
98
        {
99
            try
100
            {
101
                string[] pointArr = sPoint.Split(new char[] { ',' });
102
                if (pointArr.Length == 2)
103
                {
104
                    dX = Convert.ToDouble(pointArr[0]);
105
                    dY = Convert.ToDouble(pointArr[1]);
106
                }
107
            }
108
            catch (Exception)
109
            {
110
                dX = 0;
111
                dY = 0;
112
                return false;
113
            }
114
115
            return true;
116
        }
117 bca81f4c gaqhf
118 39a2a688 gaqhf
        public static void ConvertSPPIDPoint(ref double dX, ref double dY, double dDwgX, double dDwgY, double SPPID_Width, double SPPID_Height)
119
        {
120 1b261371 gaqhf
            decimal calcX = 0;
121
            decimal calcY = 0;
122
            decimal tempX = Convert.ToDecimal(dX);
123
            decimal tempY = Convert.ToDecimal(dY);
124
            decimal tempWidth = Convert.ToDecimal(SPPID_Width);
125
            decimal tempHeight = Convert.ToDecimal(SPPID_Height);
126
            decimal tempDwgX = Convert.ToDecimal(dDwgX);
127
            decimal tempDwgY = Convert.ToDecimal(dDwgY);
128
129 c3d2e266 gaqhf
            //calcX = (tempX * tempWidth) / tempDwgX;
130
            //calcX = Math.Truncate(calcX * 1000) / 1000;
131
            //calcY = tempHeight - ((tempY * tempHeight) / tempDwgY);
132
            //calcY = Math.Truncate(calcY * 1000) / 1000;
133
            //dX = Math.Round(Convert.ToDouble(calcX), 10);
134
            //dY = Math.Round(Convert.ToDouble(calcY), 10);
135
136 1b261371 gaqhf
            calcX = (tempX * tempWidth) / tempDwgX;
137
            calcY = tempHeight - ((tempY * tempHeight) / tempDwgY);
138 c3d2e266 gaqhf
            dX = Convert.ToDouble(calcX);
139
            dY = Convert.ToDouble(calcY);
140 39a2a688 gaqhf
        }
141 bca81f4c gaqhf
142 06b40010 gaqhf
        public static void ConvertGridPoint(ref double x, ref double y)
143
        {
144
            GridSetting _GridSetting = GridSetting.GetInstance();
145
            if (_GridSetting.UseSnapGrid)
146
            {
147
                if (_GridSetting.Unit == GridUnit.Inch)
148
                {
149
                    double length = _GridSetting.Density * 0.0254;
150
                    double tempX = x;
151
                    double tempY = y;
152
                    x = Math.Round(tempX / length) * length;
153
                    y = Math.Round(tempY / length) * length;
154 b66a2996 gaqhf
                }
155
            }
156
        }
157 4d2571ab gaqhf
158 b66a2996 gaqhf
        public static void ConvertGridPointOnlyOnePoint(ref double value)
159
        {
160
            GridSetting _GridSetting = GridSetting.GetInstance();
161
            if (_GridSetting.UseSnapGrid)
162
            {
163
                if (_GridSetting.Unit == GridUnit.Inch)
164
                {
165
                    double length = _GridSetting.Density * 0.0254;
166
                    double temp = value;
167
                    value = Math.Round(value / length) * length;
168 06b40010 gaqhf
                }
169
            }
170
        }
171
172 5dfb8a24 gaqhf
        public static SlopeType CalcSlope(double x1, double y1, double x2, double y2)
173
        {
174
            if (x1 - x2 == 0)
175
            {
176
                return SlopeType.VERTICAL;
177
            }
178
            else
179
            {
180
                double angle = Math.Atan(Math.Abs(y2 - y1) / Math.Abs(x2 - x1)) * 180 / Math.PI;
181 0bbd73b5 gaqhf
                if (angle <= 10)
182 5dfb8a24 gaqhf
                    return SlopeType.HORIZONTAL;
183 0bbd73b5 gaqhf
                else if (angle >= 80)
184 5dfb8a24 gaqhf
                    return SlopeType.VERTICAL;
185
                else
186
                    return SlopeType.Slope;
187
            }
188
        }
189
190 5e6ecf05 gaqhf
        public static double CalcLineToPointDistance(double lineX1, double lineY1, double lineX2, double lineY2, double x, double y)
191
        {
192
193
            double distance = 0;
194
            if (lineX1 == lineX2)
195
                distance = Math.Abs(x - lineX1);
196
            else
197
            {
198
                double a;
199
                double b;
200
                double c;
201
202
                a = (lineY2 - lineY1) / (lineX2 - lineX1);
203
                b = -1;
204
                c = -a * lineX1 + lineY1;
205
206
                distance = Math.Abs(a * x + b * y + c) / Math.Pow(a * a + b * b, 0.5);
207
            }
208
            return distance;
209
        }
210
211 56bc67e1 gaqhf
        public static double[] CalcLineCrossingPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
212
        {
213
            x1 = Math.Round(x1, 10);
214
            x2 = Math.Round(x2, 10);
215
            x3 = Math.Round(x3, 10);
216
            x4 = Math.Round(x4, 10);
217
218
            y1 = Math.Round(y1, 10);
219
            y2 = Math.Round(y2, 10);
220
            y3 = Math.Round(y3, 10);
221
            y4 = Math.Round(y4, 10);
222
223
            double a1;
224
            double a2;
225
            double b1;
226
            double b2;
227
228
            if (x1 == x2)
229
                a1 = 0;
230
            else
231
                a1 = (y2 - y1) / (x2 - x1);
232
            if (x3 == x4)
233
                a2 = 0;
234
            else
235
                a2 = (y4 - y3) / (x4 - x3);
236
237
            b1 = -a1 * x1 + y1;
238
            b2 = -a2 * x3 + y3;
239
240
            if ((x1 == x2 && x3 == x4) ||
241
                (y1 == y2 && y3 == y4))
242
            {
243
                return null;
244
            }
245
            else if (x1 == x2)
246
            {
247
                return new double[] { x1, a2*x1 + b2 };
248
            }
249
            else if (x3 == x4)
250
            {
251
                return new double[] { x3, a1 * x3 + b1 };
252
            }
253
            else
254
            {
255
                return new double[] { -(b1 - b2) / (a1 - a2), a1 * (-(b1 - b2) / (a1 - a2) + b1) };
256
            }
257
        }
258 d1eac84d gaqhf
259 30a9ffce gaqhf
        public static double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
260
        {
261
            return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
262
        }
263
264 bca81f4c gaqhf
        public static object FindObjectByUID(Document document, string UID)
265
        {
266
            foreach (Symbol item in document.SYMBOLS)
267
            {
268
                if (item.UID == UID)
269
                    return item;
270
            }
271
272
            foreach (Line item in document.LINES)
273
            {
274
                if (item.UID == UID)
275
                    return item;
276
            }
277
278
            foreach (Text item in document.TEXTINFOS)
279
            {
280
                if (item.UID == UID)
281
                    return item;
282
            }
283
284
            foreach (Note item in document.NOTES)
285
            {
286
                if (item.UID == UID)
287
                    return item;
288
            }
289
290
            foreach (LineNumber item in document.LINENUMBERS)
291
            {
292
                if (item.UID == UID)
293
                    return item;
294
            }
295
296 809a7640 gaqhf
            foreach (Equipment item in document.Equipments)
297
            {
298
                if (item.UID == UID)
299
                    return item;
300
            }
301
302 bca81f4c gaqhf
            return null;
303
        }
304 d1eac84d gaqhf
305 335b7a24 gaqhf
        public static List<Line> FindLinesByModelId(Document document, string ModelItemId)
306
        {
307
            List<Line> lines = new List<Line>();
308
            foreach (Line item in document.LINES)
309
            {
310
                if (item.SPPID.ModelItemId == ModelItemId)
311
                    lines.Add(item);
312
            }
313
314
            return lines;
315
        }
316 d1eac84d gaqhf
317 4d2571ab gaqhf
        public static void FindConnectedSymbolGroup(Document document, Symbol symbol, List<Symbol> group)
318 b9cc9254 gaqhf
        {
319 4d2571ab gaqhf
            foreach (var connector in symbol.CONNECTORS)
320 b9cc9254 gaqhf
            {
321 4d2571ab gaqhf
                object connectedItem = FindObjectByUID(document, connector.CONNECTEDITEM);
322
                if (connectedItem != null && connectedItem.GetType() == typeof(Symbol))
323 b9cc9254 gaqhf
                {
324 4d2571ab gaqhf
                    Symbol connSymbol = connectedItem as Symbol;
325
                    if (!group.Contains(connSymbol))
326 b9cc9254 gaqhf
                    {
327 4d2571ab gaqhf
                        group.Add(connSymbol);
328
                        FindConnectedSymbolGroup(document, connSymbol, group);
329 b9cc9254 gaqhf
                    }
330
                }
331
            }
332
        }
333
334 4d2571ab gaqhf
        public static Symbol FindCenterAtThreeSymbols(Document document, List<Symbol> group)
335 d1eac84d gaqhf
        {
336 4d2571ab gaqhf
            Symbol result = null;
337
338
            // Group의 가운데 찾기
339
            if (group.Count == 3)
340 d1eac84d gaqhf
            {
341 4d2571ab gaqhf
                foreach (var symbol in group)
342
                {
343
                    int count = 0;
344
                    foreach (var connector in symbol.CONNECTORS)
345
                    {
346
                        object item = FindObjectByUID(document, connector.CONNECTEDITEM);
347
                        if (item != null && item.GetType() == typeof(Symbol) && group.Contains(item as Symbol))
348
                            count++;
349
                    }
350 d1eac84d gaqhf
351 4d2571ab gaqhf
                    if (count == 2)
352
                    {
353
                        result = symbol;
354
                        break;
355
                    }
356
                }
357 d1eac84d gaqhf
            }
358
359 4d2571ab gaqhf
            return result;
360 d1eac84d gaqhf
        }
361
362
        private static void GetConnectedSymbol(Document document, Symbol symbol, List<Symbol> symbolGroup)
363
        {
364
            foreach (Connector connector in symbol.CONNECTORS)
365
            {
366
                object item = FindObjectByUID(document, connector.CONNECTEDITEM);
367
                if (item != null && item.GetType() == typeof(Symbol))
368
                {
369
                    Symbol connSymbol = item as Symbol;
370
                    if (connSymbol != null && !symbolGroup.Contains(connSymbol))
371
                    {
372
                        symbolGroup.Add(connSymbol);
373
                        GetConnectedSymbol(document, connSymbol, symbolGroup);
374
                    }
375
                }
376
            }
377
        }
378 2fdb56bf gaqhf
379
        public static Connector FindSymbolConnectorByUID(Document document, string uid, Symbol targetSymbol)
380
        {
381
            foreach (var connector in targetSymbol.CONNECTORS)
382
            {
383
                if (connector.CONNECTEDITEM == uid)
384
                {
385
                    return connector;
386
                }
387
            }
388
389
            return null;
390
        }
391 71ba1ca3 gaqhf
392
        public static Symbol FindSymbolByRepresentationID(Document document, string repID)
393
        {
394
            Symbol findSymbol = null;
395
            foreach (var symbol in document.SYMBOLS)
396
            {
397
                if (symbol.SPPID.RepresentationId == repID)
398
                {
399
                    findSymbol = symbol;
400
                }
401
                else
402
                {
403
                    ChildSymbol childSymbol = FindChildSymbolByRepresentationID(document, symbol, repID);
404
                    if (childSymbol != null)
405
                        findSymbol = symbol;
406
                }
407
408
                if (findSymbol != null)
409
                    break;
410
            }
411
412
            return findSymbol;
413
        }
414
415
        public static ChildSymbol FindChildSymbolByRepresentationID(Document document, Symbol symbol, string repID)
416
        {
417
            ChildSymbol childSymbol = null;
418
419
            foreach (ChildSymbol loopChild in symbol.ChildSymbols)
420
            {
421
                if (loopChild.SPPID.RepresentationId == repID)
422
                {
423
                    childSymbol = loopChild;
424
                    break;
425
                }
426
                else
427
                {
428
                    childSymbol = FindChildSymbolByRepresentationIDLoop(document, repID, loopChild);
429
                    if (childSymbol != null)
430
                        break;
431
                }
432
            }
433
434
            return childSymbol;
435
        }
436
437
        private static ChildSymbol FindChildSymbolByRepresentationIDLoop(Document document, string repID, ChildSymbol childSymbol)
438
        {
439
            ChildSymbol findChild = null;
440
441
            foreach (var item in childSymbol.ChildSymbols)
442
            {
443
                if (item.SPPID.RepresentationId == repID)
444
                {
445
                    findChild = item;
446
                    break;
447
                }
448
                else
449
                {
450
                    findChild = FindChildSymbolByRepresentationIDLoop(document, repID, item);
451
                    if (findChild != null)
452
                        break;
453
                }
454
            }
455
456
            return findChild;
457
        }
458 23eb98bf gaqhf
    }
459
}
클립보드 이미지 추가 (최대 크기: 500 MB)