(defun c:GT ()
(setq CL (getvar "CLAYER"))
(setq MFR "GLASTENDER")
(setq MODEL "CBA-36L")
(setq ORN (getstring "Enter Orientation :"))
(setq MFRMODEL (strcat MFR "_" MODEL "_" ORN))
(lambda ( nbnm obnm nlyr olyr cmd )
(cond
((not (tblsearch "LAYER" nlyr)) (princ (strcat "\nLayer \"" nlyr "\" does not exist.")))
((not olyr) (princ "\nInvalid \"CL\" symbol.") )
((not (tblsearch "LAYER" olyr)) (princ (strcat "\nLayer \"" olyr "\" does not exist.")))
(
(setvar 'cmdecho 0)(setvar 'clayer nlyr)
(cond
((not nbnm) (princ "\nInvalid \"MFRMODEL\" symbol.") )
((tblsearch "BLOCK" nbnm) (not (command "_.INSERT" nbnm "\\" "1" "1" "\\")) )
((not obnm) (princ "\nInvalid \"ORN\" symbol.") )
((not (tblsearch "BLOCK" obnm)) (princ (strcat "\nBlock \"" obnm "\" does not exist.")) )
(T (not (command "_.INSERT" obnm "\\" "1" "1" "\\")) (command "_.RENAME" "block" obnm nbnm) )
)
(setvar 'clayer olyr)(setvar 'cmdecho cmd)
)
)
(princ)
)
(if (and (eq 'STR (type MFRMODEL)) (snvalid MFRMODEL)) MFRMODEL)
(cdr (assoc ORN '(("PLAN" . "GTC005P") ("ELEV" . "GTC005E") ("SIDE" . "GTJ011S") ("3D" . "GTC0053"))))
"QF-EQPM"
(if (and (eq 'STR (type CL)) (snvalid CL)) CL)
(getvar 'cmdecho)
)
代码中的问题是,在为ORN符号设置引号时,使用了getstring而不是getkword。
此外,还必须使用initget预定义getkword的选项。
另一个非常重要的问题是你没有本地化你的变量。
因此,使用创建基本例程的经典方法简单地开始:
(defun c:GT ( / CL MFR MODEL ORN MFRMODEL ) ; Localise your variables (important)
; Declare the initial variables:
(setq CL (getvar "CLAYER"))
(setq MFR "GLASTENDER")
(setq MODEL "CBA-36L")
(initget "Plan Elev Side 3D")
(if (setq ORN (getkword "Enter Orientation :")) ; get user input(s)
(progn ; if the user input(s) are correct, proceed...
(setq MFRMODEL (strcat MFR "_" MODEL "_" ORN))
; ...
); progn
); if
); defun
就像我的国家说的那样:我认为你是“把马车放在马的前面”。
此外,FWIW注意到setq函数的命名是因为它是以下任一函数的收缩:
(set (quote var) value)
(set 'var value)
因此,该函数不是“设置引号”,而是为符号赋值(引号/撇号只是为了防止第一个参数被计算,从而作为符号提供)。
李 谢谢李,
我知道setq函数为什么这样命名。
事实上,我解释得很糟糕(解释东西不是我最好的方式)。
为了我自己的理解,我将详细说明(如果不是为任何人,至少为我自己)指定的符号成为代码变量:
_$ (boundp 'a)
nil
_$ (set 'a "something")
"something"
_$ (boundp 'a)
T
_$
至少我将变量分类如下:
[列表]
[*]代码变量-(set/setq中使用的符号)
[*]系统变量-(setvar/getvar中使用的符号,即:“osmode”cmdecho)
[/列表]
我从中得出的结论是,引号/撇号用于取消评估内容,基本上两者(引号/撇号)没有任何不同:
_$ (mapcar
(quote (lambda (x) (strcat (strcase x) (strcase x t))))
(quote ("A" "B" "C" "D" "E"))
)
("Aa" "Bb" "Cc" "Dd" "Ee")
_$
我真的很感谢你的意见! 我尝试将代码与建议一起使用,但在选择了ORN变量后,代码仍会暂停。
(defun c:GT ( / CL MFR MODEL ORN MFRMODEL ) ; Localise your variables (important)
; Declare the initial variables:
(setq CL (getvar "CLAYER"))
(setq MFR "GLASTENDER")
(setq MODEL "CBA-36L")
(initget "Plan Elev Side 3D")
(if (setq ORN (getkword "Enter Orientation :")) ; get user input(s)
(progn ; if the user input(s) are correct, proceed...
(setq MFRMODEL (strcat MFR "_" MODEL "_" ORN))
; ...
); progn
); if
(lambda ( nbnm obnm nlyr olyr cmd )
(cond
( (not (tblsearch "LAYER" nlyr)) (princ (strcat "\nLayer \"" nlyr "\" does not exist.")) )
( (not olyr) (princ "\nInvalid \"CL\" symbol.") )
( (not (tblsearch "LAYER" olyr)) (princ (strcat "\nLayer \"" olyr "\" does not exist.")) )
(
(setvar 'cmdecho 0)(setvar 'clayer nlyr)
(cond
( (not nbnm) (princ "\nInvalid \"MFRMODEL\" symbol.") )
( (tblsearch "BLOCK" nbnm) (not (command "_.INSERT" nbnm "\\" "1" "1" "\\")) )
( (not obnm) (princ "\nInvalid \"ORN\" symbol.") )
( (not (tblsearch "BLOCK" obnm)) (princ (strcat "\nBlock \"" obnm "\" does not exist.")) )
(T (not (command "_.INSERT" obnm "\\" "1" "1" "\\")) (command "_.RENAME" "block" obnm nbnm) )
)
(setvar 'clayer olyr)(setvar 'cmdecho cmd)
)
)
(princ)
)
(if (and (eq 'STR (type MFRMODEL)) (snvalid MFRMODEL)) MFRMODEL)
(cdr (assoc ORN '(("PLAN" . "GTC005P") ("ELEV" . "GTC005E") ("SIDE" . "GTJ011S") ("3D" . "GTC0053"))))
"QF-EQPM"
(if (and (eq 'STR (type CL)) (snvalid CL)) CL)
(getvar 'cmdecho)
)
你看过代码中的注释了吗?
这个lambda必须在progn块中使用,您只是将它复制到我提出的基本结构下面,并删除了语法,因此它不会进行计算。
无论如何FWIW:
(defun C:GT ( / MFR MODEL nlyr olyr cmd vb MFRMODEL ) ; always localise your variables (unless you know what you're doing)
(if ; main (if) function: (if <User Inputs> <Code do its stuff> <Else print whats wrong>)
(setq ; Declare the initial variables (assign value to each symbol):
MFR "GLASTENDER"
MODEL "CBA-36L"
nlyr "QF-EQPM"
olyr (getvar 'clayer)
cmd (getvar 'cmdecho)
vb ; get user input (find the orientation and the corresponding block)
(assoc
(progn
(initget "Plan Elev Side 3D")
(getkword "Enter Orientation :")
)
'(("Plan" . "GTC005P") ("Elev" . "GTC005E") ("Side" . "GTJ011S") ("3D" . "GTC0053"))
); assoc
); setq
(progn ; if the user input are correct, proceed...
(setq MFRMODEL (strcat MFR "_" MODEL "_" (strcase (car vb)))) ; assemble the total name
(setvar 'cmdecho 0) (if (tblsearch "LAYER" nlyr) (setvar 'clayer nlyr)) ; temporarily change system variables
(cond
( (tblsearch "BLOCK" MFRMODEL) ; if such block exist
(command "_.INSERT" MFRMODEL "\\" "1" "1" "\\") ; prompt the user to inser it and exit
)
( (tblsearch "BLOCK" (cdr vb)) ; if MFRMODEL block do not exist, check if a corrseponding block exist
(and
(not (command "_.INSERT" (cdr vb) "\\" "1" "1" "\\")) ; if a corresponding block exist, prompt to insert it
(not (command "_.RENAME" "block" (cdr vb) MFRMODEL)) ; if the block is inserted, rename it
)
)
( (princ (strcat "\nBlocks \"" MFRMODEL "\" and \"" (cdr vb) "\" were not found.")) ) ; if none of the blocks were found, display this message
)
(setvar 'clayer olyr)(setvar 'cmdecho cmd) ; restore the system variables
); progn
(princ "\nOrientation was not specified.") ; print what went wrong
); if
(princ) ; exit cleanly
); defun 我把它插到了几个不同的地方,但都没用。只是尝试使用这个版本,它说,块没有找到。它不应该找到重命名的,因为它第一次不存在。但是,它也找不到文件夹中的DWG。
如果块已在图形中,则此操作非常有效。
页:
1
[2]