-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSEdit2.cs
354 lines (321 loc) · 12.1 KB
/
SSEdit2.cs
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/// ssedit2.cs (c)2012 Tony Tanzillo
///
/// An updated version of ssedit.cs, that was originally
/// published on the Autodesk discussion group server in
/// 2012.
///
/// More recent updates:
///
/// A revision was made to eliminate a distracting message
/// from appearing when the EditSelection() extension method
/// is called ("nnn found"). that appeared before the initial
/// "Select objects: " prompt was displayed.
///
/// Zoom support:
///
/// Support for automatically zooming to the extents of the
/// selection set to be edited has been added but has not
/// been tested thoroughly.
///
/// The optional third argument to the EditSelection() extension
/// method specifies if the editor is zoomed to the extents of
/// the selection set to be edited. The value of this argument
/// is false by default.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Internal;
using Autodesk.AutoCAD.Runtime;
namespace Autodesk.AutoCAD.EditorInput
{
/// <summary>
/// Adds the EditSelection() extension method to the
/// Editor class, that provides access to the included
/// SelectionSetEditor class that enables interactive
/// editing of the contents of an existing selection
/// set.
///
/// When called and passed an existing selection set,
/// the objects in that selection set are highlighted
/// and the user can add and/or remove entities to/from
/// the selection. Optionally, the editor can be zoomed
/// to the extents of the existing selection set.
///
/// Usage notes:
///
/// When this method is called, some or all entities
/// in the existing selection set may not be visible
/// in the current view. So, depending on the use case,
/// it may be necessary or appropriate to zoom the view
/// to the extents of the entities in the selection set
/// just before this method is called, or the optional
/// ZOOM argument can be specified. However, if all of
/// the objects in the selection set are in the current
/// view, the view will still be zoomed, which may not
/// be desirable.
///
/// A future update to this method may attempt to avoid
/// zooming to the extents of the objects if they are all
/// already visible in the current view, however doing
/// that is non-trivial.
///
/// Selection metadata:
///
/// Graphical selection information from objects in the
/// selection set to be edited is not preserved when
/// editing it. The resulting selection set will contain
/// only metadata describing how objects that were added
/// to the selection set were selected.
///
/// </summary>
public static partial class EditorExtensionMethods
{
/// <summary>
/// Allows interactive editing of the contents of an
/// existing selection set, returning a new selection
/// set containg the result, which can include none,
/// some, or all entities in the existing selection
/// set, along with other entities that were added.
/// </summary>
/// <param name="ed">The Editor of the active document</param>
/// <param name="ss">The selection set to be edited, which
/// can be either a selection set or an IEnumerable<ObjectId>
/// </param>
/// <param name="options">The PromptSelectionOptions to be used
/// in the editing operation, or null to use the default options</param>
/// <param name="zoom">A value indicating if the current view
/// should be zoomed to the extents of the selection set that
/// is to be edited. The default value is false (no zoom)</param>
/// <returns></returns>
public static PromptSelectionResult EditSelection(this Editor ed, SelectionSet ss, PromptSelectionOptions options = null, bool zoom = false)
{
return SelectionSetEditor.Edit(ed, ss, options, zoom);
}
public static PromptSelectionResult EditSelection(this Editor ed, IEnumerable<ObjectId> ids, PromptSelectionOptions options = null, bool zoom = false)
{
return SelectionSetEditor.Edit(ed, ids, options, zoom);
}
}
/// <summary>
/// A class that allows the contents of a selection set
/// to be edited interactively, allowing the user to add
/// and/or remove objects to/from the selection.
/// </summary>
class SelectionSetEditor
{
Editor ed = null;
PromptSelectionOptions options = null;
ObjectId[] selection = null;
const string keyword = "SSEDIT";
public SelectionSetEditor(Editor editor, SelectionSet ss, PromptSelectionOptions options = null)
: this(editor, ss?.GetObjectIds(), options)
{
}
public SelectionSetEditor(Editor editor, IEnumerable<ObjectId> ss, PromptSelectionOptions options = null)
{
if(editor is null)
throw new ArgumentNullException("editor");
if(ss is null)
throw new ArgumentNullException("ss");
this.ed = editor;
this.selection = ss as ObjectId[] ?? ss.ToArray();
if(selection.Length > 0)
{
this.options = options ?? new PromptSelectionOptions();
this.options.Keywords.Add(keyword, keyword, keyword, false, true);
}
}
void keywordInput(object sender, SelectionTextInputEventArgs e)
{
options.KeywordInput -= keywordInput;
if(selection is not null && selection.Length > 0)
{
e.AddObjects(selection);
Application.SetSystemVariable("NOMUTT", 1);
Application.Idle += idle;
}
selection = null;
}
/// <summary>
/// Added to supress the "nnn added" message that
/// appears after the above code adds the initial
/// objects to the selection.
///
/// This handler merely resets the NOMUTT sysvar
/// to 0 and displays the "Select objects: " prompt
/// that is also supressed by NOMUTT:
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void idle(object sender, EventArgs e)
{
Application.Idle -= idle;
Application.SetSystemVariable("NOMUTT", 0);
string msg = options.MessageForAdding;
if(string.IsNullOrWhiteSpace(msg))
msg = "\nSelect objects: "; // Localization required
ed.WriteMessage(msg);
}
public static PromptSelectionResult Edit(Editor editor,
SelectionSet selectionSet,
PromptSelectionOptions options = null,
bool zoom = false)
{
if(editor is null)
throw new ArgumentNullException(nameof(editor));
if(selectionSet is null)
throw new ArgumentNullException(nameof(selectionSet));
return new SelectionSetEditor(editor, selectionSet, options)
.EditSelection(zoom);
}
public static PromptSelectionResult Edit(Editor editor,
IEnumerable<ObjectId> selectionSet,
PromptSelectionOptions options = null,
bool zoom = false)
{
if(editor is null)
throw new ArgumentNullException(nameof(editor));
if(selectionSet is null)
throw new ArgumentNullException(nameof(selectionSet));
return new SelectionSetEditor(editor, selectionSet, options)
.EditSelection(zoom);
}
PromptSelectionResult EditSelection(bool zoom)
{
if(zoom)
ZoomObjects(ed, this.selection, 0.95);
if(selection is not null && selection.Length > 0)
{
options.KeywordInput += keywordInput;
ed.Document.SendStringToExecute($"{keyword}\n", true, false, false);
}
return ed.GetSelection(options);
}
static void ZoomObjects(Editor editor, ObjectId[] ids, double factor = 1.0, bool restorePickfirst = true)
{
if(editor is null)
throw new ArgumentNullException(nameof(editor));
if(ids is null || ids.Length == 0)
return;
SelectionSet pickfirst = null;
if(restorePickfirst)
{
var psr = editor.SelectImplied();
if(psr.Status == PromptStatus.OK && psr.Value?.Count > 0)
pickfirst = psr.Value;
}
using(new SysVar("GRIPS", 0))
using(new SysVar("GTAUTO", 0))
{
editor.SetImpliedSelection(ids);
try
{
using(var view = editor.GetCurrentView())
{
try
{
Utils.ZoomObjects(view.PerspectiveEnabled);
if(factor != 1.0)
ViewUtil.GsLensLength *= factor;
}
catch(FileLoadException)
{
}
}
}
finally
{
if(restorePickfirst)
editor.SetImpliedSelection(pickfirst);
}
}
}
}
/// <summary>
/// Lifted from SystemVariable.cs
/// </summary>
class SysVar : IDisposable
{
object oldvalue = null;
string name;
public SysVar(string name, object value)
{
if(string.IsNullOrWhiteSpace(name))
throw new ArgumentException(nameof(name));
this.name = name;
object current = Application.GetSystemVariable(name);
if(!object.Equals(current, value))
{
oldvalue = current;
Application.SetSystemVariable(name, value);
}
}
public void Dispose()
{
if(oldvalue != null)
{
Application.SetSystemVariable(name, oldvalue);
oldvalue = null;
}
}
}
/// <summary>
/// An example showing how the SelectionSetEditor class is used:
/// </summary>
public static class SSEditExampleCommands
{
static SelectionSet currentSelection;
/// <summary>
/// A command to define the selection set to be edited.
/// </summary>
[CommandMethod("SSEDITSELECT")]
public static void SSEditSelect()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
/// Get an initial selection set from the
/// user that we will then allow them to edit:
Editor ed = doc.Editor;
ed.WriteMessage("\nSpecify initial selection set to edit,");
var psr = ed.GetSelection();
if(psr.Status != PromptStatus.OK)
return;
ed.WriteMessage($"\nInitial selection count = {psr.Value.Count}");
currentSelection = psr.Value;
}
[CommandMethod("SSEDIT", CommandFlags.Redraw)]
public static void SSEdit()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
try
{
if(currentSelection == null)
{
SSEditSelect();
if(currentSelection == null)
return;
}
/// Allow the user to edit the selection set
/// acquired using the SSEDITSELECT command:
ed.WriteMessage("\nSpecify objects to add " +
"to/remove from original selection,");
PromptSelectionResult result = ed.EditSelection(currentSelection);
if(result != null && result.Value != null)
{
var ids = result.Value.GetObjectIds();
ed.SetImpliedSelection(result.Value.GetObjectIds());
doc.Editor.WriteMessage(
$"\nEdited selection Count = {result.Value.Count}");
}
currentSelection = null;
}
catch(System.Exception ex)
{
ed.WriteMessage($"Error: {ex.ToString()}");
}
}
}
}