lijun337 发表于 2009-1-28 18:56:00

[求助]想试着用VBA画出线段AB出来

我是个初学者,想学习VBA FOR ACAD,<br/><br/>虽然,我看了《入门》,但是上面的例子还是不太好懂,所以我自己试着写了个,请帮我看看,是否正确?<br/><br/>想试着用VBA画出线段AB出来,<br/><br/>构思是这样的:在ACAD的宏运行时,弹出一个执行窗体,在文字框里输入原始数据后,按命令按钮,得到A点和B点的坐标数值,在ACAD里自动连接AB点,画出线段AB,<br/><br/><br/><br/>具体操作是这样的,<br/>我在AUTOCAD2004---》工具----》宏----》Visua Basic 编辑器&nbsp;&nbsp;插入一个用户窗体和一个模块,<br/><br/>在用户窗体里添加一个命令按钮:CommandButton1<br/>和<br/>12个文字框:<br/>TextBox1.Text、TextBox2.Text、TextBox3.Text、<br/>TextBox4.Text、TextBox5.Text,TextBox6.Text<br/><br/>TextBox7.Text、TextBox8.Text、TextBox9.Text、<br/>TextBox10.Text、TextBox11.Text,TextBox12.Text<br/>输入如下代码:<br/><br/>Dim X, b,c,Y,e ,f,X2,g,h,Y2,i,j As Single<br/><br/><br/>b= Val(TextBox2.Text)<br/>c= Val(TextBox3.Text)<br/>e= Val(TextBox5.Text)<br/>f = Val(TextBox6.Text)<br/>g= Val(TextBox8.Text)<br/>h= Val(TextBox9.Text)<br/>i= Val(TextBox11.Text)<br/>j= Val(TextBox12.Text)<br/><br/>Private Sub CommandButton1_Click()<br/>X=b+c<br/>Y=e+f<br/>X2=g+h<br/>Y2=i+j<br/>TextBox1.Text=val(X)<br/>TextBox4.Text=val(Y)<br/>TextBox7.Text=val(X2)<br/>TextBox10.Text=val(Y2)<br/>end sub<br/><br/>用户窗体,我打算就这么着写,和学习VB的方法思路完全一样,但是如何画到CAD里<br/>去?<br/><br/>请问,怎么在模块里写呀?<br/><br/>又如何和模块结合起来一起执行呢?<br/><br/>帮我补充下,好么?<br/><br/>或者干脆不要模块,直接在用户窗体里写在一起,是不是也可以呢?<br/><br/>如何写呢?<br/><br/>谢谢!

dianbotang 发表于 2009-3-25 23:07:00

<p>问题很简单,看看示例文件就可自己解决。文本框控件显得有点乱,可再整理一下。</p><p>在窗体模块中添加以下代码:</p><p>Option Explicit<br/>Private Sub CommandButton1_Click()<br/>&nbsp; Dim X, b, c, Y, e, f, X2, g, h, Y2, i, j As Double<br/>&nbsp; <br/>&nbsp; b = Val(TextBox2.Text)<br/>&nbsp; c = Val(TextBox3.Text)<br/>&nbsp; e = Val(TextBox5.Text)<br/>&nbsp; f = Val(TextBox6.Text)<br/>&nbsp; g = Val(TextBox8.Text)<br/>&nbsp; h = Val(TextBox9.Text)<br/>&nbsp; i = Val(TextBox11.Text)<br/>&nbsp; j = Val(TextBox12.Text)<br/>&nbsp; <br/>&nbsp; X = b + c<br/>&nbsp; Y = e + f<br/>&nbsp; X2 = g + h<br/>&nbsp; Y2 = i + j<br/>&nbsp; TextBox1.Text = X<br/>&nbsp; TextBox4.Text = Y<br/>&nbsp; TextBox7.Text = X2<br/>&nbsp; TextBox10.Text = Y2<br/>&nbsp; <br/>&nbsp; Dim lineObj As AcadLine<br/>&nbsp; Dim startPoint(0 To 2) As Double<br/>&nbsp; Dim endPoint(0 To 2) As Double<br/>&nbsp; <br/>&nbsp; ' 定义直线的起点和终点<br/>&nbsp; startPoint(0) = X: startPoint(1) = Y: startPoint(2) = 0#<br/>&nbsp; endPoint(0) = X2: endPoint(1) = Y2: endPoint(2) = 0#<br/>&nbsp; <br/>&nbsp; ' 在模型空间中创建直线<br/>&nbsp; Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)<br/>&nbsp; ZoomAll<br/>&nbsp; <br/>End Sub</p>
页: [1]
查看完整版本: [求助]想试着用VBA画出线段AB出来