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

Wrote a small lisp to select blocks & explode. What would cause it not to work.

9 REPLIES 9
Reply
Message 1 of 10
dglenn9000
4066 Views, 9 Replies

Wrote a small lisp to select blocks & explode. What would cause it not to work.

I wrote a lisp to select blocks with the work *linear* in it, and then explode them. What would cause it not to work?

 

 

(defun c:explodelin ()
(if (tblsearch "BLOCK" "*linear*")
(sssetfirst nil (ssget "_x" '((0 . "INSERT")(2 . "*linear*"))))
(command "explode")
)
(princ)
)

 

Thanks

9 REPLIES 9
Message 2 of 10
Kent1Cooper
in reply to: dglenn9000


@dglenn9000 wrote:

I wrote a lisp to select blocks with the work *linear* in it, and then explode them. What would cause it not to work?

....

(if (tblsearch "BLOCK" "*linear*")
(sssetfirst nil (ssget "_x" '((0 . "INSERT")(2 . "*linear*"))))
(command "explode")
....


1.  The (tblsearch) function won't accept wildcards [compare the results from (tblsearch "style" "standard") and (tblsearch "style" "*anda*")], as (ssget) will.  You could do something like look at the names of Blocks by stepping through the table with (tblnext) until it finds one that contains that, and then stop stepping.

 

2.  For some reason, Explode in a (command) function can't do a multiple-Block selection set all at once.  You need to either step through and Explode them one at a time, or deal with the mysterious and undocumented QAFLAGS System Variable.  Put Explode and QAFLAGS into the Search window, and you'll learn more than you want to know.

Kent Cooper, AIA
Message 3 of 10
hmsilva
in reply to: dglenn9000

dglenn9000,
I think you can not use wild cards in a tblsearch, you need to use the exact block name.
Also think that you can not use the command explode for multiple objects via lisp, only using the command line version.
You can try something like

 

(defun c:explodelin (/ ss old-echo item)
  (setq old-echo (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (if (setq ss (ssget "_x" '((0 . "INSERT")(2 . "*linear*"))))
    (foreach item (mapcar 'cadr (ssnamex ss))
      (command "Explode" item)
      )
    )
  (setq ss nil)
  (setvar "CMDECHO" old-echo)
  (princ)
)

 

Hope that helps you.

 

Henrique

EESignature

Message 4 of 10
Kent1Cooper
in reply to: Kent1Cooper


@Kent1Cooper wrote:

....  You could do something like look at the names of Blocks by stepping through the table with (tblnext) until it finds one that contains that, and then stop stepping.

....


[That suggestion was based on your apparent original intent to determine whether there are any Block definitions with names like that.  But hmsilva's approach, looking directly for whether there are any insertions of Blocks with such names, makes a lot more sense.  It's a one-step process, rather than needing one step to find whether there are Block definitions and another step to find any insertions.]

Kent Cooper, AIA
Message 5 of 10
pbejse
in reply to: hmsilva


@hmsilva wrote:

dglenn9000,
I think you can not use wild cards in a tblsearch, you need to use the exact block name.
Also think that you can not use the command explode for multiple objects via lisp, only using the command line version.
You can try something like

 

......
    (foreach item (mapcar 'cadr (ssnamex ss))
      (command "Explode" item)
     .......

 

Hope that helps you.

 

Henrique


 If you're leaning to use command line approach, careful with items not on current space. 

 

dglenn9000,

 

Consider  blocks on locked layer and also blocks with Explodable=vlax:false flag.

 

HTH

Message 6 of 10
hmsilva
in reply to: pbejse

pbejse,

thanks for your alerts, you're completely right.

 

  But when I wrote

: ...think that you can not use the command explode for multiple objects via lisp, only using the command line version...

 

what I meant, is the command to explode behaves differently when is used via lisp, or typing directly in command line.

 

Via lisp:

 

Command: 'VLIDE explode

Select object: _p *Invalid selection*

Expects a point or Last/ALL/Group

Select object: *Cancel*

 

only allows us to select the last object, or one at a time.

 

typing directly in command line:

 

Command: explode

Select objects: p 2 found

Select objects:

 

and allows us to select using:

 

Window/Last/Crossing/BOX/ALL/Fence/WPolygon/CPolygon/Group/Add/Remove/Multiple/Previous/Undo/AUto/SIngle

 

I have used the "vla-sendcommand" method to explode multiple objects.

 

i.e.

(vla-sendcommand MyDoc "Explode p \r")

 

Cheers

Henrique

EESignature

Message 7 of 10
hmsilva
in reply to: pbejse

pbejse,

one more thing, after using

 

(vla-sendcommand MyDoc "Explode p \r")

 

I have tried to select the objects resulting from "explode" with

 

(command "select" "P" "")

 

unsuccessfully, continues to select objects previous to the command "explode".

You know of any way to solve this situation?

 

Henrique

EESignature

Message 8 of 10
devitg
in reply to: dglenn9000

From the VLIDE HELP

 

Explodes the compound object into subentities.

See Also | Example

Signature

RetVal = object.Explode

Object

3DPolyline , BlockRef, ExternalReference, LightweightPolyline, MInsertBlock, Polygonmesh, Polyline, Region
The object or objects this method applies to.

RetVal

Variant (array of objects)
The array of exploded objects.

Remarks

Depending on the type of compound object you're exploding, different results occur. Refer to the EXPLODE command topic in the AutoCAD Command Reference for a detailed list of explodable objects and their results.

You do not have to explode a block in order to manipulate its constituent entities. All block definitions have an Item method that allow you to manipulate the entities within the block without exploding the block definition itself. 

 

 

Message 9 of 10
pbejse
in reply to: hmsilva


@hmsilva wrote:

 

 ..... I have tried to select the objects resulting from "explode" with

 (command "select" "P" "")....

 

 Henrique


 

It doesnt  really matter how you evaluate the command line "explode", it will still not process objects NOT on CURRENT SPACE. Unless you step through the layoutlist, only then it will process those objects. That is if you're sticking with native explode command.

 

As for your query

 

(defun c:demo (/ a selset)
(command "_explode" pause)
(setq selset (ssget "_P" '((0 . "INSERT"))));<-- whatever the entity type
(sssetfirst nil selset)
)

 

As Kent already pointed out:

 

.......2.  For some reason, Explode in a (command) function can't do a multiple-Block selection set all at once.  You need to either step through and Explode them one at a time,

 

so, its either this

 

(defun c:demo1 (/ a selset ss i)
(if (setq  selset (ssadd) ss (ssget "_X" '((0 . "INSERT")(2 . "BlockName")(410 . "Model"))))
		(repeat (sslength ss)
		  (setq	ent (ssname ss 0))
		  	(command "_explode" ent)
		  	(if  (setq a (ssget "_P" '((0 . "INSERT"))))
			  	(repeat (setq i (sslength a))
				  	(ssadd (ssname a (setq i (1- i))) selset)))	
		  	(ssdel (ssname ss 0) ss)
		  )
  )
(sssetfirst nil selset)
  (print (sslength selset))(princ)
  )

 

or this

 

(defun c:demo2 (/ ss ent a selset)
(if (setq selset (ssadd) ss (ssget "_X" '((0 . "INSERT")(2 . "Legend")(410 . "Model"))))
		(repeat (sslength ss)
		  (setq	ent (ssname ss 0) a (entlast))
		  	(command "_explode" ent)
			(while (setq a (entnext a))
			(if (eq (cdr (assoc 0 (entget a))) "INSERT") (ssadd a selset))
			)
		  	(ssdel (ssname ss 0) ss)
		  )
  )
  (sssetfirst nil selset)
  (print (sslength selset))(princ)
  )

 

When processing entities other than the current layout better not use command line approach instead use Entmod or VL mehod.

 

just my 2 cents Smiley Wink

 

HTH

 

Message 10 of 10
hmsilva
in reply to: pbejse

pbejse,
Thank you very much.

 

Henrique

EESignature

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

Post to forums  

Autodesk Design & Make Report

”Boost