[求助]请问如何实现自动选择图元?谢谢!
<p></p><p>请问:</p><p>根据“使用VBA进行截断(break)和修剪(trim)”,我现在想利用双元素来实现“Trim”,请问怎么实现给定坐标点让CAD自动来选择图元,谢谢!<br/>Sub Trim()<br/><br/> Dim Pnt1 As Variant<br/> Dim entObj1 As AcadEntity<br/> ThisDrawing.Utility.GetEntity entObj1, Pnt1, "选择图元:"<br/> Dim det1 As String<br/> det1 = axEnt2lspEnt(entObj1)<br/><br/> Dim Pnt2 As Variant<br/> Dim entObj2 As AcadEntity<br/> ThisDrawing.Utility.GetEntity entObj2, Pnt2, "选择被剪图元:"<br/> Dim det2 As String<br/> det2 = GetDoubleEntTable(entObj2, Pnt2)<br/><br/> ThisDrawing.SendCommand "_trim" & vbCr & det1 & vbCr & vbCr & det2 & vbCr & vbCr<br/><br/>End Sub<br/><br/>'转换双元表的函数<br/><br/>Public Function GetDoubleEntTable(entObj As AcadEntity, Pnt As Variant) As String<br/> Dim entHandle As String<br/> entHandle = entObj.Handle<br/> GetDoubleEntTable = "(list(handent " & Chr(34) & entHandle & Chr(34) & _<br/> ")(list " & Str(Pnt(0)) & Str(Pnt(1)) & Str(Pnt(2)) & "))"<br/>End Function<br/><br/>'转换点的函数<br/><br/>Public Function axPoint2lspPoint(Pnt As Variant) As String<br/> axPoint2lspPoint = Pnt(0) & "," & Pnt(1) & "," & Pnt(2)<br/>End Function<br/><br/>'转换图元函数<br/><br/>Public Function axEnt2lspEnt(entObj As AcadEntity) As String<br/> Dim entHandle As String<br/> entHandle = entObj.Handle<br/> axEnt2lspEnt = "(handent " & Chr(34) & entHandle & Chr(34) & ")"<br/>End Function</p> <p>给定坐标点,要选择到图元,则需要点刚好在图元上, 这样用选择集的SelectAtPoint方法来取得经过 该点的图元。</p> <p>给两点意见:</p><p>1.你自已的指令不要与AUTOCAD自身的指令一致(TRIM是CAD自带的命令代号),这是很不好的行为,因为你会改变AUTOCAD本身的命令,我们在任何时候都不可以这样做,否则在大范围推广时会备受“功击”,并不是每个人都认为你的指令有意义或比CAD自身的更好用;</p><p>2.既然是用VBA做,建议不要在用 SendCommand 和 LISP 的一些方法(特别是SendCommand,能不用尽量不用,尽量用算法解决),原因我在此不累述了,你可以看下明经版主写的一本VBA开发的书。</p> 收到 谢谢二位的解答启发 我一一尝试下 谢谢! <p>我进行了一个尝试,可是到那个sendcommand命令时总是执行错误,请帮我改改,谢谢!</p><p>Dim oAcadApp</p><p>Public Sub SelectAtPoint(ByRef SSet As AcadSelectionSet, ByVal pt As Variant)<br/> <br/> 'SSet.Select acSelectionSetCrossing, pt, pt<br/> <br/> Dim pt1 As Variant, pt2 As Variant<br/> Dim objUtility As Object<br/> Set objUtility = oAcadApp.ActiveDocument.Utility ' 必须使用后期绑定<br/> objUtility.CreateTypedArray pt1, vbDouble, pt(0) - 0.0001, pt(1) - 0.0001, pt(2)<br/> objUtility.CreateTypedArray pt2, vbDouble, pt(0) + 0.0001, pt(1) + 0.0001, pt(2)<br/> <br/> SSet.Select acSelectionSetCrossing, pt1, pt2</p><p>End Sub</p><p>Private Sub Command10_Click()<br/>Dim pt1(0 To 2) As Double<br/> Dim pt2(0 To 2) As Double<br/> Dim pt5(0 To 2) As Double<br/>Dim ss As AcadSelectionSet<br/>Dim dd As AcadSelectionSet<br/> Dim line1, line2, r1</p><p> pt1(0) = 0<br/> pt1(1) = 0<br/> pt1(2) = 0<br/> <br/> pt5(0) = 5<br/> pt5(1) = 0<br/> pt5(2) = 0<br/> <br/> pt2(0) = 10<br/> pt2(1) = 0<br/> pt2(2) = 0<br/> Dim pt3(0 To 2) As Double<br/> Dim pt4(0 To 2) As Double<br/> Dim pt6(0 To 2) As Double<br/> pt3(0) = 1<br/> pt3(1) = 0<br/> pt3(2) = 0<br/> <br/> pt4(0) = 1<br/> pt4(1) = 10<br/> pt4(2) = 0<br/> <br/> pt6(0) = 1<br/> pt6(1) = 5<br/> pt6(2) = 0<br/> <br/> r1 = 1<br/> Set line1 = AddLine(pt1, pt2)<br/>Set line2 = AddLine(pt4, pt3)<br/>Set ss = oAcadApp.ActiveDocument.SelectionSets.Add("d1")<br/>Set dd = oAcadApp.ActiveDocument.SelectionSets.Add("d2")<br/>SelectAtPoint ss, pt5<br/>SelectAtPoint dd, pt6<br/>Dim x1 As AcadLine<br/>Dim x2 As AcadLine<br/>Set x1 = ss.Item(0)<br/>Set x2 = dd.Item(0)<br/>MsgBox "A new SelectionSet called " & ss.Count & " has been added to the SelectionSets collection.", vbInformation, "SelectionSets 示例"<br/>MsgBox "A new SelectionSet called " & dd.Count & " has been added to the SelectionSets collection.", vbInformation, "SelectionSets 示例"</p><p>oAcadApp.ActiveDocument.SendCommand "_FILLET" & vbCr & "r" & vbCr & r1 & vbCr & _<br/>"(handent " & Chr(34) & x1.Handle & Chr(34) & ")" & vbCr & "(handent " & Chr(34) & x2.Handle & Chr(34) & ")" & vbCr</p><p><br/>End Sub</p>
页:
[1]