TestExport.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. elements.AddRange(elements2);
  32. var wrappers = elements.Select(e => new ElementWrapper(e)).ToList();
  33. ExportInstance report = new ExportInstance(wrappers);
  34. JBimParseContext context = new JBimParseContext(ParseCore.GetUseParsers());
  35. var dd = false;
  36. context.Parser.Parse(report);
  37. var result = context.Serialize();
  38. string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
  39. string path = Path.Combine(@"D:\", $"{fileName}.json");
  40. File.WriteAllText(path, result);
  41. }
  42. }
  43. /// <summary>
  44. /// 测试提取数据
  45. /// </summary>
  46. [Transaction(TransactionMode.Manual)]
  47. [Regeneration(RegenerationOption.Manual)]
  48. public class PickDataCommand : IExternalCommand, IExternalCommandAvailability
  49. {
  50. public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  51. {
  52. try
  53. {
  54. TestExport.Document = commandData.Application.ActiveUIDocument.Document;
  55. TestExport.Export();
  56. TaskDialog dialog = new TaskDialog("导出");
  57. dialog.MainInstruction ="导出成功";
  58. dialog.Show();
  59. }
  60. catch (Exception e)
  61. {
  62. TaskDialog dialog = new TaskDialog("导出");
  63. dialog.MainInstruction =e.Message+"\n\t"+ e.StackTrace.ToString();
  64. dialog.Show();
  65. return Result.Cancelled;
  66. }
  67. return Result.Succeeded;
  68. }
  69. /// <summary>
  70. /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
  71. /// when a document is open.
  72. /// </summary>
  73. public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
  74. {
  75. return false;
  76. }
  77. }
  78. }