Text and associated leaders can be accessed from the TextNote class. Note
A TextNote can have plain text or formatted text. The overloaded TextNote.Create() method provides options for creating unwrapped and line-wrapping text note elements. The width for the area of the text content can be specified on creation, but is restricted by a minimum and maximum width that is based on properties of the text and its type. The overloaded methods GetMinimumAllowedWidth() and GetMaximumAllowedWidth(), inherited from TextElement, return the constraints for either a specific TextNote or for a given document and text type id.
The following example creates a new TextNote at a user specified point and with a given width and TextNoteOptions.
Code Region: Create a TextNote |
public TextNote AddNewTextNote(UIDocument uiDoc) { Document doc = uiDoc.Document; XYZ textLoc = uiDoc.Selection.PickPoint("Pick a point for sample text."); ElementId defaultTextTypeId = doc.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType); double noteWidth = .2; // make sure note width works for the text type double minWidth = TextNote.GetMinimumAllowedWidth(doc, defaultTextTypeId); double maxWidth = TextNote.GetMaximumAllowedWidth(doc, defaultTextTypeId); if (noteWidth < minWidth) { noteWidth = minWidth; } else if (noteWidth > maxWidth) { noteWidth = maxWidth; } TextNoteOptions opts = new TextNoteOptions(defaultTextTypeId); opts.HorizontalAlignment = HorizontalTextAlignment.Left; opts.Rotation = Math.PI / 4; TextNote textNote = TextNote.Create(doc, doc.ActiveView.Id, textLoc, noteWidth, "New sample text", opts); return textNote; } |
Code Region: Format text in a TextNote |
public void FormatText(TextNote textNote) { // TextNote created with "New sample text" FormattedText formatText = textNote.GetFormattedText(); // italicize "New" TextRange range = new TextRange(0, 3); formatText.SetItalicStatus(range, true); // make "sample" bold range = formatText.Find("sample", 0, false, true); if (range.Length > 0) formatText.SetBoldStatus(range, true); // make "text" underlined range = formatText.Find("text", 0, false, true); if (range.Length > 0) formatText.SetUnderlineStatus(range, true); // make all text uppercase formatText.SetAllCapsStatus(true); textNote.SetFormattedText(formatText); } |
Code Region: Inserting new text |
public void AppendText(TextNote textNote) { FormattedText formatText = textNote.GetFormattedText(); TextRange range = formatText.AsTextRange(); range.Start = range.End - 1; // set Length to 0 to insert range.Length = 0; string someNewText = "\rThis is a new paragraph\vThis is a new line without a paragraph break\r"; formatText.SetPlainText(range, someNewText); // get range for entire text range = formatText.AsTextRange(); range.Start = range.End - 1; range.Length = 0; string someListText = "\rBulleted List item 1\rItem 2\vSecond line for Item 2\rThird bullet point"; formatText.SetPlainText(range, someListText); range.Start++; range.Length = someListText.Length; formatText.SetListType(range, ListType.Bullet); if (formatText.GetAllCapsStatus(range) != FormatStatus.None) { formatText.SetAllCapsStatus(range, false); } textNote.SetFormattedText(formatText); } |
Code Region: Setting text editor options |
public void SetEditorOptions() { TextEditorOptions editorOptions = TextEditorOptions.GetTextEditorOptions(); editorOptions.ShowBorder = false; editorOptions.ShowOpaqueBackground = true; } |
Revit supports two kinds of Leaders: straight leaders and arc leaders. Leaders can be added to a TextNote using the AddLeader() method, specifying the leader type using the TextNoteLeaderType enumerated type:
Table 39: Leader Types
Function | Member Name |
-Add a right arc leader | TNLT_ARC_R |
-Add a left arc leader | TNLT_ARC_L |
-Add a right leader. | TNLT_STRAIGHT_R |
-Add a left leader. | TNLT_STRAIGHT_L |
The TextNote.LeaderCount property returns the number of leaders and the GetLeaders() method returns all leaders currently attached to the text component. LeaderLeftAttachment and LeaderRightAttachment indicate the attachment position of the leaders on the corresponding side of the TextNote. Options for the LeaderAttachment are TopLine, MidPoint, and BottomLine. Use the RemoveLeaders() method to remove all leaders from the TextNote.