12qq21 发表于 2009-8-24 22:05:00

[求助]请问如何实现自动选择图元?谢谢!

<p></p><p>请问:</p><p>根据“使用VBA进行截断(break)和修剪(trim)”,我现在想利用双元素来实现“Trim”,请问怎么实现给定坐标点让CAD自动来选择图元,谢谢!<br/>Sub Trim()<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;Dim Pnt1 As Variant<br/>&nbsp;&nbsp;&nbsp;&nbsp;Dim entObj1 As AcadEntity<br/>&nbsp;&nbsp;&nbsp;&nbsp;ThisDrawing.Utility.GetEntity entObj1, Pnt1, "选择图元:"<br/>&nbsp;&nbsp;&nbsp;&nbsp;Dim det1 As String<br/>&nbsp;&nbsp;&nbsp;&nbsp;det1 = axEnt2lspEnt(entObj1)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;Dim Pnt2 As Variant<br/>&nbsp;&nbsp;&nbsp;&nbsp;Dim entObj2 As AcadEntity<br/>&nbsp;&nbsp;&nbsp;&nbsp;ThisDrawing.Utility.GetEntity entObj2, Pnt2, "选择被剪图元:"<br/>&nbsp;&nbsp;&nbsp;&nbsp;Dim det2 As String<br/>&nbsp;&nbsp;&nbsp;&nbsp;det2 = GetDoubleEntTable(entObj2, Pnt2)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;ThisDrawing.SendCommand "_trim" &amp; vbCr &amp; det1 &amp; vbCr &amp; vbCr &amp; det2 &amp; vbCr &amp; vbCr<br/><br/>End Sub<br/><br/>'转换双元表的函数<br/><br/>Public Function GetDoubleEntTable(entObj As AcadEntity, Pnt As Variant) As String<br/>&nbsp;&nbsp;&nbsp;&nbsp;Dim entHandle As String<br/>&nbsp;&nbsp;&nbsp;&nbsp;entHandle = entObj.Handle<br/>&nbsp;&nbsp;&nbsp;&nbsp;GetDoubleEntTable = "(list(handent " &amp; Chr(34) &amp; entHandle &amp; Chr(34) &amp; _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ")(list " &amp; Str(Pnt(0)) &amp; Str(Pnt(1)) &amp; Str(Pnt(2)) &amp; "))"<br/>End Function<br/><br/>'转换点的函数<br/><br/>Public Function axPoint2lspPoint(Pnt As Variant) As String<br/>&nbsp;&nbsp;&nbsp;&nbsp;axPoint2lspPoint = Pnt(0) &amp; "," &amp; Pnt(1) &amp; "," &amp; Pnt(2)<br/>End Function<br/><br/>'转换图元函数<br/><br/>Public Function axEnt2lspEnt(entObj As AcadEntity) As String<br/>&nbsp;&nbsp;&nbsp;&nbsp;Dim entHandle As String<br/>&nbsp;&nbsp;&nbsp;&nbsp;entHandle = entObj.Handle<br/>&nbsp;&nbsp;&nbsp;&nbsp;axEnt2lspEnt = "(handent " &amp; Chr(34) &amp; entHandle &amp; Chr(34) &amp; ")"<br/>End Function</p>

mccad 发表于 2009-8-25 06:32:00

<p>给定坐标点,要选择到图元,则需要点刚好在图元上, 这样用选择集的SelectAtPoint方法来取得经过 该点的图元。</p>

xiaoyaobest 发表于 2009-8-25 22:19:00

<p>给两点意见:</p><p>1.你自已的指令不要与AUTOCAD自身的指令一致(TRIM是CAD自带的命令代号),这是很不好的行为,因为你会改变AUTOCAD本身的命令,我们在任何时候都不可以这样做,否则在大范围推广时会备受“功击”,并不是每个人都认为你的指令有意义或比CAD自身的更好用;</p><p>2.既然是用VBA做,建议不要在用 SendCommand 和 LISP 的一些方法(特别是SendCommand,能不用尽量不用,尽量用算法解决),原因我在此不累述了,你可以看下明经版主写的一本VBA开发的书。</p>

12qq21 发表于 2009-8-26 21:47:00

收到 谢谢二位的解答启发 我一一尝试下 谢谢!

12qq21 发表于 2009-8-27 00:15:00

<p>我进行了一个尝试,可是到那个sendcommand命令时总是执行错误,请帮我改改,谢谢!</p><p>Dim oAcadApp</p><p>Public Sub SelectAtPoint(ByRef SSet As AcadSelectionSet, ByVal pt As Variant)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; 'SSet.Select acSelectionSetCrossing, pt, pt<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim pt1 As Variant, pt2 As Variant<br/>&nbsp;&nbsp;&nbsp; Dim objUtility As Object<br/>&nbsp;&nbsp;&nbsp; Set objUtility = oAcadApp.ActiveDocument.Utility&nbsp;&nbsp;&nbsp; ' 必须使用后期绑定<br/>&nbsp;&nbsp;&nbsp; objUtility.CreateTypedArray pt1, vbDouble, pt(0) - 0.0001, pt(1) - 0.0001, pt(2)<br/>&nbsp;&nbsp;&nbsp; objUtility.CreateTypedArray pt2, vbDouble, pt(0) + 0.0001, pt(1) + 0.0001, pt(2)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; SSet.Select acSelectionSetCrossing, pt1, pt2</p><p>End Sub</p><p>Private Sub Command10_Click()<br/>Dim pt1(0 To 2) As Double<br/>&nbsp;Dim pt2(0 To 2) As Double<br/>&nbsp; Dim pt5(0 To 2) As Double<br/>Dim ss As AcadSelectionSet<br/>Dim dd As AcadSelectionSet<br/>&nbsp; Dim line1, line2, r1</p><p>&nbsp;&nbsp; pt1(0) = 0<br/>&nbsp;&nbsp; pt1(1) = 0<br/>&nbsp;&nbsp; pt1(2) = 0<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pt5(0) = 5<br/>&nbsp;&nbsp; pt5(1) = 0<br/>&nbsp;&nbsp; pt5(2) = 0<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pt2(0) = 10<br/>&nbsp;&nbsp; pt2(1) = 0<br/>&nbsp;&nbsp; pt2(2) = 0<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim pt3(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp;&nbsp; Dim pt4(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim pt6(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pt3(0) = 1<br/>&nbsp;&nbsp; pt3(1) = 0<br/>&nbsp;&nbsp; pt3(2) = 0<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pt4(0) = 1<br/>&nbsp;&nbsp; pt4(1) = 10<br/>&nbsp;&nbsp; pt4(2) = 0<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pt6(0) = 1<br/>&nbsp;&nbsp; pt6(1) = 5<br/>&nbsp;&nbsp; pt6(2) = 0<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r1 = 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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 " &amp; ss.Count &amp; " has been added to the SelectionSets collection.", vbInformation, "SelectionSets 示例"<br/>MsgBox "A new SelectionSet called " &amp; dd.Count &amp; " has been added to the SelectionSets collection.", vbInformation, "SelectionSets 示例"</p><p>oAcadApp.ActiveDocument.SendCommand "_FILLET" &amp; vbCr &amp; "r" &amp; vbCr &amp; r1 &amp; vbCr &amp; _<br/>"(handent " &amp; Chr(34) &amp; x1.Handle &amp; Chr(34) &amp; ")" &amp; vbCr &amp; "(handent " &amp; Chr(34) &amp; x2.Handle &amp; Chr(34) &amp; ")" &amp; vbCr</p><p><br/>End Sub</p>
页: [1]
查看完整版本: [求助]请问如何实现自动选择图元?谢谢!