调整附注内容换行

This commit is contained in:
sladro 2026-01-07 15:53:54 +08:00
parent 5cfad1923a
commit b3d10f49a6
2 changed files with 92 additions and 96 deletions

View File

@ -446,7 +446,7 @@ namespace CadParamPluging.Cad
}
// === 特征9: 硬度符号 ===
TryDrawHardnessSymbol(ctx, ox + R, oy, H);
}
/// <summary>
@ -538,7 +538,7 @@ namespace CadParamPluging.Cad
}
// === 特征9: 硬度符号(有要求才画) ===
TryDrawHardnessSymbol(ctx, ox + W, oy, H);
}
private static void DrawSectionInnerBoundaryBold(DrawingContext ctx, double x, double yBottom, double height)
@ -1147,100 +1147,7 @@ namespace CadParamPluging.Cad
}
}
private static void TryDrawHardnessSymbol(DrawingContext ctx, double xRight, double yBottom, double height)
{
if (ctx == null || ctx.Bag == null)
{
return;
}
var raw = ctx.Bag.GetString("Hardness");
if (string.IsNullOrWhiteSpace(raw))
{
return;
}
var upper = raw.Trim().ToUpperInvariant();
string symbol;
if (upper.Contains("HB"))
{
symbol = "HB";
}
else if (upper.Contains("HRC"))
{
symbol = "HRC";
}
else if (upper.Contains("HV"))
{
symbol = "HV";
}
else
{
symbol = ExtractLeadingLetters(upper);
if (string.IsNullOrWhiteSpace(symbol))
{
symbol = "HB";
}
}
try
{
var attach = new Point3d(xRight - 2, yBottom + height * 0.25, 0);
var circleCenter = new Point3d(xRight + 25, yBottom + height * 0.25, 0);
const double circleR = 6;
var leader = new Line(attach, new Point3d(circleCenter.X - circleR, circleCenter.Y, 0));
ctx.Style?.Apply(leader, DrawingStyleManager.Role.Text);
ctx.Btr.AppendEntity(leader);
ctx.Tr.AddNewlyCreatedDBObject(leader, true);
var circle = new Circle(circleCenter, new Vector3d(0, 0, 1), circleR);
ctx.Style?.Apply(circle, DrawingStyleManager.Role.Text);
ctx.Btr.AppendEntity(circle);
ctx.Tr.AddNewlyCreatedDBObject(circle, true);
var text = new DBText();
text.TextString = symbol;
text.Height = 3.5;
text.HorizontalMode = TextHorizontalMode.TextCenter;
text.VerticalMode = TextVerticalMode.TextVerticalMid;
text.AlignmentPoint = circleCenter;
text.Position = circleCenter;
ctx.Style?.Apply(text, DrawingStyleManager.Role.Text);
ctx.Btr.AppendEntity(text);
ctx.Tr.AddNewlyCreatedDBObject(text, true);
}
catch
{
// ignore
}
}
private static string ExtractLeadingLetters(string s)
{
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
var chars = new List<char>();
foreach (var c in s)
{
if (c >= 'A' && c <= 'Z')
{
chars.Add(c);
}
else
{
if (chars.Count > 0)
{
break;
}
}
}
return chars.Count == 0 ? string.Empty : new string(chars.ToArray());
}
#endregion

View File

@ -430,7 +430,96 @@ namespace CadParamPluging.Cad
mt.Location = insertPoint;
mt.TextHeight = 3.5;
mt.Attachment = AttachmentPoint.BottomLeft; // 关键:锚点在左下,文字向上延伸
mt.Width = w * 0.45; // 宽度限制为45%
// 智能动态宽度:尝试检测右下角标题栏(表格)的左边界
// 修正搜索区域扩大防止表格过宽超过50%)导致检测不到左边界,从而产生重叠
// X方向从左侧 25% 开始扫描(涵盖右侧 75% 区域)
// Y方向从底部扫描至 50% 高度(防止标题栏上方有较高的更改栏)
var searchMinX = f.MinPoint.X + w * 0.25;
var searchMaxX = f.MaxPoint.X - 5.0;
var searchMinY = f.MinPoint.Y;
var searchMaxY = f.MinPoint.Y + h * 0.5;
var candidatesX = new List<double>();
foreach (var ent in spaceEntList)
{
if (ent is Line ln)
{
// 垂直线
if (Math.Abs(ln.StartPoint.X - ln.EndPoint.X) < 1.0 &&
ln.StartPoint.X > searchMinX && ln.StartPoint.X < searchMaxX &&
Math.Max(ln.StartPoint.Y, ln.EndPoint.Y) > searchMinY &&
Math.Min(ln.StartPoint.Y, ln.EndPoint.Y) < searchMaxY)
{
candidatesX.Add(ln.StartPoint.X);
}
}
else if (ent is Polyline pl)
{
for (int i = 0; i < pl.NumberOfVertices; i++)
{
var pt = pl.GetPoint3dAt(i);
if (pt.X > searchMinX && pt.X < searchMaxX && pt.Y > searchMinY && pt.Y < searchMaxY)
{
candidatesX.Add(pt.X);
}
}
}
else if (ent is BlockReference br)
{
try
{
var ext = br.GeometricExtents;
// 只要块的一部分落在搜索区域内,我们就考虑其最左边界
if (ext.MaxPoint.X > searchMinX && ext.MinPoint.X < searchMaxX &&
ext.MaxPoint.Y > searchMinY && ext.MinPoint.Y < searchMaxY)
{
// 确保最左边界在合理范围内(不小于搜索起点)
// 如果块非常大(如整个图框块),我们要小心不要把图框左边当成表格左边
// 假设表格块通常比起点 (0.25w) 要靠右
if (ext.MinPoint.X > searchMinX)
{
candidatesX.Add(ext.MinPoint.X);
}
}
}
catch { }
}
}
// 默认最左边界为图框右边界
double boundaryX = f.MaxPoint.X;
bool foundTable = false;
if (candidatesX.Count > 0)
{
// 取最小值作为表格左边界
boundaryX = candidatesX.Min();
foundTable = true;
}
double availableWidth;
if (foundTable)
{
// 如果找到了表格,宽度 = 表格左边界 - 插入点 - 留白
availableWidth = boundaryX - insertX - 5.0;
}
else
{
// 没找到表格 -> 使用更保守的默认比例 (33%),避免覆盖
availableWidth = w * 0.33;
}
// 安全检查:如果计算出的宽度太小(比如小于总宽 10%),保留最小可读宽度或回退
if (availableWidth < w * 0.10)
{
// 空间过小,强行给予 30% 宽度,可能会重叠但保证文字显示
availableWidth = w * 0.30;
}
mt.Width = availableWidth;
mt.ColorIndex = 7;
mt.Layer = "0";