forked from Pyxxil/EcogyPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.cs
More file actions
355 lines (287 loc) · 13.6 KB
/
Commands.cs
File metadata and controls
355 lines (287 loc) · 13.6 KB
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
355
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System.Linq;
using System.Text.RegularExpressions;
using iText.Kernel.Pdf;
using System.IO;
using System;
using iText.Kernel.Geom;
using System.Collections.Generic;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(Ecogy.Commands))]
namespace Ecogy
{
// This class is instantiated by AutoCAD for each document when
// a command is called by the user the first time in the context
// of a given document. In other words, non static data in this class
// is implicitly per-document!
public class Commands
{
private static readonly string REG_KEY_NAME = "Google Drive";
private static readonly string REG_KEY_DEPTH = "Google Drive Depth";
private static readonly RegistryKey dialogs = Registry.CurrentUser.OpenSubKey(
$@"{HostApplicationServices.Current.UserRegistryProductRootKey}\Profiles\{Application.GetSystemVariable("CPROFILE")}\Dialogs\AllAnavDialogs"
, true);
private static readonly Regex rgx = new Regex(@"PlacesOrder(\d+)$", RegexOptions.Compiled);
private static readonly Regex deleteRegex = new Regex(@"^PlacesOrder(\d+)", RegexOptions.Compiled);
private static int SpecSheetCount = 0;
public static void GoogleDrive()
{
if (dialogs.GetValue(REG_KEY_NAME) == null)
return;
var path = dialogs.GetValue(REG_KEY_NAME).ToString();
var depth = (int)dialogs.GetValue(REG_KEY_DEPTH);
var doc = Application.DocumentManager.MdiActiveDocument;
var documentPath = doc.Database.OriginalFileName;
var pathPostfix = "";
for (int i = 0; i < depth; i++)
{
documentPath = Directory.GetParent(documentPath).ToString();
pathPostfix = new DirectoryInfo(documentPath).Name + @"\" + pathPostfix;
}
var directory = $@"{path}\{pathPostfix}";
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var pos = 0;
var entries = dialogs
.GetValueNames()
.AsEnumerable()
.Where(value => rgx.IsMatch(value) && dialogs.GetValue(value).ToString().Length != 0)
.Select(value =>
{
int position = int.Parse(rgx.Match(value).Groups[1].ToString());
return (
position: pos++,
val: dialogs.GetValue($"PlacesOrder{position}").ToString(),
display: (dialogs.GetValue($"PlacesOrder{position}Display") ?? "").ToString(),
ext: (dialogs.GetValue($"PlacesOrder{position}Ext") ?? "").ToString()
);
})
.OrderBy(value => value.position)
.ToList();
foreach (var entry in dialogs.GetValueNames().AsEnumerable().Where(entry => deleteRegex.IsMatch(entry)))
{
dialogs.DeleteValue(entry);
}
foreach (var (position, val, display, ext) in entries)
{
dialogs.SetValue($"PlacesOrder{position}", val);
dialogs.SetValue($"PlacesOrder{position}Display", display);
dialogs.SetValue($"PlacesOrder{position}Ext", ext);
}
var match = entries.FindIndex(value => value.display == REG_KEY_NAME);
var idx = match == -1 ? entries.Count : entries[match].position;
var end = match == -1 ? entries.Count + 1 : entries.Count;
dialogs.SetValue($"PlacesOrder{idx}", directory);
dialogs.SetValue($"PlacesOrder{idx}Display", REG_KEY_NAME);
dialogs.SetValue($"PlacesOrder{idx}Ext", "");
dialogs.SetValue($"PlacesOrder{end}", "");
}
[CommandMethod("Ecogy", "AddGoogleDrive", CommandFlags.Modal)]
public void AddGoogleDrive()
{
var doc = Application.DocumentManager.MdiActiveDocument;
if (doc != null)
{
var ed = doc.Editor;
var flags = Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles |
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DefaultIsFolder |
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.ForceDefaultFolder |
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowFoldersOnly;
var ofd = new Autodesk.AutoCAD.Windows.OpenFileDialog("Select Google Drive Path", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "*", "Select Spec Sheet(s)", flags);
var dr = ofd.ShowDialog();
if (dr != System.Windows.Forms.DialogResult.OK)
{
return;
}
var path = ofd.Filename;
dialogs.SetValue(REG_KEY_NAME, path);
var depthPrompt = new PromptIntegerOptions("\nAt what depth? ");
var depthResponse = ed.GetInteger(depthPrompt);
var depth = depthResponse.Value;
dialogs.SetValue(REG_KEY_DEPTH, depth);
GoogleDrive();
}
}
private static readonly double X_OFFSET = 0;
private static readonly double Y_OFFSET = 0;
private static readonly int SHEETS_PER_LINE = 4;
private static readonly double PIXELS_PER_INCH = 72;
private static List<string> Import(string path, double scale)
{
if (File.GetAttributes(path).HasFlag(FileAttributes.Directory))
{
var pdfs = new List<string>();
foreach (var file in Directory.GetFiles(path))
{
pdfs.AddRange(Import(file, scale));
}
return pdfs;
}
else
{
return new List<string> { path };
}
}
[CommandMethod("Ecogy", "ImportSpecSheet", CommandFlags.Modal)]
public void ImportSpecSheet()
{
var doc = Application.DocumentManager.MdiActiveDocument;
if (doc != null)
{
var ed = doc.Editor;
var scalePrompt = new PromptDoubleOptions("\nAt what scale? ");
var scaleResponse = ed.GetDouble(scalePrompt);
if (scaleResponse.Status != PromptStatus.OK)
{
ed.WriteMessage("You must supply a valid scale\n");
return;
}
var noMutt = Application.GetSystemVariable("NOMUTT");
Application.SetSystemVariable("NOMUTT", 1);
var scale = scaleResponse.Value;
var flags = Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles |
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DefaultIsFolder |
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.ForceDefaultFolder |
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple |
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowAnyExtension;
var openFileDialog = new Autodesk.AutoCAD.Windows.OpenFileDialog("Select Spec Sheet(s)", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "*", "Select Spec Sheet(s)", flags);
var result = openFileDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
var pdfs = new List<string>();
var files = openFileDialog.GetFilenames();
foreach (var file in files)
{
pdfs.AddRange(Import(file, scale));
}
var pages = new List<(string, int)>();
foreach (var pdf in pdfs)
{
PdfReader reader = new PdfReader(pdf);
PdfDocument document = new PdfDocument(reader);
var pageCount = document.GetNumberOfPages();
for (var page = 0; page < pageCount; page++)
{
pages.Add((pdf, page + 1));
}
}
var y_offsets = new List<double>();
for (var i = 0; i < pages.Count; i++)
{
var x_offset = 0.0;
for (var j = 0; j < SHEETS_PER_LINE; j++)
{
if ((i * SHEETS_PER_LINE + j) >= pages.Count())
{
break;
}
var (pdf, page) = pages[i * SHEETS_PER_LINE + j];
PdfReader reader = new PdfReader(pdf);
PdfDocument document = new PdfDocument(reader);
Rectangle rectangle = document.GetPage(page).GetPageSize();
if (y_offsets.Count <= j)
{
y_offsets.Add(0.0);
}
else
{
y_offsets[j] += rectangle.GetHeight() / PIXELS_PER_INCH * scale;
}
doc.Editor.Command(
$"-PDFATTACH",
pdf,
page,
new Point2d(
X_OFFSET + x_offset,
Y_OFFSET - y_offsets[j]
),
scale,
0
);
x_offset += rectangle.GetWidth() / PIXELS_PER_INCH * scale;
}
}
}
Application.SetSystemVariable("NOMUTT", noMutt);
}
}
private static ObjectIdCollection GetPolylineEntities(string layer = null)
{
var doc = Application.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
TypedValue[] filter;
if (!String.IsNullOrEmpty(layer))
{
filter = new TypedValue[]{
new TypedValue((int)DxfCode.Operator, "<and"),
new TypedValue((int)DxfCode.Start, "LWPolyline"),
new TypedValue((int)DxfCode.LayerName, layer),
new TypedValue((int)DxfCode.Operator, "and>")
};
}
else
{
filter = new TypedValue[] { new TypedValue((int)DxfCode.Start, "LWPolyline") };
}
var selectionFilter = new SelectionFilter(filter);
var promptStatusResult = ed.SelectAll(selectionFilter);
if (promptStatusResult.Status == PromptStatus.OK)
return new ObjectIdCollection(promptStatusResult.Value.GetObjectIds());
return new ObjectIdCollection();
}
[CommandMethod("Ecogy", "FilletAll", CommandFlags.Modal)]
public void FilletAll()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var radiusPrompt = new PromptDoubleOptions("\nWhat radius? ");
var radiusResponse = ed.GetDouble(radiusPrompt);
var layerPrompt = new PromptKeywordOptions("\nOn what Layer? ");
using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
{
LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
foreach (ObjectId layerId in lt)
{
var layer = tr.GetObject(layerId, OpenMode.ForWrite) as LayerTableRecord;
layerPrompt.Keywords.Add(layer.Name);
}
}
layerPrompt.Keywords.Default = "PV-string";
var lay = ed.GetKeywords(layerPrompt);
if (radiusResponse.Status != PromptStatus.OK)
{
ed.WriteMessage("You must supply a valid radius\n");
return;
}
else if (lay.Status != PromptStatus.OK)
{
ed.WriteMessage("You must select one of the layers\n");
return;
}
var noMutt = Application.GetSystemVariable("NOMUTT");
Application.SetSystemVariable("NOMUTT", 1);
ed.Command("FILLETRAD", radiusResponse.Value);
using (var pm = new ProgressMeter())
{
pm.Start("Filleting Polylines");
var collection = GetPolylineEntities(lay.StringResult);
foreach (ObjectId id in collection)
{
ed.Command("_.FILLET", "_P", id);
pm.MeterProgress();
System.Windows.Forms.Application.DoEvents();
}
pm.Stop();
System.Windows.Forms.Application.DoEvents();
}
Application.SetSystemVariable("NOMUTT", noMutt);
}
}
}