1
1
using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
2
4
using Autodesk . Revit . UI ;
3
- using Autodesk . Revit . ApplicationServices ;
4
- using pyRevitAssemblyBuilder . Shared ;
5
- using static System . Net . Mime . MediaTypeNames ;
6
5
using pyRevitAssemblyBuilder . AssemblyMaker ;
6
+ using pyRevitAssemblyBuilder . Shared ;
7
7
8
8
namespace pyRevitAssemblyBuilder . SessionManager
9
9
{
10
10
public class UIManagerService : IUIManager
11
11
{
12
- private readonly UIApplication _uiApplication ;
12
+ private readonly UIApplication _uiApp ;
13
13
14
- public UIManagerService ( UIApplication uiApplication )
14
+ public UIManagerService ( UIApplication uiApp )
15
15
{
16
- _uiApplication = uiApplication ;
16
+ _uiApp = uiApp ;
17
17
}
18
18
19
19
public void BuildUI ( IExtension extension , ExtensionAssemblyInfo assemblyInfo )
20
20
{
21
- string tabName = extension . Name ;
22
- string panelName = "Commands" ;
21
+ if ( extension ? . Children == null )
22
+ return ;
23
23
24
- try
24
+ foreach ( var obj in extension . Children as IEnumerable < object > ?? Enumerable . Empty < object > ( ) )
25
25
{
26
- _uiApplication . CreateRibbonTab ( tabName ) ;
26
+ RecursivelyBuildUI ( obj , null , null , extension . Name , assemblyInfo ) ;
27
27
}
28
- catch { }
28
+ }
29
29
30
- RibbonPanel panel = null ;
31
- foreach ( var existingPanel in _uiApplication . GetRibbonPanels ( tabName ) )
30
+ private void RecursivelyBuildUI ( object obj , object parentComponent , RibbonPanel parentPanel , string tabName , ExtensionAssemblyInfo assemblyInfo )
31
+ {
32
+ var component = obj as ICommandComponent ;
33
+ if ( component == null )
34
+ return ;
35
+
36
+ var type = CommandComponentTypeExtensions . FromExtension ( component . Type ) ;
37
+
38
+ switch ( type )
32
39
{
33
- if ( existingPanel . Name == panelName )
34
- {
35
- panel = existingPanel ;
40
+ case CommandComponentType . Tab :
41
+ try { _uiApp . CreateRibbonTab ( component . Name ) ; } catch { }
42
+ foreach ( var child in component . Children as IEnumerable < object > ?? Enumerable . Empty < object > ( ) )
43
+ RecursivelyBuildUI ( child , component , null , component . Name , assemblyInfo ) ;
36
44
break ;
37
- }
38
- }
39
- if ( panel == null )
40
- {
41
- panel = _uiApplication . CreateRibbonPanel ( tabName , panelName ) ;
42
- }
43
45
44
- foreach ( var cmd in extension . GetAllCommands ( ) )
45
- {
46
- var pushButtonData = new PushButtonData (
47
- name : cmd . Name ,
48
- text : cmd . Name ,
49
- assemblyName : assemblyInfo . Location ,
50
- className : cmd . UniqueId ) ;
51
-
52
- var button = panel . AddItem ( pushButtonData ) as PushButton ;
53
- if ( ! string . IsNullOrEmpty ( cmd . Tooltip ) )
54
- {
55
- button . ToolTip = cmd . Tooltip ;
56
- }
46
+ case CommandComponentType . Panel :
47
+ var panel = _uiApp . GetRibbonPanels ( tabName ) . FirstOrDefault ( p => p . Name == component . Name )
48
+ ?? _uiApp . CreateRibbonPanel ( tabName , component . Name ) ;
49
+ foreach ( var child in component . Children as IEnumerable < object > ?? Enumerable . Empty < object > ( ) )
50
+ RecursivelyBuildUI ( child , component , panel , tabName , assemblyInfo ) ;
51
+ break ;
52
+
53
+ case CommandComponentType . Stack :
54
+ var buttonDatas = new List < RibbonItemData > ( ) ;
55
+
56
+ foreach ( var child in component . Children as IEnumerable < object > ?? Enumerable . Empty < object > ( ) )
57
+ {
58
+ var subCmd = child as ICommandComponent ;
59
+ if ( subCmd == null ) continue ;
60
+
61
+ var subType = CommandComponentTypeExtensions . FromExtension ( subCmd . Type ) ;
62
+ switch ( subType )
63
+ {
64
+ case CommandComponentType . PushButton :
65
+ buttonDatas . Add ( new PushButtonData ( subCmd . Name , subCmd . Name , assemblyInfo . Location , subCmd . UniqueId ) ) ;
66
+ break ;
67
+ case CommandComponentType . PullDown :
68
+ buttonDatas . Add ( new PulldownButtonData ( subCmd . Name , subCmd . Name ) ) ;
69
+ break ;
70
+ }
71
+ }
72
+
73
+ if ( buttonDatas . Count == 2 )
74
+ parentPanel ? . AddStackedItems ( buttonDatas [ 0 ] , buttonDatas [ 1 ] ) ;
75
+ else if ( buttonDatas . Count >= 3 )
76
+ parentPanel ? . AddStackedItems ( buttonDatas [ 0 ] , buttonDatas [ 1 ] , buttonDatas [ 2 ] ) ;
77
+ break ;
78
+
79
+ case CommandComponentType . PushButton :
80
+ case CommandComponentType . SmartButton :
81
+ var pbData = new PushButtonData ( component . Name , component . Name , assemblyInfo . Location , component . UniqueId ) ;
82
+ var btn = parentPanel ? . AddItem ( pbData ) as PushButton ;
83
+ if ( ! string . IsNullOrEmpty ( component . Tooltip ) )
84
+ btn . ToolTip = component . Tooltip ;
85
+ break ;
86
+
87
+ case CommandComponentType . PullDown :
88
+ var pdBtnData = new PulldownButtonData ( component . Name , component . Name ) ;
89
+ var pdBtn = parentPanel ? . AddItem ( pdBtnData ) as PulldownButton ;
90
+ if ( pdBtn == null ) return ;
91
+
92
+ foreach ( var child in component . Children as IEnumerable < object > ?? Enumerable . Empty < object > ( ) )
93
+ {
94
+ if ( child is ICommandComponent subCmd &&
95
+ CommandComponentTypeExtensions . FromExtension ( subCmd . Type ) == CommandComponentType . PushButton )
96
+ {
97
+ var subData = new PushButtonData ( subCmd . Name , subCmd . Name , assemblyInfo . Location , subCmd . UniqueId ) ;
98
+ pdBtn . AddPushButton ( subData ) ;
99
+ }
100
+ }
101
+ break ;
102
+
103
+ case CommandComponentType . SplitButton :
104
+ case CommandComponentType . SplitPushButton :
105
+ var splitData = new SplitButtonData ( component . Name , component . Name ) ;
106
+ var splitBtn = parentPanel ? . AddItem ( splitData ) as SplitButton ;
107
+ if ( splitBtn == null ) return ;
108
+
109
+ foreach ( var child in component . Children as IEnumerable < object > ?? Enumerable . Empty < object > ( ) )
110
+ {
111
+ if ( child is ICommandComponent subCmd &&
112
+ CommandComponentTypeExtensions . FromExtension ( subCmd . Type ) == CommandComponentType . PushButton )
113
+ {
114
+ var subData = new PushButtonData ( subCmd . Name , subCmd . Name , assemblyInfo . Location , subCmd . UniqueId ) ;
115
+ splitBtn . AddPushButton ( subData ) ;
116
+ }
117
+ }
118
+ break ;
57
119
}
58
120
}
59
-
60
121
}
61
122
}
0 commit comments