-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChh
326 lines (273 loc) · 11 KB
/
Chh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
Here is he code of class MyCadDataHandler:
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
namespace ShowProgressBar
{
public class MyCadDataHandler : ILongProcessingObject
{
private enum LongProcessingType
{
Type1=0,
Type2=1,
}
private Document _dwg;
private LongProcessingType _processingType = LongProcessingType.Type1;
public MyCadDataHandler(Document dwg)
{
_dwg = dwg;
}
#region public methods
public void DoWorkWithKnownLoopCount()
{
_processingType = LongProcessingType.Type1;
using (var progress=new ProcessingProgressBar(this))
{
progress.Start();
}
}
public void DoWorkWithUnknownLoopCount()
{
_processingType = LongProcessingType.Type2;
using (var progress = new ProcessingProgressBar(this))
{
progress.Start();
}
}
#endregion
#region Implementing ILongProcessingObject interface
public event LongProcessStarted ProcessingStarted;
public event LongProcessingProgressed ProcessingProgressed;
public event EventHandler ProcessingEnded;
public event EventHandler CloseProgressUIRequested;
public void DoLongProcessingWork()
{
switch(_processingType)
{
case LongProcessingType.Type1:
LoopThroughModelSpace();
break;
case LongProcessingType.Type2:
SearchForTopBlocks("StationLabel", 500);
break;
}
}
#endregion
#region private methods
private void LoopThroughModelSpace()
{
try
{
//run 2 long processing loops
for (int n = 0; n < 2; n++)
{
using (var tran =
_dwg.TransactionManager.StartTransaction())
{
//Get all entities' ID in ModelSpace
BlockTableRecord model = (BlockTableRecord)
tran.GetObject(
SymbolUtilityServices.GetBlockModelSpaceId(
_dwg.Database), OpenMode.ForRead);
ObjectId[] entIds = model.Cast<ObjectId>().ToArray();
if (ProcessingStarted != null)
{
string process = n == 0 ?
"Searching ModelSpace for AAAA" :
"Search ModelSpace for BBBB";
LongProcessStartedEventArgs e =
new LongProcessStartedEventArgs(
process, entIds.Length, true);
ProcessingStarted(this, e);
}
int count = 0;
foreach (var entId in entIds)
{
count++;
if (ProcessingProgressed != null)
{
string progMsg = string.Format(
"{0} out of {1}. {2} remaining...\n" +
"Processing entity: {3}",
count,
entIds.Length,
entIds.Length-count,
entId.ObjectClass.DxfName);
LongProcessingProgressEventArgs e =
new LongProcessingProgressEventArgs(progMsg);
ProcessingProgressed(this, e);
//Since this processing is cancellable, we
//test if user clicked the "Stop" button in the
//progressing dialog box
if (e.Cancel) break;
}
//Do something with the entity
Entity ent = (Entity)tran.GetObject(
entId, OpenMode.ForRead);
long s = 0;
for (int i = 0; i < 1000000; i++)
{
s += i * i;
}
}
if (ProcessingEnded != null)
{
ProcessingEnded(this, EventArgs.Empty);
}
tran.Commit();
}
}
}
finally
{
//Make sure the CloseProgressUIRequested event always fires, so
//that the progress dialog box gets closed because of this event
if (CloseProgressUIRequested != null)
{
CloseProgressUIRequested(this, EventArgs.Empty);
}
}
}
private void SearchForTopBlocks(string blkName, int targetCount)
{
List<ObjectId> blkIds = new List<ObjectId>();
try
{
if (ProcessingStarted != null)
{
string msg = string.Format(
"Searching first {0} block refeences: \"{1}\"",
targetCount, blkName);
LongProcessStartedEventArgs e =
new LongProcessStartedEventArgs(msg);
ProcessingStarted(this, e);
}
using (var tran = _dwg.TransactionManager.StartTransaction())
{
//Get all entities' ID in ModelSpace
BlockTableRecord model = (BlockTableRecord)tran.GetObject(
SymbolUtilityServices.GetBlockModelSpaceId(
_dwg.Database), OpenMode.ForRead);
foreach (ObjectId id in model)
{
if (ProcessingProgressed != null)
{
string progMsg = string.Format(
"{0} found\n" +
"Processing entity: {1}",
blkIds.Count, id.ObjectClass.DxfName);
LongProcessingProgressEventArgs e =
new LongProcessingProgressEventArgs(progMsg);
ProcessingProgressed(this, e);
}
if (IsTargetBlock(id, blkName, tran))
{
blkIds.Add(id);
if (blkIds.Count == targetCount) break;
}
}
tran.Commit();
}
if (ProcessingEnded != null)
{
ProcessingEnded(this, EventArgs.Empty);
}
}
finally
{
//Make sure the CloseProgressUIRequested event always fires,
//so that the progress dialog box gets closed because of
//this event
if (CloseProgressUIRequested != null)
{
CloseProgressUIRequested(this, EventArgs.Empty);
}
}
}
private bool IsTargetBlock(
ObjectId entId, string blkName, Transaction tran)
{
//kill a bit time to allow progress bar effect
long s = 0;
for (int i = 0; i < 10000000; i++)
{
s += i * i;
}
BlockReference blk = tran.GetObject(
entId, OpenMode.ForRead) as BlockReference;
if (blk!=null)
{
string name;
if (blk.IsDynamicBlock)
{
BlockTableRecord br = (BlockTableRecord)
tran.GetObject(
blk.DynamicBlockTableRecord, OpenMode.ForRead);
name = br.Name;
}
else
{
name = blk.Name
}
return name.ToUpper() == blkName.ToUpper();
}
return false;
}
#endregion
}
}
As the code shows, it has 2 public methods that can be called in a CommandMethod to start the 2 lengthy data processing executions. The third public method DoLongProcessingWork() is the ILongProcessObject interface implementing method and is called when the progress bar hosting dialog box shows.
When I implementing the ILongProcessingObject interface, I have freedom to decide what exactly I want to do when ILongProcessObject.DoLongProcessWork() is called. In the code shown here, I used private enum type LongProcessingType to indicate what long processing operation to be executed. I also has the freedom (and am responsible) to decide when to raise events that stimulate progress bar and close the progress bar form.
Here is the CommandClass that uses MyCadDataHandler:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(ShowProgressBar.MyCadCommands))]
namespace ShowProgressBar
{
public class MyCadCommands
{
[CommandMethod("LongWork1")]
public static void RunLongWork_1()
{
Document dwg = CadApp.DocumentManager.MdiActiveDocument;
Editor ed = dwg.Editor;
try
{
MyCadDataHandler dataHandler = new MyCadDataHandler(dwg);
dataHandler.DoWorkWithKnownLoopCount();
}
catch (System.Exception ex)
{
ed.WriteMessage(
"\nError: {0}\n{1}", ex.Message, ex.StackTrace);
}
finally
{
Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
}
}
[CommandMethod("LongWork2")]
public static void RunLongWork_2()
{
Document dwg = CadApp.DocumentManager.MdiActiveDocument;
Editor ed = dwg.Editor;
try
{
MyCadDataHandler dataHandler = new MyCadDataHandler(dwg);
dataHandler.DoWorkWithUnknownLoopCount();
}
catch (System.Exception ex)
{
ed.WriteMessage(
"\nError: {0}\n{1}", ex.Message, ex.StackTrace);
}
finally
{
Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
}
}
}