From 7bc3a7732a754863078a02d52727cc0c328c7ec8 Mon Sep 17 00:00:00 2001 From: sladro Date: Tue, 23 Dec 2025 18:03:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BA=BF=E5=AE=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cad/DrawingStyleManager.cs | 18 ++++- Cad/TemplateDrawingService.cs | 135 ++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 3 deletions(-) diff --git a/Cad/DrawingStyleManager.cs b/Cad/DrawingStyleManager.cs index 64f178c..6e75af6 100644 --- a/Cad/DrawingStyleManager.cs +++ b/Cad/DrawingStyleManager.cs @@ -57,13 +57,13 @@ namespace CadParamPluging.Cad case Role.OutlineBold: return new LayerSpec { LayerName = "粗实线", DefaultLinetypeName = "Continuous", DefaultLineWeight = LineWeight.LineWeight050, DefaultColorIndex = 7 }; case Role.PartContour: - return new LayerSpec { LayerName = "双点划线", DefaultLinetypeName = "PHANTOM", DefaultLineWeight = LineWeight.LineWeight025, DefaultColorIndex = 7 }; + return new LayerSpec { LayerName = "双点划线", DefaultLinetypeName = "PHANTOM", DefaultLineWeight = LineWeight.LineWeight015, DefaultColorIndex = 7 }; case Role.Centerline: - return new LayerSpec { LayerName = "中心线", DefaultLinetypeName = "CENTER", DefaultLineWeight = LineWeight.LineWeight025, DefaultColorIndex = 7 }; + return new LayerSpec { LayerName = "中心线", DefaultLinetypeName = "Continuous", DefaultLineWeight = LineWeight.LineWeight015, DefaultColorIndex = 7 }; case Role.Hidden: return new LayerSpec { LayerName = "虚线", DefaultLinetypeName = "DASHED", DefaultLineWeight = LineWeight.LineWeight025, DefaultColorIndex = 7 }; case Role.Hatch: - return new LayerSpec { LayerName = "剖面线", DefaultLinetypeName = "Continuous", DefaultLineWeight = LineWeight.LineWeight018, DefaultColorIndex = 4 }; + return new LayerSpec { LayerName = "剖面线", DefaultLinetypeName = "Continuous", DefaultLineWeight = LineWeight.LineWeight015, DefaultColorIndex = 4 }; case Role.Dimension: return new LayerSpec { LayerName = "尺寸标注", DefaultLinetypeName = "Continuous", DefaultLineWeight = LineWeight.LineWeight018, DefaultColorIndex = 7 }; case Role.Text: @@ -98,6 +98,18 @@ namespace CadParamPluging.Cad existingLayer.IsOff = false; existingLayer.IsFrozen = false; existingLayer.IsLocked = false; + + existingLayer.Color = Color.FromColorIndex(ColorMethod.ByAci, colorIndex); + existingLayer.LineWeight = lineWeight; + + if (!string.IsNullOrWhiteSpace(linetypeName)) + { + var lt = (LinetypeTable)_tr.GetObject(_db.LinetypeTableId, OpenMode.ForRead); + if (lt.Has(linetypeName)) + { + existingLayer.LinetypeObjectId = lt[linetypeName]; + } + } } } catch diff --git a/Cad/TemplateDrawingService.cs b/Cad/TemplateDrawingService.cs index 147832f..896de75 100644 --- a/Cad/TemplateDrawingService.cs +++ b/Cad/TemplateDrawingService.cs @@ -958,9 +958,144 @@ namespace CadParamPluging.Cad } } + try + { + // Keep the template's white frame but enforce the desired lineweight. + ApplyWhiteFrameLineWeight(ctx, LineWeight.LineWeight050); + } + catch + { + // ignore + } + return result; } + private static int ApplyWhiteFrameLineWeight(CadContext ctx, LineWeight lineWeight) + { + if (ctx == null) + { + return 0; + } + + var db = ctx.Database; + var tr = ctx.Transaction; + + var frameExtents = ComputeWhiteFrameExtents(ctx); + if (!frameExtents.HasValue) + { + return 0; + } + + var frame = frameExtents.Value; + var w = frame.MaxPoint.X - frame.MinPoint.X; + var h = frame.MaxPoint.Y - frame.MinPoint.Y; + var tol = Math.Max(w, h) * 0.01; // 1% + + var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); + var ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); + + var updated = 0; + foreach (ObjectId id in ms) + { + var ent = tr.GetObject(id, OpenMode.ForWrite, false) as Entity; + if (ent == null || ent.IsErased) + { + continue; + } + + if (!(ent is Line) && !(ent is Polyline)) + { + continue; + } + + if (!IsWhiteColor(ent, tr)) + { + continue; + } + + if (!IsOnFrameBoundary(ent, frame, tol)) + { + continue; + } + + try { ent.LineWeight = lineWeight; } catch { } + + try + { + var layer = tr.GetObject(ent.LayerId, OpenMode.ForWrite) as LayerTableRecord; + if (layer != null) + { + layer.LineWeight = lineWeight; + } + } + catch + { + // ignore + } + + updated++; + } + + return updated; + } + + private static bool IsOnFrameBoundary(Entity ent, Extents3d frame, double tol) + { + try + { + if (ent is Line line) + { + var sp = line.StartPoint; + var ep = line.EndPoint; + + var minX = Math.Min(sp.X, ep.X); + var maxX = Math.Max(sp.X, ep.X); + var minY = Math.Min(sp.Y, ep.Y); + var maxY = Math.Max(sp.Y, ep.Y); + + var isHorizontal = Math.Abs(sp.Y - ep.Y) <= tol; + if (isHorizontal) + { + var y = (sp.Y + ep.Y) / 2.0; + var onTop = Math.Abs(y - frame.MaxPoint.Y) <= tol; + var onBottom = Math.Abs(y - frame.MinPoint.Y) <= tol; + if ((onTop || onBottom) + && minX <= frame.MinPoint.X + tol + && maxX >= frame.MaxPoint.X - tol) + { + return true; + } + } + + var isVertical = Math.Abs(sp.X - ep.X) <= tol; + if (isVertical) + { + var x = (sp.X + ep.X) / 2.0; + var onLeft = Math.Abs(x - frame.MinPoint.X) <= tol; + var onRight = Math.Abs(x - frame.MaxPoint.X) <= tol; + if ((onLeft || onRight) + && minY <= frame.MinPoint.Y + tol + && maxY >= frame.MaxPoint.Y - tol) + { + return true; + } + } + + return false; + } + + var ext = ent.GeometricExtents; + var matchX = Math.Abs(ext.MinPoint.X - frame.MinPoint.X) <= tol && Math.Abs(ext.MaxPoint.X - frame.MaxPoint.X) <= tol; + var matchY = Math.Abs(ext.MinPoint.Y - frame.MinPoint.Y) <= tol && Math.Abs(ext.MaxPoint.Y - frame.MaxPoint.Y) <= tol; + return matchX && matchY; + } + catch + { + return false; + } + } + /// /// 删除模板中残留的“尺寸占位符”文本(例如 (Φ*)),避免与新生成的真实尺寸标注混淆。 ///