프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ dd291d65

이력 | 보기 | 이력해설 | 다운로드 (16.3 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 e0828ff4 gaqhf
            //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
            
244 b7b123ba gaqhf
            if (application.ActiveSelectSet.Count > 0)
245 d9fc7084 gaqhf
            {
246 b7b123ba gaqhf
                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 d9fc7084 gaqhf
                {
252 b7b123ba gaqhf
                    return;
253
                    foreach (var attributes in symbol2D.AttributeSets)
254 90662b0b gaqhf
                    {
255 b7b123ba gaqhf
                        foreach (var attribute in attributes)
256
                        {
257
                            if (attribute.Name == "ModelID")
258
                                modelItemId = attribute.GetValue().ToString();
259
                        }
260 90662b0b gaqhf
                    }
261
262 b7b123ba gaqhf
                    if (modelItemId!=null)
263
                    {
264 e0828ff4 gaqhf
                        LMADataSource dataSource = new LMADataSource();
265 b7b123ba gaqhf
                        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 4e865771 gaqhf
                }
285 b7b123ba gaqhf
                else if (dependency != null)
286 4e865771 gaqhf
                {
287 b7b123ba gaqhf
                    foreach (dynamic item in dependency.DrawingObjects)
288
                    {
289
                        item.Move(0, 0, 0, GridSetting.GetInstance().Length);
290
                    }
291 d9fc7084 gaqhf
                }
292
            }
293 4e865771 gaqhf
294 154d8f43 gaqhf
            return;
295 4941f5fe gaqhf
            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 32205389 gaqhf
326 ca6e0f51 gaqhf
327 4941f5fe gaqhf
            //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 ca6e0f51 gaqhf
        }
339 4941f5fe gaqhf
340 ca6e0f51 gaqhf
        private void AutoJoinPipeRun()
341
        {
342 d4c3e39f gaqhf
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
343
            WrapperApplication wApp = new WrapperApplication(application.Application);
344
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
345
346 ca6e0f51 gaqhf
            string modelItemId = null;
347
            List<double[]> vertices = new List<double[]>();
348 a31a512e gaqhf
            if (radApp.ActiveSelectSet.Count == 0)
349
            {
350
                return;
351
            }
352 ca6e0f51 gaqhf
            dynamic OID = radApp.ActiveSelectSet[0].Key();
353
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
354
            foreach (var attributes in drawingObject.AttributeSets)
355 4ba01591 gaqhf
            {
356 ca6e0f51 gaqhf
                foreach (var attribute in attributes)
357 4ba01591 gaqhf
                {
358 ca6e0f51 gaqhf
                    if (attribute.Name == "ModelID")
359
                        modelItemId = attribute.GetValue().ToString();
360
                }
361
            }
362
            radApp.ActiveSelectSet.RemoveAll();
363 32205389 gaqhf
364 7e4a64a3 gaqhf
        }
365
        #endregion
366
367 7aee331b gaqhf
368 9628f54b gaqhf
        [DllImport("user32.dll")]
369
        public static extern int FindWindow(string lpClassName, string lpWindowName);
370 3734dcc5 gaqhf
371
        [DllImport("user32.dll", SetLastError = true)]
372
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
373
374 b427ebf9 humkyung
        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 65881d60 gaqhf
    }
384
}
클립보드 이미지 추가 (최대 크기: 500 MB)