프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ 442bd51e

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