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;
|
|
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
|
|
|
|
|
namespace ShrlAlgo.RvKits.ModelManager;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
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]
|
2025-02-10 20:53:40 +08:00
|
|
|
|
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]
|
2024-09-22 11:05:41 +08:00
|
|
|
|
private void ModifyFont()
|
|
|
|
|
|
{
|
|
|
|
|
|
handler.Raise(
|
|
|
|
|
|
uiapp =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var doc = uiapp.ActiveUIDocument.Document;
|
|
|
|
|
|
doc.InvokeGroup(
|
|
|
|
|
|
tg =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var col = doc.OfClass<Family>()
|
|
|
|
|
|
.Cast<Family>()
|
|
|
|
|
|
.Where(f => f.FamilyCategory.CategoryType == CategoryType.Annotation);
|
2025-02-10 20:53:40 +08:00
|
|
|
|
foreach (var family in col)
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
2025-02-10 20:53:40 +08:00
|
|
|
|
if (!family.IsEditable)
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
var familyEditing = doc.EditFamily(family);
|
|
|
|
|
|
//所有标签文字;
|
|
|
|
|
|
familyEditing.Invoke(
|
|
|
|
|
|
ts =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var textElements = familyEditing.OfClass<TextElement>().Cast<TextElement>();
|
2025-02-10 20:53:40 +08:00
|
|
|
|
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}");
|
2025-02-10 20:53:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Autodesk.Revit.Exceptions.ArgumentException)
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
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<TextNote>().Cast<TextNote>();
|
2025-02-10 20:53:40 +08:00
|
|
|
|
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}");
|
2025-02-10 20:53:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Autodesk.Revit.Exceptions.ArgumentException)
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
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));
|
|
|
|
|
|
},
|
|
|
|
|
|
"载入族");
|
|
|
|
|
|
}
|
2025-02-10 20:53:40 +08:00
|
|
|
|
}, "调整文字");
|
2024-09-22 11:05:41 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|