Jump to content

Can anyone help me? get the attribute value based on the block name


Kvlar

Recommended Posts

is there anyone who can help me? I need autolisp code with a function to fill in attribute values based on the block name.

For example, I have an object block with the name A and an object attribute,

when I run the autolisp command, I select the object with the name A and I click on the object attribute so that the attribute value contains the name of the object block A.

 

is this possible to create by autolisp? Thank you

Link to comment
Share on other sites

Basically I don't know why do you need this, but here is the code... It changes values of all attributes to match blocks names... And I doubt you're searching for this as I am guessing that you need to fill just single attribute by each block; you just didn't wrote which attribute is that - it's tag name...

 

(defun c:blknames2attvalues ( / s i e n x )
  (prompt "\nSelect blocks to fill their attributes with their block names...")
  (if (setq s (ssget "_:L" (list (cons 0 "INSERT") (cons 66 1))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (setq n (cdr (assoc 2 (entget e))))
      (while (= (cdr (assoc 0 (setq x (entget (setq e (entnext e)))))) "ATTRIB")
        (entupd (cdr (assoc -1 (entmod (subst (cons 1 n) (assoc 1 x) x)))))
      )
    )
  )
  (princ)
)

 

  • Funny 1
Link to comment
Share on other sites

14 minutes ago, marko_ribar said:

Basically I don't know why do you need this, but here is the code... It changes values of all attributes to match blocks names... And I doubt you're searching for this as I am guessing that you need to fill just single attribute by each block; you just didn't wrote which attribute is that - it's tag name...

 

(defun c:blknames2attvalues ( / s i e n x )
  (prompt "\nSelect blocks to fill their attributes with their block names...")
  (if (setq s (ssget "_:L" (list (cons 0 "INSERT") (cons 66 1))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (setq n (cdr (assoc 2 (entget e))))
      (while (= (cdr (assoc 0 (setq x (entget (setq e (entnext e)))))) "ATTRIB")
        (entupd (cdr (assoc -1 (entmod (subst (cons 1 n) (assoc 1 x) x)))))
      )
    )
  )
  (princ)
)

 

this is almost what I need. but what I need is something like I have 1 object block with name A and 1 object attribute

and when I run the autolisp command, I select the block object with the name A and I click on the object attribute so that the attribute value contains the block name of object A.

Link to comment
Share on other sites

Posted (edited)

Give this a try not tested and you need to update the "ATT ID" to what ever you want the block name to be. You could also update the ssget with the block name (line 7) to limit only selecting the block with the attribute. if the attribute is in the same block

 

;;----------------------------------------------------------------------------;;
;; Pull block name to update Attribute 
;; https://www.cadtutor.net/forum/topic/85706-can-anyone-help-me-get-the-attribute-value-based-on-the-block-name/
(defun c:B2A (/ s n att)
  (prompt "\nSelect Blocks to Pull its Names...")
  (while (setq s (ssget "_+.:E:S" '((0 . "INSERT"))))
    (progn
      (setq n (getpropertyvalue (ssname s 0) "Name"))
      (prompt "Select Block with Attribute to update")
      (setq att (ssget "_+.:E:S" '((0 . "INSERT"))))
      (setpropertyvalue (ssname att 0) "ATT ID" n)
    )
  )
  (princ)
)

 

Edited by mhupp
Updated code
Link to comment
Share on other sites

7 minutes ago, mhupp said:

Give this a try not tested and you need to update the "ATT ID" to what ever you want the block name to be. You could also update the ssget with the block name (line 7) to limit only selecting the block with the attribute. if the attribute is in the same block

 

;;----------------------------------------------------------------------------;;
;; Pull block name to update Attribute 
;; https://www.cadtutor.net/forum/topic/85706-can-anyone-help-me-get-the-attribute-value-based-on-the-block-name/
(defun c:B2A (/ s n att)
  (prompt "\nSelect Blocks to Pull its Names...")
  (while (setq s (ssget "_+.:E:S" ((0 . "INSERT"))))
    (progn
      (setq n (getpropertyvalue (ssname s 0) "Name"))
      (prompt "Select Block with Attribute to update")
      (setq att (ssget "_+.:E:S" ((0 . "INSERT"))))
      (setpropertyvalue (ssname att 0) "ATT ID" n)
    )
  )
  (princ)
)

 

I've tried it, but I get an error like this:  error: bad argument type: consp "INSERT"

Link to comment
Share on other sites

Posted (edited)

@Kvlar

What do you get when you pick an attribute with this syntax :

(cdr (assoc 2 (entget (car (nentsel)))))

 

You should get TAGNAME of your attribute...

Then change my code with this TAGNAME string in line 8 :

1 hour ago, marko_ribar said:
(defun c:blknames2attvalues ( / s i e n x )
  (prompt "\nSelect blocks to fill their attributes with their block names...")
  (if (setq s (ssget "_:L" (list (cons 0 "INSERT") (cons 66 1))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (setq n (cdr (assoc 2 (entget e))))
      (while (= (cdr (assoc 0 (setq x (entget (setq e (entnext e)))))) "ATTRIB")
        (if (= (strcase (cdr (assoc 2 x))) (strcase TAGNAME))
          (entupd (cdr (assoc -1 (entmod (subst (cons 1 n) (assoc 1 x) x)))))
        )
      )
    )
  )
  (princ)
)

 

 

Edited by marko_ribar
Link to comment
Share on other sites

Posted (edited)

@Kvlar

Just another question :

Is your attribute real attribute nested into block, or it's separate object still unnested as ATTDEF waiting to be nested into desired block... If it's ATTDEF, then this mhupp's code is good - I changed it just slightly...

 

;;----------------------------------------------------------------------------;;
;; Pull block name to update Attribute 
;; https://www.cadtutor.net/forum/topic/85706-can-anyone-help-me-get-the-attribute-value-based-on-the-block-name/
(defun c:B2A (/ s n att)
  (prompt "\nSelect Blocks to Pull its Names...")
  (while (setq s (ssget "_+.:E:S" '((0 . "INSERT"))))
    (progn
      (setq n (getpropertyvalue (ssname s 0) "Name"))
      (prompt "Select Attribute Definition to update")
      (setq att (ssget "_+.:E:S" '((0 . "ATTDEF"))))
      (setpropertyvalue (ssname att 0) "TextString" n)
    )
  )
  (princ)
)

 

Edited by marko_ribar
Link to comment
Share on other sites

11 minutes ago, marko_ribar said:

@Kvlar

What do you get when you pick an attribute with this syntax :

(cdr (assoc 2 (entget (car (nentsel)))))

 

You should get TAGNAME of your attribute...

Then change my code with this TAGNAME string in line 8 :

 

What code do I need to enter to get the TagName?

Link to comment
Share on other sites

7 minutes ago, marko_ribar said:

@Kvlar

Just another question :

Is your attribute real attribute nested into block, or it's separate object still unnested as ATTDEF waiting to be nested into desired block... If it's ATTDEF, then this mhupp's code is good - I changed it just slightly...

 

;;----------------------------------------------------------------------------;;
;; Pull block name to update Attribute 
;; https://www.cadtutor.net/forum/topic/85706-can-anyone-help-me-get-the-attribute-value-based-on-the-block-name/
(defun c:B2A (/ s n att)
  (prompt "\nSelect Blocks to Pull its Names...")
  (while (setq s (ssget "_+.:E:S" '((0 . "INSERT"))))
    (progn
      (setq n (getpropertyvalue (ssname s 0) "Name"))
      (prompt "Select Attribute Definition to update")
      (setq att (ssget "_+.:E:S" '((0 . "ATTDEF"))))
      (setpropertyvalue (ssname att 0) "ATT_ID" n)
    )
  )
  (princ)
)

 

object block and attribute definition are separate objects

Link to comment
Share on other sites

Change this line in qouted code :

(setpropertyvalue (ssname att 0) "ATT_ID" n)

To this line :

(setpropertyvalue (ssname att 0) "TextString" n)

Link to comment
Share on other sites

12 minutes ago, marko_ribar said:

@Kvlar

Just another question :

Is your attribute real attribute nested into block, or it's separate object still unnested as ATTDEF waiting to be nested into desired block... If it's ATTDEF, then this mhupp's code is good - I changed it just slightly...

 

;;----------------------------------------------------------------------------;;
;; Pull block name to update Attribute 
;; https://www.cadtutor.net/forum/topic/85706-can-anyone-help-me-get-the-attribute-value-based-on-the-block-name/
(defun c:B2A (/ s n att)
  (prompt "\nSelect Blocks to Pull its Names...")
  (while (setq s (ssget "_+.:E:S" '((0 . "INSERT"))))
    (progn
      (setq n (getpropertyvalue (ssname s 0) "Name"))
      (prompt "Select Attribute Definition to update")
      (setq att (ssget "_+.:E:S" '((0 . "ATTDEF"))))
      (setpropertyvalue (ssname att 0) "TextString" n)
    )
  )
  (princ)
)

 

I got this error:
error: ADS request error

do you know what happened?

Link to comment
Share on other sites

Posted (edited)

You didn't answered to question :

What do you get when you pick an attribute with this syntax :

(cdr (assoc 2 (entget (car (nentsel)))))

 

Then you should nest ATTRIB entity to desired block with this firstly posted code in this post : https://www.theswamp.org/index.php?topic=55172.msg594767#msg594767

 

After you nest ATTDEF, change line 8 in my posted code with correct TAGNAME you pulled from written syntax : (cdr (assoc 2 (entget (car (nentsel)))))...

Here is the code where you should alter line 8 :

 

(defun c:blknames2attvalues ( / s i e n x )
  (prompt "\nSelect blocks to fill their attributes with their block names...")
  (if (setq s (ssget "_:L" (list (cons 0 "INSERT") (cons 66 1))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (setq n (cdr (assoc 2 (entget e))))
      (while (= (cdr (assoc 0 (setq x (entget (setq e (entnext e)))))) "ATTRIB")
        (if (= (strcase (cdr (assoc 2 x))) (strcase TAGNAME))
          (entupd (cdr (assoc -1 (entmod (subst (cons 1 n) (assoc 1 x) x)))))
        )
      )
    )
  )
  (princ)
)

 

HTH.

M.R.

Edited by marko_ribar
Link to comment
Share on other sites

Posted (edited)
20 minutes ago, marko_ribar said:

Change this line in qouted code :

(setpropertyvalue (ssname att 0) "ATT_ID" n)

To this line :

(setpropertyvalue (ssname att 0) "TextString" n)

 

Chage "ATT ID" to what ever the tag is

 

autocad-tips-attributes-battman-1.png.437ab35e694dafdbaa49250a2c3d73ee.png

 

if you wanted to update the Date text

 

(setpropertyvalue (ssname att 0) "DATE"  "Today")

 

Edited by mhupp
Link to comment
Share on other sites

37 minutes ago, mhupp said:

 

Chage "ATT ID" to what ever the tag is

 

autocad-tips-attributes-battman-1.png.437ab35e694dafdbaa49250a2c3d73ee.png

 

if you wanted to update the Date text

 

(setpropertyvalue (ssname att 0) "DATE"  "Today")

 

I got this error:
error: ADS request error

 

do you know what happened?

Link to comment
Share on other sites

45 minutes ago, marko_ribar said:

You didn't answered to question :

What do you get when you pick an attribute with this syntax :

(cdr (assoc 2 (entget (car (nentsel)))))

 

 

I apologize in advance, I don't understand the syntax. I'm still a beginner at this.

 

What I want to get after selecting the attribute is that the attribute value will contain the name of the object block that I previously selected.

Link to comment
Share on other sites

Posted (edited)

Sorry got caught up in something. select the block then select the attribute "text" you want to update.

 

;;----------------------------------------------------------------------------;;
;; PULL BLOCKS NAME TO UPDATE ATTRIBUTE
(defun C:ATTBLKNAME () (C:ABN))
(defun C:ABN (/ SS lst Name Att)
  (while (setq ent (car (entsel "\nSelect Block"))) ;gets an entity name using while will keep repeating if you keep selecting an entity
    (progn
      (if (= (cdr (assoc 0 (setq lst (entget ent)))) "INSERT") ;test if its a block 
        (progn
          (setq Name (cdr (assoc 2 lst))) ; pulls the name
          (setq Att (vlax-ename->vla-object (car (nentsel "Select Attribute Text to update")))) ; nentsel can get entity names inisde blocks
          (vla-put-textstring Att Name) ;update entity with name
        )
        (progn ;if entity isn't a block will prompt user
          (prompt "\nNot a Block Pick again")
          (c:ABN) ;and start the comman over again.
        ) 
      )
    )
  )
  (princ)
)

 

--edit

updated code

Edited by mhupp
Link to comment
Share on other sites


"Sorry got caught up in something. select the block then select the attribute "text" you want to update."

 

Just select attribute ! As mentioned using nentsel this can return the tag name, then use (ssget pt) where pt is the (cadr (nentsel)) then (ssname ss 0) 

 

(setq ent (nentsel "\nPick an attribute "))
(setq pt (cadr ent))
(setq tagname (cdr (assoc 2 (entget (car ent)))))
(setq ss (ssget pt))
(setq blk (ssname ss 0))
(setq blkname (cdr (assoc 2 (entget blk)))) ; dont really need this for the task

 

Link to comment
Share on other sites

50 minutes ago, BIGAL said:


"Sorry got caught up in something. select the block then select the attribute "text" you want to update."

 

Just select attribute ! As mentioned using nentsel this can return the tag name, then use (ssget pt) where pt is the (cadr (nentsel)) then (ssname ss 0) 

 

(setq ent (nentsel "\nPick an attribute "))
(setq pt (cadr ent))
(setq tagname (cdr (assoc 2 (entget (car ent)))))
(setq ss (ssget pt))
(setq blk (ssname ss 0))
(setq blkname (cdr (assoc 2 (entget blk)))) ; dont really need this for the task

 

I don't know what's wrong, but I get an error like this:

error: bad argument type: VLA-OBJECT <Entity name: 181bbe5aaf0>

But, thank you for helping me

 

Link to comment
Share on other sites

2 hours ago, mhupp said:

Sorry got caught up in something. select the block then select the attribute "text" you want to update.

 

;;----------------------------------------------------------------------------;;
;; PULL BLOCKS NAME TO UPDATE ATTRIBUTE
(defun C:ATTBLKNAME () (C:ABN))
(defun C:ABN (/ SS lst Name Att)
  (while (setq ent (car (entsel "\nSelect Block"))) ;gets an entity name using while will keep repeating if you keep selecting an entity
    (progn
      (if (= (cdr (assoc 0 (setq lst (entget ent)))) "INSERT") ;test if its a block 
        (progn
          (setq Name (cdr (assoc 2 lst))) ; pulls the name
          (setq Att (car (nentsel "Select Attribute Text to update"))); nentsel can get entity names inisde blocks
          (vla-put-textstring Att Name) ;update entity with name
        )
        (progn ;if entity isn't a block will prompt user
          (prompt "\nNot a Block Pick again")
          (c:ABN) ;and start the comman over again.
        ) 
      )
    )
  )
  (princ)
)

 

--edit

added some comments

i get an error like this:

error: bad argument type: VLA-OBJECT <Entity name: 181bbe5aaf0>

 

Is there anything I did wrong?
But, thank you for helping me

Link to comment
Share on other sites

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...