diff --git a/Cad/ShaftRawFreeForgeSquareShaftDrawer.cs b/Cad/ShaftRawFreeForgeSquareShaftDrawer.cs index ecd3da2..eb0e486 100644 --- a/Cad/ShaftRawFreeForgeSquareShaftDrawer.cs +++ b/Cad/ShaftRawFreeForgeSquareShaftDrawer.cs @@ -84,23 +84,38 @@ namespace CadParamPluging.Cad // 1. Draw Main View - 如果需要断线,则绘制带缺口的轮廓 double xBreak = ox + S1 / 4.0; - // 1. Draw Main View - 如果需要断线,则绘制带缺口的轮廓 - if (needBreakLines) - { - DrawRectWithBreak(ctx, ox, oy, S1, S2, filletR, xBreak, lineSpacing); - } - else - { - DrawRectWithFillets(ctx, ox, oy, S1, S2, filletR, DrawingStyleManager.Role.OutlineBold); - } - - // 2. 绘制左右两端的弧线(向外凸出) - // 弧高设为 S2/5(尺寸2的1/5) + // 计算平滑相切数据模型 double arcHeight = S2 / 5.0; - DrawEndArcs(ctx, ox, oy, S1, S2, arcHeight); + double r = filletR; + double w = arcHeight; - // 3. 绘制中心线 (横向,在中间位置) - 已移除 - // DrawHorizontalCenterLine(ctx, ctx.Center.Y, ox, ox + S1, needBreakLines, xBreak, lineSpacing); + double Rm = (S2 * S2) / (8.0 * w) + w / 2.0; + + double xInnerLeft = ox; + double xc_left = xInnerLeft - w + Rm; + double D_tangent = (Rm >= r) ? Math.Sqrt(Math.Max(0, (Rm - r) * (Rm - r) - (S2 / 2.0 - r) * (S2 / 2.0 - r))) : (Rm - w); + double xf_left = r > 0 ? xc_left - D_tangent : xInnerLeft; + + double xInnerRight = ox + S1; + double xc_right = xInnerRight + w - Rm; + double xf_right = r > 0 ? xc_right + D_tangent : xInnerRight; + + double fx_L_tang = r > 0 ? xc_left + (Rm / (Rm - r)) * (xf_left - xc_left) : xInnerLeft; + double fy_L_tangBot = r > 0 ? (oy + S2 / 2.0) + (Rm / (Rm - r)) * ((oy + r) - (oy + S2 / 2.0)) : oy; + + double fx_R_tang = r > 0 ? xc_right + (Rm / (Rm - r)) * (xf_right - xc_right) : xInnerRight; + double fy_R_tangBot = r > 0 ? (oy + S2 / 2.0) + (Rm / (Rm - r)) * ((oy + r) - (oy + S2 / 2.0)) : oy; + + double fx_L_tangTop = r > 0 ? xc_left + (Rm / (Rm - r)) * (xf_left - xc_left) : xInnerLeft; + double fy_L_tangTop = r > 0 ? (oy + S2 / 2.0) + (Rm / (Rm - r)) * ((oy + S2 - r) - (oy + S2 / 2.0)) : oy + S2; + + double fx_R_tangTop = r > 0 ? xc_right + (Rm / (Rm - r)) * (xf_right - xc_right) : xInnerRight; + double fy_R_tangTop = r > 0 ? (oy + S2 / 2.0) + (Rm / (Rm - r)) * ((oy + S2 - r) - (oy + S2 / 2.0)) : oy + S2; + + // 1. 绘制主体轮廓 (矩形 + 平滑相切圆角) 和左右端弧线一体化 + DrawShaftRoundHeadUnifiedOutline(ctx, ox, oy, S1, S2, w, r, xf_left, xf_right, fx_L_tangTop, fy_L_tangTop, fx_L_tang, fy_L_tangBot, fx_R_tangTop, fy_R_tangTop, fx_R_tang, fy_R_tangBot, needBreakLines, xBreak, lineSpacing); + + // 2. 中心线 (由于方轴暂无纵向中心线需求,可跳过) // 4. Draw Break Lines if needed if (needBreakLines) @@ -146,9 +161,8 @@ namespace CadParamPluging.Cad } double dimY = oy - 25; - double dimOriginY = oy + filletR; - FeatureDrivenDrawer.AddLinearDim(ctx, new Point3d(ox, dimOriginY, 0), new Point3d(ox + S1, dimOriginY, 0), new Point3d(ctx.Center.X, dimY, 0), 0, dimText); + FeatureDrivenDrawer.AddLinearDim(ctx, new Point3d(fx_L_tang, fy_L_tangBot, 0), new Point3d(fx_R_tang, fy_R_tangBot, 0), new Point3d(ctx.Center.X, dimY, 0), 0, dimText); } { @@ -167,10 +181,17 @@ namespace CadParamPluging.Cad dimText += @"\X(" + FeatureDrivenDrawer.FormatDimNumber(valPrime.Value) + ")"; } - double dimX = ox + S1 + 20; - double dimOriginX = ox + S1 - filletR; + double dimX = ox + S1 + 20 + arcHeight; - FeatureDrivenDrawer.AddLinearDim(ctx, new Point3d(dimOriginX, oy, 0), new Point3d(dimOriginX, oy + S2, 0), new Point3d(dimX, ctx.Center.Y, 0), 90, dimText); + FeatureDrivenDrawer.AddLinearDim(ctx, new Point3d(xf_right, oy, 0), new Point3d(xf_right, oy + S2, 0), new Point3d(dimX, ctx.Center.Y, 0), 90, dimText); + } + + // 补加未注侧倒角标注指示 + if (filletR > 0) + { + double cxRight = xf_right; + double cyTop = oy + S2 - filletR; + DrawHeadFilletRadiusLeader(ctx, cxRight, cyTop, filletR); } if (S3 > 0) @@ -258,46 +279,214 @@ namespace CadParamPluging.Cad ctx.Tr.AddNewlyCreatedDBObject(line2, true); } - /// - /// 绘制带断口的矩形轮廓(在断线位置打断上下横线) - /// - private static void DrawRectWithBreak(FeatureDrivenDrawer.DrawingContext ctx, double x, double y, double w, double h, double r, double xBreak, double breakWidth) + private static void DrawShaftRoundHeadUnifiedOutline(FeatureDrivenDrawer.DrawingContext ctx, double ox, double oy, double W, double H, double w, double r, double xf_left, double xf_right, double fx_L_tangTop, double fy_L_tangTop, double fx_L_tangBotX, double fy_L_tangBot, double fx_R_tangTop, double fy_R_tangTop, double fx_R_tangBotX, double fy_R_tangBot, bool needBreakLines, double xBreak, double breakWidth) { - // 左边竖线 - var leftLine = new Line(new Point3d(x, y, 0), new Point3d(x, y + h, 0)); - ctx.Style?.Apply(leftLine, DrawingStyleManager.Role.OutlineBold); - ctx.Btr.AppendEntity(leftLine); - ctx.Tr.AddNewlyCreatedDBObject(leftLine, true); + double Rm = (H * H) / (8.0 * w) + w / 2.0; - // 右边竖线 - var rightLine = new Line(new Point3d(x + w, y, 0), new Point3d(x + w, y + h, 0)); - ctx.Style?.Apply(rightLine, DrawingStyleManager.Role.OutlineBold); - ctx.Btr.AppendEntity(rightLine); - ctx.Tr.AddNewlyCreatedDBObject(rightLine, true); + // -------- Left Unified End -------- + double xInnerLeft = ox; + double xc_left = xInnerLeft - w + Rm; + Point2d C_m_L = new Point2d(xc_left, oy + H / 2.0); + Point2d C_fTop_L = new Point2d(xf_left, oy + H - r); + Point2d C_fBot_L = new Point2d(xf_left, oy + r); - // 底边:断开 [xBreak, xBreak + breakWidth] - // 左段 - var bottomLeft = new Line(new Point3d(x, y, 0), new Point3d(xBreak, y, 0)); - ctx.Style?.Apply(bottomLeft, DrawingStyleManager.Role.OutlineBold); - ctx.Btr.AppendEntity(bottomLeft); - ctx.Tr.AddNewlyCreatedDBObject(bottomLeft, true); - // 右段 - var bottomRight = new Line(new Point3d(xBreak + breakWidth, y, 0), new Point3d(x + w, y, 0)); - ctx.Style?.Apply(bottomRight, DrawingStyleManager.Role.OutlineBold); - ctx.Btr.AppendEntity(bottomRight); - ctx.Tr.AddNewlyCreatedDBObject(bottomRight, true); + var leftPoly = new Polyline(); + if (r > 0.01) + { + double angTop1 = Math.PI / 2.0; + double angTop2 = Math.Atan2(fy_L_tangTop - C_fTop_L.Y, fx_L_tangTop - C_fTop_L.X); + double diffTop = angTop2 - angTop1; + while (diffTop < 0) diffTop += Math.PI * 2; + while (diffTop >= Math.PI * 2) diffTop -= Math.PI * 2; + double bulgeTop = Math.Tan(diffTop / 4.0); - // 顶边:断开 [xBreak, xBreak + breakWidth] - // 左段 - var topLeft = new Line(new Point3d(x, y + h, 0), new Point3d(xBreak, y + h, 0)); - ctx.Style?.Apply(topLeft, DrawingStyleManager.Role.OutlineBold); - ctx.Btr.AppendEntity(topLeft); - ctx.Tr.AddNewlyCreatedDBObject(topLeft, true); - // 右段 - var topRight = new Line(new Point3d(xBreak + breakWidth, y + h, 0), new Point3d(x + w, y + h, 0)); - ctx.Style?.Apply(topRight, DrawingStyleManager.Role.OutlineBold); - ctx.Btr.AppendEntity(topRight); - ctx.Tr.AddNewlyCreatedDBObject(topRight, true); + double angM1 = Math.Atan2(fy_L_tangTop - C_m_L.Y, fx_L_tangTop - C_m_L.X); + double angM2 = Math.Atan2(fy_L_tangBot - C_m_L.Y, fx_L_tangBotX - C_m_L.X); + double diffM = angM2 - angM1; + while (diffM < 0) diffM += Math.PI * 2; + while (diffM >= Math.PI * 2) diffM -= Math.PI * 2; + double bulgeM = Math.Tan(diffM / 4.0); + + double angBot1 = Math.Atan2(fy_L_tangBot - C_fBot_L.Y, fx_L_tangBotX - C_fBot_L.X); + double angBot2 = -Math.PI / 2.0; + double diffBot = angBot2 - angBot1; + while (diffBot < 0) diffBot += Math.PI * 2; + while (diffBot >= Math.PI * 2) diffBot -= Math.PI * 2; + double bulgeBot = Math.Tan(diffBot / 4.0); + + leftPoly.AddVertexAt(0, new Point2d(xf_left, oy + H), bulgeTop, 0, 0); + leftPoly.AddVertexAt(1, new Point2d(fx_L_tangTop, fy_L_tangTop), bulgeM, 0, 0); + leftPoly.AddVertexAt(2, new Point2d(fx_L_tangBotX, fy_L_tangBot), bulgeBot, 0, 0); + leftPoly.AddVertexAt(3, new Point2d(xf_left, oy), 0, 0, 0); + } + else + { + double angM1 = Math.Atan2(oy + H - C_m_L.Y, xInnerLeft - C_m_L.X); + double angM2 = Math.Atan2(oy - C_m_L.Y, xInnerLeft - C_m_L.X); + double diffM = angM2 - angM1; + while (diffM < 0) diffM += Math.PI * 2; + while (diffM >= Math.PI * 2) diffM -= Math.PI * 2; + double bulgeM = Math.Tan(diffM / 4.0); + + leftPoly.AddVertexAt(0, new Point2d(xInnerLeft, oy + H), bulgeM, 0, 0); + leftPoly.AddVertexAt(1, new Point2d(xInnerLeft, oy), 0, 0, 0); + } + ctx.Style?.Apply(leftPoly, DrawingStyleManager.Role.OutlineBold); + ctx.Btr.AppendEntity(leftPoly); + ctx.Tr.AddNewlyCreatedDBObject(leftPoly, true); + + var leftClosureLine = new Line(new Point3d(fx_L_tangTop, fy_L_tangTop, 0), new Point3d(fx_L_tangBotX, fy_L_tangBot, 0)); + ctx.Style?.Apply(leftClosureLine, DrawingStyleManager.Role.OutlineBold); + ctx.Btr.AppendEntity(leftClosureLine); + ctx.Tr.AddNewlyCreatedDBObject(leftClosureLine, true); + + // -------- Right Unified End -------- + double xInnerRight = ox + W; + double xc_right = xInnerRight + w - Rm; + Point2d C_m_R = new Point2d(xc_right, oy + H / 2.0); + Point2d C_fTop_R = new Point2d(xf_right, oy + H - r); + Point2d C_fBot_R = new Point2d(xf_right, oy + r); + + var rightPoly = new Polyline(); + if (r > 0.01) + { + double angBot1 = -Math.PI / 2.0; + double angBot2 = Math.Atan2(fy_R_tangBot - C_fBot_R.Y, fx_R_tangBotX - C_fBot_R.X); + double diffBot = angBot2 - angBot1; + while (diffBot < 0) diffBot += Math.PI * 2; + while (diffBot >= Math.PI * 2) diffBot -= Math.PI * 2; + double bulgeBot = Math.Tan(diffBot / 4.0); + + double angM1 = Math.Atan2(fy_R_tangBot - C_m_R.Y, fx_R_tangBotX - C_m_R.X); + double angM2 = Math.Atan2(fy_R_tangTop - C_m_R.Y, fx_R_tangTop - C_m_R.X); + double diffM = angM2 - angM1; + while (diffM < 0) diffM += Math.PI * 2; + while (diffM >= Math.PI * 2) diffM -= Math.PI * 2; + double bulgeM = Math.Tan(diffM / 4.0); + + double angTop1 = Math.Atan2(fy_R_tangTop - C_fTop_R.Y, fx_R_tangTop - C_fTop_R.X); + double angTop2 = Math.PI / 2.0; + double diffTop = angTop2 - angTop1; + while (diffTop < 0) diffTop += Math.PI * 2; + while (diffTop >= Math.PI * 2) diffTop -= Math.PI * 2; + double bulgeTop = Math.Tan(diffTop / 4.0); + + rightPoly.AddVertexAt(0, new Point2d(xf_right, oy), bulgeBot, 0, 0); + rightPoly.AddVertexAt(1, new Point2d(fx_R_tangBotX, fy_R_tangBot), bulgeM, 0, 0); + rightPoly.AddVertexAt(2, new Point2d(fx_R_tangTop, fy_R_tangTop), bulgeTop, 0, 0); + rightPoly.AddVertexAt(3, new Point2d(xf_right, oy + H), 0, 0, 0); + } + else + { + double angM1 = Math.Atan2(oy - C_m_R.Y, xInnerRight - C_m_R.X); + double angM2 = Math.Atan2(oy + H - C_m_R.Y, xInnerRight - C_m_R.X); + double diffM = angM2 - angM1; + while (diffM < 0) diffM += Math.PI * 2; + while (diffM >= Math.PI * 2) diffM -= Math.PI * 2; + double bulgeM = Math.Tan(diffM / 4.0); + + rightPoly.AddVertexAt(0, new Point2d(xInnerRight, oy), bulgeM, 0, 0); + rightPoly.AddVertexAt(1, new Point2d(xInnerRight, oy + H), 0, 0, 0); + } + ctx.Style?.Apply(rightPoly, DrawingStyleManager.Role.OutlineBold); + ctx.Btr.AppendEntity(rightPoly); + ctx.Tr.AddNewlyCreatedDBObject(rightPoly, true); + + var rightClosureLine = new Line(new Point3d(fx_R_tangTop, fy_R_tangTop, 0), new Point3d(fx_R_tangBotX, fy_R_tangBot, 0)); + ctx.Style?.Apply(rightClosureLine, DrawingStyleManager.Role.OutlineBold); + ctx.Btr.AppendEntity(rightClosureLine); + ctx.Tr.AddNewlyCreatedDBObject(rightClosureLine, true); + + // -------- Horizontal Connectors -------- + if (!needBreakLines) + { + var top = new Line(new Point3d(xf_left, oy + H, 0), new Point3d(xf_right, oy + H, 0)); + var bot = new Line(new Point3d(xf_left, oy, 0), new Point3d(xf_right, oy, 0)); + ctx.Style?.Apply(top, DrawingStyleManager.Role.OutlineBold); + ctx.Style?.Apply(bot, DrawingStyleManager.Role.OutlineBold); + ctx.Btr.AppendEntity(top); + ctx.Btr.AppendEntity(bot); + ctx.Tr.AddNewlyCreatedDBObject(top, true); + ctx.Tr.AddNewlyCreatedDBObject(bot, true); + } + else + { + var tL = new Line(new Point3d(xf_left, oy + H, 0), new Point3d(xBreak, oy + H, 0)); + var tR = new Line(new Point3d(xBreak + breakWidth, oy + H, 0), new Point3d(xf_right, oy + H, 0)); + var bL = new Line(new Point3d(xf_left, oy, 0), new Point3d(xBreak, oy, 0)); + var bR = new Line(new Point3d(xBreak + breakWidth, oy, 0), new Point3d(xf_right, oy, 0)); + ctx.Style?.Apply(tL, DrawingStyleManager.Role.OutlineBold); + ctx.Style?.Apply(tR, DrawingStyleManager.Role.OutlineBold); + ctx.Style?.Apply(bL, DrawingStyleManager.Role.OutlineBold); + ctx.Style?.Apply(bR, DrawingStyleManager.Role.OutlineBold); + ctx.Btr.AppendEntity(tL); ctx.Tr.AddNewlyCreatedDBObject(tL, true); + ctx.Btr.AppendEntity(tR); ctx.Tr.AddNewlyCreatedDBObject(tR, true); + ctx.Btr.AppendEntity(bL); ctx.Tr.AddNewlyCreatedDBObject(bL, true); + ctx.Btr.AppendEntity(bR); ctx.Tr.AddNewlyCreatedDBObject(bR, true); + } + } + + private static void DrawHeadFilletRadiusLeader(FeatureDrivenDrawer.DrawingContext ctx, double cx, double cy, double r) + { + try + { + var val = ctx.OriginalBag?.GetDoubleOrNull(FeatureDrivenDrawer.KeySquareShaftFilletRadiusMax); + if (!val.HasValue || val.Value <= 0) return; + + double angleDeg = 45; // 右上角向右上方画出 + double angleRad = angleDeg * Math.PI / 180.0; + + double px = cx + r * Math.Cos(angleRad); + double py = cy + r * Math.Sin(angleRad); + + double ex = px + 25 * Math.Cos(angleRad); + double ey = py + 25 * Math.Sin(angleRad); + + var line = new Line(new Point3d(px, py, 0), new Point3d(ex, ey, 0)); + ctx.Style?.Apply(line, DrawingStyleManager.Role.Dimension); + line.ColorIndex = 4; // Cyan + ctx.Btr.AppendEntity(line); + ctx.Tr.AddNewlyCreatedDBObject(line, true); + + double arrowLength = 5.0; + double arrowWidth = 1.5; + double baseX = px + arrowLength * Math.Cos(angleRad); + double baseY = py + arrowLength * Math.Sin(angleRad); + + double perpAngle = angleRad + Math.PI / 2; + double p1X = baseX + (arrowWidth / 2) * Math.Cos(perpAngle); + double p1Y = baseY + (arrowWidth / 2) * Math.Sin(perpAngle); + double p2X = baseX - (arrowWidth / 2) * Math.Cos(perpAngle); + double p2Y = baseY - (arrowWidth / 2) * Math.Sin(perpAngle); + + var arrow = new Solid(new Point3d(px, py, 0), new Point3d(p1X, p1Y, 0), new Point3d(p2X, p2Y, 0)); + arrow.ColorIndex = 4; + ctx.Btr.AppendEntity(arrow); + ctx.Tr.AddNewlyCreatedDBObject(arrow, true); + + var textStr = $"R\\U+2264{val.Value}"; + var text = new DBText(); + text.TextString = textStr; + text.Height = 3.5; + text.ColorIndex = 4; // Cyan + text.Rotation = angleRad; + text.HorizontalMode = TextHorizontalMode.TextCenter; + text.VerticalMode = TextVerticalMode.TextBottom; + + double offsetX = 2.0 * Math.Cos(angleRad + Math.PI / 2); + double offsetY = 2.0 * Math.Sin(angleRad + Math.PI / 2); + + var tp = new Point3d(ex + offsetX, ey + offsetY, 0); + text.AlignmentPoint = tp; + text.Position = tp; + + ctx.Style?.Apply(text, DrawingStyleManager.Role.Text); + text.ColorIndex = 4; + + ctx.Btr.AppendEntity(text); + ctx.Tr.AddNewlyCreatedDBObject(text, true); + } + catch { } } private static void DrawRectWithFillets(FeatureDrivenDrawer.DrawingContext ctx, double x, double y, double w, double h, double r, DrawingStyleManager.Role role = DrawingStyleManager.Role.OutlineBold) @@ -406,46 +595,7 @@ namespace CadParamPluging.Cad return newBag; } - /// - /// 绘制左右两端的弧线(向外凸出) - /// 使用Polyline的bulge(凸度)值绘制,确保端点精确对齐 - /// - private static void DrawEndArcs(FeatureDrivenDrawer.DrawingContext ctx, double ox, double oy, double W, double H, double arcHeight) - { - try - { - // 弧线端点: - // 左侧弧线:从(ox, oy+H)到(ox, oy),向左凸出arcHeight - // 右侧弧线:从(ox+W, oy)到(ox+W, oy+H),向右凸出arcHeight - - double chordLength = H; - double sagitta = arcHeight; - - // 计算凸度:bulge = 2 * 拱高 / 弦长 - double bulge = (2.0 * sagitta) / chordLength; - - // 左侧弧线:从(ox, oy+H)到(ox, oy),向左凸出(正凸度) - var leftArc = new Polyline(); - leftArc.AddVertexAt(0, new Point2d(ox, oy + H), bulge, 0, 0); - leftArc.AddVertexAt(1, new Point2d(ox, oy), 0, 0, 0); - ctx.Style?.Apply(leftArc, DrawingStyleManager.Role.OutlineBold); - ctx.Btr.AppendEntity(leftArc); - ctx.Tr.AddNewlyCreatedDBObject(leftArc, true); - - // 右侧弧线:从(ox+W, oy)到(ox+W, oy+H),向右凸出(正凸度) - var rightArc = new Polyline(); - rightArc.AddVertexAt(0, new Point2d(ox + W, oy), bulge, 0, 0); - rightArc.AddVertexAt(1, new Point2d(ox + W, oy + H), 0, 0, 0); - ctx.Style?.Apply(rightArc, DrawingStyleManager.Role.OutlineBold); - ctx.Btr.AppendEntity(rightArc); - ctx.Tr.AddNewlyCreatedDBObject(rightArc, true); - } - catch - { - // ignore - } - } - + // (Legacy EndArcs logic replaced by unified drawing)