应用程序:AutoUpdate
Revit平台:所有
Revit版本:2011.0
首次发布用于:2010.0
编程语言:C#
技能等级:中等
类别:基础
类型:外部应用
主题:打开文档后自动更新Revit模型。
摘要:此示例演示了如何在事件处理程序方法的事件处理程序中订阅DocumentOpened事件和修改模型。
相关类:
Autodesk.Revit.UI.IExternalApplication
Autodesk.Revit.DB.Events
项目文件:
ExternalApplication.cs
此文件包含一个类Application,该类实现IExternalApplation接口。当Revit启动时,OnStartUp方法将注册DocumentOpened事件。我们只是演示如何更改ProjectInformation.Address属性,但用户可以对当前项目进行其他修改,如删除、创建等。
功能:
该示例实现了IExternalApplication接口,并提供了以下功能:
-没有为用户提供UI,示例将为DocumentOpened事件订阅事件处理程序,字符串“United States Massachusetts-Waltham-610 Lincoln St”将分配给ProjectInformation.Address属性作为新地址。
-可以从DocumentOpened事件的EventArgs.Document属性访问ProjectInformation.Address属性。
-修改完成后,ProjectInformation.Address属性的值将转储到日志文件中。
-DocumentOpenedEventArgs属性Cancel、Cancelable和Status将在引发事件时转储到日志中。
实施:
1.运行Revit并打开文档。
2.然后,进入“设置” 项目信息 项目地址,然后您可以看到此属性的值已更改为我们指定的值。在本示例中,我们从UI触发此事件,因此它必须在ControlledApplication上注册。
3.您也可以参考日志文件来检查操作是否成功。
注:
由于族文档不支持“项目信息”,因此此示例不应在族文档上运行。
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
ExternalApplication.cs
//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.AutoUpdate.CS
{
/// <summary>
/// This class implements IExternalApplication and demonstrate how to subscribe event
/// and modify model in event handler. We just demonstrate changing
/// the ProjectInformation.Address property, but user can do other modification for
/// current project, like delete element, create new element, etc.
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class ExternalApplication : IExternalApplication
{
#region Class Member Variables
/// <summary>
/// This listener is used to monitor the events arguments and the result of the sample.
/// It will be bound to log file AutoUpdate.log, it will be added to Trace.Listeners.
/// </summary>
private TextWriterTraceListener m_txtListener;
/// <summary>
/// get assembly path.
/// </summary>
private static String m_directory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
/// <summary>
/// create the temp file name.
/// </summary>
private String m_tempFile = Path.Combine(m_directory, "temp.log");
#endregion
#region IExternalApplication Members
/// <summary>
/// Implement this method to subscribe event.
/// </summary>
/// <param name="application">the controlled application.</param>
/// <returns>Return the status of the external application.
/// A result of Succeeded means that the external application successfully started.
/// Cancelled can be used to signify that the user cancelled the external operation at
/// some point.
/// If false is returned then Revit should inform the user that the external application
/// failed to load and the release the internal reference.</returns>
public Autodesk.Revit.UI.Result OnStartup(UIControlledApplication application)
{
try
{
// Create a temp file to dump the log, and copy this file to log file
// at the end of the sample.
CreateTempFile();
// Register event. In this sample, we trigger this event from UI, so it must
// be registered on ControlledApplication.
application.ControlledApplication.DocumentOpened += new EventHandler
<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(application_DocumentOpened);
}
catch (Exception)
{
return Autodesk.Revit.UI.Result.Failed;
}
return Autodesk.Revit.UI.Result.Succeeded;
}
/// <summary>
/// Implement this method to implement the external application which should be called when
/// Revit is about to exit,Any documents must have been closed before this method is called.
/// </summary>
/// <param name="application">An object that is passed to the external application
/// which contains the controlled application.</param>
/// <returns>Return the status of the external application.
/// A result of Succeeded means that the external application successfully shutdown.
/// Cancelled can be used to signify that the user cancelled the external operation at
/// some point.
/// If false is returned then the Revit user should be warned of the failure of the external
/// application to shut down correctly.</returns>
public Autodesk.Revit.UI.Result OnShutdown(UIControlledApplication application)
{
// remove the event.
application.ControlledApplication.DocumentOpened -= application_DocumentOpened;
CloseLogFile();
return Autodesk.Revit.UI.Result.Succeeded;
}
#endregion
#region Event Handler
/// <summary>
/// This is the event handler. We modify the model and dump log in this method.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
public void application_DocumentOpened(object sender, DocumentOpenedEventArgs args)
{
// dump the args to log file.
DumpEventArgs(args);
// get document from event args.
Document doc = args.Document;
if (doc.IsFamilyDocument)
{
return;
}
try
{
//now event framework will not provide transaction,user need start by self(2009/11/18)
Transaction eventTransaction = new Transaction(doc, "Event handler modify project information");
eventTransaction.Start();
// assign specified value to ProjectInformation.Address property.
// User can change another properties of document or create(delete) something as he likes.
// Please pay attention that ProjectInformation property is empty for family document.
// But it isn't the correct usage. So please don't run this sample on family document.
doc.ProjectInformation.Address =
"United States - Massachusetts - Waltham - 610 Lincoln St";
eventTransaction.Commit();
}
catch (Exception ee)
{
Trace.WriteLine("Failed to modify project information!-"+ee.Message);
}
// write the value to log file to check whether the operation is successful.
Trace.WriteLine("The value after running the sample ------>");
Trace.WriteLine(" [Address] :" + doc.ProjectInformation.Address);
}
#endregion
#region Class Methods
/// <summary>
/// Dump the events arguments to log file: AutoUpdat.log.
/// </summary>
/// <param name="args"></param>
private void DumpEventArgs(DocumentOpenedEventArgs args)
{
Trace.WriteLine("DocumentOpenedEventArgs Parameters ------>");
Trace.WriteLine(" Event Cancel : " + args.IsCancelled()); // is it be cancelled?
Trace.WriteLine(" Event Cancellable : " + args.Cancellable); // Cancellable
Trace.WriteLine(" Status : " + args.Status); // Status
}
/// <summary>
/// Create a log file to track the subscribed events' work process.
/// </summary>
private void CreateTempFile()
{
if (File.Exists(m_tempFile)) File.Delete(m_tempFile);
m_txtListener = new TextWriterTraceListener(m_tempFile);
Trace.AutoFlush = true;
Trace.Listeners.Add(m_txtListener);
}
/// <summary>
/// Close the log file and remove the corresponding listener.
/// </summary>
private void CloseLogFile()
{
// close listeners now.
Trace.Flush();
Trace.Listeners.Remove(m_txtListener);
Trace.Close();
m_txtListener.Close();
// copy temp file to log file and delete the temp file.
String logFile = Path.Combine(m_directory, "AutoUpdate.log");
if (File.Exists(logFile)) File.Delete(logFile);
File.Copy(m_tempFile, logFile);
File.Delete(m_tempFile);
}
#endregion
}
}
版权所有 :无锡模信建筑科技有限公司 苏ICP备2021028830号-1 BIM建模|BIM技术应用|BIM软件开发
联系地址:江苏省无锡市新吴区龙山路4号B座705 手机:18761516598