RevitCoreContext.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* ==============================================================================
  2. * 功能描述:RevitCoreContext
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/6/27 11:10:20
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net.Mime;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using Autodesk.Revit;
  15. using Autodesk.Revit.ApplicationServices;
  16. using Autodesk.Revit.DB;
  17. using Autodesk.RevitAddIns;
  18. namespace ExportStart
  19. {
  20. public class RevitCoreContext
  21. {
  22. // 此路径为动态反射搜索路径 、 此路径可为任意路径(只要路径下有RevitNET 所需依赖项即可,完整依赖项可在 Naviswork 2016 下面找到)
  23. static readonly string[] Searchs = RevitUtils.GetSearchPath().ToArray();// RevitProductUtility.GetAllInstalledRevitProducts().Where(t => t.Name == "Revit 2017").Select(x => x.InstallLocation).ToArray();
  24. static readonly object lockobj = new object();
  25. static RevitCoreContext _instance;
  26. private Product _product;
  27. public Application Application { get => _product.Application; }
  28. public static RevitCoreContext Instance
  29. {
  30. get
  31. {
  32. if (_instance == null)
  33. {
  34. lock (lockobj)
  35. {
  36. if (_instance == null)
  37. {
  38. _instance = new RevitCoreContext();
  39. }
  40. }
  41. }
  42. return _instance;
  43. }
  44. }
  45. static RevitCoreContext()
  46. {
  47. AddEnvironmentPaths(Searchs);
  48. AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
  49. }
  50. public void Run()
  51. {
  52. _product = Product.GetInstalledProduct();
  53. var clientId = new ClientApplicationId(Guid.NewGuid(), "DotNet", "BIMAPI");
  54. // I am authorized by Autodesk to use this UI-less functionality. 必须是此字符串。 Autodesk 规定的.
  55. _product.Init(clientId, "I am authorized by Autodesk to use this UI-less functionality.");
  56. }
  57. public void Stop()
  58. {
  59. _product?.Exit();
  60. }
  61. static void AddEnvironmentPaths(params string[] paths)
  62. {
  63. var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
  64. //加在最前面,
  65. var newPath = string.Join(System.IO.Path.PathSeparator.ToString(), paths.Concat(path));
  66. Environment.SetEnvironmentVariable("PATH", newPath);
  67. }
  68. private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
  69. {
  70. var assemblyName = new AssemblyName(args.Name);
  71. foreach (var item in Searchs)
  72. {
  73. var file = string.Format("{0}.dll", System.IO.Path.Combine(item, assemblyName.Name));
  74. if (File.Exists(file))
  75. {
  76. //NeedAssemblys.Add(assemblyName.Name);
  77. return Assembly.LoadFrom(file);
  78. }
  79. }
  80. return args.RequestingAssembly;
  81. }
  82. public static List<string> NeedAssemblys = new List<string>();
  83. public static void PrintAllAssemblys()
  84. {
  85. string str = string.Join("\r\n", NeedAssemblys);
  86. Console.WriteLine(str);
  87. }
  88. }
  89. }