乐筑天下

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

[编程交流] 透镜型零误差

[复制链接]

95

主题

477

帖子

383

银币

后起之秀

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

铜币
475
发表于 2022-7-5 18:03:59 | 显示全部楼层 |阅读模式
因此,我有一个快速的问题,为什么我在这段代码中点击enter键时会出现错误“lentyp nil”。代码运行良好,但每次我通过点击空格键/回车退出代码时都会出现此错误。我已经看了很多遍了,但我找不到我的错误。我的代码是
  1. (defun c:hatchangle (/ hsel pt1 pt2 angr angd whilestop *error*)
  2. (defun *error* (msg)
  3.    (if (not
  4.   (member msg '("Function cancelled" "quit / exit abort"))
  5. )
  6.      (princ (strcat "\nError: " msg))
  7.    ))
  8.    (setq whilestop t)
  9.    (while whilestop
  10.      (setq hsel (car (entsel "\nSelect hatch: ")))
  11.      (cond
  12. ((= "HATCH" (cdr (assoc 0 (entget hsel))))
  13. (if (and (setq pt1 (getpoint "\nSelect 1st point for angle: "))
  14.    (setq pt2 (getpoint "\nSelect 2nd point for angle: ")))
  15.    (progn
  16.      (setq angr (angle pt1 pt2))
  17.      (setq angd (/ (* angr 180) pi))
  18.      (command "._-hatchedit" hsel "" "" "" angd)
  19.      (princ)
  20.      )
  21.    (setq whilestop nil)
  22.    )
  23. )
  24. ((= hsel nil)
  25.    (setq whilestop nil)
  26.    )
  27. (princ "\n Not a hatch, try again.")
  28. )
  29.      )      
  30.    (princ)
  31. )
回复

使用道具 举报

28

主题

317

帖子

292

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
140
发表于 2022-7-5 18:41:28 | 显示全部楼层
嗨bronkos
 
您的错误来自这样一个事实,即当您不选择任何内容时,hsel为零。
当您的hsel为零时,您尝试做的第一件事是条件约束
(=“HATCH”(cdr(assoc 0(entget hsel)))
你无法获取它,它会导致你的错误。
 
只需将两个条件交换在一起,首先计算代码停止条件,然后立即退出例程
 
  1. (defun c:hatchangle (/ hsel pt1 pt2 angr angd whilestop *error*)
  2. (defun *error* (msg)
  3.    (if        (not
  4.   (member msg '("Function cancelled" "quit / exit abort"))
  5. )
  6.      (princ (strcat "\nError: " msg))
  7.    )
  8. )
  9. (setq whilestop t)
  10. (while whilestop
  11.    (setq hsel (car (entsel "\nSelect hatch: ")))
  12.    (cond
  13.      ((= hsel nil)
  14.       (setq whilestop nil)
  15.      )
  16.      ((= "HATCH" (cdr (assoc 0 (entget hsel))))
  17.       (if (and        (setq pt1 (getpoint "\nSelect 1st point for angle: "))
  18.         (setq pt2 (getpoint "\nSelect 2nd point for angle: "))
  19.    )
  20. (progn
  21.    (setq angr (angle pt1 pt2))
  22.    (setq angd (/ (* angr 180) pi))
  23.    (command "._-hatchedit" hsel "" "" "" angd)
  24.    (princ)
  25. )
  26. (setq whilestop nil)
  27.       )
  28.      )
  29.      (princ "\n Not a hatch, try again.")
  30.    )
  31. )
  32. (princ)
  33. )

 
干杯
杰夫!
回复

使用道具 举报

95

主题

477

帖子

383

银币

后起之秀

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

铜币
475
发表于 2022-7-5 18:45:08 | 显示全部楼层
谢谢Jef!你的回答非常有用。最后,我对代码进行了一点修改,试图让while to exit循环仅在用户点击enter时退出,而不是在用户意外未选择任何内容时退出。此外,多亏了你,它工作得很好!我真的很感激,我确实学会了更仔细地检查条件句的顺序。 
  1. (defun c:hatchangle (/ hsel pt1 pt2 angr angd whilestop *error*)
  2. (defun *error* (msg)
  3.    (if (not
  4.   (member msg '("Function cancelled" "quit / exit abort"))
  5. )
  6.      (princ (strcat "\nError: " msg))
  7.    )
  8. )
  9. (while (setq hsel (car (entsel "\nSelect hatch: ")))
  10.    (cond
  11.      ((= hsel nil)
  12.       (princ "\nNothing selected.")
  13.      )
  14.      ((= "HATCH" (cdr (assoc 0 (entget hsel))))
  15.       (if (and (setq pt1 (getpoint "\nSelect 1st point for reference angle: "))
  16. (setq pt2 (getpoint "\nSelect 2nd point for reference angle: "))
  17.    )
  18. (progn
  19.    (setq angr (angle pt1 pt2))
  20.    (setq angd (/ (* angr 180) pi))
  21.    (command "._-hatchedit" hsel "" "" "" angd)
  22.    (princ)
  23. )
  24.       )
  25.      )
  26.      (t
  27.       (princ "\nNot a hatch, try again.")
  28.       )
  29.    )
  30. )
  31. (princ)
  32. )
回复

使用道具 举报

28

主题

317

帖子

292

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
140
发表于 2022-7-5 19:20:50 | 显示全部楼层
我很高兴能帮上忙。
 
关于最后发布的代码的一点评论。。。while语句只有在所包含的下一个表达式不是nil时才会继续。
如果你有
  1. (while (setq hsel (car (entsel "\nSelect hatch: ")))

...这意味着,如果您误点击(和/或选择nothing),hsel将为零,并且while语句此时停止计算。这也意味着,事实上,您的第一个conditional(=hsel nil)没有用处,永远无法满足/执行。有时我使用一些标志变量作为while标准(有时与“and或”或“or”组合到任何其他标准以满足我的需要),对于每个可能的结果,我使用/更改标志来控制while。非常基本的示例
  1. (defun c:hatchangle (/ hsel pt1 pt2 angr angd whilestop keepgoingflag *error*)
  2. (defun *error* (msg)
  3.   (if (not
  4.          (member msg '("Function cancelled" "quit / exit abort"))
  5.       )
  6.      (princ (strcat "\nError: " msg))
  7.    )
  8. )
  9. [color="darkorange"](setq keepgoingflag T)[/color]
  10. (while [color="darkorange"](or[/color] (setq hsel (car (entsel "\nSelect hatch: "))); [b]with a "or" if you misclick and select nothing...[/b]
  11.      [color="darkorange"]keepgoingflag)[/color]; [b]...the flag will prevent the while statement to stop being evaluated[/b]
  12.    (cond
  13.      ((= hsel nil);[b]now this cond can be evaluated[/b]
  14.       (princ "\nNothing selected. Try again or hit escape to end command"); [b]and now escape is the only way out[/b]
  15.      )
  16.      ((= "HATCH" (cdr (assoc 0 (entget hsel))))
  17.       (if (and (setq pt1 (getpoint "\nSelect 1st point for reference angle: "))
  18. (setq pt2 (getpoint "\nSelect 2nd point for reference angle: "))
  19.    )
  20. (progn
  21.    (setq angr (angle pt1 pt2))
  22.    (setq angd (/ (* angr 180) pi))
  23.    (command "._-hatchedit" hsel "" "" "" angd)
  24.    (princ)
  25. )
  26.       )
  27.      )
  28.      (t
  29.       (princ "\nNot a hatch, try again.")
  30.       )
  31.    )
  32. )
  33. (princ)
  34. )

 
有时候,简单的一段时间就足够了,有时候你需要绕着它转一圈。
 
快乐的编码!
杰夫!
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-13 01:11 , Processed in 0.578082 second(s), 60 queries .

© 2020-2025 乐筑天下

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