123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
-
- /* ==============================================================================
- * 功能描述:启动revit时执行的Application
- * 创 建 者:SAGACLOUD
- * 创建日期:2017/8/30
- * ==============================================================================*/
- using System;
- using System.IO;
- using System.Windows;
- using Autodesk.Revit.Attributes;
- using Autodesk.Revit.DB.Events;
- using Autodesk.Revit.UI;
- using Autodesk.Revit.DB;
- using Autodesk.Revit.UI.Events;
- using SAGA.DotNetUtils;
- using SAGA.DotNetUtils.Extend;
- using SAGA.DotNetUtils.Logger;
- using SAGA.RevitMenu.Configuration;
- using SAGA.RevitMenu.Dockpane;
- using SAGA.RevitUtils.ErrorSupports;
- using SAGA.RevitUtils.Extends;
- using SAGA.RevitUtils.Log;
- using System.Threading;
- using SAGA.RevitUtils;
- namespace SAGA.RevitMenu
- {
- [Transaction(TransactionMode.Manual), Regeneration(RegenerationOption.Manual)]
- public class MenuApp : ExternalApplication
- {
- public override Result OnShutdown(UIControlledApplication application)
- {
- ErrorHandlers.Stop();
- RevitEventsBingding.RemoveEvents(application);
- base.OnShutdown(application);
- TszDocChanged -= App_DocumentChanged;
- application.ControlledApplication.DocumentClosing -= ControlledApplication_DocumentClosing;
- return Result.Succeeded;
- }
- public override Result OnStartup(UIControlledApplication application)
- {
- ErrorHandlers.Start();
- base.OnStartup(application);
-
- StartSetting.RegiterPane(application);
- //使用快捷键自动创建空间,需要先设置快捷键
- //ScreenMenuKeyBoardShortCut.SetKeyboardShortcuts(application.ControlledApplication.VersionName);
- //记录空间设备修改;Chang时仅保存到临时文件中;Save时保存到log文件中;关闭是清除临时文件
- TszDocChanged += App_DocumentChanged;
- application.ControlledApplication.DocumentClosing += ControlledApplication_DocumentClosing;
- RevitEventsBingding.AddEvents(application);
- //将加载菜单逻辑后移
- if (!MenuConfig.LoadMenus(application))
- {
- return Result.Failed;
- }
- //删除Addin文件
- MenuConfig.ClearAddInFile(application);
- return Result.Succeeded;
- }
- private void ControlledApplication_DocumentClosing(object sender, DocumentClosingEventArgs e)
- {
- DocumentChangedLog.DeleteTempLog(e.Document.PathName);
- }
- /// <summary>
- /// 记录操作日志
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void App_DocumentChanged(object sender, Autodesk.Revit.DB.Events.DocumentChangedEventArgs e)
- {
- var doc = e.GetDocument();
- if (doc.IsFamilyDocument) return;
- var pathName = doc.PathName;
- //楼层文件名称,无后缀
- var docName = pathName.GetFileName();
-
- Func<ElementId, string> getTypeName = (elementId) => doc.GetElement(elementId).GetType().Name;
- Func<ElementId, string> getFamilyName = (elementId) => doc.GetElement(elementId).GetFamily()?.Name;
- Func<ElementId, string> getCategory = (elementId) => doc.GetElement(elementId).GetCategory().ToString();
- //删除
- var deletes = e.GetDeletedElementIds();
- if (deletes.Count > 0)
- {
- foreach (var elementId in deletes)
- {
- DocumentChangedLogMode mode = new DocumentChangedLogMode();
- mode.DataTime = DateTime.Now.ToString();
- mode.DocPath = pathName;
- mode.DocName = docName;
- mode.Operator = DocumentChangedOperator.Delete.ToString();
- mode.Id = elementId.ToString();
-
- DocumentChangedLog.Log(mode.DocPath, mode.ToJson());
- }
- }
- //增加
- var adds = e.GetAddedElementIds();
- if (adds.Count > 0)
- {
- foreach (var elementId in adds)
- {
- //if(!IsWatchType(doc,elementId))continue;
- DocumentChangedLogMode mode = new DocumentChangedLogMode();
- mode.DataTime = DateTime.Now.ToString();
- mode.DocPath = pathName;
- mode.DocName = docName;
- mode.Operator = DocumentChangedOperator.Add.ToString();
- mode.Id = elementId.ToString();
- mode.Type = getTypeName(elementId);
- mode.Family = getFamilyName(elementId);
- mode.Category = getCategory(elementId);
- DocumentChangedLog.Log(mode.DocPath, mode.ToJson());
- }
- }
- //修改
- var modifieds = e.GetModifiedElementIds();
- if (modifieds.Count > 0)
- {
- foreach (var elementId in modifieds)
- {
- //if (!IsWatchType(doc, elementId)) continue;
- DocumentChangedLogMode mode = new DocumentChangedLogMode();
- mode.DataTime = DateTime.Now.ToString();
- mode.DocPath = pathName;
- mode.DocName = docName;
- mode.Operator = DocumentChangedOperator.Modified.ToString();
- mode.Id = elementId.ToString();
- mode.Type = getTypeName(elementId);
- mode.Family = getFamilyName(elementId);
- mode.Category = getCategory(elementId);
- DocumentChangedLog.Log(mode.DocPath, mode.ToJson());
- }
- }
- }
- /// <summary>
- /// 是否为需要监视的类型
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="elementId"></param>
- /// <returns></returns>
- private bool IsWatchType(Document doc, ElementId elementId)
- {
- var type = doc.GetElement(elementId).GetType().Name;
- //族类型命名规则具有特殊性,不做有判定依据
- var family = doc.GetElement(elementId).GetFamily()?.Name;
- bool result = false;
- switch (type)
- {
- case "SpaceTag":
- result = true;
- break;
- case "Space":
- result = true;
- break;
- case "FamilyInstance":
- result = true;
- break;
- }
- return result;
- }
-
- }
- }
|