[讨论]在Excel中申明为通用对象IntersectWith方法无法求出延伸交点
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align=center><B style="mso-bidi-font-weight: normal">为什么在<FONT face="Times New Roman">Excel</FONT></B><B style="mso-bidi-font-weight: normal">中申明为通用对象<FONT face="Times New Roman">IntersectWith</FONT></B><B style="mso-bidi-font-weight: normal">方法无法求出延伸交点<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></B></P><P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Sub </FONT>求多段线交点<FONT face="Times New Roman">()</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Dim acadapp As AcadApplication</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">'</FONT>上句申明<FONT face="Times New Roman">acadapp</FONT>为明确的<FONT face="Times New Roman">Acad</FONT>对象,须引用<FONT face="Times New Roman">"AutoCAD 2006 Type Library"</FONT>类型库。如修改为<FONT face="Times New Roman">Dim acadapp As Object</FONT>(优点:不需引用<FONT face="Times New Roman">CAD</FONT>类型库而将程序做成通用程序)其余一点不变,则本程序无法用<FONT face="Times New Roman">IntersectWith</FONT>求出延伸交点,不知为什么?各位高手和斑主能否帮助解释一下。</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Dim plineObj1 As Object '</FONT>轻便多段线<FONT face="Times New Roman">1<o:p></o:p></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Dim points1(0 To 13) As Double</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Dim plineObj2 As Object '</FONT>轻便多段线<FONT face="Times New Roman">2<o:p></o:p></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Dim points2(0 To 13) As Double</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">On Error Resume Next</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Set acadapp = GetObject(, "AUTOCAD.APPLICATION")</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">If Err Then</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Set acadapp = CreateObject("AUTOCAD.APPLICATION")</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">End If</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">acadapp.Visible = True</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points1(0) = -17.25: points1(1) = 2.43</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points1(2) = -12.75: points1(3) = 5.43</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points1(4) = -12: points1(5) = 5.46</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points1(6) = 0: points1(7) = 5.7</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points1(8) = 12: points1(9) = 5.46</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points1(10) = 12.75: points1(11) = 5.43</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points1(12) = 17.25: points1(13) = 2.43</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Set plineObj1 = acadapp.ActiveDocument.ModelSpace.AddLightWeightPolyline(points1)</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points2(0) = -18.3: points2(1) = 2.2</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points2(2) = -15.5: points2(3) = 2.3</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points2(4) = -8: points2(5) = 3.5</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points2(6) = 0: points2(7) = 4.6</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points2(8) = 8.2: points2(9) = 2.2</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points2(10) = 12.5: points2(11) = 1.8</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">points2(12) = 16.3: points2(13) = 3</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Set plineObj2 = acadapp.ActiveDocument.ModelSpace.AddLightWeightPolyline(points2)</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Dim intPoints As Variant </FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">intPoints = plineObj2.IntersectWith(plineObj1, acExtendBoth)</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">MsgBox UBound(intPoints) </FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">'</FONT>上句<FONT face="Times New Roman">Dim acadapp As AcadApplication</FONT>显示为<FONT face="Times New Roman">5</FONT>,如<FONT face="Times New Roman">Dim acadapp As Object</FONT>显示为<FONT face="Times New Roman">-1</FONT>即无交点,为什么?</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Dim I As Integer, j As Integer, k As Integer</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Dim str As String</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">If VarType(intPoints) <> vbEmpty Then</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">For I = LBound(intPoints) To UBound(intPoints)</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">str = "</FONT>交点<FONT face="Times New Roman">(" & k & ")</FONT>:<FONT face="Times New Roman">" & intPoints(j) & "," & intPoints(j + 1) & "," & intPoints(j + 2)</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">MsgBox str, , "IntersectWith Example"</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">str = ""</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">I = I + 2</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">j = j + 3 '+3</FONT>是因为一个点有<FONT face="Times New Roman">3</FONT>个坐标<FONT face="Times New Roman">x.y.z</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">k = k + 1 '</FONT>循环次数累加</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">Next</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">End If</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Times New Roman">End Sub</FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-line-height-alt: 0pt" align=left>结果为:交点(0):-17.5550943396226,2.22660377358491,0<o:p></o:p></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-line-height-alt: 0pt" align=left> 交点(1):16.3644642857143,3.02035714285714,0<o:p></o:p></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt">恳请各位高手和斑主帮助回答我的问题,万分感谢!</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><o:p><FONT face="Times New Roman"> </FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align=center>黄玉宏 <FONT face="Times New Roman">2006.5.29 </FONT>于姜堰梁徐</P>
页:
[1]