Revit后期添加按钮和面板
今天在唐曾老师的博客上看到了这个项目以后觉得很稀奇,就自己也跟着实现了一下。我看了唐曾老师的展示视频,就自己写了功能。我看到老师留下了github地址,应该是有分享源码吧,我这个是大概的实现了整个的思路,但是可能存在很多的Bug.只是作为一个记录学习的过程分享在这里,希望可以帮助到有需要的朋友。
注意:需要多添加一个AddWindows.dll的引用才可以,这个引用可以在添加RevitAPI.dll的文件夹找到
下面是WPF界面的XMAL代码:
<Window x:Class="自定义Tab的添加.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:自定义Tab的添加"
mc:Ignorable="d"
Title="MainWindow" MinHeight="500" MinWidth="450" MaxHeight="500" MaxWidth="450" ResizeMode="NoResize">
<Grid>
<StackPanel Width="450" Height="400" HorizontalAlignment="Left">
<Grid >
</Grid>
<StackPanel Width="300" Height="30" HorizontalAlignment="Center" Orientation="Horizontal" >
<Label Name="labelTab" Width="120" Content="TabName:" FontSize="15" HorizontalContentAlignment="Right"/>
<TextBox Name="TabTxt" FontSize="15" Width="100" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Width="300" Height="30" Margin="0 10 0 0" HorizontalAlignment="Center" Orientation="Horizontal" >
<Label Name="labelRibbon" Width="120" Content="RibbonName:" FontSize="15" HorizontalContentAlignment="Right"/>
<TextBox Name="ribbonTxt" FontSize="15" Width="100" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Width="300" Height="30" Margin="0 10 0 0" HorizontalAlignment="Center" Orientation="Horizontal" >
<Label Name="labelButtonName" Width="120" Content="ButtonName:" FontSize="15" HorizontalContentAlignment="Right"/>
<TextBox Name="ButtonNameTxt" FontSize="15" Width="100" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Width="300" Height="30" Margin="0 10 0 0" HorizontalAlignment="Center" Orientation="Horizontal" >
<Label Name="labelPanelName" Width="120" Content="PanelName:" FontSize="15" HorizontalContentAlignment="Right"/>
<TextBox Name="PanelNameTxt" FontSize="15" Width="100" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Width="300" Height="30" Margin="0 10 0 0" HorizontalAlignment="Center" Orientation="Horizontal" >
<Label Name="labelCLassName" Width="120" Content="FullName:" FontSize="15" HorizontalContentAlignment="Right"/>
<TextBox Name="FullNameTxt" FontSize="15" Width="100" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Width="300" Height="30" Margin="0 10 0 0" HorizontalAlignment="Center" Orientation="Horizontal" >
<Label Name="labelToolTip" Width="120" Content="ToolTip:" FontSize="15" HorizontalContentAlignment="Right"/>
<TextBox Name="ToolTipTxt" FontSize="15" Width="100" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Width="300" Height="30" Margin="0 10 0 0" HorizontalAlignment="Center" Orientation="Horizontal" >
<Label Name="labeldll" Width="120" Content="dll文件路径:" FontSize="15" HorizontalContentAlignment="Right"/>
<Button Name="btnDLL" FontSize="15" Width="100" Content="选择文件" HorizontalAlignment="Left" Click="BtnDLL_Click" />
</StackPanel>
<StackPanel Width="300" Height="30" Margin="0 10 0 0" HorizontalAlignment="Center" Orientation="Horizontal" >
<Label Name="labelicon" Width="120" Content="Icon文件路径:" FontSize="15" HorizontalContentAlignment="Right"/>
<Button Name="btnIcon" FontSize="15" Width="100" Content="选择图片" HorizontalAlignment="Left" Click="BtnIcon_Click" />
</StackPanel>
<StackPanel Width="300" Height="50" Margin="0 10 0 0" HorizontalAlignment="Center" Orientation="Horizontal" >
<Button Name="btnCancel" Content="取消" FontSize="15" Width="100" Height="40"
HorizontalAlignment="Left" VerticalAlignment="Bottom" Click="BtnCancel_Click"/>
<Button Name="btnOK" Content="确定" FontSize="15" Width="100" Height="40"
HorizontalAlignment="Right" Margin="50 0 0 0 " VerticalAlignment="Bottom" Click="BtnOK_Click"/>
</StackPanel>
</StackPanel>
</Grid>
</Window>
界面效果如下:
界面的后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Win32;
namespace 自定义Tab的添加
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public string TabNamae { get; set; }
public string PanelName { get; set; }
public string RibbinName { get; set; }
public string ButtonName { get; set; }
public new string ToolTip { get; set; }
public string Dll { get; set; }
public new string Icon { get; set; }
public string ClassName { get; set; }
public MainWindow()
{
InitializeComponent();
}
private void BtnDLL_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
string file = ofd.FileName;
this.Dll = file;
//MessageBox.Show(file);
}
private void BtnIcon_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
string file = ofd.FileName;
this.Icon = file;
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void BtnOK_Click(object sender, RoutedEventArgs e)
{
this.TabNamae = this.TabTxt.Text;
this.PanelName = this.PanelNameTxt.Text;
this.RibbinName = this.ribbonTxt.Text;
this.ButtonName = this.ribbonTxt.Text;
this.ToolTip = this.ToolTipTxt.Text;
this.ClassName = this.FullNameTxt.Text;
this.Close();
}
}
}
插件的命令:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using System.Windows.Media.Imaging;
using Autodesk.Windows;
namespace 自定义Tab的添加
{
[Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
MainWindow window = new MainWindow();
window.ShowDialog();
UIApplication uiapp = commandData.Application;
string tabName = window.TabNamae;
bool isExit = false;
foreach(Autodesk.Windows.RibbonTab tab in Autodesk.Windows.ComponentManager.Ribbon.Tabs)
{
if (tabName.Equals(tab.Name))
{
tabName = tab.Name;
isExit = true;
}
}
if(!isExit)
{
uiapp.CreateRibbonTab(tabName);
}
Autodesk.Revit.UI.RibbonPanel panel = uiapp.CreateRibbonPanel(tabName, window.PanelName);
PushButton button = panel.AddItem(new PushButtonData(window.ButtonName, window.ToolTip, window.Dll, window.ClassName)) as PushButton;
button.LargeImage = new BitmapImage(new Uri(window.Icon));
return Result.Succeeded;
}
}
}