我用COND重写了代码。此外,最好在尝试访问新文件之前验证用户是否为其提供了名称。
- (defun C:d2 ( / [color=red]DCL NCarta Escala lspdir txtfile fichTXT[/color] )
- (setq DCL (load_dialog "d2")) ;
- (new_dialog "CARTA2" DCL)
- (action_tile "IDCarta" "(setq NCarta (get_tile "IDCarta"))")
- (action_tile "25M" "(setq Escala 25000)")
- (action_tile "10M" "(setq Escala 10000)")
- (action_tile "5M" "(setq Escala 5000)")
- (action_tile "accept" "(done_dialog)") ;
- (action_tile "cancel" "(setq NCarta nil)(done_dialog)")
- (start_dialog) ; Inicia
- (unload_dialog DCL) ;
- ;----------------------------------
- (setq lspdir "E:\\CartasTXT\")
- (setq txtfile (strcat lspdir "notes"))
- [color=red] (if (and NCarta[/color]
- [color=red] Escala[/color]
- [color=red] (setq fichTXT (getfiled "Defina o nome e a localização do ficheiro TXT da carta."[/color]
- [color=red] txtfile "txt" 1)))[/color]
- [color=red] (progn[/color]
- (setq fichTXT (open fichTXT "w"))
- (prin1 (type fichTXT))
- [color=red] (cond[/color]
- ((= Escala 25000)
- (write-line (strcat NCarta "-your choice is 25000, line1") fichTXT)
- (write-line (strcat NCarta "-your choice is 25000, line2") fichTXT)
- (write-line (strcat NCarta "-your choice is 25000, line3") fichTXT)
- ;......an so on
- )
- ((= Escala 10000)
- (write-line (strcat NCarta "-your choice is 10000, line1") fichTXT)
- (write-line (strcat NCarta "-your choice is 10000, line2") fichTXT)
- (write-line (strcat NCarta "-your choice is 10000, line3") fichTXT)
- ;......an so on
- )
- ((= Escala 5000)
- (write-line (strcat NCarta "-your choice is 5000, line1") fichTXT)
- (write-line (strcat NCarta "-your choice is 5000, line2") fichTXT)
- (write-line (strcat NCarta "-your choice is 5000, line3") fichTXT)
- ;......an so on
- )
- [color=red] )[/color]
- [color=red] )[/color]
- [color=red] )[/color]
- (close fichTXT)
- )
如果要使用If,则不要忘记用PROGN封装表达式:
- (if (= Escala 10000)
- [color=red] (progn[/color]
- (write-line (strcat NCarta "-your choice is 10000, line1") fichTXT)
- (write-line (strcat NCarta "-your choice is 10000, line2") fichTXT)
- (write-line (strcat NCarta "-your choice is 10000, line3") fichTXT)
- ;......an so on
- [color=red] )[/color]
- )
此外,我将让CLOSE调用在“条件”代码之外,以确保文件已关闭。
当做
米尔恰 |