子定义和局部变量B
我已经创建了一个包含DCL文件的lisp&我希望将其用作任何未来将受益于切换界面的lisp的模板。它起作用了,有点。。
这真的是两个问题合一。
1) 主功能内部或外部的子定义?
(defun getvalues()和/或(defun create_dialog()应该在里面吗?还是在外面?
2) 在多定义LISP中定位变量。
每个defun应该定位哪些变量?
我的代码:
(defun c:main ( / an ans1 ans2 ans3 ans4 dcl_id fn fname)
(vl-load-com)
(create_dialog)
(setq dcl_id (load_dialog fname))
(if (not (new_dialog "temp" dcl_id))
(exit)
(progn
(action_tile "accept" "(getValues)")
(action_tile "cancel" "(done_dialog 0)")
(setq an (start_dialog))
) ;end progn
) ;end if
(unload_dialog dcl_id)
(cond ((= an 1)
(alert (strcat "Layer to 0 is: < " ans1 " > \nLTscale to 1 is: < " ans2 " > \nColour to Bylayer is: < " ans3 " > \nLType to Bylayer is: < " ans4 " >"))
)
)
(vl-file-delete fname)
(princ)
);defun c:test
(defun getvalues ()
(setq ans1 (get_tile "t1")
ans2 (get_tile "t2")
ans3 (get_tile "t3")
ans4 (get_tile "t4"))
(done_dialog 1)
); defun getvalues
(defun create_dialog ()
(setq fname (vl-filename-mktemp "dcl.dcl"))
(setq fn (open fname "w"))
(write-line
"temp :dialog { label = \"Set Sub-Entities to:\" ;
:toggle{
label = \"Layer to 0\" ;
alignment = centered;
key = t1;
value =1;
}
:toggle{
label = \"LTscale to 1\" ;
alignment = centered;
key = t2;
value =1;
}
:toggle{
label = \"Colour to Bylayer\" ;
alignment = centered;
key = t3;
value =1;
}
:toggle{
label = \"LType to Bylayer\" ;
alignment =centered;
key = t4;
value =1;
}
ok_cancel;
} " fn)
(close fn)
);defun create_dialog 你好,Simon1976,
我只是尝试在我的主函数中添加subdefun,它工作得很好。
通常,子函数(也称为“子函数”)位于主函数之外,因此可以缩短主代码,并且相同的子函数可以轻松地在其他新代码中重用。
还下面是一个示例,如果subdefun位于主函数内,它将如何工作:
(defun C:main ( / args )
(defun subfunction1)
(defun subfunction2)
; Start the main code:
(if
(and
(stuff)
);and
(progn
(stuff)
);progn
);if
);main defun
下面是一个例子,您会得到错误“无法加载xxx.lsp”
(defun C:main ( / args )
; Start the main code:
(if
(and
(stuff)
);and
(progn
(defun subfunction1)
(defun subfunction2)
(stuff)
);progn
);if
);main defun
据我所知,用户输入的变量必须本地化(以及其他与这些变量有联系的人等等),否则当例程结束时,它们不会重置为零。
通常我会尝试在每段代码中本地化所有变量(我不确定这样做是否正确),但我记得取消变量本地化的唯一原因是为了记住用户的输入:
;Remembers selected choice
;LM Dynamic Default
(defun C:test ()
(if (not *ans*) (setq *ans* "A4"))
(initget "A0 A1 A2 A3 A4")
(setq *ans* (cond ( (getkword (strcat "\nSelect option <" *ans* ">: "))) ( *ans* )))
(cond
((= *ans* "A0") (princ (strcat "\nFinal choice is " *ans* )))
((= *ans* "A1") (princ (strcat "\nFinal choice is " *ans* )))
((= *ans* "A2") (princ (strcat "\nFinal choice is " *ans* )))
((= *ans* "A3") (princ (strcat "\nFinal choice is " *ans* )))
((= *ans* "A4") (princ (strcat "\nFinal choice is " *ans* )))
)
(princ)
);defun 嗨,西蒙,
本地化功能被认为是最好的方式,除非你在另一个程序中使用相同的功能,并将其放入你的ACADDOC。lsp文件。
在您的示例中,我建议对函数进行本地化,并确保在调用函数之前将函数放置在程序中,以确保函数已加载,并避免整个程序出现任何故障。
祝你好运
或者,只要您确定全局变量*ans*持有一个值并且不等于零。
(princ (strcat "\nFinal choice is " (car (member *ans* '("A0" "A1" "A2" "A3" "A4")))))
不幸的是,我认为你们都没有抓住要点(可能解释得很糟糕),我不想要任何全局变量
第一季度:
如果是:
(defun main
code
.......
.......
(defun getvalues); This is INSIDE main
code
........
........
);end defun getvalues
);end defun main
OR:
(defun main
code
.......
.......
);end defun main
(defun getvalues); This is OUTSIDE main
code
........
........
);end defun getvalues
Q2:
Should main be:
(defun c:main ( / an ans1 ans2 ans3 ans4 dcl_id fn fname); All declared as variables
or
(defun c:main (ans1 ans2 ans3 ans4 / an dcl_id fn fname); ans1 - ans4 declared as arguements, an dcl_id fn fname declared as variables
And should getvalues be:
(defun getvalues ()
or
(defun getvalues ( ans1 ans2 ans3 ans4 / ); all declared as arguments[/颜色]
很抱歉,代码标签按钮似乎对我不起作用&我最终手动将它们放入 西蒙,请修改你的帖子并使用标签代码,看看这个
Simon上述回复直接发送给Grrr。
这就是我的意思:
(defun c:main ( / getvalues)
(defun getvalues (/ a b c)
(setq a 1
b 2
c 3
)
(+ a b c)
)
(alert (itoa (getvalues)))
(princ)
)
因此,上述操作应该有效,并在消息框中给出6的总数。
以及以下函数getvalues未本地化且位于主函数之外的函数。
(defun c:main ( / )
(alert (itoa (getvalues)))
(princ)
)
(defun getvalues (/ a b c)
(setq a 1
b 2
c 3
)
(+ a b c)
)
最后一个应该给出一个错误,因为函数SumValues之前没有加载,所以在加载之前调用它。
(defun c:main ( / )
(alert (itoa (SumValues)))
(defun SumValues (/ a b c)
(setq a 1
b 2
c 3
)
(+ a b c)
)
(princ)
)
错误消息: Simon上面的回复是直接发给Grrr的“哦。
我正在努力理解这一点,非常感谢塔瓦。 非常欢迎你,西蒙。
如果你有任何问题,尽管问。 谢谢,塔瓦!
你可以看到,我试图避免使用列表,但这是一个非常古老的例子。
至少在我开始学习LISP时,我曾经用这种方式操作设置好的引号。
我知道这是一种不好的方式,但我正在慢慢学习如何转换代码,使其包含列表并进行操作。
我很惊讶你怎么把它缩短成这样: 您好,Grrr,
列表处理和编程不能仅仅从学习中学习,你需要在实践中学习,并在例程中犯下错误,你会逐渐熟悉函数,并获得最佳性能。
正如我所见,你是AutoLISP论坛的优秀追随者,因此,试着编写代码,参与你认为能够回复或至少能够提供有用信息的每一个线程,即使其他用户编写了完整的例程。
如果你想成为一名合格的Lisper,请继续编码。
页:
[1]