Skip to content

Instantly share code, notes, and snippets.

@AleksandrPidlozhevich
Last active January 16, 2024 19:41
Show Gist options
  • Save AleksandrPidlozhevich/412f5920f2a4a45ec302644223b61907 to your computer and use it in GitHub Desktop.
Save AleksandrPidlozhevich/412f5920f2a4a45ec302644223b61907 to your computer and use it in GitHub Desktop.
Changing the font in a Revit file
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
namespace EEPRevitPlagin.EEPRPCommandModules.FontChanging
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
internal class FontChangingCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
try
{
LanguageChangeCommandWPF window = new LanguageChangeCommandWPF(doc);
window.ShowDialog();
return Result.Succeeded;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return Result.Cancelled;
}
}
public void TextNoteMethod(StyleTextDictionary[] styles, Document doc, string choiceFont)
{
using (Transaction tran = new Transaction(doc, "Changing Font"))
{
tran.Start();
ICollection<Element> textType =
new FilteredElementCollector(doc).OfClass(typeof(TextNoteType)).ToElements();
foreach (Element item in textType)
{
item.get_Parameter(BuiltInParameter.TEXT_FONT).Set(choiceFont);
}
StyleTextMethod(styles, textType);
tran.Commit();
}
}
public void TextElementTypeMethod(StyleTextDictionary[] styles, Document doc, string choiceFont)
{
using (Transaction tran = new Transaction(doc, "Changing TextElementType"))
{
tran.Start();
ICollection<Element> textElementType =
new FilteredElementCollector(doc).OfClass(typeof(TextElementType)).ToElements();
foreach (Element item in textElementType)
{
item.get_Parameter(BuiltInParameter.TEXT_FONT).Set(choiceFont);
}
StyleTextMethod(styles, textElementType);
tran.Commit();
}
}
public void DimensionTypeMethod(StyleTextDictionary[] styles, Document doc, string choiceFont)
{
using (Transaction tran = new Transaction(doc, "Changing TextElementType"))
{
tran.Start();
ICollection<Element> textElementType =
new FilteredElementCollector(doc).OfClass(typeof(DimensionType)).ToElements();
foreach (Element item in textElementType)
{
item.get_Parameter(BuiltInParameter.TEXT_FONT).Set(choiceFont);
}
StyleTextMethod(styles, textElementType);
tran.Commit();
}
}
public void ChangingFamilyMethod(StyleTextDictionary[] styles, Document doc, string choiceFont)
{
ICollection<Element> families =
new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).WhereElementIsElementType().ToElements();
List<string> filteredElements = new List<string>();
foreach (Element e in families)
{
if (e.Category.CategoryType.ToString() == "Annotation")
{
ElementType ele = e as ElementType;
string familyName = ele.FamilyName;
if (!filteredElements.Contains(familyName))
{
filteredElements.Add(familyName);
FamilySymbol fs = e as FamilySymbol;
Family f = fs.Family;
Document editFamily = doc.EditFamily(f);
doc.EditFamily((e as FamilySymbol).Family);
ICollection<Element> elementTextType = new FilteredElementCollector(editFamily).OfClass(typeof(TextNoteType)).ToElements();
if (elementTextType.Count > 0) ChangingFamilyMethod(styles, editFamily, choiceFont);
TextNoteMethod(styles, editFamily, choiceFont);
TextElementTypeMethod(styles, editFamily, choiceFont);
editFamily.LoadFamily(doc, new FamilyOptions());
}
}
}
}
public void StyleTextMethod(StyleTextDictionary[] styles, ICollection<Element> elements)
{
foreach (StyleTextDictionary style in styles)
{
foreach (Element e in elements)
{
if(style.IsStyleUsed == true) e.get_Parameter(style.BuiltInParameterEnum).Set(1);
else e.get_Parameter(style.BuiltInParameterEnum).Set(0);
}
}
}
public class FamilyOptions : IFamilyLoadOptions
{
public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
{
overwriteParameterValues = true;
return true;
}
public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source,
out bool overwriteParameterValues)
{
source = FamilySource.Family;
overwriteParameterValues = true;
return true;
}
}
}
}
<Window x:Class="EEPRevitPlagin.EEPRPCommandModules.FontChanging.LanguageChangeCommandWPF"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:EEPRevitPlagin.EEPRPCommandModules.FontChanging"
xmlns:re="clr-namespace:EEPRevitPlagin.SecondaryCommand.LangResources"
mc:Ignorable="d"
Title="{x:Static re:Language.Font_Change}"
WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen" FontFamily="Times New Roman" Width="335" Height="135" ResizeMode="NoResize" SizeToContent="WidthAndHeight">
<Grid VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="90"/>
</Grid.RowDefinitions>
<Label Content="Choose a font" HorizontalAlignment="Left" Margin="10,0,0,0" Width="220" Height="28" VerticalAlignment="Center"/>
<Button x:Name="doButton" Content="Apply" Margin="253,0,10,0" Click="Button_Click" FontFamily="Times New Roman" Height="20" VerticalAlignment="Top" Grid.Row="1" Width="70"/>
<ComboBox x:Name="comboBoxFonts" Margin="10,4,170,0" VerticalAlignment="Top" FontFamily="Times New Roman" Grid.Row="1" Width="155" Height="20"/>
<CheckBox x:Name="Bold" Content="Bold font" HorizontalAlignment="Left" Margin="10,29,0,0" Grid.Row="1" VerticalAlignment="Top" IsChecked="False"/>
<CheckBox x:Name="Italic" Content="Italic font" HorizontalAlignment="Left" Margin="10,49,0,26" Grid.Row="1" Checked="Italic_Checked"/>
<CheckBox x:Name="Underline" Content="Underline font" HorizontalAlignment="Left" Margin="10,69,0,6" Grid.Row="1" RenderTransformOrigin="0.486,-0.593"/>
</Grid>
</Window>
using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Windows;
using System.Windows.Controls;
namespace EEPRevitPlagin.EEPRPCommandModules.FontChanging
{
/// <summary>
/// Interaction logic for FontChangingCommandWPF.xaml
/// </summary>
public partial class LanguageChangeCommandWPF : Window
{
private Document Doc;
public LanguageChangeCommandWPF(Document doc)
{
InitializeComponent();
Doc = doc;
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
FontFamily[] fontFamilies = installedFontCollection.Families;
int count = fontFamilies.Length;
for (int i = 0; i < count; ++i)
{
comboBoxFonts.Items.Add(fontFamilies[i].Name);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string choiceFont = comboBoxFonts.SelectedItem.ToString();
StyleTextDictionary boldStyle =
new StyleTextDictionary("boldStyle", Bold.IsChecked, BuiltInParameter.TEXT_STYLE_BOLD);
StyleTextDictionary italicStyle =
new StyleTextDictionary("italicStyle", Italic.IsChecked, BuiltInParameter.TEXT_STYLE_ITALIC);
StyleTextDictionary underlineStyle =
new StyleTextDictionary("underlineStyle", Underline.IsChecked, BuiltInParameter.TEXT_STYLE_UNDERLINE);
StyleTextDictionary[] styles = { boldStyle, italicStyle, underlineStyle };
FontChangingCommand textNote = new FontChangingCommand();
textNote.TextNoteMethod(styles, Doc, choiceFont);
FontChangingCommand textElement = new FontChangingCommand();
textElement.TextElementTypeMethod(styles, Doc, choiceFont);
FontChangingCommand dimension = new FontChangingCommand();
dimension.DimensionTypeMethod(styles, Doc, choiceFont);
FontChangingCommand changingFamily = new FontChangingCommand();
changingFamily.ChangingFamilyMethod(styles, Doc, choiceFont);
MessageBox.Show("Operation completed");
}
private void Italic_Checked(object sender, RoutedEventArgs e)
{
}
}
public class StyleTextDictionary
{
public string NameOfStyle { get; set; }
public bool? IsStyleUsed { get; set; }
public BuiltInParameter BuiltInParameterEnum { get; set; }
public StyleTextDictionary(string nameOfStyle, bool? isStyleUsed, BuiltInParameter builtInParameterEnum)
{
NameOfStyle = nameOfStyle;
IsStyleUsed = isStyleUsed;
BuiltInParameterEnum = builtInParameterEnum;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment