using System.Drawing; using System.Drawing.Text; using System.Windows; using Autodesk.Revit.DB; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Nice3point.Revit.Toolkit.External.Handlers; using Nice3point.Revit.Toolkit.Options; namespace Sai.RvKits.ModelManager; public partial class TemplateManagerViewModel : ObservableObject { private readonly ActionEventHandler handler; [ObservableProperty] private FontFamily[] fontFamilies; [ObservableProperty] private FontFamily selectFontFamily; [ObservableProperty] private double fontSize; [ObservableProperty] private double widthScale; public TemplateManagerViewModel() { handler = new ActionEventHandler(); // 创建 InstalledFontCollection 对象 var installedFonts = new InstalledFontCollection(); // 获取已安装的字体数组 fontFamilies = installedFonts.Families; } [RelayCommand] private void ModifyFont() { handler.Raise( uiapp => { var doc = uiapp.ActiveUIDocument.Document; doc.InvokeGroup( tg => { var col = doc.OfClass() .Cast() .Where(f => f.FamilyCategory.CategoryType == CategoryType.Annotation); foreach(var family in col) { if(!family.IsEditable) { continue; } var familyEditing = doc.EditFamily(family); //所有标签文字; familyEditing.Invoke( ts => { var textElements = familyEditing.OfClass().Cast(); foreach(var text in textElements) { var type = text.Symbol; ElementType typeCopy; try { typeCopy = type.Duplicate( $"{SelectFontFamily.Name} {FontSize} - {WidthScale}"); } catch(Autodesk.Revit.Exceptions.ArgumentException) { } type.get_Parameter(BuiltInParameter.TEXT_FONT)?.SetValueString( $"{SelectFontFamily.Name}"); //字体大小 type.get_Parameter(BuiltInParameter.TEXT_SIZE)?.Set(FontSize); //宽度系数 type.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE)?.Set(WidthScale); } var textNotes = familyEditing.OfClass().Cast(); foreach(var text in textNotes) { var type = text.Symbol; try { var typeCopy = type.Duplicate( $"{SelectFontFamily.Name} {FontSize} - {WidthScale}"); } catch(Autodesk.Revit.Exceptions.ArgumentException) { } type.get_Parameter(BuiltInParameter.TEXT_FONT)?.SetValueString( $"{SelectFontFamily.Name}"); //字体大小 type.get_Parameter(BuiltInParameter.TEXT_SIZE)?.Set(FontSize); //宽度系数 type.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE)?.Set(WidthScale); } }); doc.Invoke( ts => { familyEditing.LoadFamily(doc, new FamilyLoadOptions(true)); }, "载入族"); } },"调整文字"); }); } }