RevitCoreContext.cs 3.3 KB

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