添加比例文字
This commit is contained in:
parent
34028c1ac6
commit
73ef21855a
@ -1493,5 +1493,116 @@ namespace CadParamPluging.Cad
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新图纸标题栏中的比例 - 查找"比例"标题文字并在其下方创建比例值文本
|
||||
/// </summary>
|
||||
public static bool UpdateScaleAttribute(CadContext ctx, string layoutName, bool scanModelSpace, double scaleFactor)
|
||||
{
|
||||
if (ctx == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var db = ctx.Database;
|
||||
var tr = ctx.Transaction;
|
||||
|
||||
// 格式化比例文本
|
||||
var scaleText = FormatScaleText(scaleFactor);
|
||||
|
||||
// 在ModelSpace中查找"比例"标题文字
|
||||
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
|
||||
var ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
|
||||
|
||||
Point3d? scaleLabelPos = null;
|
||||
double textHeight = 2.5;
|
||||
string textStyle = "STANDARD";
|
||||
|
||||
// 遍历所有实体,查找"比例"文字
|
||||
foreach (ObjectId id in ms)
|
||||
{
|
||||
var ent = tr.GetObject(id, OpenMode.ForRead, false) as Entity;
|
||||
if (ent == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ent is DBText dbText)
|
||||
{
|
||||
var content = dbText.TextString?.Trim() ?? string.Empty;
|
||||
var contentNoSpace = content.Replace(" ", "").Replace(" ", "");
|
||||
// 匹配:去掉空格后是"比例"
|
||||
if (contentNoSpace == "比例")
|
||||
{
|
||||
scaleLabelPos = dbText.Position;
|
||||
textHeight = dbText.Height;
|
||||
textStyle = dbText.TextStyleName ?? "STANDARD";
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (ent is MText mText)
|
||||
{
|
||||
var content = mText.Contents?.Trim() ?? string.Empty;
|
||||
var contentNoSpace = content.Replace(" ", "").Replace(" ", "");
|
||||
if (contentNoSpace == "比例")
|
||||
{
|
||||
scaleLabelPos = mText.Location;
|
||||
textHeight = mText.TextHeight;
|
||||
textStyle = mText.TextStyleName ?? "STANDARD";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!scaleLabelPos.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// 在"比例"文字下方创建比例值文本
|
||||
var valuePos = new Point3d(
|
||||
scaleLabelPos.Value.X,
|
||||
scaleLabelPos.Value.Y - textHeight * 2.5,
|
||||
scaleLabelPos.Value.Z
|
||||
);
|
||||
|
||||
var newText = new DBText
|
||||
{
|
||||
Position = valuePos,
|
||||
TextString = scaleText,
|
||||
Height = textHeight,
|
||||
ColorIndex = 7,
|
||||
Layer = "0"
|
||||
};
|
||||
|
||||
// 设置文字样式
|
||||
var textStyleTbl = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
|
||||
if (textStyleTbl.Has(textStyle))
|
||||
{
|
||||
newText.TextStyleId = textStyleTbl[textStyle];
|
||||
}
|
||||
|
||||
ms.AppendEntity(newText);
|
||||
tr.AddNewlyCreatedDBObject(newText, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static string FormatScaleText(double scaleFactor)
|
||||
{
|
||||
if (scaleFactor >= 0.9999 && scaleFactor <= 1.0001)
|
||||
{
|
||||
return "1:1";
|
||||
}
|
||||
|
||||
var ratio = 1.0 / scaleFactor;
|
||||
|
||||
if (Math.Abs(ratio - Math.Round(ratio)) < 0.05)
|
||||
{
|
||||
return $"1:{Math.Round(ratio)}";
|
||||
}
|
||||
|
||||
return $"1:{ratio:F1}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -673,6 +673,22 @@ namespace CadParamPluging.UI
|
||||
scaleFactor // 缩放比例
|
||||
);
|
||||
|
||||
// 更新标题栏中的比例属性
|
||||
var scaleUpdated = TemplateDrawingService.UpdateScaleAttribute(
|
||||
ctx,
|
||||
_selectedTemplate.LayoutName,
|
||||
_selectedModelWindow.HasValue,
|
||||
scaleFactor
|
||||
);
|
||||
if (scaleUpdated)
|
||||
{
|
||||
AppendLog($"已更新标题栏比例: {(scaleFactor >= 0.9999 ? "1:1" : $"1:{(1/scaleFactor):F1}")}");
|
||||
}
|
||||
else
|
||||
{
|
||||
AppendLog("未找到标题栏中的'比例'文字,无法更新比例");
|
||||
}
|
||||
|
||||
ctx.Commit();
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user