请帮忙看看为什么出错??
以下代码,如果先画出直线,再画多段线就能求出交点但如果先画多段线,再画直线就求不出交点(只要有一条多段线画的顺序在直线前边就不行),请帮忙看一下,在线等待
Sub Example_Select()<BR> <BR> ' 创建选择集<BR> Dim ssetObj As AcadSelectionSet<BR> <BR> On Error Resume Next<BR> Set ssetObj = ThisDrawing.SelectionSets("SSET")<BR> If Err Then<BR> Err.Clear<BR> Set ssetObj = ThisDrawing.SelectionSets.Add("SSET")<BR> End If<BR> ssetObj.Clear<BR> <BR> '构造过滤机制<BR> Dim groupCode(0) As Integer<BR> Dim dataCode(0) As Variant<BR> groupCode(0) = 0<BR> dataCode(0) = "lwPolyline"
ssetObj.Select acSelectionSetAll, , , groupCode, dataCode<BR>
<BR> '获取直线的外框<BR> Dim corner1 As Variant<BR> Dim corner2 As Variant<BR> Dim lineObj As AcadLine<BR> Set lineObj = ThisDrawing.ModelSpace(0)<BR> lineObj.GetBoundingBox corner1, corner2 'lineObj为位于0层的直线<BR> <BR> ssetObj.Select acSelectionSetCrossing, corner1, corner2, groupCode, dataCode<BR> <BR> '枚举交点,判断是否相交<BR> Dim Pts As Variant<BR> Dim i As Integer<BR> Dim j As Integer<BR> For i = 0 To ssetObj.Count - 1<BR> Pts = ssetObj(i).IntersectWith(lineObj, acExtendNone)<BR> If Not IsEmpty(Pts) Then<BR> Debug.Print "多段线(" & ssetObj(i).Handle & ")与直线(" & lineObj.Handle & ")相交"<BR> For j = 0 To UBound(Pts) Step 3<BR> Debug.Print "交点:" & Pts(j) & "," & Pts(j + 1) & "," & Pts(j + 2)<BR> Next<BR> End If<BR> Next<BR>End Sub Set lineObj = ThisDrawing.ModelSpace(0)
问题出在这一句,它是返回模型空间的第一个实体,如果多段线先画,当然就是返回多段线,而不是直线了,后面自然也是错误的。
On Error Resume Next避免了错误的提示,在调试的时候尽量少用,而等最终代码无误了再加上。 知道了,谢谢efan2000大侠
送鲜花一朵 “On Error Resume Next避免了错误的提示,在调试的时候尽量少用,而等最终代码无误了再加上。”
在这里学到的不仅仅是技术,还有良好的编程习惯,送鲜花一朵
页:
[1]