Jump to content

Recommended Posts

Posted

How do I set an attribute value that is within a block to a variable using a lisp statement? I do not want any user input to be used, it needs to be scriptable. There will only be one instance of the block in the drawings. If you need more information, I will gladly give it. Thanks

Posted

Welcome to CADTutor. :)

 

Have a play with the following and you should be able to see the list of attributes in the command line;

 

(defun c:Test ( / s sn e lst)
 (if (setq s (ssget "_X" '((0 . "INSERT")(66 . 1) (2 . "MyBlock")))) [color="red"];; Change the MyBlock name to your block name[/color]
   (progn
     (setq sn (ssname s 0)) [color="red"];; Assumed to run on one block instance / reference.[/color]
     (while (= "ATTRIB" (cdr (assoc 0 (setq e (entget (setq sn (entnext sn)))))))
       (setq lst (cons (cdr (assoc 1 e)) lst))
       )
     )
   )
 lst [color="red"];; list of attributes values if available.[/color]
 )

Posted

I ran your code with my block name inserted. I then type test in the command line and the only thing I get back is nil. I am not sure what I am supposed to do here.

Posted

Is you block dynamic or regular?

And how did you add your block name into the codes?

Posted

Another (just for fun) :

(defun GetBlkAttVals ( bnm / o L )
 (if (and (setq o (FirstBlkOccurrence bnm)) (setq L (vlax-invoke o 'GetAttributes)) )
   (mapcar 'vla-get-TextString L)
 )
); defun GetBlkAttVals

; recursive
(defun FirstBlkOccurrence ( n / rec )
 (defun rec ( b n i / o )
   (cond
     ( (vl-catch-all-error-p (setq o (vl-catch-all-apply 'vla-item (list b i)))) (prompt "\nError") )
     ( (and (vlax-property-available-p o 'EffectiveName) (= n (vla-get-EffectiveName o))) o)
     ( (rec b n (1+ i)) )
   ); cond
 ); defun 
 (rec (vla-get-Block (vla-get-ActiveLayout (vla-get-ActiveDocument (vlax-get-acad-object)))) n 0)
); defun FirstBlkOccurrence

; iterative
(defun FirstBlkOccurrence ( n / i->L b o )
 (defun i->L ( i / L ) (if (eq 'INT (type i)) (repeat i (setq L (cons (setq i (1- i)) L)))) )
 (setq b (vla-get-Block (vla-get-ActiveLayout (vla-get-ActiveDocument (vlax-get-acad-object)))))
 (vl-some 
   (function 
     (lambda (x)
       (if 
         (and
           (setq o (vla-item b x))
           (vlax-property-available-p o 'EffectiveName)
           (= n (vla-get-EffectiveName o))
         )
       o
       )
     )
   )
   (i->L (vla-get-Count b))
 ); vl-some
); defun FirstBlkOccurrence

 

Usage example - (where the blockname is case-sensitive) :

(GetBlkAttVals "MyBlockName")

Posted
Is you block dynamic or regular?

And how did you add your block name into the codes?

 

My block is static. I inserted my block name like so:

 

(if (setq s (ssget "_X" '((0 . "INSERT")(66 . 1) (2 . "FCA_TB")))) ;; Change the MyBlock name to your block name

Posted

Nothing is special in my posted codes above, so if you can upload a sample drawing that would be great to check that out closely.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...