-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelectionSetRenameService.cs
More file actions
106 lines (95 loc) · 3.49 KB
/
Copy pathSelectionSetRenameService.cs
File metadata and controls
106 lines (95 loc) · 3.49 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
using System;
using System.Collections.Generic;
using Autodesk.Navisworks.Api;
namespace AutisAnalytics.NavisworksAtributos
{
internal sealed class SetUppercaseAnalysis
{
public int TotalSets { get; internal set; }
public int SelectionSets { get; internal set; }
public int SearchSets { get; internal set; }
public int OtherSets { get; internal set; }
public int AlreadyUppercase { get; internal set; }
public int ToRename => TotalSets - AlreadyUppercase;
}
internal sealed class SetUppercaseResult
{
public int Renamed { get; internal set; }
public int AlreadyUppercase { get; internal set; }
public int Errors { get; internal set; }
}
/// <summary>
/// Renomeia Selection Sets e Search Sets existentes sem recriar os itens,
/// preservando pastas, pesquisas e selecoes salvas.
/// </summary>
internal static class SelectionSetRenameService
{
public static SetUppercaseAnalysis Analyze(Document document)
{
var analysis = new SetUppercaseAnalysis();
foreach (var set in CollectSets(document))
{
analysis.TotalSets++;
if (set.HasExplicitModelItems)
analysis.SelectionSets++;
else if (set.HasSearch)
analysis.SearchSets++;
else
analysis.OtherSets++;
var currentName = set.DisplayName ?? "";
if (string.Equals(
currentName,
currentName.ToUpperInvariant(),
StringComparison.Ordinal))
{
analysis.AlreadyUppercase++;
}
}
return analysis;
}
public static SetUppercaseResult RenameAll(Document document)
{
if (document == null) throw new ArgumentNullException(nameof(document));
var result = new SetUppercaseResult();
foreach (var set in CollectSets(document))
{
var currentName = set.DisplayName ?? "";
var uppercaseName = currentName.ToUpperInvariant();
if (string.Equals(currentName, uppercaseName, StringComparison.Ordinal))
{
result.AlreadyUppercase++;
continue;
}
try
{
document.SelectionSets.EditDisplayName(set, uppercaseName);
result.Renamed++;
}
catch (Exception ex)
{
result.Errors++;
System.Diagnostics.Debug.WriteLine(
$"[Autis] Error renaming set '{currentName}' to uppercase: {ex}");
}
}
return result;
}
private static List<SelectionSet> CollectSets(Document document)
{
var sets = new List<SelectionSet>();
if (document?.SelectionSets?.RootItem is GroupItem root)
CollectSets(root, sets);
return sets;
}
private static void CollectSets(GroupItem group, List<SelectionSet> result)
{
foreach (SavedItem item in group.Children)
{
if (item is SelectionSet set)
result.Add(set);
else if (item is GroupItem subGroup)
CollectSets(subGroup, result);
}
}
}
}