找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 603|回复: 0

[求助] 判断一点处直线相交形状,VBA有没有比较快的方法

[复制链接]
发表于 2017-11-1 13:29:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 zhn158 于 2017-11-1 13:50 编辑

用VBA判断某一点处直线的形状,比如以下四种形状,见图。我的思路和代码如下,但运行的实在太慢,请教各位大神,有没有好的办法,多谢

某一点处直线相交的形状

某一点处直线相交的形状


我的思路是从该点附近的四个方向,分别做栏选,判断每次是不是有直线选中,然后综合栏选结果,判断直线形状:


思路

思路



代码如下:
'输入AcadDocument对象(CadDOc),点(pnt),栏选偏移量(offset_value),形状代号(k:1,2,3,4),输出在该点处的直线是否具有形状代号代表的形状
Function criss_cross(cadDoc As AcadDocument, pnt As Variant, offset_value As Double, k As Integer) As Boolean
    Dim x As Double
    Dim y As Double

    Dim up_line As Boolean
    Dim down_line As Boolean
    Dim left_line As Boolean
    Dim right_line As Boolean

    up_line = False
    down_line = False
    left_line = False
    right_line = False

    x = pnt(0)
    y = pnt(1)
    x_left = x - offset_value
    x_right = x + offset_value
    y_up = y + offset_value
    y_down = y - offset_value

    Dim sel_line As AcadSelectionSet
    On Error Resume Next
    If Not IsNull(cadDoc.SelectionSets.Item("line1")) Then
        Set xx_sel = cadDoc.SelectionSets.Item("line1")
        xx_sel.Delete
    End If
    On Error GoTo 0
    Set sel_line = cadDoc.SelectionSets.Add("line1")
    BuildFilter pType1, pData1, 0, "LINE,LWPOLYLINE"

    Dim up_pt_list(0 To 5) As Double  
    up_pt_list(0) = x_left: up_pt_list(1) = y_up: up_pt_list(2) = 0#
    up_pt_list(3) = x_right: up_pt_list(4) = y_up: up_pt_list(5) = 0#

    Dim down_pt_list(0 To 5) As Double
    down_pt_list(0) = x_left: down_pt_list(1) = y_down: down_pt_list(2) = 0#
    down_pt_list(3) = x_right: down_pt_list(4) = y_down: down_pt_list(5) = 0#

    Dim left_pt_list(0 To 5) As Double
    left_pt_list(0) = x_left: left_pt_list(1) = y_up: left_pt_list(2) = 0#
    left_pt_list(3) = x_left: left_pt_list(4) = y_down: left_pt_list(5) = 0#

    Dim right_pt_list(0 To 5) As Double
    right_pt_list(0) = x_right: right_pt_list(1) = y_up: right_pt_list(2) = 0#
    right_pt_list(3) = x_right: right_pt_list(4) = y_down: right_pt_list(5) = 0#




    sel_line.SelectByPolygon acSelectionSetFence, up_pt_list, pType1, pData1
    If sel_line.Count > 0 Then up_line = True
    sel_line.Clear

    sel_line.SelectByPolygon acSelectionSetFence, down_pt_list, pType1, pData1
    If sel_line.Count > 0 Then down_line = True
    sel_line.Clear

    sel_line.SelectByPolygon acSelectionSetFence, left_pt_list, pType1, pData1
    If sel_line.Count > 0 Then left_line = True
    sel_line.Clear

    sel_line.SelectByPolygon acSelectionSetFence, right_pt_list, pType1, pData1
    If sel_line.Count > 0 Then right_line = True
    sel_line.Clear

    Dim dic_point_type As New Dictionary
    dic_point_type(1) = False
    dic_point_type(2) = False
    dic_point_type(3) = False
    dic_point_type(4) = False
    If up_line And left_line Then dic_point_type(1) = True
    If up_line And right_line Then dic_point_type(2) = True
    If down_line And right_line Then dic_point_type(3) = True
    If down_line And left_line Then dic_point_type(4) = True
    criss_cross = dic_point_type(k)
End Function
上面这段代码对下图的每个交点处理一遍,耗时4分钟左右:
表格.png





论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-12-23 00:28 , Processed in 0.370767 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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