mwade93 Posted January 31, 2017 Posted January 31, 2017 (edited) I think I've got this code somewhat close. Before issuing this part of the lisp, I the code sets up the MFRMODEL and ORN variables. What I want to happen is to have the LISP insert the existing block (if available). If that block is not available, select the block to insert based on the previously entered ORN variable and follow the commands. The problem is that this code only works if the block is currently present in the drawing. All of my blocks are located as external DWG files and they are not inserting. Any help would be appreciated. (if (tblsearch "BLOCK" MFRMODEL) (command "layer" "s" "QF-EQPM" "" "insert" MFRMODEL pause "1" "1" pause "setvar" "clayer" CL) ( (cond ((wcmatch ORN "PLAN")(command "layer" "s" "QF-EQPM" "" "insert" "GTC005P" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTC005P" MFRMODEL)) ((wcmatch ORN "ELEV")(command "layer" "s" "QF-EQPM" "" "insert" "GTC005E" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTC005E" MFRMODEL)) ((wcmatch ORN "SIDE")(command "layer" "s" "QF-EQPM" "" "insert" "GTJ011S" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTJ011S" MFRMODEL)) ((wcmatch ORN "3D")(command "layer" "s" "QF-EQPM" "" "insert" "GTC0053" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTC0053" MFRMODEL)) ) ) ) Edited January 31, 2017 by mwade93 Quote
mwade93 Posted January 31, 2017 Author Posted January 31, 2017 I've got it working now for the most part. However, it crashes once it gets to the rename part. Had to put quotation marks around the block names. Quote
David Bethel Posted January 31, 2017 Posted January 31, 2017 RENAME balks when the target block name already exists in the database. Should MFRMODEL be in quotation marks also ? (tblsearch "BLOCK" MFRMODEL) -David Quote
mwade93 Posted January 31, 2017 Author Posted January 31, 2017 It doesn't work if I have those on it. The MFRMODEL is set before the LISP part of the command is issued. The blocks shouldn't be renamed or inserted if they already exist. The block will be inserted in the true part of the tblsearch. Quote
Grrr Posted January 31, 2017 Posted January 31, 2017 (edited) Why you use the additional syntaxes? (if (tblsearch "BLOCK" MFRMODEL) (command "layer" "s" "QF-EQPM" "" "insert" MFRMODEL pause "1" "1" pause "setvar" "clayer" CL) [b][color=red]([/color][/b] (cond ((wcmatch ORN "PLAN")(command "layer" "s" "QF-EQPM" "" "insert" "GTC005P" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTC005P" MFRMODEL)) ((wcmatch ORN "ELEV")(command "layer" "s" "QF-EQPM" "" "insert" "GTC005E" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTC005E" MFRMODEL)) ((wcmatch ORN "SIDE")(command "layer" "s" "QF-EQPM" "" "insert" "GTJ011S" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTJ011S" MFRMODEL)) ((wcmatch ORN "3D")(command "layer" "s" "QF-EQPM" "" "insert" "GTC0053" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTC0053" MFRMODEL)) ) [b][color=red])[/color][/b] ) However try something like this [uNTESTED] (I remember that you don't like setq's for some reason): ( (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) ) P.S. Don't judge me Roy! Edited January 31, 2017 by Grrr Modified the code, alot of typos Quote
mwade93 Posted January 31, 2017 Author Posted January 31, 2017 Thanks for the post Grrr. The second set of quotes is because that is the false statement of the tblsearch. I'm sorry, I have no idea what that second code you posted does. I'm by no means an expert at this. The code I posted took me about a whole day to get to that point. Why you use the additional syntaxes? (if (tblsearch "BLOCK" MFRMODEL) (command "layer" "s" "QF-EQPM" "" "insert" MFRMODEL pause "1" "1" pause "setvar" "clayer" CL) [b][color=red]([/color][/b] (cond ((wcmatch ORN "PLAN")(command "layer" "s" "QF-EQPM" "" "insert" "GTC005P" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTC005P" MFRMODEL)) ((wcmatch ORN "ELEV")(command "layer" "s" "QF-EQPM" "" "insert" "GTC005E" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTC005E" MFRMODEL)) ((wcmatch ORN "SIDE")(command "layer" "s" "QF-EQPM" "" "insert" "GTJ011S" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTJ011S" MFRMODEL)) ((wcmatch ORN "3D")(command "layer" "s" "QF-EQPM" "" "insert" "GTC0053" pause "1" "1" pause "setvar" "clayer" CL "rename" "block" "GTC0053" MFRMODEL)) ) [b][color=red])[/color][/b] ) However try something like this [uNTESTED] (I remember that you don't like setq's for some reason): ( (lambda ( nbnm obnm nlyr olyr cmd ) (cond ( (apply '= (cons 'STR (mapcar 'type (list nlyr olyr)))) (setvar 'cmdecho 0)(setvar 'clayer nlyr) (cond ( nbnm (not (command "_.INSERT" nbnm "\\" "1" "1" "\\")) ) ( (tblsearch "BLOCK" obnm) (not (command "_.INSERT" obnm "\\" "1" "1" "\\")) (command "_.RENAME" "block" obnm nbnm) ) ) (setvar 'clayer olyr)(setvar 'cmdecho cmd) ) ( (not obnm) (princ "\nInvalid \"ORN\" symbol.") ) ( (not (tblsearch "BLOCK" obnm)) (princ "\nBlock with the \"ORN\" symbol was not found.") ) ( (not (tblsearch "BLOCK" obnm)) (princ (strcat "\nBlock \"" obnm "\" does not exist.")) ) ( (not nlyr) (princ "\nLayer \"QF-EQPM\" does not exist.") ) ( (not CL) (princ "\nInvalid \"CL\" symbol, or such layer does not exist.") ) ) (princ) ) (if (and (eq 'STR (type MFRMODEL)) (snvalid MFRMODEL) (tblsearch "BLOCK" MFRMODEL)) MFRMODEL) (cdr (assoc ORN '(("PLAN" . "GTC005P") ("ELEV" . "GTC005E") ("SIDE" . "GTJ011S") ("3D" . "GTC0053")))) (if (tblsearch "LAYER" "QF-EQPM") "QF-EQPM") (if (tblsearch "LAYER" CL) CL) (getvar 'cmdecho) ) P.S. Don't judge me Roy! Quote
Grrr Posted January 31, 2017 Posted January 31, 2017 (edited) The second set of quotes is because that is the false statement of the tblsearch. You should get the Error: bad function: ... if the test expression from the if function is false (I mean nil). I'm sorry, I have no idea what that second code you posted does. Should behave the same as yours, waiting for the MFRMODEL ORN CL symbol inputs and run the "INSERT" command, else point out what was the invalid input. Edited January 31, 2017 by Grrr Quote
mwade93 Posted January 31, 2017 Author Posted January 31, 2017 It seems to run fine. Thank You Grrr I have some other models, but I think I see where to change the code. It is just on that one line that I would need to change right? Line that is listed as: cdr (assoc ORN '(("PLAN" . "GTC005P") ("ELEV" . "GTC005E") ("SIDE" . "GTJ011S") ("3D" . "GTC0053")))) Quote
Grrr Posted January 31, 2017 Posted January 31, 2017 It seems to run fine. Thank You Grrr I have some other models, but I think I see where to change the code. It is just on that one line that I would need to change right? Line that is listed as: cdr (assoc ORN '(("PLAN" . "GTC005P") ("ELEV" . "GTC005E") ("SIDE" . "GTJ011S") ("3D" . "GTC0053")))) Yes, this assoc list kinda substitutes the cond you used. BTW I've modified the code in my post (had alot of typos). Quote
mwade93 Posted January 31, 2017 Author Posted January 31, 2017 Going to have to look into that a little bit. Just haven't had the motivation to dig into this too deep. Thanks for the help Quote
mwade93 Posted January 31, 2017 Author Posted January 31, 2017 So if I wanted to add in setting variables into this where/how would I do it? I took a shot, but the code stops after setting the ORN variable. (defun c:GT () (setq CL (getvar "CLAYER")) (setq MFR "GLASTENDER") (setq MODEL "CBA-36L") (setq ORN (getstring "Enter Orientation [PLAN / ELEV / SIDE / 3D]:")) (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) ) Quote
Grrr Posted January 31, 2017 Posted January 31, 2017 (edited) So if I wanted to add in setting variables into this where/how would I do it? I took a shot, but the code stops after setting the ORN variable. The problem in your code is that you used getstring instead of getkword, when setting quote for the ORN symbol. Also you have to use initget to predefine the options for the getkword. Another very important problem is that you didn't localise your variables. So to start simple using the classical way of creating a basic routine: (defun c:GT ( / CL MFR MODEL ORN MFRMODEL ) [color="green"]; Localise your variables (important)[/color] [color="green"] ; Declare the initial variables:[/color] (setq CL (getvar "CLAYER")) (setq MFR "GLASTENDER") (setq MODEL "CBA-36L") (initget "Plan Elev Side 3D") (if (setq ORN (getkword "Enter Orientation [Plan/Elev/Side/3D]:")) [color="green"]; get user input(s)[/color] (progn [color="green"]; if the user input(s) are correct, proceed...[/color] (setq MFRMODEL (strcat MFR "_" MODEL "_" ORN)) ; ... )[color="green"]; progn[/color] )[color="green"]; if[/color] )[color="green"]; defun [/color] Like from my country say: I think that you're "placing the waggon infront of the horse". Edited February 2, 2017 by Grrr Quote
Lee Mac Posted February 1, 2017 Posted February 1, 2017 ...when setting quote for the ORN symbol. Aside, FWIW note that the setq function is named as such because it is a contraction of either of: (set (quote var) value) (set 'var value) As such, the function is not 'setting a quote', but rather assigning a value to a symbol (the quote/apostrophe is merely to prevent the first argument being evaluated and hence supplied as a symbol). Lee Quote
Grrr Posted February 1, 2017 Posted February 1, 2017 Thanks Lee, I was aware of why setq function is named like that. Indeed I poorly explained (explaining stuff is not my best). As such, the function is not 'setting a quote', but rather assigning a value to a symbol For my own understanding I would elaborate (if not for anyone, atleast for myself) that the assigned symbol becomes a code variable: _$ (boundp 'a) nil _$ (set 'a "something") "something" _$ (boundp 'a) T _$ Atleast I categorize the variables like this: code variables - (symbols used within set/setq) system variables - (symbols used within setvar/getvar, i.e.: 'osmode 'cmdecho) (the quote/apostrophe is merely to prevent the first argument being evaluated and hence supplied as a symbol) The conclusion I'm doing from this is that the quote/apostrophe is used to UNevaluate stuff, and basically both (quote/apostrophe) doesn't differ in any way: _$ (mapcar (quote (lambda (x) (strcat (strcase x) (strcase x t)))) (quote ("A" "B" "C" "D" "E")) ) ("Aa" "Bb" "Cc" "Dd" "Ee") _$ I really appreciate your input! Quote
mwade93 Posted February 1, 2017 Author Posted February 1, 2017 I tried using the code with the suggestion, but it still pauses after selected the ORN variable. (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 [PLAN/ELEV/SIDE/3D]:")) ; 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) ) The problem in your code is that you used getstring instead of getkword, when setting quote for the ORN symbol.Also you have to use initget to predefine the options for the getkword. Another very important problem is that you didn't localise your variables. So to start simple using the classical way of creating a basic routine: (defun c:GT ( / CL MFR MODEL ORN MFRMODEL ) [color="green"]; Localise your variables (important)[/color] [color="green"] ; Declare the initial variables:[/color] (setq CL (getvar "CLAYER")) (setq MFR "GLASTENDER") (setq MODEL "CBA-36L") (initget "Plan Elev Side 3D") (if (setq ORN (getkword "Enter Orientation [Plan/Elev/Side/3D]:")) [color="green"]; get user input(s)[/color] (progn [color="green"]; if the user input(s) are correct, proceed...[/color] (setq MFRMODEL (strcat MFR "_" MODEL "_" ORN)) ; ... )[color="green"]; progn[/color] )[color="green"]; if[/color] )[color="green"]; defun [/color] Like from my country say: I think that you're "placing the horse infront of the waggon". Quote
Grrr Posted February 2, 2017 Posted February 2, 2017 Did you read the comments in the code? This lambda must be used inside the progn block, you just copied it below the basic structure I proposed and removed the syntaxes, so it won't evaluate. Anyway 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/Elev/Side/3D]:") ) '(("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 Quote
mwade93 Posted February 2, 2017 Author Posted February 2, 2017 (edited) I tired inserting it in a couple of different places, nothing worked. Just tried to use this version and it says that the block is not found. It shouldn't find the renamed because it doesn't exist the first time. However, it is also not finding the DWG located in the folder. This works perfectly if the block is already in the drawing. Did you read the comments in the code? This lambda must be used inside the progn block, you just copied it below the basic structure I proposed and removed the syntaxes, so it won't evaluate. Anyway 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/Elev/Side/3D]:") ) '(("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 Edited February 2, 2017 by mwade93 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.