Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to 'enter' this code

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
dshannon
3157 Views, 7 Replies

How to 'enter' this code

Dear Forum,

 

While trying to find out how to select all blocks and explode them until there are no more blocks (but not  3D surfaces), I found this post:

 

http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Lisp-to-select-all-blocks-amp-explode...

 

At the end of the thread CADDapult writes out some code that seems to do what I want. Question is... how to do enter/use this code. I have not used code in autocad before (have done a small bit in excel).

 

Any insight woudl be appreciated.

7 REPLIES 7
Message 2 of 8
hgasty1001
in reply to: dshannon

Hi,

 

 

As is saturday, it seems you are in a hurry, try this little code to explode all the explodable blocks in the drawing

(defun c:blkxplode(/ ss)
  (setq ss (ssget "x" '((0 . "INSERT"))))
  (if ss
    (progn
     (setvar "qaflags" 1) 
     (command "._explode" ss "") 
     (setvar "qaflags" 0)
    )
    (alert "No Blocks selected")
  )  
)  

In order to load lisp files, you have to issue the Appload command at the command line or in the Tools Menu, Load Application, and then in the command line again, type the command BlkXplode. In order to use other lisp files, you have to inspect the file for functions with the pattern: (defun C:SomeNameHere()...)  They are Autolisp defined commands.

 


Gaston Nunez

Message 3 of 8
dshannon
in reply to: hgasty1001

Thank you very much Gaston!! Does the trick. Was worth giving up the saturday after all!!! Smiley Happy

So I pasted that into notepad, saved with a .lsp file name and loaded up as you mentioned. So is that the norm?

Message 4 of 8
Kent1Cooper
in reply to: hgasty1001


@hgasty1001 wrote:
.... try this little code to explode all the explodable blocks in the drawing
....

If there are any nested Blocks as ingredients in the definitions of other Blocks, and you want all of those also Exploded, you would want to run that several times.  Or you could alter it to run itself as many times as it continues to find Blocks.  In simplest terms:

 

(defun c:blkxplodestack (/ ss)
  (setvar "qaflags" 1) 
  (while

    (setq ss (ssget "x" '((0 . "INSERT"))))
    (command "._explode" ss "")
  ); while

  (setvar "qaflags" 0)
)

 

However, that [and gasty's original] would also find Xrefs [and, if you ever use them, Windows Metafiles] that can't be Exploded.  So really, it ought to run through each item in a selection set, and only if it's an Explodable kind of Insert object, Explode it.  [Also, doing it that way (Exploding only one at a time), no messing with the QAFLAGS System Variable is necessary.]  There should also be some kind of marker so it will know once it has encountered a selection set that contains no ordinary Blocks among the Insert objects it found, and stop checking -- otherwise it would get into an endless loop.  I would bet that there's something out there already that will do something like that, but if you don't find it, it wouldn't be hard to work out.

Kent Cooper, AIA
Message 5 of 8
Lee_Mac
in reply to: Kent1Cooper

The following program will recursively explode all primary & nested block references (nested to any depth) excluding xrefs, in all drawing layouts (the EXPLODE command will ignore objects in inactive drawing layouts):

 

;; Explode All Blocks  -  Lee Mac
;; Recursively explodes all primary & nested blocks (nested to any depth) in all drawing layouts.

(defun c:eab ( )
    (vlax-for blk (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
        (if (= :vlax-false (vla-get-isxref blk))
            (vlax-for obj blk (explodeblock obj))
        )
    )
    (princ)
)
(defun explodeblock ( obj / lst )
    (if
        (and
            (= "AcDbBlockReference" (vla-get-objectname obj))
            (vlax-method-applicable-p obj 'explode)
            (vlax-write-enabled-p obj)
        )
        (if
            (not
                (vl-catch-all-error-p
                    (setq lst
                        (vl-catch-all-apply 'vlax-invoke (list obj 'explode))
                    )
                )
            )
            (progn
                (vla-delete obj)
                (foreach obj lst (explodeblock obj))
            )
        )
    )
)
(vl-load-com) (princ)

 

Note that the above will ignore objects on locked layers.

 

Lee

Message 6 of 8
dshannon
in reply to: Lee_Mac

Thank you all for your replies. This will make the upcoming week some what manageable Smiley Indifferent

Gaston's initial response did what I initally needed (although as Kent mentioned, does need to be rerun for nested blocks). I therefore marked it as the solution. However, Lee's solution is super sweet as it takes the repeat running out of it.

Again, thanks to all!

Message 7 of 8
Lee_Mac
in reply to: dshannon

You're most welcome dshannon - I'm glad it helps! Smiley Happy

Message 8 of 8
pbejse
in reply to: Lee_Mac


@Lee_Mac wrote:

 

vlax-write-enabled-p
Lee

Whoa.. Never knew that function exists at all.  Nice LM

 

pbe

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost