AutoLISP: Change Text Styles

Today’s featured routine was posted by “Peter” at Augi.com found here: http://forums.augi.com/showthread.php?22959-Help-Changing-text-style-in-blocks

This routine helps changes the text style of text objects and even attributes inside of blocks to a user specified text style. This is helpful for when you receive drawings from another source and would like to change text styles to match your text styles.

For this routine to work, the desired text style must exist in the drawing.

The format of how to run this routine is different than other routines that you might be used to. You load the lisp routine as normal, but there isn’t a command that you enter at the commandline. What you do is pass feed the LISP routine the “function” that runs the routine and then the 2 variables in order for it to run.

The format that you feed the command line is:
(changestyle “oldtextstylename” “newtextstylename”)

changestyle = starts the function (starts the routine)
oldtextstylename = replace this text with the name of the text style that you would like to be replaced. Note – keep the name in “quotes”
newtextstylename = replace this text with the name of the text style that you would like to replace the previous style. Note – keep the name in “quotes”

Text Style Change

 


;;; Changes objects that are set to one text style to another text style. Both styles need to be defined in the drawing.
;;; Posted by Peter
;;; http://forums.augi.com/showthread.php?22959-Help-Changing-text-style-in-blocks
;;;
;;; Use the foloowing format in the command line after loading the routine:
;;; (changestyle "oldtextstylename" "newtextstylename")
;;; 
(defun ChangeStyle (strStyle1 strStyle2 / entItem objBlock objDocument objItem )
 (vl-load-com)
 (setq objDocument (vla-get-activedocument (vlax-get-acad-object)))
 (if (and (tblobjname "style" strStyle1)
          (tblobjname "style" strStyle2)         
     )
  (vlax-for objBlock (vla-get-blocks objDocument)
   (if (> (vla-get-count objBlock) 0)
    (progn
     (setq objItem (vla-item objBlock 0)
           entItem (vlax-vla-object->ename objItem)
     )
     (while entItem
      (if (and (vlax-property-available-p (setq objItem (vlax-ename->vla-object entItem)) "StyleName")
               (= (strcase (vla-get-stylename objItem)) (strcase strStyle1))
          )
       (vla-put-stylename objItem strStyle2)
      )
      (setq entItem (entnext entItem))
     )
    )
   )
  )
  (princ "\nError check if styles exist: ")
 )
 (vla-regen objDocument 0)
)

Text Style Merge 1

 

Text Style Merge 2

About these ads

About AutoCAD Tips

As weird as it sounds, AutoCAD is a hobby for me. But I am fortunate enough to have a career doing my hobby. I work for a large engineering firm supporting CAD users in various disciplines. My dream job would be to work for a company like Autodesk or Disney's Imagineering where inspiring others and using innovative approaches and software are adding value to projects and bringing design in all areas up to a new level. This blog serves as a repository or knowledge base for myself so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting experience better.
This entry was posted in AutoLISP, AutoLISP: Attributes, AutoLISP: Manage, AutoLISP: Text. Bookmark the permalink.

2 Responses to AutoLISP: Change Text Styles

  1. Mudassir says:

    Dear All,
    We have a found some thing very critical situation
    I have all text same text but text names are different an example (T10 & T5) these both are same but i want select only T5 how can i select whole drawing to catch only T5 please let me know if you achieve this hadik :p
    Good Luckkkkkkkkk

    • AutoCAD Tips says:

      From your question, I think that you are describing 2 scenarios:
      1) Selecting Text that contains “T5″ spelled out in the text
      2) Selecting all text that is on a text style named “T5″

      Either way, both can easily be done within AutoCAD.

      To select text that has “T5″ in it, use the FIND command

      To select objects that are using the text style named “T5″ use the command SELECTSIMILAR

    • SELECTSIMILAR
    • SE to change the settings for the command
    • check the box next to “Object style”
    • Select Similar Settings