프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / Util / SPPIDUtil.cs @ c3e8fa8e

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

1
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
using Converter.SPPID.DB;
9
using Converter.BaseModel;
10
using System.Windows.Forms;
11
using Converter.SPPID.Model;
12
using System.Drawing;
13

    
14
namespace Converter.SPPID.Util
15
{
16
    public enum SlopeType
17
    {
18
        None,
19
        Slope,
20
        HORIZONTAL,
21
        VERTICAL
22
    }
23
    public class SPPIDUtil
24
    {
25
        public static bool ConvertToSPPIDInfo(string jsonString)
26
        {
27
            SPPID_DBInfo _SPPIDInfo = SPPID_DBInfo.GetInstance();
28
            try
29
            {
30
                SPPID_DBInfo jsonSPPIDInfo = JsonConvert.DeserializeObject<SPPID_DBInfo>(jsonString);
31

    
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
                _SPPIDInfo.Enable = false;
52
                return false;
53
            }
54
            return true;
55
        }
56

    
57
        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
                _ETCSetting.VendorPackageSymbolPath = jsonETCSetting.VendorPackageSymbolPath;
68
                _ETCSetting.TextLocation = jsonETCSetting.TextLocation;
69
                _ETCSetting.NoteLocation = jsonETCSetting.NoteLocation;
70
                _ETCSetting.LineNumberLocation = jsonETCSetting.LineNumberLocation;
71
                _ETCSetting.FlowMarkSymbolPath = jsonETCSetting.FlowMarkSymbolPath;
72
                _ETCSetting.BorderFilePath = jsonETCSetting.BorderFilePath;
73
            }
74
            catch (Exception ex)
75
            {
76
                return false;
77
            }
78
            return true;
79
        }
80

    
81
        public static bool ConvertToGridSetting(string jsonString)
82
        {
83
            GridSetting _GridSetting = GridSetting.GetInstance();
84
            try
85
            {
86
                GridSetting jsonGridSetting = JsonConvert.DeserializeObject<GridSetting>(jsonString);
87

    
88
                _GridSetting.UseSnapGrid = jsonGridSetting.UseSnapGrid;
89
                _GridSetting.Density = jsonGridSetting.Density;
90
                _GridSetting.Unit = jsonGridSetting.Unit;
91
                _GridSetting.DrainValveCellCount = jsonGridSetting.DrainValveCellCount;
92
            }
93
            catch (Exception ex)
94
            {
95
                return false;
96
            }
97
            return true;
98
        }
99

    
100
        public static bool ConvertPointBystring(string sPoint, ref double dX, ref double dY)
101
        {
102
            try
103
            {
104
                string[] pointArr = sPoint.Split(new char[] { ',' });
105
                if (pointArr.Length == 2)
106
                {
107
                    dX = Convert.ToDouble(pointArr[0]);
108
                    dY = Convert.ToDouble(pointArr[1]);
109
                }
110
            }
111
            catch (Exception)
112
            {
113
                dX = 0;
114
                dY = 0;
115
                return false;
116
            }
117

    
118
            return true;
119
        }
120

    
121
        public static void ConvertSPPIDPoint(ref double dX, ref double dY, double dDwgX, double dDwgY, double SPPID_Width, double SPPID_Height)
122
        {
123
            decimal calcX = 0;
124
            decimal calcY = 0;
125
            decimal tempX = Convert.ToDecimal(dX);
126
            decimal tempY = Convert.ToDecimal(dY);
127
            decimal tempWidth = Convert.ToDecimal(SPPID_Width);
128
            decimal tempHeight = Convert.ToDecimal(SPPID_Height);
129
            decimal tempDwgX = Convert.ToDecimal(dDwgX);
130
            decimal tempDwgY = Convert.ToDecimal(dDwgY);
131

    
132
            calcX = (tempX * tempWidth) / tempDwgX;
133
            calcY = tempHeight - ((tempY * tempHeight) / tempDwgY);
134
            dX = Convert.ToDouble(calcX);
135
            dY = Convert.ToDouble(calcY);
136
        }
137

    
138
        public static void ConvertGridPoint(ref double x, ref double y)
139
        {
140
            GridSetting _GridSetting = GridSetting.GetInstance();
141
            if (_GridSetting.UseSnapGrid)
142
            {
143
                if (_GridSetting.Unit == GridUnit.Inch)
144
                {
145
                    double length = _GridSetting.Density * 0.0254;
146
                    double tempX = x;
147
                    double tempY = y;
148
                    x = Math.Round(tempX / length) * length;
149
                    y = Math.Round(tempY / length) * length;
150
                }
151
            }
152
        }
153

    
154
        public static void ConvertGridPointOnlyOnePoint(ref double value)
155
        {
156
            GridSetting _GridSetting = GridSetting.GetInstance();
157
            if (_GridSetting.UseSnapGrid)
158
            {
159
                if (_GridSetting.Unit == GridUnit.Inch)
160
                {
161
                    double temp = value;
162
                    value = Math.Round(value / _GridSetting.Length) * _GridSetting.Length;
163
                }
164
            }
165
        }
166
        public static double ConvertGridPointOnlyOnePoint(double value)
167
        {
168
            GridSetting _GridSetting = GridSetting.GetInstance();
169
            if (_GridSetting.UseSnapGrid)
170
            {
171
                if (_GridSetting.Unit == GridUnit.Inch)
172
                {
173
                    double temp = value;
174
                    value = Math.Round(value / _GridSetting.Length) * _GridSetting.Length;
175
                }
176
            }
177

    
178
            return value;
179
        }
180

    
181
        public static SlopeType CalcSlope(double x1, double y1, double x2, double y2, double degree = 15)
182
        {
183
            if (x1 - x2 == 0)
184
            {
185
                return SlopeType.VERTICAL;
186
            }
187
            else
188
            {
189
                double angle = Math.Atan(Math.Abs(y2 - y1) / Math.Abs(x2 - x1)) * 180 / Math.PI;
190
                if (angle <= degree)
191
                    return SlopeType.HORIZONTAL;
192
                else if (angle >= 90 - degree)
193
                    return SlopeType.VERTICAL;
194
                else
195
                    return SlopeType.Slope;
196
            }
197
        }
198
        public static double CalcAngle(double x1, double y1, double x2, double y2)
199
        {
200
            double result = 90;
201
            if (x1 - x2 != 0)
202
            {
203
                result = Math.Atan(Math.Abs(y2 - y1) / Math.Abs(x2 - x1)) * 180 / Math.PI;
204
            }
205
            return result;
206
        }
207
        public static double CalcLineToPointDistance(double lineX1, double lineY1, double lineX2, double lineY2, double x, double y)
208
        {
209

    
210
            double distance = 0;
211
            if (lineX1 == lineX2)
212
                distance = Math.Abs(x - lineX1);
213
            else
214
            {
215
                double a;
216
                double b;
217
                double c;
218

    
219
                a = (lineY2 - lineY1) / (lineX2 - lineX1);
220
                b = -1;
221
                c = -a * lineX1 + lineY1;
222

    
223
                distance = Math.Abs(a * x + b * y + c) / Math.Pow(a * a + b * b, 0.5);
224
            }
225
            return distance;
226
        }
227

    
228
        public static double[] CalcLineCrossingPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
229
        {
230
            double a1;
231
            double a2;
232
            double b1;
233
            double b2;
234

    
235
            if (x1 == x2)
236
                a1 = 0;
237
            else
238
                a1 = (y2 - y1) / (x2 - x1);
239
            if (x3 == x4)
240
                a2 = 0;
241
            else
242
                a2 = (y4 - y3) / (x4 - x3);
243

    
244
            b1 = -a1 * x1 + y1;
245
            b2 = -a2 * x3 + y3;
246

    
247
            if (x1 == x2 && x3 == x4)
248
            {
249
                return null;
250
            }
251
            else if (x1 == x2)
252
            {
253
                return new double[] { x1, a2*x1 + b2 };
254
            }
255
            else if (x3 == x4)
256
            {
257
                return new double[] { x3, a1 * x3 + b1 };
258
            }
259
            else
260
            {
261
                return new double[] { -(b1 - b2) / (a1 - a2), a1 * -(b1 - b2) / (a1 - a2) + b1 };
262
            }            
263
        }
264

    
265
        public static double CalcGradient(double x1, double y1, double x2, double y2)
266
        {
267
            double result = double.NaN;
268

    
269
            if (x1 != x2)
270
                result = (y2 - y1) / (x2 - x1);
271

    
272
            return result;
273
        }
274

    
275
        public static double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
276
        {
277
            return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
278
        }
279

    
280
        public static object FindObjectByUID(Document document, string UID)
281
        {
282
            if (!string.IsNullOrEmpty(UID) && UID != "None")
283
            {
284
                foreach (Symbol item in document.SYMBOLS)
285
                {
286
                    if (item.UID == UID)
287
                        return item;
288
                }
289

    
290
                foreach (Line item in document.LINES)
291
                {
292
                    if (item.UID == UID)
293
                        return item;
294
                }
295

    
296
                foreach (Text item in document.TEXTINFOS)
297
                {
298
                    if (item.UID == UID)
299
                        return item;
300
                }
301

    
302
                foreach (Note item in document.NOTES)
303
                {
304
                    if (item.UID == UID)
305
                        return item;
306
                }
307

    
308
                foreach (LineNumber item in document.LINENUMBERS)
309
                {
310
                    if (item.UID == UID)
311
                        return item;
312
                }
313

    
314
                foreach (Equipment item in document.Equipments)
315
                {
316
                    if (item.UID == UID)
317
                        return item;
318
                }
319

    
320
                foreach (SpecBreak item in document.SpecBreaks)
321
                {
322
                    if (item.UID == UID)
323
                        return item;
324
                }
325

    
326
                foreach (EndBreak item in document.EndBreaks)
327
                {
328
                    if (item.UID == UID)
329
                        return item;
330
                }
331

    
332
                foreach (VendorPackage item in document.VendorPackages)
333
                {
334
                    if (item.UID == UID)
335
                        return item;
336
                }
337
            }
338

    
339
            return null;
340
        }
341

    
342
        public static List<Line> FindLinesByModelId(Document document, string ModelItemId)
343
        {
344
            List<Line> lines = new List<Line>();
345
            foreach (Line item in document.LINES)
346
            {
347
                if (item.SPPID.ModelItemId == ModelItemId)
348
                    lines.Add(item);
349
            }
350

    
351
            return lines;
352
        }
353

    
354
        public static void FindConnectedSymbolGroup(Document document, Symbol symbol, List<Symbol> group)
355
        {
356
            if (!group.Contains(symbol))
357
                group.Add(symbol);
358

    
359
            foreach (var connector in symbol.CONNECTORS)
360
            {
361
                object connectedItem = FindObjectByUID(document, connector.CONNECTEDITEM);
362
                if (connectedItem != null && connectedItem.GetType() == typeof(Symbol))
363
                {
364
                    Symbol connSymbol = connectedItem as Symbol;
365
                    if (!group.Contains(connSymbol))
366
                    {
367
                        group.Add(connSymbol);
368
                        FindConnectedSymbolGroup(document, connSymbol, group);
369
                    }
370
                }
371
            }
372
        }
373

    
374
        public static Symbol FindCenterAtThreeSymbols(Document document, List<Symbol> group)
375
        {
376
            Symbol result = null;
377

    
378
            // Group의 가운데 찾기
379
            if (group.Count == 3)
380
            {
381
                foreach (var symbol in group)
382
                {
383
                    int count = 0;
384
                    foreach (var connector in symbol.CONNECTORS)
385
                    {
386
                        object item = FindObjectByUID(document, connector.CONNECTEDITEM);
387
                        if (item != null && item.GetType() == typeof(Symbol) && group.Contains(item as Symbol))
388
                            count++;
389
                    }
390

    
391
                    if (count == 2)
392
                    {
393
                        result = symbol;
394
                        break;
395
                    }
396
                }
397
            }
398

    
399
            return result;
400
        }
401

    
402
        private static void GetConnectedSymbol(Document document, Symbol symbol, List<Symbol> symbolGroup)
403
        {
404
            foreach (Connector connector in symbol.CONNECTORS)
405
            {
406
                object item = FindObjectByUID(document, connector.CONNECTEDITEM);
407
                if (item != null && item.GetType() == typeof(Symbol))
408
                {
409
                    Symbol connSymbol = item as Symbol;
410
                    if (connSymbol != null && !symbolGroup.Contains(connSymbol))
411
                    {
412
                        symbolGroup.Add(connSymbol);
413
                        GetConnectedSymbol(document, connSymbol, symbolGroup);
414
                    }
415
                }
416
            }
417
        }
418

    
419
        public static Connector FindSymbolConnectorByUID(Document document, string uid, Symbol targetSymbol)
420
        {
421
            foreach (var connector in targetSymbol.CONNECTORS)
422
            {
423
                if (connector.CONNECTEDITEM == uid)
424
                {
425
                    return connector;
426
                }
427
            }
428

    
429
            return null;
430
        }
431

    
432
        public static Symbol FindSymbolByRepresentationID(Document document, string repID)
433
        {
434
            Symbol findSymbol = null;
435
            foreach (var symbol in document.SYMBOLS)
436
            {
437
                if (symbol.SPPID.RepresentationId == repID)
438
                {
439
                    findSymbol = symbol;
440
                }
441
                else
442
                {
443
                    ChildSymbol childSymbol = FindChildSymbolByRepresentationID(document, symbol, repID);
444
                    if (childSymbol != null)
445
                        findSymbol = symbol;
446
                }
447

    
448
                if (findSymbol != null)
449
                    break;
450
            }
451

    
452
            return findSymbol;
453
        }
454

    
455
        public static ChildSymbol FindChildSymbolByRepresentationID(Document document, Symbol symbol, string repID)
456
        {
457
            ChildSymbol childSymbol = null;
458

    
459
            foreach (ChildSymbol loopChild in symbol.ChildSymbols)
460
            {
461
                if (loopChild.SPPID.RepresentationId == repID)
462
                {
463
                    childSymbol = loopChild;
464
                    break;
465
                }
466
                else
467
                {
468
                    childSymbol = FindChildSymbolByRepresentationIDLoop(document, repID, loopChild);
469
                    if (childSymbol != null)
470
                        break;
471
                }
472
            }
473

    
474
            return childSymbol;
475
        }
476

    
477
        private static ChildSymbol FindChildSymbolByRepresentationIDLoop(Document document, string repID, ChildSymbol childSymbol)
478
        {
479
            ChildSymbol findChild = null;
480

    
481
            foreach (var item in childSymbol.ChildSymbols)
482
            {
483
                if (item.SPPID.RepresentationId == repID)
484
                {
485
                    findChild = item;
486
                    break;
487
                }
488
                else
489
                {
490
                    findChild = FindChildSymbolByRepresentationIDLoop(document, repID, item);
491
                    if (findChild != null)
492
                        break;
493
                }
494
            }
495

    
496
            return findChild;
497
        }
498

    
499
        public static bool IsBranchLine(Line line1, Line line2)
500
        {
501
            bool result = true;
502

    
503
            Connector conn1 = line1.CONNECTORS.Find(x => x.CONNECTEDITEM == line2.UID);
504
            Connector conn2 = line2.CONNECTORS.Find(x => x.CONNECTEDITEM == line1.UID);
505

    
506
            if (conn1 != null && conn2 != null)
507
                result = false;
508

    
509
            return result;
510
        }
511

    
512
        public static bool IsBranchLine(Line line)
513
        {
514
            Connector connector = line.CONNECTORS.Find(x =>
515
            x.ConnectedObject != null &&
516
            x.ConnectedObject.GetType() == typeof(Line) &&
517
            IsBranchLine(line, x.ConnectedObject as Line));
518
            return connector != null ? true : false;
519
        }
520

    
521
        public static bool IsBranchedLine(Document document,Line line)
522
        {
523
            return document.LINES.Find(x => x.CONNECTORS.Find(y => y.CONNECTEDITEM == line.UID && IsBranchLine(x, y.ConnectedObject as Line)) != null) != null ? true : false;
524
        }
525

    
526
        public static void CalcOverlap(double[] range1, double[] range2, ref double x, ref double y, ref bool overlapX, ref bool overlapY)
527
        {
528
            if (range1[0] <= range2[2] && range1[2] >= range2[0])
529
            {
530
                overlapX = true;
531
                x = Math.Min(Math.Abs(range1[0] - range2[2]), Math.Abs(range1[2] - range2[0]));
532
            }
533

    
534
            if (range1[1] <= range2[3] && range1[3] >= range2[1])
535
            {
536
                overlapY = true;
537
                y = Math.Min(Math.Abs(range1[1] - range2[3]), Math.Abs(range1[3] - range2[1]));
538
            }
539
        }
540

    
541
        public static bool IsOverlap(double[] range1, double[] range2)
542
        {
543
            if (range1[0] <= range2[2] && range1[2] >= range2[0] && range1[1] <= range2[3] && range1[3] >= range2[1])
544
                return true;
545
            else
546
                return false;
547
        }
548

    
549
        public static void CalcNewCoordinateForSymbol(Symbol symbol, Symbol prevSymbol, double distanceX, double distanceY)
550
        {
551
            SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
552
            GridSetting _GridSetting = GridSetting.GetInstance();
553
            if (slopeType == SlopeType.HORIZONTAL)
554
            {
555
                double length = (Math.Ceiling(distanceX / _GridSetting.Length) + 1) * _GridSetting.Length;
556
                if (prevSymbol.SPPID.ORIGINAL_X < symbol.SPPID.ORIGINAL_X)
557
                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + length;
558
                else
559
                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - length;
560
            }
561
            else if (slopeType == SlopeType.VERTICAL)
562
            {
563
                double length = (Math.Ceiling(distanceY / _GridSetting.Length) + 1) * _GridSetting.Length;
564
                if (prevSymbol.SPPID.ORIGINAL_Y < symbol.SPPID.ORIGINAL_Y)
565
                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + length;
566
                else
567
                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - length;
568
            }
569
        }
570

    
571
        public static Line GetConnectedLine(Symbol symbol1, Symbol symbol2)
572
        {
573
            Line line = null;
574
            Connector connector1 = symbol1.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Line) &&
575
            ((Line)x.ConnectedObject).CONNECTORS.Find(y => y.ConnectedObject == symbol2) != null);
576
            Connector connector2 = symbol2.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Line) &&
577
            ((Line)x.ConnectedObject).CONNECTORS.Find(y => y.ConnectedObject == symbol1) != null);
578
            if (connector1 != null && connector2 != null)
579
                line = connector1.ConnectedObject as Line;
580

    
581
            return line;
582
        }
583

    
584
        public static int GetBranchLineCount(Document document, Line line)
585
        {
586
            return document.LINES.FindAll(x => IsBranchLine(line, x) && x.CONNECTORS.Find(y => y.ConnectedObject == line) != null).Count;
587
        }
588

    
589
        public static bool GetLineConnectorPoint(Line line, Connector connector, ref double x, ref double y)
590
        {
591
            bool bStart = false;
592
            int index = line.CONNECTORS.IndexOf(connector);
593
            if (index == 0)
594
            {
595
                x = line.SPPID.START_X;
596
                y = line.SPPID.START_Y;
597
                bStart = true;
598
            }
599
            else
600
            {
601
                x = line.SPPID.END_X;
602
                y = line.SPPID.END_Y;
603
            }
604
            return bStart;
605
        }
606

    
607
        public static bool IsSegment(Document document, Line line1, Line line2)
608
        {
609
            bool result = false;
610

    
611
            SpecBreak specBreak = document.SpecBreaks.Find(x =>
612
                (x.DownStreamUID == line1.UID && x.UpStreamUID == line2.UID) || 
613
                (x.DownStreamUID == line2.UID && x.UpStreamUID == line1.UID));
614

    
615
            EndBreak endBreak = document.EndBreaks.Find(x =>
616
            (x.OWNER == line1.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == line2.UID) ||
617
            (x.OWNER == line2.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == line1.UID));
618

    
619
            if (specBreak != null || endBreak != null)
620
                result = true;
621

    
622
            return result;
623
        }
624

    
625
        public static bool IsSegment(Document document, Symbol symbol1, Symbol symbol2)
626
        {
627
            bool result = false;
628
            SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
629
            (x.DownStreamUID == symbol1.UID && x.UpStreamUID == symbol2.UID) ||
630
            (x.DownStreamUID == symbol2.UID && x.UpStreamUID == symbol1.UID));
631

    
632
            EndBreak startEndBreak = document.EndBreaks.Find(x =>
633
            (x.OWNER == symbol1.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol2.UID) ||
634
            (x.OWNER == symbol2.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol1.UID));
635

    
636
            if (startSpecBreak != null || startEndBreak != null)
637
                result = true;
638

    
639
            return result;
640
        }
641
    }
642
}
클립보드 이미지 추가 (최대 크기: 500 MB)