block attributes extraction error

block attributes extraction error

nam.lethai1995
Enthusiast Enthusiast
402 Views
4 Replies
Message 1 of 5

block attributes extraction error

nam.lethai1995
Enthusiast
Enthusiast
Am trying to make the extractattributes function work on multi selected block in test function but the code seem to make a loop or what ever error that make autoca freeze or calculating for so long that i have to turn it off via task manager. Any help pls 
(
defun c:test (/ blk blkref atts)
  (setq blk (car (entsel "\nSelect the block: ")))
  (setq blkref (entnext blk))
  (while (not (eq (cdr (assoc 0 (setq atts (entget blkref)))) "SEQEND"))
    (if (eq (cdr (assoc 0 atts)) "ATTRIB")
      (progn
        (setq attname (cdr (assoc 2 atts)))
        (setq attval (cdr (assoc 1 atts)))
        (princ (strcat "\nAttribute Name: " attname))
        (princ (strcat "\nAttribute Value: " attval))
      )
    (setq blkref (entnext blkref))
  ))

  (princ)
)
(defun C:ExtractAttributes (/ blk blkref atts attrList)
  (setq blk (car (entsel "\nSelect the block: ")))
  (setq blkref (entnext blk))
  (setq attrList '())
 
  (while (not (eq (cdr (assoc 0 (setq atts (entget blkref)))) "SEQEND"))
    (if (eq (cdr (assoc 0 atts)) "ATTRIB")
      (progn
        (setq attname (cdr (assoc 2 atts)))
        (setq attval (cdr (assoc 1 atts)))
        (setq attrList (cons (list attname attval) attrList))
      )
    )
    (setq blkref (entnext blkref))
  )
 
  (foreach attr attrList
    (princ (strcat "\nAttribute Name: " (car attr)))
    (princ (strcat "\nAttribute Value: " (cadr attr)))
  )
  (princ)
)
0 Likes
Accepted solutions (2)
403 Views
4 Replies
Replies (4)
Message 2 of 5

paullimapa
Mentor
Mentor

I would first make sure the block selected has attributes before proceeding by using 

(ssget "_+.:S" '((0 . "INSERT") (66 . 1))))

check out this example 

https://www.cadtutor.net/forum/topic/76989-is-there-a-simple-way-to-extract-attribute-value-from-blo...


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 5

Moshe-A
Mentor
Mentor
Accepted solution

@nam.lethai1995 ,

 

fixed

 

enjoy

Moshe

 

 

(defun c:test (/ blk blkref atts)
  (if (setq blk (car (entsel "\nSelect the block: ")))
   (progn 
    (setq blkref (entnext blk))
    (while (not (eq (cdr (assoc 0 (setq atts (entget blkref)))) "SEQEND"))
     (if (eq (cdr (assoc 0 atts)) "ATTRIB")
      (progn
        (setq attname (cdr (assoc 2 atts)))
        (setq attval (cdr (assoc 1 atts)))
        (princ (strcat "\nAttribute Name: " attname))
        (princ (strcat "\nAttribute Value: " attval))
      ); progn
     ); if
     (setq blkref (entnext blkref))
    ); while  
   ); progn
  ); if

 (princ)
)


(defun C:ExtractAttributes (/ blk blkref atts attrList)
  (if (setq blk (car (entsel "\nSelect the block: ")))
   (progn 
    (setq blkref (entnext blk))
    (setq attrList '())
 
    (while (not (eq (cdr (assoc 0 (setq atts (entget blkref)))) "SEQEND"))
     (if (eq (cdr (assoc 0 atts)) "ATTRIB")
       (progn
        (setq attname (cdr (assoc 2 atts)))
        (setq attval (cdr (assoc 1 atts)))
        (setq attrList (cons (list attname attval) attrList))
       ); progn
     ); if
     (setq blkref (entnext blkref))
    ); while
 
    (foreach attr attrList
     (princ (strcat "\nAttribute Name: " (car attr)))
     (princ (strcat "\nAttribute Value: " (cadr attr)))
    )
    
   ); progn
  ); if
  (princ)
)

 

 

 

0 Likes
Message 4 of 5

komondormrex
Mentor
Mentor
Accepted solution

hth,

(defun c:extract_attributes (/ insert_sset)
	(setq insert_sset (ssget '((0 . "insert") (66 . 1))))
	(if insert_sset
		(foreach insert (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex insert_sset))))
			(princ "\n============================")
			(princ "\nBlock name: ")
			(princ (vla-get-effectivename insert))
			(princ "\n============================")
			(foreach tag_value_list (mapcar '(lambda (attribute) (cons (vla-get-tagstring attribute)
										   (vla-get-textstring attribute)
									     )
							 )
							 (vlax-invoke insert 'getattributes)
						)
				(princ "\nTag: ")
				(princ (car tag_value_list))
				(princ "\nValue: ")
				(princ (cdr tag_value_list))
			)
		)
		(alert "No attributed block reference was selected!")
	)
	(princ)
)
Message 5 of 5

nam.lethai1995
Enthusiast
Enthusiast

thanks guys. Now it work just fine, much appreciated

0 Likes