using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Autodesk.Revit;
using Autodesk.Revit.DB;
namespace RevitLess
{
///
/// Revit环境
///
internal class RevitContext
{
///
/// 使用此构造方法前需要调用 RevitContext.AddEnv();
///
static RevitContext()
{
OnAssemblyResolve();
}
public event EventHandler InitFinished;
//用于判断当前类的实例是否存在
private static readonly object lockobj = new();
private static RevitContext _instance;
///
/// revit程序目录
///
public static string RevitAppPath;
// 此路径为动态反射搜索路径 、 此路径可为任意路径(只要路径下有RevitNET 所需依赖项即可,完整依赖项可在 Naviswork 2016 下面找到)
private Product revitProduct;
///
/// Revit应用程序实例
///
public Autodesk.Revit.ApplicationServices.Application Application => revitProduct?.Application;
///
/// 自身静态类,单例,内存中只有一份
///
public static RevitContext Instance
{
get
{
if (_instance == null)
{
lock (lockobj)
{
_instance ??= new RevitContext();
}
}
return _instance;
}
}
///
/// 打开REVIT文件时的设置
///
public OpenOptions OpenOptions { get; set; }
public void Exit()
{
revitProduct?.Exit();
}
public void InitRevitAppAndOpenFile()
{
InitFinished?.Invoke(this, revitProduct);
}
///
/// 添加revit安装路径到环境变量以便加载相应的DLL
///
/// 添加revit安装路径
public static void OnAssemblyResolve(string revitPath = null)
{
if (revitPath != null)
{
RevitAppPath = revitPath;
}
AddEnvironmentPaths(RevitAppPath);
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
///
/// 打开文件
///
///
///
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.");
}
///
/// 添加环境变量
///
/// revit安装路径
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);
}
///
/// 动态加载revit相关的dll
///
///
///
///
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;
}
}
}