Jump to content

explode all blocks


LISP2LEARN

Recommended Posts

Hello guys,

 

Code works just fine but I'm just curious on how would you approach this with "while" function. I need to explode all blocks in a drawing including nested one.

Statement would be.. as long as there are blocks in the drawing continue to explode them. I use repeat 10 times to make sure everything was exploded. Thanks.

 

(vl-load-com)
(defun C:test (/ AB)
(setvar 'qaflags 1)
(repeat 10
(setq AB (ssget "_X" '((0 . "INSERT"))))
(vl-cmdf "_.explode" AB ""))
(setvar 'qaflags 0))

Link to comment
Share on other sites

MSasu,

 

Tried your code and my autocad hangs up.

 

You should cycle as long as the selection by SSGET return something:

(while (setq AB (ssget "_X" '((0 . "INSERT"))))
(vl-cmdf "_.explode" AB "")
)

Link to comment
Share on other sites

I cannot test it into AutoCAD 2009, but worked well for me in 2010 and 2013. Maybe try to explode the blocks one at a time:

(while (setq AB (ssget "_X" '((0 . "INSERT"))))
(repeat (setq index (sslength AB))
 (command "_EXPLODE" (ssname AB (setq index (1- index))))
)
)

Sorry for delay, for a while there was a constant error while trying to access the CADTutor...

Link to comment
Share on other sites

This isn't great, but should perform as required:

(defun c:explodeall ( / explode layouts )
   (setq layouts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
         explode t
   )
   (while explode
       (setq explode nil)
       (vlax-for layout layouts
           (vlax-for obj (vla-get-block layout)
               (and
                   (= "AcDbBlockReference" (vla-get-objectname obj))
                   (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-explode (list obj))))
                   (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-delete  (list obj))))
                   (setq explode t)
               )
           )
       )
   )
   (princ)
)
(vl-load-com) (princ)

Link to comment
Share on other sites

I'm the one who suppose to be apologizing, fell asleep on you. Sorry.

 

Sorry for delay, for a while there was a constant error while trying to access the CADTutor...

Link to comment
Share on other sites

Thanks again Lee. Works wonderful. You rewrite everything and now I'm lost again. Another lisp to dissect on.

 

One more thing. How do i incorporate this with your code. Not familiar with visual lisp yet. I want to delete certain blocks on certain layers and omit block with certain names.

 

This is my selection set.

(ssget "_X" '((0 . "INSERT") 
(8 . "0,A-WALL,A-WINDOWS,A-DOOR") 
  (-4 . "<NOT") 
    (2 . "A-MILLWORK,A-DESK,A-CHAIR,A-TABLE,F-*") 
  (-4 . "NOT>")))

 

This isn't great, but should perform as required:

(defun c:explodeall ( / explode layouts )
   (setq layouts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
         explode t
   )
   (while explode
       (setq explode nil)
       (vlax-for layout layouts
           (vlax-for obj (vla-get-block layout)
               (and
                   (= "AcDbBlockReference" (vla-get-objectname obj))
                   (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-explode (list obj))))
                   (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-delete  (list obj))))
                   (setq explode t)
               )
           )
       )
   )
   (princ)
)
(vl-load-com) (princ)

Link to comment
Share on other sites

Thanks again Lee. Works wonderful. You rewrite everything and now I'm lost again. Another lisp to dissect on.

 

You're welcome, feel free to ask if you have any questions about the code.

 

One more thing. How do i incorporate this with your code. Not familiar with visual lisp yet. I want to delete certain blocks on certain layers and omit block with certain names.

 

This is my selection set.

(ssget "_X" '((0 . "INSERT") 
(8 . "0,A-WALL,A-WINDOWS,A-DOOR") 
  (-4 . "<NOT") 
    (2 . "A-MILLWORK,A-DESK,A-CHAIR,A-TABLE,F-*") 
  (-4 . "NOT>")))

 

The following modifications should suffice:

(defun c:explodeall ( / explode layouts name )
   (setq layouts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
         explode t
   )
   (while explode
       (setq explode nil)
       (vlax-for layout layouts
           (vlax-for obj (vla-get-block layout)
               (and
                   (= "AcDbBlockReference" (vla-get-objectname obj))
                   [color=red](wcmatch (strcase (vla-get-layer obj)) "0,A-WALL,A-WINDOWS,A-DOOR")
                   (or
                       (and
                           (vlax-property-available-p obj 'effectivename)
                           (setq name (strcase (vla-get-effectivename obj)))
                       )
                       (setq name (strcase (vla-get-name obj)))
                   )
                   (not (wcmatch name "A-MILLWORK,A-DESK,A-CHAIR,A-TABLE,F-*"))[/color]
                   (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-explode (list obj))))
                   (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-delete  (list obj))))
                   (setq explode t)
               )
           )
       )
   )
   (princ)
)
(vl-load-com) (princ)

Untested code, but should perform as required.

Link to comment
Share on other sites

Thanks Lee. Perfect.

Understand it all now, every bit of it. Except for one thing.

 

(setq layouts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) [color="red"]explode t[/color])

 

Why did you assign variable "explode" before the while function? Tried to remove it and code didn't work. Reason behind it?

 

 

You're welcome, feel free to ask if you have any questions about the code.

Link to comment
Share on other sites

Thanks Lee. Perfect.

 

Excellent.

 

Understand it all now, every bit of it. Except for one thing.

(setq layouts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) [color=red]explode t[/color])

Why did you assign variable "explode" before the while function? Tried to remove it and code didn't work. Reason behind it?

 

Hint: the value of the explode variable is the test expression for the while loop. ;)

Link to comment
Share on other sites

got it. knew I was over thinking that one. if there is no explode "while" will exit automatically. learned something. thanks again for your help lee.

Link to comment
Share on other sites

got it. knew I was over thinking that one. if there is no explode "while" will exit automatically. learned something. thanks again for your help lee.

 

Exactly: since the explode symbol is declared local to the function it has a null value when the program is evaluated until assigned a non-nil value. The explode variable is then reset to nil for each iteration of the while loop to ensure the loop ceases when no more blocks can be exploded.

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