It was only recently that I was able to enjoy the new Muppets film. It was a great family movie and much like the Muppets I grew up with. Anyways, that is what inspired the title of this article, the song "Are You A Man or A Muppet" more specifically.
Note: This article does not work for those using AutoCAD for Mac because it does not support ActiveX.
Using AutoLISP, you can access the Blocks table in the current drawing and determine the types of blocks in a drawing. Blocks can be the kind that you insert via the INSERT command, attach as an xref, or those that are containers for the geometry on a layout such as Model or Layout1. The same logic can be applied to determine the types of blocks inserted into model or paper space.
The following code demonstrates how to determine the block type for each block in the current drawing using the IsXref, IsLayout, and IsDynamicBlock methods/properties. It also steps through the objects in each block to see if it contains attributes or not.
(vl-load-com)
(defun c:WhatKindOfBlockAmI ( / acadObj doc msg block)
(setq acadObj (vlax-get-acad-object))
(setq doc (vla-get-ActiveDocument acadObj))
(setq msg "")
;; Step through all the blocks in the Blocks table
(vlax-for block (vla-get-Blocks doc)
(cond
;; Standard or dynamic block?
((and (= (vla-get-IsLayout block) :vlax-false)
(= (vla-get-IsXRef block) :vlax-false))
(if (= (vla-get-IsDynamicBlock block) :vlax-false)
(setq msg (strcat msg (vla-get-Name block) ": Standard"))
(setq msg (strcat msg (vla-get-Name block) ": Dynamic"))
)
;; Has attributes?
(setq attsExist "")
(vlax-for ent block
(if (= (vla-get-ObjectName ent) "AcDbAttributeDefinition")
(setq attsExist " with attributes")
)
)
(setq msg (strcat msg attsExist))
)
;; Xref?
((= (vla-get-IsXRef block) :vlax-true)
(setq msg (strcat msg (vla-get-Name block) ": Xref"))
(if (= (vla-get-IsLayout block) :vlax-true)
(setq msg (strcat msg (vla-get-Name block) " and layout"))
)
)
;; Layout?
((= (vla-get-IsLayout block) :vlax-true)
(setq msg (strcat msg (vla-get-Name block) ": Layout only"))
)
)
(setq msg (strcat msg "\n"))
)
;; Display the block information for this drawing
(alert (strcat "This drawing contains blocks of the following types: " msg))
)
Sincerely,
Lee
Hi Lee, great post - this routine is really useful!
I am using one of Lee-Mac's routines called REDEFALL which redefines all blocks in a drawing with the equivalent blocks in a specified folder. (see code below)
I have a few dynamic blocks in the drawing (with a visibility parameter) but this routine does not update these dynamic blocks. I'm not sure why, when vlax-get-blocks is used which should pick up all blocks regardless.
Any ideas?
Cheers, Pete
;; Redefine All Blocks - Lee Mac
(defun c:redefall ( / bln dir doc dwg lst obj org spc blk)
;; Enter Directory of Block Library on the line below; or use nil to use Support Paths
(setq dir nil
)
(if dir
(setq dir (strcat (vl-string-right-trim "\\" (vl-string-translate "/" "\\" dir)) "\\"))
(setq dir "")
)
(cond
( (= 4 (logand 4 (cdr (assoc 70 (tblsearch "layer" (getvar 'clayer))))))
(princ "\nCurrent layer locked.")
)
( (setq doc (vla-get-activedocument (vlax-get-acad-object))
spc (vla-get-modelspace doc)
org (vlax-3D-point 0 0)
)
(vlax-for blk (vla-get-blocks doc) ;gets all blocks in the current drawing
(if
(and
(= :vlax-false (vla-get-isxref blk)) ;removes xrefs from the selection
(= :vlax-false (vla-get-islayout blk)) ; ignores paperspace objects?
(not (wcmatch (setq bln (vla-get-name blk)) "`**,_*,*|*"))
(setq dwg (findfile (strcat dir bln ".dwg")))
)
(progn
(setq obj (vla-insertblock spc org dwg 1.0 1.0 1.0 0.0))
(if (= :vlax-true (vla-get-hasattributes obj))
(setq lst (vl-list* bln lst))
)
(vla-delete obj)
)
)
)
(if lst (foreach blk lst
(vl-cmdf "_.attsync" "_N" blk)
);foreach
)
(vla-regen doc acallviewports)
)
)
(princ)
)
(vl-load-com) (princ)
Posted by: Peter Jeffs | Thursday, May 21, 2015 at 03:43 PM