Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Change the Font (and Font size) of several TextNote in Viewsheet

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
2798 Views, 2 Replies

Change the Font (and Font size) of several TextNote in Viewsheet

Hi,

 

I'm trying to add several texts in a ViewSheet.

 

The texts have different Fonts and Sizes.

 

I une TextNote like this :

 

using (Transaction t = new Transaction(doc, "Text Creation"))
{
     t.Start();
                    
     // Add Text in Sheet
     XYZ origin = new XYZ(dStartX, dStartY, 0);
     ElementId defaultTypeId = doc.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType);

     TextNoteOptions opts = new TextNoteOptions(defaultTypeId);
     opts.HorizontalAlignment = HorizontalTextAlignment.Left;
     opts.Rotation = dAngle;

     TextNote textNote = TextNote.Create(doc, m_view_sheet.Id, origin, 20, "New sample text", opts);

     t.Commit();
}

 

Can somebody post a exemple how to manage the Font and Size of defferent texts, it will be very helpfull for me.

 

Thanks in advance

2 REPLIES 2
Message 2 of 3
stever66
in reply to: Anonymous

The text font and size are dependent on the text style - which is basically a family type in Revit, or a family symbol in the API.   So, for example,  if you change it for one text note with the style "1/8" Arial", it will change for all text notes in the project with the same style.  

 

I doubt if that is what you want, so the example I've posed does a couple of things.   It places a text note with the default style, and then changes the font size for that style.  (Again, that could change the size of a lot of different text notes in your project.)

 

But then it creates a new text style, changes the font of that style to arial narrow, and finally changes the style of the text note to the new style.

 

You will have to select what you want to use and delete the rest, but it should give you an example to follow to get exactly what you are after.

 

This is a macro, so if you are using visual studio, you will have to change the first two UIdocument and Document lines

 

 

public void ChangeSizeAndFont()
		{	
            UIDocument uidoc = this.ActiveUIDocument;   
            Document doc = this.ActiveUIDocument.Document;     
                    using (Transaction tran = new Transaction(doc, "change text font and size"))
                        {
                          
                           tran.Start();
                           ElementId defaultTypeId = doc.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType);
                           XYZ origin = new XYZ(0, 0, 0);                 
                           TextNote note = TextNote.Create(doc, doc.ActiveView.Id, origin,  "Hello", defaultTypeId);
                           //change the text size of a text type:
                            
                            TextElementType textType = note.Symbol;
                            BuiltInParameter paraIndex = BuiltInParameter.TEXT_SIZE;
    						Parameter textSize = textType.get_Parameter(paraIndex);
    						
                            //units are in feet, so this is 1/4"
 						    double newSize = ( 1.0 / 4.0 ) * ( 1.0 / 12.0 ); 
 							textSize.Set( newSize );
                           	
 							
 							//create a new text type and change the font
 							// Get access to all the TextNote Elements
							FilteredElementCollector collectorUsed = new FilteredElementCollector(doc);
							ICollection<ElementId> textNotes = collectorUsed.OfClass(typeof(TextNote)).ToElementIds();
							
							foreach (ElementId textNoteid in textNotes)
      							{
								TextNote mytextNote = doc.GetElement(textNoteid) as TextNote;

         						// Create a duplicate type
								Element ele = mytextNote.TextNoteType.Duplicate("ADSK_NewTextNoteType");
								TextNoteType noteType = ele as TextNoteType;
								if (null != noteType)
									{		
          							BuiltInParameter paraIndex2 = BuiltInParameter.TEXT_FONT;
    								Parameter noteFont = noteType.get_Parameter(paraIndex2);
    								//change its font
    								noteFont.Set("Arial Narrow");
									}
								
								//change the text notes type to the new type
								//Dim elemId as DB.ElementId = noteType.Id
									note.ChangeTypeId (noteType.Id);
								
								//note.Symbol = noteType;
								//only duplicate the first type
								break;
								
								
								
								}
							
                           tran.Commit();
                        } // end using  
                    
                    

			
			
		}

 

 

.

 

 

Message 3 of 3
Anonymous
in reply to: stever66

Hi Thank you for your help.

 

Here is also some other sources that can help :

 

- https://adndevblog.typepad.com/aec/2012/08/creating-new-textnotetype-programmatically-with-code.html

- https://thebuildingcoder.typepad.com/blog/2011/07/text-size.html

- https://forums.augi.com/showthread.php?108893-TextNote-Font

 - https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/BuildingCoder/C...

 

Here you can find my code :

 

/// <summary>
        /// Creates the text style and applies to the given TextNote.
        /// a different TextNoteType is created each time there is a change in parameters : strFont / dSize / fBold
        /// </summary>
        /// <param name="textNote">TextNote object</param>
        /// <param name="strFont">Name of the font</param>
        /// <param name="dSize">Size of the font/text in mm</param>
        /// <param name="fBold">True is font is Bold</param>
        static void _setTextFontAndSize(TextNote textNote, string strFont, double dSize, bool fBold)
        {
            Document doc = Application.uiApplication.ActiveUIDocument.Document;

            // Create Note type Name
            string strStyleName = "TB-" + strFont + "-" + Math.Round(dSize,2).ToString();
            if (fBold) strStyleName += "-Bold";

            // Get the TextNoteType if exists
            TextNoteType textNoteType = new FilteredElementCollector(doc)
                .OfClass(typeof(TextNoteType))
                .WhereElementIsElementType()
                .Cast<TextNoteType>()
                .Where(tt => Equals(strStyleName, tt.Name))
                .FirstOrDefault();

            if (textNoteType == null)
            {
                // Create new Note type
                Element ele = textNote.TextNoteType.Duplicate(strStyleName);
                textNoteType = ele as TextNoteType;

                // Set Size
                TextElementType textType = textNote.Symbol;
                BuiltInParameter paraIndex = BuiltInParameter.TEXT_SIZE;
                Parameter textSize = textNoteType.get_Parameter(paraIndex);
                textSize.Set(dSize / LENGTH_CONVERSION_FACTOR); //units are in feet

                // Set font
                BuiltInParameter paraIndex2 = BuiltInParameter.TEXT_FONT;
                Parameter noteFont = textNoteType.get_Parameter(paraIndex2);
                noteFont.Set(strFont);

                // Set Bold
                BuiltInParameter paraIndex3 = BuiltInParameter.TEXT_STYLE_BOLD;
                Parameter textBold = textNoteType.get_Parameter(paraIndex3);
                if (fBold) textBold.Set(1);
                else textBold.Set(0);
            }

            // Change the text notes type to the new type
            textNote.ChangeTypeId(textNoteType.Id);

        }

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Rail Community