revit api事件
DocumentOpened事件
public class Application_DocumentOpened : IExternalApplication
{public IExternalApplication.Result OnStartup(ControlledApplication application){try{//注册事件application.DocumentOpened += new EventHandler<Autodesk.Revit.Events.DocumentOpenedEventArgs>(application_DocumentOpened);}catch (Exception){return Autodesk.Revit.UI.Result.Failed;}return Autodesk.Revit.UI.Result.Succeeded;}public IExternalApplication.Result OnShutdown(ControlledApplication application){//注销事件application.DocumentOpened -= new EventHandler<Autodesk.Revit.Events.DocumentOpenedEventArgs>(application_DocumentOpened);return Autodesk.Revit.UI.Result.Succeeded;}public void application_DocumentOpened(object sender, DocumentOpenedEventArgs args){//从事件参数中获得文档对象Document doc = args.Document;Transaction transaction = new Transaction(doc, "Edit Address");if (transaction.Start() == TransactionStatus.Started){doc.ProjectInformation.Address ="United States - Massachusetts - Waltham - 1560 Trapelo Road";transaction.Commit();}}
}DocumentSavingAs事件响应函数
private void CheckProjectStatusInitial(Object sender, DocumentSavingAsEventArgs args)
{Document doc = args.Document;ProjectInfo proInfo = doc.ProjectInformation;// 项目信息只存在于项目文档中。if (null != proInfo){if (string.IsNullOrEmpty(proInfo.Status)){// 取消另存行为args.Cancel = true;MessageBox.Show("项目参数没有设置。取消保存。");}}
}闲置事件响应函数
//实现外部程序的Execute函数
//创建一个文字,并且注册一个闲置事件
TextNote textNote = null;
String oldDateTime = null;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{UIApplication uiApp = new UIApplication(commandData.Application.Application);Document doc = commandData.Application.ActiveUIDocument.Document;using (Transaction t = new Transaction(doc, "Text Note Creation")){t.Start();textNote = doc.Create.NewTextNote(doc.ActiveView, XYZ.Zero, XYZ.BasisX, XYZ.BasisY, 0, TextAlignFlags.TEF_ALIGN_LEFT, DateTime.Now.ToString());t.Commit();}oldDateTime = DateTime.Now.ToString();uiApp.Idling += new EventHandler<IdlingEventArgs>(idleUpdate);return Result.Succeeded;
}//闲置事件处理函数
//当时间有更新就将文字内容改为当前时间
public void idleUpdate(object sender, IdlingEventArgs e)
{UIApplication uiApp = sender as UIApplication;Document doc = uiApp.ActiveUIDocument.Document;if (oldDateTime != DateTime.Now.ToString()){using (Transaction transaction = new Transaction(doc, "Text Note Update")){transaction.Start();textNote.Text = DateTime.Now.ToString();transaction.Commit();}oldDateTime = DateTime.Now.ToString();}
}实现IExternalEventHandler接口
public class ExternalEventExample : IExternalEventHandler
{public void Execute(UIApplication app){TaskDialog.Show("External Event", "Click Close to close.");}public string GetName(){return "External Event Example";}
}外部事件的注册和触发
public class ExternalEventExampleApp : IExternalApplication
{public static ExternalEventExampleApp thisApp = null;// 非模态对话框实例private ExternalEventExampleDialog m_MyForm;public Result OnShutdown(UIControlledApplication application){if (m_MyForm != null && m_MyForm.Visible){m_MyForm.Close();}return Result.Succeeded;}public Result OnStartup(UIControlledApplication application){m_MyForm = null;   // 在外部命令中创建非模态对话框thisApp = this;  // 静态变量,保存Application实例return Result.Succeeded;}// 外部命令调用此方法public void ShowForm(UIApplication uiapp){// 如果没有创建对话框,创建并显示它if (m_MyForm == null || m_MyForm.IsDisposed){// 新建一个外部事件响应实例ExternalEventExample handler = new ExternalEventExample();// 新建一个外部事件实例ExternalEvent exEvent = ExternalEvent.Create(handler);// 把上面两个实例传给对话框.m_MyForm = new ExternalEventExampleDialog(exEvent, handler);m_MyForm.Show();}}
}[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
public class Command : IExternalCommand
{public virtual Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements){try{ExternalEventExampleApp.thisApp.ShowForm(commandData.Application);return Result.Succeeded;}catch (Exception ex){message = ex.Message;return Result.Failed;}}
}public partial class ExternalEventExampleDialog : Form
{private ExternalEvent m_ExEvent;private ExternalEventExample m_Handler;public ExternalEventExampleDialog(ExternalEvent exEvent, ExternalEventExample handler){InitializeComponent();m_ExEvent = exEvent;m_Handler = handler;}protected override void OnFormClosed(FormClosedEventArgs e){// 保存的实例需要释放m_ExEvent.Dispose();m_ExEvent = null;m_Handler = null;base.OnFormClosed(e);}private void closeButton_Click(object sender, EventArgs e){Close();}//按钮响应函数,点击按钮触发外部事件private void showMessageButton_Click(object sender, EventArgs e){m_ExEvent.Raise();}
}本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/952111.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!