相同函数组合使用时如何简化?谢谢~
(defun c:tt ()(defun make_dcl(/ lst_str str file f)
(setq lst_str
'(
"csdcl: dialog "
"{fixed_height = true; label=\"功能测试\"; "
":row{ "
" :boxed_column "
" {fixed_height = true; label=\"序号1\"; "
" :toggle {key = \"cs1\";label = \"填充\";}"
" } "
" :boxed_column "
" {fixed_height = true; label=\"序号2\"; "
" :toggle {key = \"cs2\";label = \"填充\";}"
" } "
" :boxed_column "
" {fixed_height = true; label=\"序号3\"; "
" :toggle {key = \"cs3\";label = \"填充\";}"
" } "
" :boxed_column "
" {fixed_height = true; label=\"序号4\"; "
" :toggle {key = \"cs4\";label = \"填充\";}"
" } "
" :boxed_column "
" {fixed_height = true; label=\"序号5\"; "
" :toggle {key = \"cs5\";label = \"填充\";}"
" } "
" spacer_1; "
" } "
":row{ "
" :button {key = \accept\;is_default = true;fixed_width = true; label = \"确定\";} "
" :button {key = \"cancel\"; is_cancel = true; fixed_width = true; label = \"取消\";} "
" "
" } "
"}"
)
)
(setq file (vl-filename-mktemp "DclTemp.dcl") )
(setq f (open file "w") )
(foreach str lst_str
(princ "\n" f)
(princ str f)
)
(close f)
file
)
(if (= (setq dcl_id (load_dialog (setq dcl_file (make_dcl) ) ) ) 0) (exit) )
(if dcl_file (vl-file-delete dcl_file) )
(if (not (new_dialog "csdcl" dcl_id) ) (exit) )
(if (/= 'str (type cs1) ) (setq cs1 0) )
(if (/= 'str (type cs2) ) (setq cs2 0) )
(if (/= 'str (type cs3) ) (setq cs3 0) )
(if (/= 'str (type cs4) ) (setq cs4 0) )
(if (/= 'str (type cs5) ) (setq cs5 0) )
(action_tile "cs1" "(setq cs1 1)" )
(action_tile "cs2" "(setq cs2 1)" )
(action_tile "cs3" "(setq cs3 1)" )
(action_tile "cs4" "(setq cs4 1)" )
(action_tile "cs5" "(setq cs5 1)" )
(action_tile "accept" "(done_dialog 1)")
(action_tile "cancel" "(done_dialog 0)")
(start_dialog)
(unload_dialog dcl_id)
(setq p0 (getpoint "\n插入点: ")
t1_0 (list (+ (car p0) 200) (cadr p0) )
t1_1 (list (+ (car t1_0) 200) (cadr t1_0) )
t1_2 (list (+ (car t1_0) 200)(+ (cadr t1_0) 200) )
t1_3 (list (car t1_0) (+ (cadr t1_0) 200) )
t2_0 (list (+ (car t1_1) 200) (cadr p0) )
t2_1 (list (+ (car t2_0) 200) (cadr t2_0) )
t2_2 (list (+ (car t2_0) 200)(+ (cadr t2_0) 200) )
t2_3 (list (car t2_0) (+ (cadr t2_0) 200) )
t3_0 (list (+ (car t2_1) 200) (cadr p0) )
t3_1 (list (+ (car t3_0) 200) (cadr t3_0) )
t3_2 (list (+ (car t3_0) 200)(+ (cadr t3_0) 200) )
t3_3 (list (car t3_0) (+ (cadr t3_0) 200) )
t4_0 (list (+ (car t3_1) 200) (cadr p0) )
t4_1 (list (+ (car t4_0) 200) (cadr t4_0) )
t4_2 (list (+ (car t4_0) 200)(+ (cadr t4_0) 200) )
t4_3 (list (car t4_0) (+ (cadr t4_0) 200) )
t5_0 (list (+ (car t4_1) 200) (cadr p0) )
t5_1 (list (+ (car t5_0) 200) (cadr t5_0) )
t5_2 (list (+ (car t5_0) 200)(+ (cadr t5_0) 200) )
t5_3 (list (car t5_0) (+ (cadr t5_0) 200) )
)
(setq
a (list (list t1_0 t1_1 t1_2 t1_3) (list t2_0 t2_1 t2_2 t2_3) (list t3_0 t3_1 t3_2 t3_3) (list t4_0 t4_1 t4_2 t4_3) (list t5_0 t5_1 t5_2 t5_3) )
)
(mapcar '(lambda (a) (entmake (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") (cons 90 (length a) ) (cons 70 1) ) (mapcar '(lambda (pt) (cons 10 pt) ) a) ) ) ) a)
(defun tct1 () (entmake (list '(0 . "HATCH") '(100 . "AcDbEntity") '(67 . 0) '(410 . "Model") '(100 . "AcDbHatch") '(10 0.0 0.0 0.0) '(210 0.0 0.0 1.0) '(2 . "ANSI31") '(70 . 1) '(71 . 0) '(91 . 1) '(92 . 3) '(72 . 0) '(73 . 1) '(93 . 4)
(cons 10 t1_0) (cons 10 t1_1) (cons 10 t1_2) (cons 10 t1_3) '(97 . 0) '(75 . 0) '(76 . 1) '(98 . 0) ) ) )
(defun tct2 () (entmake (list '(0 . "HATCH") '(100 . "AcDbEntity") '(67 . 0) '(410 . "Model") '(100 . "AcDbHatch") '(10 0.0 0.0 0.0) '(210 0.0 0.0 1.0) '(2 . "ANSI31") '(70 . 1) '(71 . 0) '(91 . 1) '(92 . 3) '(72 . 0) '(73 . 1) '(93 . 4)
(cons 10 t2_0) (cons 10 t2_1) (cons 10 t2_2) (cons 10 t2_3) '(97 . 0) '(75 . 0) '(76 . 1) '(98 . 0) ) ) )
(defun tct3 () (entmake (list '(0 . "HATCH") '(100 . "AcDbEntity") '(67 . 0) '(410 . "Model") '(100 . "AcDbHatch") '(10 0.0 0.0 0.0) '(210 0.0 0.0 1.0) '(2 . "ANSI31") '(70 . 1) '(71 . 0) '(91 . 1) '(92 . 3) '(72 . 0) '(73 . 1) '(93 . 4)
(cons 10 t3_0) (cons 10 t3_1) (cons 10 t3_2) (cons 10 t3_3) '(97 . 0) '(75 . 0) '(76 . 1) '(98 . 0) ) ) )
(defun tct4 () (entmake (list '(0 . "HATCH") '(100 . "AcDbEntity") '(67 . 0) '(410 . "Model") '(100 . "AcDbHatch") '(10 0.0 0.0 0.0) '(210 0.0 0.0 1.0) '(2 . "ANSI31") '(70 . 1) '(71 . 0) '(91 . 1) '(92 . 3) '(72 . 0) '(73 . 1) '(93 . 4)
(cons 10 t4_0) (cons 10 t4_1) (cons 10 t4_2) (cons 10 t4_3) '(97 . 0) '(75 . 0) '(76 . 1) '(98 . 0) ) ) )
(defun tct5 () (entmake (list '(0 . "HATCH") '(100 . "AcDbEntity") '(67 . 0) '(410 . "Model") '(100 . "AcDbHatch") '(10 0.0 0.0 0.0) '(210 0.0 0.0 1.0) '(2 . "ANSI31") '(70 . 1) '(71 . 0) '(91 . 1) '(92 . 3) '(72 . 0) '(73 . 1) '(93 . 4)
(cons 10 t5_0) (cons 10 t5_1) (cons 10 t5_2) (cons 10 t5_3) '(97 . 0) '(75 . 0) '(76 . 1) '(98 . 0) ) ) )
(if (= cs1 1) (tct1) )
(if (= cs2 1) (tct2) )
(if (= cs3 1) (tct3) )
(if (= cs4 1) (tct4) )
(if (= cs5 1) (tct5) )
)
)当有多个相同的函数组合在一起使用的时候,请问如何简化?每个都写一遍的话太臃肿了,谢谢~
;把函数写成带参数的函数,调用时给相应的参数,改为
(defun tct (t_0 t_1 t_2 t_3)
(entmake (list '(0 . "HATCH") '(100 . "AcDbEntity") '(67 . 0) '(410 . "Model") '(100 . "AcDbHatch") '(10 0.0 0.0 0.0) '(210 0.0 0.0 1.0) '(2 . "ANSI31") '(70 . 1) '(71 . 0) '(91 . 1) '(92 . 3) '(72 . 0) '(73 . 1) '(93 . 4)
(cons 10 t_0) (cons 10 t_1) (cons 10 t_2) (cons 10 t_3) '(97 . 0) '(75 . 0) '(76 . 1) '(98 . 0) ) )
)
;调用函数:(tct t1_0 t1_1 t1_2 t1_3),(tct t2_0 t2_1 t2_2 t2_3).......... 要么动态编译,
要么换语言后你就学会一个叫map的东西,
就不用那么麻烦想这些事情了. 自定义函数啊,需要的时候就调用 那一堆action_tile可以用mapcar去简化,把那些键值和后面对应的表达式分别弄成2个表,2个表中的键值和表达式要相互对应,再用mapcar函数把2个表中的项交给action_tile去处理
(setq key '("cs1" "cs2" "cs3" "cs4" "cs5" "accept" "cancel")
bds '("(setq cs1 1)" "(setq cs2 1)" "(setq cs3 1)" "(setq cs4 1)" "(setq cs5 1)" "(done_dialog 1)" "
(done_dialog 0)"))
(mapcar 'action_tile key bds)
后面那一堆生成图元的函数,只要写一个、弄成带参数的就行了,给它不同的参数就生成不同的图元。还有那一堆堆的if,像这种需要同时处理多种条件的,可以用cond wosiguwozai0830 发表于 2025-4-11 17:22
;把函数写成带参数的函数,调用时给相应的参数,改为
(defun tct (t_0 t_1 t_2 t_3)
(entmake (list '(0 ...
学到了,谢谢~ 你有种再说一遍 发表于 2025-4-11 21:10
要么动态编译,
要么换语言后你就学会一个叫map的东西,
就不用那么麻烦想这些事情了.
我还是先从简单点的开始入手学习吧,感谢回复~ MZ_li 发表于 2025-4-12 08:49
自定义函数啊,需要的时候就调用
哈哈,还不太会用那个,需要看例子学习,感谢回复~ ljfdjjj 发表于 2025-4-12 09:25
那一堆action_tile可以用mapcar去简化,把那些键值和后面对应的表达式分别弄成2个表,2个表中的键值和表达 ...
因为12345是独立的,也可以多个组合,之前用cond好像也要分多个,在这里和if差不多,当然也可能是我没有正确使用…感谢指点,学习到了~
页:
[1]