TestExport.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:TestExport
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/18 15:43:54
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using Autodesk.Revit.Attributes;
  14. using Autodesk.Revit.DB;
  15. using Autodesk.Revit.DB.Mechanical;
  16. using Autodesk.Revit.DB.Plumbing;
  17. using Autodesk.Revit.UI;
  18. using RevitExport;
  19. using RevitToJBim.ComponentParse;
  20. using SAGA.RevitUtils;
  21. using SAGA.RevitUtils.Extends;
  22. namespace RevitToJBim
  23. {
  24. public class TestExport
  25. {
  26. public static Document Document { get; set; }
  27. public static void Export()
  28. {
  29. var elements = Document.GetElements(typeof(Pipe));
  30. var elements2 = Document.GetElements(typeof(SpatialElement)).OfType<Space>().ToList();
  31. var elements3 = Document.GetElements(typeof(Duct)).OfType<Duct>().ToList();
  32. elements.AddRange(elements2);
  33. elements.AddRange(elements3);
  34. var wrappers = elements.Select(e => new ElementWrapper(e)).ToList();
  35. ExportInstance report = new ExportInstance(wrappers);
  36. JBimParseContext context = new JBimParseContext(ParseCore.GetUseParsers());
  37. var dd = false;
  38. context.Parser.Parse(report);
  39. var result = context.Serialize();
  40. string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
  41. string path = Path.Combine(@"D:\", $"{fileName}.json");
  42. File.WriteAllText(path, result);
  43. }
  44. }
  45. /// <summary>
  46. /// 测试提取数据
  47. /// </summary>
  48. [Transaction(TransactionMode.Manual)]
  49. [Regeneration(RegenerationOption.Manual)]
  50. public class PickDataCommand : IExternalCommand, IExternalCommandAvailability
  51. {
  52. public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  53. {
  54. try
  55. {
  56. TestExport.Document = commandData.Application.ActiveUIDocument.Document;
  57. TestExport.Export();
  58. TaskDialog dialog = new TaskDialog("导出");
  59. dialog.MainInstruction ="导出成功";
  60. dialog.Show();
  61. }
  62. catch (Exception e)
  63. {
  64. TaskDialog dialog = new TaskDialog("导出");
  65. dialog.MainInstruction =e.Message+"\n\t"+ e.StackTrace.ToString();
  66. dialog.Show();
  67. return Result.Cancelled;
  68. }
  69. return Result.Succeeded;
  70. }
  71. /// <summary>
  72. /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
  73. /// when a document is open.
  74. /// </summary>
  75. public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
  76. {
  77. return false;
  78. }
  79. }
  80. }