13 Jan 2021

Use custom Fonts on Design Automation

Most products work with fonts that are installed on the system. If you check how to use custom fonts in Inventor or Revit the articles will tell you to install that font on Windows. That is not an option on Design Automation as the core console app runs in a sandbox. 
In AutoCAD, you could just add the font to the SupportPath you specify in the app bundle - see How to use user defined font on Forge design automation with AutoCAD Plot API

Inventor (and might be the same for Revit) will just use whatever fonts are available on the system and won't look for font files (*.otf, *.ttf, etc) if a font is not available on the system. 

There is a class in .NET which enables you to load custom fonts into the application process: System.Drawing.Text.PrivateFontCollection
Once the custom font is loaded in Inventor through an in-process add-in, Inventor will be able to use that font as well. ?

This mechanism also works on Design Automation - I assume it would work for AutoCAD and Revit too.

In order to test this, I went on fonts.google.com to find a font that can easily be recognized, e.g. Dancing Script, and downloaded it.

Dancing Script font on google

Installed it.

Installing custom font

Then I used it in a new Inventor drawing - see picture on top.

If I try to save this drawing to PDF using Design Automation, then I'll get this message in the work item report file:

[01/13/2021 10:33:13]     InventorCoreConsole.exe Warning: 0 : Inventor message: Published with a warning(s)
[01/13/2021 10:33:13]     InventorCoreConsole.exe Warning: 0 : Inventor inner xml: <Warning Message="Cannot create font Dancing Script" />

And the result will not be what I wanted.

Font problem in PDF  

I provided both the drawing (CustomFontTest.dwg) and font file (DancingScript-Regular.ttf) in the Contents folder of my app bundle, but as expected, InventorCoreConsole.exe did not load the font and so a default font was used instead.

In the commandLine of my activity, the first custom argument provides the path to the app bundle (\"$(appbundles[DaSamplePlugin].path)\"), so I can retrieve it from the NameValueMap of the RunWithArguments() entry function using map.Item["_1"] - in the case of other products you could follow the blog post Handle command line arguments

If we load the custom font before opening our drawing then Inventor server will be able to use it:

public void RunWithArguments(Document doc, NameValueMap map)
    string bundlePath = map.Item["_1"] as string;
    string contents = System.IO.Path.Combine(bundlePath, @"DaSamplePlugin.bundle\Contents");

    string dwgPath = System.IO.Path.Combine(contents, "CustomFontTest.dwg");
    string fontPath = System.IO.Path.Combine(contents, "DancingScript-Regular.ttf");

    PrivateFontCollection pfc = new PrivateFontCollection();

    doc = inventorApplication.Documents.Open(dwgPath, false);

    doc.SaveAs("result.pdf", true);

This time we won't get a warning in the work item's report file and the result is good too ? - see picture on top


Related Article