프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / Util / SPPIDUtil.cs @ 404185fc

이력 | 보기 | 이력해설 | 다운로드 (17.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.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
            //calcX = Math.Truncate(calcX * 1000) / 1000;
131
            //calcY = tempHeight - ((tempY * tempHeight) / tempDwgY);
132
            //calcY = Math.Truncate(calcY * 1000) / 1000;
133
            //dX = Math.Round(Convert.ToDouble(calcX), 10);
134
            //dY = Math.Round(Convert.ToDouble(calcY), 10);
135

    
136
            calcX = (tempX * tempWidth) / tempDwgX;
137
            calcY = tempHeight - ((tempY * tempHeight) / tempDwgY);
138
            dX = Convert.ToDouble(calcX);
139
            dY = Convert.ToDouble(calcY);
140
        }
141

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

    
158
        public static void ConvertGridPointOnlyOnePoint(ref double value)
159
        {
160
            GridSetting _GridSetting = GridSetting.GetInstance();
161
            if (_GridSetting.UseSnapGrid)
162
            {
163
                if (_GridSetting.Unit == GridUnit.Inch)
164
                {
165
                    double temp = value;
166
                    value = Math.Round(value / _GridSetting.Length) * _GridSetting.Length;
167
                }
168
            }
169
        }
170

    
171
        public static SlopeType CalcSlope(double x1, double y1, double x2, double y2)
172
        {
173
            if (x1 - x2 == 0)
174
            {
175
                return SlopeType.VERTICAL;
176
            }
177
            else
178
            {
179
                double angle = Math.Atan(Math.Abs(y2 - y1) / Math.Abs(x2 - x1)) * 180 / Math.PI;
180
                if (angle <= 15)
181
                    return SlopeType.HORIZONTAL;
182
                else if (angle >= 75)
183
                    return SlopeType.VERTICAL;
184
                else
185
                    return SlopeType.Slope;
186
            }
187
        }
188

    
189
        public static double CalcLineToPointDistance(double lineX1, double lineY1, double lineX2, double lineY2, double x, double y)
190
        {
191

    
192
            double distance = 0;
193
            if (lineX1 == lineX2)
194
                distance = Math.Abs(x - lineX1);
195
            else
196
            {
197
                double a;
198
                double b;
199
                double c;
200

    
201
                a = (lineY2 - lineY1) / (lineX2 - lineX1);
202
                b = -1;
203
                c = -a * lineX1 + lineY1;
204

    
205
                distance = Math.Abs(a * x + b * y + c) / Math.Pow(a * a + b * b, 0.5);
206
            }
207
            return distance;
208
        }
209

    
210
        public static double[] CalcLineCrossingPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
211
        {
212
            x1 = Math.Round(x1, 10);
213
            x2 = Math.Round(x2, 10);
214
            x3 = Math.Round(x3, 10);
215
            x4 = Math.Round(x4, 10);
216

    
217
            y1 = Math.Round(y1, 10);
218
            y2 = Math.Round(y2, 10);
219
            y3 = Math.Round(y3, 10);
220
            y4 = Math.Round(y4, 10);
221

    
222
            double a1;
223
            double a2;
224
            double b1;
225
            double b2;
226

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

    
236
            b1 = -a1 * x1 + y1;
237
            b2 = -a2 * x3 + y3;
238

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

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

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

    
273
                foreach (Line item in document.LINES)
274
                {
275
                    if (item.UID == UID)
276
                        return item;
277
                }
278

    
279
                foreach (Text item in document.TEXTINFOS)
280
                {
281
                    if (item.UID == UID)
282
                        return item;
283
                }
284

    
285
                foreach (Note item in document.NOTES)
286
                {
287
                    if (item.UID == UID)
288
                        return item;
289
                }
290

    
291
                foreach (LineNumber item in document.LINENUMBERS)
292
                {
293
                    if (item.UID == UID)
294
                        return item;
295
                }
296

    
297
                foreach (Equipment item in document.Equipments)
298
                {
299
                    if (item.UID == UID)
300
                        return item;
301
                }
302
            }
303

    
304
            return null;
305
        }
306

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

    
316
            return lines;
317
        }
318

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

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

    
339
        public static Symbol FindCenterAtThreeSymbols(Document document, List<Symbol> group)
340
        {
341
            Symbol result = null;
342

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

    
356
                    if (count == 2)
357
                    {
358
                        result = symbol;
359
                        break;
360
                    }
361
                }
362
            }
363

    
364
            return result;
365
        }
366

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

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

    
394
            return null;
395
        }
396

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

    
413
                if (findSymbol != null)
414
                    break;
415
            }
416

    
417
            return findSymbol;
418
        }
419

    
420
        public static ChildSymbol FindChildSymbolByRepresentationID(Document document, Symbol symbol, string repID)
421
        {
422
            ChildSymbol childSymbol = null;
423

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

    
439
            return childSymbol;
440
        }
441

    
442
        private static ChildSymbol FindChildSymbolByRepresentationIDLoop(Document document, string repID, ChildSymbol childSymbol)
443
        {
444
            ChildSymbol findChild = null;
445

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

    
461
            return findChild;
462
        }
463

    
464
        public static bool IsBranchLine(Line line1, Line line2)
465
        {
466
            bool result = true;
467

    
468
            Connector conn1 = line1.CONNECTORS.Find(x => x.CONNECTEDITEM == line2.UID);
469
            Connector conn2 = line2.CONNECTORS.Find(x => x.CONNECTEDITEM == line1.UID);
470

    
471
            if (conn1 != null && conn2 != null)
472
                result = false;
473

    
474
            return result;
475
        }
476

    
477
        public static void CalcOverlap(double[] range1, double[] range2, ref double x, ref double y, ref bool overlapX, ref bool overlapY)
478
        {
479
            if (range1[0] <= range2[2] && range1[2] >= range2[0])
480
            {
481
                overlapX = true;
482
                x = Math.Min(Math.Abs(range1[0] - range2[2]), Math.Abs(range1[2] - range2[0]));
483
            }
484

    
485
            if (range1[1] <= range2[3] && range1[3] >= range2[1])
486
            {
487
                overlapY = true;
488
                y = Math.Min(Math.Abs(range1[1] - range2[3]), Math.Abs(range1[3] - range2[1]));
489
            }
490
        }
491

    
492
        public static void CalcNewCoordinateForSymbol(Symbol symbol, Symbol prevSymbol, double distanceX, double distanceY)
493
        {
494
            SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
495
            GridSetting _GridSetting = GridSetting.GetInstance();
496
            if (slopeType == SlopeType.HORIZONTAL)
497
            {
498
                double length = (Math.Ceiling(distanceX / _GridSetting.Length) + 1) * _GridSetting.Length;
499
                if (prevSymbol.SPPID.ORIGINAL_X < symbol.SPPID.ORIGINAL_X)
500
                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + length;
501
                else
502
                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - length;
503
            }
504
            else if (slopeType == SlopeType.VERTICAL)
505
            {
506
                double length = (Math.Ceiling(distanceY / _GridSetting.Length) + 1) * _GridSetting.Length;
507
                if (prevSymbol.SPPID.ORIGINAL_Y < symbol.SPPID.ORIGINAL_Y)
508
                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + length;
509
                else
510
                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - length;
511
            }
512
        }
513
    }
514
}
클립보드 이미지 추가 (최대 크기: 500 MB)