프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ e835aef5

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

1 65881d60 gaqhf
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 69b7387a gaqhf
using System.IO;
12 65881d60 gaqhf
using Microsoft.VisualBasic;
13 e3e2d41f gaqhf
using Ingr.RAD2D;
14 d19ae675 gaqhf
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 4d4dce52 esham21
using Plaice;
21
using Llama;
22 ca214bc3 gaqhf
using DevExpress.XtraSplashScreen;
23 224535bb gaqhf
using Newtonsoft.Json;
24 9628f54b gaqhf
using System.Runtime.InteropServices;
25 4941f5fe gaqhf
using System.Reflection;
26 154d8f43 gaqhf
using Converter.SPPID.OPC;
27 9c451472 gaqhf
using DevExpress.Utils.MVVM;
28 65881d60 gaqhf
29
namespace Converter.SPPID.Wrapper
30
{
31
    public partial class ConverterDocking : UserControl
32
    {
33 4941f5fe gaqhf
        Ingr.RAD2D.Application application;
34 ecf30b64 gaqhf
        internal static bool addEvent = false;
35 65881d60 gaqhf
        public ConverterDocking()
36
        {
37
            InitializeComponent();
38 c3e8fa8e gaqhf
            spinEditSymmetry.Properties.Mask.EditMask = "f0";
39 4941f5fe gaqhf
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
40 4d4dce52 esham21
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
41
            application = wApp.RADApplication;
42 d4c3e39f gaqhf
            
43 4941f5fe gaqhf
            ReleaseCOMObjects(dApplication);
44
            dApplication = null;
45 6a7573b0 gaqhf
            try
46
            {
47 4d4dce52 esham21
                if (Settings.Default.DrawingX == 0)
48
                {
49
                    textEditDrawingX.EditValue = 0.866501770913601;
50
                }
51
                else
52
                {
53
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
54
                }
55
                if (Settings.Default.DrawingY == 0)
56
                {
57
                    textEditDrawingY.EditValue = 0.665463201701641;
58
                }
59
                else
60
                {
61
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
62
                }
63 27e624cd gaqhf
64 154d8f43 gaqhf
                Project_Info _ProjectInfo = Project_Info.GetInstance();
65 8847ea67 gaqhf
                _ProjectInfo.DefaultPath = Settings.Default.ProjectPath;
66
                _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType;
67
                _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP;
68
                _ProjectInfo.Port = Settings.Default.ProjectPort;
69
                _ProjectInfo.DBUser = Settings.Default.ProjectDBUser;
70
                _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword;
71 25c5df17 gaqhf
                if (Project_DB.ConnTestAndCreateTable())
72
                {
73
                    _ProjectInfo.Enable = true;
74 9fcc54bb gaqhf
                    layoutControlGroupUtils.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
75 25c5df17 gaqhf
76
                    DataTable dt = Project_DB.SelectSetting();
77
                    foreach (DataRow item in dt.Rows)
78
                    {
79
                        string settingType = item["SettingType"].ToString();
80
                        if (settingType == "ETCSetting")
81
                            SPPIDUtil.ConvertToETCSetting(item["JsonString"].ToString());
82
                        else if (settingType == "GridSetting")
83
                            SPPIDUtil.ConvertToGridSetting(item["JsonString"].ToString());
84
                    }
85
                }
86
                else
87
                {
88 9fcc54bb gaqhf
                    layoutControlGroupUtils.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
89 25c5df17 gaqhf
                }
90 f14b4e3b gaqhf
91
                if (!addEvent)
92
                {
93
                    application.EventObject.BeforeApplicationExit += ApplicationEvents_ApplicationExit;
94
                    addEvent = true;
95
                }
96 6a7573b0 gaqhf
            }
97
            catch (Exception ex)
98
            {
99
                StringBuilder sb = new StringBuilder();
100
                sb.AppendLine(ex.Message);
101
                sb.AppendLine(ex.StackTrace);
102
                MessageBox.Show(sb.ToString());
103
            }
104 9fcc54bb gaqhf
105
#if DEBUG
106 4d4dce52 esham21
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
107
            layoutControlGroup1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
108 9fcc54bb gaqhf
109
#else
110
            layoutControlGroup1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
111
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
112
#endif
113
114
            //#if DEBUG
115
            //            layoutControlGroupConverter.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
116
            //            layoutControlGroupUtils.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
117
            //            layoutControlGroupShortCut.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
118
            //            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
119
            //            this.layoutControlItem3.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
120
            //#endif
121 65881d60 gaqhf
        }
122 1b09fe17 gaqhf
        private void ApplicationEvents_ApplicationExit(out bool cancel)
123
        {
124
            cancel = false;
125
        }
126 5a9396ae humkyung
        /// <summary>
127
        /// 선택한 도면들을 읽어서 SPPID로 변환한다.
128
        /// </summary>
129
        /// <param name="sender"></param>
130
        /// <param name="e"></param>
131 e3e2d41f gaqhf
        private void btnConverter_Click(object sender, EventArgs e)
132 65881d60 gaqhf
        {
133 1ba9c671 gaqhf
            ConverterForm converterForm = new ConverterForm();
134
            if (converterForm.ShowDialog() == DialogResult.OK)
135 65881d60 gaqhf
            {
136 4941f5fe gaqhf
                //Ingr.RAD2D.Document doc = application.Documents.Add();
137
138 1ba9c671 gaqhf
                try
139
                {
140
                    CloseOPCForm.Run();
141 ca214bc3 gaqhf
142 d5ec4d0f gaqhf
                    for (int i = 0; i < converterForm.Documents.Count; i++)
143 1ba9c671 gaqhf
                    {
144 d5ec4d0f gaqhf
                        SPPID_Document document = converterForm.Documents[i];
145 1ba9c671 gaqhf
                        if (document.SetSPPIDMapping() && document.Enable)
146
                        {
147 bccacd6c gaqhf
                            using (AutoModeling modeling = new AutoModeling(document, converterForm.checkEditCloseDocument.Checked))
148 5a9396ae humkyung
                            {
149
                                modeling.DocumentLabelText = string.Format("Drawing Name : {0} ({1}/{2})", document.DrawingName, i + 1, converterForm.Documents.Count);
150
                                modeling.Run();
151
                            }
152 1ba9c671 gaqhf
                        }
153
                    }
154
                }
155
                catch (Exception ex)
156
                {
157
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
158
                }
159
                finally
160 d19ae675 gaqhf
                {
161 1ba9c671 gaqhf
                    CloseOPCForm.Stop();
162 4941f5fe gaqhf
163
                    //doc.SaveOnClose = false;
164
                    //doc.Close(false);
165
166
                    //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
167
                    //dApplication.Drawings[1].CloseDrawing(false);
168
                    //ReleaseCOMObjects(dApplication);
169
                    //dApplication = null;
170 1ba9c671 gaqhf
                }
171
172
                MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
173
            }
174
        }
175
176 69b7387a gaqhf
        private void btnLinkOPC_Click(object sender, EventArgs e)
177
        {
178 a80baec5 gaqhf
            Placement placement = new Placement();
179
            LMADataSource dataSource = placement.PIDDataSource;
180
181
            LMPlantSettings plantSettings = new LMPlantSettings();
182
            plantSettings.Collect(dataSource);
183
            string settingValue = SPPIDUtil.T_PLANTSETTING_Value("Plant Path");
184
185
            if (string.IsNullOrEmpty(settingValue))
186
                return;
187
            
188
            LMAFilter filter = new LMAFilter();
189
            LMACriterion criterion = new LMACriterion();
190
            filter.ItemType = "Drawing";
191
            criterion.SourceAttributeName = "Name";
192
            criterion.Operator = "=";
193 4d4dce52 esham21
            criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
194
            filter.get_Criteria().Add(criterion);
195 a80baec5 gaqhf
196
            LMDrawings drawings = new LMDrawings();
197
            drawings.Collect(dataSource, Filter: filter);
198 154d8f43 gaqhf
199 a80baec5 gaqhf
            // Input Drawing Attribute
200
            LMDrawing drawing = ((dynamic)drawings).Nth(1);
201 8eee15b4 Denny
            if (drawing == null)
202
            {
203
                MessageBox.Show("Please open the drawing", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
204
                return;
205
            }
206 a80baec5 gaqhf
            LMAFilter filter2 = new LMAFilter();
207
            filter2.ItemType = "REPRESENTATION";
208
209
            LMACriterion criterion2 = new LMACriterion();
210
            criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
211
            criterion2.Operator = "=";
212 4d4dce52 esham21
            criterion2.set_ValueAttribute("39");
213
            filter2.get_Criteria().Add(criterion2);
214 a80baec5 gaqhf
215
            LMRepresentations representations = new LMRepresentations();
216
            representations.Collect(dataSource, Filter: filter2);
217
218
            string drawingID = drawing.Id;
219
            List<Tuple<LMRepresentation, string>> CurrentDrawing = new List<Tuple<LMRepresentation, string>>();
220
            List<Tuple<LMRepresentation, string>> OtherDrawing = new List<Tuple<LMRepresentation, string>>(); 
221
            foreach (LMRepresentation representation in representations)
222 154d8f43 gaqhf
            {
223 4d4dce52 esham21
                if (representation.get_ItemStatus() == "Active" && representation.get_InStockpile() == "False")
224 154d8f43 gaqhf
                {
225 a80baec5 gaqhf
                    LMSymbol symbol = dataSource.GetSymbol(representation.Id);
226 69b7387a gaqhf
227 a80baec5 gaqhf
                    if (symbol != null)
228 154d8f43 gaqhf
                    {
229 a80baec5 gaqhf
                        string sResult = "";
230
                        bool bResult = true;
231
                        foreach (LMConnector item in symbol.Connect1Connectors)
232 154d8f43 gaqhf
                        {
233 4d4dce52 esham21
                            if (item.get_ItemStatus() == "Active" && item.get_InStockpile() == "False" && item.ModelItemObject.get_ItemTypeName() == "PipeRun")
234 154d8f43 gaqhf
                            {
235 a80baec5 gaqhf
                                LMPipeRun pipeRun = dataSource.GetPipeRun(item.ModelItemID);
236
                                if (pipeRun != null)
237
                                {
238 4d4dce52 esham21
                                    dynamic value = pipeRun.get_ItemTag();
239 a80baec5 gaqhf
                                    if (!DBNull.Value.Equals(value))
240
                                    {
241
                                        if (string.IsNullOrEmpty(sResult))
242
                                            sResult = value;
243
                                        else
244
                                            bResult = false;
245
                                    }
246
                                }
247
                                ReleaseCOMObjects(pipeRun);
248 154d8f43 gaqhf
                            }
249
                        }
250 a80baec5 gaqhf
                        foreach (LMConnector item in symbol.Connect2Connectors)
251
                        {
252 4d4dce52 esham21
                            if (item.get_ItemStatus() == "Active" && item.get_InStockpile() == "False" && item.ModelItemObject.get_ItemTypeName() == "PipeRun")
253 a80baec5 gaqhf
                            {
254
                                LMPipeRun pipeRun = dataSource.GetPipeRun(item.ModelItemID);
255
                                if (pipeRun != null)
256
                                {
257 4d4dce52 esham21
                                    dynamic value = pipeRun.get_ItemTag();
258 a80baec5 gaqhf
                                    if (!DBNull.Value.Equals(value))
259
                                    {
260
                                        if (string.IsNullOrEmpty(sResult))
261
                                            sResult = value;
262
                                        else
263
                                            bResult = false;
264
                                    }
265
                                }
266
                                ReleaseCOMObjects(pipeRun);
267
                            }
268
                        }
269
270
                        if (bResult)
271
                        {
272 4d4dce52 esham21
                            if (representation.DrawingID == drawingID)
273 a80baec5 gaqhf
                                CurrentDrawing.Add(new Tuple<LMRepresentation, string>(representation, sResult));
274
                            else
275
                                OtherDrawing.Add(new Tuple<LMRepresentation, string>(representation, sResult));
276
                        }
277 154d8f43 gaqhf
                    }
278 a80baec5 gaqhf
279
                    ReleaseCOMObjects(symbol);
280 154d8f43 gaqhf
                }
281
            }
282
283 a80baec5 gaqhf
            List<OPC_Info> _OPC_Infos = new List<OPC_Info>();
284 7b262903 gaqhf
            DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook();
285
            workbook.BeginUpdate();
286 a80baec5 gaqhf
287 7b262903 gaqhf
            workbook.Worksheets[0][0, 0].Value = "OPC ModelItemID";
288
            workbook.Worksheets[0][0, 1].Value = "Tag Value";
289
            workbook.Worksheets[0][0, 2].Value = "Pair Drawing Name";
290
            workbook.Worksheets[0][0, 3].Value = "Match RepID";
291
292
            int rowIndex = 1;
293 a80baec5 gaqhf
            foreach (var current in CurrentDrawing)
294
            {
295 7b262903 gaqhf
                // 여기 로그
296 a80baec5 gaqhf
                LMOPC currentOPC = dataSource.GetOPC(current.Item1.ModelItemID);
297 4d4dce52 esham21
                string fileName = current.Item1.get_FileName();
298 a80baec5 gaqhf
                string tag = current.Item2;
299
                List<LMOPC> findOPCs = new List<LMOPC>(); 
300
                foreach (var other in OtherDrawing)
301
                {
302 4d4dce52 esham21
                    if (tag == other.Item2 && fileName == other.Item1.get_FileName())
303 a80baec5 gaqhf
                    {
304
                        LMOPC otherOPC = dataSource.GetOPC(other.Item1.ModelItemID);
305 8eee15b4 Denny
                        if (otherOPC != null)
306
                            findOPCs.Add(otherOPC);
307 a80baec5 gaqhf
                    }
308
                }
309
310 7b262903 gaqhf
                workbook.Worksheets[0][rowIndex, 0].Value = current.Item1.ModelItemID.ToString();
311
                workbook.Worksheets[0][rowIndex, 1].Value = string.IsNullOrEmpty(tag) ? "Tag is null" : tag;
312
313
                if (findOPCs.Count >= 1 && !string.IsNullOrEmpty(tag))
314 a80baec5 gaqhf
                {
315 7b262903 gaqhf
                    List<OPC_Info> tempInfos = new List<OPC_Info>();
316
                    foreach (var findOPC in findOPCs)
317 a80baec5 gaqhf
                    {
318 4d4dce52 esham21
                        LMRepresentation currentPairRepresentation = currentOPC.pairedWithOPCObject.Representations.Nth[1];
319
                        LMRepresentation findPairRepresentation = findOPC.pairedWithOPCObject.Representations.Nth[1];
320 7b262903 gaqhf
                        if (currentPairRepresentation == null || findPairRepresentation == null)
321
                            continue;
322 4d4dce52 esham21
                        else if (currentPairRepresentation.get_ItemStatus() == "Active" && currentPairRepresentation.get_InStockpile() == "True" &&
323
                            findPairRepresentation.get_ItemStatus() == "Active" && findPairRepresentation.get_InStockpile() == "True")
324 a80baec5 gaqhf
                        {
325 4d4dce52 esham21
                            string currentGraphicOID = currentOPC.Representations.Nth[1].get_GraphicOID().ToString();
326
                            LMRepresentation findRepresentation = findOPC.Representations.Nth[1];
327
                            string findGraphicOID = findRepresentation.get_GraphicOID().ToString();
328 7b262903 gaqhf
                            LMDrawing findDrawing = findRepresentation.DrawingObject;
329 4d4dce52 esham21
                            string findDrawingPath = settingValue + findDrawing.get_Path();
330 7b262903 gaqhf
331
                            tempInfos.Add(new OPC_Info()
332
                            {
333
                                CurrentGraphicOID = currentGraphicOID,
334
                                TagValue = tag,
335
                                FindGraphicOID = findGraphicOID,
336
                                FindDrawingPath = findDrawingPath,
337
                                FindDrawingName = Path.GetFileNameWithoutExtension(findDrawingPath),
338 4d4dce52 esham21
                                CurrentRepID = currentOPC.Representations.Nth[1].Id,
339 7b262903 gaqhf
                                FindRepID = findRepresentation.Id
340
                            });
341
342
                            ReleaseCOMObjects(findDrawing);
343
                            ReleaseCOMObjects(findRepresentation);
344
                        }
345 a80baec5 gaqhf
                    }
346
347 7b262903 gaqhf
                    if (tempInfos.Count == 1)
348
                    {
349
                        _OPC_Infos.Add(tempInfos[0]);
350
                        workbook.Worksheets[0][rowIndex, 2].Value = tempInfos[0].FindDrawingName;
351
                        workbook.Worksheets[0][rowIndex, 3].Value = tempInfos[0].FindRepID;
352
                    }
353
                    else
354
                    {
355
                        foreach (var tempInfo in tempInfos)
356
                        {
357
                            workbook.Worksheets[0][rowIndex, 2].Value = tempInfo.FindDrawingName;
358
                            workbook.Worksheets[0][rowIndex, 3].Value = tempInfo.FindRepID;
359
                            if (tempInfos.IndexOf(tempInfo) != tempInfos.Count - 1)
360
                                rowIndex++;
361
                        }
362
                    }   
363
                }
364
                rowIndex++;
365 a80baec5 gaqhf
                foreach (var item in findOPCs)
366
                    ReleaseCOMObjects(item);
367
                ReleaseCOMObjects(currentOPC);
368
            }
369
370
            if (_OPC_Infos.Count > 0)
371
            {
372
                foreach (var item in _OPC_Infos)
373
                {
374
                    LMRepresentation removeRep = dataSource.GetRepresentation(item.CurrentRepID);
375
                    LMSymbol removeSymbol = dataSource.GetSymbol(item.CurrentRepID);
376
                    LMOPC removeOPC = dataSource.GetOPC(removeSymbol.ModelItemID);
377
378
                    LMRepresentation findRep = dataSource.GetRepresentation(item.FindRepID);
379
                    LMOPC findOPC = dataSource.GetOPC(findRep.ModelItemID);
380
                    LMOPC pairOPC = findOPC.pairedWithOPCObject;
381
382 4d4dce52 esham21
                    int mirror = removeSymbol.get_IsMirroredIndex();
383
                    double angle = Convert.ToDouble(removeSymbol.get_RotationAngle());
384
                    double x = removeSymbol.get_XCoordinate();
385
                    double y = removeSymbol.get_YCoordinate();
386 a80baec5 gaqhf
                    LMConnector connConnector = null;
387
                    
388
                    foreach (LMConnector connector in removeSymbol.Connect1Connectors)
389 4d4dce52 esham21
                        if (connector.get_ItemStatus() == "Active")
390 a80baec5 gaqhf
                            connConnector = connector;
391
                    if (connConnector == null)
392
                        foreach (LMConnector connector in removeSymbol.Connect2Connectors)
393 4d4dce52 esham21
                            if (connector.get_ItemStatus() == "Active")
394 a80baec5 gaqhf
                                connConnector = connector;
395
                    
396
                    if (connConnector != null)
397
                    {
398 4d4dce52 esham21
                        ZoomObjectByGraphicOID(removeRep.get_GraphicOID().ToString());
399 a80baec5 gaqhf
400
                        placement.PIDRemovePlacement(removeRep);
401
                        removeRep.Commit();
402
403 4d4dce52 esham21
                        LMSymbol newSymbol = placement.PIDPlaceSymbol(findRep.get_FileName(), x, y, mirror, angle, pairOPC.AsLMAItem(), connConnector);
404 a80baec5 gaqhf
                        newSymbol.Commit();
405
406
                        LMOPC newOPC = dataSource.GetOPC(newSymbol.ModelItemID);
407 4d4dce52 esham21
                        object descValue = removeOPC.get_Description();
408
                        object toFromValue = removeOPC.get_ToFromText();
409
                        newOPC.set_Description(descValue);
410
                        newOPC.set_ToFromText(toFromValue);
411 a80baec5 gaqhf
                        newOPC.Commit();
412
413
                        ReleaseCOMObjects(newSymbol);
414
                        ReleaseCOMObjects(newOPC);
415
                    }
416
417
                    ReleaseCOMObjects(findOPC);
418
                    ReleaseCOMObjects(pairOPC);
419
                    ReleaseCOMObjects(findRep);
420
                    ReleaseCOMObjects(removeOPC);
421
                    ReleaseCOMObjects(removeSymbol);
422
                    ReleaseCOMObjects(removeRep);
423
                    ReleaseCOMObjects(connConnector);
424
                }
425
426
                application.ActiveDocument.Save();
427
            }
428
429 7b262903 gaqhf
            workbook.EndUpdate();
430
            workbook.Worksheets[0].Columns.AutoFit(0, 3);
431
            SaveFileDialog save = new SaveFileDialog();
432
            save.Filter = "excel file (*.xlsx)|*.xlsx";
433
            if (save.ShowDialog() == DialogResult.OK)
434
            {
435
                workbook.SaveDocument(save.FileName, DevExpress.Spreadsheet.DocumentFormat.Xlsx);
436
            }
437
            workbook.Dispose();
438 a80baec5 gaqhf
439
            foreach (var item in CurrentDrawing)
440
                ReleaseCOMObjects(item);
441
            foreach (var item in OtherDrawing)
442
                ReleaseCOMObjects(item);
443
444
            ReleaseCOMObjects(representations);
445
446
            ReleaseCOMObjects(filter2);
447
            ReleaseCOMObjects(criterion2);
448
            ReleaseCOMObjects(drawing);
449
            ReleaseCOMObjects(drawings);
450
            ReleaseCOMObjects(filter);
451
            ReleaseCOMObjects(criterion);
452
453
            ReleaseCOMObjects(dataSource);
454
            ReleaseCOMObjects(placement);
455
456
            return;
457
458
            //DataTable tOPCInfo = Project_DB.SelectOPCInfo();
459
            //DataTable tOPCRelations = Project_DB.SelectOPCRelations();
460
            //DataTable tDrawingInfo = Project_DB.SelectDrawingInfo();
461
            //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
462
463
            //foreach (DataRow row in tOPCInfo.Rows)
464
            //{
465
            //    if (!Convert.ToBoolean(row["PAIRED"]))
466
            //    {
467
            //        string drawingUID = row["ID2_DRAWING_UID"].ToString();
468
            //        string OPCUID = row["ID2_OPC_UID"].ToString();
469
            //        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));
470
471
            //        if (rows.Length == 2)
472
            //        {
473
            //            string fromDrawingsUID1 = rows[0]["From_Drawings_UID"].ToString();
474
            //            string fromDrawingsUID2 = rows[1]["From_Drawings_UID"].ToString();
475
            //            string fromOPCUID1 = rows[0]["From_OPC_UID"].ToString();
476
            //            string fromOPCUID2 = rows[1]["From_OPC_UID"].ToString();
477
            //            string toDrawingsUID1 = rows[0]["To_Drawings_UID"].ToString();
478
            //            string toDrawingsUID2 = rows[1]["To_Drawings_UID"].ToString();
479
            //            string toOPCUID1 = rows[0]["To_OPC_UID"].ToString();
480
            //            string toOPCUID2 = rows[1]["To_OPC_UID"].ToString();
481
482
            //            DataRow[] fromDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", fromDrawingsUID1));
483
            //            DataRow[] toDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", toDrawingsUID1));
484
            //            DataRow[] fromOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", fromOPCUID1));
485
            //            DataRow[] toOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", toOPCUID1));
486
487
            //            if (fromOPCUID1 == toOPCUID2 && fromOPCUID2 == toOPCUID1 && fromDrawing.Length == 1 && toDrawing.Length == 1 && fromOPCInfoRows.Length == 1 && toOPCInfoRows.Length == 1)
488
            //            {
489
            //                DataRow fromOPCInfoRow = fromOPCInfoRows[0];
490
            //                DataRow toOPCInfoRow = toOPCInfoRows[0];
491
            //                string fromOPCModelId = fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
492
            //                string toOPCModelId = toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
493
            //                string toDrawingName = toDrawing[0]["DRAWINGNAME"].ToString();
494
            //                List<string[]> toOPCAttributes = JsonConvert.DeserializeObject<List<string[]>>(toOPCInfoRow["ATTRIBUTES"].ToString());
495
            //                AutoModeling_OPC opc = new AutoModeling_OPC(dApplication, application, fromOPCModelId, toOPCModelId, toDrawingName, toOPCAttributes);
496
            //                if (opc.Run())
497
            //                {
498
            //                    fromOPCInfoRow["PAIRED"] = true;
499
            //                    toOPCInfoRow["PAIRED"] = true;
500
501
            //                    Project_DB.InsertOPCInfo(fromOPCInfoRow["ID2_OPC_UID"].ToString(), fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), fromOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
502
            //                    Project_DB.InsertOPCInfo(toOPCInfoRow["ID2_OPC_UID"].ToString(), toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), toOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
503
            //                }
504
            //            }
505
            //        }
506
            //    }
507
            //}
508
509
            //tOPCInfo.Dispose();
510
            //tOPCRelations.Dispose();
511
            //tDrawingInfo.Dispose();
512
            //ReleaseCOMObjects(dApplication);
513
            //dApplication = null;
514 58993ba9 gaqhf
515 a80baec5 gaqhf
            //MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
516 69b7387a gaqhf
        }
517
518 4fb0f8d5 gaqhf
        public void ReleaseCOMObjects(params object[] objVars)
519
        {
520
            int intNewRefCount = 0;
521
            foreach (object obj in objVars)
522
            {
523
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
524
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
525
            }
526
        }
527
528 ea54adc8 gaqhf
        
529
530 82c7f66d gaqhf
        #region SPPID Utils
531 5a47c65c gaqhf
532 29ac1959 gaqhf
        #region Symmetry
533 82c7f66d gaqhf
        private void btnSymmetry_Click(object sender, EventArgs e)
534
        {
535 c3e8fa8e gaqhf
            if (application.ActiveSelectSet.Count == 1 && application.ActiveSelectSet[0].GetType() == typeof(Symbol2d))
536
            {
537
                int symCount = (int)spinEditSymmetry.Value;
538
                Symbol2d symbol = application.ActiveSelectSet[0] as Symbol2d;
539
                double x, y;
540
                symbol.GetOrigin(out x, out y);
541
                string rep = GetRepresentationId(symbol);
542
                List<string> verticalRepID = new List<string>();
543
                List<string> horizontalRepID = new List<string>();
544
545
                if ((symbol.LinearName.Contains("Piping") ||
546
                    symbol.LinearName.Contains("Instrument")) &&
547
                    !string.IsNullOrEmpty(rep))
548
                {
549
                    Placement placement = new Placement();
550
                    LMADataSource dataSource = placement.PIDDataSource;
551
                    List<List<string>> datas = SetSymbol(dataSource, rep, x, y, symCount);
552
                    Dictionary<SymmetryArrow, List<string>> resultDatas = new Dictionary<SymmetryArrow, List<string>>();
553
                    if (datas.Count >= 2 && datas.Find(loop => loop.Count != symCount) == null)
554
                    {
555
                        SymmetryArrow arrow = SymmetryArrow.None;
556
                        foreach (var data in datas)
557
                        {
558
                            LMSymbol firstSymbol = dataSource.GetSymbol(data[0]);
559 4d4dce52 esham21
                            double fX = firstSymbol.get_XCoordinate();
560
                            double fY = firstSymbol.get_YCoordinate();
561 c3e8fa8e gaqhf
562
                            SlopeType type = SPPIDUtil.CalcSlope(x, y, fX, fY, 1);
563
                            if (type == SlopeType.HORIZONTAL)
564
                            {
565
                                if (fX < x)
566
                                {
567
                                    arrow |= SymmetryArrow.Left;
568
                                    resultDatas.Add(SymmetryArrow.Left, data);
569
                                }
570
                                else
571
                                {
572
                                    arrow |= SymmetryArrow.Right;
573
                                    resultDatas.Add(SymmetryArrow.Right, data);
574
                                }
575
576
                            }
577
                            else if (type == SlopeType.VERTICAL)
578
                            {
579
                                if (fY < y)
580
                                {
581
                                    arrow |= SymmetryArrow.Down;
582
                                    resultDatas.Add(SymmetryArrow.Down, data);
583
                                }
584
                                else
585
                                {
586
                                    arrow |= SymmetryArrow.Up;
587
                                    resultDatas.Add(SymmetryArrow.Up, data);
588
                                }
589
                            }
590
591
                            ReleaseCOMObjects(firstSymbol);
592
                        }
593
594
                        SymmetryForm form = new SymmetryForm(arrow);
595
                        if (form.ShowDialog() == DialogResult.OK)
596
                        {
597
                            MoveByResult(dataSource, resultDatas, x, y, form.Result, rep);
598
                        }
599
                    }
600
                    else
601
                        MessageBox.Show("Check Symmetry Rules", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
602
                    ReleaseCOMObjects(dataSource);
603
                    ReleaseCOMObjects(placement);
604
                }
605
            }
606
607
            return;
608
609
            List<Symbol2d> symbols = new List<Symbol2d>();
610
            foreach (var item in application.ActiveSelectSet)
611
            {
612
                Type type = item.GetType();
613
                if (type == typeof(Symbol2d))
614
                {
615
                    Symbol2d symbol = item as Symbol2d;
616
                    if (symbol.LinearName.Contains("Piping") ||
617
                        symbol.LinearName.Contains("Instrument"))
618
                        symbols.Add(symbol);
619
                }
620
                //if (item.GetType() == typeof(Symbol2d) || item.GetType() == typeof(Point2d))
621
                //    items.Add(item);
622
            }
623
624
            List<Symbol2d> verticalSymbols = new List<Symbol2d>();
625
            List<Symbol2d> horizontalSymbols = new List<Symbol2d>();
626
            Symbol2d mainSymbol = null;
627
            for (int i = 0; i < symbols.Count - 1; i++)
628
            {
629
                Symbol2d symbol1 = symbols[i];
630
                for (int j = 1; j < symbols.Count; j++)
631
                {
632
                    Symbol2d symbol2 = symbols[j];
633
634
                    if (symbol1 != symbol2)
635
                    {
636
                        double x1, y1, x2, y2;
637
                        symbol1.GetOrigin(out x1, out y1);
638
                        symbol2.GetOrigin(out x2, out y2);
639
                        SlopeType slopeType = SPPIDUtil.CalcSlope(x1, y1, x2, y2, 1);
640
                        if (slopeType == SlopeType.HORIZONTAL)
641
                        {
642
                            if (!horizontalSymbols.Contains(symbol1))
643
                                horizontalSymbols.Add(symbol1);
644
                            if (!horizontalSymbols.Contains(symbol2))
645
                                horizontalSymbols.Add(symbol2);
646
647
                            if (verticalSymbols.Contains(symbol1))
648
                                mainSymbol = symbol1;
649
                            if (verticalSymbols.Contains(symbol2))
650
                                mainSymbol = symbol2;
651
                        }
652
                        else if (slopeType == SlopeType.VERTICAL)
653
                        {
654
                            if (!verticalSymbols.Contains(symbol1))
655
                                verticalSymbols.Add(symbol1);
656
                            if (!verticalSymbols.Contains(symbol2))
657
                                verticalSymbols.Add(symbol2);
658
659
                            if (horizontalSymbols.Contains(symbol1))
660
                                mainSymbol = symbol1;
661
                            if (horizontalSymbols.Contains(symbol2))
662
                                mainSymbol = symbol2;
663
                        }
664
                    }
665
                }
666
            }
667
668
            application.ActiveSelectSet.RemoveAll();
669
            foreach (var item in verticalSymbols)
670
                application.ActiveSelectSet.Add(item);
671
            foreach (var item in horizontalSymbols)
672
                application.ActiveSelectSet.Add(item);
673
                
674
675
            if (MessageBox.Show("Continue?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
676
            {
677
                application.ActiveSelectSet.RemoveAll();
678
679
            }
680
        }
681
        private void MoveByResult(LMADataSource dataSource,Dictionary<SymmetryArrow, List<string>> resultDatas, double x, double y, SymmetryArrow arrow, string centerRepID)
682
        {
683
            List<string> datas = resultDatas[arrow];
684
            foreach (var item in resultDatas)
685
            {
686
                if (item.Key != arrow)
687
                {
688
                    for (int i = 0; i < item.Value.Count; i++)
689
                    {
690
                        Symbol2d moveSymbol2d = GetSymbol2DByRepID(dataSource, item.Value[i]);
691
                        Symbol2d symbol2d = GetSymbol2DByRepID(dataSource, datas[i]);
692
693
                        double x1, y1, x2, y2;
694
                        symbol2d.GetOrigin(out x1, out y1);
695
                        moveSymbol2d.GetOrigin(out x2, out y2);
696
                        double distance = SPPIDUtil.CalcPointToPointdDistance(x, y, x1, y1);
697
698 77d6e710 gaqhf
                        string symbol1RepID;
699
                        string symbol2RepID;
700 c3e8fa8e gaqhf
                        List<Point2d> point2ds;
701
                        if (i == 0)
702 77d6e710 gaqhf
                        {
703
                            symbol1RepID = centerRepID;
704
                            symbol2RepID = item.Value[i];
705
                        }
706 c3e8fa8e gaqhf
                        else
707 77d6e710 gaqhf
                        {
708
                            symbol1RepID = item.Value[i - 1];
709
                            symbol2RepID = item.Value[i];
710
                        }
711
                        point2ds = FindAllPoint2d(dataSource, symbol1RepID, symbol2RepID);
712 c3e8fa8e gaqhf
                        double moveX = 0;
713
                        double moveY = 0;
714
715
                        if (item.Key == SymmetryArrow.Left)
716
                            moveX = x - distance - x2;
717
                        else if (item.Key == SymmetryArrow.Right)
718
                            moveX = x + distance - x2;
719
                        else if (item.Key == SymmetryArrow.Down)
720
                            moveY = y - distance - y2;
721
                        else if (item.Key == SymmetryArrow.Up)
722
                            moveY = y + distance - y2;
723
724
                        moveSymbol2d.Move(0, 0, moveX, moveY);
725
                        MovePoint2d(dataSource, item.Value[i], arrow, moveX, moveY);
726
                        foreach (var point in point2ds)
727 77d6e710 gaqhf
                        {
728
                            LMSymbol branch = dataSource.GetSymbol(GetRepresentationId(point));
729
                            LMSymbol connSymbol = GetFirstSymbolBySlope(dataSource, branch, symbol1RepID, symbol2RepID);
730 c3e8fa8e gaqhf
                            point.Move(0, 0, moveX, moveY);
731 77d6e710 gaqhf
                            if (connSymbol != null)
732
                            {
733
                                Symbol2d connSymbol2d = GetSymbol2DByRepID(dataSource, connSymbol.AsLMRepresentation().Id);
734
                                connSymbol2d.Move(0, 0, moveX, moveY);
735
                                ReleaseCOMObjects(connSymbol);
736
                            }
737
                            ReleaseCOMObjects(branch);
738
                        }
739
                            
740 c3e8fa8e gaqhf
                    }
741
                }
742
            }
743
        }
744 77d6e710 gaqhf
        private LMSymbol GetFirstSymbolBySlope(LMADataSource dataSource, LMSymbol branch, string symbol1RepID, string symbol2RepID)
745
        {
746
            LMSymbol result = null;
747
            foreach (LMConnector connector in branch.Connect1Connectors)
748
            {
749
                if (connector.ConnectItem1SymbolObject != null && 
750 4d4dce52 esham21
                    connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
751
                    connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
752 77d6e710 gaqhf
                {
753
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
754
                    if (repID != symbol1RepID && repID != symbol2RepID)
755
                        result = connector.ConnectItem1SymbolObject;
756
                }
757
758
                if (connector.ConnectItem2SymbolObject != null &&
759 4d4dce52 esham21
                    connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
760
                    connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
761 77d6e710 gaqhf
                {
762
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
763
                    if (repID != symbol1RepID && repID != symbol2RepID)
764
                        result = connector.ConnectItem2SymbolObject;
765
                }
766
            }
767
768
            foreach (LMConnector connector in branch.Connect2Connectors)
769
            {
770
                if (connector.ConnectItem1SymbolObject != null &&
771 4d4dce52 esham21
                    connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
772
                    connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
773 77d6e710 gaqhf
                {
774
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
775
                    if (repID != symbol1RepID && repID != symbol2RepID)
776
                        result = connector.ConnectItem1SymbolObject;
777
                }
778
779
                if (connector.ConnectItem2SymbolObject != null &&
780 4d4dce52 esham21
                    connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
781
                    connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
782 77d6e710 gaqhf
                {
783
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
784
                    if (repID != symbol1RepID && repID != symbol2RepID)
785
                        result = connector.ConnectItem2SymbolObject;
786
                }
787
            }
788
789
790
            return result;
791
        }
792 c3e8fa8e gaqhf
        private string GetRepresentationId(Symbol2d symbol)
793
        {
794
            foreach (var attributes in symbol.AttributeSets)
795
            {
796
                foreach (var attribute in attributes)
797
                {
798
                    string name = attribute.Name;
799
                    object value = attribute.GetValue();
800
                    if (name == "DrawingID")
801
                    {
802
                        return value.ToString();  
803
                    }
804
                }
805
            }
806
            return null;
807
        }
808 77d6e710 gaqhf
        private string GetRepresentationId(Point2d point2d)
809
        {
810
            foreach (var attributes in point2d.AttributeSets)
811
            {
812
                foreach (var attribute in attributes)
813
                {
814
                    string name = attribute.Name;
815
                    object value = attribute.GetValue();
816
                    if (name == "DrawingID")
817
                    {
818
                        return value.ToString();
819
                    }
820
                }
821
            }
822
            return null;
823
        }
824 c3e8fa8e gaqhf
        private List<List<string>> SetSymbol(LMADataSource dataSource, string rep, double x, double y, int count)
825
        {
826
            LMSymbol _LMSymbol = dataSource.GetSymbol(rep);
827
            List<List<string>> result = new List<List<string>>();
828
            List<string> oldIDs = new List<string>() { rep };
829
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
830
            {
831 4d4dce52 esham21
                if (connector.get_ItemStatus() != "Active")
832 c3e8fa8e gaqhf
                    continue;
833
834
                string repID = connector.AsLMRepresentation().Id;
835 4d4dce52 esham21
                string status = connector.get_ItemStatus();
836 c3e8fa8e gaqhf
                if (status == "Active" && !oldIDs.Contains(repID))
837
                {
838
                    List<string> symbols = new List<string>();
839
                    oldIDs.Add(repID);
840
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
841
                    result.Add(symbols);
842
                }
843
            }
844
845
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
846
            {
847 4d4dce52 esham21
                if (connector.get_ItemStatus() != "Active")
848 c3e8fa8e gaqhf
                    continue;
849
850
                string repID = connector.AsLMRepresentation().Id;
851 4d4dce52 esham21
                string status = connector.get_ItemStatus();
852 c3e8fa8e gaqhf
                if (status == "Active" && !oldIDs.Contains(repID))
853
                {
854
                    List<string> symbols = new List<string>();
855
                    oldIDs.Add(repID);
856
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
857
                    result.Add(symbols);
858
                }
859
            }
860
861
            ReleaseCOMObjects(_LMSymbol);
862
            return result;
863
        }
864
        private void loop(LMADataSource dataSource, List<string> oldIDs, List<string> symbols, LMConnector connector, double x, double y, int count)
865
        {
866
            if (symbols.Count >= count)
867
                return;
868
869
            if (connector.ConnectItem1SymbolObject != null && !oldIDs.Contains(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id))
870
            {
871
                string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
872
                oldIDs.Add(repID);
873 4d4dce52 esham21
                if (connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch" && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
874 c3e8fa8e gaqhf
                {
875 4d4dce52 esham21
                    double sX = connector.ConnectItem1SymbolObject.get_XCoordinate(), sY = connector.ConnectItem1SymbolObject.get_YCoordinate(); ;
876 c3e8fa8e gaqhf
                    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, sX, sY, 1);
877
                    if (slopeType == SlopeType.HORIZONTAL || slopeType == SlopeType.VERTICAL)
878
                        symbols.Add(repID);
879
                }
880
881
                loop(dataSource, oldIDs, symbols, connector.ConnectItem1SymbolObject, x, y, count);
882
            }
883
884
            if (symbols.Count >= count)
885
                return;
886
887
            if (connector.ConnectItem2SymbolObject != null && !oldIDs.Contains(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id))
888
            {
889
                string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
890
                oldIDs.Add(repID);
891 4d4dce52 esham21
                if (connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch" && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
892 c3e8fa8e gaqhf
                {
893 4d4dce52 esham21
                    double sX = connector.ConnectItem2SymbolObject.get_XCoordinate(), sY = connector.ConnectItem2SymbolObject.get_YCoordinate(); ;
894 c3e8fa8e gaqhf
                    SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, sX, sY, 1);
895
                    if (slopeType == SlopeType.HORIZONTAL || slopeType == SlopeType.VERTICAL)
896
                        symbols.Add(repID);
897
                }
898
899
                loop(dataSource, oldIDs, symbols, connector.ConnectItem2SymbolObject, x, y, count);
900
            }
901
        }
902
        private void loop(LMADataSource dataSource, List<string> oldIDs, List<string> symbols, LMSymbol _LMSymbol, double x, double y, int count)
903
        {
904
            if (symbols.Count >= count)
905
                return;
906
907
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
908
            {
909 4d4dce52 esham21
                if (connector.get_ItemStatus() != "Active")
910 c3e8fa8e gaqhf
                    continue;
911
912
                string repID = connector.AsLMRepresentation().Id;
913 4d4dce52 esham21
                string status = connector.get_ItemStatus();
914 c3e8fa8e gaqhf
                if (status == "Active" && !oldIDs.Contains(repID))
915
                {
916
                    oldIDs.Add(repID);
917
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
918
                }
919
            }
920 82c7f66d gaqhf
921 c3e8fa8e gaqhf
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
922
            {
923 4d4dce52 esham21
                if (connector.get_ItemStatus() != "Active")
924 c3e8fa8e gaqhf
                    continue;
925
926
                string repID = connector.AsLMRepresentation().Id;
927 4d4dce52 esham21
                string status = connector.get_ItemStatus();
928 c3e8fa8e gaqhf
                if (status == "Active" && !oldIDs.Contains(repID))
929
                {
930
                    oldIDs.Add(repID);
931
                    loop(dataSource, oldIDs, symbols, connector, x, y, count);
932
                }
933
            }
934
        }
935
        private Symbol2d GetSymbol2DByRepID(LMADataSource dataSource, string repID)
936
        {
937
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
938 4d4dce52 esham21
            Symbol2d symbol2D = application.ActiveDocument.ActiveSheet.DrawingObjects[_LMSymbol.get_GraphicOID().ToString()];
939 c3e8fa8e gaqhf
            ReleaseCOMObjects(_LMSymbol);
940
            return symbol2D;
941 82c7f66d gaqhf
        }
942 c3e8fa8e gaqhf
        private void MovePoint2d(LMADataSource datasource, string repID, SymmetryArrow arrow, double moveX, double moveY)
943
        {
944
            LMSymbol _LMSymbol = datasource.GetSymbol(repID);
945
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
946
            {
947 4d4dce52 esham21
                if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()))
948 c3e8fa8e gaqhf
                {
949 4d4dce52 esham21
                    if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
950 c3e8fa8e gaqhf
                    {
951 4d4dce52 esham21
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()];
952 c3e8fa8e gaqhf
                        point.X += moveX;
953
                        point.Y += moveY;
954
                    }
955 4d4dce52 esham21
                    else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
956 c3e8fa8e gaqhf
                    {
957 4d4dce52 esham21
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()];
958 c3e8fa8e gaqhf
                        point.X += moveX;
959
                        point.Y += moveY;
960
                    }
961
                }
962
            }
963
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
964
            {
965 4d4dce52 esham21
                if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()))
966 c3e8fa8e gaqhf
                {
967 4d4dce52 esham21
                    if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
968 c3e8fa8e gaqhf
                    {
969 4d4dce52 esham21
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()];
970 c3e8fa8e gaqhf
                        point.X += moveX;
971
                        point.Y += moveY;
972
                    }
973 4d4dce52 esham21
                    else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
974 c3e8fa8e gaqhf
                    {
975 4d4dce52 esham21
                        Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()];
976 c3e8fa8e gaqhf
                        point.X += moveX;
977
                        point.Y += moveY;
978
                    }
979
                }
980
            }
981
            ReleaseCOMObjects(_LMSymbol);
982
        }
983
        private List<Point2d> FindAllPoint2d(LMADataSource dataSource, string repID, string nextRepID)
984
        {
985
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
986
            List<string> endIDs = new List<string>() { repID };
987
            List<string> graphicOIDs = new List<string>();
988
            List<Point2d> result = new List<Point2d>();
989
            FindPointsLoop(dataSource, _LMSymbol, endIDs, nextRepID, graphicOIDs);
990
            ReleaseCOMObjects(_LMSymbol);
991
            foreach (var item in graphicOIDs)
992
            {
993
                Point2d point = application.ActiveDocument.ActiveSheet.DrawingObjects[item] as Point2d;
994
                result.Add(point);
995
            }
996
            
997
            return result;
998
        }
999
        private bool FindPointsLoop(LMADataSource dataSource, LMSymbol _LMSymbol, List<string> endIDs, string targetRepID, List<string> graphicOIDs)
1000
        {
1001
            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
1002
            {
1003 4d4dce52 esham21
                if (connector.get_ItemStatus() != "Active")
1004 c3e8fa8e gaqhf
                    continue;
1005
1006 4d4dce52 esham21
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
1007 c3e8fa8e gaqhf
                {
1008
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
1009
                    if (!endIDs.Contains(repID))
1010
                    {
1011
                        endIDs.Add(repID);
1012 4d4dce52 esham21
                        if (connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
1013 c3e8fa8e gaqhf
                        {
1014
                            if (FindPointsLoop(dataSource, connector.ConnectItem1SymbolObject, endIDs, targetRepID, graphicOIDs))
1015
                            {
1016 4d4dce52 esham21
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1017
                                    graphicOIDs.Add(connector.ConnectItem1SymbolObject.get_GraphicOID().ToString());
1018 c3e8fa8e gaqhf
                                return true;
1019
                            }
1020
                        }
1021
                        else if (targetRepID == repID)
1022
                        {
1023
                            return true;
1024
                        }
1025
                    }
1026
                }
1027 4d4dce52 esham21
                if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
1028 c3e8fa8e gaqhf
                {
1029
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
1030
                    if (!endIDs.Contains(repID))
1031
                    {
1032
                        endIDs.Add(repID);
1033 4d4dce52 esham21
                        if (connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
1034 c3e8fa8e gaqhf
                        {
1035
                            if (FindPointsLoop(dataSource, connector.ConnectItem2SymbolObject, endIDs, targetRepID, graphicOIDs))
1036
                            {
1037 4d4dce52 esham21
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1038
                                    graphicOIDs.Add(connector.ConnectItem2SymbolObject.get_GraphicOID().ToString());
1039 c3e8fa8e gaqhf
                                return true;
1040
                            }
1041
                        }
1042
                        else if (targetRepID == repID)
1043
                        {
1044
                            return true;
1045
                        }
1046
                    }
1047
                }
1048
            }
1049
            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
1050
            {
1051 4d4dce52 esham21
                if (connector.get_ItemStatus() != "Active")
1052 c3e8fa8e gaqhf
                    continue;
1053
1054 4d4dce52 esham21
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
1055 c3e8fa8e gaqhf
                {
1056
                    string repID = connector.ConnectItem1SymbolObject.AsLMRepresentation().Id;
1057
                    if (!endIDs.Contains(repID))
1058
                    {
1059
                        endIDs.Add(repID);
1060 4d4dce52 esham21
                        if (connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
1061 c3e8fa8e gaqhf
                        {
1062
                            if (FindPointsLoop(dataSource, connector.ConnectItem1SymbolObject, endIDs, targetRepID, graphicOIDs))
1063
                            {
1064 4d4dce52 esham21
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1065
                                    graphicOIDs.Add(connector.ConnectItem1SymbolObject.get_GraphicOID().ToString());
1066 c3e8fa8e gaqhf
                                return true;
1067
                            }
1068
                        }
1069
                        else if (targetRepID == repID)
1070
                        {
1071
                            return true;
1072
                        }
1073
                    }
1074
                }
1075 4d4dce52 esham21
                if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
1076 c3e8fa8e gaqhf
                {
1077
                    string repID = connector.ConnectItem2SymbolObject.AsLMRepresentation().Id;
1078
                    if (!endIDs.Contains(repID))
1079
                    {
1080
                        endIDs.Add(repID);
1081 4d4dce52 esham21
                        if (connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
1082 c3e8fa8e gaqhf
                        {
1083
                            if (FindPointsLoop(dataSource, connector.ConnectItem2SymbolObject, endIDs, targetRepID, graphicOIDs))
1084
                            {
1085 4d4dce52 esham21
                                if (!Convert.ToBoolean(connector.get_IsZeroLength()))
1086
                                    graphicOIDs.Add(connector.ConnectItem2SymbolObject.get_GraphicOID().ToString());
1087 c3e8fa8e gaqhf
                                return true;
1088
                            }
1089
                        }
1090
                        else if (targetRepID == repID)
1091
                        {
1092
                            return true;
1093
                        }
1094
                    }
1095
                }
1096
            }
1097
1098
            return false;
1099
        }
1100 29ac1959 gaqhf
        #endregion
1101 5a47c65c gaqhf
1102 29ac1959 gaqhf
        #region SpecBreak
1103
        private void btnSpecBreakRelocation_Click(object sender, EventArgs e)
1104
        {
1105
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
1106 4d4dce52 esham21
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
1107
            application = wApp.RADApplication;
1108 29ac1959 gaqhf
1109 5b161eb1 Denny
            int count = application.ActiveSelectSet.Count;
1110
            int dependencyCount = 0;
1111
            foreach (var item in application.ActiveSelectSet)
1112
                if (item.GetType() == typeof(DependencyObject))
1113
                    dependencyCount++;
1114
1115
            if (count > 0 && application.ActiveSelectSet[0].GetType() == typeof(DependencyObject))
1116
            {
1117
                MessageBox.Show("First selected item is DependencyObject!\r\nPlease move symbol", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1118
                return;
1119
            }
1120
1121
            if ((count == 3 || count == 4) && application.ActiveSelectSet[0].GetType() == typeof(Symbol2d) &&
1122
                (dependencyCount == 2 || dependencyCount == 3))
1123 29ac1959 gaqhf
            {
1124 5b161eb1 Denny
                Symbol2d symbol = application.ActiveSelectSet[0] as Symbol2d;
1125
1126
                if (!symbol.DefinitionName.Contains(@"Design\Annotation\Graphics\"))
1127
                {
1128
                    MessageBox.Show("Select SpecBreak!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1129
                    return;
1130
                }
1131
1132
                DependencyObject dependency1 = application.ActiveSelectSet[1] as DependencyObject;
1133
                DependencyObject dependency2 = application.ActiveSelectSet[2] as DependencyObject;
1134
                DependencyObject dependency3 = null;
1135
                if (count == 4)
1136
                    dependency3 = application.ActiveSelectSet[3] as DependencyObject;
1137
1138
                application.ActiveSelectSet.RemoveAll();
1139
                double angle = symbol.Angle;
1140
                if (symbol.GetTransform().HasReflection)
1141
                    angle += Math.PI;
1142
1143
                double degree = double.NaN;
1144
                if (angle > -0.1 && angle < 0.1)
1145
                    degree = 0;
1146
                else if (angle > 1.56 && angle < 1.58)
1147
                    degree = 90;
1148
                else if (angle > 3.13 && angle < 3.15)
1149
                    degree = 180;
1150
                else if (angle > 4.7 && angle < 4.72)
1151
                    degree = 270;
1152
                else if (angle > 6.27 && angle < 6.29)
1153
                    degree = 0;
1154
                else if (angle > -1.58 && angle < -1.56)
1155
                    degree = 270;
1156
                else if (angle > -3.15 && angle < -3.13)
1157
                    degree = 180;
1158
                else
1159
                    throw new Exception("Check Angle");
1160
1161
1162
                double originX, originY;
1163
                symbol.GetOrigin(out originX, out originY);
1164
1165
                double crossX = 0;
1166
                double crossY = 0;
1167
                double topX = 0;
1168
                double topY = 0;
1169
                foreach (var item in symbol.DrawingObjects)
1170
                {
1171
                    Line2d line2d = item as Line2d;
1172
                    if (line2d != null)
1173
                    {
1174
                        double x1, y1, x2, y2;
1175
                        line2d.GetStartPoint(out x1, out y1);
1176
                        line2d.GetEndPoint(out x2, out y2);
1177
                        SlopeType slopeType = SPPIDUtil.CalcSlope(x1, y1, x2, y2, 0.1);
1178
1179
                        if (slopeType == SlopeType.HORIZONTAL)
1180
                        {
1181
                            if (crossY == 0)
1182
                                crossY = y1;
1183
                            else
1184
                                crossY = (crossY + y1) / 2;
1185
1186
                            switch (degree)
1187
                            {
1188
                                case 90:
1189
                                    if (topX == 0)
1190
                                        topX = Math.Min(x1, x2);
1191
                                    else
1192
                                        topX = Math.Min(topX, Math.Min(x1, x2));
1193
                                    break;
1194
                                case 270:
1195
                                    if (topX == 0)
1196
                                        topX = Math.Max(x1, x2);
1197
                                    else
1198
                                        topX = Math.Max(topX, Math.Max(x1, x2));
1199
                                    break;
1200
                                default:
1201
                                    break;
1202
                            }
1203
                        }
1204
                        else if (slopeType == SlopeType.VERTICAL)
1205
                        {
1206
                            if (crossX == 0)
1207
                                crossX = x1;
1208
                            else
1209
                                crossX = (crossX + x1) / 2;
1210
1211
                            switch (degree)
1212
                            {
1213
                                case 0:
1214
                                    if (topY == 0)
1215
                                        topY = Math.Max(y1, y2);
1216
                                    else
1217
                                        topY = Math.Max(topY, Math.Max(y1, y2));
1218
                                    break;
1219
                                case 180:
1220
                                    if (topY == 0)
1221
                                        topY = Math.Min(y1, y2);
1222
                                    else
1223
                                        topY = Math.Min(topY, Math.Min(y1, y2));
1224
                                    break;
1225
                                default:
1226
                                    break;
1227
                            }
1228
                        }
1229
                    }
1230
                }
1231
                switch (degree)
1232
                {
1233
                    case 0:
1234
                        crossX = originX;
1235
                        topX = crossX;
1236
                        break;
1237
                    case 90:
1238
                        crossY = originY;
1239
                        topY = crossY;
1240
                        break;
1241
                    case 180:
1242
                        crossX = originX;
1243
                        topX = crossX;
1244
                        break;
1245
                    case 270:
1246
                        crossY = originY;
1247
                        topY = crossY;
1248
                        break;
1249
                    default:
1250
                        break;
1251
                }
1252
1253
                SpecBreakRelocation(degree, originX, originY, crossX, crossY, topX, topY, dependency1, dependency2, dependency3);
1254
                SetSpecBreakParameters(symbol, dependency1, dependency2, degree);
1255 29ac1959 gaqhf
            }
1256 5b161eb1 Denny
            else
1257 29ac1959 gaqhf
            {
1258 5b161eb1 Denny
                MessageBox.Show("Check Rule!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
1259 29ac1959 gaqhf
            }
1260
        }
1261 4d4dce52 esham21
        private void SetSpecBreakParameters(Symbol2d symbol, DependencyObject dependency1, DependencyObject dependency2, double degree)
1262 518c7651 gaqhf
        {
1263
            bool mirror = symbol.GetTransform().HasReflection;
1264
            string repID = GetRepresentationId(symbol);
1265
            Placement placement = new Placement();
1266
            LMADataSource dataSource = placement.PIDDataSource;
1267
1268
            LMSymbol _LMSymbol = dataSource.GetSymbol(repID);
1269
            if (_LMSymbol != null)
1270
            {
1271
                double sX1, sY1, sX2, sY2, sOriginX, sOriginY;
1272
                symbol.Range(out sX1, out sY1, out sX2, out sY2);
1273
                symbol.GetOrigin(out sOriginX, out sOriginY);
1274
                double d1X1, d1Y1, d1X2, d1Y2, d2X1, d2Y1, d2X2, d2Y2;
1275
                FindRangeWithOutLineString2d(dependency1, out d1X1, out d1Y1, out d1X2, out d1Y2);
1276
                FindRangeWithOutLineString2d(dependency2, out d2X1, out d2Y1, out d2X2, out d2Y2);
1277
1278
                double left = 0, right = 0;
1279
                switch (degree)
1280
                {
1281
                    case 0:
1282
                        if (!mirror)
1283
                        {
1284
                            left = Math.Abs(d1X1 - sOriginX);
1285
                            right = Math.Abs(d2X2 - sOriginX);
1286
                        }
1287
                        else
1288
                        {
1289
                            right = Math.Abs(d1X1 - sOriginX);
1290
                            left = Math.Abs(d2X2 - sOriginX);
1291
                        }
1292
                        break;
1293
                    case 90:
1294
                        if (!mirror)
1295
                        {
1296
                            left = Math.Abs(d1Y1 - sOriginY);
1297
                            right = Math.Abs(d2Y2 - sOriginY);
1298
                        }
1299
                        else
1300
                        {
1301
                            right = Math.Abs(d1Y1 - sOriginY);
1302
                            left = Math.Abs(d2Y2 - sOriginY);
1303
                        }
1304
                        break;
1305
                    case 180:
1306
                        if (!mirror)
1307
                        {
1308
                            right = Math.Abs(d1X1 - sOriginX);
1309
                            left = Math.Abs(d2X2 - sOriginX);
1310
                        }
1311
                        else
1312
                        {
1313
                            left = Math.Abs(d1X1 - sOriginX);
1314
                            right = Math.Abs(d2X2 - sOriginX);
1315
                            
1316
                        }
1317
                        break;
1318
                    case 270:
1319
                        if (!mirror)
1320
                        {
1321
                            right = Math.Abs(d1Y1 - sOriginY);
1322
                            left = Math.Abs(d2Y2 - sOriginY);
1323
                        }
1324
                        else
1325
                        {
1326
                            left = Math.Abs(d1Y1 - sOriginY);
1327
                            right = Math.Abs(d2Y2 - sOriginY);
1328
                        }
1329
                        break;
1330
                    default:
1331
                        break;
1332
                }
1333
1334
                string[] array1 = new string[] { "", "Left", "Right" };
1335
                string[] array2 = new string[] { "", left.ToString(), right.ToString() };
1336
                placement.PIDApplyParameters(_LMSymbol.AsLMRepresentation(), array1, array2);
1337
            }
1338
1339
            ReleaseCOMObjects(_LMSymbol);
1340
            ReleaseCOMObjects(dataSource);
1341
            ReleaseCOMObjects(placement);
1342
        }
1343 4d4dce52 esham21
        private void SpecBreakRelocation(double degree, double originX, double originY, double crossX, double crossY, double topX, double topY, DependencyObject dependency1, DependencyObject dependency2, DependencyObject dependency3)
1344 29ac1959 gaqhf
        {
1345 519902b7 gaqhf
            double d1X1, d1Y1, d1X2, d1Y2, d2X1, d2Y1, d2X2, d2Y2, d3X1 = 0, d3Y1 = 0, d3X2 = 0, d3Y2 = 0;
1346 30a774f6 gaqhf
            
1347 54311b41 gaqhf
            FindRangeWithOutLineString2d(dependency1, out d1X1, out d1Y1, out d1X2, out d1Y2);
1348
            FindRangeWithOutLineString2d(dependency2, out d2X1, out d2Y1, out d2X2, out d2Y2);
1349 519902b7 gaqhf
            if (dependency3 != null)
1350 cf520d0d gaqhf
                FindRangeWithOutLineString2dAndTextBox(dependency3, out d3X1, out d3Y1, out d3X2, out d3Y2);
1351 29ac1959 gaqhf
1352 25c5df17 gaqhf
            GridSetting gridSetting = GridSetting.GetInstance();
1353
            double move = gridSetting.Length / 2;
1354 29ac1959 gaqhf
            switch (degree)
1355
            {
1356
                case 0:
1357 4d4dce52 esham21
                    MoveDependency(dependency1, d1X2, d1Y2, crossX - move, crossY);
1358
                    MoveDependency(dependency2, d2X1, d2Y2, crossX + move, crossY);
1359 519902b7 gaqhf
                    if (dependency3 != null)
1360 4d4dce52 esham21
                        MoveDependency(dependency3, (d3X1 + d3X2) / 2, d3Y1, topX, topY);
1361 29ac1959 gaqhf
                    break;
1362
                case 90:
1363 4d4dce52 esham21
                    MoveDependency(dependency1, d1X1, d1Y2, crossX + move, crossY);
1364
                    MoveDependency(dependency2, d2X1, d2Y1, crossX + move, crossY);
1365 519902b7 gaqhf
                    if (dependency3 != null)
1366 4d4dce52 esham21
                        MoveDependency(dependency3, d3X1, (d3Y1 + d3Y2) / 2, originX, originY);
1367 29ac1959 gaqhf
                    break;
1368
                case 180:
1369 4d4dce52 esham21
                    MoveDependency(dependency1, d1X2, d1Y1, crossX - move, crossY);
1370
                    MoveDependency(dependency2, d2X1, d2Y1, crossX + move, crossY);
1371 519902b7 gaqhf
                    if (dependency3 != null)
1372 4d4dce52 esham21
                        MoveDependency(dependency3, (d3X1 + d3X2) / 2, d3Y2, topX, topY);
1373 29ac1959 gaqhf
                    break;
1374
                case 270:
1375 4d4dce52 esham21
                    MoveDependency(dependency1, d1X2, d1Y2, crossX - move, crossY);
1376
                    MoveDependency(dependency2, d2X2, d2Y1, crossX - move, crossY);
1377 519902b7 gaqhf
                    if (dependency3 != null)
1378 4d4dce52 esham21
                        MoveDependency(dependency3, d3X2, (d3Y1 + d3Y2) / 2, originX, originY);
1379 29ac1959 gaqhf
                    break;
1380
                default:
1381
                    break;
1382
            }
1383
        }
1384 4d4dce52 esham21
        private void MoveDependency(DependencyObject dependency, double xFrom, double yFrom, double xTo, double yTo)
1385 6a659fd9 gaqhf
        {
1386 54311b41 gaqhf
            application.ActiveSelectSet.Add(dependency);
1387
1388
            Transform transform = dependency.GetTransform();
1389
            transform.DefineByMove2d(xTo - xFrom, yTo - yFrom);
1390
            application.ActiveSelectSet.Transform(transform, false);
1391
1392
            application.ActiveSelectSet.RemoveAll();
1393 6a659fd9 gaqhf
        }
1394 4d4dce52 esham21
        private string GetDrawingItemType(DependencyObject dependency)
1395
        {
1396
            string result = string.Empty;
1397 29ac1959 gaqhf
1398 4d4dce52 esham21
            foreach (var attributes in dependency.AttributeSets)
1399
            {
1400
                foreach (var attribute in attributes)
1401
                {
1402
                    if (attribute.Name == "DrawingItemType")
1403
                        return attribute.GetValue().ToString();
1404
                }
1405
            }
1406
1407
            return result;
1408
        }
1409
        private void FindRangeWithOutLineString2d(DependencyObject dependency, out double x1, out double y1, out double x2, out double y2)
1410 29ac1959 gaqhf
        {
1411 54311b41 gaqhf
            x1 = double.MaxValue;
1412
            y1 = double.MaxValue;
1413
            x2 = double.MinValue;
1414
            y2 = double.MinValue;
1415 4d4dce52 esham21
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1416 518c7651 gaqhf
            {
1417
                if (item.GetType() != typeof(LineString2d))
1418
                {
1419
                    double minX, minY, maxX, maxY;
1420
                    item.Range(out minX, out minY, out maxX, out maxY);
1421
                    if (x1 > minX)
1422
                        x1 = minX;
1423
                    if (y1 > minY)
1424
                        y1 = minY;
1425
                    if (x2 < maxX)
1426
                        x2 = maxX;
1427
                    if (y2 < maxY)
1428
                        y2 = maxY;
1429
                }
1430
            }
1431
1432
        }
1433 4d4dce52 esham21
        private void FindWidthHeightWidthOutLineString2d(DependencyObject dependency, out double width, out double height)
1434
        {
1435
            width = 0;
1436
            height = 0;
1437
1438
            double x1 = double.MaxValue;
1439
            double y1 = double.MaxValue;
1440
            double x2 = double.MinValue;
1441
            double y2 = double.MinValue;
1442
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1443
            {
1444
                if (item.GetType() != typeof(LineString2d))
1445
                {
1446
                    double minX, minY, maxX, maxY;
1447
                    item.Range(out minX, out minY, out maxX, out maxY);
1448
                    if (x1 > minX)
1449
                        x1 = minX;
1450
                    if (y1 > minY)
1451
                        y1 = minY;
1452
                    if (x2 < maxX)
1453
                        x2 = maxX;
1454
                    if (y2 < maxY)
1455
                        y2 = maxY;
1456
                }
1457
            }
1458
1459
            width = x2 - x1;
1460
            height = y2 - y1;
1461
        }
1462
        private void FindRangeWithOutLineString2dAndTextBox(DependencyObject dependency, out double x1, out double y1, out double x2, out double y2)
1463 cf520d0d gaqhf
        {
1464
            x1 = double.MaxValue;
1465
            y1 = double.MaxValue;
1466
            x2 = double.MinValue;
1467
            y2 = double.MinValue;
1468 4d4dce52 esham21
            foreach (DrawingObjectBase item in dependency.DrawingObjects)
1469 cf520d0d gaqhf
            {
1470
                if (item.GetType() != typeof(LineString2d) && item.GetType() != typeof(Ingr.RAD2D.TextBox))
1471
                {
1472
                    double minX, minY, maxX, maxY;
1473
                    item.Range(out minX, out minY, out maxX, out maxY);
1474
                    if (x1 > minX)
1475
                        x1 = minX;
1476
                    if (y1 > minY)
1477
                        y1 = minY;
1478 54311b41 gaqhf
                    if (x2 < maxX)
1479
                        x2 = maxX;
1480
                    if (y2 < maxY)
1481
                        y2 = maxY;
1482 29ac1959 gaqhf
                }
1483
            }
1484
1485
        }
1486
        #endregion
1487 5a47c65c gaqhf
1488 1b09fe17 gaqhf
        #region Hot Key
1489 5a47c65c gaqhf
        private void toggleSwitchSnapGrid_Toggled(object sender, EventArgs e)
1490
        {
1491
            if (toggleSwitchSnapGrid.IsOn)
1492
            {
1493
                RegisterHotKey(this.Handle, 0, (int)KeyModifier.Shift, Keys.A.GetHashCode());
1494
            }
1495
            else
1496
            {
1497
                UnregisterHotKey(this.Handle, 0);
1498
            }
1499
        }
1500
        private void toggleSwitchMoveSymbol_Toggled(object sender, EventArgs e)
1501
        {
1502
            if (toggleSwitchMoveSymbol.IsOn)
1503
            {
1504
                RegisterHotKey(this.Handle, 1, (int)KeyModifier.Shift, Keys.Left.GetHashCode());
1505
                RegisterHotKey(this.Handle, 2, (int)KeyModifier.Shift, Keys.Up.GetHashCode());
1506
                RegisterHotKey(this.Handle, 3, (int)KeyModifier.Shift, Keys.Right.GetHashCode());
1507
                RegisterHotKey(this.Handle, 4, (int)KeyModifier.Shift, Keys.Down.GetHashCode());
1508
            }
1509
            else
1510
            {
1511
                UnregisterHotKey(this.Handle, 1);
1512
                UnregisterHotKey(this.Handle, 2);
1513
                UnregisterHotKey(this.Handle, 3);
1514
                UnregisterHotKey(this.Handle, 4);
1515
            }
1516
        }
1517
        public void ClearHotKey()
1518
        {
1519
            if (toggleSwitchMoveSymbol.IsOn)
1520
            {
1521
                UnregisterHotKey(this.Handle, 1);
1522
                UnregisterHotKey(this.Handle, 2);
1523
                UnregisterHotKey(this.Handle, 3);
1524
                UnregisterHotKey(this.Handle, 4);
1525
            }
1526
            if (toggleSwitchSnapGrid.IsOn)
1527
            {
1528
                UnregisterHotKey(this.Handle, 0);
1529
            }
1530
        }
1531 70b0b5d7 gaqhf
        [System.Runtime.InteropServices.DllImport("user32.dll")]
1532
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
1533
        [System.Runtime.InteropServices.DllImport("user32.dll")]
1534
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
1535
        enum KeyModifier
1536
        {
1537
            None = 0,
1538
            Alt = 1,
1539
            Control = 2,
1540
            Shift = 4,
1541
            WinKey = 8
1542
        }
1543
        protected override void WndProc(ref Message m)
1544
        {
1545
            base.WndProc(ref m);
1546
            if (m.Msg == 0x0312)
1547
            {
1548
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
1549
                KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF);
1550
                int id = m.WParam.ToInt32();
1551 5a47c65c gaqhf
                switch (id)
1552
                {
1553
                    case 0:
1554
                        application.RunCommand(CommandConstants.igcmdGridSnap);
1555
                        break;
1556
                    case 1:
1557
                        MoveSymbol(Arrow.Left);
1558
                        break;
1559
                    case 2:
1560
                        MoveSymbol(Arrow.Up);
1561
                        break;
1562
                    case 3:
1563
                        MoveSymbol(Arrow.Right);
1564
                        break;
1565
                    case 4:
1566
                        MoveSymbol(Arrow.Down);
1567
                        break;
1568
                    default:
1569
                        break;
1570
                }
1571
                
1572 70b0b5d7 gaqhf
            }
1573
1574
        }
1575 1b09fe17 gaqhf
        #endregion
1576 5a47c65c gaqhf
1577
        #region Move Symbol
1578
        enum Arrow
1579
        {
1580
            Left,
1581
            Up,
1582
            Right,
1583
            Down
1584
        }
1585
        private void MoveSymbol(Arrow arrow)
1586
        {
1587
            if (application.ActiveSelectSet.Count > 0)
1588
            {
1589
                Placement placement = new Placement();
1590
                LMADataSource dataSource = placement.PIDDataSource;
1591 f250a747 gaqhf
                System.Collections.ObjectModel.Collection<DrawingObjectBase> originalDrawingObjectBases = new System.Collections.ObjectModel.Collection<DrawingObjectBase>();
1592
                foreach (var item in application.ActiveSelectSet)
1593
                    originalDrawingObjectBases.Add(item);
1594 5a47c65c gaqhf
                System.Collections.ObjectModel.Collection<DrawingObjectBase> drawingObjectBases = new System.Collections.ObjectModel.Collection<DrawingObjectBase>();
1595
                Transform transform = null;
1596
                foreach (DrawingObjectBase drawingObject in application.ActiveSelectSet)
1597
                {
1598 901feb22 gaqhf
                    if (drawingObject.GetType() == typeof(Symbol2d))
1599 5a47c65c gaqhf
                    {
1600 901feb22 gaqhf
                        Symbol2d symbol2D = drawingObject as Symbol2d;
1601 5a47c65c gaqhf
                        if (transform == null)
1602
                            transform = symbol2D.GetTransform();
1603
                        drawingObjectBases.Add(symbol2D);
1604
                        LMSymbol _LMSymbol = dataSource.GetSymbol(GetRepresentationId(symbol2D));
1605
                        if (_LMSymbol != null)
1606
                        {
1607
                            foreach (LMConnector connector in _LMSymbol.Connect1Connectors)
1608
                            {
1609 4d4dce52 esham21
                                if (connector.get_ItemStatus() == "Active")
1610 5a47c65c gaqhf
                                {
1611
                                    #region Zero Length And Branch
1612 4d4dce52 esham21
                                    if (Convert.ToBoolean(connector.get_IsZeroLength()))
1613 5a47c65c gaqhf
                                    {
1614 4d4dce52 esham21
                                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolID != _LMSymbol.Id)
1615 5a47c65c gaqhf
                                        {
1616 4d4dce52 esham21
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1617 5a47c65c gaqhf
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1618
                                                drawingObjectBases.Add(point2D);
1619
                                        }
1620 4d4dce52 esham21
                                        else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolID != _LMSymbol.Id)
1621 5a47c65c gaqhf
                                        {
1622 4d4dce52 esham21
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1623 5a47c65c gaqhf
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1624
                                                drawingObjectBases.Add(point2D);
1625
                                        }
1626
                                    }
1627
                                    #endregion
1628
                                    #region Not Zero Length And Branch And Vertical,Horizontal
1629
                                    else
1630
                                    {
1631
                                        if (connector.ConnectItem1SymbolObject != null && 
1632 4d4dce52 esham21
                                            connector.ConnectItem1SymbolID != _LMSymbol.Id &&
1633
                                            connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
1634
                                            connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch" &&
1635 5a47c65c gaqhf
                                            IsMovePoint2D(connector.ConnectItem1SymbolObject, connector, arrow))
1636
                                        {
1637 4d4dce52 esham21
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1638 5a47c65c gaqhf
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1639
                                                drawingObjectBases.Add(point2D);
1640
                                        }
1641
                                            
1642
                                        else if (connector.ConnectItem2SymbolObject != null && 
1643 4d4dce52 esham21
                                            connector.ConnectItem2SymbolID != _LMSymbol.Id &&
1644
                                            connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
1645
                                            connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch" &&
1646 5a47c65c gaqhf
                                            IsMovePoint2D(connector.ConnectItem2SymbolObject, connector, arrow))
1647
                                        {
1648 4d4dce52 esham21
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1649 5a47c65c gaqhf
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1650
                                                drawingObjectBases.Add(point2D);
1651
                                        }
1652
                                            
1653
                                    }
1654
                                    #endregion
1655
                                }
1656
                            }
1657
                            foreach (LMConnector connector in _LMSymbol.Connect2Connectors)
1658
                            {
1659 4d4dce52 esham21
                                if (connector.get_ItemStatus() == "Active")
1660 5a47c65c gaqhf
                                {
1661
                                    #region Zero Length And Branch
1662 4d4dce52 esham21
                                    if (Convert.ToBoolean(connector.get_IsZeroLength()))
1663 5a47c65c gaqhf
                                    {
1664 4d4dce52 esham21
                                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolID != _LMSymbol.Id)
1665 5a47c65c gaqhf
                                        {
1666 4d4dce52 esham21
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1667 5a47c65c gaqhf
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1668
                                                drawingObjectBases.Add(point2D);
1669
                                        }
1670 4d4dce52 esham21
                                        else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolID != _LMSymbol.Id)
1671 5a47c65c gaqhf
                                        {
1672 4d4dce52 esham21
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1673 5a47c65c gaqhf
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1674
                                                drawingObjectBases.Add(point2D);
1675
                                        }
1676
                                    }
1677
                                    #endregion
1678
                                    #region Not Zero Length And Branch And Vertical,Horizontal
1679
                                    else
1680
                                    {
1681
                                        if (connector.ConnectItem1SymbolObject != null &&
1682 4d4dce52 esham21
                                            connector.ConnectItem1SymbolID != _LMSymbol.Id &&
1683
                                            connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active" &&
1684
                                            connector.ConnectItem1SymbolObject.get_RepresentationType() == "Branch" &&
1685 5a47c65c gaqhf
                                            IsMovePoint2D(connector.ConnectItem1SymbolObject, connector, arrow))
1686
                                        {
1687 4d4dce52 esham21
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()] as Point2d;
1688 5a47c65c gaqhf
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1689
                                                drawingObjectBases.Add(point2D);
1690
                                        }
1691
1692
                                        else if (connector.ConnectItem2SymbolObject != null &&
1693 4d4dce52 esham21
                                            connector.ConnectItem2SymbolID != _LMSymbol.Id &&
1694
                                            connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active" &&
1695
                                            connector.ConnectItem2SymbolObject.get_RepresentationType() == "Branch" &&
1696 5a47c65c gaqhf
                                            IsMovePoint2D(connector.ConnectItem2SymbolObject, connector, arrow))
1697
                                        {
1698 4d4dce52 esham21
                                            Point2d point2D = application.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()] as Point2d;
1699 5a47c65c gaqhf
                                            if (point2D != null && !drawingObjectBases.Contains(point2D))
1700
                                                drawingObjectBases.Add(point2D);
1701
                                        }
1702
1703
                                    }
1704
                                    #endregion
1705
                                }
1706
                            }
1707
                        }
1708
                        ReleaseCOMObjects(_LMSymbol);
1709
                    }
1710 901feb22 gaqhf
                    else if (drawingObject.GetType() == typeof(Point2d))
1711
                    {
1712
                        Point2d point2D = drawingObject as Point2d;
1713
                        if (!drawingObjectBases.Contains(point2D))
1714
                            drawingObjectBases.Add(point2D);
1715
                    }
1716 5a47c65c gaqhf
                }
1717
1718
                application.ActiveSelectSet.RemoveAll();
1719 64922e03 gaqhf
                if (drawingObjectBases.Count > 0 && transform != null)
1720 5a47c65c gaqhf
                {
1721
                    application.ActiveSelectSet.AddObjects(drawingObjectBases);
1722
                    SetTransform(transform, arrow);
1723
                    application.ActiveSelectSet.Transform(transform, false);
1724
1725
                    application.ActiveSelectSet.RemoveAll();
1726 f250a747 gaqhf
                    application.ActiveSelectSet.AddObjects(originalDrawingObjectBases);
1727 5a47c65c gaqhf
                }
1728
                
1729
                //foreach (DrawingObjectBaseEx item in drawingObjectBases)
1730
                //    MoveSymbol(item, arrow);
1731
1732
                ReleaseCOMObjects(dataSource);
1733
                ReleaseCOMObjects(placement);
1734
            }
1735
        }
1736
        private bool IsMovePoint2D(LMSymbol branchSymbol, LMConnector targetConnector, Arrow arrow)
1737
        {
1738
            bool result = false;
1739 4d4dce52 esham21
            
1740
            DependencyObject dependency = application.ActiveDocument.ActiveSheet.DrawingObjects[targetConnector.get_GraphicOID().ToString()] as DependencyObject;
1741 5a47c65c gaqhf
            SlopeType mainSlope = GetLineSlopeType(dependency);
1742
1743
            if (mainSlope ==  SlopeType.HORIZONTAL || mainSlope == SlopeType.VERTICAL)
1744
            {
1745
                List<SlopeType> types = new List<SlopeType>();
1746
1747
                foreach (LMConnector connector in branchSymbol.Connect1Connectors)
1748
                {
1749 4d4dce52 esham21
                    if (connector.get_ItemStatus() == "Active" && 
1750 5a47c65c gaqhf
                        connector.Id != targetConnector.Id && 
1751 4d4dce52 esham21
                        !Convert.ToBoolean(connector.get_IsZeroLength()))
1752
                        types.Add(GetLineSlopeType(application.ActiveDocument.ActiveSheet.DrawingObjects[connector.get_GraphicOID().ToString()] as DependencyObject));
1753 5a47c65c gaqhf
                }
1754
1755
                foreach (LMConnector connector in branchSymbol.Connect2Connectors)
1756
                {
1757 4d4dce52 esham21
                    if (connector.get_ItemStatus() == "Active" &&
1758 5a47c65c gaqhf
                        connector.Id != targetConnector.Id &&
1759 4d4dce52 esham21
                        !Convert.ToBoolean(connector.get_IsZeroLength()))
1760
                        types.Add(GetLineSlopeType(application.ActiveDocument.ActiveSheet.DrawingObjects[connector.get_GraphicOID().ToString()] as DependencyObject));
1761 5a47c65c gaqhf
                }
1762
1763
                foreach (var type in types)
1764
                {
1765
                    if (type == mainSlope)
1766
                    {
1767
                        if (type == SlopeType.HORIZONTAL && (arrow == Arrow.Up || arrow == Arrow.Down))
1768
                            result = true;
1769
                        else if (type == SlopeType.VERTICAL && (arrow == Arrow.Left || arrow == Arrow.Right))
1770
                            result = true;
1771
                        else
1772
                        {
1773
                            result = false;
1774
                            break;
1775
                        }
1776
                    }
1777
                    else
1778
                        result = true;
1779
                }
1780
1781
                if (result)
1782
                {
1783
                    if ((arrow == Arrow.Down || arrow == Arrow.Up) && mainSlope == SlopeType.VERTICAL)
1784
                        result = false;
1785
                    else if ((arrow == Arrow.Left || arrow == Arrow.Right) && mainSlope == SlopeType.HORIZONTAL)
1786
                        result = false;
1787
                }
1788
            }
1789
1790
            return result;
1791
        }
1792 4d4dce52 esham21
        private SlopeType GetLineSlopeType(DependencyObject dependency)
1793 5a47c65c gaqhf
        {
1794
            if (dependency != null && dependency.DrawingObjects.Count == 1 && dependency.DrawingObjects[0].GetType() == typeof(LineString2d))
1795
            {
1796
                LineString2d line = dependency.DrawingObjects[0] as LineString2d;
1797
                double x1 = 0, y1 = 0, x2 = double.MaxValue, y2 = double.MaxValue;
1798
                for (int i = 0; i < line.KeyPointCount; i++)
1799
                {
1800
                    double x, y, z;
1801
                    KeyPointType keyPointType;
1802
                    HandleType handleType;
1803
                    line.GetKeyPoint(i, out x, out y, out z, out keyPointType, out handleType);
1804
                    if (keyPointType == KeyPointType.igKeyPointStart)
1805
                    {
1806
                        x1 = x;
1807
                        y1 = y;
1808
                    }
1809
                    else if (keyPointType == KeyPointType.igKeyPointEnd)
1810
                    {
1811
                        x2 = x;
1812
                        y2 = y;
1813
                    }
1814
                }
1815
                return SPPIDUtil.CalcSlope(x1, y1, x2, y2, 0.5);
1816
            }
1817
1818
            return SlopeType.None;
1819
        }
1820 4d4dce52 esham21
        private void MoveSymbol(DrawingObjectBaseEx drawingObjectBase, Arrow arrow)
1821
        {
1822
            if (drawingObjectBase == null)
1823
                return;
1824
1825
            GridSetting gridSetting = GridSetting.GetInstance();
1826
            switch (arrow)
1827
            {
1828
                case Arrow.Left:
1829
                    drawingObjectBase.Move(0, 0, -gridSetting.Length, 0);
1830
                    break;
1831
                case Arrow.Up:
1832
                    drawingObjectBase.Move(0, 0, 0, gridSetting.Length);
1833
                    break;
1834
                case Arrow.Right:
1835
                    drawingObjectBase.Move(0, 0, gridSetting.Length, 0);
1836
                    break;
1837
                case Arrow.Down:
1838
                    drawingObjectBase.Move(0, 0, 0, -gridSetting.Length);
1839
                    break;
1840
                default:
1841
                    break;
1842
            }
1843
        }
1844 5a47c65c gaqhf
        private void SetTransform(Transform transform, Arrow arrow)
1845
        {
1846 404a7c7b gaqhf
            if (transform == null)
1847
                return;
1848
1849 5a47c65c gaqhf
            GridSetting gridSetting = GridSetting.GetInstance();
1850
            switch (arrow)
1851
            {
1852
                case Arrow.Left:
1853
                    transform.DefineByMove2d(-gridSetting.Length, 0);
1854
                    break;
1855
                case Arrow.Up:
1856
                    transform.DefineByMove2d(0, gridSetting.Length);
1857
                    break;
1858
                case Arrow.Right:
1859
                    transform.DefineByMove2d(gridSetting.Length, 0);
1860
                    break;
1861
                case Arrow.Down:
1862
                    transform.DefineByMove2d(0, -gridSetting.Length);
1863
                    break;
1864
                default:
1865
                    break;
1866
            }
1867
        }
1868
        #endregion
1869
1870 82c7f66d gaqhf
        #endregion
1871 70b0b5d7 gaqhf
1872 4d4dce52 esham21
1873
        #region TEST
1874
        private void GetSPPIDSymbolRange(Ingr.RAD2D.Symbol2d symbol2d, ref double[] range)
1875
        {
1876
            {
1877
                double x1 = 0;
1878
                double y1 = 0;
1879
                double x2 = 0;
1880
                double y2 = 0;
1881
1882
                x1 = double.MaxValue;
1883
                y1 = double.MaxValue;
1884
                x2 = double.MinValue;
1885
                y2 = double.MinValue;
1886
                range = new double[] { x1, y1, x2, y2 };
1887
1888
                foreach (var item in symbol2d.DrawingObjects)
1889
                {
1890
                    if (item.GetType() == typeof(Ingr.RAD2D.Line2d))
1891
                    {
1892
                        Ingr.RAD2D.Line2d rangeObject = item as Ingr.RAD2D.Line2d;
1893
                        if (rangeObject.Layer == "Default")
1894
                        {
1895
                            rangeObject.Range(out x1, out y1, out x2, out y2);
1896
                            range = new double[] {
1897
                                Math.Min(x1, range[0]),
1898
                                Math.Min(y1, range[1]),
1899
                                Math.Max(x2, range[2]),
1900
                                Math.Max(y2, range[3])
1901
                            };
1902
                        }
1903
                    }
1904
                    else if (item.GetType() == typeof(Ingr.RAD2D.Circle2d))
1905
                    {
1906
                        Ingr.RAD2D.Circle2d rangeObject = item as Ingr.RAD2D.Circle2d;
1907
                        if (rangeObject.Layer == "Default")
1908
                        {
1909
                            rangeObject.Range(out x1, out y1, out x2, out y2);
1910
                            range = new double[] {
1911
                                Math.Min(x1, range[0]),
1912
                                Math.Min(y1, range[1]),
1913
                                Math.Max(x2, range[2]),
1914
                                Math.Max(y2, range[3])
1915
                            };
1916
                        }
1917
                    }
1918
                    else if (item.GetType() == typeof(Ingr.RAD2D.Rectangle2d))
1919
                    {
1920
                        Ingr.RAD2D.Rectangle2d rangeObject = item as Ingr.RAD2D.Rectangle2d;
1921
                        if (rangeObject.Layer == "Default")
1922
                        {
1923
                            rangeObject.Range(out x1, out y1, out x2, out y2);
1924
                            range = new double[] {
1925
                                Math.Min(x1, range[0]),
1926
                                Math.Min(y1, range[1]),
1927
                                Math.Max(x2, range[2]),
1928
                                Math.Max(y2, range[3])
1929
                            };
1930
                        }
1931
                    }
1932
                    else if (item.GetType() == typeof(Ingr.RAD2D.Arc2d))
1933
                    {
1934
                        Ingr.RAD2D.Arc2d rangeObject = item as Ingr.RAD2D.Arc2d;
1935
                        if (rangeObject.Layer == "Default")
1936
                        {
1937
                            rangeObject.Range(out x1, out y1, out x2, out y2);
1938
                            range = new double[] {
1939
                                Math.Min(x1, range[0]),
1940
                                Math.Min(y1, range[1]),
1941
                                Math.Max(x2, range[2]),
1942
                                Math.Max(y2, range[3])
1943
                            };
1944
                        }
1945
                    }
1946
                }
1947
            }
1948
        }
1949
        private void simpleButton1_Click(object sender, EventArgs e)
1950
        {
1951
            double x1 = double.MaxValue;
1952
            double y1 = double.MaxValue;
1953
            double x2 = double.MinValue;
1954
            double y2 = double.MinValue;
1955
            foreach (DrawingObjectBase item in application.ActiveSelectSet)
1956
            {
1957
                double[] range = new double[] { };
1958
                GetSPPIDSymbolRange(item as Symbol2d, ref range);
1959
            }
1960
            return;
1961
1962
            //SPPIDUtil.test();
1963
            if (application.ActiveSelectSet.Count == 0)
1964
                return;
1965
1966
            foreach (DrawingObjectBase item in application.ActiveSelectSet)
1967
            {
1968
                DependencyObject dependency = item as DependencyObject;
1969
                if (dependency != null)
1970
                {
1971
                    foreach (var attributes in dependency.AttributeSets)
1972
                    {
1973
                        foreach (var attribute in attributes)
1974
                        {
1975
                            string name = attribute.Name;
1976
                            object value = attribute.GetValue();
1977
                            if (name == "ModelID")
1978
                            {
1979
                                Placement placement = new Placement();
1980
                                LMADataSource dataSource = placement.PIDDataSource;
1981
                                LMPipeRun pipeRun = dataSource.GetPipeRun(value);
1982
                                string attrName = "PlantGroup.Name";
1983
                                LMAAttribute lMAAttribute = pipeRun.Attributes[attrName];
1984
                                if (lMAAttribute != null)
1985
                                {
1986
                                    lMAAttribute.set_Value("25");
1987
                                    pipeRun.Commit();
1988
                                }
1989
                                else
1990
                                {
1991
1992
                                }
1993
                            }
1994
                        }
1995
                    }
1996
                }
1997
            }
1998
            
1999
            
2000
2001
            //string a = "0A509911F33441A2AF088BFBA78B770D";
2002
            //LMLabelPersist label = dataSource.GetLabelPersist(a);
2003
            //label.set_XCoordinate(0.4);
2004
2005
2006
2007
2008
            //LMOptionSettings
2009
2010
2011
            //LMAFilter filter = new LMAFilter();
2012
            //LMACriterion criterion = new LMACriterion();
2013
            //filter.ItemType = "Relationship";
2014
            //criterion.SourceAttributeName = "SP_DRAWINGID";
2015
            //criterion.Operator = "=";
2016
            //criterion.set_ValueAttribute(drawingID);
2017
            //filter.get_Criteria().Add(criterion);
2018
2019
            //LMRelationships relationships = new LMRelationships();
2020
            //relationships.Collect(dataSource, Filter: filter);
2021
2022
        }
2023
        private void AutoJoinPipeRun()
2024
        {
2025
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
2026
            WrapperApplication wApp = new WrapperApplication(application.Application);
2027
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
2028
2029
            string modelItemId = null;
2030
            List<double[]> vertices = new List<double[]>();
2031
            if (radApp.ActiveSelectSet.Count == 0)
2032
            {
2033
                return;
2034
            }
2035
            dynamic OID = radApp.ActiveSelectSet[0].Key();
2036
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2037
            foreach (var attributes in drawingObject.AttributeSets)
2038
            {
2039
                foreach (var attribute in attributes)
2040
                {
2041
                    if (attribute.Name == "ModelID")
2042
                        modelItemId = attribute.GetValue().ToString();
2043
                }
2044
            }
2045
            radApp.ActiveSelectSet.RemoveAll();
2046
2047
        }
2048
        #endregion
2049
2050
2051 9628f54b gaqhf
        [DllImport("user32.dll")]
2052
        public static extern int FindWindow(string lpClassName, string lpWindowName);
2053 3734dcc5 gaqhf
2054
        [DllImport("user32.dll", SetLastError = true)]
2055
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
2056
2057 4d4dce52 esham21
        private void btnLinkOPCTest_Click(object sender, EventArgs e)
2058
        {
2059
            Placement placement = new Placement();
2060
            LMADataSource dataSource = placement.PIDDataSource;
2061
2062
            LMAFilter filter = new LMAFilter();
2063
            filter.ItemType = "REPRESENTATION";
2064
            
2065
            LMACriterion criterion = new LMACriterion();
2066
            criterion.SourceAttributeName = "REPRESENTATIONTYPE";
2067
            criterion.Operator = "=";
2068
            criterion.set_ValueAttribute("39");
2069
            filter.get_Criteria().Add(criterion);
2070
2071
            LMRepresentations representations = new LMRepresentations();
2072
            representations.Collect(dataSource, Filter: filter);
2073
2074
            foreach (LMRepresentation representation in representations)
2075
            {
2076
                if (representation.get_ItemStatus() == "Active" && representation.DrawingID != "0")
2077
                {
2078
                    LMOPC opc = dataSource.GetOPC(representation.ModelItemID);
2079
                    LMOPC pairOPC = opc.pairedWithOPCObject;
2080
                    bool isPaired = false;
2081
                    foreach (LMRepresentation rep in pairOPC.Representations)
2082
                        if (rep.DrawingID != "0")
2083
                            isPaired = true;
2084
2085
                    if (!isPaired)
2086
                    {
2087
                        LMSymbol newOPC = placement.PIDPlaceSymbol(representation.get_FileName().ToString(), 0, 0, ExistingItem: pairOPC.AsLMAItem());
2088
                        ReleaseCOMObjects(newOPC);
2089
                    }
2090
2091
                    ReleaseCOMObjects(opc);
2092
                    ReleaseCOMObjects(pairOPC);
2093
                    
2094
                }
2095
            }
2096
2097
            ReleaseCOMObjects(representations);
2098
            ReleaseCOMObjects(criterion);
2099
            ReleaseCOMObjects(filter);
2100
            ReleaseCOMObjects(dataSource);
2101
            ReleaseCOMObjects(placement);
2102
        }
2103
2104 9fcc54bb gaqhf
        #region Test For SECL
2105
        private void btnAutoBreakTest_Click(object sender, EventArgs e)
2106
        {
2107
            
2108
        }
2109
2110
        private void FindRun(LMSymbol startSymbol, LMSymbol endSymbol, string model)
2111
        {
2112
            Placement placement = new Placement();
2113
            LMADataSource dataSource = placement.PIDDataSource;
2114
            List<string> endRepID = new List<string>();
2115
            List<string> findRepID = new List<string>();
2116
            endRepID.Add(startSymbol.AsLMRepresentation().Id);
2117
            findRepID.Add(startSymbol.AsLMRepresentation().Id);
2118
            bool end = false;
2119
            LoopRunTest(startSymbol.AsLMRepresentation(), endRepID, endSymbol.AsLMRepresentation().Id, ref end, findRepID);
2120
2121
2122
            //findRepID.Reverse();
2123
            for (int i = 0; i < findRepID.Count; i++)
2124
            {
2125
                string repID = findRepID[i];
2126
2127
                LMRepresentation representation = dataSource.GetRepresentation(repID);
2128
                if (representation != null)
2129
                {
2130 4d4dce52 esham21
                    string fileName = representation.get_FileName().ToString();
2131 9fcc54bb gaqhf
                    if (fileName.Contains(@"\Piping\Valves"))
2132
                    {
2133
                        LMModelItem modelItem = representation.ModelItemObject;
2134
                        LMSymbol tarsym = dataSource.GetSymbol(representation.Id);
2135
                        LMRepresentation prevRep = dataSource.GetRepresentation(findRepID[i - 1]);
2136
                        LMConnector removeConnector = dataSource.GetConnector(prevRep.Id);
2137
2138
                        List<double[]> vertices = GetConnectorVertices(removeConnector);
2139
2140
                        ZoomObjectByGraphicOID(vertices[0][0], vertices[0][1]);
2141 4d4dce52 esham21
                        Array array = new double[] { 0, vertices[0][0], vertices[0][1] };
2142
                        LMLabelPersist _LmLabelPersist = placement.PIDPlaceLabel(model, ref array, LabeledItem: removeConnector.AsLMRepresentation(), IsLeaderVisible: true);
2143 9fcc54bb gaqhf
                        if (_LmLabelPersist == null)
2144
                        {
2145 4d4dce52 esham21
                            if (removeConnector.get_ItemStatus() == "Active")
2146 9fcc54bb gaqhf
                            {
2147
                                LMConnector newConnector = ReModelingLine(removeConnector, removeConnector.ConnectItem1SymbolObject, removeConnector.ConnectItem2SymbolObject, true, false);
2148
                                FindRun(startSymbol, endSymbol, model);
2149
                            }
2150
                        }
2151
                        
2152
2153
2154
                        //string dd = startSymbol.ModelItemObject.Attributes["SteamOutTemperature"].get_Value();
2155
                        //string dd2 = endSymbol.ModelItemObject.Attributes["SteamOutTemperature"].get_Value();
2156
                        //double value1 = Convert.ToDouble(dd.Split(new char[] { ' ' })[0]);
2157
                        //double value2 = Convert.ToDouble(dd2.Split(new char[] { ' ' })[0]);
2158
2159
                        //if (value1 > value2)
2160
                        //{
2161
                        //    LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i + 1]);
2162
                        //    placement.PIDRemovePlacement(prevRep);
2163
                        //}
2164
                        //else
2165
                        //{
2166
                        //    LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i - 1]);
2167
                        //    placement.PIDRemovePlacement(prevRep);
2168
                        //}
2169
2170
                        //LMRepresentation prevRep = dataSource.GetRepresentation(endRepID[i - 1]);
2171
                        //placement.PIDRemovePlacement(prevRep);
2172
2173
2174
                        //application.ActiveDocument.Save();
2175
                        //ReleaseCOMObjects(modelItem);
2176
                        break;
2177
                    }
2178
                }
2179
2180
                ReleaseCOMObjects(representation);
2181
            }
2182
        }
2183
        private LMConnector ReModelingLine(LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
2184
        {
2185
            Placement _placement = new Placement();
2186
            LMADataSource dataSource = _placement.PIDDataSource;
2187
2188
            string symbolPath = string.Empty;
2189
            #region get symbol path
2190
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
2191
            symbolPath = GetSPPIDFileName(modelItem);
2192
            ReleaseCOMObjects(modelItem);
2193
            #endregion
2194
            bool diagonal = false;
2195 4d4dce52 esham21
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2196 9fcc54bb gaqhf
            LMConnector newConnector = null;
2197 4d4dce52 esham21
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
2198
            DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2199 9fcc54bb gaqhf
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2200
            int verticesCount = lineStringGeometry.VertexCount;
2201
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2202
2203
            List<double[]> vertices = new List<double[]>();
2204
            for (int i = 1; i <= verticesCount; i++)
2205
            {
2206
                double x = 0;
2207
                double y = 0;
2208
                lineStringGeometry.GetVertex(i, ref x, ref y);
2209
                vertices.Add(new double[] { x, y });
2210
            }
2211
2212
            for (int i = 0; i < vertices.Count; i++)
2213
            {
2214
                double[] points = vertices[i];
2215
                // 시작 심볼이 있고 첫번째 좌표일 때
2216
                if (startSymbol != null && i == 0)
2217
                {
2218
                    if (bStart)
2219
                    {
2220
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
2221
                        if (slopeType == SlopeType.HORIZONTAL)
2222
                            placeRunInputs.AddPoint(points[0], -0.1);
2223
                        else if (slopeType == SlopeType.VERTICAL)
2224
                            placeRunInputs.AddPoint(-0.1, points[1]);
2225
                        else
2226
                            placeRunInputs.AddPoint(points[0], -0.1);
2227
2228
                        placeRunInputs.AddPoint(points[0], points[1]);
2229
                    }
2230
                    else
2231
                    {
2232
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2233
                    }
2234
                }
2235
                // 마지막 심볼이 있고 마지막 좌표일 때
2236
                else if (endSymbol != null && i == vertices.Count - 1)
2237
                {
2238
                    if (bEnd)
2239
                    {
2240
                        placeRunInputs.AddPoint(points[0], points[1]);
2241
2242
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2243
                        if (slopeType == SlopeType.HORIZONTAL)
2244
                            placeRunInputs.AddPoint(points[0], -0.1);
2245
                        else if (slopeType == SlopeType.VERTICAL)
2246
                            placeRunInputs.AddPoint(-0.1, points[1]);
2247
                        else
2248
                            placeRunInputs.AddPoint(points[0], -0.1);
2249
                    }
2250
                    else
2251
                    {
2252
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2253
                    }
2254
                }
2255 5cf7fb68 Denny
                // 첫번째이며 시작 심볼이 아니고 Connector일 경우
2256 9fcc54bb gaqhf
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2257
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2258 5cf7fb68 Denny
                // 마지막이며 마지막 심볼이 아니고 Connector일 경우
2259 9fcc54bb gaqhf
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2260
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2261
                else
2262
                    placeRunInputs.AddPoint(points[0], points[1]);
2263
            }
2264
2265
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2266
            Thread.Sleep(1000);
2267
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2268
2269
            ReleaseCOMObjects(placeRunInputs);
2270
            ReleaseCOMObjects(_LMAItem);
2271
            ReleaseCOMObjects(modelItem);
2272
2273
            if (newConnector != null)
2274
            {
2275
                newConnector.Commit();
2276
                if (startSymbol != null && bStart)
2277
                {
2278
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2279
                    placeRunInputs = new PlaceRunInputs();
2280
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2281
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2282
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2283
                    if (_LMConnector != null)
2284
                    {
2285
                        _LMConnector.Commit();
2286
                        newConnector = RemoveConnectorForReModelingLine(newConnector);
2287
                        ReleaseCOMObjects(_LMConnector);
2288
                    }
2289
                    ReleaseCOMObjects(placeRunInputs);
2290
                    ReleaseCOMObjects(_LMAItem);
2291
                }
2292
2293
                if (endSymbol != null && bEnd)
2294
                {
2295
                    if (startSymbol != null)
2296
                    {
2297 4d4dce52 esham21
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2298 9fcc54bb gaqhf
                        newConnector = dicVertices.First().Key;
2299
                    }
2300
2301
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2302
                    placeRunInputs = new PlaceRunInputs();
2303
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2304
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2305
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2306
                    if (_LMConnector != null)
2307
                    {
2308
                        _LMConnector.Commit();
2309
                        newConnector = RemoveConnectorForReModelingLine(newConnector);
2310
                        ReleaseCOMObjects(_LMConnector);
2311
                    }
2312
                    ReleaseCOMObjects(placeRunInputs);
2313
                    ReleaseCOMObjects(_LMAItem);
2314
                }
2315
2316
                
2317
            }
2318
            ReleaseCOMObjects(modelItem);
2319
            return newConnector;
2320
            
2321
        }
2322
2323
        private LMConnector RemoveConnectorForReModelingLine(LMConnector connector)
2324
        {
2325
            Placement _placement = new Placement();
2326
            LMADataSource dataSource = _placement.PIDDataSource;
2327
2328 4d4dce52 esham21
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2329 9fcc54bb gaqhf
            foreach (var item in dicVertices)
2330
            {
2331
                if (item.Value.Count == 2)
2332
                {
2333
                    bool result = false;
2334
                    foreach (var point in item.Value)
2335
                    {
2336
                        if (point[0] < 0 || point[1] < 0)
2337
                        {
2338
                            result = true;
2339
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2340
                            break;
2341
                        }
2342
                    }
2343
2344
                    if (result)
2345
                        break;
2346
                }
2347
            }
2348
2349
            LMConnector result111 = null;
2350
            foreach (var item in dicVertices)
2351
            {
2352 4d4dce52 esham21
                if (item.Key.get_ItemStatus() == "Active")
2353 9fcc54bb gaqhf
                    result111 = item.Key;
2354
                else
2355
                    ReleaseCOMObjects(item.Key);
2356
            }
2357
2358
            return result111;
2359
        }
2360
2361
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId, bool ContainZeroLength = true)
2362
        {
2363
            Placement _placement = new Placement();
2364
            LMADataSource dataSource = _placement.PIDDataSource;
2365
2366
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2367
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2368
2369
            if (modelItem != null)
2370
            {
2371
                foreach (LMRepresentation rep in modelItem.Representations)
2372
                {
2373 4d4dce52 esham21
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2374 9fcc54bb gaqhf
                    {
2375
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2376 4d4dce52 esham21
                        if (!ContainZeroLength && Convert.ToBoolean(_LMConnector.get_IsZeroLength()))
2377 9fcc54bb gaqhf
                        {
2378
                            ReleaseCOMObjects(_LMConnector);
2379
                            _LMConnector = null;
2380
                            continue;
2381
                        }
2382
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2383 4d4dce52 esham21
                        dynamic OID = rep.get_GraphicOID().ToString();
2384
                        DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2385 9fcc54bb gaqhf
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2386
                        int verticesCount = lineStringGeometry.VertexCount;
2387
                        double[] vertices = null;
2388
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2389
                        for (int i = 0; i < verticesCount; i++)
2390
                        {
2391
                            double x = 0;
2392
                            double y = 0;
2393
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2394
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
2395
                        }
2396
                    }
2397
                }
2398
2399
                ReleaseCOMObjects(modelItem);
2400
            }
2401
2402
            return connectorVertices;
2403
        }
2404
        private string GetSPPIDFileName(LMModelItem modelItem)
2405
        {
2406
            string symbolPath = null;
2407
            foreach (LMRepresentation rep in modelItem.Representations)
2408
            {
2409 4d4dce52 esham21
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2410 9fcc54bb gaqhf
                {
2411 4d4dce52 esham21
                    symbolPath = rep.get_FileName();
2412 9fcc54bb gaqhf
                    break;
2413
                }
2414
            }
2415
            return symbolPath;
2416
        }
2417
        private string GetSPPIDFileName(string modelItemId)
2418
        {
2419
            Placement placement = new Placement();
2420
            LMADataSource dataSource = placement.PIDDataSource;
2421
2422
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2423
            string symbolPath = null;
2424
            foreach (LMRepresentation rep in modelItem.Representations)
2425
            {
2426 4d4dce52 esham21
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2427 9fcc54bb gaqhf
                {
2428 4d4dce52 esham21
                    symbolPath = rep.get_FileName();
2429 9fcc54bb gaqhf
                    break;
2430
                }
2431
            }
2432
            ReleaseCOMObjects(modelItem);
2433
            return symbolPath;
2434
        }
2435
        private List<double[]> GetConnectorVertices(LMConnector connector)
2436
        {
2437
            List<double[]> vertices = new List<double[]>();
2438
            if (connector != null)
2439
            {
2440 4d4dce52 esham21
                dynamic OID = connector.get_GraphicOID().ToString();
2441
                DependencyObject drawingObject = application.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2442 9fcc54bb gaqhf
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2443
                int verticesCount = lineStringGeometry.VertexCount;
2444
                double[] value = null;
2445
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
2446
                for (int i = 0; i < verticesCount; i++)
2447
                {
2448
                    double x = 0;
2449
                    double y = 0;
2450
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2451
                    vertices.Add(new double[] { x, y });
2452
                }
2453
            }
2454
            return vertices;
2455
        }
2456
        private void LoopRunTest(LMRepresentation rep, List<string> endRepID, string endID,ref bool end, List<string> findRepID)
2457
        {
2458
            if (!end)
2459
            {
2460
                foreach (LMRelationship item in rep.Relation1Relationships)
2461
                {
2462
                    if (end)
2463
                        break;
2464
2465 4d4dce52 esham21
                    if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID) && item.Item1RepresentationObject.get_ItemStatus() == "Active" &&
2466
                        (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item1RepresentationObject.get_RepresentationClass() == "Connector"))
2467 9fcc54bb gaqhf
                    {
2468 4d4dce52 esham21
                        if (endID != item.Item1RepresentationID)
2469 9fcc54bb gaqhf
                        {
2470 4d4dce52 esham21
                            endRepID.Add(item.Item1RepresentationID);
2471 9fcc54bb gaqhf
                            LoopRunTest(item.Item1RepresentationObject, endRepID, endID, ref end, findRepID);
2472
                            if (end)
2473 4d4dce52 esham21
                                findRepID.Add(item.Item1RepresentationID);
2474 9fcc54bb gaqhf
                        }
2475
                        else
2476
                            end = true;
2477
                    }
2478
2479
                    if (end)
2480
                        break;
2481
2482 4d4dce52 esham21
                    if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID) && item.Item2RepresentationObject.get_ItemStatus() == "Active" &&
2483
                        (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item2RepresentationObject.get_RepresentationClass() == "Connector"))
2484 9fcc54bb gaqhf
                    {
2485 4d4dce52 esham21
                        if (endID != item.Item2RepresentationID)
2486 9fcc54bb gaqhf
                        {
2487 4d4dce52 esham21
                            endRepID.Add(item.Item2RepresentationID);
2488 9fcc54bb gaqhf
                            LoopRunTest(item.Item2RepresentationObject, endRepID, endID, ref end, findRepID);
2489
                            if (end)
2490 4d4dce52 esham21
                                findRepID.Add(item.Item2RepresentationID);
2491 9fcc54bb gaqhf
                        }
2492
                        else
2493
                            end = true;
2494
                    }
2495
                }
2496
            }
2497
2498
            if (!end)
2499
            {
2500
                foreach (LMRelationship item in rep.Relation2Relationships)
2501
                {
2502
                    if (end)
2503
                        break;
2504
2505 4d4dce52 esham21
                    if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID) && item.Item1RepresentationObject.get_ItemStatus() == "Active" &&
2506
                        (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item1RepresentationObject.get_RepresentationClass() == "Connector"))
2507 9fcc54bb gaqhf
                    {
2508 4d4dce52 esham21
                        if (endID != item.Item1RepresentationID)
2509 9fcc54bb gaqhf
                        {
2510 4d4dce52 esham21
                            endRepID.Add(item.Item1RepresentationID);
2511 9fcc54bb gaqhf
                            LoopRunTest(item.Item1RepresentationObject, endRepID, endID, ref end, findRepID);
2512
                            if (end)
2513 4d4dce52 esham21
                                findRepID.Add(item.Item1RepresentationID);
2514 9fcc54bb gaqhf
                        }
2515
                        else
2516
                            end = true;
2517
                    }
2518
2519
                    if (end)
2520
                        break;
2521
2522 4d4dce52 esham21
                    if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID) && item.Item2RepresentationObject.get_ItemStatus() == "Active" &&
2523
                        (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol" || item.Item2RepresentationObject.get_RepresentationClass() == "Connector"))
2524 9fcc54bb gaqhf
                    {
2525 4d4dce52 esham21
                        if (endID != item.Item2RepresentationID)
2526 9fcc54bb gaqhf
                        {
2527 4d4dce52 esham21
                            endRepID.Add(item.Item2RepresentationID);
2528 9fcc54bb gaqhf
                            LoopRunTest(item.Item2RepresentationObject, endRepID, endID, ref end, findRepID);
2529
                            if (end)
2530 4d4dce52 esham21
                                findRepID.Add(item.Item2RepresentationID);
2531 9fcc54bb gaqhf
                        }
2532
                        else
2533
                            end = true;
2534
                    }
2535
                }
2536
            }
2537
        }
2538
2539 4d4dce52 esham21
        private void btnOPC_Click(object sender, EventArgs e)
2540
        {
2541
            Placement _placement;
2542
            LMADataSource dataSource;
2543
            _placement = new Placement();
2544
            dataSource = _placement.PIDDataSource;
2545
2546
            OPCFlowDirection();
2547
2548
            void OPCFlowDirection()
2549
            {
2550
                //current LMDrawing 가져오기
2551
                LMAFilter filter = new LMAFilter();
2552
                LMACriterion criterion = new LMACriterion();
2553
                filter.ItemType = "Drawing";
2554
                criterion.SourceAttributeName = "Name";
2555
                criterion.Operator = "=";
2556
                criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
2557
                filter.get_Criteria().Add(criterion);
2558
2559
                LMDrawings drawings = new LMDrawings();
2560
                drawings.Collect(dataSource, Filter: filter);
2561
2562
                // Input Drawing Attribute
2563
                LMDrawing drawing = ((dynamic)drawings).Nth(1);
2564
2565
                LMAFilter filter2 = new LMAFilter();
2566
                filter2.ItemType = "REPRESENTATION";
2567
2568
                LMACriterion criterion2 = new LMACriterion();
2569
                criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
2570
                criterion2.Operator = "=";
2571
                criterion2.set_ValueAttribute("39");
2572
                filter2.get_Criteria().Add(criterion2);
2573
2574
                LMRepresentations representations = new LMRepresentations();
2575
                representations.Collect(dataSource, Filter: filter2);
2576
2577
                foreach (LMRepresentation representation in representations)
2578
                {
2579
                    if (representation.DrawingID == drawing.Id)
2580
                    {
2581
                        RunOPC(representation);
2582
                    }
2583
                }
2584
                
2585
                ReleaseCOMObjects(filter);
2586
                ReleaseCOMObjects(filter2);
2587
                ReleaseCOMObjects(criterion);
2588
                ReleaseCOMObjects(criterion2);
2589
                ReleaseCOMObjects(drawing);
2590
                ReleaseCOMObjects(drawings);
2591
                ReleaseCOMObjects(representations);
2592
            }
2593
2594
            void RunOPC(LMRepresentation representation)
2595
            {
2596
                LMSymbol symbol = dataSource.GetSymbol(representation.Id);
2597
                List<string> endRepId = new List<string>();
2598
                endRepId.Add(symbol.AsLMRepresentation().Id);
2599
                LoopSymbol(symbol, endRepId);
2600
                
2601
                ReleaseCOMObjects(symbol);
2602
2603
                void LoopSymbol(LMSymbol loopSymbol, List<string> endRepID)
2604
                {
2605
                    foreach (LMRelationship item in loopSymbol.Relation1Relationships)
2606
                    {
2607
                        if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID))
2608
                        {
2609
                            endRepID.Add(item.Item1RepresentationID);
2610
                            if (item.Item1RepresentationObject.get_RepresentationClass() == "Connector")
2611
                            {
2612
                                LMModelItem modelItem = item.Item1RepresentationObject.ModelItemObject;
2613
                                SetAttribute(modelItem);
2614
                                LoopConnector(dataSource.GetConnector(item.Item1RepresentationID), endRepID);
2615
                            }
2616
                            else if (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol")
2617
                            {
2618
                                LoopSymbol(dataSource.GetSymbol(item.Item1RepresentationID), endRepID);
2619
                            }
2620
                        }
2621
2622
                        if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID))
2623
                        {
2624
                            endRepID.Add(item.Item2RepresentationID);
2625
                            if (item.Item2RepresentationObject.get_RepresentationClass() == "Connector")
2626
                            {
2627
                                LMModelItem modelItem = item.Item2RepresentationObject.ModelItemObject;
2628
                                SetAttribute(modelItem);
2629
                                LoopConnector(dataSource.GetConnector(item.Item2RepresentationID), endRepID);
2630
                            }
2631
                            else if (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol")
2632
                            {
2633
                                LoopSymbol(dataSource.GetSymbol(item.Item2RepresentationID), endRepID);
2634
                            }
2635
                        }
2636
                    }
2637
2638
                    foreach (LMRelationship item in loopSymbol.Relation2Relationships)
2639
                    {
2640
                        if (!DBNull.Value.Equals(item.Item1RepresentationID) && !endRepID.Contains(item.Item1RepresentationID))
2641
                        {
2642
                            endRepID.Add(item.Item1RepresentationID);
2643
                            if (item.Item1RepresentationObject.get_RepresentationClass() == "Connector")
2644
                            {
2645
                                LMModelItem modelItem = item.Item1RepresentationObject.ModelItemObject;
2646
                                SetAttribute(modelItem);
2647
                                LoopConnector(dataSource.GetConnector(item.Item1RepresentationID), endRepID);
2648
                            }
2649
                            else if (item.Item1RepresentationObject.get_RepresentationClass() == "Symbol")
2650
                            {
2651
                                LoopSymbol(dataSource.GetSymbol(item.Item1RepresentationID), endRepID);
2652
                            }
2653
                        }
2654
2655
                        if (!DBNull.Value.Equals(item.Item2RepresentationID) && !endRepID.Contains(item.Item2RepresentationID))
2656
                        {
2657
                            endRepID.Add(item.Item2RepresentationID);
2658
                            if (item.Item2RepresentationObject.get_RepresentationClass() == "Connector")
2659
                            {
2660
                                LMModelItem modelItem = item.Item2RepresentationObject.ModelItemObject;
2661
                                SetAttribute(modelItem);
2662
                                LoopConnector(dataSource.GetConnector(item.Item2RepresentationID), endRepID);
2663
                            }
2664
                            else if (item.Item2RepresentationObject.get_RepresentationClass() == "Symbol")
2665
                            {
2666
                                LoopSymbol(dataSource.GetSymbol(item.Item2RepresentationID), endRepID);
2667
                            }
2668
                        }
2669
                    }
2670
2671
                    foreach (LMConnector item in loopSymbol.Connect1Connectors)//나가는것
2672
                    {
2673
                        if (item.get_ItemStatus() != "Active" || endRepID.Contains(item.AsLMRepresentation().Id))
2674
                            continue;
2675
2676
                        endRepID.Add(item.AsLMRepresentation().Id);
2677
                        if (item.get_ItemStatus() == "Active")
2678
                        {
2679
                            LMModelItem modelItem = item.ModelItemObject;
2680
                            SetAttribute(modelItem);
2681
                            LoopConnector(item, endRepID);
2682
                        }
2683
                    }
2684
2685
                    foreach (LMConnector item in loopSymbol.Connect2Connectors)//들어오는것
2686
                    {
2687
                        if (item.get_ItemStatus() != "Active" || endRepID.Contains(item.AsLMRepresentation().Id))
2688
                            continue;
2689
2690
                        endRepID.Add(item.AsLMRepresentation().Id);
2691
                        if (item.get_ItemStatus() == "Active")
2692
                        {
2693
                            LMModelItem modelItem = item.ModelItemObject;
2694
                            SetAttribute(modelItem);
2695
                            LoopConnector(item, endRepID);
2696
                        }
2697
                    }
2698
                }
2699
2700
                void LoopConnector(LMConnector connector, List<string> endRepID)
2701
                {
2702
                    if (connector.ConnectItem1SymbolObject != null && !endRepID.Contains(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id))
2703
                    {
2704
                        endRepID.Add(connector.ConnectItem1SymbolObject.AsLMRepresentation().Id);
2705
                        //LoopSymbol(connector.ConnectItem1SymbolObject, endRepID);
2706
                    }
2707
2708
                    if (connector.ConnectItem2SymbolObject != null && !endRepID.Contains(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id))
2709
                    {
2710
                        endRepID.Add(connector.ConnectItem2SymbolObject.AsLMRepresentation().Id);
2711
                        //LoopSymbol(connector.ConnectItem2SymbolObject, endRepID);
2712
                    }
2713
                }
2714
            }
2715
2716
            void SetAttribute(LMModelItem modelItem)
2717
            {
2718
                LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
2719
                if (attribute != null)
2720
                    attribute.set_Value("End 1 is upstream (Inlet)");
2721
                modelItem.Commit();
2722
            }
2723
2724
            ReleaseCOMObjects(_placement);
2725
            ReleaseCOMObjects(dataSource);
2726
2727
            MessageBox.Show("End OPC Flow Direction");
2728
        }
2729
2730
        private void btnCreateStreamBreak_Click(object sender, EventArgs e)
2731
        {
2732
            Placement _placement;
2733
            LMADataSource dataSource;
2734
2735
            StreamNoBreak();
2736
2737
            void StreamNoBreak()
2738
            {
2739
                
2740
                _placement = new Placement();
2741
                dataSource = _placement.PIDDataSource;
2742
2743
                OPCFlowDirection();
2744
2745
                void OPCFlowDirection()
2746
                {
2747
                    //current LMDrawing 가져오기
2748
                    LMAFilter filter = new LMAFilter();
2749
                    LMACriterion criterion = new LMACriterion();
2750
                    filter.ItemType = "Drawing";
2751
                    criterion.SourceAttributeName = "Name";
2752
                    criterion.Operator = "=";
2753
                    criterion.set_ValueAttribute(application.ActiveDocument.Name.Replace(".pid", ""));
2754
                    filter.get_Criteria().Add(criterion);
2755
2756
                    LMDrawings drawings = new LMDrawings();
2757
                    drawings.Collect(dataSource, Filter: filter);
2758
2759
                    // Input Drawing Attribute
2760
                    LMDrawing drawing = ((dynamic)drawings).Nth(1);
2761
2762
                    LMAFilter filter2 = new LMAFilter();
2763
                    filter2.ItemType = "REPRESENTATION";
2764
2765
                    LMACriterion criterion2 = new LMACriterion();
2766
                    criterion2.SourceAttributeName = "REPRESENTATIONTYPE";
2767
                    criterion2.Operator = "=";
2768
                    criterion2.set_ValueAttribute("46");
2769
                    filter2.get_Criteria().Add(criterion2);
2770
2771
                    LMRepresentations representations = new LMRepresentations();
2772
                    representations.Collect(dataSource, Filter: filter2);
2773
2774
                    foreach (LMRepresentation representation in representations)
2775
                    {
2776
                        if (representation.DrawingID == drawing.Id)
2777
                        {
2778
                            LMSymbol symbol = dataSource.GetSymbol(representation.Id);
2779
                            CreateSpec(symbol);
2780
                        }
2781
                    }
2782
2783
                    ReleaseCOMObjects(filter);
2784
                    ReleaseCOMObjects(filter2);
2785
                    ReleaseCOMObjects(criterion);
2786
                    ReleaseCOMObjects(criterion2);
2787
                    ReleaseCOMObjects(drawing);
2788
                    ReleaseCOMObjects(drawings);
2789
                    ReleaseCOMObjects(representations);
2790
                }
2791
            }
2792
2793
            void CreateSpec(LMSymbol symbol)
2794
            {
2795
                ZoomObjectByGraphicOID(symbol.get_XCoordinate(), symbol.get_YCoordinate());
2796
2797
                foreach (LMConnector item in symbol.Connect1Connectors)
2798
                {
2799
                    if (item.get_ItemStatus() == "Active")
2800
                    {
2801
                        Array array = new double[] { 0, symbol.get_XCoordinate(), symbol.get_YCoordinate() };
2802
                        LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(@"\Piping\Segment Breaks\Above\stream Number Break.sym", ref array, LabeledItem: item.AsLMRepresentation(), IsLeaderVisible: true);
2803
                    }
2804
                }
2805
2806
                foreach (LMConnector item in symbol.Connect2Connectors)
2807
                {
2808
                    if (item.get_ItemStatus() == "Active")
2809
                    {
2810
                        Array array = new double[] { 0, symbol.get_XCoordinate(), symbol.get_YCoordinate() };
2811
                        LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(@"\Piping\Segment Breaks\Above\stream Number Break.sym", ref array, LabeledItem: item.AsLMRepresentation(), IsLeaderVisible: true);
2812
                    }
2813
                }
2814
2815
                Thread.Sleep(500);
2816
            }
2817
2818
            MessageBox.Show("End Modeling");
2819
        }
2820
        
2821
        private void btnPipeRunBreak_Click(object sender, EventArgs e)
2822
        {
2823
            Placement placement = new Placement();
2824
            LMADataSource dataSource = placement.PIDDataSource;
2825
2826
            string sFrom = textEdit1.EditValue.ToString();
2827
            string sTo = textEdit2.EditValue.ToString();
2828
2829
            LMModelItem item1 = dataSource.GetModelItem(sFrom);
2830
            LMModelItem item2 = dataSource.GetModelItem(sTo);
2831
            if (item1 != null && item1.Representations.Count == 1 &&
2832
                item2 != null && item2.Representations.Count == 1)
2833
            {
2834
                LMSymbol startSymbol = dataSource.GetSymbol(item1.Representations.Nth[1].Id);
2835
                LMSymbol endSymbol = dataSource.GetSymbol(item2.Representations.Nth[1].Id);
2836
2837
                if (startSymbol != null && endSymbol != null)
2838
                {
2839
                    FindRun(startSymbol, endSymbol, @"\Piping\Segment Breaks\End of Group.sym");
2840
                }
2841
                else
2842
                {
2843
                    MessageBox.Show("From To 심볼 확인");
2844
                }
2845
            }
2846
            else
2847
            {
2848
                MessageBox.Show("From To 심볼 확인");
2849
            }
2850
        }
2851
2852
        private void btnSegmentBreak_Click(object sender, EventArgs e)
2853
        {
2854
            Placement placement = new Placement();
2855
            LMADataSource dataSource = placement.PIDDataSource;
2856
2857
            string sFrom = textEdit1.EditValue.ToString();
2858
            string sTo = textEdit2.EditValue.ToString();
2859
2860
            LMModelItem item1 = dataSource.GetModelItem(sFrom);
2861
            LMModelItem item2 = dataSource.GetModelItem(sTo);
2862
            if (item1 != null && item1.Representations.Count == 1 &&
2863
                item2 != null && item2.Representations.Count == 1)
2864
            {
2865
                LMSymbol startSymbol = dataSource.GetSymbol(item1.Representations.Nth[1].Id);
2866
                LMSymbol endSymbol = dataSource.GetSymbol(item2.Representations.Nth[1].Id);
2867
2868
                if (startSymbol != null && endSymbol != null)
2869
                {
2870
                    FindRun(startSymbol, endSymbol, @"\Piping\Segment Breaks\Max Design Press - Temp.sym");
2871
                }
2872
                else
2873
                {
2874
                    MessageBox.Show("From To 심볼 확인");
2875
                }
2876
            }
2877
            else
2878
            {
2879
                MessageBox.Show("From To 심볼 확인");
2880
            }
2881
        }
2882 9fcc54bb gaqhf
2883
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
2884
        {
2885
            if (application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
2886
            {
2887
                double minX = 0;
2888
                double minY = 0;
2889
                double maxX = 0;
2890
                double maxY = 0;
2891 4d4dce52 esham21
                application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
2892
                application.ActiveWindow.ZoomArea2(minX - 0.01, minY - 0.01, maxX + 0.01, maxY + 0.01, null);
2893 9fcc54bb gaqhf
2894
                Thread.Sleep(milliseconds);
2895
            }
2896
        }
2897
2898
        private void ZoomObjectByGraphicOID(double x, double y, int milliseconds = 150)
2899
        {
2900 4d4dce52 esham21
            application.ActiveWindow.ZoomArea2(x - 0.01, y - 0.01, x + 0.01, y + 0.01, null);
2901 9fcc54bb gaqhf
            Thread.Sleep(milliseconds);
2902
        }
2903 27e624cd gaqhf
2904 9fcc54bb gaqhf
        #endregion
2905
2906 27e624cd gaqhf
        private void btnGetDrawingSize_Click(object sender, EventArgs e)
2907
        {
2908
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
2909 4d4dce52 esham21
            WrapperApplication wApp = new WrapperApplication(application.Application);
2910
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
2911 27e624cd gaqhf
2912
            if (radApp.ActiveSelectSet.Count > 0)
2913
            {
2914 4d4dce52 esham21
                DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject;
2915 27e624cd gaqhf
                if (line2D != null)
2916
                {
2917
                    double minX = 0;
2918
                    double minY = 0;
2919
                    double maxX = 0;
2920
                    double maxY = 0;
2921
                    line2D.Range(out minX, out minY, out maxX, out maxY);
2922
2923
                    Settings.Default.DrawingX = maxX - minX;
2924
                    Settings.Default.DrawingY = maxY - minY;
2925
                    Settings.Default.Save();
2926
2927
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
2928
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
2929
                }
2930
                else
2931
                    MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2932
            }
2933
            else
2934
                MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2935
        }
2936 41afada5 gaqhf
2937
        private void btnExportPDF_Click(object sender, EventArgs e)
2938
        {
2939
            dynamic dApplication = Microsoft.VisualBasic.Interaction.GetObject("", "PIDAutomation.Application");
2940 4d4dce52 esham21
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
2941
            Ingr.RAD2D.Application application = wApp.RADApplication;
2942 41afada5 gaqhf
            SaveFileDialog dialog = new SaveFileDialog();
2943
            dialog.FileName = application.ActiveDocument.Name.Replace(".pid", "");
2944
            dialog.Filter = "pdf file (*.pdf)|*.pdf";
2945
            if (dialog.ShowDialog() == DialogResult.OK)
2946
            {
2947
                application.ActiveDocument.SaveAsPDF(
2948
                dialog.FileName,
2949
                false,
2950
                Ingr.RAD2D.PDFResolutionConstants.igPDFResolution1200DPI,
2951
                Ingr.RAD2D.PDFColorModeConstants.igPDFColorModePureBlackAndWhite,
2952
                Ingr.RAD2D.PDFJpegCompressionConstants.igPDFJpegGoodQualityGoodCompression);
2953
            }
2954
        }
2955 9c451472 gaqhf
2956
        private void btnUploadDrawingAttribute_Click(object sender, EventArgs e)
2957
        {
2958
            OpenFileDialog openFileDialog = new OpenFileDialog();
2959
            openFileDialog.Filter = "excel files (*.xls;*.xlsx)|*.xls;*.xlsx";
2960
            openFileDialog.Multiselect = false;
2961
            if (openFileDialog.ShowDialog() == DialogResult.OK)
2962
            {
2963
                try
2964
                {
2965
                    DevExpress.Spreadsheet.IWorkbook workbook = new DevExpress.Spreadsheet.Workbook();
2966
                    workbook.LoadDocument(openFileDialog.FileName);
2967
2968
                    DevExpress.Spreadsheet.Worksheet worksheet = workbook.Worksheets[0];
2969
2970
                    DevExpress.Spreadsheet.Row headerNameRow = worksheet.Rows[0];
2971
                    DataTable dt = new DataTable();
2972
                    int colIndex = 0;
2973
                    while (true)
2974
                    {
2975
                        string colName = headerNameRow[colIndex].DisplayText;
2976
                        if (string.IsNullOrEmpty(colName))
2977
                            break;
2978
                        dt.Columns.Add(colName);
2979
                        colIndex++;
2980
                    }
2981
2982
                    int index = 1;
2983
                    while (true)
2984
                    {
2985
                        List<string> value = new List<string>();
2986
                        DevExpress.Spreadsheet.Row row = worksheet.Rows[index];
2987
                        string Drawing_Name = row[0].DisplayText;
2988
                        if (string.IsNullOrEmpty(Drawing_Name))
2989
                            break;
2990
2991
                        DataRow dataRow = dt.NewRow();
2992
                        for (int i = 0; i < dt.Columns.Count; i++)
2993
                        {
2994
                            string sValue = row[i].DisplayText;
2995
                            dataRow[i] = sValue;
2996
                        }
2997
                        dt.Rows.Add(dataRow);
2998
                        index++;
2999
                    }
3000
3001
                    foreach (DataRow row in dt.Rows)
3002
                    {
3003
                        string drawingName = row[0].ToString();
3004
                        LMDrawing drawing = GetDrawingByName(drawingName);
3005
                        if (drawing != null)
3006
                        {
3007
                            for (int i = 1; i < dt.Columns.Count; i++)
3008
                            {
3009
                                string value = row[i].ToString();
3010
                                if (string.IsNullOrEmpty(value))
3011
                                    continue;
3012
                                
3013
                                string attrName = dt.Columns[i].ColumnName;
3014
                                LMAAttribute attribute = drawing.Attributes[attrName];
3015
                                if (attribute != null)
3016 4d4dce52 esham21
                                    attribute.set_Value(value);
3017 9c451472 gaqhf
                            }
3018
                            drawing.Commit();
3019
                        }
3020
                    }
3021
3022
                    workbook.Dispose();
3023
                    MessageBox.Show("End upload drawing attribute", "SPPID Converter", MessageBoxButtons.OK, MessageBoxIcon.Information);
3024
                }
3025
                catch (Exception ex)
3026
                {
3027
                    MessageBox.Show(ex.Message);
3028
                }
3029
            }
3030
3031
            LMDrawing GetDrawingByName(string name)
3032
            {
3033
                Placement placement = new Placement();
3034
                LMADataSource dataSource = placement.PIDDataSource;
3035
3036
                LMAFilter filter = new LMAFilter();
3037
                LMACriterion criterion = new LMACriterion();
3038
                filter.ItemType = "Drawing";
3039
                criterion.SourceAttributeName = "Name";
3040
                criterion.Operator = "=";
3041 4d4dce52 esham21
                criterion.set_ValueAttribute(name);
3042
                filter.get_Criteria().Add(criterion);
3043 9c451472 gaqhf
3044
                LMDrawings drawings = new LMDrawings();
3045
                drawings.Collect(dataSource, Filter: filter);
3046
3047
                // Input Drawing Attribute
3048
                LMDrawing drawing = ((dynamic)drawings).Nth(1);
3049
                return drawing;
3050
            }
3051
        }
3052
3053 4d4dce52 esham21
        string fileName = string.Empty;
3054
        Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
3055
        private void btnRussia_Click(object sender, EventArgs e)
3056
        {
3057
            string noteSymbolPath = @"\Design\Annotation\Labels\DL_Item Note & Label.sym";
3058
            List<string> opcSymbolPath = new List<string>()
3059
            {
3060
                @"\Piping\Piping OPC's\Off-Drawing.sym",
3061
                @"\Instrumentation\Instrument OPC's\Off-Drawing.sym"
3062
            };
3063
3064
            if (string.IsNullOrEmpty(fileName))
3065
            {
3066
                OpenFileDialog openFileDialog = new OpenFileDialog();
3067
                openFileDialog.Filter = "excel files (*.xls;*.xlsx)|*.xls;*.xlsx";
3068
                openFileDialog.Multiselect = false;
3069
                if (openFileDialog.ShowDialog() == DialogResult.OK)
3070
                    fileName = openFileDialog.FileName;
3071
                else
3072
                    return;
3073
3074
                changeList();
3075
            }
3076
3077
            application.ActiveWindow.Zoom = 2000;
3078
3079
            Plaice.Placement placement = new Plaice.Placement();
3080
            LMADataSource dataSource = placement.PIDDataSource;
3081
            
3082
            ChangeNote();
3083
            ChangeOPC();
3084
3085
            void ChangeNote()
3086
            {
3087
                LMSymbols symbols = GetSymbols(noteSymbolPath);
3088
3089
                foreach (LMSymbol symbol in symbols)
3090
                {
3091
                    LMItemNote itemNote = dataSource.GetItemNote(symbol.ModelItemID);
3092
                    LMAAttribute attr = itemNote.Attributes["Note.Body"];
3093
                    object value = attr.get_Value();
3094
                    if (!DBNull.Value.Equals(value))
3095
                    {
3096
                        string sValue = value.ToString();
3097
                        string changeText = string.Empty;
3098
                        if (keyValuePairs.ContainsKey(sValue))
3099
                            changeText = keyValuePairs[sValue];
3100
3101
                        if (!string.IsNullOrEmpty(changeText))
3102
                        {
3103
                            CopyNoteItem(symbol, itemNote, changeText);
3104
                        }
3105
                    }
3106
                }
3107
            }
3108
            void ChangeOPC()
3109
            {
3110
                foreach (var file in opcSymbolPath)
3111
                {
3112
                    LMSymbols symbols = GetSymbols(file);
3113
                    foreach (LMSymbol symbol in symbols)
3114
                    {
3115
                        LMOPC opc = dataSource.GetOPC(symbol.ModelItemID);
3116
                        string gOID = symbol.get_GraphicOID().ToString();
3117
                        foreach (LMLabelPersist item in symbol.LabelPersists)
3118
                        {
3119
                            OPCText(item.get_GraphicOID().ToString());
3120
                        }
3121
                    }
3122
                }
3123
            }
3124
3125
            
3126 8f24b438 gaqhf
3127 4d4dce52 esham21
            void CopyNoteItem(LMSymbol symbol, LMItemNote itemNote, string text)
3128
            {
3129
                double x = symbol.get_XCoordinate();
3130
                double y = symbol.get_YCoordinate();
3131
                double rotate = symbol.get_RotationAngle();
3132
                ZoomObjectByGraphicOID(symbol.get_GraphicOID().ToString());
3133
3134
                LMSymbol newSymbol = placement.PIDPlaceSymbol(noteSymbolPath, x, y, Rotation: rotate);
3135
                LMItemNote newItemNote = dataSource.GetItemNote(newSymbol.ModelItemID);
3136
                LMAAttribute newAttr = newItemNote.Attributes["Note.Body"];
3137
                newAttr.set_Value(text);
3138
                newItemNote.Commit();
3139
                newSymbol.Commit();
3140
3141
                double[] symbolRange = null, newSymbolRange = null;
3142
                GetSPPIDSymbolRange(symbol.LabelPersists.Nth[1], ref symbolRange);
3143
                GetSPPIDSymbolRange(newSymbol.LabelPersists.Nth[1], ref newSymbolRange);
3144
3145
                Symbol2d moveSymbol2d = GetSymbol2DByRepID(dataSource, newSymbol.AsLMRepresentation().Id);
3146
                if (rotate > -0.1 && rotate < 0.1)
3147
                {
3148
                    moveSymbol2d.Move(0, 0, 0, symbolRange[1] - newSymbolRange[3]);
3149
                }
3150
                else
3151
                {
3152
                    moveSymbol2d.Move(0, 0, symbolRange[2] - newSymbolRange[0], 0);
3153
                }
3154
            }
3155
            
3156
            LMSymbols GetSymbols(string file)
3157
            {
3158
                LMAFilter filter = new LMAFilter();
3159
                LMACriterion criterion1 = new LMACriterion();
3160
                criterion1.SourceAttributeName = "SP_DRAWINGID";
3161
                criterion1.Operator = "=";
3162
                criterion1.set_ValueAttribute(dataSource.PIDMgr.Drawing.ID);
3163
                criterion1.Conjunctive = true;
3164
                filter.get_Criteria().Add(criterion1);
3165
                filter.ItemType = "Symbol";
3166
3167
                LMACriterion criterion2 = new LMACriterion();
3168
                criterion2.SourceAttributeName = "ITEMSTATUS";
3169
                criterion2.Operator = "=";
3170
                criterion2.set_ValueAttribute("1");
3171
                criterion2.Conjunctive = true;
3172
                filter.get_Criteria().Add(criterion2);
3173
3174
                LMACriterion criterion3 = new LMACriterion();
3175
                criterion3.SourceAttributeName = "INSTOCKPILE";
3176
                criterion3.Operator = "=";
3177
                criterion3.set_ValueAttribute("1");
3178
                criterion3.Conjunctive = true;
3179
                filter.get_Criteria().Add(criterion3);
3180
3181
                LMACriterion criterion4 = new LMACriterion();
3182
                criterion4.SourceAttributeName = "FILENAME";
3183
                criterion4.Operator = "=";
3184
                criterion4.set_ValueAttribute(file);
3185
                criterion4.Conjunctive = true;
3186
                filter.get_Criteria().Add(criterion4);
3187
3188
                LMSymbols items = new LMSymbols();
3189
                items.Collect(dataSource, Filter: filter);
3190
3191
                return items;
3192
            }
3193
3194
            void changeList()
3195
            {
3196
                DevExpress.Spreadsheet.Workbook workbook = new DevExpress.Spreadsheet.Workbook();
3197
                try
3198
                {
3199
                    workbook.LoadDocument(fileName);
3200
                    DevExpress.Spreadsheet.Worksheet sheet = workbook.Worksheets["SPPIDConverter"];
3201
                    if (sheet == null)
3202
                        return;
3203
3204
                    int index = 0;
3205
                    while (true)
3206
                    {
3207
                        DevExpress.Spreadsheet.Row row = sheet.Rows[index];
3208
                        string from = row[0].DisplayText;
3209
                        string to = row[1].DisplayText;
3210
                        if (string.IsNullOrEmpty(from) || string.IsNullOrEmpty(to))
3211
                            break;
3212
                        else if(!keyValuePairs.ContainsKey(from))
3213
                            keyValuePairs.Add(from, to);
3214
                        index++;
3215
                    }
3216
3217
                }
3218
                catch (Exception ex)
3219
                {
3220
                    MessageBox.Show("error");
3221
                }
3222
                finally
3223
                {
3224
                    workbook.Dispose();
3225
                }
3226
            }
3227
3228
            void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3229
            {
3230
                if (application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3231
                {
3232
                    double minX = 0;
3233
                    double minY = 0;
3234
                    double maxX = 0;
3235
                    double maxY = 0;
3236
                    application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3237
                    application.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3238
3239
                    Thread.Sleep(milliseconds);
3240
                }
3241
            }
3242
3243
            void GetSPPIDSymbolRange(LMLabelPersist labelPersist, ref double[] range)
3244
            {
3245
                if (labelPersist != null)
3246
                {
3247
                    double x1 = double.MaxValue;
3248
                    double y1 = double.MaxValue;
3249
                    double x2 = double.MinValue;
3250
                    double y2 = double.MinValue;
3251
                    range = new double[] { x1, y1, x2, y2 };
3252
3253
                    Ingr.RAD2D.DependencyObject dependency = application.ActiveDocument.ActiveSheet.DrawingObjects[labelPersist.get_GraphicOID().ToString()] as DependencyObject;
3254
                    foreach (var item in dependency.DrawingObjects)
3255
                    {
3256
                        Ingr.RAD2D.TextBox textBox = item as Ingr.RAD2D.TextBox;
3257
                        if (textBox != null)
3258
                        {
3259
                            if (dependency != null)
3260
                            {
3261
                                double tempX1;
3262
                                double tempY1;
3263
                                double tempX2;
3264
                                double tempY2;
3265
                                textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
3266
                                x1 = Math.Min(x1, tempX1);
3267
                                y1 = Math.Min(y1, tempY1);
3268
                                x2 = Math.Max(x2, tempX2);
3269
                                y2 = Math.Max(y2, tempY2);
3270
3271
                                range = new double[] { x1, y1, x2, y2 };
3272
                            }
3273
                        }
3274
                    }
3275
3276
                }
3277
            }
3278
3279
            void OPCText(string graphicOID)
3280
            {
3281
                if (application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3282
                {
3283
                    DependencyObject dependency = application.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] as DependencyObject;
3284
                    foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
3285
                    {
3286
                        Ingr.RAD2D.TextBox textBox = drawingObject as Ingr.RAD2D.TextBox;
3287
                        if (textBox != null)
3288
                        {
3289
                            string text = textBox.Text;
3290
                            if (!string.IsNullOrEmpty(text))
3291
                            {
3292
                                string changeText;
3293
                                ChangeText(text, out changeText);
3294
                                
3295
                                if (!text.Equals(changeText))
3296
                                {
3297
                                    double tempX1, tempY1, tempX2, tempY2;
3298
                                    textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
3299
3300
                                    double x = tempX1, y = tempY1;
3301
3302
                                    LMSymbol newSymbol = placement.PIDPlaceSymbol(noteSymbolPath, x, y);
3303
                                    LMItemNote newItemNote = dataSource.GetItemNote(newSymbol.ModelItemID);
3304
                                    LMAAttribute newAttr = newItemNote.Attributes["Note.Body"];
3305
                                    newAttr.set_Value(changeText);
3306
                                    newItemNote.Commit();
3307
                                    newSymbol.Commit();
3308
3309
                                    double[] newSymbolRange = null;
3310
                                    GetSPPIDSymbolRange(newSymbol.LabelPersists.Nth[1], ref newSymbolRange);
3311
                                    Symbol2d moveSymbol2d = GetSymbol2DByRepID(dataSource, newSymbol.AsLMRepresentation().Id);
3312
                                    moveSymbol2d.Move(newSymbolRange[2], newSymbolRange[1], tempX1, tempY1);
3313
3314
                                    ZoomObjectByGraphicOID(graphicOID);
3315
                                }
3316
                            }
3317
                        }
3318
                    }
3319
                }
3320
            }
3321
3322
            void ChangeText(string from, out string to)
3323
            {
3324
                to = from;
3325
3326
                foreach (var item in keyValuePairs)
3327
                {
3328
                    string value = item.Key;
3329
                    if (to.Contains(value))
3330
                        to = to.Replace(value, item.Value);
3331
                }
3332
            }
3333
        }
3334 65881d60 gaqhf
    }
3335
}
클립보드 이미지 추가 (최대 크기: 500 MB)