프로젝트

일반

사용자정보

통계
| 개정판:

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

이력 | 보기 | 이력해설 | 다운로드 (22.2 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
            }
91
            catch (Exception ex)
92
            {
93
                return false;
94
            }
95
            return true;
96
        }
97

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

    
116
            return true;
117
        }
118

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

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

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

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

    
176
            return value;
177
        }
178

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

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

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

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

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

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

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

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

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

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

    
270
            return result;
271
        }
272

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

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

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

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

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

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

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

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

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

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

    
337
            return null;
338
        }
339

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

    
349
            return lines;
350
        }
351

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

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

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

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

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

    
397
            return result;
398
        }
399

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

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

    
427
            return null;
428
        }
429

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

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

    
450
            return findSymbol;
451
        }
452

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

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

    
472
            return childSymbol;
473
        }
474

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

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

    
494
            return findChild;
495
        }
496

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

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

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

    
507
            return result;
508
        }
509

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

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

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

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

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

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

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

    
579
            return line;
580
        }
581

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

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

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

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

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

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

    
620
            return result;
621
        }
622

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

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

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

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