zhangyuekai
2024-08-10 a18b907beff8b21fb4c9d6fb72678ac5e9f7b80d
HStation.RevitDev/RevitDataExport/Plugin/Application.cs
@@ -1,45 +1,235 @@
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI;
using System.IO;
using System.Reflection;
using System.Windows.Media.Imaging;
namespace HStation.RevitDev.RevitDataExport.Plugin
{
    [Transaction(TransactionMode.Manual)]
    public class Application : IExternalApplication
    {
        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }
        public Result OnStartup(UIControlledApplication application)
        {
            //创建自定义Ribbon菜单
            //1.创建RibbonTab选项卡页
            application.CreateRibbonTab("义维定制");
            //2.在RibbonTab选项卡页中创建RibbonPanel面板
            RibbonPanel panel1 = application.CreateRibbonPanel("义维定制", "管道系统链路检测");
            //3.命令按钮
            //3.1指定程序集的名称、所使用的类名、程序集路径
            //程序集路径--最好写成相对位置
            string Path1 = Assembly.GetExecutingAssembly().Location;
            //程序集的名称、所使用的类名——"主程序集命名空间.主程序类名"
            string Class1 = "RevitDataExport.Export";
            //3.2定义PushButtonData命令按钮数据资料,绑定程序集
            PushButtonData pdata = new PushButtonData("命令按钮", "系统详情", Path1, Class1);
            //3.3将PushButton添加到面板中
            PushButton push = panel1.AddItem(pdata) as PushButton;
            //3.4为PushButton添加图标
            //可把图片资源加载入项目中,方便把图片的路径写成相对路径,并进行修改属性(图片的生成操作,改位:嵌入的资源)
            Stream imgPath = Assembly.GetExecutingAssembly().GetManifestResourceStream("RevitDataExport.Resources.piping_system_detect2.png");
            push.LargeImage = BitmapFrame.Create(imgPath);
            //3.5设置命令按钮的默认提示信息
            push.ToolTip = "Revit插件简介";
            return Result.Succeeded;
        }
    }
}
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HStation.RevitDev.Model.ModelEnum;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Entity;
using HStation.RevitDev.RevitDataExport.Utility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using System.Windows.Media.Imaging;
namespace HStation.RevitDev.RevitDataExport.Plugin
{
    [Transaction(TransactionMode.Manual)]
    public class Application : IExternalApplication
    {
        UIControlledApplication m_application;
        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }
        public Result OnStartup(UIControlledApplication application)
        {
            m_application = application;
            Ribbon ribbon = new Ribbon(application);
            RibbonPanel ribbonPanel = ribbon.CreateRibbon();
            ribbonPanel.AddSeparator();
            GlobalResource.CurrentRevitVersion = application.ControlledApplication.VersionNumber;
            CreateFamilyBottons(ribbon, ribbonPanel);
            ribbonPanel.AddSeparator();
            CreateExportBottons(ribbon, ribbonPanel);
            ribbonPanel.AddSeparator();
            CreateVisibleBotton(ribbon, ribbonPanel);
            ribbonPanel.AddSeparator();
            CreateCheckBotton(ribbon, ribbonPanel);
            ribbonPanel.AddSeparator();
            //AutoClassified SystemSelect
            CreateAutoClassifiedBotton(ribbon, ribbonPanel);
            DockPaneUtil.RegistInstanceDockablePanel(application);
            ribbonPanel.AddSeparator();
            CreateSystemSelectBotton(ribbon, ribbonPanel);
            ribbonPanel.AddSeparator();
            CreateFamilyManagerBotton(ribbon, ribbonPanel);
            ribbonPanel.AddSeparator();
            RegistEvent();
            return Result.Succeeded;
        }
        private void CreateFamilyManagerBotton(Ribbon ribbon, RibbonPanel ribbonPanel)
        {
            //SystemSelect
            string className = "HStation.RevitDev.RevitDataExport.FamilyManager";
            string path = Assembly.GetExecutingAssembly().Location;
            PushButtonData pdata = new PushButtonData("FamilyManager", "族库管理", path, className);
            PushButton pBtn = ribbonPanel.AddItem(pdata) as PushButton;
            string imagePath = Path.Combine(GlobalResource.ImageDirectory, $"族库管理.png");
            pBtn.LargeImage = new BitmapImage(new Uri(imagePath));//32 * 32
            pBtn.ToolTip = "族库管理";
        }
        private void CreateSystemSelectBotton(Ribbon ribbon, RibbonPanel ribbonPanel)
        {
            //SystemSelect
            string className = "HStation.RevitDev.RevitDataExport.SystemSelect";
            string path = Assembly.GetExecutingAssembly().Location;
            PushButtonData pdata = new PushButtonData("SystemSelect", "系统分类", path, className);
            PushButton pBtn = ribbonPanel.AddItem(pdata) as PushButton;
            string imagePath = Path.Combine(GlobalResource.ImageDirectory, $"系统分类.png");
            pBtn.LargeImage = new BitmapImage(new Uri(imagePath));//32 * 32
            pBtn.ToolTip = "系统分类";
        }
        private void CreateAutoClassifiedBotton(Ribbon ribbon, RibbonPanel ribbonPanel)
        {
            //Classified Elements
            string className = "HStation.RevitDev.RevitDataExport.AutoClassified";
            string path = Assembly.GetExecutingAssembly().Location;
            PushButtonData pdata = new PushButtonData("AutoClassified", "构件分类", path, className);
            PushButton pBtn = ribbonPanel.AddItem(pdata) as PushButton;
            string imagePath = Path.Combine(GlobalResource.ImageDirectory, $"构件分类.png");
            pBtn.LargeImage = new BitmapImage(new Uri(imagePath));//32 * 32
            pBtn.ToolTip = "构件分类";
        }
        private void CreateCheckBotton(Ribbon ribbon, RibbonPanel ribbonPanel)
        {
            //Check Links
            string className = "HStation.RevitDev.RevitDataExport.SystemCheck";
            string path = Assembly.GetExecutingAssembly().Location;
            PushButtonData pdata = new PushButtonData("Check", "系统检查", path, className);
            PushButton pBtn = ribbonPanel.AddItem(pdata) as PushButton;
            string imagePath = Path.Combine(GlobalResource.ImageDirectory, $"系统检查.png");
            pBtn.LargeImage = new BitmapImage(new Uri(imagePath));//32 * 32
            pBtn.ToolTip = "系统检查  ";
        }
        private void CreateVisibleBotton(Ribbon ribbon, RibbonPanel ribbonPanel)
        {
            //Hide Or Display Models
            string className = "HStation.RevitDev.RevitDataExport.HideModels";
            string path = Assembly.GetExecutingAssembly().Location;
            PushButtonData pdata = new PushButtonData("Hide", "一键显隐", path, className);
            PushButton pBtn = ribbonPanel.AddItem(pdata) as PushButton;
            string imagePath = Path.Combine(GlobalResource.ImageDirectory, $"一键显隐.png");
            pBtn.LargeImage = new BitmapImage(new Uri(imagePath));//32 * 32
            pBtn.ToolTip = "一键显隐";
        }
        private void CreateExportBottons(Ribbon ribbon, RibbonPanel ribbonPanel)
        {
            string className = "HStation.RevitDev.RevitDataExport.ExportModels";
            string path = Assembly.GetExecutingAssembly().Location;
            PushButtonData pdata = new PushButtonData("Export", "导出", path, className);
            PushButton pBtn = ribbonPanel.AddItem(pdata) as PushButton;
            string imagePath = Path.Combine(GlobalResource.ImageDirectory, $"导出.png");
            pBtn.LargeImage = new BitmapImage(new Uri(imagePath));//32 * 32
            pBtn.ToolTip = "导出模型数据";
        }
        private static void CreateFamilyBottons(Ribbon ribbon, RibbonPanel ribbonPanel)
        {
            string className1 = "HStation.RevitDev.RevitDataExport.PumpSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_Pump, className1);
            string className2 = "HStation.RevitDev.RevitDataExport.ValveSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_Valve, className2);
            string className3 = "HStation.RevitDev.RevitDataExport.PipeSystem";
            ribbon.CreateButton_PipeSystem(ribbonPanel, RevitType.RFT_Pipe, className3);
            string className4 = "HStation.RevitDev.RevitDataExport.HeatExchangerSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_HeatExchanger, className4);
            string className5 = "HStation.RevitDev.RevitDataExport.BlockerSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_Blocker, className5);
            string className6 = "HStation.RevitDev.RevitDataExport.ShowerSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_Shower, className6);
            string className7 = "HStation.RevitDev.RevitDataExport.ThreeJointSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_ThreeJoint, className7);
            string className8 = "HStation.RevitDev.RevitDataExport.FourJointSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_FourJoint, className8);
            string className9 = "HStation.RevitDev.RevitDataExport.WaterMeterSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_WaterMeter, className9);
            string className10 = "HStation.RevitDev.RevitDataExport.WaterPoolSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_WaterPool, className10);
            string className11 = "HStation.RevitDev.RevitDataExport.WaterBoxSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_WaterBox, className11);
            string className12 = "HStation.RevitDev.RevitDataExport.ElbowSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_Elbow, className12);
            string className13 = "HStation.RevitDev.RevitDataExport.FireHydrantSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_FireHydrant, className13);
            string className14 = "HStation.RevitDev.RevitDataExport.ReducingSystem";
            ribbon.CreateButton_SystemAnalysis(ribbonPanel, RevitType.RFT_Converter, className14);
        }
        private void RegistEvent()
        {
            m_application.ControlledApplication.DocumentOpened += ControlledApplication_DocumentOpened;
            m_application.ControlledApplication.DocumentOpening += ControlledApplication_DocumentOpening;
            m_application.ControlledApplication.DocumentCreated += ControlledApplication_DocumentCreated;
            m_application.ControlledApplication.DocumentCreating += ControlledApplication_DocumentCreating;
            m_application.ControlledApplication.DocumentSaved += ControlledApplication_DocumentSaved;
            m_application.ControlledApplication.DocumentSavedAs += ControlledApplication_DocumentSavedAs;
        }
        private void ControlledApplication_DocumentSavedAs(object sender, Autodesk.Revit.DB.Events.DocumentSavedAsEventArgs e)
        {
            CacheUtil.SaveCache(e.Document);
        }
        private void ControlledApplication_DocumentSaved(object sender, Autodesk.Revit.DB.Events.DocumentSavedEventArgs e)
        {
            CacheUtil.SaveCache(e.Document);
        }
        private void ControlledApplication_DocumentOpening(object sender, Autodesk.Revit.DB.Events.DocumentOpeningEventArgs e)
        {
            DockPaneUtil.HideAllDockablePane(m_application);
        }
        private void ControlledApplication_DocumentCreated(object sender, Autodesk.Revit.DB.Events.DocumentCreatedEventArgs e)
        {
            DockPaneUtil.HideAllDockablePane(m_application);
            GlobalResource.RevitModels = new Dictionary<RevitType, ObservableCollection<ElementModel>>();
            GlobalResource.CurrentDocument = e.Document;
        }
        private void ControlledApplication_DocumentOpened(object sender, Autodesk.Revit.DB.Events.DocumentOpenedEventArgs e)
        {
            DockPaneUtil.HideAllDockablePane(m_application);
            GlobalResource.CurrentDocument = e.Document;
            CacheUtil.InitCache(e.Document);
        }
        private void ControlledApplication_DocumentCreating(object sender, Autodesk.Revit.DB.Events.DocumentCreatingEventArgs e)
        {
            DockPaneUtil.HideAllDockablePane(m_application);
        }
    }
}