프로젝트

일반

사용자정보

통계
| 개정판:

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

이력 | 보기 | 이력해설 | 다운로드 (21 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

    
331
            return null;
332
        }
333

    
334
        public static List<Line> FindLinesByModelId(Document document, string ModelItemId)
335
        {
336
            List<Line> lines = new List<Line>();
337
            foreach (Line item in document.LINES)
338
            {
339
                if (item.SPPID.ModelItemId == ModelItemId)
340
                    lines.Add(item);
341
            }
342

    
343
            return lines;
344
        }
345

    
346
        public static void FindConnectedSymbolGroup(Document document, Symbol symbol, List<Symbol> group)
347
        {
348
            if (!group.Contains(symbol))
349
                group.Add(symbol);
350

    
351
            foreach (var connector in symbol.CONNECTORS)
352
            {
353
                object connectedItem = FindObjectByUID(document, connector.CONNECTEDITEM);
354
                if (connectedItem != null && connectedItem.GetType() == typeof(Symbol))
355
                {
356
                    Symbol connSymbol = connectedItem as Symbol;
357
                    if (!group.Contains(connSymbol))
358
                    {
359
                        group.Add(connSymbol);
360
                        FindConnectedSymbolGroup(document, connSymbol, group);
361
                    }
362
                }
363
            }
364
        }
365

    
366
        public static Symbol FindCenterAtThreeSymbols(Document document, List<Symbol> group)
367
        {
368
            Symbol result = null;
369

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

    
383
                    if (count == 2)
384
                    {
385
                        result = symbol;
386
                        break;
387
                    }
388
                }
389
            }
390

    
391
            return result;
392
        }
393

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

    
411
        public static Connector FindSymbolConnectorByUID(Document document, string uid, Symbol targetSymbol)
412
        {
413
            foreach (var connector in targetSymbol.CONNECTORS)
414
            {
415
                if (connector.CONNECTEDITEM == uid)
416
                {
417
                    return connector;
418
                }
419
            }
420

    
421
            return null;
422
        }
423

    
424
        public static Symbol FindSymbolByRepresentationID(Document document, string repID)
425
        {
426
            Symbol findSymbol = null;
427
            foreach (var symbol in document.SYMBOLS)
428
            {
429
                if (symbol.SPPID.RepresentationId == repID)
430
                {
431
                    findSymbol = symbol;
432
                }
433
                else
434
                {
435
                    ChildSymbol childSymbol = FindChildSymbolByRepresentationID(document, symbol, repID);
436
                    if (childSymbol != null)
437
                        findSymbol = symbol;
438
                }
439

    
440
                if (findSymbol != null)
441
                    break;
442
            }
443

    
444
            return findSymbol;
445
        }
446

    
447
        public static ChildSymbol FindChildSymbolByRepresentationID(Document document, Symbol symbol, string repID)
448
        {
449
            ChildSymbol childSymbol = null;
450

    
451
            foreach (ChildSymbol loopChild in symbol.ChildSymbols)
452
            {
453
                if (loopChild.SPPID.RepresentationId == repID)
454
                {
455
                    childSymbol = loopChild;
456
                    break;
457
                }
458
                else
459
                {
460
                    childSymbol = FindChildSymbolByRepresentationIDLoop(document, repID, loopChild);
461
                    if (childSymbol != null)
462
                        break;
463
                }
464
            }
465

    
466
            return childSymbol;
467
        }
468

    
469
        private static ChildSymbol FindChildSymbolByRepresentationIDLoop(Document document, string repID, ChildSymbol childSymbol)
470
        {
471
            ChildSymbol findChild = null;
472

    
473
            foreach (var item in childSymbol.ChildSymbols)
474
            {
475
                if (item.SPPID.RepresentationId == repID)
476
                {
477
                    findChild = item;
478
                    break;
479
                }
480
                else
481
                {
482
                    findChild = FindChildSymbolByRepresentationIDLoop(document, repID, item);
483
                    if (findChild != null)
484
                        break;
485
                }
486
            }
487

    
488
            return findChild;
489
        }
490

    
491
        public static bool IsBranchLine(Line line1, Line line2)
492
        {
493
            bool result = true;
494

    
495
            Connector conn1 = line1.CONNECTORS.Find(x => x.CONNECTEDITEM == line2.UID);
496
            Connector conn2 = line2.CONNECTORS.Find(x => x.CONNECTEDITEM == line1.UID);
497

    
498
            if (conn1 != null && conn2 != null)
499
                result = false;
500

    
501
            return result;
502
        }
503

    
504
        public static bool IsBranchLine(Line line)
505
        {
506
            Connector connector = line.CONNECTORS.Find(x =>
507
            x.ConnectedObject != null &&
508
            x.ConnectedObject.GetType() == typeof(Line) &&
509
            IsBranchLine(line, x.ConnectedObject as Line));
510
            return connector != null ? true : false;
511
        }
512

    
513
        public static bool IsBranchedLine(Document document,Line line)
514
        {
515
            return document.LINES.Find(x => x.CONNECTORS.Find(y => y.CONNECTEDITEM == line.UID && IsBranchLine(x, y.ConnectedObject as Line)) != null) != null ? true : false;
516
        }
517

    
518
        public static void CalcOverlap(double[] range1, double[] range2, ref double x, ref double y, ref bool overlapX, ref bool overlapY)
519
        {
520
            if (range1[0] <= range2[2] && range1[2] >= range2[0])
521
            {
522
                overlapX = true;
523
                x = Math.Min(Math.Abs(range1[0] - range2[2]), Math.Abs(range1[2] - range2[0]));
524
            }
525

    
526
            if (range1[1] <= range2[3] && range1[3] >= range2[1])
527
            {
528
                overlapY = true;
529
                y = Math.Min(Math.Abs(range1[1] - range2[3]), Math.Abs(range1[3] - range2[1]));
530
            }
531
        }
532

    
533
        public static bool IsOverlap(double[] range1, double[] range2)
534
        {
535
            if (range1[0] <= range2[2] && range1[2] >= range2[0] && range1[1] <= range2[3] && range1[3] >= range2[1])
536
                return true;
537
            else
538
                return false;
539
        }
540

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

    
563
        public static bool GetLineConnectorPoint(Line line, Connector connector, ref double x, ref double y)
564
        {
565
            bool bStart = false;
566
            int index = line.CONNECTORS.IndexOf(connector);
567
            if (index == 0)
568
            {
569
                x = line.SPPID.START_X;
570
                y = line.SPPID.START_Y;
571
                bStart = true;
572
            }
573
            else
574
            {
575
                x = line.SPPID.END_X;
576
                y = line.SPPID.END_Y;
577
            }
578
            return bStart;
579
        }
580

    
581
        public static bool IsSegment(Document document, Line line1, Line line2)
582
        {
583
            bool result = false;
584

    
585
            SpecBreak specBreak = document.SpecBreaks.Find(x =>
586
                (x.DownStreamUID == line1.UID && x.UpStreamUID == line2.UID) || 
587
                (x.DownStreamUID == line2.UID && x.UpStreamUID == line1.UID));
588

    
589
            EndBreak endBreak = document.EndBreaks.Find(x =>
590
            (x.OWNER == line1.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == line2.UID) ||
591
            (x.OWNER == line2.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == line1.UID));
592

    
593
            if (specBreak != null || endBreak != null)
594
                result = true;
595

    
596
            return result;
597
        }
598

    
599
        public static bool IsSegment(Document document, Symbol symbol1, Symbol symbol2)
600
        {
601
            bool result = false;
602
            SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
603
            (x.DownStreamUID == symbol1.UID && x.UpStreamUID == symbol2.UID) ||
604
            (x.DownStreamUID == symbol2.UID && x.UpStreamUID == symbol1.UID));
605

    
606
            EndBreak startEndBreak = document.EndBreaks.Find(x =>
607
            (x.OWNER == symbol1.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol2.UID) ||
608
            (x.OWNER == symbol2.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol1.UID));
609

    
610
            if (startSpecBreak != null || startEndBreak != null)
611
                result = true;
612

    
613
            return result;
614
        }
615
    }
616
}
클립보드 이미지 추가 (최대 크기: 500 MB)