增加参数保存功能,修复这个功能bug,现在可以用了
This commit is contained in:
parent
2a4894657f
commit
fed5a15745
@ -264,11 +264,71 @@ namespace CadParamPluging.Cad
|
||||
{
|
||||
public int Score;
|
||||
public string Kind;
|
||||
public ObjectId TargetId; // Added for persistence tracking
|
||||
public string PlainText;
|
||||
public string OriginalContents;
|
||||
public Action<string> Apply;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在实体扩展字典中保存原始模板文本,以便下次重新生成时可以再次使用
|
||||
/// </summary>
|
||||
private static void SetNoteTemplateData(Transaction tr, ObjectId id, string template)
|
||||
{
|
||||
if (string.IsNullOrEmpty(template)) return;
|
||||
|
||||
var ent = tr.GetObject(id, OpenMode.ForWrite) as Entity;
|
||||
if (ent == null) return;
|
||||
|
||||
if (ent.ExtensionDictionary.IsNull)
|
||||
{
|
||||
ent.CreateExtensionDictionary();
|
||||
}
|
||||
|
||||
var dict = (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForWrite);
|
||||
const string DictName = "CadParamPluging_NoteData";
|
||||
|
||||
// XRecord
|
||||
var rb = new ResultBuffer(new TypedValue((int)DxfCode.Text, template));
|
||||
var xrec = new Xrecord { Data = rb };
|
||||
|
||||
if (dict.Contains(DictName))
|
||||
{
|
||||
var oldId = dict.GetAt(DictName);
|
||||
var oldXrec = (Xrecord)tr.GetObject(oldId, OpenMode.ForWrite);
|
||||
oldXrec.Data = rb;
|
||||
}
|
||||
else
|
||||
{
|
||||
dict.SetAt(DictName, xrec);
|
||||
tr.AddNewlyCreatedDBObject(xrec, true);
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetNoteTemplateData(Transaction tr, ObjectId id)
|
||||
{
|
||||
var ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
|
||||
if (ent == null || ent.ExtensionDictionary.IsNull) return null;
|
||||
|
||||
var dict = (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForRead);
|
||||
const string DictName = "CadParamPluging_NoteData";
|
||||
|
||||
if (!dict.Contains(DictName)) return null;
|
||||
|
||||
var xrec = (Xrecord)tr.GetObject(dict.GetAt(DictName), OpenMode.ForRead);
|
||||
var rb = xrec.Data;
|
||||
if (rb == null) return null;
|
||||
|
||||
foreach (var tv in rb)
|
||||
{
|
||||
if (tv.TypeCode == (int)DxfCode.Text)
|
||||
{
|
||||
return tv.Value as string;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static NoteApplyResult ApplyNoteTemplate(CadContext ctx, string layoutName, bool scanModelSpace, TemplateSchemaDefinition schema, ParamBag bag)
|
||||
{
|
||||
if (ctx == null)
|
||||
@ -291,8 +351,8 @@ namespace CadParamPluging.Cad
|
||||
return new NoteApplyResult { Applied = false, Message = "未配置附注绑定,跳过。" };
|
||||
}
|
||||
|
||||
var tr = ctx.Transaction;
|
||||
var db = ctx.Database;
|
||||
var tr = ctx.Transaction;
|
||||
|
||||
var space = GetTargetSpace(tr, db, layoutName, scanModelSpace);
|
||||
if (space == null)
|
||||
@ -300,6 +360,29 @@ namespace CadParamPluging.Cad
|
||||
return new NoteApplyResult { Applied = false, Message = "未找到目标空间,跳过附注替换。" };
|
||||
}
|
||||
|
||||
// 1. 优先查找已经标记过的 Note Entity (重生成场景)
|
||||
foreach (ObjectId id in space)
|
||||
{
|
||||
var storedTemplate = GetNoteTemplateData(tr, id);
|
||||
if (!string.IsNullOrEmpty(storedTemplate))
|
||||
{
|
||||
// 找到了之前的附注,使用存储的模板重新渲染
|
||||
var effectiveStored = NoteTemplateEngine.BuildEffectiveValueKeyBindings(schema.NoteBindings);
|
||||
var renderedStored = NoteTemplateEngine.Render(storedTemplate, effectiveStored, bag.GetString);
|
||||
|
||||
ApplyNoteTextToObject(tr, id, "ExistingTaggedNote", renderedStored, null);
|
||||
|
||||
return new NoteApplyResult
|
||||
{
|
||||
Applied = true,
|
||||
TargetKind = "ExistingTaggedNote",
|
||||
PlaceholderCountInDwg = 0,
|
||||
RenderedText = renderedStored,
|
||||
Message = "已更新现有附注 (基于存储的模板)。"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
var candidates = new List<NoteCandidate>();
|
||||
var visitedBlocks = new HashSet<ObjectId>();
|
||||
foreach (ObjectId id in space)
|
||||
@ -368,13 +451,16 @@ namespace CadParamPluging.Cad
|
||||
space.AppendEntity(mt);
|
||||
tr.AddNewlyCreatedDBObject(mt, true);
|
||||
|
||||
// 关键:标记该实体,保存模板文本
|
||||
SetNoteTemplateData(tr, mt.ObjectId, schema.NoteTemplateText);
|
||||
|
||||
return new NoteApplyResult
|
||||
{
|
||||
Applied = true,
|
||||
TargetKind = "CreatedMText",
|
||||
PlaceholderCountInDwg = 0,
|
||||
RenderedText = renderedForCreation,
|
||||
Message = "未找到附注定位,已自动创建 (左下角对齐)。"
|
||||
Message = "未找到附注定位,已自动创建并标记。"
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -405,13 +491,18 @@ namespace CadParamPluging.Cad
|
||||
var rendered = NoteTemplateEngine.Render(templateText, effective, bag.GetString);
|
||||
best.Apply(rendered);
|
||||
|
||||
if (!best.TargetId.IsNull)
|
||||
{
|
||||
SetNoteTemplateData(tr, best.TargetId, templateText);
|
||||
}
|
||||
|
||||
return new NoteApplyResult
|
||||
{
|
||||
Applied = true,
|
||||
TargetKind = best.Kind,
|
||||
PlaceholderCountInDwg = placeholderCount,
|
||||
RenderedText = rendered,
|
||||
Message = "附注已替换。"
|
||||
Message = "附注已替换并标记。"
|
||||
};
|
||||
}
|
||||
|
||||
@ -533,6 +624,7 @@ namespace CadParamPluging.Cad
|
||||
{
|
||||
Score = score,
|
||||
Kind = kind,
|
||||
TargetId = id, // Store ID
|
||||
PlainText = plainText,
|
||||
OriginalContents = originalContents,
|
||||
Apply = rendered => ApplyNoteTextToObject(tr, id, kind, rendered, originalContents)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user