乐筑天下

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

[编程交流] 为Pol创建3条中心曲线

[复制链接]

95

主题

477

帖子

383

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
475
发表于 2022-7-5 16:59:04 | 显示全部楼层 |阅读模式
我正在尝试创建一个LISP,该LISP允许我在知道偏移距离的基础上轻松为多段线创建3条中心曲线(例如,一条带100'-55'-200'w/2'和8'偏移的3中心曲线)。我的问题是,我不知道如何通过LISP计算不同圆弧的切线值。附件是一个。dwg显示了我所说的内容。
3个中心曲线。图纸
回复

使用道具 举报

95

主题

477

帖子

383

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
475
发表于 2022-7-5 17:05:35 | 显示全部楼层
到目前为止,我对代码的了解是:
  1. (defun c:3centercurve (/ *error* prd ent1 ent2 rad1 rad2 rad3 off1 off2)
  2. (defun *error* (msg)
  3.    (if (not
  4.          (member msg '("Function cancelled" "quit / exit abort"))
  5.        )
  6.      (princ (strcat "\nError: " msg))
  7.    )
  8.    (princ)
  9. )
  10. (setq prd
  11.         '(lambda (x)
  12.            (wcmatch
  13.              (cdr (assoc 0 (entget x)))
  14.              "LWPOLYLINE"
  15.            )
  16.          )
  17. )
  18. (if (and (setq ent1 (selectif "\nSelect approach polyline: " prd))
  19.           (setq ent2 (selectif "\nSelect the intersecting pline: " prd))
  20.           (progn
  21.             (initget (+ 1 2 4))
  22.             (setq rad1 (getreal "\nSpecify the approach radius: "))
  23.           )
  24.           (progn
  25.             (initget (+ 1 2 4))
  26.             (setq rad2 (getreal "\nSpecify the center radius: "))
  27.           )
  28.           (progn
  29.             (initget (+ 1 2 4))
  30.             (setq rad3 (getreal "\nSpecify the ending radius: "))
  31.           )
  32.           (progn
  33.             (initget (+ 1 2 4))
  34.             (setq off1 (getreal "\nSpecify the approach offset: "))
  35.           )
  36.           (progn
  37.             (initget (+ 1 2 4))
  38.             (setq off2 (getreal "\nSpecify the tie-in offset: "))
  39.           )
  40.      )
  41.    (progn
  42. ;;;This is where I am getting stuck
  43.    )
  44. )
  45. (princ)
  46. )
  47. ;;;Select if written by Lee Mac
  48. (defun selectif (msg prd / ent)
  49. (while
  50.    (progn (setq ent (car (entsel msg)))
  51.           (cond
  52.             ((= 7 (getvar 'errno))
  53.              (princ "\nMissed, try again.")
  54.             )
  55.             ((not ent) nil)
  56.             ((not (apply prd (list ent)))
  57.              (princ "\nInvalid object selected.")
  58.             )
  59.           )
  60.    )
  61. )
  62. ent
  63. )
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 17:08:43 | 显示全部楼层
没有解决方案对不起,只需更正您的内容(为了可读性):
  1. (defun c:test ( / *error* _GRP selfoo ent1 ent2 rad1 rad2 rad3 off1 off2 )
  2. (defun *error* (msg)
  3.    (and msg
  4.                 (not (member msg '("Function cancelled" "quit / exit abort")))
  5.                 (princ (strcat "\nError: " msg))
  6.         )
  7.    (princ)
  8. )
  9. (defun _GRP ( msg / rtn ) ; "Get Real Positive"
  10.         (and (not (initget (+ 1 2 4))) (setq rtn (getreal (strcat "\n" msg))) )
  11.         rtn
  12. )
  13. (setq selfoo (lambda ( x ) (eq "LWPOLYLINE" (cdr (assoc 0 (entget (car x)))))))
  14. (if
  15.         (and
  16.                 (setq ent1 (car (LM:SelectIf "\nSelect approach polyline: " selfoo entsel nil)))
  17.                 (setq ent2 (car (LM:SelectIf "\nSelect the intersecting pline: " selfoo entsel nil)))
  18.                 (setq rad1 (_GRP "Specify the approach radius: "))
  19.                 (setq rad2 (_GRP "Specify the center radius: "))
  20.                 (setq rad3 (_GRP "Specify the ending radius: "))
  21.                 (setq off1 (_GRP "Specify the approach offset: "))
  22.                 (setq off2 (_GRP "Specify the tie-in offset: "))
  23.         ); and
  24.         (progn
  25.                 ;;;This is where I am getting stuck
  26.         )
  27. )
  28. (princ)
  29. )
  30. ;;---------------------=={ Select if }==----------------------;;
  31. ;;                                                            ;;
  32. ;;  Provides continuous selection prompts until either a      ;;
  33. ;;  predicate function is validated or a keyword is supplied. ;;
  34. ;;------------------------------------------------------------;;
  35. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  36. ;;------------------------------------------------------------;;
  37. ;;  Arguments:                                                ;;
  38. ;;  msg  - prompt string                                      ;;
  39. ;;  pred - optional predicate function [selection list arg]   ;;
  40. ;;  func - selection function to invoke                       ;;
  41. ;;  keyw - optional initget argument list                     ;;
  42. ;;------------------------------------------------------------;;
  43. ;;  Returns:  Entity selection list, keyword, or nil          ;;
  44. ;;------------------------------------------------------------;;
  45. (defun LM:SelectIf ( msg pred func keyw / sel ) (setq pred (eval pred))  
  46. (while
  47.         (progn (setvar 'ERRNO 0) (if keyw (apply 'initget keyw)) (setq sel (func msg))
  48.                 (cond
  49.                         ( (= 7 (getvar 'ERRNO))
  50.                                 (princ "\nMissed, Try again.")
  51.                         )
  52.                         ( (eq 'STR (type sel))
  53.                                 nil
  54.                         )
  55.                         ( (vl-consp sel)
  56.                                 (if (and pred (not (pred sel)))
  57.                                         (princ "\nInvalid Object Selected.")
  58.                                 )
  59.                         )
  60.                 )
  61.         )
  62. )
  63. sel
  64. )
回复

使用道具 举报

95

主题

477

帖子

383

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
475
发表于 2022-7-5 17:12:18 | 显示全部楼层
谢谢Grr!我没有想过创建一个单独的函数,这比反复使用progn要干净得多。
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 17:14:52 | 显示全部楼层
顺便说一句,您使用的(selectif)函数有问题,因为我无法选择LWPOLYLINE(并且假设因为errno变量没有重置为0)。
无论如何,请使用LM:Selectif并保留标题,这样您就不会违反LM的使用条款。
此外,您可能会发现比格尔的示例对于从曲线中查找切线很有用,因为这是您任务的一部分。
很抱歉,我还不习惯数学Lisp程序,所以我不知道我能帮你进一步吗
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 17:21:12 | 显示全部楼层
这看起来是一个有趣的几何挑战!
 
我已经有一段时间没有推我的几何了,因此我尝试了一个解决方案(非常有限的测试!):
  1. (defun c:3cc ( / acn aof ard cen crd ecn eof erd int per sg1 sg2 vc1 vc2 )
  2.    (while
  3.        (and
  4.            (setq sg1 (getsegment "\nSelect approach line <exit>: "))
  5.            (setq sg2 (getsegment "\nSelect intersecting line <exit>: "))
  6.            (not (setq int (apply 'inters (append sg1 sg2 '(())))))
  7.        )
  8.        (princ "\nLines do not intersect.")
  9.    )
  10.    (cond
  11.        (   (not
  12.                (and sg1 sg2
  13.                    (vl-every '(lambda ( sym msg ) (initget 6) (set sym (getdist msg)))
  14.                       '(ard crd erd aof eof)
  15.                       '(   "\nSpecify approach radius: "
  16.                            "\nSpecify center radius: "
  17.                            "\nSpecify end radius: "
  18.                            "\nSpecify approach offset: "
  19.                            "\nSpecify tie-in offset: "
  20.                        )
  21.                    )
  22.                )
  23.            )
  24.        )
  25.        (   (<= ard crd)
  26.            (princ "\nApproach radius must be greater than center radius.")
  27.        )
  28.        (   (<= erd crd)
  29.            (princ "\nEnd radius must be greater than center radius.")
  30.        )
  31.        (   t
  32.            (if (< (distance int (car sg1)) (distance int (cadr sg1)))
  33.                (setq sg1 (reverse sg1))
  34.            )
  35.            (if (< (distance int (cadr sg2)) (distance int (car sg2)))
  36.                (setq sg2 (reverse sg2))
  37.            )
  38.            (setq per'((x) (vx1 (list (- (cadr x)) (car x))))
  39.                  vc1 (per (apply 'mapcar (cons '- sg1)))
  40.                  vc2 (per (apply 'mapcar (cons '- sg2)))
  41.                  cen
  42.                (apply 'inters
  43.                    (append
  44.                        (apply 'append
  45.                            (mapcar
  46.                               '(lambda ( x v ) (mapcar '(lambda ( y ) (mapcar '+ y v)) x))
  47.                                (list sg1 sg2)
  48.                                (mapcar 'vxs (list vc1 vc2) (list (+ crd aof) (+ crd eof)))
  49.                            )
  50.                        )
  51.                       '( ( ) )
  52.                    )
  53.                )
  54.            )
  55.            (setq acn
  56.                (last
  57.                    (apply 'LM:inters-line-circle
  58.                        (append
  59.                            (mapcar '(lambda ( x ) (mapcar '+ x (vxs vc1 ard))) sg1)
  60.                            (list cen (- ard crd))
  61.                        )
  62.                    )
  63.                )
  64.            )
  65.            (setq ecn
  66.                (car
  67.                    (apply 'LM:inters-line-circle
  68.                        (append
  69.                            (mapcar '(lambda ( x ) (mapcar '+ x (vxs vc2 erd))) sg2)
  70.                            (list cen (- erd crd))
  71.                        )
  72.                    )
  73.                )
  74.            )
  75.            (arc acn ard (angle acn cen) (angle '(0 0) (mapcar '- vc1)))
  76.            (arc ecn erd (angle '(0 0) (mapcar '- vc2)) (angle ecn cen))
  77.            (arc cen crd (angle ecn cen) (angle acn cen))
  78.        )
  79.    )
  80.    (princ)
  81. )
  82. (defun arc ( cen rad sta ena )
  83.    (entmake
  84.        (list
  85.           '(000 . "ARC")
  86.            (cons 010 cen)
  87.            (cons 040 rad)
  88.            (cons 050 sta)
  89.            (cons 051 ena)
  90.        )
  91.    )
  92. )
  93. (defun getsegment ( msg / ent enx par rtn sel typ )
  94.    (while
  95.        (progn (setvar 'errno 0) (setq sel (entsel msg))
  96.            (cond
  97.                (   (= 7 (getvar 'errno))
  98.                    (princ "\nMissed, try again.")
  99.                )
  100.                (   (null sel) nil)
  101.                (   (= "LINE"
  102.                        (setq ent (car sel)
  103.                              enx (entget ent)
  104.                              typ (cdr (assoc 0 enx))
  105.                        )
  106.                    )
  107.                    (setq rtn
  108.                        (list
  109.                            (trans (cdr (assoc 10 enx)) 0 1)
  110.                            (trans (cdr (assoc 11 enx)) 0 1)
  111.                        )
  112.                    )
  113.                    nil
  114.                )
  115.                (   (= "LWPOLYLINE" typ)
  116.                    (setq par (vlax-curve-getparamatpoint ent (vlax-curve-getclosestpointto ent (trans (cadr sel) 1 0)))
  117.                          rtn
  118.                        (list
  119.                            (trans (vlax-curve-getpointatparam ent     (fix par))  0 1)
  120.                            (trans (vlax-curve-getpointatparam ent (1+ (fix par))) 0 1)
  121.                        )
  122.                    )
  123.                    nil
  124.                )
  125.                (   (princ "\nPlease select a line or 2D polyline."))
  126.            )
  127.        )
  128.    )
  129.    rtn
  130. )
  131. ;; Line-Circle Intersection (vector version)  -  Lee Mac
  132. ;; Returns the point(s) of intersection between an infinite line defined by
  133. ;; points p,q and circle with centre c and radius r
  134. (defun LM:inters-line-circle ( p q c r / v s )
  135.    (setq v (mapcar '- q p)
  136.          s (mapcar '- p c)
  137.    )
  138.    (mapcar '(lambda ( s ) (mapcar '+ p (vxs v s)))
  139.        (quad (vxv v v) (* 2 (vxv v s)) (- (vxv s s) (* r r)))
  140.    )
  141. )
  142. ;; Quadratic Solution  -  Lee Mac
  143. ;; Args: a,b,c - coefficients of ax^2 + bx + c = 0
  144. (defun quad ( a b c / d r )
  145.    (cond
  146.        (   (equal 0.0 (setq d (- (* b b) (* 4.0 a c))) 1e-8)
  147.            (list (/ b (* -2.0 a)))
  148.        )
  149.        (   (< 0 d)
  150.            (setq r (sqrt d))
  151.            (list (/ (- r b) (* 2.0 a)) (/ (- (- b) r) (* 2.0 a)))
  152.        )
  153.    )
  154. )
  155. ;; Vector Dot Product  -  Lee Mac
  156. ;; Args: u,v - vectors in R^n
  157. (defun vxv ( u v )
  158.    (apply '+ (mapcar '* u v))
  159. )
  160. ;; Vector x Scalar  -  Lee Mac
  161. ;; Args: v - vector in R^n, s - real scalar
  162. (defun vxs ( v s )
  163.    (mapcar '(lambda ( n ) (* n s)) v)
  164. )
  165. ;; Unit Vector  -  Lee Mac
  166. ;; Args: v - vector in R^2 or R^3
  167. (defun vx1 ( v )
  168.    (   (lambda ( n ) (if (equal 0.0 n 1e-10) nil (mapcar '/ v (list n n n))))
  169.        (distance '(0.0 0.0 0.0) v)
  170.    )
  171. )
  172. (vl-load-com) (princ)

 
这一争吵让我感到沮丧:
  1. ([color=BLUE]vl-every[/color] '([color=BLUE]lambda[/color] ( sym msg ) ([color=BLUE]initget[/color] 6) ([color=BLUE]set[/color] sym ([color=BLUE]getdist[/color] msg)))
  2. '(ard crd erd aof eof)
  3. '(   [color=MAROON]"\nSpecify approach radius: "[/color]
  4.         [color=MAROON]"\nSpecify center radius: "[/color]
  5.         [color=MAROON]"\nSpecify end radius: "[/color]
  6.         [color=MAROON]"\nSpecify approach offset: "[/color]
  7.         [color=MAROON]"\nSpecify tie-in offset: "[/color]
  8. )
  9. )

到底发生了什么,是用嵌套列表(())追加的吗?
 
虽然我试过了,但仍然不知道这个例程做了什么,也许一些动画会很好。
 
我认为CADtutor应该包括一个“添加到收藏夹”按钮,用于查看很棒的帖子。
回复

使用道具 举报

5

主题

1334

帖子

1410

银币

限制会员

铜币
-20
发表于 2022-7-5 17:24:34 | 显示全部楼层
 
哎呀!固定的
 
 
谢谢
 
 
这可能有助于了解:
 
sg1=()
sg2=()
(())=(无)
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 17:25:24 | 显示全部楼层
李,非常感谢!我刚刚开始测试代码,还没有发现任何大的bug!随着我深入研究代码,我对您的提示方法很好奇。与使用我在原始代码中显示的方式相比,使用vl方式有明显的优势吗?
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 17:32:09 | 显示全部楼层
我已经开始对代码进行更多的测试,我发现一些奇怪的行为,至少有点随机。有时,当我选择多段线时,曲线最终不会相切,几乎会“翻转”。但是代码没有出错,所以我不知道是什么导致了问题。我附上了一个。dwg来展示我在说什么。
3CT测试。图纸
回复

使用道具 举报

95

主题

477

帖子

383

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
475
发表于 2022-7-5 17:33:52 | 显示全部楼层
 
没有优势,这是同一件事,用不同的方式写。
目标是:摆脱束缚,以同样的方式为他人编写代码,但每次都做不到。
当然,他使用getdist而不是getreal,我认为这是因为用户需要获得“视觉比例”。
 
但在我看来,如果不是本地化5个变量,而是本地化1个assoc列表,其中包含5个关联,那么这种方法会更有用(至少我有这个想法)。
编辑:
这样地:
  1. ([color=BLUE]not[/color] ([color=BLUE]setq[/color] int ([color=BLUE]apply[/color] '[color=BLUE]inters[/color] ([color=BLUE]append[/color] sg1 sg2 '(())))))

嗯,我希望至少李能明白这个想法。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-13 08:13 , Processed in 0.509686 second(s), 72 queries .

© 2020-2025 乐筑天下

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