프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / SPPIDConverter / Util / SPPIDUtil.cs @ 6d12a734

이력 | 보기 | 이력해설 | 다운로드 (17.1 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 temp = value;
166 d9794a6c gaqhf
                    value = Math.Round(value / _GridSetting.Length) * _GridSetting.Length;
167 06b40010 gaqhf
                }
168
            }
169
        }
170
171 5dfb8a24 gaqhf
        public static SlopeType CalcSlope(double x1, double y1, double x2, double y2)
172
        {
173
            if (x1 - x2 == 0)
174
            {
175
                return SlopeType.VERTICAL;
176
            }
177
            else
178
            {
179
                double angle = Math.Atan(Math.Abs(y2 - y1) / Math.Abs(x2 - x1)) * 180 / Math.PI;
180 d9794a6c gaqhf
                if (angle <= 15)
181 5dfb8a24 gaqhf
                    return SlopeType.HORIZONTAL;
182 d9794a6c gaqhf
                else if (angle >= 75)
183 5dfb8a24 gaqhf
                    return SlopeType.VERTICAL;
184
                else
185
                    return SlopeType.Slope;
186
            }
187
        }
188
189 5e6ecf05 gaqhf
        public static double CalcLineToPointDistance(double lineX1, double lineY1, double lineX2, double lineY2, double x, double y)
190
        {
191
192
            double distance = 0;
193
            if (lineX1 == lineX2)
194
                distance = Math.Abs(x - lineX1);
195
            else
196
            {
197
                double a;
198
                double b;
199
                double c;
200
201
                a = (lineY2 - lineY1) / (lineX2 - lineX1);
202
                b = -1;
203
                c = -a * lineX1 + lineY1;
204
205
                distance = Math.Abs(a * x + b * y + c) / Math.Pow(a * a + b * b, 0.5);
206
            }
207
            return distance;
208
        }
209
210 56bc67e1 gaqhf
        public static double[] CalcLineCrossingPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
211
        {
212
            x1 = Math.Round(x1, 10);
213
            x2 = Math.Round(x2, 10);
214
            x3 = Math.Round(x3, 10);
215
            x4 = Math.Round(x4, 10);
216
217
            y1 = Math.Round(y1, 10);
218
            y2 = Math.Round(y2, 10);
219
            y3 = Math.Round(y3, 10);
220
            y4 = Math.Round(y4, 10);
221
222
            double a1;
223
            double a2;
224
            double b1;
225
            double b2;
226
227
            if (x1 == x2)
228
                a1 = 0;
229
            else
230
                a1 = (y2 - y1) / (x2 - x1);
231
            if (x3 == x4)
232
                a2 = 0;
233
            else
234
                a2 = (y4 - y3) / (x4 - x3);
235
236
            b1 = -a1 * x1 + y1;
237
            b2 = -a2 * x3 + y3;
238
239
            if ((x1 == x2 && x3 == x4) ||
240
                (y1 == y2 && y3 == y4))
241
            {
242
                return null;
243
            }
244
            else if (x1 == x2)
245
            {
246
                return new double[] { x1, a2*x1 + b2 };
247
            }
248
            else if (x3 == x4)
249
            {
250
                return new double[] { x3, a1 * x3 + b1 };
251
            }
252
            else
253
            {
254
                return new double[] { -(b1 - b2) / (a1 - a2), a1 * (-(b1 - b2) / (a1 - a2) + b1) };
255
            }
256
        }
257 d1eac84d gaqhf
258 30a9ffce gaqhf
        public static double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
259
        {
260
            return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
261
        }
262
263 bca81f4c gaqhf
        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 d1eac84d gaqhf
304 335b7a24 gaqhf
        public static List<Line> FindLinesByModelId(Document document, string ModelItemId)
305
        {
306
            List<Line> lines = new List<Line>();
307
            foreach (Line item in document.LINES)
308
            {
309
                if (item.SPPID.ModelItemId == ModelItemId)
310
                    lines.Add(item);
311
            }
312
313
            return lines;
314
        }
315 d1eac84d gaqhf
316 4d2571ab gaqhf
        public static void FindConnectedSymbolGroup(Document document, Symbol symbol, List<Symbol> group)
317 b9cc9254 gaqhf
        {
318 4d2571ab gaqhf
            foreach (var connector in symbol.CONNECTORS)
319 b9cc9254 gaqhf
            {
320 4d2571ab gaqhf
                object connectedItem = FindObjectByUID(document, connector.CONNECTEDITEM);
321
                if (connectedItem != null && connectedItem.GetType() == typeof(Symbol))
322 b9cc9254 gaqhf
                {
323 4d2571ab gaqhf
                    Symbol connSymbol = connectedItem as Symbol;
324
                    if (!group.Contains(connSymbol))
325 b9cc9254 gaqhf
                    {
326 4d2571ab gaqhf
                        group.Add(connSymbol);
327
                        FindConnectedSymbolGroup(document, connSymbol, group);
328 b9cc9254 gaqhf
                    }
329
                }
330
            }
331
        }
332
333 4d2571ab gaqhf
        public static Symbol FindCenterAtThreeSymbols(Document document, List<Symbol> group)
334 d1eac84d gaqhf
        {
335 4d2571ab gaqhf
            Symbol result = null;
336
337
            // Group의 가운데 찾기
338
            if (group.Count == 3)
339 d1eac84d gaqhf
            {
340 4d2571ab gaqhf
                foreach (var symbol in group)
341
                {
342
                    int count = 0;
343
                    foreach (var connector in symbol.CONNECTORS)
344
                    {
345
                        object item = FindObjectByUID(document, connector.CONNECTEDITEM);
346
                        if (item != null && item.GetType() == typeof(Symbol) && group.Contains(item as Symbol))
347
                            count++;
348
                    }
349 d1eac84d gaqhf
350 4d2571ab gaqhf
                    if (count == 2)
351
                    {
352
                        result = symbol;
353
                        break;
354
                    }
355
                }
356 d1eac84d gaqhf
            }
357
358 4d2571ab gaqhf
            return result;
359 d1eac84d gaqhf
        }
360
361
        private static void GetConnectedSymbol(Document document, Symbol symbol, List<Symbol> symbolGroup)
362
        {
363
            foreach (Connector connector in symbol.CONNECTORS)
364
            {
365
                object item = FindObjectByUID(document, connector.CONNECTEDITEM);
366
                if (item != null && item.GetType() == typeof(Symbol))
367
                {
368
                    Symbol connSymbol = item as Symbol;
369
                    if (connSymbol != null && !symbolGroup.Contains(connSymbol))
370
                    {
371
                        symbolGroup.Add(connSymbol);
372
                        GetConnectedSymbol(document, connSymbol, symbolGroup);
373
                    }
374
                }
375
            }
376
        }
377 2fdb56bf gaqhf
378
        public static Connector FindSymbolConnectorByUID(Document document, string uid, Symbol targetSymbol)
379
        {
380
            foreach (var connector in targetSymbol.CONNECTORS)
381
            {
382
                if (connector.CONNECTEDITEM == uid)
383
                {
384
                    return connector;
385
                }
386
            }
387
388
            return null;
389
        }
390 71ba1ca3 gaqhf
391
        public static Symbol FindSymbolByRepresentationID(Document document, string repID)
392
        {
393
            Symbol findSymbol = null;
394
            foreach (var symbol in document.SYMBOLS)
395
            {
396
                if (symbol.SPPID.RepresentationId == repID)
397
                {
398
                    findSymbol = symbol;
399
                }
400
                else
401
                {
402
                    ChildSymbol childSymbol = FindChildSymbolByRepresentationID(document, symbol, repID);
403
                    if (childSymbol != null)
404
                        findSymbol = symbol;
405
                }
406
407
                if (findSymbol != null)
408
                    break;
409
            }
410
411
            return findSymbol;
412
        }
413
414
        public static ChildSymbol FindChildSymbolByRepresentationID(Document document, Symbol symbol, string repID)
415
        {
416
            ChildSymbol childSymbol = null;
417
418
            foreach (ChildSymbol loopChild in symbol.ChildSymbols)
419
            {
420
                if (loopChild.SPPID.RepresentationId == repID)
421
                {
422
                    childSymbol = loopChild;
423
                    break;
424
                }
425
                else
426
                {
427
                    childSymbol = FindChildSymbolByRepresentationIDLoop(document, repID, loopChild);
428
                    if (childSymbol != null)
429
                        break;
430
                }
431
            }
432
433
            return childSymbol;
434
        }
435
436
        private static ChildSymbol FindChildSymbolByRepresentationIDLoop(Document document, string repID, ChildSymbol childSymbol)
437
        {
438
            ChildSymbol findChild = null;
439
440
            foreach (var item in childSymbol.ChildSymbols)
441
            {
442
                if (item.SPPID.RepresentationId == repID)
443
                {
444
                    findChild = item;
445
                    break;
446
                }
447
                else
448
                {
449
                    findChild = FindChildSymbolByRepresentationIDLoop(document, repID, item);
450
                    if (findChild != null)
451
                        break;
452
                }
453
            }
454
455
            return findChild;
456
        }
457 d9794a6c gaqhf
458
        public static bool IsBranchLine(Line line1, Line line2)
459
        {
460
            bool result = true;
461
462
            Connector conn1 = line1.CONNECTORS.Find(x => x.CONNECTEDITEM == line2.UID);
463
            Connector conn2 = line2.CONNECTORS.Find(x => x.CONNECTEDITEM == line1.UID);
464
465
            if (conn1 != null && conn2 != null)
466
                result = false;
467
468
            return result;
469
        }
470
471 6d12a734 gaqhf
        public static void CalcOverlap(double[] range1, double[] range2, ref double x, ref double y, ref bool overlapX, ref bool overlapY)
472 d9794a6c gaqhf
        {
473
            if (range1[0] <= range2[2] && range1[2] >= range2[0])
474
            {
475 6d12a734 gaqhf
                overlapX = true;
476 d9794a6c gaqhf
                x = Math.Min(Math.Abs(range1[0] - range2[2]), Math.Abs(range1[2] - range2[0]));
477
            }
478
479
            if (range1[1] <= range2[3] && range1[3] >= range2[1])
480
            {
481 6d12a734 gaqhf
                overlapY = true;
482 d9794a6c gaqhf
                y = Math.Min(Math.Abs(range1[1] - range2[3]), Math.Abs(range1[3] - range2[1]));
483
            }
484
        }
485
486
        public static void CalcNewCoordinateForSymbol(Symbol symbol, Symbol prevSymbol, double distanceX, double distanceY)
487
        {
488
            SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
489
            GridSetting _GridSetting = GridSetting.GetInstance();
490
            if (slopeType == SlopeType.HORIZONTAL)
491
            {
492
                double length = (Math.Ceiling(distanceX / _GridSetting.Length) + 1) * _GridSetting.Length;
493
                if (prevSymbol.SPPID.ORIGINAL_X < symbol.SPPID.ORIGINAL_X)
494
                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + length;
495
                else
496
                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - length;
497
            }
498
            else if (slopeType == SlopeType.VERTICAL)
499
            {
500
                double length = (Math.Ceiling(distanceY / _GridSetting.Length) + 1) * _GridSetting.Length;
501
                if (prevSymbol.SPPID.ORIGINAL_Y < symbol.SPPID.ORIGINAL_Y)
502
                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + length;
503
                else
504
                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - length;
505
            }
506
        }
507 23eb98bf gaqhf
    }
508
}
클립보드 이미지 추가 (최대 크기: 500 MB)