프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ 30a35908

이력 | 보기 | 이력해설 | 다운로드 (154 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
using DevExpress.Utils.MVVM;
28

    
29
namespace Converter.SPPID.Wrapper
30
{
31
    public partial class ConverterDocking : UserControl
32
    {
33
        Ingr.RAD2D.Application application;
34
        internal static bool addEvent = false;
35
        public ConverterDocking()
36
        {
37
            InitializeComponent();
38
            spinEditSymmetry.Properties.Mask.EditMask = "f0";
39
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
40
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
41
            application = wApp.RADApplication;
42
            
43
            ReleaseCOMObjects(dApplication);
44
            dApplication = null;
45
            try
46
            {
47
                if (Settings.Default.DrawingX == 0)
48
                {
49
                    textEditDrawingX.EditValue = 0.866501770913601;
50
                }
51
                else
52
                {
53
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
54
                }
55
                if (Settings.Default.DrawingY == 0)
56
                {
57
                    textEditDrawingY.EditValue = 0.665463201701641;
58
                }
59
                else
60
                {
61
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
62
                }
63

    
64
                Project_Info _ProjectInfo = Project_Info.GetInstance();
65
                _ProjectInfo.DefaultPath = Settings.Default.ProjectPath;
66
                _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType;
67
                _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP;
68
                _ProjectInfo.Port = Settings.Default.ProjectPort;
69
                _ProjectInfo.DBUser = Settings.Default.ProjectDBUser;
70
                _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword;
71
                if (Project_DB.ConnTestAndCreateTable())
72
                {
73
                    _ProjectInfo.Enable = true;
74
                    layoutControlGroupUtils.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
75

    
76
                    DataTable dt = Project_DB.SelectSetting();
77
                    foreach (DataRow item in dt.Rows)
78
                    {
79
                        string settingType = item["SettingType"].ToString();
80
                        if (settingType == "ETCSetting")
81
                            SPPIDUtil.ConvertToETCSetting(item["JsonString"].ToString());
82
                        else if (settingType == "GridSetting")
83
                            SPPIDUtil.ConvertToGridSetting(item["JsonString"].ToString());
84
                    }
85
                }
86
                else
87
                {
88
                    layoutControlGroupUtils.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
89
                }
90

    
91
                if (!addEvent)
92
                {
93
                    application.EventObject.BeforeApplicationExit += ApplicationEvents_ApplicationExit;
94
                    addEvent = true;
95
                }
96
            }
97
            catch (Exception ex)
98
            {
99
                StringBuilder sb = new StringBuilder();
100
                sb.AppendLine(ex.Message);
101
                sb.AppendLine(ex.StackTrace);
102
                MessageBox.Show(sb.ToString());
103
            }
104

    
105
#if DEBUG
106
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
107
            layoutControlGroup1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
108

    
109
#else
110
            layoutControlGroup1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
111
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
112
#endif
113

    
114
            //#if DEBUG
115
            //            layoutControlGroupConverter.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
116
            //            layoutControlGroupUtils.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
117
            //            layoutControlGroupShortCut.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
118
            //            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
119
            //            this.layoutControlItem3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
120
            //#endif
121
        }
122
        private void ApplicationEvents_ApplicationExit(out bool cancel)
123
        {
124
            cancel = false;
125
        }
126
        /// <summary>
127
        /// 선택한 도면들을 읽어서 SPPID로 변환한다.
128
        /// </summary>
129
        /// <param name="sender"></param>
130
        /// <param name="e"></param>
131
        private void btnConverter_Click(object sender, EventArgs e)
132
        {
133
            ConverterForm converterForm = new ConverterForm();
134
            if (converterForm.ShowDialog() == DialogResult.OK)
135
            {
136
                //Ingr.RAD2D.Document doc = application.Documents.Add();
137

    
138
                try
139
                {
140
                    CloseOPCForm.Run();
141

    
142
                    for (int i = 0; i < converterForm.Documents.Count; i++)
143
                    {
144
                        SPPID_Document document = converterForm.Documents[i];
145
                        if (document.SetSPPIDMapping() && document.Enable)
146
                        {
147
                            using (AutoModeling modeling = new AutoModeling(document, converterForm.checkEditCloseDocument.Checked))
148
                            {
149
                                modeling.DocumentLabelText = string.Format("Drawing Name : {0} ({1}/{2})", document.DrawingName, i + 1, converterForm.Documents.Count);
150
                                modeling.Run();
151

    
152
                                List<string> endLine = new List<string>();
153
                                Placement placement = new Placement();
154
                                LMADataSource dataSource = placement.PIDDataSource;
155
                                
156
                                foreach (var lineNumber in document.LINENUMBERS)
157
                                {
158
                                    foreach (LineRun run in lineNumber.RUNS)
159
                                    {
160
                                        foreach (var item in run.RUNITEMS)
161
                                        {
162
                                            if (item.GetType() == typeof(Line))
163
                                            {
164
                                                Line line = item as Line;
165
                                                if (line != null && !endLine.Contains(line.SPPID.ModelItemId))
166
                                                {
167
                                                    LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
168
                                                    if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
169
                                                    {
170
                                                        foreach (var attribute in lineNumber.ATTRIBUTES)
171
                                                        {
172
                                                            LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
173
                                                            if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None" && mapping.SPPIDATTRIBUTENAME == "PlantGroup.Name")
174
                                                            {
175
                                                                LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
176
                                                                if (_LMAAttribute != null)
177
                                                                {
178
                                                                    if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
179
                                                                        _LMAAttribute.set_Value(attribute.VALUE);
180
                                                                    else if (_LMAAttribute.get_Value() != attribute.VALUE)
181
                                                                        _LMAAttribute.set_Value(attribute.VALUE);
182
                                                                }
183
                                                            }
184
                                                        }
185
                                                        _LMModelItem.Commit();
186
                                                    }
187
                                                    if (_LMModelItem != null)
188
                                                        ReleaseCOMObjects(_LMModelItem);
189
                                                    endLine.Add(line.SPPID.ModelItemId);
190
                                                }
191
                                            }
192
                                        }
193
                                    }
194
                                }
195

    
196
                                ReleaseCOMObjects(dataSource);
197
                                ReleaseCOMObjects(placement);
198
                            }
199
                        }
200
                    }
201
                }
202
                catch (Exception ex)
203
                {
204
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
205
                }
206
                finally
207
                {
208
                    CloseOPCForm.Stop();
209

    
210
                    //doc.SaveOnClose = false;
211
                    //doc.Close(false);
212

    
213
                    //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
214
                    //dApplication.Drawings[1].CloseDrawing(false);
215
                    //ReleaseCOMObjects(dApplication);
216
                    //dApplication = null;
217
                }
218

    
219
                MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
220
            }
221
        }
222

    
223
        private void btnLinkOPC_Click(object sender, EventArgs e)
224
        {
225
            Placement placement = new Placement();
226
            LMADataSource dataSource = placement.PIDDataSource;
227

    
228
            LMPlantSettings plantSettings = new LMPlantSettings();
229
            plantSettings.Collect(dataSource);
230
            string settingValue = SPPIDUtil.T_PLANTSETTING_Value("Plant Path");
231

    
232
            if (string.IsNullOrEmpty(settingValue))
233
                return;
234
            
235
            LMAFilter filter = new LMAFilter();
236
            LMACriterion criterion = new LMACriterion();
237
            filter.ItemType = "Drawing";
238
            criterion.SourceAttributeName = "Name";
239
            criterion.Operator = "=";
240
            criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
241
            filter.get_Criteria().Add(criterion);
242

    
243
            LMDrawings drawings = new LMDrawings();
244
            drawings.Collect(dataSource, Filter: filter);
245

    
246
            // Input Drawing Attribute
247
            LMDrawing drawing = ((dynamic)drawings).Nth(1);
248

    
249
            LMAFilter filter2 = new LMAFilter();
250
            filter2.ItemType = "REPRESENTATION";
251

    
252
            LMACriterion criterion2 = new LMACriterion();
253
            criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
254
            criterion2.Operator = "=";
255
            criterion2.set_ValueAttribute("39");
256
            filter2.get_Criteria().Add(criterion2);
257

    
258
            LMRepresentations representations = new LMRepresentations();
259
            representations.Collect(dataSource, Filter: filter2);
260

    
261
            string drawingID = drawing.Id;
262
            List<Tuple<LMRepresentation, string>> CurrentDrawing = new List<Tuple<LMRepresentation, string>>();
263
            List<Tuple<LMRepresentation, string>> OtherDrawing = new List<Tuple<LMRepresentation, string>>(); 
264
            foreach (LMRepresentation representation in representations)
265
            {
266
                if (representation.get_ItemStatus() == "Active" && representation.get_InStockpile() == "False")
267
                {
268
                    LMSymbol symbol = dataSource.GetSymbol(representation.Id);
269

    
270
                    if (symbol != null)
271
                    {
272
                        string sResult = "";
273
                        bool bResult = true;
274
                        foreach (LMConnector item in symbol.Connect1Connectors)
275
                        {
276
                            if (item.get_ItemStatus() == "Active" && item.get_InStockpile() == "False" && item.ModelItemObject.get_ItemTypeName() == "PipeRun")
277
                            {
278
                                LMPipeRun pipeRun = dataSource.GetPipeRun(item.ModelItemID);
279
                                if (pipeRun != null)
280
                                {
281
                                    dynamic value = pipeRun.get_ItemTag();
282
                                    if (!DBNull.Value.Equals(value))
283
                                    {
284
                                        if (string.IsNullOrEmpty(sResult))
285
                                            sResult = value;
286
                                        else
287
                                            bResult = false;
288
                                    }
289
                                }
290
                                ReleaseCOMObjects(pipeRun);
291
                            }
292
                        }
293
                        foreach (LMConnector item in symbol.Connect2Connectors)
294
                        {
295
                            if (item.get_ItemStatus() == "Active" && item.get_InStockpile() == "False" && item.ModelItemObject.get_ItemTypeName() == "PipeRun")
296
                            {
297
                                LMPipeRun pipeRun = dataSource.GetPipeRun(item.ModelItemID);
298
                                if (pipeRun != null)
299
                                {
300
                                    dynamic value = pipeRun.get_ItemTag();
301
                                    if (!DBNull.Value.Equals(value))
302
                                    {
303
                                        if (string.IsNullOrEmpty(sResult))
304
                                            sResult = value;
305
                                        else
306
                                            bResult = false;
307
                                    }
308
                                }
309
                                ReleaseCOMObjects(pipeRun);
310
                            }
311
                        }
312

    
313
                        if (bResult)
314
                        {
315
                            if (representation.DrawingID == drawingID)
316
                                CurrentDrawing.Add(new Tuple<LMRepresentation, string>(representation, sResult));
317
                            else
318
                                OtherDrawing.Add(new Tuple<LMRepresentation, string>(representation, sResult));
319
                        }
320
                    }
321

    
322
                    ReleaseCOMObjects(symbol);
323
                }
324
            }
325

    
326
            List<OPC_Info> _OPC_Infos = new List<OPC_Info>();
327
            DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook();
328
            workbook.BeginUpdate();
329

    
330
            workbook.Worksheets[0][0, 0].Value = "OPC ModelItemID";
331
            workbook.Worksheets[0][0, 1].Value = "Tag Value";
332
            workbook.Worksheets[0][0, 2].Value = "Pair Drawing Name";
333
            workbook.Worksheets[0][0, 3].Value = "Match RepID";
334

    
335
            int rowIndex = 1;
336
            foreach (var current in CurrentDrawing)
337
            {
338
                // 여기 로그
339
                LMOPC currentOPC = dataSource.GetOPC(current.Item1.ModelItemID);
340
                string fileName = current.Item1.get_FileName();
341
                string tag = current.Item2;
342
                List<LMOPC> findOPCs = new List<LMOPC>(); 
343
                foreach (var other in OtherDrawing)
344
                {
345
                    if (tag == other.Item2 && fileName == other.Item1.get_FileName())
346
                    {
347
                        LMOPC otherOPC = dataSource.GetOPC(other.Item1.ModelItemID);
348
                        findOPCs.Add(otherOPC);
349
                    }
350
                }
351

    
352
                workbook.Worksheets[0][rowIndex, 0].Value = current.Item1.ModelItemID.ToString();
353
                workbook.Worksheets[0][rowIndex, 1].Value = string.IsNullOrEmpty(tag) ? "Tag is null" : tag;
354

    
355
                if (findOPCs.Count >= 1 && !string.IsNullOrEmpty(tag))
356
                {
357
                    List<OPC_Info> tempInfos = new List<OPC_Info>();
358
                    foreach (var findOPC in findOPCs)
359
                    {
360
                        LMRepresentation currentPairRepresentation = currentOPC.pairedWithOPCObject.Representations.Nth[1];
361
                        LMRepresentation findPairRepresentation = findOPC.pairedWithOPCObject.Representations.Nth[1];
362
                        if (currentPairRepresentation == null || findPairRepresentation == null)
363
                            continue;
364
                        else if (currentPairRepresentation.get_ItemStatus() == "Active" && currentPairRepresentation.get_InStockpile() == "True" &&
365
                            findPairRepresentation.get_ItemStatus() == "Active" && findPairRepresentation.get_InStockpile() == "True")
366
                        {
367
                            string currentGraphicOID = currentOPC.Representations.Nth[1].get_GraphicOID().ToString();
368
                            LMRepresentation findRepresentation = findOPC.Representations.Nth[1];
369
                            string findGraphicOID = findRepresentation.get_GraphicOID().ToString();
370
                            LMDrawing findDrawing = findRepresentation.DrawingObject;
371
                            string findDrawingPath = settingValue + findDrawing.get_Path();
372

    
373
                            tempInfos.Add(new OPC_Info()
374
                            {
375
                                CurrentGraphicOID = currentGraphicOID,
376
                                TagValue = tag,
377
                                FindGraphicOID = findGraphicOID,
378
                                FindDrawingPath = findDrawingPath,
379
                                FindDrawingName = Path.GetFileNameWithoutExtension(findDrawingPath),
380
                                CurrentRepID = currentOPC.Representations.Nth[1].Id,
381
                                FindRepID = findRepresentation.Id
382
                            });
383

    
384
                            ReleaseCOMObjects(findDrawing);
385
                            ReleaseCOMObjects(findRepresentation);
386
                        }
387
                    }
388

    
389
                    if (tempInfos.Count == 1)
390
                    {
391
                        _OPC_Infos.Add(tempInfos[0]);
392
                        workbook.Worksheets[0][rowIndex, 2].Value = tempInfos[0].FindDrawingName;
393
                        workbook.Worksheets[0][rowIndex, 3].Value = tempInfos[0].FindRepID;
394
                    }
395
                    else
396
                    {
397
                        foreach (var tempInfo in tempInfos)
398
                        {
399
                            workbook.Worksheets[0][rowIndex, 2].Value = tempInfo.FindDrawingName;
400
                            workbook.Worksheets[0][rowIndex, 3].Value = tempInfo.FindRepID;
401
                            if (tempInfos.IndexOf(tempInfo) != tempInfos.Count - 1)
402
                                rowIndex++;
403
                        }
404
                    }   
405
                }
406
                rowIndex++;
407
                foreach (var item in findOPCs)
408
                    ReleaseCOMObjects(item);
409
                ReleaseCOMObjects(currentOPC);
410
            }
411

    
412
            if (_OPC_Infos.Count > 0)
413
            {
414
                foreach (var item in _OPC_Infos)
415
                {
416
                    LMRepresentation removeRep = dataSource.GetRepresentation(item.CurrentRepID);
417
                    LMSymbol removeSymbol = dataSource.GetSymbol(item.CurrentRepID);
418
                    LMOPC removeOPC = dataSource.GetOPC(removeSymbol.ModelItemID);
419

    
420
                    LMRepresentation findRep = dataSource.GetRepresentation(item.FindRepID);
421
                    LMOPC findOPC = dataSource.GetOPC(findRep.ModelItemID);
422
                    LMOPC pairOPC = findOPC.pairedWithOPCObject;
423

    
424
                    int mirror = removeSymbol.get_IsMirroredIndex();
425
                    double angle = Convert.ToDouble(removeSymbol.get_RotationAngle());
426
                    double x = removeSymbol.get_XCoordinate();
427
                    double y = removeSymbol.get_YCoordinate();
428
                    LMConnector connConnector = null;
429
                    
430
                    foreach (LMConnector connector in removeSymbol.Connect1Connectors)
431
                        if (connector.get_ItemStatus() == "Active")
432
                            connConnector = connector;
433
                    if (connConnector == null)
434
                        foreach (LMConnector connector in removeSymbol.Connect2Connectors)
435
                            if (connector.get_ItemStatus() == "Active")
436
                                connConnector = connector;
437
                    
438
                    if (connConnector != null)
439
                    {
440
                        ZoomObjectByGraphicOID(removeRep.get_GraphicOID().ToString());
441

    
442
                        placement.PIDRemovePlacement(removeRep);
443
                        removeRep.Commit();
444

    
445
                        LMSymbol newSymbol = placement.PIDPlaceSymbol(findRep.get_FileName(), x, y, mirror, angle, pairOPC.AsLMAItem(), connConnector);
446
                        newSymbol.Commit();
447

    
448
                        LMOPC newOPC = dataSource.GetOPC(newSymbol.ModelItemID);
449
                        object descValue = removeOPC.get_Description();
450
                        object toFromValue = removeOPC.get_ToFromText();
451
                        newOPC.set_Description(descValue);
452
                        newOPC.set_ToFromText(toFromValue);
453
                        newOPC.Commit();
454

    
455
                        ReleaseCOMObjects(newSymbol);
456
                        ReleaseCOMObjects(newOPC);
457
                    }
458

    
459
                    ReleaseCOMObjects(findOPC);
460
                    ReleaseCOMObjects(pairOPC);
461
                    ReleaseCOMObjects(findRep);
462
                    ReleaseCOMObjects(removeOPC);
463
                    ReleaseCOMObjects(removeSymbol);
464
                    ReleaseCOMObjects(removeRep);
465
                    ReleaseCOMObjects(connConnector);
466
                }
467

    
468
                application.ActiveDocument.Save();
469
            }
470

    
471
            workbook.EndUpdate();
472
            workbook.Worksheets[0].Columns.AutoFit(0, 3);
473
            SaveFileDialog save = new SaveFileDialog();
474
            save.Filter = "excel file (*.xlsx)|*.xlsx";
475
            if (save.ShowDialog() == DialogResult.OK)
476
            {
477
                workbook.SaveDocument(save.FileName, DevExpress.Spreadsheet.DocumentFormat.Xlsx);
478
            }
479
            workbook.Dispose();
480

    
481
            foreach (var item in CurrentDrawing)
482
                ReleaseCOMObjects(item);
483
            foreach (var item in OtherDrawing)
484
                ReleaseCOMObjects(item);
485

    
486
            ReleaseCOMObjects(representations);
487

    
488
            ReleaseCOMObjects(filter2);
489
            ReleaseCOMObjects(criterion2);
490
            ReleaseCOMObjects(drawing);
491
            ReleaseCOMObjects(drawings);
492
            ReleaseCOMObjects(filter);
493
            ReleaseCOMObjects(criterion);
494

    
495
            ReleaseCOMObjects(dataSource);
496
            ReleaseCOMObjects(placement);
497

    
498
            return;
499

    
500
            //DataTable tOPCInfo = Project_DB.SelectOPCInfo();
501
            //DataTable tOPCRelations = Project_DB.SelectOPCRelations();
502
            //DataTable tDrawingInfo = Project_DB.SelectDrawingInfo();
503
            //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
504

    
505
            //foreach (DataRow row in tOPCInfo.Rows)
506
            //{
507
            //    if (!Convert.ToBoolean(row["PAIRED"]))
508
            //    {
509
            //        string drawingUID = row["ID2_DRAWING_UID"].ToString();
510
            //        string OPCUID = row["ID2_OPC_UID"].ToString();
511
            //        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));
512

    
513
            //        if (rows.Length == 2)
514
            //        {
515
            //            string fromDrawingsUID1 = rows[0]["From_Drawings_UID"].ToString();
516
            //            string fromDrawingsUID2 = rows[1]["From_Drawings_UID"].ToString();
517
            //            string fromOPCUID1 = rows[0]["From_OPC_UID"].ToString();
518
            //            string fromOPCUID2 = rows[1]["From_OPC_UID"].ToString();
519
            //            string toDrawingsUID1 = rows[0]["To_Drawings_UID"].ToString();
520
            //            string toDrawingsUID2 = rows[1]["To_Drawings_UID"].ToString();
521
            //            string toOPCUID1 = rows[0]["To_OPC_UID"].ToString();
522
            //            string toOPCUID2 = rows[1]["To_OPC_UID"].ToString();
523

    
524
            //            DataRow[] fromDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", fromDrawingsUID1));
525
            //            DataRow[] toDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", toDrawingsUID1));
526
            //            DataRow[] fromOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", fromOPCUID1));
527
            //            DataRow[] toOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", toOPCUID1));
528

    
529
            //            if (fromOPCUID1 == toOPCUID2 && fromOPCUID2 == toOPCUID1 && fromDrawing.Length == 1 && toDrawing.Length == 1 && fromOPCInfoRows.Length == 1 && toOPCInfoRows.Length == 1)
530
            //            {
531
            //                DataRow fromOPCInfoRow = fromOPCInfoRows[0];
532
            //                DataRow toOPCInfoRow = toOPCInfoRows[0];
533
            //                string fromOPCModelId = fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
534
            //                string toOPCModelId = toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
535
            //                string toDrawingName = toDrawing[0]["DRAWINGNAME"].ToString();
536
            //                List<string[]> toOPCAttributes = JsonConvert.DeserializeObject<List<string[]>>(toOPCInfoRow["ATTRIBUTES"].ToString());
537
            //                AutoModeling_OPC opc = new AutoModeling_OPC(dApplication, application, fromOPCModelId, toOPCModelId, toDrawingName, toOPCAttributes);
538
            //                if (opc.Run())
539
            //                {
540
            //                    fromOPCInfoRow["PAIRED"] = true;
541
            //                    toOPCInfoRow["PAIRED"] = true;
542

    
543
            //                    Project_DB.InsertOPCInfo(fromOPCInfoRow["ID2_OPC_UID"].ToString(), fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), fromOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
544
            //                    Project_DB.InsertOPCInfo(toOPCInfoRow["ID2_OPC_UID"].ToString(), toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), toOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
545
            //                }
546
            //            }
547
            //        }
548
            //    }
549
            //}
550

    
551
            //tOPCInfo.Dispose();
552
            //tOPCRelations.Dispose();
553
            //tDrawingInfo.Dispose();
554
            //ReleaseCOMObjects(dApplication);
555
            //dApplication = null;
556

    
557
            //MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
558
        }
559

    
560
        public void ReleaseCOMObjects(params object[] objVars)
561
        {
562
            int intNewRefCount = 0;
563
            foreach (object obj in objVars)
564
            {
565
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
566
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
567
            }
568
        }
569

    
570
        
571

    
572
        #region SPPID Utils
573

    
574
        #region Symmetry
575
        private void btnSymmetry_Click(object sender, EventArgs e)
576
        {
577
            if (application.ActiveSelectSet.Count == 1 && application.ActiveSelectSet[0].GetType() == typeof(Symbol2d))
578
            {
579
                int symCount = (int)spinEditSymmetry.Value;
580
                Symbol2d symbol = application.ActiveSelectSet[0] as Symbol2d;
581
                double x, y;
582
                symbol.GetOrigin(out x, out y);
583
                string rep = GetRepresentationId(symbol);
584
                List<string> verticalRepID = new List<string>();
585
                List<string> horizontalRepID = new List<string>();
586

    
587
                if ((symbol.LinearName.Contains("Piping") ||
588
                    symbol.LinearName.Contains("Instrument")) &&
589
                    !string.IsNullOrEmpty(rep))
590
                {
591
                    Placement placement = new Placement();
592
                    LMADataSource dataSource = placement.PIDDataSource;
593
                    List<List<string>> datas = SetSymbol(dataSource, rep, x, y, symCount);
594
                    Dictionary<SymmetryArrow, List<string>> resultDatas = new Dictionary<SymmetryArrow, List<string>>();
595
                    if (datas.Count >= 2 && datas.Find(loop => loop.Count != symCount) == null)
596
                    {
597
                        SymmetryArrow arrow = SymmetryArrow.None;
598
                        foreach (var data in datas)
599
                        {
600
                            LMSymbol firstSymbol = dataSource.GetSymbol(data[0]);
601
                            double fX = firstSymbol.get_XCoordinate();
602
                            double fY = firstSymbol.get_YCoordinate();
603

    
604
                            SlopeType type = SPPIDUtil.CalcSlope(x, y, fX, fY, 1);
605
                            if (type == SlopeType.HORIZONTAL)
606
                            {
607
                                if (fX < x)
608
                                {
609
                                    arrow |= SymmetryArrow.Left;
610
                                    resultDatas.Add(SymmetryArrow.Left, data);
611
                                }
612
                                else
613
                                {
614
                                    arrow |= SymmetryArrow.Right;
615
                                    resultDatas.Add(SymmetryArrow.Right, data);
616
                                }
617

    
618
                            }
619
                            else if (type == SlopeType.VERTICAL)
620
                            {
621
                                if (fY < y)
622
                                {
623
                                    arrow |= SymmetryArrow.Down;
624
                                    resultDatas.Add(SymmetryArrow.Down, data);
625
                                }
626
                                else
627
                                {
628
                                    arrow |= SymmetryArrow.Up;
629
                                    resultDatas.Add(SymmetryArrow.Up, data);
630
                                }
631
                            }
632

    
633
                            ReleaseCOMObjects(firstSymbol);
634
                        }
635

    
636
                        SymmetryForm form = new SymmetryForm(arrow);
637
                        if (form.ShowDialog() == DialogResult.OK)
638
                        {
639
                            MoveByResult(dataSource, resultDatas, x, y, form.Result, rep);
640
                        }
641
                    }
642
                    else
643
                        MessageBox.Show("Check Symmetry Rules", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
644
                    ReleaseCOMObjects(dataSource);
645
                    ReleaseCOMObjects(placement);
646
                }
647
            }
648

    
649
            return;
650

    
651
            List<Symbol2d> symbols = new List<Symbol2d>();
652
            foreach (var item in application.ActiveSelectSet)
653
            {
654
                Type type = item.GetType();
655
                if (type == typeof(Symbol2d))
656
                {
657
                    Symbol2d symbol = item as Symbol2d;
658
                    if (symbol.LinearName.Contains("Piping") ||
659
                        symbol.LinearName.Contains("Instrument"))
660
                        symbols.Add(symbol);
661
                }
662
                //if (item.GetType() == typeof(Symbol2d) || item.GetType() == typeof(Point2d))
663
                //    items.Add(item);
664
            }
665

    
666
            List<Symbol2d> verticalSymbols = new List<Symbol2d>();
667
            List<Symbol2d> horizontalSymbols = new List<Symbol2d>();
668
            Symbol2d mainSymbol = null;
669
            for (int i = 0; i < symbols.Count - 1; i++)
670
            {
671
                Symbol2d symbol1 = symbols[i];
672
                for (int j = 1; j < symbols.Count; j++)
673
                {
674
                    Symbol2d symbol2 = symbols[j];
675

    
676
                    if (symbol1 != symbol2)
677
                    {
678
                        double x1, y1, x2, y2;
679
                        symbol1.GetOrigin(out x1, out y1);
680
                        symbol2.GetOrigin(out x2, out y2);
681
                        SlopeType slopeType = SPPIDUtil.CalcSlope(x1, y1, x2, y2, 1);
682
                        if (slopeType == SlopeType.HORIZONTAL)
683
                        {
684
                            if (!horizontalSymbols.Contains(symbol1))
685
                                horizontalSymbols.Add(symbol1);
686
                            if (!horizontalSymbols.Contains(symbol2))
687
                                horizontalSymbols.Add(symbol2);
688

    
689
                            if (verticalSymbols.Contains(symbol1))
690
                                mainSymbol = symbol1;
691
                            if (verticalSymbols.Contains(symbol2))
692
                                mainSymbol = symbol2;
693
                        }
694
                        else if (slopeType == SlopeType.VERTICAL)
695
                        {
696
                            if (!verticalSymbols.Contains(symbol1))
697
                                verticalSymbols.Add(symbol1);
698
                            if (!verticalSymbols.Contains(symbol2))
699
                                verticalSymbols.Add(symbol2);
700

    
701
                            if (horizontalSymbols.Contains(symbol1))
702
                                mainSymbol = symbol1;
703
                            if (horizontalSymbols.Contains(symbol2))
704
                                mainSymbol = symbol2;
705
                        }
706
                    }
707
                }
708
            }
709

    
710
            application.ActiveSelectSet.RemoveAll();
711
            foreach (var item in verticalSymbols)
712
                application.ActiveSelectSet.Add(item);
713
            foreach (var item in horizontalSymbols)
714
                application.ActiveSelectSet.Add(item);
715
                
716

    
717
            if (MessageBox.Show("Continue?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
718
            {
719
                application.ActiveSelectSet.RemoveAll();
720

    
721
            }
722
        }
723
        private void MoveByResult(LMADataSource dataSource,Dictionary<SymmetryArrow, List<string>> resultDatas, double x, double y, SymmetryArrow arrow, string centerRepID)
724
        {
725
            List<string> datas = resultDatas[arrow];
726
            foreach (var item in resultDatas)
727
            {
728
                if (item.Key != arrow)
729
                {
730
                    for (int i = 0; i < item.Value.Count; i++)
731
                    {
732
                        Symbol2d moveSymbol2d = GetSymbol2DByRepID(dataSource, item.Value[i]);
733
                        Symbol2d symbol2d = GetSymbol2DByRepID(dataSource, datas[i]);
734

    
735
                        double x1, y1, x2, y2;
736
                        symbol2d.GetOrigin(out x1, out y1);
737
                        moveSymbol2d.GetOrigin(out x2, out y2);
738
                        double distance = SPPIDUtil.CalcPointToPointdDistance(x, y, x1, y1);
739

    
740
                        string symbol1RepID;
741
                        string symbol2RepID;
742
                        List<Point2d> point2ds;
743
                        if (i == 0)
744
                        {
745
                            symbol1RepID = centerRepID;
746
                            symbol2RepID = item.Value[i];
747
                        }
748
                        else
749
                        {
750
                            symbol1RepID = item.Value[i - 1];
751
                            symbol2RepID = item.Value[i];
752
                        }
753
                        point2ds = FindAllPoint2d(dataSource, symbol1RepID, symbol2RepID);
754
                        double moveX = 0;
755
                        double moveY = 0;
756

    
757
                        if (item.Key == SymmetryArrow.Left)
758
                            moveX = x - distance - x2;
759
                        else if (item.Key == SymmetryArrow.Right)
760
                            moveX = x + distance - x2;
761
                        else if (item.Key == SymmetryArrow.Down)
762
                            moveY = y - distance - y2;
763
                        else if (item.Key == SymmetryArrow.Up)
764
                            moveY = y + distance - y2;
765

    
766
                        moveSymbol2d.Move(0, 0, moveX, moveY);
767
                        MovePoint2d(dataSource, item.Value[i], arrow, moveX, moveY);
768
                        foreach (var point in point2ds)
769
                        {
770
                            LMSymbol branch = dataSource.GetSymbol(GetRepresentationId(point));
771
                            LMSymbol connSymbol = GetFirstSymbolBySlope(dataSource, branch, symbol1RepID, symbol2RepID);
772
                            point.Move(0, 0, moveX, moveY);
773
                            if (connSymbol != null)
774
                            {
775
                                Symbol2d connSymbol2d = GetSymbol2DByRepID(dataSource, connSymbol.AsLMRepresentation().Id);
776
                                connSymbol2d.Move(0, 0, moveX, moveY);
777
                                ReleaseCOMObjects(connSymbol);
778
                            }
779
                            ReleaseCOMObjects(branch);
780
                        }
781
                            
782
                    }
783
                }
784
            }
785
        }
786
        private LMSymbol GetFirstSymbolBySlope(LMADataSource dataSource, LMSymbol branch, string symbol1RepID, string symbol2RepID)
787
        {
788
            LMSymbol result = null;
789
            foreach (LMConnector connector in branch.Connect1Connectors)
790
            {
791
                if (connector.ConnectItem1SymbolObject != null && 
792
                    connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
793
                    connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
794
                {
795
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
796
                    if (repID != symbol1RepID && repID != symbol2RepID)
797
                        result = connector.ConnectItem1SymbolObject;
798
                }
799

    
800
                if (connector.ConnectItem2SymbolObject != null &&
801
                    connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
802
                    connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
803
                {
804
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
805
                    if (repID != symbol1RepID && repID != symbol2RepID)
806
                        result = connector.ConnectItem2SymbolObject;
807
                }
808
            }
809

    
810
            foreach (LMConnector connector in branch.Connect2Connectors)
811
            {
812
                if (connector.ConnectItem1SymbolObject != null &&
813
                    connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
814
                    connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
815
                {
816
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
817
                    if (repID != symbol1RepID && repID != symbol2RepID)
818
                        result = connector.ConnectItem1SymbolObject;
819
                }
820

    
821
                if (connector.ConnectItem2SymbolObject != null &&
822
                    connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
823
                    connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
824
                {
825
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
826
                    if (repID != symbol1RepID && repID != symbol2RepID)
827
                        result = connector.ConnectItem2SymbolObject;
828
                }
829
            }
830

    
831

    
832
            return result;
833
        }
834
        private string GetRepresentationId(Symbol2d symbol)
835
        {
836
            foreach (var attributes in symbol.AttributeSets)
837
            {
838
                foreach (var attribute in attributes)
839
                {
840
                    string name = attribute.Name;
841
                    object value = attribute.GetValue();
842
                    if (name == "DrawingID")
843
                    {
844
                        return value.ToString();  
845
                    }
846
                }
847
            }
848
            return null;
849
        }
850
        private string GetRepresentationId(Point2d point2d)
851
        {
852
            foreach (var attributes in point2d.AttributeSets)
853
            {
854
                foreach (var attribute in attributes)
855
                {
856
                    string name = attribute.Name;
857
                    object value = attribute.GetValue();
858
                    if (name == "DrawingID")
859
                    {
860
                        return value.ToString();
861
                    }
862
                }
863
            }
864
            return null;
865
        }
866
        private List<List<string>> SetSymbol(LMADataSource dataSource, string rep, double x, double y, int count)
867
        {
868
            LMSymbol _LMSymbol = dataSource.GetSymbol(rep);
869
            List<List<string>> result = new List<List<string>>();
870
            List<string> oldIDs = new List<string>() { rep };
871
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
872
            {
873
                if (connector.get_ItemStatus() != "Active")
874
                    continue;
875

    
876
                string repID = connector.AsLMRepresentation().Id;
877
                string status = connector.get_ItemStatus();
878
                if (status == "Active" && !oldIDs.Contains(repID))
879
                {
880
                    List<string> symbols = new List<string>();
881
                    oldIDs.Add(repID);
882
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
883
                    result.Add(symbols);
884
                }
885
            }
886

    
887
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
888
            {
889
                if (connector.get_ItemStatus() != "Active")
890
                    continue;
891

    
892
                string repID = connector.AsLMRepresentation().Id;
893
                string status = connector.get_ItemStatus();
894
                if (status == "Active" && !oldIDs.Contains(repID))
895
                {
896
                    List<string> symbols = new List<string>();
897
                    oldIDs.Add(repID);
898
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
899
                    result.Add(symbols);
900
                }
901
            }
902

    
903
            ReleaseCOMObjects(_LMSymbol);
904
            return result;
905
        }
906
        private void loop(LMADataSource dataSource, List<string> oldIDs, List<string> symbols, LMConnector connector, double x, double y, int count)
907
        {
908
            if (symbols.Count >= count)
909
                return;
910

    
911
            if (connector.ConnectItem1SymbolObject != null && !oldIDs.Contains(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id))
912
            {
913
                string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
914
                oldIDs.Add(repID);
915
                if (connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch" && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
916
                {
917
                    double sX = connector.ConnectItem1SymbolObject.get_XCoordinate(), sY = connector.ConnectItem1SymbolObject.get_YCoordinate(); ;
918
                    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, sX, sY, 1);
919
                    if (slopeType == SlopeType.HORIZONTAL || slopeType == SlopeType.VERTICAL)
920
                        symbols.Add(repID);
921
                }
922

    
923
                loop(dataSource, oldIDs, symbols, connector.ConnectItem1SymbolObject, x, y, count);
924
            }
925

    
926
            if (symbols.Count >= count)
927
                return;
928

    
929
            if (connector.ConnectItem2SymbolObject != null && !oldIDs.Contains(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id))
930
            {
931
                string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
932
                oldIDs.Add(repID);
933
                if (connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch" && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
934
                {
935
                    double sX = connector.ConnectItem2SymbolObject.get_XCoordinate(), sY = connector.ConnectItem2SymbolObject.get_YCoordinate(); ;
936
                    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, sX, sY, 1);
937
                    if (slopeType == SlopeType.HORIZONTAL || slopeType == SlopeType.VERTICAL)
938
                        symbols.Add(repID);
939
                }
940

    
941
                loop(dataSource, oldIDs, symbols, connector.ConnectItem2SymbolObject, x, y, count);
942
            }
943
        }
944
        private void loop(LMADataSource dataSource, List<string> oldIDs, List<string> symbols, LMSymbol _LMSymbol, double x, double y, int count)
945
        {
946
            if (symbols.Count >= count)
947
                return;
948

    
949
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
950
            {
951
                if (connector.get_ItemStatus() != "Active")
952
                    continue;
953

    
954
                string repID = connector.AsLMRepresentation().Id;
955
                string status = connector.get_ItemStatus();
956
                if (status == "Active" && !oldIDs.Contains(repID))
957
                {
958
                    oldIDs.Add(repID);
959
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
960
                }
961
            }
962

    
963
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
964
            {
965
                if (connector.get_ItemStatus() != "Active")
966
                    continue;
967

    
968
                string repID = connector.AsLMRepresentation().Id;
969
                string status = connector.get_ItemStatus();
970
                if (status == "Active" && !oldIDs.Contains(repID))
971
                {
972
                    oldIDs.Add(repID);
973
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
974
                }
975
            }
976
        }
977
        private Symbol2d GetSymbol2DByRepID(LMADataSource dataSource, string repID)
978
        {
979
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
980
            Symbol2d symbol2D = application.ActiveDocument.ActiveSheet.DrawingObjects[_LMSymbol.get_GraphicOID().ToString()];
981
            ReleaseCOMObjects(_LMSymbol);
982
            return symbol2D;
983
        }
984
        private void MovePoint2d(LMADataSource datasource, string repID, SymmetryArrow arrow, double moveX, double moveY)
985
        {
986
            LMSymbol _LMSymbol = datasource.GetSymbol(repID);
987
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
988
            {
989
                if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()))
990
                {
991
                    if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
992
                    {
993
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()];
994
                        point.X += moveX;
995
                        point.Y += moveY;
996
                    }
997
                    else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
998
                    {
999
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()];
1000
                        point.X += moveX;
1001
                        point.Y += moveY;
1002
                    }
1003
                }
1004
            }
1005
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
1006
            {
1007
                if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()))
1008
                {
1009
                    if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
1010
                    {
1011
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()];
1012
                        point.X += moveX;
1013
                        point.Y += moveY;
1014
                    }
1015
                    else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
1016
                    {
1017
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()];
1018
                        point.X += moveX;
1019
                        point.Y += moveY;
1020
                    }
1021
                }
1022
            }
1023
            ReleaseCOMObjects(_LMSymbol);
1024
        }
1025
        private List<Point2d> FindAllPoint2d(LMADataSource dataSource, string repID, string nextRepID)
1026
        {
1027
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
1028
            List<string> endIDs = new List<string>() { repID };
1029
            List<string> graphicOIDs = new List<string>();
1030
            List<Point2d> result = new List<Point2d>();
1031
            FindPointsLoop(dataSource, _LMSymbol, endIDs, nextRepID, graphicOIDs);
1032
            ReleaseCOMObjects(_LMSymbol);
1033
            foreach (var item in graphicOIDs)
1034
            {
1035
                Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[item] as Point2d;
1036
                result.Add(point);
1037
            }
1038
            
1039
            return result;
1040
        }
1041
        private bool FindPointsLoop(LMADataSource dataSource, LMSymbol _LMSymbol, List<string> endIDs, string targetRepID, List<string> graphicOIDs)
1042
        {
1043
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
1044
            {
1045
                if (connector.get_ItemStatus() != "Active")
1046
                    continue;
1047

    
1048
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
1049
                {
1050
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
1051
                    if (!endIDs.Contains(repID))
1052
                    {
1053
                        endIDs.Add(repID);
1054
                        if (connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
1055
                        {
1056
                            if (FindPointsLoop(dataSource, connector.ConnectItem1SymbolObject, endIDs, targetRepID, graphicOIDs))
1057
                            {
1058
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1059
                                    graphicOIDs.Add(connector.ConnectItem1SymbolObject.get_GraphicOID().ToString());
1060
                                return true;
1061
                            }
1062
                        }
1063
                        else if (targetRepID == repID)
1064
                        {
1065
                            return true;
1066
                        }
1067
                    }
1068
                }
1069
                if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
1070
                {
1071
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
1072
                    if (!endIDs.Contains(repID))
1073
                    {
1074
                        endIDs.Add(repID);
1075
                        if (connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
1076
                        {
1077
                            if (FindPointsLoop(dataSource, connector.ConnectItem2SymbolObject, endIDs, targetRepID, graphicOIDs))
1078
                            {
1079
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1080
                                    graphicOIDs.Add(connector.ConnectItem2SymbolObject.get_GraphicOID().ToString());
1081
                                return true;
1082
                            }
1083
                        }
1084
                        else if (targetRepID == repID)
1085
                        {
1086
                            return true;
1087
                        }
1088
                    }
1089
                }
1090
            }
1091
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
1092
            {
1093
                if (connector.get_ItemStatus() != "Active")
1094
                    continue;
1095

    
1096
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
1097
                {
1098
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
1099
                    if (!endIDs.Contains(repID))
1100
                    {
1101
                        endIDs.Add(repID);
1102
                        if (connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
1103
                        {
1104
                            if (FindPointsLoop(dataSource, connector.ConnectItem1SymbolObject, endIDs, targetRepID, graphicOIDs))
1105
                            {
1106
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1107
                                    graphicOIDs.Add(connector.ConnectItem1SymbolObject.get_GraphicOID().ToString());
1108
                                return true;
1109
                            }
1110
                        }
1111
                        else if (targetRepID == repID)
1112
                        {
1113
                            return true;
1114
                        }
1115
                    }
1116
                }
1117
                if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
1118
                {
1119
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
1120
                    if (!endIDs.Contains(repID))
1121
                    {
1122
                        endIDs.Add(repID);
1123
                        if (connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
1124
                        {
1125
                            if (FindPointsLoop(dataSource, connector.ConnectItem2SymbolObject, endIDs, targetRepID, graphicOIDs))
1126
                            {
1127
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1128
                                    graphicOIDs.Add(connector.ConnectItem2SymbolObject.get_GraphicOID().ToString());
1129
                                return true;
1130
                            }
1131
                        }
1132
                        else if (targetRepID == repID)
1133
                        {
1134
                            return true;
1135
                        }
1136
                    }
1137
                }
1138
            }
1139

    
1140
            return false;
1141
        }
1142
        #endregion
1143

    
1144
        #region SpecBreak
1145
        private void btnSpecBreakRelocation_Click(object sender, EventArgs e)
1146
        {
1147
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
1148
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
1149
            application = wApp.RADApplication;
1150

    
1151
            int count = application.ActiveSelectSet.Count;
1152
            int dependencyCount = 0;
1153
            foreach (var item in application.ActiveSelectSet)
1154
                if (item.GetType() == typeof(DependencyObject))
1155
                    dependencyCount++;
1156

    
1157
            if (count > 0 && application.ActiveSelectSet[0].GetType() == typeof(DependencyObject))
1158
            {
1159
                MessageBox.Show("First selected item is DependencyObject!\r\nPlease move symbol", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1160
                return;
1161
            }
1162

    
1163
            if ((count == 3 || count == 4) && application.ActiveSelectSet[0].GetType() == typeof(Symbol2d) &&
1164
                (dependencyCount == 2 || dependencyCount == 3))
1165
            {
1166
                Symbol2d symbol = application.ActiveSelectSet[0] as Symbol2d;
1167

    
1168
                if (!symbol.DefinitionName.Contains(@"Design\Annotation\Graphics\"))
1169
                {
1170
                    MessageBox.Show("Select SpecBreak!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1171
                    return;
1172
                }
1173

    
1174
                DependencyObject dependency1 = application.ActiveSelectSet[1] as DependencyObject;
1175
                DependencyObject dependency2 = application.ActiveSelectSet[2] as DependencyObject;
1176
                DependencyObject dependency3 = null;
1177
                if (count == 4)
1178
                    dependency3 = application.ActiveSelectSet[3] as DependencyObject;
1179

    
1180
                application.ActiveSelectSet.RemoveAll();
1181
                double angle = symbol.Angle;
1182
                if (symbol.GetTransform().HasReflection)
1183
                    angle += Math.PI;
1184

    
1185
                double degree = double.NaN;
1186
                if (angle > -0.1 && angle < 0.1)
1187
                    degree = 0;
1188
                else if (angle > 1.56 && angle < 1.58)
1189
                    degree = 90;
1190
                else if (angle > 3.13 && angle < 3.15)
1191
                    degree = 180;
1192
                else if (angle > 4.7 && angle < 4.72)
1193
                    degree = 270;
1194
                else if (angle > 6.27 && angle < 6.29)
1195
                    degree = 0;
1196
                else if (angle > -1.58 && angle < -1.56)
1197
                    degree = 270;
1198
                else if (angle > -3.15 && angle < -3.13)
1199
                    degree = 180;
1200
                else
1201
                    throw new Exception("Check Angle");
1202

    
1203

    
1204
                double originX, originY;
1205
                symbol.GetOrigin(out originX, out originY);
1206

    
1207
                double crossX = 0;
1208
                double crossY = 0;
1209
                double topX = 0;
1210
                double topY = 0;
1211
                foreach (var item in symbol.DrawingObjects)
1212
                {
1213
                    Line2d line2d = item as Line2d;
1214
                    if (line2d != null)
1215
                    {
1216
                        double x1, y1, x2, y2;
1217
                        line2d.GetStartPoint(out x1, out y1);
1218
                        line2d.GetEndPoint(out x2, out y2);
1219
                        SlopeType slopeType = SPPIDUtil.CalcSlope(x1, y1, x2, y2, 0.1);
1220

    
1221
                        if (slopeType == SlopeType.HORIZONTAL)
1222
                        {
1223
                            if (crossY == 0)
1224
                                crossY = y1;
1225
                            else
1226
                                crossY = (crossY + y1) / 2;
1227

    
1228
                            switch (degree)
1229
                            {
1230
                                case 90:
1231
                                    if (topX == 0)
1232
                                        topX = Math.Min(x1, x2);
1233
                                    else
1234
                                        topX = Math.Min(topX, Math.Min(x1, x2));
1235
                                    break;
1236
                                case 270:
1237
                                    if (topX == 0)
1238
                                        topX = Math.Max(x1, x2);
1239
                                    else
1240
                                        topX = Math.Max(topX, Math.Max(x1, x2));
1241
                                    break;
1242
                                default:
1243
                                    break;
1244
                            }
1245
                        }
1246
                        else if (slopeType == SlopeType.VERTICAL)
1247
                        {
1248
                            if (crossX == 0)
1249
                                crossX = x1;
1250
                            else
1251
                                crossX = (crossX + x1) / 2;
1252

    
1253
                            switch (degree)
1254
                            {
1255
                                case 0:
1256
                                    if (topY == 0)
1257
                                        topY = Math.Max(y1, y2);
1258
                                    else
1259
                                        topY = Math.Max(topY, Math.Max(y1, y2));
1260
                                    break;
1261
                                case 180:
1262
                                    if (topY == 0)
1263
                                        topY = Math.Min(y1, y2);
1264
                                    else
1265
                                        topY = Math.Min(topY, Math.Min(y1, y2));
1266
                                    break;
1267
                                default:
1268
                                    break;
1269
                            }
1270
                        }
1271
                    }
1272
                }
1273
                switch (degree)
1274
                {
1275
                    case 0:
1276
                        crossX = originX;
1277
                        topX = crossX;
1278
                        break;
1279
                    case 90:
1280
                        crossY = originY;
1281
                        topY = crossY;
1282
                        break;
1283
                    case 180:
1284
                        crossX = originX;
1285
                        topX = crossX;
1286
                        break;
1287
                    case 270:
1288
                        crossY = originY;
1289
                        topY = crossY;
1290
                        break;
1291
                    default:
1292
                        break;
1293
                }
1294

    
1295
                SpecBreakRelocation(degree, originX, originY, crossX, crossY, topX, topY, dependency1, dependency2, dependency3);
1296
                SetSpecBreakParameters(symbol, dependency1, dependency2, degree);
1297
            }
1298
            else
1299
            {
1300
                MessageBox.Show("Check Rule!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1301
            }
1302
        }
1303
        private void SetSpecBreakParameters(Symbol2d symbol, DependencyObject dependency1, DependencyObject dependency2, double degree)
1304
        {
1305
            bool mirror = symbol.GetTransform().HasReflection;
1306
            string repID = GetRepresentationId(symbol);
1307
            Placement placement = new Placement();
1308
            LMADataSource dataSource = placement.PIDDataSource;
1309

    
1310
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
1311
            if (_LMSymbol != null)
1312
            {
1313
                double sX1, sY1, sX2, sY2, sOriginX, sOriginY;
1314
                symbol.Range(out sX1, out sY1, out sX2, out sY2);
1315
                symbol.GetOrigin(out sOriginX, out sOriginY);
1316
                double d1X1, d1Y1, d1X2, d1Y2, d2X1, d2Y1, d2X2, d2Y2;
1317
                FindRangeWithOutLineString2d(dependency1, out d1X1, out d1Y1, out d1X2, out d1Y2);
1318
                FindRangeWithOutLineString2d(dependency2, out d2X1, out d2Y1, out d2X2, out d2Y2);
1319

    
1320
                double left = 0, right = 0;
1321
                switch (degree)
1322
                {
1323
                    case 0:
1324
                        if (!mirror)
1325
                        {
1326
                            left = Math.Abs(d1X1 - sOriginX);
1327
                            right = Math.Abs(d2X2 - sOriginX);
1328
                        }
1329
                        else
1330
                        {
1331
                            right = Math.Abs(d1X1 - sOriginX);
1332
                            left = Math.Abs(d2X2 - sOriginX);
1333
                        }
1334
                        break;
1335
                    case 90:
1336
                        if (!mirror)
1337
                        {
1338
                            left = Math.Abs(d1Y1 - sOriginY);
1339
                            right = Math.Abs(d2Y2 - sOriginY);
1340
                        }
1341
                        else
1342
                        {
1343
                            right = Math.Abs(d1Y1 - sOriginY);
1344
                            left = Math.Abs(d2Y2 - sOriginY);
1345
                        }
1346
                        break;
1347
                    case 180:
1348
                        if (!mirror)
1349
                        {
1350
                            right = Math.Abs(d1X1 - sOriginX);
1351
                            left = Math.Abs(d2X2 - sOriginX);
1352
                        }
1353
                        else
1354
                        {
1355
                            left = Math.Abs(d1X1 - sOriginX);
1356
                            right = Math.Abs(d2X2 - sOriginX);
1357
                            
1358
                        }
1359
                        break;
1360
                    case 270:
1361
                        if (!mirror)
1362
                        {
1363
                            right = Math.Abs(d1Y1 - sOriginY);
1364
                            left = Math.Abs(d2Y2 - sOriginY);
1365
                        }
1366
                        else
1367
                        {
1368
                            left = Math.Abs(d1Y1 - sOriginY);
1369
                            right = Math.Abs(d2Y2 - sOriginY);
1370
                        }
1371
                        break;
1372
                    default:
1373
                        break;
1374
                }
1375

    
1376
                string[] array1 = new string[] { "", "Left", "Right" };
1377
                string[] array2 = new string[] { "", left.ToString(), right.ToString() };
1378
                placement.PIDApplyParameters(_LMSymbol.AsLMRepresentation(), array1, array2);
1379
            }
1380

    
1381
            ReleaseCOMObjects(_LMSymbol);
1382
            ReleaseCOMObjects(dataSource);
1383
            ReleaseCOMObjects(placement);
1384
        }
1385
        private void SpecBreakRelocation(double degree, double originX, double originY, double crossX, double crossY, double topX, double topY, DependencyObject dependency1, DependencyObject dependency2, DependencyObject dependency3)
1386
        {
1387
            double d1X1, d1Y1, d1X2, d1Y2, d2X1, d2Y1, d2X2, d2Y2, d3X1 = 0, d3Y1 = 0, d3X2 = 0, d3Y2 = 0;
1388
            
1389
            FindRangeWithOutLineString2d(dependency1, out d1X1, out d1Y1, out d1X2, out d1Y2);
1390
            FindRangeWithOutLineString2d(dependency2, out d2X1, out d2Y1, out d2X2, out d2Y2);
1391
            if (dependency3 != null)
1392
                FindRangeWithOutLineString2dAndTextBox(dependency3, out d3X1, out d3Y1, out d3X2, out d3Y2);
1393

    
1394
            GridSetting gridSetting = GridSetting.GetInstance();
1395
            double move = gridSetting.Length / 2;
1396
            switch (degree)
1397
            {
1398
                case 0:
1399
                    MoveDependency(dependency1, d1X2, d1Y2, crossX - move, crossY);
1400
                    MoveDependency(dependency2, d2X1, d2Y2, crossX + move, crossY);
1401
                    if (dependency3 != null)
1402
                        MoveDependency(dependency3, (d3X1 + d3X2) / 2, d3Y1, topX, topY);
1403
                    break;
1404
                case 90:
1405
                    MoveDependency(dependency1, d1X1, d1Y2, crossX + move, crossY);
1406
                    MoveDependency(dependency2, d2X1, d2Y1, crossX + move, crossY);
1407
                    if (dependency3 != null)
1408
                        MoveDependency(dependency3, d3X1, (d3Y1 + d3Y2) / 2, originX, originY);
1409
                    break;
1410
                case 180:
1411
                    MoveDependency(dependency1, d1X2, d1Y1, crossX - move, crossY);
1412
                    MoveDependency(dependency2, d2X1, d2Y1, crossX + move, crossY);
1413
                    if (dependency3 != null)
1414
                        MoveDependency(dependency3, (d3X1 + d3X2) / 2, d3Y2, topX, topY);
1415
                    break;
1416
                case 270:
1417
                    MoveDependency(dependency1, d1X2, d1Y2, crossX - move, crossY);
1418
                    MoveDependency(dependency2, d2X2, d2Y1, crossX - move, crossY);
1419
                    if (dependency3 != null)
1420
                        MoveDependency(dependency3, d3X2, (d3Y1 + d3Y2) / 2, originX, originY);
1421
                    break;
1422
                default:
1423
                    break;
1424
            }
1425
        }
1426
        private void MoveDependency(DependencyObject dependency, double xFrom, double yFrom, double xTo, double yTo)
1427
        {
1428
            application.ActiveSelectSet.Add(dependency);
1429

    
1430
            Transform transform = dependency.GetTransform();
1431
            transform.DefineByMove2d(xTo - xFrom, yTo - yFrom);
1432
            application.ActiveSelectSet.Transform(transform, false);
1433

    
1434
            application.ActiveSelectSet.RemoveAll();
1435
        }
1436
        private string GetDrawingItemType(DependencyObject dependency)
1437
        {
1438
            string result = string.Empty;
1439

    
1440
            foreach (var attributes in dependency.AttributeSets)
1441
            {
1442
                foreach (var attribute in attributes)
1443
                {
1444
                    if (attribute.Name == "DrawingItemType")
1445
                        return attribute.GetValue().ToString();
1446
                }
1447
            }
1448

    
1449
            return result;
1450
        }
1451
        private void FindRangeWithOutLineString2d(DependencyObject dependency, out double x1, out double y1, out double x2, out double y2)
1452
        {
1453
            x1 = double.MaxValue;
1454
            y1 = double.MaxValue;
1455
            x2 = double.MinValue;
1456
            y2 = double.MinValue;
1457
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1458
            {
1459
                if (item.GetType() != typeof(LineString2d))
1460
                {
1461
                    double minX, minY, maxX, maxY;
1462
                    item.Range(out minX, out minY, out maxX, out maxY);
1463
                    if (x1 > minX)
1464
                        x1 = minX;
1465
                    if (y1 > minY)
1466
                        y1 = minY;
1467
                    if (x2 < maxX)
1468
                        x2 = maxX;
1469
                    if (y2 < maxY)
1470
                        y2 = maxY;
1471
                }
1472
            }
1473

    
1474
        }
1475
        private void FindWidthHeightWidthOutLineString2d(DependencyObject dependency, out double width, out double height)
1476
        {
1477
            width = 0;
1478
            height = 0;
1479

    
1480
            double x1 = double.MaxValue;
1481
            double y1 = double.MaxValue;
1482
            double x2 = double.MinValue;
1483
            double y2 = double.MinValue;
1484
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1485
            {
1486
                if (item.GetType() != typeof(LineString2d))
1487
                {
1488
                    double minX, minY, maxX, maxY;
1489
                    item.Range(out minX, out minY, out maxX, out maxY);
1490
                    if (x1 > minX)
1491
                        x1 = minX;
1492
                    if (y1 > minY)
1493
                        y1 = minY;
1494
                    if (x2 < maxX)
1495
                        x2 = maxX;
1496
                    if (y2 < maxY)
1497
                        y2 = maxY;
1498
                }
1499
            }
1500

    
1501
            width = x2 - x1;
1502
            height = y2 - y1;
1503
        }
1504
        private void FindRangeWithOutLineString2dAndTextBox(DependencyObject dependency, out double x1, out double y1, out double x2, out double y2)
1505
        {
1506
            x1 = double.MaxValue;
1507
            y1 = double.MaxValue;
1508
            x2 = double.MinValue;
1509
            y2 = double.MinValue;
1510
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1511
            {
1512
                if (item.GetType() != typeof(LineString2d) && item.GetType() != typeof(Ingr.RAD2D.TextBox))
1513
                {
1514
                    double minX, minY, maxX, maxY;
1515
                    item.Range(out minX, out minY, out maxX, out maxY);
1516
                    if (x1 > minX)
1517
                        x1 = minX;
1518
                    if (y1 > minY)
1519
                        y1 = minY;
1520
                    if (x2 < maxX)
1521
                        x2 = maxX;
1522
                    if (y2 < maxY)
1523
                        y2 = maxY;
1524
                }
1525
            }
1526

    
1527
        }
1528
        #endregion
1529

    
1530
        #region Hot Key
1531
        private void toggleSwitchSnapGrid_Toggled(object sender, EventArgs e)
1532
        {
1533
            if (toggleSwitchSnapGrid.IsOn)
1534
            {
1535
                RegisterHotKey(this.Handle, 0, (int)KeyModifier.Shift, Keys.A.GetHashCode());
1536
            }
1537
            else
1538
            {
1539
                UnregisterHotKey(this.Handle, 0);
1540
            }
1541
        }
1542
        private void toggleSwitchMoveSymbol_Toggled(object sender, EventArgs e)
1543
        {
1544
            if (toggleSwitchMoveSymbol.IsOn)
1545
            {
1546
                RegisterHotKey(this.Handle, 1, (int)KeyModifier.Shift, Keys.Left.GetHashCode());
1547
                RegisterHotKey(this.Handle, 2, (int)KeyModifier.Shift, Keys.Up.GetHashCode());
1548
                RegisterHotKey(this.Handle, 3, (int)KeyModifier.Shift, Keys.Right.GetHashCode());
1549
                RegisterHotKey(this.Handle, 4, (int)KeyModifier.Shift, Keys.Down.GetHashCode());
1550
            }
1551
            else
1552
            {
1553
                UnregisterHotKey(this.Handle, 1);
1554
                UnregisterHotKey(this.Handle, 2);
1555
                UnregisterHotKey(this.Handle, 3);
1556
                UnregisterHotKey(this.Handle, 4);
1557
            }
1558
        }
1559
        public void ClearHotKey()
1560
        {
1561
            if (toggleSwitchMoveSymbol.IsOn)
1562
            {
1563
                UnregisterHotKey(this.Handle, 1);
1564
                UnregisterHotKey(this.Handle, 2);
1565
                UnregisterHotKey(this.Handle, 3);
1566
                UnregisterHotKey(this.Handle, 4);
1567
            }
1568
            if (toggleSwitchSnapGrid.IsOn)
1569
            {
1570
                UnregisterHotKey(this.Handle, 0);
1571
            }
1572
        }
1573
        [System.Runtime.InteropServices.DllImport("user32.dll")]
1574
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
1575
        [System.Runtime.InteropServices.DllImport("user32.dll")]
1576
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
1577
        enum KeyModifier
1578
        {
1579
            None = 0,
1580
            Alt = 1,
1581
            Control = 2,
1582
            Shift = 4,
1583
            WinKey = 8
1584
        }
1585
        protected override void WndProc(ref Message m)
1586
        {
1587
            base.WndProc(ref m);
1588
            if (m.Msg == 0x0312)
1589
            {
1590
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
1591
                KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF);
1592
                int id = m.WParam.ToInt32();
1593
                switch (id)
1594
                {
1595
                    case 0:
1596
                        application.RunCommand(CommandConstants.igcmdGridSnap);
1597
                        break;
1598
                    case 1:
1599
                        MoveSymbol(Arrow.Left);
1600
                        break;
1601
                    case 2:
1602
                        MoveSymbol(Arrow.Up);
1603
                        break;
1604
                    case 3:
1605
                        MoveSymbol(Arrow.Right);
1606
                        break;
1607
                    case 4:
1608
                        MoveSymbol(Arrow.Down);
1609
                        break;
1610
                    default:
1611
                        break;
1612
                }
1613
                
1614
            }
1615

    
1616
        }
1617
        #endregion
1618

    
1619
        #region Move Symbol
1620
        enum Arrow
1621
        {
1622
            Left,
1623
            Up,
1624
            Right,
1625
            Down
1626
        }
1627
        private void MoveSymbol(Arrow arrow)
1628
        {
1629
            if (application.ActiveSelectSet.Count > 0)
1630
            {
1631
                Placement placement = new Placement();
1632
                LMADataSource dataSource = placement.PIDDataSource;
1633
                System.Collections.ObjectModel.Collection<DrawingObjectBase> originalDrawingObjectBases = new System.Collections.ObjectModel.Collection<DrawingObjectBase>();
1634
                foreach (var item in application.ActiveSelectSet)
1635
                    originalDrawingObjectBases.Add(item);
1636
                System.Collections.ObjectModel.Collection<DrawingObjectBase> drawingObjectBases = new System.Collections.ObjectModel.Collection<DrawingObjectBase>();
1637
                Transform transform = null;
1638
                foreach (DrawingObjectBase drawingObject in application.ActiveSelectSet)
1639
                {
1640
                    if (drawingObject.GetType() == typeof(Symbol2d))
1641
                    {
1642
                        Symbol2d symbol2D = drawingObject as Symbol2d;
1643
                        if (transform == null)
1644
                            transform = symbol2D.GetTransform();
1645
                        drawingObjectBases.Add(symbol2D);
1646
                        LMSymbol _LMSymbol = dataSource.GetSymbol(GetRepresentationId(symbol2D));
1647
                        if (_LMSymbol != null)
1648
                        {
1649
                            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
1650
                            {
1651
                                if (connector.get_ItemStatus() == "Active")
1652
                                {
1653
                                    #region Zero Length And Branch
1654
                                    if (Convert.ToBoolean(connector.get_IsZeroLength()))
1655
                                    {
1656
                                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolID != _LMSymbol.Id)
1657
                                        {
1658
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1659
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1660
                                                drawingObjectBases.Add(point2D);
1661
                                        }
1662
                                        else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolID != _LMSymbol.Id)
1663
                                        {
1664
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1665
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1666
                                                drawingObjectBases.Add(point2D);
1667
                                        }
1668
                                    }
1669
                                    #endregion
1670
                                    #region Not Zero Length And Branch And Vertical,Horizontal
1671
                                    else
1672
                                    {
1673
                                        if (connector.ConnectItem1SymbolObject != null && 
1674
                                            connector.ConnectItem1SymbolID != _LMSymbol.Id &&
1675
                                            connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
1676
                                            connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch" &&
1677
                                            IsMovePoint2D(connector.ConnectItem1SymbolObject, connector, arrow))
1678
                                        {
1679
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1680
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1681
                                                drawingObjectBases.Add(point2D);
1682
                                        }
1683
                                            
1684
                                        else if (connector.ConnectItem2SymbolObject != null && 
1685
                                            connector.ConnectItem2SymbolID != _LMSymbol.Id &&
1686
                                            connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
1687
                                            connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch" &&
1688
                                            IsMovePoint2D(connector.ConnectItem2SymbolObject, connector, arrow))
1689
                                        {
1690
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1691
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1692
                                                drawingObjectBases.Add(point2D);
1693
                                        }
1694
                                            
1695
                                    }
1696
                                    #endregion
1697
                                }
1698
                            }
1699
                            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
1700
                            {
1701
                                if (connector.get_ItemStatus() == "Active")
1702
                                {
1703
                                    #region Zero Length And Branch
1704
                                    if (Convert.ToBoolean(connector.get_IsZeroLength()))
1705
                                    {
1706
                                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolID != _LMSymbol.Id)
1707
                                        {
1708
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1709
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1710
                                                drawingObjectBases.Add(point2D);
1711
                                        }
1712
                                        else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolID != _LMSymbol.Id)
1713
                                        {
1714
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1715
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1716
                                                drawingObjectBases.Add(point2D);
1717
                                        }
1718
                                    }
1719
                                    #endregion
1720
                                    #region Not Zero Length And Branch And Vertical,Horizontal
1721
                                    else
1722
                                    {
1723
                                        if (connector.ConnectItem1SymbolObject != null &&
1724
                                            connector.ConnectItem1SymbolID != _LMSymbol.Id &&
1725
                                            connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
1726
                                            connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch" &&
1727
                                            IsMovePoint2D(connector.ConnectItem1SymbolObject, connector, arrow))
1728
                                        {
1729
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1730
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1731
                                                drawingObjectBases.Add(point2D);
1732
                                        }
1733

    
1734
                                        else if (connector.ConnectItem2SymbolObject != null &&
1735
                                            connector.ConnectItem2SymbolID != _LMSymbol.Id &&
1736
                                            connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
1737
                                            connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch" &&
1738
                                            IsMovePoint2D(connector.ConnectItem2SymbolObject, connector, arrow))
1739
                                        {
1740
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1741
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1742
                                                drawingObjectBases.Add(point2D);
1743
                                        }
1744

    
1745
                                    }
1746
                                    #endregion
1747
                                }
1748
                            }
1749
                        }
1750
                        ReleaseCOMObjects(_LMSymbol);
1751
                    }
1752
                    else if (drawingObject.GetType() == typeof(Point2d))
1753
                    {
1754
                        Point2d point2D = drawingObject as Point2d;
1755
                        if (!drawingObjectBases.Contains(point2D))
1756
                            drawingObjectBases.Add(point2D);
1757
                    }
1758
                }
1759

    
1760
                application.ActiveSelectSet.RemoveAll();
1761
                if (drawingObjectBases.Count > 0 && transform != null)
1762
                {
1763
                    application.ActiveSelectSet.AddObjects(drawingObjectBases);
1764
                    SetTransform(transform, arrow);
1765
                    application.ActiveSelectSet.Transform(transform, false);
1766

    
1767
                    application.ActiveSelectSet.RemoveAll();
1768
                    application.ActiveSelectSet.AddObjects(originalDrawingObjectBases);
1769
                }
1770
                
1771
                //foreach (DrawingObjectBaseEx item in drawingObjectBases)
1772
                //    MoveSymbol(item, arrow);
1773

    
1774
                ReleaseCOMObjects(dataSource);
1775
                ReleaseCOMObjects(placement);
1776
            }
1777
        }
1778
        private bool IsMovePoint2D(LMSymbol branchSymbol, LMConnector targetConnector, Arrow arrow)
1779
        {
1780
            bool result = false;
1781
            
1782
            DependencyObject dependency = application.ActiveDocument.ActiveSheet.DrawingObjects[targetConnector.get_GraphicOID().ToString()] as DependencyObject;
1783
            SlopeType mainSlope = GetLineSlopeType(dependency);
1784

    
1785
            if (mainSlope ==  SlopeType.HORIZONTAL || mainSlope == SlopeType.VERTICAL)
1786
            {
1787
                List<SlopeType> types = new List<SlopeType>();
1788

    
1789
                foreach (LMConnector connector in branchSymbol.Connect1Connectors)
1790
                {
1791
                    if (connector.get_ItemStatus() == "Active" && 
1792
                        connector.Id != targetConnector.Id && 
1793
                        !Convert.ToBoolean(connector.get_IsZeroLength()))
1794
                        types.Add(GetLineSlopeType(application.ActiveDocument.ActiveSheet.DrawingObjects[connector.get_GraphicOID().ToString()] as DependencyObject));
1795
                }
1796

    
1797
                foreach (LMConnector connector in branchSymbol.Connect2Connectors)
1798
                {
1799
                    if (connector.get_ItemStatus() == "Active" &&
1800
                        connector.Id != targetConnector.Id &&
1801
                        !Convert.ToBoolean(connector.get_IsZeroLength()))
1802
                        types.Add(GetLineSlopeType(application.ActiveDocument.ActiveSheet.DrawingObjects[connector.get_GraphicOID().ToString()] as DependencyObject));
1803
                }
1804

    
1805
                foreach (var type in types)
1806
                {
1807
                    if (type == mainSlope)
1808
                    {
1809
                        if (type == SlopeType.HORIZONTAL && (arrow == Arrow.Up || arrow == Arrow.Down))
1810
                            result = true;
1811
                        else if (type == SlopeType.VERTICAL && (arrow == Arrow.Left || arrow == Arrow.Right))
1812
                            result = true;
1813
                        else
1814
                        {
1815
                            result = false;
1816
                            break;
1817
                        }
1818
                    }
1819
                    else
1820
                        result = true;
1821
                }
1822

    
1823
                if (result)
1824
                {
1825
                    if ((arrow == Arrow.Down || arrow == Arrow.Up) && mainSlope == SlopeType.VERTICAL)
1826
                        result = false;
1827
                    else if ((arrow == Arrow.Left || arrow == Arrow.Right) && mainSlope == SlopeType.HORIZONTAL)
1828
                        result = false;
1829
                }
1830
            }
1831

    
1832
            return result;
1833
        }
1834
        private SlopeType GetLineSlopeType(DependencyObject dependency)
1835
        {
1836
            if (dependency != null && dependency.DrawingObjects.Count == 1 && dependency.DrawingObjects[0].GetType() == typeof(LineString2d))
1837
            {
1838
                LineString2d line = dependency.DrawingObjects[0] as LineString2d;
1839
                double x1 = 0, y1 = 0, x2 = double.MaxValue, y2 = double.MaxValue;
1840
                for (int i = 0; i < line.KeyPointCount; i++)
1841
                {
1842
                    double x, y, z;
1843
                    KeyPointType keyPointType;
1844
                    HandleType handleType;
1845
                    line.GetKeyPoint(i, out x, out y, out z, out keyPointType, out handleType);
1846
                    if (keyPointType == KeyPointType.igKeyPointStart)
1847
                    {
1848
                        x1 = x;
1849
                        y1 = y;
1850
                    }
1851
                    else if (keyPointType == KeyPointType.igKeyPointEnd)
1852
                    {
1853
                        x2 = x;
1854
                        y2 = y;
1855
                    }
1856
                }
1857
                return SPPIDUtil.CalcSlope(x1, y1, x2, y2, 0.5);
1858
            }
1859

    
1860
            return SlopeType.None;
1861
        }
1862
        private void MoveSymbol(DrawingObjectBaseEx drawingObjectBase, Arrow arrow)
1863
        {
1864
            if (drawingObjectBase == null)
1865
                return;
1866

    
1867
            GridSetting gridSetting = GridSetting.GetInstance();
1868
            switch (arrow)
1869
            {
1870
                case Arrow.Left:
1871
                    drawingObjectBase.Move(0, 0, -gridSetting.Length, 0);
1872
                    break;
1873
                case Arrow.Up:
1874
                    drawingObjectBase.Move(0, 0, 0, gridSetting.Length);
1875
                    break;
1876
                case Arrow.Right:
1877
                    drawingObjectBase.Move(0, 0, gridSetting.Length, 0);
1878
                    break;
1879
                case Arrow.Down:
1880
                    drawingObjectBase.Move(0, 0, 0, -gridSetting.Length);
1881
                    break;
1882
                default:
1883
                    break;
1884
            }
1885
        }
1886
        private void SetTransform(Transform transform, Arrow arrow)
1887
        {
1888
            if (transform == null)
1889
                return;
1890

    
1891
            GridSetting gridSetting = GridSetting.GetInstance();
1892
            switch (arrow)
1893
            {
1894
                case Arrow.Left:
1895
                    transform.DefineByMove2d(-gridSetting.Length, 0);
1896
                    break;
1897
                case Arrow.Up:
1898
                    transform.DefineByMove2d(0, gridSetting.Length);
1899
                    break;
1900
                case Arrow.Right:
1901
                    transform.DefineByMove2d(gridSetting.Length, 0);
1902
                    break;
1903
                case Arrow.Down:
1904
                    transform.DefineByMove2d(0, -gridSetting.Length);
1905
                    break;
1906
                default:
1907
                    break;
1908
            }
1909
        }
1910
        #endregion
1911

    
1912
        #endregion
1913

    
1914

    
1915
        #region TEST
1916
        private void GetSPPIDSymbolRange(Ingr.RAD2D.Symbol2d symbol2d, ref double[] range)
1917
        {
1918
            {
1919
                double x1 = 0;
1920
                double y1 = 0;
1921
                double x2 = 0;
1922
                double y2 = 0;
1923

    
1924
                x1 = double.MaxValue;
1925
                y1 = double.MaxValue;
1926
                x2 = double.MinValue;
1927
                y2 = double.MinValue;
1928
                range = new double[] { x1, y1, x2, y2 };
1929

    
1930
                foreach (var item in symbol2d.DrawingObjects)
1931
                {
1932
                    if (item.GetType() == typeof(Ingr.RAD2D.Line2d))
1933
                    {
1934
                        Ingr.RAD2D.Line2d rangeObject = item as Ingr.RAD2D.Line2d;
1935
                        if (rangeObject.Layer == "Default")
1936
                        {
1937
                            rangeObject.Range(out x1, out y1, out x2, out y2);
1938
                            range = new double[] {
1939
                                Math.Min(x1, range[0]),
1940
                                Math.Min(y1, range[1]),
1941
                                Math.Max(x2, range[2]),
1942
                                Math.Max(y2, range[3])
1943
                            };
1944
                        }
1945
                    }
1946
                    else if (item.GetType() == typeof(Ingr.RAD2D.Circle2d))
1947
                    {
1948
                        Ingr.RAD2D.Circle2d rangeObject = item as Ingr.RAD2D.Circle2d;
1949
                        if (rangeObject.Layer == "Default")
1950
                        {
1951
                            rangeObject.Range(out x1, out y1, out x2, out y2);
1952
                            range = new double[] {
1953
                                Math.Min(x1, range[0]),
1954
                                Math.Min(y1, range[1]),
1955
                                Math.Max(x2, range[2]),
1956
                                Math.Max(y2, range[3])
1957
                            };
1958
                        }
1959
                    }
1960
                    else if (item.GetType() == typeof(Ingr.RAD2D.Rectangle2d))
1961
                    {
1962
                        Ingr.RAD2D.Rectangle2d rangeObject = item as Ingr.RAD2D.Rectangle2d;
1963
                        if (rangeObject.Layer == "Default")
1964
                        {
1965
                            rangeObject.Range(out x1, out y1, out x2, out y2);
1966
                            range = new double[] {
1967
                                Math.Min(x1, range[0]),
1968
                                Math.Min(y1, range[1]),
1969
                                Math.Max(x2, range[2]),
1970
                                Math.Max(y2, range[3])
1971
                            };
1972
                        }
1973
                    }
1974
                    else if (item.GetType() == typeof(Ingr.RAD2D.Arc2d))
1975
                    {
1976
                        Ingr.RAD2D.Arc2d rangeObject = item as Ingr.RAD2D.Arc2d;
1977
                        if (rangeObject.Layer == "Default")
1978
                        {
1979
                            rangeObject.Range(out x1, out y1, out x2, out y2);
1980
                            range = new double[] {
1981
                                Math.Min(x1, range[0]),
1982
                                Math.Min(y1, range[1]),
1983
                                Math.Max(x2, range[2]),
1984
                                Math.Max(y2, range[3])
1985
                            };
1986
                        }
1987
                    }
1988
                }
1989
            }
1990
        }
1991
        private void simpleButton1_Click(object sender, EventArgs e)
1992
        {
1993
            double x1 = double.MaxValue;
1994
            double y1 = double.MaxValue;
1995
            double x2 = double.MinValue;
1996
            double y2 = double.MinValue;
1997
            foreach (DrawingObjectBase item in application.ActiveSelectSet)
1998
            {
1999
                double[] range = new double[] { };
2000
                GetSPPIDSymbolRange(item as Symbol2d, ref range);
2001
            }
2002
            return;
2003

    
2004
            //SPPIDUtil.test();
2005
            if (application.ActiveSelectSet.Count == 0)
2006
                return;
2007

    
2008
            foreach (DrawingObjectBase item in application.ActiveSelectSet)
2009
            {
2010
                DependencyObject dependency = item as DependencyObject;
2011
                if (dependency != null)
2012
                {
2013
                    foreach (var attributes in dependency.AttributeSets)
2014
                    {
2015
                        foreach (var attribute in attributes)
2016
                        {
2017
                            string name = attribute.Name;
2018
                            object value = attribute.GetValue();
2019
                            if (name == "ModelID")
2020
                            {
2021
                                Placement placement = new Placement();
2022
                                LMADataSource dataSource = placement.PIDDataSource;
2023
                                LMPipeRun pipeRun = dataSource.GetPipeRun(value);
2024
                                string attrName = "PlantGroup.Name";
2025
                                LMAAttribute lMAAttribute = pipeRun.Attributes[attrName];
2026
                                if (lMAAttribute != null)
2027
                                {
2028
                                    lMAAttribute.set_Value("25");
2029
                                    pipeRun.Commit();
2030
                                }
2031
                                else
2032
                                {
2033

    
2034
                                }
2035
                            }
2036
                        }
2037
                    }
2038
                }
2039
            }
2040
            
2041
            
2042

    
2043
            //string a = "0A509911F33441A2AF088BFBA78B770D";
2044
            //LMLabelPersist label = dataSource.GetLabelPersist(a);
2045
            //label.set_XCoordinate(0.4);
2046

    
2047

    
2048

    
2049

    
2050
            //LMOptionSettings
2051

    
2052

    
2053
            //LMAFilter filter = new LMAFilter();
2054
            //LMACriterion criterion = new LMACriterion();
2055
            //filter.ItemType = "Relationship";
2056
            //criterion.SourceAttributeName = "SP_DRAWINGID";
2057
            //criterion.Operator = "=";
2058
            //criterion.set_ValueAttribute(drawingID);
2059
            //filter.get_Criteria().Add(criterion);
2060

    
2061
            //LMRelationships relationships = new LMRelationships();
2062
            //relationships.Collect(dataSource, Filter: filter);
2063

    
2064
        }
2065
        private void AutoJoinPipeRun()
2066
        {
2067
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
2068
            WrapperApplication wApp = new WrapperApplication(application.Application);
2069
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
2070

    
2071
            string modelItemId = null;
2072
            List<double[]> vertices = new List<double[]>();
2073
            if (radApp.ActiveSelectSet.Count == 0)
2074
            {
2075
                return;
2076
            }
2077
            dynamic OID = radApp.ActiveSelectSet[0].Key();
2078
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2079
            foreach (var attributes in drawingObject.AttributeSets)
2080
            {
2081
                foreach (var attribute in attributes)
2082
                {
2083
                    if (attribute.Name == "ModelID")
2084
                        modelItemId = attribute.GetValue().ToString();
2085
                }
2086
            }
2087
            radApp.ActiveSelectSet.RemoveAll();
2088

    
2089
        }
2090
        #endregion
2091

    
2092

    
2093
        [DllImport("user32.dll")]
2094
        public static extern int FindWindow(string lpClassName, string lpWindowName);
2095

    
2096
        [DllImport("user32.dll", SetLastError = true)]
2097
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
2098

    
2099
        private void btnLinkOPCTest_Click(object sender, EventArgs e)
2100
        {
2101
            Placement placement = new Placement();
2102
            LMADataSource dataSource = placement.PIDDataSource;
2103

    
2104
            LMAFilter filter = new LMAFilter();
2105
            filter.ItemType = "REPRESENTATION";
2106
            
2107
            LMACriterion criterion = new LMACriterion();
2108
            criterion.SourceAttributeName = "REPRESENTATIONTYPE";
2109
            criterion.Operator = "=";
2110
            criterion.set_ValueAttribute("39");
2111
            filter.get_Criteria().Add(criterion);
2112

    
2113
            LMRepresentations representations = new LMRepresentations();
2114
            representations.Collect(dataSource, Filter: filter);
2115

    
2116
            foreach (LMRepresentation representation in representations)
2117
            {
2118
                if (representation.get_ItemStatus() == "Active" && representation.DrawingID != "0")
2119
                {
2120
                    LMOPC opc = dataSource.GetOPC(representation.ModelItemID);
2121
                    LMOPC pairOPC = opc.pairedWithOPCObject;
2122
                    bool isPaired = false;
2123
                    foreach (LMRepresentation rep in pairOPC.Representations)
2124
                        if (rep.DrawingID != "0")
2125
                            isPaired = true;
2126

    
2127
                    if (!isPaired)
2128
                    {
2129
                        LMSymbol newOPC = placement.PIDPlaceSymbol(representation.get_FileName().ToString(), 0, 0, ExistingItem: pairOPC.AsLMAItem());
2130
                        ReleaseCOMObjects(newOPC);
2131
                    }
2132

    
2133
                    ReleaseCOMObjects(opc);
2134
                    ReleaseCOMObjects(pairOPC);
2135
                    
2136
                }
2137
            }
2138

    
2139
            ReleaseCOMObjects(representations);
2140
            ReleaseCOMObjects(criterion);
2141
            ReleaseCOMObjects(filter);
2142
            ReleaseCOMObjects(dataSource);
2143
            ReleaseCOMObjects(placement);
2144
        }
2145

    
2146
        #region Test For SECL
2147
        private void btnAutoBreakTest_Click(object sender, EventArgs e)
2148
        {
2149
            
2150
        }
2151

    
2152
        private void FindRun(LMSymbol startSymbol, LMSymbol endSymbol, string model)
2153
        {
2154
            Placement placement = new Placement();
2155
            LMADataSource dataSource = placement.PIDDataSource;
2156
            List<string> endRepID = new List<string>();
2157
            List<string> findRepID = new List<string>();
2158
            endRepID.Add(startSymbol.AsLMRepresentation().Id);
2159
            findRepID.Add(startSymbol.AsLMRepresentation().Id);
2160
            bool end = false;
2161
            LoopRunTest(startSymbol.AsLMRepresentation(), endRepID, endSymbol.AsLMRepresentation().Id, ref end, findRepID);
2162

    
2163

    
2164
            //findRepID.Reverse();
2165
            for (int i = 0; i < findRepID.Count; i++)
2166
            {
2167
                string repID = findRepID[i];
2168

    
2169
                LMRepresentation representation = dataSource.GetRepresentation(repID);
2170
                if (representation != null)
2171
                {
2172
                    string fileName = representation.get_FileName().ToString();
2173
                    if (fileName.Contains(@"\Piping\Valves"))
2174
                    {
2175
                        LMModelItem modelItem = representation.ModelItemObject;
2176
                        LMSymbol tarsym = dataSource.GetSymbol(representation.Id);
2177
                        LMRepresentation prevRep = dataSource.GetRepresentation(findRepID[i - 1]);
2178
                        LMConnector removeConnector = dataSource.GetConnector(prevRep.Id);
2179

    
2180
                        List<double[]> vertices = GetConnectorVertices(removeConnector);
2181

    
2182
                        ZoomObjectByGraphicOID(vertices[0][0], vertices[0][1]);
2183
                        Array array = new double[] { 0, vertices[0][0], vertices[0][1] };
2184
                        LMLabelPersist _LmLabelPersist = placement.PIDPlaceLabel(model, ref array, LabeledItem: removeConnector.AsLMRepresentation(), IsLeaderVisible: true);
2185
                        if (_LmLabelPersist == null)
2186
                        {
2187
                            if (removeConnector.get_ItemStatus() == "Active")
2188
                            {
2189
                                LMConnector newConnector = ReModelingLine(removeConnector, removeConnector.ConnectItem1SymbolObject, removeConnector.ConnectItem2SymbolObject, true, false);
2190
                                FindRun(startSymbol, endSymbol, model);
2191
                            }
2192
                        }
2193
                        
2194

    
2195

    
2196
                        //string dd = startSymbol.ModelItemObject.Attributes["SteamOutTemperature"].get_Value();
2197
                        //string dd2 = endSymbol.ModelItemObject.Attributes["SteamOutTemperature"].get_Value();
2198
                        //double value1 = Convert.ToDouble(dd.Split(new char[] { ' ' })[0]);
2199
                        //double value2 = Convert.ToDouble(dd2.Split(new char[] { ' ' })[0]);
2200

    
2201
                        //if (value1 > value2)
2202
                        //{
2203
                        //    LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i + 1]);
2204
                        //    placement.PIDRemovePlacement(prevRep);
2205
                        //}
2206
                        //else
2207
                        //{
2208
                        //    LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i - 1]);
2209
                        //    placement.PIDRemovePlacement(prevRep);
2210
                        //}
2211

    
2212
                        //LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i - 1]);
2213
                        //placement.PIDRemovePlacement(prevRep);
2214

    
2215

    
2216
                        //application.ActiveDocument.Save();
2217
                        //ReleaseCOMObjects(modelItem);
2218
                        break;
2219
                    }
2220
                }
2221

    
2222
                ReleaseCOMObjects(representation);
2223
            }
2224
        }
2225
        private LMConnector ReModelingLine(LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
2226
        {
2227
            Placement _placement = new Placement();
2228
            LMADataSource dataSource = _placement.PIDDataSource;
2229

    
2230
            string symbolPath = string.Empty;
2231
            #region get symbol path
2232
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
2233
            symbolPath = GetSPPIDFileName(modelItem);
2234
            ReleaseCOMObjects(modelItem);
2235
            #endregion
2236
            bool diagonal = false;
2237
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2238
            LMConnector newConnector = null;
2239
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
2240
            DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2241
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2242
            int verticesCount = lineStringGeometry.VertexCount;
2243
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2244

    
2245
            List<double[]> vertices = new List<double[]>();
2246
            for (int i = 1; i <= verticesCount; i++)
2247
            {
2248
                double x = 0;
2249
                double y = 0;
2250
                lineStringGeometry.GetVertex(i, ref x, ref y);
2251
                vertices.Add(new double[] { x, y });
2252
            }
2253

    
2254
            for (int i = 0; i < vertices.Count; i++)
2255
            {
2256
                double[] points = vertices[i];
2257
                // 시작 심볼이 있고 첫번째 좌표일 때
2258
                if (startSymbol != null && i == 0)
2259
                {
2260
                    if (bStart)
2261
                    {
2262
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
2263
                        if (slopeType == SlopeType.HORIZONTAL)
2264
                            placeRunInputs.AddPoint(points[0], -0.1);
2265
                        else if (slopeType == SlopeType.VERTICAL)
2266
                            placeRunInputs.AddPoint(-0.1, points[1]);
2267
                        else
2268
                            placeRunInputs.AddPoint(points[0], -0.1);
2269

    
2270
                        placeRunInputs.AddPoint(points[0], points[1]);
2271
                    }
2272
                    else
2273
                    {
2274
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2275
                    }
2276
                }
2277
                // 마지막 심볼이 있고 마지막 좌표일 때
2278
                else if (endSymbol != null && i == vertices.Count - 1)
2279
                {
2280
                    if (bEnd)
2281
                    {
2282
                        placeRunInputs.AddPoint(points[0], points[1]);
2283

    
2284
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2285
                        if (slopeType == SlopeType.HORIZONTAL)
2286
                            placeRunInputs.AddPoint(points[0], -0.1);
2287
                        else if (slopeType == SlopeType.VERTICAL)
2288
                            placeRunInputs.AddPoint(-0.1, points[1]);
2289
                        else
2290
                            placeRunInputs.AddPoint(points[0], -0.1);
2291
                    }
2292
                    else
2293
                    {
2294
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2295
                    }
2296
                }
2297
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2298
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2299
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2300
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2301
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2302
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2303
                else
2304
                    placeRunInputs.AddPoint(points[0], points[1]);
2305
            }
2306

    
2307
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2308
            Thread.Sleep(1000);
2309
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2310

    
2311
            ReleaseCOMObjects(placeRunInputs);
2312
            ReleaseCOMObjects(_LMAItem);
2313
            ReleaseCOMObjects(modelItem);
2314

    
2315
            if (newConnector != null)
2316
            {
2317
                newConnector.Commit();
2318
                if (startSymbol != null && bStart)
2319
                {
2320
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2321
                    placeRunInputs = new PlaceRunInputs();
2322
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2323
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2324
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2325
                    if (_LMConnector != null)
2326
                    {
2327
                        _LMConnector.Commit();
2328
                        newConnector = RemoveConnectorForReModelingLine(newConnector);
2329
                        ReleaseCOMObjects(_LMConnector);
2330
                    }
2331
                    ReleaseCOMObjects(placeRunInputs);
2332
                    ReleaseCOMObjects(_LMAItem);
2333
                }
2334

    
2335
                if (endSymbol != null && bEnd)
2336
                {
2337
                    if (startSymbol != null)
2338
                    {
2339
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2340
                        newConnector = dicVertices.First().Key;
2341
                    }
2342

    
2343
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2344
                    placeRunInputs = new PlaceRunInputs();
2345
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2346
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2347
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2348
                    if (_LMConnector != null)
2349
                    {
2350
                        _LMConnector.Commit();
2351
                        newConnector = RemoveConnectorForReModelingLine(newConnector);
2352
                        ReleaseCOMObjects(_LMConnector);
2353
                    }
2354
                    ReleaseCOMObjects(placeRunInputs);
2355
                    ReleaseCOMObjects(_LMAItem);
2356
                }
2357

    
2358
                
2359
            }
2360
            ReleaseCOMObjects(modelItem);
2361
            return newConnector;
2362
            
2363
        }
2364

    
2365
        private LMConnector RemoveConnectorForReModelingLine(LMConnector connector)
2366
        {
2367
            Placement _placement = new Placement();
2368
            LMADataSource dataSource = _placement.PIDDataSource;
2369

    
2370
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2371
            foreach (var item in dicVertices)
2372
            {
2373
                if (item.Value.Count == 2)
2374
                {
2375
                    bool result = false;
2376
                    foreach (var point in item.Value)
2377
                    {
2378
                        if (point[0] < 0 || point[1] < 0)
2379
                        {
2380
                            result = true;
2381
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2382
                            break;
2383
                        }
2384
                    }
2385

    
2386
                    if (result)
2387
                        break;
2388
                }
2389
            }
2390

    
2391
            LMConnector result111 = null;
2392
            foreach (var item in dicVertices)
2393
            {
2394
                if (item.Key.get_ItemStatus() == "Active")
2395
                    result111 = item.Key;
2396
                else
2397
                    ReleaseCOMObjects(item.Key);
2398
            }
2399

    
2400
            return result111;
2401
        }
2402

    
2403
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId, bool ContainZeroLength = true)
2404
        {
2405
            Placement _placement = new Placement();
2406
            LMADataSource dataSource = _placement.PIDDataSource;
2407

    
2408
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2409
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2410

    
2411
            if (modelItem != null)
2412
            {
2413
                foreach (LMRepresentation rep in modelItem.Representations)
2414
                {
2415
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2416
                    {
2417
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2418
                        if (!ContainZeroLength && Convert.ToBoolean(_LMConnector.get_IsZeroLength()))
2419
                        {
2420
                            ReleaseCOMObjects(_LMConnector);
2421
                            _LMConnector = null;
2422
                            continue;
2423
                        }
2424
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2425
                        dynamic OID = rep.get_GraphicOID().ToString();
2426
                        DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2427
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2428
                        int verticesCount = lineStringGeometry.VertexCount;
2429
                        double[] vertices = null;
2430
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2431
                        for (int i = 0; i < verticesCount; i++)
2432
                        {
2433
                            double x = 0;
2434
                            double y = 0;
2435
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2436
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
2437
                        }
2438
                    }
2439
                }
2440

    
2441
                ReleaseCOMObjects(modelItem);
2442
            }
2443

    
2444
            return connectorVertices;
2445
        }
2446
        private string GetSPPIDFileName(LMModelItem modelItem)
2447
        {
2448
            string symbolPath = null;
2449
            foreach (LMRepresentation rep in modelItem.Representations)
2450
            {
2451
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2452
                {
2453
                    symbolPath = rep.get_FileName();
2454
                    break;
2455
                }
2456
            }
2457
            return symbolPath;
2458
        }
2459
        private string GetSPPIDFileName(string modelItemId)
2460
        {
2461
            Placement placement = new Placement();
2462
            LMADataSource dataSource = placement.PIDDataSource;
2463

    
2464
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2465
            string symbolPath = null;
2466
            foreach (LMRepresentation rep in modelItem.Representations)
2467
            {
2468
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2469
                {
2470
                    symbolPath = rep.get_FileName();
2471
                    break;
2472
                }
2473
            }
2474
            ReleaseCOMObjects(modelItem);
2475
            return symbolPath;
2476
        }
2477
        private List<double[]> GetConnectorVertices(LMConnector connector)
2478
        {
2479
            List<double[]> vertices = new List<double[]>();
2480
            if (connector != null)
2481
            {
2482
                dynamic OID = connector.get_GraphicOID().ToString();
2483
                DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2484
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2485
                int verticesCount = lineStringGeometry.VertexCount;
2486
                double[] value = null;
2487
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
2488
                for (int i = 0; i < verticesCount; i++)
2489
                {
2490
                    double x = 0;
2491
                    double y = 0;
2492
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2493
                    vertices.Add(new double[] { x, y });
2494
                }
2495
            }
2496
            return vertices;
2497
        }
2498
        private void LoopRunTest(LMRepresentation rep, List<string> endRepID, string endID,ref bool end, List<string> findRepID)
2499
        {
2500
            if (!end)
2501
            {
2502
                foreach (LMRelationship item in rep.Relation1Relationships)
2503
                {
2504
                    if (end)
2505
                        break;
2506

    
2507
                    if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID) && item.Item1RepresentationObject.get_ItemStatus() == "Active" &&
2508
                        (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item1RepresentationObject.get_RepresentationClass() == "Connector"))
2509
                    {
2510
                        if (endID != item.Item1RepresentationID)
2511
                        {
2512
                            endRepID.Add(item.Item1RepresentationID);
2513
                            LoopRunTest(item.Item1RepresentationObject, endRepID, endID, ref end, findRepID);
2514
                            if (end)
2515
                                findRepID.Add(item.Item1RepresentationID);
2516
                        }
2517
                        else
2518
                            end = true;
2519
                    }
2520

    
2521
                    if (end)
2522
                        break;
2523

    
2524
                    if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID) && item.Item2RepresentationObject.get_ItemStatus() == "Active" &&
2525
                        (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item2RepresentationObject.get_RepresentationClass() == "Connector"))
2526
                    {
2527
                        if (endID != item.Item2RepresentationID)
2528
                        {
2529
                            endRepID.Add(item.Item2RepresentationID);
2530
                            LoopRunTest(item.Item2RepresentationObject, endRepID, endID, ref end, findRepID);
2531
                            if (end)
2532
                                findRepID.Add(item.Item2RepresentationID);
2533
                        }
2534
                        else
2535
                            end = true;
2536
                    }
2537
                }
2538
            }
2539

    
2540
            if (!end)
2541
            {
2542
                foreach (LMRelationship item in rep.Relation2Relationships)
2543
                {
2544
                    if (end)
2545
                        break;
2546

    
2547
                    if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID) && item.Item1RepresentationObject.get_ItemStatus() == "Active" &&
2548
                        (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item1RepresentationObject.get_RepresentationClass() == "Connector"))
2549
                    {
2550
                        if (endID != item.Item1RepresentationID)
2551
                        {
2552
                            endRepID.Add(item.Item1RepresentationID);
2553
                            LoopRunTest(item.Item1RepresentationObject, endRepID, endID, ref end, findRepID);
2554
                            if (end)
2555
                                findRepID.Add(item.Item1RepresentationID);
2556
                        }
2557
                        else
2558
                            end = true;
2559
                    }
2560

    
2561
                    if (end)
2562
                        break;
2563

    
2564
                    if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID) && item.Item2RepresentationObject.get_ItemStatus() == "Active" &&
2565
                        (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item2RepresentationObject.get_RepresentationClass() == "Connector"))
2566
                    {
2567
                        if (endID != item.Item2RepresentationID)
2568
                        {
2569
                            endRepID.Add(item.Item2RepresentationID);
2570
                            LoopRunTest(item.Item2RepresentationObject, endRepID, endID, ref end, findRepID);
2571
                            if (end)
2572
                                findRepID.Add(item.Item2RepresentationID);
2573
                        }
2574
                        else
2575
                            end = true;
2576
                    }
2577
                }
2578
            }
2579
        }
2580

    
2581
        private void btnOPC_Click(object sender, EventArgs e)
2582
        {
2583
            Placement _placement;
2584
            LMADataSource dataSource;
2585
            _placement = new Placement();
2586
            dataSource = _placement.PIDDataSource;
2587

    
2588
            OPCFlowDirection();
2589

    
2590
            void OPCFlowDirection()
2591
            {
2592
                //current LMDrawing 가져오기
2593
                LMAFilter filter = new LMAFilter();
2594
                LMACriterion criterion = new LMACriterion();
2595
                filter.ItemType = "Drawing";
2596
                criterion.SourceAttributeName = "Name";
2597
                criterion.Operator = "=";
2598
                criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
2599
                filter.get_Criteria().Add(criterion);
2600

    
2601
                LMDrawings drawings = new LMDrawings();
2602
                drawings.Collect(dataSource, Filter: filter);
2603

    
2604
                // Input Drawing Attribute
2605
                LMDrawing drawing = ((dynamic)drawings).Nth(1);
2606

    
2607
                LMAFilter filter2 = new LMAFilter();
2608
                filter2.ItemType = "REPRESENTATION";
2609

    
2610
                LMACriterion criterion2 = new LMACriterion();
2611
                criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
2612
                criterion2.Operator = "=";
2613
                criterion2.set_ValueAttribute("39");
2614
                filter2.get_Criteria().Add(criterion2);
2615

    
2616
                LMRepresentations representations = new LMRepresentations();
2617
                representations.Collect(dataSource, Filter: filter2);
2618

    
2619
                foreach (LMRepresentation representation in representations)
2620
                {
2621
                    if (representation.DrawingID == drawing.Id)
2622
                    {
2623
                        RunOPC(representation);
2624
                    }
2625
                }
2626
                
2627
                ReleaseCOMObjects(filter);
2628
                ReleaseCOMObjects(filter2);
2629
                ReleaseCOMObjects(criterion);
2630
                ReleaseCOMObjects(criterion2);
2631
                ReleaseCOMObjects(drawing);
2632
                ReleaseCOMObjects(drawings);
2633
                ReleaseCOMObjects(representations);
2634
            }
2635

    
2636
            void RunOPC(LMRepresentation representation)
2637
            {
2638
                LMSymbol symbol = dataSource.GetSymbol(representation.Id);
2639
                List<string> endRepId = new List<string>();
2640
                endRepId.Add(symbol.AsLMRepresentation().Id);
2641
                LoopSymbol(symbol, endRepId);
2642
                
2643
                ReleaseCOMObjects(symbol);
2644

    
2645
                void LoopSymbol(LMSymbol loopSymbol, List<string> endRepID)
2646
                {
2647
                    foreach (LMRelationship item in loopSymbol.Relation1Relationships)
2648
                    {
2649
                        if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID))
2650
                        {
2651
                            endRepID.Add(item.Item1RepresentationID);
2652
                            if (item.Item1RepresentationObject.get_RepresentationClass() == "Connector")
2653
                            {
2654
                                LMModelItem modelItem = item.Item1RepresentationObject.ModelItemObject;
2655
                                SetAttribute(modelItem);
2656
                                LoopConnector(dataSource.GetConnector(item.Item1RepresentationID), endRepID);
2657
                            }
2658
                            else if (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol")
2659
                            {
2660
                                LoopSymbol(dataSource.GetSymbol(item.Item1RepresentationID), endRepID);
2661
                            }
2662
                        }
2663

    
2664
                        if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID))
2665
                        {
2666
                            endRepID.Add(item.Item2RepresentationID);
2667
                            if (item.Item2RepresentationObject.get_RepresentationClass() == "Connector")
2668
                            {
2669
                                LMModelItem modelItem = item.Item2RepresentationObject.ModelItemObject;
2670
                                SetAttribute(modelItem);
2671
                                LoopConnector(dataSource.GetConnector(item.Item2RepresentationID), endRepID);
2672
                            }
2673
                            else if (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol")
2674
                            {
2675
                                LoopSymbol(dataSource.GetSymbol(item.Item2RepresentationID), endRepID);
2676
                            }
2677
                        }
2678
                    }
2679

    
2680
                    foreach (LMRelationship item in loopSymbol.Relation2Relationships)
2681
                    {
2682
                        if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID))
2683
                        {
2684
                            endRepID.Add(item.Item1RepresentationID);
2685
                            if (item.Item1RepresentationObject.get_RepresentationClass() == "Connector")
2686
                            {
2687
                                LMModelItem modelItem = item.Item1RepresentationObject.ModelItemObject;
2688
                                SetAttribute(modelItem);
2689
                                LoopConnector(dataSource.GetConnector(item.Item1RepresentationID), endRepID);
2690
                            }
2691
                            else if (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol")
2692
                            {
2693
                                LoopSymbol(dataSource.GetSymbol(item.Item1RepresentationID), endRepID);
2694
                            }
2695
                        }
2696

    
2697
                        if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID))
2698
                        {
2699
                            endRepID.Add(item.Item2RepresentationID);
2700
                            if (item.Item2RepresentationObject.get_RepresentationClass() == "Connector")
2701
                            {
2702
                                LMModelItem modelItem = item.Item2RepresentationObject.ModelItemObject;
2703
                                SetAttribute(modelItem);
2704
                                LoopConnector(dataSource.GetConnector(item.Item2RepresentationID), endRepID);
2705
                            }
2706
                            else if (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol")
2707
                            {
2708
                                LoopSymbol(dataSource.GetSymbol(item.Item2RepresentationID), endRepID);
2709
                            }
2710
                        }
2711
                    }
2712

    
2713
                    foreach (LMConnector item in loopSymbol.Connect1Connectors)//나가는것
2714
                    {
2715
                        if (item.get_ItemStatus() != "Active" || endRepID.Contains(item.AsLMRepresentation().Id))
2716
                            continue;
2717

    
2718
                        endRepID.Add(item.AsLMRepresentation().Id);
2719
                        if (item.get_ItemStatus() == "Active")
2720
                        {
2721
                            LMModelItem modelItem = item.ModelItemObject;
2722
                            SetAttribute(modelItem);
2723
                            LoopConnector(item, endRepID);
2724
                        }
2725
                    }
2726

    
2727
                    foreach (LMConnector item in loopSymbol.Connect2Connectors)//들어오는것
2728
                    {
2729
                        if (item.get_ItemStatus() != "Active" || endRepID.Contains(item.AsLMRepresentation().Id))
2730
                            continue;
2731

    
2732
                        endRepID.Add(item.AsLMRepresentation().Id);
2733
                        if (item.get_ItemStatus() == "Active")
2734
                        {
2735
                            LMModelItem modelItem = item.ModelItemObject;
2736
                            SetAttribute(modelItem);
2737
                            LoopConnector(item, endRepID);
2738
                        }
2739
                    }
2740
                }
2741

    
2742
                void LoopConnector(LMConnector connector, List<string> endRepID)
2743
                {
2744
                    if (connector.ConnectItem1SymbolObject != null && !endRepID.Contains(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id))
2745
                    {
2746
                        endRepID.Add(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id);
2747
                        //LoopSymbol(connector.ConnectItem1SymbolObject, endRepID);
2748
                    }
2749

    
2750
                    if (connector.ConnectItem2SymbolObject != null && !endRepID.Contains(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id))
2751
                    {
2752
                        endRepID.Add(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id);
2753
                        //LoopSymbol(connector.ConnectItem2SymbolObject, endRepID);
2754
                    }
2755
                }
2756
            }
2757

    
2758
            void SetAttribute(LMModelItem modelItem)
2759
            {
2760
                LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
2761
                if (attribute != null)
2762
                    attribute.set_Value("End 1 is upstream (Inlet)");
2763
                modelItem.Commit();
2764
            }
2765

    
2766
            ReleaseCOMObjects(_placement);
2767
            ReleaseCOMObjects(dataSource);
2768

    
2769
            MessageBox.Show("End OPC Flow Direction");
2770
        }
2771

    
2772
        private void btnCreateStreamBreak_Click(object sender, EventArgs e)
2773
        {
2774
            Placement _placement;
2775
            LMADataSource dataSource;
2776

    
2777
            StreamNoBreak();
2778

    
2779
            void StreamNoBreak()
2780
            {
2781
                
2782
                _placement = new Placement();
2783
                dataSource = _placement.PIDDataSource;
2784

    
2785
                OPCFlowDirection();
2786

    
2787
                void OPCFlowDirection()
2788
                {
2789
                    //current LMDrawing 가져오기
2790
                    LMAFilter filter = new LMAFilter();
2791
                    LMACriterion criterion = new LMACriterion();
2792
                    filter.ItemType = "Drawing";
2793
                    criterion.SourceAttributeName = "Name";
2794
                    criterion.Operator = "=";
2795
                    criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
2796
                    filter.get_Criteria().Add(criterion);
2797

    
2798
                    LMDrawings drawings = new LMDrawings();
2799
                    drawings.Collect(dataSource, Filter: filter);
2800

    
2801
                    // Input Drawing Attribute
2802
                    LMDrawing drawing = ((dynamic)drawings).Nth(1);
2803

    
2804
                    LMAFilter filter2 = new LMAFilter();
2805
                    filter2.ItemType = "REPRESENTATION";
2806

    
2807
                    LMACriterion criterion2 = new LMACriterion();
2808
                    criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
2809
                    criterion2.Operator = "=";
2810
                    criterion2.set_ValueAttribute("46");
2811
                    filter2.get_Criteria().Add(criterion2);
2812

    
2813
                    LMRepresentations representations = new LMRepresentations();
2814
                    representations.Collect(dataSource, Filter: filter2);
2815

    
2816
                    foreach (LMRepresentation representation in representations)
2817
                    {
2818
                        if (representation.DrawingID == drawing.Id)
2819
                        {
2820
                            LMSymbol symbol = dataSource.GetSymbol(representation.Id);
2821
                            CreateSpec(symbol);
2822
                        }
2823
                    }
2824

    
2825
                    ReleaseCOMObjects(filter);
2826
                    ReleaseCOMObjects(filter2);
2827
                    ReleaseCOMObjects(criterion);
2828
                    ReleaseCOMObjects(criterion2);
2829
                    ReleaseCOMObjects(drawing);
2830
                    ReleaseCOMObjects(drawings);
2831
                    ReleaseCOMObjects(representations);
2832
                }
2833
            }
2834

    
2835
            void CreateSpec(LMSymbol symbol)
2836
            {
2837
                ZoomObjectByGraphicOID(symbol.get_XCoordinate(), symbol.get_YCoordinate());
2838

    
2839
                foreach (LMConnector item in symbol.Connect1Connectors)
2840
                {
2841
                    if (item.get_ItemStatus() == "Active")
2842
                    {
2843
                        Array array = new double[] { 0, symbol.get_XCoordinate(), symbol.get_YCoordinate() };
2844
                        LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(@"\Piping\Segment Breaks\Above\stream Number Break.sym", ref array, LabeledItem: item.AsLMRepresentation(), IsLeaderVisible: true);
2845
                    }
2846
                }
2847

    
2848
                foreach (LMConnector item in symbol.Connect2Connectors)
2849
                {
2850
                    if (item.get_ItemStatus() == "Active")
2851
                    {
2852
                        Array array = new double[] { 0, symbol.get_XCoordinate(), symbol.get_YCoordinate() };
2853
                        LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(@"\Piping\Segment Breaks\Above\stream Number Break.sym", ref array, LabeledItem: item.AsLMRepresentation(), IsLeaderVisible: true);
2854
                    }
2855
                }
2856

    
2857
                Thread.Sleep(500);
2858
            }
2859

    
2860
            MessageBox.Show("End Modeling");
2861
        }
2862
        
2863
        private void btnPipeRunBreak_Click(object sender, EventArgs e)
2864
        {
2865
            Placement placement = new Placement();
2866
            LMADataSource dataSource = placement.PIDDataSource;
2867

    
2868
            string sFrom = textEdit1.EditValue.ToString();
2869
            string sTo = textEdit2.EditValue.ToString();
2870

    
2871
            LMModelItem item1 = dataSource.GetModelItem(sFrom);
2872
            LMModelItem item2 = dataSource.GetModelItem(sTo);
2873
            if (item1 != null && item1.Representations.Count == 1 &&
2874
                item2 != null && item2.Representations.Count == 1)
2875
            {
2876
                LMSymbol startSymbol = dataSource.GetSymbol(item1.Representations.Nth[1].Id);
2877
                LMSymbol endSymbol = dataSource.GetSymbol(item2.Representations.Nth[1].Id);
2878

    
2879
                if (startSymbol != null && endSymbol != null)
2880
                {
2881
                    FindRun(startSymbol, endSymbol, @"\Piping\Segment Breaks\End of Group.sym");
2882
                }
2883
                else
2884
                {
2885
                    MessageBox.Show("From To 심볼 확인");
2886
                }
2887
            }
2888
            else
2889
            {
2890
                MessageBox.Show("From To 심볼 확인");
2891
            }
2892
        }
2893

    
2894
        private void btnSegmentBreak_Click(object sender, EventArgs e)
2895
        {
2896
            Placement placement = new Placement();
2897
            LMADataSource dataSource = placement.PIDDataSource;
2898

    
2899
            string sFrom = textEdit1.EditValue.ToString();
2900
            string sTo = textEdit2.EditValue.ToString();
2901

    
2902
            LMModelItem item1 = dataSource.GetModelItem(sFrom);
2903
            LMModelItem item2 = dataSource.GetModelItem(sTo);
2904
            if (item1 != null && item1.Representations.Count == 1 &&
2905
                item2 != null && item2.Representations.Count == 1)
2906
            {
2907
                LMSymbol startSymbol = dataSource.GetSymbol(item1.Representations.Nth[1].Id);
2908
                LMSymbol endSymbol = dataSource.GetSymbol(item2.Representations.Nth[1].Id);
2909

    
2910
                if (startSymbol != null && endSymbol != null)
2911
                {
2912
                    FindRun(startSymbol, endSymbol, @"\Piping\Segment Breaks\Max Design Press - Temp.sym");
2913
                }
2914
                else
2915
                {
2916
                    MessageBox.Show("From To 심볼 확인");
2917
                }
2918
            }
2919
            else
2920
            {
2921
                MessageBox.Show("From To 심볼 확인");
2922
            }
2923
        }
2924

    
2925
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
2926
        {
2927
            if (application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
2928
            {
2929
                double minX = 0;
2930
                double minY = 0;
2931
                double maxX = 0;
2932
                double maxY = 0;
2933
                application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
2934
                application.ActiveWindow.ZoomArea2(minX - 0.01, minY - 0.01, maxX + 0.01, maxY + 0.01, null);
2935

    
2936
                Thread.Sleep(milliseconds);
2937
            }
2938
        }
2939

    
2940
        private void ZoomObjectByGraphicOID(double x, double y, int milliseconds = 150)
2941
        {
2942
            application.ActiveWindow.ZoomArea2(x - 0.01, y - 0.01, x + 0.01, y + 0.01, null);
2943
            Thread.Sleep(milliseconds);
2944
        }
2945

    
2946
        #endregion
2947

    
2948
        private void btnGetDrawingSize_Click(object sender, EventArgs e)
2949
        {
2950
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
2951
            WrapperApplication wApp = new WrapperApplication(application.Application);
2952
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
2953

    
2954
            if (radApp.ActiveSelectSet.Count > 0)
2955
            {
2956
                DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject;
2957
                if (line2D != null)
2958
                {
2959
                    double minX = 0;
2960
                    double minY = 0;
2961
                    double maxX = 0;
2962
                    double maxY = 0;
2963
                    line2D.Range(out minX, out minY, out maxX, out maxY);
2964

    
2965
                    Settings.Default.DrawingX = maxX - minX;
2966
                    Settings.Default.DrawingY = maxY - minY;
2967
                    Settings.Default.Save();
2968

    
2969
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
2970
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
2971
                }
2972
                else
2973
                    MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2974
            }
2975
            else
2976
                MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2977
        }
2978

    
2979
        private void btnExportPDF_Click(object sender, EventArgs e)
2980
        {
2981
            dynamic dApplication = Microsoft.VisualBasic.Interaction.GetObject("", "PIDAutomation.Application");
2982
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
2983
            Ingr.RAD2D.Application application = wApp.RADApplication;
2984
            SaveFileDialog dialog = new SaveFileDialog();
2985
            dialog.FileName = application.ActiveDocument.Name.Replace(".pid", "");
2986
            dialog.Filter = "pdf file (*.pdf)|*.pdf";
2987
            if (dialog.ShowDialog() == DialogResult.OK)
2988
            {
2989
                application.ActiveDocument.SaveAsPDF(
2990
                dialog.FileName,
2991
                false,
2992
                Ingr.RAD2D.PDFResolutionConstants.igPDFResolution1200DPI,
2993
                Ingr.RAD2D.PDFColorModeConstants.igPDFColorModePureBlackAndWhite,
2994
                Ingr.RAD2D.PDFJpegCompressionConstants.igPDFJpegGoodQualityGoodCompression);
2995
            }
2996
        }
2997

    
2998
        private void btnUploadDrawingAttribute_Click(object sender, EventArgs e)
2999
        {
3000
            OpenFileDialog openFileDialog = new OpenFileDialog();
3001
            openFileDialog.Filter = "excel files (*.xls;*.xlsx)|*.xls;*.xlsx";
3002
            openFileDialog.Multiselect = false;
3003
            if (openFileDialog.ShowDialog() == DialogResult.OK)
3004
            {
3005
                try
3006
                {
3007
                    DevExpress.Spreadsheet.IWorkbook workbook = new DevExpress.Spreadsheet.Workbook();
3008
                    workbook.LoadDocument(openFileDialog.FileName);
3009

    
3010
                    DevExpress.Spreadsheet.Worksheet worksheet = workbook.Worksheets[0];
3011

    
3012
                    DevExpress.Spreadsheet.Row headerNameRow = worksheet.Rows[0];
3013
                    DataTable dt = new DataTable();
3014
                    int colIndex = 0;
3015
                    while (true)
3016
                    {
3017
                        string colName = headerNameRow[colIndex].DisplayText;
3018
                        if (string.IsNullOrEmpty(colName))
3019
                            break;
3020
                        dt.Columns.Add(colName);
3021
                        colIndex++;
3022
                    }
3023

    
3024
                    int index = 1;
3025
                    while (true)
3026
                    {
3027
                        List<string> value = new List<string>();
3028
                        DevExpress.Spreadsheet.Row row = worksheet.Rows[index];
3029
                        string Drawing_Name = row[0].DisplayText;
3030
                        if (string.IsNullOrEmpty(Drawing_Name))
3031
                            break;
3032

    
3033
                        DataRow dataRow = dt.NewRow();
3034
                        for (int i = 0; i < dt.Columns.Count; i++)
3035
                        {
3036
                            string sValue = row[i].DisplayText;
3037
                            dataRow[i] = sValue;
3038
                        }
3039
                        dt.Rows.Add(dataRow);
3040
                        index++;
3041
                    }
3042

    
3043
                    foreach (DataRow row in dt.Rows)
3044
                    {
3045
                        string drawingName = row[0].ToString();
3046
                        LMDrawing drawing = GetDrawingByName(drawingName);
3047
                        if (drawing != null)
3048
                        {
3049
                            for (int i = 1; i < dt.Columns.Count; i++)
3050
                            {
3051
                                string value = row[i].ToString();
3052
                                if (string.IsNullOrEmpty(value))
3053
                                    continue;
3054
                                
3055
                                string attrName = dt.Columns[i].ColumnName;
3056
                                LMAAttribute attribute = drawing.Attributes[attrName];
3057
                                if (attribute != null)
3058
                                    attribute.set_Value(value);
3059
                            }
3060
                            drawing.Commit();
3061
                        }
3062
                    }
3063

    
3064
                    workbook.Dispose();
3065
                    MessageBox.Show("End upload drawing attribute", "SPPID Converter", MessageBoxButtons.OK, MessageBoxIcon.Information);
3066
                }
3067
                catch (Exception ex)
3068
                {
3069
                    MessageBox.Show(ex.Message);
3070
                }
3071
            }
3072

    
3073
            LMDrawing GetDrawingByName(string name)
3074
            {
3075
                Placement placement = new Placement();
3076
                LMADataSource dataSource = placement.PIDDataSource;
3077

    
3078
                LMAFilter filter = new LMAFilter();
3079
                LMACriterion criterion = new LMACriterion();
3080
                filter.ItemType = "Drawing";
3081
                criterion.SourceAttributeName = "Name";
3082
                criterion.Operator = "=";
3083
                criterion.set_ValueAttribute(name);
3084
                filter.get_Criteria().Add(criterion);
3085

    
3086
                LMDrawings drawings = new LMDrawings();
3087
                drawings.Collect(dataSource, Filter: filter);
3088

    
3089
                // Input Drawing Attribute
3090
                LMDrawing drawing = ((dynamic)drawings).Nth(1);
3091
                return drawing;
3092
            }
3093
        }
3094

    
3095
        string fileName = string.Empty;
3096
        Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
3097
        private void btnRussia_Click(object sender, EventArgs e)
3098
        {
3099
            string noteSymbolPath = @"\Design\Annotation\Labels\DL_Item Note & Label.sym";
3100
            List<string> opcSymbolPath = new List<string>()
3101
            {
3102
                @"\Piping\Piping OPC's\Off-Drawing.sym",
3103
                @"\Instrumentation\Instrument OPC's\Off-Drawing.sym"
3104
            };
3105

    
3106
            if (string.IsNullOrEmpty(fileName))
3107
            {
3108
                OpenFileDialog openFileDialog = new OpenFileDialog();
3109
                openFileDialog.Filter = "excel files (*.xls;*.xlsx)|*.xls;*.xlsx";
3110
                openFileDialog.Multiselect = false;
3111
                if (openFileDialog.ShowDialog() == DialogResult.OK)
3112
                    fileName = openFileDialog.FileName;
3113
                else
3114
                    return;
3115

    
3116
                changeList();
3117
            }
3118

    
3119
            application.ActiveWindow.Zoom = 2000;
3120

    
3121
            Plaice.Placement placement = new Plaice.Placement();
3122
            LMADataSource dataSource = placement.PIDDataSource;
3123
            
3124
            ChangeNote();
3125
            ChangeOPC();
3126

    
3127
            void ChangeNote()
3128
            {
3129
                LMSymbols symbols = GetSymbols(noteSymbolPath);
3130

    
3131
                foreach (LMSymbol symbol in symbols)
3132
                {
3133
                    LMItemNote itemNote = dataSource.GetItemNote(symbol.ModelItemID);
3134
                    LMAAttribute attr = itemNote.Attributes["Note.Body"];
3135
                    object value = attr.get_Value();
3136
                    if (!DBNull.Value.Equals(value))
3137
                    {
3138
                        string sValue = value.ToString();
3139
                        string changeText = string.Empty;
3140
                        if (keyValuePairs.ContainsKey(sValue))
3141
                            changeText = keyValuePairs[sValue];
3142

    
3143
                        if (!string.IsNullOrEmpty(changeText))
3144
                        {
3145
                            CopyNoteItem(symbol, itemNote, changeText);
3146
                        }
3147
                    }
3148
                }
3149
            }
3150
            void ChangeOPC()
3151
            {
3152
                foreach (var file in opcSymbolPath)
3153
                {
3154
                    LMSymbols symbols = GetSymbols(file);
3155
                    foreach (LMSymbol symbol in symbols)
3156
                    {
3157
                        LMOPC opc = dataSource.GetOPC(symbol.ModelItemID);
3158
                        string gOID = symbol.get_GraphicOID().ToString();
3159
                        foreach (LMLabelPersist item in symbol.LabelPersists)
3160
                        {
3161
                            OPCText(item.get_GraphicOID().ToString());
3162
                        }
3163
                    }
3164
                }
3165
            }
3166

    
3167
            
3168

    
3169
            void CopyNoteItem(LMSymbol symbol, LMItemNote itemNote, string text)
3170
            {
3171
                double x = symbol.get_XCoordinate();
3172
                double y = symbol.get_YCoordinate();
3173
                double rotate = symbol.get_RotationAngle();
3174
                ZoomObjectByGraphicOID(symbol.get_GraphicOID().ToString());
3175

    
3176
                LMSymbol newSymbol = placement.PIDPlaceSymbol(noteSymbolPath, x, y, Rotation: rotate);
3177
                LMItemNote newItemNote = dataSource.GetItemNote(newSymbol.ModelItemID);
3178
                LMAAttribute newAttr = newItemNote.Attributes["Note.Body"];
3179
                newAttr.set_Value(text);
3180
                newItemNote.Commit();
3181
                newSymbol.Commit();
3182

    
3183
                double[] symbolRange = null, newSymbolRange = null;
3184
                GetSPPIDSymbolRange(symbol.LabelPersists.Nth[1], ref symbolRange);
3185
                GetSPPIDSymbolRange(newSymbol.LabelPersists.Nth[1], ref newSymbolRange);
3186

    
3187
                Symbol2d moveSymbol2d = GetSymbol2DByRepID(dataSource, newSymbol.AsLMRepresentation().Id);
3188
                if (rotate > -0.1 && rotate < 0.1)
3189
                {
3190
                    moveSymbol2d.Move(0, 0, 0, symbolRange[1] - newSymbolRange[3]);
3191
                }
3192
                else
3193
                {
3194
                    moveSymbol2d.Move(0, 0, symbolRange[2] - newSymbolRange[0], 0);
3195
                }
3196
            }
3197
            
3198
            LMSymbols GetSymbols(string file)
3199
            {
3200
                LMAFilter filter = new LMAFilter();
3201
                LMACriterion criterion1 = new LMACriterion();
3202
                criterion1.SourceAttributeName = "SP_DRAWINGID";
3203
                criterion1.Operator = "=";
3204
                criterion1.set_ValueAttribute(dataSource.PIDMgr.Drawing.ID);
3205
                criterion1.Conjunctive = true;
3206
                filter.get_Criteria().Add(criterion1);
3207
                filter.ItemType = "Symbol";
3208

    
3209
                LMACriterion criterion2 = new LMACriterion();
3210
                criterion2.SourceAttributeName = "ITEMSTATUS";
3211
                criterion2.Operator = "=";
3212
                criterion2.set_ValueAttribute("1");
3213
                criterion2.Conjunctive = true;
3214
                filter.get_Criteria().Add(criterion2);
3215

    
3216
                LMACriterion criterion3 = new LMACriterion();
3217
                criterion3.SourceAttributeName = "INSTOCKPILE";
3218
                criterion3.Operator = "=";
3219
                criterion3.set_ValueAttribute("1");
3220
                criterion3.Conjunctive = true;
3221
                filter.get_Criteria().Add(criterion3);
3222

    
3223
                LMACriterion criterion4 = new LMACriterion();
3224
                criterion4.SourceAttributeName = "FILENAME";
3225
                criterion4.Operator = "=";
3226
                criterion4.set_ValueAttribute(file);
3227
                criterion4.Conjunctive = true;
3228
                filter.get_Criteria().Add(criterion4);
3229

    
3230
                LMSymbols items = new LMSymbols();
3231
                items.Collect(dataSource, Filter: filter);
3232

    
3233
                return items;
3234
            }
3235

    
3236
            void changeList()
3237
            {
3238
                DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook();
3239
                try
3240
                {
3241
                    workbook.LoadDocument(fileName);
3242
                    DevExpress.Spreadsheet.Worksheet sheet = workbook.Worksheets["SPPIDConverter"];
3243
                    if (sheet == null)
3244
                        return;
3245

    
3246
                    int index = 0;
3247
                    while (true)
3248
                    {
3249
                        DevExpress.Spreadsheet.Row row = sheet.Rows[index];
3250
                        string from = row[0].DisplayText;
3251
                        string to = row[1].DisplayText;
3252
                        if (string.IsNullOrEmpty(from) || string.IsNullOrEmpty(to))
3253
                            break;
3254
                        else if(!keyValuePairs.ContainsKey(from))
3255
                            keyValuePairs.Add(from, to);
3256
                        index++;
3257
                    }
3258

    
3259
                }
3260
                catch (Exception ex)
3261
                {
3262
                    MessageBox.Show("error");
3263
                }
3264
                finally
3265
                {
3266
                    workbook.Dispose();
3267
                }
3268
            }
3269

    
3270
            void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3271
            {
3272
                if (application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3273
                {
3274
                    double minX = 0;
3275
                    double minY = 0;
3276
                    double maxX = 0;
3277
                    double maxY = 0;
3278
                    application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3279
                    application.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3280

    
3281
                    Thread.Sleep(milliseconds);
3282
                }
3283
            }
3284

    
3285
            void GetSPPIDSymbolRange(LMLabelPersist labelPersist, ref double[] range)
3286
            {
3287
                if (labelPersist != null)
3288
                {
3289
                    double x1 = double.MaxValue;
3290
                    double y1 = double.MaxValue;
3291
                    double x2 = double.MinValue;
3292
                    double y2 = double.MinValue;
3293
                    range = new double[] { x1, y1, x2, y2 };
3294

    
3295
                    Ingr.RAD2D.DependencyObject dependency = application.ActiveDocument.ActiveSheet.DrawingObjects[labelPersist.get_GraphicOID().ToString()] as DependencyObject;
3296
                    foreach (var item in dependency.DrawingObjects)
3297
                    {
3298
                        Ingr.RAD2D.TextBox textBox = item as Ingr.RAD2D.TextBox;
3299
                        if (textBox != null)
3300
                        {
3301
                            if (dependency != null)
3302
                            {
3303
                                double tempX1;
3304
                                double tempY1;
3305
                                double tempX2;
3306
                                double tempY2;
3307
                                textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
3308
                                x1 = Math.Min(x1, tempX1);
3309
                                y1 = Math.Min(y1, tempY1);
3310
                                x2 = Math.Max(x2, tempX2);
3311
                                y2 = Math.Max(y2, tempY2);
3312

    
3313
                                range = new double[] { x1, y1, x2, y2 };
3314
                            }
3315
                        }
3316
                    }
3317

    
3318
                }
3319
            }
3320

    
3321
            void OPCText(string graphicOID)
3322
            {
3323
                if (application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3324
                {
3325
                    DependencyObject dependency = application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] as DependencyObject;
3326
                    foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
3327
                    {
3328
                        Ingr.RAD2D.TextBox textBox = drawingObject as Ingr.RAD2D.TextBox;
3329
                        if (textBox != null)
3330
                        {
3331
                            string text = textBox.Text;
3332
                            if (!string.IsNullOrEmpty(text))
3333
                            {
3334
                                string changeText;
3335
                                ChangeText(text, out changeText);
3336
                                
3337
                                if (!text.Equals(changeText))
3338
                                {
3339
                                    double tempX1, tempY1, tempX2, tempY2;
3340
                                    textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
3341

    
3342
                                    double x = tempX1, y = tempY1;
3343

    
3344
                                    LMSymbol newSymbol = placement.PIDPlaceSymbol(noteSymbolPath, x, y);
3345
                                    LMItemNote newItemNote = dataSource.GetItemNote(newSymbol.ModelItemID);
3346
                                    LMAAttribute newAttr = newItemNote.Attributes["Note.Body"];
3347
                                    newAttr.set_Value(changeText);
3348
                                    newItemNote.Commit();
3349
                                    newSymbol.Commit();
3350

    
3351
                                    double[] newSymbolRange = null;
3352
                                    GetSPPIDSymbolRange(newSymbol.LabelPersists.Nth[1], ref newSymbolRange);
3353
                                    Symbol2d moveSymbol2d = GetSymbol2DByRepID(dataSource, newSymbol.AsLMRepresentation().Id);
3354
                                    moveSymbol2d.Move(newSymbolRange[2], newSymbolRange[1], tempX1, tempY1);
3355

    
3356
                                    ZoomObjectByGraphicOID(graphicOID);
3357
                                }
3358
                            }
3359
                        }
3360
                    }
3361
                }
3362
            }
3363

    
3364
            void ChangeText(string from, out string to)
3365
            {
3366
                to = from;
3367

    
3368
                foreach (var item in keyValuePairs)
3369
                {
3370
                    string value = item.Key;
3371
                    if (to.Contains(value))
3372
                        to = to.Replace(value, item.Value);
3373
                }
3374
            }
3375
        }
3376
    }
3377
}
클립보드 이미지 추가 (최대 크기: 500 MB)