From a18b907beff8b21fb4c9d6fb72678ac5e9f7b80d Mon Sep 17 00:00:00 2001
From: zhangyuekai <zhangyuekai@126.com>
Date: 星期六, 10 八月 2024 21:05:33 +0800
Subject: [PATCH] fix-bug nullreference

---
 HStation.RevitDev/RevitDataExport/Plugin/Application.cs |  349 +++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 235 insertions(+), 114 deletions(-)

diff --git a/HStation.RevitDev/RevitDataExport/Plugin/Application.cs b/HStation.RevitDev/RevitDataExport/Plugin/Application.cs
index 07f356f..07cb212 100644
--- a/HStation.RevitDev/RevitDataExport/Plugin/Application.cs
+++ b/HStation.RevitDev/RevitDataExport/Plugin/Application.cs
@@ -1,114 +1,235 @@
-锘縰sing Autodesk.Revit.Attributes;
-using Autodesk.Revit.UI;
-using HStation.RevitDev.RevitDataExport.Forms;
-using Spire.AI.Api;
-using System;
-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_uiControlledApp;
-        private readonly string m_familyPanelName = "鏃忓垪琛�";
-        public Result OnShutdown(UIControlledApplication application)
-        {
-            return Result.Succeeded;
-        }
-
-        public Result OnStartup(UIControlledApplication application)
-        {
-            m_uiControlledApp = application;
-
-            //鍒涘缓鑿滃崟 绠¢亾绯荤粺鍒嗘瀽
-            CreateRibbon_PipeSystemAnalysis(application);
-
-            //鍒涘缓鑿滃崟 娉电郴缁熷垎鏋�
-            CreateRibbon_PumpSystemAnalysis(application);
-
-            return Result.Succeeded;
-        }
-
-        private void CreateRibbon_PumpSystemAnalysis(UIControlledApplication application)
-        {
-            application.CreateRibbonTab("娉电郴缁熷垎鏋�");
-            RibbonPanel panel = application.CreateRibbonPanel("娉电郴缁熷垎鏋�", "娉电郴缁熷垎鏋�");
-
-            string path = Assembly.GetExecutingAssembly().Location;                            
-            string className = "HStation.RevitDev.RevitDataExport.PumpSystemAnalysis";
-            PushButtonData pdata = new PushButtonData("鍛戒护鎸夐挳", "绯荤粺璇︽儏", path, className);
-            PushButton pBtn = panel.AddItem(pdata) as PushButton;
-
-            string imagePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "../../../../Data/Image/analysis.png";
-            pBtn.LargeImage = new BitmapImage(new Uri(imagePath));//32 * 32
-            pBtn.ToolTip = "Revit鎻掍欢绠�浠�";
-
-            //娉ㄥ唽鍋滈潬闈㈡澘
-            RegistDockablePanel(application);
-            application.ControlledApplication.DocumentOpened += ControlledApplication_DocumentOpened;
-            application.ControlledApplication.DocumentCreated += ControlledApplication_DocumentCreated;
-        }
-
-        private void ControlledApplication_DocumentCreated(object sender, Autodesk.Revit.DB.Events.DocumentCreatedEventArgs e)
-        {
-            HideDockablePane();
-        }
-
-        private void ControlledApplication_DocumentOpened(object sender, Autodesk.Revit.DB.Events.DocumentOpenedEventArgs e)
-        {
-            HideDockablePane();
-        }
-
-        private void HideDockablePane()
-        {
-            Guid guid = new Guid(Common.GlobalResource.Guid_FamilyPanel);
-            DockablePaneId id = new DockablePaneId(guid);
-            DockablePane pane = null;
-            try
-            {
-                pane = m_uiControlledApp.GetDockablePane(id);
-
-            }
-            catch (Exception ex)
-            {
-                string message = ex.Message;
-            }
-            pane.Hide();
-        }
-
-        private void CreateRibbon_PipeSystemAnalysis(UIControlledApplication application)
-        {
-            //1.鍒涘缓RibbonTab閫夐」鍗¢〉
-            application.CreateRibbonTab("涔夌淮瀹氬埗");
-            //2.鍦≧ibbonTab閫夐」鍗¢〉涓垱寤篟ibbonPanel闈㈡澘
-            RibbonPanel panel1 = application.CreateRibbonPanel("涔夌淮瀹氬埗", "绠¢亾绯荤粺閾捐矾妫�娴�");
-
-            //3.鍛戒护鎸夐挳
-            //3.1鎸囧畾绋嬪簭闆嗙殑鍚嶇О銆佹墍浣跨敤鐨勭被鍚嶃�佺▼搴忛泦璺緞
-            //绋嬪簭闆嗚矾寰�--鏈�濂藉啓鎴愮浉瀵逛綅缃�
-            string Path1 = Assembly.GetExecutingAssembly().Location;
-            //绋嬪簭闆嗙殑鍚嶇О銆佹墍浣跨敤鐨勭被鍚嶁�斺��"涓荤▼搴忛泦鍛藉悕绌洪棿.涓荤▼搴忕被鍚�"                                
-            string Class1 = "HStation.RevitDev.RevitDataExport.Export";
-            //3.2瀹氫箟PushButtonData鍛戒护鎸夐挳鏁版嵁璧勬枡锛岀粦瀹氱▼搴忛泦
-            PushButtonData pdata = new PushButtonData("鍛戒护鎸夐挳", "绯荤粺璇︽儏", Path1, Class1);
-            //3.3灏哖ushButton娣诲姞鍒伴潰鏉夸腑
-            PushButton push = panel1.AddItem(pdata) as PushButton;
-            //3.4涓篜ushButton娣诲姞鍥炬爣
-            //鍙妸鍥剧墖璧勬簮鍔犺浇鍏ラ」鐩腑锛屾柟渚挎妸鍥剧墖鐨勮矾寰勫啓鎴愮浉瀵硅矾寰勶紝骞惰繘琛屼慨鏀瑰睘鎬э紙鍥剧墖鐨勭敓鎴愭搷浣�,鏀逛綅锛氬祵鍏ョ殑璧勬簮锛�
-            string imagePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "../../../../Data/Image/analysis.png";
-            push.LargeImage = new BitmapImage(new Uri(imagePath));//32 * 32
-            //3.5璁剧疆鍛戒护鎸夐挳鐨勯粯璁ゆ彁绀轰俊鎭�
-            push.ToolTip = "Revit鎻掍欢绠�浠�";
-        }
-
-        private void RegistDockablePanel(UIControlledApplication uiApp)
-        {
-            DockablePaneId panelId = new DockablePaneId(new Guid(Common.GlobalResource.Guid_FamilyPanel));
-            Wpf_FamilyPanel panel = new Wpf_FamilyPanel();
-            uiApp.RegisterDockablePane(panelId, m_familyPanelName, panel);
-        }
-    }
-}
+锘縰sing 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);
+        }
+    }
+}

--
Gitblit v1.9.3