Modifying Text

Discussion in 'AutoCAD' started by kmc, Aug 12, 2003.

  1. kmc

    kmc Guest

    can anyone help ?
    I have a dwg that has a lot of text that needs to be rejustified and also have the width factor reset to 1

    does anyone have a Lisp routine that will do this for me

    Thanks in advance

    keith
     
    kmc, Aug 12, 2003
    #1
  2. kmc

    Jarod Arney Guest

    This does a lot more than what you asked but it should work.

    (defun c:ct1 ()
    (init_spots_error '(list '("cmdecho" 0 "texteval" 1) T))

    (defun filter_text ()
    (setq ss1 (sslength selectedtext))
    (setq textset (ssadd))
    (if (> ss1 25)
    (princ "\nVerifying the selected entities. -- Please wait. ")
    )
    (while (> ss1 0)
    (setq ss1 (1- ss1))
    (setq temp2 (ssname selectedtext ss1))
    (setq enttype (cdr (assoc 0 (entget temp2))))
    (if (or (= enttype "TEXT") (= enttype "MTEXT") (= enttype "ATTDEF"))
    (ssadd temp2 textset)
    )
    )
    (setq ss1 (sslength textset))
    (princ ss1)
    (princ " text entities found. ")
    )

    (defun casechange ()
    (setq count -1)
    (setq t2 "T")
    (while (boundp 't2)
    (setq count (1+ count))
    (setq temp (entget (ssname textset count)))
    (setq txtoc (cdr (assoc 1 temp)))
    (cond
    ((= cse "l") (setq case T))
    ((= cse "u") (setq case nil))
    )
    (setq n_str (strcase txtoc case))
    (setq oldvalue (assoc 1 temp))
    (setq newvalue (cons 1 n_str))
    (setq newtext (subst newvalue oldvalue temp))
    (entmod newtext)
    (setq t2 (ssname textset (1+ count)))
    )
    )

    (defun propchange ()
    (setq count -1)
    (setq t2 "T")
    (while (boundp 't2)
    (setq count (1+ count))
    (setq temp (entget (ssname textset count)))
    (setq oldvalue (assoc x temp))
    (setq newvalue (cons x value))
    (setq newtext (subst newvalue oldvalue temp))
    (entmod newtext)
    (setq t2 (ssname textset (1+ count)))
    )
    )

    (defun just_options ()
    (if (getvar "dimclrd")
    (progn
    (initget 128 "TL TC TR ML MC MR BL BC BR")
    (setq justp (strcase (getkword "\nTL)eft, TC)enter, TR)ight, ML)eft,
    MC)enter, MR)ight, BL)eft, BC)enter or BR)ight : ") T))
    )
    (progn
    (initget 128 "A C F L M R")
    (setq justp (strcase (getkword "\nA)ligned, C)enter, F)it, L)eft,
    M)iddle or R)ight : ") T))
    )
    )
    (cond
    ((= justp "l") (setq justp 0 justq 0))
    ((= justp "c") (setq justp 1 justq 0))
    ((= justp "r") (setq justp 2 justq 0))
    ((= justp "a") (setq justp 3 justq 0))
    ((= justp "f") (setq justp 5 justq 0))
    ((= justp "tl") (setq justp 0 justq 3))
    ((= justp "tc") (setq justp 1 justq 3))
    ((= justp "tr") (setq justp 2 justq 3))
    ((= justp "ml") (setq justp 0 justq 2))
    ((= justp "m") (setq justp 4 justq 0))
    ((= justp "mc") (setq justp 1 justq 2))
    ((= justp "mr") (setq justp 2 justq 2))
    ((= justp "bl") (setq justp 0 justq 1))
    ((= justp "bc") (setq justp 1 justq 1))
    ((= justp "br") (setq justp 2 justq 1))
    (T (setq justp nil))
    )
    (if (/= nil justp)
    (ch_justification)
    )
    )

    (defun ch_justification ()
    (setq sslen (sslength textset))
    (while (> sslen 0)
    (setq sslen (1- sslen))
    (setq ent (entget (ssname textset sslen)))
    (setq ent (subst (cons 72 justp) (assoc 72 ent) ent))

    (setq pt1 (cadr (assoc 11 ent)))
    (setq pt2 (caddr (assoc 11 ent)))
    (setq pt3 (cadddr (assoc 11 ent)))
    (setq opt (trans (list pt1 pt2 pt3) (cdr (assoc -1 ent)) 1))
    (if (getvar "dimclrd")
    (setq ent (subst (cons 73 justq) (assoc 73 ent) ent))
    )
    (cond
    ((or (= justp 3) (= justp 5))
    (prompt "\nNew text alignment points: ")
    (redraw (cdr (assoc -1 ent)) 3)
    (initget 1)
    (setq loc (getpoint))
    (initget 1)
    (setq loc1 (getpoint loc))
    (redraw (cdr (assoc -1 ent)) 1)
    (setq ent (subst (cons 10 loc) (assoc 10 ent) ent))
    (setq ent (subst (cons 11 loc1) (assoc 11 ent) ent))
    )
    ((or (/= justp 0) (/= justq 0))
    (redraw (cdr (assoc -1 ent)) 3)
    (prompt "\nNew text location: ")
    (setq loc (getpoint opt))
    (redraw (cdr (assoc -1 ent)) 1)
    (if (null loc)
    (setq loc opt)
    (setq loc (trans loc 1 (cdr (assoc -1 ent))))
    )
    (setq ent (subst (cons 11 loc) (assoc 11 ent) ent))
    )
    )
    (entmod ent)
    )
    )

    ;change the location of an entry.
    (defun ch_location ()
    (setq sslen (sslength textset))
    (setq style "" hgt "")
    (setq rot "" txt "")
    (command "change" textset "" "")
    (while (> sslen 0)
    (setq ent (entget (ssname textset (setq sslen (1- sslen)))))
    (setq opt (list (cadr (assoc 11 ent)) (caddr (assoc 11 ent)) (cadddr
    (assoc 11 ent))))
    (prompt "\nNew text location: ")
    (command pause)
    (if (null loc)
    (setq loc opt)
    )
    (command style hgt rot txt)
    )
    )

    ;Search and Replace
    (defun text_options ()
    (setq sslen (sslength textset))
    (cond
    ((= ans "individually")
    (while (> sslen 0)
    (setq sn (ssname textset (setq sslen (1- sslen))))
    (redraw sn 3)
    (setq ss (ssadd))
    (ssadd (ssname textset sslen) ss)
    (chgtext ss)
    (redraw sn 1)
    )
    )
    ((= ans "globally")
    (chgtext textset) ; change 'em all
    )
    )
    )

    (defun chgtext (objs / last_o tot_o ent o_str n_str st s_temp n_slen o_slen
    si chf chm cont ans)
    (setq chm 0)
    (if objs
    (progn
    (if (= (type objs) 'ENAME)
    (progn
    (setq ent (entget objs))
    (princ (strcat "\nExisting string: " (cdr (assoc 1 ent))))
    )
    (if (= (sslength objs) 1)
    (progn
    (setq ent (entget (ssname objs 0)))
    (princ (strcat "\nExisting string: " (cdr (assoc 1 ent))))
    )
    )
    )
    (setq o_str (getstring "\nMatch string : " t))
    (setq o_slen (strlen o_str))
    (if (/= o_slen 0)
    (progn
    (setq n_str (getstring "\nNew string : " t))
    (setq n_slen (strlen n_str))
    (setq last_o 0)
    (if (= (type objs) 'ENAME)
    (setq tot_o 1)
    (setq tot_o (sslength objs))
    )
    (while (< last_o tot_o)
    (setq tempvar3 (setq ent (entget (ssname objs last_o))))
    (setq chf nil si 1)
    (setq s_temp (cdr (assoc 1 ent)))
    (while (= o_slen (strlen (setq st (substr s_temp si o_slen))))
    (if (= st o_str)
    (progn
    (if (> si 1)
    (setq tempvar1 (substr s_temp 1 (1- si)))
    (setq tempvar1 "")
    )
    (setq tempvar2 (substr s_temp (+ si o_slen)))
    (setq s_temp (strcat tempvar1 n_str tempvar2))
    (setq chf t) ; Found old string
    (setq si (+ si n_slen))
    )
    (setq si (1+ si))
    )
    )
    (if chf
    (progn
    (entmod (subst (cons 1 s_temp) (assoc 1 ent) ent))
    (setq chm (1+ chm))
    )
    )
    (setq last_o (1+ last_o))
    )
    )
    ;; else go on to the next line...
    )
    )
    )
    (if (/= (type objs) 'ENAME)
    (if (/= (sslength objs) 1) ; Print total lines changed
    (princ (strcat "Changed " (rtos chm 2 0) " text lines.")
    )
    )
    )
    (terpri)
    )

    (initget 128 "Global Height Angle Width Style Rotation Case Justification
    Location REplace")
    (setq selectedmode (strcase (getkword "\nChange text G)lobal, H)eight,
    A)ngle, W)idth, S)tyle, R)otation, J)ustifcation, L)ocation, RE)place or
    C)ase: ") T))
    (if (/= "global" selectedmode)
    (setq selectedtext (ssget))
    (progn
    (setq selectedtext (ssget "X"))
    (initget 128 "Height Angle Width Style Rotation Case Justification
    Location REplace")
    (setq selectedmode (strcase (getkword "\nChange text H)eight, A)ngle,
    W)idth, S)tyle, R)otation, J)ustification, L)ocation, RE)place or C)ase: ")
    T))
    )
    )

    (filter_text)

    (cond
    ((= "height" selectedmode)
    (setq x 40)
    (setq value (getreal "\nNew Height: "))
    (propchange)
    )
    ((= "angle" selectedmode)
    (setq x 51)
    (setq value (* pi (/ (getreal "\nNew Angle: ") 180.0)))
    (propchange)
    )
    ((= "width" selectedmode)
    (setq x 41)
    (setq value (getreal "\nNew Width: "))
    (propchange)
    )
    ((= "style" selectedmode)
    (setq x 7)
    (setq value (getstring"\nNew Style: "))
    (propchange)
    )
    ((= "rotation" selectedmode)
    (setq x 50)
    (setq value (cvunit (getreal "\nNew Rotation (Degrees): ") "degree"
    "radian"))
    (propchange)
    )
    ((= "case" selectedmode)
    (initget 1 "L U")
    (setq cse (strcase (getkword"\nWhat case (U/L) : ") T))
    (casechange)
    )
    ((= "replace" selectedmode)
    (initget "Globally Individually")
    (setq ans (strcase (getkword "\nSearch and Replace text. I)ndividually or
    G)lobally : ") T))
    (text_options)
    )
    ((= "justification" selectedmode)
    (just_options)
    )
    ((= "location" selectedmode)
    (ch_location)
    )
    (T
    (exit)
    )
    )

    (restore_olderror)
    )
     
    Jarod Arney, Aug 12, 2003
    #2
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.