프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ 919bb48e

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