Author Topic: changing font of text style at startup  (Read 6795 times)

0 Members and 1 Guest are viewing this topic.

rkitek

  • Guest
changing font of text style at startup
« on: September 19, 2005, 02:30:09 PM »
Our office recently decided to change our 'standard' font from a TTF to a SHX. Rather than tediously going drawing by drawing to changing the font definition w/ 'style', I thought it would be nice if I could make the font change upon opening the dwg. I'm having a little trouble getting it to work, so any help would be much appreciated...

Basically, I'm trying to get all styles w/ font 'cgor45x.ttf' be replaced w/ 'cgor45x.shx'. The ttf font actually is listed in the flyout as "CG Omega"

Here's what I've got now:
Code: [Select]
(VL-LOAD-COM)
   (SETQ *DOC*
      (VLA-GET-ACTIVEDOCUMENT
      (VLAX-GET-ACAD-OBJECT)
      )
   )
   (SETQ STYLES
      (VLA-GET-TEXTSTYLES *DOC*)
   )
   (VLAX-FOR STYLE STYLES
      (IF (= (VLA-GET-FONTFILE STYLE) "CG Omega") ;;;I also tried "cgor45xf.ttf"
      (VLA-PUT-FONTFILE STYLE "cgor45xf.shx"))
   )

Am I way off here? Thanks in advance.

Sdoman

  • Guest
Re: changing font of text style at startup
« Reply #1 on: September 19, 2005, 03:36:33 PM »
Try setting the textstyle you want to modify active, then run the following code:

(vlax-dump-object  (vla-get-activetextstyle *doc*) t)

The report generated should show what the font file name is.  Also, beware of string case when comparing strings.

(IF (= (strcase (VLA-GET-FONTFILE STYLE)) "YOURFONT.TTF")...



CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: changing font of text style at startup
« Reply #2 on: September 19, 2005, 04:56:08 PM »
Yep, that should do it...
Here is another way to wrap it.
Code: [Select]
(defun c:style_Update (/ st)

  (vl-load-com)

  (vlax-for st
               (vla-get-textstyles
                 (vla-get-activedocument (vlax-get-acad-object))
               )
    ;;  Old font file in CAPS
    (if (= (strcase (vla-get-fontfile st)) "CGOR45XF.TTF")
      (vla-put-fontfile st "cgor45xf.shx") ; New font file name
    )
  )
  (princ)
)

(c:style_Update) ; run it
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

rkitek

  • Guest
Re: changing font of text style at startup
« Reply #3 on: September 19, 2005, 05:17:03 PM »
Thanks for your help, works perfectly now!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: changing font of text style at startup
« Reply #4 on: September 19, 2005, 05:34:47 PM »
Heh, CAB,

Just a comment to acknowledge how much your code has changed in the last couple of years.

I know you may come over all blushy and blame some of the guys here, but I see it primarily as a result of the effort you put in.

great stuff !!

Regards
Kerry
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: changing font of text style at startup
« Reply #5 on: September 19, 2005, 05:56:12 PM »
Thank you sir. :-)

If you hang around here & AUGI long enough you gota learn somethin.
As you said I do practice a lot. Although posting a lot of code shows your
progress you risk a failure now & then. But I think it's worth the risk.

Now if I could only learn to spel. :-D
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Hugo

  • Bull Frog
  • Posts: 431
Re: changing font of text style at startup
« Reply #6 on: November 12, 2012, 08:23:43 AM »
For me this does not work, the style is not changed.
Please help

Bei mir funktioniert das nicht der Stil wird nicht geändert.
Bitte um Hilfe


Code: [Select]
(defun c:style_Update (/ st)

  (vl-load-com)

  (vlax-for st
               (vla-get-textstyles
                 (vla-get-activedocument (vlax-get-acad-object))
               )
    ;;  Old font file in CAPS
    (if (= (strcase (vla-get-fontfile st)) "ARIAL UNICODE MS.tff")  ;;"ARIALUNI.TTF")
      (vla-put-fontfile st "ARIAL.ttf") ; New font file name
    )
  )
  (princ)
)

(c:style_Update) ; run it

Because the font is much too large
Denn die Schrift ist viel zu gross

Why is the font does not replace what is wrong
Thank you

Warum wird die Schrift nicht ersetzt wo liegt der Fehler
Danke
« Last Edit: November 14, 2012, 12:42:39 AM by Hugo »

Hangman

  • Swamp Rat
  • Posts: 566
Re: changing font of text style at startup
« Reply #7 on: November 14, 2012, 02:58:51 PM »
Hello Hugo,

Just a guess here as I'm not quite proficient with vlisp yet but you have the code
Code: [Select]
(if (= (strcase (vla-get-fontfile st))Notice the keyword "strcase" which means you are matching the font by the case (upper case, lower case)
So the question here then 'is your fonts lower case or upper case?'
Code: [Select]
"ARIAL UNICODE MS.tff"  This is uppercase with a lowercase extension ".ttf"
Code: [Select]
"ARIALUNI.TTF"  This is uppercase all the way.

Perhaps I am misleading here, but according to the 'help' files,
Quote
strcase, Returns a string where all alphabetic characters have been converted to uppercase or lowercase
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hugo

  • Bull Frog
  • Posts: 431
Re: changing font of text style at startup
« Reply #8 on: November 15, 2012, 12:57:47 AM »
Hallo Hangman

That's also not get the following error

Quote
Befehl: STYLE_UPDATE
; Fehler: Automatisierungsfehler Dateifehler

Das ist es auch nicht bekomme folgende Fehlermeldung

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: changing font of text style at startup
« Reply #9 on: November 15, 2012, 10:14:07 AM »
Try this:
Code: [Select]
;;  returns nil if not found, else font name if found
;;  TTF file names should have the extension already added
;;  "romant.ttf"
(defun find_font (fname / path font$)
  (setq path (getenv "windir"))
  (if (vl-position (substr path (strlen path)) '("/" "\\"))
    (setq path (strcat path "FONTS\\"))
    (setq path (strcat path "\\FONTS\\"))
  )
  (setq font$ (if (wcmatch fname "*\\*,*`.*")
                fname
                (strcat fname ".shx")
              )
  )

  (cond
    ((findfile font$) font$)
    ((and path (findfile (strcat path font$))) font$)
    (t nil)
  )
)

(find_font "ARIALUNI.TTF")
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: changing font of text style at startup
« Reply #10 on: November 15, 2012, 10:19:41 AM »
As Hangman suggested:
Code: [Select]
(defun c:style_update (/ st)
  (vl-load-com)
  (vlax-for st (vla-get-textstyles (vla-get-activedocument (vlax-get-acad-object)))
    ;;  Old font file in CAPS
    (if (= (strcase (vla-get-fontfile st)) "ARIAL UNICODE MS.TTF")
      ;;"ARIALUNI.TTF")
      (vla-put-fontfile st "ARIAL.ttf") ; New font file name
    )
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: changing font of text style at startup
« Reply #11 on: November 15, 2012, 10:59:48 AM »
@CAB,
Here is an alternative method to retrieve the Fonts path:

Code - Auto/Visual Lisp: [Select]
  1. ;; Special Folder  -  Lee Mac
  2. ;; Queries the WshSpecialFolders collection for the specified folder
  3. ;; Ref: http://msdn.microsoft.com/en-us/library/9x9e7edx%28v=vs.85%29.aspx
  4.  
  5. (defun LM:SpecialFolder ( folder / res spf wsh )
  6.     (setq res
  7.         (vl-catch-all-apply
  8.             (function
  9.                 (lambda ( )
  10.                     (setq wsh (vlax-get-or-create-object "wscript.shell")
  11.                           spf (vlax-get-property wsh 'specialfolders)
  12.                     )
  13.                     (vlax-invoke-method spf 'item folder)
  14.                 )
  15.             )
  16.         )
  17.     )
  18.     (if spf  (vlax-release-object  spf))
  19.     (if wsh  (vlax-release-object  wsh))
  20.     (if (not (vl-catch-all-error-p res))
  21.         res
  22.     )
  23. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (LM:SpecialFolder "Fonts")
  2. "C:\\Windows\\Fonts"

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: changing font of text style at startup
« Reply #12 on: November 15, 2012, 11:18:31 AM »
Thanks Lee  8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Hugo

  • Bull Frog
  • Posts: 431
Re: changing font of text style at startup
« Reply #13 on: November 16, 2012, 03:05:13 AM »
Hallo CAB

Scripture is found.


Quote
Befehl: (find_font "ARIALUNI.TTF")
"ARIALUNI.TTF"


Schrift wird gefunden.

eldanimal

  • Mosquito
  • Posts: 16
Re: changing font of text style at startup
« Reply #14 on: November 05, 2017, 03:15:42 PM »
I think I am having problems similar to Hugo, and mein Deutsch ist nicht so gut, but I'm not sure this thread fixed his problem.

I am using the following w/ just modified fonts from CAB's original code w/out success:

(defun c:style_Update (/ st)

  (vl-load-com)

  (vlax-for st
               (vla-get-textstyles
                 (vla-get-activedocument (vlax-get-acad-object))
               )
    ;;  Old font file in CAPS
    (if (= (strcase (vla-get-fontfile st)) "SIMSUNB.TTF")
      (vla-put-fontfile st "SIMSUN.TTC") ; New font file name
    )
  )
  (princ)
)

(c:style_Update) ; run it

On load I get the following: ; error: Automation Error. Filer error
This is the same as: ; Fehler: Automatisierungsfehler Dateifehler

Also, Hugo's translator read that "Scripture is found", and that sure sounds like a resolution....Amen, Amen, but I think he really meant that the find_font script is just finding the Font.

I have also run the find_font script on both of the attached, neither finds "SIMSUN" and both find "SIMSUN.TTC", but the style_update script is not working on either.

I also tried removing strcase from the style_update script, and the filer error disappeared, but the results did not change.

Thank you all for any help,
Dan

eldanimal

  • Mosquito
  • Posts: 16
Re: changing font of text style at startup
« Reply #15 on: November 05, 2017, 05:25:11 PM »
OK, so i think that my problem is the font i am trying to use, SimSun, is peculiar, it is a ttc rather than a ttf or shx.  I added to CAB's code with a different "put wrap" (i am making up terminology because I know very little) from Fixo on a similar problem, and I changed the input font to simplex_.ttf to test.  I also tried to use simsun.ttc, and it did replace the font in the styles, but it did not recognize the "SimSun" font I would be able to manually select if I went through all my different styles in all our different template drawings.  Instead it gave me the attached jpg.

Here is the code I am currently working with:
Code: [Select]
(defun c:style_Update (/ st)

  (vl-load-com)

  (vlax-for st
               (vla-get-textstyles
                 (vla-get-activedocument (vlax-get-acad-object))
               )
    ;;  Old font file in CAPS
    (if (= (strcase(vla-get-fontfile st)) "SIMSUNB.TTF")
      (vl-catch-all-apply
      '(lambda ()
(progn
   (setq fontfile (findfile (strcat (getenv "WINDIR") "\\fonts\\Simplex_.ttf")))
   (vla-put-fontfile st fontfile)
   )
)
    )
  )
  )
  (princ)
)


I guess I am actually looking for a way to access the ttf's in simsun.ttc, but like I said, I really don't know very much.

Thank you all, again,
Dan

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: changing font of text style at startup
« Reply #16 on: November 06, 2017, 04:08:40 AM »
If you select the SimSun.ttc manually, what is then the fontfile property of the text tyle?

eldanimal

  • Mosquito
  • Posts: 16
Re: changing font of text style at startup
« Reply #17 on: November 06, 2017, 09:40:29 AM »
If you select the SimSun.ttc manually, what is then the fontfile property of the text tyle?

If I manually change the font in the text style to SimSun and then run this
Code: [Select]
(vl-load-com)
(defun c:cf()
    ;; This example returns the current setting of
    ;; the FontFile property. It then changes the value, and
    ;; finally resets the value back to the original setting.
    (setq acadObj (vlax-get-acad-object))
    (setq doc (vla-get-ActiveDocument acadObj))
 
    (setq textStyle1 (vla-get-ActiveTextStyle doc))
   
    ;; Retrieve the current FontFile value
    (setq currFontFile (vla-get-FontFile textStyle1))
    (alert (strcat "The current value for FontFile is " currFontFile))
)
....I get the first screen capture, but then when i run this
Code: [Select]
;;  returns nil if not found, else font name if found
;;  TTF file names should have the extension already added
;;  "romant.ttf"
(defun find_font (fname / path font$)
  (setq path (getenv "windir"))
  (if (vl-position (substr path (strlen path)) '("/" "\\"))
    (setq path (strcat path "FONTS\\"))
    (setq path (strcat path "\\FONTS\\"))
  )
  (setq font$ (if (wcmatch fname "*\\*,*`.*")
                fname
                (strcat fname ".shx")
              )
  )

  (cond
    ((findfile font$) font$)
    ((and path (findfile (strcat path font$))) font$)
    (t nil)
  )
)
....I get the second capture.




roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: changing font of text style at startup
« Reply #18 on: November 06, 2017, 10:23:32 AM »
Try changing the FontFile property to "SimSun.ttf" programmatically. And if that works revise the 'findfile' portion of the code.

eldanimal

  • Mosquito
  • Posts: 16
Re: changing font of text style at startup
« Reply #19 on: November 06, 2017, 10:37:33 AM »
Try changing the FontFile property to "SimSun.ttf" programmatically. And if that works revise the 'findfile' portion of the code.

It does not work. I think I am stuck hoping there is a VLISP process for unpacking a .ttc that I just can't find.

Thank you for the responses, Roy. I think I have a clearer target now.

eldanimal

  • Mosquito
  • Posts: 16
Re: changing font of text style at startup
« Reply #20 on: November 21, 2017, 03:31:05 PM »
I did manage to break through on this....unfortunately, I was not very methodical, and I'm uncertain of exactly how I pulled it off, but I think that it could be worthwhile for someone down the road to know what I did to accomplish what I wanted, so here goes....

First, I tried to download a copy of the SimSun.ttf package w/out the *.ttc package.  Windows seemed to reject the installation of that file, because it knew it was already there.

Next, I searched for other programs that handle ttc's and found that the filetype uses an index for each of the ttf components of the ttc....so i started to mess w/ the code I was using to try to call the simsun.ttf. i.e.,
(setq fontfile (findfile (strcat (getenv "WINDIR") "\\fonts\\SimSun.ttf, 0")))
(setq fontfile (findfile (strcat (getenv "WINDIR") "\\fonts\\SimSun.ttf; 0")))
(setq fontfile (findfile (strcat (getenv "WINDIR") "\\fonts\\SimSun.ttf[0]")))

And then I threw a random Hail Mary:  (setq fontfile (findfile (strcat (getenv "WINDIR") "\\fonts\\SimSun.ttf")))...and the code worked.

I went back and checked the fonts, and I now had NSimSun.ttc that included a NSimSun.ttf and a SimSun.ttf and a SimSun_0.ttf.   I was unsure why the above code worked considering, but it did.

Now after 20 restarts or so, I have NSimSun.ttc and SimSun.ttc in my fonts, both contain the NSimSun.ttf and the SimSun.ttf with no independent SimSun_0.ttf to be found.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: changing font of text style at startup
« Reply #21 on: November 21, 2017, 03:45:34 PM »
Also check the registry to make sure the font is using the correct ttf file. Those numbered ttf files come from installing a font multiple times and if you install a wonky one, then you're hosed.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts

If you're using Windows 10 and want to see the font files individually, you can paste this into Windows Explorer to do so:
\\%computername%\c$\Windows\Fonts
« Last Edit: November 21, 2017, 03:54:14 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

eldanimal

  • Mosquito
  • Posts: 16
Re: changing font of text style at startup
« Reply #22 on: November 21, 2017, 04:13:34 PM »
Thank you for the heads up, Ron.....I do have the SimSun_0.ttf file in there.  I don't think I am in trouble, because I ran the code on a shared file that another user was able to open, use and plot w/out any trouble or font installations.  Is that a file I can simply delete for the future? 

I'm thinking that I was actually ready to roll when I tried to install the plain SimSun.ttf despite Windows' complaint, and my fancy index calls just kept me occupied for a few hours..... :idiot2: