• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

change block name from space to underscore

  • Thread starter Thread starter 77marco77
  • Start date Start date

77marco77

Guest
Good evening, I am looking for a way to change the name of a multitude of blocks (about 6500) present in a drawing. the necessary change is to replace the spaces present in the names of the blocks (some names of blocks do not have, others have 1, other 2 or even 3...) with the dash below. I tried with rename with all possible combinations of * but it does not work unless you opt for multiple letters and numbers and reduce the field to 2 or 3 at a time.
Thank you.
 
Have you already done a search in the forum? is a topic already discussed several times and maybe you find a ready script written in the veque discussions.
 
so it should go. the two functions vl-search-string-t and vl-string-subst-t are derived from the functions vl-string-search and vl-string-subst. the difference is that these last two recognize only the first substring.
Code:
(defun c:renblock ()
(setq blocks (vla-get-blocks))
(vlax-for elem blocks)
(if (vl-string-search-t " (vla-get-name elem))
(vla-put-name elem (vl-string-subst-t "_" (vla-get-name elem))
)
)
)

[Bleep] [Bleep] [Bleep] [Bleep [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep]
;;; function that returns a list with the positions of a substring in a string
(defun vl-string-search-t (string/number list)
(setq counter 0 list() number 0)
(while number
(setq number (vl-string-search element string counter))
(if number
(progn)
(Sects Lists)
counter (1+ number)
)
)
)
)
(reverse listings)
)

[Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Ble
;;; function which replaces substrings with others, in a string
(defun vl-string-subst-t (stringanuova stringavekkia string / stringa1 counter number)
(setq listings (vl-string-search-t stringavekkia string))
(setq string1 string)
(setq counter 0)
(foreach elem list
(setq number (vl-string-search stringavekkia stringa1 (+ elem counter))))
(setq stringa1 (vl-string-subst stringavekkia stringa1 number))
(setq counter (- (strlen string1)(strlen string)))
)
string1
)
 
so it should go. the two functions vl-search-string-t and vl-string-subst-t are derived from the functions vl-string-search and vl-string-subst. the difference is that these last two recognize only the first substring.
Code:
(defun c:renblock ()
(setq blocks (vla-get-blocks))
(vlax-for elem blocks)
(if (vl-string-search-t " (vla-get-name elem))
(vla-put-name elem (vl-string-subst-t "_" (vla-get-name elem))
)
)
)

[Bleep] [Bleep] [Bleep] [Bleep [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep]
;;; function that returns a list with the positions of a substring in a string
(defun vl-string-search-t (string/number list)
(setq counter 0 list() number 0)
(while number
(setq number (vl-string-search element string counter))
(if number
(progn)
(Sects Lists)
counter (1+ number)
)
)
)
)
(reverse listings)
)

[Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Bleep] [Ble
;;; function which replaces substrings with others, in a string
(defun vl-string-subst-t (stringanuova stringavekkia string / stringa1 counter number)
(setq listings (vl-string-search-t stringavekkia string))
(setq string1 string)
(setq counter 0)
(foreach elem list
(setq number (vl-string-search stringavekkia stringa1 (+ elem counter))))
(setq stringa1 (vl-string-subst stringavekkia stringa1 number))
(setq counter (- (strlen string1)(strlen string)))
)
string1
)
This is magic.

Thank you very much, do exactly what you have to do!
 
I know that the problem has been solved and I understand that by copying/pasteing leads to a prolisso code with double loop to other fragments that today do not think are necessary (perhaps with old versions, boh), in my opinion (although as it works) the code can be cleaner and optimized.

I propose my version, tested on the 2016 version of autocad and works.
I didn't have 6,500 blocks available, but there should be no problem. . .
Code:
(defun c:renblock2)
(setq blocks (vla-get blocks)
(vlax-for electric blocks)
(vl-string-search" (vla-get-name elem)
(vl-put-name elem (vl-string-translate "_"(vla-get-name elem))))
)
)
)
If someone wants to test it, I would like your feedback, even to understand if it works as it should.
 
Here, I missed the vl-string-translate function, never used among other things (no one is perfect). the two functions derived I wrote them long ago, I took advantage of their existence to do the command with fewer lines. thanks to the advice and optimization.
 
I know that the problem has been solved and I understand that by copying/pasteing leads to a prolisso code with double loop to other fragments that today do not think are necessary (perhaps with old versions, boh), in my opinion (although as it works) the code can be cleaner and optimized.

I propose my version, tested on the 2016 version of autocad and works.
I didn't have 6,500 blocks available, but there should be no problem. . .
Code:
(defun c:renblock2)
(setq blocks (vla-get blocks)
(vlax-for electric blocks)
(vl-string-search" (vla-get-name elem)
(vl-put-name elem (vl-string-translate "_"(vla-get-name elem))))
)
)
)
If someone wants to test it, I would like your feedback, even to understand if it works as it should.
When I see how these things with few lines are resolved I understand how much I really am "small". . .
works perfectly with 6358 blocks :)
Thank you so much!
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top