• Revit二次开发示例:AutoUpdate


    在Revit打开文件时,修改文件信息。并记录状态,存到log文件中。

    #region Namespaces
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using Autodesk.Revit.ApplicationServices;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.DB.Events;
    using Autodesk.Revit.UI;
    #endregion
    
    namespace AutoUpdate
    {
        [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
        [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
        [Autodesk.Revit.Attributes.Journaling(JournalingMode.NoCommandData)]
        class App : IExternalApplication
        {
            private TextWriterTraceListener m_txtListener;
            private static string m_directory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            private string m_tempFile = Path.Combine(m_directory, "temp.log");
    
            public Result OnStartup(UIControlledApplication a)
            {
                try
                {
                    CreateTempFile();
    
                    a.ControlledApplication.DocumentOpened += ControlledApplication_DocumentOpened;
                }
                catch (Exception)
                {
                    return Result.Failed;
                }
                return Result.Succeeded;
            }
    
            public Result OnShutdown(UIControlledApplication a)
            {
                a.ControlledApplication.DocumentOpened -= ControlledApplication_DocumentOpened;
                CloseLogFile();
                return Result.Succeeded;
            }
    
            void ControlledApplication_DocumentOpened(object sender, Autodesk.Revit.DB.Events.DocumentOpenedEventArgs e)
            {
                DumpEventArgs(e);
                Document doc = e.Document;
    
                if (doc.IsFamilyDocument)
                {
                    return;
                }
    
                try
                {
                    Transaction eventTransaction = new Transaction(doc, "Event handler modify project information");
                    eventTransaction.Start();
                    doc.ProjectInformation.Address =
                       "United States - Massachusetts - Waltham - 610 Lincoln St";
                    eventTransaction.Commit();
                }
                catch (Exception ee)
                {
                    Trace.WriteLine("Failed to modify project information!-" + ee.Message);
                }
    
                Trace.WriteLine("The value after running the sample ------>");
                Trace.WriteLine("    [Address]         :" + doc.ProjectInformation.Address);
            }
    
            
    
            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);
            }
    
            private void DumpEventArgs(DocumentOpenedEventArgs args)
            {
                Trace.WriteLine("DocumentOpenedEventArgs Parameters ------>");
                Trace.WriteLine("     Event Cancel          : " + args.IsCancelled());
                Trace.WriteLine("     Event Cancvellable    : " + args.Cancellable);
                Trace.WriteLine("     Status                : " + args.Status);
            }
    
            private void CloseLogFile()
            {
                Trace.Flush();
                Trace.Listeners.Remove(m_txtListener);
                Trace.Close();
                m_txtListener.Close();
    
                string logFile = Path.Combine(m_directory, "AutoUpdate.log");
                if (File.Exists(logFile)) File.Delete(logFile);
                File.Copy(m_tempFile, logFile);
                File.Delete(m_tempFile);
            }
        }
    }
  • 相关阅读:
    2016第7周五
    优秀it博客和文章
    java惯用法转载
    2016第7周二
    人,终究是要找到自己喜欢的...才行
    2016第6周日
    2016第6周六
    2016第6周五
    2016第6周四
    2016第6周三
  • 原文地址:https://www.cnblogs.com/xpvincent/p/3600835.html
Copyright © 2020-2023  润新知