Jump to content

Change All Fonts in File


Recommended Posts

The attached script will change the font of all text objects in a drawing, even objects that are part of a group, symbol or viewport annoation. A dialog box allows the user to select the desired font.

Depending on how a PluginObject has been written it might or might not be able to change the font of text that is part of a PIO.

Free for your use.

BACKUP your file before use. No Warranty is Expressed or Implied. Do not operate heavy machinery while using this script. If a rash or sneezing develops for more than 4 hours consult a doctor immediately.

Procedure ChangeAllFonts;

{Changes the font of all text on a drawing to the selected font}
{Traverses into groups, symbols and viewport annotations}

{January 6, 2012}
{? 2012, Coviana, Inc - Pat Stanford pat@coviana.com}
{Licensed under the GNU Lesser General Public License}



var	Hd		:Handle;
Font	:String;
dInt	:Integer;
dReal	:Real;

Procedure ChangeFont(Hd:Handle);

Begin
if GetType(Hd)= 10 then 
begin
	SetTextFont(Hd, 0,len(gettext(Hd)),GetFontID(Font));
end
end;


Begin
Font:='Arial';
FormatTextDialog(Font,dInt,dReal,dInt,dReal,dInt,dInt,62);
ForEachObject(ChangeFont,(INSYMBOL & INOBJECT & INVIEWPORT & (ALL)));
end;

Run(ChangeAllFonts);

Link to comment
  • 6 years later...
  • 2 years later...

Hi Andrew,

   I adapted Pat's script to also set the Point Size of the text. This will set all objects to the same size which could make some documents look weird. You could break the script into two scripts to give you more control. To just set the point size, comment out (or remove) the SetTextFont() command.

 

   You can also make the script work only on SELECTED text objects only by changing the  "& ALL"  to  "& SEL"  in the ForEachObject() command near the bottom.

PROCEDURE ChangeAllFonts;
{ Changes the font of all text on a drawing to the selected font }
{ Traverses into groups, symbols and viewport annotations }

{ January 6, 2012 }
{ © 2012, Coviana, Inc - Pat Stanford pat@coviana.com }
{ Licensed under the GNU Lesser General Public License }

{ 17 February 2020 - R. Mullin }
{ Added ability to set the Point Size of all text objects at the same time. }
{ Same GNU License as above. }

VAR
	Hd :Handle;
	Font :String;
	FontID, dInt :Integer;
	PointSize, dReal	:Real;

	Procedure ChangeFont(Hd:Handle);
	Begin
		if (GetTypeN(Hd) = 10) then begin
			SetTextFont(Hd, 0, len(gettext(Hd)), GetFontID(Font));
			SetTextSize(Hd, 0, len(gettext(Hd)), PointSize);
		end;
	End;

BEGIN
	Font := 'Arial';
	FormatTextDialog(Font, dInt, PointSize, dInt, dReal, dInt, dInt, 60);	{ correct Point Size ommission - RJM 7 Oct 2020 }
	ForEachObject(ChangeFont, INSYMBOL & INOBJECT & INVIEWPORT & ALL);
END;
Run(ChangeAllFonts);

 

Raymond

 

Edited by MullinRJ
  • Like 1
Link to comment
  • 7 months later...

@MullinRJ @Pat Stanford 

 

Is it possible to modify this script to replace a single font - rather than every font in the drawing?

i.e. if the existing Font = "Arial" then replace it with "xxx" ?

 

Our office formerly used a font that no longer functions properly. It was everywhere, inside symbols, etc.

Rather than trying to replace every object one by one, we opted to re-map the font to a new one.  (This was before Text Styles)

 

This basically solved our problem.  However, exported files revert back to the offending font, so the display goes wonky. It would be better to replace all instances of the corrupt font and scripting looks like the best way to do it.

 

Link to comment
10 minutes ago, MullinRJ said:

Yes,

 

BRB,

Raymond

 

Excellent.

 

I assume that I would need to modify this portion of the script to also limit the selection of objects based upon it's font.

 

On 1/6/2012 at 12:17 PM, Pat Stanford said:

Begin if GetType(Hd)= 10 then begin SetTextFont(Hd, 0,len(gettext(Hd)),GetFontID(Font)); end end;

 

I see that Type 10 = Text Objects, but I don't know how to further limit the selection via font.

Could you assist me in that regard?

 

Link to comment

Hi @Taproot ,

 

41 minutes ago, Taproot said:

I see that Type 10 = Text Objects, but I don't know how to further limit the selection via font.

Could you assist me in that regard?

 

   You have a good eye, but there is not an easy way in VS to describe how to determine the font assigned to a text block. Rather than try to guide you through it, I made the changes to the previous script and posted it below. As you can see, it still took me about an hour to do this and I still took the easy way out by only checking the first character of a text block to check the font assignment. See next paragraph.

 

   I modified the previous script to only change the font type of Arial text blocks. You can specify the font to be changed by changing the constant "kFontNm" at the top of the program. There is one caveat, this script assumes that if a text block starts with a character in the Arial font, the whole text block is Arial. This script does not change individual characters within a text block. That can be done, but requires a bit more time to code.

 

<<< THIS SCRIPT IS LIGHTLY TESTED — VERY LIGHTLY TESTED.  USE AT YOUR OWN RISK,  AND PLEASE TRY IT ON A TEST FILE FIRST. >>>

 

   Also, I corrected a mistake in the previous script that did not pass back the point size in variable "PointSize" from the FormatTextDialog() procedure. That is now corrected in the above script and in the one posted below.

 

   If anyone finds any problems with this script, please write back and I'll have @PatStanford get right on it. 😉

 

PROCEDURE ChangeSomeFonts;
{ Changes the font of all text on a drawing to the selected font }
{ Traverses into groups, symbols and viewport annotations }

{ January 6, 2012 }
{ © 2012, Coviana, Inc - Pat Stanford pat@coviana.com }
{ Licensed under the GNU Lesser General Public License }

{ 17 February 2020 - R. Mullin }
{ Added ability to set the Point Size of all text objects at the same time. }
{ Same GNU License as above. }

{ 7 October 2020 - R. Mullin }
{ Only change Text Blocks with name specified in constant "kFontNm" to target font name "kNewFontNm". }
{ Same GNU License as above. }

CONST
	kFontNm = 'Arial';				{ change from this font name }
VAR
	Hd :Handle;
	NewFontNm :String;
	FontID, dummyInt :Integer;
	PointSize, dummyReal	:Real;

	Procedure ChangeFont(Hd :Handle);
	Var
		FID :Integer;
	Begin
		if (GetTypeN(Hd) = 10) then begin
			FID := GetTextFont(Hd, 0);	{ assumes all characters in txtBlk are the same fontID }
			if (FontID = FID) then begin
				SetTextFont(Hd, 0, len(GetText(Hd)), GetFontID(NewFontNm));
				SetTextSize(Hd, 0, len(GetText(Hd)), PointSize);
			end;		{ if (FontID = FID) }
		end;		{ if GetTypeN() }
	End;		{ ChangeFont }

BEGIN
	FontID := GetFontID(kFontNm);			{ existing FontID }
	FormatTextDialog(NewFontNm, dummyInt, PointSize, dummyInt, dummyReal, dummyInt, dummyInt, 60);
	ForEachObject(ChangeFont, INSYMBOL & INOBJECT & INVIEWPORT & ALL);
END;
Run(ChangeSomeFonts);

 

HTH,

Raymond

 

Link to comment
  • 5 months later...
  • 2 weeks later...

Not sure how I just saw this but this is awesome.  I know I posted a "wish list" for something like this integrated into VW a long way back but just this is great.  Long story short for me it helps in consistency when getting files from other incorporating into yours and suddenly text from .dwgs or other items don't all match.  Super nice.

Link to comment
  • 11 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...