Author Topic: Changing text style settings  (Read 4804 times)

0 Members and 1 Guest are viewing this topic.

glee

  • Guest
Changing text style settings
« on: March 24, 2006, 01:06:17 PM »
I have searched through the postings here but don't seem to be able to find a routine I need.
I know barely enough lisp to be able to modify existing lisp routines.

Maybe if I list what I am trying to do. 
Hev existing drawings.  Lot's of them.  I'm trying to figure out a way of batch processing them without having to open them all.
They have a text style called Helvetica and using the font Helv.shx.
I need to rename the text style to Arial and change the font for that text style to arial narrow (which I think is arialn.ttf).
And set the width to 0.9.  I don't need to convert any text or mtext in the drawings to that font.  Just need to redefine the specific text style name and the font it uses.  It's a text style used for detail title attributes and I'm prefer to use a different font that can fit more info within the details and look nicer too. 

This is what i do now and I know there's a better way. 
I open the file, go into the style dialog box, select helvetice and rename it to arialn.  Then go into the font and change it from helv.shx to arial narrow.  then go to width factor and change that to 0.9.  Then hit apply and set the current style back to romans and close it. 
Did that make sense?  I do this for tons of details. 
Jurg has a lisp for style_update.lsp and CAB has one for CTS.lsp.  But they don't quite do what I was trying to do and my brain can't wrap itself around what they have done to sort of mesh them together to do what i need.
This is sort of what I think it needs to do.  But really, I'm no programmer.  If someone can point out what I'm doing wrong or where the heck I have messed it up, please let me know.  Plus a way to automate this to run on multiple files would be good.  But I would be happy just to run this on each file I open.  Thanks.
Code: [Select]
(defun c:srn

  (vl-load-com)

;replace font file
   (command "-style" "helv" "arialn.ttf" 0 .9 0 "No" "No")

;rename style name
    (command "-rename" "s" "helv" "ARIALGLA")


 (princ)
)
Ugly.  I know.  I'm not really trying to code it but sort of break it down as to what command I would normally type. 

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Changing text style settings
« Reply #1 on: March 24, 2006, 01:29:40 PM »
Code: [Select]
(vla-put-fontfile
  (vla-get-activetextstyle
    (vla-get-activedocument
      (vlax-get-acad-object)
   )
)
 (strcat (getenv "SYSTEMROOT") "\\fonts\\arial.ttf")
)

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Changing text style settings
« Reply #2 on: March 24, 2006, 03:17:22 PM »
Hi Gerald,
After looking at doing this with ObjectDBX, which would allow you to do all your drawings pretty quickly, I think your idea of just running a small lisp like you posted is probably the best. The reaseon being, the Object Model exposes the StyleName property to us but it is Read-Only. Meaning that to "change" it we would have to create a new Style duplicating the old one, change the width and fontfile to what you desire, cycle through all objects in the drawing looking for the old stylename and changing any that are found to the new one. This really isn't too hard, until you realize that all Dimension styles and Mtext objects would also need to be checked.

I've now fooled around with this for about an hour and still don't have it working properly. If I have time over the weekend I'll investigate further.

In the mean time, just add those 2 lines of code to the end of your acaddoc.lsp
Code: [Select]
;replace font file
   (command "-style" "helv" "arialn.ttf" 0 .9 0 "No" "No")

;rename style name
    (command "-rename" "s" "helv" "ARIALGLA")
providing the syntax is correct (I didn't check the command line to see if these actually work) then it should work for you. Although you may want to check if the change has already been done....something like:
Code: [Select]
(if (not (tblsearch "STYLE" "ARIALGLA"))
   (progn
      ;;the code to add/change the font
   )
)

Good luck,
Jeff
« Last Edit: March 24, 2006, 03:21:54 PM by Jeff_M »

glee

  • Guest
Re: Changing text style settings
« Reply #3 on: March 24, 2006, 03:21:22 PM »
Uh.  I'm a bit slow. 
But if I insert that and change a few things, I get an error saying too few arguments.  Not sure what I am doing wrong.

Code: [Select]
(defun c:srn

  (vl-load-com)

(vla-put-fontfile
  (vla-get-activetextstyle
    (vla-get-activedocument
      (vlax-get-acad-object)
   )
)
(strcat (getenv "SYSTEMROOT") "\\fonts\\arialn.ttf")
)

;rename style name
 (setq txtstyle "Helv")
    (command "-style" "arialgla" "arialn.ttf" 0 0.9 0 "No" "No" "No")


 (princ)
)

Or did I mess it up.  Would not (setg txtstyle set the current style to Helv by doing it that way.  then running the command for -style then allow me to change the style name and it's other settings.  Or does the vla-getactivestyle get in the way since helv is not the active text style. 

glee

  • Guest
Re: Changing text style settings
« Reply #4 on: March 24, 2006, 03:26:38 PM »
Jeff,
Just saw your post.  I'll give that a try.  I'm not sure but sometimes when I run my lisp it does change helv to arialgla.  Rename it that is but it won't run the second command line.  That is based on the first two lines of code I did.  The scond lisp routine I pasted, was just trying to find a way without me smashing my head into the desk too many times. 
I'll try your if routine. 
This was supposed to make my life easier.  But now that I've started down this path, I may as well keep banging away at it.  Thanks a bunch. 

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Changing text style settings
« Reply #5 on: March 24, 2006, 03:50:45 PM »
One thing I just saw in your "style" command....change the .9 to 0.9....

glee

  • Guest
Re: Changing text style settings
« Reply #6 on: March 24, 2006, 04:04:09 PM »
Jeff,
I just made some changes and tried it out.  basically debugging and trying things out as I go along.

Code: [Select]
(defun c:srn ()

  (vl-load-com)
(vla-put-fontfile
    (vla-get-activedocument
      (vlax-get-acad-object)
   )
)
(strcat (getenv "SYSTEMROOT") "\\fonts\\arialn.ttf")
)

;rename style name

(setq ss (ssget))     
   (command "-rename" "s" "helv" "ARIALGLA")

 (princ)
)

With the above I can get it to change the name of the style helv to arialgla.  Was getting all sort of problems before.  I am guessing with the ssget command, it makes me select an object first.  I removed the "(vla-get-activetextstyle" because the active style is romans and it was changing the font of that to arialgla.  I'm guessing because of the (strcat (getenv "SYSTEMROOT") "\\fonts\\arialn.ttf") line. 
I'm a hack.  Really sorry.  But that's how I figure things out. 

glee

  • Guest
Re: Changing text style settings
« Reply #7 on: March 24, 2006, 04:22:04 PM »
Jeff,
Ok I finally got this to work.
Code: [Select]
(defun c:srn ()

  (vl-load-com)
(vla-put-fontfile
    (vla-get-activedocument
      (vlax-get-acad-object)
   )
)
)

;rename style name
(setq ss (ssget))     
   (command "-rename" "s" "helv" "ARIALGLA")
       (command "-style" "ARIALGLA" "arialn.ttf" 0 "0.9" 0 "No" "No" "No")
              (command "-style" "romans" "" 0 "" 0 "No" "No" "No")
 (princ)
)
It seems to automaticall ask me to pick on my block, and once I do, it renames helv to arialgla and the second line gets it to change the font to the settings I want.  It's really ugly code but right now it's doing what I want it to do, and returns romans as the active style. 
I haven't figured out how to do the "if" search and replace function you told me to add for pre existing ariagla style. 
Appreciate the help and advice you have given. 
Is there more I can do to at least make it more elegant?   

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Changing text style settings
« Reply #8 on: March 24, 2006, 04:42:32 PM »
How about this?
Code: [Select]
(defun c:srn (/ cstyl)
  (if (and (tblsearch "STYLE" "Helv")
   (not (tblsearch "STYLE" "ARIALGLA"))
   )
    (progn
      (setq cstyl (getvar "TEXTSTYLE"))
      (command "-style" "helv" "arialn.ttf" 0 0.9 0 "No" "No")
      (command "-rename" "s" "helv" "ARIALGLA")
      (setvar "TEXTSTYLE" cstyl)
      )
    )
  (princ)
  )

glee

  • Guest
Re: Changing text style settings
« Reply #9 on: March 24, 2006, 05:09:26 PM »
Jeff,
Thanks a bunch.  it works great.
Just for my edification.  I am assuming (/ cstyl) at the front helps you getvar the existing active style and at the end, setvar "textstyle" cstyl returns the active style?

This is great.  thanks a bunch.  I just hack away at stuff until it sorts of does what I need.  Not even close to what you just did.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Changing text style settings
« Reply #10 on: March 24, 2006, 06:43:08 PM »
The (/ cstyl) is just telling the Lisp interpreter that the variable named cstyl will only be used in this function, ie it is a Local variable.

I then set cstyl to the TEXTSTYLE sysvar with (getvar "TEXTSTYLE")

Then at the end, you are correct. I reset it to what it was before we started.

glee

  • Guest
Re: Changing text style settings
« Reply #11 on: March 24, 2006, 06:53:18 PM »
Jeff,
Ok.  I got that now.  Makes it clearer.
Thanks again.  I appreciate the help.