-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMergeForm.cs
More file actions
1168 lines (1025 loc) · 48.9 KB
/
Copy pathMergeForm.cs
File metadata and controls
1168 lines (1025 loc) · 48.9 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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Navisworks.Api;
using Color = System.Drawing.Color;
using NwColor = Autodesk.Navisworks.Api.Color;
namespace AutisAnalytics.NavisworksAtributos
{
public class MergeForm : Form
{
// ── Config phase controls ──────────────────────────────────────
private Panel pnlConfig;
private Label lblOldModel;
private Label lblNewModel;
private CheckBox chkAttributes;
private CheckBox chkSets;
private RadioButton radioAuto;
private RadioButton radioDeep;
private RadioButton radioUltra;
private ComboBox cmbPreferredId;
private Label lblStatus;
private Button btnAnalyze;
private ProgressBar progressBar;
private Label lblPercent;
private Label lblProgressDetail;
// ── Results phase controls ─────────────────────────────────────
private Panel pnlResults;
private Label lblSummaryMatched;
private Label lblSummaryNew;
private Label lblSummaryRemoved;
private Label lblSummaryCandidates;
private Label lblSummaryTransfer;
private TabControl tabResults;
private DataGridView gridMatched;
private DataGridView gridNew;
private DataGridView gridRemoved;
private DataGridView gridCandidates;
private Button btnExecuteMerge;
private Button btnExportCsv;
// ── Data ───────────────────────────────────────────────────────
private readonly string _newNwdPath;
private MergeReport _report;
private bool _colorsApplied;
private List<ModelItem> _oldRoots;
private HashSet<Guid> _oldModelGuids = new HashSet<Guid>();
private HashSet<Guid> _appendedModelGuids = new HashSet<Guid>();
private bool _mergeExecuted;
public MergeForm(string newNwdPath)
{
_newNwdPath = newNwdPath;
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
Montar();
}
// ════════════════════════════════════════════════════════════════
// BUILD UI
// ════════════════════════════════════════════════════════════════
private void Montar()
{
Text = "Autis Analytics";
Size = new Size(950, 700);
MinimumSize = new Size(800, 550);
StartPosition = FormStartPosition.CenterScreen;
BackColor = UITheme.Color.Background;
Font = UITheme.Typography.Body;
// ── Header ─────────────────────────────────────────────────
var pnlHeader = UITheme.CreateHeader("Merge NWD",
"Transfer attributes between model revisions");
Controls.Add(pnlHeader);
// ── Footer ─────────────────────────────────────────────────
var pnlFooter = UITheme.CreateFooter();
lblStatus = new Label
{
Text = "",
Font = UITheme.Typography.BodySmall,
ForeColor = UITheme.Color.TextTertiary,
AutoSize = true,
Location = new Point(UITheme.Spacing.LG, 16)
};
pnlFooter.Controls.Add(lblStatus);
var btnClose = UITheme.CreateSecondaryButton("Close");
btnClose.Width = 100;
btnClose.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
btnClose.Click += (s, e) => Close();
pnlFooter.Controls.Add(btnClose);
btnExportCsv = UITheme.CreateSecondaryButton("Export CSV");
btnExportCsv.Width = 110;
btnExportCsv.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
btnExportCsv.Visible = false;
btnExportCsv.Click += BtnExportCsv_Click;
pnlFooter.Controls.Add(btnExportCsv);
btnExecuteMerge = UITheme.CreateSuccessButton("Execute Merge");
btnExecuteMerge.Width = 130;
btnExecuteMerge.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
btnExecuteMerge.Visible = false;
btnExecuteMerge.Click += BtnExecuteMerge_Click;
pnlFooter.Controls.Add(btnExecuteMerge);
pnlFooter.Resize += (s, e) =>
{
btnClose.Location = new Point(pnlFooter.Width - 116, 8);
btnExportCsv.Location = new Point(pnlFooter.Width - 242, 8);
btnExecuteMerge.Location = new Point(pnlFooter.Width - 388, 8);
};
Controls.Add(pnlFooter);
CancelButton = btnClose;
// ── Body ───────────────────────────────────────────────────
var pnlBody = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(UITheme.Spacing.LG, UITheme.Spacing.MD,
UITheme.Spacing.LG, UITheme.Spacing.MD)
};
Controls.Add(pnlBody);
pnlBody.BringToFront();
MontarConfigPanel(pnlBody);
MontarResultsPanel(pnlBody);
// Start in config phase
pnlConfig.Visible = true;
pnlResults.Visible = false;
}
// ────────────────────────────────────────────────────────────────
// CONFIG PANEL (Phase 1)
// ────────────────────────────────────────────────────────────────
private void MontarConfigPanel(Panel parent)
{
pnlConfig = new Panel { Dock = DockStyle.Fill };
parent.Controls.Add(pnlConfig);
var card = UITheme.CreateCard();
card.Dock = DockStyle.Fill;
card.Padding = new Padding(UITheme.Spacing.XL);
pnlConfig.Controls.Add(card);
int y = UITheme.Spacing.XL;
// ── Models info ────────────────────────────────────────────
var doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
var currentFile = doc?.FileName ?? doc?.Title ?? "(unknown)";
var lblTitleModels = new Label
{
Text = "MODELS",
Font = UITheme.Typography.Label,
ForeColor = UITheme.Color.TextSecondary,
Location = new Point(UITheme.Spacing.XL, y),
AutoSize = true
};
card.Controls.Add(lblTitleModels);
y += 22;
// Current model
var lblOldTitle = new Label
{
Text = "Current Model:",
Font = UITheme.Typography.Label,
ForeColor = UITheme.Color.TextPrimary,
Location = new Point(UITheme.Spacing.XL, y),
AutoSize = true
};
card.Controls.Add(lblOldTitle);
lblOldModel = new Label
{
Text = Path.GetFileName(currentFile),
Font = UITheme.Typography.Body,
ForeColor = UITheme.Color.TextSecondary,
Location = new Point(140, y),
AutoSize = true
};
card.Controls.Add(lblOldModel);
y += 24;
// New model
var lblNewTitle = new Label
{
Text = "New Model:",
Font = UITheme.Typography.Label,
ForeColor = UITheme.Color.TextPrimary,
Location = new Point(UITheme.Spacing.XL, y),
AutoSize = true
};
card.Controls.Add(lblNewTitle);
lblNewModel = new Label
{
Text = Path.GetFileName(_newNwdPath),
Font = UITheme.Typography.Body,
ForeColor = UITheme.Color.Success,
Location = new Point(140, y),
AutoSize = true
};
card.Controls.Add(lblNewModel);
y += 36;
// ── Divider ────────────────────────────────────────────────
var div1 = UITheme.CreateDivider();
div1.Location = new Point(UITheme.Spacing.XL, y);
div1.Width = card.Width - UITheme.Spacing.XL * 2;
div1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
card.Controls.Add(div1);
y += 16;
// ── What to transfer ───────────────────────────────────────
var lblTitleTransfer = new Label
{
Text = "WHAT TO TRANSFER",
Font = UITheme.Typography.Label,
ForeColor = UITheme.Color.TextSecondary,
Location = new Point(UITheme.Spacing.XL, y),
AutoSize = true
};
card.Controls.Add(lblTitleTransfer);
y += 22;
chkAttributes = new CheckBox
{
Text = "Custom Attributes (Autis_Attributes)",
Font = UITheme.Typography.Body,
ForeColor = UITheme.Color.TextPrimary,
Checked = true,
Location = new Point(UITheme.Spacing.XL + 4, y),
AutoSize = true
};
card.Controls.Add(chkAttributes);
y += 26;
chkSets = new CheckBox
{
Text = "Set Associations (Autis_AWP)",
Font = UITheme.Typography.Body,
ForeColor = UITheme.Color.TextPrimary,
Checked = true,
Location = new Point(UITheme.Spacing.XL + 4, y),
AutoSize = true
};
card.Controls.Add(chkSets);
y += 36;
// ── Divider ────────────────────────────────────────────────
var div2 = UITheme.CreateDivider();
div2.Location = new Point(UITheme.Spacing.XL, y);
div2.Width = card.Width - UITheme.Spacing.XL * 2;
div2.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
card.Controls.Add(div2);
y += 16;
// ── Matching depth ─────────────────────────────────────────
var lblTitleDepth = new Label
{
Text = "MATCHING DEPTH",
Font = UITheme.Typography.Label,
ForeColor = UITheme.Color.TextSecondary,
Location = new Point(UITheme.Spacing.XL, y),
AutoSize = true
};
card.Controls.Add(lblTitleDepth);
y += 22;
radioAuto = new RadioButton
{
Text = "Automatic (Levels 1-3, fast)",
Font = UITheme.Typography.Body,
ForeColor = UITheme.Color.TextPrimary,
Checked = true,
Location = new Point(UITheme.Spacing.XL + 4, y),
AutoSize = true
};
card.Controls.Add(radioAuto);
y += 24;
radioDeep = new RadioButton
{
Text = "Deep (Levels 1-4, slower) — coming soon",
Font = UITheme.Typography.Body,
ForeColor = UITheme.Color.TextDisabled,
Enabled = false,
Location = new Point(UITheme.Spacing.XL + 4, y),
AutoSize = true
};
card.Controls.Add(radioDeep);
y += 24;
radioUltra = new RadioButton
{
Text = "Ultra (Levels 1-5 with AI) — coming soon",
Font = UITheme.Typography.Body,
ForeColor = UITheme.Color.TextDisabled,
Enabled = false,
Location = new Point(UITheme.Spacing.XL + 4, y),
AutoSize = true
};
card.Controls.Add(radioUltra);
y += 36;
// ── Divider ────────────────────────────────────────────────
var div3 = UITheme.CreateDivider();
div3.Location = new Point(UITheme.Spacing.XL, y);
div3.Width = card.Width - UITheme.Spacing.XL * 2;
div3.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
card.Controls.Add(div3);
y += 16;
// ── Preferred ID ───────────────────────────────────────────
var lblTitleId = new Label
{
Text = "PREFERRED ID PROPERTY",
Font = UITheme.Typography.Label,
ForeColor = UITheme.Color.TextSecondary,
Location = new Point(UITheme.Spacing.XL, y),
AutoSize = true
};
card.Controls.Add(lblTitleId);
y += 22;
cmbPreferredId = new ComboBox
{
Font = UITheme.Typography.Body,
Location = new Point(UITheme.Spacing.XL + 4, y),
Width = 250,
DropDownStyle = ComboBoxStyle.DropDownList
};
cmbPreferredId.Items.Add("(auto-detect)");
cmbPreferredId.Items.AddRange(ElementFingerprint.IdFieldNames.Cast<object>().ToArray());
cmbPreferredId.SelectedIndex = 0;
card.Controls.Add(cmbPreferredId);
var lblIdHint = new Label
{
Text = "Detected after analysis starts",
Font = UITheme.Typography.BodySmall,
ForeColor = UITheme.Color.TextTertiary,
Location = new Point(280, y + 3),
AutoSize = true
};
card.Controls.Add(lblIdHint);
y += 40;
// ── Analyze button ─────────────────────────────────────────
btnAnalyze = UITheme.CreatePrimaryButton("Analyze Models");
btnAnalyze.Width = 160;
btnAnalyze.Location = new Point(UITheme.Spacing.XL, y);
btnAnalyze.Click += BtnAnalyze_Click;
card.Controls.Add(btnAnalyze);
y += 50;
// ── Progress section (hidden until analysis starts) ──────────
// Percentage — big and bold, first line
lblPercent = new Label
{
Text = "0%",
Font = UITheme.Typography.H1,
ForeColor = UITheme.Color.Primary,
Location = new Point(UITheme.Spacing.XL, y),
AutoSize = true,
Visible = false
};
card.Controls.Add(lblPercent);
y += 34;
// Progress bar — full width below the percentage
progressBar = new ProgressBar
{
Location = new Point(UITheme.Spacing.XL, y),
Height = 18,
Minimum = 0,
Maximum = 100,
Value = 0,
Style = ProgressBarStyle.Continuous,
Visible = false,
Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right
};
card.Controls.Add(progressBar);
// Set width after adding to card so anchor works from card width
progressBar.Width = card.Width > 0 ? card.Width - UITheme.Spacing.XL * 2 : 600;
y += 28;
// Detail text below bar
lblProgressDetail = new Label
{
Text = "",
Font = UITheme.Typography.Body,
ForeColor = UITheme.Color.TextSecondary,
Location = new Point(UITheme.Spacing.XL, y),
AutoSize = true,
Visible = false
};
card.Controls.Add(lblProgressDetail);
}
// ────────────────────────────────────────────────────────────────
// RESULTS PANEL (Phase 2)
// ────────────────────────────────────────────────────────────────
private void MontarResultsPanel(Panel parent)
{
pnlResults = new Panel { Dock = DockStyle.Fill };
parent.Controls.Add(pnlResults);
// ── Summary bar ────────────────────────────────────────────
var pnlSummary = new Panel
{
Dock = DockStyle.Top,
Height = 80,
BackColor = UITheme.Color.Surface,
Padding = new Padding(UITheme.Spacing.LG)
};
pnlSummary.Paint += (s, e) =>
{
using (var pen = new Pen(UITheme.Color.BorderLight))
e.Graphics.DrawLine(pen, 0, pnlSummary.Height - 1,
pnlSummary.Width, pnlSummary.Height - 1);
};
// Colored badges
int bx = UITheme.Spacing.LG;
lblSummaryMatched = CriarBadgeSummary(pnlSummary, ref bx,
Color.FromArgb(66, 133, 244), "0 Matched");
lblSummaryNew = CriarBadgeSummary(pnlSummary, ref bx,
Color.FromArgb(46, 184, 70), "0 New");
lblSummaryRemoved = CriarBadgeSummary(pnlSummary, ref bx,
Color.FromArgb(211, 47, 47), "0 Removed");
lblSummaryCandidates = CriarBadgeSummary(pnlSummary, ref bx,
Color.FromArgb(245, 166, 35), "0 Candidates");
lblSummaryTransfer = new Label
{
Font = UITheme.Typography.BodySmall,
ForeColor = UITheme.Color.TextSecondary,
Location = new Point(UITheme.Spacing.LG, 48),
AutoSize = true,
Text = ""
};
pnlSummary.Controls.Add(lblSummaryTransfer);
pnlResults.Controls.Add(pnlSummary);
// ── Tabs with grids ────────────────────────────────────────
tabResults = new TabControl
{
Dock = DockStyle.Fill,
Font = UITheme.Typography.Body
};
// Tab Matched
var tabMatched = new TabPage("Matched");
gridMatched = CriarGridResultado();
gridMatched.Columns.Add("OldName", "Old Element");
gridMatched.Columns.Add("NewName", "New Element");
gridMatched.Columns.Add("Level", "Level");
gridMatched.Columns.Add("Score", "Score");
gridMatched.Columns.Add("Attrs", "Attributes");
gridMatched.Columns.Add("Detail", "Detail");
gridMatched.Columns["Level"].Width = 50;
gridMatched.Columns["Score"].Width = 55;
gridMatched.Columns["Attrs"].Width = 70;
tabMatched.Controls.Add(gridMatched);
tabResults.TabPages.Add(tabMatched);
// Tab New
var tabNew = new TabPage("New");
gridNew = CriarGridResultado();
gridNew.Columns.Add("Name", "Element");
gridNew.Columns.Add("Type", "Type");
gridNew.Columns.Add("Hierarchy", "Hierarchy");
tabNew.Controls.Add(gridNew);
tabResults.TabPages.Add(tabNew);
// Tab Removed
var tabRemoved = new TabPage("Removed");
gridRemoved = CriarGridResultado();
gridRemoved.Columns.Add("Name", "Element");
gridRemoved.Columns.Add("Type", "Type");
gridRemoved.Columns.Add("Attrs", "Attributes");
gridRemoved.Columns.Add("Sets", "Sets");
gridRemoved.Columns["Attrs"].Width = 70;
tabRemoved.Controls.Add(gridRemoved);
tabResults.TabPages.Add(tabRemoved);
// Tab Candidates
var tabCandidates = new TabPage("Candidates");
gridCandidates = CriarGridResultado();
var colAccept = new DataGridViewCheckBoxColumn
{
Name = "Accept",
HeaderText = "Accept",
Width = 55
};
gridCandidates.Columns.Add(colAccept);
gridCandidates.Columns.Add("OldName", "Old Element");
gridCandidates.Columns.Add("NewName", "New Element");
gridCandidates.Columns.Add("Score", "Score");
gridCandidates.Columns.Add("Detail", "Justification");
gridCandidates.Columns["Score"].Width = 55;
tabCandidates.Controls.Add(gridCandidates);
tabResults.TabPages.Add(tabCandidates);
pnlResults.Controls.Add(tabResults);
tabResults.BringToFront();
}
private Label CriarBadgeSummary(Panel parent, ref int x, Color cor, string text)
{
// Color dot
var dot = new Panel
{
Size = new Size(12, 12),
Location = new Point(x, 16),
BackColor = cor
};
dot.Paint += (s, e) =>
{
using (var path = CriarRoundedRect(new Rectangle(0, 0, 12, 12), 3))
using (var brush = new SolidBrush(cor))
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.FillPath(brush, path);
}
};
dot.BackColor = Color.Transparent;
parent.Controls.Add(dot);
x += 16;
var lbl = new Label
{
Text = text,
Font = UITheme.Typography.H3,
ForeColor = UITheme.Color.TextPrimary,
Location = new Point(x, 12),
AutoSize = true
};
parent.Controls.Add(lbl);
x += TextRenderer.MeasureText(text, UITheme.Typography.H3).Width + 20;
return lbl;
}
private DataGridView CriarGridResultado()
{
var grid = new DataGridView
{
Dock = DockStyle.Fill,
ReadOnly = false,
AllowUserToAddRows = false,
AllowUserToDeleteRows = false,
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
MultiSelect = false,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
RowHeadersVisible = false
};
UITheme.StyleDataGridView(grid);
return grid;
}
// ════════════════════════════════════════════════════════════════
// ANALYZE (Phase 1 → Phase 2 transition)
// ════════════════════════════════════════════════════════════════
private void BtnAnalyze_Click(object sender, EventArgs e)
{
btnAnalyze.Enabled = false;
btnAnalyze.Text = "Analyzing...";
ShowProgress(true);
// Progress allocation:
// 0-5% Appending model
// 5-40% Extracting old fingerprints
// 40-75% Extracting new fingerprints
// 75-80% Detecting preferred ID
// 80-95% Running matching
// 95-100% Applying colors
try
{
var doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
if (doc == null) return;
// ── Step 1: Capture old roots ──────────────────────────
UpdateProgress(0, "Preparing...");
_mergeExecuted = false;
_oldRoots = new List<ModelItem>();
_oldModelGuids = new HashSet<Guid>();
_appendedModelGuids = new HashSet<Guid>();
foreach (Model model in doc.Models)
_oldModelGuids.Add(model.Guid);
foreach (var root in doc.Models.RootItems)
_oldRoots.Add(root);
// ── Step 2: Append new NWD ─────────────────────────────
UpdateProgress(2, "Appending new model...");
if (!doc.TryAppendFile(_newNwdPath))
{
ShowProgress(false);
MessageBox.Show("Failed to append the new NWD file.\n\n" +
"Make sure the file exists and is a valid NWD.",
"AWP Autis", MessageBoxButtons.OK, MessageBoxIcon.Warning);
ResetAnalyzeButton();
return;
}
UpdateProgress(5, "Model appended.");
// Identify new model roots
var newRoots = new List<ModelItem>();
foreach (Model model in doc.Models)
{
if (_oldModelGuids.Contains(model.Guid))
continue;
_appendedModelGuids.Add(model.Guid);
if (model.RootItem != null)
newRoots.Add(model.RootItem);
}
if (newRoots.Count == 0)
{
CleanupTemporaryAnalysisState(doc);
ShowProgress(false);
MessageBox.Show("Could not identify the appended model.",
"AWP Autis", MessageBoxButtons.OK, MessageBoxIcon.Warning);
ResetAnalyzeButton();
return;
}
// ── Step 3: Extract OLD fingerprints (5% → 40%) ────────
var oldFPs = MergeService.ExtractFingerprints(
_oldRoots,
Path.GetFileName(doc.FileName ?? "Current Model"),
readAutisData: true,
progress: (text, pct) =>
UpdateProgress(5 + (int)(pct * 0.35), text));
// ── Step 4: Extract NEW fingerprints (40% → 75%) ───────
var newFPs = MergeService.ExtractFingerprints(
newRoots,
Path.GetFileName(_newNwdPath),
readAutisData: false,
progress: (text, pct) =>
UpdateProgress(40 + (int)(pct * 0.35), text));
// ── Step 5: Detect preferred ID (75% → 80%) ───────────
UpdateProgress(75, "Detecting best ID property...");
string preferredId = null;
if (cmbPreferredId.SelectedIndex == 0)
{
preferredId = MergeService.DetectPreferredId(oldFPs);
UpdateProgress(80, $"Auto-detected ID: {preferredId ?? "none"}");
}
else
{
preferredId = cmbPreferredId.SelectedItem?.ToString();
UpdateProgress(80, $"Using ID: {preferredId}");
}
// ── Step 6: Run matching (80% → 95%) ──────────────────
var config = new MergeConfig
{
NewNwdPath = _newNwdPath,
TransferAttributes = chkAttributes.Checked,
TransferSets = chkSets.Checked,
Depth = MergeDepth.Automatic,
PreferredIdProperty = preferredId
};
_report = MergeService.RunMatching(oldFPs, newFPs, config,
(text, pct) => UpdateProgress(80 + (int)(pct * 0.15), text));
// ── Step 7: Apply colors (95% → 100%) ─────────────────
UpdateProgress(95, "Applying color coding...");
ApplyColorCoding(doc);
UpdateProgress(100, "Complete!");
// Transition to results
ShowProgress(false);
PopulateResults();
pnlConfig.Visible = false;
pnlResults.Visible = true;
pnlResults.BringToFront();
btnExecuteMerge.Visible = true;
btnExportCsv.Visible = true;
UpdateStatus($"Analysis complete in {_report.AnalysisDuration.TotalSeconds:F1}s");
}
catch (Exception ex)
{
CleanupTemporaryAnalysisState(Autodesk.Navisworks.Api.Application.ActiveDocument);
ShowProgress(false);
MessageBox.Show($"Error during analysis:\n\n{ex.Message}",
"AWP Autis", MessageBoxButtons.OK, MessageBoxIcon.Error);
ResetAnalyzeButton();
}
}
private void ResetAnalyzeButton()
{
btnAnalyze.Enabled = true;
btnAnalyze.Text = "Analyze Models";
}
private void UpdateStatus(string text)
{
lblStatus.Text = text;
if (lblProgressDetail.Visible)
lblProgressDetail.Text = text;
System.Windows.Forms.Application.DoEvents();
}
private void UpdateProgress(int percent, string text = null)
{
if (percent < 0) percent = 0;
if (percent > 100) percent = 100;
progressBar.Value = percent;
lblPercent.Text = $"{percent}%";
if (text != null)
{
lblProgressDetail.Text = text;
lblStatus.Text = text;
}
System.Windows.Forms.Application.DoEvents();
}
private void ShowProgress(bool visible)
{
progressBar.Visible = visible;
lblPercent.Visible = visible;
lblProgressDetail.Visible = visible;
if (visible)
progressBar.Value = 0;
}
// ════════════════════════════════════════════════════════════════
// POPULATE RESULTS
// ════════════════════════════════════════════════════════════════
private void PopulateResults()
{
if (_report == null) return;
// Summary
lblSummaryMatched.Text = $"{_report.Matched.Count} Matched " +
$"(L1:{_report.MatchedLevel1} L2:{_report.MatchedLevel2} L3:{_report.MatchedLevel3})";
lblSummaryNew.Text = $"{_report.NewElements.Count} New";
lblSummaryRemoved.Text = $"{_report.RemovedElements.Count} Removed";
lblSummaryCandidates.Text = $"{_report.Candidates.Count} Candidates";
lblSummaryTransfer.Text =
$"{_report.TotalAttributesToTransfer} attributes to transfer | " +
$"{_report.TotalSetsToReconstruct} sets to reconstruct | " +
$"Analysis: {_report.AnalysisDuration.TotalSeconds:F1}s";
// Grid: Matched
gridMatched.Rows.Clear();
foreach (var (old, newFP, result) in _report.Matched)
{
gridMatched.Rows.Add(
old.DisplayName,
newFP.DisplayName,
result.Level,
$"{result.Score:F0}%",
old.AutisAttributes?.Count ?? 0,
result.Justification);
}
// Grid: New
gridNew.Rows.Clear();
foreach (var fp in _report.NewElements)
{
gridNew.Rows.Add(
fp.DisplayName,
fp.TypeName ?? fp.ClassName,
fp.HierarchyPath);
}
// Grid: Removed
gridRemoved.Rows.Clear();
foreach (var fp in _report.RemovedElements)
{
gridRemoved.Rows.Add(
fp.DisplayName,
fp.TypeName ?? fp.ClassName,
fp.AutisAttributes?.Count ?? 0,
fp.AutisSets != null ? string.Join(", ", fp.AutisSets) : "");
}
// Grid: Candidates
gridCandidates.Rows.Clear();
foreach (var (old, newFP, result) in _report.Candidates)
{
gridCandidates.Rows.Add(
false, // Accept checkbox
old.DisplayName,
newFP.DisplayName,
$"{result.Score:F0}%",
result.Justification);
}
// Color code grid rows
ColorCodeGrid(gridMatched, Color.FromArgb(232, 240, 254)); // light blue
ColorCodeGrid(gridNew, Color.FromArgb(232, 250, 232)); // light green
ColorCodeGrid(gridRemoved, Color.FromArgb(254, 232, 232)); // light red
ColorCodeGrid(gridCandidates, Color.FromArgb(255, 243, 224)); // light orange
}
private void ColorCodeGrid(DataGridView grid, Color rowColor)
{
foreach (DataGridViewRow row in grid.Rows)
row.DefaultCellStyle.BackColor = rowColor;
}
// ════════════════════════════════════════════════════════════════
// COLOR CODING IN NAVISWORKS
// ════════════════════════════════════════════════════════════════
private void ApplyColorCoding(Document doc)
{
try
{
using (var trans = doc.BeginTransaction("Autis_Merge_Colors"))
{
// BLUE — matched new elements
var matchedItems = new ModelItemCollection();
foreach (var (_, newFP, _) in _report.Matched)
matchedItems.Add(newFP.Item);
if (matchedItems.Count > 0)
{
doc.Models.OverridePermanentColor(
(IEnumerable<ModelItem>)matchedItems,
new NwColor(0.3, 0.5, 0.85));
}
// GREEN — new elements
var newItems = new ModelItemCollection();
foreach (var fp in _report.NewElements)
newItems.Add(fp.Item);
if (newItems.Count > 0)
{
doc.Models.OverridePermanentColor(
(IEnumerable<ModelItem>)newItems,
new NwColor(0.2, 0.75, 0.3));
}
// RED — removed elements (old model items)
var removedItems = new ModelItemCollection();
foreach (var fp in _report.RemovedElements)
removedItems.Add(fp.Item);
if (removedItems.Count > 0)
{
doc.Models.OverridePermanentColor(
(IEnumerable<ModelItem>)removedItems,
new NwColor(0.85, 0.2, 0.2));
}
// ORANGE — candidates
var candidateItems = new ModelItemCollection();
foreach (var (_, newFP, _) in _report.Candidates)
candidateItems.Add(newFP.Item);
if (candidateItems.Count > 0)
{
doc.Models.OverridePermanentColor(
(IEnumerable<ModelItem>)candidateItems,
new NwColor(0.95, 0.65, 0.15));
}
trans.Commit();
_colorsApplied = true;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Autis] Merge color error: {ex.Message}");
}
}
private void ResetColors()
{
if (!_colorsApplied) return;
try
{
var doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
if (doc == null) return;
var colorizedItems = CollectColorizedItems();
if (colorizedItems.Count > 0)
doc.Models.ResetPermanentMaterials((IEnumerable<ModelItem>)colorizedItems);
_colorsApplied = false;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[Autis] Merge reset color error: {ex.Message}");
}
}
// ════════════════════════════════════════════════════════════════
// EXECUTE MERGE
// ════════════════════════════════════════════════════════════════
private void BtnExecuteMerge_Click(object sender, EventArgs e)
{
if (_report == null) return;
if (HasDuplicateAcceptedCandidates())
{
MessageBox.Show(
"There are accepted candidates pointing to the same new element.\n\n" +
"Keep only one accepted candidate for each target element before executing the merge.",
"AWP Autis",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
return;
}
// Collect accepted candidates
int acceptedCount = 0;
for (int i = 0; i < gridCandidates.Rows.Count; i++)
{
var accepted = gridCandidates.Rows[i].Cells["Accept"].Value as bool? ?? false;
if (accepted && i < _report.Candidates.Count)
{
_report.Candidates[i].Result.Status = MatchStatus.Matched;
acceptedCount++;
}
}
int totalToTransfer = _report.TotalAttributesToTransfer +
_report.Candidates.Where(c => c.Result.Status == MatchStatus.Matched)
.Sum(c => c.Old.AutisAttributes?.Count ?? 0);
// Confirmation
var confirmResult = MessageBox.Show(
$"Transfer attributes to {_report.Matched.Count + acceptedCount} elements?\n\n" +
$" {totalToTransfer} attributes\n" +
$" {_report.TotalSetsToReconstruct} sets\n\n" +
"This action cannot be undone.",
"AWP Autis — Confirm Merge",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (confirmResult != DialogResult.Yes) return;
btnExecuteMerge.Enabled = false;
btnExecuteMerge.Text = "Merging...";
_mergeExecuted = true;
try
{
var config = new MergeConfig
{
TransferAttributes = chkAttributes.Checked,
TransferSets = chkSets.Checked
};
var result = MergeService.ExecuteMerge(_report, config,
(s, pct) => UpdateStatus(s));
ToastNotification.Show(
result.errors == 0
? $"Merge complete! {result.transferred} elements transferred."
: $"Merge done with {result.errors} errors. {result.transferred} transferred.",
result.errors == 0
? ToastNotification.ToastType.Success
: ToastNotification.ToastType.Warning);
MessageBox.Show(result.message, "AWP Autis — Merge Result",
MessageBoxButtons.OK,
result.errors > 0 ? MessageBoxIcon.Warning : MessageBoxIcon.Information);
}
catch (Exception ex)
{
_mergeExecuted = false;
MessageBox.Show($"Error during merge:\n\n{ex.Message}",
"AWP Autis", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
btnExecuteMerge.Enabled = true;
btnExecuteMerge.Text = "Execute Merge";