DwgExportUtils.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:DwgExportUtils
  3. * 作者:xulisong
  4. * 创建时间: 2019/7/25 9:59:03
  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.DB;
  14. namespace LRH.Tool
  15. {
  16. public static class DwgExportUtils
  17. {
  18. /// <summary>
  19. /// 将视图导出Dwg格式
  20. /// </summary>
  21. /// <param name="view">使用的view</param>
  22. /// <param name="exportFileName">导出的位置</param>
  23. /// <param name="visibleCategories">设置显示的category,如果传入null,则不对category进行限制</param>
  24. /// <returns></returns>
  25. public static bool ExportDwg(View view, string exportFileName, List<Category> visibleCategories)
  26. {
  27. List<Category> useCategories = visibleCategories;
  28. string folder = Path.GetDirectoryName(exportFileName);
  29. string fileName = Path.GetFileName(exportFileName);
  30. DWGExportOptions dwgExportOptions1 = new DWGExportOptions();
  31. dwgExportOptions1.Colors = ExportColorMode.IndexColors;
  32. dwgExportOptions1.LineScaling = LineScaling.PaperSpace;
  33. dwgExportOptions1.TargetUnit = ExportUnit.Millimeter;
  34. dwgExportOptions1.FileVersion = ACADVersion.R2013;
  35. dwgExportOptions1.SharedCoords = false;
  36. dwgExportOptions1.PropOverrides = PropOverrideMode.ByEntity;
  37. dwgExportOptions1.LayerMapping = "AIA";
  38. View useView = view;
  39. ElementId viewId = view.Id;
  40. var document = useView.Document;
  41. if (useCategories != null)
  42. {
  43. var categories = useView.Document.Settings.Categories;
  44. foreach (Category category in categories)
  45. {
  46. useView.SetCategoryHidden(category.Id, true);
  47. }
  48. foreach (Category useCategory in useCategories)
  49. {
  50. useView.SetCategoryHidden(useCategory.Id, false);
  51. }
  52. }
  53. return document.Export(folder, fileName, new List<ElementId>() { viewId }, dwgExportOptions1);
  54. }
  55. }
  56. }