using System.Drawing; using System.Drawing.Text; using Autodesk.Revit.DB; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Nice3point.Revit.Toolkit.External.Handlers; using Nice3point.Revit.Toolkit.Options; namespace ShrlAlgo.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 CleanFontType() { handler.Raise( uiapp => { var doc = uiapp.ActiveUIDocument.Document; var textTypes = doc.OfClass().ToList(); foreach (var type in textTypes) { { if (type is TextNoteType textNoteType) { try { var font = textNoteType.get_Parameter(BuiltInParameter.TEXT_FONT).AsString(); var size = textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsValueString().TrimEnd('m'); var dSize = Math.Round(Convert.ToDouble(size), 1, MidpointRounding.AwayFromZero); if (dSize < 0.23) { return; } textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).SetValueString(dSize.ToString()); var scale = textNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsValueString(); //var color = textNoteType.get_Parameter(BuiltInParameter.LINE_COLOR).AsInteger(); var name = $"{font}-{dSize}mm-{scale}"; var isExist = textTypes.FirstOrDefault(t => t.Name == name); if (isExist != null) { var texts = doc.OfClass().Where(e => e.GetTypeId() == textNoteType.Id).ToList(); texts.ForEach(e => e.ChangeTypeId(isExist.Id)); doc.Regenerate(); doc.Delete(textNoteType.Id); } else { textNoteType.Name = name; } } catch (Exception) { throw; } } } } }); } [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)); }, "载入族"); } }, "调整文字"); }); } }