프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ 41e26743

이력 | 보기 | 이력해설 | 다운로드 (16.3 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
                    addEvent = true;
61
                }
62
            }
63
            catch (Exception ex)
64
            {
65
                StringBuilder sb = new StringBuilder();
66
                sb.AppendLine(ex.Message);
67
                sb.AppendLine(ex.StackTrace);
68
                MessageBox.Show(sb.ToString());
69
            }
70
        }
71

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

    
84
                try
85
                {
86
                    CloseOPCForm.Run();
87

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

    
109
                    //doc.SaveOnClose = false;
110
                    //doc.Close(false);
111

    
112
                    //dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
113
                    //dApplication.Drawings[1].CloseDrawing(false);
114
                    //ReleaseCOMObjects(dApplication);
115
                    //dApplication = null;
116
                }
117

    
118
                MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
119
            }
120
        }
121

    
122
        private void btnLinkOPC_Click(object sender, EventArgs e)
123
        {
124
            DataTable tOPCInfo = Project_DB.SelectOPCInfo();
125
            DataTable tOPCRelations = Project_DB.SelectOPCRelations();
126
            DataTable tDrawingInfo = Project_DB.SelectDrawingInfo();
127
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
128

    
129
            foreach (DataRow row in tOPCInfo.Rows)
130
            {
131
                if (!Convert.ToBoolean(row["PAIRED"]))
132
                {
133
                    string drawingUID = row["ID2_DRAWING_UID"].ToString();
134
                    string OPCUID = row["ID2_OPC_UID"].ToString();
135
                    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));
136

    
137
                    if (rows.Length == 2)
138
                    {
139
                        string fromDrawingsUID1 = rows[0]["From_Drawings_UID"].ToString();
140
                        string fromDrawingsUID2 = rows[1]["From_Drawings_UID"].ToString();
141
                        string fromOPCUID1 = rows[0]["From_OPC_UID"].ToString();
142
                        string fromOPCUID2 = rows[1]["From_OPC_UID"].ToString();
143
                        string toDrawingsUID1 = rows[0]["To_Drawings_UID"].ToString();
144
                        string toDrawingsUID2 = rows[1]["To_Drawings_UID"].ToString();
145
                        string toOPCUID1 = rows[0]["To_OPC_UID"].ToString();
146
                        string toOPCUID2 = rows[1]["To_OPC_UID"].ToString();
147

    
148
                        DataRow[] fromDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", fromDrawingsUID1));
149
                        DataRow[] toDrawing = tDrawingInfo.Select(string.Format("ID2_DRAWING_UID = '{0}'", toDrawingsUID1));
150
                        DataRow[] fromOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", fromOPCUID1));
151
                        DataRow[] toOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", toOPCUID1));
152

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

    
167
                                Project_DB.InsertOPCInfo(fromOPCInfoRow["ID2_OPC_UID"].ToString(), fromOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), fromOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
168
                                Project_DB.InsertOPCInfo(toOPCInfoRow["ID2_OPC_UID"].ToString(), toOPCInfoRow["SPPID_OPC_MODELITEM_ID"].ToString(), toOPCInfoRow["ID2_DRAWING_UID"].ToString(), true);
169
                            }
170
                        }
171
                    }
172
                }
173
            }
174

    
175
            tOPCInfo.Dispose();
176
            tOPCRelations.Dispose();
177
            tDrawingInfo.Dispose();
178
            ReleaseCOMObjects(dApplication);
179
            dApplication = null;
180

    
181
            MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
182
        }
183

    
184
        public void ReleaseCOMObjects(params object[] objVars)
185
        {
186
            int intNewRefCount = 0;
187
            foreach (object obj in objVars)
188
            {
189
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
190
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
191
            }
192
        }
193

    
194
        private void btnGetDrawingSize_Click(object sender, EventArgs e)
195
        {
196
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
197
            WrapperApplication wApp = new WrapperApplication(application.Application);
198
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
199

    
200
            if (radApp.ActiveSelectSet.Count > 0)
201
            {
202
                DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject;
203
                if (line2D != null)
204
                {
205
                    double minX = 0;
206
                    double minY = 0;
207
                    double maxX = 0;
208
                    double maxY = 0;
209
                    line2D.Range(out minX, out minY, out maxX, out maxY);
210

    
211
                    Settings.Default.DrawingX = maxX - minX;
212
                    Settings.Default.DrawingY = maxY - minY;
213
                    Settings.Default.Save();
214

    
215
                    textEditDrawingX.EditValue = Settings.Default.DrawingX;
216
                    textEditDrawingY.EditValue = Settings.Default.DrawingY;
217
                }
218
                else
219
                    MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
220
            }
221
            else
222
                MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
223
        }
224

    
225
        #region TEST
226
        private void ApplicationEvents_ApplicationExit(out bool cancel)
227
        {
228
            application.Documents.Close();
229
            //SheetWindow sheetWindow = application.ActiveWindow;
230
            cancel = false;
231
        }
232

    
233
        private void simpleButton1_Click(object sender, EventArgs e)
234
        {
235
            Placement placement = new Placement();
236
            LMADataSource dataSource = placement.PIDDataSource;
237
            LMModelItem modelitem = dataSource.GetModelItem("98F75D05005747969BECFC8E06340129");
238
            foreach (LMAAttribute attri in modelitem.Attributes)
239
            {
240
                string name = attri.Name;
241
                dynamic value = attri.get_Value();
242
            }
243
            return;
244
            if (application.ActiveSelectSet.Count > 0)
245
            {
246
                string modelItemId = null;
247
                Symbol2d symbol2D = application.ActiveSelectSet[0] as Symbol2d;
248
                DependencyObject dependency = application.ActiveSelectSet[0] as DependencyObject;
249
                application.ActiveSelectSet.RemoveAll();
250
                if (symbol2D != null)
251
                {
252
                    return;
253
                    foreach (var attributes in symbol2D.AttributeSets)
254
                    {
255
                        foreach (var attribute in attributes)
256
                        {
257
                            if (attribute.Name == "ModelID")
258
                                modelItemId = attribute.GetValue().ToString();
259
                        }
260
                    }
261

    
262
                    if (modelItemId!=null)
263
                    {
264
                        //LMADataSource dataSource = new LMADataSource();
265
                        LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
266
                        foreach (LMRepresentation item in modelItem.Representations)
267
                        {
268
                            if (item.ModelItemID == modelItemId)
269
                            {
270
                                dataSource = placement.PIDDataSource;
271
                                LMSymbol symbol = dataSource.GetSymbol(item.Id);
272
                                double oldx = symbol.get_XCoordinate();
273
                                double oldy = symbol.get_YCoordinate();
274
                                symbol2D.Move(0, 0, 0, GridSetting.GetInstance().Length);
275

    
276
                                dataSource = new LMADataSource();
277
                                //dataSource = placement.PIDDataSource;
278
                                symbol = dataSource.GetSymbol(item.Id);
279
                                double x = symbol.get_XCoordinate();
280
                                double y = symbol.get_YCoordinate();
281
                            }
282
                        }
283
                    }
284
                }
285
                else if (dependency != null)
286
                {
287
                    foreach (dynamic item in dependency.DrawingObjects)
288
                    {
289
                        item.Move(0, 0, 0, GridSetting.GetInstance().Length);
290
                    }
291
                }
292
            }
293

    
294
            return;
295
            Thread outThread = new Thread(func2 =>
296
            {
297
                for (int i = 100; i < 120; i++)
298
                {
299
                    Thread thread = new Thread(func =>
300
                    {
301
                        dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
302
                        //WrapperApplication wApp = new WrapperApplication(application.Application);
303
                        //Ingr.RAD2D.Application radApp = wApp.RADApplication;
304
                        dynamic drawing = application.Drawings.Add("Unit1", "D-Size.pid", i.ToString(), i.ToString());//application.Drawings.OpenDrawing("123");
305

    
306
                        Thread.Sleep(5000);
307

    
308
                        drawing.CloseDrawing(true);
309

    
310
                        //application.Quit();
311
                        ReleaseCOMObjects(application);
312
                        application = null;
313
                        ReleaseCOMObjects(drawing);
314
                        drawing = null;
315
                        Thread.Sleep(5000);
316
                    });
317
                    thread.Start();
318
                    thread.Join();
319
                }
320
            });
321
            outThread.Start();
322
            
323
            
324

    
325

    
326

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

    
329
            //radApp.ActiveDocument.Close(true);
330
            //drawing.CloseDrawing(true);
331

    
332
            //ReleaseCOMObjects(application);
333
            //ReleaseCOMObjects(drawing);
334
            //application = null;
335
            //wApp = null;
336
            //radApp = null;
337
            //drawing = null;
338
        }
339

    
340
        private void AutoJoinPipeRun()
341
        {
342
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
343
            WrapperApplication wApp = new WrapperApplication(application.Application);
344
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
345

    
346
            string modelItemId = null;
347
            List<double[]> vertices = new List<double[]>();
348
            if (radApp.ActiveSelectSet.Count == 0)
349
            {
350
                return;
351
            }
352
            dynamic OID = radApp.ActiveSelectSet[0].Key();
353
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
354
            foreach (var attributes in drawingObject.AttributeSets)
355
            {
356
                foreach (var attribute in attributes)
357
                {
358
                    if (attribute.Name == "ModelID")
359
                        modelItemId = attribute.GetValue().ToString();
360
                }
361
            }
362
            radApp.ActiveSelectSet.RemoveAll();
363

    
364
        }
365
        #endregion
366

    
367

    
368
        [DllImport("user32.dll")]
369
        public static extern int FindWindow(string lpClassName, string lpWindowName);
370

    
371
        [DllImport("user32.dll", SetLastError = true)]
372
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
373

    
374
        private void ConverterDocking_Load(object sender, EventArgs e)
375
        {
376
#if DEBUG
377
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
378
#else
379
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
380
#endif
381

    
382
        }
383
    }
384
}
클립보드 이미지 추가 (최대 크기: 500 MB)