프로젝트

일반

사용자정보

통계
| 개정판:

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

이력 | 보기 | 이력해설 | 다운로드 (42 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
using Llama;
14
using System.Data;
15
using Plaice;
16
using Microsoft.VisualBasic;
17
using DevExpress.XtraBars.ViewInfo;
18
using Converter.SPPID.SPPIDModel;
19

    
20
namespace Converter.SPPID.Util
21
{
22
    public enum BulkAttributeItemType
23
    {
24
        PipeRun,
25
        Symbol,
26
    }
27
    public enum SlopeType
28
    {
29
        None,
30
        Slope,
31
        HORIZONTAL,
32
        VERTICAL
33
    }
34
    public class SPPIDUtil
35
    {
36
        public static LMPipeRun PipeRun;
37
        public static List<BaseModel.SPPIDCodeList> CodeListSet;
38
        
39
        public static bool ConvertToSPPIDInfo(string jsonString)
40
        {
41
            SPPID_DBInfo _SPPIDInfo = SPPID_DBInfo.GetInstance();
42
            try
43
            {
44
                SPPID_DBInfo jsonSPPIDInfo = JsonConvert.DeserializeObject<SPPID_DBInfo>(jsonString);
45

    
46
                _SPPIDInfo.DBType = jsonSPPIDInfo.DBType;
47
                _SPPIDInfo.Service = jsonSPPIDInfo.Service;
48
                _SPPIDInfo.Site = jsonSPPIDInfo.Site;
49
                _SPPIDInfo.ServerIP = jsonSPPIDInfo.ServerIP;
50
                _SPPIDInfo.Port = jsonSPPIDInfo.Port;
51
                _SPPIDInfo.DBUser = jsonSPPIDInfo.DBUser;
52
                _SPPIDInfo.DBPassword = jsonSPPIDInfo.DBPassword;
53
                _SPPIDInfo.PlantPath = jsonSPPIDInfo.PlantPath;
54
                _SPPIDInfo.PlantDic = jsonSPPIDInfo.PlantDic;
55
                _SPPIDInfo.PlantPID = jsonSPPIDInfo.PlantPID;
56
                _SPPIDInfo.PlantPIDDic = jsonSPPIDInfo.PlantPIDDic;
57
                _SPPIDInfo.Plant = jsonSPPIDInfo.Plant;
58
                _SPPIDInfo.Enable = jsonSPPIDInfo.Enable;
59
                _SPPIDInfo.SelectedPlant = jsonSPPIDInfo.SelectedPlant;
60
                _SPPIDInfo.PlantList = jsonSPPIDInfo.PlantList;
61
                _SPPIDInfo.SQLSERVER_DBNAME = jsonSPPIDInfo.SQLSERVER_DBNAME;
62
                _SPPIDInfo.PlantDic_DataBase = jsonSPPIDInfo.PlantDic_DataBase;
63
                _SPPIDInfo.Plant_DataBase = jsonSPPIDInfo.Plant_DataBase;
64
                _SPPIDInfo.PlantPID_DataBase = jsonSPPIDInfo.PlantPID_DataBase;
65
                _SPPIDInfo.PlantPIDDic_DataBase = jsonSPPIDInfo.PlantPIDDic_DataBase;
66

    
67
            }
68
            catch (Exception ex)
69
            {
70
                _SPPIDInfo.Enable = false;
71
                return false;
72
            }
73
            return true;
74
        }
75

    
76
        public static bool ConvertToETCSetting(string jsonString)
77
        {
78
            ETCSetting _ETCSetting = ETCSetting.GetInstance();
79
            try
80
            {
81
                ETCSetting jsonETCSetting = JsonConvert.DeserializeObject<ETCSetting>(jsonString);
82

    
83
                _ETCSetting.NoteSymbolPath = jsonETCSetting.NoteSymbolPath;
84
                _ETCSetting.TextSymbolPath = jsonETCSetting.TextSymbolPath;
85
                _ETCSetting.DrainValveSize = jsonETCSetting.DrainValveSize;
86
                _ETCSetting.VendorPackageSymbolPath = jsonETCSetting.VendorPackageSymbolPath;
87
                _ETCSetting.TextLocation = jsonETCSetting.TextLocation;
88
                _ETCSetting.NoteLocation = jsonETCSetting.NoteLocation;
89
                _ETCSetting.LineNumberLocation = jsonETCSetting.LineNumberLocation;
90
                _ETCSetting.FlowMarkSymbolPath = jsonETCSetting.FlowMarkSymbolPath;
91
                _ETCSetting.BorderFilePath = jsonETCSetting.BorderFilePath;
92
                _ETCSetting.UnitSetting = jsonETCSetting.UnitSetting;
93
                _ETCSetting.BulkAttributes = jsonETCSetting.BulkAttributes;
94
            }
95
            catch (Exception ex)
96
            {
97
                return false;
98
            }
99
            return true;
100
        }
101

    
102
        public static bool ConvertToGridSetting(string jsonString)
103
        {
104
            GridSetting _GridSetting = GridSetting.GetInstance();
105
            try
106
            {
107
                GridSetting jsonGridSetting = JsonConvert.DeserializeObject<GridSetting>(jsonString);
108

    
109
                _GridSetting.UseSnapGrid = jsonGridSetting.UseSnapGrid;
110
                _GridSetting.Density = jsonGridSetting.Density;
111
                _GridSetting.Unit = jsonGridSetting.Unit;
112
                _GridSetting.DrainValveCellCount = jsonGridSetting.DrainValveCellCount;
113
            }
114
            catch (Exception ex)
115
            {
116
                return false;
117
            }
118
            return true;
119
        }
120

    
121
        public static bool ConvertPointBystring(string sPoint, ref double dX, ref double dY)
122
        {
123
            try
124
            {
125
                string[] pointArr = sPoint.Split(new char[] { ',' });
126
                if (pointArr.Length == 2)
127
                {
128
                    dX = Convert.ToDouble(pointArr[0]);
129
                    dY = Convert.ToDouble(pointArr[1]);
130
                }
131
            }
132
            catch (Exception)
133
            {
134
                dX = 0;
135
                dY = 0;
136
                return false;
137
            }
138

    
139
            return true;
140
        }
141

    
142
        public static void ConvertSPPIDPoint(ref double dX, ref double dY, double dDwgX, double dDwgY, double SPPID_Width, double SPPID_Height)
143
        {
144
            decimal calcX = 0;
145
            decimal calcY = 0;
146
            decimal tempX = Convert.ToDecimal(dX);
147
            decimal tempY = Convert.ToDecimal(dY);
148
            decimal tempWidth = Convert.ToDecimal(SPPID_Width);
149
            decimal tempHeight = Convert.ToDecimal(SPPID_Height);
150
            decimal tempDwgX = Convert.ToDecimal(dDwgX);
151
            decimal tempDwgY = Convert.ToDecimal(dDwgY);
152

    
153
            calcX = (tempX * tempWidth) / tempDwgX;
154
            calcY = tempHeight - ((tempY * tempHeight) / tempDwgY);
155
            dX = Convert.ToDouble(calcX);
156
            dY = Convert.ToDouble(calcY);
157
        }
158

    
159
        public static void ConvertGridPoint(ref double x, ref double y)
160
        {
161
            GridSetting _GridSetting = GridSetting.GetInstance();
162
            if (_GridSetting.UseSnapGrid)
163
            {
164
                if (_GridSetting.Unit == GridUnit.Inch)
165
                {
166
                    double length = _GridSetting.Density * 0.0254;
167
                    double tempX = x;
168
                    double tempY = y;
169
                    x = Math.Round(tempX / length) * length;
170
                    y = Math.Round(tempY / length) * length;
171
                }
172
            }
173
        }
174

    
175
        public static void ConvertGridPointOnlyOnePoint(ref double value)
176
        {
177
            GridSetting _GridSetting = GridSetting.GetInstance();
178
            if (_GridSetting.UseSnapGrid)
179
            {
180
                if (_GridSetting.Unit == GridUnit.Inch)
181
                {
182
                    double temp = value;
183
                    value = Math.Round(value / _GridSetting.Length) * _GridSetting.Length;
184
                }
185
            }
186
        }
187
        public static double ConvertGridPointOnlyOnePoint(double value)
188
        {
189
            GridSetting _GridSetting = GridSetting.GetInstance();
190
            if (_GridSetting.UseSnapGrid)
191
            {
192
                if (_GridSetting.Unit == GridUnit.Inch)
193
                {
194
                    double temp = value;
195
                    value = Math.Round(value / _GridSetting.Length) * _GridSetting.Length;
196
                }
197
            }
198

    
199
            return value;
200
        }
201

    
202
        public static bool EqualSpacing(double value1, double value2)
203
        {
204
            GridSetting _GridSetting = GridSetting.GetInstance();
205
            double multiplier = 100000000000000;
206
            value1 = Math.Truncate(value1 * multiplier) / multiplier;
207
            value2 = Math.Truncate(value2 * multiplier) / multiplier;
208

    
209
            return value1 == value2;
210
        }
211

    
212
        public static SlopeType CalcSlope(double x1, double y1, double x2, double y2, double degree = 15)
213
        {
214
            if (x1 - x2 == 0)
215
            {
216
                return SlopeType.VERTICAL;
217
            }
218
            else
219
            {
220
                double angle = Math.Atan(Math.Abs(y2 - y1) / Math.Abs(x2 - x1)) * 180 / Math.PI;
221
                if (angle <= degree)
222
                    return SlopeType.HORIZONTAL;
223
                else if (angle >= 90 - degree)
224
                    return SlopeType.VERTICAL;
225
                else
226
                    return SlopeType.Slope;
227
            }
228
        }
229
        public static double CalcAngle(double x1, double y1, double x2, double y2)
230
        {
231
            double result = 90;
232
            if (x1 - x2 != 0)
233
            {
234
                result = Math.Atan(Math.Abs(y2 - y1) / Math.Abs(x2 - x1)) * 180 / Math.PI;
235
            }
236
            return result;
237
        }
238
        public static double CalcLineToPointDistance(double lineX1, double lineY1, double lineX2, double lineY2, double x, double y)
239
        {
240

    
241
            double distance = 0;
242
            if (lineX1 == lineX2)
243
                distance = Math.Abs(x - lineX1);
244
            else
245
            {
246
                double a;
247
                double b;
248
                double c;
249

    
250
                a = (lineY2 - lineY1) / (lineX2 - lineX1);
251
                b = -1;
252
                c = -a * lineX1 + lineY1;
253

    
254
                distance = Math.Abs(a * x + b * y + c) / Math.Pow(a * a + b * b, 0.5);
255
            }
256
            return distance;
257
        }
258

    
259
        public static double[] CalcLineCrossingPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
260
        {
261
            double a1;
262
            double a2;
263
            double b1;
264
            double b2;
265

    
266
            if (x1 == x2)
267
                a1 = 0;
268
            else
269
                a1 = (y2 - y1) / (x2 - x1);
270
            if (x3 == x4)
271
                a2 = 0;
272
            else
273
                a2 = (y4 - y3) / (x4 - x3);
274

    
275
            b1 = -a1 * x1 + y1;
276
            b2 = -a2 * x3 + y3;
277

    
278
            if (x1 == x2 && x3 == x4)
279
            {
280
                return null;
281
            }
282
            else if (x1 == x2)
283
            {
284
                return new double[] { x1, a2*x1 + b2 };
285
            }
286
            else if (x3 == x4)
287
            {
288
                return new double[] { x3, a1 * x3 + b1 };
289
            }
290
            else
291
            {
292
                return new double[] { -(b1 - b2) / (a1 - a2), a1 * -(b1 - b2) / (a1 - a2) + b1 };
293
            }            
294
        }
295

    
296
        public static double CalcGradient(double x1, double y1, double x2, double y2)
297
        {
298
            double result = double.NaN;
299

    
300
            if (x1 != x2)
301
                result = (y2 - y1) / (x2 - x1);
302

    
303
            return result;
304
        }
305

    
306
        public static double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
307
        {
308
            return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
309
        }
310

    
311
        public static object FindObjectByUID(Document document, string UID)
312
        {
313
            if (!string.IsNullOrEmpty(UID) && UID != "None")
314
            {
315
                foreach (Symbol item in document.SYMBOLS)
316
                {
317
                    if (item.UID == UID)
318
                        return item;
319
                }
320

    
321
                foreach (Line item in document.LINES)
322
                {
323
                    if (item.UID == UID)
324
                        return item;
325
                }
326

    
327
                foreach (Text item in document.TEXTINFOS)
328
                {
329
                    if (item.UID == UID)
330
                        return item;
331
                }
332

    
333
                foreach (Note item in document.NOTES)
334
                {
335
                    if (item.UID == UID)
336
                        return item;
337
                }
338

    
339
                foreach (LineNumber item in document.LINENUMBERS)
340
                {
341
                    if (item.UID == UID)
342
                        return item;
343
                }
344

    
345
                foreach (Equipment item in document.Equipments)
346
                {
347
                    if (item.UID == UID)
348
                        return item;
349
                }
350

    
351
                foreach (SpecBreak item in document.SpecBreaks)
352
                {
353
                    if (item.UID == UID)
354
                        return item;
355
                }
356

    
357
                foreach (EndBreak item in document.EndBreaks)
358
                {
359
                    if (item.UID == UID)
360
                        return item;
361
                }
362

    
363
                foreach (VendorPackage item in document.VendorPackages)
364
                {
365
                    if (item.UID == UID)
366
                        return item;
367
                }
368
            }
369

    
370
            return null;
371
        }
372

    
373
        public static List<Line> FindLinesByModelId(Document document, string ModelItemId)
374
        {
375
            List<Line> lines = new List<Line>();
376
            foreach (Line item in document.LINES)
377
            {
378
                if (item.SPPID.ModelItemId == ModelItemId)
379
                    lines.Add(item);
380
            }
381

    
382
            return lines;
383
        }
384

    
385
        public static void FindConnectedSymbolGroup(Document document, Symbol symbol, List<Symbol> group)
386
        {
387
            if (!group.Contains(symbol))
388
                group.Add(symbol);
389

    
390
            foreach (var connector in symbol.CONNECTORS)
391
            {
392
                object connectedItem = FindObjectByUID(document, connector.CONNECTEDITEM);
393
                if (connectedItem != null && connectedItem.GetType() == typeof(Symbol))
394
                {
395
                    Symbol connSymbol = connectedItem as Symbol;
396
                    if (!group.Contains(connSymbol))
397
                    {
398
                        group.Add(connSymbol);
399
                        FindConnectedSymbolGroup(document, connSymbol, group);
400
                    }
401
                }
402
            }
403
        }
404

    
405
        public static Symbol FindCenterAtThreeSymbols(Document document, List<Symbol> group)
406
        {
407
            Symbol result = null;
408

    
409
            // Group의 가운데 찾기
410
            if (group.Count == 3)
411
            {
412
                foreach (var symbol in group)
413
                {
414
                    int count = 0;
415
                    foreach (var connector in symbol.CONNECTORS)
416
                    {
417
                        object item = FindObjectByUID(document, connector.CONNECTEDITEM);
418
                        if (item != null && item.GetType() == typeof(Symbol) && group.Contains(item as Symbol))
419
                            count++;
420
                    }
421

    
422
                    if (count == 2)
423
                    {
424
                        result = symbol;
425
                        break;
426
                    }
427
                }
428
            }
429

    
430
            return result;
431
        }
432

    
433
        private static void GetConnectedSymbol(Document document, Symbol symbol, List<Symbol> symbolGroup)
434
        {
435
            foreach (Connector connector in symbol.CONNECTORS)
436
            {
437
                object item = FindObjectByUID(document, connector.CONNECTEDITEM);
438
                if (item != null && item.GetType() == typeof(Symbol))
439
                {
440
                    Symbol connSymbol = item as Symbol;
441
                    if (connSymbol != null && !symbolGroup.Contains(connSymbol))
442
                    {
443
                        symbolGroup.Add(connSymbol);
444
                        GetConnectedSymbol(document, connSymbol, symbolGroup);
445
                    }
446
                }
447
            }
448
        }
449

    
450
        public static int SortSymbolPriority(Symbol a, Symbol b)
451
        {
452
            int resultValue = a.SPPID.ORIGINAL_Y.CompareTo(b.SPPID.ORIGINAL_Y);
453
            if (resultValue == 0)
454
                resultValue = a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
455
            return resultValue;
456
        }
457

    
458
        public static Connector FindSymbolConnectorByUID(Document document, string uid, Symbol targetSymbol)
459
        {
460
            foreach (var connector in targetSymbol.CONNECTORS)
461
            {
462
                if (connector.CONNECTEDITEM == uid)
463
                {
464
                    return connector;
465
                }
466
            }
467

    
468
            return null;
469
        }
470

    
471
        public static Symbol FindSymbolByRepresentationID(Document document, string repID)
472
        {
473
            Symbol findSymbol = null;
474
            foreach (var symbol in document.SYMBOLS)
475
            {
476
                if (symbol.SPPID.RepresentationId == repID)
477
                {
478
                    findSymbol = symbol;
479
                }
480
                else
481
                {
482
                    ChildSymbol childSymbol = FindChildSymbolByRepresentationID(document, symbol, repID);
483
                    if (childSymbol != null)
484
                        findSymbol = symbol;
485
                }
486

    
487
                if (findSymbol != null)
488
                    break;
489
            }
490

    
491
            return findSymbol;
492
        }
493

    
494
        public static ChildSymbol FindChildSymbolByRepresentationID(Document document, Symbol symbol, string repID)
495
        {
496
            ChildSymbol childSymbol = null;
497

    
498
            foreach (ChildSymbol loopChild in symbol.ChildSymbols)
499
            {
500
                if (loopChild.SPPID.RepresentationId == repID)
501
                {
502
                    childSymbol = loopChild;
503
                    break;
504
                }
505
                else
506
                {
507
                    childSymbol = FindChildSymbolByRepresentationIDLoop(document, repID, loopChild);
508
                    if (childSymbol != null)
509
                        break;
510
                }
511
            }
512

    
513
            return childSymbol;
514
        }
515

    
516
        private static ChildSymbol FindChildSymbolByRepresentationIDLoop(Document document, string repID, ChildSymbol childSymbol)
517
        {
518
            ChildSymbol findChild = null;
519

    
520
            foreach (var item in childSymbol.ChildSymbols)
521
            {
522
                if (item.SPPID.RepresentationId == repID)
523
                {
524
                    findChild = item;
525
                    break;
526
                }
527
                else
528
                {
529
                    findChild = FindChildSymbolByRepresentationIDLoop(document, repID, item);
530
                    if (findChild != null)
531
                        break;
532
                }
533
            }
534

    
535
            return findChild;
536
        }
537

    
538
        public static bool IsBranchLine(Line line1, Line line2)
539
        {
540
            bool result = true;
541

    
542
            Connector conn1 = line1.CONNECTORS.Find(x => x.CONNECTEDITEM == line2.UID);
543
            Connector conn2 = line2.CONNECTORS.Find(x => x.CONNECTEDITEM == line1.UID);
544

    
545
            if (conn1 != null && conn2 != null)
546
                result = false;
547

    
548
            return result;
549
        }
550

    
551
        public static bool IsBranchLine(Line line)
552
        {
553
            Connector connector = line.CONNECTORS.Find(x =>
554
            x.ConnectedObject != null &&
555
            x.ConnectedObject.GetType() == typeof(Line) &&
556
            IsBranchLine(line, x.ConnectedObject as Line));
557
            return connector != null ? true : false;
558
        }
559

    
560
        public static bool IsBranchedLine(Document document,Line line)
561
        {
562
            return document.LINES.Find(x => x.CONNECTORS.Find(y => y.CONNECTEDITEM == line.UID && IsBranchLine(x, y.ConnectedObject as Line)) != null) != null ? true : false;
563
        }
564

    
565
        public static void CalcOverlap(double[] range1, double[] range2, ref double x, ref double y, ref bool overlapX, ref bool overlapY)
566
        {
567
            if (range1[0] <= range2[2] && range1[2] >= range2[0])
568
            {
569
                overlapX = true;
570
                x = Math.Min(Math.Abs(range1[0] - range2[2]), Math.Abs(range1[2] - range2[0]));
571
            }
572

    
573
            if (range1[1] <= range2[3] && range1[3] >= range2[1])
574
            {
575
                overlapY = true;
576
                y = Math.Min(Math.Abs(range1[1] - range2[3]), Math.Abs(range1[3] - range2[1]));
577
            }
578
        }
579

    
580
        public static bool IsOverlap(double[] range1, double[] range2)
581
        {
582
            if (range1[0] <= range2[2] && range1[2] >= range2[0] && range1[1] <= range2[3] && range1[3] >= range2[1])
583
                return true;
584
            else
585
                return false;
586
        }
587

    
588
        public static void CalcNewCoordinateForSymbol(Symbol symbol, Symbol prevSymbol, double distanceX, double distanceY)
589
        {
590
            SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
591
            GridSetting _GridSetting = GridSetting.GetInstance();
592
            if (slopeType == SlopeType.HORIZONTAL)
593
            {
594
                double length = (Math.Ceiling(distanceX / _GridSetting.Length) + 1) * _GridSetting.Length;
595
                if (prevSymbol.SPPID.ORIGINAL_X < symbol.SPPID.ORIGINAL_X)
596
                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + length;
597
                else
598
                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - length;
599
            }
600
            else if (slopeType == SlopeType.VERTICAL)
601
            {
602
                double length = (Math.Ceiling(distanceY / _GridSetting.Length) + 1) * _GridSetting.Length;
603
                if (prevSymbol.SPPID.ORIGINAL_Y < symbol.SPPID.ORIGINAL_Y)
604
                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + length;
605
                else
606
                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - length;
607
            }
608
        }
609

    
610
        public static Line GetConnectedLine(Symbol symbol1, Symbol symbol2)
611
        {
612
            Line line = null;
613
            Connector connector1 = symbol1.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Line) &&
614
            ((Line)x.ConnectedObject).CONNECTORS.Find(y => y.ConnectedObject == symbol2) != null);
615
            Connector connector2 = symbol2.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Line) &&
616
            ((Line)x.ConnectedObject).CONNECTORS.Find(y => y.ConnectedObject == symbol1) != null);
617
            if (connector1 != null && connector2 != null)
618
                line = connector1.ConnectedObject as Line;
619

    
620
            return line;
621
        }
622

    
623
        public static int GetBranchLineCount(Document document, Line line)
624
        {
625
            return document.LINES.FindAll(x => IsBranchLine(line, x) && x.CONNECTORS.Find(y => y.ConnectedObject == line) != null).Count;
626
        }
627

    
628
        public static bool GetLineConnectorPoint(Line line, Connector connector, ref double x, ref double y)
629
        {
630
            bool bStart = false;
631
            int index = line.CONNECTORS.IndexOf(connector);
632
            if (index == 0)
633
            {
634
                x = line.SPPID.START_X;
635
                y = line.SPPID.START_Y;
636
                bStart = true;
637
            }
638
            else
639
            {
640
                x = line.SPPID.END_X;
641
                y = line.SPPID.END_Y;
642
            }
643
            return bStart;
644
        }
645

    
646
        public static bool IsSegment(Document document, Line line1, Line line2)
647
        {
648
            bool result = false;
649

    
650
            SpecBreak specBreak = document.SpecBreaks.Find(x =>
651
                (x.DownStreamUID == line1.UID && x.UpStreamUID == line2.UID) || 
652
                (x.DownStreamUID == line2.UID && x.UpStreamUID == line1.UID));
653

    
654
            EndBreak endBreak = document.EndBreaks.Find(x =>
655
            (x.OWNER == line1.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == line2.UID) ||
656
            (x.OWNER == line2.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == line1.UID));
657

    
658
            if (specBreak != null || endBreak != null)
659
                result = true;
660

    
661
            return result;
662
        }
663

    
664
        public static bool IsSegment(Document document, Symbol symbol1, Symbol symbol2)
665
        {
666
            bool result = false;
667
            SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
668
            (x.DownStreamUID == symbol1.UID && x.UpStreamUID == symbol2.UID) ||
669
            (x.DownStreamUID == symbol2.UID && x.UpStreamUID == symbol1.UID));
670

    
671
            EndBreak startEndBreak = document.EndBreaks.Find(x =>
672
            (x.OWNER == symbol1.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol2.UID) ||
673
            (x.OWNER == symbol2.UID && x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol1.UID));
674

    
675
            if (startSpecBreak != null || startEndBreak != null)
676
                result = true;
677

    
678
            return result;
679
        }
680
        public static void test()
681
        {
682
            Placement placement = new Placement();
683
            LMADataSource dataSource = new LMADataSource();
684
            LMSymbol symbol = dataSource.GetSymbol("BBAB475FA9AF4C01AACF4C51F56241F8");
685

    
686
            string[] array1 = new string[] { "", "Top" };
687
            string[] array2 = new string[] { "", "0.005" };
688
            placement.PIDApplyParameters(symbol.AsLMRepresentation(), array1, array2);
689

    
690
            ReleaseCOMObjects(dataSource);
691
        }
692
        public static DataTable GetUnitInfo()
693
        {
694
            LMADataSource dataSource = new LMADataSource();
695
            LMPlantGroups units = new LMPlantGroups();
696
            units.Collect(dataSource);
697
            DataTable dt = new DataTable();
698
            dt.Columns.Add("LEVEL", typeof(string));
699
            dt.Columns.Add("SP_ID", typeof(string));
700
            dt.Columns.Add("PARENTID", typeof(string));
701
            dt.Columns.Add("NAME", typeof(string));
702

    
703
            foreach (LMPlantGroup unit in units)
704
            {
705
                object tt = unit.get_Dir_Path();
706
                if (tt.Equals(DBNull.Value))
707
                    continue;
708
                string dir = unit.get_Dir_Path();
709
                string[] array = dir.Split('\\');
710
                List<string> parents = new List<string>();
711
                string prevId = "-1";
712
                for (int i = 1; i < array.Length; i++)
713
                {
714
                    string name = array[i];
715
                    string uid = Guid.NewGuid().ToString();
716
                    DataRow[] rows = dt.Select(string.Format("LEVEL = '{0}' AND NAME = '{1}' AND PARENTID = '{2}'", i, name, prevId));
717
                    if (rows.Length == 0)
718
                        dt.Rows.Add(i, uid, prevId, name);
719
                    else
720
                        uid = rows[0]["SP_ID"].ToString();
721
                    prevId = uid;
722
                }
723
            }
724

    
725
            dt.Columns.Remove("LEVEL");
726

    
727
            ReleaseCOMObjects(units);
728
            ReleaseCOMObjects(dataSource);
729

    
730
            return dt;
731
        }
732
        public static string T_OPTIONSETTING_Value(string name)
733
        {
734
            string result = string.Empty;
735
            LMADataSource dataSource = new LMADataSource();
736
            LMOptionSettings settings = new LMOptionSettings();
737

    
738
            LMAFilter filter = new LMAFilter();
739
            LMACriterion criterion = new LMACriterion();
740
            filter.ItemType = "OptionSetting";
741
            criterion.SourceAttributeName = "NAME";
742
            criterion.Operator = "=";
743
            criterion.set_ValueAttribute(name);
744
            filter.get_Criteria().Add(criterion);
745
            settings.Collect(dataSource, Filter: filter);
746

    
747
            foreach (LMOptionSetting item in settings)
748
            {
749
                result = item.get_Value();
750
                break;
751
            }
752

    
753
            ReleaseCOMObjects(criterion);
754
            ReleaseCOMObjects(filter);
755
            ReleaseCOMObjects(settings);
756
            ReleaseCOMObjects(dataSource);
757
            return result;
758
        }
759

    
760
        public static string T_PLANTSETTING_Value(string name)
761
        {
762
            string result = string.Empty;
763
            LMADataSource dataSource = new LMADataSource();
764
            LMPlantSettings settings = new LMPlantSettings();
765

    
766
            LMAFilter filter = new LMAFilter();
767
            LMACriterion criterion = new LMACriterion();
768
            filter.ItemType = "PlantSetting";
769
            criterion.SourceAttributeName = "NAME";
770
            criterion.Operator = "=";
771
            criterion.set_ValueAttribute(name);
772
            filter.get_Criteria().Add(criterion);
773
            settings.Collect(dataSource, Filter: filter);
774

    
775
            foreach (LMPlantSetting item in settings)
776
            {
777
                result = item.get_Value();
778
                break;
779
            }
780

    
781
            ReleaseCOMObjects(criterion);
782
            ReleaseCOMObjects(filter);
783
            ReleaseCOMObjects(settings);
784
            ReleaseCOMObjects(dataSource);
785
            return result;
786
        }
787

    
788
        public static void ReleaseCOMObjects(params object[] objVars)
789
        {
790
            int intNewRefCount = 0;
791
            foreach (object obj in objVars)
792
            {
793
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
794
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
795
            }
796
        }
797

    
798
        public static void BulkAttribute(LMADataSource dataSource, List<BulkAttribute> attributes, BulkAttributeItemType type)
799
        {
800
            switch (type)
801
            {
802
                case BulkAttributeItemType.PipeRun:
803
                    BulkAttribute_PipeRun();
804
                    break;
805
                case BulkAttributeItemType.Symbol:
806
                    BulkAttribute_Symbol();
807
                    break;
808
                default:
809
                    break;
810
            }
811
            void BulkAttribute_PipeRun()
812
            {
813
                string drawingId = dataSource.PIDMgr.Drawing.ID;
814

    
815
                LMAFilter filter = new LMAFilter();
816

    
817
                LMACriterion criterion = new LMACriterion();
818
                criterion.SourceAttributeName = "REPRESENTATION.SP_DRAWINGID";
819
                criterion.Operator = "=";
820
                criterion.set_ValueAttribute(drawingId);
821
                filter.get_Criteria().Add(criterion);
822
                filter.ItemType = "PIPERUN";
823

    
824
                LMACriterion criterion2 = new LMACriterion();
825
                criterion2.SourceAttributeName = "REPRESENTATION.INSTOCKPILE";
826
                criterion2.Operator = "=";
827
                criterion2.set_ValueAttribute("1");
828
                criterion2.Conjunctive = true;
829
                filter.get_Criteria().Add(criterion2);
830

    
831
                LMACriterion criterion3 = new LMACriterion();
832
                criterion3.SourceAttributeName = "REPRESENTATION.ITEMSTATUS";
833
                criterion3.Operator = "=";
834
                criterion3.set_ValueAttribute("1");
835
                criterion3.Conjunctive = true;
836
                filter.get_Criteria().Add(criterion3);
837

    
838
                LMPipeRuns items = new LMPipeRuns();
839
                items.Collect(dataSource, Filter: filter);
840

    
841
                foreach (LMPipeRun item in items)
842
                {
843
                    foreach (var attribute in attributes)
844
                    {
845
                        if (item.Attributes[attribute.SPPIDAttributeName] != null)
846
                            item.Attributes[attribute.SPPIDAttributeName].set_Value(attribute.Value);
847
                    }
848
                    item.Commit();
849
                }
850

    
851
                ReleaseCOMObjects(items);
852
                ReleaseCOMObjects(filter);
853
                ReleaseCOMObjects(criterion);
854
                ReleaseCOMObjects(criterion2);
855
                ReleaseCOMObjects(criterion3);
856
            }
857
            void BulkAttribute_Symbol()
858
            {
859
                string drawingId = dataSource.PIDMgr.Drawing.ID;
860

    
861
                LMAFilter filter = new LMAFilter();
862

    
863
                LMACriterion criterion = new LMACriterion();
864
                criterion.SourceAttributeName = "SP_DRAWINGID";
865
                criterion.Operator = "=";
866
                criterion.set_ValueAttribute(drawingId);
867
                filter.get_Criteria().Add(criterion);
868
                filter.ItemType = "SYMBOL";
869

    
870
                LMACriterion criterion2 = new LMACriterion();
871
                criterion2.SourceAttributeName = "INSTOCKPILE";
872
                criterion2.Operator = "=";
873
                criterion2.set_ValueAttribute("1");
874
                criterion2.Conjunctive = true;
875
                filter.get_Criteria().Add(criterion2);
876

    
877
                LMACriterion criterion3 = new LMACriterion();
878
                criterion3.SourceAttributeName = "ITEMSTATUS";
879
                criterion3.Operator = "=";
880
                criterion3.set_ValueAttribute("1");
881
                criterion3.Conjunctive = true;
882
                filter.get_Criteria().Add(criterion3);
883

    
884
                LMSymbols items = new LMSymbols();
885
                items.Collect(dataSource, Filter: filter);
886

    
887
                foreach (LMSymbol item in items)
888
                {
889
                    foreach (var attribute in attributes)
890
                    {
891
                        if (item.Attributes[attribute.SPPIDAttributeName] != null)
892
                            item.Attributes[attribute.SPPIDAttributeName].set_Value(attribute.Value);
893
                    }
894
                    item.Commit();
895
                }
896

    
897
                ReleaseCOMObjects(items);
898
                ReleaseCOMObjects(filter);
899
                ReleaseCOMObjects(criterion);
900
                ReleaseCOMObjects(criterion2);
901
                ReleaseCOMObjects(criterion3);
902
            }
903
        }
904
        public static void UpdatePipeRunProperty(LMADataSource dataSource, Model.SPPID_PipeRunPropSetting setting)
905
        {
906
            string drawingId = dataSource.PIDMgr.Drawing.ID;
907
            string type = setting.DataType.Substring(0, 1);
908
            LMAFilter filter = new LMAFilter();
909
            filter.ItemType = "PIPERUN";
910

    
911
            LMACriterion criterionID = new LMACriterion();
912
            criterionID.SourceAttributeName = "REPRESENTATION.SP_DRAWINGID";
913
            criterionID.Operator = "=";
914
            criterionID.set_ValueAttribute(drawingId);
915
            filter.get_Criteria().Add(criterionID);
916

    
917
            SetCriterionPipeRun(setting.Cond1_Name, setting.Cond1_Value, setting.Cond1_DataType);
918
            if (!string.IsNullOrWhiteSpace(setting.Cond2_Name))
919
                SetCriterionPipeRun(setting.Cond2_Name, setting.Cond2_Value, setting.Cond2_DataType);
920

    
921
            LMPipeRuns items = new LMPipeRuns();
922
            items.Collect(dataSource, Filter: filter);
923

    
924
            foreach (LMPipeRun item in items)
925
            {
926
                if (item.Attributes[setting.Name] != null)
927
                {
928
                    object oValue = null;
929
                    if (type == "S" || type == "C")
930
                        oValue = setting.Value;
931
                    else
932
                        oValue = Convert.ToDouble(setting.Value);
933

    
934
                    item.Attributes[setting.Name].set_Value(oValue);
935
                }
936
                try
937
                {
938
                    item.Commit();
939
                }
940
                catch (Exception ex)
941
                {
942
                }
943
            }
944

    
945
            ReleaseCOMObjects(items);
946
            ReleaseCOMObjects(filter);
947

    
948
            void SetCriterionPipeRun(string condName, string condValue, string condDataType)
949
            {
950
                if (condValue.Contains("~"))
951
                {
952
                    string[] value_Arr = condValue.Split('~');
953

    
954
                    object fromValue = GetPipeRunSiValue(dataSource, condName, value_Arr[0]);
955
                    if (fromValue.GetType() == typeof(double))
956
                    {
957
                        double dfrom = Convert.ToDouble(fromValue);
958
                        dfrom -= 0.000000000001;
959
                        fromValue = dfrom;
960
                    }
961
                    object toValue = null;
962
                    if (value_Arr.Length > 1)
963
                    {
964
                        toValue = GetPipeRunSiValue(dataSource, condName, value_Arr[1]);
965
                        if (toValue.GetType() == typeof(double))
966
                        {
967
                            double dTo = Convert.ToDouble(toValue);
968
                            dTo += 0.000000000001;
969
                            toValue = dTo;
970
                        }
971
                    }
972

    
973
                    LMACriterion criterionFrom = new LMACriterion();
974
                    criterionFrom.SourceAttributeName = condName;
975
                    criterionFrom.Operator = ">";
976
                    criterionFrom.set_ValueAttribute(fromValue);
977
                    criterionFrom.Conjunctive = true;
978
                    filter.get_Criteria().Add(criterionFrom);
979
                    ReleaseCOMObjects(criterionFrom);
980

    
981
                    if (toValue != null)
982
                    {
983
                        LMACriterion criterionTo = new LMACriterion();
984
                        criterionTo.SourceAttributeName = condName;
985
                        criterionTo.Operator = "<";
986
                        criterionTo.set_ValueAttribute(toValue);
987
                        criterionTo.Conjunctive = true;
988
                        filter.get_Criteria().Add(criterionTo);
989

    
990
                        ReleaseCOMObjects(criterionTo);
991
                    }
992
                }
993
                else
994
                {
995
                    LMACriterion criterion = new LMACriterion();
996
                    criterion.SourceAttributeName = condName;
997
                    criterion.Operator = "=";
998
                    double dValue = 0;
999
                    object oValue = condValue;
1000
                    if (condDataType.StartsWith("C"))
1001
                    {
1002
                        oValue = GetCodeIndex(condDataType, condValue.Trim());
1003
                    }
1004
                    else if (double.TryParse(condValue.Trim(), out dValue))
1005
                    {
1006
                        oValue = dValue;
1007
                    }
1008
                    else
1009
                    {
1010
                        oValue = GetPipeRunSiValue(dataSource, condName, condValue);
1011
                    }
1012
                    criterion.set_ValueAttribute(oValue);
1013
                    criterion.Conjunctive = true;
1014
                    filter.get_Criteria().Add(criterion);
1015
                    ReleaseCOMObjects(criterion);
1016
                }
1017
                ReleaseCOMObjects(criterionID);
1018
            }
1019
        }
1020

    
1021
        private static object GetPipeRunSiValue(LMADataSource dataSource, string attributeName, string displayValue)
1022
        {
1023
            object valueObject = null;
1024

    
1025
            if (PipeRun == null)
1026
            {
1027
                string drawingId = dataSource.PIDMgr.Drawing.ID;
1028
                LMAFilter filter = new LMAFilter();
1029
                LMACriterion criterion = new LMACriterion();
1030
                criterion.SourceAttributeName = "REPRESENTATION.SP_DRAWINGID";
1031
                criterion.Operator = "=";
1032
                criterion.set_ValueAttribute(drawingId);
1033
                filter.get_Criteria().Add(criterion);
1034
                filter.ItemType = "PIPERUN";
1035

    
1036
                LMPipeRuns items = new LMPipeRuns();
1037
                items.Collect(dataSource, Filter: filter);
1038

    
1039
                foreach (LMPipeRun run in items)
1040
                {
1041
                    PipeRun = run;
1042
                    break;
1043
                }
1044
                ReleaseCOMObjects(criterion);
1045
                ReleaseCOMObjects(criterion);
1046
                ReleaseCOMObjects(items);
1047
            }
1048

    
1049
            if (PipeRun != null)
1050
            {
1051
                if (PipeRun.Attributes[attributeName] != null)
1052
                {
1053
                    object prevValue = PipeRun.Attributes[attributeName].get_Value();
1054
                    PipeRun.Attributes[attributeName].set_Value(displayValue);
1055
                    valueObject = PipeRun.Attributes[attributeName].SIValue;                    
1056
                    PipeRun.Attributes[attributeName].set_Value(prevValue);
1057
                }
1058
            }
1059

    
1060
            if (valueObject == null)
1061
            {
1062
                valueObject = displayValue;
1063
            }
1064
            return valueObject;
1065
        }
1066

    
1067
        private static void InitCodeList()
1068
        {
1069
            if (CodeListSet == null || CodeListSet.Count == 0)
1070
            {
1071
                CodeListSet = new List<SPPIDCodeList>();
1072
                DataTable dt = SPPID_DB.GetSPPIDCodeList();
1073
                try
1074
                {
1075
                    Plaice.Placement placement = new Plaice.Placement();
1076
                    LMADataSource dataSource = placement.PIDDataSource;
1077
                    foreach (LMAEnumAttList item in dataSource.CodeLists)
1078
                    {
1079
                        DataRow[] rows = dt.Select(string.Format("ATTRIBUTE_DATATYPE = '{0}'", item.Name));
1080
                        foreach (DataRow row in rows)
1081
                        {
1082
                            string name = row["ATTRIBUTE_NAME"].ToString();
1083
                            if (CodeListSet.FirstOrDefault(x => x.Name.Equals(name)) == null)
1084
                            {
1085
                                SPPIDCodeList code = new SPPIDCodeList();
1086
                                code.Name = name;
1087
                                code.CodeName = item.Name;
1088
                                int nullIndex = item.EnumeratedAttributes.GetItemIndex("");
1089
                                if (nullIndex >= 0)
1090
                                {
1091
                                    code.Values.Add(nullIndex, "");
1092
                                }
1093
                                foreach (LMAEnumeratedAttribute attr in item.EnumeratedAttributes)
1094
                                {
1095
                                    if (attr.Name == "") continue;
1096
                                    code.Values.Add(attr.Index, attr.Name);
1097
                                }
1098

    
1099
                                CodeListSet.Add(code);
1100
                            }
1101
                        }
1102
                        SPPIDUtil.ReleaseCOMObjects(item);
1103
                    }
1104
                    SPPIDUtil.ReleaseCOMObjects(dataSource);
1105
                    SPPIDUtil.ReleaseCOMObjects(placement);
1106
                }
1107
                catch (Exception ex)
1108
                {
1109
                    MessageBox.Show(ex.Message);
1110
                }
1111
            }
1112
        }
1113
        public static SPPIDCodeList GetCodeList(string codeName)
1114
        {
1115
            InitCodeList();
1116
            SPPIDCodeList findCodeList = CodeListSet.FirstOrDefault(f => f.CodeName == codeName);
1117
            if (findCodeList == null)
1118
            {
1119
                findCodeList = CodeListSet.FirstOrDefault(f => f.Name == codeName);
1120
            }
1121

    
1122
            return findCodeList;
1123
        }
1124
        public static int GetCodeIndex(string codeName, string codeText)
1125
        {
1126
            int codeIndex = -1;
1127
            InitCodeList();
1128
            SPPIDCodeList findCodeList = CodeListSet.FirstOrDefault(f => f.CodeName == codeName);
1129
            if (findCodeList != null && findCodeList.Values.ContainsValue(codeText))
1130
            {
1131
                codeIndex = findCodeList.Values.Where(w => w.Value == codeText).Select(s => s.Key).FirstOrDefault();
1132
            }
1133
            return codeIndex;
1134
        }
1135
    }
1136
}
클립보드 이미지 추가 (최대 크기: 500 MB)