Files
ShrlAlgoToolkit/ShrlAlgoToolkit.RevitAddins/Standardizer/TemplateManagerViewModel.cs

173 lines
7.7 KiB
C#
Raw Normal View History

2024-09-22 11:05:41 +08:00
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;
2026-02-22 20:03:42 +08:00
using Color = Autodesk.Revit.DB.Color;
2026-02-22 20:03:42 +08:00
namespace ShrlAlgoToolkit.RevitAddins.Standardizer;
2024-09-22 11:05:41 +08:00
public partial class TemplateManagerViewModel : ObservableObject
{
private readonly ActionEventHandler handler;
[ObservableProperty]
public partial FontFamily[] FontFamilies { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
public partial FontFamily SelectFontFamily { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
public partial double FontSize { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
public partial double WidthScale { get; set; }
2024-09-22 11:05:41 +08:00
public TemplateManagerViewModel()
{
handler = new ActionEventHandler();
// 创建 InstalledFontCollection 对象
var installedFonts = new InstalledFontCollection();
FontFamilies = installedFonts.Families;
2024-09-22 11:05:41 +08:00
}
2026-02-22 20:03:42 +08:00
/// <summary>
/// 清理导入的线型图案
/// </summary>
/// <param name="doc"></param>
private static void CleanImportLinePattern(Document doc)
{
var templates = doc.OfClass<LinePatternElement>()
.Cast<LinePatternElement>()
.Where(e => e.Name.StartsWith("IMPORT"))
.Select(e => e.Id).ToList();
doc.Delete(templates);
}
2024-09-22 11:05:41 +08:00
[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;
}
}
}
}
});
}
2026-02-22 20:03:42 +08:00
[RelayCommand]
2024-09-22 11:05:41 +08:00
private void ModifyFont()
{
handler.Raise(
uiapp =>
{
var doc = uiapp.ActiveUIDocument.Document;
doc.InvokeGroup(
_ =>
2024-09-22 11:05:41 +08:00
{
var col = doc.OfClass<Family>()
.Cast<Family>()
.Where(f => f.FamilyCategory.CategoryType == CategoryType.Annotation);
foreach (var family in col)
2024-09-22 11:05:41 +08:00
{
if (!family.IsEditable)
2024-09-22 11:05:41 +08:00
{
continue;
}
var familyEditing = doc.EditFamily(family);
//所有标签文字;
familyEditing.Invoke(
_ =>
2024-09-22 11:05:41 +08:00
{
var textElements = familyEditing.OfClass<TextElement>().Cast<TextElement>();
foreach (var text in textElements)
2024-09-22 11:05:41 +08:00
{
var type = text.Symbol;
ElementType typeCopy;
try
{
typeCopy = type.Duplicate(
$"{SelectFontFamily.Name} {FontSize} - {WidthScale}");
}
catch (Autodesk.Revit.Exceptions.ArgumentException)
2024-09-22 11:05:41 +08:00
{
}
type.get_Parameter(BuiltInParameter.TEXT_FONT)?.SetValueString(
$"{SelectFontFamily.Name}");
//字体大小
2025-04-24 20:56:44 +08:00
type.get_Parameter(BuiltInParameter.TEXT_SIZE)?.Set((double)FontSize);
2024-09-22 11:05:41 +08:00
//宽度系数
2025-04-24 20:56:44 +08:00
type.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE)?.Set((double)WidthScale);
2024-09-22 11:05:41 +08:00
}
var textNotes = familyEditing.OfClass<TextNote>().Cast<TextNote>();
foreach (var text in textNotes)
2024-09-22 11:05:41 +08:00
{
var type = text.Symbol;
try
{
var typeCopy = type.Duplicate(
$"{SelectFontFamily.Name} {FontSize} - {WidthScale}");
}
catch (Autodesk.Revit.Exceptions.ArgumentException)
2024-09-22 11:05:41 +08:00
{
}
type.get_Parameter(BuiltInParameter.TEXT_FONT)?.SetValueString(
$"{SelectFontFamily.Name}");
//字体大小
2025-04-24 20:56:44 +08:00
type.get_Parameter(BuiltInParameter.TEXT_SIZE)?.Set((double)FontSize);
2024-09-22 11:05:41 +08:00
//宽度系数
2025-04-24 20:56:44 +08:00
type.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE)?.Set((double)WidthScale);
2024-09-22 11:05:41 +08:00
}
});
doc.Invoke(
_ =>
2024-09-22 11:05:41 +08:00
{
familyEditing.LoadFamily(doc, new FamilyLoadOptions(true));
},
"载入族");
}
}, "调整文字");
2024-09-22 11:05:41 +08:00
});
}
}