feat: 新增 TemplateDrawingService,用于打开CAD模板图纸并移除匹配参数标注。

This commit is contained in:
sladro 2026-03-02 10:37:07 +08:00
parent aff5d6ad23
commit 2c816847f4

View File

@ -942,13 +942,79 @@ namespace CadParamPluging.Cad
if (obj is DBText t)
{
t.TextString = CollapseToSingleLine(rendered);
var collapsed = CollapseToSingleLine(rendered);
if (collapsed.Length > 250)
{
// DBText 在 CAD 中有硬性的 255 长度限制,长文本需升级为 MText
var newMt = new MText();
newMt.Contents = ToMTextContents(rendered); // 恢复原有折行
newMt.Location = t.Position;
newMt.TextHeight = t.Height;
newMt.Layer = t.Layer;
newMt.ColorIndex = t.ColorIndex;
newMt.TextStyleId = t.TextStyleId;
newMt.Rotation = t.Rotation;
if (!t.BlockId.IsNull)
{
var btr = (BlockTableRecord)tr.GetObject(t.BlockId, OpenMode.ForWrite);
btr.AppendEntity(newMt);
tr.AddNewlyCreatedDBObject(newMt, true);
t.Erase(true);
}
else
{
t.TextString = collapsed.Substring(0, 250);
}
}
else
{
t.TextString = collapsed;
}
return;
}
if (obj is AttributeReference att)
{
att.TextString = CollapseToSingleLine(rendered);
var collapsed = CollapseToSingleLine(rendered);
if (collapsed.Length > 250)
{
// AttributeReference 同样受 255 长度限制,用同等 MText 挂载并隐去原文本
var newMt = new MText();
newMt.Contents = ToMTextContents(rendered);
newMt.Location = att.Position;
newMt.TextHeight = att.Height;
newMt.Layer = att.Layer;
newMt.ColorIndex = att.ColorIndex;
newMt.TextStyleId = att.TextStyleId;
newMt.Rotation = att.Rotation;
if (!att.OwnerId.IsNull)
{
var blk = tr.GetObject(att.OwnerId, OpenMode.ForRead) as BlockReference;
if (blk != null && !blk.OwnerId.IsNull)
{
var space = (BlockTableRecord)tr.GetObject(blk.OwnerId, OpenMode.ForWrite);
space.AppendEntity(newMt);
tr.AddNewlyCreatedDBObject(newMt, true);
att.TextString = "";
att.Invisible = true;
}
else
{
att.TextString = collapsed.Substring(0, 250);
}
}
else
{
att.TextString = collapsed.Substring(0, 250);
}
}
else
{
att.TextString = collapsed;
}
}
}
@ -964,9 +1030,18 @@ namespace CadParamPluging.Cad
// 提取每个占位符被替换成的值
var replacements = ExtractPlaceholderReplacements(plainTemplate, renderedPlainText);
if (replacements.Count == 0)
if (replacements == null || replacements.Count == 0)
{
return ToMTextContents(renderedPlainText);
string newContents = ToMTextContents(renderedPlainText);
if (originalContents.StartsWith("{") && originalContents.Contains(";"))
{
var match = System.Text.RegularExpressions.Regex.Match(originalContents, @"^(\{\\[^;]+;)");
if (match.Success)
{
newContents = match.Groups[1].Value + newContents + "}";
}
}
return newContents;
}
// 在原始MText内容中直接替换*占位符
@ -1057,24 +1132,68 @@ namespace CadParamPluging.Cad
{
// ****保持不变rendered中也应该有****
ti += 4;
ri += 4;
if (ri + 4 <= rendered.Length && rendered.Substring(ri, 4) == "****")
{
ri += 4;
}
}
else
{
// 每个*对应rendered中的一个字符
for (var k = 0; k < starCount && ri < rendered.Length; k++)
// 提取替换内容
int nextTi = ti + starCount;
string nextLiteral = "";
while (nextTi < template.Length && template[nextTi] != '*')
{
replacements.Add(rendered[ri].ToString());
ti++;
ri++;
nextLiteral += template[nextTi];
nextTi++;
}
if (string.IsNullOrEmpty(nextLiteral))
{
// 如果后面没有文字了,那么直接把 rendered 中剩下的所有文字都给第一颗 *
string rep = rendered.Substring(ri);
for (int k = 0; k < starCount; k++)
{
replacements.Add(k == 0 ? rep : "");
}
ri = rendered.Length;
ti += starCount;
}
else
{
// 在 rendered 中寻找 nextLiteral
int foundIndex = rendered.IndexOf(nextLiteral, ri);
if (foundIndex >= 0)
{
string rep = rendered.Substring(ri, foundIndex - ri);
for (int k = 0; k < starCount; k++)
{
replacements.Add(k == 0 ? rep : "");
}
ri = foundIndex;
ti += starCount;
}
else
{
// 找不到说明因为换行、标点清理或重编号导致了较大差异,直接回退整段格式
return new List<string>();
}
}
}
}
else
{
// 非*字符,两边应该相同,直接跳过
ti++;
ri++;
if (tc == rendered[ri])
{
ti++;
ri++;
}
else
{
// 不匹配说明有由于处理(如空行、或者清理了符号)导致文字不一致
// 直接返回以使用完全的 ToMTextContents(rendered) 包含 MText 的原字体格式
return new List<string>();
}
}
}