In continuation to the previous post on this topic, in this post, I am including a short code snippet, which shows how to implement the logic mentioned in the post with the same title. This was requested for, by one of the readers – in the response to the previous post.
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.HelloRevit.CS
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
Document doc =
commandData.Application.ActiveUIDocument.Document;
// 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 textNote = doc.GetElement(
textNoteid) as TextNote;
Transaction trans = new Transaction(doc);
trans.Start("ChangeParam");
// Create a duplicate
Element ele =
textNote.TextNoteType.Duplicate("ADSK_NewTextNoteType");
TextNoteType noteType = ele as TextNoteType;
if (null != noteType)
{
// Just for example sake, change the font parameter
noteType.get_Parameter("Text Font").Set("Arial Narrow");
}
trans.Commit();
}
return Result.Succeeded;
}
}
}
After running this code, we can see that a new TextNoteType has been created with the name specified in the code and also the Text Font parameter has been changed to ‘Arial Narrow' from ‘Arial’.
Once the new TextNoteType has been created, we can set the current (or desired) text note element’s type to be the new one we just created, programmatically.