I had been looking for a way to create text styles, especially annotative text styles without using the style command for various reasons and found there wasn't really anything out there that I could find, so in checking various places, this is what I have come up with:
(defun CW:CreateTextStyle (StyleName Font Annotative Height WidthFactor MakeActive / *ThisDrawing* *Styles* Obj DestinationTypeFace Font_Path xtype xdata TypeFace Bold Italic charSet PitchandFamily)
;Create Text Style
;2022-04-11 - Chris Wade - cmwade77 on cadtutor.net and theswamp.org
(vl-load-com)
(setq *ThisDrawing* (vla-get-activedocument (vlax-get-acad-object))
*Styles* (vla-get-textstyles *ThisDrawing*)
Obj (vla-add *Styles* StyleName)
)
(if Obj
(progn
(if (= (strcase (vl-filename-extension Font)) (strcase ".ttf"))
(progn
(if (not (setq Font_Path (findfile (strcat (getenv "WINDIR") "\\fonts\\" Font))))
(progn
(setq Font_Path (findfile Font))
(setq Font_Path (getfiled "Select Font File" "ttf"))
)
)
)
)
(if Font_Path
(setq DestinationTypeFace (vl-filename-base Font))
(progn
(alert "Couldn't find font path, exiting!")
(repeat 3
(vla-purgeall *ThisDrawing*)
)
(exit)
)
)
(vla-GetFont Obj 'typeFace 'Bold 'Italic 'charSet 'PitchandFamily)
(vla-put-fontfile Obj Font_Path)
(vla-SetFont Obj DestinationTypeFace Bold Italic charSet PitchandFamily)
(vla-put-height Obj Height)
(vla-put-width Obj WidthFactor)
(vla-put-ObliqueAngle Obj 0.0)
(vla-put-TextGenerationFlag Obj 0)
;Code to make Annotative adapted from codw by ksperopoulos at https://www.cadtutor.net/forum/topic/59375-making-text-style-annotative/?do=findComment&comment=491676
(if Annotative
(progn
(setq xtype (vlax-make-safearray vlax-vbInteger '(0 . 5))
xdata (vlax-make-safearray vlax-vbVariant '(0 . 5))
)
(vlax-safearray-put-element xtype 0 1001)
(vlax-safearray-put-element xdata 0 "AcadAnnotative")
(vlax-safearray-put-element xtype 1 1000)
(vlax-safearray-put-element xdata 1 "AnnotativeData")
(vlax-safearray-put-element xtype 2 1002)
(vlax-safearray-put-element xdata 2 "{")
(vlax-safearray-put-element xtype 3 1070)
(vlax-safearray-put-element xdata 3 1)
(vlax-safearray-put-element xtype 4 1070)
(vlax-safearray-put-element xdata 4 1)
(vlax-safearray-put-element xtype 5 1002)
(vlax-safearray-put-element xdata 5 "}")
(vla-SetXData Obj xtype xdata)
)
)
(if MakeActive
(vla-put-activetextstyle *ThisDrawing* Obj)
)
)
)
Obj
)
It returns the text style as an ActiveX object so that you can make further adjustments if needed to minimize the number of parameters needed.
Please let me know what you think or if there is a better way to do this.
EDIT: Later versions in this thread will also change existing text styles.