Lisp to delete all blocks of various name

Lisp to delete all blocks of various name

Anonymous
Not applicable
5,657 Views
22 Replies
Message 1 of 23

Lisp to delete all blocks of various name

Anonymous
Not applicable

My experience with lisp is finding code that sort of does what I want, editing and hoping for the best. Thru trial and error, research and a little luck I usually stumble on a workable solution. Today I'm stuck.

For construction drawings I need a number of entities in floor plan cad that client wants stripped from cad before passing it to them. I'm trying to delete all instances of a list of different blocks.

Code below works for single line selecting "pq-*" or "RefQ*" but with both lines nothing is deleted and error at bottom is returned. Hope this is a simple one, what am I missing?

 

(defun c:del4q ()
(sssetfirst nil (ssget "_x" '((2 . "pq-*"))))(command "_.erase")
(sssetfirst nil (ssget "_x" '((2 . "RefQ*"))))(command "_.erase")
(princ)
)

 

Command: DEL4Q
_.erase
Select objects: _.erase
*Invalid selection*
Expects a point or Window/Last/Crossing/BOX/ALL/Fence/WPolygon/CPolygon/Group/Add/Remove/Multiple/Previous/Undo/AUto/SIngle/SUbobject/Object

 

Thanks in advance.

0 Likes
Accepted solutions (2)
5,658 Views
22 Replies
Replies (22)
Message 2 of 23

David_W_Koch
Mentor
Mentor

I have not tested this at all, but suggest you try the following:

(defun c:del4q ()
(sssetfirst nil (ssget "_x" '((2 . "pq-*"))))(command "_.erase" "")
(sssetfirst nil (ssget "_x" '((2 . "RefQ*"))))(command "_.erase" "")
(princ)
)

Adding the "" at the end of each command function acts the same a pressing the ENTER key, closing the command.  That should result in the objects selected in the first sssetfirst function being erased, and result in there being no active command when the second line runs.  The command line feedback you posted shows the second erase command being fed to the first erase command, causing an error and resulting in no erasures.


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

0 Likes
Message 3 of 23

Anonymous
Not applicable

Thank you for suggestion. I've tried code as shown and with "" at several other locations. With code as shown nothing is deleted and command dialog shows as follows

 

DelBlk.lsp successfully loaded.
Command:
Command:
Command: DEL4Q
_.erase
Select objects:
Command: _.erase
Select objects:
Command:

 

If I add the double quotes to the first line only objects in the second line are deleted, adding double quotes to second line only nothing is deleted, the following is command dialog

DelBlk.lsp successfully loaded.
Command:
Command:
Command: DEL4Q
_.erase
Select objects: _.erase
*Invalid selection*
Expects a point or Window/Last/Crossing/BOX/ALL/Fence/WPolygon/CPolygon/Group/Add/Remove/Multiple/Previous/Undo/AUto/SIngle/SUbobject/Object
; error: Function cancelled
Select objects:

0 Likes
Message 4 of 23

David_W_Koch
Mentor
Mentor
Accepted solution

Ok, I did test it and it was not working well for me, either.  Using sssetfirst and then using the command function to operate on the selected objects is not what I would normally do in an AutoLISP routine.  The following code should erase all of the blocks in your file whose block names start with either pq- or RefQ.

(defun c:del4q (/ ss1)
  (setq	ss1 (ssget "_X"
		   '((-4 . "<OR")
		       (2 . "pq-*")
		       (2 . "RefQ*")
		     (-4 . "OR>")
		    ) ;_ End filter list.
	    ) ;_ End ssget.
  ) ;_ End setq.
  (command "_.erase" ss1 "")
  (princ)
) ;_ End c:del4q.

David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

0 Likes
Message 5 of 23

Anonymous
Not applicable

That's great, works perfectly. Thanks so much.

 

If you don't mind one additional question if you have brief explanation what the -4 and 2 and I've also seen -1 in code do in the selection set.

 

Thanks again

0 Likes
Message 6 of 23

David_W_Koch
Mentor
Mentor
Accepted solution

The "_X" mode of the ssget function selects all of the objects in the database, unless you add a filter list, in which case only objects whose properties match those given in the filter list are selected.  A filter list is an association list, in the same format as the data returned by the entget function.  Each item is a dotted pair, with the first element being the DXF group code for the property and the second item being the value.  The 2 DXF group code is the same for all objects, and is a "name".  For Block References, it is the Block name.

 

You can find links to the DXF group codes associated with various graphical objects here,

 

The -4 DXF group code does not actually occur on an object, but is used to allow relational tests or logical grouping of filter tests in the ssget function.  The Help pages to which those links will take you will explain their use far better than I can.  In this particular example, I used the OR logical grouping of filter tests to allow one ssget function to gather all block references whose names start with either of two strings.  If you had the need, additional (2 . "xxx*") items could be placed between the two -4 groups to get block references starting with other initial strings [where xxx represents the initial characters in the block reference names you want to get].


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

0 Likes
Message 7 of 23

Anonymous
Not applicable

Thanks David, your explanation did what I was unable to find reading helps and google search results for hours.

You've been a great help, thanks again

0 Likes
Message 8 of 23

Lee.MalaspinaJMHCX
Participant
Participant

I have not been able to get this to work when scripted. I use the AUTOSCRIPT add-on. I wrote my script as usual, telling it to load the script and then calling the command to run the script. AutoCAD responds with the "Unknown Command" response. I have altered the script numerous times and it still will not run. Any ideas?

0 Likes
Message 9 of 23

pendean
Community Legend
Community Legend

@Lee.MalaspinaJMHCX wrote:

AutoCAD responds with the "Unknown Command" response. I have altered the script numerous times and it still will not run. Any ideas?


That can simply mean your script file is incorrectly coded. Nothing more. Show your work, share your actual script file here.

0 Likes
Message 10 of 23

Lee.MalaspinaJMHCX
Participant
Participant

I simplified the LSP name to DBLKS and used this format. It works this way with many other LPSs [space] is a blank space:

 

(LOAD "DBLKS")
DBLKS
QSAVE

[space]

0 Likes
Message 11 of 23

Lee.MalaspinaJMHCX
Participant
Participant

PS, it works fine if i drag it into the DWG and thenb call the command. This is the modified LSP:

 

 

(defun c:DBLKS (/ ss1)
(setq ss1 (ssget "_X"
'((-4 . "<OR")
(2 . "---*")
(-4 . "OR>")
) ;_ End filter list.
) ;_ End ssget.
) ;_ End setq.
(command "_.erase" ss1 "")
(princ)
) ;_ End c:DBLKS.

0 Likes
Message 12 of 23

David_W_Koch
Mentor
Mentor

For some reason, scripts always give me a challenge.  I never seem to get the syntax right.  Have you tried adding a blank line between the (LOAD "DBLKS") and DBLKS lines?  Not sure if that will work, but I have had occasions where calling the command immediately after the load did not work because the load did not execute immediately.

 


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

0 Likes
Message 13 of 23

pendean
Community Legend
Community Legend
May I ask why is your lisp NOT set to autoload with the start of your program?
Do you not have an ACADDOC.LSP to autoload it for all sessions?
Or Startup Suite in APPLOAD command?
0 Likes
Message 14 of 23

Lee.MalaspinaJMHCX
Participant
Participant

That is a long and sad story. We have software issues and no support here. I keep it simple and do not have the same problem with the other lsp routines that I use. I just reinstalled AutoCAD Electrical and will not customize it until it proves out to be a stable installatioin.

0 Likes
Message 15 of 23

Lee.MalaspinaJMHCX
Participant
Participant

So, aside fron your concern as to why I am calling the command only when I actually need it loaded, do you have any thoughts on why this will not run automated?

0 Likes
Message 16 of 23

pendean
Community Legend
Community Legend

@Lee.MalaspinaJMHCX See the other reply that pretty much agrees your way of doing can be troublesome.

Change it since you confirmed that pre-loading the LISP works just fine too.

 

If you are having software stability issues then reach out to Autodesk Support through your accounts page

https://knowledge.autodesk.com/customer-service/account-management/users-software/support-options/we...

and consider posting in the installation forum that is dedicated to helping folks with unstable software get stabilized https://forums.autodesk.com/t5/installation-licensing/bd-p/24

 

 

HTH

 

0 Likes
Message 17 of 23

Lee.MalaspinaJMHCX
Participant
Participant

OK, I have seen simirar difficulties like this. I will APPLOAD if I need to use it. Thanks.

 

0 Likes
Message 18 of 23

mc5fan
Advocate
Advocate

I cannot get this to work, no matter what I do. The suggestions previously offered do not work.I am doing something that has introduced a real need for a .lsp file that will run, AUTOMATED. Can anyone offer anything worth while on this problem?????

Lee M.
0 Likes
Message 19 of 23

pendean
Community Legend
Community Legend

@mc5fan wrote:

I am doing something that has introduced a real need for a .lsp file that will run, AUTOMATED. Can anyone offer anything worth while on this problem?????


read this https://blogs.autodesk.com/autocad/create-load-startup-lisp-routine/ 

and this https://knowledge.autodesk.com/support/autocad/learn-explore/caas/sfdcarticles/sfdcarticles/Automati...

 

 

0 Likes
Message 20 of 23

mc5fan
Advocate
Advocate

OK, nobody gets it. APPLOAD ans a STARTUP lsp is not the solution. I have done these and it still dose not function aside what I outlined.

 

Lee M.
0 Likes