用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> ThisDrawing.SelectionSets("New_SelectionSet").Delete<BR> Set ssetObj = ThisDrawing.SelectionSets.Add("New_SelectionSet")</P>
<P><BR>'检查选择集是否为空,是则退出程序<BR> ssetObj.SelectOnScreen<BR> n = ThisDrawing.SelectionSets("New_SelectionSet").Count<BR> If n = 0 Then<BR> Exit Sub<BR> 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> p1 = ThisDrawing.Utility.GetPoint(, "请选择旋转中心:")<BR> p2 = ThisDrawing.Utility.GetPoint(p1, "请选择基点:")<BR> k = (p2(1) - p1(1)) / (p2(0) - p1(0))<BR> 'MsgBox "k=" & k<BR>'除数为零,k=无穷大<BR> If Err = 11 Then<BR> If p2(1) < p1(1) Then<BR> angle1 = 1.5 * 3.14159265358979<BR> Else<BR> angle1 = 0.5 * 3.14159265358979<BR> End If<BR> End If<BR> angle1 = Atn(k)<BR>'p2在第二、三象限<BR> If p2(0) < p1(0) Then<BR> angle1 = angle1 + 3.14159265358979<BR> End If<BR> </P>
<P>Dim icount As Integer</P>
<P><BR> While incount < 1000<BR>'如果异常发生,退出程序<BR> If Err <> 0 Then<BR> Exit Sub<BR> Else<BR> p2 = ThisDrawing.Utility.GetPoint(p1, "请选择目标点:")<BR> k = (p2(1) - p1(1)) / (p2(0) - p1(0))<BR> <BR>'除数为零,k=无穷大<BR> If Err = 11 Then<BR> If p2(1) < p1(1) Then<BR> angle2 = 1.5 * 3.14159265358979<BR> Else<BR> angle2 = 0.5 * 3.14159265358979<BR> End If<BR> End If<BR> angle2 = Atn(k)<BR>'p2在第二、三象限<BR> If p2(0) < p1(0) Then<BR> angle2 = angle2 + 3.14159265358979<BR> End If<BR> <BR> angle = angle2 - angle1<BR> <BR> For i = 0 To n - 1<BR> Set ent = ssetObj.Item(i).Copy<BR> ent.Rotate p1, angle<BR> Next<BR> <BR> End If<BR> <BR> Wend<BR> </P>
<P>End Sub</P> <P>没人顶啊!</P> <P>你的程序很好呀。</P>
<P>我改了一个地方应该是输入问题吧。</P>
<P> While icount < 1000</P>
<P>不过你的循环可以用错误处理来代替。因为icount 这个变量的使用。并没有体现出While的真正功能。</P>
<P>个人观点。不要见怪。</P> <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> For i = 0 To ssetObj.Count - 1<BR> Set ent = ssetObj.Item(i).Copy<BR> Angle = Angle2 - Angle1<BR> ent.Rotate ThisDrawing.Utility.TranslateCoordinates(P1, acUCS, acWorld, False), Angle<BR> Next<BR> 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> </P> 强啊,太谢谢各位了!真是太厉害了,以后要多多学习
页:
[1]