프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ 1b09fe17

이력 | 보기 | 이력해설 | 다운로드 (16.9 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
        static bool addEvent = false;
34
        public ConverterDocking()
35
        {
36
            InitializeComponent();
37

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

    
49
                Project_Info _ProjectInfo = Project_Info.GetInstance();
50
                _ProjectInfo.DefaultPath = Settings.Default.ProjectPath;
51
                _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType;
52
                _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP;
53
                _ProjectInfo.Port = Settings.Default.ProjectPort;
54
                _ProjectInfo.DBUser = Settings.Default.ProjectDBUser;
55
                _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword;
56

    
57
                if (!addEvent)
58
                {
59
                    application.EventObject.BeforeApplicationExit += ApplicationEvents_ApplicationExit;
60
                    RegisterHotKey(this.Handle, 0, (int)KeyModifier.Shift, Keys.A.GetHashCode());
61
                    addEvent = true;
62
                }
63
            }
64
            catch (Exception ex)
65
            {
66
                StringBuilder sb = new StringBuilder();
67
                sb.AppendLine(ex.Message);
68
                sb.AppendLine(ex.StackTrace);
69
                MessageBox.Show(sb.ToString());
70
            }
71
        }
72
        private void ApplicationEvents_ApplicationExit(out bool cancel)
73
        {
74
            UnregisterHotKey(this.Handle, 0);
75
            cancel = false;
76
        }
77

    
78
        /// <summary>
79
        /// 선택한 도면들을 읽어서 SPPID로 변환한다.
80
        /// </summary>
81
        /// <param name="sender"></param>
82
        /// <param name="e"></param>
83
        private void btnConverter_Click(object sender, EventArgs e)
84
        {
85
            ConverterForm converterForm = new ConverterForm();
86
            if (converterForm.ShowDialog() == DialogResult.OK)
87
            {
88
                //Ingr.RAD2D.Document doc = application.Documents.Add();
89

    
90
                try
91
                {
92
                    CloseOPCForm.Run();
93

    
94
                    for (int i = 0; i < converterForm.Documents.Count; i++)
95
                    {
96
                        SPPID_Document document = converterForm.Documents[i];
97
                        if (document.SetSPPIDMapping() && document.Enable)
98
                        {
99
                            using (AutoModeling modeling = new AutoModeling(document, converterForm.checkEditCloseDocument.Checked))
100
                            {
101
                                modeling.DocumentLabelText = string.Format("Drawing Name : {0} ({1}/{2})", document.DrawingName, i + 1, converterForm.Documents.Count);
102
                                modeling.Run();
103
                            }
104
                        }
105
                    }
106
                }
107
                catch (Exception ex)
108
                {
109
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
110
                }
111
                finally
112
                {
113
                    CloseOPCForm.Stop();
114

    
115
                    //doc.SaveOnClose = false;
116
                    //doc.Close(false);
117

    
118
                    //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
119
                    //dApplication.Drawings[1].CloseDrawing(false);
120
                    //ReleaseCOMObjects(dApplication);
121
                    //dApplication = null;
122
                }
123

    
124
                MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
125
            }
126
        }
127

    
128
        private void btnLinkOPC_Click(object sender, EventArgs e)
129
        {
130
            DataTable tOPCInfo = Project_DB.SelectOPCInfo();
131
            DataTable tOPCRelations = Project_DB.SelectOPCRelations();
132
            DataTable tDrawingInfo = Project_DB.SelectDrawingInfo();
133
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
134

    
135
            foreach (DataRow row in tOPCInfo.Rows)
136
            {
137
                if (!Convert.ToBoolean(row["PAIRED"]))
138
                {
139
                    string drawingUID = row["ID2_DRAWING_UID"].ToString();
140
                    string OPCUID = row["ID2_OPC_UID"].ToString();
141
                    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));
142

    
143
                    if (rows.Length == 2)
144
                    {
145
                        string fromDrawingsUID1 = rows[0]["From_Drawings_UID"].ToString();
146
                        string fromDrawingsUID2 = rows[1]["From_Drawings_UID"].ToString();
147
                        string fromOPCUID1 = rows[0]["From_OPC_UID"].ToString();
148
                        string fromOPCUID2 = rows[1]["From_OPC_UID"].ToString();
149
                        string toDrawingsUID1 = rows[0]["To_Drawings_UID"].ToString();
150
                        string toDrawingsUID2 = rows[1]["To_Drawings_UID"].ToString();
151
                        string toOPCUID1 = rows[0]["To_OPC_UID"].ToString();
152
                        string toOPCUID2 = rows[1]["To_OPC_UID"].ToString();
153

    
154
                        DataRow[] fromDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", fromDrawingsUID1));
155
                        DataRow[] toDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", toDrawingsUID1));
156
                        DataRow[] fromOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", fromOPCUID1));
157
                        DataRow[] toOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", toOPCUID1));
158

    
159
                        if (fromOPCUID1 == toOPCUID2 && fromOPCUID2 == toOPCUID1 && fromDrawing.Length == 1 && toDrawing.Length == 1 && fromOPCInfoRows.Length == 1 && toOPCInfoRows.Length == 1)
160
                        {
161
                            DataRow fromOPCInfoRow = fromOPCInfoRows[0];
162
                            DataRow toOPCInfoRow = toOPCInfoRows[0];
163
                            string fromOPCModelId = fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
164
                            string toOPCModelId = toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString();
165
                            string toDrawingName = toDrawing[0]["DRAWINGNAME"].ToString();
166
                            List<string[]> toOPCAttributes = JsonConvert.DeserializeObject<List<string[]>>(toOPCInfoRow["ATTRIBUTES"].ToString());
167
                            AutoModeling_OPC opc = new AutoModeling_OPC(dApplication, application, fromOPCModelId, toOPCModelId, toDrawingName, toOPCAttributes);
168
                            if (opc.Run())
169
                            {
170
                                fromOPCInfoRow["PAIRED"] = true;
171
                                toOPCInfoRow["PAIRED"] = true;
172

    
173
                                Project_DB.InsertOPCInfo(fromOPCInfoRow["ID2_OPC_UID"].ToString(), fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), fromOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
174
                                Project_DB.InsertOPCInfo(toOPCInfoRow["ID2_OPC_UID"].ToString(), toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), toOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
175
                            }
176
                        }
177
                    }
178
                }
179
            }
180

    
181
            tOPCInfo.Dispose();
182
            tOPCRelations.Dispose();
183
            tDrawingInfo.Dispose();
184
            ReleaseCOMObjects(dApplication);
185
            dApplication = null;
186

    
187
            MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
188
        }
189

    
190
        public void ReleaseCOMObjects(params object[] objVars)
191
        {
192
            int intNewRefCount = 0;
193
            foreach (object obj in objVars)
194
            {
195
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
196
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
197
            }
198
        }
199

    
200
        private void btnGetDrawingSize_Click(object sender, EventArgs e)
201
        {
202
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
203
            WrapperApplication wApp = new WrapperApplication(application.Application);
204
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
205

    
206
            if (radApp.ActiveSelectSet.Count > 0)
207
            {
208
                DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject;
209
                if (line2D != null)
210
                {
211
                    double minX = 0;
212
                    double minY = 0;
213
                    double maxX = 0;
214
                    double maxY = 0;
215
                    line2D.Range(out minX, out minY, out maxX, out maxY);
216

    
217
                    Settings.Default.DrawingX = maxX - minX;
218
                    Settings.Default.DrawingY = maxY - minY;
219
                    Settings.Default.Save();
220

    
221
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
222
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
223
                }
224
                else
225
                    MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
226
            }
227
            else
228
                MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
229
        }
230

    
231
        #region Hot Key
232
        [System.Runtime.InteropServices.DllImport("user32.dll")]
233
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
234

    
235
        [System.Runtime.InteropServices.DllImport("user32.dll")]
236
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
237

    
238
        enum KeyModifier
239
        {
240
            None = 0,
241
            Alt = 1,
242
            Control = 2,
243
            Shift = 4,
244
            WinKey = 8
245
        }
246

    
247
        protected override void WndProc(ref Message m)
248
        {
249
            base.WndProc(ref m);
250
            if (m.Msg == 0x0312)
251
            {
252
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
253
                KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF);
254
                int id = m.WParam.ToInt32();
255
                application.RunCommand(CommandConstants.igcmdGridSnap);
256
            }
257

    
258
        }
259
        #endregion
260

    
261
        #region TEST
262

    
263
        private void simpleButton1_Click(object sender, EventArgs e)
264
        {
265
            Placement placement = new Placement();
266
            if (application.ActiveSelectSet.Count > 0)
267
            {
268
                string modelItemId = null;
269
                Symbol2d symbol2D = application.ActiveSelectSet[0] as Symbol2d;
270
                DependencyObject dependency = application.ActiveSelectSet[0] as DependencyObject;
271
                application.ActiveSelectSet.RemoveAll();
272
                if (symbol2D != null)
273
                {
274
                    return;
275
                    foreach (var attributes in symbol2D.AttributeSets)
276
                    {
277
                        foreach (var attribute in attributes)
278
                        {
279
                            if (attribute.Name == "ModelID")
280
                                modelItemId = attribute.GetValue().ToString();
281
                        }
282
                    }
283

    
284
                    if (modelItemId!=null)
285
                    {
286
                        LMADataSource dataSource = new LMADataSource();
287
                        LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
288
                        foreach (LMRepresentation item in modelItem.Representations)
289
                        {
290
                            if (item.ModelItemID == modelItemId)
291
                            {
292
                                dataSource = placement.PIDDataSource;
293
                                LMSymbol symbol = dataSource.GetSymbol(item.Id);
294
                                double oldx = symbol.get_XCoordinate();
295
                                double oldy = symbol.get_YCoordinate();
296
                                symbol2D.Move(0, 0, 0, GridSetting.GetInstance().Length);
297

    
298
                                dataSource = new LMADataSource();
299
                                //dataSource = placement.PIDDataSource;
300
                                symbol = dataSource.GetSymbol(item.Id);
301
                                double x = symbol.get_XCoordinate();
302
                                double y = symbol.get_YCoordinate();
303
                            }
304
                        }
305
                    }
306
                }
307
                else if (dependency != null)
308
                {
309
                    foreach (dynamic item in dependency.DrawingObjects)
310
                    {
311
                        item.Move(0, 0, 0, GridSetting.GetInstance().Length);
312
                    }
313
                }
314
            }
315

    
316
            return;
317
            Thread outThread = new Thread(func2 =>
318
            {
319
                for (int i = 100; i < 120; i++)
320
                {
321
                    Thread thread = new Thread(func =>
322
                    {
323
                        dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
324
                        //WrapperApplication wApp = new WrapperApplication(application.Application);
325
                        //Ingr.RAD2D.Application radApp = wApp.RADApplication;
326
                        dynamic drawing = application.Drawings.Add("Unit1", "D-Size.pid", i.ToString(), i.ToString());//application.Drawings.OpenDrawing("123");
327

    
328
                        Thread.Sleep(5000);
329

    
330
                        drawing.CloseDrawing(true);
331

    
332
                        //application.Quit();
333
                        ReleaseCOMObjects(application);
334
                        application = null;
335
                        ReleaseCOMObjects(drawing);
336
                        drawing = null;
337
                        Thread.Sleep(5000);
338
                    });
339
                    thread.Start();
340
                    thread.Join();
341
                }
342
            });
343
            outThread.Start();
344
            
345
            
346

    
347

    
348

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

    
351
            //radApp.ActiveDocument.Close(true);
352
            //drawing.CloseDrawing(true);
353

    
354
            //ReleaseCOMObjects(application);
355
            //ReleaseCOMObjects(drawing);
356
            //application = null;
357
            //wApp = null;
358
            //radApp = null;
359
            //drawing = null;
360
        }
361

    
362
        private void AutoJoinPipeRun()
363
        {
364
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
365
            WrapperApplication wApp = new WrapperApplication(application.Application);
366
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
367

    
368
            string modelItemId = null;
369
            List<double[]> vertices = new List<double[]>();
370
            if (radApp.ActiveSelectSet.Count == 0)
371
            {
372
                return;
373
            }
374
            dynamic OID = radApp.ActiveSelectSet[0].Key();
375
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
376
            foreach (var attributes in drawingObject.AttributeSets)
377
            {
378
                foreach (var attribute in attributes)
379
                {
380
                    if (attribute.Name == "ModelID")
381
                        modelItemId = attribute.GetValue().ToString();
382
                }
383
            }
384
            radApp.ActiveSelectSet.RemoveAll();
385

    
386
        }
387
        #endregion
388

    
389

    
390
        [DllImport("user32.dll")]
391
        public static extern int FindWindow(string lpClassName, string lpWindowName);
392

    
393
        [DllImport("user32.dll", SetLastError = true)]
394
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
395

    
396
        private void ConverterDocking_Load(object sender, EventArgs e)
397
        {
398
#if DEBUG
399
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
400
#else
401
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
402
#endif
403

    
404
        }
405
    }
406
}
클립보드 이미지 추가 (최대 크기: 500 MB)