• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

add attribute to an existing block

  • Thread starter Thread starter 77marco77
  • Start date Start date

77marco77

Guest
Hello.
I found this code:
HTML:
(defun c:add$ ( / ss i blk blks def attobj)
    (and
       (setq ss (ssget '((0 . "insert"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i)))))))
          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
       )
    )
    (foreach blk blks
         (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
         (setq attobj
            (vla-addattribute def
              8
              acattributemodelockposition
              "enter item #"
              (vlax-3d-point 72 84)
              "price\u+0020"
              "100"
            )
         )
         (vlax-put attobj 'alignment acalignmentmiddle) ;; 4
         (command "_.attsync" "_n" blk)
     )
    (princ)
)
(vl-load-com) (princ)
who: and does what should, that is, add the attribute with tag price, enter item message # and content 100 to one or more selected blocks. the attribute is inserted at the point of insertion of the block (then often just above the design) with half-center justification.

what I would like to understand (and be able to change) are:
  1. the justification: I would like to know what and where to write by middle center, half left and half right
  2. the location: we say that (0.0) is the insertion point of the block I would like to add the attribute for example to (0,-10) design unit
thank you in advance who will help me
 
(vlax-3d-point 72 84)
the coordinates relative to the insertion point of the block where the attribute is placed.
edit the 84 in 74 and get your -10 on y.
 
thanks for the answer, I tried but it does not work, the attribute is always placed on the base point of the block...
 
(vlax-put actbj 'alignment acalignmentmiddle); 4

if you delete the line the attribute is placed in coordination but does not change the alignment point.
with _battman solve in any case.
 
so it should go, to get the alignment in between, you have to add a vla-move.
Code:
(defun c:add$ ( / ss i blk blks def attobj)
    (and
       (setq ss (ssget '((0 . "insert"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i)))))))
          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
       )
    )
    (foreach blk blks
         (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
         (setq attobj
            (vla-addattribute def
              8
              acattributemodelockposition
              "enter item #"
              (vlax-3d-point 72 84)
              "price\u+0020"
              "pippo"
            )
         )
        (vlax-put attobj 'alignment acalignmentmiddle) ;; 4
        (vla-move attobj (vlax-3d-point 0 0)(vlax-3d-point 0 -10))
        (command "_.attsync" "_n" blk)
     )
        
    (princ)
)
(vl-load-com) (princ)
 
Last edited:
so it should go, to get the alignment in between, you have to add a vla-move.
Code:
(defun c:add$ ( / ss i blk blks def attobj)
    (and
       (setq ss (ssget '((0 . "insert"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i)))))))
          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
       )
    )
    (foreach blk blks
         (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
         (setq attobj
            (vla-addattribute def
              8
              acattributemodelockposition
              "enter item #"
              (vlax-3d-point 72 84)
              "price\u+0020"
              "pippo"
            )
         )
        (vlax-put attobj 'alignment acalignmentmiddle) ;; 4
        (vla-move attobj (vlax-3d-point 0 0)(vlax-3d-point 0 -10))
        (command "_.attsync" "_n" blk)
     )
       
    (princ)
)
(vl-load-com) (princ)
Thank you so much!
I must admit that I feel like a child who is witnessing the disappearance of the statue of freedom by David copperfield: small and without words:):giggle:.
 
(vlax-put actbj 'alignment acalignmentmiddle); 4

if you delete the line the attribute is placed in coordination but does not change the alignment point.
with _battman solve in any case.
thanks I tried and it would work, but having to do it for 6358 blocks it becomes a little long, thanks however for the idea and help!
 
so it should go, to get the alignment in between, you have to add a vla-move.
Code:
(defun c:add$ ( / ss i blk blks def attobj)
    (and
       (setq ss (ssget '((0 . "insert"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i)))))))
          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
       )
    )
    (foreach blk blks
         (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
         (setq attobj
            (vla-addattribute def
              8
              acattributemodelockposition
              "enter item #"
              (vlax-3d-point 72 84)
              "price\u+0020"
              "pippo"
            )
         )
        (vlax-put attobj 'alignment acalignmentmiddle) ;; 4
        (vla-move attobj (vlax-3d-point 0 0)(vlax-3d-point 0 -10))
        (command "_.attsync" "_n" blk)
     )
      
    (princ)
)
(vl-load-com) (princ)
still a little soda (maybe... )
the text height put to 2 (instead of 8), you can enter a width factor to 0.9 or do it later with another lisp? (I think I already have it)
 
Code:
(defun c:add$ ( / ss i blk blks def attobj)
    (and
       (setq ss (ssget '((0 . "insert"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i)))))))
          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
       )
    )
    (foreach blk blks
         (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
         (setq attobj
            (vla-addattribute def
              8
              acattributemodelockposition
              "enter item #"
              (vlax-3d-point 72 84)
              "price\u+0020"
              "pippo"
            )
         )
        (vlax-put attobj 'alignment acalignmentmiddle) ;; 4
        (vla-move attobj (vlax-3d-point 0 0)(vlax-3d-point 0 -10))
        (vla-put-scalefactor attobj 0.9)
        (command "_.attsync" "_n" blk)
     )
    (princ)
)
(vl-load-com) (princ)
 
This was a good opportunity to learn something new, I speak with confutatis.
I have seen that rather than moving the attribute it is possible to change the textalignmentpoint property in two ways:
(vlax-put-property actbj 'textalignmentpoint (vlax-3d-point 0 -10 0))
(vla-put-textalignmentpoint actbj (vlax-3d-point 0 -10 0))

same speech for scalefactor
(vlax-put-property actbj 'scalefactor 0.9)
(vla-put-scalefactor actbj 0.9)

in the definition phase of the attribute simply indicate the coordinate 0.0.0
later you change the textalignmentpoint

greetings
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top