프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ 154d8f43

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

1
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
using System.IO;
12
using Microsoft.VisualBasic;
13
using Ingr.RAD2D;
14
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
using Plaice;
21
using Llama;
22
using DevExpress.XtraSplashScreen;
23
using Newtonsoft.Json;
24
using System.Runtime.InteropServices;
25
using System.Reflection;
26
using Converter.SPPID.OPC;
27

    
28
namespace Converter.SPPID.Wrapper
29
{
30
    public partial class ConverterDocking : UserControl
31
    {
32
        Ingr.RAD2D.Application application;
33
        public ConverterDocking()
34
        {
35
            InitializeComponent();
36

    
37
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
38
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
39
            application = wApp.RADApplication;
40
            
41
            ReleaseCOMObjects(dApplication);
42
            dApplication = null;
43
            try
44
            {
45
                textEditDrawingX.EditValue = Settings.Default.DrawingX;
46
                textEditDrawingY.EditValue = Settings.Default.DrawingY;
47

    
48
                Project_Info _ProjectInfo = Project_Info.GetInstance();
49
                _ProjectInfo.DefaultPath = Settings.Default.LatestProjectPath;
50
            }
51
            catch (Exception ex)
52
            {
53
                StringBuilder sb = new StringBuilder();
54
                sb.AppendLine(ex.Message);
55
                sb.AppendLine(ex.StackTrace);
56
                MessageBox.Show(sb.ToString());
57
            }
58
        }
59

    
60
        /// <summary>
61
        /// 선택한 도면들을 읽어서 SPPID로 변환한다.
62
        /// </summary>
63
        /// <param name="sender"></param>
64
        /// <param name="e"></param>
65
        private void btnConverter_Click(object sender, EventArgs e)
66
        {
67
            ConverterForm converterForm = new ConverterForm();
68
            if (converterForm.ShowDialog() == DialogResult.OK)
69
            {
70
                //Ingr.RAD2D.Document doc = application.Documents.Add();
71

    
72
                try
73
                {
74
                    CloseOPCForm.Run();
75

    
76
                    for (int i = 0; i < converterForm.Documents.Count; i++)
77
                    {
78
                        SPPID_Document document = converterForm.Documents[i];
79
                        if (document.SetSPPIDMapping() && document.Enable)
80
                        {
81
                            using (AutoModeling modeling = new AutoModeling(document, converterForm.checkEditCloseDocument.Checked))
82
                            {
83
                                modeling.DocumentLabelText = string.Format("Drawing Name : {0} ({1}/{2})", document.DrawingName, i + 1, converterForm.Documents.Count);
84
                                modeling.Run();
85
                            }
86
                        }
87
                    }
88
                }
89
                catch (Exception ex)
90
                {
91
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
92
                }
93
                finally
94
                {
95
                    CloseOPCForm.Stop();
96

    
97
                    //doc.SaveOnClose = false;
98
                    //doc.Close(false);
99

    
100
                    //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
101
                    //dApplication.Drawings[1].CloseDrawing(false);
102
                    //ReleaseCOMObjects(dApplication);
103
                    //dApplication = null;
104
                }
105

    
106
                MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
107
            }
108
        }
109

    
110
        private void btnLinkOPC_Click(object sender, EventArgs e)
111
        {
112
            DataTable tOPCInfo = Project_DB.SelectOPCInfo();
113
            DataTable tOPCRelations = Project_DB.SelectOPCRelations();
114
            DataTable tDrawingInfo = Project_DB.SelectDrawingInfo();
115
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
116

    
117
            foreach (DataRow row in tOPCInfo.Rows)
118
            {
119
                if (!Convert.ToBoolean(row["PAIRED"]))
120
                {
121
                    string drawingUID = row["ID2_DRAWING_UID"].ToString();
122
                    string OPCUID = row["ID2_OPC_UID"].ToString();
123
                    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));
124

    
125
                    if (rows.Length == 2)
126
                    {
127
                        string fromDrawingsUID1 = rows[0]["From_Drawings_UID"].ToString();
128
                        string fromDrawingsUID2 = rows[1]["From_Drawings_UID"].ToString();
129
                        string fromOPCUID1 = rows[0]["From_OPC_UID"].ToString();
130
                        string fromOPCUID2 = rows[1]["From_OPC_UID"].ToString();
131
                        string toDrawingsUID1 = rows[0]["To_Drawings_UID"].ToString();
132
                        string toDrawingsUID2 = rows[1]["To_Drawings_UID"].ToString();
133
                        string toOPCUID1 = rows[0]["To_OPC_UID"].ToString();
134
                        string toOPCUID2 = rows[1]["To_OPC_UID"].ToString();
135

    
136
                        DataRow[] fromDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", fromDrawingsUID1));
137
                        DataRow[] toDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", toDrawingsUID1));
138

    
139
                        if (fromOPCUID1 == toOPCUID2 && fromOPCUID2 == toOPCUID1 && fromDrawing.Length == 1 && toDrawing.Length == 1)
140
                        {
141
                            DataRow fromOPCInfoRow = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", fromOPCUID1))[0];
142
                            DataRow toOPCInfoRow = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", toOPCUID1))[0];
143
                            string fromOPCModelId = fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
144
                            string toOPCModelId = toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
145
                            string toDrawingName = toDrawing[0]["DRAWINGNAME"].ToString();
146
                            AutoModeling_OPC opc = new AutoModeling_OPC(dApplication, application, fromOPCModelId, toOPCModelId, toDrawingName);
147
                            if (opc.Run())
148
                            {
149
                                fromOPCInfoRow["PAIRED"] = true;
150
                                toOPCInfoRow["PAIRED"] = true;
151

    
152
                                Project_DB.InsertOPCInfo(fromOPCInfoRow["ID2_OPC_UID"].ToString(), fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), fromOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
153
                                Project_DB.InsertOPCInfo(toOPCInfoRow["ID2_OPC_UID"].ToString(), toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), toOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
154
                            }
155
                            break;
156
                        }
157
                    }
158
                }
159
            }
160

    
161
            tOPCInfo.Dispose();
162
            tOPCRelations.Dispose();
163
            tDrawingInfo.Dispose();
164
            ReleaseCOMObjects(dApplication);
165
            dApplication = null;
166
            //dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
167
            //WrapperApplication wApp = new WrapperApplication(application.Application);
168
            //Ingr.RAD2D.Application radApp = wApp.RADApplication;
169

    
170
            //LMADataSource dataSource = new LMADataSource();
171
            //LMDrawings drawings = new LMDrawings();
172

    
173
            ////try
174
            //{
175
            //    Project_Info _ProjectInfo = Project_Info.GetInstance();
176
            //    _ProjectInfo.DefaultPath = Settings.Default.LatestProjectPath;
177
            //    if (Project_DB.ConnTestAndCreateTable())
178
            //    {
179
            //        DataTable dt = Project_DB.SelectSPPID_DB_INFO();
180
            //        if (dt.Columns.Count > 0 && dt.Rows.Count > 0)
181
            //            SPPIDUtil.ConvertToSPPIDInfo(dt.Rows[0][0].ToString());
182
            //        else
183
            //            SPPID_DBInfo.Clear();
184

    
185
            //        SPPID_DBInfo sPPID_DBInfo = SPPID_DBInfo.GetInstance();
186
            //        if (sPPID_DBInfo.Enable)
187
            //        {
188
            //            drawings.Collect(dataSource);
189

    
190
            //            DataTable drawingTable = Project_DB.SelectDrawingInfo();
191
            //            drawingTable.Columns.Add("EXIST", typeof(bool));
192
            //            drawingTable.Columns.Add("SPPIDPATH", typeof(string));
193

    
194
            //            foreach (LMDrawing item in drawings)
195
            //            {
196
            //                DataRow[] rows = drawingTable.Select(string.Format("DRAWINGNUMBER = '{0}'", item.Attributes["DrawingNumber"].get_Value()));
197
            //                foreach (DataRow row in rows)
198
            //                {
199
            //                    row["EXIST"] = true;
200
            //                    row["DRAWINGNAME"] = item.Attributes["Name"].get_Value();
201
            //                    row["SPPIDPATH"] = item.get_Path();
202
            //                }
203
            //            }
204

    
205
            //            List<SPPID_Document> allDocuments = new List<SPPID_Document>();
206
            //            foreach (DataRow row in drawingTable.Rows)
207
            //                allDocuments.Add((SPPID_Document)row["DOCUMENT"]);
208

    
209
            //            AutoModeling_OPC opc = new AutoModeling_OPC(allDocuments, application, radApp);
210
            //            opc.Run();
211
            //        }
212
            //    }
213
            //    else
214
            //    {
215
            //        MessageBox.Show(Msg.ConnectionFail, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
216
            //    }
217
            //}
218
            ////catch (Exception ex)
219
            //{
220
            //    //MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
221
            //}
222
            ////finally
223
            //{
224
            //    ReleaseCOMObjects(dataSource);
225
            //    ReleaseCOMObjects(drawings);
226
            //}
227
        }
228

    
229
        public void ReleaseCOMObjects(params object[] objVars)
230
        {
231
            int intNewRefCount = 0;
232
            foreach (object obj in objVars)
233
            {
234
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
235
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
236
            }
237
        }
238

    
239
        private void btnGetDrawingSize_Click(object sender, EventArgs e)
240
        {
241
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
242
            WrapperApplication wApp = new WrapperApplication(application.Application);
243
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
244

    
245
            if (radApp.ActiveSelectSet.Count > 0)
246
            {
247
                DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject;
248
                if (line2D != null)
249
                {
250
                    double minX = 0;
251
                    double minY = 0;
252
                    double maxX = 0;
253
                    double maxY = 0;
254
                    line2D.Range(out minX, out minY, out maxX, out maxY);
255

    
256
                    Settings.Default.DrawingX = maxX - minX;
257
                    Settings.Default.DrawingY = maxY - minY;
258
                    Settings.Default.Save();
259

    
260
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
261
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
262
                }
263
                else
264
                    MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
265
            }
266
            else
267
                MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
268
        }
269

    
270
        #region TEST
271
        private void simpleButton1_Click(object sender, EventArgs e)
272
        {
273
            return;
274
            Thread outThread = new Thread(func2 =>
275
            {
276
                for (int i = 100; i < 120; i++)
277
                {
278
                    Thread thread = new Thread(func =>
279
                    {
280
                        dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
281
                        //WrapperApplication wApp = new WrapperApplication(application.Application);
282
                        //Ingr.RAD2D.Application radApp = wApp.RADApplication;
283
                        dynamic drawing = application.Drawings.Add("Unit1", "D-Size.pid", i.ToString(), i.ToString());//application.Drawings.OpenDrawing("123");
284

    
285
                        Thread.Sleep(5000);
286

    
287
                        drawing.CloseDrawing(true);
288

    
289
                        //application.Quit();
290
                        ReleaseCOMObjects(application);
291
                        application = null;
292
                        ReleaseCOMObjects(drawing);
293
                        drawing = null;
294
                        Thread.Sleep(5000);
295
                    });
296
                    thread.Start();
297
                    thread.Join();
298
                }
299
            });
300
            outThread.Start();
301
            
302
            
303

    
304

    
305

    
306
            //radApp.Documents.Add(@"\\server70\DEVDOF1Site\GS\PLANT\PLANT1\Unit1\11111.pid");
307

    
308
            //radApp.ActiveDocument.Close(true);
309
            //drawing.CloseDrawing(true);
310

    
311
            //ReleaseCOMObjects(application);
312
            //ReleaseCOMObjects(drawing);
313
            //application = null;
314
            //wApp = null;
315
            //radApp = null;
316
            //drawing = null;
317
        }
318

    
319
        private void AutoJoinPipeRun()
320
        {
321
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
322
            WrapperApplication wApp = new WrapperApplication(application.Application);
323
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
324

    
325
            string modelItemId = null;
326
            List<double[]> vertices = new List<double[]>();
327
            if (radApp.ActiveSelectSet.Count == 0)
328
            {
329
                return;
330
            }
331
            dynamic OID = radApp.ActiveSelectSet[0].Key();
332
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
333
            foreach (var attributes in drawingObject.AttributeSets)
334
            {
335
                foreach (var attribute in attributes)
336
                {
337
                    if (attribute.Name == "ModelID")
338
                        modelItemId = attribute.GetValue().ToString();
339
                }
340
            }
341
            radApp.ActiveSelectSet.RemoveAll();
342

    
343
        }
344
        #endregion
345

    
346

    
347
        [DllImport("user32.dll")]
348
        public static extern int FindWindow(string lpClassName, string lpWindowName);
349

    
350
        [DllImport("user32.dll", SetLastError = true)]
351
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
352

    
353
        private void ConverterDocking_Load(object sender, EventArgs e)
354
        {
355
#if DEBUG
356
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
357
#else
358
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
359
#endif
360

    
361
        }
362
    }
363
}
클립보드 이미지 추가 (최대 크기: 500 MB)