此篇文章仅是自己的开发经验分享,不具备官方参考价值,如有不足,欢迎批评指正
开发目的:
创建一个带箭头的文字注释 字体为新宋体,大小2.5mm,宽度系数0.7,箭头为30度实心箭头 like this 

分析开发目标:
1、分析在Revit里面创建目标文字注释的步骤
第一步,Revit的菜单目录,“注释”——>“文字”,创建好以后发现,Revit创建文字注释默认没有箭头

第二步,选中文字注释,菜单栏出现如图右上角的图标,选择想要创建的样式和方向(可以试一下四个的效果,分别是左直线,右直线,左弧线,右弧线,这张图里面弧线是灰色的,若要使其正常,在上图左侧的属性栏里面勾选上“弧引线”即可)。由此步骤可以得知,在Revit二次开发的过程当中,引线也是可以选择是否创建,选择创建的方向(左还是右),选择创建样式(直线还是弧线),还可以改变位置,如图引线有两个点,查阅资料可得,这两点分别是Leader.Elbow(弯头点),和Leader.End(端点,即箭头上的那个点)

2、分析创建好了的文字注释的属性
第一步:选中创建好的文字注释,“编辑属性”

第二步:分析属性(一般要求会修改颜色,文字字体,宽度系数),我的开发目标还要修改引线箭头

开发:
我们可以查到创建文字注释在新版本中做了修改,以前是用文档对象进行创建,图片截自
https://blog.csdn.net/weixin_40626630/article/details/84108269

新版本:现在是用TextNote对象进行创建

那么我们回顾一下开发目的(创建一个带箭头的文字注释,字体为新宋体,大小2.5mm,宽度系数0.7,箭头为30度实心箭头)

代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | using Autodesk.Revit.Attributes; using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; namespace CreateTextNode { [Regeneration(RegenerationOption.Manual)] [Transaction(TransactionMode.Manual)] public class CreateTextNode : IExternalCommand { public Result Execute(ExternalCommandData cmdData,ref string msg,ElementSet elems) { UIApplication uiapp = cmdData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Document doc = uidoc.Document; View view = doc.ActiveView; //若此视图没有工作平面则新建工作平面(比如二次开发在剖面是需要新建工作平面的) if (view.SketchPlane == null) { Transaction ts1 = new Transaction(doc, "新建工作平面"); ts1.Start(); Plane plane = Plane.CreateByNormalAndOrigin(doc.ActiveView.ViewDirection, doc.ActiveView.Origin); SketchPlane sp = SketchPlane.Create(doc, plane); doc.ActiveView.SketchPlane = sp; view = doc.ActiveView; ts1.Commit(); } Selection S1 = uidoc.Selection; XYZ textNodeLocationPt = null; try { textNodeLocationPt = S1.PickPoint("请选择文字注释创建位置"); } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { } if (textNodeLocationPt != null) { //创建文字注释Option TextNoteOptions options = new TextNoteOptions(); options.HorizontalAlignment = HorizontalTextAlignment.Left; //文字水平对齐方式 options.TypeId = doc.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType); options.VerticalAlignment = VerticalTextAlignment.Top;//文字垂直对齐方式 double dWidth = 0.07;//文字宽度 //箭头端点 XYZ leaderEnd = new XYZ(textNodeLocationPt.X -1500/304.8, textNodeLocationPt.Y+1500/304.8, textNodeLocationPt.Z); using (Transaction tran = new Transaction(doc, "Create Textnote")) { tran.Start(); //创建文字注释 TextNote note = TextNote.Create(doc, doc.ActiveView.Id, textNodeLocationPt, dWidth, "净高控制线", options); note.AddLeader(TextNoteLeaderTypes.TNLT_STRAIGHT_L); //引线方向,一共四种,左直,右直,左弧,右弧 note.LeaderLeftAttachment = LeaderAtachement.TopLine;//引线的位置,top代表引线位置在第一行文本的位置 IList<Leader> leaderList = note.GetLeaders(); foreach (Leader leader in leaderList) { leader.End = leaderEnd;//给箭头端点设置值 XYZ pointElbow = new XYZ(leaderEnd.X, leader.Anchor.Y, leaderEnd.Z); leader.Elbow = pointElbow;//给箭头弯头点设置值 } //创建文字注释的族类型,族类型名称“宋体_2.5mm”,首先判断当前族类型是不是,不是就判断有没有该族类型,有就用,没有就创建了再用 if (note.TextNoteType.Name != "宋体_2.5mm") { Parameter familyType = (note as Element).get_Parameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM); if (familyType != null && familyType.StorageType == StorageType.ElementId && familyType.AsElementId().IntegerValue >= 0) { Element elem_2 = doc.GetElement(familyType.AsElementId()); TextNoteType type = elem_2 as TextNoteType; if (type != null) { ElementId elementId = null; bool symbolExist = false; FilteredElementCollector collectorSymbol = new FilteredElementCollector(doc); IList<Element> textSymbols = collectorSymbol.OfClass(typeof(TextNoteType)).ToElements(); foreach (var item in collectorSymbol) { if (item.Name == "宋体_2.5mm") { symbolExist = true; elementId = item.Id; } } if (symbolExist) { (note as TextNote).ChangeTypeId(elementId);//依据ID改变族类型 } else { TextNoteType duplicatedtextType = null; duplicatedtextType = type.Duplicate("宋体_2.5mm") as TextNoteType; string textNode = duplicatedtextType.LookupParameter("文字大小").AsString() + duplicatedtextType.LookupParameter("文字大小").AsValueString(); string dut_gerneral = duplicatedtextType.LookupParameter("宽度系数").AsString() + duplicatedtextType.LookupParameter("宽度系数").AsValueString(); string text_Form = duplicatedtextType.LookupParameter("文字字体").AsString() + duplicatedtextType.LookupParameter("文字字体").AsValueString(); string text_Elbow = duplicatedtextType.LookupParameter("引线箭头").AsString() + duplicatedtextType.LookupParameter("引线箭头").AsValueString(); if (textNode != null && textNode != "2.5mm") { duplicatedtextType.LookupParameter("文字大小").SetValueString("2.5mm"); } if (dut_gerneral != null && dut_gerneral != 0.7.ToString()) { duplicatedtextType.LookupParameter("宽度系数").Set(0.70); } if (text_Form != null && text_Form != "新宋体") { duplicatedtextType.LookupParameter("文字字体").Set("新宋体"); } if (text_Elbow != null && text_Elbow != "楼梯碰头_30度实心箭头") { //创建族类型——楼梯碰头_30度实心箭头 Element arrowType = doc.GetElement(duplicatedtextType.LookupParameter("引线箭头").AsElementId()); arrowType.LookupParameter("箭头样式").Set(8); arrowType.Name = "楼梯碰头_30度实心箭头"; arrowType.LookupParameter("箭头宽度角").Set(0.523598775598298); arrowType.LookupParameter("填充记号").Set(1); arrowType.LookupParameter("记号尺寸").Set(0.00984251968503937); } note.TextNoteType = duplicatedtextType; } } } } //设置显示颜色(也可修改文字注释颜色的属性) Color color = new Color((byte)255, (byte)128, (byte)128); OverrideGraphicSettings ogs = new OverrideGraphicSettings(); ogs.SetProjectionLineColor(color);//投影表面线的颜色 view.SetElementOverrides(note.Id, ogs); tran.Commit(); } } return Result.Succeeded; } } } |
最后的效果,文字注释的族类型名称为“宋体_2.5mm”,引线箭头族类型是“楼梯碰头_30度实心箭头”,字体是新宋体,大小是2.5mm,宽度系数0.7
