min_zxm 发表于 2005-9-14 18:38:00

用vba实现连续旋转加复制功能

<P>程序清单:</P>
<P>Sub copyAndRotate()</P>
<P>Dim ssetObj As AcadSelectionSet<BR>Dim ent As AcadEntity<BR>Dim i As Integer<BR>Dim n As Integer</P>

<P>'新建选择集<BR>On Error Resume Next<BR>&nbsp;&nbsp;&nbsp; ThisDrawing.SelectionSets("New_SelectionSet").Delete<BR>&nbsp;&nbsp;&nbsp; Set ssetObj = ThisDrawing.SelectionSets.Add("New_SelectionSet")</P>
<P><BR>'检查选择集是否为空,是则退出程序<BR>&nbsp;&nbsp;&nbsp; ssetObj.SelectOnScreen<BR>&nbsp;&nbsp;&nbsp; n = ThisDrawing.SelectionSets("New_SelectionSet").Count<BR>&nbsp;&nbsp;&nbsp; If n = 0 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit Sub<BR>&nbsp;&nbsp;&nbsp; End If</P>
<P><BR>'确定目标点<BR>Dim p1 As Variant<BR>Dim p2 As Variant<BR>Dim k As Double<BR>Dim angle1 As Double<BR>Dim angle2 As Double<BR>Dim angle As Double<BR>&nbsp;p1 = ThisDrawing.Utility.GetPoint(, "请选择旋转中心:")<BR>&nbsp;p2 = ThisDrawing.Utility.GetPoint(p1, "请选择基点:")<BR>&nbsp;k = (p2(1) - p1(1)) / (p2(0) - p1(0))<BR>&nbsp;'MsgBox "k=" &amp; k<BR>'除数为零,k=无穷大<BR>&nbsp;If Err = 11 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp; If p2(1) &lt; p1(1) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp; angle1 = 1.5 * 3.14159265358979<BR>&nbsp;&nbsp;&nbsp;&nbsp; Else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; angle1 = 0.5 * 3.14159265358979<BR>&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>&nbsp;End If<BR>&nbsp;angle1 = Atn(k)<BR>'p2在第二、三象限<BR>&nbsp;If p2(0) &lt; p1(0) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp; angle1 = angle1 + 3.14159265358979<BR>&nbsp;End If<BR>&nbsp;</P>
<P>Dim icount As Integer</P>
<P><BR>&nbsp;While incount &lt; 1000<BR>'如果异常发生,退出程序<BR>&nbsp; If Err &lt;&gt; 0 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit Sub<BR>&nbsp; Else<BR>&nbsp;&nbsp;&nbsp; p2 = ThisDrawing.Utility.GetPoint(p1, "请选择目标点:")<BR>&nbsp;&nbsp;&nbsp; k = (p2(1) - p1(1)) / (p2(0) - p1(0))<BR>&nbsp;&nbsp;&nbsp; <BR>'除数为零,k=无穷大<BR>&nbsp;If Err = 11 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp; If p2(1) &lt; p1(1) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp; angle2 = 1.5 * 3.14159265358979<BR>&nbsp;&nbsp;&nbsp;&nbsp; Else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; angle2 = 0.5 * 3.14159265358979<BR>&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>&nbsp; End If<BR>&nbsp;angle2 = Atn(k)<BR>'p2在第二、三象限<BR>&nbsp;If p2(0) &lt; p1(0) Then<BR>&nbsp;&nbsp;&nbsp;&nbsp; angle2 = angle2 + 3.14159265358979<BR>&nbsp;End If<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; angle = angle2 - angle1<BR>&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp; For i = 0 To n - 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set ent = ssetObj.Item(i).Copy<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ent.Rotate p1, angle<BR>&nbsp;&nbsp;&nbsp;&nbsp; Next<BR>&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp; End If<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;Wend<BR>&nbsp;</P>
<P>End Sub</P>

min_zxm 发表于 2005-9-16 12:42:00

<P>没人顶啊!</P>

MJTD_7777 发表于 2005-9-16 15:03:00

<P>你的程序很好呀。</P>
<P>我改了一个地方应该是输入问题吧。</P>
<P>&nbsp;While icount &lt; 1000</P>
<P>不过你的循环可以用错误处理来代替。因为icount 这个变量的使用。并没有体现出While的真正功能。</P>
<P>个人观点。不要见怪。</P>

MJTD_7777 发表于 2005-9-16 15:33:00

<P>我修改的程序:(旨在抛砖引玉,没有剽窃原创的意思)</P>
<P><BR>Sub copyAndRotate()</P>
<P>Dim ssetObj As AcadSelectionSet<BR>Dim ent As AcadEntity<BR>Dim i As Integer</P>
<P>Dim Osmode As Integer<BR>ThisDrawing.StartUndoMark<BR>Osmode = ThisDrawing.GetVariable("osmode")<BR>ThisDrawing.SetVariable "osmode", 37 '设成什么看自己习惯了</P>
<P>'新建选择集<BR>On Error Resume Next<BR>ThisDrawing.SelectionSets("New_SelectionSet").Delete<BR>Set ssetObj = ThisDrawing.SelectionSets.Add("New_SelectionSet")</P>
<P><BR>'检查选择集是否为空,是则退出程序<BR>ssetObj.SelectOnScreen<BR>If ssetObj.Count = 0 Then GoTo ExitSub</P>
<P>'确定目标点<BR>Dim P1 As Variant<BR>Dim Angle1 As Double<BR>Dim Angle2 As Double<BR>Dim Angle As Double<BR>P1 = ThisDrawing.Utility.GetPoint(, "请选择旋转中心:")<BR>P1 = ThisDrawing.Utility.TranslateCoordinates(P1, acWorld, acUCS, False) '因为用户当前坐标系可能不是世界坐标系<BR>If Err Then GoTo ExitSub<BR>Angle1 = ThisDrawing.Utility.GetAngle(P1, "请选择基点:")<BR>If Err Then GoTo ExitSub<BR>Angle2 = ThisDrawing.Utility.GetAngle(P1, "请选择目标点:")<BR>If Err Then GoTo ExitSub</P>
<P>Do While Err.Number = 0<BR>&nbsp;&nbsp;&nbsp; For i = 0 To ssetObj.Count - 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set ent = ssetObj.Item(i).Copy<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Angle = Angle2 - Angle1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ent.Rotate ThisDrawing.Utility.TranslateCoordinates(P1, acUCS, acWorld, False), Angle<BR>&nbsp;&nbsp;&nbsp; Next<BR>&nbsp;&nbsp;&nbsp; Angle2 = ThisDrawing.Utility.GetAngle(P1, "请选择目标点:")<BR>Loop</P>
<P>ExitSub:<BR>ThisDrawing.SetVariable "osmode", Osmode<BR>ThisDrawing.EndUndoMark<BR>End Sub</P>
<P><BR><BR>&nbsp;</P>

min_zxm 发表于 2005-9-16 19:20:00

强啊,太谢谢各位了!真是太厉害了,以后要多多学习
页: [1]
查看完整版本: 用vba实现连续旋转加复制功能