TestExport.cs 3.0 KB

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