Block Deletion using Script

Block Deletion using Script

IsisControls
Contributor Contributor
5,634 Views
6 Replies
Message 1 of 7

Block Deletion using Script

IsisControls
Contributor
Contributor

Is there a way to delete a specific block from within a drawing using a script? I've looked through many forums and can't find an exact answer but I do have a script working as below and so long as I insert the block as the last operation it will do exactly what I want.

 

The objective is to delete an existing title block, purge the drawing, save it and then close the sheet and repeat the cycle until all sheets have been updated. I then run another script that replaces the title block and this works great.

 

An even better solution would be to delete the old title block, purge and replace it with the new one all from within a single script but again is this possible as both blocks would have the same name albeit I could have the replacement block in a different file location.

 

Some of our projects can have up to 150 sheets so to change the drawing revision etc. manually can be very tedious.

 

My current script is as follows: -

 

_.open "C:\SteveW\P01.dwg"
Erase
L
_PURGE
_A
*
_N
_.qsave
_.close

_.open "C:\SteveW\P02.dwg"
Erase
L
_PURGE
_A
*
_N
_.qsave
_.close

 

etc.

0 Likes
Accepted solutions (2)
5,635 Views
6 Replies
Replies (6)
Message 2 of 7

pendean
Community Legend
Community Legend
Scripts can't do all of that in LT.

Read the INSERT method in this second half of the tutorial https://www.ellenfinkelstein.com/acadblog/replace-one-block-with-another/
Message 3 of 7

IsisControls
Contributor
Contributor

Hi Pendean,

 

Thank you for your reply. I've had a look at that and it works fine when not used in a script, so I must be doing something wrong. I have used the script as below:-

 

_.open "C:\SteveW\P01.dwg"

_.insert

TitleBlock2=C:\SteveW\TitleBlock2

Y

_.qsave

_.close

 

In Ellen's example after the Yes command to redefine the block you escape to quit otherwise you copy the new block over the old one and this is where my script fails as it asks for the insertion point etc.

 

So I assume I need to add something after the Yes to escape from the insert command and continue running through the rest of the script, looking through other posts and forums i have seen that some people say (command), (script-cancel) or simple carriage returns achieves this but this doesn't work so I'm stumped.

0 Likes
Message 4 of 7

pendean
Community Legend
Community Legend
Accepted solution
(command) is for lisp equipped versions of AutoCAD (aka not LT).

Finish the INSERT then just DELETE LAST.
0 Likes
Message 5 of 7

Johneng
Collaborator
Collaborator
Accepted solution

@IsisControls  A script file is nothing more than what you type on the command line. So yes there is a way to delete a specific block from within a drawing using a script. Just select it with an absolute coordinate point (X,Y,Z). The first sentence in the below link should say "You can use absolute or relative Cartesian (rectangular) coordinates to locate points when creating or selecting objects.

 

http://help.autodesk.com/view/ACD/2018/ENU/?guid=GUID-F64F8008-E1C0-49CC-A268-A6B8C6E9B566

 

If you do not know where an absolute coordinate point is on your title block use the ID command to find one.

 

ID command help link

http://help.autodesk.com/view/ACD/2018/ENU/?guid=GUID-F258AC00-5E9F-4B6B-A670-33F7708E3FB6

 

To select your title block find a point somewhere on your title block (ID command if you do not know one) and use those X,Y,Z numbers in place of the "L". (a lot of people insert their title block with the lower left corner at 0,0,0 so I will use that in the example below) To finish the select object portion of the ERASE command you will need a blank line between the L and -purge

Erase

0,0,0

 

-purge

 

the purge portion of your script is OK

 

To put in the insert portion of the script in you will just enter what you would type on the command line. You will need to know the following information.

block to insert

insertion point

X scale factor

Y scale factor

rotation angle

 

So assuming you are inserting your new title block at 0,0,0 at full size with 0 rotation, the insert new block portion of your script should look like this

 

-insert

C:\SteveW\TITLEBLOCK.dwg

0,0,0

1

1

0

 

Your save and close portions are OK

 

See the attached script file for an example of how it will look when all put together. Feel free to rename and modify it to suit your needs.

 

 

@pendeanwrote:
Scripts can't do all of that in LT.

 

WHY NOT? Did I miss something?

 

open a drawing file

_.open "C:\SteveW\P01.dwg"

 

delete a specific block

Erase

0,0,0

 

purge the drawing

_PURGE
_A
*
_N

 

insert a new title block

-insert

C:\SteveW\TITLEBLOCK.dwg

0,0,0

1

1

0

 

save and close

_.qsave
_.close

 

0 Likes
Message 6 of 7

pendean
Community Legend
Community Legend
@Johneng as long as you assume a block is always installed and can be selected from 0,0,0, but there is no guarantee.

Your solution, while valid, makes too many assumptions. I believe the OP already solved the issue with the less-assumptive method of replacing the block, a more useful tip that can be applied to any block needing redefinition.
0 Likes
Message 7 of 7

IsisControls
Contributor
Contributor

Thank you both for your responses, I have played around with the scripts and now got the solution I was looking for and it works great.