From f120d2e34ba4e08af53922317e84872ad8803ddc Mon Sep 17 00:00:00 2001 From: sladro Date: Thu, 18 Dec 2025 10:25:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=9F=E5=9B=BE=E5=92=8C=E6=AD=A3=E5=BC=8F=E7=94=9F=E5=9B=BE?= =?UTF-8?q?=E7=9A=84=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cad/HalfSectionDrawer.cs | 125 +++++++++++++++++++------------- UI/ParamDrawingPanel.cs | 153 ++++----------------------------------- 2 files changed, 89 insertions(+), 189 deletions(-) diff --git a/Cad/HalfSectionDrawer.cs b/Cad/HalfSectionDrawer.cs index 0284a7b..6f9b0e7 100644 --- a/Cad/HalfSectionDrawer.cs +++ b/Cad/HalfSectionDrawer.cs @@ -77,7 +77,7 @@ namespace CadParamPluging.Cad var tr = ctx.Transaction; var btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); - // 读取锻件尺寸 + // 读取尺寸 double outerDia = bag.GetDouble("OuterDiameter1"); double innerDia = bag.GetDouble("InnerDiameter2"); double height = bag.GetDouble("Height1"); @@ -87,44 +87,54 @@ namespace CadParamPluging.Cad return; } - // 锻件外轮廓(半剖:只绘制右侧) + // 绘制水平圆柱 (侧视图) + // 中心线 (X轴) - 已移除,以匹配参考图样式 + + // 1. 锻件外轮廓 + double R = outerDia / 2.0; var polyForging = new Polyline(); - polyForging.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0); - polyForging.AddVertexAt(1, new Point2d(outerDia / 2, 0), 0, 0, 0); - polyForging.AddVertexAt(2, new Point2d(outerDia / 2, height), 0, 0, 0); - polyForging.AddVertexAt(3, new Point2d(0, height), 0, 0, 0); - polyForging.Closed = false; - polyForging.ColorIndex = 7; + polyForging.AddVertexAt(0, new Point2d(0, -R), 0, 0, 0); + polyForging.AddVertexAt(1, new Point2d(height, -R), 0, 0, 0); + polyForging.AddVertexAt(2, new Point2d(height, R), 0, 0, 0); + polyForging.AddVertexAt(3, new Point2d(0, R), 0, 0, 0); + polyForging.Closed = true; + polyForging.ColorIndex = 7; // 白色 btr.AppendEntity(polyForging); tr.AddNewlyCreatedDBObject(polyForging, true); - // 锻件内孔轮廓 + // 3. 内孔轮廓 (虚线) if (innerDia > 0) { - var polyForgingInner = new Polyline(); - polyForgingInner.AddVertexAt(0, new Point2d(innerDia / 2, 0), 0, 0, 0); - polyForgingInner.AddVertexAt(1, new Point2d(innerDia / 2, height), 0, 0, 0); - polyForgingInner.Closed = false; - polyForgingInner.ColorIndex = 7; + double r = innerDia / 2.0; + TryLoadLinetype(db, tr, "DASHED"); - btr.AppendEntity(polyForgingInner); - tr.AddNewlyCreatedDBObject(polyForgingInner, true); + var lineHiddenTop = new Line(new Point3d(0, r, 0), new Point3d(height, r, 0)); + lineHiddenTop.ColorIndex = 2; // 黄色 + lineHiddenTop.Linetype = "DASHED"; + btr.AppendEntity(lineHiddenTop); + tr.AddNewlyCreatedDBObject(lineHiddenTop, true); + + var lineHiddenBot = new Line(new Point3d(0, -r, 0), new Point3d(height, -r, 0)); + lineHiddenBot.ColorIndex = 2; + lineHiddenBot.Linetype = "DASHED"; + btr.AppendEntity(lineHiddenBot); + tr.AddNewlyCreatedDBObject(lineHiddenBot, true); + + // 内孔标注 + AddLinearDim(db, tr, btr, new Point3d(0, r, 0), new Point3d(0, -r, 0), + new Point3d(height + 15, 0, 0), 90, $"%%c{innerDia}"); } - // 中心线 - double centerLineExt = 20.0; - var lineCenter = new Line( - new Point3d(-centerLineExt, height / 2, 0), - new Point3d(outerDia / 2 + centerLineExt, height / 2, 0)); - lineCenter.ColorIndex = 4; + // 外径标注 + AddLinearDim(db, tr, btr, new Point3d(0, R, 0), new Point3d(0, -R, 0), + new Point3d(-15, 0, 0), 90, $"%%c{outerDia}"); + + // 长度标注 + AddLinearDim(db, tr, btr, new Point3d(0, -R, 0), new Point3d(height, -R, 0), + new Point3d(height / 2, -R - 15, 0), 0, $"{height}min"); - TryLoadCenterLinetype(db, tr, lineCenter); - - btr.AppendEntity(lineCenter); - tr.AddNewlyCreatedDBObject(lineCenter, true); - - // 车加工态:绘制零件轮廓及填充 + // 4. 车加工零件 (填充) if (IsMachined(deliveryStatus)) { double outerDiaPrime = bag.GetDouble("OuterDiameter1Prime"); @@ -133,27 +143,28 @@ namespace CadParamPluging.Cad if (outerDiaPrime > 0 && heightPrime > 0) { - double partOffsetY = (height - heightPrime) / 2.0; - - // 零件轮廓(闭合) + double offsetX = (height - heightPrime) / 2.0; + double Rp = outerDiaPrime / 2.0; + + // 零件轮廓 var polyPart = new Polyline(); - polyPart.AddVertexAt(0, new Point2d(innerDiaPrime / 2, partOffsetY), 0, 0, 0); - polyPart.AddVertexAt(1, new Point2d(outerDiaPrime / 2, partOffsetY), 0, 0, 0); - polyPart.AddVertexAt(2, new Point2d(outerDiaPrime / 2, partOffsetY + heightPrime), 0, 0, 0); - polyPart.AddVertexAt(3, new Point2d(innerDiaPrime / 2, partOffsetY + heightPrime), 0, 0, 0); + polyPart.AddVertexAt(0, new Point2d(offsetX, -Rp), 0, 0, 0); + polyPart.AddVertexAt(1, new Point2d(offsetX + heightPrime, -Rp), 0, 0, 0); + polyPart.AddVertexAt(2, new Point2d(offsetX + heightPrime, Rp), 0, 0, 0); + polyPart.AddVertexAt(3, new Point2d(offsetX, Rp), 0, 0, 0); polyPart.Closed = true; polyPart.ColorIndex = 7; btr.AppendEntity(polyPart); tr.AddNewlyCreatedDBObject(polyPart, true); - // 剖面填充 + // 填充 try { var hatch = new Hatch(); + hatch.SetDatabaseDefaults(); hatch.Normal = new Vector3d(0, 0, 1); hatch.Elevation = 0.0; - hatch.SetDatabaseDefaults(); btr.AppendEntity(hatch); tr.AddNewlyCreatedDBObject(hatch, true); @@ -170,30 +181,46 @@ namespace CadParamPluging.Cad } catch { - // 填充失败时忽略 + // 忽略 } + + // 可以在这里添加零件尺寸标注 + AddLinearDim(db, tr, btr, new Point3d(offsetX, Rp, 0), new Point3d(offsetX, -Rp, 0), + new Point3d(offsetX - 5, 0, 0), 90, $"%%c{outerDiaPrime}"); + + AddLinearDim(db, tr, btr, new Point3d(offsetX, -Rp, 0), new Point3d(offsetX + heightPrime, -Rp, 0), + new Point3d(offsetX + heightPrime / 2, -Rp - 10, 0), 0, $"{heightPrime}"); } } } - private static void TryLoadCenterLinetype(Database db, Transaction tr, Line line) + private static void AddLinearDim(Database db, Transaction tr, BlockTableRecord btr, + Point3d pt1, Point3d pt2, Point3d dimLinePt, double rotationDeg, string textOverride) + { + try + { + double rotRad = rotationDeg * Math.PI / 180.0; + var dim = new RotatedDimension(rotRad, pt1, pt2, dimLinePt, textOverride, db.Dimstyle); + dim.ColorIndex = 4; // 青色 + btr.AppendEntity(dim); + tr.AddNewlyCreatedDBObject(dim, true); + } + catch + { + // 忽略标注错误 + } + } + + private static void TryLoadLinetype(Database db, Transaction tr, string linetypeName) { try { var linetypeTbl = (LinetypeTable)tr.GetObject(db.LinetypeTableId, OpenMode.ForRead); - if (linetypeTbl.Has("CENTER")) - { - line.Linetype = "CENTER"; - } - else + if (!linetypeTbl.Has(linetypeName)) { try { - db.LoadLineTypeFile("CENTER", "acad.lin"); - if (linetypeTbl.Has("CENTER")) - { - line.Linetype = "CENTER"; - } + db.LoadLineTypeFile(linetypeName, "acad.lin"); } catch { diff --git a/UI/ParamDrawingPanel.cs b/UI/ParamDrawingPanel.cs index 542e4d7..e318417 100644 --- a/UI/ParamDrawingPanel.cs +++ b/UI/ParamDrawingPanel.cs @@ -771,46 +771,19 @@ namespace CadParamPluging.UI try { // ========================================== - // 参数定义 - 调整比例接近参考图 + // 测试参数构造 // ========================================== + var bag = new ParamBag(); - // 1. 锻件尺寸参数 (扁平环形件) - double Φ1 = 500.0; // 锻件外径 - double a1 = 2.0; // 外径上差 - double b1 = -1.0; // 外径下差 + // 锻件参数 (调整为细长型,符合参考图比例) + bag.Set("OuterDiameter1", "160"); + bag.Set("InnerDiameter2", "100"); + bag.Set("Height1", "400"); - double Φ2 = 150.0; // 锻件内径 - double a2 = 1.5; // 内径上差 - double b2 = -0.5; // 内径下差 - - double H1 = 100.0; // 锻件高度 - double a3 = 1.0; // 高度上差 - double b3 = -1.0; // 高度下差 - - double T = 45.0; // 最小壁厚 (设定值) - - // --- 零件尺寸 --- - double Φ_1 = 485.0; // 零件外径 (Φ'1) - double Φ_2 = 165.0; // 零件内径 (Φ'2) - double H_1 = 90.0; // 零件高度 (H'1) - - - // ========================================== - // 2. 逻辑校验 (使用 T 参数) - // ========================================== - double actualWallThickness = (Φ1 - Φ2) / 2.0; - if (actualWallThickness < T) - { - AppendLog($"[警告] 实际壁厚 ({actualWallThickness}) 小于设定最小壁厚 T ({T})"); - } - - - // ========================================== - // 3. 绘图逻辑 (使用其余几何参数) - // ========================================== - - // 仅计算相对位置,不增加额外参数 - double partOffsetY = (H1 - H_1) / 2.0; + // 零件参数 (车加工) + bag.Set("OuterDiameter1Prime", "150"); + bag.Set("InnerDiameter2Prime", "110"); + bag.Set("Height1Prime", "380"); var doc = AcadApp.DocumentManager.MdiActiveDocument; if (doc == null) @@ -822,112 +795,12 @@ namespace CadParamPluging.UI using (doc.LockDocument()) using (var ctx = new CadContext(doc)) { - var db = ctx.Database; - var tr = ctx.Transaction; - var btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); - - // --- 半剖视图:只绘制右侧 --- + // 调用统一的绘图逻辑 + HalfSectionDrawer.Draw(ctx, bag, "车加工", "环形"); - // 锻件外轮廓 (不闭合,用于显示剖面边界) - var polyForging = new Polyline(); - polyForging.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0); // 中心线底部 - polyForging.AddVertexAt(1, new Point2d(Φ1 / 2, 0), 0, 0, 0); // 外径底部 - polyForging.AddVertexAt(2, new Point2d(Φ1 / 2, H1), 0, 0, 0); // 外径顶部 - polyForging.AddVertexAt(3, new Point2d(0, H1), 0, 0, 0); // 中心线顶部 - polyForging.Closed = false; - polyForging.ColorIndex = 7; // White - - // 锻件内孔轮廓 - var polyForgingInner = new Polyline(); - polyForgingInner.AddVertexAt(0, new Point2d(Φ2 / 2, 0), 0, 0, 0); - polyForgingInner.AddVertexAt(1, new Point2d(Φ2 / 2, H1), 0, 0, 0); - polyForgingInner.Closed = false; - polyForgingInner.ColorIndex = 7; // White - - // 零件轮廓 (闭合,用于填充) - var polyPart = new Polyline(); - polyPart.AddVertexAt(0, new Point2d(Φ_2 / 2, partOffsetY), 0, 0, 0); - polyPart.AddVertexAt(1, new Point2d(Φ_1 / 2, partOffsetY), 0, 0, 0); - polyPart.AddVertexAt(2, new Point2d(Φ_1 / 2, partOffsetY + H_1), 0, 0, 0); - polyPart.AddVertexAt(3, new Point2d(Φ_2 / 2, partOffsetY + H_1), 0, 0, 0); - polyPart.Closed = true; - polyPart.ColorIndex = 7; // White - - // --- 绘制中心线 (水平,表示轴线) --- - double centerLineExt = 20.0; - var lineCenter = new Line( - new Point3d(-centerLineExt, H1 / 2, 0), - new Point3d(Φ1 / 2 + centerLineExt, H1 / 2, 0)); - lineCenter.ColorIndex = 4; // Cyan - - // 尝试加载线型 - try - { - var linetypeTbl = (LinetypeTable)tr.GetObject(db.LinetypeTableId, OpenMode.ForRead); - if (linetypeTbl.Has("CENTER")) - { - lineCenter.Linetype = "CENTER"; - } - else - { - try - { - db.LoadLineTypeFile("CENTER", "acad.lin"); - if (linetypeTbl.Has("CENTER")) - { - lineCenter.Linetype = "CENTER"; - } - } - catch { /* Ignore */ } - } - } - catch { /* Ignore */ } - - // 添加到数据库 - btr.AppendEntity(polyForging); - tr.AddNewlyCreatedDBObject(polyForging, true); - - btr.AppendEntity(polyForgingInner); - tr.AddNewlyCreatedDBObject(polyForgingInner, true); - - btr.AppendEntity(polyPart); - tr.AddNewlyCreatedDBObject(polyPart, true); - - btr.AppendEntity(lineCenter); - tr.AddNewlyCreatedDBObject(lineCenter, true); - - // --- 剖面填充 --- - try - { - var hatch = new Hatch(); - hatch.Normal = new Vector3d(0, 0, 1); - hatch.Elevation = 0.0; - hatch.SetDatabaseDefaults(); - - btr.AppendEntity(hatch); - tr.AddNewlyCreatedDBObject(hatch, true); - - hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31"); - hatch.PatternScale = 5.0; - hatch.Associative = true; - hatch.ColorIndex = 4; // Cyan - - var ids = new ObjectIdCollection(); - ids.Add(polyPart.ObjectId); - hatch.AppendLoop(HatchLoopTypes.External, ids); - hatch.EvaluateHatch(true); - } - catch (Exception ex) - { - AppendLog($"填充失败: {ex.Message}"); - } - ctx.Commit(); - // --- 日志输出 --- - AppendLog($"半剖视图绘制完成"); - AppendLog($"锻件: Φ{Φ1}, 内Φ{Φ2}, H{H1}"); - AppendLog($"零件: Φ'{Φ_1}, 内Φ'{Φ_2}, H'{H_1}"); + AppendLog($"测试绘制完成 (调用 HalfSectionDrawer)"); } // Zoom Extents