Bare bones ...
(defun mpx-get-bounding-box ( object / a b )
(vl-catch-all-apply 'vlax-invoke-method (list object 'GetBoundingBox 'a 'b))
(if a (mapcar 'vlax-safearray->list (list a b)))
)
(defun mpx-restrict-attrib-width ( block-reference max-width / bb dx ratio flag )
(and
(eq 'vla-object (type block-reference))
(eq "AcDbBlockReference" (vla-get-objectname block-reference))
(eq :vlax-true (vla-get-hasattributes block-reference))
(foreach a (vlax-invoke block-reference 'GetAttributes)
(and
(setq bb (mpx-get-bounding-box a))
(setq dx (abs (apply '- (mapcar 'car bb))))
(< 1.0 (setq ratio (/ dx max-width)))
(setq flag t)
(vla-put-scalefactor a (* (vla-get-scalefactor a) (/ 1.0 ratio)))
)
)
(if flag (vla-update block-reference))
)
(princ)
)
(defun c:foo ( / ename block-reference max-width )
(and
(vl-load-com)
(setq ename (car (entsel "\nSelect an attributed block: ")));;select attribute
(setq block-reference (vlax-ename->vla-object ename));;set block reference variable
(eq "AcDbBlockReference" (vla-get-objectname block-reference));;get block reference name ???
(eq :vlax-true (vla-get-hasattributes block-reference));;if block has attributes, do the following???
(progn
(initget (logior 1 2 4));;Not sure what this is doing. I'll do some reading.
(setq max-width (getdist "\nEnter the maximum attribute width: "));;set the max width to user selected width
)
(mpx-restrict-attrib-width block-reference max-width);;keep attribute width inside block reference maximum width
)
(princ)
)
This works nicely. Please, check my comments. I'm not sure I understand exactly what's happening.
It would be nice if this occurred without user input.
I'll try to explain the process a little bit better...
User fills out a card in the EDM software which is linked to the attribute in AutoCAD. TitleLine1 can be exceptionally long (we're working on shortening that on the EDM side right now)
User completes the card creation and closes the card.
User selects the drawing file to open from the EDM software
AutoCAD opens and the title block is filled out automagically via the CARD from the EDM.
TileLine1 has a set width of .85, if the CARD in the EDM has a TitleLine1 that's too big to fit, it overruns the titleblock.
User has to manually adjust the attribute to fit. Instead of manually here, maybe upon startup, check TitleLine1 if it's outside the title block width (bounding box? Maybe, I don't know)
It works fine the way it is and the boss was expecting a simple button push. So, I can give him what he wants with credit to MP.