TestExport.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. System.Diagnostics.Process.Start("notepad.exe", path);
  45. }
  46. }
  47. /// <summary>
  48. /// 测试提取数据
  49. /// </summary>
  50. [Transaction(TransactionMode.Manual)]
  51. [Regeneration(RegenerationOption.Manual)]
  52. public class PickDataCommand : IExternalCommand, IExternalCommandAvailability
  53. {
  54. public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  55. {
  56. try
  57. {
  58. TestExport.Document = commandData.Application.ActiveUIDocument.Document;
  59. TestExport.Export();
  60. TaskDialog dialog = new TaskDialog("导出");
  61. dialog.MainInstruction ="导出成功";
  62. dialog.Show();
  63. }
  64. catch (Exception e)
  65. {
  66. TaskDialog dialog = new TaskDialog("导出");
  67. dialog.MainInstruction =e.Message+"\n\t"+ e.StackTrace.ToString();
  68. dialog.Show();
  69. return Result.Cancelled;
  70. }
  71. return Result.Succeeded;
  72. }
  73. /// <summary>
  74. /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
  75. /// when a document is open.
  76. /// </summary>
  77. public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
  78. {
  79. return false;
  80. }
  81. }
  82. }