using System; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using CadParamPluging.Common; namespace CadParamPluging.Cad { /// /// 半剖视图绘制器 - 已重构为特征驱动模式 /// 新的绘制逻辑在 FeatureDrivenDrawer 中实现 /// public static class HalfSectionDrawer { /// /// 绘制图形(特征驱动模式) /// /// CAD上下文 /// 参数包 - 根据存在的参数动态绘制对应特征 /// 交付状态(毛料态/车加工态) /// 结构特征(环形/饼盘/轴杆/方体) /// 特殊条件(中心冲孔/有圆头等) /// 工艺方法(轧制/自由锻) /// 绘图中心点 /// 缩放比例(1.0表示不缩放) public static void Draw(CadContext ctx, ParamBag bag, string deliveryStatus, string structuralFeature, string specialCondition = null, string processMethod = null, Point3d? center = null, double scaleFactor = 1.0) { // 委托给特征驱动绘图引擎 FeatureDrivenDrawer.Draw(ctx, bag, deliveryStatus, structuralFeature, specialCondition, processMethod, center, scaleFactor); } /// /// 兼容旧接口 /// [Obsolete("请使用新的 Draw 方法,支持 specialCondition 和 processMethod 参数")] public static void DrawLegacy(CadContext ctx, ParamBag bag, string deliveryStatus, string structuralFeature, Point3d? center = null) { Draw(ctx, bag, deliveryStatus, structuralFeature, null, null, center); } #region 旧代码保留供参考(已废弃) private static bool IsRing(string feature) { return !string.IsNullOrWhiteSpace(feature) && feature.IndexOf("环形", StringComparison.OrdinalIgnoreCase) >= 0; } private static bool IsDisk(string feature) { return !string.IsNullOrWhiteSpace(feature) && (feature.IndexOf("饼盘", StringComparison.OrdinalIgnoreCase) >= 0 || feature.IndexOf("饼", StringComparison.OrdinalIgnoreCase) >= 0 || feature.IndexOf("盘", StringComparison.OrdinalIgnoreCase) >= 0); } private static bool IsShaft(string feature) { return !string.IsNullOrWhiteSpace(feature) && (feature.IndexOf("轴杆", StringComparison.OrdinalIgnoreCase) >= 0 || feature.IndexOf("轴", StringComparison.OrdinalIgnoreCase) >= 0 || feature.IndexOf("杆", StringComparison.OrdinalIgnoreCase) >= 0); } private static bool IsBlock(string feature) { return !string.IsNullOrWhiteSpace(feature) && feature.IndexOf("方体", StringComparison.OrdinalIgnoreCase) >= 0; } private static bool IsMachined(string deliveryStatus) { return !string.IsNullOrWhiteSpace(deliveryStatus) && deliveryStatus.IndexOf("车加工", StringComparison.OrdinalIgnoreCase) >= 0; } [Obsolete("已迁移到 FeatureDrivenDrawer")] private static void DrawRingLegacy(CadContext ctx, ParamBag bag, string deliveryStatus, Point3d center) { var db = ctx.Database; 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"); if (outerDia <= 0 || height <= 0) { return; } // 计算偏移量,使图形居中于 center // 图形原本以 (0,0) 为左端中心,宽度为 height,需要偏移使其居中 double ox = center.X - height / 2.0; double oy = center.Y; // 绘制水平圆柱 (侧视图) // 中心线 (X轴) - 已移除,以匹配参考图样式 // 1. 锻件外轮廓 double R = outerDia / 2.0; var polyForging = new Polyline(); polyForging.AddVertexAt(0, new Point2d(ox + 0, oy - R), 0, 0, 0); polyForging.AddVertexAt(1, new Point2d(ox + height, oy - R), 0, 0, 0); polyForging.AddVertexAt(2, new Point2d(ox + height, oy + R), 0, 0, 0); polyForging.AddVertexAt(3, new Point2d(ox + 0, oy + R), 0, 0, 0); polyForging.Closed = true; polyForging.ColorIndex = 7; // 白色 btr.AppendEntity(polyForging); tr.AddNewlyCreatedDBObject(polyForging, true); // 3. 内孔轮廓 (虚线) if (innerDia > 0) { double r = innerDia / 2.0; TryLoadLinetype(db, tr, "DASHED"); var lineHiddenTop = new Line(new Point3d(ox + 0, oy + r, 0), new Point3d(ox + height, oy + r, 0)); lineHiddenTop.ColorIndex = 2; // 黄色 lineHiddenTop.Linetype = "DASHED"; btr.AppendEntity(lineHiddenTop); tr.AddNewlyCreatedDBObject(lineHiddenTop, true); var lineHiddenBot = new Line(new Point3d(ox + 0, oy - r, 0), new Point3d(ox + height, oy - r, 0)); lineHiddenBot.ColorIndex = 2; lineHiddenBot.Linetype = "DASHED"; btr.AppendEntity(lineHiddenBot); tr.AddNewlyCreatedDBObject(lineHiddenBot, true); // 内孔标注 AddLinearDim(db, tr, btr, new Point3d(ox + 0, oy + r, 0), new Point3d(ox + 0, oy - r, 0), new Point3d(ox + height + 15, oy, 0), 90, $"%%c{innerDia}"); } // 外径标注 AddLinearDim(db, tr, btr, new Point3d(ox + 0, oy + R, 0), new Point3d(ox + 0, oy - R, 0), new Point3d(ox - 15, oy, 0), 90, $"%%c{outerDia}"); // 长度标注 AddLinearDim(db, tr, btr, new Point3d(ox + 0, oy - R, 0), new Point3d(ox + height, oy - R, 0), new Point3d(ox + height / 2, oy - R - 15, 0), 0, $"{height}min"); // 4. 车加工零件 (填充) if (IsMachined(deliveryStatus)) { double outerDiaPrime = bag.GetDouble("OuterDiameter1Prime"); double innerDiaPrime = bag.GetDouble("InnerDiameter2Prime"); double heightPrime = bag.GetDouble("Height1Prime"); if (outerDiaPrime > 0 && heightPrime > 0) { double offsetX = (height - heightPrime) / 2.0; double Rp = outerDiaPrime / 2.0; // 零件轮廓 var polyPart = new Polyline(); polyPart.AddVertexAt(0, new Point2d(ox + offsetX, oy - Rp), 0, 0, 0); polyPart.AddVertexAt(1, new Point2d(ox + offsetX + heightPrime, oy - Rp), 0, 0, 0); polyPart.AddVertexAt(2, new Point2d(ox + offsetX + heightPrime, oy + Rp), 0, 0, 0); polyPart.AddVertexAt(3, new Point2d(ox + offsetX, oy + 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; btr.AppendEntity(hatch); tr.AddNewlyCreatedDBObject(hatch, true); hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31"); hatch.PatternScale = 10; hatch.PatternAngle = 0; hatch.Associative = false; hatch.ColorIndex = 4; // 青色 hatch.LineWeight = LineWeight.LineWeight025; // 0.25mm线宽 var ids = new ObjectIdCollection(); ids.Add(polyPart.ObjectId); hatch.AppendLoop(HatchLoopTypes.External, ids); hatch.EvaluateHatch(true); } catch { // 忽略 } // 可以在这里添加零件尺寸标注 AddLinearDim(db, tr, btr, new Point3d(ox + offsetX, oy + Rp, 0), new Point3d(ox + offsetX, oy - Rp, 0), new Point3d(ox + offsetX - 5, oy, 0), 90, $"%%c{outerDiaPrime}"); AddLinearDim(db, tr, btr, new Point3d(ox + offsetX, oy - Rp, 0), new Point3d(ox + offsetX + heightPrime, oy - Rp, 0), new Point3d(ox + offsetX + heightPrime / 2, oy - Rp - 10, 0), 0, $"{heightPrime}"); } } } 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(linetypeName)) { try { db.LoadLineTypeFile(linetypeName, "acad.lin"); } catch { // 忽略 } } } catch { // 忽略 } } #endregion } }