RevitCoreContext.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* ==============================================================================
  2. * 功能描述:RevitCoreContext
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/6/27 11:10:20
  5. * ==============================================================================*/
  6. using Autodesk.Revit;
  7. using Autodesk.Revit.ApplicationServices;
  8. using Autodesk.Revit.DB;
  9. using SAGA.DotNetUtils.Revit;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Reflection;
  15. namespace ExportStart
  16. {
  17. public class RevitCoreContext
  18. {
  19. // 此路径为动态反射搜索路径 、 此路径可为任意路径(只要路径下有RevitNET 所需依赖项即可,完整依赖项可在 Naviswork 2016 下面找到)
  20. private static readonly string[] Searchs = GetRevitProductPath();
  21. //new[] { @"E:\Program Files\Autodesk\Revit 2017" };
  22. //new []{ @"E:\Program Files\Autodesk\Revit 2018" };
  23. /// <summary>
  24. /// 选择版本时,预先将版本信息写在RevitFileVision.txt文件中
  25. /// </summary>
  26. /// <returns></returns>
  27. static string[] GetRevitProductPath()
  28. {
  29. //保存版本号
  30. var vision = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, StarterConst.RevitFileVisionFile));
  31. var revitVision = $"Revit {vision}";
  32. return RevitProductUtility.GetAllInstalledRevitProducts().Where(t => Directory.GetParent(t).Name == revitVision).ToArray();
  33. }
  34. static readonly object lockobj = new object();
  35. static RevitCoreContext _instance;
  36. private Product _product;
  37. public Application Application { get => _product.Application; }
  38. public static RevitCoreContext Instance
  39. {
  40. get
  41. {
  42. if (_instance == null)
  43. {
  44. lock (lockobj)
  45. {
  46. if (_instance == null)
  47. {
  48. _instance = new RevitCoreContext();
  49. }
  50. }
  51. }
  52. return _instance;
  53. }
  54. }
  55. static RevitCoreContext()
  56. {
  57. AddEnvironmentPaths(Searchs);
  58. AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
  59. }
  60. public void Run()
  61. {
  62. _product = Product.GetInstalledProduct();
  63. var clientId = new ClientApplicationId(Guid.NewGuid(), "DotNet", "BIMAPI");
  64. // I am authorized by Autodesk to use this UI-less functionality. 必须是此字符串。 Autodesk 规定的.
  65. _product.Init(clientId, "I am authorized by Autodesk to use this UI-less functionality.");
  66. }
  67. public void Stop()
  68. {
  69. _product?.Exit();
  70. }
  71. static void AddEnvironmentPaths(params string[] paths)
  72. {
  73. var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
  74. //加在最前面,
  75. var newPath = string.Join(System.IO.Path.PathSeparator.ToString(), paths.Concat(path));
  76. Environment.SetEnvironmentVariable("PATH", newPath);
  77. }
  78. private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
  79. {
  80. var assemblyName = new AssemblyName(args.Name);
  81. //File.AppendAllText(@"E:\startlog.txt","args:"+ args.Name + "\r\n");
  82. foreach (var item in Searchs)
  83. {
  84. var file = string.Format("{0}.dll", System.IO.Path.Combine(item, assemblyName.Name));
  85. if (File.Exists(file))
  86. {
  87. AddAssemblys(assemblyName.Name);
  88. //File.Copy(file,Path.Combine(@"D:\Revit\RevitExport\JBIM\OutputDll\2018",assemblyName.Name+".dll"),true);
  89. return Assembly.LoadFile(file);
  90. }
  91. }
  92. return args.RequestingAssembly;
  93. }
  94. public static List<string> NeedAssemblys = new List<string>();
  95. public static void AddAssemblys(string str)
  96. {
  97. if(!NeedAssemblys.Contains(str))
  98. NeedAssemblys.Add(str);
  99. }
  100. }
  101. }