Changing the Font in a SolidWorks Macro

Hi all

I have a question, I'm working on a macro in solidworks that allows you to write a text and then apply a material removal to it. The macro works but I don't understand how to change the font of my text in my macro. Do you have any solutions?

Thanks in advance

Hello
Post your code, it's easier to adapt later.

2 Likes

Hello

The ITextFormat object of the API is used to manage text fonts in SolidWorks.
If it's sketch text, the sketch should be being edited.

In the very short example attached, you must also have selected the text to apply the changes to it.

Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swSelMgr                    As SldWorks.SelectionMgr
    Dim SkText                      As SldWorks.SketchText
    Dim swTextFormat                As SldWorks.TextFormat
    Dim bRet                        As Boolean
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set SkText = swSelMgr.GetSelectedObject6(1, -1)
        
        Set swTextFormat = SkText.GetTextFormat
        'Change text to be 10mm, bold, italic, and Comic Sans MS font
        swTextFormat.TypeFaceName = "Comic Sans MS"
        swTextFormat.CharHeight = 0.01
        swTextFormat.Bold = True
        swTextFormat.Italic = True
        bRet = SkText.SetTextFormat(False, swTextFormat)
End Sub

Kind regards.

3 Likes

Thank you it works very well now