Revit二次开发DockableDialog (可停靠窗体)

冷暖自知 提交于 2019-12-09 04:15:47

三个步骤

1、首先可停靠窗体为 Page 窗体,即WPF控件需继承自Page。此外还需继承 IDockablePaneProvider 接口,然后在WPF控件后台代码汇总实现函数SetupDockablePane。

2、在IExternalApplication的OnStartup函数中完成注册(RegisterDockablePane)可停靠窗口,同时添加一个Revit命令按钮。

3、在IExternalCommand里调用Page窗口, uiapp.GetDockablePane(dockablepaneid),然后dockablepane.Show()。

注:如果想要实现在Revit里选择一个构件,然后在可停靠窗口里显示一些信息,可以用空闲事件来实现。

代码如下:

<Page x:Class="DockableDialogsTest.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:DockableDialogsTest"
             Title="" Background="LightGray"
             Height="500" Width="200">
    <Grid>
        <Button Click="Button_Click" Width="50" Height="30">test</Button>    
    </Grid>
</Page>
namespace DockableDialogsTest
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl1 : Page, IDockablePaneProvider
    {
        public UserControl1()
        {
            InitializeComponent();
        }


        public void SetupDockablePane(DockablePaneProviderData data)
        {
            data.FrameworkElement = this as FrameworkElement;
            DockablePaneProviderData d = new DockablePaneProviderData();

            data.InitialState = new DockablePaneState();
            data.InitialState.DockPosition = DockPosition.Right;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}

Make sure you do not reuse an existing GUID if you decide to copy and paste the code above.  

namespace DockableDialogsTest
{
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;

            string PageGuid = "321044f7-b0b2-4b1c-af18-e71a19252be0";
            UserControl1 page = new UserControl1();
            Guid guid = Guid.Empty;
            guid = new Guid(PageGuid);
            DockablePaneId paneId = new DockablePaneId(guid);
            DockablePane pane = uiapp.GetDockablePane(paneId);
            pane.Show();

            return Result.Succeeded;
        }
    }
}
namespace DockableDialogsTest
{
    public class App : IExternalApplication
    {
        static string AddInPath = typeof(App).Assembly.Location;
        public Result OnStartup(UIControlledApplication uiApp)
        {
            
            try
            {
                RibbonPanel ribbonPanel = uiApp.CreateRibbonPanel("test");
                //在新的Ribbon面板上添加pushButton2按钮
                PushButton pushButton2 = ribbonPanel.AddItem(new PushButtonData("test", "test",
                    AddInPath, "DockableDialogsTest.Command")) as PushButton;

                //给pushButton2按钮添加图片
                Uri uriImage2 = new Uri(@"E:\Revit Plug-in\DockableDialogsTest\DockableDialogsTest\Icon\red.png");
                BitmapImage largeImage2 = new BitmapImage(uriImage2);
                pushButton2.LargeImage = largeImage2;

                string PageGuid = "321044f7-b0b2-4b1c-af18-e71a19252be0";
                UserControl1 page = new UserControl1();
                Guid guid = Guid.Empty;
                guid = new Guid(PageGuid);
                DockablePaneId paneId = new DockablePaneId(guid);
                uiApp.RegisterDockablePane(paneId, "属性", (IDockablePaneProvider)page);
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                TaskDialog.Show("ERROR", ex.ToString());
                return Result.Failed;
            }
            
        }

        public Result OnShutdown(UIControlledApplication a)
        {
            return Result.Succeeded;
        }
    }
}

源代码地址

https://download.csdn.net/download/weixin_40626630/10862764

参考文章

http://blog.sina.com.cn/s/blog_e0020e2d0102yb4a.html

https://spiderinnet.typepad.com/blog/2014/10/revit-net-api-create-custom-dockable-pane-dockablepane-pt-1.html

https://www.cnblogs.com/huangkailang/p/5792960.html

https://blog.csdn.net/The_Eyes/article/details/52701173

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!