Revit後期添加按鈕和麪板

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;

		}
	}
}

插件的效果
參考文章1
參考文章2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章