Need help to replace block with space in name

Need help to replace block with space in name

s.nijdam
Contributor Contributor
1,942 Views
7 Replies
Message 1 of 8

Need help to replace block with space in name

s.nijdam
Contributor
Contributor

I tried a lot of solutions to replace a block in a drawing. My script works fine when the to replace block has no space in the name. The name of the block I have to replace is "Title Block". I use below code in a script.

-INSERT Title Block=D:\Title_Block
(COMMAND \E "RESUME")
ATTSYNC name
Title_Block
Accepted solutions (1)
1,943 Views
7 Replies
Replies (7)
Message 2 of 8

Moshe-A
Mentor
Mentor

@s.nijdam  hi,

 

explore RENAME command.

 

you can not rename a block by inserting other block  over it. the procedure you took only override the block content not it's name.

 

moshe

 

0 Likes
Message 3 of 8

ronjonp
Mentor
Mentor

For scripting -RENAME 🙂

 

Or if you want to bypass command calls:

(defun _renameblock (old new / e)
  ;; RJP » 2019-08-01
  ;; Use at your own risk ;)
  (cond	((and (setq e (tblobjname "block" old)) (null (tblobjname "block" new)))
	 (setq e (entget (cdr (assoc 330 (entget e)))))
	 (entmod (subst (cons 2 new) (assoc 2 e) e))
	 (princ (strcat "\nBlock '" old "' renamed to '" new "'"))
	)
	((princ "\nBlock not renamed!!"))
  )
  (princ)
)
;; (_renameblock "foo" "face")
0 Likes
Message 4 of 8

s.nijdam
Contributor
Contributor

Renaming is not the goal of the script. The titleblocks are different. If I can add a rename function to get rid of the space in the blockname that would be nice. I'm a comlete noob in coding scripts. The part I have I grabbed from the internet.

0 Likes
Message 5 of 8

Moshe-A
Mentor
Mentor

@s.nijdam  hi,

 

check this RSB command. it let you Rename Selected Blocks.

it refers to the space character (or any other that might be there) as TOKEN and substitute it with another token.

for example in your case you want to eliminate spaces so i declare these constants:

(setq SOR-TOKEN " ") ; one space

to be substitute with

(setq TAR-TOKEN "")   ; no space

 

by setting these 2 constant variables you can control the blocks rename very easily. like changing from "_" to "-" and visa versa. remember that after you eliminate any tokens you want be able to change it back except for undo.

 

at end you will get a report of successful (or failed) rename.

 

enjoy

moshe

 

 

(vl-load-com)

; Rename Selected Blocks (replace tokens)

(defun c:RSB (/ subst-tokens ; local function
	        SOR-TOKEN TAR-TOKEN blocks ss sorBN tarBN AcDbBlkRef AcDbBlkTblRec)

 ; substitute tokens
 (defun subst-tokens (str) 
  (while (vl-string-search SOR-TOKEN str)
   (setq str (vl-string-subst TAR-TOKEN SOR-TOKEN str))
  )

  str 
 ); subst-tokens


 ; here start (c:RSB) command
 (setq SOR-TOKEN " "
TAR-TOKEN "") ; const
(setq blocks (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))) (if (setq ss (ssget '((0 . "insert")))) (foreach ename (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))) (setq AcDbBlkRef (vlax-ename->vla-object ename)) (setq sorBN (vla-get-effectiveName AcDbBlkRef)) ; does renamed block can be applied? (if (null (tblsearch "block" (setq tarBN (subst-tokens sorBN)))) (progn (setq AcDbBlkTblRec (vla-item blocks sorBN)) (vla-put-name AcDbBlkTblRec tarBN) (vlax-release-object AcDbBlkTblRec) (prompt (strcat "\nblock \"" sorBN "\" Rename to \"" tarBN "\".")) ); progn (prompt (strcat "\nblock \"" sorBN "\" Failed to rename to \"" tarBN "\".")) ); if (vlax-release-object AcDbBlkRef) ); foreach ); if (vlax-release-object blocks) (textscr) (princ) ); c:RSB
0 Likes
Message 6 of 8

Paul_Gander
Advocate
Advocate
Accepted solution

Did you try this?

 

-INSERT "Title Block=D:\Title_Block"

 

Message 7 of 8

s.nijdam
Contributor
Contributor

Thank you for the solution.

0 Likes
Message 8 of 8

todd.santee
Participant
Participant

This thread has been helpful. I was trying to redefine a block whose name had spaces in it. It turns out that if you put quotation marks around the block name, it recognizes the spaces as part of the string (rather than interpreting it as an "enter"). The script I used is below. The name of my block was _Town of Severance Address

 

-INSERT
"_Town of Severance Address="
Y
(COMMAND \e "RESUME")

 

I hope this helps someone. Cheers!

0 Likes