프로젝트

일반

사용자정보

통계
| 개정판:

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

이력 | 보기 | 이력해설 | 다운로드 (18 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.TextLocation = jsonETCSetting.TextLocation;
68
                _ETCSetting.NoteLocation = jsonETCSetting.NoteLocation;
69
                _ETCSetting.LineNumberLocation = jsonETCSetting.LineNumberLocation;
70
                _ETCSetting.FlowMarkSymbolPath = jsonETCSetting.FlowMarkSymbolPath;
71
            }
72
            catch (Exception ex)
73
            {
74
                return false;
75
            }
76
            return true;
77
        }
78

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

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

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

    
115
            return true;
116
        }
117

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

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

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

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

    
175
            return value;
176
        }
177

    
178
        public static SlopeType CalcSlope(double x1, double y1, double x2, double y2)
179
        {
180
            if (x1 - x2 == 0)
181
            {
182
                return SlopeType.VERTICAL;
183
            }
184
            else
185
            {
186
                double angle = Math.Atan(Math.Abs(y2 - y1) / Math.Abs(x2 - x1)) * 180 / Math.PI;
187
                if (angle <= 15)
188
                    return SlopeType.HORIZONTAL;
189
                else if (angle >= 75)
190
                    return SlopeType.VERTICAL;
191
                else
192
                    return SlopeType.Slope;
193
            }
194
        }
195

    
196
        public static double CalcLineToPointDistance(double lineX1, double lineY1, double lineX2, double lineY2, double x, double y)
197
        {
198

    
199
            double distance = 0;
200
            if (lineX1 == lineX2)
201
                distance = Math.Abs(x - lineX1);
202
            else
203
            {
204
                double a;
205
                double b;
206
                double c;
207

    
208
                a = (lineY2 - lineY1) / (lineX2 - lineX1);
209
                b = -1;
210
                c = -a * lineX1 + lineY1;
211

    
212
                distance = Math.Abs(a * x + b * y + c) / Math.Pow(a * a + b * b, 0.5);
213
            }
214
            return distance;
215
        }
216

    
217
        public static double[] CalcLineCrossingPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
218
        {
219
            double a1;
220
            double a2;
221
            double b1;
222
            double b2;
223

    
224
            if (x1 == x2)
225
                a1 = 0;
226
            else
227
                a1 = (y2 - y1) / (x2 - x1);
228
            if (x3 == x4)
229
                a2 = 0;
230
            else
231
                a2 = (y4 - y3) / (x4 - x3);
232

    
233
            b1 = -a1 * x1 + y1;
234
            b2 = -a2 * x3 + y3;
235

    
236
            if ((x1 == x2 && x3 == x4) ||
237
                (y1 == y2 && y3 == y4))
238
            {
239
                return null;
240
            }
241
            else if (x1 == x2)
242
            {
243
                return new double[] { x1, a2*x1 + b2 };
244
            }
245
            else if (x3 == x4)
246
            {
247
                return new double[] { x3, a1 * x3 + b1 };
248
            }
249
            else
250
            {
251
                return new double[] { -(b1 - b2) / (a1 - a2), a1 * (-(b1 - b2) / (a1 - a2) + b1) };
252
            }
253
        }
254

    
255
        public static double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
256
        {
257
            return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
258
        }
259

    
260
        public static object FindObjectByUID(Document document, string UID)
261
        {
262
            if (!string.IsNullOrEmpty(UID) && UID != "None")
263
            {
264
                foreach (Symbol item in document.SYMBOLS)
265
                {
266
                    if (item.UID == UID)
267
                        return item;
268
                }
269

    
270
                foreach (Line item in document.LINES)
271
                {
272
                    if (item.UID == UID)
273
                        return item;
274
                }
275

    
276
                foreach (Text item in document.TEXTINFOS)
277
                {
278
                    if (item.UID == UID)
279
                        return item;
280
                }
281

    
282
                foreach (Note item in document.NOTES)
283
                {
284
                    if (item.UID == UID)
285
                        return item;
286
                }
287

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

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

    
301
            return null;
302
        }
303

    
304
        public static List<Line> FindLinesByModelId(Document document, string ModelItemId)
305
        {
306
            List<Line> lines = new List<Line>();
307
            foreach (Line item in document.LINES)
308
            {
309
                if (item.SPPID.ModelItemId == ModelItemId)
310
                    lines.Add(item);
311
            }
312

    
313
            return lines;
314
        }
315

    
316
        public static void FindConnectedSymbolGroup(Document document, Symbol symbol, List<Symbol> group)
317
        {
318
            if (!group.Contains(symbol))
319
                group.Add(symbol);
320

    
321
            foreach (var connector in symbol.CONNECTORS)
322
            {
323
                object connectedItem = FindObjectByUID(document, connector.CONNECTEDITEM);
324
                if (connectedItem != null && connectedItem.GetType() == typeof(Symbol))
325
                {
326
                    Symbol connSymbol = connectedItem as Symbol;
327
                    if (!group.Contains(connSymbol))
328
                    {
329
                        group.Add(connSymbol);
330
                        FindConnectedSymbolGroup(document, connSymbol, group);
331
                    }
332
                }
333
            }
334
        }
335

    
336
        public static Symbol FindCenterAtThreeSymbols(Document document, List<Symbol> group)
337
        {
338
            Symbol result = null;
339

    
340
            // Group의 가운데 찾기
341
            if (group.Count == 3)
342
            {
343
                foreach (var symbol in group)
344
                {
345
                    int count = 0;
346
                    foreach (var connector in symbol.CONNECTORS)
347
                    {
348
                        object item = FindObjectByUID(document, connector.CONNECTEDITEM);
349
                        if (item != null && item.GetType() == typeof(Symbol) && group.Contains(item as Symbol))
350
                            count++;
351
                    }
352

    
353
                    if (count == 2)
354
                    {
355
                        result = symbol;
356
                        break;
357
                    }
358
                }
359
            }
360

    
361
            return result;
362
        }
363

    
364
        private static void GetConnectedSymbol(Document document, Symbol symbol, List<Symbol> symbolGroup)
365
        {
366
            foreach (Connector connector in symbol.CONNECTORS)
367
            {
368
                object item = FindObjectByUID(document, connector.CONNECTEDITEM);
369
                if (item != null && item.GetType() == typeof(Symbol))
370
                {
371
                    Symbol connSymbol = item as Symbol;
372
                    if (connSymbol != null && !symbolGroup.Contains(connSymbol))
373
                    {
374
                        symbolGroup.Add(connSymbol);
375
                        GetConnectedSymbol(document, connSymbol, symbolGroup);
376
                    }
377
                }
378
            }
379
        }
380

    
381
        public static Connector FindSymbolConnectorByUID(Document document, string uid, Symbol targetSymbol)
382
        {
383
            foreach (var connector in targetSymbol.CONNECTORS)
384
            {
385
                if (connector.CONNECTEDITEM == uid)
386
                {
387
                    return connector;
388
                }
389
            }
390

    
391
            return null;
392
        }
393

    
394
        public static Symbol FindSymbolByRepresentationID(Document document, string repID)
395
        {
396
            Symbol findSymbol = null;
397
            foreach (var symbol in document.SYMBOLS)
398
            {
399
                if (symbol.SPPID.RepresentationId == repID)
400
                {
401
                    findSymbol = symbol;
402
                }
403
                else
404
                {
405
                    ChildSymbol childSymbol = FindChildSymbolByRepresentationID(document, symbol, repID);
406
                    if (childSymbol != null)
407
                        findSymbol = symbol;
408
                }
409

    
410
                if (findSymbol != null)
411
                    break;
412
            }
413

    
414
            return findSymbol;
415
        }
416

    
417
        public static ChildSymbol FindChildSymbolByRepresentationID(Document document, Symbol symbol, string repID)
418
        {
419
            ChildSymbol childSymbol = null;
420

    
421
            foreach (ChildSymbol loopChild in symbol.ChildSymbols)
422
            {
423
                if (loopChild.SPPID.RepresentationId == repID)
424
                {
425
                    childSymbol = loopChild;
426
                    break;
427
                }
428
                else
429
                {
430
                    childSymbol = FindChildSymbolByRepresentationIDLoop(document, repID, loopChild);
431
                    if (childSymbol != null)
432
                        break;
433
                }
434
            }
435

    
436
            return childSymbol;
437
        }
438

    
439
        private static ChildSymbol FindChildSymbolByRepresentationIDLoop(Document document, string repID, ChildSymbol childSymbol)
440
        {
441
            ChildSymbol findChild = null;
442

    
443
            foreach (var item in childSymbol.ChildSymbols)
444
            {
445
                if (item.SPPID.RepresentationId == repID)
446
                {
447
                    findChild = item;
448
                    break;
449
                }
450
                else
451
                {
452
                    findChild = FindChildSymbolByRepresentationIDLoop(document, repID, item);
453
                    if (findChild != null)
454
                        break;
455
                }
456
            }
457

    
458
            return findChild;
459
        }
460

    
461
        public static bool IsBranchLine(Line line1, Line line2)
462
        {
463
            bool result = true;
464

    
465
            Connector conn1 = line1.CONNECTORS.Find(x => x.CONNECTEDITEM == line2.UID);
466
            Connector conn2 = line2.CONNECTORS.Find(x => x.CONNECTEDITEM == line1.UID);
467

    
468
            if (conn1 != null && conn2 != null)
469
                result = false;
470

    
471
            return result;
472
        }
473

    
474
        public static bool IsBranchLine(Line line)
475
        {
476
            Connector connector = line.CONNECTORS.Find(x => 
477
            x.ConnectedObject != null && 
478
            x.ConnectedObject.GetType() == typeof(Line) && 
479
            IsBranchLine(line, x.ConnectedObject as Line));
480
            return connector != null ? true : false;
481
        }
482
        public static void CalcOverlap(double[] range1, double[] range2, ref double x, ref double y, ref bool overlapX, ref bool overlapY)
483
        {
484
            if (range1[0] <= range2[2] && range1[2] >= range2[0])
485
            {
486
                overlapX = true;
487
                x = Math.Min(Math.Abs(range1[0] - range2[2]), Math.Abs(range1[2] - range2[0]));
488
            }
489

    
490
            if (range1[1] <= range2[3] && range1[3] >= range2[1])
491
            {
492
                overlapY = true;
493
                y = Math.Min(Math.Abs(range1[1] - range2[3]), Math.Abs(range1[3] - range2[1]));
494
            }
495
        }
496

    
497
        public static void CalcNewCoordinateForSymbol(Symbol symbol, Symbol prevSymbol, double distanceX, double distanceY)
498
        {
499
            SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
500
            GridSetting _GridSetting = GridSetting.GetInstance();
501
            if (slopeType == SlopeType.HORIZONTAL)
502
            {
503
                double length = (Math.Ceiling(distanceX / _GridSetting.Length) + 1) * _GridSetting.Length;
504
                if (prevSymbol.SPPID.ORIGINAL_X < symbol.SPPID.ORIGINAL_X)
505
                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + length;
506
                else
507
                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - length;
508
            }
509
            else if (slopeType == SlopeType.VERTICAL)
510
            {
511
                double length = (Math.Ceiling(distanceY / _GridSetting.Length) + 1) * _GridSetting.Length;
512
                if (prevSymbol.SPPID.ORIGINAL_Y < symbol.SPPID.ORIGINAL_Y)
513
                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + length;
514
                else
515
                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - length;
516
            }
517
        }
518

    
519
        public static bool GetLineConnectorPoint(Line line, Connector connector, ref double x, ref double y)
520
        {
521
            bool bStart = false;
522
            int index = line.CONNECTORS.IndexOf(connector);
523
            if (index == 0)
524
            {
525
                x = line.SPPID.START_X;
526
                y = line.SPPID.START_Y;
527
                bStart = true;
528
            }
529
            else
530
            {
531
                x = line.SPPID.END_X;
532
                y = line.SPPID.END_Y;
533
            }
534
            return bStart;
535
        }
536
    }
537
}
클립보드 이미지 추가 (최대 크기: 500 MB)