Incremental rename bunch of components
-
-
If it doesnt exist itd be pretty easy to make.
Just to clarify, is the "name" in "name#N" constant?
-
And
DefinitionList.unique_name
does most of the job:
http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/definitionlist.html#unique_name@unknownuser said:
The unique_name is used to generate a unique name for a definition based on a base_name string. For example, a base_name of "Joe" might return "Joe #2"
Iterate the selected instances, collect the definitions.
Iterate the definitions naming them by whatDefinitionList.unique_name
.
Bob's your uncle. -
@remus said:
If it doesnt exist itd be pretty easy to make.
Just to clarify, is the "name" in "name#N" constant?
Yes. Something like this - cab#1, cab#2 ...
-
Copy/paste this code into a file called
componentrenamer.rb
in the Plugins folder.
To run it you typecomponentrenamer "NewName#1"
in the Ruby Console to change the name of selected component-instances' definitions toNewName#1
,NewName#2
,NewName#3
etc...
Note: starting with 'NewName
' increments the second one to 'NewName#1
'def componentrenamer(newname) model=Sketchup.active_model ss=model.selection defs=[] ss.each{|e|defs << e.definition if e.class==Sketchup;;ComponentInstance} defs.uniq.each{|d|d.name=model.definitions.unique_name(newname)} end#def
-
-
Nice!
I'm guessing the # sign is part of the internal SketchUp API? #s cause some problems with how I use models externally. I looked at the (dang simple) code and didn't see any immediate way to suppress the #.
This is for keeping all instances the same component, but using a different name, correct? It's not taking all instances and making them unique when renaming? (Similar to what happens when two components of the same name come in but are different -- the second one is named: name#1.)
-
To strip the # try adding an extra argument
def componentrenamer(newname,nnn=nil) model=Sketchup.active_model ss=model.selection defs=[] ss.each{|e|defs << e.definition if e.class==Sketchup;;ComponentInstance} defs.uniq.each{|d| unn=model.definitions.unique_name(newname) unn=unn.tr("#",nnn)if nnn d.name=unn } end#def
The second string in the translation
tr
nnn
can be anything you want - e.g. "-" or "_" ?
The usage now has an optional second argument to replace the # with something else
e.g.componentrenamer("NewName","_")
If you don't add it then the # remains unchanged...
-
Thanks TIG! I'll try it out (and learn a little more about SU Ruby)
Advertisement