LISP to change SansSerif font to romans.shx

LISP to change SansSerif font to romans.shx

Anonymous
Not applicable
2,656 Views
11 Replies
Message 1 of 12

LISP to change SansSerif font to romans.shx

Anonymous
Not applicable

I have about 500 drawings that have a text style SansSerif_Bold that use the SansSerif font and I need to change these all to use the romans.shx font.  I know that there are lots of posts about LISPs for changing the text style font from one font to another.  I think that I have read them all.  I have a bat file that calls a scrip that call my LISP for a directory of drawings.  The problem is that my LISP file isn't working.  It appears to change the font to romans but when I go back into the drawing to check it; when I go to style and select the SansSerif_Bold, it has romans in the font name but it wants me to Apply it when exiting text style.  I don't have to select romans I just have to click Apply.  So for some reason my LISP isn't doing the Apply.  I tried changing romans to romans.shx in my LISP but that caused not to work at all.  Below is the LISP that I am using.  Any help here would be greatly appreciated as I am pretty weak when it comes to this stuff.  The LISP I am using was found on another forum and I modified with the text styles/fonts that I need to change.

 

(defun C:P6()
(vl-load-com)
(vla-setfont
(vla-item
(vla-get-textstyles
(setq adoc (vla-get-activedocument
(vlax-get-acad-object))))
"SansSerif_Bold"
)
"Romans" ; font name
:vlax-false ; non-bold (otherwise :vlax-true)
:vlax-true ; italic (otherwise :vlax-false)
0 ; symbol's flag
66 ;; sum of flag values for ticks and characters
)
(vla-regen adoc acallviewports)
(princ)
)

 

 

0 Likes
Accepted solutions (1)
2,657 Views
11 Replies
Replies (11)
Message 2 of 12

Kent1Cooper
Consultant
Consultant

There is a meaningful difference between assigning .TTF and .SHX fonts when used in Styles, which might be your problem.  If assigning a .SHX font, compared to a .TTF font, you need to give it one more input.  I wouldn't typically do this with the (vla-...) approach, but in a simpler way with a (command) function, so I'm not sure about the exact inputs to use your way [should your 66 bit-code be different?], but going through the steps manually at the command line in a drawing [and just hitting Enter for the defaults except for the font], here's the difference:

Command: -STYLE
Enter name of text style or [?]: Anything

[....Existing or New style, depending....]

Specify font name or font filename (for SHX) <default>: sanss___.ttf
Specify height of text or [Annotative] <0'-0">:
Specify width factor <1.0>:

Specify obliquing angle <0d0'0">:
Display text backwards? [Yes/No] <No>:
Display text upside-down? [Yes/No] <No>:

.... NOTE: Vertical question is  not asked, as it is for .SHX font below ....

"Anything" is now the current text style.

 

Whereas:

Command: -STYLE
Enter name of text style or [?]: Whatever

[....Existing or New style, depending....]

Specify font name or font filename (for SHX) <default>: romans.shx
Specify height of text or [Annotative] <0'-0">:
Specify width factor <1.0>:

Specify obliquing angle <0d0'0">:
Display text backwards? [Yes/No] <No>:
Display text upside-down? [Yes/No] <No>:
Vertical? [Yes/No] <No>: <----- NOT ASKED for a .TTF font, but needs an answer for .SHX

"Whatever" is now the current text style.

 

To be a little pickier about it, the vertical question will not be asked for any .SHX font, only those that are defined for the possibility, but ROMANS is one of those.

Kent Cooper, AIA
0 Likes
Message 3 of 12

john.uhden
Mentor
Mentor

I have never tried vla-setfont.

Try this instead...

(vlax-for style (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'textstyles)
  (if (= (vlax-get style 'fontfile) "sanss___.ttf")
    (vlax-put style 'fontfile "romans.shx")
  )
)

 You may have to add your own regen.

John F. Uhden

0 Likes
Message 4 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

I have about 500 drawings that have a text style SansSerif_Bold that use the SansSerif font and I need to change these all to use the romans.shx font.  ....


Also, just a general caveat:

 

Changing a font assignment is never as simple as just doing that.  You should, unfortunately, go through your 500 drawings and look them over carefully, because:
A)  you will get Mtext with line wrappings that fall at different places, and therefore sometimes different numbers of lines, which could be either more lines or fewer lines depending on the relationship between the previous font's and the new font's character widths, and;

B)  you will get Dimensions whose text either no longer fits between the extension lines and will therefore be kicked outboard, or that used to be outboard but now fits inside.

C)  even plain Text will change length, which could be getting either longer or shorter.

 

In the first possibility in each of those, things could newly overlap other drawn elements.

 

I'm not sure without experimenting what the effect would be on MultiLeaders with leaders attached to Mtext content other than at the top-left beginning corner, i.e. whether the attachments will all fix themselves [and if they do, they could also overlap other elements that they didn't before].

Kent Cooper, AIA
Message 5 of 12

Anonymous
Not applicable

@Kent1Cooper thanks for the responses.  I have done this before as a manual exercise so I am aware how it is affecting the rest of the drawing and that's all good.  I am just hoping to simplify the process this time around as it seems like there should be a way.  

 

@Kent1Cooper @john.uhden I need to read about vla and vlax as I am not familiar with either.  I have been trial and mostly erroring trying to figure out something that would work.  And the LISP that I showed got me closer than anything else so I was hoping a tweak with it would be the missing piece.  

 

@john.uhden when I paste your code right into the command line, it updates 2 of the 3 SansSerif styles that I need to update.  One of the SansSerif styles has a Bold Font Style so it apparently doesn't like that.

 

Thank you both for trying help me with my ignorance here!

 

Frank

0 Likes
Message 6 of 12

john.uhden
Mentor
Mentor
Accepted solution

I guess that the bold version is a different font file.

Instead of

(= (vlax-get style 'fontfile) "sanss___.ttf")

try

(wcmatch (strcase (vlax-get style 'fontfile)) "SANSS*.TTF")

 

John F. Uhden

0 Likes
Message 7 of 12

Anonymous
Not applicable

@john.uhden that works great when I paste code right into the drawing.  Thanks!  All 3 SansSerif fonts changed to romans.shx.  Now, can I put this in a LISP?  I tried the one below but after loading it, it won't find P6 to run.

 

(defun C:P6()
(vl-load-com)
(vlax-for style (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'textstyles)
(if (wcmatch (strcase (vlax-get style 'fontfile)) "SANSS*.TTF")
(vlax-put style 'fontfile "romans.shx")
)
)

0 Likes
Message 8 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

....  I tried the one below but after loading it, it won't find P6 to run.

 

(defun C:P6()
(vl-load-com)
(vlax-for style (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'textstyles)
(if (wcmatch (strcase (vlax-get style 'fontfile)) "SANSS*.TTF")
(vlax-put style 'fontfile "romans.shx")
)
)


Did you not get some kind of error message when loading?  This is an example of why indenting can help, with any opening left parenthesis whose closing right parenthesis isn't in the same line  of code has that in the same horizontal position below, so you can easily see the pairings:

 

(defun C:P6()
  (vl-load-com)
  (vlax-for style (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'textstyles)
    (if (wcmatch (strcase (vlax-get style 'fontfile)) "SANSS*.TTF")
      (vlax-put style 'fontfile "romans.shx"); then argument
    ); end if
  ); end vlax-for

); end defun  <--- missing

Kent Cooper, AIA
0 Likes
Message 9 of 12

Anonymous
Not applicable

@Kent1Cooper thank you!  I was just googling malformed list on input and was counting parenthesis but I think that I was overlooking the first one in front of defun lol.  I had also noticed that some routines had indentions and others didn't but wasn't sure if there was a rhyme or reason as to why some used them and some didn't.  I get it now though.   

0 Likes
Message 10 of 12

john.uhden
Mentor
Mentor
The code looks fine (as though I've seen it before 🙂
You should paste it into your acaddoc.lsp file, or if you don't have one,
then create one and put it in you Support directory. Notepad will work
just fine.
To see if you have one, at the command prompt enter:
(findfile "acaddoc.lsp")
If it returns other than nil then you have one and it shows the full path
to the file.
After the pasting, add one line (after and outside the function):
(c:P6)

John F. Uhden

0 Likes
Message 11 of 12

husamcivil
Enthusiast
Enthusiast
Thanks, Kent. I missed it too.
That's why it's so important to use the "insert code" icon, to maintain
indentures when pasting.
0 Likes
Message 12 of 12

john.uhden
Mentor
Mentor
That's odd because I had posted that response (unless someone has changed
my name). @john.uhden speaking.

John F. Uhden

0 Likes