hytos / DTI_PID / APIDConverter / Utils / AvevaThread.cs @ 31cd836b
이력 | 보기 | 이력해설 | 다운로드 (11.3 KB)
1 | c8b025e0 | gaqhf | 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 | c94252bf | gaqhf | LineNumberModeling = 1, |
44 | c8b025e0 | gaqhf | AddProperty = 2, |
45 | d03dde83 | gaqhf | DuplicationWarning = 4, |
46 | GetAttributeInformationFromForm = 8, |
||
47 | c8b025e0 | gaqhf | } |
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 | 87f22597 | gaqhf | [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 | c8b025e0 | gaqhf | |
69 | 87f22597 | gaqhf | 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 | c8b025e0 | gaqhf | |
83 | 87f22597 | gaqhf | const int BM_CLICK = 0x00F5; |
84 | const int LoopTime = 500; |
||
85 | c8b025e0 | gaqhf | 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 | c94252bf | gaqhf | if (type.HasFlag(ThreadType.LineNumberModeling) && !Running.HasFlag(ThreadType.LineNumberModeling)) |
100 | c8b025e0 | gaqhf | { |
101 | c94252bf | gaqhf | Thread thread = new Thread(LineNumberModeling); |
102 | c8b025e0 | gaqhf | thread.IsBackground = true; |
103 | thread.Start(); |
||
104 | c94252bf | gaqhf | Running |= ThreadType.LineNumberModeling; |
105 | c8b025e0 | gaqhf | } |
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 | c94252bf | gaqhf | 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 | d03dde83 | gaqhf | 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 | c8b025e0 | gaqhf | } |
128 | } |
||
129 | |||
130 | public static void Stop(ThreadType type) |
||
131 | { |
||
132 | Running &= ~type; |
||
133 | } |
||
134 | |||
135 | c94252bf | gaqhf | public static void LineNumberModeling() |
136 | c8b025e0 | gaqhf | { |
137 | f25808d5 | gaqhf | while (Running.HasFlag(ThreadType.LineNumberModeling)) |
138 | c8b025e0 | gaqhf | { |
139 | f25808d5 | gaqhf | int handle = FindPropertiesFormHandle(); |
140 | if (handle > 0) |
||
141 | c8b025e0 | gaqhf | { |
142 | f25808d5 | gaqhf | string name = GetClassNameOfWindow((IntPtr)handle); |
143 | c94252bf | gaqhf | |
144 | f25808d5 | gaqhf | 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 | c94252bf | gaqhf | |
153 | f25808d5 | gaqhf | 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 | c8b025e0 | gaqhf | } |
159 | } |
||
160 | f25808d5 | gaqhf | Thread.Sleep(LoopTime); |
161 | c8b025e0 | gaqhf | } |
162 | } |
||
163 | public static void AddProperty(List<Tuple<string, string>> datas) |
||
164 | { |
||
165 | f25808d5 | gaqhf | while (Running.HasFlag(ThreadType.AddProperty)) |
166 | c8b025e0 | gaqhf | { |
167 | f25808d5 | gaqhf | int handle = FindPropertiesFormHandle(); |
168 | if (handle > 0) |
||
169 | c8b025e0 | gaqhf | { |
170 | f25808d5 | gaqhf | System.Windows.Forms.Control control = System.Windows.Forms.Control.FromHandle((IntPtr)handle); |
171 | if (control != null && control.GetType() == typeof(ComponentPropertiesUI)) |
||
172 | c8b025e0 | gaqhf | { |
173 | f25808d5 | gaqhf | ComponentPropertiesUI UI = control as ComponentPropertiesUI; |
174 | foreach (var item in AvevaACAD18String.LabelNames) |
||
175 | datas.Add(new Tuple<string, string>(item, "No")); |
||
176 | de72e600 | gaqhf | |
177 | f25808d5 | gaqhf | 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 | c8b025e0 | gaqhf | |
184 | f25808d5 | gaqhf | int button = FindWindowEx(handle, 0, null, "OK"); |
185 | if (button > 0) |
||
186 | { |
||
187 | SendMessage((IntPtr)button, BM_CLICK, (IntPtr)0, (IntPtr)0); |
||
188 | c8b025e0 | gaqhf | } |
189 | } |
||
190 | f25808d5 | gaqhf | Running &= ~ThreadType.AddProperty; |
191 | break; |
||
192 | c8b025e0 | gaqhf | } |
193 | f25808d5 | gaqhf | Thread.Sleep(LoopTime); |
194 | c8b025e0 | gaqhf | } |
195 | } |
||
196 | c94252bf | gaqhf | 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 | 20dd244c | gaqhf | int button = FindWindowEx(handle, 0, null, "OK"); |
206 | c94252bf | gaqhf | if (button > 0) |
207 | { |
||
208 | SendMessage((IntPtr)button, BM_CLICK, (IntPtr)0, (IntPtr)0); |
||
209 | } |
||
210 | } |
||
211 | 87f22597 | gaqhf | Thread.Sleep(LoopTime); |
212 | c94252bf | gaqhf | } |
213 | } |
||
214 | catch (System.Exception ex) |
||
215 | { |
||
216 | |||
217 | } |
||
218 | } |
||
219 | d03dde83 | gaqhf | public static void GetAttributeInformationFromForm() |
220 | { |
||
221 | faba1fc7 | gaqhf | List<Tuple<string, string, string, string>> datas = new List<Tuple<string, string, string, string>>(); |
222 | d03dde83 | gaqhf | 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 | faba1fc7 | gaqhf | |
232 | d03dde83 | gaqhf | 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 | a6d63eda | gaqhf | if (field.EditMode == PropertyEditMode.ReadOnly || |
239 | field.EditMode == PropertyEditMode.HideAndReadOnly || |
||
240 | field.PropertyType == PropertyType.Optional) |
||
241 | continue; |
||
242 | faba1fc7 | gaqhf | string attributeKey = field.Key; |
243 | string attributeName = field.Name; |
||
244 | datas.Add(new Tuple<string, string, string, string>(level1, level2, attributeKey, attributeName)); |
||
245 | d03dde83 | gaqhf | } |
246 | } |
||
247 | |||
248 | int button = FindWindowEx(handle, 0, null, "Cancel"); |
||
249 | if (button > 0) |
||
250 | { |
||
251 | Running &= ~ThreadType.GetAttributeInformationFromForm; |
||
252 | a6d63eda | gaqhf | SendMessage((IntPtr)button, BM_CLICK, (IntPtr)0, (IntPtr)0); |
253 | d03dde83 | gaqhf | break; |
254 | } |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | faba1fc7 | gaqhf | |
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 | d03dde83 | gaqhf | } |
265 | c8b025e0 | gaqhf | |
266 | faba1fc7 | gaqhf | |
267 | 87f22597 | gaqhf | 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 | e7770ee1 | gaqhf | |
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 | d03dde83 | gaqhf | |
290 | c8b025e0 | gaqhf | } |
291 | } |