123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- /* ==============================================================================
- * 功能描述:DocExtend
- * 创 建 者:Garrett
- * 创建日期:2018/6/11 16:00:53
- * ==============================================================================*/
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using Autodesk.Revit.DB;
- using SAGA.DotNetUtils;
- using SAGA.RevitUtils.Extends;
- namespace MBIUtility.Extend
- {
- /// <summary>
- /// DocExtend
- /// </summary>
- public static class DocExtend
- {
- public static readonly string MBIResourcePath = Path.Combine(AppBaseInfo.AppRunPath, "MBIResource");
- public static readonly string EmptyFilePath = Path.Combine(MBIResourcePath, "EmptyFile\\EmptyProject.rvt");
- /// <summary>
- /// 关闭文档
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static bool CloseDoc(string path)
- {
- if (path == null || !File.Exists(path)) return false;
- bool result = true;
- try
- {
- //打开空文件并激活
- var placeholderFile = EmptyFilePath;
- var uiApp = ExternalDataWrapper.Current.UiApp;
- var docs = uiApp.Application.Documents;
- if (docs.IsEmpty) return result;
- if (uiApp.ActiveUIDocument != null && uiApp.ActiveUIDocument.Document.PathName == path)
- {
- var docCur = uiApp.ActiveUIDocument;
- uiApp.OpenAndActivateDocument(placeholderFile);
- result = docCur.SaveAndClose();
- }
- else
- {
- foreach (Document doc in docs)
- {
- if (doc.PathName == path)
- {
- //版本升级,保存
- var verionName = doc.Application.VersionName;
- var info = BasicFileInfo.Extract(doc.PathName);
- var savedInVersion = info.SavedInVersion; //<==Autodesk Revit 2014
- result = doc.Close(verionName != savedInVersion || doc.IsModified);
- }
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- return result;
- }
- /// <summary>
- /// 关闭文档
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static bool CloseDoc(this Document doc)
- {
- return CloseDoc(doc?.PathName);
- }
- /// <summary>
- /// 简单关闭模型不保存
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static bool CloseDocSimple(this Document doc)
- {
- bool result = true;
- var basePath = doc.PathName;
- var uiApp = ExternalDataWrapper.Current.UiApp;
- if (uiApp.ActiveUIDocument != null && uiApp.ActiveUIDocument.Document.PathName == basePath)
- {
- result= false;
- }
- else
- {
- result= doc.Close();
- }
- return result;
- }
- /// <summary>
- /// 由code获取FamilySymbol
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="code"></param>
- /// <returns></returns>
- public static FamilySymbol GetFamilySymbol(this Document doc, string code)
- {
- Func<string, bool> filterFamily = (t) =>
- {
- return Regex.IsMatch(t, $@"{code}\s*-\s*\S*");
- };
- List<Family> listFamily = doc.GetFamilys();
- var fa = listFamily.FirstOrDefault(t => filterFamily(t.Name));
- List<FamilySymbol> symbolList = fa?.GetFamilySymbols();
- var fs= symbolList?.FirstOrDefault();
- if (fs!=null&&!fs.IsActive)
- fs.Activate();
- return fs;
- }
- }
- }
|