1、下载并安装AutoCAD 2004正式版,再打开。选择工具菜单下的宏下的Visual Basic编辑器。
2、在工程的空白处插入模块。
3、复制以上代码进模块并保存,然后返回CAD点击工具菜单下的加载应用程序,选择刚保存的这个文件点击加载。
4、然后再点击工具菜单下的宏-宏(Alt+F8)运行此脚本。经过对CAD屏幕的缩放后就可以看到你刚才编辑的这条直线了。起点(012)为起点(XYZ)的坐标。
5、为什么要提二次开发呢,这重点是为了方便和准确,以上的XYZ坐标值还可以是变量,经过变量的相关计算我们还可以制作更多更方便的脚本。
6、以下就是一个比较复杂的按变量和公式画图的例子,只写了关于确定按钮的事件(以下的例子是我在一玻璃切割公司使用滠锖斟绸此脚本制作的方便切割绘图的小程序之一): Private Sub CommandButton1_Click()c = Val(TextBox1.Text)d = Val(TextBox2.Text)x = Val(TextBox3.Text)dj1 = Val(TextBox4.Text)TextBox5.Text = d - x - dj1If OptionButton1.Value = True ThenIf OptionButton3.Value = True ThenFor i = 0 To Int(Val(TextBox7.Text) / 2) Step 1i = ifk1 = c * Val(TextBox6.Text)fk2 = (d + x) * iDim 封口 As AcadLineDim 封口起点(2) As DoubleDim 封口端点(2) As Double封口起点(0) = fk2封口起点(1) = 0 - Val(TextBox10.Text)封口端点(0) = fk2封口端点(1) = fk1 + Val(TextBox8.Text)Set 封口 = ThisDrawing.ModelSpace.AddLine(封口起点, 封口端点)NextElseFor i = 0 To Int(Val(TextBox7.Text) / 2) Step 1i = ifk1 = c * Val(TextBox6.Text)fk2 = (d + d) * iDim 封口1 As AcadLineDim 封口起点1(2) As DoubleDim 封口端点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)NextEnd IfIf OptionButton3.Value = True ThenFor n = 0 To Int(Val(TextBox7.Text) / 2 + (Val(TextBox7.Text) / 2 - Int(Val(TextBox7.Text) / 2)) * 2 - 1) Step 1n = nfk12 = c * Val(TextBox6.Text)fk22 = d + (d + x) * nDim 封口2 As AcadLineDim 封口起点2(2) As DoubleDim 封口端点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)NextElseFor n = 0 To Int(Val(TextBox7.Text) / 2 + (Val(TextBox7.Text) / 2 - Int(Val(TextBox7.Text) / 2)) * 2 - 1) Step 1n = nfk12 = c * Val(TextBox6.Text)fk22 = d + (d + d) * nDim 封口21 As AcadLineDim 封口起点21(2) As DoubleDim 封口端点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)NextEnd IfIf OptionButton3.Value = True ThenFor m = 0 To (Val(TextBox6.Text)) Step 1m = mfk13 = (d + x) * Int(Val(TextBox7.Text) / 2)fk23 = c * mDim 封口3 As AcadLineDim 封口起点3(2) As DoubleDim 封口端点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) = fk23Set 封口3 = ThisDrawing.ModelSpace.AddLine(封口起点3, 封口端点3)NextElseFor m = 0 To (Val(TextBox6.Text)) Step 1m = mfk13 = (d + d) * Int(Val(TextBox7.Text) / 2)fk23 = c * mDim 封口31 As AcadLineDim 封口起点31(2) As DoubleDim 封口端点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) = fk23Set 封口31 = ThisDrawing.ModelSpace.AddLine(封口起点31, 封口端点31)NextEnd IfElseIf OptionButton2.Value = True ThenIf OptionButton3.Value = True ThenFor i = 0 To Int(Val(TextBox7.Text) / 2) Step 1i = ifk1 = c * Val(TextBox6.Text)fk2 = dj1 + (d + x) * iDim 封口12 As AcadLineDim 封口起点12(2) As DoubleDim 封口端点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)NextElseFor i = 0 To Int(Val(TextBox7.Text) - 1) Step 1i = ifk1 = c * Val(TextBox6.Text)fk2 = dj1 + d * iDim 封口121 As AcadLineDim 封口起点121(2) As DoubleDim 封口端点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)NextEnd IfIf OptionButton3.Value = True ThenFor n = 0 To Int(Val(TextBox7.Text) / 2 + (Val(TextBox7.Text) / 2 - Int(Val(TextBox7.Text) / 2)) * 2 - 1) Step 1n = nfk12 = c * Val(TextBox6.Text)fk22 = dj1 + x + (d + x) * nDim 封口122 As AcadLineDim 封口起点122(2) As DoubleDim 封口端点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)NextElseFor n = 0 To Int(Val(TextBox7.Text) - 1) Step 1n = nfk12 = c * Val(TextBox6.Text)fk22 = (x + dj1) + d * nDim 封口222 As AcadLineDim 封口起点222(2) As DoubleDim 封口端点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)NextEnd IfIf OptionButton3.Value = True ThenFor m = 0 To (Val(TextBox6.Text)) Step 1m = mfk13 = (d + x) * Int(Val(TextBox7.Text) / 2)fk23 = c * mDim 封口311 As AcadLineDim 封口起点311(2) As DoubleDim 封口端点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) = fk23Set 封口311 = ThisDrawing.ModelSpace.AddLine(封口起点311, 封口端点311)NextElseFor m = 0 To (Val(TextBox6.Text)) Step 1m = mfk13 = d * (Int(Val(TextBox7.Text) - 1))fk23 = c * mDim 封口312 As AcadLineDim 封口起点312(2) As DoubleDim 封口端点312(2) As Double封口起点312(0) = dj1 - Val(TextBox9.Text) - dj1封口起点312(1) = fk23封口端点312(0) = fk13 + d + Val(TextBox11.Text)封口端点312(1) = fk23Set 封口312 = ThisDrawing.ModelSpace.AddLine(封口起点312, 封口端点312)NextEnd IfElseMsgBox "请选择一个正面或是反面进行绘图!", vbOKOnly, "提示:"End IfEnd IfEnd Sub
7、假如说你新建的窗体名称是叫hpcc,那你就必须新建模块如下加以启动它: Sub AAA()hpcc.ShowEnd Sub意思是加载此模块时显示该窗体。