Aftertouch 发表于 2022-7-5 15:46:51

了解李';s.c

大家好,
 
在尝试学习李·麦克的许多代码时,我偶然发现了这段非常“简单”的代码。。。
 

;; Active Document-Lee Mac
;; Returns the VLA Active Document Object

(defun LM:acdoc nil
   (eval (list 'defun 'LM:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))
   (LM:acdoc)
)

 
为什么这段代码如此“复杂”,它不只是定义为:

(defun LM:acdoc ( / )
(vla-get-activedocument (vlax-get-acad-object))
)

 
因为它似乎返回相同的值??

MURAl_KMD 发表于 2022-7-5 16:06:56

嗨,Aftertouch
 
如果已经加载了函数LM:acdoc(可能是一些旧函数),
它将被李的编码LM:acdoc(最新编码)所取代。
 
这是我的理解。

Grrr 发表于 2022-7-5 16:17:49

欢迎学习LeeSp
 
我认为李称之为“运行时评估”。关键是第一次调用(LM:acdoc)时会发生什么。
 
以下是控制台上的一些观察结果:
_$ (defun printhello nil
(eval (list 'defun 'printhello 'nil (print "Hello")))
(printhello)
)
PRINTHELLO
_$ (printhello)

"Hello" "Hello" ; !!!
_$ (printhello)
"Hello"         ; now it exits cleanly?
_$ (printhello)
"Hello"
_$ (printhello)
"Hello"
 
_$ (defun printhello nil
(print "Hello")
)
PRINTHELLO
_$ (printhello)

"Hello" "Hello"
_$ (printhello)

"Hello" "Hello"
_$ (printhello)

"Hello" "Hello"
 
除此之外,我的知识是有限的,所以如果有人解释他为什么使用这个会更好。

Roy_043 发表于 2022-7-5 16:30:35

@Grrr:
也许这有助于解释你的“干净退出”问题:
第一次调用时,Lee的LM:acdoc函数重新定义了自身。新定义直接返回活动文档对象,因此比Aftertouch提出的不太复杂的函数快(分钟)。

Grrr 发表于 2022-7-5 16:35:57

罗伊,
 
谢谢-现在我看到我的观察给这带来了更多的困惑。因此,回报没有差异:
_$ (defun test nil
(eval (list 'defun 'test 'nil "return_this_string"))
(test)
)
TEST
_$ (test)
"return_this_string"
_$ (test)
"return_this_string"
_$ (test)
"return_this_string"

6
 
我会坚持你的信息:

Aftertouch 发表于 2022-7-5 16:55:28

好的,我们已经解决了这个案子。谢谢你的夸奖!
页: [1]
查看完整版本: 了解李';s.c