修改备注尺寸线位置和样式

This commit is contained in:
sladro 2025-12-23 19:01:08 +08:00
parent 7bc3a7732a
commit 5ba9f3e08d

View File

@ -615,7 +615,7 @@ namespace CadParamPluging.Cad
}
/// <summary>
/// 轧制:内径标注(从对称轴到右边内孔,标注在图形内部上方
/// 轧制:内径标注(从对称轴到右边内孔,放到下方并与最小壁厚标注共线
/// </summary>
private static void DrawInnerDiameterDimensionHalf(DrawingContext ctx, double oxAxis, double xInnerRight, double yBottom, double height,
double diameter, double? tolPlus, double? tolMinus, double? diameterPrime)
@ -626,12 +626,14 @@ namespace CadParamPluging.Cad
: baseText;
double innerRadius = xInnerRight - oxAxis;
// 标注放在图形顶部稍上方
// 放到下方,并与最小壁厚(30min)同一条水平尺寸线yBottom - 10
var dimLineY = yBottom - 10;
AddLinearDim(
ctx,
new Point3d(oxAxis, yBottom + height, 0),
new Point3d(xInnerRight, yBottom + height, 0),
new Point3d(oxAxis + innerRadius / 2, yBottom + height + 20, 0),
new Point3d(oxAxis, yBottom, 0),
new Point3d(xInnerRight, yBottom, 0),
new Point3d(oxAxis + innerRadius / 2, dimLineY, 0),
0,
dimText);
}
@ -1514,27 +1516,37 @@ namespace CadParamPluging.Cad
return baseText;
}
var result = baseText;
// 公差显示为上下并排(堆叠文本)。
// AutoCAD MText stack: \Supper#lower; => 上下堆叠且无分隔线。
// 这里将公差缩小到 0.7 倍以贴近常见标注样式。
const double tolScale = 0.7;
var plusStr = string.Empty;
var minusStr = string.Empty;
if (tolPlus.HasValue)
{
plusStr = tolPlus.Value >= 0
? $"+{FormatDimNumber(tolPlus.Value)}"
: $"{FormatDimNumber(tolPlus.Value)}";
}
if (tolMinus.HasValue)
{
// 下偏差通常显示为负号;如果传入为正数则强制加上负号。
minusStr = tolMinus.Value <= 0
? $"{FormatDimNumber(tolMinus.Value)}"
: $"-{FormatDimNumber(tolMinus.Value)}";
}
// 公差格式: 基本尺寸 +上差/-下差
if (tolPlus.HasValue && tolMinus.HasValue)
{
var plusStr = tolPlus.Value >= 0 ? $"+{tolPlus.Value}" : $"{tolPlus.Value}";
var minusStr = tolMinus.Value >= 0 ? $"+{tolMinus.Value}" : $"{tolMinus.Value}";
result += $"({plusStr}/{minusStr})";
}
else if (tolPlus.HasValue)
{
var plusStr = tolPlus.Value >= 0 ? $"+{tolPlus.Value}" : $"{tolPlus.Value}";
result += $"({plusStr})";
}
else if (tolMinus.HasValue)
{
var minusStr = tolMinus.Value >= 0 ? $"+{tolMinus.Value}" : $"{tolMinus.Value}";
result += $"({minusStr})";
return $"{baseText}{{\\H{tolScale}x;\\S{plusStr}#{minusStr};}}";
}
return result;
// 只有单边公差时,仍缩小显示。
var single = tolPlus.HasValue ? plusStr : minusStr;
return $"{baseText}{{\\H{tolScale}x;{single}}}";
}
private static void AddLinearDim(DrawingContext ctx, Point3d pt1, Point3d pt2, Point3d dimLinePt,
@ -1553,6 +1565,7 @@ namespace CadParamPluging.Cad
try { dim.SetDatabaseDefaults(); } catch { }
try { dim.Normal = Vector3d.ZAxis; } catch { }
TryApplyDimSizeOverrides(dim);
TryApplyDimLayoutOverrides(dim, pt1, pt2, dimLinePt, rotRad);
ctx.Style?.Apply(dim, DrawingStyleManager.Role.Dimension);
ctx.Btr.AppendEntity(dim);
ctx.Tr.AddNewlyCreatedDBObject(dim, true);
@ -1595,6 +1608,80 @@ namespace CadParamPluging.Cad
TrySetDimProp(dim, "Dimexo", 1.0);
}
private static void TryApplyDimLayoutOverrides(Dimension dim, Point3d pt1, Point3d pt2, Point3d dimLinePt, double rotRad)
{
if (dim == null)
{
return;
}
// 1) 文本与尺寸线平行(避免模板把文字强制水平)。
TrySetDimProp(dim, "Dimtih", (short)0);
TrySetDimProp(dim, "Dimtoh", (short)0);
// 2) 不抑制尺寸界线(修复半圆图左侧中心线位置“尺寸界线为空”的情况)。
TrySetDimProp(dim, "Dimse1", false);
TrySetDimProp(dim, "Dimse2", false);
// 3) 尺寸线位于尺寸(文字)上方:将文字整体移到尺寸线的“下方”(相对尺寸线法向的顺时针侧)。
try
{
var dimtxt = TryGetDoubleProp(dim, "Dimtxt") ?? 3.5;
var dimgap = TryGetDoubleProp(dim, "Dimgap") ?? 1.0;
var offset = Math.Max(1.0, dimtxt + dimgap);
var v = new Vector3d(-Math.Sin(rotRad), Math.Cos(rotRad), 0); // 尺寸线法向(逆时针)
var q1 = ProjectToLineAlongNormal(pt1, dimLinePt, v);
var q2 = ProjectToLineAlongNormal(pt2, dimLinePt, v);
var mid = new Point3d((q1.X + q2.X) / 2.0, (q1.Y + q2.Y) / 2.0, 0);
var textPos = mid - v.MultiplyBy(offset);
TrySetDimProp(dim, "TextPosition", textPos);
TrySetDimProp(dim, "TextRotation", rotRad);
}
catch
{
// ignore
}
}
private static Point3d ProjectToLineAlongNormal(Point3d p, Point3d linePoint, Vector3d normal)
{
// 线:通过 linePoint方向与 normal 垂直(即 (x-linePoint)·normal = 0
// 沿 normal 方向投影q = p - normal * ((p-linePoint)·normal)
var d = (p - linePoint).DotProduct(normal);
return p - normal.MultiplyBy(d);
}
private static double? TryGetDoubleProp(object obj, string propName)
{
if (obj == null || string.IsNullOrWhiteSpace(propName))
{
return null;
}
try
{
var prop = obj.GetType().GetProperty(propName);
if (prop == null || !prop.CanRead)
{
return null;
}
var v = prop.GetValue(obj, null);
if (v is double d) return d;
if (v is float f) return f;
if (v is int i) return i;
if (v is short s) return s;
return null;
}
catch
{
return null;
}
}
private static void TrySetDimProp(object obj, string propName, double value)
{
if (obj == null || string.IsNullOrWhiteSpace(propName))
@ -1621,6 +1708,126 @@ namespace CadParamPluging.Cad
prop.SetValue(obj, (float)value, null);
return;
}
if (prop.PropertyType == typeof(int))
{
prop.SetValue(obj, (int)Math.Round(value), null);
return;
}
if (prop.PropertyType == typeof(short))
{
prop.SetValue(obj, (short)Math.Round(value), null);
return;
}
if (prop.PropertyType == typeof(bool))
{
prop.SetValue(obj, Math.Abs(value) > 0.000001, null);
return;
}
}
catch
{
// ignore
}
}
private static void TrySetDimProp(object obj, string propName, bool value)
{
if (obj == null || string.IsNullOrWhiteSpace(propName))
{
return;
}
try
{
var prop = obj.GetType().GetProperty(propName);
if (prop == null || !prop.CanWrite)
{
return;
}
if (prop.PropertyType == typeof(bool))
{
prop.SetValue(obj, value, null);
return;
}
if (prop.PropertyType == typeof(short))
{
prop.SetValue(obj, (short)(value ? 1 : 0), null);
return;
}
if (prop.PropertyType == typeof(int))
{
prop.SetValue(obj, value ? 1 : 0, null);
}
}
catch
{
// ignore
}
}
private static void TrySetDimProp(object obj, string propName, short value)
{
if (obj == null || string.IsNullOrWhiteSpace(propName))
{
return;
}
try
{
var prop = obj.GetType().GetProperty(propName);
if (prop == null || !prop.CanWrite)
{
return;
}
if (prop.PropertyType == typeof(short))
{
prop.SetValue(obj, value, null);
return;
}
if (prop.PropertyType == typeof(int))
{
prop.SetValue(obj, (int)value, null);
return;
}
if (prop.PropertyType == typeof(double))
{
prop.SetValue(obj, (double)value, null);
}
}
catch
{
// ignore
}
}
private static void TrySetDimProp(object obj, string propName, Point3d value)
{
if (obj == null || string.IsNullOrWhiteSpace(propName))
{
return;
}
try
{
var prop = obj.GetType().GetProperty(propName);
if (prop == null || !prop.CanWrite)
{
return;
}
if (prop.PropertyType == typeof(Point3d))
{
prop.SetValue(obj, value, null);
}
}
catch
{