diff --git a/Cad/TemplateDrawingService.cs b/Cad/TemplateDrawingService.cs
index c2f1592..262e966 100644
--- a/Cad/TemplateDrawingService.cs
+++ b/Cad/TemplateDrawingService.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Text.RegularExpressions;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
@@ -10,6 +11,12 @@ namespace CadParamPluging.Cad
{
public static class TemplateDrawingService
{
+ private static readonly Regex MatchFieldRegex = new Regex(
+ @"^\s*(交付状态|工艺方法|结构特征|特殊条件)\s*[::]\s*(.+?)\s*$",
+ RegexOptions.Compiled);
+
+ private static readonly Regex MTextFormatRegex = new Regex(@"\\[A-Za-z]+[^;]*;", RegexOptions.Compiled);
+
public static Document CreateDocumentFromTemplate(TemplateInfo template)
{
var doc = Application.DocumentManager.Add(template.FilePath);
@@ -17,6 +24,162 @@ namespace CadParamPluging.Cad
return doc;
}
+ ///
+ /// 移除模板中用于“匹配模板/图纸”的参数标注文本(交付状态/工艺方法/结构特征/特殊条件)。
+ /// 仅清理当前生成图纸中目标空间(Layout 或 ModelSpace)里的文本实体/块属性。
+ ///
+ public static int RemoveMatchParameterAnnotations(CadContext ctx, string layoutName, bool scanModelSpace)
+ {
+ if (ctx == null)
+ {
+ throw new ArgumentNullException(nameof(ctx));
+ }
+
+ var db = ctx.Database;
+ var tr = ctx.Transaction;
+
+ if (scanModelSpace)
+ {
+ var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
+ var ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
+ return EraseMatchTextEntities(tr, ms);
+ }
+
+ if (string.IsNullOrWhiteSpace(layoutName))
+ {
+ return 0;
+ }
+
+ var layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
+ ObjectId layoutId = ObjectId.Null;
+ foreach (DBDictionaryEntry entry in layoutDict)
+ {
+ if (string.Equals(entry.Key, layoutName, StringComparison.OrdinalIgnoreCase))
+ {
+ layoutId = entry.Value;
+ break;
+ }
+ }
+
+ if (layoutId.IsNull)
+ {
+ return 0;
+ }
+
+ var layout = (Layout)tr.GetObject(layoutId, OpenMode.ForRead);
+ var btr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite);
+ return EraseMatchTextEntities(tr, btr);
+ }
+
+ private static int EraseMatchTextEntities(Transaction tr, BlockTableRecord btr)
+ {
+ if (tr == null || btr == null)
+ {
+ return 0;
+ }
+
+ var removed = 0;
+ var ids = btr.Cast().ToList();
+ foreach (var id in ids)
+ {
+ var ent = tr.GetObject(id, OpenMode.ForWrite, false) as Entity;
+ if (ent == null)
+ {
+ continue;
+ }
+
+ if (ent is DBText t)
+ {
+ if (ContainsMatchField(t.TextString))
+ {
+ ent.Erase(true);
+ removed++;
+ }
+ continue;
+ }
+
+ if (ent is MText mt)
+ {
+ var plain = SimplifyMText(mt.Contents);
+ if (ContainsMatchField(plain))
+ {
+ ent.Erase(true);
+ removed++;
+ }
+ continue;
+ }
+
+ if (ent is BlockReference br)
+ {
+ foreach (ObjectId attId in br.AttributeCollection)
+ {
+ var att = tr.GetObject(attId, OpenMode.ForWrite, false) as AttributeReference;
+ if (att == null)
+ {
+ continue;
+ }
+
+ if (ContainsMatchField(att.TextString))
+ {
+ try
+ {
+ att.Erase(true);
+ removed++;
+ }
+ catch
+ {
+ // ignore
+ }
+ }
+ }
+ }
+ }
+
+ return removed;
+ }
+
+ private static bool ContainsMatchField(string raw)
+ {
+ if (string.IsNullOrWhiteSpace(raw))
+ {
+ return false;
+ }
+
+ var lines = raw
+ .Replace("\r\n", "\n")
+ .Replace("\r", "\n")
+ .Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
+ .Select(s => (s ?? string.Empty).Trim())
+ .Where(s => s.Length > 0);
+
+ foreach (var line in lines)
+ {
+ if (MatchFieldRegex.IsMatch(line))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private static string SimplifyMText(string contents)
+ {
+ if (string.IsNullOrEmpty(contents))
+ {
+ return string.Empty;
+ }
+
+ var s = contents
+ .Replace("\\P", "\n")
+ .Replace("\\p", "\n")
+ .Replace("\\~", " ");
+
+ s = s.Replace("{", string.Empty).Replace("}", string.Empty);
+ s = MTextFormatRegex.Replace(s, string.Empty);
+ return s;
+ }
+
public static void KeepOnlyLayout(Document doc, string layoutName)
{
if (doc == null)
diff --git a/PluginEntry.cs b/PluginEntry.cs
index 4a1e593..156e860 100644
--- a/PluginEntry.cs
+++ b/PluginEntry.cs
@@ -16,13 +16,13 @@ namespace CadParamPluging
{
private static PaletteSet _palette;
private static bool _ribbonButtonCreated;
+ private static bool _firstShow = true;
private const string RibbonTabId = "CadParamPlugingTab";
private const string RibbonPanelId = "CadParamPlugingPanel";
private const string RibbonButtonId = "CadParamPlugingPanelButton";
public void Initialize()
{
- EnsurePaletteCreated();
CreateRibbonButton();
Application.Idle += OnApplicationIdle;
}
@@ -32,7 +32,7 @@ namespace CadParamPluging
// 可在此释放资源或记录日志
}
- private void EnsurePaletteCreated()
+ private void ShowPalette()
{
if (_palette == null)
{
@@ -41,23 +41,32 @@ namespace CadParamPluging
Style = PaletteSetStyles.ShowCloseButton |
PaletteSetStyles.ShowAutoHideButton |
PaletteSetStyles.Snappable,
- DockEnabled = DockSides.Right,
- Dock = DockSides.Right
+ DockEnabled = DockSides.Left | DockSides.Right
};
var panel = new ParamDrawingPanel();
_palette.Add("Main", panel);
_palette.MinimumSize = new Size(240, 200);
}
+
+ _palette.Visible = true;
+
+ if (_firstShow)
+ {
+ _firstShow = false;
+ Application.Idle += OnDockToRight;
+ }
+
+ _palette.Activate(0);
}
- private void ShowPalette()
+ private void OnDockToRight(object sender, EventArgs e)
{
- EnsurePaletteCreated();
-
- _palette.Dock = DockSides.Right;
- _palette.Visible = true;
- _palette.Activate(0);
+ Application.Idle -= OnDockToRight;
+ if (_palette != null)
+ {
+ _palette.Dock = DockSides.Right;
+ }
}
private void CreateRibbonButton()
diff --git a/UI/ParamDrawingPanel.cs b/UI/ParamDrawingPanel.cs
index 1a3ac6c..9455725 100644
--- a/UI/ParamDrawingPanel.cs
+++ b/UI/ParamDrawingPanel.cs
@@ -559,6 +559,15 @@ namespace CadParamPluging.UI
using (doc.LockDocument())
using (var ctx = new CadContext(doc))
{
+ var removed = TemplateDrawingService.RemoveMatchParameterAnnotations(
+ ctx,
+ _selectedTemplate.LayoutName,
+ _selectedModelWindow.HasValue);
+ if (removed > 0)
+ {
+ AppendLog($"已移除模板匹配参数标注文本: {removed} 处");
+ }
+
var cadService = new CadDrawingService(ctx);
DomainFacade.DrawByParams(_selectedTemplate, drawingParams, cadService);
ctx.Commit();