프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ 8487ecb2

이력 | 보기 | 이력해설 | 다운로드 (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
                textEditDrawingX.EditValue = Settings.Default.DrawingX;
48
                textEditDrawingY.EditValue = Settings.Default.DrawingY;
49

    
50
                Project_Info _ProjectInfo = Project_Info.GetInstance();
51
                _ProjectInfo.DefaultPath = Settings.Default.ProjectPath;
52
                _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType;
53
                _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP;
54
                _ProjectInfo.Port = Settings.Default.ProjectPort;
55
                _ProjectInfo.DBUser = Settings.Default.ProjectDBUser;
56
                _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword;
57
                if (Project_DB.ConnTestAndCreateTable())
58
                {
59
                    _ProjectInfo.Enable = true;
60
                    layoutControlGroupUtils.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
61

    
62
                    DataTable dt = Project_DB.SelectSetting();
63
                    foreach (DataRow item in dt.Rows)
64
                    {
65
                        string settingType = item["SettingType"].ToString();
66
                        if (settingType == "ETCSetting")
67
                            SPPIDUtil.ConvertToETCSetting(item["JsonString"].ToString());
68
                        else if (settingType == "GridSetting")
69
                            SPPIDUtil.ConvertToGridSetting(item["JsonString"].ToString());
70
                    }
71
                }
72
                else
73
                {
74
                    layoutControlGroupUtils.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
75
                }
76

    
77
                if (!addEvent)
78
                {
79
                    application.EventObject.BeforeApplicationExit += ApplicationEvents_ApplicationExit;
80
                    addEvent = true;
81
                }
82
            }
83
            catch (Exception ex)
84
            {
85
                StringBuilder sb = new StringBuilder();
86
                sb.AppendLine(ex.Message);
87
                sb.AppendLine(ex.StackTrace);
88
                MessageBox.Show(sb.ToString());
89
            }
90

    
91
#if DEBUG
92
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
93
            layoutControlGroup1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
94

    
95
#else
96
            layoutControlGroup1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
97
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
98
#endif
99

    
100
            //#if DEBUG
101
            //            layoutControlGroupConverter.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
102
            //            layoutControlGroupUtils.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
103
            //            layoutControlGroupShortCut.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
104
            //            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
105
            //            this.layoutControlItem3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
106
            //#endif
107
        }
108
        private void ApplicationEvents_ApplicationExit(out bool cancel)
109
        {
110
            cancel = false;
111
        }
112
        /// <summary>
113
        /// 선택한 도면들을 읽어서 SPPID로 변환한다.
114
        /// </summary>
115
        /// <param name="sender"></param>
116
        /// <param name="e"></param>
117
        private void btnConverter_Click(object sender, EventArgs e)
118
        {
119
            ConverterForm converterForm = new ConverterForm();
120
            if (converterForm.ShowDialog() == DialogResult.OK)
121
            {
122
                //Ingr.RAD2D.Document doc = application.Documents.Add();
123

    
124
                try
125
                {
126
                    CloseOPCForm.Run();
127

    
128
                    for (int i = 0; i < converterForm.Documents.Count; i++)
129
                    {
130
                        SPPID_Document document = converterForm.Documents[i];
131
                        if (document.SetSPPIDMapping() && document.Enable)
132
                        {
133
                            using (AutoModeling modeling = new AutoModeling(document, converterForm.checkEditCloseDocument.Checked))
134
                            {
135
                                modeling.DocumentLabelText = string.Format("Drawing Name : {0} ({1}/{2})", document.DrawingName, i + 1, converterForm.Documents.Count);
136
                                modeling.Run();
137

    
138
                                List<string> endLine = new List<string>();
139
                                Placement placement = new Placement();
140
                                LMADataSource dataSource = placement.PIDDataSource;
141
                                
142
                                foreach (var lineNumber in document.LINENUMBERS)
143
                                {
144
                                    foreach (LineRun run in lineNumber.RUNS)
145
                                    {
146
                                        foreach (var item in run.RUNITEMS)
147
                                        {
148
                                            if (item.GetType() == typeof(Line))
149
                                            {
150
                                                Line line = item as Line;
151
                                                if (line != null && !endLine.Contains(line.SPPID.ModelItemId))
152
                                                {
153
                                                    LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
154
                                                    if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
155
                                                    {
156
                                                        foreach (var attribute in lineNumber.ATTRIBUTES)
157
                                                        {
158
                                                            LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
159
                                                            if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None" && mapping.SPPIDATTRIBUTENAME == "PlantGroup.Name")
160
                                                            {
161
                                                                LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
162
                                                                if (_LMAAttribute != null)
163
                                                                {
164
                                                                    if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
165
                                                                        _LMAAttribute.set_Value(attribute.VALUE);
166
                                                                    else if (_LMAAttribute.get_Value() != attribute.VALUE)
167
                                                                        _LMAAttribute.set_Value(attribute.VALUE);
168
                                                                }
169
                                                            }
170
                                                        }
171
                                                        _LMModelItem.Commit();
172
                                                    }
173
                                                    if (_LMModelItem != null)
174
                                                        ReleaseCOMObjects(_LMModelItem);
175
                                                    endLine.Add(line.SPPID.ModelItemId);
176
                                                }
177
                                            }
178
                                        }
179
                                    }
180
                                }
181

    
182
                                ReleaseCOMObjects(dataSource);
183
                                ReleaseCOMObjects(placement);
184
                            }
185
                        }
186
                    }
187
                }
188
                catch (Exception ex)
189
                {
190
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
191
                }
192
                finally
193
                {
194
                    CloseOPCForm.Stop();
195

    
196
                    //doc.SaveOnClose = false;
197
                    //doc.Close(false);
198

    
199
                    //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
200
                    //dApplication.Drawings[1].CloseDrawing(false);
201
                    //ReleaseCOMObjects(dApplication);
202
                    //dApplication = null;
203
                }
204

    
205
                MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
206
            }
207
        }
208

    
209
        private void btnLinkOPC_Click(object sender, EventArgs e)
210
        {
211
            Placement placement = new Placement();
212
            LMADataSource dataSource = placement.PIDDataSource;
213

    
214
            LMPlantSettings plantSettings = new LMPlantSettings();
215
            plantSettings.Collect(dataSource);
216
            string settingValue = SPPIDUtil.T_PLANTSETTING_Value("Plant Path");
217

    
218
            if (string.IsNullOrEmpty(settingValue))
219
                return;
220
            
221
            LMAFilter filter = new LMAFilter();
222
            LMACriterion criterion = new LMACriterion();
223
            filter.ItemType = "Drawing";
224
            criterion.SourceAttributeName = "Name";
225
            criterion.Operator = "=";
226
            criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
227
            filter.get_Criteria().Add(criterion);
228

    
229
            LMDrawings drawings = new LMDrawings();
230
            drawings.Collect(dataSource, Filter: filter);
231

    
232
            // Input Drawing Attribute
233
            LMDrawing drawing = ((dynamic)drawings).Nth(1);
234

    
235
            LMAFilter filter2 = new LMAFilter();
236
            filter2.ItemType = "REPRESENTATION";
237

    
238
            LMACriterion criterion2 = new LMACriterion();
239
            criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
240
            criterion2.Operator = "=";
241
            criterion2.set_ValueAttribute("39");
242
            filter2.get_Criteria().Add(criterion2);
243

    
244
            LMRepresentations representations = new LMRepresentations();
245
            representations.Collect(dataSource, Filter: filter2);
246

    
247
            string drawingID = drawing.Id;
248
            List<Tuple<LMRepresentation, string>> CurrentDrawing = new List<Tuple<LMRepresentation, string>>();
249
            List<Tuple<LMRepresentation, string>> OtherDrawing = new List<Tuple<LMRepresentation, string>>(); 
250
            foreach (LMRepresentation representation in representations)
251
            {
252
                if (representation.get_ItemStatus() == "Active" && representation.get_InStockpile() == "False")
253
                {
254
                    LMSymbol symbol = dataSource.GetSymbol(representation.Id);
255

    
256
                    if (symbol != null)
257
                    {
258
                        string sResult = "";
259
                        bool bResult = true;
260
                        foreach (LMConnector item in symbol.Connect1Connectors)
261
                        {
262
                            if (item.get_ItemStatus() == "Active" && item.get_InStockpile() == "False" && item.ModelItemObject.get_ItemTypeName() == "PipeRun")
263
                            {
264
                                LMPipeRun pipeRun = dataSource.GetPipeRun(item.ModelItemID);
265
                                if (pipeRun != null)
266
                                {
267
                                    dynamic value = pipeRun.get_ItemTag();
268
                                    if (!DBNull.Value.Equals(value))
269
                                    {
270
                                        if (string.IsNullOrEmpty(sResult))
271
                                            sResult = value;
272
                                        else
273
                                            bResult = false;
274
                                    }
275
                                }
276
                                ReleaseCOMObjects(pipeRun);
277
                            }
278
                        }
279
                        foreach (LMConnector item in symbol.Connect2Connectors)
280
                        {
281
                            if (item.get_ItemStatus() == "Active" && item.get_InStockpile() == "False" && item.ModelItemObject.get_ItemTypeName() == "PipeRun")
282
                            {
283
                                LMPipeRun pipeRun = dataSource.GetPipeRun(item.ModelItemID);
284
                                if (pipeRun != null)
285
                                {
286
                                    dynamic value = pipeRun.get_ItemTag();
287
                                    if (!DBNull.Value.Equals(value))
288
                                    {
289
                                        if (string.IsNullOrEmpty(sResult))
290
                                            sResult = value;
291
                                        else
292
                                            bResult = false;
293
                                    }
294
                                }
295
                                ReleaseCOMObjects(pipeRun);
296
                            }
297
                        }
298

    
299
                        if (bResult)
300
                        {
301
                            if (representation.DrawingID == drawingID)
302
                                CurrentDrawing.Add(new Tuple<LMRepresentation, string>(representation, sResult));
303
                            else
304
                                OtherDrawing.Add(new Tuple<LMRepresentation, string>(representation, sResult));
305
                        }
306
                    }
307

    
308
                    ReleaseCOMObjects(symbol);
309
                }
310
            }
311

    
312
            List<OPC_Info> _OPC_Infos = new List<OPC_Info>();
313
            DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook();
314
            workbook.BeginUpdate();
315

    
316
            workbook.Worksheets[0][0, 0].Value = "OPC ModelItemID";
317
            workbook.Worksheets[0][0, 1].Value = "Tag Value";
318
            workbook.Worksheets[0][0, 2].Value = "Pair Drawing Name";
319
            workbook.Worksheets[0][0, 3].Value = "Match RepID";
320

    
321
            int rowIndex = 1;
322
            foreach (var current in CurrentDrawing)
323
            {
324
                // 여기 로그
325
                LMOPC currentOPC = dataSource.GetOPC(current.Item1.ModelItemID);
326
                string fileName = current.Item1.get_FileName();
327
                string tag = current.Item2;
328
                List<LMOPC> findOPCs = new List<LMOPC>(); 
329
                foreach (var other in OtherDrawing)
330
                {
331
                    if (tag == other.Item2 && fileName == other.Item1.get_FileName())
332
                    {
333
                        LMOPC otherOPC = dataSource.GetOPC(other.Item1.ModelItemID);
334
                        findOPCs.Add(otherOPC);
335
                    }
336
                }
337

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

    
341
                if (findOPCs.Count >= 1 && !string.IsNullOrEmpty(tag))
342
                {
343
                    List<OPC_Info> tempInfos = new List<OPC_Info>();
344
                    foreach (var findOPC in findOPCs)
345
                    {
346
                        LMRepresentation currentPairRepresentation = currentOPC.pairedWithOPCObject.Representations.Nth[1];
347
                        LMRepresentation findPairRepresentation = findOPC.pairedWithOPCObject.Representations.Nth[1];
348
                        if (currentPairRepresentation == null || findPairRepresentation == null)
349
                            continue;
350
                        else if (currentPairRepresentation.get_ItemStatus() == "Active" && currentPairRepresentation.get_InStockpile() == "True" &&
351
                            findPairRepresentation.get_ItemStatus() == "Active" && findPairRepresentation.get_InStockpile() == "True")
352
                        {
353
                            string currentGraphicOID = currentOPC.Representations.Nth[1].get_GraphicOID().ToString();
354
                            LMRepresentation findRepresentation = findOPC.Representations.Nth[1];
355
                            string findGraphicOID = findRepresentation.get_GraphicOID().ToString();
356
                            LMDrawing findDrawing = findRepresentation.DrawingObject;
357
                            string findDrawingPath = settingValue + findDrawing.get_Path();
358

    
359
                            tempInfos.Add(new OPC_Info()
360
                            {
361
                                CurrentGraphicOID = currentGraphicOID,
362
                                TagValue = tag,
363
                                FindGraphicOID = findGraphicOID,
364
                                FindDrawingPath = findDrawingPath,
365
                                FindDrawingName = Path.GetFileNameWithoutExtension(findDrawingPath),
366
                                CurrentRepID = currentOPC.Representations.Nth[1].Id,
367
                                FindRepID = findRepresentation.Id
368
                            });
369

    
370
                            ReleaseCOMObjects(findDrawing);
371
                            ReleaseCOMObjects(findRepresentation);
372
                        }
373
                    }
374

    
375
                    if (tempInfos.Count == 1)
376
                    {
377
                        _OPC_Infos.Add(tempInfos[0]);
378
                        workbook.Worksheets[0][rowIndex, 2].Value = tempInfos[0].FindDrawingName;
379
                        workbook.Worksheets[0][rowIndex, 3].Value = tempInfos[0].FindRepID;
380
                    }
381
                    else
382
                    {
383
                        foreach (var tempInfo in tempInfos)
384
                        {
385
                            workbook.Worksheets[0][rowIndex, 2].Value = tempInfo.FindDrawingName;
386
                            workbook.Worksheets[0][rowIndex, 3].Value = tempInfo.FindRepID;
387
                            if (tempInfos.IndexOf(tempInfo) != tempInfos.Count - 1)
388
                                rowIndex++;
389
                        }
390
                    }   
391
                }
392
                rowIndex++;
393
                foreach (var item in findOPCs)
394
                    ReleaseCOMObjects(item);
395
                ReleaseCOMObjects(currentOPC);
396
            }
397

    
398
            if (_OPC_Infos.Count > 0)
399
            {
400
                foreach (var item in _OPC_Infos)
401
                {
402
                    LMRepresentation removeRep = dataSource.GetRepresentation(item.CurrentRepID);
403
                    LMSymbol removeSymbol = dataSource.GetSymbol(item.CurrentRepID);
404
                    LMOPC removeOPC = dataSource.GetOPC(removeSymbol.ModelItemID);
405

    
406
                    LMRepresentation findRep = dataSource.GetRepresentation(item.FindRepID);
407
                    LMOPC findOPC = dataSource.GetOPC(findRep.ModelItemID);
408
                    LMOPC pairOPC = findOPC.pairedWithOPCObject;
409

    
410
                    int mirror = removeSymbol.get_IsMirroredIndex();
411
                    double angle = Convert.ToDouble(removeSymbol.get_RotationAngle());
412
                    double x = removeSymbol.get_XCoordinate();
413
                    double y = removeSymbol.get_YCoordinate();
414
                    LMConnector connConnector = null;
415
                    
416
                    foreach (LMConnector connector in removeSymbol.Connect1Connectors)
417
                        if (connector.get_ItemStatus() == "Active")
418
                            connConnector = connector;
419
                    if (connConnector == null)
420
                        foreach (LMConnector connector in removeSymbol.Connect2Connectors)
421
                            if (connector.get_ItemStatus() == "Active")
422
                                connConnector = connector;
423
                    
424
                    if (connConnector != null)
425
                    {
426
                        ZoomObjectByGraphicOID(removeRep.get_GraphicOID().ToString());
427

    
428
                        placement.PIDRemovePlacement(removeRep);
429
                        removeRep.Commit();
430

    
431
                        LMSymbol newSymbol = placement.PIDPlaceSymbol(findRep.get_FileName(), x, y, mirror, angle, pairOPC.AsLMAItem(), connConnector);
432
                        newSymbol.Commit();
433

    
434
                        LMOPC newOPC = dataSource.GetOPC(newSymbol.ModelItemID);
435
                        object descValue = removeOPC.get_Description();
436
                        object toFromValue = removeOPC.get_ToFromText();
437
                        newOPC.set_Description(descValue);
438
                        newOPC.set_ToFromText(toFromValue);
439
                        newOPC.Commit();
440

    
441
                        ReleaseCOMObjects(newSymbol);
442
                        ReleaseCOMObjects(newOPC);
443
                    }
444

    
445
                    ReleaseCOMObjects(findOPC);
446
                    ReleaseCOMObjects(pairOPC);
447
                    ReleaseCOMObjects(findRep);
448
                    ReleaseCOMObjects(removeOPC);
449
                    ReleaseCOMObjects(removeSymbol);
450
                    ReleaseCOMObjects(removeRep);
451
                    ReleaseCOMObjects(connConnector);
452
                }
453

    
454
                application.ActiveDocument.Save();
455
            }
456

    
457
            workbook.EndUpdate();
458
            workbook.Worksheets[0].Columns.AutoFit(0, 3);
459
            SaveFileDialog save = new SaveFileDialog();
460
            save.Filter = "excel file (*.xlsx)|*.xlsx";
461
            if (save.ShowDialog() == DialogResult.OK)
462
            {
463
                workbook.SaveDocument(save.FileName, DevExpress.Spreadsheet.DocumentFormat.Xlsx);
464
            }
465
            workbook.Dispose();
466

    
467
            foreach (var item in CurrentDrawing)
468
                ReleaseCOMObjects(item);
469
            foreach (var item in OtherDrawing)
470
                ReleaseCOMObjects(item);
471

    
472
            ReleaseCOMObjects(representations);
473

    
474
            ReleaseCOMObjects(filter2);
475
            ReleaseCOMObjects(criterion2);
476
            ReleaseCOMObjects(drawing);
477
            ReleaseCOMObjects(drawings);
478
            ReleaseCOMObjects(filter);
479
            ReleaseCOMObjects(criterion);
480

    
481
            ReleaseCOMObjects(dataSource);
482
            ReleaseCOMObjects(placement);
483

    
484
            return;
485

    
486
            //DataTable tOPCInfo = Project_DB.SelectOPCInfo();
487
            //DataTable tOPCRelations = Project_DB.SelectOPCRelations();
488
            //DataTable tDrawingInfo = Project_DB.SelectDrawingInfo();
489
            //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
490

    
491
            //foreach (DataRow row in tOPCInfo.Rows)
492
            //{
493
            //    if (!Convert.ToBoolean(row["PAIRED"]))
494
            //    {
495
            //        string drawingUID = row["ID2_DRAWING_UID"].ToString();
496
            //        string OPCUID = row["ID2_OPC_UID"].ToString();
497
            //        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));
498

    
499
            //        if (rows.Length == 2)
500
            //        {
501
            //            string fromDrawingsUID1 = rows[0]["From_Drawings_UID"].ToString();
502
            //            string fromDrawingsUID2 = rows[1]["From_Drawings_UID"].ToString();
503
            //            string fromOPCUID1 = rows[0]["From_OPC_UID"].ToString();
504
            //            string fromOPCUID2 = rows[1]["From_OPC_UID"].ToString();
505
            //            string toDrawingsUID1 = rows[0]["To_Drawings_UID"].ToString();
506
            //            string toDrawingsUID2 = rows[1]["To_Drawings_UID"].ToString();
507
            //            string toOPCUID1 = rows[0]["To_OPC_UID"].ToString();
508
            //            string toOPCUID2 = rows[1]["To_OPC_UID"].ToString();
509

    
510
            //            DataRow[] fromDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", fromDrawingsUID1));
511
            //            DataRow[] toDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", toDrawingsUID1));
512
            //            DataRow[] fromOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", fromOPCUID1));
513
            //            DataRow[] toOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", toOPCUID1));
514

    
515
            //            if (fromOPCUID1 == toOPCUID2 && fromOPCUID2 == toOPCUID1 && fromDrawing.Length == 1 && toDrawing.Length == 1 && fromOPCInfoRows.Length == 1 && toOPCInfoRows.Length == 1)
516
            //            {
517
            //                DataRow fromOPCInfoRow = fromOPCInfoRows[0];
518
            //                DataRow toOPCInfoRow = toOPCInfoRows[0];
519
            //                string fromOPCModelId = fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
520
            //                string toOPCModelId = toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
521
            //                string toDrawingName = toDrawing[0]["DRAWINGNAME"].ToString();
522
            //                List<string[]> toOPCAttributes = JsonConvert.DeserializeObject<List<string[]>>(toOPCInfoRow["ATTRIBUTES"].ToString());
523
            //                AutoModeling_OPC opc = new AutoModeling_OPC(dApplication, application, fromOPCModelId, toOPCModelId, toDrawingName, toOPCAttributes);
524
            //                if (opc.Run())
525
            //                {
526
            //                    fromOPCInfoRow["PAIRED"] = true;
527
            //                    toOPCInfoRow["PAIRED"] = true;
528

    
529
            //                    Project_DB.InsertOPCInfo(fromOPCInfoRow["ID2_OPC_UID"].ToString(), fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), fromOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
530
            //                    Project_DB.InsertOPCInfo(toOPCInfoRow["ID2_OPC_UID"].ToString(), toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), toOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
531
            //                }
532
            //            }
533
            //        }
534
            //    }
535
            //}
536

    
537
            //tOPCInfo.Dispose();
538
            //tOPCRelations.Dispose();
539
            //tDrawingInfo.Dispose();
540
            //ReleaseCOMObjects(dApplication);
541
            //dApplication = null;
542

    
543
            //MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
544
        }
545

    
546
        public void ReleaseCOMObjects(params object[] objVars)
547
        {
548
            int intNewRefCount = 0;
549
            foreach (object obj in objVars)
550
            {
551
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
552
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
553
            }
554
        }
555

    
556
        
557

    
558
        #region SPPID Utils
559

    
560
        #region Symmetry
561
        private void btnSymmetry_Click(object sender, EventArgs e)
562
        {
563
            if (application.ActiveSelectSet.Count == 1 && application.ActiveSelectSet[0].GetType() == typeof(Symbol2d))
564
            {
565
                int symCount = (int)spinEditSymmetry.Value;
566
                Symbol2d symbol = application.ActiveSelectSet[0] as Symbol2d;
567
                double x, y;
568
                symbol.GetOrigin(out x, out y);
569
                string rep = GetRepresentationId(symbol);
570
                List<string> verticalRepID = new List<string>();
571
                List<string> horizontalRepID = new List<string>();
572

    
573
                if ((symbol.LinearName.Contains("Piping") ||
574
                    symbol.LinearName.Contains("Instrument")) &&
575
                    !string.IsNullOrEmpty(rep))
576
                {
577
                    Placement placement = new Placement();
578
                    LMADataSource dataSource = placement.PIDDataSource;
579
                    List<List<string>> datas = SetSymbol(dataSource, rep, x, y, symCount);
580
                    Dictionary<SymmetryArrow, List<string>> resultDatas = new Dictionary<SymmetryArrow, List<string>>();
581
                    if (datas.Count >= 2 && datas.Find(loop => loop.Count != symCount) == null)
582
                    {
583
                        SymmetryArrow arrow = SymmetryArrow.None;
584
                        foreach (var data in datas)
585
                        {
586
                            LMSymbol firstSymbol = dataSource.GetSymbol(data[0]);
587
                            double fX = firstSymbol.get_XCoordinate();
588
                            double fY = firstSymbol.get_YCoordinate();
589

    
590
                            SlopeType type = SPPIDUtil.CalcSlope(x, y, fX, fY, 1);
591
                            if (type == SlopeType.HORIZONTAL)
592
                            {
593
                                if (fX < x)
594
                                {
595
                                    arrow |= SymmetryArrow.Left;
596
                                    resultDatas.Add(SymmetryArrow.Left, data);
597
                                }
598
                                else
599
                                {
600
                                    arrow |= SymmetryArrow.Right;
601
                                    resultDatas.Add(SymmetryArrow.Right, data);
602
                                }
603

    
604
                            }
605
                            else if (type == SlopeType.VERTICAL)
606
                            {
607
                                if (fY < y)
608
                                {
609
                                    arrow |= SymmetryArrow.Down;
610
                                    resultDatas.Add(SymmetryArrow.Down, data);
611
                                }
612
                                else
613
                                {
614
                                    arrow |= SymmetryArrow.Up;
615
                                    resultDatas.Add(SymmetryArrow.Up, data);
616
                                }
617
                            }
618

    
619
                            ReleaseCOMObjects(firstSymbol);
620
                        }
621

    
622
                        SymmetryForm form = new SymmetryForm(arrow);
623
                        if (form.ShowDialog() == DialogResult.OK)
624
                        {
625
                            MoveByResult(dataSource, resultDatas, x, y, form.Result, rep);
626
                        }
627
                    }
628
                    else
629
                        MessageBox.Show("Check Symmetry Rules", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
630
                    ReleaseCOMObjects(dataSource);
631
                    ReleaseCOMObjects(placement);
632
                }
633
            }
634

    
635
            return;
636

    
637
            List<Symbol2d> symbols = new List<Symbol2d>();
638
            foreach (var item in application.ActiveSelectSet)
639
            {
640
                Type type = item.GetType();
641
                if (type == typeof(Symbol2d))
642
                {
643
                    Symbol2d symbol = item as Symbol2d;
644
                    if (symbol.LinearName.Contains("Piping") ||
645
                        symbol.LinearName.Contains("Instrument"))
646
                        symbols.Add(symbol);
647
                }
648
                //if (item.GetType() == typeof(Symbol2d) || item.GetType() == typeof(Point2d))
649
                //    items.Add(item);
650
            }
651

    
652
            List<Symbol2d> verticalSymbols = new List<Symbol2d>();
653
            List<Symbol2d> horizontalSymbols = new List<Symbol2d>();
654
            Symbol2d mainSymbol = null;
655
            for (int i = 0; i < symbols.Count - 1; i++)
656
            {
657
                Symbol2d symbol1 = symbols[i];
658
                for (int j = 1; j < symbols.Count; j++)
659
                {
660
                    Symbol2d symbol2 = symbols[j];
661

    
662
                    if (symbol1 != symbol2)
663
                    {
664
                        double x1, y1, x2, y2;
665
                        symbol1.GetOrigin(out x1, out y1);
666
                        symbol2.GetOrigin(out x2, out y2);
667
                        SlopeType slopeType = SPPIDUtil.CalcSlope(x1, y1, x2, y2, 1);
668
                        if (slopeType == SlopeType.HORIZONTAL)
669
                        {
670
                            if (!horizontalSymbols.Contains(symbol1))
671
                                horizontalSymbols.Add(symbol1);
672
                            if (!horizontalSymbols.Contains(symbol2))
673
                                horizontalSymbols.Add(symbol2);
674

    
675
                            if (verticalSymbols.Contains(symbol1))
676
                                mainSymbol = symbol1;
677
                            if (verticalSymbols.Contains(symbol2))
678
                                mainSymbol = symbol2;
679
                        }
680
                        else if (slopeType == SlopeType.VERTICAL)
681
                        {
682
                            if (!verticalSymbols.Contains(symbol1))
683
                                verticalSymbols.Add(symbol1);
684
                            if (!verticalSymbols.Contains(symbol2))
685
                                verticalSymbols.Add(symbol2);
686

    
687
                            if (horizontalSymbols.Contains(symbol1))
688
                                mainSymbol = symbol1;
689
                            if (horizontalSymbols.Contains(symbol2))
690
                                mainSymbol = symbol2;
691
                        }
692
                    }
693
                }
694
            }
695

    
696
            application.ActiveSelectSet.RemoveAll();
697
            foreach (var item in verticalSymbols)
698
                application.ActiveSelectSet.Add(item);
699
            foreach (var item in horizontalSymbols)
700
                application.ActiveSelectSet.Add(item);
701
                
702

    
703
            if (MessageBox.Show("Continue?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
704
            {
705
                application.ActiveSelectSet.RemoveAll();
706

    
707
            }
708
        }
709
        private void MoveByResult(LMADataSource dataSource,Dictionary<SymmetryArrow, List<string>> resultDatas, double x, double y, SymmetryArrow arrow, string centerRepID)
710
        {
711
            List<string> datas = resultDatas[arrow];
712
            foreach (var item in resultDatas)
713
            {
714
                if (item.Key != arrow)
715
                {
716
                    for (int i = 0; i < item.Value.Count; i++)
717
                    {
718
                        Symbol2d moveSymbol2d = GetSymbol2DByRepID(dataSource, item.Value[i]);
719
                        Symbol2d symbol2d = GetSymbol2DByRepID(dataSource, datas[i]);
720

    
721
                        double x1, y1, x2, y2;
722
                        symbol2d.GetOrigin(out x1, out y1);
723
                        moveSymbol2d.GetOrigin(out x2, out y2);
724
                        double distance = SPPIDUtil.CalcPointToPointdDistance(x, y, x1, y1);
725

    
726
                        string symbol1RepID;
727
                        string symbol2RepID;
728
                        List<Point2d> point2ds;
729
                        if (i == 0)
730
                        {
731
                            symbol1RepID = centerRepID;
732
                            symbol2RepID = item.Value[i];
733
                        }
734
                        else
735
                        {
736
                            symbol1RepID = item.Value[i - 1];
737
                            symbol2RepID = item.Value[i];
738
                        }
739
                        point2ds = FindAllPoint2d(dataSource, symbol1RepID, symbol2RepID);
740
                        double moveX = 0;
741
                        double moveY = 0;
742

    
743
                        if (item.Key == SymmetryArrow.Left)
744
                            moveX = x - distance - x2;
745
                        else if (item.Key == SymmetryArrow.Right)
746
                            moveX = x + distance - x2;
747
                        else if (item.Key == SymmetryArrow.Down)
748
                            moveY = y - distance - y2;
749
                        else if (item.Key == SymmetryArrow.Up)
750
                            moveY = y + distance - y2;
751

    
752
                        moveSymbol2d.Move(0, 0, moveX, moveY);
753
                        MovePoint2d(dataSource, item.Value[i], arrow, moveX, moveY);
754
                        foreach (var point in point2ds)
755
                        {
756
                            LMSymbol branch = dataSource.GetSymbol(GetRepresentationId(point));
757
                            LMSymbol connSymbol = GetFirstSymbolBySlope(dataSource, branch, symbol1RepID, symbol2RepID);
758
                            point.Move(0, 0, moveX, moveY);
759
                            if (connSymbol != null)
760
                            {
761
                                Symbol2d connSymbol2d = GetSymbol2DByRepID(dataSource, connSymbol.AsLMRepresentation().Id);
762
                                connSymbol2d.Move(0, 0, moveX, moveY);
763
                                ReleaseCOMObjects(connSymbol);
764
                            }
765
                            ReleaseCOMObjects(branch);
766
                        }
767
                            
768
                    }
769
                }
770
            }
771
        }
772
        private LMSymbol GetFirstSymbolBySlope(LMADataSource dataSource, LMSymbol branch, string symbol1RepID, string symbol2RepID)
773
        {
774
            LMSymbol result = null;
775
            foreach (LMConnector connector in branch.Connect1Connectors)
776
            {
777
                if (connector.ConnectItem1SymbolObject != null && 
778
                    connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
779
                    connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
780
                {
781
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
782
                    if (repID != symbol1RepID && repID != symbol2RepID)
783
                        result = connector.ConnectItem1SymbolObject;
784
                }
785

    
786
                if (connector.ConnectItem2SymbolObject != null &&
787
                    connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
788
                    connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
789
                {
790
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
791
                    if (repID != symbol1RepID && repID != symbol2RepID)
792
                        result = connector.ConnectItem2SymbolObject;
793
                }
794
            }
795

    
796
            foreach (LMConnector connector in branch.Connect2Connectors)
797
            {
798
                if (connector.ConnectItem1SymbolObject != null &&
799
                    connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
800
                    connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
801
                {
802
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
803
                    if (repID != symbol1RepID && repID != symbol2RepID)
804
                        result = connector.ConnectItem1SymbolObject;
805
                }
806

    
807
                if (connector.ConnectItem2SymbolObject != null &&
808
                    connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
809
                    connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
810
                {
811
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
812
                    if (repID != symbol1RepID && repID != symbol2RepID)
813
                        result = connector.ConnectItem2SymbolObject;
814
                }
815
            }
816

    
817

    
818
            return result;
819
        }
820
        private string GetRepresentationId(Symbol2d symbol)
821
        {
822
            foreach (var attributes in symbol.AttributeSets)
823
            {
824
                foreach (var attribute in attributes)
825
                {
826
                    string name = attribute.Name;
827
                    object value = attribute.GetValue();
828
                    if (name == "DrawingID")
829
                    {
830
                        return value.ToString();  
831
                    }
832
                }
833
            }
834
            return null;
835
        }
836
        private string GetRepresentationId(Point2d point2d)
837
        {
838
            foreach (var attributes in point2d.AttributeSets)
839
            {
840
                foreach (var attribute in attributes)
841
                {
842
                    string name = attribute.Name;
843
                    object value = attribute.GetValue();
844
                    if (name == "DrawingID")
845
                    {
846
                        return value.ToString();
847
                    }
848
                }
849
            }
850
            return null;
851
        }
852
        private List<List<string>> SetSymbol(LMADataSource dataSource, string rep, double x, double y, int count)
853
        {
854
            LMSymbol _LMSymbol = dataSource.GetSymbol(rep);
855
            List<List<string>> result = new List<List<string>>();
856
            List<string> oldIDs = new List<string>() { rep };
857
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
858
            {
859
                if (connector.get_ItemStatus() != "Active")
860
                    continue;
861

    
862
                string repID = connector.AsLMRepresentation().Id;
863
                string status = connector.get_ItemStatus();
864
                if (status == "Active" && !oldIDs.Contains(repID))
865
                {
866
                    List<string> symbols = new List<string>();
867
                    oldIDs.Add(repID);
868
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
869
                    result.Add(symbols);
870
                }
871
            }
872

    
873
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
874
            {
875
                if (connector.get_ItemStatus() != "Active")
876
                    continue;
877

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

    
889
            ReleaseCOMObjects(_LMSymbol);
890
            return result;
891
        }
892
        private void loop(LMADataSource dataSource, List<string> oldIDs, List<string> symbols, LMConnector connector, double x, double y, int count)
893
        {
894
            if (symbols.Count >= count)
895
                return;
896

    
897
            if (connector.ConnectItem1SymbolObject != null && !oldIDs.Contains(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id))
898
            {
899
                string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
900
                oldIDs.Add(repID);
901
                if (connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch" && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
902
                {
903
                    double sX = connector.ConnectItem1SymbolObject.get_XCoordinate(), sY = connector.ConnectItem1SymbolObject.get_YCoordinate(); ;
904
                    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, sX, sY, 1);
905
                    if (slopeType == SlopeType.HORIZONTAL || slopeType == SlopeType.VERTICAL)
906
                        symbols.Add(repID);
907
                }
908

    
909
                loop(dataSource, oldIDs, symbols, connector.ConnectItem1SymbolObject, x, y, count);
910
            }
911

    
912
            if (symbols.Count >= count)
913
                return;
914

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

    
927
                loop(dataSource, oldIDs, symbols, connector.ConnectItem2SymbolObject, x, y, count);
928
            }
929
        }
930
        private void loop(LMADataSource dataSource, List<string> oldIDs, List<string> symbols, LMSymbol _LMSymbol, double x, double y, int count)
931
        {
932
            if (symbols.Count >= count)
933
                return;
934

    
935
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
936
            {
937
                if (connector.get_ItemStatus() != "Active")
938
                    continue;
939

    
940
                string repID = connector.AsLMRepresentation().Id;
941
                string status = connector.get_ItemStatus();
942
                if (status == "Active" && !oldIDs.Contains(repID))
943
                {
944
                    oldIDs.Add(repID);
945
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
946
                }
947
            }
948

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

    
1034
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
1035
                {
1036
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
1037
                    if (!endIDs.Contains(repID))
1038
                    {
1039
                        endIDs.Add(repID);
1040
                        if (connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
1041
                        {
1042
                            if (FindPointsLoop(dataSource, connector.ConnectItem1SymbolObject, endIDs, targetRepID, graphicOIDs))
1043
                            {
1044
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1045
                                    graphicOIDs.Add(connector.ConnectItem1SymbolObject.get_GraphicOID().ToString());
1046
                                return true;
1047
                            }
1048
                        }
1049
                        else if (targetRepID == repID)
1050
                        {
1051
                            return true;
1052
                        }
1053
                    }
1054
                }
1055
                if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
1056
                {
1057
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
1058
                    if (!endIDs.Contains(repID))
1059
                    {
1060
                        endIDs.Add(repID);
1061
                        if (connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
1062
                        {
1063
                            if (FindPointsLoop(dataSource, connector.ConnectItem2SymbolObject, endIDs, targetRepID, graphicOIDs))
1064
                            {
1065
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1066
                                    graphicOIDs.Add(connector.ConnectItem2SymbolObject.get_GraphicOID().ToString());
1067
                                return true;
1068
                            }
1069
                        }
1070
                        else if (targetRepID == repID)
1071
                        {
1072
                            return true;
1073
                        }
1074
                    }
1075
                }
1076
            }
1077
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
1078
            {
1079
                if (connector.get_ItemStatus() != "Active")
1080
                    continue;
1081

    
1082
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
1083
                {
1084
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
1085
                    if (!endIDs.Contains(repID))
1086
                    {
1087
                        endIDs.Add(repID);
1088
                        if (connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
1089
                        {
1090
                            if (FindPointsLoop(dataSource, connector.ConnectItem1SymbolObject, endIDs, targetRepID, graphicOIDs))
1091
                            {
1092
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1093
                                    graphicOIDs.Add(connector.ConnectItem1SymbolObject.get_GraphicOID().ToString());
1094
                                return true;
1095
                            }
1096
                        }
1097
                        else if (targetRepID == repID)
1098
                        {
1099
                            return true;
1100
                        }
1101
                    }
1102
                }
1103
                if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
1104
                {
1105
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
1106
                    if (!endIDs.Contains(repID))
1107
                    {
1108
                        endIDs.Add(repID);
1109
                        if (connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
1110
                        {
1111
                            if (FindPointsLoop(dataSource, connector.ConnectItem2SymbolObject, endIDs, targetRepID, graphicOIDs))
1112
                            {
1113
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1114
                                    graphicOIDs.Add(connector.ConnectItem2SymbolObject.get_GraphicOID().ToString());
1115
                                return true;
1116
                            }
1117
                        }
1118
                        else if (targetRepID == repID)
1119
                        {
1120
                            return true;
1121
                        }
1122
                    }
1123
                }
1124
            }
1125

    
1126
            return false;
1127
        }
1128
        #endregion
1129

    
1130
        #region SpecBreak
1131
        private void btnSpecBreakRelocation_Click(object sender, EventArgs e)
1132
        {
1133
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
1134
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
1135
            application = wApp.RADApplication;
1136

    
1137
            int count = application.ActiveSelectSet.Count;
1138
            int dependencyCount = 0;
1139
            foreach (var item in application.ActiveSelectSet)
1140
                if (item.GetType() == typeof(DependencyObject))
1141
                    dependencyCount++;
1142

    
1143
            if (count > 0 && application.ActiveSelectSet[0].GetType() == typeof(DependencyObject))
1144
            {
1145
                MessageBox.Show("First selected item is DependencyObject!\r\nPlease move symbol", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1146
                return;
1147
            }
1148

    
1149
            if ((count == 3 || count == 4) && application.ActiveSelectSet[0].GetType() == typeof(Symbol2d) &&
1150
                (dependencyCount == 2 || dependencyCount == 3))
1151
            {
1152
                Symbol2d symbol = application.ActiveSelectSet[0] as Symbol2d;
1153

    
1154
                if (!symbol.DefinitionName.Contains(@"Design\Annotation\Graphics\"))
1155
                {
1156
                    MessageBox.Show("Select SpecBreak!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1157
                    return;
1158
                }
1159

    
1160
                DependencyObject dependency1 = application.ActiveSelectSet[1] as DependencyObject;
1161
                DependencyObject dependency2 = application.ActiveSelectSet[2] as DependencyObject;
1162
                DependencyObject dependency3 = null;
1163
                if (count == 4)
1164
                    dependency3 = application.ActiveSelectSet[3] as DependencyObject;
1165

    
1166
                application.ActiveSelectSet.RemoveAll();
1167
                double angle = symbol.Angle;
1168
                if (symbol.GetTransform().HasReflection)
1169
                    angle += Math.PI;
1170

    
1171
                double degree = double.NaN;
1172
                if (angle > -0.1 && angle < 0.1)
1173
                    degree = 0;
1174
                else if (angle > 1.56 && angle < 1.58)
1175
                    degree = 90;
1176
                else if (angle > 3.13 && angle < 3.15)
1177
                    degree = 180;
1178
                else if (angle > 4.7 && angle < 4.72)
1179
                    degree = 270;
1180
                else if (angle > 6.27 && angle < 6.29)
1181
                    degree = 0;
1182
                else if (angle > -1.58 && angle < -1.56)
1183
                    degree = 270;
1184
                else if (angle > -3.15 && angle < -3.13)
1185
                    degree = 180;
1186
                else
1187
                    throw new Exception("Check Angle");
1188

    
1189

    
1190
                double originX, originY;
1191
                symbol.GetOrigin(out originX, out originY);
1192

    
1193
                double crossX = 0;
1194
                double crossY = 0;
1195
                double topX = 0;
1196
                double topY = 0;
1197
                foreach (var item in symbol.DrawingObjects)
1198
                {
1199
                    Line2d line2d = item as Line2d;
1200
                    if (line2d != null)
1201
                    {
1202
                        double x1, y1, x2, y2;
1203
                        line2d.GetStartPoint(out x1, out y1);
1204
                        line2d.GetEndPoint(out x2, out y2);
1205
                        SlopeType slopeType = SPPIDUtil.CalcSlope(x1, y1, x2, y2, 0.1);
1206

    
1207
                        if (slopeType == SlopeType.HORIZONTAL)
1208
                        {
1209
                            if (crossY == 0)
1210
                                crossY = y1;
1211
                            else
1212
                                crossY = (crossY + y1) / 2;
1213

    
1214
                            switch (degree)
1215
                            {
1216
                                case 90:
1217
                                    if (topX == 0)
1218
                                        topX = Math.Min(x1, x2);
1219
                                    else
1220
                                        topX = Math.Min(topX, Math.Min(x1, x2));
1221
                                    break;
1222
                                case 270:
1223
                                    if (topX == 0)
1224
                                        topX = Math.Max(x1, x2);
1225
                                    else
1226
                                        topX = Math.Max(topX, Math.Max(x1, x2));
1227
                                    break;
1228
                                default:
1229
                                    break;
1230
                            }
1231
                        }
1232
                        else if (slopeType == SlopeType.VERTICAL)
1233
                        {
1234
                            if (crossX == 0)
1235
                                crossX = x1;
1236
                            else
1237
                                crossX = (crossX + x1) / 2;
1238

    
1239
                            switch (degree)
1240
                            {
1241
                                case 0:
1242
                                    if (topY == 0)
1243
                                        topY = Math.Max(y1, y2);
1244
                                    else
1245
                                        topY = Math.Max(topY, Math.Max(y1, y2));
1246
                                    break;
1247
                                case 180:
1248
                                    if (topY == 0)
1249
                                        topY = Math.Min(y1, y2);
1250
                                    else
1251
                                        topY = Math.Min(topY, Math.Min(y1, y2));
1252
                                    break;
1253
                                default:
1254
                                    break;
1255
                            }
1256
                        }
1257
                    }
1258
                }
1259
                switch (degree)
1260
                {
1261
                    case 0:
1262
                        crossX = originX;
1263
                        topX = crossX;
1264
                        break;
1265
                    case 90:
1266
                        crossY = originY;
1267
                        topY = crossY;
1268
                        break;
1269
                    case 180:
1270
                        crossX = originX;
1271
                        topX = crossX;
1272
                        break;
1273
                    case 270:
1274
                        crossY = originY;
1275
                        topY = crossY;
1276
                        break;
1277
                    default:
1278
                        break;
1279
                }
1280

    
1281
                SpecBreakRelocation(degree, originX, originY, crossX, crossY, topX, topY, dependency1, dependency2, dependency3);
1282
                SetSpecBreakParameters(symbol, dependency1, dependency2, degree);
1283
            }
1284
            else
1285
            {
1286
                MessageBox.Show("Check Rule!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1287
            }
1288
        }
1289
        private void SetSpecBreakParameters(Symbol2d symbol, DependencyObject dependency1, DependencyObject dependency2, double degree)
1290
        {
1291
            bool mirror = symbol.GetTransform().HasReflection;
1292
            string repID = GetRepresentationId(symbol);
1293
            Placement placement = new Placement();
1294
            LMADataSource dataSource = placement.PIDDataSource;
1295

    
1296
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
1297
            if (_LMSymbol != null)
1298
            {
1299
                double sX1, sY1, sX2, sY2, sOriginX, sOriginY;
1300
                symbol.Range(out sX1, out sY1, out sX2, out sY2);
1301
                symbol.GetOrigin(out sOriginX, out sOriginY);
1302
                double d1X1, d1Y1, d1X2, d1Y2, d2X1, d2Y1, d2X2, d2Y2;
1303
                FindRangeWithOutLineString2d(dependency1, out d1X1, out d1Y1, out d1X2, out d1Y2);
1304
                FindRangeWithOutLineString2d(dependency2, out d2X1, out d2Y1, out d2X2, out d2Y2);
1305

    
1306
                double left = 0, right = 0;
1307
                switch (degree)
1308
                {
1309
                    case 0:
1310
                        if (!mirror)
1311
                        {
1312
                            left = Math.Abs(d1X1 - sOriginX);
1313
                            right = Math.Abs(d2X2 - sOriginX);
1314
                        }
1315
                        else
1316
                        {
1317
                            right = Math.Abs(d1X1 - sOriginX);
1318
                            left = Math.Abs(d2X2 - sOriginX);
1319
                        }
1320
                        break;
1321
                    case 90:
1322
                        if (!mirror)
1323
                        {
1324
                            left = Math.Abs(d1Y1 - sOriginY);
1325
                            right = Math.Abs(d2Y2 - sOriginY);
1326
                        }
1327
                        else
1328
                        {
1329
                            right = Math.Abs(d1Y1 - sOriginY);
1330
                            left = Math.Abs(d2Y2 - sOriginY);
1331
                        }
1332
                        break;
1333
                    case 180:
1334
                        if (!mirror)
1335
                        {
1336
                            right = Math.Abs(d1X1 - sOriginX);
1337
                            left = Math.Abs(d2X2 - sOriginX);
1338
                        }
1339
                        else
1340
                        {
1341
                            left = Math.Abs(d1X1 - sOriginX);
1342
                            right = Math.Abs(d2X2 - sOriginX);
1343
                            
1344
                        }
1345
                        break;
1346
                    case 270:
1347
                        if (!mirror)
1348
                        {
1349
                            right = Math.Abs(d1Y1 - sOriginY);
1350
                            left = Math.Abs(d2Y2 - sOriginY);
1351
                        }
1352
                        else
1353
                        {
1354
                            left = Math.Abs(d1Y1 - sOriginY);
1355
                            right = Math.Abs(d2Y2 - sOriginY);
1356
                        }
1357
                        break;
1358
                    default:
1359
                        break;
1360
                }
1361

    
1362
                string[] array1 = new string[] { "", "Left", "Right" };
1363
                string[] array2 = new string[] { "", left.ToString(), right.ToString() };
1364
                placement.PIDApplyParameters(_LMSymbol.AsLMRepresentation(), array1, array2);
1365
            }
1366

    
1367
            ReleaseCOMObjects(_LMSymbol);
1368
            ReleaseCOMObjects(dataSource);
1369
            ReleaseCOMObjects(placement);
1370
        }
1371
        private void SpecBreakRelocation(double degree, double originX, double originY, double crossX, double crossY, double topX, double topY, DependencyObject dependency1, DependencyObject dependency2, DependencyObject dependency3)
1372
        {
1373
            double d1X1, d1Y1, d1X2, d1Y2, d2X1, d2Y1, d2X2, d2Y2, d3X1 = 0, d3Y1 = 0, d3X2 = 0, d3Y2 = 0;
1374
            
1375
            FindRangeWithOutLineString2d(dependency1, out d1X1, out d1Y1, out d1X2, out d1Y2);
1376
            FindRangeWithOutLineString2d(dependency2, out d2X1, out d2Y1, out d2X2, out d2Y2);
1377
            if (dependency3 != null)
1378
                FindRangeWithOutLineString2dAndTextBox(dependency3, out d3X1, out d3Y1, out d3X2, out d3Y2);
1379

    
1380
            GridSetting gridSetting = GridSetting.GetInstance();
1381
            double move = gridSetting.Length / 2;
1382
            switch (degree)
1383
            {
1384
                case 0:
1385
                    MoveDependency(dependency1, d1X2, d1Y2, crossX - move, crossY);
1386
                    MoveDependency(dependency2, d2X1, d2Y2, crossX + move, crossY);
1387
                    if (dependency3 != null)
1388
                        MoveDependency(dependency3, (d3X1 + d3X2) / 2, d3Y1, topX, topY);
1389
                    break;
1390
                case 90:
1391
                    MoveDependency(dependency1, d1X1, d1Y2, crossX + move, crossY);
1392
                    MoveDependency(dependency2, d2X1, d2Y1, crossX + move, crossY);
1393
                    if (dependency3 != null)
1394
                        MoveDependency(dependency3, d3X1, (d3Y1 + d3Y2) / 2, originX, originY);
1395
                    break;
1396
                case 180:
1397
                    MoveDependency(dependency1, d1X2, d1Y1, crossX - move, crossY);
1398
                    MoveDependency(dependency2, d2X1, d2Y1, crossX + move, crossY);
1399
                    if (dependency3 != null)
1400
                        MoveDependency(dependency3, (d3X1 + d3X2) / 2, d3Y2, topX, topY);
1401
                    break;
1402
                case 270:
1403
                    MoveDependency(dependency1, d1X2, d1Y2, crossX - move, crossY);
1404
                    MoveDependency(dependency2, d2X2, d2Y1, crossX - move, crossY);
1405
                    if (dependency3 != null)
1406
                        MoveDependency(dependency3, d3X2, (d3Y1 + d3Y2) / 2, originX, originY);
1407
                    break;
1408
                default:
1409
                    break;
1410
            }
1411
        }
1412
        private void MoveDependency(DependencyObject dependency, double xFrom, double yFrom, double xTo, double yTo)
1413
        {
1414
            application.ActiveSelectSet.Add(dependency);
1415

    
1416
            Transform transform = dependency.GetTransform();
1417
            transform.DefineByMove2d(xTo - xFrom, yTo - yFrom);
1418
            application.ActiveSelectSet.Transform(transform, false);
1419

    
1420
            application.ActiveSelectSet.RemoveAll();
1421
        }
1422
        private string GetDrawingItemType(DependencyObject dependency)
1423
        {
1424
            string result = string.Empty;
1425

    
1426
            foreach (var attributes in dependency.AttributeSets)
1427
            {
1428
                foreach (var attribute in attributes)
1429
                {
1430
                    if (attribute.Name == "DrawingItemType")
1431
                        return attribute.GetValue().ToString();
1432
                }
1433
            }
1434

    
1435
            return result;
1436
        }
1437
        private void FindRangeWithOutLineString2d(DependencyObject dependency, out double x1, out double y1, out double x2, out double y2)
1438
        {
1439
            x1 = double.MaxValue;
1440
            y1 = double.MaxValue;
1441
            x2 = double.MinValue;
1442
            y2 = double.MinValue;
1443
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1444
            {
1445
                if (item.GetType() != typeof(LineString2d))
1446
                {
1447
                    double minX, minY, maxX, maxY;
1448
                    item.Range(out minX, out minY, out maxX, out maxY);
1449
                    if (x1 > minX)
1450
                        x1 = minX;
1451
                    if (y1 > minY)
1452
                        y1 = minY;
1453
                    if (x2 < maxX)
1454
                        x2 = maxX;
1455
                    if (y2 < maxY)
1456
                        y2 = maxY;
1457
                }
1458
            }
1459

    
1460
        }
1461
        private void FindWidthHeightWidthOutLineString2d(DependencyObject dependency, out double width, out double height)
1462
        {
1463
            width = 0;
1464
            height = 0;
1465

    
1466
            double x1 = double.MaxValue;
1467
            double y1 = double.MaxValue;
1468
            double x2 = double.MinValue;
1469
            double y2 = double.MinValue;
1470
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1471
            {
1472
                if (item.GetType() != typeof(LineString2d))
1473
                {
1474
                    double minX, minY, maxX, maxY;
1475
                    item.Range(out minX, out minY, out maxX, out maxY);
1476
                    if (x1 > minX)
1477
                        x1 = minX;
1478
                    if (y1 > minY)
1479
                        y1 = minY;
1480
                    if (x2 < maxX)
1481
                        x2 = maxX;
1482
                    if (y2 < maxY)
1483
                        y2 = maxY;
1484
                }
1485
            }
1486

    
1487
            width = x2 - x1;
1488
            height = y2 - y1;
1489
        }
1490
        private void FindRangeWithOutLineString2dAndTextBox(DependencyObject dependency, out double x1, out double y1, out double x2, out double y2)
1491
        {
1492
            x1 = double.MaxValue;
1493
            y1 = double.MaxValue;
1494
            x2 = double.MinValue;
1495
            y2 = double.MinValue;
1496
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1497
            {
1498
                if (item.GetType() != typeof(LineString2d) && item.GetType() != typeof(Ingr.RAD2D.TextBox))
1499
                {
1500
                    double minX, minY, maxX, maxY;
1501
                    item.Range(out minX, out minY, out maxX, out maxY);
1502
                    if (x1 > minX)
1503
                        x1 = minX;
1504
                    if (y1 > minY)
1505
                        y1 = minY;
1506
                    if (x2 < maxX)
1507
                        x2 = maxX;
1508
                    if (y2 < maxY)
1509
                        y2 = maxY;
1510
                }
1511
            }
1512

    
1513
        }
1514
        #endregion
1515

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

    
1602
        }
1603
        #endregion
1604

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

    
1720
                                        else if (connector.ConnectItem2SymbolObject != null &&
1721
                                            connector.ConnectItem2SymbolID != _LMSymbol.Id &&
1722
                                            connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
1723
                                            connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch" &&
1724
                                            IsMovePoint2D(connector.ConnectItem2SymbolObject, connector, arrow))
1725
                                        {
1726
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1727
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1728
                                                drawingObjectBases.Add(point2D);
1729
                                        }
1730

    
1731
                                    }
1732
                                    #endregion
1733
                                }
1734
                            }
1735
                        }
1736
                        ReleaseCOMObjects(_LMSymbol);
1737
                    }
1738
                    else if (drawingObject.GetType() == typeof(Point2d))
1739
                    {
1740
                        Point2d point2D = drawingObject as Point2d;
1741
                        if (!drawingObjectBases.Contains(point2D))
1742
                            drawingObjectBases.Add(point2D);
1743
                    }
1744
                }
1745

    
1746
                application.ActiveSelectSet.RemoveAll();
1747
                if (drawingObjectBases.Count > 0 && transform != null)
1748
                {
1749
                    application.ActiveSelectSet.AddObjects(drawingObjectBases);
1750
                    SetTransform(transform, arrow);
1751
                    application.ActiveSelectSet.Transform(transform, false);
1752

    
1753
                    application.ActiveSelectSet.RemoveAll();
1754
                    application.ActiveSelectSet.AddObjects(originalDrawingObjectBases);
1755
                }
1756
                
1757
                //foreach (DrawingObjectBaseEx item in drawingObjectBases)
1758
                //    MoveSymbol(item, arrow);
1759

    
1760
                ReleaseCOMObjects(dataSource);
1761
                ReleaseCOMObjects(placement);
1762
            }
1763
        }
1764
        private bool IsMovePoint2D(LMSymbol branchSymbol, LMConnector targetConnector, Arrow arrow)
1765
        {
1766
            bool result = false;
1767
            
1768
            DependencyObject dependency = application.ActiveDocument.ActiveSheet.DrawingObjects[targetConnector.get_GraphicOID().ToString()] as DependencyObject;
1769
            SlopeType mainSlope = GetLineSlopeType(dependency);
1770

    
1771
            if (mainSlope ==  SlopeType.HORIZONTAL || mainSlope == SlopeType.VERTICAL)
1772
            {
1773
                List<SlopeType> types = new List<SlopeType>();
1774

    
1775
                foreach (LMConnector connector in branchSymbol.Connect1Connectors)
1776
                {
1777
                    if (connector.get_ItemStatus() == "Active" && 
1778
                        connector.Id != targetConnector.Id && 
1779
                        !Convert.ToBoolean(connector.get_IsZeroLength()))
1780
                        types.Add(GetLineSlopeType(application.ActiveDocument.ActiveSheet.DrawingObjects[connector.get_GraphicOID().ToString()] as DependencyObject));
1781
                }
1782

    
1783
                foreach (LMConnector connector in branchSymbol.Connect2Connectors)
1784
                {
1785
                    if (connector.get_ItemStatus() == "Active" &&
1786
                        connector.Id != targetConnector.Id &&
1787
                        !Convert.ToBoolean(connector.get_IsZeroLength()))
1788
                        types.Add(GetLineSlopeType(application.ActiveDocument.ActiveSheet.DrawingObjects[connector.get_GraphicOID().ToString()] as DependencyObject));
1789
                }
1790

    
1791
                foreach (var type in types)
1792
                {
1793
                    if (type == mainSlope)
1794
                    {
1795
                        if (type == SlopeType.HORIZONTAL && (arrow == Arrow.Up || arrow == Arrow.Down))
1796
                            result = true;
1797
                        else if (type == SlopeType.VERTICAL && (arrow == Arrow.Left || arrow == Arrow.Right))
1798
                            result = true;
1799
                        else
1800
                        {
1801
                            result = false;
1802
                            break;
1803
                        }
1804
                    }
1805
                    else
1806
                        result = true;
1807
                }
1808

    
1809
                if (result)
1810
                {
1811
                    if ((arrow == Arrow.Down || arrow == Arrow.Up) && mainSlope == SlopeType.VERTICAL)
1812
                        result = false;
1813
                    else if ((arrow == Arrow.Left || arrow == Arrow.Right) && mainSlope == SlopeType.HORIZONTAL)
1814
                        result = false;
1815
                }
1816
            }
1817

    
1818
            return result;
1819
        }
1820
        private SlopeType GetLineSlopeType(DependencyObject dependency)
1821
        {
1822
            if (dependency != null && dependency.DrawingObjects.Count == 1 && dependency.DrawingObjects[0].GetType() == typeof(LineString2d))
1823
            {
1824
                LineString2d line = dependency.DrawingObjects[0] as LineString2d;
1825
                double x1 = 0, y1 = 0, x2 = double.MaxValue, y2 = double.MaxValue;
1826
                for (int i = 0; i < line.KeyPointCount; i++)
1827
                {
1828
                    double x, y, z;
1829
                    KeyPointType keyPointType;
1830
                    HandleType handleType;
1831
                    line.GetKeyPoint(i, out x, out y, out z, out keyPointType, out handleType);
1832
                    if (keyPointType == KeyPointType.igKeyPointStart)
1833
                    {
1834
                        x1 = x;
1835
                        y1 = y;
1836
                    }
1837
                    else if (keyPointType == KeyPointType.igKeyPointEnd)
1838
                    {
1839
                        x2 = x;
1840
                        y2 = y;
1841
                    }
1842
                }
1843
                return SPPIDUtil.CalcSlope(x1, y1, x2, y2, 0.5);
1844
            }
1845

    
1846
            return SlopeType.None;
1847
        }
1848
        private void MoveSymbol(DrawingObjectBaseEx drawingObjectBase, Arrow arrow)
1849
        {
1850
            if (drawingObjectBase == null)
1851
                return;
1852

    
1853
            GridSetting gridSetting = GridSetting.GetInstance();
1854
            switch (arrow)
1855
            {
1856
                case Arrow.Left:
1857
                    drawingObjectBase.Move(0, 0, -gridSetting.Length, 0);
1858
                    break;
1859
                case Arrow.Up:
1860
                    drawingObjectBase.Move(0, 0, 0, gridSetting.Length);
1861
                    break;
1862
                case Arrow.Right:
1863
                    drawingObjectBase.Move(0, 0, gridSetting.Length, 0);
1864
                    break;
1865
                case Arrow.Down:
1866
                    drawingObjectBase.Move(0, 0, 0, -gridSetting.Length);
1867
                    break;
1868
                default:
1869
                    break;
1870
            }
1871
        }
1872
        private void SetTransform(Transform transform, Arrow arrow)
1873
        {
1874
            if (transform == null)
1875
                return;
1876

    
1877
            GridSetting gridSetting = GridSetting.GetInstance();
1878
            switch (arrow)
1879
            {
1880
                case Arrow.Left:
1881
                    transform.DefineByMove2d(-gridSetting.Length, 0);
1882
                    break;
1883
                case Arrow.Up:
1884
                    transform.DefineByMove2d(0, gridSetting.Length);
1885
                    break;
1886
                case Arrow.Right:
1887
                    transform.DefineByMove2d(gridSetting.Length, 0);
1888
                    break;
1889
                case Arrow.Down:
1890
                    transform.DefineByMove2d(0, -gridSetting.Length);
1891
                    break;
1892
                default:
1893
                    break;
1894
            }
1895
        }
1896
        #endregion
1897

    
1898
        #endregion
1899

    
1900

    
1901
        #region TEST
1902
        private void GetSPPIDSymbolRange(Ingr.RAD2D.Symbol2d symbol2d, ref double[] range)
1903
        {
1904
            {
1905
                double x1 = 0;
1906
                double y1 = 0;
1907
                double x2 = 0;
1908
                double y2 = 0;
1909

    
1910
                x1 = double.MaxValue;
1911
                y1 = double.MaxValue;
1912
                x2 = double.MinValue;
1913
                y2 = double.MinValue;
1914
                range = new double[] { x1, y1, x2, y2 };
1915

    
1916
                foreach (var item in symbol2d.DrawingObjects)
1917
                {
1918
                    if (item.GetType() == typeof(Ingr.RAD2D.Line2d))
1919
                    {
1920
                        Ingr.RAD2D.Line2d rangeObject = item as Ingr.RAD2D.Line2d;
1921
                        if (rangeObject.Layer == "Default")
1922
                        {
1923
                            rangeObject.Range(out x1, out y1, out x2, out y2);
1924
                            range = new double[] {
1925
                                Math.Min(x1, range[0]),
1926
                                Math.Min(y1, range[1]),
1927
                                Math.Max(x2, range[2]),
1928
                                Math.Max(y2, range[3])
1929
                            };
1930
                        }
1931
                    }
1932
                    else if (item.GetType() == typeof(Ingr.RAD2D.Circle2d))
1933
                    {
1934
                        Ingr.RAD2D.Circle2d rangeObject = item as Ingr.RAD2D.Circle2d;
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.Rectangle2d))
1947
                    {
1948
                        Ingr.RAD2D.Rectangle2d rangeObject = item as Ingr.RAD2D.Rectangle2d;
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.Arc2d))
1961
                    {
1962
                        Ingr.RAD2D.Arc2d rangeObject = item as Ingr.RAD2D.Arc2d;
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
                }
1975
            }
1976
        }
1977
        private void simpleButton1_Click(object sender, EventArgs e)
1978
        {
1979
            double x1 = double.MaxValue;
1980
            double y1 = double.MaxValue;
1981
            double x2 = double.MinValue;
1982
            double y2 = double.MinValue;
1983
            foreach (DrawingObjectBase item in application.ActiveSelectSet)
1984
            {
1985
                double[] range = new double[] { };
1986
                GetSPPIDSymbolRange(item as Symbol2d, ref range);
1987
            }
1988
            return;
1989

    
1990
            //SPPIDUtil.test();
1991
            if (application.ActiveSelectSet.Count == 0)
1992
                return;
1993

    
1994
            foreach (DrawingObjectBase item in application.ActiveSelectSet)
1995
            {
1996
                DependencyObject dependency = item as DependencyObject;
1997
                if (dependency != null)
1998
                {
1999
                    foreach (var attributes in dependency.AttributeSets)
2000
                    {
2001
                        foreach (var attribute in attributes)
2002
                        {
2003
                            string name = attribute.Name;
2004
                            object value = attribute.GetValue();
2005
                            if (name == "ModelID")
2006
                            {
2007
                                Placement placement = new Placement();
2008
                                LMADataSource dataSource = placement.PIDDataSource;
2009
                                LMPipeRun pipeRun = dataSource.GetPipeRun(value);
2010
                                string attrName = "PlantGroup.Name";
2011
                                LMAAttribute lMAAttribute = pipeRun.Attributes[attrName];
2012
                                if (lMAAttribute != null)
2013
                                {
2014
                                    lMAAttribute.set_Value("25");
2015
                                    pipeRun.Commit();
2016
                                }
2017
                                else
2018
                                {
2019

    
2020
                                }
2021
                            }
2022
                        }
2023
                    }
2024
                }
2025
            }
2026
            
2027
            
2028

    
2029
            //string a = "0A509911F33441A2AF088BFBA78B770D";
2030
            //LMLabelPersist label = dataSource.GetLabelPersist(a);
2031
            //label.set_XCoordinate(0.4);
2032

    
2033

    
2034

    
2035

    
2036
            //LMOptionSettings
2037

    
2038

    
2039
            //LMAFilter filter = new LMAFilter();
2040
            //LMACriterion criterion = new LMACriterion();
2041
            //filter.ItemType = "Relationship";
2042
            //criterion.SourceAttributeName = "SP_DRAWINGID";
2043
            //criterion.Operator = "=";
2044
            //criterion.set_ValueAttribute(drawingID);
2045
            //filter.get_Criteria().Add(criterion);
2046

    
2047
            //LMRelationships relationships = new LMRelationships();
2048
            //relationships.Collect(dataSource, Filter: filter);
2049

    
2050
        }
2051
        private void AutoJoinPipeRun()
2052
        {
2053
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
2054
            WrapperApplication wApp = new WrapperApplication(application.Application);
2055
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
2056

    
2057
            string modelItemId = null;
2058
            List<double[]> vertices = new List<double[]>();
2059
            if (radApp.ActiveSelectSet.Count == 0)
2060
            {
2061
                return;
2062
            }
2063
            dynamic OID = radApp.ActiveSelectSet[0].Key();
2064
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2065
            foreach (var attributes in drawingObject.AttributeSets)
2066
            {
2067
                foreach (var attribute in attributes)
2068
                {
2069
                    if (attribute.Name == "ModelID")
2070
                        modelItemId = attribute.GetValue().ToString();
2071
                }
2072
            }
2073
            radApp.ActiveSelectSet.RemoveAll();
2074

    
2075
        }
2076
        #endregion
2077

    
2078

    
2079
        [DllImport("user32.dll")]
2080
        public static extern int FindWindow(string lpClassName, string lpWindowName);
2081

    
2082
        [DllImport("user32.dll", SetLastError = true)]
2083
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
2084

    
2085
        private void btnLinkOPCTest_Click(object sender, EventArgs e)
2086
        {
2087
            Placement placement = new Placement();
2088
            LMADataSource dataSource = placement.PIDDataSource;
2089

    
2090
            LMAFilter filter = new LMAFilter();
2091
            filter.ItemType = "REPRESENTATION";
2092
            
2093
            LMACriterion criterion = new LMACriterion();
2094
            criterion.SourceAttributeName = "REPRESENTATIONTYPE";
2095
            criterion.Operator = "=";
2096
            criterion.set_ValueAttribute("39");
2097
            filter.get_Criteria().Add(criterion);
2098

    
2099
            LMRepresentations representations = new LMRepresentations();
2100
            representations.Collect(dataSource, Filter: filter);
2101

    
2102
            foreach (LMRepresentation representation in representations)
2103
            {
2104
                if (representation.get_ItemStatus() == "Active" && representation.DrawingID != "0")
2105
                {
2106
                    LMOPC opc = dataSource.GetOPC(representation.ModelItemID);
2107
                    LMOPC pairOPC = opc.pairedWithOPCObject;
2108
                    bool isPaired = false;
2109
                    foreach (LMRepresentation rep in pairOPC.Representations)
2110
                        if (rep.DrawingID != "0")
2111
                            isPaired = true;
2112

    
2113
                    if (!isPaired)
2114
                    {
2115
                        LMSymbol newOPC = placement.PIDPlaceSymbol(representation.get_FileName().ToString(), 0, 0, ExistingItem: pairOPC.AsLMAItem());
2116
                        ReleaseCOMObjects(newOPC);
2117
                    }
2118

    
2119
                    ReleaseCOMObjects(opc);
2120
                    ReleaseCOMObjects(pairOPC);
2121
                    
2122
                }
2123
            }
2124

    
2125
            ReleaseCOMObjects(representations);
2126
            ReleaseCOMObjects(criterion);
2127
            ReleaseCOMObjects(filter);
2128
            ReleaseCOMObjects(dataSource);
2129
            ReleaseCOMObjects(placement);
2130
        }
2131

    
2132
        #region Test For SECL
2133
        private void btnAutoBreakTest_Click(object sender, EventArgs e)
2134
        {
2135
            
2136
        }
2137

    
2138
        private void FindRun(LMSymbol startSymbol, LMSymbol endSymbol, string model)
2139
        {
2140
            Placement placement = new Placement();
2141
            LMADataSource dataSource = placement.PIDDataSource;
2142
            List<string> endRepID = new List<string>();
2143
            List<string> findRepID = new List<string>();
2144
            endRepID.Add(startSymbol.AsLMRepresentation().Id);
2145
            findRepID.Add(startSymbol.AsLMRepresentation().Id);
2146
            bool end = false;
2147
            LoopRunTest(startSymbol.AsLMRepresentation(), endRepID, endSymbol.AsLMRepresentation().Id, ref end, findRepID);
2148

    
2149

    
2150
            //findRepID.Reverse();
2151
            for (int i = 0; i < findRepID.Count; i++)
2152
            {
2153
                string repID = findRepID[i];
2154

    
2155
                LMRepresentation representation = dataSource.GetRepresentation(repID);
2156
                if (representation != null)
2157
                {
2158
                    string fileName = representation.get_FileName().ToString();
2159
                    if (fileName.Contains(@"\Piping\Valves"))
2160
                    {
2161
                        LMModelItem modelItem = representation.ModelItemObject;
2162
                        LMSymbol tarsym = dataSource.GetSymbol(representation.Id);
2163
                        LMRepresentation prevRep = dataSource.GetRepresentation(findRepID[i - 1]);
2164
                        LMConnector removeConnector = dataSource.GetConnector(prevRep.Id);
2165

    
2166
                        List<double[]> vertices = GetConnectorVertices(removeConnector);
2167

    
2168
                        ZoomObjectByGraphicOID(vertices[0][0], vertices[0][1]);
2169
                        Array array = new double[] { 0, vertices[0][0], vertices[0][1] };
2170
                        LMLabelPersist _LmLabelPersist = placement.PIDPlaceLabel(model, ref array, LabeledItem: removeConnector.AsLMRepresentation(), IsLeaderVisible: true);
2171
                        if (_LmLabelPersist == null)
2172
                        {
2173
                            if (removeConnector.get_ItemStatus() == "Active")
2174
                            {
2175
                                LMConnector newConnector = ReModelingLine(removeConnector, removeConnector.ConnectItem1SymbolObject, removeConnector.ConnectItem2SymbolObject, true, false);
2176
                                FindRun(startSymbol, endSymbol, model);
2177
                            }
2178
                        }
2179
                        
2180

    
2181

    
2182
                        //string dd = startSymbol.ModelItemObject.Attributes["SteamOutTemperature"].get_Value();
2183
                        //string dd2 = endSymbol.ModelItemObject.Attributes["SteamOutTemperature"].get_Value();
2184
                        //double value1 = Convert.ToDouble(dd.Split(new char[] { ' ' })[0]);
2185
                        //double value2 = Convert.ToDouble(dd2.Split(new char[] { ' ' })[0]);
2186

    
2187
                        //if (value1 > value2)
2188
                        //{
2189
                        //    LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i + 1]);
2190
                        //    placement.PIDRemovePlacement(prevRep);
2191
                        //}
2192
                        //else
2193
                        //{
2194
                        //    LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i - 1]);
2195
                        //    placement.PIDRemovePlacement(prevRep);
2196
                        //}
2197

    
2198
                        //LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i - 1]);
2199
                        //placement.PIDRemovePlacement(prevRep);
2200

    
2201

    
2202
                        //application.ActiveDocument.Save();
2203
                        //ReleaseCOMObjects(modelItem);
2204
                        break;
2205
                    }
2206
                }
2207

    
2208
                ReleaseCOMObjects(representation);
2209
            }
2210
        }
2211
        private LMConnector ReModelingLine(LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
2212
        {
2213
            Placement _placement = new Placement();
2214
            LMADataSource dataSource = _placement.PIDDataSource;
2215

    
2216
            string symbolPath = string.Empty;
2217
            #region get symbol path
2218
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
2219
            symbolPath = GetSPPIDFileName(modelItem);
2220
            ReleaseCOMObjects(modelItem);
2221
            #endregion
2222
            bool diagonal = false;
2223
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2224
            LMConnector newConnector = null;
2225
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
2226
            DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2227
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2228
            int verticesCount = lineStringGeometry.VertexCount;
2229
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2230

    
2231
            List<double[]> vertices = new List<double[]>();
2232
            for (int i = 1; i <= verticesCount; i++)
2233
            {
2234
                double x = 0;
2235
                double y = 0;
2236
                lineStringGeometry.GetVertex(i, ref x, ref y);
2237
                vertices.Add(new double[] { x, y });
2238
            }
2239

    
2240
            for (int i = 0; i < vertices.Count; i++)
2241
            {
2242
                double[] points = vertices[i];
2243
                // 시작 심볼이 있고 첫번째 좌표일 때
2244
                if (startSymbol != null && i == 0)
2245
                {
2246
                    if (bStart)
2247
                    {
2248
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
2249
                        if (slopeType == SlopeType.HORIZONTAL)
2250
                            placeRunInputs.AddPoint(points[0], -0.1);
2251
                        else if (slopeType == SlopeType.VERTICAL)
2252
                            placeRunInputs.AddPoint(-0.1, points[1]);
2253
                        else
2254
                            placeRunInputs.AddPoint(points[0], -0.1);
2255

    
2256
                        placeRunInputs.AddPoint(points[0], points[1]);
2257
                    }
2258
                    else
2259
                    {
2260
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2261
                    }
2262
                }
2263
                // 마지막 심볼이 있고 마지막 좌표일 때
2264
                else if (endSymbol != null && i == vertices.Count - 1)
2265
                {
2266
                    if (bEnd)
2267
                    {
2268
                        placeRunInputs.AddPoint(points[0], points[1]);
2269

    
2270
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2271
                        if (slopeType == SlopeType.HORIZONTAL)
2272
                            placeRunInputs.AddPoint(points[0], -0.1);
2273
                        else if (slopeType == SlopeType.VERTICAL)
2274
                            placeRunInputs.AddPoint(-0.1, points[1]);
2275
                        else
2276
                            placeRunInputs.AddPoint(points[0], -0.1);
2277
                    }
2278
                    else
2279
                    {
2280
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2281
                    }
2282
                }
2283
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2284
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2285
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2286
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2287
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2288
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2289
                else
2290
                    placeRunInputs.AddPoint(points[0], points[1]);
2291
            }
2292

    
2293
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2294
            Thread.Sleep(1000);
2295
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2296

    
2297
            ReleaseCOMObjects(placeRunInputs);
2298
            ReleaseCOMObjects(_LMAItem);
2299
            ReleaseCOMObjects(modelItem);
2300

    
2301
            if (newConnector != null)
2302
            {
2303
                newConnector.Commit();
2304
                if (startSymbol != null && bStart)
2305
                {
2306
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2307
                    placeRunInputs = new PlaceRunInputs();
2308
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2309
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2310
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2311
                    if (_LMConnector != null)
2312
                    {
2313
                        _LMConnector.Commit();
2314
                        newConnector = RemoveConnectorForReModelingLine(newConnector);
2315
                        ReleaseCOMObjects(_LMConnector);
2316
                    }
2317
                    ReleaseCOMObjects(placeRunInputs);
2318
                    ReleaseCOMObjects(_LMAItem);
2319
                }
2320

    
2321
                if (endSymbol != null && bEnd)
2322
                {
2323
                    if (startSymbol != null)
2324
                    {
2325
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2326
                        newConnector = dicVertices.First().Key;
2327
                    }
2328

    
2329
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2330
                    placeRunInputs = new PlaceRunInputs();
2331
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2332
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2333
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2334
                    if (_LMConnector != null)
2335
                    {
2336
                        _LMConnector.Commit();
2337
                        newConnector = RemoveConnectorForReModelingLine(newConnector);
2338
                        ReleaseCOMObjects(_LMConnector);
2339
                    }
2340
                    ReleaseCOMObjects(placeRunInputs);
2341
                    ReleaseCOMObjects(_LMAItem);
2342
                }
2343

    
2344
                
2345
            }
2346
            ReleaseCOMObjects(modelItem);
2347
            return newConnector;
2348
            
2349
        }
2350

    
2351
        private LMConnector RemoveConnectorForReModelingLine(LMConnector connector)
2352
        {
2353
            Placement _placement = new Placement();
2354
            LMADataSource dataSource = _placement.PIDDataSource;
2355

    
2356
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2357
            foreach (var item in dicVertices)
2358
            {
2359
                if (item.Value.Count == 2)
2360
                {
2361
                    bool result = false;
2362
                    foreach (var point in item.Value)
2363
                    {
2364
                        if (point[0] < 0 || point[1] < 0)
2365
                        {
2366
                            result = true;
2367
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2368
                            break;
2369
                        }
2370
                    }
2371

    
2372
                    if (result)
2373
                        break;
2374
                }
2375
            }
2376

    
2377
            LMConnector result111 = null;
2378
            foreach (var item in dicVertices)
2379
            {
2380
                if (item.Key.get_ItemStatus() == "Active")
2381
                    result111 = item.Key;
2382
                else
2383
                    ReleaseCOMObjects(item.Key);
2384
            }
2385

    
2386
            return result111;
2387
        }
2388

    
2389
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId, bool ContainZeroLength = true)
2390
        {
2391
            Placement _placement = new Placement();
2392
            LMADataSource dataSource = _placement.PIDDataSource;
2393

    
2394
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2395
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2396

    
2397
            if (modelItem != null)
2398
            {
2399
                foreach (LMRepresentation rep in modelItem.Representations)
2400
                {
2401
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2402
                    {
2403
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2404
                        if (!ContainZeroLength && Convert.ToBoolean(_LMConnector.get_IsZeroLength()))
2405
                        {
2406
                            ReleaseCOMObjects(_LMConnector);
2407
                            _LMConnector = null;
2408
                            continue;
2409
                        }
2410
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2411
                        dynamic OID = rep.get_GraphicOID().ToString();
2412
                        DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2413
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2414
                        int verticesCount = lineStringGeometry.VertexCount;
2415
                        double[] vertices = null;
2416
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2417
                        for (int i = 0; i < verticesCount; i++)
2418
                        {
2419
                            double x = 0;
2420
                            double y = 0;
2421
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2422
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
2423
                        }
2424
                    }
2425
                }
2426

    
2427
                ReleaseCOMObjects(modelItem);
2428
            }
2429

    
2430
            return connectorVertices;
2431
        }
2432
        private string GetSPPIDFileName(LMModelItem modelItem)
2433
        {
2434
            string symbolPath = null;
2435
            foreach (LMRepresentation rep in modelItem.Representations)
2436
            {
2437
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2438
                {
2439
                    symbolPath = rep.get_FileName();
2440
                    break;
2441
                }
2442
            }
2443
            return symbolPath;
2444
        }
2445
        private string GetSPPIDFileName(string modelItemId)
2446
        {
2447
            Placement placement = new Placement();
2448
            LMADataSource dataSource = placement.PIDDataSource;
2449

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

    
2493
                    if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID) && item.Item1RepresentationObject.get_ItemStatus() == "Active" &&
2494
                        (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item1RepresentationObject.get_RepresentationClass() == "Connector"))
2495
                    {
2496
                        if (endID != item.Item1RepresentationID)
2497
                        {
2498
                            endRepID.Add(item.Item1RepresentationID);
2499
                            LoopRunTest(item.Item1RepresentationObject, endRepID, endID, ref end, findRepID);
2500
                            if (end)
2501
                                findRepID.Add(item.Item1RepresentationID);
2502
                        }
2503
                        else
2504
                            end = true;
2505
                    }
2506

    
2507
                    if (end)
2508
                        break;
2509

    
2510
                    if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID) && item.Item2RepresentationObject.get_ItemStatus() == "Active" &&
2511
                        (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item2RepresentationObject.get_RepresentationClass() == "Connector"))
2512
                    {
2513
                        if (endID != item.Item2RepresentationID)
2514
                        {
2515
                            endRepID.Add(item.Item2RepresentationID);
2516
                            LoopRunTest(item.Item2RepresentationObject, endRepID, endID, ref end, findRepID);
2517
                            if (end)
2518
                                findRepID.Add(item.Item2RepresentationID);
2519
                        }
2520
                        else
2521
                            end = true;
2522
                    }
2523
                }
2524
            }
2525

    
2526
            if (!end)
2527
            {
2528
                foreach (LMRelationship item in rep.Relation2Relationships)
2529
                {
2530
                    if (end)
2531
                        break;
2532

    
2533
                    if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID) && item.Item1RepresentationObject.get_ItemStatus() == "Active" &&
2534
                        (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item1RepresentationObject.get_RepresentationClass() == "Connector"))
2535
                    {
2536
                        if (endID != item.Item1RepresentationID)
2537
                        {
2538
                            endRepID.Add(item.Item1RepresentationID);
2539
                            LoopRunTest(item.Item1RepresentationObject, endRepID, endID, ref end, findRepID);
2540
                            if (end)
2541
                                findRepID.Add(item.Item1RepresentationID);
2542
                        }
2543
                        else
2544
                            end = true;
2545
                    }
2546

    
2547
                    if (end)
2548
                        break;
2549

    
2550
                    if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID) && item.Item2RepresentationObject.get_ItemStatus() == "Active" &&
2551
                        (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item2RepresentationObject.get_RepresentationClass() == "Connector"))
2552
                    {
2553
                        if (endID != item.Item2RepresentationID)
2554
                        {
2555
                            endRepID.Add(item.Item2RepresentationID);
2556
                            LoopRunTest(item.Item2RepresentationObject, endRepID, endID, ref end, findRepID);
2557
                            if (end)
2558
                                findRepID.Add(item.Item2RepresentationID);
2559
                        }
2560
                        else
2561
                            end = true;
2562
                    }
2563
                }
2564
            }
2565
        }
2566

    
2567
        private void btnOPC_Click(object sender, EventArgs e)
2568
        {
2569
            Placement _placement;
2570
            LMADataSource dataSource;
2571
            _placement = new Placement();
2572
            dataSource = _placement.PIDDataSource;
2573

    
2574
            OPCFlowDirection();
2575

    
2576
            void OPCFlowDirection()
2577
            {
2578
                //current LMDrawing 가져오기
2579
                LMAFilter filter = new LMAFilter();
2580
                LMACriterion criterion = new LMACriterion();
2581
                filter.ItemType = "Drawing";
2582
                criterion.SourceAttributeName = "Name";
2583
                criterion.Operator = "=";
2584
                criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
2585
                filter.get_Criteria().Add(criterion);
2586

    
2587
                LMDrawings drawings = new LMDrawings();
2588
                drawings.Collect(dataSource, Filter: filter);
2589

    
2590
                // Input Drawing Attribute
2591
                LMDrawing drawing = ((dynamic)drawings).Nth(1);
2592

    
2593
                LMAFilter filter2 = new LMAFilter();
2594
                filter2.ItemType = "REPRESENTATION";
2595

    
2596
                LMACriterion criterion2 = new LMACriterion();
2597
                criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
2598
                criterion2.Operator = "=";
2599
                criterion2.set_ValueAttribute("39");
2600
                filter2.get_Criteria().Add(criterion2);
2601

    
2602
                LMRepresentations representations = new LMRepresentations();
2603
                representations.Collect(dataSource, Filter: filter2);
2604

    
2605
                foreach (LMRepresentation representation in representations)
2606
                {
2607
                    if (representation.DrawingID == drawing.Id)
2608
                    {
2609
                        RunOPC(representation);
2610
                    }
2611
                }
2612
                
2613
                ReleaseCOMObjects(filter);
2614
                ReleaseCOMObjects(filter2);
2615
                ReleaseCOMObjects(criterion);
2616
                ReleaseCOMObjects(criterion2);
2617
                ReleaseCOMObjects(drawing);
2618
                ReleaseCOMObjects(drawings);
2619
                ReleaseCOMObjects(representations);
2620
            }
2621

    
2622
            void RunOPC(LMRepresentation representation)
2623
            {
2624
                LMSymbol symbol = dataSource.GetSymbol(representation.Id);
2625
                List<string> endRepId = new List<string>();
2626
                endRepId.Add(symbol.AsLMRepresentation().Id);
2627
                LoopSymbol(symbol, endRepId);
2628
                
2629
                ReleaseCOMObjects(symbol);
2630

    
2631
                void LoopSymbol(LMSymbol loopSymbol, List<string> endRepID)
2632
                {
2633
                    foreach (LMRelationship item in loopSymbol.Relation1Relationships)
2634
                    {
2635
                        if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID))
2636
                        {
2637
                            endRepID.Add(item.Item1RepresentationID);
2638
                            if (item.Item1RepresentationObject.get_RepresentationClass() == "Connector")
2639
                            {
2640
                                LMModelItem modelItem = item.Item1RepresentationObject.ModelItemObject;
2641
                                SetAttribute(modelItem);
2642
                                LoopConnector(dataSource.GetConnector(item.Item1RepresentationID), endRepID);
2643
                            }
2644
                            else if (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol")
2645
                            {
2646
                                LoopSymbol(dataSource.GetSymbol(item.Item1RepresentationID), endRepID);
2647
                            }
2648
                        }
2649

    
2650
                        if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID))
2651
                        {
2652
                            endRepID.Add(item.Item2RepresentationID);
2653
                            if (item.Item2RepresentationObject.get_RepresentationClass() == "Connector")
2654
                            {
2655
                                LMModelItem modelItem = item.Item2RepresentationObject.ModelItemObject;
2656
                                SetAttribute(modelItem);
2657
                                LoopConnector(dataSource.GetConnector(item.Item2RepresentationID), endRepID);
2658
                            }
2659
                            else if (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol")
2660
                            {
2661
                                LoopSymbol(dataSource.GetSymbol(item.Item2RepresentationID), endRepID);
2662
                            }
2663
                        }
2664
                    }
2665

    
2666
                    foreach (LMRelationship item in loopSymbol.Relation2Relationships)
2667
                    {
2668
                        if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID))
2669
                        {
2670
                            endRepID.Add(item.Item1RepresentationID);
2671
                            if (item.Item1RepresentationObject.get_RepresentationClass() == "Connector")
2672
                            {
2673
                                LMModelItem modelItem = item.Item1RepresentationObject.ModelItemObject;
2674
                                SetAttribute(modelItem);
2675
                                LoopConnector(dataSource.GetConnector(item.Item1RepresentationID), endRepID);
2676
                            }
2677
                            else if (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol")
2678
                            {
2679
                                LoopSymbol(dataSource.GetSymbol(item.Item1RepresentationID), endRepID);
2680
                            }
2681
                        }
2682

    
2683
                        if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID))
2684
                        {
2685
                            endRepID.Add(item.Item2RepresentationID);
2686
                            if (item.Item2RepresentationObject.get_RepresentationClass() == "Connector")
2687
                            {
2688
                                LMModelItem modelItem = item.Item2RepresentationObject.ModelItemObject;
2689
                                SetAttribute(modelItem);
2690
                                LoopConnector(dataSource.GetConnector(item.Item2RepresentationID), endRepID);
2691
                            }
2692
                            else if (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol")
2693
                            {
2694
                                LoopSymbol(dataSource.GetSymbol(item.Item2RepresentationID), endRepID);
2695
                            }
2696
                        }
2697
                    }
2698

    
2699
                    foreach (LMConnector item in loopSymbol.Connect1Connectors)//나가는것
2700
                    {
2701
                        if (item.get_ItemStatus() != "Active" || endRepID.Contains(item.AsLMRepresentation().Id))
2702
                            continue;
2703

    
2704
                        endRepID.Add(item.AsLMRepresentation().Id);
2705
                        if (item.get_ItemStatus() == "Active")
2706
                        {
2707
                            LMModelItem modelItem = item.ModelItemObject;
2708
                            SetAttribute(modelItem);
2709
                            LoopConnector(item, endRepID);
2710
                        }
2711
                    }
2712

    
2713
                    foreach (LMConnector item in loopSymbol.Connect2Connectors)//들어오는것
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

    
2728
                void LoopConnector(LMConnector connector, List<string> endRepID)
2729
                {
2730
                    if (connector.ConnectItem1SymbolObject != null && !endRepID.Contains(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id))
2731
                    {
2732
                        endRepID.Add(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id);
2733
                        //LoopSymbol(connector.ConnectItem1SymbolObject, endRepID);
2734
                    }
2735

    
2736
                    if (connector.ConnectItem2SymbolObject != null && !endRepID.Contains(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id))
2737
                    {
2738
                        endRepID.Add(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id);
2739
                        //LoopSymbol(connector.ConnectItem2SymbolObject, endRepID);
2740
                    }
2741
                }
2742
            }
2743

    
2744
            void SetAttribute(LMModelItem modelItem)
2745
            {
2746
                LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
2747
                if (attribute != null)
2748
                    attribute.set_Value("End 1 is upstream (Inlet)");
2749
                modelItem.Commit();
2750
            }
2751

    
2752
            ReleaseCOMObjects(_placement);
2753
            ReleaseCOMObjects(dataSource);
2754

    
2755
            MessageBox.Show("End OPC Flow Direction");
2756
        }
2757

    
2758
        private void btnCreateStreamBreak_Click(object sender, EventArgs e)
2759
        {
2760
            Placement _placement;
2761
            LMADataSource dataSource;
2762

    
2763
            StreamNoBreak();
2764

    
2765
            void StreamNoBreak()
2766
            {
2767
                
2768
                _placement = new Placement();
2769
                dataSource = _placement.PIDDataSource;
2770

    
2771
                OPCFlowDirection();
2772

    
2773
                void OPCFlowDirection()
2774
                {
2775
                    //current LMDrawing 가져오기
2776
                    LMAFilter filter = new LMAFilter();
2777
                    LMACriterion criterion = new LMACriterion();
2778
                    filter.ItemType = "Drawing";
2779
                    criterion.SourceAttributeName = "Name";
2780
                    criterion.Operator = "=";
2781
                    criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
2782
                    filter.get_Criteria().Add(criterion);
2783

    
2784
                    LMDrawings drawings = new LMDrawings();
2785
                    drawings.Collect(dataSource, Filter: filter);
2786

    
2787
                    // Input Drawing Attribute
2788
                    LMDrawing drawing = ((dynamic)drawings).Nth(1);
2789

    
2790
                    LMAFilter filter2 = new LMAFilter();
2791
                    filter2.ItemType = "REPRESENTATION";
2792

    
2793
                    LMACriterion criterion2 = new LMACriterion();
2794
                    criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
2795
                    criterion2.Operator = "=";
2796
                    criterion2.set_ValueAttribute("46");
2797
                    filter2.get_Criteria().Add(criterion2);
2798

    
2799
                    LMRepresentations representations = new LMRepresentations();
2800
                    representations.Collect(dataSource, Filter: filter2);
2801

    
2802
                    foreach (LMRepresentation representation in representations)
2803
                    {
2804
                        if (representation.DrawingID == drawing.Id)
2805
                        {
2806
                            LMSymbol symbol = dataSource.GetSymbol(representation.Id);
2807
                            CreateSpec(symbol);
2808
                        }
2809
                    }
2810

    
2811
                    ReleaseCOMObjects(filter);
2812
                    ReleaseCOMObjects(filter2);
2813
                    ReleaseCOMObjects(criterion);
2814
                    ReleaseCOMObjects(criterion2);
2815
                    ReleaseCOMObjects(drawing);
2816
                    ReleaseCOMObjects(drawings);
2817
                    ReleaseCOMObjects(representations);
2818
                }
2819
            }
2820

    
2821
            void CreateSpec(LMSymbol symbol)
2822
            {
2823
                ZoomObjectByGraphicOID(symbol.get_XCoordinate(), symbol.get_YCoordinate());
2824

    
2825
                foreach (LMConnector item in symbol.Connect1Connectors)
2826
                {
2827
                    if (item.get_ItemStatus() == "Active")
2828
                    {
2829
                        Array array = new double[] { 0, symbol.get_XCoordinate(), symbol.get_YCoordinate() };
2830
                        LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(@"\Piping\Segment Breaks\Above\stream Number Break.sym", ref array, LabeledItem: item.AsLMRepresentation(), IsLeaderVisible: true);
2831
                    }
2832
                }
2833

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

    
2843
                Thread.Sleep(500);
2844
            }
2845

    
2846
            MessageBox.Show("End Modeling");
2847
        }
2848
        
2849
        private void btnPipeRunBreak_Click(object sender, EventArgs e)
2850
        {
2851
            Placement placement = new Placement();
2852
            LMADataSource dataSource = placement.PIDDataSource;
2853

    
2854
            string sFrom = textEdit1.EditValue.ToString();
2855
            string sTo = textEdit2.EditValue.ToString();
2856

    
2857
            LMModelItem item1 = dataSource.GetModelItem(sFrom);
2858
            LMModelItem item2 = dataSource.GetModelItem(sTo);
2859
            if (item1 != null && item1.Representations.Count == 1 &&
2860
                item2 != null && item2.Representations.Count == 1)
2861
            {
2862
                LMSymbol startSymbol = dataSource.GetSymbol(item1.Representations.Nth[1].Id);
2863
                LMSymbol endSymbol = dataSource.GetSymbol(item2.Representations.Nth[1].Id);
2864

    
2865
                if (startSymbol != null && endSymbol != null)
2866
                {
2867
                    FindRun(startSymbol, endSymbol, @"\Piping\Segment Breaks\End of Group.sym");
2868
                }
2869
                else
2870
                {
2871
                    MessageBox.Show("From To 심볼 확인");
2872
                }
2873
            }
2874
            else
2875
            {
2876
                MessageBox.Show("From To 심볼 확인");
2877
            }
2878
        }
2879

    
2880
        private void btnSegmentBreak_Click(object sender, EventArgs e)
2881
        {
2882
            Placement placement = new Placement();
2883
            LMADataSource dataSource = placement.PIDDataSource;
2884

    
2885
            string sFrom = textEdit1.EditValue.ToString();
2886
            string sTo = textEdit2.EditValue.ToString();
2887

    
2888
            LMModelItem item1 = dataSource.GetModelItem(sFrom);
2889
            LMModelItem item2 = dataSource.GetModelItem(sTo);
2890
            if (item1 != null && item1.Representations.Count == 1 &&
2891
                item2 != null && item2.Representations.Count == 1)
2892
            {
2893
                LMSymbol startSymbol = dataSource.GetSymbol(item1.Representations.Nth[1].Id);
2894
                LMSymbol endSymbol = dataSource.GetSymbol(item2.Representations.Nth[1].Id);
2895

    
2896
                if (startSymbol != null && endSymbol != null)
2897
                {
2898
                    FindRun(startSymbol, endSymbol, @"\Piping\Segment Breaks\Max Design Press - Temp.sym");
2899
                }
2900
                else
2901
                {
2902
                    MessageBox.Show("From To 심볼 확인");
2903
                }
2904
            }
2905
            else
2906
            {
2907
                MessageBox.Show("From To 심볼 확인");
2908
            }
2909
        }
2910

    
2911
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
2912
        {
2913
            if (application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
2914
            {
2915
                double minX = 0;
2916
                double minY = 0;
2917
                double maxX = 0;
2918
                double maxY = 0;
2919
                application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
2920
                application.ActiveWindow.ZoomArea2(minX - 0.01, minY - 0.01, maxX + 0.01, maxY + 0.01, null);
2921

    
2922
                Thread.Sleep(milliseconds);
2923
            }
2924
        }
2925

    
2926
        private void ZoomObjectByGraphicOID(double x, double y, int milliseconds = 150)
2927
        {
2928
            application.ActiveWindow.ZoomArea2(x - 0.01, y - 0.01, x + 0.01, y + 0.01, null);
2929
            Thread.Sleep(milliseconds);
2930
        }
2931

    
2932
        #endregion
2933

    
2934
        private void btnGetDrawingSize_Click(object sender, EventArgs e)
2935
        {
2936
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
2937
            WrapperApplication wApp = new WrapperApplication(application.Application);
2938
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
2939

    
2940
            if (radApp.ActiveSelectSet.Count > 0)
2941
            {
2942
                DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject;
2943
                if (line2D != null)
2944
                {
2945
                    double minX = 0;
2946
                    double minY = 0;
2947
                    double maxX = 0;
2948
                    double maxY = 0;
2949
                    line2D.Range(out minX, out minY, out maxX, out maxY);
2950

    
2951
                    Settings.Default.DrawingX = maxX - minX;
2952
                    Settings.Default.DrawingY = maxY - minY;
2953
                    Settings.Default.Save();
2954

    
2955
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
2956
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
2957
                }
2958
                else
2959
                    MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2960
            }
2961
            else
2962
                MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2963
        }
2964

    
2965
        private void btnExportPDF_Click(object sender, EventArgs e)
2966
        {
2967
            dynamic dApplication = Microsoft.VisualBasic.Interaction.GetObject("", "PIDAutomation.Application");
2968
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
2969
            Ingr.RAD2D.Application application = wApp.RADApplication;
2970
            SaveFileDialog dialog = new SaveFileDialog();
2971
            dialog.FileName = application.ActiveDocument.Name.Replace(".pid", "");
2972
            dialog.Filter = "pdf file (*.pdf)|*.pdf";
2973
            if (dialog.ShowDialog() == DialogResult.OK)
2974
            {
2975
                application.ActiveDocument.SaveAsPDF(
2976
                dialog.FileName,
2977
                false,
2978
                Ingr.RAD2D.PDFResolutionConstants.igPDFResolution1200DPI,
2979
                Ingr.RAD2D.PDFColorModeConstants.igPDFColorModePureBlackAndWhite,
2980
                Ingr.RAD2D.PDFJpegCompressionConstants.igPDFJpegGoodQualityGoodCompression);
2981
            }
2982
        }
2983

    
2984
        private void btnUploadDrawingAttribute_Click(object sender, EventArgs e)
2985
        {
2986
            OpenFileDialog openFileDialog = new OpenFileDialog();
2987
            openFileDialog.Filter = "excel files (*.xls;*.xlsx)|*.xls;*.xlsx";
2988
            openFileDialog.Multiselect = false;
2989
            if (openFileDialog.ShowDialog() == DialogResult.OK)
2990
            {
2991
                try
2992
                {
2993
                    DevExpress.Spreadsheet.IWorkbook workbook = new DevExpress.Spreadsheet.Workbook();
2994
                    workbook.LoadDocument(openFileDialog.FileName);
2995

    
2996
                    DevExpress.Spreadsheet.Worksheet worksheet = workbook.Worksheets[0];
2997

    
2998
                    DevExpress.Spreadsheet.Row headerNameRow = worksheet.Rows[0];
2999
                    DataTable dt = new DataTable();
3000
                    int colIndex = 0;
3001
                    while (true)
3002
                    {
3003
                        string colName = headerNameRow[colIndex].DisplayText;
3004
                        if (string.IsNullOrEmpty(colName))
3005
                            break;
3006
                        dt.Columns.Add(colName);
3007
                        colIndex++;
3008
                    }
3009

    
3010
                    int index = 1;
3011
                    while (true)
3012
                    {
3013
                        List<string> value = new List<string>();
3014
                        DevExpress.Spreadsheet.Row row = worksheet.Rows[index];
3015
                        string Drawing_Name = row[0].DisplayText;
3016
                        if (string.IsNullOrEmpty(Drawing_Name))
3017
                            break;
3018

    
3019
                        DataRow dataRow = dt.NewRow();
3020
                        for (int i = 0; i < dt.Columns.Count; i++)
3021
                        {
3022
                            string sValue = row[i].DisplayText;
3023
                            dataRow[i] = sValue;
3024
                        }
3025
                        dt.Rows.Add(dataRow);
3026
                        index++;
3027
                    }
3028

    
3029
                    foreach (DataRow row in dt.Rows)
3030
                    {
3031
                        string drawingName = row[0].ToString();
3032
                        LMDrawing drawing = GetDrawingByName(drawingName);
3033
                        if (drawing != null)
3034
                        {
3035
                            for (int i = 1; i < dt.Columns.Count; i++)
3036
                            {
3037
                                string value = row[i].ToString();
3038
                                if (string.IsNullOrEmpty(value))
3039
                                    continue;
3040
                                
3041
                                string attrName = dt.Columns[i].ColumnName;
3042
                                LMAAttribute attribute = drawing.Attributes[attrName];
3043
                                if (attribute != null)
3044
                                    attribute.set_Value(value);
3045
                            }
3046
                            drawing.Commit();
3047
                        }
3048
                    }
3049

    
3050
                    workbook.Dispose();
3051
                    MessageBox.Show("End upload drawing attribute", "SPPID Converter", MessageBoxButtons.OK, MessageBoxIcon.Information);
3052
                }
3053
                catch (Exception ex)
3054
                {
3055
                    MessageBox.Show(ex.Message);
3056
                }
3057
            }
3058

    
3059
            LMDrawing GetDrawingByName(string name)
3060
            {
3061
                Placement placement = new Placement();
3062
                LMADataSource dataSource = placement.PIDDataSource;
3063

    
3064
                LMAFilter filter = new LMAFilter();
3065
                LMACriterion criterion = new LMACriterion();
3066
                filter.ItemType = "Drawing";
3067
                criterion.SourceAttributeName = "Name";
3068
                criterion.Operator = "=";
3069
                criterion.set_ValueAttribute(name);
3070
                filter.get_Criteria().Add(criterion);
3071

    
3072
                LMDrawings drawings = new LMDrawings();
3073
                drawings.Collect(dataSource, Filter: filter);
3074

    
3075
                // Input Drawing Attribute
3076
                LMDrawing drawing = ((dynamic)drawings).Nth(1);
3077
                return drawing;
3078
            }
3079
        }
3080

    
3081
        string fileName = string.Empty;
3082
        Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
3083
        private void btnRussia_Click(object sender, EventArgs e)
3084
        {
3085
            string noteSymbolPath = @"\Design\Annotation\Labels\DL_Item Note & Label.sym";
3086
            List<string> opcSymbolPath = new List<string>()
3087
            {
3088
                @"\Piping\Piping OPC's\Off-Drawing.sym",
3089
                @"\Instrumentation\Instrument OPC's\Off-Drawing.sym"
3090
            };
3091

    
3092
            if (string.IsNullOrEmpty(fileName))
3093
            {
3094
                OpenFileDialog openFileDialog = new OpenFileDialog();
3095
                openFileDialog.Filter = "excel files (*.xls;*.xlsx)|*.xls;*.xlsx";
3096
                openFileDialog.Multiselect = false;
3097
                if (openFileDialog.ShowDialog() == DialogResult.OK)
3098
                    fileName = openFileDialog.FileName;
3099
                else
3100
                    return;
3101

    
3102
                changeList();
3103
            }
3104

    
3105
            application.ActiveWindow.Zoom = 2000;
3106

    
3107
            Plaice.Placement placement = new Plaice.Placement();
3108
            LMADataSource dataSource = placement.PIDDataSource;
3109
            
3110
            ChangeNote();
3111
            ChangeOPC();
3112

    
3113
            void ChangeNote()
3114
            {
3115
                LMSymbols symbols = GetSymbols(noteSymbolPath);
3116

    
3117
                foreach (LMSymbol symbol in symbols)
3118
                {
3119
                    LMItemNote itemNote = dataSource.GetItemNote(symbol.ModelItemID);
3120
                    LMAAttribute attr = itemNote.Attributes["Note.Body"];
3121
                    object value = attr.get_Value();
3122
                    if (!DBNull.Value.Equals(value))
3123
                    {
3124
                        string sValue = value.ToString();
3125
                        string changeText = string.Empty;
3126
                        if (keyValuePairs.ContainsKey(sValue))
3127
                            changeText = keyValuePairs[sValue];
3128

    
3129
                        if (!string.IsNullOrEmpty(changeText))
3130
                        {
3131
                            CopyNoteItem(symbol, itemNote, changeText);
3132
                        }
3133
                    }
3134
                }
3135
            }
3136
            void ChangeOPC()
3137
            {
3138
                foreach (var file in opcSymbolPath)
3139
                {
3140
                    LMSymbols symbols = GetSymbols(file);
3141
                    foreach (LMSymbol symbol in symbols)
3142
                    {
3143
                        LMOPC opc = dataSource.GetOPC(symbol.ModelItemID);
3144
                        string gOID = symbol.get_GraphicOID().ToString();
3145
                        foreach (LMLabelPersist item in symbol.LabelPersists)
3146
                        {
3147
                            OPCText(item.get_GraphicOID().ToString());
3148
                        }
3149
                    }
3150
                }
3151
            }
3152

    
3153
            
3154

    
3155
            void CopyNoteItem(LMSymbol symbol, LMItemNote itemNote, string text)
3156
            {
3157
                double x = symbol.get_XCoordinate();
3158
                double y = symbol.get_YCoordinate();
3159
                double rotate = symbol.get_RotationAngle();
3160
                ZoomObjectByGraphicOID(symbol.get_GraphicOID().ToString());
3161

    
3162
                LMSymbol newSymbol = placement.PIDPlaceSymbol(noteSymbolPath, x, y, Rotation: rotate);
3163
                LMItemNote newItemNote = dataSource.GetItemNote(newSymbol.ModelItemID);
3164
                LMAAttribute newAttr = newItemNote.Attributes["Note.Body"];
3165
                newAttr.set_Value(text);
3166
                newItemNote.Commit();
3167
                newSymbol.Commit();
3168

    
3169
                double[] symbolRange = null, newSymbolRange = null;
3170
                GetSPPIDSymbolRange(symbol.LabelPersists.Nth[1], ref symbolRange);
3171
                GetSPPIDSymbolRange(newSymbol.LabelPersists.Nth[1], ref newSymbolRange);
3172

    
3173
                Symbol2d moveSymbol2d = GetSymbol2DByRepID(dataSource, newSymbol.AsLMRepresentation().Id);
3174
                if (rotate > -0.1 && rotate < 0.1)
3175
                {
3176
                    moveSymbol2d.Move(0, 0, 0, symbolRange[1] - newSymbolRange[3]);
3177
                }
3178
                else
3179
                {
3180
                    moveSymbol2d.Move(0, 0, symbolRange[2] - newSymbolRange[0], 0);
3181
                }
3182
            }
3183
            
3184
            LMSymbols GetSymbols(string file)
3185
            {
3186
                LMAFilter filter = new LMAFilter();
3187
                LMACriterion criterion1 = new LMACriterion();
3188
                criterion1.SourceAttributeName = "SP_DRAWINGID";
3189
                criterion1.Operator = "=";
3190
                criterion1.set_ValueAttribute(dataSource.PIDMgr.Drawing.ID);
3191
                criterion1.Conjunctive = true;
3192
                filter.get_Criteria().Add(criterion1);
3193
                filter.ItemType = "Symbol";
3194

    
3195
                LMACriterion criterion2 = new LMACriterion();
3196
                criterion2.SourceAttributeName = "ITEMSTATUS";
3197
                criterion2.Operator = "=";
3198
                criterion2.set_ValueAttribute("1");
3199
                criterion2.Conjunctive = true;
3200
                filter.get_Criteria().Add(criterion2);
3201

    
3202
                LMACriterion criterion3 = new LMACriterion();
3203
                criterion3.SourceAttributeName = "INSTOCKPILE";
3204
                criterion3.Operator = "=";
3205
                criterion3.set_ValueAttribute("1");
3206
                criterion3.Conjunctive = true;
3207
                filter.get_Criteria().Add(criterion3);
3208

    
3209
                LMACriterion criterion4 = new LMACriterion();
3210
                criterion4.SourceAttributeName = "FILENAME";
3211
                criterion4.Operator = "=";
3212
                criterion4.set_ValueAttribute(file);
3213
                criterion4.Conjunctive = true;
3214
                filter.get_Criteria().Add(criterion4);
3215

    
3216
                LMSymbols items = new LMSymbols();
3217
                items.Collect(dataSource, Filter: filter);
3218

    
3219
                return items;
3220
            }
3221

    
3222
            void changeList()
3223
            {
3224
                DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook();
3225
                try
3226
                {
3227
                    workbook.LoadDocument(fileName);
3228
                    DevExpress.Spreadsheet.Worksheet sheet = workbook.Worksheets["SPPIDConverter"];
3229
                    if (sheet == null)
3230
                        return;
3231

    
3232
                    int index = 0;
3233
                    while (true)
3234
                    {
3235
                        DevExpress.Spreadsheet.Row row = sheet.Rows[index];
3236
                        string from = row[0].DisplayText;
3237
                        string to = row[1].DisplayText;
3238
                        if (string.IsNullOrEmpty(from) || string.IsNullOrEmpty(to))
3239
                            break;
3240
                        else if(!keyValuePairs.ContainsKey(from))
3241
                            keyValuePairs.Add(from, to);
3242
                        index++;
3243
                    }
3244

    
3245
                }
3246
                catch (Exception ex)
3247
                {
3248
                    MessageBox.Show("error");
3249
                }
3250
                finally
3251
                {
3252
                    workbook.Dispose();
3253
                }
3254
            }
3255

    
3256
            void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3257
            {
3258
                if (application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3259
                {
3260
                    double minX = 0;
3261
                    double minY = 0;
3262
                    double maxX = 0;
3263
                    double maxY = 0;
3264
                    application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3265
                    application.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3266

    
3267
                    Thread.Sleep(milliseconds);
3268
                }
3269
            }
3270

    
3271
            void GetSPPIDSymbolRange(LMLabelPersist labelPersist, ref double[] range)
3272
            {
3273
                if (labelPersist != null)
3274
                {
3275
                    double x1 = double.MaxValue;
3276
                    double y1 = double.MaxValue;
3277
                    double x2 = double.MinValue;
3278
                    double y2 = double.MinValue;
3279
                    range = new double[] { x1, y1, x2, y2 };
3280

    
3281
                    Ingr.RAD2D.DependencyObject dependency = application.ActiveDocument.ActiveSheet.DrawingObjects[labelPersist.get_GraphicOID().ToString()] as DependencyObject;
3282
                    foreach (var item in dependency.DrawingObjects)
3283
                    {
3284
                        Ingr.RAD2D.TextBox textBox = item as Ingr.RAD2D.TextBox;
3285
                        if (textBox != null)
3286
                        {
3287
                            if (dependency != null)
3288
                            {
3289
                                double tempX1;
3290
                                double tempY1;
3291
                                double tempX2;
3292
                                double tempY2;
3293
                                textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
3294
                                x1 = Math.Min(x1, tempX1);
3295
                                y1 = Math.Min(y1, tempY1);
3296
                                x2 = Math.Max(x2, tempX2);
3297
                                y2 = Math.Max(y2, tempY2);
3298

    
3299
                                range = new double[] { x1, y1, x2, y2 };
3300
                            }
3301
                        }
3302
                    }
3303

    
3304
                }
3305
            }
3306

    
3307
            void OPCText(string graphicOID)
3308
            {
3309
                if (application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3310
                {
3311
                    DependencyObject dependency = application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] as DependencyObject;
3312
                    foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
3313
                    {
3314
                        Ingr.RAD2D.TextBox textBox = drawingObject as Ingr.RAD2D.TextBox;
3315
                        if (textBox != null)
3316
                        {
3317
                            string text = textBox.Text;
3318
                            if (!string.IsNullOrEmpty(text))
3319
                            {
3320
                                string changeText;
3321
                                ChangeText(text, out changeText);
3322
                                
3323
                                if (!text.Equals(changeText))
3324
                                {
3325
                                    double tempX1, tempY1, tempX2, tempY2;
3326
                                    textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
3327

    
3328
                                    double x = tempX1, y = tempY1;
3329

    
3330
                                    LMSymbol newSymbol = placement.PIDPlaceSymbol(noteSymbolPath, x, y);
3331
                                    LMItemNote newItemNote = dataSource.GetItemNote(newSymbol.ModelItemID);
3332
                                    LMAAttribute newAttr = newItemNote.Attributes["Note.Body"];
3333
                                    newAttr.set_Value(changeText);
3334
                                    newItemNote.Commit();
3335
                                    newSymbol.Commit();
3336

    
3337
                                    double[] newSymbolRange = null;
3338
                                    GetSPPIDSymbolRange(newSymbol.LabelPersists.Nth[1], ref newSymbolRange);
3339
                                    Symbol2d moveSymbol2d = GetSymbol2DByRepID(dataSource, newSymbol.AsLMRepresentation().Id);
3340
                                    moveSymbol2d.Move(newSymbolRange[2], newSymbolRange[1], tempX1, tempY1);
3341

    
3342
                                    ZoomObjectByGraphicOID(graphicOID);
3343
                                }
3344
                            }
3345
                        }
3346
                    }
3347
                }
3348
            }
3349

    
3350
            void ChangeText(string from, out string to)
3351
            {
3352
                to = from;
3353

    
3354
                foreach (var item in keyValuePairs)
3355
                {
3356
                    string value = item.Key;
3357
                    if (to.Contains(value))
3358
                        to = to.Replace(value, item.Value);
3359
                }
3360
            }
3361
        }
3362
    }
3363
}
클립보드 이미지 추가 (최대 크기: 500 MB)