프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / SPPIDConverter / ConverterDocking.cs @ a9773437

이력 | 보기 | 이력해설 | 다운로드 (15.1 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 f14b4e3b gaqhf
236 4e865771 gaqhf
            foreach (RADObject item in application.ActiveDocument.SelectSet)
237 d9fc7084 gaqhf
            {
238 4e865771 gaqhf
                DependencyObject dependencyObject = item as DependencyObject;
239
                if (dependencyObject != null)
240 d9fc7084 gaqhf
                {
241 90662b0b gaqhf
                    Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = dependencyObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
242
                    int verticesCount = lineStringGeometry.VertexCount;
243
                    double[] vertices = null;
244
                    lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
245
                    for (int i = 0; i < verticesCount; i++)
246
                    {
247
                        double x = 0;
248
                        double y = 0;
249
                        lineStringGeometry.GetVertex(i + 1, ref x, ref y);
250
                    }
251
252 4e865771 gaqhf
                    double minX = 0;
253
                    double minY = 0;
254
                    double maxX = 0;
255
                    double maxY = 0;
256
                    dependencyObject.DrawingObjects[0].Range(out minX, out minY, out maxX, out maxY);
257
                }
258
                Symbol2d symbol2D = item as Symbol2d;
259
                if (symbol2D != null)
260
                {
261
                    double minX = 0;
262
                    double minY = 0;
263
                    double maxX = 0;
264
                    double maxY = 0;
265
                    symbol2D.Range(out minX, out minY, out maxX, out maxY);
266 d9fc7084 gaqhf
                }
267
            }
268 90662b0b gaqhf
            
269 4e865771 gaqhf
270 154d8f43 gaqhf
            return;
271 4941f5fe gaqhf
            Thread outThread = new Thread(func2 =>
272
            {
273
                for (int i = 100; i < 120; i++)
274
                {
275
                    Thread thread = new Thread(func =>
276
                    {
277
                        dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
278
                        //WrapperApplication wApp = new WrapperApplication(application.Application);
279
                        //Ingr.RAD2D.Application radApp = wApp.RADApplication;
280
                        dynamic drawing = application.Drawings.Add("Unit1", "D-Size.pid", i.ToString(), i.ToString());//application.Drawings.OpenDrawing("123");
281
282
                        Thread.Sleep(5000);
283
284
                        drawing.CloseDrawing(true);
285
286
                        //application.Quit();
287
                        ReleaseCOMObjects(application);
288
                        application = null;
289
                        ReleaseCOMObjects(drawing);
290
                        drawing = null;
291
                        Thread.Sleep(5000);
292
                    });
293
                    thread.Start();
294
                    thread.Join();
295
                }
296
            });
297
            outThread.Start();
298
            
299
            
300
301 32205389 gaqhf
302 ca6e0f51 gaqhf
303 4941f5fe gaqhf
            //radApp.Documents.Add(@"\\server70\DEVDOF1Site\GS\PLANT\PLANT1\Unit1\11111.pid");
304
305
            //radApp.ActiveDocument.Close(true);
306
            //drawing.CloseDrawing(true);
307
308
            //ReleaseCOMObjects(application);
309
            //ReleaseCOMObjects(drawing);
310
            //application = null;
311
            //wApp = null;
312
            //radApp = null;
313
            //drawing = null;
314 ca6e0f51 gaqhf
        }
315 4941f5fe gaqhf
316 ca6e0f51 gaqhf
        private void AutoJoinPipeRun()
317
        {
318 d4c3e39f gaqhf
            dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
319
            WrapperApplication wApp = new WrapperApplication(application.Application);
320
            Ingr.RAD2D.Application radApp = wApp.RADApplication;
321
322 ca6e0f51 gaqhf
            string modelItemId = null;
323
            List<double[]> vertices = new List<double[]>();
324 a31a512e gaqhf
            if (radApp.ActiveSelectSet.Count == 0)
325
            {
326
                return;
327
            }
328 ca6e0f51 gaqhf
            dynamic OID = radApp.ActiveSelectSet[0].Key();
329
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
330
            foreach (var attributes in drawingObject.AttributeSets)
331 4ba01591 gaqhf
            {
332 ca6e0f51 gaqhf
                foreach (var attribute in attributes)
333 4ba01591 gaqhf
                {
334 ca6e0f51 gaqhf
                    if (attribute.Name == "ModelID")
335
                        modelItemId = attribute.GetValue().ToString();
336
                }
337
            }
338
            radApp.ActiveSelectSet.RemoveAll();
339 32205389 gaqhf
340 7e4a64a3 gaqhf
        }
341
        #endregion
342
343 7aee331b gaqhf
344 9628f54b gaqhf
        [DllImport("user32.dll")]
345
        public static extern int FindWindow(string lpClassName, string lpWindowName);
346 3734dcc5 gaqhf
347
        [DllImport("user32.dll", SetLastError = true)]
348
        static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
349
350 b427ebf9 humkyung
        private void ConverterDocking_Load(object sender, EventArgs e)
351
        {
352
#if DEBUG
353
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
354
#else
355
            this.layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
356
#endif
357
358
        }
359 65881d60 gaqhf
    }
360
}
클립보드 이미지 추가 (최대 크기: 500 MB)