158 lines
5.1 KiB
C#
158 lines
5.1 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using Autodesk.Revit;
|
||
using Autodesk.Revit.DB;
|
||
|
||
namespace RevitLess
|
||
{
|
||
/// <summary>
|
||
/// Revit环境
|
||
/// </summary>
|
||
internal class RevitContext
|
||
{
|
||
/// <summary>
|
||
/// 使用此构造方法前需要调用 RevitContext.AddEnv();
|
||
/// </summary>
|
||
static RevitContext()
|
||
{
|
||
OnAssemblyResolve();
|
||
}
|
||
|
||
public event EventHandler<Product> InitFinished;
|
||
|
||
//用于判断当前类的实例是否存在
|
||
private static readonly object lockobj = new();
|
||
|
||
private static RevitContext _instance;
|
||
|
||
/// <summary>
|
||
/// revit程序目录
|
||
/// </summary>
|
||
public static string RevitAppPath;
|
||
// 此路径为动态反射搜索路径 、 此路径可为任意路径(只要路径下有RevitNET 所需依赖项即可,完整依赖项可在 Naviswork 2016 下面找到)
|
||
|
||
private Product revitProduct;
|
||
|
||
/// <summary>
|
||
/// Revit应用程序实例
|
||
/// </summary>
|
||
public Autodesk.Revit.ApplicationServices.Application Application => revitProduct?.Application;
|
||
|
||
/// <summary>
|
||
/// 自身静态类,单例,内存中只有一份
|
||
/// </summary>
|
||
public static RevitContext Instance
|
||
{
|
||
get
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
lock (lockobj)
|
||
{
|
||
_instance ??= new RevitContext();
|
||
}
|
||
}
|
||
|
||
return _instance;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打开REVIT文件时的设置
|
||
/// </summary>
|
||
public OpenOptions OpenOptions { get; set; }
|
||
|
||
public void Exit()
|
||
{
|
||
revitProduct?.Exit();
|
||
}
|
||
|
||
public void InitRevitAppAndOpenFile()
|
||
{
|
||
InitFinished?.Invoke(this, revitProduct);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加revit安装路径到环境变量以便加载相应的DLL
|
||
/// </summary>
|
||
/// <param name="revitPath">添加revit安装路径</param>
|
||
public static void OnAssemblyResolve(string revitPath = null)
|
||
{
|
||
if (revitPath != null)
|
||
{
|
||
RevitAppPath = revitPath;
|
||
}
|
||
|
||
AddEnvironmentPaths(RevitAppPath);
|
||
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打开文件
|
||
/// </summary>
|
||
/// <param name="filename"></param>
|
||
/// <returns></returns>
|
||
public Document OpenFile(string filename)
|
||
{
|
||
if (File.Exists(filename))
|
||
{
|
||
ModelPath model = new FilePath(filename);
|
||
return revitProduct.Application.OpenDocumentFile(model, OpenOptions);
|
||
}
|
||
|
||
throw new FileNotFoundException($"{filename}不存在。");
|
||
}
|
||
|
||
public void Run()
|
||
{
|
||
OpenOptions = new OpenOptions
|
||
{
|
||
Audit = true,
|
||
AllowOpeningLocalByWrongUser = false,
|
||
DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets //从中心模型分离
|
||
};
|
||
revitProduct = Product.GetInstalledProduct();
|
||
var clientApplicationId = new ClientApplicationId(Guid.NewGuid(), "RevitContext", "BIM");
|
||
revitProduct.SetPreferredLanguage(Autodesk.Revit.ApplicationServices.LanguageType.Chinese_Simplified);
|
||
revitProduct.Initialize_ForAutodeskInternalUseOnly(clientApplicationId, "I am authorized by Autodesk to use this UI-less functionality.");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加环境变量
|
||
/// </summary>
|
||
/// <param name="paths">revit安装路径</param>
|
||
private static void AddEnvironmentPaths(params string[] paths)
|
||
{
|
||
string[] first =
|
||
{
|
||
Environment.GetEnvironmentVariable("PATH") ?? string.Empty
|
||
};
|
||
var value = string.Join(Path.PathSeparator.ToString(), first.Concat(paths));
|
||
Environment.SetEnvironmentVariable("PATH", value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 动态加载revit相关的dll
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="args"></param>
|
||
/// <returns></returns>
|
||
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
||
{
|
||
var assemblyName = new AssemblyName(args.Name);
|
||
var text = $"{Path.Combine(RevitAppPath, assemblyName.Name)}.dll";
|
||
////$字符将{0}占位直接用对象相关字段替换
|
||
//var file = string.Format("{0}.dll", System.IO.Path.Combine(item, assemblyName.Name));
|
||
|
||
if (File.Exists(text))
|
||
{
|
||
//Console.WriteLine($"加载Revit Dll路径:{text}");
|
||
return Assembly.LoadFrom(text);
|
||
}
|
||
|
||
return args.RequestingAssembly;
|
||
}
|
||
}
|
||
} |