可以生图了,但是还有不少细节需要调整
This commit is contained in:
parent
1ec011c398
commit
947ab96a3e
210
Cad/HalfSectionDrawer.cs
Normal file
210
Cad/HalfSectionDrawer.cs
Normal file
@ -0,0 +1,210 @@
|
||||
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;
|
||||
}
|
||||
|
||||
// 锻件外轮廓(半剖:只绘制右侧)
|
||||
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;
|
||||
|
||||
btr.AppendEntity(polyForging);
|
||||
tr.AddNewlyCreatedDBObject(polyForging, true);
|
||||
|
||||
// 锻件内孔轮廓
|
||||
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;
|
||||
|
||||
btr.AppendEntity(polyForgingInner);
|
||||
tr.AddNewlyCreatedDBObject(polyForgingInner, true);
|
||||
}
|
||||
|
||||
// 中心线
|
||||
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;
|
||||
|
||||
TryLoadCenterLinetype(db, tr, lineCenter);
|
||||
|
||||
btr.AppendEntity(lineCenter);
|
||||
tr.AddNewlyCreatedDBObject(lineCenter, true);
|
||||
|
||||
// 车加工态:绘制零件轮廓及填充
|
||||
if (IsMachined(deliveryStatus))
|
||||
{
|
||||
double outerDiaPrime = bag.GetDouble("OuterDiameter1Prime");
|
||||
double innerDiaPrime = bag.GetDouble("InnerDiameter2Prime");
|
||||
double heightPrime = bag.GetDouble("Height1Prime");
|
||||
|
||||
if (outerDiaPrime > 0 && heightPrime > 0)
|
||||
{
|
||||
double partOffsetY = (height - heightPrime) / 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.Closed = true;
|
||||
polyPart.ColorIndex = 7;
|
||||
|
||||
btr.AppendEntity(polyPart);
|
||||
tr.AddNewlyCreatedDBObject(polyPart, 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;
|
||||
|
||||
var ids = new ObjectIdCollection();
|
||||
ids.Add(polyPart.ObjectId);
|
||||
hatch.AppendLoop(HatchLoopTypes.External, ids);
|
||||
hatch.EvaluateHatch(true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 填充失败时忽略
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void TryLoadCenterLinetype(Database db, Transaction tr, Line line)
|
||||
{
|
||||
try
|
||||
{
|
||||
var linetypeTbl = (LinetypeTable)tr.GetObject(db.LinetypeTableId, OpenMode.ForRead);
|
||||
if (linetypeTbl.Has("CENTER"))
|
||||
{
|
||||
line.Linetype = "CENTER";
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
db.LoadLineTypeFile("CENTER", "acad.lin");
|
||||
if (linetypeTbl.Has("CENTER"))
|
||||
{
|
||||
line.Linetype = "CENTER";
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -77,6 +77,7 @@
|
||||
<Compile Include="Common\Logger.cs" />
|
||||
<Compile Include="Cad\CadContext.cs" />
|
||||
<Compile Include="Cad\CadDrawingService.cs" />
|
||||
<Compile Include="Cad\HalfSectionDrawer.cs" />
|
||||
<Compile Include="Cad\TemplateAnnotationOptionsExtractor.cs" />
|
||||
<Compile Include="Cad\TemplateLayoutAnnotationExtractor.cs" />
|
||||
<Compile Include="Cad\TemplateModelSheetExtractor.cs" />
|
||||
|
||||
@ -6,6 +6,7 @@ using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
using Autodesk.AutoCAD.ApplicationServices;
|
||||
using Autodesk.AutoCAD.DatabaseServices;
|
||||
using Autodesk.AutoCAD.Geometry;
|
||||
using CadParamPluging.Cad;
|
||||
using CadParamPluging.Common;
|
||||
using CadParamPluging.Domain;
|
||||
@ -159,9 +160,13 @@ namespace CadParamPluging.UI
|
||||
var btnSettings = new Button { Text = "设置", AutoSize = true };
|
||||
btnSettings.Click += (_, __) => OnOpenSettings();
|
||||
|
||||
var btnTestDraw = new Button { Text = "测试绘制", AutoSize = true };
|
||||
btnTestDraw.Click += (_, __) => OnTestDraw();
|
||||
|
||||
panel.Controls.Add(btnGenerate);
|
||||
panel.Controls.Add(btnSaveAs);
|
||||
panel.Controls.Add(btnSettings);
|
||||
panel.Controls.Add(btnTestDraw);
|
||||
return panel;
|
||||
}
|
||||
|
||||
@ -607,8 +612,14 @@ namespace CadParamPluging.UI
|
||||
}
|
||||
}
|
||||
|
||||
var cadService = new CadDrawingService(ctx);
|
||||
DomainFacade.DrawByParams(_selectedTemplate, drawingParams, cadService);
|
||||
// 根据模板参数绘制半剖视图
|
||||
HalfSectionDrawer.Draw(
|
||||
ctx,
|
||||
bag,
|
||||
tplParams.ProjectType, // 交付状态
|
||||
tplParams.SheetSize // 结构特征
|
||||
);
|
||||
|
||||
ctx.Commit();
|
||||
}
|
||||
|
||||
@ -755,6 +766,179 @@ namespace CadParamPluging.UI
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTestDraw()
|
||||
{
|
||||
try
|
||||
{
|
||||
// ==========================================
|
||||
// 参数定义 - 调整比例接近参考图
|
||||
// ==========================================
|
||||
|
||||
// 1. 锻件尺寸参数 (扁平环形件)
|
||||
double Φ1 = 500.0; // 锻件外径
|
||||
double a1 = 2.0; // 外径上差
|
||||
double b1 = -1.0; // 外径下差
|
||||
|
||||
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;
|
||||
|
||||
var doc = AcadApp.DocumentManager.MdiActiveDocument;
|
||||
if (doc == null)
|
||||
{
|
||||
AppendLog("未找到活动图纸。");
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// --- 半剖视图:只绘制右侧 ---
|
||||
|
||||
// 锻件外轮廓 (不闭合,用于显示剖面边界)
|
||||
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}");
|
||||
}
|
||||
|
||||
// Zoom Extents
|
||||
doc.SendStringToExecute("._ZOOM _E ", true, false, false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AppendLog($"测试绘制失败: {ex.Message}");
|
||||
Logger.Error("TestDraw", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendLog(string message)
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user