CAD二次开发基础——VBA画直线
来源:网络收集 点击: 时间:2024-05-08下载并安装AutoCAD 2004正式版,再打开。选择工具菜单下的宏下的Visual Basic编辑器。

在工程的空白处插入模块。

复制以上代码进模块并保存,然后返回CAD点击工具菜单下的加载应用程序,选择刚保存的这个文件点击加载。


然后再点击工具菜单下的宏-宏(Alt+F8)运行此脚本。经过对CAD屏幕的缩放后就可以看到你刚才编辑的这条直线了。起点(012)为起点(XYZ)的坐标。


为什么要提二次开发呢,这重点是为了方便和准确,以上的XYZ坐标值还可以是变量,经过变量的相关计算我们还可以制作更多更方便的脚本。
6/7以下就是一个比较复杂的按变量和公式画图的例子,只写了关于确定按钮的事件(以下的例子是我在一玻璃切割公司使用此脚本制作的方便切割绘图的小程序之一): Private Sub CommandButton1_Click()
c = Val(TextBox1.Text)
d = Val(TextBox2.Text)
x = Val(TextBox3.Text)
dj1 = Val(TextBox4.Text)
TextBox5.Text = d - x - dj1
If OptionButton1.Value = True Then
If OptionButton3.Value = True Then
For i = 0 To Int(Val(TextBox7.Text) / 2) Step 1
i = i
fk1 = c * Val(TextBox6.Text)
fk2 = (d + x) * i
Dim 封口 As AcadLine
Dim 封口起点(2) As Double
Dim 封口端点(2) As Double
封口起点(0) = fk2
封口起点(1) = 0 - Val(TextBox10.Text)
封口端点(0) = fk2
封口端点(1) = fk1 + Val(TextBox8.Text)
Set 封口 = ThisDrawing.ModelSpace.AddLine(封口起点, 封口端点)
Next
Else
For i = 0 To Int(Val(TextBox7.Text) / 2) Step 1
i = i
fk1 = c * Val(TextBox6.Text)
fk2 = (d + d) * i
Dim 封口1 As AcadLine
Dim 封口起点1(2) As Double
Dim 封口端点1(2) As Double
封口起点1(0) = fk2
封口起点1(1) = 0 - Val(TextBox10.Text)
封口端点1(0) = fk2
封口端点1(1) = fk1 + Val(TextBox8.Text)
Set 封口1 = ThisDrawing.ModelSpace.AddLine(封口起点1, 封口端点1)
Next
End If
If OptionButton3.Value = True Then
For n = 0 To Int(Val(TextBox7.Text) / 2 + (Val(TextBox7.Text) / 2 - Int(Val(TextBox7.Text) / 2)) * 2 - 1) Step 1
n = n
fk12 = c * Val(TextBox6.Text)
fk22 = d + (d + x) * n
Dim 封口2 As AcadLine
Dim 封口起点2(2) As Double
Dim 封口端点2(2) As Double
封口起点2(0) = fk22
封口起点2(1) = 0 - Val(TextBox10.Text)
封口端点2(0) = fk22
封口端点2(1) = fk12 + Val(TextBox8.Text)
Set 封口2 = ThisDrawing.ModelSpace.AddLine(封口起点2, 封口端点2)
Next
Else
For n = 0 To Int(Val(TextBox7.Text) / 2 + (Val(TextBox7.Text) / 2 - Int(Val(TextBox7.Text) / 2)) * 2 - 1) Step 1
n = n
fk12 = c * Val(TextBox6.Text)
fk22 = d + (d + d) * n
Dim 封口21 As AcadLine
Dim 封口起点21(2) As Double
Dim 封口端点21(2) As Double
封口起点21(0) = fk22
封口起点21(1) = 0 - Val(TextBox10.Text)
封口端点21(0) = fk22
封口端点21(1) = fk12 + Val(TextBox8.Text)
Set 封口21 = ThisDrawing.ModelSpace.AddLine(封口起点21, 封口端点21)
Next
End If
If OptionButton3.Value = True Then
For m = 0 To (Val(TextBox6.Text)) Step 1
m = m
fk13 = (d + x) * Int(Val(TextBox7.Text) / 2)
fk23 = c * m
Dim 封口3 As AcadLine
Dim 封口起点3(2) As Double
Dim 封口端点3(2) As Double
封口起点3(0) = 0 - Val(TextBox9.Text)
封口起点3(1) = fk23
封口端点3(0) = fk13 + (Val(TextBox7.Text) / 2 - Int(Val(TextBox7.Text) / 2)) * 2 * d + Val(TextBox11.Text)
封口端点3(1) = fk23
Set 封口3 = ThisDrawing.ModelSpace.AddLine(封口起点3, 封口端点3)
Next
Else
For m = 0 To (Val(TextBox6.Text)) Step 1
m = m
fk13 = (d + d) * Int(Val(TextBox7.Text) / 2)
fk23 = c * m
Dim 封口31 As AcadLine
Dim 封口起点31(2) As Double
Dim 封口端点31(2) As Double
封口起点31(0) = 0 - Val(TextBox9.Text)
封口起点31(1) = fk23
封口端点31(0) = fk13 + Val(TextBox11.Text) + d * ((Val(TextBox7.Text) / 2 - Int(Val(TextBox7.Text) / 2)) * 2)
封口端点31(1) = fk23
Set 封口31 = ThisDrawing.ModelSpace.AddLine(封口起点31, 封口端点31)
Next
End If
Else
If OptionButton2.Value = True Then
If OptionButton3.Value = True Then
For i = 0 To Int(Val(TextBox7.Text) / 2) Step 1
i = i
fk1 = c * Val(TextBox6.Text)
fk2 = dj1 + (d + x) * i
Dim 封口12 As AcadLine
Dim 封口起点12(2) As Double
Dim 封口端点12(2) As Double
封口起点12(0) = fk2
封口起点12(1) = 0 - Val(TextBox10.Text)
封口端点12(0) = fk2
封口端点12(1) = fk1 + Val(TextBox8.Text)
Set 封口12 = ThisDrawing.ModelSpace.AddLine(封口起点12, 封口端点12)
Next
Else
For i = 0 To Int(Val(TextBox7.Text) - 1) Step 1
i = i
fk1 = c * Val(TextBox6.Text)
fk2 = dj1 + d * i
Dim 封口121 As AcadLine
Dim 封口起点121(2) As Double
Dim 封口端点121(2) As Double
封口起点121(0) = fk2
封口起点121(1) = 0 - Val(TextBox10.Text)
封口端点121(0) = fk2
封口端点121(1) = fk1 + Val(TextBox8.Text)
Set 封口121 = ThisDrawing.ModelSpace.AddLine(封口起点121, 封口端点121)
Next
End If
If OptionButton3.Value = True Then
For n = 0 To Int(Val(TextBox7.Text) / 2 + (Val(TextBox7.Text) / 2 - Int(Val(TextBox7.Text) / 2)) * 2 - 1) Step 1
n = n
fk12 = c * Val(TextBox6.Text)
fk22 = dj1 + x + (d + x) * n
Dim 封口122 As AcadLine
Dim 封口起点122(2) As Double
Dim 封口端点122(2) As Double
封口起点122(0) = fk22
封口起点122(1) = 0 - Val(TextBox10.Text)
封口端点122(0) = fk22
封口端点122(1) = fk12 + Val(TextBox8.Text)
Set 封口122 = ThisDrawing.ModelSpace.AddLine(封口起点122, 封口端点122)
Next
Else
For n = 0 To Int(Val(TextBox7.Text) - 1) Step 1
n = n
fk12 = c * Val(TextBox6.Text)
fk22 = (x + dj1) + d * n
Dim 封口222 As AcadLine
Dim 封口起点222(2) As Double
Dim 封口端点222(2) As Double
封口起点222(0) = fk22
封口起点222(1) = 0 - Val(TextBox10.Text)
封口端点222(0) = fk22
封口端点222(1) = fk12 + Val(TextBox8.Text)
Set 封口222 = ThisDrawing.ModelSpace.AddLine(封口起点222, 封口端点222)
Next
End If
If OptionButton3.Value = True Then
For m = 0 To (Val(TextBox6.Text)) Step 1
m = m
fk13 = (d + x) * Int(Val(TextBox7.Text) / 2)
fk23 = c * m
Dim 封口311 As AcadLine
Dim 封口起点311(2) As Double
Dim 封口端点311(2) As Double
封口起点311(0) = 0 - Val(TextBox9.Text)
封口起点311(1) = fk23
封口端点311(0) = fk13 + (Val(TextBox7.Text) / 2 - Int(Val(TextBox7.Text) / 2)) * 2 * d + Val(TextBox11.Text) + dj1 + dj2
封口端点311(1) = fk23
Set 封口311 = ThisDrawing.ModelSpace.AddLine(封口起点311, 封口端点311)
Next
Else
For m = 0 To (Val(TextBox6.Text)) Step 1
m = m
fk13 = d * (Int(Val(TextBox7.Text) - 1))
fk23 = c * m
Dim 封口312 As AcadLine
Dim 封口起点312(2) As Double
Dim 封口端点312(2) As Double
封口起点312(0) = dj1 - Val(TextBox9.Text) - dj1
封口起点312(1) = fk23
封口端点312(0) = fk13 + d + Val(TextBox11.Text)
封口端点312(1) = fk23
Set 封口312 = ThisDrawing.ModelSpace.AddLine(封口起点312, 封口端点312)
Next
End If
Else
MsgBox 请选择一个正面或是反面进行绘图!, vbOKOnly, 提示:
End If
End If
End Sub

假如说你新建的窗体名称是叫hpcc,那你就必须新建模块如下加以启动它: Sub AAA()
hpcc.Show
End Sub
意思是加载此模块时显示该窗体。
注意事项必须要有充足完整的可计算的数据,不然会出错。
——版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_713398.html