mbrandt5 Posted July 2, 2015 Share Posted July 2, 2015 I'm wondering how I can make one variable determine another variable I'd like to be able to alter this code have old string run through all fractions then have new string change to all of these stacked diagonal fractions So 1/4 would turn into this {\H0.75x;\S1#4;} and So 3/8 would turn into this {\H0.75x;\S3#8;} Without requiring a user to input string (defun c:superstr() (setq olsosmode (getvar "OSMODE")) (setvar "OSMODE" 0) (setq p (ssget)) (if p (progn (setq osl (strlen (setq os (getstring "\nOld string: " t)))) (setq nsl (strlen (setq ns (getstring "\nNew string: " t)))) (setq l 0 chm 0 n (sslength p)) (setq adj (cond ((/= osl nsl) (- nsl osl)) (T nsl) ) ) (while (< l n) (setq d (entget (setq e (ssname p l)))) (if (and (= (atext 0) "INSERT")(= (atext 66) 1)) (progn (setq e (entnext e)) (while e (setq d (entget e)) (cond ((= (atext 0) "ATTRIB") (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 d)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (cond ((= st os) (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) (setq si (+ si adj)) ) ) (setq si (1+ si)) ) (if chf (progn (setq d (subst (cons 1 s) as d)) (entmod d) (entupd e) (setq chm (1+ chm)) ) ) (setq e (entnext e)) ) ((= (atext 0) "SEQEND") (setq e nil)) (T (setq e (entnext e))) ) ) ) ) (if (= "MTEXT" ; Look for MTEXT entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) Quote Link to comment Share on other sites More sharing options...
BIGAL Posted July 3, 2015 Share Posted July 3, 2015 I would start by breaking your code into defuns your ssget can get basicly anything, then use a cond on the object type TEXT, MTEXT or Blocks, others ignore. This way you just work with 1 defun rather than a mega IF. Again a seperate defun that is called once you have the text string has a series of conds 1/8 1/4 3/8 1/2 etc and sets the correct mtext string. What I am saying is you can have defuns called inside defuns. defun change text defun ssget stuff defun what is it defun text option convert to mtext, defun change text defun mtext get value, defun change text defun insert get attributes, defun change text program starts here Quote Link to comment Share on other sites More sharing options...
Commandobill Posted July 3, 2015 Share Posted July 3, 2015 This bit of code would be useful to convert the old string to what you are looking for: (if (vl-string-search "/" os) (setq ns (strcat "{\\H0.75x;\\S" (vl-string-subst "#" "/" os 0) ";}"))) Quote Link to comment Share on other sites More sharing options...
mbrandt5 Posted July 6, 2015 Author Share Posted July 6, 2015 Sorry I just realized that i didn't post the full code originally (defun c:superstr() (setq olsosmode (getvar "OSMODE")) (setvar "OSMODE" 0) (setq p (ssget)) (if p (progn (setq osl (strlen (setq os (getstring "\nOld string: " t)))) (setq nsl (strlen (setq ns (getstring "\nNew string: " t)))) (setq l 0 chm 0 n (sslength p)) (setq adj (cond ((/= osl nsl) (- nsl osl)) (T nsl) ) ) (while (< l n) (setq d (entget (setq e (ssname p l)))) (if (and (= (atext 0) "INSERT")(= (atext 66) 1)) (progn (setq e (entnext e)) (while e (setq d (entget e)) (cond ((= (atext 0) "ATTRIB") (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 d)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (cond ((= st os) (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) (setq si (+ si adj)) ) ) (setq si (1+ si)) ) (if chf (progn (setq d (subst (cons 1 s) as d)) (entmod d) (entupd e) (setq chm (1+ chm)) ) ) (setq e (entnext e)) ) ((= (atext 0) "SEQEND") (setq e nil)) (T (setq e (entnext e))) ) ) ) ) (if (= "MTEXT" ; Look for MTEXT entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (if (= "DIMENSION" ; Look for DIMENSION entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (if (= "TEXT" ; Look for TEXT entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (setq l (1+ l)) ) ) ) (if (> chm 1) (princ (strcat "\nUpdated " (itoa chm) " text strings")) (princ (strcat "\nUpdated " (itoa chm) " text string")) ) (setvar "OSMODE" oldosmode) (terpri) ) ; (defun atext (num) (cdr (assoc num d)) ) Also, I apologize for my poor coding skills but I'm not sure exactly what I would delete or where I would add this replacement function you've given me? (if (vl-string-search "/" os) (setq ns (strcat "{\\H0.75x;\\S" (vl-string-subst "#" "/" os 0) ";}"))) Anyone willing to assist Quote Link to comment Share on other sites More sharing options...
mbrandt5 Posted July 6, 2015 Author Share Posted July 6, 2015 (edited) So I figured out how to add the code you supplied me with to make the entered fraction replace with the stacked version Here Is The Updated Code: (defun c:superstr() (setq olsosmode (getvar "OSMODE")) (setvar "OSMODE" 0) (setq p (ssget)) (if p (progn (setq osl (strlen (setq os (getstring "\nOld string: " t)))) (setq nsl (strlen (setq ns os))) (if (vl-string-search "/" os) (setq ns (strcat "{\\H0.75x;\\S" (vl-string-subst "#" "/" os 0) ";}"))) (setq l 0 chm 0 n (sslength p)) (setq adj (cond ((/= osl nsl) (- nsl osl)) (T nsl) ) ) (while (< l n) (setq d (entget (setq e (ssname p l)))) (if (and (= (atext 0) "INSERT")(= (atext 66) 1)) (progn (setq e (entnext e)) (while e (setq d (entget e)) (cond ((= (atext 0) "ATTRIB") (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 d)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (cond ((= st os) (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) (setq si (+ si adj)) ) ) (setq si (1+ si)) ) (if chf (progn (setq d (subst (cons 1 s) as d)) (entmod d) (entupd e) (setq chm (1+ chm)) ) ) (setq e (entnext e)) ) ((= (atext 0) "SEQEND") (setq e nil)) (T (setq e (entnext e))) ) ) ) ) (if (= "MTEXT" ; Look for MTEXT entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (if (= "DIMENSION" ; Look for DIMENSION entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (if (= "TEXT" ; Look for TEXT entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (setq l (1+ l)) ) ) ) (if (> chm 1) (princ (strcat "\nUpdated " (itoa chm) " text strings")) (princ (strcat "\nUpdated " (itoa chm) " text string")) ) (setvar "OSMODE" oldosmode) (terpri) ) ; (defun atext (num) (cdr (assoc num d)) ) Now, I looking for a way to have the all fractions searched for instead of having a string entered... Is there any way that I can set os to multiple fractions at once? Or am I going to have to set up some sort of continuation from 1/16th to 15/16th, seeing as I'm only requiring the 16ths to change... Edited July 7, 2015 by mbrandt5 Quote Link to comment Share on other sites More sharing options...
mbrandt5 Posted July 7, 2015 Author Share Posted July 7, 2015 So I got one fraction to replace with a stacked diagonal... now i just need the other 14 Please help me ...lol (defun c:superstr() (setq olsosmode (getvar "OSMODE")) (setvar "OSMODE" 0) (setq p (ssget)) (if p (progn (setq osl (strlen (setq os "1/4"))) (setq nsl (strlen (setq ns os))) (if (vl-string-search "/" os) (setq ns (strcat "{\\H0.75x;\\S" (vl-string-subst "#" "/" os 0) ";}"))) (setq l 0 chm 0 n (sslength p)) (setq adj (cond ((/= osl nsl) (- nsl osl)) (T nsl) ) ) (while (< l n) (setq d (entget (setq e (ssname p l)))) (if (and (= (atext 0) "INSERT")(= (atext 66) 1)) (progn (setq e (entnext e)) (while e (setq d (entget e)) (cond ((= (atext 0) "ATTRIB") (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 d)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (cond ((= st os) (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) (setq si (+ si adj)) ) ) (setq si (1+ si)) ) (if chf (progn (setq d (subst (cons 1 s) as d)) (entmod d) (entupd e) (setq chm (1+ chm)) ) ) (setq e (entnext e)) ) ((= (atext 0) "SEQEND") (setq e nil)) (T (setq e (entnext e))) ) ) ) ) (if (= "MTEXT" ; Look for MTEXT entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (if (= "DIMENSION" ; Look for DIMENSION entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (if (= "TEXT" ; Look for TEXT entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (setq l (1+ l)) ) ) ) (if (> chm 1) (princ (strcat "\nUpdated " (itoa chm) " text strings")) (princ (strcat "\nUpdated " (itoa chm) " text string")) ) (setvar "OSMODE" oldosmode) (terpri) ) ; (defun atext (num) (cdr (assoc num d)) ) Quote Link to comment Share on other sites More sharing options...
mbrandt5 Posted July 11, 2015 Author Share Posted July 11, 2015 I've tried applying a list using mapcar with no success getting a result of bad variable...will someone let me know what i'm doing wrong and help fix it? (defun c:superstr() (setq olsosmode (getvar "OSMODE")) (setvar "OSMODE" 0) (setq p (ssget)) (if p (progn (setq osl (strlen (setq os (mapcar 'yourfun stringlist)))) (setq nsl (strlen (setq ns os))) (if (vl-string-search "/" os) (setq ns (strcat "{\\H0.75x;\\S" (vl-string-subst "#" "/" os 0) ";}"))) (setq l 0 chm 0 n (sslength p)) (setq adj (cond ((/= osl nsl) (- nsl osl)) (T nsl) ) ) (while (< l n) (setq d (entget (setq e (ssname p l)))) (if (and (= (atext 0) "INSERT")(= (atext 66) 1)) (progn (setq e (entnext e)) (while e (setq d (entget e)) (cond ((= (atext 0) "ATTRIB") (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 d)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (cond ((= st os) (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) (setq si (+ si adj)) ) ) (setq si (1+ si)) ) (if chf (progn (setq d (subst (cons 1 s) as d)) (entmod d) (entupd e) (setq chm (1+ chm)) ) ) (setq e (entnext e)) ) ((= (atext 0) "SEQEND") (setq e nil)) (T (setq e (entnext e))) ) ) ) ) (if (= "MTEXT" ; Look for MTEXT entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (if (= "DIMENSION" ; Look for DIMENSION entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (if (= "TEXT" ; Look for TEXT entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (setq l (1+ l)) ) ) ) (if (> chm 1) (princ (strcat "\nUpdated " (itoa chm) " text strings")) (princ (strcat "\nUpdated " (itoa chm) " text string")) ) (setvar "OSMODE" oldosmode) (terpri) ) ; (defun atext (num) (cdr (assoc num d)) ) ; (defun yourfun (os) (setq stringlist '(list "1/4" "1/2")) ) Quote Link to comment Share on other sites More sharing options...
mbrandt5 Posted July 11, 2015 Author Share Posted July 11, 2015 (defun c:superstr() (setq p (ssget)) (if p (progn (setq osl (strlen (setq os " 1/4"))) (setq nsl (strlen (setq ns os))) (if (vl-string-search "/" os) (setq ns (strcat "{\\H0.75x;\\S" (vl-string-subst "#" "/" os 0) ";}"))) (setq ns (vl-string-subst "" " " ns)) (setq l 0 chm 0 n (sslength p)) (setq adj (cond ((/= osl nsl) (- nsl osl)) (T nsl) ) ) (while (< l n) (setq d (entget (setq e (ssname p l)))) (if (and (= (atext 0) "INSERT")(= (atext 66) 1)) (progn (setq e (entnext e)) (while e (setq d (entget e)) (cond ((= (atext 0) "ATTRIB") (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 d)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (cond ((= st os) (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) (setq si (+ si adj)) ) ) (setq si (1+ si)) ) (if chf (progn (setq d (subst (cons 1 s) as d)) (entmod d) (entupd e) (setq chm (1+ chm)) ) ) (setq e (entnext e)) ) ((= (atext 0) "SEQEND") (setq e nil)) (T (setq e (entnext e))) ) ) ) ) (if (= "MTEXT" ; Look for MTEXT entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (if (= "DIMENSION" ; Look for DIMENSION entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (if (= "TEXT" ; Look for TEXT entity type (group 0) (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq chf nil si 1) (setq s (cdr (setq as (assoc 1 e)))) (while (= osl (setq sl (strlen (setq st (substr s si osl))))) (if (= st os) (progn (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl)))) (setq chf t) ; Found old string (setq si (+ si nsl)) ) (setq si (1+ si)) ) ) (if chf (progn ; Substitute new string for old (setq e (subst (cons 1 s) as e)) (entmod e) ; Modify the TEXT entity (setq chm (1+ chm)) )) ) ) (setq l (1+ l)) ) ) ) (if (> chm 1) (princ (strcat "\nUpdated " (itoa chm) " text strings")) (princ (strcat "\nUpdated " (itoa chm) " text string")) ) (terpri) ) ; (defun atext (num) (cdr (assoc num d)) ) Quote Link to comment Share on other sites More sharing options...
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.