프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / Util / SPPIDUtil.cs @ 58bf9dec

이력 | 보기 | 이력해설 | 다운로드 (22.3 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
            }
73
            catch (Exception ex)
74
            {
75
                return false;
76
            }
77
            return true;
78
        }
79

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

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

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

    
117
            return true;
118
        }
119

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

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

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

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

    
177
            return value;
178
        }
179

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

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

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

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

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

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

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

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

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

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

    
271
            return result;
272
        }
273

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

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

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

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

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

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

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

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

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

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

    
338
            return null;
339
        }
340

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

    
350
            return lines;
351
        }
352

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

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

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

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

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

    
398
            return result;
399
        }
400

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

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

    
428
            return null;
429
        }
430

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

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

    
451
            return findSymbol;
452
        }
453

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

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

    
473
            return childSymbol;
474
        }
475

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

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

    
495
            return findChild;
496
        }
497

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

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

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

    
508
            return result;
509
        }
510

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

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

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

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

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

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

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

    
580
            return line;
581
        }
582

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

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

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

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

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

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

    
621
            return result;
622
        }
623

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

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

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

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