using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Windows.Forms;
namespace BeamSectionPlugin
{
public class BeamSectionDrawer
{
private static BeamSettings settings = new BeamSettings();
// 使用缩写命令
[CommandMethod("BP")]
public void DrawBeamSection()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
// 显示参数设置对话框
using (var form = new SettingsForm(settings))
{
// 使用完整命名空间解决歧义
if (Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form) != DialogResult.OK)
return;
}
while (true)
{
try
{
// 步骤1:获取梁的第一点
PromptPointResult firstPointRes = ed.GetPoint("\n请点击梁的第一点: ");
if (firstPointRes.Status != PromptStatus.OK) return;
// 步骤2:获取梁的第二点(带选项)
PromptPointOptions secondPointOpts = new PromptPointOptions("\n请点击梁的第二点[板厚(A)升降板(S)]: ");
secondPointOpts.Keywords.Add("A");
secondPointOpts.Keywords.Add("S");
secondPointOpts.AllowNone = true;
PromptPointResult secondPointRes = ed.GetPoint(secondPointOpts);
if (secondPointRes.Status == PromptStatus.Keyword)
{
if (secondPointRes.StringResult == "A")
{
// 修正GetDouble调用方式
PromptDoubleOptions pdo = new PromptDoubleOptions("\n输入板厚(mm)<120>: ");
pdo.DefaultValue = 120;
pdo.UseDefaultValue = true;
PromptDoubleResult slabThicknessRes = ed.GetDouble(pdo);
if (slabThicknessRes.Status != PromptStatus.OK) return;
settings.SlabThickness = slabThicknessRes.Value;
}
else if (secondPointRes.StringResult == "S")
{
PromptDoubleOptions pdo = new PromptDoubleOptions("\n请输入升降板高度(升板为正数,降板为负数)mm<0.00>: ");
pdo.DefaultValue = 0;
pdo.UseDefaultValue = true;
PromptDoubleResult slabHeightRes = ed.GetDouble(pdo);
if (slabHeightRes.Status != PromptStatus.OK) return;
settings.SlabHeightOffset = slabHeightRes.Value;
}
secondPointRes = ed.GetPoint("\n请点击梁的第二点: ");
}
if (secondPointRes.Status != PromptStatus.OK) return;
// 步骤3:获取梁高
PromptDoubleOptions heightOpts = new PromptDoubleOptions("\n请输入梁高(mm): ");
PromptDoubleResult beamHeightRes = ed.GetDouble(heightOpts);
if (beamHeightRes.Status != PromptStatus.OK) return;
double beamHeight = beamHeightRes.Value;
// 绘制梁侧模
if (settings.DrawBeamFormwork)
DrawBeamFormwork(firstPointRes.Value, secondPointRes.Value, beamHeight);
// 绘制板下龙骨
if (settings.DrawSlabSupport)
DrawSlabSupport(firstPointRes.Value, secondPointRes.Value, beamHeight);
}
catch (System.Exception ex)
{
ed.WriteMessage($"\n错误: {ex.Message}");
}
}
}
private void DrawBeamFormwork(Point3d start, Point3d end, double height)
{
double length = start.DistanceTo(end);
double halfLength = length / 2;
using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
// 插入梁侧模图块
if (bt.Has("ScaffoldPole梁侧模"))
{
BlockReference beamRef = new BlockReference(start, bt["ScaffoldPole梁侧模"]);
BlockTableRecord btr = tr.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(beamRef);
tr.AddNewlyCreatedDBObject(beamRef, true);
// 设置动态块参数
DynamicBlockReferencePropertyCollection props = beamRef.DynamicBlockReferencePropertyCollection;
foreach (DynamicBlockReferenceProperty prop in props)
{
if (prop.PropertyName == "宽度左侧") prop.Value = halfLength;
if (prop.PropertyName == "宽度右侧") prop.Value = halfLength;
if (prop.PropertyName == "左高度") prop.Value = height;
if (prop.PropertyName == "右高度") prop.Value = height;
}
// 旋转图块
Vector3d direction = end - start;
double angle = direction.GetAngleTo(Vector3d.XAxis);
beamRef.TransformBy(Matrix3d.Rotation(angle, Vector3d.ZAxis, start));
}
tr.Commit();
}
}
private void DrawSlabSupport(Point3d beamStart, Point3d beamEnd, double beamHeight)
{
Vector3d beamDirection = (beamEnd - beamStart).GetNormal();
double beamLength = beamStart.DistanceTo(beamEnd);
// 计算板底位置
Point3d slabStart = beamStart + (Vector3d.ZAxis * (beamHeight + settings.SlabThickness + settings.SlabHeightOffset));
Point3d slabEnd = beamEnd + (Vector3d.ZAxis * (beamHeight + settings.SlabThickness + settings.SlabHeightOffset));
using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
if (settings.SlabMainDirection == "横向")
{
// 横向龙骨:阵列次龙骨
int arrayCount = (int)Math.Floor(beamLength / settings.SlabSecondarySpacing) + 1;
double spacing = beamLength / (arrayCount - 1);
for (int i = 0; i < arrayCount; i++)
{
Point3d position = slabStart + (beamDirection * (i * spacing));
string blockName = settings.SlabSecondaryMaterial.Contains("木方") ?
"ScaffoldPole断面木方" : "ScaffoldPole断面方钢";
if (bt.Has(blockName))
{
BlockReference supportRef = new BlockReference(position, bt[blockName]);
btr.AppendEntity(supportRef);
tr.AddNewlyCreatedDBObject(supportRef, true);
// 设置材料可见性
DynamicBlockReferencePropertyCollection props = supportRef.DynamicBlockReferencePropertyCollection;
foreach (DynamicBlockReferenceProperty prop in props)
{
if (prop.PropertyName == "材料")
prop.Value = settings.SlabSecondaryMaterial;
}
}
}
}
else
{
// 纵向龙骨:单根横向龙骨
Point3d center = slabStart + ((slabEnd - slabStart) / 2);
if (bt.Has("ScaffoldPole横向龙骨"))
{
BlockReference supportRef = new BlockReference(center, bt["ScaffoldPole横向龙骨"]);
btr.AppendEntity(supportRef);
tr.AddNewlyCreatedDBObject(supportRef, true);
// 设置长度属性
DynamicBlockReferencePropertyCollection props = supportRef.DynamicBlockReferencePropertyCollection;
foreach (DynamicBlockReferenceProperty prop in props)
{
if (prop.PropertyName == "长度")
prop.Value = beamLength;
if (prop.PropertyName == "材料")
prop.Value = settings.SlabSecondaryMaterial;
}
// 旋转方向
supportRef.TransformBy(Matrix3d.Rotation(Math.PI / 2, Vector3d.ZAxis, center));
}
}
tr.Commit();
}
}
}
public class BeamSettings
{
public bool DrawBeamFormwork { get; set; } = true;
public bool DrawBeamSupport { get; set; } = true;
public string BeamMainDirection { get; set; } = "横向";
public double BeamExtension { get; set; } = 50;
public string BeamMainMaterial { get; set; } = "100方钢";
public string BeamSecondaryMaterial { get; set; } = "50木方";
public double BeamSecondarySpacing { get; set; } = 100;
public double BeamSecondaryHeight { get; set; } = 90;
public double BeamSecondaryWidth { get; set; } = 40;
public double SlabThickness { get; set; } = 250;
public bool DrawSlabSupport { get; set; } = true;
public string SlabMainDirection { get; set; } = "横向";
public bool KeepSupport { get; set; } = true;
public string SlabMainMaterial { get; set; } = "100方钢";
public string SlabSecondaryMaterial { get; set; } = "3木方";
public double SlabSecondarySpacing { get; set; } = 250;
public double SlabSecondaryHeight { get; set; } = 90;
public double SlabSecondaryWidth { get; set; } = 40;
public double SlabHeightOffset { get; set; } = 0;
}
public class SettingsForm : Form
{
private BeamSettings settings;
public SettingsForm(BeamSettings settings)
{
this.settings = settings;
InitializeForm();
}
private void InitializeForm()
{
this.Text = "梁板剖面绘制参数设置";
this.Size = new System.Drawing.Size(450, 600);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
TabControl tabControl = new TabControl();
tabControl.Dock = DockStyle.Fill;
// 梁下龙骨选项卡
TabPage beamTab = new TabPage("梁下龙骨");
InitializeBeamTab(beamTab);
// 板下龙骨选项卡
TabPage slabTab = new TabPage("板下龙骨");
InitializeSlabTab(slabTab);
tabControl.TabPages.Add(beamTab);
tabControl.TabPages.Add(slabTab);
// 按钮面板
Panel buttonPanel = new Panel();
buttonPanel.Dock = DockStyle.Bottom;
buttonPanel.Height = 40;
Button okButton = new Button();
okButton.Text = "开始绘制";
okButton.DialogResult = DialogResult.OK;
okButton.Location = new System.Drawing.Point(280, 10);
Button cancelButton = new Button();
cancelButton.Text = "取消";
cancelButton.DialogResult = DialogResult.Cancel;
cancelButton.Location = new System.Drawing.Point(360, 10);
buttonPanel.Controls.Add(okButton);
buttonPanel.Controls.Add(cancelButton);
this.Controls.Add(tabControl);
this.Controls.Add(buttonPanel);
}
private void InitializeBeamTab(TabPage tab)
{
tab.Controls.Clear();
tab.AutoScroll = true;
int yPos = 20;
// 绘制梁侧模
GroupBox gbFormwork = new GroupBox();
gbFormwork.Text = "梁侧模设置";
gbFormwork.Location = new System.Drawing.Point(20, yPos);
gbFormwork.Size = new System.Drawing.Size(380, 80);
RadioButton rbFormworkYes = new RadioButton();
rbFormworkYes.Text = "是";
rbFormworkYes.Checked = settings.DrawBeamFormwork;
rbFormworkYes.Location = new System.Drawing.Point(20, 30);
RadioButton rbFormworkNo = new RadioButton();
rbFormworkNo.Text = "否";
rbFormworkNo.Checked = !settings.DrawBeamFormwork;
rbFormworkNo.Location = new System.Drawing.Point(100, 30);
gbFormwork.Controls.Add(rbFormworkYes);
gbFormwork.Controls.Add(rbFormworkNo);
tab.Controls.Add(gbFormwork);
yPos += 100;
// 主龙骨方向
GroupBox gbDirection = new GroupBox();
gbDirection.Text = "主龙骨方向";
gbDirection.Location = new System.Drawing.Point(20, yPos);
gbDirection.Size = new System.Drawing.Size(380, 80);
RadioButton rbHorizontal = new RadioButton();
rbHorizontal.Text = "横向";
rbHorizontal.Checked = (settings.BeamMainDirection == "横向");
rbHorizontal.Location = new System.Drawing.Point(20, 30);
RadioButton rbVertical = new RadioButton();
rbVertical.Text = "纵向";
rbVertical.Checked = (settings.BeamMainDirection == "纵向");
rbVertical.Location = new System.Drawing.Point(100, 30);
gbDirection.Controls.Add(rbHorizontal);
gbDirection.Controls.Add(rbVertical);
tab.Controls.Add(gbDirection);
yPos += 100;
// 材料设置
GroupBox gbMaterial = new GroupBox();
gbMaterial.Text = "材料设置";
gbMaterial.Location = new System.Drawing.Point(20, yPos);
gbMaterial.Size = new System.Drawing.Size(380, 120);
Label lblMainMaterial = new Label();
lblMainMaterial.Text = "主龙骨:";
lblMainMaterial.Location = new System.Drawing.Point(20, 30);
ComboBox cbMainMaterial = new ComboBox();
cbMainMaterial.Items.AddRange(new object[] { "48钢管", "40方钢", "50方钢", "100方钢", "8#槽钢", "10#槽钢" });
cbMainMaterial.Text = settings.BeamMainMaterial;
cbMainMaterial.Location = new System.Drawing.Point(120, 25);
cbMainMaterial.Width = 150;
Label lblSecondaryMaterial = new Label();
lblSecondaryMaterial.Text = "次龙骨:";
lblSecondaryMaterial.Location = new System.Drawing.Point(20, 70);
ComboBox cbSecondaryMaterial = new ComboBox();
cbSecondaryMaterial.Items.AddRange(new object[] { "3木方", "5大方", "50钢包木" });
cbSecondaryMaterial.Text = settings.BeamSecondaryMaterial;
cbSecondaryMaterial.Location = new System.Drawing.Point(120, 65);
cbSecondaryMaterial.Width = 150;
gbMaterial.Controls.Add(lblMainMaterial);
gbMaterial.Controls.Add(cbMainMaterial);
gbMaterial.Controls.Add(lblSecondaryMaterial);
gbMaterial.Controls.Add(cbSecondaryMaterial);
tab.Controls.Add(gbMaterial);
}
private void InitializeSlabTab(TabPage tab)
{
tab.Controls.Clear();
tab.AutoScroll = true;
int yPos = 20;
// 默认板厚
GroupBox gbThickness = new GroupBox();
gbThickness.Text = "板厚设置";
gbThickness.Location = new System.Drawing.Point(20, yPos);
gbThickness.Size = new System.Drawing.Size(380, 60);
TextBox tbThickness = new TextBox();
tbThickness.Text = settings.SlabThickness.ToString();
tbThickness.Location = new System.Drawing.Point(150, 20);
tbThickness.Width = 80;
gbThickness.Controls.Add(new Label()
{
Text = "默认板厚(mm):",
Location = new System.Drawing.Point(20, 25)
});
gbThickness.Controls.Add(tbThickness);
tab.Controls.Add(gbThickness);
yPos += 80;
// 绘制板龙骨
GroupBox gbDrawSlab = new GroupBox();
gbDrawSlab.Text = "绘制板龙骨";
gbDrawSlab.Location = new System.Drawing.Point(20, yPos);
gbDrawSlab.Size = new System.Drawing.Size(380, 60);
RadioButton rbSlabYes = new RadioButton();
rbSlabYes.Text = "是";
rbSlabYes.Checked = settings.DrawSlabSupport;
rbSlabYes.Location = new System.Drawing.Point(20, 25);
RadioButton rbSlabNo = new RadioButton();
rbSlabNo.Text = "否";
rbSlabNo.Checked = !settings.DrawSlabSupport;
rbSlabNo.Location = new System.Drawing.Point(100, 25);
gbDrawSlab.Controls.Add(rbSlabYes);
gbDrawSlab.Controls.Add(rbSlabNo);
tab.Controls.Add(gbDrawSlab);
yPos += 80;
// 主龙骨方向
GroupBox gbSlabDirection = new GroupBox();
gbSlabDirection.Text = "主龙骨方向";
gbSlabDirection.Location = new System.Drawing.Point(20, yPos);
gbSlabDirection.Size = new System.Drawing.Size(380, 60);
RadioButton rbSlabHorizontal = new RadioButton();
rbSlabHorizontal.Text = "横向";
rbSlabHorizontal.Checked = (settings.SlabMainDirection == "横向");
rbSlabHorizontal.Location = new System.Drawing.Point(20, 25);
RadioButton rbSlabVertical = new RadioButton();
rbSlabVertical.Text = "纵向";
rbSlabVertical.Checked = (settings.SlabMainDirection == "纵向");
rbSlabVertical.Location = new System.Drawing.Point(100, 25);
gbSlabDirection.Controls.Add(rbSlabHorizontal);
gbSlabDirection.Controls.Add(rbSlabVertical);
tab.Controls.Add(gbSlabDirection);
yPos += 80;
// 次龙骨设置
GroupBox gbSecondary = new GroupBox();
gbSecondary.Text = "次龙骨设置";
gbSecondary.Location = new System.Drawing.Point(20, yPos);
gbSecondary.Size = new System.Drawing.Size(380, 150);
Label lblMaterial = new Label();
lblMaterial.Text = "材料:";
lblMaterial.Location = new System.Drawing.Point(20, 30);
ComboBox cbMaterial = new ComboBox();
cbMaterial.Items.AddRange(new object[] { "3木方", "5大方", "50钢包木" });
cbMaterial.Text = settings.SlabSecondaryMaterial;
cbMaterial.Location = new System.Drawing.Point(120, 25);
cbMaterial.Width = 150;
Label lblSpacing = new Label();
lblSpacing.Text = "间距(mm):";
lblSpacing.Location = new System.Drawing.Point(20, 70);
TextBox tbSpacing = new TextBox();
tbSpacing.Text = settings.SlabSecondarySpacing.ToString();
tbSpacing.Location = new System.Drawing.Point(120, 65);
tbSpacing.Width = 80;
Label lblHeight = new Label();
lblHeight.Text = "高度(mm):";
lblHeight.Location = new System.Drawing.Point(20, 110);
TextBox tbHeight = new TextBox();
tbHeight.Text = settings.SlabSecondaryHeight.ToString();
tbHeight.Location = new System.Drawing.Point(120, 105);
tbHeight.Width = 80;
gbSecondary.Controls.Add(lblMaterial);
gbSecondary.Controls.Add(cbMaterial);
gbSecondary.Controls.Add(lblSpacing);
gbSecondary.Controls.Add(tbSpacing);
gbSecondary.Controls.Add(lblHeight);
gbSecondary.Controls.Add(tbHeight);
tab.Controls.Add(gbSecondary);
}
}
}
CS0104“Application”是“Autodesk.utoCAD.ApplicationServices.Application"和System.Windows.Forms.Application"之间的不明确的引用
最新发布