Lisp explode not working with selection

Lisp explode not working with selection

Mario.van.der.raaf
Enthusiast Enthusiast
837 Views
10 Replies
Message 1 of 11

Lisp explode not working with selection

Mario.van.der.raaf
Enthusiast
Enthusiast

Hello programmers!

 

I have the following issue with my lisp code. I am trying to explode all items on a certain layer so that they can be exported to excel. The problem i have is that the code makes a nice selection of all the items on that layer, and when i manually enter the explode command all works fine. But the explode command in my lisp code does not work.

 

This may be a really simple question to answer but im new to lisp and autocad so im having a rough time figuring it out. Any help would be appreciated!!

My code:

  (setq layerName "Maatvoering_Export") ; set layername to "maatvoering_export"
  (command "-LAYER" "M" layerName "") ; Give layer command in autocad using M to make current layer.
  (sssetfirst nil (ssget "X" (list (cons 8 (getvar "clayer"))))) ; selects everything
  (command "EXPLODE") ; This explodes everything on the curent layer so it can be brought into excel

 

0 Likes
Accepted solutions (1)
838 Views
10 Replies
Replies (10)
Message 2 of 11

ronjonp
Advisor
Advisor

Try this:

(sssetfirst nil (ssget "X" (list (cons 8 (getvar "clayer")))))
(initcommandversion 2)
(command "_.EXPLODE")
0 Likes
Message 3 of 11

Moshe-A
Mentor
Mentor

@Mario.van.der.raaf  hi,

 

Here is a better way to do this with (ssget) function which have an option to filter objects on specific layer

(but also have many other great options)

 

Moshe

 

 

(defun c:xpl_on_lay (/ ss)
 (if (setq ss (ssget "_x" '((8 . "Maatvoering_Export"))))
  (progn 
   (command "._explode" ss)
   (prompt "\nDone explode.")
  ); progn
  (prompt "\nNoting selected.")
 ); if

 (princ)
)

 

0 Likes
Message 4 of 11

Kent1Cooper
Consultant
Consultant
Accepted solution

EXPLODE has, for some inexplicable reason, always been limited to Exploding only one object when inside an AutoLisp (command) function if you don't take steps to get around that.  If you feed it a multi-object selection set, one of them will be Exploded.  The (initcommandversion) approach [which Message 3 will need added to it, along with "" completion of the selection] is the easiest way to get the command to work as it does at the Command: line, and do more than one.  [The 2 argument in Message 2 doesn't seem to be necessary.]

 

The other way involves the mysterious and undocumented QAFLAGS System Variable.  You can Search the Forum for references to that, if you want to know more about it, but be careful -- it's critical that you set it back if you change the setting.

 

[A third way would be to step through the selection set and Explode each thing in it separately.]

Kent Cooper, AIA
Message 5 of 11

komondormrex
Mentor
Mentor

hey,

 when you create a layer with the command -layer it becomes active layer with no entities on it. 

(ssget "X" (list (cons 8 (getvar "clayer")))) will select nil.

 

0 Likes
Message 6 of 11

Kent1Cooper
Consultant
Consultant

@komondormrex wrote:

 when you create a layer with the command -layer it becomes active layer with no entities on it. 

(ssget "X" (list (cons 8 (getvar "clayer")))) will select nil.


It becomes current only if you create it with the Make option, not if you do so with the New option.  And if the Layer already exists, it merely sets it current -- it does not wipe out any entities already drawn on it, if that's what you're suggesting.

Kent Cooper, AIA
0 Likes
Message 7 of 11

komondormrex
Mentor
Mentor

wow)

then why do not simply set active layer thru this (setvar 'clayer "Maatvoering_Export")?

0 Likes
Message 8 of 11

Kent1Cooper
Consultant
Consultant

@komondormrex wrote:

then why do not simply set active layer thru this (setvar 'clayer "Maatvoering_Export")?


That works only if the Layer exists.  It's common to use the Make option to make a Layer current whether or not it already exists.

Kent Cooper, AIA
0 Likes
Message 9 of 11

komondormrex
Mentor
Mentor

that tricky)

0 Likes
Message 10 of 11

Kent1Cooper
Consultant
Consultant

In that situation, it is also common to include Layer options like color and linetype and plot-or-not in the Making of the Layer, in case it doesn't exist yet, and you don't want it to just have the default properties [continuous linetype, color 7, etc.].  The potential drawback is if it's possible that the Layer already exists with different properties than those assigned to it in the Layer/Make operation.  The common way will correct those properties to the standard, which may often be what's wanted.  But when you can assume there's a valid reason for the non-standard properties, and you want to allow those non-standard properties to remain, that's the situation in which you should run a check on whether the Layer exists before Making it, and if it does, only use the Set option [or (setvar 'clayer "LayerName")] to make it current.

Kent Cooper, AIA
0 Likes
Message 11 of 11

Mario.van.der.raaf
Enthusiast
Enthusiast

Thanks for the explanation. Based on your info i made the changes to the code and now it works for exploding the selected items.

 (if (setq ss (ssget "_x" '((8 . "Maatvoering_Export"))))
  (progn
   (initcommandversion 2)
   (command "._explode" ss "")
   (prompt "\nDone explode.")
  ); progn
  (prompt "\nNoting selected.")
 ); if

 (princ)

Kind regards,
 
0 Likes