Jump to content

Insert block from pull down menu, Autoname block


sketch11

Recommended Posts

In AutoCAD you can insert objects from another drawing as a block, using a pull down menu.

In this project, I have a number of files corresponding to steel sections, 1 file per section.

There are the MNU and script files in the same folder (see attachments).

 

 

The issue: If I insert in a drawing, the name of the block seems to take the name of the file. I want to change it to a random name such as "A$C7ABC15J1".

 

The reason is that if I insert the same Section in two separate drawings, and edit the blocks in different ways, and then copy/paste between each other, the appearance does not carry over. Reason is that they have the same name. This may be confusing so don't worry if it doesn't make sense. I just need to have a random name. Exploding and re-pasting as a block is a nuisance and at times I do forget to do this.

 

STRUCTURAL_STEEL.mnu STRUCTURAL_STEEL.scr

Link to comment
Share on other sites

Rather than obscure names as part of menu I would change your insert to a lisp so it could check if block exists, if so look for -x ie add -1 to block name so insert 3 times block name is UB125-3 you would rename 1st block to say -1 then insert UB125 and rename. Need to have a think about it.

 

-Rename block blockname newname note all blocks of this name in dwg are renamed.

 

       [->UB]
                 [610UB125]^C^C(blkins "610UB125")

 

[610UB125]^C^C(if (not blkins)(load "blkins"))(blkins "610UB125")

Edited by BIGAL
Link to comment
Share on other sites

OK, thanks. This is a bit confusing, I'm going to have to read up on LISP.

With the above algorithm though, you could have the same block name in 2 separate drawings, when copied/pasted, this is an issue.

Link to comment
Share on other sites

You will still have to insert and rename as simplest, you can copy files to a new name then insert. If your happy then 610UB125-37 for the 37th time you have inserted. 610UB125-123456.

 

Get filename and add 1 then rename. This way you have only 2 files at any time. Original and a renumbered copy. The lisp can check if no -1 else make it.

 

if you want to use the rename you need to store somewhere a file with last number. This can be local setenv,  but if network need to be on server.

Link to comment
Share on other sites

 

 

Ver 1, You need say a text file that gets read,  say with every UB in it line by line with -0 on end.  When you insert the block it reads this file and looks for the number on the end. It then rewrites the file with the new number added.

 

Ver 2 just have a file some where that has a number in it and add 1 to it as it becomes the new block name. Much easier than ver 1, but you may end up with 610UB125-345 for the 345th time you have inserted any of the UB blocks.

 

You did not answer one question is it to be used by multiple users or just you, for just you (setenv "UB" 23) then you can (getenv "UB"), for multi users it would need a file on the server.

 

It is very solvable but you need to say which way you would like to go, plenty here can write some code to do this, as I said earlier I would not make random block names 30+ years of cad.

 

Being able to find blocks "UB*" has significant advantages.

 

Link to comment
Share on other sites

To answer the question: It will only be used by me, local computer only.

I guess it's the code I need help with.

I would not worry about the name, I've never searched for a block name in the past and would be happy with a random name, if this makes things simpler, otherwise a name such as you have noted above.

Link to comment
Share on other sites

Do these blocks have attributes associated with them? 

How many insertions of each of these unique blocks do you place in each drawing? 

Thinking dynamic blocks, xrefs, or groups may work better for what you're trying to do.

Link to comment
Share on other sites

There are no attributes. Just simple lines and arcs.

Number of insertions: More than 1, up to a handful as a guess.

Dynamic blocks, xrefs, or groups? I would prefer to keep as simple as possible, just a simple block.

 

Is there not something I can add to the line of code to differentiate, such as a time stamp?

 

With line below, something like "UniversalBeams_610UB125+2019.12.14.1025"

[610UB125]^C^C-INSERT UniversalBeams_610UB125;scale;1;

 

Link to comment
Share on other sites

Should work not tested but every time ran will make x + 1

 

to test copy this to command line

(setenv "ub" "0) ; only needed to set seed X can be reset any time each dwg ?

 

(setq x (atoi (getenv "UB")))(setenv "UB" (rtos (+ x 1) 2 0)) -INSERT UniversalBeams_610UB125;scale;1;(getreal)(command "-rename" "block" "UniversalBeams_610UB125" (strcat "UniversalBeams_610UB125" "-" (rtos x 2 0))

 

Simplest way is a defun passing block name. ^c^C(blockins "UniversalBeams_610UB125") 

Link to comment
Share on other sites

If it helps this will produce a random name that will never repeat using current date to the nearest second converted to hexadecimal.

(defun datename ( / STD-NUM->HEX cdate day ctime hdate)
	(defun STD-NUM->HEX	(i / s a) ; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/decimal-to-hexadecimal/m-p/2874070#M293991
		(setq s "")
		(while (> i 0)
			(setq a	(rem i 16)
				  i	(lsh i -4)
			) ;_  setq
			(setq s	(strcat
						(if	(< a 10)
							(chr (+ 48 a))	; 48: (ascii "0")
							(chr (+ 55 a))
						) ;_  if
						s
					) ;_  strcat
			) ;_  setq
		) ;_  while
	) ;_  defun
		(setq cdate (getvar "cdate")
		day (fix cdate)
		ctime (atoi(substr (rtos (rem cdate day) 2 6)2))
		hdate (strcat (STD-NUM->HEX day)(STD-NUM->HEX ctime))
	)
)

 

Link to comment
Share on other sites

Try this it will keep adding numbers at any time to reset number so (setenv "UB" "0") it can be changed to have a separate UB ANG UC CHS etc number. You will need to load it 1st so simplest would be to add to your menu as a ^c^c(load "blkins") or else add to autoload.

 

; insert a block make a new block with name + 1
' By Alan H Dec 2019 info@alanh.com.au

(defun blkins (blkname / x temp)
(if  (tblsearch "BLOCK" blkname)
  (progn
    (alert "Block of that name already exists\n \nwill temporary rename")
	(command "-rename" "block" blkname "tempblk")
	(setq temp 1)
  )
)
(if (= (getenv "UB") nil)
  (setenv "UB" "0")
)
(setq x (+ (atoi (getenv "UB")) 1))
(setenv "UB" (rtos x 2 0))
(command "-INSERT" blkname (getpoint "\nPick insertion point") (getreal "\nEnter scale") "" (getreal "\nEnter rotation"))
(command "-rename" "block" blkname  (strcat blkname "-" (rtos x 2 0)))
(if (= temp 1)
(command "-rename" "block" "tempblk" blkname)
)
)

;^c^c(blkins "UniversalBeams_610UB125")

image.png.db90b6041b5961bcb61ffc643425d11a.png

Link to comment
Share on other sites

Thanks for the assistance.

I am still lost though.

 

With tombu's post, how do I incorporate this into the mnu file?

 

With BIGAL's code, I tried this but mnu file could not be loaded due to errors. How do I write into the mnu file? Also apologies but I find it really difficult to understand what you are saying. The other thing is that if you have 2 separate drawings, and you copy/paste, can you get the same block name?

 

I compiled this code years ago. It was not so much written but taken from a reference  book on AutoCAD which I can't find anymore. Please assume my coding skills are non existent.

 

Link to comment
Share on other sites

This is what your menu lines should look like I added one line for testing, you must copy and paste the code to notepad and save Blkins.lsp to a supported Autocad path, or else edit the (Load "c:\\yourdirectory\\your path\\etc\\Blkins") to suit, you can give it any name you like, just match. So pick "Load blocks" only need to do once.

 

***MENUGROUP=STRUCTURAL_STEEL
***POP12
**CTOPopSTRUCTURAL_STEEL
               [Structural Steel]
               [load blocks]^c^c(load "blkins")

               [->UB]

                 [610UB125Test]^C^C(blkins "UniversalBeams_610UB125")
                 [610UB125]^C^C-INSERT UniversalBeams_610UB125;scale;1;
                 [610UB113]^C^C-INSERT UniversalBeams_610UB113;scale;1;
                 [610UB101]^C^C-INSERT UniversalBeams_610UB101;scale;1;




               [->UC]
                 [310UC158]^C^C-INSERT UniversalColumns_310UC158;scale;1;
                 [310UC137]^C^C-INSERT UniversalColumns_310UC137;scale;1;
                 [310UC118]^C^C-INSERT UniversalColumns_310UC118;scale;1;
                 [310UC96.8]^C^C-INSERT UniversalColumns_310UC96.8;scale;1;

 

 

Blkins.lsp

Edited by BIGAL
  • Thanks 1
Link to comment
Share on other sites

Thanks BIGAL, this now works for me.

I will try to incorporate with different sections now.

 

As a final question, with:

Quote

So pick "Load blocks" only need to do once

 

Is it possible to execute this from the script file? Or is it that ".scr" files are different to ".lsp" files.

 

Link to comment
Share on other sites

Sorry Alan but I've been progressing and come up with an issue.

If Cancel is pressed at any stage, there is an error message and cannot run again (; error: Function cancelled). I tried adding the code below after the first line. It sometimes worked and sometimes not.

 

I was wondering if this can be done.

- Make as lean as possible: (1) Get rid of the error message that states "name already exists". (2) Don't ask for scale, default at 1 (3) Don't ask for rotation, default at 0°.

This is preferential as I want to cut out as many steps as possible, insertion point is the only thing required.

 

Would the issues go away if the block name was equivalent to a Date, to the nearest second  ... such as in tombu's post, Wednesday 9:47pm.

 

Understand if this is becoming too onerous though.

 

 

  ;; *error* local redefinition
  (defun *error* (msg)
    (if (/= msg "Function cancelled")
      (princ (strcat "\nError: " msg))
    )
    (if oom
      (setvar "osmode" oom)
    )
    (princ)
  )

 

Edited by sketch11
Link to comment
Share on other sites

Remove the Alert line

 

Remove the two getreals and replace with 1 and 0

 

Are you setting oom somewhere ? else just (setvar 'osmode 47) or your preferred value don't need if.

 

If you crash it should run again choosing menu.

Edited by BIGAL
  • Thanks 1
Link to comment
Share on other sites

Thanks for the above instructions Alan.

 

I'm still having issues with the Cancel though, even with the "(setvar 'osmode 47)" added.

One thing I forgot to say yesterday, was that this error message turns up on AutoCAD: "A block named "tempblk" already exists."

This was another reason I wanted to name the block as the date to the nearest second, even though it's not as pleasing as your solution, it cuts out one step, of having a temporary block name.

 

No need to take it any further as it's up to me to learn more LISP and try to resolve.

Cheers.

 

; insert a block make a new block with name + 1
; By Alan H Dec 2019 info@alanh.com.au

(defun blkins (blkname / x temp)

  ;; *error*
  (defun *error* (msg)
    (if (/= msg "Function cancelled")
      (princ (strcat "\nError: " msg))
    )
       (setvar 'osmode 47)
    (princ)
  )

(if  (tblsearch "BLOCK" blkname)
  (progn
;   (alert "Block of that name already exists\n \nwill temporary rename")
    (command "-rename" "block" blkname "tempblk")
    (setq temp 1)
  )
)
(if (= (getenv "UB") nil)
  (setenv "UB" "0")
)
(setq x (+ (atoi (getenv "UB")) 1))
(setenv "UB" (rtos x 2 0))
(command "-INSERT" blkname (getpoint "\nPick insertion point") 1 "" 0)
(command "-rename" "block" blkname  (strcat blkname "-" (rtos x 2 0)))
(if (= temp 1)
(command "-rename" "block" "tempblk" blkname)
)
)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...