乐筑天下

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

[编程交流] 如何评估lisp代码?

[复制链接]

218

主题

699

帖子

483

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1090
发表于 2022-7-5 23:08:57 | 显示全部楼层 |阅读模式
你好
 
我知道在发了500多篇帖子后,这个问题听起来很复杂,但我仍然不知道评价的顺序是什么?前缀C:fun有顺序优势吗?是否先计算到按钮,然后再计算到C
 
如果你真的想解释,让我们以李为例
 
  1. ;;----------------=={ Add Objects to Block }==----------------;;
  2. ;;                                                            ;;
  3. ;;  Adds all objects in the provided SelectionSet to the      ;;
  4. ;;  definition of the specified block.                        ;;
  5. ;;------------------------------------------------------------;;
  6. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  7. ;;------------------------------------------------------------;;
  8. ;;  Arguments:                                                ;;
  9. ;;  doc   - Document Object in which block resides.           ;;
  10. ;;  block - Entity name of reference insert                   ;;
  11. ;;  ss    - SelectionSet of objects to add to definition      ;;
  12. ;;------------------------------------------------------------;;
  13. (defun LM:AddObjectstoBlock ( doc block ss / lst mat )
  14. (setq lst (LM:ss->vla ss)
  15.        mat (LM:Ref->Def block)
  16.        mat (vlax-tmatrix (append (mapcar 'append (car mat) (mapcar 'list (cadr mat))) '((0. 0. 0. 1.))))
  17. )
  18. (foreach obj lst (vla-transformby obj mat))
  19. (vla-CopyObjects doc (LM:SafearrayVariant vlax-vbobject lst)
  20.    (vla-item (vla-get-Blocks doc) (cdr (assoc 2 (entget block))))
  21. )
  22. (foreach obj lst (vla-delete obj))
  23. (vla-regen doc acAllViewports)
  24. )
  25. ;;-----------------=={ Remove From Block }==------------------;;
  26. ;;                                                            ;;
  27. ;;  Removes an Entity from a Block Definition                 ;;
  28. ;;------------------------------------------------------------;;
  29. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  30. ;;------------------------------------------------------------;;
  31. ;;  Arguments:                                                ;;
  32. ;;  ent - Entity name of Object to Delete from Block [ENAME]  ;;
  33. ;;------------------------------------------------------------;;
  34. (defun LM:RemovefromBlock ( doc ent )
  35. (vla-delete (vlax-ename->vla-object ent))
  36. (vla-regen doc acAllViewports)
  37. (princ)
  38. )
  39. ;;------------------=={ Safearray Variant }==-----------------;;
  40. ;;                                                            ;;
  41. ;;  Creates a populated Safearray Variant of a specified      ;;
  42. ;;  data type                                                 ;;
  43. ;;------------------------------------------------------------;;
  44. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  45. ;;------------------------------------------------------------;;
  46. ;;  Arguments:                                                ;;
  47. ;;  datatype - variant type enum (eg vlax-vbDouble)           ;;
  48. ;;  data     - list of static type data                       ;;
  49. ;;------------------------------------------------------------;;
  50. ;;  Returns:  VLA Variant Object of type specified            ;;
  51. ;;------------------------------------------------------------;;
  52.                         
  53. (defun LM:SafearrayVariant ( datatype data )
  54. (vlax-make-variant
  55.    (vlax-safearray-fill
  56.      (vlax-make-safearray datatype (cons 0 (1- (length data)))) data
  57.    )   
  58. )
  59. )
  60. ;;------------=={ SelectionSet -> VLA Objects }==-------------;;
  61. ;;                                                            ;;
  62. ;;  Converts a SelectionSet to a list of VLA Objects          ;;
  63. ;;------------------------------------------------------------;;
  64. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  65. ;;------------------------------------------------------------;;
  66. ;;  Arguments:                                                ;;
  67. ;;  ss - Valid SelectionSet (Pickset)                         ;;
  68. ;;------------------------------------------------------------;;
  69. ;;  Returns:  List of VLA Objects, else nil                   ;;
  70. ;;------------------------------------------------------------;;
  71. (defun LM:ss->vla ( ss / i l )
  72. (if ss
  73.    (repeat (setq i (sslength ss))
  74.      (setq l (cons (vlax-ename->vla-object (ssname ss (setq i (1- i)))) l))
  75.    )
  76. )
  77. )
  78. ;;---------------=={ Block Ref -> Block Def }==---------------;;
  79. ;;                                                            ;;
  80. ;;  Returns the Transformation Matrix and Translation Vector  ;;
  81. ;;  for transforming Block Reference Geometry to the Block    ;;
  82. ;;  Definiton.                                                ;;
  83. ;;------------------------------------------------------------;;
  84. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  85. ;;------------------------------------------------------------;;
  86. ;;  Arguments:                                                ;;
  87. ;;  e - Block Reference Entity                                ;;
  88. ;;------------------------------------------------------------;;
  89. ;;  Returns:  List of 3x3 Transformation Matrix, Vector       ;;
  90. ;;------------------------------------------------------------;;
  91. (defun LM:Ref->Def ( e / _dxf a l n )
  92. (defun _dxf ( x l ) (cdr (assoc x l)))
  93. (setq l (entget e) a (- (_dxf 50 l)) n (_dxf 210 l))
  94. (
  95.    (lambda ( m )
  96.      (list m
  97.        (mapcar '- (_dxf 10 (tblsearch "BLOCK" (_dxf 2 l)))
  98.          (mxv m
  99.            (trans (_dxf 10 l) n 0)
  100.          )
  101.        )
  102.      )
  103.    )
  104.    (mxm
  105.      (list
  106.        (list (/ 1. (_dxf 41 l)) 0. 0.)
  107.        (list 0. (/ 1. (_dxf 42 l)) 0.)
  108.        (list 0. 0. (/ 1. (_dxf 43 l)))
  109.      )
  110.      (mxm
  111.        (list
  112.          (list (cos a) (sin (- a)) 0.)
  113.          (list (sin a) (cos a)     0.)
  114.          (list    0.        0.     1.)
  115.        )
  116.        (mapcar '(lambda ( e ) (trans e n 0 t))
  117.         '(
  118.            (1. 0. 0.)
  119.            (0. 1. 0.)
  120.            (0. 0. 1.)
  121.          )
  122.        )
  123.      )
  124.    )
  125. )
  126. )
  127. ;; Matrix x Vector  -  Vladimir Nesterovsky
  128. (defun mxv ( m v )
  129. (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  130. )
  131. ;; Matrix x Matrix  -  Vladimir Nesterovsky
  132. (defun mxm ( m q )
  133. (mapcar (function (lambda ( r ) (mxv (trp q) r))) m)
  134. )
  135. ;; Matrix Transpose  -  Doug Wilson
  136. (defun trp ( m )
  137. (apply 'mapcar (cons 'list m))
  138. )
  139. ;;---------------------=={ Select if }==----------------------;;
  140. ;;                                                            ;;
  141. ;;  Provides continuous selection prompts until either a      ;;
  142. ;;  predicate function is validated or a keyword is supplied. ;;
  143. ;;------------------------------------------------------------;;
  144. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  145. ;;------------------------------------------------------------;;
  146. ;;  Arguments:                                                ;;
  147. ;;  msg  - prompt string                                      ;;
  148. ;;  pred - optional predicate function [selection list arg]   ;;
  149. ;;  func - selection function to invoke                       ;;
  150. ;;  keyw - optional initget argument list                     ;;
  151. ;;------------------------------------------------------------;;
  152. ;;  Returns:  Entity selection list, keyword, or nil          ;;
  153. ;;------------------------------------------------------------;;
  154. (defun LM:SelectIf ( msg pred func keyw / sel ) (setq pred (eval pred))  
  155. (while
  156.    (progn (setvar 'ERRNO 0) (if keyw (apply 'initget keyw)) (setq sel (func msg))
  157.      (cond
  158.        ( (= 7 (getvar 'ERRNO))
  159.          (princ "\nMissed, Try again.")
  160.        )
  161.        ( (eq 'STR (type sel))
  162.          nil
  163.        )
  164.        ( (vl-consp sel)
  165.          (if (and pred (not (pred sel)))
  166.            (princ "\nInvalid Object Selected.")
  167.          )
  168.        )
  169.      )
  170.    )
  171. )
  172. sel
  173. )
  174. ;-------------------------------------------------------------;
  175. ;                   -- Test Functions --                      ;
  176. ;-------------------------------------------------------------;
  177. (defun c:Add2Block ( / *error* _StartUndo _EndUndo acdoc ss e )
  178. (defun *error* ( msg )
  179.    (if acdoc (_EndUndo acdoc))
  180.    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
  181.        (princ (strcat "\n** Error: " msg " **")))
  182.    (princ)
  183. )
  184. (defun _StartUndo ( doc ) (_EndUndo doc)
  185.    (vla-StartUndoMark doc)
  186. )
  187. (defun _EndUndo ( doc )
  188.    (if (= 8 (logand 8 (getvar 'UNDOCTL)))
  189.      (vla-EndUndoMark doc)
  190.    )
  191. )
  192. (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  193. (if
  194.    (and (setq ss (ssget "_:L"))
  195.      (setq e
  196.        (LM:SelectIf "\nSelect Block to Add Objects to: "
  197.         '(lambda ( x ) (eq "INSERT" (cdr (assoc 0 (entget (car x)))))) entsel nil
  198.        )
  199.      )
  200.    )
  201.    (progn
  202.      (_StartUndo acdoc) (LM:AddObjectstoBlock acdoc (car e) ss) (_EndUndo acdoc)
  203.    )
  204. )
  205. (princ)
  206. )
  207. ;-------------------------------------------------------------;
  208. (defun c:Remove ( / *error* _StartUndo _EndUndo acdoc e )
  209. (defun *error* ( msg )
  210.    (if acdoc (_EndUndo acdoc))
  211.    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
  212.        (princ (strcat "\n** Error: " msg " **")))
  213.    (princ)
  214. )
  215. (defun _StartUndo ( doc ) (_EndUndo doc)
  216.    (vla-StartUndoMark doc)
  217. )
  218. (defun _EndUndo ( doc )
  219.    (if (= 8 (logand 8 (getvar 'UNDOCTL)))
  220.      (vla-EndUndoMark doc)
  221.    )
  222. )
  223. (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  224. (while (setq e (car (nentsel "\nSelect Object to Remove: ")))
  225.    (_StartUndo acdoc) (LM:RemovefromBlock acdoc e) (_EndUndo acdoc)
  226. )
  227. (princ)
  228. )
  229. (vl-load-com) (princ)
  230. ;;------------------------------------------------------------;;
  231. ;;                         End of File                        ;;
  232. ;;------------------------------------------------------------;;
回复

使用道具 举报

4

主题

194

帖子

192

银币

初来乍到

Rank: 1

铜币
25
发表于 2022-7-6 00:17:30 | 显示全部楼层
samifox,
 
它非常简单,从上到下进行评估,在评估过程中,无论您将其发送到哪里,都会加载函数,但在单独调用之前不会进行评估。这就是为什么需要将子函数列在c:defun内,或者列在被调用的c:defun外。
 
整个lisp文件在加载时进行评估,然后评估加载时或通过命令提示指定的“c:defun”。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-11 03:33 , Processed in 1.474345 second(s), 56 queries .

© 2020-2025 乐筑天下

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