我正在尝试调用以下函数:
(defun c:Add ()
(setq a (getint "Enter a number to add 2 to it"))
(setq a (+ a 2))
)
此LOOPER函数内部:
(defun LOOPER (func)
;repeats 'func' until user enters 'no'
(setq dummy "w")
(while dummy
(func) ;this is obviously the problem
(setq order (getstring "\nContinue? (Y or N):"))
(if (or (= order "N") (= order "n")) (setq dummy nil))
)
)
赞:
(defun c:Adder ()
(LOOPER (c:Add))
)
如何解决func
函数中未定义LOOPER
的事实?
答案 0 :(得分:2)
您可以将一个函数作为参数传递给另一个函数,如以下示例所示:
(defun c:add ( / a )
(if (setq a (getint "\nEnter a number to add 2 to it: "))
(+ a 2)
)
)
(defun looper ( func )
(while
(progn
(initget "Y N")
(/= "N" (getkword "\nContinue? [Y/N] <Y>: "))
)
(func)
)
)
(defun c:adder ( )
(looper c:add)
)
在这里,对符号c:add
进行评估以产生指向函数定义的指针,然后将其绑定到func
函数范围内的符号looper
。因此,在looper
函数的范围内,符号func
和c:add
评估相同的函数。
或者,您可以将符号c:add
用作带引号的符号,在这种情况下,符号func
的值就是符号c:add
,然后可以对其进行求值以得出功能:
(defun c:add ( / a )
(if (setq a (getint "\nEnter a number to add 2 to it: "))
(+ a 2)
)
)
(defun looper ( func )
(while
(progn
(initget "Y N")
(/= "N" (getkword "\nContinue? [Y/N] <Y>: "))
)
((eval func))
)
)
(defun c:adder ( )
(looper 'c:add)
)
将带引号的符号作为函数参数传递与标准的AutoLISP函数(例如mapcar
,apply
等)更加一致。
答案 1 :(得分:0)
据我所知,您不能发送函数名称作为参数,但是在这里,我给您提供了一种可以起到类似作用的技术。
我的计算机上没有安装Autocad,因此无法测试此代码。但是如果有任何小错误,您可以删除它,也可以抓住这个概念,以便自己实现。
(defun c:Add ()
(setq a (getint "Enter a number to add 2 to it"))
(setq a (+ a 2))
)
(defun c:sub ()
(setq a (getint "Enter a number to substract from 2:"))
(setq a (-2 a))
)
(defun c:mul ()
(setq a (getint "Enter a number to multiply with 2:"))
(setq a (* a 2))
)
;----This function use to call other function from function name
;----Function name string is case sensitive
;----As per need you can Add function name to this function
(Defun callFunction(name)
(setq output nil)
;here you can add nested if condition but for simplicity I use If alone
(if (= name "C:Add")(setq output (C:Add)))
(if (= name "C:sub")(setq output (C:sub)))
(if (= name "C:mul")(setq output (C:mub)))
output
)
;----------Function end here
(defun LOOPER (func)
;repeats 'func' until user enters 'no'
(setq dummy "w")
(while dummy
(callFunction func) ;Change here
(setq order (getstring "\nContinue? (Y or N):"))
(if (or (= order "N") (= order "n")) (setq dummy nil))
)
)
您喜欢这样运行此程序:
(defun c:Adder ()
(LOOPER ("c:Add"))
)
(defun c:substaker ()
(LOOPER ("c:sub"))
)
(defun c:multiplyer ()
(LOOPER ("c:mul"))
)
希望这会有所帮助: