乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 138|回复: 8

找到可能的路线并计算2个街区之间的路线长度

[复制链接]

1

主题

5

帖子

1

银币

初来乍到

Rank: 1

铜币
9
发表于 2012-8-15 08:25:19 | 显示全部楼层 |阅读模式
大家好,
我花了几天时间从网上找,但没有答案。我希望一些朋友愿意帮助我。
我想为我的工作编写VBA应用程序。该工具将取出许多穿过电缆槽的电缆。

例如:有一个托盘系统,许多电缆在块之间延伸。那么这个工具怎么能找到可能的路线并“跑”过它呢。


可以给我一些代码来计算从块A到块1的长度...
我还会发送样图。非常感谢,

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

16

主题

506

帖子

6

银币

中流砥柱

Rank: 25

铜币
570
发表于 2012-8-15 10:41:01 | 显示全部楼层
听起来像是旅行推销员问题-算法开发的经典课堂作业之一。
回复

使用道具 举报

1

主题

5

帖子

1

银币

初来乍到

Rank: 1

铜币
9
发表于 2012-8-15 10:53:59 | 显示全部楼层
谢谢你家伙
但是现在我不知道如何“从A到1的路”,而它们由许多线连接,有许多交叉点。找到一些方法后,我们可以通过线计算长度
所以你能给我一些示例代码吗
回复

使用道具 举报

1

主题

5

帖子

1

银币

初来乍到

Rank: 1

铜币
9
发表于 2012-8-15 11:32:05 | 显示全部楼层
考虑使用Dijkstra的算法,其中每条路径的“权重”是线段的长度。
回复

使用道具 举报

1

主题

5

帖子

1

银币

初来乍到

Rank: 1

铜币
9
发表于 2012-8-15 11:41:32 | 显示全部楼层
非常感谢你,李。我会尽力的
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2012-8-16 04:11:16 | 显示全部楼层
我找到了CAB先生的代码,有人能帮我“翻译”到VBA
http://www.theswamp.org/index.php?topic=1749.msg22949#msg22949
  1. ;;  ***************************************************************
  2. ;;   pline path finder.lsp
  3. ;;   Charles Alan Butler 07/08/2004
  4. ;;   Modified routine to find a path from picked start entity
  5. ;;   to picked end entity.
  6. ;;
  7. ;;   Returns the first path if it exist else nil, not the shortest path
  8. ;;   Selects & highlites the path also
  9. ;;  ***************************************************************
  10. ;shortcut
  11. (defun c:plp () (c:PlinePath))
  12. ;;;  ***************************************************************
  13. ;;;               Original Routine                                 
  14. ;;;
  15. ;;;  ;; based on Inline.lsp by John Uhden
  16. ;;;  ;; modified Joe Burke 5/15/03
  17. ;;;  ;; pick a line, arc or lwpline
  18. ;;;  ;; creates a selection set of objects which meet end to end
  19. ;;;  ;; only selects objects on the same layer as picked object
  20. ;;;  ;; pass selection set to pedit join...
  21. ;;;
  22. ;;;  ***************************************************************
  23. ;;===================================
  24. ;;      -==-      
  25. ;;===================================
  26. ;;  Return (ename Startpt Endpt)
  27. (defun @arc (ent / e rp r ba ea p1 p2)
  28.   (setq e  (cdr (assoc -1 ent))
  29.         rp (cdr (assoc 10 ent))
  30.         r  (cdr (assoc 40 ent))
  31.         ba (cdr (assoc 50 ent))
  32.         ea (cdr (assoc 51 ent))
  33.         p1 (trans (polar rp ba r) e 0)
  34.         p2 (trans (polar rp ea r) e 0)
  35.   )
  36.   (list e p1 p2)
  37. ) ;end
  38. ;;  Return (ename Startpt Endpt)
  39. (defun @line (ent)
  40.   (list
  41.     (cdr (assoc -1 ent))
  42.     (cdr (assoc 10 ent))
  43.     (cdr (assoc 11 ent))
  44.   )
  45. ) ;end
  46. ;;  Return (ename Startpt Endpt)
  47. (defun @pline (ent / e)
  48.   (setq e (cdr (assoc -1 ent)))
  49.   (list
  50.     e
  51.     (car (getends e))
  52.     (cadr (getends e))
  53.   )
  54. ) ;end
  55. ;;  Add ent-> (ename Startpt Endpt) to list
  56. (defun @list (e / ent)
  57.   (setq ent (entget e))
  58.   (cond
  59.     ((= (cdr (assoc 0 ent)) "LINE")
  60.      (setq sslist (cons (@line ent) sslist))
  61.     )
  62.     ((= (cdr (assoc 0 ent)) "ARC")
  63.      (setq sslist (cons (@arc ent) sslist))
  64.     )
  65.     ((= (cdr (assoc 0 ent)) "LWPOLYLINE")
  66.      (setq sslist (cons (@pline ent) sslist))
  67.     )
  68.   )
  69. ) ;end
  70. ;;argument: an ename - returns: Start and End points as a list
  71. (defun getends (vobj / name stpt endpt)
  72.   (if (= (type vobj) 'ename)
  73.     (setq vobj (vlax-ename->vla-object vobj))
  74.   )
  75.   (and
  76.     (setq name (vla-get-objectname vobj))
  77.     (cond
  78.       ((vl-position
  79.          name
  80.          '("AcDbArc"           "AcDbLine"          "AcDbEllipse"
  81.            "AcDbSpline"        "AcDbPolyline"      "AcDb2dPolyline"
  82.            "AcDb3dPolyline"
  83.           )
  84.        )
  85.        (setq stpt (vlax-curve-getstartpoint vobj))
  86.        (setq endpt (vlax-curve-getendpoint vobj))
  87.       )
  88.     ) ;cond
  89.   ) ;and
  90.   (list stpt endpt)
  91. ) ;end
  92. ;; get list of (ename startpt endpt) for picked ent
  93. (defun get:elst(ent)
  94.   (cond
  95.     ((= (cdr (assoc 0 ent)) "ARC")
  96.      (setq ent (@arc ent))
  97.     )
  98.     ((= (cdr (assoc 0 ent)) "LINE")
  99.      (setq ent (@line ent))
  100.     )
  101.     ((= (cdr (assoc 0 ent)) "LWPOLYLINE")
  102.      (setq ent (@pline ent))
  103.     )
  104.   )
  105.   ent
  106. ); end defun
  107. ;; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  108. ;;          main function               
  109. ;; *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  110. (defun c:plinepath (/      sslist elist  ss     ssres  i      e      e2
  111.                     found  ent    ent2   ok     start  end    start2 end2
  112.                     fuzz   layer  ssex   typlst
  113.                    )
  114.   ;; Get the start object
  115.   (if ; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  116.     (and
  117.       (cadr (ssgetfirst)) ;objects are selected
  118.       ;;at least one arc, line or pline
  119.       (setq ssex (ssget "i" (list (cons 0 "LINE,ARC,LWPOLYLINE"))))
  120.     ) ;and
  121.      ;; ======  then  =============
  122.      (setq e (ssname ssex 0))
  123.      ;; ======  else  =============
  124.      (progn
  125.        (sssetfirst)
  126.        (setq typlst '("LINE" "ARC" "LWPOLYLINE"))
  127.        (while
  128.          (or
  129.            (not (setq e (car (entsel "\nSelect Starting line, pline or arc: "))))
  130.            (not (member (cdr (assoc 0 (entget e))) typlst))
  131.          )
  132.           (princ "\nMissed pick or wrong object type: ")
  133.        ) ;while
  134.      ) ;progn
  135.   ) ;if  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  136.   ;;  Get the End object added by CAB
  137.   (setq typlst '("LINE" "ARC" "LWPOLYLINE"))
  138.   (while
  139.     (or
  140.       (not (setq e2 (car (entsel "\nSelect Ending line, pline or arc: "))))
  141.       (not (member (cdr (assoc 0 (entget e2))) typlst))
  142.     )
  143.      (princ "\nMissed pick or wrong object type: ")
  144.   ) ;while
  145.   (and
  146.     (setq ok   1
  147.           fuzz 1e-8 ; 0.00000001
  148.     )
  149.     (setq ent (entget e)) ; first or picked ent
  150.     (setq ent2 (entget e2)) ; second picked ent, CAB
  151.     (setq layer (cdr (assoc 8 ent))) ; layer to match
  152.     (= layer (cdr (assoc 8 ent2))) ; layers match
  153.     (setq ent    (get:elst ent)
  154.           elist  '()
  155.           start  (cadr ent)
  156.           end    (caddr ent)
  157.           ent2   (get:elst ent2); CAB
  158.           start2 (cadr ent2)
  159.           end2   (caddr ent2)
  160.     )
  161.     (setq ss ; get all objects that matched picked
  162.            (ssget "X" (list '(0 . "LINE,ARC,LWPOLYLINE") (cons 8 layer)))
  163.     )
  164.     (ssdel e ss) ; remove picked start from selection set
  165.     ;;  make a list of all from ss  ((ename startpt endpt) ....)
  166.     (setq i 0)
  167.     (repeat (sslength ss)
  168.       (@list (ssname ss i))
  169.       (setq i (1+ i))
  170.     ) ;repeat
  171.     ;;  CAB revised from here down
  172.     ;;  find attached items, does not test all branches
  173.     (@ckpoint start ent sslist)
  174.     (if (not found)
  175.       (@ckpoint end ent sslist)
  176.     )
  177.   ) ;and
  178.   (if found
  179.     (progn
  180.       (setq elist (cons ent elist))
  181.       (setq ssres (ssadd))
  182.       (foreach x elist ; creat a selection set of the list
  183.         (ssadd (car x) ssres)
  184.       )
  185.       (prompt "\n*-* Done *-*\n")
  186.       (cadr(sssetfirst nil ssres)) ; display the selected items
  187.     ); progn
  188.     (prompt "\n*-* Path not found *-*")
  189.   )
  190. ) ;end
  191. ;; -------------------------------
  192. ;;  @ckPoint by CAB
  193. ;;  check the list for matching points
  194. ;;  p point to match
  195. ;;  elst (ename startpt endpt) of pt
  196. ;;  |List list pf remaining elst
  197. (defun @ckpoint( p elst |list / entx ex p1 p2 idx res)
  198.   (setq idx (length |List))
  199.   (while (and (not found) (>= (setq idx (1- idx)) 0))
  200.     (setq entx (nth idx |List)
  201.           ex  (car entx)
  202.           p1  (cadr entx)
  203.           p2  (caddr entx)
  204.      )
  205.     (cond ; test point match with fuzz factor
  206.       ((equal p start2 fuzz) ; text for target
  207.        (setq found 1)
  208.        (setq elist (cons ent2 elist))
  209.       )
  210.       ((equal p end2 fuzz) ; text for target
  211.        (setq found 1)
  212.        (setq elist (cons ent2 elist))
  213.       )
  214.       ((equal p p1 fuzz) ; test for next branch
  215.        (setq res (@ckpoint p2 entx (vl-remove entx |List)))
  216.        (if found ; we are backing out collecting the path
  217.         (setq elist (cons entx elist))
  218.        )
  219.       )
  220.       ((equal p p2 fuzz) ; test for next branch
  221.        (setq res (@ckpoint p1 entx (vl-remove entx |List)))
  222.        (if found; we are backing out collecting the path
  223.         (setq elist (cons entx elist))
  224.        )
  225.       )
  226.     )
  227.   ); while
  228.   T ; return to satisfy AND
  229. ); defun
  230. ;;========================
  231. ;;   End Of File         
  232. ;;========================

回复

使用道具 举报

1

主题

5

帖子

1

银币

初来乍到

Rank: 1

铜币
9
发表于 2012-8-20 07:11:01 | 显示全部楼层
试试这个,假设这只适用于直线段,
而不是在你的绘图上处理,只是我从我的旧代码
  1. Option Explicit
  2. ''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~''
  3. Public Sub blockPath()
  4. Dim oSset As AcadSelectionSet
  5. Dim pt As Variant
  6. Dim blk1 As AcadBlockReference
  7. Dim blk2 As AcadBlockReference
  8. Dim ent As AcadEntity, ent1 As AcadEntity, ent2 As AcadEntity
  9. Dim ft(0) As Integer
  10. Dim fd(0) As Variant
  11. ft(0) = 0: fd(0) = "lwpolyline"
  12. ThisDrawing.Utility.GetEntity ent1, pt, vbCrLf & "Select First Block:"
  13. If Not TypeOf ent1 Is AcadBlockReference Then
  14. Exit Sub
  15. End If
  16. Set blk1 = ent1
  17. Dim verts1() As Double
  18. verts1 = BoundingBoxTest(ent1)
  19. Dim PointsList1(0 To 11) As Double
  20. Dim cnt, i
  21. cnt = 0
  22. For i = 0 To UBound(verts1, 1)
  23. PointsList1(cnt) = verts1(i, 0)
  24. PointsList1(cnt + 1) = verts1(i, 1)
  25. PointsList1(cnt + 2) = verts1(i, 2)
  26. cnt = cnt + 3
  27. Next
  28. ThisDrawing.Utility.GetEntity ent2, pt, vbCrLf & "Select Second Block:"
  29. If Not TypeOf ent2 Is AcadBlockReference Then
  30. Exit Sub
  31. End If
  32. Set blk2 = ent2
  33. Dim verts2() As Double
  34. verts2 = BoundingBoxTest(ent2)
  35. Dim PointsList2(0 To 11) As Double
  36. cnt = 0
  37. For i = 0 To UBound(verts2, 1)
  38. PointsList2(cnt) = verts2(i, 0)
  39. PointsList2(cnt + 1) = verts2(i, 1)
  40. PointsList2(cnt + 2) = verts2(i, 2)
  41. cnt = cnt + 3
  42. Next
  43. ThisDrawing.Utility.GetEntity ent, pt, vbCrLf & "Select main polyline:"
  44. If Not TypeOf ent Is AcadLWPolyline Then
  45. Exit Sub
  46. End If
  47. Dim mpoly As AcadLWPolyline
  48. Set mpoly = ent
  49.      
  50.           With ThisDrawing.SelectionSets
  51.                While .Count > 0
  52.                     .Item(0).Delete
  53.                Wend
  54.           End With
  55.      With ThisDrawing.SelectionSets
  56.           Set oSset = .Add("$PolySet$")
  57.      End With
  58. Dim mode As Integer
  59. mode = acSelectionSetCrossingPolygon
  60. oSset.SelectByPolygon mode, PointsList1, ft, fd
  61. Dim pline1 As AcadLWPolyline
  62. Set ent = oSset.Item(0)
  63. Set pline1 = ent
  64.           With ThisDrawing.SelectionSets
  65.                While .Count > 0
  66.                     .Item(0).Delete
  67.                Wend
  68.           End With
  69.      With ThisDrawing.SelectionSets
  70.           Set oSset = .Add("$PolySet$")
  71.      End With
  72. oSset.SelectByPolygon mode, PointsList2, ft, fd
  73. Dim pline2 As AcadLWPolyline
  74. Set ent = oSset.Item(0)
  75. Set pline2 = ent
  76. Dim intpts1 As Variant
  77. Dim j
  78. intpts1 = pline1.IntersectWith(mpoly, acExtendNone)
  79. Dim inspt1(0 To 2) As Double
  80. If VarType(intpts1)  vbEmpty Then
  81.         For i = LBound(intpts1) To UBound(intpts1)
  82.             inspt1(0) = intpts1(j): inspt1(1) = intpts1(j + 1): inspt1(2) = intpts1(j + 2)
  83.             i = i + 2
  84.             j = j + 3
  85.         Next
  86.     End If
  87. Dim intpts2 As Variant
  88. intpts2 = pline2.IntersectWith(mpoly, acExtendNone)
  89. j = 0
  90. Dim inspt2(0 To 2) As Double
  91. If VarType(intpts2)  vbEmpty Then
  92.         For i = LBound(intpts2) To UBound(intpts2)
  93.             inspt2(0) = intpts2(j): inspt2(1) = intpts2(j + 1): inspt2(2) = intpts2(j + 2)
  94.             i = i + 2
  95.             j = j + 3
  96.         Next
  97.     End If
  98. Dim leg As Double
  99. leg = Distance(inspt1, inspt2)
  100. MsgBox "Common Length: " & vbCr & CStr(leg + pline1.Length + pline2.Length)
  101. End Sub
  102. Private Function BoundingBoxTest(oEnt As AcadEntity) As Double()
  103. Dim MaxPoint As Variant
  104. Dim MinPoint As Variant
  105. Dim Vertices(0 To 3, 0 To 2) As Double
  106. oEnt.GetBoundingBox MinPoint, MaxPoint
  107. Vertices(0, 0) = MinPoint(0)
  108. Vertices(0, 1) = MinPoint(1)
  109. Vertices(0, 2) = MinPoint(2)
  110. Vertices(1, 0) = MaxPoint(0)
  111. Vertices(1, 1) = MinPoint(1)
  112. Vertices(1, 2) = MinPoint(2)
  113. Vertices(2, 0) = MaxPoint(0)
  114. Vertices(2, 1) = MaxPoint(1)
  115. Vertices(2, 2) = MinPoint(2)
  116. Vertices(3, 0) = MinPoint(0)
  117. Vertices(3, 1) = MaxPoint(1)
  118. Vertices(3, 2) = MinPoint(2)
  119. BoundingBoxTest = Vertices
  120. End Function
  121. Private Function Distance(fPoint As Variant, sPoint As Variant) As Double
  122.     Dim x1 As Double, x2 As Double
  123.     Dim y1 As Double, y2 As Double
  124.     Dim z1 As Double, z2 As Double
  125.     Dim cDist As Double
  126.     x1 = sPoint(0): x2 = fPoint(0)
  127.     y1 = sPoint(1): y2 = fPoint(1)
  128.     z1 = sPoint(2): z2 = fPoint(2)
  129.     cDist = Sqr(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2) + ((z2 - z1) ^ 2))
  130.     Distance = cDist
  131. End Function
  132. ''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~''

~'J'~
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2012-8-23 01:35:53 | 显示全部楼层
谢谢,先生
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2012-8-29 16:06:59 | 显示全部楼层
不客气,
干杯
~'J'~
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-6-30 02:24 , Processed in 1.487547 second(s), 75 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表