(defun FontLetterWidth (Doc TextObj / MdSpc StyCol Sty FontName DictCol DictObj cnt CurLtr tempText ll ur Dist FontWidthList
WidthList StyName String TextWd TextHt StyDictObj tempStrList tempList tempStr)
; Returns a list of distances for the text string of the object supplied. The distances are that of each
; letter in the string starting from the lower left bounding box point of the text, when the text is rotated
; to 0.0 degrees.
; Idea from 'SomeCallMeDave' @ theswamp. Thanks again.
(setq StyName (vla-get-StyleName TextObj))
(setq String (vlax-get TextObj (if (= (vla-get-ObjectName TextObj) "AcDbAttributeDefinition") "TagString" "TextString")))
(setq TextWd (vla-get-ScaleFactor TextObj))
(setq TextHt (vla-get-Height TextObj))
(setq TextLen (strlen String))
(setq cnt -1)
(while (setq cnt (vl-string-search "%%" String (setq cnt (1+ cnt))))
(setq tempStrList (cons (cons (1+ cnt) (setq tempStr (strcase (substr String (1+ cnt) 3)))) tempStrList))
)
(setq MdSpc (vla-get-ModelSpace Doc))
(setq StyCol (vla-get-TextStyles Doc))
(setq Sty (vla-Item StyCol StyName))
(setq FontName
(if (findfile (vla-get-fontFile Sty))
(vl-filename-base (vla-get-fontFile Sty))
(vl-filename-base (getvar "fontalt"))
)
)
;(setq DictCol (vla-get-Dictionaries Doc))
;(if (vl-catch-all-error-p (setq DictObj (vl-catch-all-apply 'vla-Item (list DictCol "MyFontWidthDict"))))
; (setq DictObj (vla-Add DictCol "MyFontWidthDict"))
;)
;(if (vl-catch-all-error-p (setq StyDictObj (vl-catch-all-apply 'vla-Item (list DictObj StyName))))
; (setq StyDictObj (vla-Add DictObj StyName))
;)
(setq cnt 1)
(while (<= cnt TextLen)
(if (setq tempList (assoc cnt tempStrList))
(progn
(setq CurLtr (cdr tempList))
(setq cnt (+ cnt 2))
)
(setq CurLtr (substr String cnt 1))
)
(if (not (assoc CurLtr FontWidthList))
(progn
(cond
((= CurLtr " ")
(setq tempText (vlax-invoke MdSpc 'AddText "AA" '(0.0 0.0 0.0) 1.0))
(vla-put-Height tempText 1.0)
(vla-put-ScaleFactor tempText 1.0)
(vlax-put tempText 'Normal '(0. 0. 1.))
(vla-put-StyleName tempText StyName)
(vla-GetBoundingBox tempText 'll 'ur)
(setq ll (safearray-value ll))
(setq ur (safearray-value ur))
(setq Dist (distance (cons (car ll) (cdr ur)) ur))
(vla-put-TextString tempText (strcat "A" CurLtr "A"))
(vla-GetBoundingBox tempText 'll 'ur)
(setq ll (safearray-value ll))
(setq ur (safearray-value ur))
(setq Dist (* TextWd (* TextHt (- (distance (cons (car ll) (cdr ur)) ur) Dist))))
(setq FontWidthList (cons (cons CurLtr Dist) FontWidthList))
(vla-Delete tempText)
)
((and (equal cnt 3) (or (= CurLtr "%%U") (= CurLtr "%%O")))
(setq tempText (vlax-invoke MdSpc 'AddText "A" '(0.0 0.0 0.0) 1.0))
(vla-put-Height tempText 1.0)
(vla-put-ScaleFactor tempText 1.0)
(vlax-put tempText 'Normal '(0. 0. 1.))
(vla-put-StyleName tempText StyName)
(vla-GetBoundingBox tempText 'll 'ur)
(setq ll (safearray-value ll))
(setq ur (safearray-value ur))
(setq tempPt (cons (car ll) (cdr ur)))
(vla-put-TextString tempText (strcat CurLtr "A"))
(vla-GetBoundingBox tempText 'll 'ur)
(setq ll (safearray-value ll))
(setq ur (safearray-value ur))
(setq Dist (* TextWd (* TextHt (distance (cons (car ll) (cdr ur)) tempPt))))
(setq FontWidthList (cons (cons CurLtr Dist) FontWidthList))
(vla-Delete tempText)
)
(T
(setq tempText (vlax-invoke MdSpc 'AddText CurLtr '(0.0 0.0 0.0) 1.0))
(vla-put-Height tempText 1.0)
(vla-put-ScaleFactor tempText 1.0)
(vla-put-StyleName tempText StyName)
(vlax-put tempText 'Normal '(0. 0. 1.))
(vla-GetBoundingBox tempText 'll 'ur)
(setq ll (safearray-value ll))
(setq ur (safearray-value ur))
(setq Dist (distance (cons (car ll) (cdr ur)) ur))
(vla-put-TextString tempText (strcat CurLtr CurLtr))
(vla-GetBoundingBox tempText 'll 'ur)
(setq ll (safearray-value ll))
(setq ur (safearray-value ur))
(setq Dist (* TextWd (* TextHt (- (distance (cons (car ll) (cdr ur)) ur) Dist))))
(setq FontWidthList (cons (cons CurLtr Dist) FontWidthList))
(vla-Delete tempText)
)
)
)
)
(if (and (not (equal cnt 3)) (or (= CurLtr "%%U") (= CurLtr "%%O")))
(repeat 3
(setq WidthList (cons 0.0 WidthList))
)
(if (equal (strlen CurLtr) 3)
(repeat 3
(setq WidthList (cons (/ (cdr (assoc CurLtr FontWidthList)) 3.0) WidthList))
)
(setq WidthList (cons (cdr (assoc CurLtr FontWidthList)) WidthList))
)
)
(setq cnt (1+ cnt))
)
;(print FontWidthList)
(list (reverse WidthList) tempStrList)
)