TextNodeExtension.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Autodesk.Revit.DB;
  2. using FWindSoft.Revit.Common;
  3. using FWindSoft.SystemExtensions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace FWindSoft.Revit
  10. {
  11. public static class TextNodeExtension
  12. {
  13. /*
  14. * 操作TextNode时,要注意考虑缩放系数
  15. */
  16. /// <summary>
  17. /// 视图中的文字
  18. /// </summary>
  19. /// <param name="view"></param>
  20. /// <returns></returns>
  21. public static List<TextNote> GetTextNotes(this View view)
  22. {
  23. return null;// view.GetViewElements(ElementFilters.TextNote).ToListExt<TextNote>();
  24. }
  25. /// <summary>
  26. /// 项目中的文字
  27. /// </summary>
  28. /// <param name="doc"></param>
  29. /// <returns></returns>
  30. public static List<TextNote> GetTextNotes(this Document doc)
  31. {
  32. return doc.GetElements<TextNote>();
  33. }
  34. /// <summary>
  35. /// 获取项目中NoteType全集
  36. /// </summary>
  37. /// <param name="doc"></param>
  38. /// <returns></returns>
  39. public static List<TextNoteType> GetTextNoteTypes(this Document doc)
  40. {
  41. return doc.GetElements<TextNoteType>();
  42. }
  43. /// <summary>
  44. /// 获取系统默认使用NodtType
  45. /// </summary>
  46. /// <param name="doc"></param>
  47. /// <returns></returns>
  48. public static TextNoteType GetDefaultNodeType(this Document doc)
  49. {
  50. return doc.GetTextNodeType("仿宋", 3, 0.7);
  51. }
  52. /// <summary>
  53. /// 获取TextNodeType
  54. /// </summary>
  55. /// <param name="doc"></param>
  56. /// <param name="font"></param>
  57. /// <param name="fontHeight"></param>
  58. /// <param name="widthRatio"></param>
  59. /// <returns></returns>
  60. public static TextNoteType GetTextNodeType(this Document doc, string font, double fontHeight,
  61. double widthRatio)
  62. {
  63. List<TextNoteType> types = doc.GetTextNoteTypes();
  64. TextNoteType useType = null;
  65. var typeName = string.Format("{0} {1}*{2}", font, fontHeight, widthRatio );
  66. if (types.Count > 0)
  67. {
  68. typeName = RevitTools.RemoveForbiddenChars(typeName);
  69. useType = types.FirstOrDefault(t => t.Name == typeName);
  70. if (useType == null)
  71. {
  72. useType = types[0].DuplicateT<TextNoteType>(typeName);
  73. useType.SetParameter(BuiltInParameter.TEXT_WIDTH_SCALE, widthRatio);
  74. useType.SetParameter(BuiltInParameter.TEXT_SIZE, fontHeight.MmToFt());
  75. useType.SetParameter(BuiltInParameter.TEXT_FONT, font);
  76. useType.SetParameter(BuiltInParameter.TEXT_BACKGROUND, 1);
  77. useType.SetParameter(BuiltInParameter.LEADER_OFFSET_SHEET, 0.0);
  78. }
  79. }
  80. return useType;
  81. }
  82. /// <summary>
  83. /// 试图创建新的TextNode
  84. /// </summary>
  85. /// <param name="view"></param>
  86. /// <param name="typeId"></param>
  87. /// <param name="position"></param>
  88. /// <param name="text"></param>
  89. /// <param name="angle"></param>
  90. /// <returns></returns>
  91. public static TextNote NewTextNote(this View view, ElementId typeId, XYZ position, string text,
  92. double angle = 0)
  93. {
  94. TextNoteOptions options = new TextNoteOptions
  95. {
  96. TypeId = typeId,
  97. Rotation = angle
  98. };
  99. return TextNote.Create(view.Document, view.Id, position, text, options);
  100. }
  101. /// <summary>
  102. /// 试图创建新的TextNode
  103. /// </summary>
  104. /// <param name="view"></param>
  105. /// <param name="typeId"></param>
  106. /// <param name="position"></param>
  107. /// <param name="text"></param>
  108. /// <param name="angle"></param>
  109. /// <returns></returns>
  110. public static TextNote NewTextNote(this View view, ElementId typeId, XYZ position, string text, double width,
  111. TextAlignFlags textAlign,double angle = 0)
  112. {
  113. TextNoteOptions options = new TextNoteOptions
  114. {
  115. TypeId = typeId,
  116. Rotation = angle
  117. };
  118. TextNote note;
  119. if (width.IsZero())
  120. {
  121. note = TextNote.Create(view.Document, view.Id, position, text, options);
  122. }
  123. else
  124. {
  125. note = TextNote.Create(view.Document, view.Id, position, width, text, options);
  126. }
  127. note?.SetParameter(BuiltInParameter.TEXT_ALIGN_HORZ, (int)textAlign);
  128. return note;
  129. }
  130. }
  131. }