乐筑天下

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

[编程交流] 中线LISP错误

[复制链接]

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 17:07:24 | 显示全部楼层 |阅读模式
好的,所以我今天有一个绝对的nightmaire-两个线程在几分钟内!今天我的Lisp程序似乎都不想用了。。。
 
好的,我一直在尝试创建一个LISP来在一个圆上画一条中心线——像pi一样简单。
 
但我不断返回一个错误,我不知道为什么-毫无疑问,这可能是这么简单的事情。。。
 
无论如何,我将感谢任何可用的帮助,用JONTHEPOPE的话来说,“很抱歉所有的帖子……”
 
  1. ;|
  2.    Centre-Line by Lee McDonnell
  3.        November 2008
  4. |;
  5. (defun c:cl (/ oldlay oldsnap c1 c1ent c1cent c1rad crat)
  6.    
  7. ;     --- Error Trap ---
  8.    
  9.    (defun *error*(msg)
  10.    (if oldVars
  11.        (mapcar 'setvar varLst  oldVars)
  12.    ); end if
  13.    (princ "\nError or Esc pressed... ")
  14.    (princ)
  15.    ); end of *error*
  16.        (setq varLst    (list "CMDECHO" "OSMODE" "CLAYER" "DIMSCALE")
  17.          oldVars    (mapcar 'getvar varLst)
  18.    ); end setq
  19. ;    --- Error Trap ---
  20.    (setvar "cmdecho" 0)
  21.    (setq oldlay (getvar "clayer"))
  22.    (setq oldsnap (getvar "osmode"))
  23.    (mapcar 'cllayer '("1" "2" "3" "4" "5"))
  24.    (if     (not (getenv "cl:ratio"))
  25.        (setenv "cl:ratio" "122")
  26.    ) ; end if
  27.    (princ (strcat "\nType "CLSET" to Change Base Variables. << Current Settings >> C/L Ratio: "
  28.            (getvar "cl:ratio")))
  29.    (while
  30.        (setq c1 (entsel "\nSelect Circle for Centre-line: "))
  31.        (setq c1ent (entget (car c1)))
  32.        (if     (= (cdr (assoc 0 c1ent)) "CIRCLE")
  33.            (progn
  34.                (setq     c1cent     (assoc 10 c1ent)
  35.                    c1rad    (assoc 40 c1ent)
  36.                    crat    (/ (atof (getenv "cl:ratio")) 100)
  37.                ) ; end setq
  38.                (setvar "osmode" 0)
  39.                (setvar "clayer" "5")
  40.                (command "-linetype" "S" "CENTER" "")
  41.                (command "_line"
  42.                    (polar
  43.                        c1cent
  44.                        pi
  45.                        (* clrad crat)
  46.                    ) ; end polar
  47.                    (polar
  48.                        c1cent
  49.                        0
  50.                        (* clrad crat)
  51.                    ) ; end polar
  52.                    ""
  53.                ) ;end line
  54.                (command "_line"
  55.                    (polar
  56.                        c1cent
  57.                        (/ (* 3 pi) 2)
  58.                        (* clrad crat)
  59.                    ) ; end polar
  60.                    (polar
  61.                        c1cent
  62.                        (/ pi 2)
  63.                        (* clrad crat)
  64.                    ) end polar
  65.                    ""
  66.                ) ; end line
  67.            ) ; end progn
  68.            (alert "\nEntity is not a Circle.")
  69.        ) ; end if
  70.    ) ; end while
  71.    (setvar "clayer" oldlay)
  72.    (setvar "osmode" oldsnap)
  73.    (setvar "cmdecho" 1)
  74.    (princ "\nFunction Complete.")
  75.    (princ)
  76. ) ; end cl
  77. (defun cllayer (lay)
  78.    (if     (not (tblsearch "LAYER" lay))
  79.            (command "_.-layer" "_m" lay "_c" lay lay "")
  80.    ) ; end if
  81.    (princ)
  82. ) end cllayer
  83. (defun c:clset (/ cratio)
  84.    (if     (not (getenv "cl:ratio"))
  85.        (setenv "cl:ratio" "122")
  86.    ) ; end if
  87.    (princ (strcat "\n<< Current Settings >> C/L Ratio: " (getvar "cl:ratio") "%"))
  88.    (if    (setq cratio (getreal (strcat "\nSpecify C/L as % of Circle Radius <"
  89.                        (getenv "cl:ratio")
  90.                        "> : ")))
  91.        (setenv "cl:ratio" (rtos cratio))
  92.    ) ; end if
  93.    (princ "\nBase Variables Set.")
  94.    (princ)
  95. ) ; end clset

 
谢谢大家
回复

使用道具 举报

9

主题

167

帖子

163

银币

初来乍到

Rank: 1

铜币
43
发表于 2022-7-6 17:14:53 | 显示全部楼层
你现在可以借我的。。。
 
  1. (defun c:cce (/ lt osn oucs ss b d d1 ename elist cp r pn ps pw pe r2)
  2. (command "cmdecho" (getvar "cmdecho"))
  3. (setq lt "center")
  4. (if (= (tblsearch "ltype" lt) nil)
  5.    (command "-linetype" "l" lt "acad.lin" ""))
  6. (princ "\n Center cross: ")
  7. (cond
  8.    ((setq ss (ssget '((-4 . "<OR")
  9.                (0 . "CIRCLE,ARC,ELLIPSE")
  10.                (-4 . "OR>")
  11.               )
  12.       )
  13.     )
  14.     (setq b (sslength ss))                ; = number of circles in ss
  15.     (setq d b)
  16.     (repeat b
  17.       (setq d1           (1- d)
  18.      ename (ssname ss d1)        ; = last entity in ss
  19.      elist (entget ename)
  20.      cp           (cdr (assoc 10 elist))
  21.      r           (cdr (assoc 40 elist))
  22.      d           (* r 1.5)                ; greater this number, greater overlap of center lines (%)
  23.      pn           (polar cp (* pi 0.5) d)
  24.      ps           (polar cp (* pi 1.5) d)
  25.      pw           (polar cp pi d)
  26.      pe           (polar cp 0 d)
  27.      d           d1
  28.      r2           (* r 0.25)
  29.       )
  30.                                 ;(setvar "osmode" 0)
  31.       (entmake        (list
  32.           (cons 0 "LINE")
  33.           (cons 6 lt)
  34.           (cons 62 3)
  35.           (cons 10 pn)
  36.           (cons 11 ps)
  37.           (cons 48 r2)
  38.           (cons 210 (list 0.0 0.0 1.0))
  39.         )
  40.       )
  41.       (entmake        (list
  42.           (cons 0 "LINE")
  43.           (cons 6 lt)
  44.           (cons 62 3)
  45.           (cons 10 pw)
  46.           (cons 11 pe)
  47.           (cons 48 r2)
  48.           (cons 210 (list 0.0 0.0 1.0))
  49.         )
  50.       )
  51.     )
  52.    )
  53.    (T (princ "\n No circle selected! "))
  54. )
  55. (if (not (tblsearch "ltype" lt))
  56.    (princ (strcat "\n Linetype "" lt "" was not loaded."))
  57. )
  58. (princ)
  59. )
  60. (prompt "\n Type < cce > for circle or arc center lines")
回复

使用道具 举报

10

主题

109

帖子

99

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
50
发表于 2022-7-6 17:20:25 | 显示全部楼层
李,
我发现了很多小东西:“cl”的许多实例需要“c1”和几个“getvar”而不是“getenv”。需要几个(assoc)调用(cdr(assoc))。还发现了几个没有前导分号的注释。这可能没有必要,但我将错误陷阱和其他defuns移出了主程序,因为它使我更容易理解。我添加了一些代码来保存和恢复系统*错误*处理程序。也许这不是严格要求的,但这是我学会做这件事的方式,我想确保这不会造成麻烦。我不得不将主程序重命名为CLDRAW,因为CL是我系统上COPYLINK的别名。
该程序确实绘制了中心线,但我不确定它是否完全按照您的预期运行。“WHILE”循环是无限的-这可能是有意的,因此您可以在任意数量的圆上绘制中心线,但这确实意味着必须强制程序以“ESC”结束。此外,提示提示提示您可以使用CLSET重置值,但在主程序中没有暂停来允许它。程序结束时线型不会重置-它保持中心线。创建了五个层-第5层被设置为活动,并在程序结束时保持活动。
我只是不知道你的意图。我希望这个节目能达到你想要的效果。如果你完成了,请发布最终版本。我可能想自己用!
 
 
  1. (defun c:clset (/ cratio)
  2. (if (not (getenv "cl:ratio"))
  3. (setenv "cl:ratio" "122")
  4. ) ; end if
  5. (princ (strcat "\n<< Current Settings >> C/L Ratio: " (getenv "cl:ratio") "%")) ; chg getvar to getenv
  6. (if (setq cratio (getreal (strcat "\nSpecify C/L as % of Circle Radius <"
  7. (getenv "cl:ratio")
  8. "> : ")))
  9. (setenv "cl:ratio" (rtos cratio))
  10. ) ; end if
  11. (princ "\nBase Variables Set.")
  12. (princ)
  13. ) ; end clset
  14. (defun cllayer (lay) ; moved from end
  15. (if (not (tblsearch "LAYER" lay))
  16. (command "_.-layer" "_m" lay "_c" lay lay "")
  17. ) ; end if
  18. (princ)
  19. ) ; end cllayer - added semicolon
  20. ; --- Error Trap ---
  21. (defun cl_error (msg)
  22. (if oldVars
  23. (mapcar 'setvar varLst oldVars)
  24. ); end if
  25. (princ "\nError or Esc pressed... ")
  26. (setq *ERROR* sys_error)
  27. (princ)
  28. ); end of *error*
  29. ; --- Error Trap ---
  30. (defun c:cldraw (/ oldlay oldsnap c1 c1ent c1cent c1rad crat)
  31. (setq sys_error *ERROR*)
  32. (setq *ERROR* cl_error)
  33. (setq varLst (list "CMDECHO" "OSMODE" "CLAYER" "DIMSCALE")
  34. oldVars (mapcar 'getvar varLst)
  35. ); end setq
  36. (setvar "cmdecho" 0)
  37. (setq oldlay (getvar "clayer"))
  38. (setq oldsnap (getvar "osmode"))
  39. (mapcar 'cllayer '("1" "2" "3" "4" "5"))
  40. (if (not (getenv "cl:ratio"))
  41. (setenv "cl:ratio" "122")
  42. ) ; end if
  43. (princ (strcat "\nType "CLSET" to Change Base Variables. << Current Settings >> C/L Ratio: "
  44. (getenv "cl:ratio"))) ; fix
  45. (while
  46. (setq c1 (entsel "\nSelect Circle for Centre-line: "))
  47. (setq c1ent (entget (car c1)))
  48. (if (= (cdr (assoc 0 c1ent)) "CIRCLE")
  49. ; fixed c1rad & add cdr in progn
  50. (progn
  51. (setq c1cent (cdr (assoc 10 c1ent))
  52. c1rad (cdr (assoc 40 c1ent))
  53. crat (/ (atof (getenv "cl:ratio")) 100)
  54. ) ; end setq
  55. (princ "\nc1rad = ")(princ c1rad)
  56. (princ "\ncrat = ")(princ crat)
  57. (setvar "osmode" 0)
  58. (setvar "clayer" "5")
  59. (command "-linetype" "S" "CENTER" "")
  60. (command "_line"
  61. (polar
  62. c1cent
  63. pi
  64. (* c1rad crat)
  65. ) ; end polar
  66. (polar
  67. c1cent
  68. 0
  69. (* c1rad crat)
  70. ) ; end polar
  71. ""
  72. ) ; end line
  73. (command "_line"
  74. (polar
  75. c1cent
  76. (/ (* 3 pi) 2)
  77. (* c1rad crat)
  78. ) ; end polar
  79. (polar
  80. c1cent
  81. (/ pi 2)
  82. (* c1rad crat)
  83. ) ; end polar
  84. ""
  85. ) ; end line
  86. ) ; end progn
  87. (alert "\nEntity is not a Circle.")
  88. ) ; end if
  89. ) ; end while
  90. (setvar "clayer" oldlay)
  91. (setvar "osmode" oldsnap)
  92. (setvar "cmdecho" 1)
  93. (setq *ERROR* sys_error)
  94. (princ "\nFunction Complete.")
  95. (princ)
  96. ) ; end cl
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 17:22:41 | 显示全部楼层
你是一个传奇人物-我很尴尬地看到我在Lisp程序中犯了那么多荒谬的错误。。。。
 
多亏了你,Lisp程序现在效果很好…-我没有使用你的错误陷阱版本-只是因为我不完全理解它,但谢谢你的帮助。
 
但是,我不明白为什么层没有重置为原始的当前层。我已经包括:
 
然而,在“While”功能完成后,层“5”在“While”功能完成后仍保持活动状态。
 
再次感谢
 
 
LISP:
 
  1. ;|
  2.    Centre-Line by Lee McDonnell
  3.        November 2008
  4. |;
  5. (defun c:cl (/ oldlay oldsnap c1 c1ent c1cent c1rad crat)
  6.    
  7. ;     --- Error Trap ---
  8.    
  9.    (defun *error*(msg)
  10.    (if oldVars
  11.        (mapcar 'setvar varLst  oldVars)
  12.    ); end if
  13.    (princ "\nError or Esc pressed... ")
  14.    (princ)
  15.    ); end of *error*
  16.        (setq varLst    (list "CMDECHO" "OSMODE" "CLAYER" "DIMSCALE")
  17.          oldVars    (mapcar 'getvar varLst)
  18.    ); end setq
  19. ;    --- Error Trap ---
  20.    (setvar "cmdecho" 0)
  21.    (setq oldlay (getvar "clayer"))
  22.    (setq oldsnap (getvar "osmode"))
  23.    (mapcar 'cllayer '("1" "2" "3" "4" "5"))
  24.    (if     (not (getenv "cl:ratio"))
  25.        (setenv "cl:ratio" "122")
  26.    ) ; end if
  27.    (princ (strcat "\nType "CLSET" to Change Base Variables. << Current Settings >> C/L Ratio: "
  28.            (getenv "cl:ratio")))
  29.    (while
  30.        (/= (setq c1 (entsel "\nSelect Circle for Centre-line: ")) nil)
  31.        (setq c1ent (entget (car c1)))
  32.        (if     (= (cdr (assoc 0 c1ent)) "CIRCLE")
  33.            (progn
  34.                (setq     c1cent     (cdr (assoc 10 c1ent))
  35.                    c1rad    (cdr (assoc 40 c1ent))
  36.                    crat    (/ (atof (getenv "cl:ratio")) 100)
  37.                ) ; end setq
  38.                (setvar "osmode" 0)
  39.                (setvar "clayer" "5")
  40.                (command "-linetype" "S" "CENTER" "")
  41.                (command "_line"
  42.                    (polar
  43.                        c1cent
  44.                        pi
  45.                        (* c1rad crat)
  46.                    ) ; end polar
  47.                    (polar
  48.                        c1cent
  49.                        0
  50.                        (* c1rad crat)
  51.                    ) ; end polar
  52.                    ""
  53.                ) ;end line
  54.                (command "_line"
  55.                    (polar
  56.                        c1cent
  57.                        (/ (* 3 pi) 2)
  58.                        (* c1rad crat)
  59.                    ) ; end polar
  60.                    (polar
  61.                        c1cent
  62.                        (/ pi 2)
  63.                        (* c1rad crat)
  64.                    ) ; end polar
  65.                    ""
  66.                ) ; end line
  67.            ) ; end progn
  68.            (alert "\nEntity is not a Circle.")
  69.        ) ; end if
  70.        (command "-linetype" "S" "Bylayer" "")
  71.    ) ; end while
  72.    (setvar "clayer" oldlay)
  73.    (setvar "osmode" oldsnap)
  74.    (setvar "cmdecho" 1)
  75.    (princ "\nFunction Complete.")
  76.    (princ)
  77. ) ; end cl
  78. (defun cllayer (lay)
  79.    (if     (not (tblsearch "LAYER" lay))
  80.            (command "_.-layer" "_m" lay "_c" lay lay "")
  81.    ) ; end if
  82.    (princ)
  83. ) ; end cllayer
  84. (defun c:clset (/ cratio)
  85.    (if     (not (getenv "cl:ratio"))
  86.        (setenv "cl:ratio" "122")
  87.    ) ; end if
  88.    (princ (strcat "\n<< Current Settings >> C/L Ratio: " (getenv "cl:ratio") "%"))
  89.    (if    (setq cratio (getreal (strcat "\nSpecify C/L as % of Circle Radius <"
  90.                        (getenv "cl:ratio")
  91.                        "> : ")))
  92.        (setenv "cl:ratio" (rtos cratio))
  93.    ) ; end if
  94.    (princ "\nBase Variables Set.")
  95.    (princ)
  96. ) ; end clset
回复

使用道具 举报

10

主题

109

帖子

99

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
50
发表于 2022-7-6 17:28:51 | 显示全部楼层
李,
使用你最新的代码,我的系统似乎一切正常。活动图层和线型恢复为原始图层。我可以通过ESC退出程序,也可以输入或只是在屏幕上选取空白。如果我选择了任何不是圆的东西,则会出现警报框,单击“确定”返回循环。CLSET程序也运行良好。这里一切都很好,我不知道为什么图层不能为你重置。正如您所说,(setvar“clayer”oldlay)应该处理它,您的错误陷阱也通过恢复oldvar来处理它。所以,很抱歉,我对图层问题没有想法。
 
关于*错误*处理程序:这主要是一个“好形式”问题。当您写入(defun*error*(msg)时,您正在重新定义系统错误处理程序。如果不保存和恢复默认处理程序,则定义的处理程序将成为新的系统处理程序,在某些情况下,这不是您想要的。当我第一次开始学习Autolisp时,我给自己带来了一些痛苦,因为我会编写一个类似于您的处理程序,然后继续编写另一个程序,并感到困惑,因为当程序中出现错误时,显示的消息不是我所期望的。有时系统变量会包含奇怪的非默认值,需要一段时间才能找到原因。由于许多行为良好的程序很少或从不导致调用系统处理程序的错误,因此您可能会在很长一段时间内没有意识到系统处理程序已被无意中修改。因此,在研究了其他程序员的代码后,我采用了我的标准方法。在lisp的开头或附近,我用(setq sys\u error*error*)保存默认处理程序,然后是新的定义(setq*error*my\u error)。然后我编写一个单独的函数
 
(defun my_error(msg)
.
.
.
(setq*错误*系统错误)
)
 
每当调用我的处理程序时,最后一行恢复默认处理程序。我还包括主程序末尾或附近的(setq*error*sys\u error),因此处理程序在每个正常出口中都会恢复。
这种方法对我来说很可靠。如果有更好的方法,我很乐意被告知。
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 17:31:09 | 显示全部楼层
感谢您提供有关错误处理程序的信息-我现在将在代码中使用它。
 
我有一些关于我的问题的更多信息-当我通过按enter键或点击空白退出功能时,该层不会恢复。
 
但是,如果我按Esc键结束函数,图层和线型将恢复,但这只是因为我的错误处理程序正在运行。(出现错误消息)
回复

使用道具 举报

10

主题

109

帖子

99

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
50
发表于 2022-7-6 17:38:00 | 显示全部楼层
李,
您可以尝试在结束时使用在错误陷阱中使用的相同方法,即mapcar:
 
(如果是oldVars
(mapcar的setvar varLst oldVars)
); 如果结束
 
可能值得一试,它确实在一定程度上简化了代码。
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 17:41:59 | 显示全部楼层
谢谢CALCAD,我会尝试一下的——谢谢你在这件事上的帮助,一如既往,非常感谢你的任何建议。
回复

使用道具 举报

CAB

29

主题

781

帖子

430

银币

中流砥柱

Rank: 25

铜币
526
发表于 2022-7-6 17:45:56 | 显示全部楼层
在某些情况下,我很快改变了自己的风格,并根据自己的喜好进行了调整。
 
  1. 5
回复

使用道具 举报

10

主题

109

帖子

99

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
50
发表于 2022-7-6 17:51:23 | 显示全部楼层
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-4 19:09 , Processed in 0.535532 second(s), 72 queries .

© 2020-2025 乐筑天下

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