프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ a80baec5

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

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

    
28
namespace Converter.SPPID.Wrapper
29
{
30
    public partial class ConverterDocking : UserControl
31
    {
32
        Ingr.RAD2D.Application application;
33
        internal static bool addEvent = false;
34
        public ConverterDocking()
35
        {
36
            InitializeComponent();
37
            spinEditSymmetry.Properties.Mask.EditMask = "f0";
38
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
39
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
40
            application = wApp.RADApplication;
41
            
42
            ReleaseCOMObjects(dApplication);
43
            dApplication = null;
44
            try
45
            {
46
                Project_Info _ProjectInfo = Project_Info.GetInstance();
47
                _ProjectInfo.DefaultPath = Settings.Default.ProjectPath;
48
                _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType;
49
                _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP;
50
                _ProjectInfo.Port = Settings.Default.ProjectPort;
51
                _ProjectInfo.DBUser = Settings.Default.ProjectDBUser;
52
                _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword;
53
                if (Project_DB.ConnTestAndCreateTable())
54
                {
55
                    _ProjectInfo.Enable = true;
56
                    layoutControlGroupUtils.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
57

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

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

    
87
#if DEBUG
88
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
89
            layoutControlGroup1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
90

    
91
#else
92
            layoutControlGroup1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
93
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
94
#endif
95

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

    
120
                try
121
                {
122
                    CloseOPCForm.Run();
123

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

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

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

    
192
                    //doc.SaveOnClose = false;
193
                    //doc.Close(false);
194

    
195
                    //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
196
                    //dApplication.Drawings[1].CloseDrawing(false);
197
                    //ReleaseCOMObjects(dApplication);
198
                    //dApplication = null;
199
                }
200

    
201
                MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
202
            }
203
        }
204

    
205
        private void btnLinkOPC_Click(object sender, EventArgs e)
206
        {
207
            Placement placement = new Placement();
208
            LMADataSource dataSource = placement.PIDDataSource;
209

    
210
            LMPlantSettings plantSettings = new LMPlantSettings();
211
            plantSettings.Collect(dataSource);
212
            string settingValue = SPPIDUtil.T_PLANTSETTING_Value("Plant Path");
213

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

    
225
            LMDrawings drawings = new LMDrawings();
226
            drawings.Collect(dataSource, Filter: filter);
227

    
228
            // Input Drawing Attribute
229
            LMDrawing drawing = ((dynamic)drawings).Nth(1);
230

    
231
            LMAFilter filter2 = new LMAFilter();
232
            filter2.ItemType = "REPRESENTATION";
233

    
234
            LMACriterion criterion2 = new LMACriterion();
235
            criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
236
            criterion2.Operator = "=";
237
            criterion2.set_ValueAttribute("39");
238
            filter2.get_Criteria().Add(criterion2);
239

    
240
            LMRepresentations representations = new LMRepresentations();
241
            representations.Collect(dataSource, Filter: filter2);
242

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

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

    
295
                        if (bResult)
296
                        {
297
                            if (representation.DrawingID == drawingID)
298
                                CurrentDrawing.Add(new Tuple<LMRepresentation, string>(representation, sResult));
299
                            else
300
                                OtherDrawing.Add(new Tuple<LMRepresentation, string>(representation, sResult));
301
                        }
302
                    }
303

    
304
                    ReleaseCOMObjects(symbol);
305
                }
306
            }
307

    
308
            List<OPC_Info> _OPC_Infos = new List<OPC_Info>();
309

    
310
            foreach (var current in CurrentDrawing)
311
            {
312
                LMOPC currentOPC = dataSource.GetOPC(current.Item1.ModelItemID);
313
                string fileName = current.Item1.get_FileName();
314
                string tag = current.Item2;
315
                List<LMOPC> findOPCs = new List<LMOPC>(); 
316
                foreach (var other in OtherDrawing)
317
                {
318
                    if (tag == other.Item2 && fileName == other.Item1.get_FileName())
319
                    {
320
                        LMOPC otherOPC = dataSource.GetOPC(other.Item1.ModelItemID);
321
                        findOPCs.Add(otherOPC);
322
                    }
323
                }
324

    
325
                if (findOPCs.Count == 1)
326
                {
327
                    LMRepresentation currentPairRepresentation = currentOPC.pairedWithOPCObject.Representations.Nth[1];
328
                    LMRepresentation findPairRepresentation = findOPCs[0].pairedWithOPCObject.Representations.Nth[1];
329
                    if (currentPairRepresentation == null || findPairRepresentation == null)
330
                        continue;
331
                    else if (currentPairRepresentation.get_ItemStatus() == "Active" && currentPairRepresentation.get_InStockpile() == "True" &&
332
                        findPairRepresentation.get_ItemStatus() == "Active" && findPairRepresentation.get_InStockpile() == "True")
333
                    {
334
                        string currentGraphicOID = currentOPC.Representations.Nth[1].get_GraphicOID().ToString();
335
                        LMRepresentation findRepresentation = findOPCs[0].Representations.Nth[1];
336
                        string findGraphicOID = findRepresentation.get_GraphicOID().ToString();
337
                        LMDrawing findDrawing = findRepresentation.DrawingObject;
338
                        string findDrawingPath = settingValue + findDrawing.get_Path();
339

    
340
                        _OPC_Infos.Add(new OPC_Info()
341
                        {
342
                            CurrentGraphicOID = currentGraphicOID,
343
                            TagValue = tag,
344
                            FindGraphicOID = findGraphicOID,
345
                            FindDrawingPath = findDrawingPath,
346
                            FindDrawingName = Path.GetFileNameWithoutExtension(findDrawingPath),
347
                            CurrentRepID = currentOPC.Representations.Nth[1].Id,
348
                            FindRepID = findRepresentation.Id
349
                        });
350

    
351
                        ReleaseCOMObjects(findDrawing);
352
                        ReleaseCOMObjects(findRepresentation);
353

    
354
                        //application.ActiveSelectSet.Add(application.ActiveDocument.ActiveSheet.DrawingObjects[currentOPC.Representations.Nth[1].get_GraphicOID().ToString()]);
355
                    }
356
                }
357

    
358
                foreach (var item in findOPCs)
359
                    ReleaseCOMObjects(item);
360
                ReleaseCOMObjects(currentOPC);
361
            }
362

    
363
            if (_OPC_Infos.Count > 0)
364
            {
365
                foreach (var item in _OPC_Infos)
366
                {
367
                    LMRepresentation removeRep = dataSource.GetRepresentation(item.CurrentRepID);
368
                    LMSymbol removeSymbol = dataSource.GetSymbol(item.CurrentRepID);
369
                    LMOPC removeOPC = dataSource.GetOPC(removeSymbol.ModelItemID);
370

    
371
                    
372

    
373
                    LMRepresentation findRep = dataSource.GetRepresentation(item.FindRepID);
374
                    LMOPC findOPC = dataSource.GetOPC(findRep.ModelItemID);
375
                    LMOPC pairOPC = findOPC.pairedWithOPCObject;
376

    
377
                    int mirror = removeSymbol.get_IsMirroredIndex();
378
                    double angle = Convert.ToDouble(removeSymbol.get_RotationAngle());
379
                    double x = removeSymbol.get_XCoordinate();
380
                    double y = removeSymbol.get_YCoordinate();
381
                    LMConnector connConnector = null;
382
                    
383
                    foreach (LMConnector connector in removeSymbol.Connect1Connectors)
384
                        if (connector.get_ItemStatus() == "Active")
385
                            connConnector = connector;
386
                    if (connConnector == null)
387
                        foreach (LMConnector connector in removeSymbol.Connect2Connectors)
388
                            if (connector.get_ItemStatus() == "Active")
389
                                connConnector = connector;
390
                    
391
                    if (connConnector != null)
392
                    {
393
                        ZoomObjectByGraphicOID(removeRep.get_GraphicOID().ToString());
394

    
395
                        placement.PIDRemovePlacement(removeRep);
396
                        removeRep.Commit();
397

    
398
                        LMSymbol newSymbol = placement.PIDPlaceSymbol(findRep.get_FileName(), x, y, mirror, angle, pairOPC.AsLMAItem(), connConnector);
399
                        newSymbol.Commit();
400

    
401
                        LMOPC newOPC = dataSource.GetOPC(newSymbol.ModelItemID);
402
                        object descValue = removeOPC.get_Description();
403
                        object toFromValue = removeOPC.get_ToFromText();
404
                        newOPC.set_Description(descValue);
405
                        newOPC.set_ToFromText(toFromValue);
406
                        newOPC.Commit();
407

    
408
                        ReleaseCOMObjects(newSymbol);
409
                        ReleaseCOMObjects(newOPC);
410
                    }
411

    
412
                    ReleaseCOMObjects(findOPC);
413
                    ReleaseCOMObjects(pairOPC);
414
                    ReleaseCOMObjects(findRep);
415
                    ReleaseCOMObjects(removeOPC);
416
                    ReleaseCOMObjects(removeSymbol);
417
                    ReleaseCOMObjects(removeRep);
418
                    ReleaseCOMObjects(connConnector);
419
                }
420

    
421
                application.ActiveDocument.Save();
422
            }
423

    
424

    
425
            foreach (var item in CurrentDrawing)
426
                ReleaseCOMObjects(item);
427
            foreach (var item in OtherDrawing)
428
                ReleaseCOMObjects(item);
429

    
430
            ReleaseCOMObjects(representations);
431

    
432
            ReleaseCOMObjects(filter2);
433
            ReleaseCOMObjects(criterion2);
434
            ReleaseCOMObjects(drawing);
435
            ReleaseCOMObjects(drawings);
436
            ReleaseCOMObjects(filter);
437
            ReleaseCOMObjects(criterion);
438

    
439
            ReleaseCOMObjects(dataSource);
440
            ReleaseCOMObjects(placement);
441

    
442
            return;
443

    
444
            //DataTable tOPCInfo = Project_DB.SelectOPCInfo();
445
            //DataTable tOPCRelations = Project_DB.SelectOPCRelations();
446
            //DataTable tDrawingInfo = Project_DB.SelectDrawingInfo();
447
            //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
448

    
449
            //foreach (DataRow row in tOPCInfo.Rows)
450
            //{
451
            //    if (!Convert.ToBoolean(row["PAIRED"]))
452
            //    {
453
            //        string drawingUID = row["ID2_DRAWING_UID"].ToString();
454
            //        string OPCUID = row["ID2_OPC_UID"].ToString();
455
            //        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));
456

    
457
            //        if (rows.Length == 2)
458
            //        {
459
            //            string fromDrawingsUID1 = rows[0]["From_Drawings_UID"].ToString();
460
            //            string fromDrawingsUID2 = rows[1]["From_Drawings_UID"].ToString();
461
            //            string fromOPCUID1 = rows[0]["From_OPC_UID"].ToString();
462
            //            string fromOPCUID2 = rows[1]["From_OPC_UID"].ToString();
463
            //            string toDrawingsUID1 = rows[0]["To_Drawings_UID"].ToString();
464
            //            string toDrawingsUID2 = rows[1]["To_Drawings_UID"].ToString();
465
            //            string toOPCUID1 = rows[0]["To_OPC_UID"].ToString();
466
            //            string toOPCUID2 = rows[1]["To_OPC_UID"].ToString();
467

    
468
            //            DataRow[] fromDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", fromDrawingsUID1));
469
            //            DataRow[] toDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", toDrawingsUID1));
470
            //            DataRow[] fromOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", fromOPCUID1));
471
            //            DataRow[] toOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", toOPCUID1));
472

    
473
            //            if (fromOPCUID1 == toOPCUID2 && fromOPCUID2 == toOPCUID1 && fromDrawing.Length == 1 && toDrawing.Length == 1 && fromOPCInfoRows.Length == 1 && toOPCInfoRows.Length == 1)
474
            //            {
475
            //                DataRow fromOPCInfoRow = fromOPCInfoRows[0];
476
            //                DataRow toOPCInfoRow = toOPCInfoRows[0];
477
            //                string fromOPCModelId = fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
478
            //                string toOPCModelId = toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
479
            //                string toDrawingName = toDrawing[0]["DRAWINGNAME"].ToString();
480
            //                List<string[]> toOPCAttributes = JsonConvert.DeserializeObject<List<string[]>>(toOPCInfoRow["ATTRIBUTES"].ToString());
481
            //                AutoModeling_OPC opc = new AutoModeling_OPC(dApplication, application, fromOPCModelId, toOPCModelId, toDrawingName, toOPCAttributes);
482
            //                if (opc.Run())
483
            //                {
484
            //                    fromOPCInfoRow["PAIRED"] = true;
485
            //                    toOPCInfoRow["PAIRED"] = true;
486

    
487
            //                    Project_DB.InsertOPCInfo(fromOPCInfoRow["ID2_OPC_UID"].ToString(), fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), fromOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
488
            //                    Project_DB.InsertOPCInfo(toOPCInfoRow["ID2_OPC_UID"].ToString(), toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), toOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
489
            //                }
490
            //            }
491
            //        }
492
            //    }
493
            //}
494

    
495
            //tOPCInfo.Dispose();
496
            //tOPCRelations.Dispose();
497
            //tDrawingInfo.Dispose();
498
            //ReleaseCOMObjects(dApplication);
499
            //dApplication = null;
500

    
501
            //MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
502
        }
503

    
504
        public void ReleaseCOMObjects(params object[] objVars)
505
        {
506
            int intNewRefCount = 0;
507
            foreach (object obj in objVars)
508
            {
509
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
510
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
511
            }
512
        }
513

    
514
        
515

    
516
        #region SPPID Utils
517

    
518
        #region Symmetry
519
        private void btnSymmetry_Click(object sender, EventArgs e)
520
        {
521
            if (application.ActiveSelectSet.Count == 1 && application.ActiveSelectSet[0].GetType() == typeof(Symbol2d))
522
            {
523
                int symCount = (int)spinEditSymmetry.Value;
524
                Symbol2d symbol = application.ActiveSelectSet[0] as Symbol2d;
525
                double x, y;
526
                symbol.GetOrigin(out x, out y);
527
                string rep = GetRepresentationId(symbol);
528
                List<string> verticalRepID = new List<string>();
529
                List<string> horizontalRepID = new List<string>();
530

    
531
                if ((symbol.LinearName.Contains("Piping") ||
532
                    symbol.LinearName.Contains("Instrument")) &&
533
                    !string.IsNullOrEmpty(rep))
534
                {
535
                    Placement placement = new Placement();
536
                    LMADataSource dataSource = placement.PIDDataSource;
537
                    List<List<string>> datas = SetSymbol(dataSource, rep, x, y, symCount);
538
                    Dictionary<SymmetryArrow, List<string>> resultDatas = new Dictionary<SymmetryArrow, List<string>>();
539
                    if (datas.Count >= 2 && datas.Find(loop => loop.Count != symCount) == null)
540
                    {
541
                        SymmetryArrow arrow = SymmetryArrow.None;
542
                        foreach (var data in datas)
543
                        {
544
                            LMSymbol firstSymbol = dataSource.GetSymbol(data[0]);
545
                            double fX = firstSymbol.get_XCoordinate();
546
                            double fY = firstSymbol.get_YCoordinate();
547

    
548
                            SlopeType type = SPPIDUtil.CalcSlope(x, y, fX, fY, 1);
549
                            if (type == SlopeType.HORIZONTAL)
550
                            {
551
                                if (fX < x)
552
                                {
553
                                    arrow |= SymmetryArrow.Left;
554
                                    resultDatas.Add(SymmetryArrow.Left, data);
555
                                }
556
                                else
557
                                {
558
                                    arrow |= SymmetryArrow.Right;
559
                                    resultDatas.Add(SymmetryArrow.Right, data);
560
                                }
561

    
562
                            }
563
                            else if (type == SlopeType.VERTICAL)
564
                            {
565
                                if (fY < y)
566
                                {
567
                                    arrow |= SymmetryArrow.Down;
568
                                    resultDatas.Add(SymmetryArrow.Down, data);
569
                                }
570
                                else
571
                                {
572
                                    arrow |= SymmetryArrow.Up;
573
                                    resultDatas.Add(SymmetryArrow.Up, data);
574
                                }
575
                            }
576

    
577
                            ReleaseCOMObjects(firstSymbol);
578
                        }
579

    
580
                        SymmetryForm form = new SymmetryForm(arrow);
581
                        if (form.ShowDialog() == DialogResult.OK)
582
                        {
583
                            MoveByResult(dataSource, resultDatas, x, y, form.Result, rep);
584
                        }
585
                    }
586
                    else
587
                        MessageBox.Show("Check Symmetry Rules", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
588
                    ReleaseCOMObjects(dataSource);
589
                    ReleaseCOMObjects(placement);
590
                }
591
            }
592

    
593
            return;
594

    
595
            List<Symbol2d> symbols = new List<Symbol2d>();
596
            foreach (var item in application.ActiveSelectSet)
597
            {
598
                Type type = item.GetType();
599
                if (type == typeof(Symbol2d))
600
                {
601
                    Symbol2d symbol = item as Symbol2d;
602
                    if (symbol.LinearName.Contains("Piping") ||
603
                        symbol.LinearName.Contains("Instrument"))
604
                        symbols.Add(symbol);
605
                }
606
                //if (item.GetType() == typeof(Symbol2d) || item.GetType() == typeof(Point2d))
607
                //    items.Add(item);
608
            }
609

    
610
            List<Symbol2d> verticalSymbols = new List<Symbol2d>();
611
            List<Symbol2d> horizontalSymbols = new List<Symbol2d>();
612
            Symbol2d mainSymbol = null;
613
            for (int i = 0; i < symbols.Count - 1; i++)
614
            {
615
                Symbol2d symbol1 = symbols[i];
616
                for (int j = 1; j < symbols.Count; j++)
617
                {
618
                    Symbol2d symbol2 = symbols[j];
619

    
620
                    if (symbol1 != symbol2)
621
                    {
622
                        double x1, y1, x2, y2;
623
                        symbol1.GetOrigin(out x1, out y1);
624
                        symbol2.GetOrigin(out x2, out y2);
625
                        SlopeType slopeType = SPPIDUtil.CalcSlope(x1, y1, x2, y2, 1);
626
                        if (slopeType == SlopeType.HORIZONTAL)
627
                        {
628
                            if (!horizontalSymbols.Contains(symbol1))
629
                                horizontalSymbols.Add(symbol1);
630
                            if (!horizontalSymbols.Contains(symbol2))
631
                                horizontalSymbols.Add(symbol2);
632

    
633
                            if (verticalSymbols.Contains(symbol1))
634
                                mainSymbol = symbol1;
635
                            if (verticalSymbols.Contains(symbol2))
636
                                mainSymbol = symbol2;
637
                        }
638
                        else if (slopeType == SlopeType.VERTICAL)
639
                        {
640
                            if (!verticalSymbols.Contains(symbol1))
641
                                verticalSymbols.Add(symbol1);
642
                            if (!verticalSymbols.Contains(symbol2))
643
                                verticalSymbols.Add(symbol2);
644

    
645
                            if (horizontalSymbols.Contains(symbol1))
646
                                mainSymbol = symbol1;
647
                            if (horizontalSymbols.Contains(symbol2))
648
                                mainSymbol = symbol2;
649
                        }
650
                    }
651
                }
652
            }
653

    
654
            application.ActiveSelectSet.RemoveAll();
655
            foreach (var item in verticalSymbols)
656
                application.ActiveSelectSet.Add(item);
657
            foreach (var item in horizontalSymbols)
658
                application.ActiveSelectSet.Add(item);
659
                
660

    
661
            if (MessageBox.Show("Continue?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
662
            {
663
                application.ActiveSelectSet.RemoveAll();
664

    
665
            }
666
        }
667
        private void MoveByResult(LMADataSource dataSource,Dictionary<SymmetryArrow, List<string>> resultDatas, double x, double y, SymmetryArrow arrow, string centerRepID)
668
        {
669
            List<string> datas = resultDatas[arrow];
670
            foreach (var item in resultDatas)
671
            {
672
                if (item.Key != arrow)
673
                {
674
                    for (int i = 0; i < item.Value.Count; i++)
675
                    {
676
                        Symbol2d moveSymbol2d = GetSymbol2DByRepID(dataSource, item.Value[i]);
677
                        Symbol2d symbol2d = GetSymbol2DByRepID(dataSource, datas[i]);
678

    
679
                        double x1, y1, x2, y2;
680
                        symbol2d.GetOrigin(out x1, out y1);
681
                        moveSymbol2d.GetOrigin(out x2, out y2);
682
                        double distance = SPPIDUtil.CalcPointToPointdDistance(x, y, x1, y1);
683

    
684
                        string symbol1RepID;
685
                        string symbol2RepID;
686
                        List<Point2d> point2ds;
687
                        if (i == 0)
688
                        {
689
                            symbol1RepID = centerRepID;
690
                            symbol2RepID = item.Value[i];
691
                        }
692
                        else
693
                        {
694
                            symbol1RepID = item.Value[i - 1];
695
                            symbol2RepID = item.Value[i];
696
                        }
697
                        point2ds = FindAllPoint2d(dataSource, symbol1RepID, symbol2RepID);
698
                        double moveX = 0;
699
                        double moveY = 0;
700

    
701
                        if (item.Key == SymmetryArrow.Left)
702
                            moveX = x - distance - x2;
703
                        else if (item.Key == SymmetryArrow.Right)
704
                            moveX = x + distance - x2;
705
                        else if (item.Key == SymmetryArrow.Down)
706
                            moveY = y - distance - y2;
707
                        else if (item.Key == SymmetryArrow.Up)
708
                            moveY = y + distance - y2;
709

    
710
                        moveSymbol2d.Move(0, 0, moveX, moveY);
711
                        MovePoint2d(dataSource, item.Value[i], arrow, moveX, moveY);
712
                        foreach (var point in point2ds)
713
                        {
714
                            LMSymbol branch = dataSource.GetSymbol(GetRepresentationId(point));
715
                            LMSymbol connSymbol = GetFirstSymbolBySlope(dataSource, branch, symbol1RepID, symbol2RepID);
716
                            point.Move(0, 0, moveX, moveY);
717
                            if (connSymbol != null)
718
                            {
719
                                Symbol2d connSymbol2d = GetSymbol2DByRepID(dataSource, connSymbol.AsLMRepresentation().Id);
720
                                connSymbol2d.Move(0, 0, moveX, moveY);
721
                                ReleaseCOMObjects(connSymbol);
722
                            }
723
                            ReleaseCOMObjects(branch);
724
                        }
725
                            
726
                    }
727
                }
728
            }
729
        }
730
        private LMSymbol GetFirstSymbolBySlope(LMADataSource dataSource, LMSymbol branch, string symbol1RepID, string symbol2RepID)
731
        {
732
            LMSymbol result = null;
733
            foreach (LMConnector connector in branch.Connect1Connectors)
734
            {
735
                if (connector.ConnectItem1SymbolObject != null && 
736
                    connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
737
                    connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
738
                {
739
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
740
                    if (repID != symbol1RepID && repID != symbol2RepID)
741
                        result = connector.ConnectItem1SymbolObject;
742
                }
743

    
744
                if (connector.ConnectItem2SymbolObject != null &&
745
                    connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
746
                    connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
747
                {
748
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
749
                    if (repID != symbol1RepID && repID != symbol2RepID)
750
                        result = connector.ConnectItem2SymbolObject;
751
                }
752
            }
753

    
754
            foreach (LMConnector connector in branch.Connect2Connectors)
755
            {
756
                if (connector.ConnectItem1SymbolObject != null &&
757
                    connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
758
                    connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
759
                {
760
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
761
                    if (repID != symbol1RepID && repID != symbol2RepID)
762
                        result = connector.ConnectItem1SymbolObject;
763
                }
764

    
765
                if (connector.ConnectItem2SymbolObject != null &&
766
                    connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
767
                    connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
768
                {
769
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
770
                    if (repID != symbol1RepID && repID != symbol2RepID)
771
                        result = connector.ConnectItem2SymbolObject;
772
                }
773
            }
774

    
775

    
776
            return result;
777
        }
778
        private string GetRepresentationId(Symbol2d symbol)
779
        {
780
            foreach (var attributes in symbol.AttributeSets)
781
            {
782
                foreach (var attribute in attributes)
783
                {
784
                    string name = attribute.Name;
785
                    object value = attribute.GetValue();
786
                    if (name == "DrawingID")
787
                    {
788
                        return value.ToString();  
789
                    }
790
                }
791
            }
792
            return null;
793
        }
794
        private string GetRepresentationId(Point2d point2d)
795
        {
796
            foreach (var attributes in point2d.AttributeSets)
797
            {
798
                foreach (var attribute in attributes)
799
                {
800
                    string name = attribute.Name;
801
                    object value = attribute.GetValue();
802
                    if (name == "DrawingID")
803
                    {
804
                        return value.ToString();
805
                    }
806
                }
807
            }
808
            return null;
809
        }
810
        private List<List<string>> SetSymbol(LMADataSource dataSource, string rep, double x, double y, int count)
811
        {
812
            LMSymbol _LMSymbol = dataSource.GetSymbol(rep);
813
            List<List<string>> result = new List<List<string>>();
814
            List<string> oldIDs = new List<string>() { rep };
815
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
816
            {
817
                if (connector.get_ItemStatus() != "Active")
818
                    continue;
819

    
820
                string repID = connector.AsLMRepresentation().Id;
821
                string status = connector.get_ItemStatus();
822
                if (status == "Active" && !oldIDs.Contains(repID))
823
                {
824
                    List<string> symbols = new List<string>();
825
                    oldIDs.Add(repID);
826
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
827
                    result.Add(symbols);
828
                }
829
            }
830

    
831
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
832
            {
833
                if (connector.get_ItemStatus() != "Active")
834
                    continue;
835

    
836
                string repID = connector.AsLMRepresentation().Id;
837
                string status = connector.get_ItemStatus();
838
                if (status == "Active" && !oldIDs.Contains(repID))
839
                {
840
                    List<string> symbols = new List<string>();
841
                    oldIDs.Add(repID);
842
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
843
                    result.Add(symbols);
844
                }
845
            }
846

    
847
            ReleaseCOMObjects(_LMSymbol);
848
            return result;
849
        }
850
        private void loop(LMADataSource dataSource, List<string> oldIDs, List<string> symbols, LMConnector connector, double x, double y, int count)
851
        {
852
            if (symbols.Count >= count)
853
                return;
854

    
855
            if (connector.ConnectItem1SymbolObject != null && !oldIDs.Contains(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id))
856
            {
857
                string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
858
                oldIDs.Add(repID);
859
                if (connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch" && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
860
                {
861
                    double sX = connector.ConnectItem1SymbolObject.get_XCoordinate(), sY = connector.ConnectItem1SymbolObject.get_YCoordinate(); ;
862
                    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, sX, sY, 1);
863
                    if (slopeType == SlopeType.HORIZONTAL || slopeType == SlopeType.VERTICAL)
864
                        symbols.Add(repID);
865
                }
866

    
867
                loop(dataSource, oldIDs, symbols, connector.ConnectItem1SymbolObject, x, y, count);
868
            }
869

    
870
            if (symbols.Count >= count)
871
                return;
872

    
873
            if (connector.ConnectItem2SymbolObject != null && !oldIDs.Contains(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id))
874
            {
875
                string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
876
                oldIDs.Add(repID);
877
                if (connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch" && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
878
                {
879
                    double sX = connector.ConnectItem2SymbolObject.get_XCoordinate(), sY = connector.ConnectItem2SymbolObject.get_YCoordinate(); ;
880
                    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, sX, sY, 1);
881
                    if (slopeType == SlopeType.HORIZONTAL || slopeType == SlopeType.VERTICAL)
882
                        symbols.Add(repID);
883
                }
884

    
885
                loop(dataSource, oldIDs, symbols, connector.ConnectItem2SymbolObject, x, y, count);
886
            }
887
        }
888
        private void loop(LMADataSource dataSource, List<string> oldIDs, List<string> symbols, LMSymbol _LMSymbol, double x, double y, int count)
889
        {
890
            if (symbols.Count >= count)
891
                return;
892

    
893
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
894
            {
895
                if (connector.get_ItemStatus() != "Active")
896
                    continue;
897

    
898
                string repID = connector.AsLMRepresentation().Id;
899
                string status = connector.get_ItemStatus();
900
                if (status == "Active" && !oldIDs.Contains(repID))
901
                {
902
                    oldIDs.Add(repID);
903
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
904
                }
905
            }
906

    
907
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
908
            {
909
                if (connector.get_ItemStatus() != "Active")
910
                    continue;
911

    
912
                string repID = connector.AsLMRepresentation().Id;
913
                string status = connector.get_ItemStatus();
914
                if (status == "Active" && !oldIDs.Contains(repID))
915
                {
916
                    oldIDs.Add(repID);
917
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
918
                }
919
            }
920
        }
921
        private Symbol2d GetSymbol2DByRepID(LMADataSource dataSource, string repID)
922
        {
923
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
924
            Symbol2d symbol2D = application.ActiveDocument.ActiveSheet.DrawingObjects[_LMSymbol.get_GraphicOID().ToString()];
925
            ReleaseCOMObjects(_LMSymbol);
926
            return symbol2D;
927
        }
928
        private void MovePoint2d(LMADataSource datasource, string repID, SymmetryArrow arrow, double moveX, double moveY)
929
        {
930
            LMSymbol _LMSymbol = datasource.GetSymbol(repID);
931
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
932
            {
933
                if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()))
934
                {
935
                    if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
936
                    {
937
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()];
938
                        point.X += moveX;
939
                        point.Y += moveY;
940
                    }
941
                    else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
942
                    {
943
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()];
944
                        point.X += moveX;
945
                        point.Y += moveY;
946
                    }
947
                }
948
            }
949
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
950
            {
951
                if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()))
952
                {
953
                    if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
954
                    {
955
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()];
956
                        point.X += moveX;
957
                        point.Y += moveY;
958
                    }
959
                    else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
960
                    {
961
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()];
962
                        point.X += moveX;
963
                        point.Y += moveY;
964
                    }
965
                }
966
            }
967
            ReleaseCOMObjects(_LMSymbol);
968
        }
969
        private List<Point2d> FindAllPoint2d(LMADataSource dataSource, string repID, string nextRepID)
970
        {
971
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
972
            List<string> endIDs = new List<string>() { repID };
973
            List<string> graphicOIDs = new List<string>();
974
            List<Point2d> result = new List<Point2d>();
975
            FindPointsLoop(dataSource, _LMSymbol, endIDs, nextRepID, graphicOIDs);
976
            ReleaseCOMObjects(_LMSymbol);
977
            foreach (var item in graphicOIDs)
978
            {
979
                Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[item] as Point2d;
980
                result.Add(point);
981
            }
982
            
983
            return result;
984
        }
985
        private bool FindPointsLoop(LMADataSource dataSource, LMSymbol _LMSymbol, List<string> endIDs, string targetRepID, List<string> graphicOIDs)
986
        {
987
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
988
            {
989
                if (connector.get_ItemStatus() != "Active")
990
                    continue;
991

    
992
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
993
                {
994
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
995
                    if (!endIDs.Contains(repID))
996
                    {
997
                        endIDs.Add(repID);
998
                        if (connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
999
                        {
1000
                            if (FindPointsLoop(dataSource, connector.ConnectItem1SymbolObject, endIDs, targetRepID, graphicOIDs))
1001
                            {
1002
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1003
                                    graphicOIDs.Add(connector.ConnectItem1SymbolObject.get_GraphicOID().ToString());
1004
                                return true;
1005
                            }
1006
                        }
1007
                        else if (targetRepID == repID)
1008
                        {
1009
                            return true;
1010
                        }
1011
                    }
1012
                }
1013
                if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
1014
                {
1015
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
1016
                    if (!endIDs.Contains(repID))
1017
                    {
1018
                        endIDs.Add(repID);
1019
                        if (connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
1020
                        {
1021
                            if (FindPointsLoop(dataSource, connector.ConnectItem2SymbolObject, endIDs, targetRepID, graphicOIDs))
1022
                            {
1023
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1024
                                    graphicOIDs.Add(connector.ConnectItem2SymbolObject.get_GraphicOID().ToString());
1025
                                return true;
1026
                            }
1027
                        }
1028
                        else if (targetRepID == repID)
1029
                        {
1030
                            return true;
1031
                        }
1032
                    }
1033
                }
1034
            }
1035
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
1036
            {
1037
                if (connector.get_ItemStatus() != "Active")
1038
                    continue;
1039

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

    
1084
            return false;
1085
        }
1086
        #endregion
1087

    
1088
        #region SpecBreak
1089
        private void btnSpecBreakRelocation_Click(object sender, EventArgs e)
1090
        {
1091
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
1092
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
1093
            application = wApp.RADApplication;
1094

    
1095
            int count = application.ActiveSelectSet.Count;
1096
            int dependencyCount = 0;
1097
            foreach (var item in application.ActiveSelectSet)
1098
                if (item.GetType() == typeof(DependencyObject))
1099
                    dependencyCount++;
1100

    
1101
            if (count > 0 && application.ActiveSelectSet[0].GetType() == typeof(DependencyObject))
1102
            {
1103
                MessageBox.Show("First selected item is DependencyObject!\r\nPlease move symbol", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1104
                return;
1105
            }
1106

    
1107
            if ((count == 3 || count == 4) && application.ActiveSelectSet[0].GetType() == typeof(Symbol2d) &&
1108
                (dependencyCount == 2 || dependencyCount == 3))
1109
            {
1110
                Symbol2d symbol = application.ActiveSelectSet[0] as Symbol2d;
1111

    
1112
                if (!symbol.DefinitionName.Contains(@"Design\Annotation\Graphics\"))
1113
                {
1114
                    MessageBox.Show("Select SpecBreak!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1115
                    return;
1116
                }
1117

    
1118
                DependencyObject dependency1 = application.ActiveSelectSet[1] as DependencyObject;
1119
                DependencyObject dependency2 = application.ActiveSelectSet[2] as DependencyObject;
1120
                DependencyObject dependency3 = null;
1121
                if (count == 4)
1122
                    dependency3 = application.ActiveSelectSet[3] as DependencyObject;
1123

    
1124
                application.ActiveSelectSet.RemoveAll();
1125
                double angle = symbol.Angle;
1126
                if (symbol.GetTransform().HasReflection)
1127
                    angle += Math.PI;
1128

    
1129
                double degree = double.NaN;
1130
                if (angle > -0.1 && angle < 0.1)
1131
                    degree = 0;
1132
                else if (angle > 1.56 && angle < 1.58)
1133
                    degree = 90;
1134
                else if (angle > 3.13 && angle < 3.15)
1135
                    degree = 180;
1136
                else if (angle > 4.7 && angle < 4.72)
1137
                    degree = 270;
1138
                else if (angle > 6.27 && angle < 6.29)
1139
                    degree = 0;
1140
                else if (angle > -1.58 && angle < -1.56)
1141
                    degree = 270;
1142
                else if (angle > -3.15 && angle < -3.13)
1143
                    degree = 180;
1144
                else
1145
                    throw new Exception("Check Angle");
1146

    
1147

    
1148
                double originX, originY;
1149
                symbol.GetOrigin(out originX, out originY);
1150

    
1151
                double crossX = 0;
1152
                double crossY = 0;
1153
                double topX = 0;
1154
                double topY = 0;
1155
                foreach (var item in symbol.DrawingObjects)
1156
                {
1157
                    Line2d line2d = item as Line2d;
1158
                    if (line2d != null)
1159
                    {
1160
                        double x1, y1, x2, y2;
1161
                        line2d.GetStartPoint(out x1, out y1);
1162
                        line2d.GetEndPoint(out x2, out y2);
1163
                        SlopeType slopeType = SPPIDUtil.CalcSlope(x1, y1, x2, y2, 0.1);
1164

    
1165
                        if (slopeType == SlopeType.HORIZONTAL)
1166
                        {
1167
                            if (crossY == 0)
1168
                                crossY = y1;
1169
                            else
1170
                                crossY = (crossY + y1) / 2;
1171

    
1172
                            switch (degree)
1173
                            {
1174
                                case 90:
1175
                                    if (topX == 0)
1176
                                        topX = Math.Min(x1, x2);
1177
                                    else
1178
                                        topX = Math.Min(topX, Math.Min(x1, x2));
1179
                                    break;
1180
                                case 270:
1181
                                    if (topX == 0)
1182
                                        topX = Math.Max(x1, x2);
1183
                                    else
1184
                                        topX = Math.Max(topX, Math.Max(x1, x2));
1185
                                    break;
1186
                                default:
1187
                                    break;
1188
                            }
1189
                        }
1190
                        else if (slopeType == SlopeType.VERTICAL)
1191
                        {
1192
                            if (crossX == 0)
1193
                                crossX = x1;
1194
                            else
1195
                                crossX = (crossX + x1) / 2;
1196

    
1197
                            switch (degree)
1198
                            {
1199
                                case 0:
1200
                                    if (topY == 0)
1201
                                        topY = Math.Max(y1, y2);
1202
                                    else
1203
                                        topY = Math.Max(topY, Math.Max(y1, y2));
1204
                                    break;
1205
                                case 180:
1206
                                    if (topY == 0)
1207
                                        topY = Math.Min(y1, y2);
1208
                                    else
1209
                                        topY = Math.Min(topY, Math.Min(y1, y2));
1210
                                    break;
1211
                                default:
1212
                                    break;
1213
                            }
1214
                        }
1215
                    }
1216
                }
1217
                switch (degree)
1218
                {
1219
                    case 0:
1220
                        crossX = originX;
1221
                        topX = crossX;
1222
                        break;
1223
                    case 90:
1224
                        crossY = originY;
1225
                        topY = crossY;
1226
                        break;
1227
                    case 180:
1228
                        crossX = originX;
1229
                        topX = crossX;
1230
                        break;
1231
                    case 270:
1232
                        crossY = originY;
1233
                        topY = crossY;
1234
                        break;
1235
                    default:
1236
                        break;
1237
                }
1238

    
1239
                SpecBreakRelocation(degree, originX, originY, crossX, crossY, topX, topY, dependency1, dependency2, dependency3);
1240
                SetSpecBreakParameters(symbol, dependency1, dependency2, degree);
1241
            }
1242
            else
1243
            {
1244
                MessageBox.Show("Check Rule!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1245
            }
1246
        }
1247
        private void SetSpecBreakParameters(Symbol2d symbol, DependencyObject dependency1, DependencyObject dependency2, double degree)
1248
        {
1249
            bool mirror = symbol.GetTransform().HasReflection;
1250
            string repID = GetRepresentationId(symbol);
1251
            Placement placement = new Placement();
1252
            LMADataSource dataSource = placement.PIDDataSource;
1253

    
1254
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
1255
            if (_LMSymbol != null)
1256
            {
1257
                double sX1, sY1, sX2, sY2, sOriginX, sOriginY;
1258
                symbol.Range(out sX1, out sY1, out sX2, out sY2);
1259
                symbol.GetOrigin(out sOriginX, out sOriginY);
1260
                double d1X1, d1Y1, d1X2, d1Y2, d2X1, d2Y1, d2X2, d2Y2;
1261
                FindRangeWithOutLineString2d(dependency1, out d1X1, out d1Y1, out d1X2, out d1Y2);
1262
                FindRangeWithOutLineString2d(dependency2, out d2X1, out d2Y1, out d2X2, out d2Y2);
1263

    
1264
                double left = 0, right = 0;
1265
                switch (degree)
1266
                {
1267
                    case 0:
1268
                        if (!mirror)
1269
                        {
1270
                            left = Math.Abs(d1X1 - sOriginX);
1271
                            right = Math.Abs(d2X2 - sOriginX);
1272
                        }
1273
                        else
1274
                        {
1275
                            right = Math.Abs(d1X1 - sOriginX);
1276
                            left = Math.Abs(d2X2 - sOriginX);
1277
                        }
1278
                        break;
1279
                    case 90:
1280
                        if (!mirror)
1281
                        {
1282
                            left = Math.Abs(d1Y1 - sOriginY);
1283
                            right = Math.Abs(d2Y2 - sOriginY);
1284
                        }
1285
                        else
1286
                        {
1287
                            right = Math.Abs(d1Y1 - sOriginY);
1288
                            left = Math.Abs(d2Y2 - sOriginY);
1289
                        }
1290
                        break;
1291
                    case 180:
1292
                        if (!mirror)
1293
                        {
1294
                            right = Math.Abs(d1X1 - sOriginX);
1295
                            left = Math.Abs(d2X2 - sOriginX);
1296
                        }
1297
                        else
1298
                        {
1299
                            left = Math.Abs(d1X1 - sOriginX);
1300
                            right = Math.Abs(d2X2 - sOriginX);
1301
                            
1302
                        }
1303
                        break;
1304
                    case 270:
1305
                        if (!mirror)
1306
                        {
1307
                            right = Math.Abs(d1Y1 - sOriginY);
1308
                            left = Math.Abs(d2Y2 - sOriginY);
1309
                        }
1310
                        else
1311
                        {
1312
                            left = Math.Abs(d1Y1 - sOriginY);
1313
                            right = Math.Abs(d2Y2 - sOriginY);
1314
                        }
1315
                        break;
1316
                    default:
1317
                        break;
1318
                }
1319

    
1320
                string[] array1 = new string[] { "", "Left", "Right" };
1321
                string[] array2 = new string[] { "", left.ToString(), right.ToString() };
1322
                placement.PIDApplyParameters(_LMSymbol.AsLMRepresentation(), array1, array2);
1323
            }
1324

    
1325
            ReleaseCOMObjects(_LMSymbol);
1326
            ReleaseCOMObjects(dataSource);
1327
            ReleaseCOMObjects(placement);
1328
        }
1329
        private void SpecBreakRelocation(double degree, double originX, double originY, double crossX, double crossY, double topX, double topY, DependencyObject dependency1, DependencyObject dependency2, DependencyObject dependency3)
1330
        {
1331
            double d1X1, d1Y1, d1X2, d1Y2, d2X1, d2Y1, d2X2, d2Y2, d3X1 = 0, d3Y1 = 0, d3X2 = 0, d3Y2 = 0;
1332
            
1333
            FindRangeWithOutLineString2d(dependency1, out d1X1, out d1Y1, out d1X2, out d1Y2);
1334
            FindRangeWithOutLineString2d(dependency2, out d2X1, out d2Y1, out d2X2, out d2Y2);
1335
            if (dependency3 != null)
1336
                FindRangeWithOutLineString2dAndTextBox(dependency3, out d3X1, out d3Y1, out d3X2, out d3Y2);
1337

    
1338
            GridSetting gridSetting = GridSetting.GetInstance();
1339
            double move = gridSetting.Length / 2;
1340
            switch (degree)
1341
            {
1342
                case 0:
1343
                    MoveDependency(dependency1, d1X2, d1Y2, crossX - move, crossY);
1344
                    MoveDependency(dependency2, d2X1, d2Y2, crossX + move, crossY);
1345
                    if (dependency3 != null)
1346
                        MoveDependency(dependency3, (d3X1 + d3X2) / 2, d3Y1, topX, topY);
1347
                    break;
1348
                case 90:
1349
                    MoveDependency(dependency1, d1X1, d1Y2, crossX + move, crossY);
1350
                    MoveDependency(dependency2, d2X1, d2Y1, crossX + move, crossY);
1351
                    if (dependency3 != null)
1352
                        MoveDependency(dependency3, d3X1, (d3Y1 + d3Y2) / 2, originX, originY);
1353
                    break;
1354
                case 180:
1355
                    MoveDependency(dependency1, d1X2, d1Y1, crossX - move, crossY);
1356
                    MoveDependency(dependency2, d2X1, d2Y1, crossX + move, crossY);
1357
                    if (dependency3 != null)
1358
                        MoveDependency(dependency3, (d3X1 + d3X2) / 2, d3Y2, topX, topY);
1359
                    break;
1360
                case 270:
1361
                    MoveDependency(dependency1, d1X2, d1Y2, crossX - move, crossY);
1362
                    MoveDependency(dependency2, d2X2, d2Y1, crossX - move, crossY);
1363
                    if (dependency3 != null)
1364
                        MoveDependency(dependency3, d3X2, (d3Y1 + d3Y2) / 2, originX, originY);
1365
                    break;
1366
                default:
1367
                    break;
1368
            }
1369
        }
1370
        private void MoveDependency(DependencyObject dependency, double xFrom, double yFrom, double xTo, double yTo)
1371
        {
1372
            application.ActiveSelectSet.Add(dependency);
1373

    
1374
            Transform transform = dependency.GetTransform();
1375
            transform.DefineByMove2d(xTo - xFrom, yTo - yFrom);
1376
            application.ActiveSelectSet.Transform(transform, false);
1377

    
1378
            application.ActiveSelectSet.RemoveAll();
1379
        }
1380
        private string GetDrawingItemType(DependencyObject dependency)
1381
        {
1382
            string result = string.Empty;
1383

    
1384
            foreach (var attributes in dependency.AttributeSets)
1385
            {
1386
                foreach (var attribute in attributes)
1387
                {
1388
                    if (attribute.Name == "DrawingItemType")
1389
                        return attribute.GetValue().ToString();
1390
                }
1391
            }
1392

    
1393
            return result;
1394
        }
1395
        private void FindRangeWithOutLineString2d(DependencyObject dependency, out double x1, out double y1, out double x2, out double y2)
1396
        {
1397
            x1 = double.MaxValue;
1398
            y1 = double.MaxValue;
1399
            x2 = double.MinValue;
1400
            y2 = double.MinValue;
1401
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1402
            {
1403
                if (item.GetType() != typeof(LineString2d))
1404
                {
1405
                    double minX, minY, maxX, maxY;
1406
                    item.Range(out minX, out minY, out maxX, out maxY);
1407
                    if (x1 > minX)
1408
                        x1 = minX;
1409
                    if (y1 > minY)
1410
                        y1 = minY;
1411
                    if (x2 < maxX)
1412
                        x2 = maxX;
1413
                    if (y2 < maxY)
1414
                        y2 = maxY;
1415
                }
1416
            }
1417

    
1418
        }
1419
        private void FindWidthHeightWidthOutLineString2d(DependencyObject dependency, out double width, out double height)
1420
        {
1421
            width = 0;
1422
            height = 0;
1423

    
1424
            double x1 = double.MaxValue;
1425
            double y1 = double.MaxValue;
1426
            double x2 = double.MinValue;
1427
            double y2 = double.MinValue;
1428
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1429
            {
1430
                if (item.GetType() != typeof(LineString2d))
1431
                {
1432
                    double minX, minY, maxX, maxY;
1433
                    item.Range(out minX, out minY, out maxX, out maxY);
1434
                    if (x1 > minX)
1435
                        x1 = minX;
1436
                    if (y1 > minY)
1437
                        y1 = minY;
1438
                    if (x2 < maxX)
1439
                        x2 = maxX;
1440
                    if (y2 < maxY)
1441
                        y2 = maxY;
1442
                }
1443
            }
1444

    
1445
            width = x2 - x1;
1446
            height = y2 - y1;
1447
        }
1448
        private void FindRangeWithOutLineString2dAndTextBox(DependencyObject dependency, out double x1, out double y1, out double x2, out double y2)
1449
        {
1450
            x1 = double.MaxValue;
1451
            y1 = double.MaxValue;
1452
            x2 = double.MinValue;
1453
            y2 = double.MinValue;
1454
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1455
            {
1456
                if (item.GetType() != typeof(LineString2d) && item.GetType() != typeof(Ingr.RAD2D.TextBox))
1457
                {
1458
                    double minX, minY, maxX, maxY;
1459
                    item.Range(out minX, out minY, out maxX, out maxY);
1460
                    if (x1 > minX)
1461
                        x1 = minX;
1462
                    if (y1 > minY)
1463
                        y1 = minY;
1464
                    if (x2 < maxX)
1465
                        x2 = maxX;
1466
                    if (y2 < maxY)
1467
                        y2 = maxY;
1468
                }
1469
            }
1470

    
1471
        }
1472
        #endregion
1473

    
1474
        #region Hot Key
1475
        private void toggleSwitchSnapGrid_Toggled(object sender, EventArgs e)
1476
        {
1477
            if (toggleSwitchSnapGrid.IsOn)
1478
            {
1479
                RegisterHotKey(this.Handle, 0, (int)KeyModifier.Shift, Keys.A.GetHashCode());
1480
            }
1481
            else
1482
            {
1483
                UnregisterHotKey(this.Handle, 0);
1484
            }
1485
        }
1486
        private void toggleSwitchMoveSymbol_Toggled(object sender, EventArgs e)
1487
        {
1488
            if (toggleSwitchMoveSymbol.IsOn)
1489
            {
1490
                RegisterHotKey(this.Handle, 1, (int)KeyModifier.Shift, Keys.Left.GetHashCode());
1491
                RegisterHotKey(this.Handle, 2, (int)KeyModifier.Shift, Keys.Up.GetHashCode());
1492
                RegisterHotKey(this.Handle, 3, (int)KeyModifier.Shift, Keys.Right.GetHashCode());
1493
                RegisterHotKey(this.Handle, 4, (int)KeyModifier.Shift, Keys.Down.GetHashCode());
1494
            }
1495
            else
1496
            {
1497
                UnregisterHotKey(this.Handle, 1);
1498
                UnregisterHotKey(this.Handle, 2);
1499
                UnregisterHotKey(this.Handle, 3);
1500
                UnregisterHotKey(this.Handle, 4);
1501
            }
1502
        }
1503
        public void ClearHotKey()
1504
        {
1505
            if (toggleSwitchMoveSymbol.IsOn)
1506
            {
1507
                UnregisterHotKey(this.Handle, 1);
1508
                UnregisterHotKey(this.Handle, 2);
1509
                UnregisterHotKey(this.Handle, 3);
1510
                UnregisterHotKey(this.Handle, 4);
1511
            }
1512
            if (toggleSwitchSnapGrid.IsOn)
1513
            {
1514
                UnregisterHotKey(this.Handle, 0);
1515
            }
1516
        }
1517
        [System.Runtime.InteropServices.DllImport("user32.dll")]
1518
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
1519
        [System.Runtime.InteropServices.DllImport("user32.dll")]
1520
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
1521
        enum KeyModifier
1522
        {
1523
            None = 0,
1524
            Alt = 1,
1525
            Control = 2,
1526
            Shift = 4,
1527
            WinKey = 8
1528
        }
1529
        protected override void WndProc(ref Message m)
1530
        {
1531
            base.WndProc(ref m);
1532
            if (m.Msg == 0x0312)
1533
            {
1534
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
1535
                KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF);
1536
                int id = m.WParam.ToInt32();
1537
                switch (id)
1538
                {
1539
                    case 0:
1540
                        application.RunCommand(CommandConstants.igcmdGridSnap);
1541
                        break;
1542
                    case 1:
1543
                        MoveSymbol(Arrow.Left);
1544
                        break;
1545
                    case 2:
1546
                        MoveSymbol(Arrow.Up);
1547
                        break;
1548
                    case 3:
1549
                        MoveSymbol(Arrow.Right);
1550
                        break;
1551
                    case 4:
1552
                        MoveSymbol(Arrow.Down);
1553
                        break;
1554
                    default:
1555
                        break;
1556
                }
1557
                
1558
            }
1559

    
1560
        }
1561
        #endregion
1562

    
1563
        #region Move Symbol
1564
        enum Arrow
1565
        {
1566
            Left,
1567
            Up,
1568
            Right,
1569
            Down
1570
        }
1571
        private void MoveSymbol(Arrow arrow)
1572
        {
1573
            if (application.ActiveSelectSet.Count > 0)
1574
            {
1575
                Placement placement = new Placement();
1576
                LMADataSource dataSource = placement.PIDDataSource;
1577
                System.Collections.ObjectModel.Collection<DrawingObjectBase> originalDrawingObjectBases = new System.Collections.ObjectModel.Collection<DrawingObjectBase>();
1578
                foreach (var item in application.ActiveSelectSet)
1579
                    originalDrawingObjectBases.Add(item);
1580
                System.Collections.ObjectModel.Collection<DrawingObjectBase> drawingObjectBases = new System.Collections.ObjectModel.Collection<DrawingObjectBase>();
1581
                Transform transform = null;
1582
                foreach (DrawingObjectBase drawingObject in application.ActiveSelectSet)
1583
                {
1584
                    if (drawingObject.GetType() == typeof(Symbol2d))
1585
                    {
1586
                        Symbol2d symbol2D = drawingObject as Symbol2d;
1587
                        if (transform == null)
1588
                            transform = symbol2D.GetTransform();
1589
                        drawingObjectBases.Add(symbol2D);
1590
                        LMSymbol _LMSymbol = dataSource.GetSymbol(GetRepresentationId(symbol2D));
1591
                        if (_LMSymbol != null)
1592
                        {
1593
                            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
1594
                            {
1595
                                if (connector.get_ItemStatus() == "Active")
1596
                                {
1597
                                    #region Zero Length And Branch
1598
                                    if (Convert.ToBoolean(connector.get_IsZeroLength()))
1599
                                    {
1600
                                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolID != _LMSymbol.Id)
1601
                                        {
1602
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1603
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1604
                                                drawingObjectBases.Add(point2D);
1605
                                        }
1606
                                        else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolID != _LMSymbol.Id)
1607
                                        {
1608
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1609
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1610
                                                drawingObjectBases.Add(point2D);
1611
                                        }
1612
                                    }
1613
                                    #endregion
1614
                                    #region Not Zero Length And Branch And Vertical,Horizontal
1615
                                    else
1616
                                    {
1617
                                        if (connector.ConnectItem1SymbolObject != null && 
1618
                                            connector.ConnectItem1SymbolID != _LMSymbol.Id &&
1619
                                            connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
1620
                                            connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch" &&
1621
                                            IsMovePoint2D(connector.ConnectItem1SymbolObject, connector, arrow))
1622
                                        {
1623
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1624
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1625
                                                drawingObjectBases.Add(point2D);
1626
                                        }
1627
                                            
1628
                                        else if (connector.ConnectItem2SymbolObject != null && 
1629
                                            connector.ConnectItem2SymbolID != _LMSymbol.Id &&
1630
                                            connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
1631
                                            connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch" &&
1632
                                            IsMovePoint2D(connector.ConnectItem2SymbolObject, connector, arrow))
1633
                                        {
1634
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1635
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1636
                                                drawingObjectBases.Add(point2D);
1637
                                        }
1638
                                            
1639
                                    }
1640
                                    #endregion
1641
                                }
1642
                            }
1643
                            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
1644
                            {
1645
                                if (connector.get_ItemStatus() == "Active")
1646
                                {
1647
                                    #region Zero Length And Branch
1648
                                    if (Convert.ToBoolean(connector.get_IsZeroLength()))
1649
                                    {
1650
                                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolID != _LMSymbol.Id)
1651
                                        {
1652
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1653
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1654
                                                drawingObjectBases.Add(point2D);
1655
                                        }
1656
                                        else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolID != _LMSymbol.Id)
1657
                                        {
1658
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1659
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1660
                                                drawingObjectBases.Add(point2D);
1661
                                        }
1662
                                    }
1663
                                    #endregion
1664
                                    #region Not Zero Length And Branch And Vertical,Horizontal
1665
                                    else
1666
                                    {
1667
                                        if (connector.ConnectItem1SymbolObject != null &&
1668
                                            connector.ConnectItem1SymbolID != _LMSymbol.Id &&
1669
                                            connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
1670
                                            connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch" &&
1671
                                            IsMovePoint2D(connector.ConnectItem1SymbolObject, connector, arrow))
1672
                                        {
1673
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1674
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1675
                                                drawingObjectBases.Add(point2D);
1676
                                        }
1677

    
1678
                                        else if (connector.ConnectItem2SymbolObject != null &&
1679
                                            connector.ConnectItem2SymbolID != _LMSymbol.Id &&
1680
                                            connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
1681
                                            connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch" &&
1682
                                            IsMovePoint2D(connector.ConnectItem2SymbolObject, connector, arrow))
1683
                                        {
1684
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1685
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1686
                                                drawingObjectBases.Add(point2D);
1687
                                        }
1688

    
1689
                                    }
1690
                                    #endregion
1691
                                }
1692
                            }
1693
                        }
1694
                        ReleaseCOMObjects(_LMSymbol);
1695
                    }
1696
                    else if (drawingObject.GetType() == typeof(Point2d))
1697
                    {
1698
                        Point2d point2D = drawingObject as Point2d;
1699
                        if (!drawingObjectBases.Contains(point2D))
1700
                            drawingObjectBases.Add(point2D);
1701
                    }
1702
                }
1703

    
1704
                application.ActiveSelectSet.RemoveAll();
1705
                if (drawingObjectBases.Count > 0 && transform != null)
1706
                {
1707
                    application.ActiveSelectSet.AddObjects(drawingObjectBases);
1708
                    SetTransform(transform, arrow);
1709
                    application.ActiveSelectSet.Transform(transform, false);
1710

    
1711
                    application.ActiveSelectSet.RemoveAll();
1712
                    application.ActiveSelectSet.AddObjects(originalDrawingObjectBases);
1713
                }
1714
                
1715
                //foreach (DrawingObjectBaseEx item in drawingObjectBases)
1716
                //    MoveSymbol(item, arrow);
1717

    
1718
                ReleaseCOMObjects(dataSource);
1719
                ReleaseCOMObjects(placement);
1720
            }
1721
        }
1722
        private bool IsMovePoint2D(LMSymbol branchSymbol, LMConnector targetConnector, Arrow arrow)
1723
        {
1724
            bool result = false;
1725
            
1726
            DependencyObject dependency = application.ActiveDocument.ActiveSheet.DrawingObjects[targetConnector.get_GraphicOID().ToString()] as DependencyObject;
1727
            SlopeType mainSlope = GetLineSlopeType(dependency);
1728

    
1729
            if (mainSlope ==  SlopeType.HORIZONTAL || mainSlope == SlopeType.VERTICAL)
1730
            {
1731
                List<SlopeType> types = new List<SlopeType>();
1732

    
1733
                foreach (LMConnector connector in branchSymbol.Connect1Connectors)
1734
                {
1735
                    if (connector.get_ItemStatus() == "Active" && 
1736
                        connector.Id != targetConnector.Id && 
1737
                        !Convert.ToBoolean(connector.get_IsZeroLength()))
1738
                        types.Add(GetLineSlopeType(application.ActiveDocument.ActiveSheet.DrawingObjects[connector.get_GraphicOID().ToString()] as DependencyObject));
1739
                }
1740

    
1741
                foreach (LMConnector connector in branchSymbol.Connect2Connectors)
1742
                {
1743
                    if (connector.get_ItemStatus() == "Active" &&
1744
                        connector.Id != targetConnector.Id &&
1745
                        !Convert.ToBoolean(connector.get_IsZeroLength()))
1746
                        types.Add(GetLineSlopeType(application.ActiveDocument.ActiveSheet.DrawingObjects[connector.get_GraphicOID().ToString()] as DependencyObject));
1747
                }
1748

    
1749
                foreach (var type in types)
1750
                {
1751
                    if (type == mainSlope)
1752
                    {
1753
                        if (type == SlopeType.HORIZONTAL && (arrow == Arrow.Up || arrow == Arrow.Down))
1754
                            result = true;
1755
                        else if (type == SlopeType.VERTICAL && (arrow == Arrow.Left || arrow == Arrow.Right))
1756
                            result = true;
1757
                        else
1758
                        {
1759
                            result = false;
1760
                            break;
1761
                        }
1762
                    }
1763
                    else
1764
                        result = true;
1765
                }
1766

    
1767
                if (result)
1768
                {
1769
                    if ((arrow == Arrow.Down || arrow == Arrow.Up) && mainSlope == SlopeType.VERTICAL)
1770
                        result = false;
1771
                    else if ((arrow == Arrow.Left || arrow == Arrow.Right) && mainSlope == SlopeType.HORIZONTAL)
1772
                        result = false;
1773
                }
1774
            }
1775

    
1776
            return result;
1777
        }
1778
        private SlopeType GetLineSlopeType(DependencyObject dependency)
1779
        {
1780
            if (dependency != null && dependency.DrawingObjects.Count == 1 && dependency.DrawingObjects[0].GetType() == typeof(LineString2d))
1781
            {
1782
                LineString2d line = dependency.DrawingObjects[0] as LineString2d;
1783
                double x1 = 0, y1 = 0, x2 = double.MaxValue, y2 = double.MaxValue;
1784
                for (int i = 0; i < line.KeyPointCount; i++)
1785
                {
1786
                    double x, y, z;
1787
                    KeyPointType keyPointType;
1788
                    HandleType handleType;
1789
                    line.GetKeyPoint(i, out x, out y, out z, out keyPointType, out handleType);
1790
                    if (keyPointType == KeyPointType.igKeyPointStart)
1791
                    {
1792
                        x1 = x;
1793
                        y1 = y;
1794
                    }
1795
                    else if (keyPointType == KeyPointType.igKeyPointEnd)
1796
                    {
1797
                        x2 = x;
1798
                        y2 = y;
1799
                    }
1800
                }
1801
                return SPPIDUtil.CalcSlope(x1, y1, x2, y2, 0.5);
1802
            }
1803

    
1804
            return SlopeType.None;
1805
        }
1806
        private void MoveSymbol(DrawingObjectBaseEx drawingObjectBase, Arrow arrow)
1807
        {
1808
            if (drawingObjectBase == null)
1809
                return;
1810

    
1811
            GridSetting gridSetting = GridSetting.GetInstance();
1812
            switch (arrow)
1813
            {
1814
                case Arrow.Left:
1815
                    drawingObjectBase.Move(0, 0, -gridSetting.Length, 0);
1816
                    break;
1817
                case Arrow.Up:
1818
                    drawingObjectBase.Move(0, 0, 0, gridSetting.Length);
1819
                    break;
1820
                case Arrow.Right:
1821
                    drawingObjectBase.Move(0, 0, gridSetting.Length, 0);
1822
                    break;
1823
                case Arrow.Down:
1824
                    drawingObjectBase.Move(0, 0, 0, -gridSetting.Length);
1825
                    break;
1826
                default:
1827
                    break;
1828
            }
1829
        }
1830
        private void SetTransform(Transform transform, Arrow arrow)
1831
        {
1832
            if (transform == null)
1833
                return;
1834

    
1835
            GridSetting gridSetting = GridSetting.GetInstance();
1836
            switch (arrow)
1837
            {
1838
                case Arrow.Left:
1839
                    transform.DefineByMove2d(-gridSetting.Length, 0);
1840
                    break;
1841
                case Arrow.Up:
1842
                    transform.DefineByMove2d(0, gridSetting.Length);
1843
                    break;
1844
                case Arrow.Right:
1845
                    transform.DefineByMove2d(gridSetting.Length, 0);
1846
                    break;
1847
                case Arrow.Down:
1848
                    transform.DefineByMove2d(0, -gridSetting.Length);
1849
                    break;
1850
                default:
1851
                    break;
1852
            }
1853
        }
1854
        #endregion
1855

    
1856
        #endregion
1857

    
1858
        
1859
        #region TEST
1860
        private void simpleButton1_Click(object sender, EventArgs e)
1861
        {
1862
            //SPPIDUtil.test();
1863
            if (application.ActiveSelectSet.Count == 0)
1864
                return;
1865

    
1866
            foreach (DrawingObjectBase item in application.ActiveSelectSet)
1867
            {
1868
                DependencyObject dependency = item as DependencyObject;
1869
                if (dependency != null)
1870
                {
1871
                    foreach (var attributes in dependency.AttributeSets)
1872
                    {
1873
                        foreach (var attribute in attributes)
1874
                        {
1875
                            string name = attribute.Name;
1876
                            object value = attribute.GetValue();
1877
                            if (name == "ModelID")
1878
                            {
1879
                                Placement placement = new Placement();
1880
                                LMADataSource dataSource = placement.PIDDataSource;
1881
                                LMPipeRun pipeRun = dataSource.GetPipeRun(value);
1882
                                string attrName = "PlantGroup.Name";
1883
                                LMAAttribute lMAAttribute = pipeRun.Attributes[attrName];
1884
                                if (lMAAttribute != null)
1885
                                {
1886
                                    lMAAttribute.set_Value("25");
1887
                                    pipeRun.Commit();
1888
                                }
1889
                                else
1890
                                {
1891

    
1892
                                }
1893
                            }
1894
                        }
1895
                    }
1896
                }
1897
            }
1898
            
1899
            
1900

    
1901
            //string a = "0A509911F33441A2AF088BFBA78B770D";
1902
            //LMLabelPersist label = dataSource.GetLabelPersist(a);
1903
            //label.set_XCoordinate(0.4);
1904

    
1905

    
1906

    
1907

    
1908
            //LMOptionSettings
1909

    
1910

    
1911
            //LMAFilter filter = new LMAFilter();
1912
            //LMACriterion criterion = new LMACriterion();
1913
            //filter.ItemType = "Relationship";
1914
            //criterion.SourceAttributeName = "SP_DRAWINGID";
1915
            //criterion.Operator = "=";
1916
            //criterion.set_ValueAttribute(drawingID);
1917
            //filter.get_Criteria().Add(criterion);
1918

    
1919
            //LMRelationships relationships = new LMRelationships();
1920
            //relationships.Collect(dataSource, Filter: filter);
1921

    
1922
        }
1923
        private void AutoJoinPipeRun()
1924
        {
1925
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
1926
            WrapperApplication wApp = new WrapperApplication(application.Application);
1927
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
1928

    
1929
            string modelItemId = null;
1930
            List<double[]> vertices = new List<double[]>();
1931
            if (radApp.ActiveSelectSet.Count == 0)
1932
            {
1933
                return;
1934
            }
1935
            dynamic OID = radApp.ActiveSelectSet[0].Key();
1936
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1937
            foreach (var attributes in drawingObject.AttributeSets)
1938
            {
1939
                foreach (var attribute in attributes)
1940
                {
1941
                    if (attribute.Name == "ModelID")
1942
                        modelItemId = attribute.GetValue().ToString();
1943
                }
1944
            }
1945
            radApp.ActiveSelectSet.RemoveAll();
1946

    
1947
        }
1948
        #endregion
1949

    
1950

    
1951
        [DllImport("user32.dll")]
1952
        public static extern int FindWindow(string lpClassName, string lpWindowName);
1953

    
1954
        [DllImport("user32.dll", SetLastError = true)]
1955
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
1956

    
1957
        private void btnLinkOPCTest_Click(object sender, EventArgs e)
1958
        {
1959
            Placement placement = new Placement();
1960
            LMADataSource dataSource = placement.PIDDataSource;
1961

    
1962
            LMAFilter filter = new LMAFilter();
1963
            filter.ItemType = "REPRESENTATION";
1964
            
1965
            LMACriterion criterion = new LMACriterion();
1966
            criterion.SourceAttributeName = "REPRESENTATIONTYPE";
1967
            criterion.Operator = "=";
1968
            criterion.set_ValueAttribute("39");
1969
            filter.get_Criteria().Add(criterion);
1970

    
1971
            LMRepresentations representations = new LMRepresentations();
1972
            representations.Collect(dataSource, Filter: filter);
1973

    
1974
            foreach (LMRepresentation representation in representations)
1975
            {
1976
                if (representation.get_ItemStatus() == "Active" && representation.DrawingID != "0")
1977
                {
1978
                    LMOPC opc = dataSource.GetOPC(representation.ModelItemID);
1979
                    LMOPC pairOPC = opc.pairedWithOPCObject;
1980
                    bool isPaired = false;
1981
                    foreach (LMRepresentation rep in pairOPC.Representations)
1982
                        if (rep.DrawingID != "0")
1983
                            isPaired = true;
1984

    
1985
                    if (!isPaired)
1986
                    {
1987
                        LMSymbol newOPC = placement.PIDPlaceSymbol(representation.get_FileName().ToString(), 0, 0, ExistingItem: pairOPC.AsLMAItem());
1988
                        ReleaseCOMObjects(newOPC);
1989
                    }
1990

    
1991
                    ReleaseCOMObjects(opc);
1992
                    ReleaseCOMObjects(pairOPC);
1993
                    
1994
                }
1995
            }
1996

    
1997
            ReleaseCOMObjects(representations);
1998
            ReleaseCOMObjects(criterion);
1999
            ReleaseCOMObjects(filter);
2000
            ReleaseCOMObjects(dataSource);
2001
            ReleaseCOMObjects(placement);
2002
        }
2003

    
2004
        #region Test For SECL
2005
        private void btnAutoBreakTest_Click(object sender, EventArgs e)
2006
        {
2007
            
2008
        }
2009

    
2010
        private void FindRun(LMSymbol startSymbol, LMSymbol endSymbol, string model)
2011
        {
2012
            Placement placement = new Placement();
2013
            LMADataSource dataSource = placement.PIDDataSource;
2014
            List<string> endRepID = new List<string>();
2015
            List<string> findRepID = new List<string>();
2016
            endRepID.Add(startSymbol.AsLMRepresentation().Id);
2017
            findRepID.Add(startSymbol.AsLMRepresentation().Id);
2018
            bool end = false;
2019
            LoopRunTest(startSymbol.AsLMRepresentation(), endRepID, endSymbol.AsLMRepresentation().Id, ref end, findRepID);
2020

    
2021

    
2022
            //findRepID.Reverse();
2023
            for (int i = 0; i < findRepID.Count; i++)
2024
            {
2025
                string repID = findRepID[i];
2026

    
2027
                LMRepresentation representation = dataSource.GetRepresentation(repID);
2028
                if (representation != null)
2029
                {
2030
                    string fileName = representation.get_FileName().ToString();
2031
                    if (fileName.Contains(@"\Piping\Valves"))
2032
                    {
2033
                        LMModelItem modelItem = representation.ModelItemObject;
2034
                        LMSymbol tarsym = dataSource.GetSymbol(representation.Id);
2035
                        LMRepresentation prevRep = dataSource.GetRepresentation(findRepID[i - 1]);
2036
                        LMConnector removeConnector = dataSource.GetConnector(prevRep.Id);
2037

    
2038
                        List<double[]> vertices = GetConnectorVertices(removeConnector);
2039

    
2040
                        ZoomObjectByGraphicOID(vertices[0][0], vertices[0][1]);
2041
                        Array array = new double[] { 0, vertices[0][0], vertices[0][1] };
2042
                        LMLabelPersist _LmLabelPersist = placement.PIDPlaceLabel(model, ref array, LabeledItem: removeConnector.AsLMRepresentation(), IsLeaderVisible: true);
2043
                        if (_LmLabelPersist == null)
2044
                        {
2045
                            if (removeConnector.get_ItemStatus() == "Active")
2046
                            {
2047
                                LMConnector newConnector = ReModelingLine(removeConnector, removeConnector.ConnectItem1SymbolObject, removeConnector.ConnectItem2SymbolObject, true, false);
2048
                                FindRun(startSymbol, endSymbol, model);
2049
                            }
2050
                        }
2051
                        
2052

    
2053

    
2054
                        //string dd = startSymbol.ModelItemObject.Attributes["SteamOutTemperature"].get_Value();
2055
                        //string dd2 = endSymbol.ModelItemObject.Attributes["SteamOutTemperature"].get_Value();
2056
                        //double value1 = Convert.ToDouble(dd.Split(new char[] { ' ' })[0]);
2057
                        //double value2 = Convert.ToDouble(dd2.Split(new char[] { ' ' })[0]);
2058

    
2059
                        //if (value1 > value2)
2060
                        //{
2061
                        //    LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i + 1]);
2062
                        //    placement.PIDRemovePlacement(prevRep);
2063
                        //}
2064
                        //else
2065
                        //{
2066
                        //    LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i - 1]);
2067
                        //    placement.PIDRemovePlacement(prevRep);
2068
                        //}
2069

    
2070
                        //LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i - 1]);
2071
                        //placement.PIDRemovePlacement(prevRep);
2072

    
2073

    
2074
                        //application.ActiveDocument.Save();
2075
                        //ReleaseCOMObjects(modelItem);
2076
                        break;
2077
                    }
2078
                }
2079

    
2080
                ReleaseCOMObjects(representation);
2081
            }
2082
        }
2083
        private LMConnector ReModelingLine(LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
2084
        {
2085
            Placement _placement = new Placement();
2086
            LMADataSource dataSource = _placement.PIDDataSource;
2087

    
2088
            string symbolPath = string.Empty;
2089
            #region get symbol path
2090
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
2091
            symbolPath = GetSPPIDFileName(modelItem);
2092
            ReleaseCOMObjects(modelItem);
2093
            #endregion
2094
            bool diagonal = false;
2095
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2096
            LMConnector newConnector = null;
2097
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
2098
            DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2099
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2100
            int verticesCount = lineStringGeometry.VertexCount;
2101
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2102

    
2103
            List<double[]> vertices = new List<double[]>();
2104
            for (int i = 1; i <= verticesCount; i++)
2105
            {
2106
                double x = 0;
2107
                double y = 0;
2108
                lineStringGeometry.GetVertex(i, ref x, ref y);
2109
                vertices.Add(new double[] { x, y });
2110
            }
2111

    
2112
            for (int i = 0; i < vertices.Count; i++)
2113
            {
2114
                double[] points = vertices[i];
2115
                // 시작 심볼이 있고 첫번째 좌표일 때
2116
                if (startSymbol != null && i == 0)
2117
                {
2118
                    if (bStart)
2119
                    {
2120
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
2121
                        if (slopeType == SlopeType.HORIZONTAL)
2122
                            placeRunInputs.AddPoint(points[0], -0.1);
2123
                        else if (slopeType == SlopeType.VERTICAL)
2124
                            placeRunInputs.AddPoint(-0.1, points[1]);
2125
                        else
2126
                            placeRunInputs.AddPoint(points[0], -0.1);
2127

    
2128
                        placeRunInputs.AddPoint(points[0], points[1]);
2129
                    }
2130
                    else
2131
                    {
2132
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2133
                    }
2134
                }
2135
                // 마지막 심볼이 있고 마지막 좌표일 때
2136
                else if (endSymbol != null && i == vertices.Count - 1)
2137
                {
2138
                    if (bEnd)
2139
                    {
2140
                        placeRunInputs.AddPoint(points[0], points[1]);
2141

    
2142
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2143
                        if (slopeType == SlopeType.HORIZONTAL)
2144
                            placeRunInputs.AddPoint(points[0], -0.1);
2145
                        else if (slopeType == SlopeType.VERTICAL)
2146
                            placeRunInputs.AddPoint(-0.1, points[1]);
2147
                        else
2148
                            placeRunInputs.AddPoint(points[0], -0.1);
2149
                    }
2150
                    else
2151
                    {
2152
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2153
                    }
2154
                }
2155
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2156
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2157
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2158
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2159
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2160
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2161
                else
2162
                    placeRunInputs.AddPoint(points[0], points[1]);
2163
            }
2164

    
2165
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2166
            Thread.Sleep(1000);
2167
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2168

    
2169
            ReleaseCOMObjects(placeRunInputs);
2170
            ReleaseCOMObjects(_LMAItem);
2171
            ReleaseCOMObjects(modelItem);
2172

    
2173
            if (newConnector != null)
2174
            {
2175
                newConnector.Commit();
2176
                if (startSymbol != null && bStart)
2177
                {
2178
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2179
                    placeRunInputs = new PlaceRunInputs();
2180
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2181
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2182
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2183
                    if (_LMConnector != null)
2184
                    {
2185
                        _LMConnector.Commit();
2186
                        newConnector = RemoveConnectorForReModelingLine(newConnector);
2187
                        ReleaseCOMObjects(_LMConnector);
2188
                    }
2189
                    ReleaseCOMObjects(placeRunInputs);
2190
                    ReleaseCOMObjects(_LMAItem);
2191
                }
2192

    
2193
                if (endSymbol != null && bEnd)
2194
                {
2195
                    if (startSymbol != null)
2196
                    {
2197
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2198
                        newConnector = dicVertices.First().Key;
2199
                    }
2200

    
2201
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2202
                    placeRunInputs = new PlaceRunInputs();
2203
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2204
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2205
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2206
                    if (_LMConnector != null)
2207
                    {
2208
                        _LMConnector.Commit();
2209
                        newConnector = RemoveConnectorForReModelingLine(newConnector);
2210
                        ReleaseCOMObjects(_LMConnector);
2211
                    }
2212
                    ReleaseCOMObjects(placeRunInputs);
2213
                    ReleaseCOMObjects(_LMAItem);
2214
                }
2215

    
2216
                
2217
            }
2218
            ReleaseCOMObjects(modelItem);
2219
            return newConnector;
2220
            
2221
        }
2222

    
2223
        private LMConnector RemoveConnectorForReModelingLine(LMConnector connector)
2224
        {
2225
            Placement _placement = new Placement();
2226
            LMADataSource dataSource = _placement.PIDDataSource;
2227

    
2228
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2229
            foreach (var item in dicVertices)
2230
            {
2231
                if (item.Value.Count == 2)
2232
                {
2233
                    bool result = false;
2234
                    foreach (var point in item.Value)
2235
                    {
2236
                        if (point[0] < 0 || point[1] < 0)
2237
                        {
2238
                            result = true;
2239
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2240
                            break;
2241
                        }
2242
                    }
2243

    
2244
                    if (result)
2245
                        break;
2246
                }
2247
            }
2248

    
2249
            LMConnector result111 = null;
2250
            foreach (var item in dicVertices)
2251
            {
2252
                if (item.Key.get_ItemStatus() == "Active")
2253
                    result111 = item.Key;
2254
                else
2255
                    ReleaseCOMObjects(item.Key);
2256
            }
2257

    
2258
            return result111;
2259
        }
2260

    
2261
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId, bool ContainZeroLength = true)
2262
        {
2263
            Placement _placement = new Placement();
2264
            LMADataSource dataSource = _placement.PIDDataSource;
2265

    
2266
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2267
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2268

    
2269
            if (modelItem != null)
2270
            {
2271
                foreach (LMRepresentation rep in modelItem.Representations)
2272
                {
2273
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2274
                    {
2275
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2276
                        if (!ContainZeroLength && Convert.ToBoolean(_LMConnector.get_IsZeroLength()))
2277
                        {
2278
                            ReleaseCOMObjects(_LMConnector);
2279
                            _LMConnector = null;
2280
                            continue;
2281
                        }
2282
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2283
                        dynamic OID = rep.get_GraphicOID().ToString();
2284
                        DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2285
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2286
                        int verticesCount = lineStringGeometry.VertexCount;
2287
                        double[] vertices = null;
2288
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2289
                        for (int i = 0; i < verticesCount; i++)
2290
                        {
2291
                            double x = 0;
2292
                            double y = 0;
2293
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2294
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
2295
                        }
2296
                    }
2297
                }
2298

    
2299
                ReleaseCOMObjects(modelItem);
2300
            }
2301

    
2302
            return connectorVertices;
2303
        }
2304
        private string GetSPPIDFileName(LMModelItem modelItem)
2305
        {
2306
            string symbolPath = null;
2307
            foreach (LMRepresentation rep in modelItem.Representations)
2308
            {
2309
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2310
                {
2311
                    symbolPath = rep.get_FileName();
2312
                    break;
2313
                }
2314
            }
2315
            return symbolPath;
2316
        }
2317
        private string GetSPPIDFileName(string modelItemId)
2318
        {
2319
            Placement placement = new Placement();
2320
            LMADataSource dataSource = placement.PIDDataSource;
2321

    
2322
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2323
            string symbolPath = null;
2324
            foreach (LMRepresentation rep in modelItem.Representations)
2325
            {
2326
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2327
                {
2328
                    symbolPath = rep.get_FileName();
2329
                    break;
2330
                }
2331
            }
2332
            ReleaseCOMObjects(modelItem);
2333
            return symbolPath;
2334
        }
2335
        private List<double[]> GetConnectorVertices(LMConnector connector)
2336
        {
2337
            List<double[]> vertices = new List<double[]>();
2338
            if (connector != null)
2339
            {
2340
                dynamic OID = connector.get_GraphicOID().ToString();
2341
                DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2342
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2343
                int verticesCount = lineStringGeometry.VertexCount;
2344
                double[] value = null;
2345
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
2346
                for (int i = 0; i < verticesCount; i++)
2347
                {
2348
                    double x = 0;
2349
                    double y = 0;
2350
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2351
                    vertices.Add(new double[] { x, y });
2352
                }
2353
            }
2354
            return vertices;
2355
        }
2356
        private void LoopRunTest(LMRepresentation rep, List<string> endRepID, string endID,ref bool end, List<string> findRepID)
2357
        {
2358
            if (!end)
2359
            {
2360
                foreach (LMRelationship item in rep.Relation1Relationships)
2361
                {
2362
                    if (end)
2363
                        break;
2364

    
2365
                    if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID) && item.Item1RepresentationObject.get_ItemStatus() == "Active" &&
2366
                        (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item1RepresentationObject.get_RepresentationClass() == "Connector"))
2367
                    {
2368
                        if (endID != item.Item1RepresentationID)
2369
                        {
2370
                            endRepID.Add(item.Item1RepresentationID);
2371
                            LoopRunTest(item.Item1RepresentationObject, endRepID, endID, ref end, findRepID);
2372
                            if (end)
2373
                                findRepID.Add(item.Item1RepresentationID);
2374
                        }
2375
                        else
2376
                            end = true;
2377
                    }
2378

    
2379
                    if (end)
2380
                        break;
2381

    
2382
                    if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID) && item.Item2RepresentationObject.get_ItemStatus() == "Active" &&
2383
                        (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item2RepresentationObject.get_RepresentationClass() == "Connector"))
2384
                    {
2385
                        if (endID != item.Item2RepresentationID)
2386
                        {
2387
                            endRepID.Add(item.Item2RepresentationID);
2388
                            LoopRunTest(item.Item2RepresentationObject, endRepID, endID, ref end, findRepID);
2389
                            if (end)
2390
                                findRepID.Add(item.Item2RepresentationID);
2391
                        }
2392
                        else
2393
                            end = true;
2394
                    }
2395
                }
2396
            }
2397

    
2398
            if (!end)
2399
            {
2400
                foreach (LMRelationship item in rep.Relation2Relationships)
2401
                {
2402
                    if (end)
2403
                        break;
2404

    
2405
                    if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID) && item.Item1RepresentationObject.get_ItemStatus() == "Active" &&
2406
                        (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item1RepresentationObject.get_RepresentationClass() == "Connector"))
2407
                    {
2408
                        if (endID != item.Item1RepresentationID)
2409
                        {
2410
                            endRepID.Add(item.Item1RepresentationID);
2411
                            LoopRunTest(item.Item1RepresentationObject, endRepID, endID, ref end, findRepID);
2412
                            if (end)
2413
                                findRepID.Add(item.Item1RepresentationID);
2414
                        }
2415
                        else
2416
                            end = true;
2417
                    }
2418

    
2419
                    if (end)
2420
                        break;
2421

    
2422
                    if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID) && item.Item2RepresentationObject.get_ItemStatus() == "Active" &&
2423
                        (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item2RepresentationObject.get_RepresentationClass() == "Connector"))
2424
                    {
2425
                        if (endID != item.Item2RepresentationID)
2426
                        {
2427
                            endRepID.Add(item.Item2RepresentationID);
2428
                            LoopRunTest(item.Item2RepresentationObject, endRepID, endID, ref end, findRepID);
2429
                            if (end)
2430
                                findRepID.Add(item.Item2RepresentationID);
2431
                        }
2432
                        else
2433
                            end = true;
2434
                    }
2435
                }
2436
            }
2437
        }
2438

    
2439
        private void btnOPC_Click(object sender, EventArgs e)
2440
        {
2441
            Placement _placement;
2442
            LMADataSource dataSource;
2443
            _placement = new Placement();
2444
            dataSource = _placement.PIDDataSource;
2445

    
2446
            OPCFlowDirection();
2447

    
2448
            void OPCFlowDirection()
2449
            {
2450
                //current LMDrawing 가져오기
2451
                LMAFilter filter = new LMAFilter();
2452
                LMACriterion criterion = new LMACriterion();
2453
                filter.ItemType = "Drawing";
2454
                criterion.SourceAttributeName = "Name";
2455
                criterion.Operator = "=";
2456
                criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
2457
                filter.get_Criteria().Add(criterion);
2458

    
2459
                LMDrawings drawings = new LMDrawings();
2460
                drawings.Collect(dataSource, Filter: filter);
2461

    
2462
                // Input Drawing Attribute
2463
                LMDrawing drawing = ((dynamic)drawings).Nth(1);
2464

    
2465
                LMAFilter filter2 = new LMAFilter();
2466
                filter2.ItemType = "REPRESENTATION";
2467

    
2468
                LMACriterion criterion2 = new LMACriterion();
2469
                criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
2470
                criterion2.Operator = "=";
2471
                criterion2.set_ValueAttribute("39");
2472
                filter2.get_Criteria().Add(criterion2);
2473

    
2474
                LMRepresentations representations = new LMRepresentations();
2475
                representations.Collect(dataSource, Filter: filter2);
2476

    
2477
                foreach (LMRepresentation representation in representations)
2478
                {
2479
                    if (representation.DrawingID == drawing.Id)
2480
                    {
2481
                        RunOPC(representation);
2482
                    }
2483
                }
2484
                
2485
                ReleaseCOMObjects(filter);
2486
                ReleaseCOMObjects(filter2);
2487
                ReleaseCOMObjects(criterion);
2488
                ReleaseCOMObjects(criterion2);
2489
                ReleaseCOMObjects(drawing);
2490
                ReleaseCOMObjects(drawings);
2491
                ReleaseCOMObjects(representations);
2492
            }
2493

    
2494
            void RunOPC(LMRepresentation representation)
2495
            {
2496
                LMSymbol symbol = dataSource.GetSymbol(representation.Id);
2497
                List<string> endRepId = new List<string>();
2498
                endRepId.Add(symbol.AsLMRepresentation().Id);
2499
                LoopSymbol(symbol, endRepId);
2500
                
2501
                ReleaseCOMObjects(symbol);
2502

    
2503
                void LoopSymbol(LMSymbol loopSymbol, List<string> endRepID)
2504
                {
2505
                    foreach (LMRelationship item in loopSymbol.Relation1Relationships)
2506
                    {
2507
                        if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID))
2508
                        {
2509
                            endRepID.Add(item.Item1RepresentationID);
2510
                            if (item.Item1RepresentationObject.get_RepresentationClass() == "Connector")
2511
                            {
2512
                                LMModelItem modelItem = item.Item1RepresentationObject.ModelItemObject;
2513
                                SetAttribute(modelItem);
2514
                                LoopConnector(dataSource.GetConnector(item.Item1RepresentationID), endRepID);
2515
                            }
2516
                            else if (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol")
2517
                            {
2518
                                LoopSymbol(dataSource.GetSymbol(item.Item1RepresentationID), endRepID);
2519
                            }
2520
                        }
2521

    
2522
                        if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID))
2523
                        {
2524
                            endRepID.Add(item.Item2RepresentationID);
2525
                            if (item.Item2RepresentationObject.get_RepresentationClass() == "Connector")
2526
                            {
2527
                                LMModelItem modelItem = item.Item2RepresentationObject.ModelItemObject;
2528
                                SetAttribute(modelItem);
2529
                                LoopConnector(dataSource.GetConnector(item.Item2RepresentationID), endRepID);
2530
                            }
2531
                            else if (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol")
2532
                            {
2533
                                LoopSymbol(dataSource.GetSymbol(item.Item2RepresentationID), endRepID);
2534
                            }
2535
                        }
2536
                    }
2537

    
2538
                    foreach (LMRelationship item in loopSymbol.Relation2Relationships)
2539
                    {
2540
                        if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID))
2541
                        {
2542
                            endRepID.Add(item.Item1RepresentationID);
2543
                            if (item.Item1RepresentationObject.get_RepresentationClass() == "Connector")
2544
                            {
2545
                                LMModelItem modelItem = item.Item1RepresentationObject.ModelItemObject;
2546
                                SetAttribute(modelItem);
2547
                                LoopConnector(dataSource.GetConnector(item.Item1RepresentationID), endRepID);
2548
                            }
2549
                            else if (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol")
2550
                            {
2551
                                LoopSymbol(dataSource.GetSymbol(item.Item1RepresentationID), endRepID);
2552
                            }
2553
                        }
2554

    
2555
                        if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID))
2556
                        {
2557
                            endRepID.Add(item.Item2RepresentationID);
2558
                            if (item.Item2RepresentationObject.get_RepresentationClass() == "Connector")
2559
                            {
2560
                                LMModelItem modelItem = item.Item2RepresentationObject.ModelItemObject;
2561
                                SetAttribute(modelItem);
2562
                                LoopConnector(dataSource.GetConnector(item.Item2RepresentationID), endRepID);
2563
                            }
2564
                            else if (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol")
2565
                            {
2566
                                LoopSymbol(dataSource.GetSymbol(item.Item2RepresentationID), endRepID);
2567
                            }
2568
                        }
2569
                    }
2570

    
2571
                    foreach (LMConnector item in loopSymbol.Connect1Connectors)//나가는것
2572
                    {
2573
                        if (item.get_ItemStatus() != "Active" || endRepID.Contains(item.AsLMRepresentation().Id))
2574
                            continue;
2575

    
2576
                        endRepID.Add(item.AsLMRepresentation().Id);
2577
                        if (item.get_ItemStatus() == "Active")
2578
                        {
2579
                            LMModelItem modelItem = item.ModelItemObject;
2580
                            SetAttribute(modelItem);
2581
                            LoopConnector(item, endRepID);
2582
                        }
2583
                    }
2584

    
2585
                    foreach (LMConnector item in loopSymbol.Connect2Connectors)//들어오는것
2586
                    {
2587
                        if (item.get_ItemStatus() != "Active" || endRepID.Contains(item.AsLMRepresentation().Id))
2588
                            continue;
2589

    
2590
                        endRepID.Add(item.AsLMRepresentation().Id);
2591
                        if (item.get_ItemStatus() == "Active")
2592
                        {
2593
                            LMModelItem modelItem = item.ModelItemObject;
2594
                            SetAttribute(modelItem);
2595
                            LoopConnector(item, endRepID);
2596
                        }
2597
                    }
2598
                }
2599

    
2600
                void LoopConnector(LMConnector connector, List<string> endRepID)
2601
                {
2602
                    if (connector.ConnectItem1SymbolObject != null && !endRepID.Contains(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id))
2603
                    {
2604
                        endRepID.Add(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id);
2605
                        //LoopSymbol(connector.ConnectItem1SymbolObject, endRepID);
2606
                    }
2607

    
2608
                    if (connector.ConnectItem2SymbolObject != null && !endRepID.Contains(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id))
2609
                    {
2610
                        endRepID.Add(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id);
2611
                        //LoopSymbol(connector.ConnectItem2SymbolObject, endRepID);
2612
                    }
2613
                }
2614
            }
2615

    
2616
            void SetAttribute(LMModelItem modelItem)
2617
            {
2618
                LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
2619
                if (attribute != null)
2620
                    attribute.set_Value("End 1 is upstream (Inlet)");
2621
                modelItem.Commit();
2622
            }
2623

    
2624
            ReleaseCOMObjects(_placement);
2625
            ReleaseCOMObjects(dataSource);
2626

    
2627
            MessageBox.Show("End OPC Flow Direction");
2628
        }
2629

    
2630
        private void btnCreateStreamBreak_Click(object sender, EventArgs e)
2631
        {
2632
            Placement _placement;
2633
            LMADataSource dataSource;
2634

    
2635
            StreamNoBreak();
2636

    
2637
            void StreamNoBreak()
2638
            {
2639
                
2640
                _placement = new Placement();
2641
                dataSource = _placement.PIDDataSource;
2642

    
2643
                OPCFlowDirection();
2644

    
2645
                void OPCFlowDirection()
2646
                {
2647
                    //current LMDrawing 가져오기
2648
                    LMAFilter filter = new LMAFilter();
2649
                    LMACriterion criterion = new LMACriterion();
2650
                    filter.ItemType = "Drawing";
2651
                    criterion.SourceAttributeName = "Name";
2652
                    criterion.Operator = "=";
2653
                    criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
2654
                    filter.get_Criteria().Add(criterion);
2655

    
2656
                    LMDrawings drawings = new LMDrawings();
2657
                    drawings.Collect(dataSource, Filter: filter);
2658

    
2659
                    // Input Drawing Attribute
2660
                    LMDrawing drawing = ((dynamic)drawings).Nth(1);
2661

    
2662
                    LMAFilter filter2 = new LMAFilter();
2663
                    filter2.ItemType = "REPRESENTATION";
2664

    
2665
                    LMACriterion criterion2 = new LMACriterion();
2666
                    criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
2667
                    criterion2.Operator = "=";
2668
                    criterion2.set_ValueAttribute("46");
2669
                    filter2.get_Criteria().Add(criterion2);
2670

    
2671
                    LMRepresentations representations = new LMRepresentations();
2672
                    representations.Collect(dataSource, Filter: filter2);
2673

    
2674
                    foreach (LMRepresentation representation in representations)
2675
                    {
2676
                        if (representation.DrawingID == drawing.Id)
2677
                        {
2678
                            LMSymbol symbol = dataSource.GetSymbol(representation.Id);
2679
                            CreateSpec(symbol);
2680
                        }
2681
                    }
2682

    
2683
                    ReleaseCOMObjects(filter);
2684
                    ReleaseCOMObjects(filter2);
2685
                    ReleaseCOMObjects(criterion);
2686
                    ReleaseCOMObjects(criterion2);
2687
                    ReleaseCOMObjects(drawing);
2688
                    ReleaseCOMObjects(drawings);
2689
                    ReleaseCOMObjects(representations);
2690
                }
2691
            }
2692

    
2693
            void CreateSpec(LMSymbol symbol)
2694
            {
2695
                ZoomObjectByGraphicOID(symbol.get_XCoordinate(), symbol.get_YCoordinate());
2696

    
2697
                foreach (LMConnector item in symbol.Connect1Connectors)
2698
                {
2699
                    if (item.get_ItemStatus() == "Active")
2700
                    {
2701
                        Array array = new double[] { 0, symbol.get_XCoordinate(), symbol.get_YCoordinate() };
2702
                        LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(@"\Piping\Segment Breaks\Above\stream Number Break.sym", ref array, LabeledItem: item.AsLMRepresentation(), IsLeaderVisible: true);
2703
                    }
2704
                }
2705

    
2706
                foreach (LMConnector item in symbol.Connect2Connectors)
2707
                {
2708
                    if (item.get_ItemStatus() == "Active")
2709
                    {
2710
                        Array array = new double[] { 0, symbol.get_XCoordinate(), symbol.get_YCoordinate() };
2711
                        LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(@"\Piping\Segment Breaks\Above\stream Number Break.sym", ref array, LabeledItem: item.AsLMRepresentation(), IsLeaderVisible: true);
2712
                    }
2713
                }
2714

    
2715
                Thread.Sleep(500);
2716
            }
2717

    
2718
            MessageBox.Show("End Modeling");
2719
        }
2720
        
2721
        private void btnPipeRunBreak_Click(object sender, EventArgs e)
2722
        {
2723
            Placement placement = new Placement();
2724
            LMADataSource dataSource = placement.PIDDataSource;
2725

    
2726
            string sFrom = textEdit1.EditValue.ToString();
2727
            string sTo = textEdit2.EditValue.ToString();
2728

    
2729
            LMModelItem item1 = dataSource.GetModelItem(sFrom);
2730
            LMModelItem item2 = dataSource.GetModelItem(sTo);
2731
            if (item1 != null && item1.Representations.Count == 1 &&
2732
                item2 != null && item2.Representations.Count == 1)
2733
            {
2734
                LMSymbol startSymbol = dataSource.GetSymbol(item1.Representations.Nth[1].Id);
2735
                LMSymbol endSymbol = dataSource.GetSymbol(item2.Representations.Nth[1].Id);
2736

    
2737
                if (startSymbol != null && endSymbol != null)
2738
                {
2739
                    FindRun(startSymbol, endSymbol, @"\Piping\Segment Breaks\End of Group.sym");
2740
                }
2741
                else
2742
                {
2743
                    MessageBox.Show("From To 심볼 확인");
2744
                }
2745
            }
2746
            else
2747
            {
2748
                MessageBox.Show("From To 심볼 확인");
2749
            }
2750
        }
2751

    
2752
        private void btnSegmentBreak_Click(object sender, EventArgs e)
2753
        {
2754
            Placement placement = new Placement();
2755
            LMADataSource dataSource = placement.PIDDataSource;
2756

    
2757
            string sFrom = textEdit1.EditValue.ToString();
2758
            string sTo = textEdit2.EditValue.ToString();
2759

    
2760
            LMModelItem item1 = dataSource.GetModelItem(sFrom);
2761
            LMModelItem item2 = dataSource.GetModelItem(sTo);
2762
            if (item1 != null && item1.Representations.Count == 1 &&
2763
                item2 != null && item2.Representations.Count == 1)
2764
            {
2765
                LMSymbol startSymbol = dataSource.GetSymbol(item1.Representations.Nth[1].Id);
2766
                LMSymbol endSymbol = dataSource.GetSymbol(item2.Representations.Nth[1].Id);
2767

    
2768
                if (startSymbol != null && endSymbol != null)
2769
                {
2770
                    FindRun(startSymbol, endSymbol, @"\Piping\Segment Breaks\Max Design Press - Temp.sym");
2771
                }
2772
                else
2773
                {
2774
                    MessageBox.Show("From To 심볼 확인");
2775
                }
2776
            }
2777
            else
2778
            {
2779
                MessageBox.Show("From To 심볼 확인");
2780
            }
2781
        }
2782

    
2783
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
2784
        {
2785
            if (application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
2786
            {
2787
                double minX = 0;
2788
                double minY = 0;
2789
                double maxX = 0;
2790
                double maxY = 0;
2791
                application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
2792
                application.ActiveWindow.ZoomArea2(minX - 0.01, minY - 0.01, maxX + 0.01, maxY + 0.01, null);
2793

    
2794
                Thread.Sleep(milliseconds);
2795
            }
2796
        }
2797

    
2798
        private void ZoomObjectByGraphicOID(double x, double y, int milliseconds = 150)
2799
        {
2800
            application.ActiveWindow.ZoomArea2(x - 0.01, y - 0.01, x + 0.01, y + 0.01, null);
2801
            Thread.Sleep(milliseconds);
2802
        }
2803
        #endregion
2804

    
2805
        
2806
    }
2807
}
클립보드 이미지 추가 (최대 크기: 500 MB)