cchessbd 发表于 2023-3-15 07:42:54

求批量定义函数?

本帖最后由 cchessbd 于 2024-3-31 21:44 编辑

求各位高手帮我精简以下函数,尽量精简些来批量定义。因为我还想加一些、、、
但是论坛的帖子的代码,我看不懂。怎么修改那个帖子的代码,都达不到我以下代码的效果。

(defun C:+A () (TQHZ "K*+*" "A" "Q") )
(defun C:+B () (TQHZ "K*+*" "B" "Q") )
(defun C:+C () (TQHZ "K*+*" "C" "Q") )
(defun C:+D () (TQHZ "K*+*" "D" "Q") )
(defun C:+E () (TQHZ "K*+*" "E" "Q") )
(defun C:+F () (TQHZ "K*+*" "F" "Q") )
(defun C:+G () (TQHZ "K*+*" "G" "Q") )
(defun C:+H () (TQHZ "K*+*" "H" "Q") )
(defun C:+I () (TQHZ "K*+*" "I" "Q") )
(defun C:+J () (TQHZ "K*+*" "J" "Q") )
(defun C:+L () (TQHZ "K*+*" "L" "Q") )
(defun C:+M () (TQHZ "K*+*" "M" "Q") )
(defun C:+N () (TQHZ "K*+*" "N" "Q") )

以下为答案1:
(foreach n '(1 2 3 4 5 6)
      (eval (list 'defun (read(strcat"L"(itoa n))) nil (list 'setq 'key n)'(princ key) ))
)

以下为答案2:


[*](setq lChars '("E" "F" "G" "H" "I" "J" "L" "M" "N"))

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif(defun funcs_defun (lChars / sChar)

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif(foreach sChar lChars

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif    (eval (list 'defun (read(strcat "C:+" sChar)) nil (list 'TQHZ "K*+*" sChar "Q")))

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif);_for

[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif);_defun



以下为答案3:

[*](defun c:tt()
[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif(defun abc(tx)(eval(read(strcat"(defun C:+"tx"(/)(TQHZ\"K*+*\"\""tx"\"\"Q\"))"))))
[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif(mapcar 'abc '("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "L" "M" "N"))
[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif(princ)
[*]http://bbs.mjtd.com/source/plugin/imc_colorcode/images/jssc_none.gif)



liuhe 发表于 2023-3-15 09:01:32

本帖最后由 liuhe 于 2023-3-15 09:04 编辑


(setq num 65 i 23)
(repeat        i
(eval        (read (strcat "(defun-q""\tc:+"(chr NUM)"(/) (TQHZ"
                     "\"K*+*\""
                      (vl-prin1-to-string(chr NUM))
                      "\"Q\""
                      "))"
              )
        )
)
(SETQ I (1+ I))
)
(defun-q-list-ref 'C:+A )
defun-q是用来测试查看函数定义的,测试完成成后,改成defun别人就看不到定义了。

求助,如何批量定义函数 - AutoLISP/Visual LISP 编程技术 - AutoCAD论坛 - 明经CAD社区 - Powered by Discuz! (mjtd.com)

cchessbd 发表于 2023-3-15 09:20:34

liuhe 发表于 2023-3-15 09:01
(setq num 65 i 23)
(repeat        i
(eval        (read (strcat "(defun-q""\tc:+"(chr NUM)"(/) (TQHZ"


谢谢您!但是您可能没注意,+K是没有定义的。

我想要的是这样的
(foreach n '(1 2 3 4 5 6)
      (eval (list 'defun (read(strcat"L"(itoa n))) nil (list 'setq 'key n)'(princ key) ))
)

就是我把 表里面替换成 "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "L" "M" "N" ,没有“K"

229096767 发表于 2023-3-15 09:39:09

(setq lst '("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "L" "M" "N"))
(foreach item lst
(eval
    (list 'defun-q
          (read (strcat "c:+" item))
          nil
          (list 'TQHZ "K*+*" item "Q")
    )
)
)
;抄了前面的:菜卷鱼

菜卷鱼 发表于 2023-3-15 09:07:30

(defun c:tt (/ s)
(setq _ascii 65)
(repeat 13
    (setq s (chr _ascii))
    (eval (list        'defun-q
                (read (strcat "c:+" s))
                nil
                (list 'TQHZ "K*+*" s "Q")
          )
    )
    (setq _ascii (1+ _ascii))
)
)

liuhe 发表于 2023-3-15 09:09:02

菜卷鱼 发表于 2023-3-15 09:07
(defun c:tt (/ s)
(setq _ascii 65)
(repeat 13


表还可以这样用 想学习了。我以前只是玩字符串

e2002 发表于 2023-3-15 09:58:15

本帖最后由 e2002 于 2023-3-15 10:00 编辑

(setq lChars '("E" "F" "G" "H" "I" "J" "L" "M" "N"))
(defun funcs_defun (lChars / sChar)
(foreach sChar lChars
    (eval (list 'defun (read(strcat "C:+" sChar)) nil (list 'TQHZ "K*+*" sChar "Q")))
);_for
);_defun


都是一个套路哈...:D-

cchessbd 发表于 2023-3-15 10:15:36

谢谢以上各位高手的热心回复。太感谢了!

主要是最近遇到的问题太多了。自己能力又很不够,没法一一解决。

e2002 发表于 2023-3-15 10:39:15

eval 这一句,如果换成 apply ,该怎么写呢?

cchessbd 发表于 2023-3-15 10:45:48

e2002 发表于 2023-3-15 10:39
eval 这一句,如果换成 apply ,该怎么写呢?

我也搞不懂。尤其是mapcar,lamda
再加上混合了defun,list,eval,read的话,把我要绕晕。
页: [1] 2
查看完整版本: 求批量定义函数?