프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / Utils / AvevaThread.cs @ 94ac8af8

이력 | 보기 | 이력해설 | 다운로드 (11.3 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
            while (Running.HasFlag(ThreadType.LineNumberModeling))
138
            {
139
                int handle = FindPropertiesFormHandle();
140
                if (handle > 0)
141
                {
142
                    string name = GetClassNameOfWindow((IntPtr)handle);
143

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

    
153
                    int button = FindWindowEx(handle, 0, null, "OK");
154
                    if (button > 0)
155
                    {
156
                        SendMessage((IntPtr)button, BM_CLICK, (IntPtr)0, (IntPtr)0);
157
                        Running &= ~ThreadType.LineNumberModeling;
158
                    }
159
                }
160
                Thread.Sleep(LoopTime);
161
            }
162
        }
163
        public static void AddProperty(List<Tuple<string, string>> datas)
164
        {
165
            while (Running.HasFlag(ThreadType.AddProperty))
166
            {
167
                int handle = FindPropertiesFormHandle();
168
                if (handle > 0)
169
                {
170
                    System.Windows.Forms.Control control = System.Windows.Forms.Control.FromHandle((IntPtr)handle);
171
                    if (control != null && control.GetType() == typeof(ComponentPropertiesUI))
172
                    {
173
                        ComponentPropertiesUI UI = control as ComponentPropertiesUI;
174
                        foreach (var item in AvevaACAD18String.LabelNames)
175
                            datas.Add(new Tuple<string, string>(item, "No"));
176

    
177
                        foreach (var tuple in datas)
178
                        {
179
                            Infragistics.Win.UltraWinGrid.UltraGridRow row = UI.GetGridRow(tuple.Item1);
180
                            if (row != null)
181
                                row.Cells["Value"].Value = tuple.Item2;
182
                        }
183

    
184
                        int button = FindWindowEx(handle, 0, null, "OK");
185
                        if (button > 0)
186
                        {
187
                            SendMessage((IntPtr)button, BM_CLICK, (IntPtr)0, (IntPtr)0);
188
                        }
189
                    }
190
                    Running &= ~ThreadType.AddProperty;
191
                    break;
192
                }
193
                Thread.Sleep(LoopTime);
194
            }
195
        }
196
        public static void DuplicationWarningMessageBox()
197
        {
198
            try
199
            {
200
                while (Running.HasFlag(ThreadType.DuplicationWarning))
201
                {
202
                    int handle = FindWindow(null, AvevaACAD18String.DuplicationWarningMessageBox);
203
                    if (handle > 0)
204
                    {
205
                        int button = FindWindowEx(handle, 0, null, "OK");
206
                        if (button > 0)
207
                        {
208
                            SendMessage((IntPtr)button, BM_CLICK, (IntPtr)0, (IntPtr)0);
209
                        }
210
                    }
211
                    Thread.Sleep(LoopTime);
212
                }
213
            }
214
            catch (System.Exception ex)
215
            {
216

    
217
            }
218
        }
219
        public static void GetAttributeInformationFromForm()
220
        {
221
            List<Tuple<string, string, string, string>> datas = new List<Tuple<string, string, string, string>>();
222
            while (Running.HasFlag(ThreadType.GetAttributeInformationFromForm))
223
            {
224
                int handle = FindPropertiesFormHandle();
225
                if (handle > 0)
226
                {
227
                    System.Windows.Forms.Control control = System.Windows.Forms.Control.FromHandle((IntPtr)handle);
228
                    if (control != null && control.GetType() == typeof(ComponentPropertiesUI))
229
                    {
230
                        ComponentPropertiesUI UI = control as ComponentPropertiesUI;
231
                        
232
                        string level1 = UI.Text;
233
                        foreach (PropertyRegion region in UI.PropertyRegions)
234
                        {
235
                            string level2 = region.Title;
236
                            foreach (PropertyField field in region.PropertyFields)
237
                            {
238
                                if (field.EditMode == PropertyEditMode.ReadOnly || 
239
                                    field.EditMode == PropertyEditMode.HideAndReadOnly || 
240
                                    field.PropertyType == PropertyType.Optional)
241
                                    continue;
242
                                string attributeKey = field.Key;
243
                                string attributeName = field.Name;
244
                                datas.Add(new Tuple<string, string, string, string>(level1, level2, attributeKey, attributeName));
245
                            }
246
                        }
247

    
248
                        int button = FindWindowEx(handle, 0, null, "Cancel");
249
                        if (button > 0)
250
                        {
251
                            Running &= ~ThreadType.GetAttributeInformationFromForm;
252
                            SendMessage((IntPtr)button, BM_CLICK, (IntPtr)0, (IntPtr)0);
253
                            break;
254
                        }
255
                    }
256
                }
257
            }
258

    
259
            if (datas.Count > 0)
260
            {
261
                if (!Project_DB.InsertAPIDAttribute(datas))
262
                    System.Windows.Forms.MessageBox.Show("Fail save to database...", "APID Converter", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
263
            }
264
        }
265

    
266

    
267
        public static int FindPropertiesFormHandle()
268
        {
269
            int result = 0;
270
            foreach (var item in AvevaACAD18String.PropertiesFormNames)
271
            {
272
                result = FindWindow(null, item);
273
                if (result > 0)
274
                    break;
275
            }
276

    
277
            return result;
278
        }
279

    
280
        public static void Test()
281
        {
282
            while (true)
283
            {
284
                Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
285
                Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
286
                Editor editor = acDoc.Editor;
287
            }
288
        }
289
       
290
    }
291
}
클립보드 이미지 추가 (최대 크기: 500 MB)