프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ fdb1367e

이력 | 보기 | 이력해설 | 다운로드 (16 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 4941f5fe gaqhf
using System.Reflection;
26 154d8f43 gaqhf
using Converter.SPPID.OPC;
27 65881d60 gaqhf
28
namespace Converter.SPPID.Wrapper
29
{
30
    public partial class ConverterDocking : UserControl
31
    {
32 4941f5fe gaqhf
        Ingr.RAD2D.Application application;
33 f14b4e3b gaqhf
        static bool addEvent = false;
34 65881d60 gaqhf
        public ConverterDocking()
35
        {
36
            InitializeComponent();
37 4941f5fe gaqhf
38
            dynamic dApplication = Interaction.GetObject("", "PIDAutomation.Application");
39
            WrapperApplication wApp = new WrapperApplication(dApplication.Application);
40
            application = wApp.RADApplication;
41 d4c3e39f gaqhf
            
42 4941f5fe gaqhf
            ReleaseCOMObjects(dApplication);
43
            dApplication = null;
44 6a7573b0 gaqhf
            try
45
            {
46
                textEditDrawingX.EditValue = Settings.Default.DrawingX;
47
                textEditDrawingY.EditValue = Settings.Default.DrawingY;
48 154d8f43 gaqhf
49
                Project_Info _ProjectInfo = Project_Info.GetInstance();
50 8847ea67 gaqhf
                _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 f14b4e3b gaqhf
57
                if (!addEvent)
58
                {
59
                    application.EventObject.BeforeApplicationExit += ApplicationEvents_ApplicationExit;
60
                    addEvent = true;
61
                }
62 6a7573b0 gaqhf
            }
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 65881d60 gaqhf
        }
71
72 5a9396ae humkyung
        /// <summary>
73
        /// 선택한 도면들을 읽어서 SPPID로 변환한다.
74
        /// </summary>
75
        /// <param name="sender"></param>
76
        /// <param name="e"></param>
77 e3e2d41f gaqhf
        private void btnConverter_Click(object sender, EventArgs e)
78 65881d60 gaqhf
        {
79 1ba9c671 gaqhf
            ConverterForm converterForm = new ConverterForm();
80
            if (converterForm.ShowDialog() == DialogResult.OK)
81 65881d60 gaqhf
            {
82 4941f5fe gaqhf
                //Ingr.RAD2D.Document doc = application.Documents.Add();
83
84 1ba9c671 gaqhf
                try
85
                {
86
                    CloseOPCForm.Run();
87 ca214bc3 gaqhf
88 d5ec4d0f gaqhf
                    for (int i = 0; i < converterForm.Documents.Count; i++)
89 1ba9c671 gaqhf
                    {
90 d5ec4d0f gaqhf
                        SPPID_Document document = converterForm.Documents[i];
91 1ba9c671 gaqhf
                        if (document.SetSPPIDMapping() && document.Enable)
92
                        {
93 bccacd6c gaqhf
                            using (AutoModeling modeling = new AutoModeling(document, converterForm.checkEditCloseDocument.Checked))
94 5a9396ae humkyung
                            {
95
                                modeling.DocumentLabelText = string.Format("Drawing Name : {0} ({1}/{2})", document.DrawingName, i + 1, converterForm.Documents.Count);
96
                                modeling.Run();
97
                            }
98 1ba9c671 gaqhf
                        }
99
                    }
100
                }
101
                catch (Exception ex)
102
                {
103
                    MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
104
                }
105
                finally
106 d19ae675 gaqhf
                {
107 1ba9c671 gaqhf
                    CloseOPCForm.Stop();
108 4941f5fe gaqhf
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 1ba9c671 gaqhf
                }
117
118
                MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
119
            }
120
        }
121
122 69b7387a gaqhf
        private void btnLinkOPC_Click(object sender, EventArgs e)
123
        {
124 154d8f43 gaqhf
            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 69b7387a gaqhf
137 154d8f43 gaqhf
                    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 58993ba9 gaqhf
                        DataRow[] fromOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", fromOPCUID1));
151
                        DataRow[] toOPCInfoRows = tOPCInfo.Select(string.Format("ID2_OPC_UID = '{0}'", toOPCUID1));
152 154d8f43 gaqhf
153 58993ba9 gaqhf
                        if (fromOPCUID1 == toOPCUID2 && fromOPCUID2 == toOPCUID1 && fromDrawing.Length == 1 && toDrawing.Length == 1 && fromOPCInfoRows.Length == 1 && toOPCInfoRows.Length == 1)
154 154d8f43 gaqhf
                        {
155 58993ba9 gaqhf
                            DataRow fromOPCInfoRow = fromOPCInfoRows[0];
156
                            DataRow toOPCInfoRow = toOPCInfoRows[0];
157 154d8f43 gaqhf
                            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 58993ba9 gaqhf
                            List<string[]> toOPCAttributes = JsonConvert.DeserializeObject<List<string[]>>(toOPCInfoRow["ATTRIBUTES"].ToString());
161 b7a29053 gaqhf
                            AutoModeling_OPC opc = new AutoModeling_OPC(dApplication, application, fromOPCModelId, toOPCModelId, toDrawingName, toOPCAttributes);
162 154d8f43 gaqhf
                            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 58993ba9 gaqhf
181
            MessageBox.Show(Msg.EndConvert, Msg.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
182 69b7387a gaqhf
        }
183
184 4fb0f8d5 gaqhf
        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 6a7573b0 gaqhf
        private void btnGetDrawingSize_Click(object sender, EventArgs e)
195 1ba9c671 gaqhf
        {
196 d4c3e39f gaqhf
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
197
            WrapperApplication wApp = new WrapperApplication(application.Application);
198
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
199
200 6a7573b0 gaqhf
            if (radApp.ActiveSelectSet.Count > 0)
201 65881d60 gaqhf
            {
202 6a7573b0 gaqhf
                DependencyObject line2D = radApp.ActiveSelectSet[0] as DependencyObject;
203
                if (line2D != null)
204 1ba9c671 gaqhf
                {
205 6a7573b0 gaqhf
                    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 1ba9c671 gaqhf
                }
218 6a7573b0 gaqhf
                else
219
                    MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
220 65881d60 gaqhf
            }
221 6a7573b0 gaqhf
            else
222
                MessageBox.Show(Msg.SelectLine, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
223
        }
224 7e4a64a3 gaqhf
225
        #region TEST
226 f14b4e3b gaqhf
        private void ApplicationEvents_ApplicationExit(out bool cancel)
227
        {
228
            application.Documents.Close();
229
            //SheetWindow sheetWindow = application.ActiveWindow;
230
            cancel = false;
231
        }
232
233 6a7573b0 gaqhf
        private void simpleButton1_Click(object sender, EventArgs e)
234
        {
235 b7b123ba gaqhf
            Placement placement = new Placement();
236
            if (application.ActiveSelectSet.Count > 0)
237 d9fc7084 gaqhf
            {
238 b7b123ba gaqhf
                string modelItemId = null;
239
                Symbol2d symbol2D = application.ActiveSelectSet[0] as Symbol2d;
240
                DependencyObject dependency = application.ActiveSelectSet[0] as DependencyObject;
241
                application.ActiveSelectSet.RemoveAll();
242
                if (symbol2D != null)
243 d9fc7084 gaqhf
                {
244 b7b123ba gaqhf
                    return;
245
                    foreach (var attributes in symbol2D.AttributeSets)
246 90662b0b gaqhf
                    {
247 b7b123ba gaqhf
                        foreach (var attribute in attributes)
248
                        {
249
                            if (attribute.Name == "ModelID")
250
                                modelItemId = attribute.GetValue().ToString();
251
                        }
252 90662b0b gaqhf
                    }
253
254 b7b123ba gaqhf
                    if (modelItemId!=null)
255
                    {
256
                        LMADataSource dataSource = new LMADataSource();
257
                        LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
258
                        foreach (LMRepresentation item in modelItem.Representations)
259
                        {
260
                            if (item.ModelItemID == modelItemId)
261
                            {
262
                                dataSource = placement.PIDDataSource;
263
                                LMSymbol symbol = dataSource.GetSymbol(item.Id);
264
                                double oldx = symbol.get_XCoordinate();
265
                                double oldy = symbol.get_YCoordinate();
266
                                symbol2D.Move(0, 0, 0, GridSetting.GetInstance().Length);
267
268
                                dataSource = new LMADataSource();
269
                                //dataSource = placement.PIDDataSource;
270
                                symbol = dataSource.GetSymbol(item.Id);
271
                                double x = symbol.get_XCoordinate();
272
                                double y = symbol.get_YCoordinate();
273
                            }
274
                        }
275
                    }
276 4e865771 gaqhf
                }
277 b7b123ba gaqhf
                else if (dependency != null)
278 4e865771 gaqhf
                {
279 b7b123ba gaqhf
                    foreach (dynamic item in dependency.DrawingObjects)
280
                    {
281
                        item.Move(0, 0, 0, GridSetting.GetInstance().Length);
282
                    }
283 d9fc7084 gaqhf
                }
284
            }
285 4e865771 gaqhf
286 154d8f43 gaqhf
            return;
287 4941f5fe gaqhf
            Thread outThread = new Thread(func2 =>
288
            {
289
                for (int i = 100; i < 120; i++)
290
                {
291
                    Thread thread = new Thread(func =>
292
                    {
293
                        dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
294
                        //WrapperApplication wApp = new WrapperApplication(application.Application);
295
                        //Ingr.RAD2D.Application radApp = wApp.RADApplication;
296
                        dynamic drawing = application.Drawings.Add("Unit1", "D-Size.pid", i.ToString(), i.ToString());//application.Drawings.OpenDrawing("123");
297
298
                        Thread.Sleep(5000);
299
300
                        drawing.CloseDrawing(true);
301
302
                        //application.Quit();
303
                        ReleaseCOMObjects(application);
304
                        application = null;
305
                        ReleaseCOMObjects(drawing);
306
                        drawing = null;
307
                        Thread.Sleep(5000);
308
                    });
309
                    thread.Start();
310
                    thread.Join();
311
                }
312
            });
313
            outThread.Start();
314
            
315
            
316
317 32205389 gaqhf
318 ca6e0f51 gaqhf
319 4941f5fe gaqhf
            //radApp.Documents.Add(@"\\server70\DEVDOF1Site\GS\PLANT\PLANT1\Unit1\11111.pid");
320
321
            //radApp.ActiveDocument.Close(true);
322
            //drawing.CloseDrawing(true);
323
324
            //ReleaseCOMObjects(application);
325
            //ReleaseCOMObjects(drawing);
326
            //application = null;
327
            //wApp = null;
328
            //radApp = null;
329
            //drawing = null;
330 ca6e0f51 gaqhf
        }
331 4941f5fe gaqhf
332 ca6e0f51 gaqhf
        private void AutoJoinPipeRun()
333
        {
334 d4c3e39f gaqhf
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
335
            WrapperApplication wApp = new WrapperApplication(application.Application);
336
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
337
338 ca6e0f51 gaqhf
            string modelItemId = null;
339
            List<double[]> vertices = new List<double[]>();
340 a31a512e gaqhf
            if (radApp.ActiveSelectSet.Count == 0)
341
            {
342
                return;
343
            }
344 ca6e0f51 gaqhf
            dynamic OID = radApp.ActiveSelectSet[0].Key();
345
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
346
            foreach (var attributes in drawingObject.AttributeSets)
347 4ba01591 gaqhf
            {
348 ca6e0f51 gaqhf
                foreach (var attribute in attributes)
349 4ba01591 gaqhf
                {
350 ca6e0f51 gaqhf
                    if (attribute.Name == "ModelID")
351
                        modelItemId = attribute.GetValue().ToString();
352
                }
353
            }
354
            radApp.ActiveSelectSet.RemoveAll();
355 32205389 gaqhf
356 7e4a64a3 gaqhf
        }
357
        #endregion
358
359 7aee331b gaqhf
360 9628f54b gaqhf
        [DllImport("user32.dll")]
361
        public static extern int FindWindow(string lpClassName, string lpWindowName);
362 3734dcc5 gaqhf
363
        [DllImport("user32.dll", SetLastError = true)]
364
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
365
366 b427ebf9 humkyung
        private void ConverterDocking_Load(object sender, EventArgs e)
367
        {
368
#if DEBUG
369
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
370
#else
371
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
372
#endif
373
374
        }
375 65881d60 gaqhf
    }
376
}
클립보드 이미지 추가 (최대 크기: 500 MB)