프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ 6025ea51

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
307
                    ReleaseCOMObjects(symbol);
308
                }
309
            }
310

    
311
            List<OPC_Info> _OPC_Infos = new List<OPC_Info>();
312

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

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

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

    
354
                        ReleaseCOMObjects(findDrawing);
355
                        ReleaseCOMObjects(findRepresentation);
356

    
357
                        //application.ActiveSelectSet.Add(application.ActiveDocument.ActiveSheet.DrawingObjects[currentOPC.Representations.Nth[1].get_GraphicOID().ToString()]);
358
                    }
359
                }
360

    
361
                foreach (var item in findOPCs)
362
                    ReleaseCOMObjects(item);
363
                ReleaseCOMObjects(currentOPC);
364
            }
365

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

    
374
                    
375

    
376
                    LMRepresentation findRep = dataSource.GetRepresentation(item.FindRepID);
377
                    LMOPC findOPC = dataSource.GetOPC(findRep.ModelItemID);
378
                    LMOPC pairOPC = findOPC.pairedWithOPCObject;
379

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

    
398
                        placement.PIDRemovePlacement(removeRep);
399
                        removeRep.Commit();
400

    
401
                        LMSymbol newSymbol = placement.PIDPlaceSymbol(findRep.get_FileName(), x, y, mirror, angle, pairOPC.AsLMAItem(), connConnector);
402
                        newSymbol.Commit();
403

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

    
411
                        ReleaseCOMObjects(newSymbol);
412
                        ReleaseCOMObjects(newOPC);
413
                    }
414

    
415
                    ReleaseCOMObjects(findOPC);
416
                    ReleaseCOMObjects(pairOPC);
417
                    ReleaseCOMObjects(findRep);
418
                    ReleaseCOMObjects(removeOPC);
419
                    ReleaseCOMObjects(removeSymbol);
420
                    ReleaseCOMObjects(removeRep);
421
                    ReleaseCOMObjects(connConnector);
422
                }
423

    
424
                application.ActiveDocument.Save();
425
            }
426

    
427

    
428
            foreach (var item in CurrentDrawing)
429
                ReleaseCOMObjects(item);
430
            foreach (var item in OtherDrawing)
431
                ReleaseCOMObjects(item);
432

    
433
            ReleaseCOMObjects(representations);
434

    
435
            ReleaseCOMObjects(filter2);
436
            ReleaseCOMObjects(criterion2);
437
            ReleaseCOMObjects(drawing);
438
            ReleaseCOMObjects(drawings);
439
            ReleaseCOMObjects(filter);
440
            ReleaseCOMObjects(criterion);
441

    
442
            ReleaseCOMObjects(dataSource);
443
            ReleaseCOMObjects(placement);
444

    
445
            return;
446

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

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

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

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

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

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

    
498
            //tOPCInfo.Dispose();
499
            //tOPCRelations.Dispose();
500
            //tDrawingInfo.Dispose();
501
            //ReleaseCOMObjects(dApplication);
502
            //dApplication = null;
503

    
504
            //MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
505
        }
506

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

    
517
        
518

    
519
        #region SPPID Utils
520

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

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

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

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

    
580
                            ReleaseCOMObjects(firstSymbol);
581
                        }
582

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

    
596
            return;
597

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

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

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

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

    
648
                            if (horizontalSymbols.Contains(symbol1))
649
                                mainSymbol = symbol1;
650
                            if (horizontalSymbols.Contains(symbol2))
651
                                mainSymbol = symbol2;
652
                        }
653
                    }
654
                }
655
            }
656

    
657
            application.ActiveSelectSet.RemoveAll();
658
            foreach (var item in verticalSymbols)
659
                application.ActiveSelectSet.Add(item);
660
            foreach (var item in horizontalSymbols)
661
                application.ActiveSelectSet.Add(item);
662
                
663

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

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

    
682
                        double x1, y1, x2, y2;
683
                        symbol2d.GetOrigin(out x1, out y1);
684
                        moveSymbol2d.GetOrigin(out x2, out y2);
685
                        double distance = SPPIDUtil.CalcPointToPointdDistance(x, y, x1, y1);
686

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

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

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

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

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

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

    
778

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

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

    
834
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
835
            {
836
                if (connector.get_ItemStatus() != "Active")
837
                    continue;
838

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

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

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

    
870
                loop(dataSource, oldIDs, symbols, connector.ConnectItem1SymbolObject, x, y, count);
871
            }
872

    
873
            if (symbols.Count >= count)
874
                return;
875

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

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

    
896
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
897
            {
898
                if (connector.get_ItemStatus() != "Active")
899
                    continue;
900

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

    
910
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
911
            {
912
                if (connector.get_ItemStatus() != "Active")
913
                    continue;
914

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

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

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

    
1087
            return false;
1088
        }
1089
        #endregion
1090

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

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

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

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

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

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

    
1127
                application.ActiveSelectSet.RemoveAll();
1128
                double angle = symbol.Angle;
1129
                if (symbol.GetTransform().HasReflection)
1130
                    angle += Math.PI;
1131

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

    
1150

    
1151
                double originX, originY;
1152
                symbol.GetOrigin(out originX, out originY);
1153

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

    
1168
                        if (slopeType == SlopeType.HORIZONTAL)
1169
                        {
1170
                            if (crossY == 0)
1171
                                crossY = y1;
1172
                            else
1173
                                crossY = (crossY + y1) / 2;
1174

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

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

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

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

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

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

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

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

    
1377
            Transform transform = dependency.GetTransform();
1378
            transform.DefineByMove2d(xTo - xFrom, yTo - yFrom);
1379
            application.ActiveSelectSet.Transform(transform, false);
1380

    
1381
            application.ActiveSelectSet.RemoveAll();
1382
        }
1383
        private string GetDrawingItemType(DependencyObject dependency)
1384
        {
1385
            string result = string.Empty;
1386

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

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

    
1421
        }
1422
        private void FindWidthHeightWidthOutLineString2d(DependencyObject dependency, out double width, out double height)
1423
        {
1424
            width = 0;
1425
            height = 0;
1426

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

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

    
1474
        }
1475
        #endregion
1476

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

    
1563
        }
1564
        #endregion
1565

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

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

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

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

    
1714
                    application.ActiveSelectSet.RemoveAll();
1715
                    application.ActiveSelectSet.AddObjects(originalDrawingObjectBases);
1716
                }
1717
                
1718
                //foreach (DrawingObjectBaseEx item in drawingObjectBases)
1719
                //    MoveSymbol(item, arrow);
1720

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

    
1732
            if (mainSlope ==  SlopeType.HORIZONTAL || mainSlope == SlopeType.VERTICAL)
1733
            {
1734
                List<SlopeType> types = new List<SlopeType>();
1735

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

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

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

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

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

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

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

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

    
1859
        #endregion
1860

    
1861
        
1862
        #region TEST
1863
        private void simpleButton1_Click(object sender, EventArgs e)
1864
        {
1865
            double x1 = double.MaxValue;
1866
            double y1 = double.MaxValue;
1867
            double x2 = double.MinValue;
1868
            double y2 = double.MinValue;
1869
            foreach (DrawingObjectBase item in application.ActiveSelectSet)
1870
            {
1871
                double tx1, ty1, tx2, ty2;
1872
                item.Range(out tx1, out ty1, out tx2, out ty2);
1873
                x1 = Math.Min(x1, tx1);
1874
                y1 = Math.Min(y1, ty1);
1875
                x2 = Math.Max(x2, tx2);
1876
                y2 = Math.Max(y2, ty2);
1877
            }
1878
            return;
1879

    
1880
            //SPPIDUtil.test();
1881
            if (application.ActiveSelectSet.Count == 0)
1882
                return;
1883

    
1884
            foreach (DrawingObjectBase item in application.ActiveSelectSet)
1885
            {
1886
                DependencyObject dependency = item as DependencyObject;
1887
                if (dependency != null)
1888
                {
1889
                    foreach (var attributes in dependency.AttributeSets)
1890
                    {
1891
                        foreach (var attribute in attributes)
1892
                        {
1893
                            string name = attribute.Name;
1894
                            object value = attribute.GetValue();
1895
                            if (name == "ModelID")
1896
                            {
1897
                                Placement placement = new Placement();
1898
                                LMADataSource dataSource = placement.PIDDataSource;
1899
                                LMPipeRun pipeRun = dataSource.GetPipeRun(value);
1900
                                string attrName = "PlantGroup.Name";
1901
                                LMAAttribute lMAAttribute = pipeRun.Attributes[attrName];
1902
                                if (lMAAttribute != null)
1903
                                {
1904
                                    lMAAttribute.set_Value("25");
1905
                                    pipeRun.Commit();
1906
                                }
1907
                                else
1908
                                {
1909

    
1910
                                }
1911
                            }
1912
                        }
1913
                    }
1914
                }
1915
            }
1916
            
1917
            
1918

    
1919
            //string a = "0A509911F33441A2AF088BFBA78B770D";
1920
            //LMLabelPersist label = dataSource.GetLabelPersist(a);
1921
            //label.set_XCoordinate(0.4);
1922

    
1923

    
1924

    
1925

    
1926
            //LMOptionSettings
1927

    
1928

    
1929
            //LMAFilter filter = new LMAFilter();
1930
            //LMACriterion criterion = new LMACriterion();
1931
            //filter.ItemType = "Relationship";
1932
            //criterion.SourceAttributeName = "SP_DRAWINGID";
1933
            //criterion.Operator = "=";
1934
            //criterion.set_ValueAttribute(drawingID);
1935
            //filter.get_Criteria().Add(criterion);
1936

    
1937
            //LMRelationships relationships = new LMRelationships();
1938
            //relationships.Collect(dataSource, Filter: filter);
1939

    
1940
        }
1941
        private void AutoJoinPipeRun()
1942
        {
1943
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
1944
            WrapperApplication wApp = new WrapperApplication(application.Application);
1945
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
1946

    
1947
            string modelItemId = null;
1948
            List<double[]> vertices = new List<double[]>();
1949
            if (radApp.ActiveSelectSet.Count == 0)
1950
            {
1951
                return;
1952
            }
1953
            dynamic OID = radApp.ActiveSelectSet[0].Key();
1954
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1955
            foreach (var attributes in drawingObject.AttributeSets)
1956
            {
1957
                foreach (var attribute in attributes)
1958
                {
1959
                    if (attribute.Name == "ModelID")
1960
                        modelItemId = attribute.GetValue().ToString();
1961
                }
1962
            }
1963
            radApp.ActiveSelectSet.RemoveAll();
1964

    
1965
        }
1966
        #endregion
1967

    
1968

    
1969
        [DllImport("user32.dll")]
1970
        public static extern int FindWindow(string lpClassName, string lpWindowName);
1971

    
1972
        [DllImport("user32.dll", SetLastError = true)]
1973
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
1974

    
1975
        private void btnLinkOPCTest_Click(object sender, EventArgs e)
1976
        {
1977
            Placement placement = new Placement();
1978
            LMADataSource dataSource = placement.PIDDataSource;
1979

    
1980
            LMAFilter filter = new LMAFilter();
1981
            filter.ItemType = "REPRESENTATION";
1982
            
1983
            LMACriterion criterion = new LMACriterion();
1984
            criterion.SourceAttributeName = "REPRESENTATIONTYPE";
1985
            criterion.Operator = "=";
1986
            criterion.set_ValueAttribute("39");
1987
            filter.get_Criteria().Add(criterion);
1988

    
1989
            LMRepresentations representations = new LMRepresentations();
1990
            representations.Collect(dataSource, Filter: filter);
1991

    
1992
            foreach (LMRepresentation representation in representations)
1993
            {
1994
                if (representation.get_ItemStatus() == "Active" && representation.DrawingID != "0")
1995
                {
1996
                    LMOPC opc = dataSource.GetOPC(representation.ModelItemID);
1997
                    LMOPC pairOPC = opc.pairedWithOPCObject;
1998
                    bool isPaired = false;
1999
                    foreach (LMRepresentation rep in pairOPC.Representations)
2000
                        if (rep.DrawingID != "0")
2001
                            isPaired = true;
2002

    
2003
                    if (!isPaired)
2004
                    {
2005
                        LMSymbol newOPC = placement.PIDPlaceSymbol(representation.get_FileName().ToString(), 0, 0, ExistingItem: pairOPC.AsLMAItem());
2006
                        ReleaseCOMObjects(newOPC);
2007
                    }
2008

    
2009
                    ReleaseCOMObjects(opc);
2010
                    ReleaseCOMObjects(pairOPC);
2011
                    
2012
                }
2013
            }
2014

    
2015
            ReleaseCOMObjects(representations);
2016
            ReleaseCOMObjects(criterion);
2017
            ReleaseCOMObjects(filter);
2018
            ReleaseCOMObjects(dataSource);
2019
            ReleaseCOMObjects(placement);
2020
        }
2021

    
2022
        #region Test For SECL
2023
        private void btnAutoBreakTest_Click(object sender, EventArgs e)
2024
        {
2025
            
2026
        }
2027

    
2028
        private void FindRun(LMSymbol startSymbol, LMSymbol endSymbol, string model)
2029
        {
2030
            Placement placement = new Placement();
2031
            LMADataSource dataSource = placement.PIDDataSource;
2032
            List<string> endRepID = new List<string>();
2033
            List<string> findRepID = new List<string>();
2034
            endRepID.Add(startSymbol.AsLMRepresentation().Id);
2035
            findRepID.Add(startSymbol.AsLMRepresentation().Id);
2036
            bool end = false;
2037
            LoopRunTest(startSymbol.AsLMRepresentation(), endRepID, endSymbol.AsLMRepresentation().Id, ref end, findRepID);
2038

    
2039

    
2040
            //findRepID.Reverse();
2041
            for (int i = 0; i < findRepID.Count; i++)
2042
            {
2043
                string repID = findRepID[i];
2044

    
2045
                LMRepresentation representation = dataSource.GetRepresentation(repID);
2046
                if (representation != null)
2047
                {
2048
                    string fileName = representation.get_FileName().ToString();
2049
                    if (fileName.Contains(@"\Piping\Valves"))
2050
                    {
2051
                        LMModelItem modelItem = representation.ModelItemObject;
2052
                        LMSymbol tarsym = dataSource.GetSymbol(representation.Id);
2053
                        LMRepresentation prevRep = dataSource.GetRepresentation(findRepID[i - 1]);
2054
                        LMConnector removeConnector = dataSource.GetConnector(prevRep.Id);
2055

    
2056
                        List<double[]> vertices = GetConnectorVertices(removeConnector);
2057

    
2058
                        ZoomObjectByGraphicOID(vertices[0][0], vertices[0][1]);
2059
                        Array array = new double[] { 0, vertices[0][0], vertices[0][1] };
2060
                        LMLabelPersist _LmLabelPersist = placement.PIDPlaceLabel(model, ref array, LabeledItem: removeConnector.AsLMRepresentation(), IsLeaderVisible: true);
2061
                        if (_LmLabelPersist == null)
2062
                        {
2063
                            if (removeConnector.get_ItemStatus() == "Active")
2064
                            {
2065
                                LMConnector newConnector = ReModelingLine(removeConnector, removeConnector.ConnectItem1SymbolObject, removeConnector.ConnectItem2SymbolObject, true, false);
2066
                                FindRun(startSymbol, endSymbol, model);
2067
                            }
2068
                        }
2069
                        
2070

    
2071

    
2072
                        //string dd = startSymbol.ModelItemObject.Attributes["SteamOutTemperature"].get_Value();
2073
                        //string dd2 = endSymbol.ModelItemObject.Attributes["SteamOutTemperature"].get_Value();
2074
                        //double value1 = Convert.ToDouble(dd.Split(new char[] { ' ' })[0]);
2075
                        //double value2 = Convert.ToDouble(dd2.Split(new char[] { ' ' })[0]);
2076

    
2077
                        //if (value1 > value2)
2078
                        //{
2079
                        //    LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i + 1]);
2080
                        //    placement.PIDRemovePlacement(prevRep);
2081
                        //}
2082
                        //else
2083
                        //{
2084
                        //    LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i - 1]);
2085
                        //    placement.PIDRemovePlacement(prevRep);
2086
                        //}
2087

    
2088
                        //LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i - 1]);
2089
                        //placement.PIDRemovePlacement(prevRep);
2090

    
2091

    
2092
                        //application.ActiveDocument.Save();
2093
                        //ReleaseCOMObjects(modelItem);
2094
                        break;
2095
                    }
2096
                }
2097

    
2098
                ReleaseCOMObjects(representation);
2099
            }
2100
        }
2101
        private LMConnector ReModelingLine(LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
2102
        {
2103
            Placement _placement = new Placement();
2104
            LMADataSource dataSource = _placement.PIDDataSource;
2105

    
2106
            string symbolPath = string.Empty;
2107
            #region get symbol path
2108
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
2109
            symbolPath = GetSPPIDFileName(modelItem);
2110
            ReleaseCOMObjects(modelItem);
2111
            #endregion
2112
            bool diagonal = false;
2113
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2114
            LMConnector newConnector = null;
2115
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
2116
            DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2117
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2118
            int verticesCount = lineStringGeometry.VertexCount;
2119
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2120

    
2121
            List<double[]> vertices = new List<double[]>();
2122
            for (int i = 1; i <= verticesCount; i++)
2123
            {
2124
                double x = 0;
2125
                double y = 0;
2126
                lineStringGeometry.GetVertex(i, ref x, ref y);
2127
                vertices.Add(new double[] { x, y });
2128
            }
2129

    
2130
            for (int i = 0; i < vertices.Count; i++)
2131
            {
2132
                double[] points = vertices[i];
2133
                // 시작 심볼이 있고 첫번째 좌표일 때
2134
                if (startSymbol != null && i == 0)
2135
                {
2136
                    if (bStart)
2137
                    {
2138
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
2139
                        if (slopeType == SlopeType.HORIZONTAL)
2140
                            placeRunInputs.AddPoint(points[0], -0.1);
2141
                        else if (slopeType == SlopeType.VERTICAL)
2142
                            placeRunInputs.AddPoint(-0.1, points[1]);
2143
                        else
2144
                            placeRunInputs.AddPoint(points[0], -0.1);
2145

    
2146
                        placeRunInputs.AddPoint(points[0], points[1]);
2147
                    }
2148
                    else
2149
                    {
2150
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2151
                    }
2152
                }
2153
                // 마지막 심볼이 있고 마지막 좌표일 때
2154
                else if (endSymbol != null && i == vertices.Count - 1)
2155
                {
2156
                    if (bEnd)
2157
                    {
2158
                        placeRunInputs.AddPoint(points[0], points[1]);
2159

    
2160
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2161
                        if (slopeType == SlopeType.HORIZONTAL)
2162
                            placeRunInputs.AddPoint(points[0], -0.1);
2163
                        else if (slopeType == SlopeType.VERTICAL)
2164
                            placeRunInputs.AddPoint(-0.1, points[1]);
2165
                        else
2166
                            placeRunInputs.AddPoint(points[0], -0.1);
2167
                    }
2168
                    else
2169
                    {
2170
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2171
                    }
2172
                }
2173
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2174
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2175
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2176
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2177
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2178
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2179
                else
2180
                    placeRunInputs.AddPoint(points[0], points[1]);
2181
            }
2182

    
2183
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2184
            Thread.Sleep(1000);
2185
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2186

    
2187
            ReleaseCOMObjects(placeRunInputs);
2188
            ReleaseCOMObjects(_LMAItem);
2189
            ReleaseCOMObjects(modelItem);
2190

    
2191
            if (newConnector != null)
2192
            {
2193
                newConnector.Commit();
2194
                if (startSymbol != null && bStart)
2195
                {
2196
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2197
                    placeRunInputs = new PlaceRunInputs();
2198
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2199
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2200
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2201
                    if (_LMConnector != null)
2202
                    {
2203
                        _LMConnector.Commit();
2204
                        newConnector = RemoveConnectorForReModelingLine(newConnector);
2205
                        ReleaseCOMObjects(_LMConnector);
2206
                    }
2207
                    ReleaseCOMObjects(placeRunInputs);
2208
                    ReleaseCOMObjects(_LMAItem);
2209
                }
2210

    
2211
                if (endSymbol != null && bEnd)
2212
                {
2213
                    if (startSymbol != null)
2214
                    {
2215
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2216
                        newConnector = dicVertices.First().Key;
2217
                    }
2218

    
2219
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2220
                    placeRunInputs = new PlaceRunInputs();
2221
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2222
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2223
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2224
                    if (_LMConnector != null)
2225
                    {
2226
                        _LMConnector.Commit();
2227
                        newConnector = RemoveConnectorForReModelingLine(newConnector);
2228
                        ReleaseCOMObjects(_LMConnector);
2229
                    }
2230
                    ReleaseCOMObjects(placeRunInputs);
2231
                    ReleaseCOMObjects(_LMAItem);
2232
                }
2233

    
2234
                
2235
            }
2236
            ReleaseCOMObjects(modelItem);
2237
            return newConnector;
2238
            
2239
        }
2240

    
2241
        private LMConnector RemoveConnectorForReModelingLine(LMConnector connector)
2242
        {
2243
            Placement _placement = new Placement();
2244
            LMADataSource dataSource = _placement.PIDDataSource;
2245

    
2246
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2247
            foreach (var item in dicVertices)
2248
            {
2249
                if (item.Value.Count == 2)
2250
                {
2251
                    bool result = false;
2252
                    foreach (var point in item.Value)
2253
                    {
2254
                        if (point[0] < 0 || point[1] < 0)
2255
                        {
2256
                            result = true;
2257
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2258
                            break;
2259
                        }
2260
                    }
2261

    
2262
                    if (result)
2263
                        break;
2264
                }
2265
            }
2266

    
2267
            LMConnector result111 = null;
2268
            foreach (var item in dicVertices)
2269
            {
2270
                if (item.Key.get_ItemStatus() == "Active")
2271
                    result111 = item.Key;
2272
                else
2273
                    ReleaseCOMObjects(item.Key);
2274
            }
2275

    
2276
            return result111;
2277
        }
2278

    
2279
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId, bool ContainZeroLength = true)
2280
        {
2281
            Placement _placement = new Placement();
2282
            LMADataSource dataSource = _placement.PIDDataSource;
2283

    
2284
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2285
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2286

    
2287
            if (modelItem != null)
2288
            {
2289
                foreach (LMRepresentation rep in modelItem.Representations)
2290
                {
2291
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2292
                    {
2293
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2294
                        if (!ContainZeroLength && Convert.ToBoolean(_LMConnector.get_IsZeroLength()))
2295
                        {
2296
                            ReleaseCOMObjects(_LMConnector);
2297
                            _LMConnector = null;
2298
                            continue;
2299
                        }
2300
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2301
                        dynamic OID = rep.get_GraphicOID().ToString();
2302
                        DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2303
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2304
                        int verticesCount = lineStringGeometry.VertexCount;
2305
                        double[] vertices = null;
2306
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2307
                        for (int i = 0; i < verticesCount; i++)
2308
                        {
2309
                            double x = 0;
2310
                            double y = 0;
2311
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2312
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
2313
                        }
2314
                    }
2315
                }
2316

    
2317
                ReleaseCOMObjects(modelItem);
2318
            }
2319

    
2320
            return connectorVertices;
2321
        }
2322
        private string GetSPPIDFileName(LMModelItem modelItem)
2323
        {
2324
            string symbolPath = null;
2325
            foreach (LMRepresentation rep in modelItem.Representations)
2326
            {
2327
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2328
                {
2329
                    symbolPath = rep.get_FileName();
2330
                    break;
2331
                }
2332
            }
2333
            return symbolPath;
2334
        }
2335
        private string GetSPPIDFileName(string modelItemId)
2336
        {
2337
            Placement placement = new Placement();
2338
            LMADataSource dataSource = placement.PIDDataSource;
2339

    
2340
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2341
            string symbolPath = null;
2342
            foreach (LMRepresentation rep in modelItem.Representations)
2343
            {
2344
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2345
                {
2346
                    symbolPath = rep.get_FileName();
2347
                    break;
2348
                }
2349
            }
2350
            ReleaseCOMObjects(modelItem);
2351
            return symbolPath;
2352
        }
2353
        private List<double[]> GetConnectorVertices(LMConnector connector)
2354
        {
2355
            List<double[]> vertices = new List<double[]>();
2356
            if (connector != null)
2357
            {
2358
                dynamic OID = connector.get_GraphicOID().ToString();
2359
                DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2360
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2361
                int verticesCount = lineStringGeometry.VertexCount;
2362
                double[] value = null;
2363
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
2364
                for (int i = 0; i < verticesCount; i++)
2365
                {
2366
                    double x = 0;
2367
                    double y = 0;
2368
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2369
                    vertices.Add(new double[] { x, y });
2370
                }
2371
            }
2372
            return vertices;
2373
        }
2374
        private void LoopRunTest(LMRepresentation rep, List<string> endRepID, string endID,ref bool end, List<string> findRepID)
2375
        {
2376
            if (!end)
2377
            {
2378
                foreach (LMRelationship item in rep.Relation1Relationships)
2379
                {
2380
                    if (end)
2381
                        break;
2382

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

    
2397
                    if (end)
2398
                        break;
2399

    
2400
                    if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID) && item.Item2RepresentationObject.get_ItemStatus() == "Active" &&
2401
                        (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item2RepresentationObject.get_RepresentationClass() == "Connector"))
2402
                    {
2403
                        if (endID != item.Item2RepresentationID)
2404
                        {
2405
                            endRepID.Add(item.Item2RepresentationID);
2406
                            LoopRunTest(item.Item2RepresentationObject, endRepID, endID, ref end, findRepID);
2407
                            if (end)
2408
                                findRepID.Add(item.Item2RepresentationID);
2409
                        }
2410
                        else
2411
                            end = true;
2412
                    }
2413
                }
2414
            }
2415

    
2416
            if (!end)
2417
            {
2418
                foreach (LMRelationship item in rep.Relation2Relationships)
2419
                {
2420
                    if (end)
2421
                        break;
2422

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

    
2437
                    if (end)
2438
                        break;
2439

    
2440
                    if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID) && item.Item2RepresentationObject.get_ItemStatus() == "Active" &&
2441
                        (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item2RepresentationObject.get_RepresentationClass() == "Connector"))
2442
                    {
2443
                        if (endID != item.Item2RepresentationID)
2444
                        {
2445
                            endRepID.Add(item.Item2RepresentationID);
2446
                            LoopRunTest(item.Item2RepresentationObject, endRepID, endID, ref end, findRepID);
2447
                            if (end)
2448
                                findRepID.Add(item.Item2RepresentationID);
2449
                        }
2450
                        else
2451
                            end = true;
2452
                    }
2453
                }
2454
            }
2455
        }
2456

    
2457
        private void btnOPC_Click(object sender, EventArgs e)
2458
        {
2459
            Placement _placement;
2460
            LMADataSource dataSource;
2461
            _placement = new Placement();
2462
            dataSource = _placement.PIDDataSource;
2463

    
2464
            OPCFlowDirection();
2465

    
2466
            void OPCFlowDirection()
2467
            {
2468
                //current LMDrawing 가져오기
2469
                LMAFilter filter = new LMAFilter();
2470
                LMACriterion criterion = new LMACriterion();
2471
                filter.ItemType = "Drawing";
2472
                criterion.SourceAttributeName = "Name";
2473
                criterion.Operator = "=";
2474
                criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
2475
                filter.get_Criteria().Add(criterion);
2476

    
2477
                LMDrawings drawings = new LMDrawings();
2478
                drawings.Collect(dataSource, Filter: filter);
2479

    
2480
                // Input Drawing Attribute
2481
                LMDrawing drawing = ((dynamic)drawings).Nth(1);
2482

    
2483
                LMAFilter filter2 = new LMAFilter();
2484
                filter2.ItemType = "REPRESENTATION";
2485

    
2486
                LMACriterion criterion2 = new LMACriterion();
2487
                criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
2488
                criterion2.Operator = "=";
2489
                criterion2.set_ValueAttribute("39");
2490
                filter2.get_Criteria().Add(criterion2);
2491

    
2492
                LMRepresentations representations = new LMRepresentations();
2493
                representations.Collect(dataSource, Filter: filter2);
2494

    
2495
                foreach (LMRepresentation representation in representations)
2496
                {
2497
                    if (representation.DrawingID == drawing.Id)
2498
                    {
2499
                        RunOPC(representation);
2500
                    }
2501
                }
2502
                
2503
                ReleaseCOMObjects(filter);
2504
                ReleaseCOMObjects(filter2);
2505
                ReleaseCOMObjects(criterion);
2506
                ReleaseCOMObjects(criterion2);
2507
                ReleaseCOMObjects(drawing);
2508
                ReleaseCOMObjects(drawings);
2509
                ReleaseCOMObjects(representations);
2510
            }
2511

    
2512
            void RunOPC(LMRepresentation representation)
2513
            {
2514
                LMSymbol symbol = dataSource.GetSymbol(representation.Id);
2515
                List<string> endRepId = new List<string>();
2516
                endRepId.Add(symbol.AsLMRepresentation().Id);
2517
                LoopSymbol(symbol, endRepId);
2518
                
2519
                ReleaseCOMObjects(symbol);
2520

    
2521
                void LoopSymbol(LMSymbol loopSymbol, List<string> endRepID)
2522
                {
2523
                    foreach (LMRelationship item in loopSymbol.Relation1Relationships)
2524
                    {
2525
                        if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID))
2526
                        {
2527
                            endRepID.Add(item.Item1RepresentationID);
2528
                            if (item.Item1RepresentationObject.get_RepresentationClass() == "Connector")
2529
                            {
2530
                                LMModelItem modelItem = item.Item1RepresentationObject.ModelItemObject;
2531
                                SetAttribute(modelItem);
2532
                                LoopConnector(dataSource.GetConnector(item.Item1RepresentationID), endRepID);
2533
                            }
2534
                            else if (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol")
2535
                            {
2536
                                LoopSymbol(dataSource.GetSymbol(item.Item1RepresentationID), endRepID);
2537
                            }
2538
                        }
2539

    
2540
                        if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID))
2541
                        {
2542
                            endRepID.Add(item.Item2RepresentationID);
2543
                            if (item.Item2RepresentationObject.get_RepresentationClass() == "Connector")
2544
                            {
2545
                                LMModelItem modelItem = item.Item2RepresentationObject.ModelItemObject;
2546
                                SetAttribute(modelItem);
2547
                                LoopConnector(dataSource.GetConnector(item.Item2RepresentationID), endRepID);
2548
                            }
2549
                            else if (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol")
2550
                            {
2551
                                LoopSymbol(dataSource.GetSymbol(item.Item2RepresentationID), endRepID);
2552
                            }
2553
                        }
2554
                    }
2555

    
2556
                    foreach (LMRelationship item in loopSymbol.Relation2Relationships)
2557
                    {
2558
                        if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID))
2559
                        {
2560
                            endRepID.Add(item.Item1RepresentationID);
2561
                            if (item.Item1RepresentationObject.get_RepresentationClass() == "Connector")
2562
                            {
2563
                                LMModelItem modelItem = item.Item1RepresentationObject.ModelItemObject;
2564
                                SetAttribute(modelItem);
2565
                                LoopConnector(dataSource.GetConnector(item.Item1RepresentationID), endRepID);
2566
                            }
2567
                            else if (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol")
2568
                            {
2569
                                LoopSymbol(dataSource.GetSymbol(item.Item1RepresentationID), endRepID);
2570
                            }
2571
                        }
2572

    
2573
                        if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID))
2574
                        {
2575
                            endRepID.Add(item.Item2RepresentationID);
2576
                            if (item.Item2RepresentationObject.get_RepresentationClass() == "Connector")
2577
                            {
2578
                                LMModelItem modelItem = item.Item2RepresentationObject.ModelItemObject;
2579
                                SetAttribute(modelItem);
2580
                                LoopConnector(dataSource.GetConnector(item.Item2RepresentationID), endRepID);
2581
                            }
2582
                            else if (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol")
2583
                            {
2584
                                LoopSymbol(dataSource.GetSymbol(item.Item2RepresentationID), endRepID);
2585
                            }
2586
                        }
2587
                    }
2588

    
2589
                    foreach (LMConnector item in loopSymbol.Connect1Connectors)//나가는것
2590
                    {
2591
                        if (item.get_ItemStatus() != "Active" || endRepID.Contains(item.AsLMRepresentation().Id))
2592
                            continue;
2593

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

    
2603
                    foreach (LMConnector item in loopSymbol.Connect2Connectors)//들어오는것
2604
                    {
2605
                        if (item.get_ItemStatus() != "Active" || endRepID.Contains(item.AsLMRepresentation().Id))
2606
                            continue;
2607

    
2608
                        endRepID.Add(item.AsLMRepresentation().Id);
2609
                        if (item.get_ItemStatus() == "Active")
2610
                        {
2611
                            LMModelItem modelItem = item.ModelItemObject;
2612
                            SetAttribute(modelItem);
2613
                            LoopConnector(item, endRepID);
2614
                        }
2615
                    }
2616
                }
2617

    
2618
                void LoopConnector(LMConnector connector, List<string> endRepID)
2619
                {
2620
                    if (connector.ConnectItem1SymbolObject != null && !endRepID.Contains(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id))
2621
                    {
2622
                        endRepID.Add(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id);
2623
                        //LoopSymbol(connector.ConnectItem1SymbolObject, endRepID);
2624
                    }
2625

    
2626
                    if (connector.ConnectItem2SymbolObject != null && !endRepID.Contains(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id))
2627
                    {
2628
                        endRepID.Add(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id);
2629
                        //LoopSymbol(connector.ConnectItem2SymbolObject, endRepID);
2630
                    }
2631
                }
2632
            }
2633

    
2634
            void SetAttribute(LMModelItem modelItem)
2635
            {
2636
                LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
2637
                if (attribute != null)
2638
                    attribute.set_Value("End 1 is upstream (Inlet)");
2639
                modelItem.Commit();
2640
            }
2641

    
2642
            ReleaseCOMObjects(_placement);
2643
            ReleaseCOMObjects(dataSource);
2644

    
2645
            MessageBox.Show("End OPC Flow Direction");
2646
        }
2647

    
2648
        private void btnCreateStreamBreak_Click(object sender, EventArgs e)
2649
        {
2650
            Placement _placement;
2651
            LMADataSource dataSource;
2652

    
2653
            StreamNoBreak();
2654

    
2655
            void StreamNoBreak()
2656
            {
2657
                
2658
                _placement = new Placement();
2659
                dataSource = _placement.PIDDataSource;
2660

    
2661
                OPCFlowDirection();
2662

    
2663
                void OPCFlowDirection()
2664
                {
2665
                    //current LMDrawing 가져오기
2666
                    LMAFilter filter = new LMAFilter();
2667
                    LMACriterion criterion = new LMACriterion();
2668
                    filter.ItemType = "Drawing";
2669
                    criterion.SourceAttributeName = "Name";
2670
                    criterion.Operator = "=";
2671
                    criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
2672
                    filter.get_Criteria().Add(criterion);
2673

    
2674
                    LMDrawings drawings = new LMDrawings();
2675
                    drawings.Collect(dataSource, Filter: filter);
2676

    
2677
                    // Input Drawing Attribute
2678
                    LMDrawing drawing = ((dynamic)drawings).Nth(1);
2679

    
2680
                    LMAFilter filter2 = new LMAFilter();
2681
                    filter2.ItemType = "REPRESENTATION";
2682

    
2683
                    LMACriterion criterion2 = new LMACriterion();
2684
                    criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
2685
                    criterion2.Operator = "=";
2686
                    criterion2.set_ValueAttribute("46");
2687
                    filter2.get_Criteria().Add(criterion2);
2688

    
2689
                    LMRepresentations representations = new LMRepresentations();
2690
                    representations.Collect(dataSource, Filter: filter2);
2691

    
2692
                    foreach (LMRepresentation representation in representations)
2693
                    {
2694
                        if (representation.DrawingID == drawing.Id)
2695
                        {
2696
                            LMSymbol symbol = dataSource.GetSymbol(representation.Id);
2697
                            CreateSpec(symbol);
2698
                        }
2699
                    }
2700

    
2701
                    ReleaseCOMObjects(filter);
2702
                    ReleaseCOMObjects(filter2);
2703
                    ReleaseCOMObjects(criterion);
2704
                    ReleaseCOMObjects(criterion2);
2705
                    ReleaseCOMObjects(drawing);
2706
                    ReleaseCOMObjects(drawings);
2707
                    ReleaseCOMObjects(representations);
2708
                }
2709
            }
2710

    
2711
            void CreateSpec(LMSymbol symbol)
2712
            {
2713
                ZoomObjectByGraphicOID(symbol.get_XCoordinate(), symbol.get_YCoordinate());
2714

    
2715
                foreach (LMConnector item in symbol.Connect1Connectors)
2716
                {
2717
                    if (item.get_ItemStatus() == "Active")
2718
                    {
2719
                        Array array = new double[] { 0, symbol.get_XCoordinate(), symbol.get_YCoordinate() };
2720
                        LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(@"\Piping\Segment Breaks\Above\stream Number Break.sym", ref array, LabeledItem: item.AsLMRepresentation(), IsLeaderVisible: true);
2721
                    }
2722
                }
2723

    
2724
                foreach (LMConnector item in symbol.Connect2Connectors)
2725
                {
2726
                    if (item.get_ItemStatus() == "Active")
2727
                    {
2728
                        Array array = new double[] { 0, symbol.get_XCoordinate(), symbol.get_YCoordinate() };
2729
                        LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(@"\Piping\Segment Breaks\Above\stream Number Break.sym", ref array, LabeledItem: item.AsLMRepresentation(), IsLeaderVisible: true);
2730
                    }
2731
                }
2732

    
2733
                Thread.Sleep(500);
2734
            }
2735

    
2736
            MessageBox.Show("End Modeling");
2737
        }
2738
        
2739
        private void btnPipeRunBreak_Click(object sender, EventArgs e)
2740
        {
2741
            Placement placement = new Placement();
2742
            LMADataSource dataSource = placement.PIDDataSource;
2743

    
2744
            string sFrom = textEdit1.EditValue.ToString();
2745
            string sTo = textEdit2.EditValue.ToString();
2746

    
2747
            LMModelItem item1 = dataSource.GetModelItem(sFrom);
2748
            LMModelItem item2 = dataSource.GetModelItem(sTo);
2749
            if (item1 != null && item1.Representations.Count == 1 &&
2750
                item2 != null && item2.Representations.Count == 1)
2751
            {
2752
                LMSymbol startSymbol = dataSource.GetSymbol(item1.Representations.Nth[1].Id);
2753
                LMSymbol endSymbol = dataSource.GetSymbol(item2.Representations.Nth[1].Id);
2754

    
2755
                if (startSymbol != null && endSymbol != null)
2756
                {
2757
                    FindRun(startSymbol, endSymbol, @"\Piping\Segment Breaks\End of Group.sym");
2758
                }
2759
                else
2760
                {
2761
                    MessageBox.Show("From To 심볼 확인");
2762
                }
2763
            }
2764
            else
2765
            {
2766
                MessageBox.Show("From To 심볼 확인");
2767
            }
2768
        }
2769

    
2770
        private void btnSegmentBreak_Click(object sender, EventArgs e)
2771
        {
2772
            Placement placement = new Placement();
2773
            LMADataSource dataSource = placement.PIDDataSource;
2774

    
2775
            string sFrom = textEdit1.EditValue.ToString();
2776
            string sTo = textEdit2.EditValue.ToString();
2777

    
2778
            LMModelItem item1 = dataSource.GetModelItem(sFrom);
2779
            LMModelItem item2 = dataSource.GetModelItem(sTo);
2780
            if (item1 != null && item1.Representations.Count == 1 &&
2781
                item2 != null && item2.Representations.Count == 1)
2782
            {
2783
                LMSymbol startSymbol = dataSource.GetSymbol(item1.Representations.Nth[1].Id);
2784
                LMSymbol endSymbol = dataSource.GetSymbol(item2.Representations.Nth[1].Id);
2785

    
2786
                if (startSymbol != null && endSymbol != null)
2787
                {
2788
                    FindRun(startSymbol, endSymbol, @"\Piping\Segment Breaks\Max Design Press - Temp.sym");
2789
                }
2790
                else
2791
                {
2792
                    MessageBox.Show("From To 심볼 확인");
2793
                }
2794
            }
2795
            else
2796
            {
2797
                MessageBox.Show("From To 심볼 확인");
2798
            }
2799
        }
2800

    
2801
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
2802
        {
2803
            if (application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
2804
            {
2805
                double minX = 0;
2806
                double minY = 0;
2807
                double maxX = 0;
2808
                double maxY = 0;
2809
                application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
2810
                application.ActiveWindow.ZoomArea2(minX - 0.01, minY - 0.01, maxX + 0.01, maxY + 0.01, null);
2811

    
2812
                Thread.Sleep(milliseconds);
2813
            }
2814
        }
2815

    
2816
        private void ZoomObjectByGraphicOID(double x, double y, int milliseconds = 150)
2817
        {
2818
            application.ActiveWindow.ZoomArea2(x - 0.01, y - 0.01, x + 0.01, y + 0.01, null);
2819
            Thread.Sleep(milliseconds);
2820
        }
2821

    
2822
        #endregion
2823

    
2824
        private void btnGetDrawingSize_Click(object sender, EventArgs e)
2825
        {
2826
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
2827
            WrapperApplication wApp = new WrapperApplication(application.Application);
2828
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
2829

    
2830
            if (radApp.ActiveSelectSet.Count > 0)
2831
            {
2832
                DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject;
2833
                if (line2D != null)
2834
                {
2835
                    double minX = 0;
2836
                    double minY = 0;
2837
                    double maxX = 0;
2838
                    double maxY = 0;
2839
                    line2D.Range(out minX, out minY, out maxX, out maxY);
2840

    
2841
                    Settings.Default.DrawingX = maxX - minX;
2842
                    Settings.Default.DrawingY = maxY - minY;
2843
                    Settings.Default.Save();
2844

    
2845
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
2846
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
2847
                }
2848
                else
2849
                    MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2850
            }
2851
            else
2852
                MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2853
        }
2854
    }
2855
}
클립보드 이미지 추가 (최대 크기: 500 MB)