明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2611|回复: 8

框选连接直线

[复制链接]
发表于 2010-12-13 19:59 | 显示全部楼层 |阅读模式
本帖最后由 lennie 于 2010-12-13 20:00 编辑

今天刚写的代码,发上来和大家共享一下。
  1. Public Sub LJ()
  2. Dim SsLine As AcadSelectionSet
  3. Dim FilterType(0) As Integer
  4. Dim FilterData(0) As Variant
  5. CertificationSelect "ST"
  6. Set SsLine = ThisDrawing.SelectionSets.Add("ST")
  7. FilterType(0) = 0
  8. FilterData(0) = "LINE"
  9. SsLine.SelectOnScreen FilterType, FilterData
  10. Do While LineJoin(SsLine)
  11. Loop
  12. Set SsLine = Nothing
  13. End Sub

  14. Public Function LineJoin(ByVal SS As AcadSelectionSet) As Boolean
  15. If SS.Count < 2 Then
  16. LineJoin = False
  17. Exit Function
  18. End If
  19. Dim SJ1 As Double
  20. Dim SJ2 As Double
  21. Dim L1sp As Variant
  22. Dim L1ep As Variant
  23. Dim L2sp As Variant
  24. Dim L2ep As Variant
  25. Dim P1(0 To 5) As Double
  26. Dim P2(0 To 5) As Double
  27. Dim i As Integer
  28. Dim j As Integer
  29. For i = 0 To SS.Count - 1
  30. For j = i + 1 To SS.Count - 1
  31. If SS(i).Layer = SS(j).Layer Then
  32. SJ1 = SjMj(SS(i).StartPoint, SS(i).EndPoint, SS(j).StartPoint)
  33. SJ2 = SjMj(SS(i).StartPoint, SS(i).EndPoint, SS(j).EndPoint)
  34. If SJ1 + SJ2 < 0.00000001 Then '可以调节计算误差
  35. Dim Points(0 To 7) As Double
  36. Dim LineObjs(0) As AcadEntity
  37. Dim DelObjs(1) As AcadEntity
  38. Dim StartPoint(0 To 2) As Double
  39. Dim EndPoint(0 To 2) As Double
  40. Dim n As Integer
  41. L1sp = SS(i).StartPoint
  42. L1ep = SS(i).EndPoint
  43. L2sp = SS(j).StartPoint
  44. L2ep = SS(j).EndPoint
  45. Points(0) = L1sp(0): Points(1) = L1sp(1)
  46. Points(2) = L1ep(0): Points(3) = L1ep(1)
  47. Points(4) = L2sp(0): Points(5) = L2sp(1)
  48. Points(6) = L2ep(0): Points(7) = L2ep(1)
  49. StartPoint(0) = Points(0)
  50. StartPoint(1) = Points(1)
  51. For n = 0 To 7 Step 2
  52. If Points(n) < StartPoint(0) Then
  53. StartPoint(0) = Points(n)
  54. StartPoint(1) = Points(n + 1)
  55. End If
  56. If Points(n) = StartPoint(0) And Points(n + 1) < StartPoint(1) Then
  57. StartPoint(1) = Points(n + 1)
  58. End If
  59. Next
  60. EndPoint(0) = Points(0)
  61. EndPoint(1) = Points(1)
  62. For n = 0 To 7 Step 2
  63. If Points(n) > EndPoint(0) Then
  64. EndPoint(0) = Points(n)
  65. EndPoint(1) = Points(n + 1)
  66. End If
  67. If Points(n) = EndPoint(0) And Points(n + 1) > EndPoint(1) Then
  68. EndPoint(1) = Points(n + 1)
  69. End If
  70. Next
  71. Set LineObjs(0) = ThisDrawing.ModelSpace.AddLine(StartPoint, EndPoint)
  72. LineObjs(0).Layer = SS(i).Layer
  73. SS.AddItems LineObjs
  74. Set DelObjs(0) = SS(i)
  75. Set DelObjs(1) = SS(j)
  76. SS.RemoveItems DelObjs
  77. SS.Update
  78. DelObjs(0).Delete
  79. DelObjs(1).Delete
  80. LineJoin = True
  81. Exit Function
  82. End If
  83. End If
  84. Next
  85. Next
  86. LineJoin = False
  87. End Function

评分

参与人数 1威望 +1 金钱 +12 收起 理由
雪山飞狐_lzh + 1 + 12

查看全部评分

 楼主| 发表于 2010-12-13 20:01 | 显示全部楼层
本帖最后由 lennie 于 2010-12-13 20:02 编辑

要用到下面两个函数
  1. Public Function SjMj(ByVal P1 As Variant, ByVal P2 As Variant, ByVal P3 As Variant) As Double '求三点的面积
  2. On Error GoTo Err_handle
  3. Dim a As Double
  4. Dim b As Double
  5. Dim c As Double
  6. Dim p As Double
  7. a = Sqr((P1(0) - P2(0)) ^ 2 + (P1(1) - P2(1)) ^ 2)
  8. b = Sqr((P1(0) - P3(0)) ^ 2 + (P1(1) - P3(1)) ^ 2)
  9. c = Sqr((P2(0) - P3(0)) ^ 2 + (P2(1) - P3(1)) ^ 2)
  10. p = (a + b + c) / 2
  11. SjMj = Sqr(p * (p - a) * (p - b) * (p - c))
  12. Exit Function
  13. Err_handle: 'VB的计算误差有时会导致(p - a) * (p - b) * (p - c)出现负数
  14. SjMj = 0
  15. End Function

  16. Public Sub CertificationSelect(ByVal SelectName As String) '存在选择集时删除选择集
  17. Dim Entry As AcadSelectionSet
  18. For Each Entry In ThisDrawing.SelectionSets
  19. If UCase(Entry.Name) = UCase(SelectName) Then
  20. ThisDrawing.SelectionSets.Item(SelectName).Delete
  21. Exit Sub
  22. End If
  23. Next
  24. End Sub

发表于 2010-12-13 22:10 | 显示全部楼层
有啥用处,楼主能否详细说明下
 楼主| 发表于 2010-12-14 13:49 | 显示全部楼层
功能简单的 里面有个精度调节的参数 对简化图形有用

本帖子中包含更多资源

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

x
发表于 2010-12-17 10:42 | 显示全部楼层
个人比较懒,楼主可不可以直接发个dvb上来?还有,楼主开发过对应的lsp版本吗
 楼主| 发表于 2010-12-17 11:37 | 显示全部楼层
对不起 我比你还要懒
发表于 2010-12-18 20:06 | 显示全部楼层
本帖最后由 chpmould 于 2010-12-18 20:06 编辑

如果程序能改为NET写那就好了...
发表于 2013-10-10 15:46 | 显示全部楼层
赞个先
发表于 2013-10-10 21:52 | 显示全部楼层
赞踩踩踩踩踩踩踩踩
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-28 22:27 , Processed in 1.063916 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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