Files
SZMCToolkit.RevitAddins/WpfAppTest/RebarTextBlock.cs
2026-02-23 10:28:26 +08:00

82 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace WpfAppTest
{
internal class RebarTextBlock : TextBlock
{
public static readonly DependencyProperty RebarTextProperty =
DependencyProperty.Register(
nameof(RebarText),
typeof(string),
typeof(WpfAppTest.RebarTextBlock),
new PropertyMetadata(string.Empty, OnRebarTextChanged));
public string RebarText
{
get => (string)GetValue(RebarTextProperty);
set => SetValue(RebarTextProperty, value);
}
private static void OnRebarTextChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((WpfAppTest.RebarTextBlock)d).BuildInlines(e.NewValue as string);
}
//{Assembly.GetExecutingAssembly().GetName()}
internal static readonly FontFamily RebarFont = new($"pack://application:,,,/WpfAppTest;component/Fonts/#SJQY");
internal static readonly FontFamily NormalFont = new($"pack://application:,,,/WpfAppTest;component/Fonts/#汉仪粗宋简");
private void BuildInlines(string text)
{
var sjqy = (FontFamily)Application.Current.Resources["SJQY"];
Inlines.Clear();
if (string.IsNullOrEmpty(text)) return;
var regex = new Regex(
@"(?<![A-Za-z])([ABCD])(?=\s*(?:[ØΦ]\d|\d+@\d+))",
RegexOptions.Compiled);
int lastIndex = 0;
foreach (Match match in regex.Matches(text))
{
// 普通文本
if (match.Index > lastIndex)
{
this.Inlines.Add(new Run
{
Text = text.Substring(lastIndex, match.Index - lastIndex),
FontFamily = NormalFont
});
}
// 钢筋等级SJQY
this.Inlines.Add(new Run
{
Text = match.Value,
FontFamily = sjqy
});
lastIndex = match.Index + match.Length;
}
// 尾部普通文本
if (lastIndex < text.Length)
{
this.Inlines.Add(new Run
{
Text = text.Substring(lastIndex),
});
}
}
}
}