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);
        }
    }
}

你可能感兴趣的:(update)