Files
Shrlalgo.RvKits/ShrlAlgoToolkit.RevitAddins/ModelManager/TemplateManagerViewModel.cs
2025-10-04 08:52:23 +08:00

159 lines
7.3 KiB
C#

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 ShrlAlgoToolkit.RevitAddins.ModelManager;
public partial class TemplateManagerViewModel : ObservableObject
{
private readonly ActionEventHandler handler;
[ObservableProperty]
public partial FontFamily[] FontFamilies { get; set; }
[ObservableProperty]
public partial FontFamily SelectFontFamily { get; set; }
[ObservableProperty]
public partial double FontSize { get; set; }
[ObservableProperty]
public partial double WidthScale { get; set; }
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<TextNoteType>().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<TextNote>().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(
_ =>
{
var col = doc.OfClass<Family>()
.Cast<Family>()
.Where(f => f.FamilyCategory.CategoryType == CategoryType.Annotation);
foreach (var family in col)
{
if (!family.IsEditable)
{
continue;
}
var familyEditing = doc.EditFamily(family);
//所有标签文字;
familyEditing.Invoke(
_ =>
{
var textElements = familyEditing.OfClass<TextElement>().Cast<TextElement>();
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((double)FontSize);
//宽度系数
type.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE)?.Set((double)WidthScale);
}
var textNotes = familyEditing.OfClass<TextNote>().Cast<TextNote>();
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((double)FontSize);
//宽度系数
type.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE)?.Set((double)WidthScale);
}
});
doc.Invoke(
_ =>
{
familyEditing.LoadFamily(doc, new FamilyLoadOptions(true));
},
"载入族");
}
}, "调整文字");
});
}
}