How can replace text in Textnote by keeping same formatting

How can replace text in Textnote by keeping same formatting

kite15
Advocate Advocate
1,341 Views
6 Replies
Message 1 of 7

How can replace text in Textnote by keeping same formatting

kite15
Advocate
Advocate

Hi,

I am trying to replace the text with another text by keeping same formatting (Revit 2020).

Actually, there have a same discussion:

https://forums.autodesk.com/t5/revit-api-forum/replace-text-in-text-note-and-keep-formatting/m-p/347...

 

Have tried as per above discussion, However, after replace the text its became all as under line text formatting...

ReplaceTextNoteWithSameFormat.png

 Code:

 

TextNote textNote = revitDocument.GetElement(elementId) as TextNote;
                            FormattedText formattedText = textNote.GetFormattedText();
                            TextRange range = formattedText.AsTextRange();
                            string plainText = textNote.Text;
                            if (!string.IsNullOrEmpty(updatedText))
                            {
                                plainText = plainText.Replace(searchedText, updatedText);
                                formattedText.SetPlainText(range, plainText);
                            }
                            textNote.SetFormattedText(formattedText);

 

Please, help 

0 Likes
1,342 Views
6 Replies
Replies (6)
Message 2 of 7

hmunsell
Mentor
Mentor

i don't see anything in that code that would make it underline. it may be coming from the text type.

 

if its a text note in Revit, depending on where the underlining is set, you have 2 options. Option 1 is global, option 2 is by instance. 

  1. select the Text Type, Edit Type, and uncheck the Underline option. 
  2. double click on the text, select all and unselect the Underline option in the ribbon.

Howard Munsell
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.



EESignature


0 Likes
Message 3 of 7

kite15
Advocate
Advocate

Hi,

1. It's happening due to first word is formatted with underline.

2. I am looking for automation not considering the manual approach

 

ReplaceTextNoteWithSameFormat1.png

please, suggest regarding the text formatting automation procedure for TextNote element in Revit API2020, in that case.

 

Thank you

0 Likes
Message 4 of 7

hmunsell
Mentor
Mentor

so you know what is causing the problem then. It is the first word, which is probably edited by double clicking on the text and only selecting that word to underline (vs being a type setting). The code is just selecting the style of the first word and applying it to the whole text string. not sure your gonna be able to easily get around that in the code.   

 

  • is the REF (highlighted in green) the only thing changing?
  • is it the same in all instances or does it change?

Could you use the native Find and Replace tool in Revit?

Howard Munsell
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.



EESignature


0 Likes
Message 5 of 7

FAIR59
Advisor
Advisor

a FormattedText can have multiple ranges, first you'd have to find the range  of the searchText, and then you can update only that range.

 

TextRange range = formattedText.Find(searchedText,0,false,false);
string plainText = textNote.Text.Substring(range.Start,range.Length);
if (!string.IsNullOrEmpty(updatedText))
{
    plainText = plainText.Replace(searchedText, updatedText);
    formattedText.SetPlainText(range, plainText);
}

 

Message 6 of 7

kite15
Advocate
Advocate

Hi FAIR59,

Thank you very much its working fine..😊

0 Likes
Message 7 of 7

Anonymous
Not applicable

Not sure if this issue has been resolved, but here is a lengthy method for maintaining formatting when text is replaced:

 

void ReplaceTextInTextNote(TextNote txnote,
int index, //index of first character in text to be replaced
int range, //number of characters to be replaced
string addText) //text to replace the removed characters
            {
                FormattedText ftx = txnote.GetFormattedText();
                string newText = txnote.Text;

                //Collect Current UnderlineStatuses, BoldStatuses, italicstatuses, superscriptStatuses, per char in txno.Text
                                
                List<bool> underlineStatuses = new List<bool>();
                List<bool> boldStatuses = new List<bool>();
                List<bool> italicStatuses = new List<bool>();
                List<bool> superscriptStatuses = new List<bool>();
                List<bool> subscriptStatuses = new List<bool>();
                for (int i = 0; i < txno.Text.Length; i++)
                {
                    if (ftx.GetUnderlineStatus(new TextRange(i, 1)).ToString() == "All") { underlineStatuses.Add(true); }
                    else { underlineStatuses.Add(false);  }

                    if (ftx.GetBoldStatus(new TextRange(i, 1)).ToString() == "All") { boldStatuses.Add(true); }
                    else { boldStatuses.Add(false); }

                    if (ftx.GetItalicStatus(new TextRange(i, 1)).ToString() == "All") { italicStatuses.Add(true); }
                    else { italicStatuses.Add(false); }

                    if (ftx.GetSuperscriptStatus(new TextRange(i, 1)).ToString() == "All") { superscriptStatuses.Add(true); }
                    else { superscriptStatuses.Add(false); }

                    if (ftx.GetSubscriptStatus(new TextRange(i, 1)).ToString() == "All") { subscriptStatuses.Add(true); }
                    else { subscriptStatuses.Add(false); }
                }
                
                //Remove Replaced Text
                underlineStatuses.RemoveRange(index, range);
                boldStatuses.RemoveRange(index, range);
                italicStatuses.RemoveRange(index, range);
                superscriptStatuses.RemoveRange(index, range);
                subscriptStatuses.RemoveRange(index, range);

                newText = newText.Remove(index, range);
             
                //Add new Text
                newText = newText.Insert(index, addText);

                //Shall the inserted Text be underlined, bold
                bool underlineAddText = ftx.GetUnderlineStatus(new TextRange(index, 1)).ToString() == "All";
                bool boldAddText = ftx.GetBoldStatus(new TextRange(index, 1)).ToString() == "All";
                bool italicAddText = ftx.GetItalicStatus(new TextRange(index, 1)).ToString() == "All";                
                bool superscriptAddText = ftx.GetSuperscriptStatus(new TextRange(index, 1)).ToString() == "All";
                bool subscriptAddText = ftx.GetSubscriptStatus(new TextRange(index, 1)).ToString() == "All";

                //Add the same formatting to a list to be added to underline,bold, italic, subscript, superscript statuses
                underlineStatuses.InsertRange(index, (Enumerable.Repeat(underlineAddText, addText.Length)));
                boldStatuses.InsertRange(index, (Enumerable.Repeat(boldAddText, addText.Length)));
                italicStatuses.InsertRange(index, (Enumerable.Repeat(italicAddText, addText.Length)));
                superscriptStatuses.InsertRange(index, (Enumerable.Repeat(superscriptAddText, addText.Length)));
                subscriptStatuses.InsertRange(index, (Enumerable.Repeat(subscriptAddText, addText.Length)));
                

                //Set New Text
                txnote.Text = newText;
                FormattedText ftx2 = txnote.GetFormattedText();

                //Set Underline Status
                for (int i = 0; i < underlineStatuses.Count; i++)
                {
                    if (underlineStatuses[i] == true) { ftx2.SetUnderlineStatus(new TextRange(i, 1), true); }
                    else { { ftx2.SetUnderlineStatus(new TextRange(i, 1), false); } }

                    if (boldStatuses[i] == true) { ftx2.SetBoldStatus(new TextRange(i, 1), true); }
                    else { { ftx2.SetBoldStatus(new TextRange(i, 1), false); } }

                    if (italicStatuses[i] == true) { ftx2.SetItalicStatus(new TextRange(i, 1), true); }
                    else { { ftx2.SetItalicStatus(new TextRange(i, 1), false); } }

                    if (superscriptStatuses[i] == true) { ftx2.SetSuperscriptStatus(new TextRange(i, 1), true); }
                    else { { ftx2.SetSuperscriptStatus(new TextRange(i, 1), false); } }

                    if (subscriptStatuses[i] == true) { ftx2.SetSubscriptStatus(new TextRange(i, 1), true); }
                    else { { ftx2.SetSubscriptStatus(new TextRange(i, 1), false); } }
                }
                       

                txnote.SetFormattedText(ftx2);

            }

 

0 Likes