Revit二次开发示例:APIAppStartup

下面介绍一个在Revit启动和关闭时调用外部程序的例子。

 

Revit调用的dll主程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;

namespace APIAppStartup
{
    [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 AppSample:IExternalApplication
    {
        #region IExternalApplication Members

        public Result OnShutdown(UIControlledApplication application)
        {
            TaskDialog.Show("Revit", "Quit External Application!");
            return Result.Succeeded;
        }

        public Result OnStartup(UIControlledApplication application)
        {
            string version = application.ControlledApplication.VersionName;

            //display splash window for 10 seconds
            SplashWindow.StartSplash();
            SplashWindow.ShowVersion(version);
            System.Threading.Thread.Sleep(10000);
            SplashWindow.StopSplash();

            return Result.Succeeded;
        }

        #endregion
    }
}

 

在Revit启动时调用的启动画面程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace APIAppStartup
{
    
    public delegate void DelegateCloseSplash();
    

    public partial class SplashWindow : Form
    {
        public SplashWindow()
        {
            InitializeComponent();
            m_delegateClose = new DelegateCloseSplash(InternalCloseSplash);
        }

        private DelegateCloseSplash m_delegateClose;
        private static SplashWindow m_instance;
        private static Thread InstanceCaller;

        //start a new thread to display splash window
        public static void StartSplash()
        {
            m_instance = new SplashWindow();
            m_instance.TopMost = true;
            InstanceCaller = new Thread(new ThreadStart(MySplashThreadFunc));
            InstanceCaller.Start();
        }

        //kill the thread
        public static void StopSplash()
        {
            if (m_instance != null)
            {
                m_instance.Invoke(m_instance.m_delegateClose);
            }
        }

        //show Revit version info
        public static void ShowVersion(string version)
        {
            m_instance.Version.Text = version;
        }

        void InternalCloseSplash()
        {
            this.Close();
            this.Dispose();
        }


        //this is called by the new thread to show the splash screen
        private static void MySplashThreadFunc()
        {
            if (m_instance != null)
            {
                m_instance.TopMost = true;
                m_instance.ShowDialog();
            }
        }
    }
}

 

Addin文件:

"1.0" encoding="utf-8"?>

  "Application">
    APIAppStartup
    
      C:\Users\Administrator\documents\visual studio 2012\Projects\APIAppStartup\APIAppStartup\bin\Release\APIAppStartup.dll
    
    502fe383-2648-4e98-adf8-5e6047f9dc34
    APIAppStartup.AppSample
    ADSK
    http://xpvincent.cnblogs.com
  

你可能感兴趣的:(Revit二次开发示例:APIAppStartup)