[Request] Name, Definition Name of Components
-
def renameInstances() ### model=Sketchup.active_model model.definitions.each{|defn| name=defn.name puts "Defn Name= "+name name.gsub!(/[0-9]+$/,'') name='Doh!' if name=='' defn.instances.each{|inst| inst.name=name puts "Inst Name= "+name } } end#def
-
Thx but this can works on the ruby console?
I believe some scriptor will use it -
Ok I play to the coder but sorry where I input the name "Dice"?
I save that in a rb file, for appear in a menu - works fine
try some places for input "Dice"
but seems I miss something -
My quick example code works on all instances of all definitions...
If you want to affect only a certain named collection of definitions then this version will work for you...
You save it asrenameInstances.rb
in ../Plugins/ and run it by typingrenameInstances 'Dice'
in the Ruby Console - where 'Dice' [inside ' ' or " "] is the 'string' pattern we'll look for in the definitions' names. Note how 'Dice' matches that text anywhere in the name, but adding a leading '^' as in '^Dice' matches only if the string is at the very start, or with a trailing '$' as in 'Dice$' it matches the string only at the very end - AND combos inside '[]' as in '[Dd]ice' matches with both 'Dice' and 'dice', anywhere in the names etc; if you add no 'argument' then ALL are definitions/instances are processed.
I've also removed groups/images from the processing, and I've added a one step undo...def renameInstances(str = '') str=str.to_s ### makes sure! model = Sketchup.active_model model.start_operation('renameInstances') model.definitions.each{|defn| next if defn.image? or defn.group? dname = defn.name if str != '' next if dname.gsub(/#{str}/,'') == dname ### NO match to 'str' so skip it... end#if puts "Defn Name = "+dname iname = dname.gsub(/[0-9]+$/, '') ### i.e. it strips ALL numerals OFF the end of 'name' iname = 'Doh!' if iname == '' ### i.e. it was ALL numerals and so it vanished !! defn.instances.each{|inst| inst.name = iname puts "Inst Name = "+iname } } model.commit_operation end#def
???
-
In V6
I have 2 components no instancied
with definition name: Component#1, Component#2 all without name
Wishing
definition name: Dice1, name: Dice
definition name: Dice2, name: DiceI select the 2 component
launched in ruby Console your last ruby script, type renameInstances 'Dice'
result :
True
but nothing is changed for my 2 components selected -
@unknownuser said:
In V6
I have 2 components
with definition name: Component#1, Component#2 all without name
Wishing
definition name: Dice1, name: Dice
definition name: Dice2, name: DiceI select the 2 component
launched in ruby Console your last ruby script : result
True
but nothing is changed for my 2 components selectedIt doesn't work like that... AND you never asked for that, initially you said that you had a component named 'Dice1' and you wanted to name its instances as 'Dice'
Where in my description did I say it works on renaming a selected component ???
You now say you want to rename 'Component#1' as 'Dice1' and rename its instances as 'Dice'.
That's completely different!!
First you need a renameDefinitions tool to change the name of 'Component#1' to be 'Dice1' and then use the current tool to rename 'Dice...' definitions' instances as 'Dice'.
Save this code into a file in Plugins called 'renameDefinitions.rb'.
Usage: in the Ruby Console type in
renameDefinitions 'Component#', 'Dice'
to rename all components containing that text, which are renamed with the new text 'Dice' - so
'Component#1' >>> 'Dice1'
'Component#2' >>> 'Dice2'
but 'Component' stays as 'Component'
If there is no second argument the matching text is simply removed from the name.
To match only the first two 'Component#' definitions use a pattern like
renameDefinitions 'Component#[12]$', 'Dice#1'
So only
'Component#1' >>> 'Dice#1'
'Component#2' >>> 'Dice#2'
but 'Component#3' stays as 'Component#3'
To fix the '#' in the replacement 'Dice#', run again as
renameDefinitions 'Dice#', 'Dice'
so all of the 'Dice#' names >>> 'Dice' names...
OR if bizzare is needed
renameDefinitions 'Dice#', 'AfghanistanBananistan'
Sodef renameDefinitions(str = '', txt = '') str = str.to_s ### makes sure! txt = txt.to_s model = Sketchup.active_model model.start_operation('renameDefinitions') model.definitions.each{|defn| next if defn.image? or defn.group? dname = defn.name if str != '' next if dname.gsub(/#{str}/,'') == dname ### NO match to 'str' so skip it... end#if nname = dname.gsub(/#{str}/, txt) ### i.e. it replaces str with txt nname = 'Doh!' if nname == '' ### i.e. it was an exact match and txt=''!! defn.name = nname puts "Defn Name; "+dname+" = "+nname } model.commit_operation end#def
NOW use the renameInstances tool to change any instances of renamed definitions as a second step.
-
That what that: missunderstanding my first post
If you re read sequentially my first post I never said that i had component named Dice
I said "I wish" -
@unknownuser said:
I wish rename all this collection of component selected like this
First component
Definition name = Dice1
Name = DiceIF you had said:
I wish to rename all selected components like this...
Definition name = 'Component#1'
New Definition name = 'Dice1'
New Instances name = 'Dice'
That's what you would have got!
Anyway, you now have something that will do it what you want - renaming the definitions by pattern matching and then a second tool renaming the matching definitions' instances by stripping off the numerical part of the defn name and using that... -
Thx for your efforts
No possibility to enter a joker ?
because my selection of components can have any Definition names!
So make a general renaming in one pass -
Of course all your components will have a name - even if it's 'Component#1' it's still a name ???
Therefore you'd use
renameDefinitions 'Component#', 'Dice'
which acts like a 'wildcard' and every component containing the text 'Component#' will have that replaced with 'Dice' ??????
So 'Component#1 >>> 'Dice1', 'Component#2 >>> 'Dice2' etc
As I then explained further you can constrain the renaming to a smaller set by using other pattern matching like [1238] to just process the first three and the eighth one
renameDefinitions 'Component#[1238]', 'Dice#1'
to rename them all first as as 'Dice#1', 'Dice#2' etc and then in a second step using
renameDefinitions 'Dice#', 'Dice'
to rename them all as as 'Dice1', 'Dice2' etc...This does NOT work on a highlighted 'selection' !!
You make your selection using a pattern matching string in the name!!!! -
So there is no possibility to change 2 objects components Definition named
Ball
Poll
to these Definition named Dice1, Dice2 in one pass? -
No...
-
Finally i have found a trick with another Console Script of your!
I explode all components selected and call your tricky thing who tranform any selections in Components!m=Sketchup.active_model; ### tranform any selections in Components By TIG m.start_operation("Faces>Compos"); n=m.active_entities;m.selection.to_a.each{|e|(g=n.add_group(e.all_connected); g.to_component.definition.name="FaceSet#1")if e.valid? and e.parent==n.parent and e.class==Sketchup;;Face}; m.commit_operation
So obtain a generic Components' name: FaceSet#1, FaceSet#2 etc...
Call your renameInstances many posts above
So obtain an only one name for all the Components: FaceSet#
So I can use it inside the Plug Random Replace Component
and even now no kneed to change it in Dice!...but I must change my tutorial!
Thanks again for your lessons!
I have seen many things about renaming but see also that I am definitly not made for ruby scripting! -
and...In fact that is some redundant with the use of the Dialog box Info for rename a group of components!
I had an headache for a so simple solution -
My trick works only with components with no level of component inside
and for rename a same component with definition name "Ball" existing in different positions
as new components with of course new definitions name Ball1, Ball2, Ball3 etc... ?
I suppose they must be "Make Unic" then renamed
Advertisement