PDA

View Full Version : Attribute block switching routine


cyr_j
2006-03-24, 08:32 PM
I have come across a situation at work that requires one attribute block to be switched out for another one. I need to write a routine that will take all of the attribute information in one block and insert it into the other. Both blocks are identical except that one of the attributes is hidden. I have found a simple DXF routine that shows all of the attributes of an object, but it won't show the data for the block.

I would really appreciate any thoughts or comments. Thanks.

Opie
2006-03-24, 08:45 PM
Are you wanting to replace the block with the same location?

cyr_j
2006-03-27, 03:03 PM
The blocks position will not change. It needs to stay where the original block is positioned.

peter
2006-03-27, 07:00 PM
How about just hiding the attributes.

cyr_j
2006-03-27, 08:14 PM
The blocks in the drawing are inserted via a lisp routine taking the attribute data from an Excel spreadsheet. There is a column in that spreadsheet that tells the lisp routine which block, either A or B to insert. Both blocks are the same, except that Block B has an attribute that block A doesn't. In order to remain compatible with the spreadsheet, the block_type column must reflect that it is to use block B now instead of A. Otherwise, there will be confusion when someone else exports and re-imports the data because of the wrong block definition. That's why I have to pull the information from block A and reinsert it into block B.

Single block imports and exports can be done, but they are time consuming and prone to errors. This method would reduce the number of errors and simplify the changing of blocks in a drawing.

CAB2k
2006-03-27, 08:18 PM
Maybe something like this?

;;=======================[ ArrtMatch.lsp ]=======================
;;; Author: Copyright© 2006 Charles Alan Butler
;;; Version: 1.1 Mar. 27, 2006
;;; Purpose: To update attributes in a block, from a selected
;;; doner block
;;; Requirements: -None
;;; Returns: -None
;;;==============================================================
;;
(defun c:attrmatch (/ ss obj att attr_list parent)

(defun get_attr_lst (blk / lst)
(foreach att (vlax-invoke blk 'getattributes)
(setq lst (cons (cons (vla-get-tagstring att) (vla-get-textstring att)) lst))
)
)

(or *doc* (setq *doc* (vla-get-activedocument (vlax-get-acad-object))))
(prompt "\nSelect a block to copy attributes from.")
(if (setq ss (ssget "+.:E:S" '((0 . "INSERT") (66 . 1))))
(progn
(vla-startundomark *doc*)
(setq parent (vlax-ename->vla-object (ssname ss 0)))
(vla-highlight parent :vlax-true)
(setq attr_list (get_attr_lst parent))
(while
(progn (prompt "\nSelect a block to copy attributes from.")
(setq ss (ssget "+.:E:S" '((0 . "INSERT") (66 . 1))))
)
;; update matching attributes
(setq obj (vlax-ename->vla-object (ssname ss 0)))
(foreach att (vlax-invoke obj 'getattributes)
(if (assoc (setq tag (vla-get-tagstring att)) attr_list)
(vla-put-textstring att (cdr (assoc tag attr_list)))
)
)
)
(vla-highlight parent :vlax-false)
(vla-endundomark *doc*)
)
)
(princ)

)
(prompt "\nAttribute Match Loaded, Enter AttrMatch to run.")
(princ)

peter
2006-03-28, 03:15 PM
OK try this one. It will replace multiple blocks and transfer identical tagged attributes between then. It will have problems if you have multiple attributes with the same tag, just like a database would.

It should do what you want.

cyr_j
2006-03-28, 03:26 PM
Thanks for submitting that block of code. That does the trick nicely.

peter
2006-03-28, 04:12 PM
Your welcome

cyr_j
2006-03-28, 08:07 PM
Thanks to you as well CAB2k. Your solution also solved the problem.

CAB2k
2006-03-28, 08:22 PM
Thanks for saying so, hope you can make some use of it.