프로젝트

일반

사용자정보

통계
| 개정판:

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

이력 | 보기 | 이력해설 | 다운로드 (10.9 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 item in AvevaACAD18String.LabelNames)
184
                                datas.Add(new Tuple<string, string>(item, "No"));
185

    
186
                            foreach (var tuple in datas)
187
                            {
188
                                Infragistics.Win.UltraWinGrid.UltraGridRow row = UI.GetGridRow(tuple.Item1);
189
                                if (row != null)
190
                                    row.Cells["Value"].Value = tuple.Item2;
191
                            }
192

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

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

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

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

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

    
276

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

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