---恢复内容开始---
1.新建类库项目,项目中新建WPF用户控件(UserControl)得到名为UserControl1的实例。
revit中可停靠窗口的父类为page类,同时也继承了IDockablePaneProvider接口。
所以需要将相关地方改为图中所示内容。
2.接下来需要在隐藏代码中实现接口中的SetupDockablePane()函数以初始化窗口状态。
1 public void SetupDockablePane(DockablePaneProviderData Data) 2 { 3 Data.FrameworkElement = this as FrameworkElement; 4 5 Data.InitialState = new DockablePaneState(); 6 Data.InitialState.DockPosition = DockPosition.Bottom; 7 8 }
这样就完成了我们的UserControl1。
而且我们的窗口将出现在底部。
3.接下来在revit中注册一个可停靠窗口(顺带添加一个命令按钮)
1 static string path = typeof(Panel).Assembly.Location; 2 3 public Result OnStartup(UIControlledApplication application) 4 { 5 RibbonPanel ribbonPanel = application.CreateRibbonPanel("窗口"); 6 7 PushButton pushButton = ribbonPanel.AddItem(new PushButtonData 8 ( "DockPanel1","可停靠",path,"DockPanel.Test")) as PushButton; 9 10 string pageGuid = "2ab776b4-b3fc-4810-8f44-ab5f1c9f5fa8"; 11 Guid guid = new Guid(pageGuid); 12 DockablePaneId id = new DockablePaneId(guid); 13 UserControl1 panel1 = new UserControl1(); 14 application.RegisterDockablePane(id,"DockPanelTest",panel1 as IDockablePaneProvider); 15 16 return Result.Succeeded; 17 }
核心方法是
1 public void RegisterDockablePane( 2 DockablePaneId id, 3 string title, 4 IDockablePaneProvider provider 5 )
panel1 为之前写好的UserControl1 的一个实例,UserControl1继承了IDockablePaneProvider接口。
4.最后是按钮命令代码
1 [Transaction(TransactionMode.Manual)] 2 [Regeneration(RegenerationOption.Manual)] 3 [Journaling(JournalingMode.NoCommandData)] 4 public class Test : IExternalCommand 5 { 6 7 public Result Execute(ExternalCommandData commandData, ref string message, ElementSet element) 8 { 9 string pageGuid = "2ab776b4-b3fc-4810-8f44-ab5f1c9f5fa8"; 10 Guid guid = new Guid(pageGuid); 11 DockablePaneId id = new DockablePaneId(guid); 12 13 DockablePane panel = commandData.Application.GetDockablePane(id); 14 panel.Show(); 15 16 return Result.Succeeded; 17 } 18 }
5.生成成功后编写addin文件再打开revit即可看到可停靠窗口。