Author Topic: Increment a block name +1 upon insert  (Read 2059 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Bull Frog
  • Posts: 327
Increment a block name +1 upon insert
« on: March 12, 2021, 12:34:55 PM »
I have Lee Mac's CopyRenameBlock.lsp and it works great within the drawing. It's dang near what I want but in block library form.

Open library
choose block
test for the block name in the drawing with/without numeric suffix.
if it exists insert with/without suffix increment it's name by 1.

block.dwg is in the drawing
newly inserted block is incremented +1 and renamed block_1.dwg

To be clear, I'm not talking about a field or an attribute. I am talking about the block name.
Like using "block edit" to SaveAs a new block of an existing block.

This is way over my head. As are most things.

J.
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

d2010

  • Bull Frog
  • Posts: 323
Re: Increment a block name +1 upon insert
« Reply #1 on: March 12, 2021, 12:38:32 PM »
Please you upload a sample Drawing.dwg (as old-version acad2010), because progecad failed in
Lisp-function/s.If you upload a sample Drawing.dwg then you repair many bug/s inside Lisp.

I have Lee Mac's CopyRenameBlock.lsp and it works great within the drawing. It's dang near what I want but in block library form.

jlogan02

  • Bull Frog
  • Posts: 327
Re: Increment a block name +1 upon insert
« Reply #2 on: March 12, 2021, 01:08:46 PM »
My apologies for not being clear enough in my post.

I'm simply asking the question "can this be done?" or "has this been done?".

I don't have any code created, I don't have any sample blocks created.

J.

J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Increment a block name +1 upon insert
« Reply #3 on: March 12, 2021, 01:33:21 PM »
Maybe something like this?
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ bn n r)
  2.   (setq bn "BlockName"
  3.         n  -1
  4.   )
  5.   ;; While a block definition exists keep going
  6.   (while (tblobjname "block" (setq r (strcat bn "-" (itoa (setq n (1+ n)))))))
  7.   (alert r)
  8.   (princ)
  9. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Increment a block name +1 upon insert
« Reply #4 on: March 13, 2021, 06:38:45 PM »
My Copy/Rename Block Reference already includes logic to increment the block name and will offer the incremented result as a default - this is implemented at line 87 of the program source code. As such, replacing lines 88-95 with (setq new "") should cause the default to be automatically used, yielding the behaviour you have described.

jlogan02

  • Bull Frog
  • Posts: 327
Re: Increment a block name +1 upon insert
« Reply #5 on: March 19, 2021, 10:54:42 AM »
My Copy/Rename Block Reference already includes logic to increment the block name and will offer the incremented result as a default - this is implemented at line 87 of the program source code. As such, replacing lines 88-95 with (setq new "") should cause the default to be automatically used, yielding the behaviour you have described.


Told you it was over my head. I was looking at lines 57-78, thinking I have to somehow get the selection of the block picked from the slide library, check to see if it exists in the drawing and then do the magic. I see that I didn't mention "slide" library in the OP.

I'll give that a shot and see what I get.

Thanks Lee
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Increment a block name +1 upon insert
« Reply #6 on: March 19, 2021, 11:05:17 PM »
The other thing you need to do is to check what is last number for a block then add 1. Else will start to get block exists.

Again Lee has a good parse number lisp that will pull out numbers from a string, that is provided that all blockname is A-Z, if not then make a str say last 2 characters of blockname and check for number, a Blockname-X note the - would be the best way to delimit where does number start.

If your using a Menu slide library then its simple ^c^C(If (not LM:RenameBlockReference-2 ) (load "My_Copyrenameblock")) (LM:RenameBlockReference-2   t "Block_area") so the block name is passed and make changes as Lee has suggested.
« Last Edit: March 19, 2021, 11:11:15 PM by BIGAL »
A man who never made a mistake never made anything

jlogan02

  • Bull Frog
  • Posts: 327
Re: Increment a block name +1 upon insert
« Reply #7 on: March 22, 2021, 02:34:33 PM »
The other thing you need to do is to check what is last number for a block then add 1. Else will start to get block exists.

Thanks for the confirmation.
Let's say my characters before the -#(increment) are known character sets (library name abbreviation).

...-SL-#(increment).dwg
...-AC-#(increment).dwg
...-CTL-#(increment).dwg
and so on.

Does that help me in any way?

Again Lee has a good parse number lisp that will pull out numbers from a string, that is provided that all blockname is A-Z, if not then make a str say last 2 characters of blockname and check for number, a Blockname-X note the - would be the best way to delimit where does number start

So, if I have numbers in the blockname I need to make str say, last 2 or 3 characters of the name? Got it. I also need to use a hyphen not an underscore. Thanks for letting me know, as I was thinking underscore.

If your using a Menu slide library then its simple ^c^C(If (not LM:RenameBlockReference-2 ) (load "My_Copyrenameblock")) (LM:RenameBlockReference-2   t "Block_area") so the block name is passed and make changes as Lee has suggested.

Thanks for this. I would have never gotten the macro correct.

Now let's see if I can do any of this.

Hold please....

J.
« Last Edit: March 22, 2021, 02:43:55 PM by jlogan02 »
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Increment a block name +1 upon insert
« Reply #8 on: March 22, 2021, 08:03:11 PM »
Underscore is fine just use that as a search for character.

If you get stuck just ask.
A man who never made a mistake never made anything