프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ b427ebf9

이력 | 보기 | 이력해설 | 다운로드 (17.4 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 1ba9c671 gaqhf
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 65881d60 gaqhf
26
namespace Converter.SPPID.Wrapper
27
{
28
    public partial class ConverterDocking : UserControl
29
    {
30 d4c3e39f gaqhf
        
31 65881d60 gaqhf
        public ConverterDocking()
32
        {
33
            InitializeComponent();
34 d4c3e39f gaqhf
            
35 ca6e0f51 gaqhf
            
36 6a7573b0 gaqhf
            try
37
            {
38
                textEditDrawingX.EditValue = Settings.Default.DrawingX;
39
                textEditDrawingY.EditValue = Settings.Default.DrawingY;
40
            }
41
            catch (Exception ex)
42
            {
43
                StringBuilder sb = new StringBuilder();
44
                sb.AppendLine(ex.Message);
45
                sb.AppendLine(ex.StackTrace);
46
                MessageBox.Show(sb.ToString());
47
            }
48 65881d60 gaqhf
        }
49
50 e3e2d41f gaqhf
        private void btnConverter_Click(object sender, EventArgs e)
51 65881d60 gaqhf
        {
52 1ba9c671 gaqhf
            ConverterForm converterForm = new ConverterForm();
53
            if (converterForm.ShowDialog() == DialogResult.OK)
54 65881d60 gaqhf
            {
55 1ba9c671 gaqhf
                try
56
                {
57
                    CloseOPCForm.Run();
58 ca214bc3 gaqhf
59 d5ec4d0f gaqhf
                    for (int i = 0; i < converterForm.Documents.Count; i++)
60 1ba9c671 gaqhf
                    {
61 d5ec4d0f gaqhf
                        SPPID_Document document = converterForm.Documents[i];
62 1ba9c671 gaqhf
                        if (document.SetSPPIDMapping() && document.Enable)
63
                        {
64 d4c3e39f gaqhf
                            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
65
                            WrapperApplication wApp = new WrapperApplication(application.Application);
66
                            Ingr.RAD2D.Application radApp = wApp.RADApplication;
67
68 442bd51e gaqhf
                            AutoModeling modeling = new AutoModeling(document, application, radApp, converterForm.checkEditCloseDocument.Checked);
69 d5ec4d0f gaqhf
                            modeling.DocumentLabelText = string.Format("Drawing Name : {0} ({1}/{2})", document.DrawingName, i + 1, converterForm.Documents.Count);
70 1ba9c671 gaqhf
                            modeling.Run();
71 d4c3e39f gaqhf
72
                            ReleaseCOMObjects(application);
73 1ba9c671 gaqhf
                        }
74
                    }
75
                }
76
                catch (Exception ex)
77
                {
78
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
79
                }
80
                finally
81 d19ae675 gaqhf
                {
82 1ba9c671 gaqhf
                    CloseOPCForm.Stop();
83
                }
84
85
                MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
86
            }
87
        }
88
89 69b7387a gaqhf
        private void btnLinkOPC_Click(object sender, EventArgs e)
90
        {
91 d4c3e39f gaqhf
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
92
            WrapperApplication wApp = new WrapperApplication(application.Application);
93
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
94
95 4fb0f8d5 gaqhf
            LMADataSource dataSource = new LMADataSource();
96
            LMDrawings drawings = new LMDrawings();
97
98 88bac50c gaqhf
            //try
99 69b7387a gaqhf
            {
100 4fb0f8d5 gaqhf
                Project_Info _ProjectInfo = Project_Info.GetInstance();
101
                _ProjectInfo.DefaultPath = Settings.Default.LatestProjectPath;
102
                if (Project_DB.ConnTestAndCreateTable())
103
                {
104 224535bb gaqhf
                    DataTable dt = Project_DB.SelectSPPID_DB_INFO();
105
                    if (dt.Columns.Count > 0 && dt.Rows.Count > 0)
106
                        SPPIDUtil.ConvertToSPPIDInfo(dt.Rows[0][0].ToString());
107
                    else
108
                        SPPID_DBInfo.Clear();
109
110
                    SPPID_DBInfo sPPID_DBInfo = SPPID_DBInfo.GetInstance();
111
                    if (sPPID_DBInfo.Enable)
112
                    {
113
                        drawings.Collect(dataSource);
114 69b7387a gaqhf
115 224535bb gaqhf
                        DataTable drawingTable = Project_DB.SelectDrawingInfo();
116
                        drawingTable.Columns.Add("EXIST", typeof(bool));
117
                        drawingTable.Columns.Add("SPPIDPATH", typeof(string));
118 e8536f2b gaqhf
119 224535bb gaqhf
                        foreach (LMDrawing item in drawings)
120
                        {
121
                            DataRow[] rows = drawingTable.Select(string.Format("DRAWINGNUMBER = '{0}'", item.Attributes["DrawingNumber"].get_Value()));
122
                            foreach (DataRow row in rows)
123
                            {
124
                                row["EXIST"] = true;
125
                                row["DRAWINGNAME"] = item.Attributes["Name"].get_Value();
126
                                row["SPPIDPATH"] = item.get_Path();
127
                            }
128
                        }
129 69b7387a gaqhf
130 1ed39474 gaqhf
                        List<SPPID_Document> allDocuments = new List<SPPID_Document>();
131 224535bb gaqhf
                        foreach (DataRow row in drawingTable.Rows)
132 88bac50c gaqhf
                            allDocuments.Add((SPPID_Document)row["DOCUMENT"]);
133 1ed39474 gaqhf
134 88bac50c gaqhf
                        AutoModeling_OPC opc = new AutoModeling_OPC(allDocuments, application, radApp);
135 1ed39474 gaqhf
                        opc.Run();
136 224535bb gaqhf
                    }
137 4fb0f8d5 gaqhf
                }
138
                else
139
                {
140
                    MessageBox.Show(Msg.ConnectionFail, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
141
                }
142 69b7387a gaqhf
            }
143 88bac50c gaqhf
            //catch (Exception ex)
144 69b7387a gaqhf
            {
145 88bac50c gaqhf
                //MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
146 69b7387a gaqhf
            }
147 88bac50c gaqhf
            //finally
148 4fb0f8d5 gaqhf
            {
149
                ReleaseCOMObjects(dataSource);
150
                ReleaseCOMObjects(drawings);
151
            }
152 69b7387a gaqhf
        }
153
154 4fb0f8d5 gaqhf
        public void ReleaseCOMObjects(params object[] objVars)
155
        {
156
            int intNewRefCount = 0;
157
            foreach (object obj in objVars)
158
            {
159
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
160
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
161
            }
162
        }
163
164 6a7573b0 gaqhf
        private void btnGetDrawingSize_Click(object sender, EventArgs e)
165 1ba9c671 gaqhf
        {
166 d4c3e39f gaqhf
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
167
            WrapperApplication wApp = new WrapperApplication(application.Application);
168
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
169
170 6a7573b0 gaqhf
            if (radApp.ActiveSelectSet.Count > 0)
171 65881d60 gaqhf
            {
172 6a7573b0 gaqhf
                DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject;
173
                if (line2D != null)
174 1ba9c671 gaqhf
                {
175 6a7573b0 gaqhf
                    double minX = 0;
176
                    double minY = 0;
177
                    double maxX = 0;
178
                    double maxY = 0;
179
                    line2D.Range(out minX, out minY, out maxX, out maxY);
180
181
                    Settings.Default.DrawingX = maxX - minX;
182
                    Settings.Default.DrawingY = maxY - minY;
183
                    Settings.Default.Save();
184
185
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
186
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
187 1ba9c671 gaqhf
                }
188 6a7573b0 gaqhf
                else
189
                    MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
190 65881d60 gaqhf
            }
191 6a7573b0 gaqhf
            else
192
                MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
193
        }
194 7e4a64a3 gaqhf
195
        #region TEST
196 ca6e0f51 gaqhf
        Placement _placement = new Placement();
197
        LMADataSource dataSource;
198 63fbf592 gaqhf
        bool first = true;
199
        LMDrawing currentDrawing;
200 6a7573b0 gaqhf
        private void simpleButton1_Click(object sender, EventArgs e)
201
        {
202 ca6e0f51 gaqhf
            dataSource = _placement.PIDDataSource;
203 32205389 gaqhf
204 ca6e0f51 gaqhf
            string projectNumber = dataSource.ProjectNumber;
205
            LMActiveProject activeProject = dataSource.GetActiveProject();
206
            LMAEnumAttLists enumAttLists = dataSource.CodeLists;
207
208
            AutoJoinPipeRun();
209
        }
210
        private void AutoJoinPipeRun()
211
        {
212 d4c3e39f gaqhf
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
213
            WrapperApplication wApp = new WrapperApplication(application.Application);
214
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
215
216 ca6e0f51 gaqhf
            string modelItemId = null;
217
            List<double[]> vertices = new List<double[]>();
218 a31a512e gaqhf
            if (radApp.ActiveSelectSet.Count == 0)
219
            {
220
                return;
221
            }
222 ca6e0f51 gaqhf
            dynamic OID = radApp.ActiveSelectSet[0].Key();
223
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
224
            foreach (var attributes in drawingObject.AttributeSets)
225 4ba01591 gaqhf
            {
226 ca6e0f51 gaqhf
                foreach (var attribute in attributes)
227 4ba01591 gaqhf
                {
228 ca6e0f51 gaqhf
                    if (attribute.Name == "ModelID")
229
                        modelItemId = attribute.GetValue().ToString();
230
                }
231
            }
232
            radApp.ActiveSelectSet.RemoveAll();
233 32205389 gaqhf
234 63fbf592 gaqhf
235 ca6e0f51 gaqhf
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
236 dec9ecfd gaqhf
            LMPipeRun run = dataSource.GetPipeRun(modelItem.Id);
237
            dynamic dd = run.get_Name();
238
            LMAAttribute att = run.Attributes["FileName"];
239
            //foreach (LMRepresentation rep in modelItem.Representations)
240
            //{
241
            //    if (!DBNull.Value.Equals(rep.get_FileName()))
242
            //    {
243
            //        rep.set_FileName(@"\Instrumentation\Signal Line\Connect to Process.sym");
244
            //    }
245
            //}
246 ca6e0f51 gaqhf
247
248
        }
249 2b8bbe9a gaqhf
        private void JoinRun(string modelId, string mainModelId, ref string survivorId)
250 ca6e0f51 gaqhf
        {
251 2b8bbe9a gaqhf
            try
252 ca6e0f51 gaqhf
            {
253 2b8bbe9a gaqhf
                LMModelItem modelItem1 = dataSource.GetModelItem(modelId);
254
                _LMAItem item1 = modelItem1.AsLMAItem();
255
                LMModelItem modelItem2 = dataSource.GetModelItem(mainModelId);
256
                _LMAItem item2 = modelItem2.AsLMAItem();
257 1ff0105e gaqhf
258 2b8bbe9a gaqhf
                // item2가 item1으로 조인
259
                _placement.PIDJoinRuns(ref item1, ref item2);
260
                item1.Commit();
261
                item2.Commit();
262
263
                string beforeID = string.Empty;
264
                string afterID = string.Empty;
265
266
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
267
                {
268
                    beforeID = modelItem2.Id;
269
                    afterID = modelItem1.Id;
270
                    survivorId = afterID;
271
                }
272
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
273
                {
274
                    beforeID = modelItem1.Id;
275
                    afterID = modelItem2.Id;
276
                    survivorId = afterID;
277 ca6e0f51 gaqhf
                }
278 2b8bbe9a gaqhf
                else if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() == "Active")
279
                    survivorId = null;
280
                else
281 a31a512e gaqhf
                {
282
                    Log.Write("잘못된 경우");
283
                    survivorId = null;
284
                }
285
                    
286 2b8bbe9a gaqhf
287
                //if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
288
                //{
289
                //    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
290
                //    foreach (var line in lines)
291
                //        line.SPPID.ModelItemId = afterID;
292
                //}
293
294
                ReleaseCOMObjects(modelItem1);
295
                ReleaseCOMObjects(item1);
296
                ReleaseCOMObjects(modelItem2);
297
                ReleaseCOMObjects(item2);
298
            }
299
            catch (Exception ex)
300
            {
301
                Log.Write("Join Error");
302
                Log.Write(ex.Message);
303 ca6e0f51 gaqhf
            }
304
        }
305 1ff0105e gaqhf
306 a31a512e gaqhf
        private void JoinRunBySameType(string modelItemId, ref string survivorId)
307 ca6e0f51 gaqhf
        {
308
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
309
            if (modelItem != null)
310
            {
311
                foreach (LMRepresentation rep in modelItem.Representations)
312
                {
313
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
314
                    {
315
                        LMConnector connector = dataSource.GetConnector(rep.Id);
316
                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
317
                        {
318
                            LMSymbol symbol = connector.ConnectItem1SymbolObject;
319 a31a512e gaqhf
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
320
                            if (modelItemIds.Count == 1)
321
                            {
322
                                JoinRun(modelItemIds[0], modelItemId, ref survivorId);
323
                                if (survivorId != null)
324
                                    break;
325
                            }
326 ca6e0f51 gaqhf
                        }
327
                        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
328
                        {
329
                            LMSymbol symbol = connector.ConnectItem2SymbolObject;
330 a31a512e gaqhf
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
331
                            if (modelItemIds.Count == 1)
332
                            {
333
                                JoinRun(modelItemIds[0], modelItemId, ref survivorId);
334
                                if (survivorId != null)
335
                                    break;
336
                            }
337 ca6e0f51 gaqhf
                        }
338 32205389 gaqhf
                    }
339 1ff0105e gaqhf
                }
340 2b8bbe9a gaqhf
            }
341 ca6e0f51 gaqhf
        }
342 7e4a64a3 gaqhf
343 ca6e0f51 gaqhf
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
344
        {
345
            List<string> modelItemIDs = new List<string>();
346
            foreach (LMConnector connector in symbol.Connect1Connectors)
347
            {
348
                LMModelItem modelItem = connector.ModelItemObject;
349
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
350
                    modelItemIDs.Add(modelItem.Id);
351
                ReleaseCOMObjects(modelItem);
352 a31a512e gaqhf
                ReleaseCOMObjects(connector);
353 ca6e0f51 gaqhf
            }
354
355
            foreach (LMConnector connector in symbol.Connect2Connectors)
356
            {
357
                LMModelItem modelItem = connector.ModelItemObject;
358
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
359
                    modelItemIDs.Add(modelItem.Id);
360
                ReleaseCOMObjects(modelItem);
361 a31a512e gaqhf
                ReleaseCOMObjects(connector);
362 ca6e0f51 gaqhf
            }
363 7e4a64a3 gaqhf
364 ca6e0f51 gaqhf
            return modelItemIDs;
365 65881d60 gaqhf
        }
366 ca6e0f51 gaqhf
367
        private void FindOtherModelItemBySymbolWhereTypePipeRunLoop()
368
        {
369
370
        }
371
372
        private void JoinRunOnlyPipeRun()
373
        {
374
375
        }
376
377
        private void GetConnectPointsCount(LMSymbol symbol,ref int pipeRunCount, ref int signalCount)
378
        {
379
            string typeName = symbol.ModelItemObject.get_ItemTypeName().ToString();
380
            if (typeName == "Instrument")
381
            {
382
                LMInstrument item = dataSource.GetInstrument(symbol.ModelItemID);
383
                pipeRunCount = item.PipingPoints.Count;
384
                signalCount = item.SignalPoints.Count;
385
                ReleaseCOMObjects(item);
386
            }
387
            else if (typeName == "PipingComp")
388
            {
389
                LMPipingComp item = dataSource.GetPipingComp(symbol.ModelItemID);
390
                pipeRunCount = item.PipingPoints.Count;
391
                signalCount = item.SignalPoints.Count;
392
                ReleaseCOMObjects(item);
393
            }
394
        }
395
396
        
397
398 7e4a64a3 gaqhf
        private void TESTLine()
399
        {
400 d4c3e39f gaqhf
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
401
            WrapperApplication wApp = new WrapperApplication(application.Application);
402
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
403
404 7e4a64a3 gaqhf
            Placement _placement = new Placement();
405
            LMADataSource dataSource = _placement.PIDDataSource;//placement.PIDDataSource;
406
407
            foreach (var attributes in radApp.ActiveSelectSet[0].AttributeSets)
408
            {
409
                foreach (var attribute in attributes)
410
                {
411
                    if (attribute.Name == "ModelID")
412
                    {
413
                        LMModelItem modelItem = dataSource.GetModelItem(attribute.GetValue().ToString());
414
                        foreach (LMRepresentation representation  in modelItem.Representations)
415
                        {
416
                            //representa
417
                        }
418
                    }
419
                }
420
            }
421
        }
422
        #endregion
423
424 7aee331b gaqhf
425 9628f54b gaqhf
        [DllImport("user32.dll")]
426
        public static extern int FindWindow(string lpClassName, string lpWindowName);
427 3734dcc5 gaqhf
428
        [DllImport("user32.dll", SetLastError = true)]
429
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
430
431 b427ebf9 humkyung
        private void ConverterDocking_Load(object sender, EventArgs e)
432
        {
433
#if DEBUG
434
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
435
#else
436
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
437
#endif
438
439
        }
440 65881d60 gaqhf
    }
441
}