denverado Posted June 13, 2013 Posted June 13, 2013 (edited) HI all!! I have written a LISP routine to change certain text. However.... if one of the items that I want to change does not exist in the drawings the lisp routine doesn't work. Can some one tell me where I have gone wrong? My work computer wont allow me to add the file so this is a portion of the file.... (defun C:GHJ ; = Text Replace for Complete text/mtext strings, pre-defined content (/ tss tdata) (setq tss (ssget "X" (list (cons 1 "*0*VERTICAL BEND")))) (repeat (sslength tss) (setq tdata (entget (ssname tss 0)) tdata (subst (cons 1 "JOINT DEFLECTION") (assoc 1 tdata) tdata) ); end setq (entmod tdata) (ssdel (ssname tss 0) tss) ); end repeat (setq tss (ssget "X" (list (cons 1 "*4*VERTICAL BEND")))) (repeat (sslength tss) (setq tdata (entget (ssname tss 0)) tdata (subst (cons 1 "2 JOINT DEFLECTIONs") (assoc 1 tdata) tdata) ); end setq (entmod tdata) (ssdel (ssname tss 0) tss) ); end repeat ); end defun If the first instance isn't met, the second instance wont work. Edited June 18, 2013 by SLW210 Quote
BlackBox Posted June 13, 2013 Posted June 13, 2013 If the first instance isn't met, the second instance wont work. That's because you're not using any code logic to evaluate if a dependent criteria has been met prior to processing... Consider this example: (defun c:FOO (/ ss i eData asso) (if (setq ss (ssget "_x" '((1 . "*0*VERTICAL BEND,*4*VERTICAL BEND")))) (repeat (setq i (sslength ss)) (entmod (subst (cons 1 (if (wcmatch (strcase (cdr (setq asso (assoc 1 (setq eData (entget (ssname ss (setq i (1- i))) ) ) ) ) ) ) "*0*VERTICAL BEND" ) "JOINT DEFLECTION" "2 JOINT DEFLECTIONs" ) ) asso eData ) ) ) ) (princ) ) Also worthy of note, is that DXF 1 is case sensitive, so be sure to account for that as well. Quote
Lee Mac Posted June 13, 2013 Posted June 13, 2013 In addition to BlackBox's suggestions, I would recommend the following: ([color=BLUE]defun[/color] c:ghj ( [color=BLUE]/[/color] enx idx sel str ) ([color=BLUE]if[/color] ([color=BLUE]setq[/color] sel ([color=BLUE]ssget[/color] [color=MAROON]"_X"[/color] '((0 . [color=MAROON]"TEXT,MTEXT"[/color]) (1 . [color=MAROON]"*[04]*VERTICAL BEND"[/color])))) ([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] idx ([color=BLUE]sslength[/color] sel)) ([color=BLUE]setq[/color] enx ([color=BLUE]entget[/color] ([color=BLUE]ssname[/color] sel ([color=BLUE]setq[/color] idx ([color=BLUE]1-[/color] idx)))) str ([color=BLUE]assoc[/color] 1 enx) ) ([color=BLUE]entmod[/color] ([color=BLUE]subst[/color] ([color=BLUE]if[/color] ([color=BLUE]wcmatch[/color] ([color=BLUE]strcase[/color] ([color=BLUE]cdr[/color] str)) [color=MAROON]"*0*"[/color]) '(1 . [color=MAROON]"JOINT DEFLECTION"[/color]) '(1 . [color=MAROON]"2 JOINT DEFLECTIONs"[/color]) ) str enx ) ) ) ) ([color=BLUE]princ[/color]) ) Quote
denverado Posted June 13, 2013 Author Posted June 13, 2013 Thanks guys that works great. Would I need to add additional if commands for more instances? e.g. "0*VERTICAL BEND" => "JOINT DEFLECTION" "-0*VERTICAL BEND" => "JOINT DEFLECTION" "1*VERTICAL BEND" => "JOINT DEFLECTION" "-1*VERTICAL BEND" => "JOINT DEFLECTION" "2*VERTICAL BEND" => "JOINT DEFLECTION" "-2*VERTICAL BEND" => "JOINT DEFLECTION" "3*VERTICAL BEND" => "JOINT DEFLECTION" "-3*VERTICAL BEND" => "JOINT DEFLECTION" "4*VERTICAL BEND" => "2 JOINT DEFLECTIONS" "-4*VERTICAL BEND" => "2 JOINT DEFLECTIONS" "5*VERTICAL BEND" => "2 JOINT DEFLECTIONS" "-5*VERTICAL BEND" => "2 JOINT DEFLECTIONS" "6*VERTICAL BEND" => "2 JOINT DEFLECTIONS" "-6*VERTICAL BEND" => "2 JOINT DEFLECTIONS" "7*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "-7*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "8*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "-8*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "9*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "-9*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "10*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "-10*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "11*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "-11*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "12*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "-12*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "13*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "-13*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "14*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "-14*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "15*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "-15*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "16*VERTICAL BEND" => "11.25%%D VERTICAL BEND" "-16*VERTICAL BEND" => "11.25%%D VERTICAL BEND" I can change the colour of the "11.25%%D VERTICAL BEND" by the following at the end of the lisp routine. (setq tss (ssget "X" (list (cons 1 "11.25%%D VERTICAL BEND")))) (repeat (sslength tss) (command "chprop" tss "" "c" "6" "") (entmod tdata) (ssdel (ssname tss 0) tss) ); end repeat Quote
BlackBox Posted June 14, 2013 Posted June 14, 2013 ([color=BLUE]if[/color] ([color=BLUE]setq[/color] sel ([color=BLUE]ssget[/color] [color=MAROON]"_X"[/color] '((0 . [color=MAROON]"TEXT,MTEXT"[/color]) (1 . [color=MAROON]"*[04]*VERTICAL BEND"[/color])))) Doh!... I forgot about []. Also, why not: (entmod (subst (cons 1 (if (wcmatch (cdr str) "*0*") "JOINT DEFLECTION" "2 JOINT DEFLECTIONs" ) ) str enx ) ) Quote
pBe Posted June 14, 2013 Posted June 14, 2013 (edited) Whats the character after "1" "1*VERTICAL BEND" You realize there will be complications with wcmatch when differentiating between this two. "1*VERTICAL BEND" "15*VERTICAL BEND" Unless of course we start from the highest number [in this case 16] (defun c:ghj (/ enx idx sel str [color="blue"]f n _Str[/color]) (if (setq sel (ssget "_X" '((0 . "TEXT,MTEXT") (1 . "*VERTICAL BEND")))) (repeat (setq idx (sslength sel)) (setq enx (entget (ssname sel (setq idx (1- idx)))) str (assoc 1 enx) [color="blue"] _str (Cdr str)[/color]) [color="blue"] (setq f nil n 16) (while (and (not f) (> n -1)) (setq f (vl-some '(lambda (k) (if (wcmatch _str (strcat k "*VERTICAL BEND")) (atoi k))) (list (itoa n) (if (zerop n) "-0" (itoa (- n)))))) (setq n (1- n)) ) (if (setq nstr (cond ((or (<= 0 f 6) (<= -6 f -1)) "2 JOINT DEFLECTIONS") ((or (<= 7 f 16) (<= -16 f -7)) "11.25%%D VERTICAL BEND")))[/color] (entmod (subst (cons 1 nstr) str enx ) ) ) ) ) (princ) ) Edited June 14, 2013 by pBe Quote
Lee Mac Posted June 14, 2013 Posted June 14, 2013 Also, why not: (entmod (subst (cons 1 (if (wcmatch (cdr str) "*0*") "JOINT DEFLECTION" "2 JOINT DEFLECTIONs" ) ) str enx ) ) Because the cons expression is unnecessary. Quote
BlackBox Posted June 14, 2013 Posted June 14, 2013 Because the cons expression is unnecessary. ... So too is the STRCASE to match *0* (in your original code), no? The CONS may be less keystrokes, but perhaps there's a minor performance hit vs. the duplicate QUOTEd pairs. *shrug* Quote
Lee Mac Posted June 14, 2013 Posted June 14, 2013 ... So too is the STRCASE to match *0* (in your original code), no? True - the strcase is indeed unnecessary for this case. The CONS may be less keystrokes, but perhaps there's a minor performance hit vs. the duplicate QUOTEd pairs. *shrug* Only one of the quoted pairs will be evaluated depending on result of the test expression. Quote
SLW210 Posted June 14, 2013 Posted June 14, 2013 Please read the Code posting guidelines and edit your post to include the Code in Code Tags. Quote
BlackBox Posted June 14, 2013 Posted June 14, 2013 Only one of the quoted pairs will be evaluated depending on result of the test expression. No... I meant my performance (fat-finger typing). Quote
denverado Posted June 17, 2013 Author Posted June 17, 2013 Thanks for the help. It worked perfectly until I added a few more parameters. Now I get an error when I try to load it. (defun c:ghj (/ enx idx sel str f n _Str) (if (setq sel (ssget "_X" '((0 . "TEXT,MTEXT") (1 . "*VERTICAL BEND")))) (repeat (setq idx (sslength sel)) (setq enx (entget (ssname sel (setq idx (1- idx)))) str (assoc 1 enx) _str (Cdr str)) (setq f nil n 16) (while (and (not f) (> n -1)) (setq f (vl-some '(lambda (k) (if (wcmatch _str (strcat k "*VERTICAL BEND")) (atoi k))) (list (itoa n) (if (zerop n) "-0" (itoa (- n)))))) (setq n (1- n)) ) (if (setq nstr (cond ((or (<= 0 f 3) (<= -3 f -1)) "JOINT DEFLECTION") ((or (<= 4 f 6) (<= -6 f -4)) "2 JOINT DEFLECTIONS") ((or (<= 7 f 16) (<= -16 f -7)) "11.25%%D VERTICAL BEND"))) ((or (<= 17 f 28) (<= -28 f -17)) "22.5%%D VERTICAL BEND"))) ((or (<=39 f 51) (<= -51 f -39)) "45%%D VERTICAL BEND"))) (entmod (subst (cons 1 nstr) str enx ) ) ) ) ) (princ) (setq tss (ssget "X" (list (cons 1 "*VERTICAL BEND")))) (repeat (sslength tss) (command "chprop" tss "" "c" "6" "") (entmod tdata) (ssdel (ssname tss 0) tss) ); end repeat ) Quote
pBe Posted June 18, 2013 Posted June 18, 2013 (edited) Denverado. I have to say, you only replied when you hit a snag otherwise we would've never known how helpful we are here. Anyhoo. initial observation is too many closing parenthesis on the cond line. (setq nstr (cond ((or (<= 0 f 3) (<= -3 f -1)) "JOINT DEFLECTION") ((or (<= 4 f 6) (<= -6 f -4)) "2 JOINT DEFLECTIONS") ((or (<= 7 f 16) (<= -16 f -7)) "11.25%%D VERTICAL BEND") ((or (<= 17 f 28) (<= -28 f -17)) "22.5%%D VERTICAL BEND") ((or (<=39 f 51) (<= -51 f -39)) "45%%D VERTICAL BEND")) ) Also (setq f nil n [b][color="blue"]51[/color][/b]) Not sure but try it and tell us how it goes. or better yet post a sample drawing. Edited June 18, 2013 by pBe Quote
Tharwat Posted June 18, 2013 Posted June 18, 2013 Anyhoo. initial observation is too many closing parenthesis on the cond line. (<[b]=39[/b] f 51) One more issue , the space between the equal operator and the number 39 Quote
pBe Posted June 18, 2013 Posted June 18, 2013 One more issue , the space between the equal operator and the number 39 guess you are right tharwat.. still not sure if it will work. Am too lazy to debug and try the code. Cheers Tharwat Quote
denverado Posted June 19, 2013 Author Posted June 19, 2013 Thanks everyone for your help. I was on holidays.... that's why I never responded until I did. You have all been very helpful. Quote
pBe Posted June 19, 2013 Posted June 19, 2013 Thanks everyone for your help.I was on holidays.... that's why I never responded until I did. You have all been very helpful. Wow, that explains it, Good for you. Now did you made it work with the changes on the code? Quote
denverado Posted June 25, 2013 Author Posted June 25, 2013 Sorry pBe..... Stuck on site for the last week. I couldn't get it to work. the "22.5 vertical bend" and "45vvertical bend" did not work. (defun c:ghj (/ enx idx sel str f n _Str) (if (setq sel (ssget "_X" '((0 . "TEXT,MTEXT") (1 . "*VERTICAL BEND")))) (repeat (setq idx (sslength sel)) (setq enx (entget (ssname sel (setq idx (1- idx)))) str (assoc 1 enx) _str (Cdr str)) (setq f nil n 16) (while (and (not f) (> n -1)) (setq f (vl-some '(lambda (k) (if (wcmatch _str (strcat k "*VERTICAL BEND")) (atoi k))) (list (itoa n) (if (zerop n) "-0" (itoa (- n)))))) (setq n (1- n)) ) (if (setq nstr (cond ((or (<= 0 f 3) (<= -3 f -1)) "JOINT DEFLECTION") ((or (<= 4 f 6) (<= -6 f -4)) "2 JOINT DEFLECTIONS") ((or (<= 7 f 16) (<= -16 f -7)) "11.25%%D VERTICAL BEND") ((or (<= 17 f 28) (<= -28 f -17)) "22.5%%D VERTICAL BEND") ((or (<= 39 f 51) (<= -51 f -39)) "45%%D VERTICAL BEND")) ) (entmod (subst (cons 1 nstr) str enx ) ) ) ) ) (princ) ; ; horizontal bend change ; (setq tss (ssget "X" (list (cons 1 "*HORIZONTAL BEND")))) (repeat (sslength tss) (command "-layer" "n" "DEFPOINTS" "") (command "chprop" tss "" "LA" "DEFPOINTS" "") (entmod tdata) (ssdel (ssname tss 0) tss) ); end repeat ; ; vertical bend change ; (setq tss (ssget "X" (list (cons 1 "*VERTICAL BEND")))) (repeat (sslength tss) (command "chprop" tss "" "c" "6" "") (entmod tdata) (ssdel (ssname tss 0) tss) ); end repeat ) I would upload an acad file as well but work IT wont let me! Quote
pBe Posted June 26, 2013 Posted June 26, 2013 (edited) Again here: (setq f nil n [b]51[/b]) I noticed you skipped 29 to 38? why is that? and if it should be included what is the new string then? EDIT: (defun c:ghj (/ enx idx sel str f n _Str) (if (setq sel (ssget "_X" '((0 . "TEXT,MTEXT") (1 . "*VERTICAL BEND[color="blue"],*HORIZONTAL BEND"[/color])))) (repeat (setq idx (sslength sel)) (setq enx (entget (ssname sel (setq idx (1- idx)))) str (assoc 1 enx) _str (Cdr str)) (setq f nil n[b][color="blue"] 51[/color][/b]) [color="blue"] (if (wcmatch _str "*HORIZONTAL BEND") (entmod (append enx (list (cons 8 "Defpoints"))))[/color] (progn (while (and (not f) (> n -1)) (setq f (vl-some '(lambda (k) (if (wcmatch _str (strcat k "*VERTICAL BEND")) (atoi k))) (list (itoa n) (if (zerop n) "-0" (itoa (- n)))))) (setq n (1- n)) ) (if (and f (setq nstr (cond ((or (<= 0 f 3) (<= -3 f -1)) "JOINT DEFLECTION") ((or (<= 4 f 6) (<= -6 f -4)) "2 JOINT DEFLECTIONS") ((or (<= 7 f 16) (<= -16 f -7)) "11.25%%D VERTICAL BEND") ((or (<= 17 f 28) (<= -28 f -17)) "22.5%%D VERTICAL BEND") ((or (<= 39 f 51) (<= -51 f -39)) "45%%D VERTICAL BEND")) )) (setq enx (entmod (subst (cons 1 nstr) str enx)) [color="blue"] [b] enx (entmod (append enx (list (cons 62 6))))[/b][/color]) ) ) ) ) ) (princ) ) Still doesnt account for numbers 29 to 38. Edited June 26, 2013 by pBe Quote
denverado Posted June 27, 2013 Author Posted June 27, 2013 Thanks pBe, I ended up doing the following to make it work right. (defun c:ghj (/ enx idx sel str f n _Str) (if (setq sel (ssget "_X" '((0 . "TEXT,MTEXT") (1 . "*VERTICAL BEND")))) (repeat (setq idx (sslength sel)) (setq enx (entget (ssname sel (setq idx (1- idx)))) str (assoc 1 enx) _str (Cdr str)) (setq f nil n 92) (while (and (not f) (> n -1)) (setq f (vl-some '(lambda (k) (if (wcmatch _str (strcat k "*VERTICAL BEND")) (atoi k))) (list (itoa n) (if (zerop n) "-0" (itoa (- n)))))) (setq n (1- n)) ) (if (setq nstr (cond ((or (<= 0 f 3) (<= -3 f -1)) "JOINT DEFLECTION") ((or (<= 4 f 6) (<= -6 f -4)) "2 JOINT DEFLECTIONS") ((or (<= 7 f 16) (<= -16 f -7)) "11.25%%D VERTICAL BEND") ((or (<= 17 f 28) (<= -28 f -17)) "22.5%%D VERTICAL BEND") ((or (<= 29 f 38) (<= -38 f -29)) "CHECK VERTICAL BEND") ((or (<= 39 f 51) (<= -51 f -39)) "45%%D VERTICAL BEND") ((or (<= 52 f 88) (<= -88 f -52)) "CHECK VERTICAL BEND") ((or (<= 89 f 91) (<= -91 f -89)) "90%%D VERTICAL BEND")) ) (entmod (subst (cons 1 nstr) str enx ) ) ) ) ) (princ) ; ; horizontal bend change ; (setq tss (ssget "X" (list (cons 1 "*HORIZONTAL BEND")))) (repeat (sslength tss) (command "-layer" "n" "DEFPOINTS" "") (command "chprop" tss "" "LA" "DEFPOINTS" "") (entmod tdata) (ssdel (ssname tss 0) tss) ); end repeat ; ; vertical bend change ; (setq tss (ssget "X" (list (cons 1 "*VERTICAL BEND")))) (repeat (sslength tss) (command "chprop" tss "" "c" "6" "") (entmod tdata) (ssdel (ssname tss 0) tss) ); end repeat ) 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.