프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ a31a512e

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