Author Topic: Script to change block name  (Read 1341 times)

0 Members and 1 Guest are viewing this topic.

like_citrus

  • Newt
  • Posts: 114
Script to change block name
« on: February 22, 2022, 01:58:05 AM »
I have a number of drawing files with a single block inside. Each file is a for a single Structural section.

Can a script file rename the block inside each file.
For example the file name is "530UB92.4", the block should be renamed to the filename or "530UB92.4".

An attempt:

Code: [Select]
FILEDIA 0
_.open "C:\Test\UniversalBeam_530UB82.0.dwg" RENAME BLOCK HERE _.qsave _.close
_.open "C:\Test\UniversalBeam_530UB92.4.dwg" RENAME BLOCK HERE FILEDIA 1 _.qsave _.quit

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Script to change block name
« Reply #1 on: February 22, 2022, 01:26:43 PM »

For example the file name is "530UB92.4", the block should be renamed to the filename or "530UB92.4".

If I'm understanding this line you should not do this. If the filename and block name within have the same name you'll get a circular reference.
"Block 530UB92.4 references itself"

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Script to change block name
« Reply #2 on: February 22, 2022, 04:36:59 PM »

For example the file name is "530UB92.4", the block should be renamed to the filename or "530UB92.4".

If I'm understanding this line you should not do this. If the filename and block name within have the same name you'll get a circular reference.
"Block 530UB92.4 references itself"

Completely agree.

I have a number of drawing files with a single block inside.

It would make more sense for the drawings to contain the block components as opposed to a block reference; then the drawing filename becomes the block name when inserted/referenced by another drawing. You can easily convert your drawings to components by either exploding the block reference or using WBLOCK to export it to a separate file.

like_citrus

  • Newt
  • Posts: 114
Re: Script to change block name
« Reply #3 on: February 22, 2022, 10:17:42 PM »
Thanks for your responses.

I have a couple of questions. Note the following:
- I'm using a full version of AutoCAD at times and also LT at other times. Not everything works with LT.
- The blocks inside each file are dynamic blocks (Section, Plan and Elevation for each structural section).

1. Will it be possible to have "block name" = "file name" + "Blk" ... to differentiate.

2. Lee Mac, regarding WBLOCK, this is how I converted files using your code a while ago, which worked.
https://www.theswamp.org/index.php?topic=56586.msg603637#msg603637
Is it possible to add another part of code to this?
I can then test with both the full version and LT.

BIGAL

  • Swamp Rat
  • Posts: 1434
  • 40 + years of using Autocad
Re: Script to change block name
« Reply #4 on: February 24, 2022, 12:09:58 AM »
Have you looked at wiseyssteelshapes.lsp etc it may do what you want with out blocks.
A man who never made a mistake never made anything

like_citrus

  • Newt
  • Posts: 114
Re: Script to change block name
« Reply #5 on: February 24, 2022, 12:50:50 AM »
Thanks for advising, this is quite useful.
I would like to keep going with what I've got as I spent a lot of time on it, even though it's not as good.

BIGAL

  • Swamp Rat
  • Posts: 1434
  • 40 + years of using Autocad
Re: Script to change block name
« Reply #6 on: February 24, 2022, 06:47:18 PM »
Lee Mac may comment have you looked at his Steal.lsp this can be ran to pull a single block out of a dwg with no user input ie dwgname & blkname this would be in your code you select UB somehow. I use (if (not steal) (load "steal")) so program is only loaded when required.
A man who never made a mistake never made anything

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Script to change block name
« Reply #7 on: February 24, 2022, 06:51:55 PM »
I have a couple of questions. Note the following:
- I'm using a full version of AutoCAD at times and also LT at other times. Not everything works with LT.

My suggestions would also be applicable to the use of these blocks in LT.

- The blocks inside each file are dynamic blocks (Section, Plan and Elevation for each structural section).

Even more reason to export the block definitions using WBLOCK rather than storing a block reference within each file; the drawing filename then becomes the block name when the dynamic block is inserted into another drawing. Opening the drawing file will automatically open the dynamic block definition in the Block Editor.

like_citrus

  • Newt
  • Posts: 114
Re: Script to change block name
« Reply #8 on: February 24, 2022, 09:54:13 PM »
Thanks for noting WBLOCK works on LT. I tried this and it works.

The issue is complicated to explain but is as follows.
- It was setup for the full version of AutoCAD.
- The filename and block name were the same, along the lines of "UniversalBeam_530UB82.0".
- Each drawing is inserted via a MNU file. See CODE 1A.
- The MNU file uses LISP routine to insert block and truncate name I think. So "UniversalBeam_530UB82.0" becomes "530UB82.0". See CODE 1B.

With LT, LISP doesn't work. I then tried to insert directly as per CODE 2.
This is for the block with WBLOCK applied.
When inserted, only lines/arcs are shown for the first visibility state of the dynamic block, and it's not a block anymore.
The only way CODE 2 works if I go back and rename all the blocks in the files. From "UniversalBeam_530UB82.0" to "530UB82.0" as an example.



CODE 1A: USED IN FULL AUTOCAD - USES LISP
Code: [Select]
***MENUGROUP=STRUCTURAL_STEEL_DYNAMIC
***POP112
**CTOPopSTRUCTURAL_STEEL_DYNAMIC
              [Structural Steel (D)]
              [->UB]
                 [530UB92.4]^C^C(BlockInsert_NotScaled_NotExploded "UniversalBeam_530UB92.4");
                 [530UB82.0]^C^C(BlockInsert_NotScaled_NotExploded "UniversalBeam_530UB82.0");
              [--]

CODE 1B: LISP
Code: [Select]
(defun BlockInsert_NotScaled_NotExploded (blkname)
    (command "_.-insert" (substr (strcat blkname "-" (menucmd "m=$(edtime,0,yymoddhhmmss)") "=" blkname) (+ (vl-string-position (ascii "_") blkname 1) 2)) pause 1 "" 0)
)

CODE 2: USED IN LT - NO LISP
Code: [Select]
***MENUGROUP=STRUCTURAL_STEEL_DYNAMIC
***POP212
**CTOPopSTRUCTURAL_STEEL_DYNAMIC
              [Structural Steel (D)]
              [->UB]
                 [530UB92.4]^C^C-INSERT 530UB92.4_File;SCALE;1;ROTATE;0;EXPLODE;Y;
                 [530UB82.0]^C^C-INSERT 530UB82.0_File;SCALE;1;ROTATE;0;EXPLODE;Y;
              [--]
« Last Edit: February 24, 2022, 09:59:09 PM by like_citrus »

like_citrus

  • Newt
  • Posts: 114
Re: Script to change block name
« Reply #9 on: February 25, 2022, 02:09:59 AM »
OK it looks like I had the error by exploding the inserted block in the menu file.
So all that needed to be done is just apply WBLOCK as mentioned.
Thanks again Lee Mac.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Script to change block name
« Reply #10 on: February 25, 2022, 06:08:59 AM »
You're welcome - glad you got there in the end  :-)

BIGAL

  • Swamp Rat
  • Posts: 1434
  • 40 + years of using Autocad
Re: Script to change block name
« Reply #11 on: February 26, 2022, 07:25:19 PM »
For insert its pretty straight forward can use [530UB92.4]^C^C-INSERT 530UB92.4_File SCALE 1 ROTATE 0 EXPLODE Y The semicolon is used to imply a return the space will do the same, sometimes need something like ;; to imply return twice. Been using space for like 40 years in menu's.
A man who never made a mistake never made anything