BIM建筑网
更专业的BIM技术学习网站!


Revit二次开发——非模态窗口

VIP免费下载全站资源
VIP优惠来袭,免费下载全站资料和课程,技术问题可以随时提问;
查看VIP会员

非模态窗口有一个好处,就是可以一直停留在程序之前,然后持续完成操作。但是在Revit二次开发中,非模态窗口也有几个注意事项。

1、需要在文档关闭的时候,把非模态窗口也关闭掉,不然会导致文档关闭,窗口还在这样奇怪的Bug。

2、非模态的窗口的事件需要在IExternalCommand里注册。

3、每个操作必须在外部事件里进行。

以下代码关注后两个注意事项,第一个用上Document事件即可解决。

首先在IExternalCommand注册事件。

 public class Command : IExternalCommand
 {
 public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
 {
 try
 {
				ExecuteEventHandler executeEventHandler = new ExecuteEventHandler("Creat Model Line");
				ExternalEvent externalEvent = ExternalEvent.Create(executeEventHandler);
				// show UI
				ModelessView modelessView = new ModelessView(executeEventHandler, externalEvent); 

 //窗口一直显示在主程序之前
 System.Windows.Interop.WindowInteropHelper mainUI = new System.Windows.Interop.WindowInteropHelper(modelessView);
 mainUI.Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
 modelessView.Show();

 return Autodesk.Revit.UI.Result.Succeeded;
 }
 catch (Exception e)
 {
 message = e.Message;
 return Autodesk.Revit.UI.Result.Failed;
 }
 }
 }

然后写一个通用的外部事件。

 public class ExecuteEventHandler : IExternalEventHandler
 {
 public string Name { get;private set; }

 public Action<UIApplication> ExecuteAction { get; set; }

 public ExecuteEventHandler(string name)
 {
 Name = name;
 }

 public void Execute(UIApplication app)
 { 
 if(ExecuteAction!=null)
 {
 try
 {
 ExecuteAction(app);
 }
 catch
 { }
 }
 }

 public string GetName()
 {
 return Name;
 }
 }

接下来,通过控件来实现创建构件。

public partial class ModelessView : Window
 { 
 
 ExecuteEventHandler _executeEventHandler= null;
 ExternalEvent _externalEvent = null;

 public ModelessView(ExecuteEventHandler executeEventHandler,ExternalEvent externalEvent)
 {
 InitializeComponent();
 _executeEventHandler = executeEventHandler;
 _externalEvent = externalEvent; 
 }

 private void creatLine_Click(object sender, RoutedEventArgs e)
 {
 if(_externalEvent!=null)
 {
 _executeEventHandler.ExecuteAction = new Action<UIApplication>((app) =>
 {
 if (app.ActiveUIDocument == null || app.ActiveUIDocument.Document == null)
 return;

 Document revitDoc = app.ActiveUIDocument.Document;
 using (Transaction transaction=new Transaction(revitDoc,"Creat Line1"))
 {
 transaction.Start();
 Autodesk.Revit.DB.Line line = Autodesk.Revit.DB.Line.CreateBound(new XYZ(0, 0, 0), new XYZ(100, 0, 0));
 SketchPlane sketchPlane = SketchPlane.Create(revitDoc, Plane.CreateByNormalAndOrigin(XYZ.BasisZ, XYZ.Zero));
 revitDoc.Create.NewModelCurve(line as Curve, sketchPlane);
 transaction.Commit();
 } 
 });
 _externalEvent.Raise();
 } 
 }

 private void creatLine2_Click(object sender, RoutedEventArgs e)
 {
 if (_externalEvent != null)
 {
 _executeEventHandler.ExecuteAction = new Action<UIApplication>((app) =>
 {
 if (app.ActiveUIDocument == null || app.ActiveUIDocument.Document == null)
 return;

 Document revitDoc = app.ActiveUIDocument.Document;
 using (Transaction transaction = new Transaction(revitDoc, "Creat Line2"))
 {
 transaction.Start();
 Autodesk.Revit.DB.Line line = Autodesk.Revit.DB.Line.CreateBound(new XYZ(0, 100, 0), new XYZ(100, 100, 0));
 SketchPlane sketchPlane = SketchPlane.Create(revitDoc, Plane.CreateByNormalAndOrigin(XYZ.BasisZ, XYZ.Zero));
 revitDoc.Create.NewModelCurve(line as Curve, sketchPlane);
 transaction.Commit();
 }
 });
 _externalEvent.Raise();
 }
 }
 }

这就是一个简单的非模态窗口实现的办法。

微信公众号:xuebim
关注建筑行业BIM发展、研究建筑新技术,汇集建筑前沿信息!
← 微信扫一扫,关注我们+
赞(0) 打赏
BIM建筑网 » Revit二次开发——非模态窗口
100套内部BIM资料,限时领!
付费搞来的,大家都在学!
领取资料 AI解答

评论 抢沙发

评论前必须登录!

 

BIM建筑网,更专业的BIM技术学习网站!

关注建筑新动态,分享建筑新技术

联系我们关于BIM建筑网

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

扫码登录

微信「关注」,快捷登录
扫码关注后会自动登录
注册登录代表您已同意《用户许可协议》
账号登录 | 其他登录

|登录

找回密码

|账号登录注册