修复附注插入问题
This commit is contained in:
parent
e44a9a81ab
commit
2e2dc274cc
@ -313,7 +313,73 @@ namespace CadParamPluging.Cad
|
||||
|
||||
if (candidates.Count == 0)
|
||||
{
|
||||
return new NoteApplyResult { Applied = false, Message = "未找到附注文本目标(包含‘附注’且包含占位符*),跳过。" };
|
||||
// 尝试基于白框定位自动创建附注
|
||||
if (!string.IsNullOrWhiteSpace(schema.NoteTemplateText))
|
||||
{
|
||||
// 基于当前 Space 查找白框,以支持 Layout
|
||||
var spaceEntList = space.Cast<ObjectId>()
|
||||
.Select(id =>
|
||||
{
|
||||
try { return tr.GetObject(id, OpenMode.ForRead, false) as Entity; }
|
||||
catch { return null; }
|
||||
})
|
||||
.Where(e => e != null && !e.IsErased)
|
||||
.ToList();
|
||||
|
||||
var frame = ComputeWhiteFrameExtentsFromEntities(tr, spaceEntList);
|
||||
if (frame.HasValue)
|
||||
{
|
||||
var f = frame.Value;
|
||||
var w = f.MaxPoint.X - f.MinPoint.X;
|
||||
var h = f.MaxPoint.Y - f.MinPoint.Y;
|
||||
|
||||
// 改为左下角锚定 (BottomLeft),实现"贴合左边和下边"的效果
|
||||
// MText 以 BottomLeft 对齐时,插入点位于文字块左下角,内容向上生长
|
||||
var insertX = f.MinPoint.X + w * 0.005; // 左边距 0.5%
|
||||
var insertY = f.MinPoint.Y + h * 0.005; // 下边距 0.5%
|
||||
var insertPoint = new Point3d(insertX, insertY, 0);
|
||||
|
||||
var effectiveForCreation = NoteTemplateEngine.BuildEffectiveValueKeyBindings(schema.NoteBindings);
|
||||
var renderedForCreation = NoteTemplateEngine.Render(schema.NoteTemplateText, effectiveForCreation, bag.GetString);
|
||||
|
||||
var mt = new MText();
|
||||
mt.Contents = ToMTextContents(renderedForCreation);
|
||||
mt.Location = insertPoint;
|
||||
mt.TextHeight = 3.5;
|
||||
mt.Attachment = AttachmentPoint.BottomLeft; // 关键:锚点在左下,文字向上延伸
|
||||
mt.Width = w * 0.45; // 宽度限制为45%
|
||||
mt.ColorIndex = 7;
|
||||
mt.Layer = "0";
|
||||
|
||||
try
|
||||
{
|
||||
var layerTbl = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
|
||||
if (layerTbl.Has("TEXT"))
|
||||
{
|
||||
mt.Layer = "TEXT";
|
||||
}
|
||||
else if (layerTbl.Has("文字"))
|
||||
{
|
||||
mt.Layer = "文字";
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
space.AppendEntity(mt);
|
||||
tr.AddNewlyCreatedDBObject(mt, true);
|
||||
|
||||
return new NoteApplyResult
|
||||
{
|
||||
Applied = true,
|
||||
TargetKind = "CreatedMText",
|
||||
PlaceholderCountInDwg = 0,
|
||||
RenderedText = renderedForCreation,
|
||||
Message = "未找到附注定位,已自动创建 (左下角对齐)。"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return new NoteApplyResult { Applied = false, Message = "未找到附注文本目标(包含‘附注’且包含占位符*),且无法自动定位创建,跳过。" };
|
||||
}
|
||||
|
||||
var best = candidates.OrderByDescending(c => c.Score).FirstOrDefault();
|
||||
@ -356,15 +422,11 @@ namespace CadParamPluging.Cad
|
||||
return null;
|
||||
}
|
||||
|
||||
if (scanModelSpace)
|
||||
// Default to ModelSpace if scanModelSpace requested OR layoutName is null/empty implicitly aiming for Model
|
||||
if (scanModelSpace || string.IsNullOrWhiteSpace(layoutName) || string.Equals(layoutName, "Model", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
|
||||
return (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(layoutName))
|
||||
{
|
||||
return null;
|
||||
return (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
|
||||
}
|
||||
|
||||
var layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
|
||||
@ -384,7 +446,7 @@ namespace CadParamPluging.Cad
|
||||
}
|
||||
|
||||
var layout = (Layout)tr.GetObject(layoutId, OpenMode.ForRead);
|
||||
return (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead);
|
||||
return (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite);
|
||||
}
|
||||
|
||||
private static void CollectNoteCandidates(Transaction tr, Entity ent, HashSet<ObjectId> visitedBlocks, List<NoteCandidate> candidates)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user