-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAutisAtributosPlugin.cs
More file actions
504 lines (437 loc) · 20.6 KB
/
Copy pathAutisAtributosPlugin.cs
File metadata and controls
504 lines (437 loc) · 20.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.Plugins;
using Autodesk.Windows;
namespace AutisAnalytics.NavisworksAtributos
{
/// <summary>
/// EventWatcherPlugin que cria a aba "Autis" na Ribbon do Navisworks 2026.
/// Usa a API programática Autodesk.Windows (AdWindows.dll) — mesma abordagem
/// do plugin waabe_navi_mcp.
/// </summary>
[Plugin("AutisAnalytics.Atributos", "AUAN",
DisplayName = "Autis Analytics - Properties",
ToolTip = "Custom attribute tools")]
public class AutisAtributosPlugin : EventWatcherPlugin
{
private bool _ribbonCreated;
public override void OnLoaded()
{
// Timer para esperar a Ribbon estar disponível
var timer = new System.Windows.Forms.Timer { Interval = 500 };
timer.Tick += (s, e) =>
{
if (ComponentManager.Ribbon != null)
{
timer.Stop();
timer.Dispose();
CriarRibbon();
}
};
timer.Start();
}
public override void OnUnloading()
{
}
private void CriarRibbon()
{
if (_ribbonCreated) return;
_ribbonCreated = true;
var ribbon = ComponentManager.Ribbon;
if (ribbon == null) return;
// ── Criar aba "AWP Autis" ────────────────────────────────────────
var tab = new RibbonTab
{
Title = "AWP Autis",
Id = "ID_AUTIS_TAB",
Name = "AWP Autis"
};
// ── Painel "Properties" ─────────────────────────────────────────
var panelSource = new RibbonPanelSource
{
Title = "Properties",
Name = "Properties"
};
// Botão Ler
var btnLer = new RibbonButton
{
Text = "Read\nProperties",
Id = "ID_BTN_LER",
Size = RibbonItemSize.Large,
ShowText = true,
ShowImage = true,
Orientation = System.Windows.Controls.Orientation.Vertical,
LargeImage = CriarIcone("L", System.Drawing.Color.FromArgb(0, 120, 215)),
Image = CriarIcone16("L", System.Drawing.Color.FromArgb(0, 120, 215))
};
btnLer.CommandHandler = new AutisCommandHandler();
// Botão Gravar
var btnGravar = new RibbonButton
{
Text = "Write\nProperties",
Id = "ID_BTN_GRAVAR",
Size = RibbonItemSize.Large,
ShowText = true,
ShowImage = true,
Orientation = System.Windows.Controls.Orientation.Vertical,
LargeImage = CriarIcone("G", System.Drawing.Color.FromArgb(40, 167, 69)),
Image = CriarIcone16("G", System.Drawing.Color.FromArgb(40, 167, 69))
};
btnGravar.CommandHandler = new AutisCommandHandler();
// Botão Selection Inspector
var btnInspector = new RibbonButton
{
Text = "Selection\nInspector",
Id = "ID_BTN_INSPECTOR",
Size = RibbonItemSize.Large,
ShowText = true,
ShowImage = true,
Orientation = System.Windows.Controls.Orientation.Vertical,
LargeImage = CriarIcone("S", System.Drawing.Color.FromArgb(108, 92, 168)),
Image = CriarIcone16("S", System.Drawing.Color.FromArgb(108, 92, 168))
};
btnInspector.CommandHandler = new AutisCommandHandler();
panelSource.Items.Add(btnLer);
panelSource.Items.Add(btnGravar);
panelSource.Items.Add(btnInspector);
var panel = new RibbonPanel { Source = panelSource };
tab.Panels.Add(panel);
// ── Painel "Visualization" ────────────────────────────────────
var panelVisSource = new RibbonPanelSource
{
Title = "Visualization",
Name = "Visualization"
};
var btnColorizer = new RibbonButton
{
Text = "Colorizer",
Id = "ID_BTN_COLORIZER",
Size = RibbonItemSize.Large,
ShowText = true,
ShowImage = true,
Orientation = System.Windows.Controls.Orientation.Vertical,
LargeImage = CriarIcone("C", System.Drawing.Color.FromArgb(220, 53, 69)),
Image = CriarIcone16("C", System.Drawing.Color.FromArgb(220, 53, 69))
};
btnColorizer.CommandHandler = new AutisCommandHandler();
panelVisSource.Items.Add(btnColorizer);
// Botão Merge NWD
var btnMerge = new RibbonButton
{
Text = "Merge\nNWD",
Id = "ID_BTN_MERGE",
Size = RibbonItemSize.Large,
ShowText = true,
ShowImage = true,
Orientation = System.Windows.Controls.Orientation.Vertical,
LargeImage = CriarIcone("M", System.Drawing.Color.FromArgb(255, 152, 0)),
Image = CriarIcone16("M", System.Drawing.Color.FromArgb(255, 152, 0))
};
btnMerge.CommandHandler = new AutisCommandHandler();
panelVisSource.Items.Add(btnMerge);
var panelVis = new RibbonPanel { Source = panelVisSource };
tab.Panels.Add(panelVis);
ribbon.Tabs.Add(tab);
tab.IsActive = false;
}
// ── Ícones gerados programaticamente (32x32 e 16x16) ────────
private static BitmapImage CriarIcone(string letra, System.Drawing.Color cor)
{
return GerarIconeBitmap(32, letra, cor);
}
private static BitmapImage CriarIcone16(string letra, System.Drawing.Color cor)
{
return GerarIconeBitmap(16, letra, cor);
}
private static BitmapImage GerarIconeBitmap(int size, string letra, System.Drawing.Color cor)
{
using (var bmp = new Bitmap(size, size))
using (var g = System.Drawing.Graphics.FromImage(bmp))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
// Fundo arredondado
using (var brush = new SolidBrush(cor))
{
int r = size / 5;
var rect = new Rectangle(0, 0, size - 1, size - 1);
using (var path = new GraphicsPath())
{
int d = r * 2;
path.AddArc(rect.X, rect.Y, d, d, 180, 90);
path.AddArc(rect.Right - d, rect.Y, d, d, 270, 90);
path.AddArc(rect.Right - d, rect.Bottom - d, d, d, 0, 90);
path.AddArc(rect.X, rect.Bottom - d, d, d, 90, 90);
path.CloseFigure();
g.FillPath(brush, path);
}
}
// Letra centralizada
float fontSize = size * 0.55f;
using (var font = new Font("Segoe UI", fontSize, FontStyle.Bold))
using (var sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
{
g.DrawString(letra, font, Brushes.White,
new RectangleF(0, 0, size, size), sf);
}
// Converter para BitmapImage (WPF)
using (var ms = new MemoryStream())
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
var bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = ms;
bi.EndInit();
bi.Freeze();
return bi;
}
}
}
}
/// <summary>
/// Handler para os botões da Ribbon. Recebe o clique e despacha
/// pela propriedade Id do RibbonButton.
/// </summary>
internal class AutisCommandHandler : System.Windows.Input.ICommand
{
public event EventHandler CanExecuteChanged { add { } remove { } }
/// <summary>
/// Disparado após gravar atributos com sucesso. Usado pelo Selection Inspector (Integrado).
/// </summary>
internal static event EventHandler AtributosGravados;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
var btn = parameter as RibbonCommandItem;
if (btn == null) return;
switch (btn.Id)
{
case "ID_BTN_LER":
ExecutarLeitura();
break;
case "ID_BTN_GRAVAR":
ExecutarGravacao();
break;
case "ID_BTN_COLORIZER":
ExecutarColorizer();
break;
case "ID_BTN_INSPECTOR":
ExecutarInspector();
break;
case "ID_BTN_MERGE":
ExecutarMerge();
break;
}
}
// ─────────────────────────────────────────────────────────────────
// Ler Atributos
// ─────────────────────────────────────────────────────────────────
private void ExecutarLeitura()
{
var doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
if (doc == null) return;
var selecao = doc.CurrentSelection.SelectedItems;
if (selecao.Count == 0)
{
MessageBox.Show("Select at least one element in the model.",
"Autis Analytics", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
var primeiroItem = selecao[0];
var propriedades = AtributoService.LerPropriedades(primeiroItem);
using (var form = new LerAtributosForm(
primeiroItem.DisplayName, selecao.Count, propriedades))
{
form.ShowDialog();
}
}
// ─────────────────────────────────────────────────────────────────
// Selection Inspector
// ─────────────────────────────────────────────────────────────────
private void ExecutarInspector()
{
var doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
if (doc == null) return;
var selecao = doc.CurrentSelection.SelectedItems;
if (selecao.Count == 0)
{
MessageBox.Show("Select at least one element in the model.",
"AWP Autis", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
using (var form = new SelectionInspectorForm(selecao))
{
form.ShowDialog();
}
}
// ─────────────────────────────────────────────────────────────────
// Colorizer
// ─────────────────────────────────────────────────────────────────
private void ExecutarColorizer()
{
var doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
if (doc == null || doc.IsClear)
{
MessageBox.Show("No document open.",
"Autis Analytics", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
using (var form = new ColorizerForm())
{
form.ShowDialog();
}
}
// ─────────────────────────────────────────────────────────────────
// Merge NWD
// ─────────────────────────────────────────────────────────────────
private void ExecutarMerge()
{
var doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
if (doc == null || doc.IsClear)
{
MessageBox.Show("No document open.",
"AWP Autis", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
using (var ofd = new OpenFileDialog())
{
ofd.Title = "Select the NEW revised NWD";
ofd.Filter = "NWD Files (*.nwd)|*.nwd|All Files (*.*)|*.*";
if (ofd.ShowDialog() != DialogResult.OK) return;
using (var form = new MergeForm(ofd.FileName))
{
form.ShowDialog();
}
}
}
// ─────────────────────────────────────────────────────────────────
// Gravar Atributos
// ─────────────────────────────────────────────────────────────────
private void ExecutarGravacao()
{
var doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
if (doc == null) return;
var selecao = doc.CurrentSelection.SelectedItems;
if (selecao.Count == 0)
{
MessageBox.Show("Select at least one element in the model.",
"AWP Autis", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
var setsPorItem = MapearTodosSetsPorItem(doc, selecao);
var setsDetectados = CriarResumoSets(setsPorItem);
var setsSalvos = AtributoService.LerSetsSalvos(
selecao[0],
setsDetectados.Select(setInfo => setInfo.Nome),
AutisSchema.CategoriaPrincipal);
string nomeSetDetectado = setsPorItem.TryGetValue(selecao[0], out var setsPrimeiroItem) &&
setsPrimeiroItem.Count > 0
? setsPrimeiroItem[0].Nome
: null;
using (var form = new GravarAtributosForm(
selecao.Count,
setsDetectados,
nomeSetDetectado,
setsSalvos))
{
if (form.ShowDialog() != DialogResult.OK) return;
var colecao = new ModelItemCollection();
foreach (var item in selecao)
colecao.Add(item);
var setsSelecionadosPorItem = FiltrarSetsSelecionados(setsPorItem, form.NomesSetsSelecionados);
var resultado = form.SolicitarExclusao
? AtributoService.ExcluirAtributos(colecao, form.NomeCategoria)
: AtributoService.GravarAtributos(colecao, form.Atributos, form.NomeCategoria, setsSelecionadosPorItem);
MessageBox.Show(resultado.mensagem,
form.SolicitarExclusao ? "AWP Autis - Delete" : "AWP Autis - Save",
MessageBoxButtons.OK,
resultado.erros > 0 ? MessageBoxIcon.Warning : MessageBoxIcon.Information);
if (resultado.erros == 0)
AtributosGravados?.Invoke(null, EventArgs.Empty);
}
}
// ─────────────────────────────────────────────────────────────────
// Mapear todos os Sets que contêm cada item da seleção
// ─────────────────────────────────────────────────────────────────
private Dictionary<ModelItem, List<SetAssignment>> MapearTodosSetsPorItem(
Document doc, IEnumerable<ModelItem> selecao)
{
var mapa = new Dictionary<ModelItem, List<SetAssignment>>();
try
{
foreach (var item in selecao)
mapa[item] = new List<SetAssignment>();
foreach (var setInfo in SelectionSetCache.Collect(doc))
{
foreach (ModelItem itemDoSet in setInfo.Itens)
{
if (mapa.TryGetValue(itemDoSet, out var setsDoItem))
setsDoItem.Add(new SetAssignment(setInfo.NomeGravacao));
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(
$"[Autis] Erro ao mapear sets da selecao: {ex.Message}");
}
return mapa;
}
private List<GravarAtributosForm.SetResumo> CriarResumoSets(
Dictionary<ModelItem, List<SetAssignment>> setsPorItem)
{
var itensPorNome = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
foreach (var setsDoItem in setsPorItem.Values)
{
foreach (var nome in (setsDoItem ?? new List<SetAssignment>())
.Where(setInfo => !string.IsNullOrWhiteSpace(setInfo?.Nome))
.Select(setInfo => setInfo.Nome.Trim())
.Distinct(StringComparer.OrdinalIgnoreCase))
{
if (itensPorNome.TryGetValue(nome, out var quantidade))
itensPorNome[nome] = quantidade + 1;
else
itensPorNome[nome] = 1;
}
}
return itensPorNome
.OrderBy(kvp => kvp.Key)
.Select(kvp => new GravarAtributosForm.SetResumo(kvp.Key, kvp.Value))
.ToList();
}
private Dictionary<ModelItem, List<SetAssignment>> FiltrarSetsSelecionados(
Dictionary<ModelItem, List<SetAssignment>> setsPorItem,
IEnumerable<string> nomesSelecionados)
{
if (setsPorItem == null)
return null;
var nomes = new HashSet<string>(
(nomesSelecionados ?? Enumerable.Empty<string>())
.Where(nome => !string.IsNullOrWhiteSpace(nome))
.Select(nome => nome.Trim()),
StringComparer.OrdinalIgnoreCase);
var resultado = new Dictionary<ModelItem, List<SetAssignment>>();
foreach (var kvp in setsPorItem)
{
resultado[kvp.Key] = (kvp.Value ?? new List<SetAssignment>())
.Where(setInfo => !string.IsNullOrWhiteSpace(setInfo?.Nome) && nomes.Contains(setInfo.Nome.Trim()))
.GroupBy(setInfo => setInfo.Nome.Trim(), StringComparer.OrdinalIgnoreCase)
.Select(g => g.First())
.ToList();
}
return resultado;
}
}
}