Author Topic: Width of block multiline attribute  (Read 197 times)

0 Members and 1 Guest are viewing this topic.

Spainenins

  • Mosquito
  • Posts: 3
Width of block multiline attribute
« on: April 17, 2024, 11:22:44 AM »
Hello!
I have made a lisp routine that does the following:
1)adds a block reference to the model space;
2)changes the value (text string) of an attribute;
3)gets the real width (through bounding box) of the attribute;
4)changes a dynamic block property so that the width of the block reference fits the new text.

It works beautifully, when the attribute is a single-line one. But sadly my colleagues requested I make it a multi-line attribute, so they could use subscripts and superscripts in it.
The problem seems to be that Bounding box of a multi-line attribute is always as if it had it's default text string.
Maybe there is another way to get the width of a multi-line attribute?
Code below:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ko-addmyblock ()
  2.   (setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))    ;get modelspace of active document
  3.   (setq blkname "MyBlock")      ;name of my block
  4.   (setq attrname "NR")  ;block attribute name (TagString) that I want to change
  5.   (setq newattrvalue "MyNewValue")      ;the new value I want to give to the attribute
  6.   (setq propname "Width")       ;block property name that is supposed to change depending on width of the attribute
  7.   (setq pt (getpoint "Insertion point for block"))      ;get insertion point for the block
  8.   (setq blkobj (vla-insertblock mspace (vlax-3d-point pt) blkname 1 1 1 0))     ;insert block
  9.  
  10.   ;;get the relevant attribute as vla-object
  11.   (setq attrobj
  12.          (car
  13.            (vl-member-if
  14.              '(lambda (blkattrobj) (= (vlax-get-property blkattrobj "TagString") attrname));lambda
  15.              (vlax-safearray->list
  16.                (vlax-variant-value
  17.                  (vlax-invoke-method blkobj "GetAttributes")))
  18.              );vl-member-if
  19.            );car
  20.         );setq
  21.  
  22.   (vla-put-textstring attrobj newattrvalue)     ;change text of the relevant attribute
  23.   (vla-getboundingbox attrobj 'll 'ur)  ;get bounding box of attribute
  24.   (setq attrwidth (apply '- (mapcar 'car (mapcar 'safearray-value (list ur ll)))))      ;get width of the bounding box
  25.  
  26.   ;;get the relevant property as vla-object
  27.   (setq propobj
  28.          (car
  29.            (vl-member-if
  30.              '(lambda (blkpropobj) (= (vlax-get-property blkpropobj "PropertyName") propname));lambda
  31.              (vlax-invoke blkobj "GetDynamicBlockProperties")
  32.              );vl-member-if
  33.            );car
  34.         );setq
  35.  
  36.   (vla-put-value propobj (+ 2.0 attrwidth))     ;change it's value
  37.   (princ)
  38. );defun

The block is a really simple one with one attribute called "NR", and one custom property called "Width".


EDIT (John): Fixed code tag.
« Last Edit: April 17, 2024, 02:26:43 PM by JohnK »

BIGAL

  • Swamp Rat
  • Posts: 1418
  • 40 + years of using Autocad
Re: Width of block multiline attribute
« Reply #1 on: April 17, 2024, 09:53:02 PM »
I just made a simple mtext with multi line, picked 2 points for the mtext, the bounding box returned the 2 points which was way wider than the actual text.

Which leads me to maybe 2 ideas the 1st is need mtext to be with extents pulled back to just touch right side. The other way is to use mtext pt pt which will force an auto width.

Ok but the good news try this simple mod before running bounding box.

Code: [Select]
(setq ent (entget (car (entsel "\nPick object"))))
(entmod (subst (cons 41 0.0) (assoc 41 ent) ent))
A man who never made a mistake never made anything