明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1993|回复: 15

关于VBA intersectwith函数如何判断是否有交点的问题

[复制链接]
发表于 2019-11-22 12:14:04 | 显示全部楼层 |阅读模式
本帖最后由 heaven陌上花开 于 2019-11-22 12:20 编辑

Signature
VBA:
RetVal = object.IntersectWith(IntersectObject, ExtendOption)object
Type: All drawing objects (except PViewport and PolygonMesh), AttributeReference
The objects this method applies to.
IntersectObject
Access: Input-only
Type: Object
The object can be one of the supported drawing objects or an AttributeReference.
ExtendOption
Access: Input-only
Type: AcExtendOption enum
This option specifies if none, one or both, of the objects are to be extended in order to attempt an intersection.
  • acExtendNone: Does not extend either object.
  • acExtendThisEntity: Extends the base object.
  • acExtendOtherEntity: Extends the object passed as an argument.
  • acExtendBoth: Extends both objects.

Return Value (RetVal)
Type: Variant (array of doubles)
The array of points where one object intersects another object in the drawing.
==============================
以上是官方帮助文件给的内容,没有交点即无返回值。帮助文件还给了一个例子






 楼主| 发表于 2019-11-22 12:24:58 | 显示全部楼层
heaven陌上花开 发表于 2019-11-22 12:22
这段是官方给的代码,楼主亲测是存在bug的,不管是否有交点,vartype的返回值都为8179,而不是vbempty, ...

  1. Private Sub DrawAltitude(ByRef Altitude() As Double)  'VB输出数组要通过ByRef 引用才行,不能直接输出
  2.     ConnectAutoCAD
  3.     '获取交点并写入数组中
  4.     Dim CrossPoint As Variant
  5.     Dim pickedobjs1 As AcadEntity
  6.     Dim pickedobjs2 As AcadEntity
  7.     Dim nLWS, nLS As Integer
  8.     nLWS = CorrLineObj.Count: nLS = GuideLinesObj.Count  '选中的对象个数
  9.     Dim cpnts() As Double        '交点数组
  10. '    Dim Altitude() As Double     '高程数组
  11.     ReDim cpnts(0 To nLWS - 1, 0 To nLS - 1, 2) As Double '定义一个三维动态数组用于存放交点坐标
  12.     ReDim Altitude(0 To nLWS - 1, 0 To nLS - 1, 2) As Double
  13.     Dim i As Integer
  14.     Dim j As Integer
  15.     Dim k As Integer
  16.     Dim textobj As AcadText
  17.     i = 0: j = 0: k = 0
  18.     For Each pickedobjs1 In CorrLineObj
  19.         Thisdrawing.Utility.Prompt vbCrLf & (i + 1) & "/" & CorrLineObj.Count
  20.         pickedobjs1.Highlight (True) '高亮选中的实体
  21.         pickedobjs1.Update
  22.         j = 0
  23.         For Each pickedobjs2 In GuideLinesObj
  24.             pickedobjs1.Highlight (True)  '高亮选中的实体
  25.             pickedobjs1.Update

  26.             CrossPoint = pickedobjs1.IntersectWith(pickedobjs2, acExtendNone) '获取交点
  27.             
  28.             If VarType(CrossPoint) <> vbEmpty Then '执行计算
  29.                 Thisdrawing.Utility.Prompt vbCrLf & CrossPoint(1)
  30.                 Thisdrawing.Utility.Prompt vbCrLf & CrossPoint(0) & "," & CrossPoint(1) & "," & CrossPoint(2)
  31.                 cpnts(i, j, 0) = CrossPoint(0)
  32.                 cpnts(i, j, 1) = CrossPoint(1)
  33.                 cpnts(i, j, 2) = CrossPoint(2) '交点数组
  34.                 Altitude(i, j, 0) = CrossPoint(0)
  35.                 Altitude(i, j, 1) = CrossPoint(1) - CDbl(HeightBaseP(1)) + HeightBaseVal
  36.                 Altitude(i, j, 2) = CrossPoint(2) '高程数组
  37.                 '写入高程信息
  38.                 textInBasePoint(0) = CrossPoint(0)
  39.                 textInBasePoint(2) = CrossPoint(2)
  40.                 Set textobj = Thisdrawing.ModelSpace.AddText(CStr(Format(Altitude(i, j, 1), "0.00")), textInBasePoint, 1.5)
  41.                 textobj.Rotate textInBasePoint, pi / 2  '旋转90°布置
  42.             End If
  43.                  j = j + 1
  44.         Next pickedobjs2
  45.        i = i + 1
  46.     Next pickedobjs1
  47.     Thisdrawing.Utility.Prompt vbCrLf & "任务已完成!"
  48. End Sub
 楼主| 发表于 2019-11-22 12:20:47 | 显示全部楼层
  1. Sub Example_IntersectWith()
  2.     ' This example creates a line and circle and finds the points at
  3.     ' which they intersect.
  4.    
  5.     ' Create the line
  6.     Dim lineObj As AcadLine
  7.     Dim startPt(0 To 2) As Double
  8.     Dim endPt(0 To 2) As Double
  9.     startPt(0) = 1: startPt(1) = 1: startPt(2) = 0
  10.     endPt(0) = 5: endPt(1) = 5: endPt(2) = 0
  11.     Set lineObj = ThisDrawing.ModelSpace.AddLine(startPt, endPt)
  12.         
  13.     ' Create the circle
  14.     Dim circleObj As AcadCircle
  15.     Dim centerPt(0 To 2) As Double
  16.     Dim radius As Double
  17.     centerPt(0) = 3: centerPt(1) = 3: centerPt(2) = 0
  18.     radius = 1
  19.     Set circleObj = ThisDrawing.ModelSpace.AddCircle(centerPt, radius)
  20.     ZoomAll
  21.       
  22.     ' Find the intersection points between the line and the circle
  23.     Dim intPoints As Variant
  24.     intPoints = lineObj.IntersectWith(circleObj, acExtendNone)
  25.    
  26.     ' Print all the intersection points
  27.     Dim I As Integer, j As Integer, k As Integer
  28.     Dim str As String
  29.     If VarType(intPoints) <> vbEmpty Then
  30.         For I = LBound(intPoints) To UBound(intPoints)
  31.             str = "Intersection Point[" & k & "] is: " & intPoints(j) & "," & intPoints(j + 1) & "," & intPoints(j + 2)
  32.             MsgBox str, , "IntersectWith Example"
  33.             str = ""
  34.             I = I + 2
  35.             j = j + 3
  36.             k = k + 1
  37.         Next
  38.     End If
  39. End Sub
 楼主| 发表于 2019-11-22 14:35:16 | 显示全部楼层
heaven陌上花开 发表于 2019-11-22 14:31
调试了一上午,问题终于得到解决,用Ubound()-Lbound()判断交点元素的个数即可,有交点时个数是2,无交 ...
  1.   IntPoint = pickedobjs1.IntersectWith(pickedobjs2, acExtendNone) '获取交点
  2.   sizeIP = UBound(IntPoint) - LBound(IntPoint) '计算交点中元素的个数,有交点时是2,无交点时-1
  3.   If CDbl(sizeIP) = 2 Then '执行计算
  4.                         Thisdrawing.Utility.Prompt vbCrLf & "有交点!"

  5.   Else
  6.       Thisdrawing.Utility.Prompt vbCrLf & "无交点!"
  7.   End If
复制代码
 楼主| 发表于 2019-11-22 12:22:51 | 显示全部楼层

这段是官方给的代码,楼主亲测是存在bug的,不管是否有交点,vartype的返回值都为8179,而不是vbempty,下面附上我自己写的代码,一直解决不了判断是否存在交点的问题,还请老铁们指点一下迷津!
 楼主| 发表于 2019-11-22 12:29:37 | 显示全部楼层
命令:
1/1
8197
1642.23981521255,1368.34718727462,0
8197
1622.23981521255,1369.00846134891,0
8197
1602.23981521255,1369.64624601929,0
8197
1582.23981521255,1370.2276116206,0
8197
1596.11481521253,1369.83037327556,0
8197
1616.11481521252,1369.20936376302,0
8197
1636.11481521252,1368.54970245987,0
任务已完成!*取消*
 楼主| 发表于 2019-11-22 12:29:55 | 显示全部楼层

这个是有交点时的响应
 楼主| 发表于 2019-11-22 12:32:11 | 显示全部楼层
heaven陌上花开 发表于 2019-11-22 12:29
这个是有交点时的响应

命令:
1/1
8197
*无效*


这个是无交点时的响应,验证了不管有无交点,intersectwith函数返回值均为8197
 楼主| 发表于 2019-11-22 14:31:31 | 显示全部楼层
调试了一上午,问题终于得到解决,用Ubound()-Lbound()判断交点元素的个数即可,有交点时个数是2,无交点时算出来的值为-1,如果有更好的方法欢迎提出.
 楼主| 发表于 2019-11-22 14:36:11 | 显示全部楼层

方法比较笨,但搜索了大半天都没看到有人提出解决方案,这个方法目前是可行的,欢迎大家提出更好的解决方案
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 05:50 , Processed in 0.183553 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表