238 lines
9.0 KiB
C#
238 lines
9.0 KiB
C#
using System;
|
|
using Autodesk.AutoCAD.DatabaseServices;
|
|
using Autodesk.AutoCAD.Geometry;
|
|
using CadParamPluging.Common;
|
|
|
|
namespace CadParamPluging.Cad
|
|
{
|
|
public static class HalfSectionDrawer
|
|
{
|
|
public static void Draw(CadContext ctx, ParamBag bag, string deliveryStatus, string structuralFeature)
|
|
{
|
|
if (ctx == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(ctx));
|
|
}
|
|
|
|
if (bag == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(bag));
|
|
}
|
|
|
|
if (IsRing(structuralFeature))
|
|
{
|
|
DrawRing(ctx, bag, deliveryStatus);
|
|
}
|
|
else if (IsDisk(structuralFeature))
|
|
{
|
|
// 后续实现
|
|
}
|
|
else if (IsShaft(structuralFeature))
|
|
{
|
|
// 后续实现
|
|
}
|
|
else if (IsBlock(structuralFeature))
|
|
{
|
|
// 后续实现
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private static void DrawRing(CadContext ctx, ParamBag bag, string deliveryStatus)
|
|
{
|
|
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;
|
|
}
|
|
|
|
// 绘制水平圆柱 (侧视图)
|
|
// 中心线 (X轴) - 已移除,以匹配参考图样式
|
|
|
|
// 1. 锻件外轮廓
|
|
double R = outerDia / 2.0;
|
|
var polyForging = new Polyline();
|
|
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)
|
|
{
|
|
double r = innerDia / 2.0;
|
|
TryLoadLinetype(db, tr, "DASHED");
|
|
|
|
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}");
|
|
}
|
|
|
|
// 外径标注
|
|
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");
|
|
|
|
// 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(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;
|
|
|
|
btr.AppendEntity(hatch);
|
|
tr.AddNewlyCreatedDBObject(hatch, true);
|
|
|
|
hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
|
|
hatch.PatternScale = 5.0;
|
|
hatch.Associative = true;
|
|
hatch.ColorIndex = 4;
|
|
|
|
var ids = new ObjectIdCollection();
|
|
ids.Add(polyPart.ObjectId);
|
|
hatch.AppendLoop(HatchLoopTypes.External, ids);
|
|
hatch.EvaluateHatch(true);
|
|
}
|
|
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 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
|
|
{
|
|
// 忽略
|
|
}
|
|
}
|
|
}
|
|
}
|