프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ 804a7641

이력 | 보기 | 이력해설 | 다운로드 (43.8 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Data;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Windows.Forms;
10
using System.Threading;
11
using System.IO;
12
using Microsoft.VisualBasic;
13
using Ingr.RAD2D;
14
using Converter.BaseModel;
15
using Converter.SPPID.Properties;
16
using Converter.SPPID.DB;
17
using Converter.SPPID.Util;
18
using Converter.SPPID.Form;
19
using Converter.SPPID.Model;
20
using Plaice;
21
using Llama;
22
using DevExpress.XtraSplashScreen;
23
using Newtonsoft.Json;
24
using System.Runtime.InteropServices;
25
using System.Reflection;
26
using Converter.SPPID.OPC;
27

    
28
namespace Converter.SPPID.Wrapper
29
{
30
    public partial class ConverterDocking : UserControl
31
    {
32
        Ingr.RAD2D.Application application;
33
        static bool addEvent = false;
34
        public ConverterDocking()
35
        {
36
            InitializeComponent();
37
            spinEditSymmetry.Properties.Mask.EditMask = "f0";
38
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
39
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
40
            application = wApp.RADApplication;
41
            
42
            ReleaseCOMObjects(dApplication);
43
            dApplication = null;
44
            try
45
            {
46
                textEditDrawingX.EditValue = Settings.Default.DrawingX;
47
                textEditDrawingY.EditValue = Settings.Default.DrawingY;
48

    
49
                Project_Info _ProjectInfo = Project_Info.GetInstance();
50
                _ProjectInfo.DefaultPath = Settings.Default.ProjectPath;
51
                _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType;
52
                _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP;
53
                _ProjectInfo.Port = Settings.Default.ProjectPort;
54
                _ProjectInfo.DBUser = Settings.Default.ProjectDBUser;
55
                _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword;
56

    
57
                if (!addEvent)
58
                {
59
                    application.EventObject.BeforeApplicationExit += ApplicationEvents_ApplicationExit;
60
                    addEvent = true;
61
                }
62
                RegisterHotKey(this.Handle, 0, (int)KeyModifier.Shift, Keys.A.GetHashCode());
63
            }
64
            catch (Exception ex)
65
            {
66
                StringBuilder sb = new StringBuilder();
67
                sb.AppendLine(ex.Message);
68
                sb.AppendLine(ex.StackTrace);
69
                MessageBox.Show(sb.ToString());
70
            }
71
        }
72
        private void ApplicationEvents_ApplicationExit(out bool cancel)
73
        {
74
            cancel = false;
75
        }
76
        /// <summary>
77
        /// 선택한 도면들을 읽어서 SPPID로 변환한다.
78
        /// </summary>
79
        /// <param name="sender"></param>
80
        /// <param name="e"></param>
81
        private void btnConverter_Click(object sender, EventArgs e)
82
        {
83
            ConverterForm converterForm = new ConverterForm();
84
            if (converterForm.ShowDialog() == DialogResult.OK)
85
            {
86
                //Ingr.RAD2D.Document doc = application.Documents.Add();
87

    
88
                try
89
                {
90
                    CloseOPCForm.Run();
91

    
92
                    for (int i = 0; i < converterForm.Documents.Count; i++)
93
                    {
94
                        SPPID_Document document = converterForm.Documents[i];
95
                        if (document.SetSPPIDMapping() && document.Enable)
96
                        {
97
                            using (AutoModeling modeling = new AutoModeling(document, converterForm.checkEditCloseDocument.Checked))
98
                            {
99
                                modeling.DocumentLabelText = string.Format("Drawing Name : {0} ({1}/{2})", document.DrawingName, i + 1, converterForm.Documents.Count);
100
                                modeling.Run();
101
                            }
102
                        }
103
                    }
104
                }
105
                catch (Exception ex)
106
                {
107
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
108
                }
109
                finally
110
                {
111
                    CloseOPCForm.Stop();
112

    
113
                    //doc.SaveOnClose = false;
114
                    //doc.Close(false);
115

    
116
                    //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
117
                    //dApplication.Drawings[1].CloseDrawing(false);
118
                    //ReleaseCOMObjects(dApplication);
119
                    //dApplication = null;
120
                }
121

    
122
                MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
123
            }
124
        }
125

    
126
        private void btnLinkOPC_Click(object sender, EventArgs e)
127
        {
128
            DataTable tOPCInfo = Project_DB.SelectOPCInfo();
129
            DataTable tOPCRelations = Project_DB.SelectOPCRelations();
130
            DataTable tDrawingInfo = Project_DB.SelectDrawingInfo();
131
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
132

    
133
            foreach (DataRow row in tOPCInfo.Rows)
134
            {
135
                if (!Convert.ToBoolean(row["PAIRED"]))
136
                {
137
                    string drawingUID = row["ID2_DRAWING_UID"].ToString();
138
                    string OPCUID = row["ID2_OPC_UID"].ToString();
139
                    DataRow[] rows = tOPCRelations.Select(string.Format("(From_Drawings_UID = '{0}' AND From_OPC_UID = '{1}') OR (To_Drawings_UID = '{0}' AND To_OPC_UID = '{1}')", drawingUID, OPCUID));
140

    
141
                    if (rows.Length == 2)
142
                    {
143
                        string fromDrawingsUID1 = rows[0]["From_Drawings_UID"].ToString();
144
                        string fromDrawingsUID2 = rows[1]["From_Drawings_UID"].ToString();
145
                        string fromOPCUID1 = rows[0]["From_OPC_UID"].ToString();
146
                        string fromOPCUID2 = rows[1]["From_OPC_UID"].ToString();
147
                        string toDrawingsUID1 = rows[0]["To_Drawings_UID"].ToString();
148
                        string toDrawingsUID2 = rows[1]["To_Drawings_UID"].ToString();
149
                        string toOPCUID1 = rows[0]["To_OPC_UID"].ToString();
150
                        string toOPCUID2 = rows[1]["To_OPC_UID"].ToString();
151

    
152
                        DataRow[] fromDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", fromDrawingsUID1));
153
                        DataRow[] toDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", toDrawingsUID1));
154
                        DataRow[] fromOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", fromOPCUID1));
155
                        DataRow[] toOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", toOPCUID1));
156

    
157
                        if (fromOPCUID1 == toOPCUID2 && fromOPCUID2 == toOPCUID1 && fromDrawing.Length == 1 && toDrawing.Length == 1 && fromOPCInfoRows.Length == 1 && toOPCInfoRows.Length == 1)
158
                        {
159
                            DataRow fromOPCInfoRow = fromOPCInfoRows[0];
160
                            DataRow toOPCInfoRow = toOPCInfoRows[0];
161
                            string fromOPCModelId = fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
162
                            string toOPCModelId = toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
163
                            string toDrawingName = toDrawing[0]["DRAWINGNAME"].ToString();
164
                            List<string[]> toOPCAttributes = JsonConvert.DeserializeObject<List<string[]>>(toOPCInfoRow["ATTRIBUTES"].ToString());
165
                            AutoModeling_OPC opc = new AutoModeling_OPC(dApplication, application, fromOPCModelId, toOPCModelId, toDrawingName, toOPCAttributes);
166
                            if (opc.Run())
167
                            {
168
                                fromOPCInfoRow["PAIRED"] = true;
169
                                toOPCInfoRow["PAIRED"] = true;
170

    
171
                                Project_DB.InsertOPCInfo(fromOPCInfoRow["ID2_OPC_UID"].ToString(), fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), fromOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
172
                                Project_DB.InsertOPCInfo(toOPCInfoRow["ID2_OPC_UID"].ToString(), toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), toOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
173
                            }
174
                        }
175
                    }
176
                }
177
            }
178

    
179
            tOPCInfo.Dispose();
180
            tOPCRelations.Dispose();
181
            tDrawingInfo.Dispose();
182
            ReleaseCOMObjects(dApplication);
183
            dApplication = null;
184

    
185
            MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
186
        }
187

    
188
        public void ReleaseCOMObjects(params object[] objVars)
189
        {
190
            int intNewRefCount = 0;
191
            foreach (object obj in objVars)
192
            {
193
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
194
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
195
            }
196
        }
197

    
198
        private void btnGetDrawingSize_Click(object sender, EventArgs e)
199
        {
200
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
201
            WrapperApplication wApp = new WrapperApplication(application.Application);
202
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
203

    
204
            if (radApp.ActiveSelectSet.Count > 0)
205
            {
206
                DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject;
207
                if (line2D != null)
208
                {
209
                    double minX = 0;
210
                    double minY = 0;
211
                    double maxX = 0;
212
                    double maxY = 0;
213
                    line2D.Range(out minX, out minY, out maxX, out maxY);
214

    
215
                    Settings.Default.DrawingX = maxX - minX;
216
                    Settings.Default.DrawingY = maxY - minY;
217
                    Settings.Default.Save();
218

    
219
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
220
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
221
                }
222
                else
223
                    MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
224
            }
225
            else
226
                MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
227
        }
228

    
229
        #region SPPID Utils
230
        private void btnSymmetry_Click(object sender, EventArgs e)
231
        {
232
            if (application.ActiveSelectSet.Count == 1 && application.ActiveSelectSet[0].GetType() == typeof(Symbol2d))
233
            {
234
                int symCount = (int)spinEditSymmetry.Value;
235
                Symbol2d symbol = application.ActiveSelectSet[0] as Symbol2d;
236
                double x, y;
237
                symbol.GetOrigin(out x, out y);
238
                string rep = GetRepresentationId(symbol);
239
                List<string> verticalRepID = new List<string>();
240
                List<string> horizontalRepID = new List<string>();
241

    
242
                if ((symbol.LinearName.Contains("Piping") ||
243
                    symbol.LinearName.Contains("Instrument")) &&
244
                    !string.IsNullOrEmpty(rep))
245
                {
246
                    Placement placement = new Placement();
247
                    LMADataSource dataSource = placement.PIDDataSource;
248
                    List<List<string>> datas = SetSymbol(dataSource, rep, x, y, symCount);
249
                    Dictionary<SymmetryArrow, List<string>> resultDatas = new Dictionary<SymmetryArrow, List<string>>();
250
                    if (datas.Count >= 2 && datas.Find(loop => loop.Count != symCount) == null)
251
                    {
252
                        SymmetryArrow arrow = SymmetryArrow.None;
253
                        foreach (var data in datas)
254
                        {
255
                            LMSymbol firstSymbol = dataSource.GetSymbol(data[0]);
256
                            double fX = firstSymbol.get_XCoordinate();
257
                            double fY = firstSymbol.get_YCoordinate();
258

    
259
                            SlopeType type = SPPIDUtil.CalcSlope(x, y, fX, fY, 1);
260
                            if (type == SlopeType.HORIZONTAL)
261
                            {
262
                                if (fX < x)
263
                                {
264
                                    arrow |= SymmetryArrow.Left;
265
                                    resultDatas.Add(SymmetryArrow.Left, data);
266
                                }
267
                                else
268
                                {
269
                                    arrow |= SymmetryArrow.Right;
270
                                    resultDatas.Add(SymmetryArrow.Right, data);
271
                                }
272

    
273
                            }
274
                            else if (type == SlopeType.VERTICAL)
275
                            {
276
                                if (fY < y)
277
                                {
278
                                    arrow |= SymmetryArrow.Down;
279
                                    resultDatas.Add(SymmetryArrow.Down, data);
280
                                }
281
                                else
282
                                {
283
                                    arrow |= SymmetryArrow.Up;
284
                                    resultDatas.Add(SymmetryArrow.Up, data);
285
                                }
286
                            }
287

    
288
                            ReleaseCOMObjects(firstSymbol);
289
                        }
290

    
291
                        SymmetryForm form = new SymmetryForm(arrow);
292
                        if (form.ShowDialog() == DialogResult.OK)
293
                        {
294
                            MoveByResult(dataSource, resultDatas, x, y, form.Result, rep);
295
                        }
296
                    }
297
                    else
298
                        MessageBox.Show("Check Symmetry Rules", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
299
                    ReleaseCOMObjects(dataSource);
300
                    ReleaseCOMObjects(placement);
301
                }
302
            }
303

    
304
            return;
305

    
306
            List<Symbol2d> symbols = new List<Symbol2d>();
307
            foreach (var item in application.ActiveSelectSet)
308
            {
309
                Type type = item.GetType();
310
                if (type == typeof(Symbol2d))
311
                {
312
                    Symbol2d symbol = item as Symbol2d;
313
                    if (symbol.LinearName.Contains("Piping") ||
314
                        symbol.LinearName.Contains("Instrument"))
315
                        symbols.Add(symbol);
316
                }
317
                //if (item.GetType() == typeof(Symbol2d) || item.GetType() == typeof(Point2d))
318
                //    items.Add(item);
319
            }
320

    
321
            List<Symbol2d> verticalSymbols = new List<Symbol2d>();
322
            List<Symbol2d> horizontalSymbols = new List<Symbol2d>();
323
            Symbol2d mainSymbol = null;
324
            for (int i = 0; i < symbols.Count - 1; i++)
325
            {
326
                Symbol2d symbol1 = symbols[i];
327
                for (int j = 1; j < symbols.Count; j++)
328
                {
329
                    Symbol2d symbol2 = symbols[j];
330

    
331
                    if (symbol1 != symbol2)
332
                    {
333
                        double x1, y1, x2, y2;
334
                        symbol1.GetOrigin(out x1, out y1);
335
                        symbol2.GetOrigin(out x2, out y2);
336
                        SlopeType slopeType = SPPIDUtil.CalcSlope(x1, y1, x2, y2, 1);
337
                        if (slopeType == SlopeType.HORIZONTAL)
338
                        {
339
                            if (!horizontalSymbols.Contains(symbol1))
340
                                horizontalSymbols.Add(symbol1);
341
                            if (!horizontalSymbols.Contains(symbol2))
342
                                horizontalSymbols.Add(symbol2);
343

    
344
                            if (verticalSymbols.Contains(symbol1))
345
                                mainSymbol = symbol1;
346
                            if (verticalSymbols.Contains(symbol2))
347
                                mainSymbol = symbol2;
348
                        }
349
                        else if (slopeType == SlopeType.VERTICAL)
350
                        {
351
                            if (!verticalSymbols.Contains(symbol1))
352
                                verticalSymbols.Add(symbol1);
353
                            if (!verticalSymbols.Contains(symbol2))
354
                                verticalSymbols.Add(symbol2);
355

    
356
                            if (horizontalSymbols.Contains(symbol1))
357
                                mainSymbol = symbol1;
358
                            if (horizontalSymbols.Contains(symbol2))
359
                                mainSymbol = symbol2;
360
                        }
361
                    }
362
                }
363
            }
364

    
365
            application.ActiveSelectSet.RemoveAll();
366
            foreach (var item in verticalSymbols)
367
                application.ActiveSelectSet.Add(item);
368
            foreach (var item in horizontalSymbols)
369
                application.ActiveSelectSet.Add(item);
370
                
371

    
372
            if (MessageBox.Show("Continue?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
373
            {
374
                application.ActiveSelectSet.RemoveAll();
375

    
376
            }
377
        }
378
        private void MoveByResult(LMADataSource dataSource,Dictionary<SymmetryArrow, List<string>> resultDatas, double x, double y, SymmetryArrow arrow, string centerRepID)
379
        {
380
            List<string> datas = resultDatas[arrow];
381
            foreach (var item in resultDatas)
382
            {
383
                if (item.Key != arrow)
384
                {
385
                    for (int i = 0; i < item.Value.Count; i++)
386
                    {
387
                        Symbol2d moveSymbol2d = GetSymbol2DByRepID(dataSource, item.Value[i]);
388
                        Symbol2d symbol2d = GetSymbol2DByRepID(dataSource, datas[i]);
389

    
390
                        double x1, y1, x2, y2;
391
                        symbol2d.GetOrigin(out x1, out y1);
392
                        moveSymbol2d.GetOrigin(out x2, out y2);
393
                        double distance = SPPIDUtil.CalcPointToPointdDistance(x, y, x1, y1);
394

    
395
                        string symbol1RepID;
396
                        string symbol2RepID;
397
                        List<Point2d> point2ds;
398
                        if (i == 0)
399
                        {
400
                            symbol1RepID = centerRepID;
401
                            symbol2RepID = item.Value[i];
402
                        }
403
                        else
404
                        {
405
                            symbol1RepID = item.Value[i - 1];
406
                            symbol2RepID = item.Value[i];
407
                        }
408
                        point2ds = FindAllPoint2d(dataSource, symbol1RepID, symbol2RepID);
409
                        double moveX = 0;
410
                        double moveY = 0;
411

    
412
                        if (item.Key == SymmetryArrow.Left)
413
                            moveX = x - distance - x2;
414
                        else if (item.Key == SymmetryArrow.Right)
415
                            moveX = x + distance - x2;
416
                        else if (item.Key == SymmetryArrow.Down)
417
                            moveY = y - distance - y2;
418
                        else if (item.Key == SymmetryArrow.Up)
419
                            moveY = y + distance - y2;
420

    
421
                        moveSymbol2d.Move(0, 0, moveX, moveY);
422
                        MovePoint2d(dataSource, item.Value[i], arrow, moveX, moveY);
423
                        foreach (var point in point2ds)
424
                        {
425
                            LMSymbol branch = dataSource.GetSymbol(GetRepresentationId(point));
426
                            LMSymbol connSymbol = GetFirstSymbolBySlope(dataSource, branch, symbol1RepID, symbol2RepID);
427
                            point.Move(0, 0, moveX, moveY);
428
                            if (connSymbol != null)
429
                            {
430
                                Symbol2d connSymbol2d = GetSymbol2DByRepID(dataSource, connSymbol.AsLMRepresentation().Id);
431
                                connSymbol2d.Move(0, 0, moveX, moveY);
432
                                ReleaseCOMObjects(connSymbol);
433
                            }
434
                            ReleaseCOMObjects(branch);
435
                        }
436
                            
437
                    }
438
                }
439
            }
440
        }
441

    
442
        private LMSymbol GetFirstSymbolBySlope(LMADataSource dataSource, LMSymbol branch, string symbol1RepID, string symbol2RepID)
443
        {
444
            LMSymbol result = null;
445
            foreach (LMConnector connector in branch.Connect1Connectors)
446
            {
447
                if (connector.ConnectItem1SymbolObject != null && 
448
                    connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
449
                    connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
450
                {
451
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
452
                    if (repID != symbol1RepID && repID != symbol2RepID)
453
                        result = connector.ConnectItem1SymbolObject;
454
                }
455

    
456
                if (connector.ConnectItem2SymbolObject != null &&
457
                    connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
458
                    connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
459
                {
460
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
461
                    if (repID != symbol1RepID && repID != symbol2RepID)
462
                        result = connector.ConnectItem2SymbolObject;
463
                }
464
            }
465

    
466
            foreach (LMConnector connector in branch.Connect2Connectors)
467
            {
468
                if (connector.ConnectItem1SymbolObject != null &&
469
                    connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
470
                    connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
471
                {
472
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
473
                    if (repID != symbol1RepID && repID != symbol2RepID)
474
                        result = connector.ConnectItem1SymbolObject;
475
                }
476

    
477
                if (connector.ConnectItem2SymbolObject != null &&
478
                    connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
479
                    connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
480
                {
481
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
482
                    if (repID != symbol1RepID && repID != symbol2RepID)
483
                        result = connector.ConnectItem2SymbolObject;
484
                }
485
            }
486

    
487

    
488
            return result;
489
        }
490
        private string GetRepresentationId(Symbol2d symbol)
491
        {
492
            foreach (var attributes in symbol.AttributeSets)
493
            {
494
                foreach (var attribute in attributes)
495
                {
496
                    string name = attribute.Name;
497
                    object value = attribute.GetValue();
498
                    if (name == "DrawingID")
499
                    {
500
                        return value.ToString();  
501
                    }
502
                }
503
            }
504
            return null;
505
        }
506
        private string GetRepresentationId(Point2d point2d)
507
        {
508
            foreach (var attributes in point2d.AttributeSets)
509
            {
510
                foreach (var attribute in attributes)
511
                {
512
                    string name = attribute.Name;
513
                    object value = attribute.GetValue();
514
                    if (name == "DrawingID")
515
                    {
516
                        return value.ToString();
517
                    }
518
                }
519
            }
520
            return null;
521
        }
522
        private List<List<string>> SetSymbol(LMADataSource dataSource, string rep, double x, double y, int count)
523
        {
524
            LMSymbol _LMSymbol = dataSource.GetSymbol(rep);
525
            List<List<string>> result = new List<List<string>>();
526
            List<string> oldIDs = new List<string>() { rep };
527
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
528
            {
529
                if (connector.get_ItemStatus() != "Active")
530
                    continue;
531

    
532
                string repID = connector.AsLMRepresentation().Id;
533
                string status = connector.get_ItemStatus();
534
                if (status == "Active" && !oldIDs.Contains(repID))
535
                {
536
                    List<string> symbols = new List<string>();
537
                    oldIDs.Add(repID);
538
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
539
                    result.Add(symbols);
540
                }
541
            }
542

    
543
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
544
            {
545
                if (connector.get_ItemStatus() != "Active")
546
                    continue;
547

    
548
                string repID = connector.AsLMRepresentation().Id;
549
                string status = connector.get_ItemStatus();
550
                if (status == "Active" && !oldIDs.Contains(repID))
551
                {
552
                    List<string> symbols = new List<string>();
553
                    oldIDs.Add(repID);
554
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
555
                    result.Add(symbols);
556
                }
557
            }
558

    
559
            ReleaseCOMObjects(_LMSymbol);
560
            return result;
561
        }
562
        private void loop(LMADataSource dataSource, List<string> oldIDs, List<string> symbols, LMConnector connector, double x, double y, int count)
563
        {
564
            if (symbols.Count >= count)
565
                return;
566

    
567
            if (connector.ConnectItem1SymbolObject != null && !oldIDs.Contains(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id))
568
            {
569
                string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
570
                oldIDs.Add(repID);
571
                if (connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch" && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
572
                {
573
                    double sX = connector.ConnectItem1SymbolObject.get_XCoordinate(), sY = connector.ConnectItem1SymbolObject.get_YCoordinate(); ;
574
                    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, sX, sY, 1);
575
                    if (slopeType == SlopeType.HORIZONTAL || slopeType == SlopeType.VERTICAL)
576
                        symbols.Add(repID);
577
                }
578

    
579
                loop(dataSource, oldIDs, symbols, connector.ConnectItem1SymbolObject, x, y, count);
580
            }
581

    
582
            if (symbols.Count >= count)
583
                return;
584

    
585
            if (connector.ConnectItem2SymbolObject != null && !oldIDs.Contains(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id))
586
            {
587
                string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
588
                oldIDs.Add(repID);
589
                if (connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch" && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
590
                {
591
                    double sX = connector.ConnectItem2SymbolObject.get_XCoordinate(), sY = connector.ConnectItem2SymbolObject.get_YCoordinate(); ;
592
                    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, sX, sY, 1);
593
                    if (slopeType == SlopeType.HORIZONTAL || slopeType == SlopeType.VERTICAL)
594
                        symbols.Add(repID);
595
                }
596

    
597
                loop(dataSource, oldIDs, symbols, connector.ConnectItem2SymbolObject, x, y, count);
598
            }
599
        }
600
        private void loop(LMADataSource dataSource, List<string> oldIDs, List<string> symbols, LMSymbol _LMSymbol, double x, double y, int count)
601
        {
602
            if (symbols.Count >= count)
603
                return;
604

    
605
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
606
            {
607
                if (connector.get_ItemStatus() != "Active")
608
                    continue;
609

    
610
                string repID = connector.AsLMRepresentation().Id;
611
                string status = connector.get_ItemStatus();
612
                if (status == "Active" && !oldIDs.Contains(repID))
613
                {
614
                    oldIDs.Add(repID);
615
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
616
                }
617
            }
618

    
619
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
620
            {
621
                if (connector.get_ItemStatus() != "Active")
622
                    continue;
623

    
624
                string repID = connector.AsLMRepresentation().Id;
625
                string status = connector.get_ItemStatus();
626
                if (status == "Active" && !oldIDs.Contains(repID))
627
                {
628
                    oldIDs.Add(repID);
629
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
630
                }
631
            }
632
        }
633
        private Symbol2d GetSymbol2DByRepID(LMADataSource dataSource, string repID)
634
        {
635
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
636
            Symbol2d symbol2D = application.ActiveDocument.ActiveSheet.DrawingObjects[_LMSymbol.get_GraphicOID().ToString()];
637
            ReleaseCOMObjects(_LMSymbol);
638
            return symbol2D;
639
        }
640
        private void MovePoint2d(LMADataSource datasource, string repID, SymmetryArrow arrow, double moveX, double moveY)
641
        {
642
            LMSymbol _LMSymbol = datasource.GetSymbol(repID);
643
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
644
            {
645
                if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()))
646
                {
647
                    if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
648
                    {
649
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()];
650
                        point.X += moveX;
651
                        point.Y += moveY;
652
                    }
653
                    else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
654
                    {
655
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()];
656
                        point.X += moveX;
657
                        point.Y += moveY;
658
                    }
659
                }
660
            }
661
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
662
            {
663
                if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()))
664
                {
665
                    if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
666
                    {
667
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()];
668
                        point.X += moveX;
669
                        point.Y += moveY;
670
                    }
671
                    else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
672
                    {
673
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()];
674
                        point.X += moveX;
675
                        point.Y += moveY;
676
                    }
677
                }
678
            }
679
            ReleaseCOMObjects(_LMSymbol);
680
        }
681

    
682
        private List<Point2d> FindAllPoint2d(LMADataSource dataSource, string repID, string nextRepID)
683
        {
684
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
685
            List<string> endIDs = new List<string>() { repID };
686
            List<string> graphicOIDs = new List<string>();
687
            List<Point2d> result = new List<Point2d>();
688
            FindPointsLoop(dataSource, _LMSymbol, endIDs, nextRepID, graphicOIDs);
689
            ReleaseCOMObjects(_LMSymbol);
690
            foreach (var item in graphicOIDs)
691
            {
692
                Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[item] as Point2d;
693
                result.Add(point);
694
            }
695
            
696
            return result;
697
        }
698

    
699
        private bool FindPointsLoop(LMADataSource dataSource, LMSymbol _LMSymbol, List<string> endIDs, string targetRepID, List<string> graphicOIDs)
700
        {
701
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
702
            {
703
                if (connector.get_ItemStatus() != "Active")
704
                    continue;
705

    
706
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
707
                {
708
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
709
                    if (!endIDs.Contains(repID))
710
                    {
711
                        endIDs.Add(repID);
712
                        if (connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
713
                        {
714
                            if (FindPointsLoop(dataSource, connector.ConnectItem1SymbolObject, endIDs, targetRepID, graphicOIDs))
715
                            {
716
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
717
                                    graphicOIDs.Add(connector.ConnectItem1SymbolObject.get_GraphicOID().ToString());
718
                                return true;
719
                            }
720
                        }
721
                        else if (targetRepID == repID)
722
                        {
723
                            return true;
724
                        }
725
                    }
726
                }
727
                if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
728
                {
729
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
730
                    if (!endIDs.Contains(repID))
731
                    {
732
                        endIDs.Add(repID);
733
                        if (connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
734
                        {
735
                            if (FindPointsLoop(dataSource, connector.ConnectItem2SymbolObject, endIDs, targetRepID, graphicOIDs))
736
                            {
737
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
738
                                    graphicOIDs.Add(connector.ConnectItem2SymbolObject.get_GraphicOID().ToString());
739
                                return true;
740
                            }
741
                        }
742
                        else if (targetRepID == repID)
743
                        {
744
                            return true;
745
                        }
746
                    }
747
                }
748
            }
749
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
750
            {
751
                if (connector.get_ItemStatus() != "Active")
752
                    continue;
753

    
754
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
755
                {
756
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
757
                    if (!endIDs.Contains(repID))
758
                    {
759
                        endIDs.Add(repID);
760
                        if (connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
761
                        {
762
                            if (FindPointsLoop(dataSource, connector.ConnectItem1SymbolObject, endIDs, targetRepID, graphicOIDs))
763
                            {
764
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
765
                                    graphicOIDs.Add(connector.ConnectItem1SymbolObject.get_GraphicOID().ToString());
766
                                return true;
767
                            }
768
                        }
769
                        else if (targetRepID == repID)
770
                        {
771
                            return true;
772
                        }
773
                    }
774
                }
775
                if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
776
                {
777
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
778
                    if (!endIDs.Contains(repID))
779
                    {
780
                        endIDs.Add(repID);
781
                        if (connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
782
                        {
783
                            if (FindPointsLoop(dataSource, connector.ConnectItem2SymbolObject, endIDs, targetRepID, graphicOIDs))
784
                            {
785
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
786
                                    graphicOIDs.Add(connector.ConnectItem2SymbolObject.get_GraphicOID().ToString());
787
                                return true;
788
                            }
789
                        }
790
                        else if (targetRepID == repID)
791
                        {
792
                            return true;
793
                        }
794
                    }
795
                }
796
            }
797

    
798
            return false;
799
        }
800

    
801

    
802

    
803
        #region Hot Key
804
        [System.Runtime.InteropServices.DllImport("user32.dll")]
805
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
806
        [System.Runtime.InteropServices.DllImport("user32.dll")]
807
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
808
        enum KeyModifier
809
        {
810
            None = 0,
811
            Alt = 1,
812
            Control = 2,
813
            Shift = 4,
814
            WinKey = 8
815
        }
816
        protected override void WndProc(ref Message m)
817
        {
818
            base.WndProc(ref m);
819
            if (m.Msg == 0x0312)
820
            {
821
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
822
                KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF);
823
                int id = m.WParam.ToInt32();
824
                application.RunCommand(CommandConstants.igcmdGridSnap);
825
            }
826

    
827
        }
828
        #endregion
829
        #endregion
830

    
831
        #region TEST
832

    
833
        private void simpleButton1_Click(object sender, EventArgs e)
834
        {
835
            Placement placement = new Placement();
836
            if (application.ActiveSelectSet.Count > 0)
837
            {
838
                string modelItemId = null;
839
                Symbol2d symbol2D = application.ActiveSelectSet[0] as Symbol2d;
840
                DependencyObject dependency = application.ActiveSelectSet[0] as DependencyObject;
841
                application.ActiveSelectSet.RemoveAll();
842
                if (symbol2D != null)
843
                {
844
                    return;
845
                    foreach (var attributes in symbol2D.AttributeSets)
846
                    {
847
                        foreach (var attribute in attributes)
848
                        {
849
                            if (attribute.Name == "ModelID")
850
                                modelItemId = attribute.GetValue().ToString();
851
                        }
852
                    }
853

    
854
                    if (modelItemId!=null)
855
                    {
856
                        LMADataSource dataSource = new LMADataSource();
857
                        LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
858
                        foreach (LMRepresentation item in modelItem.Representations)
859
                        {
860
                            if (item.ModelItemID == modelItemId)
861
                            {
862
                                dataSource = placement.PIDDataSource;
863
                                LMSymbol symbol = dataSource.GetSymbol(item.Id);
864
                                double oldx = symbol.get_XCoordinate();
865
                                double oldy = symbol.get_YCoordinate();
866
                                symbol2D.Move(0, 0, 0, GridSetting.GetInstance().Length);
867

    
868
                                dataSource = new LMADataSource();
869
                                //dataSource = placement.PIDDataSource;
870
                                symbol = dataSource.GetSymbol(item.Id);
871
                                double x = symbol.get_XCoordinate();
872
                                double y = symbol.get_YCoordinate();
873
                            }
874
                        }
875
                    }
876
                }
877
                else if (dependency != null)
878
                {
879
                    foreach (dynamic item in dependency.DrawingObjects)
880
                    {
881
                        item.Move(0, 0, 0, GridSetting.GetInstance().Length);
882
                    }
883
                }
884
            }
885

    
886
            return;
887
            Thread outThread = new Thread(func2 =>
888
            {
889
                for (int i = 100; i < 120; i++)
890
                {
891
                    Thread thread = new Thread(func =>
892
                    {
893
                        dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
894
                        //WrapperApplication wApp = new WrapperApplication(application.Application);
895
                        //Ingr.RAD2D.Application radApp = wApp.RADApplication;
896
                        dynamic drawing = application.Drawings.Add("Unit1", "D-Size.pid", i.ToString(), i.ToString());//application.Drawings.OpenDrawing("123");
897

    
898
                        Thread.Sleep(5000);
899

    
900
                        drawing.CloseDrawing(true);
901

    
902
                        //application.Quit();
903
                        ReleaseCOMObjects(application);
904
                        application = null;
905
                        ReleaseCOMObjects(drawing);
906
                        drawing = null;
907
                        Thread.Sleep(5000);
908
                    });
909
                    thread.Start();
910
                    thread.Join();
911
                }
912
            });
913
            outThread.Start();
914
            
915
            
916

    
917

    
918

    
919
            //radApp.Documents.Add(@"\\server70\DEVDOF1Site\GS\PLANT\PLANT1\Unit1\11111.pid");
920

    
921
            //radApp.ActiveDocument.Close(true);
922
            //drawing.CloseDrawing(true);
923

    
924
            //ReleaseCOMObjects(application);
925
            //ReleaseCOMObjects(drawing);
926
            //application = null;
927
            //wApp = null;
928
            //radApp = null;
929
            //drawing = null;
930
        }
931

    
932
        private void AutoJoinPipeRun()
933
        {
934
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
935
            WrapperApplication wApp = new WrapperApplication(application.Application);
936
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
937

    
938
            string modelItemId = null;
939
            List<double[]> vertices = new List<double[]>();
940
            if (radApp.ActiveSelectSet.Count == 0)
941
            {
942
                return;
943
            }
944
            dynamic OID = radApp.ActiveSelectSet[0].Key();
945
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
946
            foreach (var attributes in drawingObject.AttributeSets)
947
            {
948
                foreach (var attribute in attributes)
949
                {
950
                    if (attribute.Name == "ModelID")
951
                        modelItemId = attribute.GetValue().ToString();
952
                }
953
            }
954
            radApp.ActiveSelectSet.RemoveAll();
955

    
956
        }
957
        #endregion
958

    
959

    
960
        [DllImport("user32.dll")]
961
        public static extern int FindWindow(string lpClassName, string lpWindowName);
962

    
963
        [DllImport("user32.dll", SetLastError = true)]
964
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
965

    
966
        private void ConverterDocking_Load(object sender, EventArgs e)
967
        {
968
#if DEBUG
969
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
970
#else
971
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
972
#endif
973

    
974
        }
975

    
976
        
977
    }
978
}
클립보드 이미지 추가 (최대 크기: 500 MB)