370 lines
15 KiB
C#
370 lines
15 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Autodesk.AutoCAD.DatabaseServices;
|
|
using Autodesk.AutoCAD.Geometry;
|
|
using CadParamPluging.Common;
|
|
|
|
namespace CadParamPluging.Cad
|
|
{
|
|
public static class ShaftRawFreeForgeRoundShaftDrawer
|
|
{
|
|
public static void Draw(CadContext ctx, ParamBag bag, Point3d center, double scaleFactor = 1.0)
|
|
{
|
|
if (ctx == null) throw new ArgumentNullException(nameof(ctx));
|
|
if (bag == null) throw new ArgumentNullException(nameof(bag));
|
|
|
|
var effectiveBag = scaleFactor < 1.0 ? ScaleParamBag(bag, scaleFactor) : bag;
|
|
|
|
var db = ctx.Database;
|
|
var tr = ctx.Transaction;
|
|
|
|
var prevDb = HostApplicationServices.WorkingDatabase;
|
|
HostApplicationServices.WorkingDatabase = db;
|
|
BlockTableRecord btr;
|
|
try
|
|
{
|
|
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
|
|
btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
|
|
}
|
|
finally
|
|
{
|
|
HostApplicationServices.WorkingDatabase = prevDb;
|
|
}
|
|
|
|
var context = new FeatureDrivenDrawer.DrawingContext
|
|
{
|
|
Ctx = ctx,
|
|
Bag = effectiveBag,
|
|
OriginalBag = bag,
|
|
Center = center,
|
|
DeliveryStatus = "毛料态",
|
|
StructuralFeature = "轴杆",
|
|
SpecialCondition = "圆轴",
|
|
ProcessMethod = "自由锻",
|
|
Btr = btr,
|
|
Style = new DrawingStyleManager(db, tr) // Reusing existing StyleManager
|
|
};
|
|
|
|
DrawCore(context);
|
|
}
|
|
|
|
private static void DrawCore(FeatureDrivenDrawer.DrawingContext ctx)
|
|
{
|
|
var bag = ctx.Bag;
|
|
|
|
// Parameters
|
|
var diameter = bag.GetDoubleOrNull(FeatureDrivenDrawer.KeyDiameter);
|
|
var length = bag.GetDoubleOrNull(FeatureDrivenDrawer.KeyLength);
|
|
|
|
if (!diameter.HasValue || diameter.Value <= 0 || !length.HasValue || length.Value <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
double D = diameter.Value;
|
|
double L = length.Value;
|
|
|
|
// Visual Size Calculation
|
|
// Keep aspect ratio but maybe limit if too extreme?
|
|
// For now, draw 1:1 visually unless scaled by scaleFactor (which is handled in bag).
|
|
|
|
// Calculate coordinates (Center is geometric center)
|
|
double ox = ctx.Center.X - L / 2.0;
|
|
double oy = ctx.Center.Y - D / 2.0;
|
|
|
|
// 1. Draw Shaft Outline (Rectangle with optional fillets)
|
|
var filletRParam = bag.GetDoubleOrNull(FeatureDrivenDrawer.KeyShaftFilletRadiusMax);
|
|
double filletR = filletRParam.HasValue && filletRParam.Value > 0 ? filletRParam.Value : 0;
|
|
|
|
// Limit fillet radius
|
|
double maxR = Math.Min(D / 2.0, L / 2.0);
|
|
if (filletR > maxR) filletR = maxR;
|
|
|
|
DrawShaftOutline(ctx, ox, oy, L, D, filletR);
|
|
|
|
// 2. Draw Symmetry Axis
|
|
DrawSymmetryAxis(ctx, ox, oy, L, D);
|
|
|
|
// 3. Draw Part Contour (Yellow dashed)
|
|
var diameterPrime = bag.GetDoubleOrNull(FeatureDrivenDrawer.KeyDiameterPrime);
|
|
var lengthPrime = bag.GetDoubleOrNull(FeatureDrivenDrawer.KeyLengthPrime);
|
|
|
|
if (diameterPrime.HasValue && diameterPrime.Value > 0 && lengthPrime.HasValue && lengthPrime.Value > 0)
|
|
{
|
|
double partD = diameterPrime.Value;
|
|
double partL = lengthPrime.Value;
|
|
|
|
// Center part contour
|
|
double partOx = ctx.Center.X - partL / 2.0;
|
|
double partOy = ctx.Center.Y - partD / 2.0;
|
|
|
|
DrawPartContour(ctx, partOx, partOy, partL, partD);
|
|
}
|
|
|
|
// 4. Dimensions
|
|
|
|
// Length Dimension (Bottom)
|
|
{
|
|
var val = ctx.OriginalBag?.GetDoubleOrNull(FeatureDrivenDrawer.KeyLength) ?? L;
|
|
var str = ctx.OriginalBag?.GetString(FeatureDrivenDrawer.KeyLength);
|
|
var tolPlus = ctx.OriginalBag?.GetDoubleOrNull(FeatureDrivenDrawer.KeyLengthTolPlus);
|
|
var tolMinus = ctx.OriginalBag?.GetDoubleOrNull(FeatureDrivenDrawer.KeyLengthTolMinus);
|
|
|
|
var dimText = BuildDimensionText(FormatDimNumber(val, str), tolPlus, tolMinus);
|
|
|
|
// Add Part Length (Prime) below
|
|
var valPrime = ctx.OriginalBag?.GetDoubleOrNull(FeatureDrivenDrawer.KeyLengthPrime);
|
|
if (valPrime.HasValue && valPrime.Value > 0)
|
|
{
|
|
dimText += $"\\X({FormatDimNumber(valPrime.Value)})";
|
|
}
|
|
|
|
// Place below
|
|
double dimY = oy - 20; // Offset downwards? Usually dependent on margin
|
|
// Let's use a standard offset. If D is small, 20 is fine.
|
|
|
|
AddLinearDim(
|
|
ctx,
|
|
new Point3d(ox, oy, 0),
|
|
new Point3d(ox + L, oy, 0),
|
|
new Point3d(ctx.Center.X, dimY, 0),
|
|
0,
|
|
dimText);
|
|
}
|
|
|
|
// Diameter Dimension (Right side usually for shafts, or put on side)
|
|
// Vertical dimension for Diameter
|
|
{
|
|
var val = ctx.OriginalBag?.GetDoubleOrNull(FeatureDrivenDrawer.KeyDiameter) ?? D;
|
|
var str = ctx.OriginalBag?.GetString(FeatureDrivenDrawer.KeyDiameter);
|
|
var tolPlus = ctx.OriginalBag?.GetDoubleOrNull(FeatureDrivenDrawer.KeyDiameterTolPlus);
|
|
var tolMinus = ctx.OriginalBag?.GetDoubleOrNull(FeatureDrivenDrawer.KeyDiameterTolMinus);
|
|
|
|
// Prefix with %%c for diameter
|
|
var dimText = BuildDimensionText("%%c" + FormatDimNumber(val, str), tolPlus, tolMinus);
|
|
|
|
// Add Part Diameter (Prime) below
|
|
var valPrime = ctx.OriginalBag?.GetDoubleOrNull(FeatureDrivenDrawer.KeyDiameterPrime);
|
|
if (valPrime.HasValue && valPrime.Value > 0)
|
|
{
|
|
dimText += $"\\X(%%c{FormatDimNumber(valPrime.Value)})";
|
|
}
|
|
|
|
double dimX = ox + L + 20;
|
|
|
|
AddLinearDim(
|
|
ctx,
|
|
new Point3d(ox + L, oy, 0),
|
|
new Point3d(ox + L, oy + D, 0),
|
|
new Point3d(dimX, ctx.Center.Y, 0),
|
|
90,
|
|
dimText);
|
|
}
|
|
|
|
// Unspecified Fillet Note
|
|
if (filletR > 0)
|
|
{
|
|
// Usually text near the drawing or a general note.
|
|
// Ring template puts it at bottom left/right.
|
|
// Let's place it at top left.
|
|
// "未注圆角 R<=..."
|
|
|
|
// Construct text
|
|
string note = $"未注圆角 R≤{FormatDimNumber(filletR)}";
|
|
|
|
// Position: Top Left, slightly above
|
|
DrawSimpleText(ctx, new Point3d(ox, oy + D + 10, 0), note, 3.5);
|
|
}
|
|
}
|
|
|
|
private static void DrawShaftOutline(FeatureDrivenDrawer.DrawingContext ctx, double x, double y, double w, double h, double r)
|
|
{
|
|
var poly = new Polyline();
|
|
|
|
if (r < 0.01)
|
|
{
|
|
poly.AddVertexAt(0, new Point2d(x, y), 0, 0, 0);
|
|
poly.AddVertexAt(1, new Point2d(x + w, y), 0, 0, 0);
|
|
poly.AddVertexAt(2, new Point2d(x + w, y + h), 0, 0, 0);
|
|
poly.AddVertexAt(3, new Point2d(x, y + h), 0, 0, 0);
|
|
}
|
|
else
|
|
{
|
|
var bulge = Math.Tan(Math.PI / 8.0);
|
|
|
|
// Bottom-Left corner
|
|
poly.AddVertexAt(0, new Point2d(x + r, y), 0, 0, 0); // Start of bottom line
|
|
|
|
// Bottom-Right corner
|
|
poly.AddVertexAt(1, new Point2d(x + w - r, y), bulge, 0, 0); // Arc start
|
|
poly.AddVertexAt(2, new Point2d(x + w, y + r), 0, 0, 0); // Right line start
|
|
|
|
// Top-Right corner
|
|
poly.AddVertexAt(3, new Point2d(x + w, y + h - r), bulge, 0, 0);
|
|
poly.AddVertexAt(4, new Point2d(x + w - r, y + h), 0, 0, 0);
|
|
|
|
// Top-Left corner
|
|
poly.AddVertexAt(5, new Point2d(x + r, y + h), bulge, 0, 0);
|
|
poly.AddVertexAt(6, new Point2d(x, y + h - r), 0, 0, 0);
|
|
|
|
// Back to bottom-left arc
|
|
poly.AddVertexAt(7, new Point2d(x, y + r), bulge, 0, 0);
|
|
}
|
|
|
|
poly.Closed = true;
|
|
ctx.Style?.Apply(poly, DrawingStyleManager.Role.OutlineBold);
|
|
ctx.Btr.AppendEntity(poly);
|
|
ctx.Tr.AddNewlyCreatedDBObject(poly, true);
|
|
|
|
// Hatching? Shafts are usually not hatched unless sectioned.
|
|
// "Raw State Free Forging" might be just the outline.
|
|
// Ring templates hatch because they are sections. Shaft is usually full view.
|
|
// But if user wants a "template", maybe it mimics others?
|
|
// Ring Free Forge has hatching. Block has hatching in section.
|
|
// If this is a SECTION of a shaft (which is just a circle?), no.
|
|
// Should be side view. No hatching typically.
|
|
}
|
|
|
|
private static void DrawSymmetryAxis(FeatureDrivenDrawer.DrawingContext ctx, double x, double y, double w, double h)
|
|
{
|
|
// Horizontal axis? Shafts are usually horizontal.
|
|
// Center Y = y + h/2
|
|
double midY = y + h / 2.0;
|
|
double extend = w * 0.1;
|
|
if (extend > 15) extend = 15;
|
|
|
|
var line = new Line(new Point3d(x - extend, midY, 0), new Point3d(x + w + extend, midY, 0));
|
|
ctx.Style?.Apply(line, DrawingStyleManager.Role.Centerline);
|
|
ctx.Btr.AppendEntity(line);
|
|
ctx.Tr.AddNewlyCreatedDBObject(line, true);
|
|
}
|
|
|
|
private static void DrawPartContour(FeatureDrivenDrawer.DrawingContext ctx, double x, double y, double w, double h)
|
|
{
|
|
var poly = new Polyline();
|
|
poly.AddVertexAt(0, new Point2d(x, y), 0, 0, 0);
|
|
poly.AddVertexAt(1, new Point2d(x + w, y), 0, 0, 0);
|
|
poly.AddVertexAt(2, new Point2d(x + w, y + h), 0, 0, 0);
|
|
poly.AddVertexAt(3, new Point2d(x, y + h), 0, 0, 0);
|
|
poly.Closed = true;
|
|
|
|
ctx.Style?.Apply(poly, DrawingStyleManager.Role.PartContour); // Yellow Dashed
|
|
ctx.Btr.AppendEntity(poly);
|
|
ctx.Tr.AddNewlyCreatedDBObject(poly, true);
|
|
}
|
|
|
|
// --- Helpers copied/adapted to ensure isolation ---
|
|
|
|
private static ParamBag ScaleParamBag(ParamBag original, double scaleFactor)
|
|
{
|
|
if (original == null) return null;
|
|
if (Math.Abs(scaleFactor - 1.0) < 0.000001) return original;
|
|
// Simplified copy for specific keys
|
|
var newBag = new ParamBag();
|
|
foreach(var k in original.GetKeys())
|
|
{
|
|
var v = original.GetString(k);
|
|
if (IsScaleableKey(k) && double.TryParse(v, out var d))
|
|
{
|
|
newBag.Set(k, (d * scaleFactor).ToString("0.###"));
|
|
}
|
|
else
|
|
{
|
|
newBag.Set(k, v);
|
|
}
|
|
}
|
|
return newBag;
|
|
}
|
|
|
|
private static bool IsScaleableKey(string key)
|
|
{
|
|
// List relevant keys for this template
|
|
return key.StartsWith("Diameter") || key.StartsWith("Length") || key.EndsWith("Prime") || key.Contains("Radius");
|
|
}
|
|
|
|
private static void AddLinearDim(FeatureDrivenDrawer.DrawingContext ctx, Point3d p1, Point3d p2, Point3d ptDim, double rot, string text)
|
|
{
|
|
try
|
|
{
|
|
var dim = new RotatedDimension();
|
|
dim.SetDatabaseDefaults();
|
|
dim.XLine1Point = p1;
|
|
dim.XLine2Point = p2;
|
|
dim.DimLinePoint = ptDim;
|
|
dim.Rotation = rot * Math.PI / 180.0;
|
|
dim.DimensionStyle = ctx.Db.Dimstyle;
|
|
dim.DimensionText = text;
|
|
|
|
// Force Green
|
|
dim.ColorIndex = 3;
|
|
|
|
// Basic overrides if possible
|
|
// Using reflection setup similar to other files
|
|
SetDimColor(dim, "Dimclrd", 3);
|
|
SetDimColor(dim, "Dimclre", 3);
|
|
SetDimColor(dim, "Dimclrt", 3);
|
|
SetDimBool(dim, "Dimtofl", true);
|
|
|
|
ctx.Style?.Apply(dim, DrawingStyleManager.Role.Dimension);
|
|
dim.ColorIndex = 3;
|
|
|
|
ctx.Btr.AppendEntity(dim);
|
|
ctx.Tr.AddNewlyCreatedDBObject(dim, true);
|
|
}
|
|
catch {}
|
|
}
|
|
|
|
private static void DrawSimpleText(FeatureDrivenDrawer.DrawingContext ctx, Point3d pos, string text, double height)
|
|
{
|
|
try
|
|
{
|
|
var mtxt = new MText();
|
|
mtxt.SetDatabaseDefaults();
|
|
mtxt.Location = pos;
|
|
mtxt.Contents = text;
|
|
mtxt.TextHeight = height;
|
|
mtxt.Attachment = AttachmentPoint.BottomLeft;
|
|
|
|
ctx.Style?.Apply(mtxt, DrawingStyleManager.Role.Text);
|
|
ctx.Btr.AppendEntity(mtxt);
|
|
ctx.Tr.AddNewlyCreatedDBObject(mtxt, true);
|
|
}
|
|
catch {}
|
|
}
|
|
|
|
private static string FormatDimNumber(double value, string rawInput = null)
|
|
{
|
|
// Simplified formatter
|
|
return value.ToString("0.###");
|
|
}
|
|
|
|
private static string BuildDimensionText(string baseText, double? tolPlus, double? tolMinus)
|
|
{
|
|
if (!tolPlus.HasValue && !tolMinus.HasValue) return baseText;
|
|
|
|
if (tolPlus.HasValue && tolMinus.HasValue && Math.Abs(Math.Abs(tolPlus.Value) - Math.Abs(tolMinus.Value)) < 0.00001)
|
|
{
|
|
return $"{baseText}%%p{Math.Abs(tolPlus.Value):0.###}";
|
|
}
|
|
|
|
string p = tolPlus.HasValue ? (tolPlus.Value >= 0 ? "+" : "") + tolPlus.Value.ToString("0.###") : "0";
|
|
string m = tolMinus.HasValue ? tolMinus.Value.ToString("0.###") : "0";
|
|
|
|
return $"{baseText}\\S{p}^{m};";
|
|
}
|
|
|
|
// Reflection helpers
|
|
private static void SetDimColor(Dimension dim, string prop, int colorIndex)
|
|
{
|
|
try {
|
|
var p = dim.GetType().GetProperty(prop);
|
|
if (p != null) p.SetValue(dim, Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, (short)colorIndex));
|
|
} catch {}
|
|
}
|
|
private static void SetDimBool(Dimension dim, string prop, bool val)
|
|
{
|
|
try { dim.GetType().GetProperty(prop)?.SetValue(dim, val); } catch {}
|
|
}
|
|
}
|
|
}
|