프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / Utils / AvevaThread.cs @ faba1fc7

이력 | 보기 | 이력해설 | 다운로드 (10.8 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Runtime.InteropServices;
5
using System.Text;
6
using System.Threading.Tasks;
7
using System.Threading;
8
using System.Collections;
9
using System.Configuration;
10
using System.Data;
11
using System.Data.Common;
12
using System.Data.SqlClient;
13
using System.IO;
14
using System.Runtime.CompilerServices;
15
using Microsoft.VisualBasic.CompilerServices;
16

    
17
using Autodesk.AutoCAD.ApplicationServices;
18
using Autodesk.AutoCAD.ApplicationServices.Core;
19
using Autodesk.AutoCAD.DatabaseServices;
20
using Autodesk.AutoCAD.EditorInput;
21
using Autodesk.AutoCAD.Geometry;
22
using Autodesk.AutoCAD.Interop;
23
using Autodesk.AutoCAD.Interop.Common;
24
using Autodesk.AutoCAD.Runtime;
25
using Autodesk.AutoCAD.Windows;
26

    
27
using AVEVA.PID.Components;
28
using AVEVA.PID.GUI;
29
using AVEVA.PID.Common;
30
using AVEVA.PID.DWGSelector;
31
using AVEVA.PID.Utilities;
32

    
33
using AVEVA.PID.CustomizationUtility.DB;
34
using AVEVA.PID.CustomizationUtility.Model;
35
using AVEVA.PID.CustomizationUtility.Properties;
36

    
37
namespace AVEVA.PID.CustomizationUtility
38
{
39
    [Flags]
40
    public enum ThreadType
41
    {
42
        None = 0,
43
        LineNumberModeling = 1,
44
        AddProperty = 2,
45
        DuplicationWarning = 4,
46
        GetAttributeInformationFromForm = 8,
47
    }
48

    
49
    class AvevaThread
50
    {
51
        [DllImport("user32")]
52
        public static extern int FindWindow(string lpClassName, string lpWindowName);
53
        [DllImport("user32.dll")]
54
        public static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);
55
        [DllImport("user32")]
56
        public static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr WParam, IntPtr LParam);
57
        [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
58
        static extern long GetClassName(IntPtr hwnd, StringBuilder lpClassName, long nMaxCount);
59
        static string GetClassNameOfWindow(IntPtr hwnd)
60
        {
61
            string className = "";
62
            StringBuilder classText = null;
63
            try
64
            {
65
                int cls_max_length = 1000;
66
                classText = new StringBuilder("", cls_max_length + 5);
67
                GetClassName(hwnd, classText, cls_max_length + 2);
68

    
69
                if (!String.IsNullOrEmpty(classText.ToString()) && !String.IsNullOrWhiteSpace(classText.ToString()))
70
                    className = classText.ToString();
71
            }
72
            catch (System.Exception ex)
73
            {
74
                className = ex.Message;
75
            }
76
            finally
77
            {
78
                classText = null;
79
            }
80
            return className;
81
        }
82

    
83
        const int BM_CLICK = 0x00F5;
84
        const int LoopTime = 500;
85
        private static ThreadType Running;
86

    
87
        public static bool IsRunning(ThreadType type)
88
        {
89
            if (Running.HasFlag(type))
90
                return true;
91
            else
92
                return false;
93
        }
94

    
95
        public static void Run(ThreadType type, params object[] param)
96
        {
97
            if ((type & (type - 1)) == 0)
98
            {
99
                if (type.HasFlag(ThreadType.LineNumberModeling) && !Running.HasFlag(ThreadType.LineNumberModeling))
100
                {
101
                    Thread thread = new Thread(LineNumberModeling);
102
                    thread.IsBackground = true;
103
                    thread.Start();
104
                    Running |= ThreadType.LineNumberModeling;
105
                }
106
                else if (type.HasFlag(ThreadType.AddProperty) && !Running.HasFlag(ThreadType.AddProperty))
107
                {
108
                    Thread thread = new Thread(() => AddProperty(param[0] as List<Tuple<string, string>>));
109
                    thread.IsBackground = true;
110
                    thread.Start();
111
                    Running |= ThreadType.AddProperty;
112
                }
113
                else if (type.HasFlag(ThreadType.DuplicationWarning) && !Running.HasFlag(ThreadType.DuplicationWarning))
114
                {
115
                    Thread thread = new Thread(DuplicationWarningMessageBox);
116
                    thread.IsBackground = true;
117
                    thread.Start();
118
                    Running |= ThreadType.DuplicationWarning;
119
                }
120
                else if (type.HasFlag(ThreadType.GetAttributeInformationFromForm) && !Running.HasFlag(ThreadType.GetAttributeInformationFromForm))
121
                {
122
                    Thread thread = new Thread(GetAttributeInformationFromForm);
123
                    thread.IsBackground = true;
124
                    thread.Start();
125
                    Running |= ThreadType.GetAttributeInformationFromForm;
126
                }
127
            }
128
        }
129

    
130
        public static void Stop(ThreadType type)
131
        {
132
            Running &= ~type;
133
        }
134

    
135
        public static void LineNumberModeling()
136
        {
137
            try
138
            {
139
                while (Running.HasFlag(ThreadType.LineNumberModeling))
140
                {
141
                    int handle = FindPropertiesFormHandle();
142
                    if (handle > 0)
143
                    {
144
                        string name = GetClassNameOfWindow((IntPtr)handle);
145

    
146
                        System.Windows.Forms.Control control = System.Windows.Forms.Control.FromHandle((IntPtr)handle);
147
                        if (control != null && control.GetType() == typeof(ComponentPropertiesUI))
148
                        {
149
                            ComponentPropertiesUI UI = control as ComponentPropertiesUI;
150
                            Infragistics.Win.UltraWinGrid.UltraGridRow row = UI.GetGridRow("AddLabel");
151
                            if (row != null)
152
                                row.Cells["Value"].Value = "Yes";
153
                        }
154

    
155
                        int button = FindWindowEx(handle, 0, null, "Ok");
156
                        if (button > 0)
157
                        {
158
                            SendMessage((IntPtr)button, BM_CLICK, (IntPtr)0, (IntPtr)0);
159
                            Running &= ~ThreadType.LineNumberModeling;
160
                        }
161
                    }
162
                    Thread.Sleep(LoopTime);
163
                }
164
            }
165
            catch (System.Exception ex)
166
            {
167

    
168
            }
169
        }
170
        public static void AddProperty(List<Tuple<string, string>> datas)
171
        {
172
            try
173
            {
174
                while (Running.HasFlag(ThreadType.AddProperty))
175
                {
176
                    int handle = FindPropertiesFormHandle();
177
                    if (handle > 0)
178
                    {
179
                        System.Windows.Forms.Control control = System.Windows.Forms.Control.FromHandle((IntPtr)handle);
180
                        if (control != null && control.GetType() == typeof(ComponentPropertiesUI))
181
                        {
182
                            ComponentPropertiesUI UI = control as ComponentPropertiesUI;
183
                            foreach (var tuple in datas)
184
                            {
185
                                Infragistics.Win.UltraWinGrid.UltraGridRow row = UI.GetGridRow(tuple.Item1);
186
                                if (row != null)
187
                                    row.Cells["Value"].Value = tuple.Item2;
188
                            }
189

    
190
                            int button = FindWindowEx(handle, 0, null, "Ok");
191
                            if (button > 0)
192
                            {
193
                                SendMessage((IntPtr)button, BM_CLICK, (IntPtr)0, (IntPtr)0);
194
                            }
195
                        }
196
                        Running &= ~ThreadType.AddProperty;
197
                        break;
198
                    }
199
                    Thread.Sleep(LoopTime);
200
                }
201
            }
202
            catch (System.Exception ex)
203
            {
204

    
205
            }
206
        }
207
        public static void DuplicationWarningMessageBox()
208
        {
209
            try
210
            {
211
                while (Running.HasFlag(ThreadType.DuplicationWarning))
212
                {
213
                    int handle = FindWindow(null, AvevaACAD18String.DuplicationWarningMessageBox);
214
                    if (handle > 0)
215
                    {
216
                        int button = FindWindowEx(handle, 0, null, "Ok");
217
                        if (button > 0)
218
                        {
219
                            SendMessage((IntPtr)button, BM_CLICK, (IntPtr)0, (IntPtr)0);
220
                        }
221
                    }
222
                    Thread.Sleep(LoopTime);
223
                }
224
            }
225
            catch (System.Exception ex)
226
            {
227

    
228
            }
229
        }
230
        public static void GetAttributeInformationFromForm()
231
        {
232
            List<Tuple<string, string, string, string>> datas = new List<Tuple<string, string, string, string>>();
233
            while (Running.HasFlag(ThreadType.GetAttributeInformationFromForm))
234
            {
235
                int handle = FindPropertiesFormHandle();
236
                if (handle > 0)
237
                {
238
                    System.Windows.Forms.Control control = System.Windows.Forms.Control.FromHandle((IntPtr)handle);
239
                    if (control != null && control.GetType() == typeof(ComponentPropertiesUI))
240
                    {
241
                        ComponentPropertiesUI UI = control as ComponentPropertiesUI;
242
                        
243
                        string level1 = UI.Text;
244
                        foreach (PropertyRegion region in UI.PropertyRegions)
245
                        {
246
                            string level2 = region.Title;
247
                            foreach (PropertyField field in region.PropertyFields)
248
                            {
249
                                string attributeKey = field.Key;
250
                                string attributeName = field.Name;
251
                                datas.Add(new Tuple<string, string, string, string>(level1, level2, attributeKey, attributeName));
252
                            }
253
                        }
254

    
255
                        int button = FindWindowEx(handle, 0, null, "Cancel");
256
                        if (button > 0)
257
                        {
258
                            SendMessage((IntPtr)button, BM_CLICK, (IntPtr)0, (IntPtr)0);
259
                            Running &= ~ThreadType.GetAttributeInformationFromForm;
260
                            break;
261
                        }
262
                    }
263
                }
264
            }
265

    
266
            if (datas.Count > 0)
267
            {
268
                if (!Project_DB.InsertAPIDAttribute(datas))
269
                    System.Windows.Forms.MessageBox.Show("Fail save to database...", "APID Converter", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
270
            }
271
        }
272

    
273

    
274
        public static int FindPropertiesFormHandle()
275
        {
276
            int result = 0;
277
            foreach (var item in AvevaACAD18String.PropertiesFormNames)
278
            {
279
                result = FindWindow(null, item);
280
                if (result > 0)
281
                    break;
282
            }
283

    
284
            return result;
285
        }
286
       
287
    }
288
}
클립보드 이미지 추가 (최대 크기: 500 MB)