Consecutively renaming over 1,000 instances of a component

I’m new to the Sketchup scene and have reached out to Justin Geis at at which point he suggested I post my question to this forum.
I have a component that I am duplicating and trying to rename each individual instance.
The first 10 components are positioned relative to each other and that relationship is constant.
They are numbered in order 0001 through 0010 by their distance from the ground (x,y plane) where 0001 is the highest (furthest from the ground) to 0010 which is the lowest (closest to the ground).
However, I am duplicating that same group of 10 a total of 136 times across the structure so I actually will have 1,360 components in the model.
Let’s say for the sake of argument that I want to label each instance from 0001 through 1360 going from top to bottom within each group of 10.
For example, the top component in the 3rd group of 10 would be β€˜0021’ and the bottom would be β€˜0030’ continuing until the last group of 10 (the 136th group) being labeled from top to bottom as β€˜1351’ to β€˜1360’ respectively.

I am using Ruby to rename the instances through scripting and the reason I’m posting is the following :
I label the original 10 manually β€˜0001’ through β€˜0010’, then I do a move with the Ctrl key to leave a copy and place the second group of 10, then multiply to create the rest. Each component now has an instance of β€˜0001’ through β€˜0010’ repeated 136 times. I want to name the unique instances from 0001 to 1360.

When I traverse this array in Ruby, element[0] through element[9] are correctly labeled,
HOWEVER, element[10] through [19], when the instance name is checked, appear to be random.
Element[10] has an instance name of β€˜0007’,[11] through [19] are 0002,0004,0008,0001,0005,0010,0009,0001 and 0003 respectively.
Elements [20] through [29] have a different randomization (ex. 6,5,9,3,1,8,4,10,2,7) and so on.

Is there a way to label each instance in the correct ascending order from β€˜0001’ to β€˜1360’ where, when traversed,
they reference from top to bottom in each group of 10 then move to the next group of 10, and so on.

Any help would be appreciated.
Craig (A Sketchup/Ruby novice)

When you say β€œgroup” do you mean an actual Sketchup::Group or are you meaning a selection set used with the Move tool?

Sounds like you would first want to sort your items by bounds.min.z (the bottom most point in the instance. Then you would simply name them using their index.

#this code assumes that all instances are selected
Sketchup.active_model.start_operation("Rename Instances")
sorted = Sketchup.active_model.selection.sort_by { |i| i.bounds.min.z }.reverse
sorted.each_with_index { |inst, idx| = (idx + 1).to_s.rjust(4, '0') }


1 Like

Sorry Dan, I should have specified. I created the first set of 10 in SU by using the move & ctrl to copy. Then I selected all 10 and moved/copied them to a new position. Lastly I used β€˜x7’ after the move to make a total of 8 copies. I then selected all 8 copies (of 80 components) and did the same with move/copy to create 16 more rows.
Each grid of 10 is placed in 8 rows and 17 columns to get the 136 groups of 10 or (1360 components in the model). I have not been using the β€˜Sketchup::Group’ in Ruby as I am still learning the OOP.
Thanks, Craig

Thanks Neil,
This is a good start for me. My only issue is that each of those 136 groups of 10 is on the same plane (for instance, the β€˜0001’,β€˜0011’,β€˜0021β€™β€¦β€˜1351’ are ALL at the exact same height from the ground at the highest point). In the same fashion, the β€˜0010’,β€˜0020’,β€˜0030β€™β€¦β€˜1360’ are all at the exact same height from the ground at the lowest point. So there is an 8 by 17 grid all sharing the same top height and then 9 other 8x17 grids at varying positions ending in the last 136 sharing the same lowest height. For some reason in SU, I can create the initial 10 and the Ruby array elements correspond to the order in which I moved/copied the component to create those original 10. However, when I move/copy that group of 10 across 7 more times along the x-axis then copy all 8 of those 16 more times along the y-axis, it seems that within each 10 elements of the array in Ruby, (i.e. element [10] through [19], [80] through [89], [1350] through [1359] the actual item within the group of 10 is random.

Ruby array of 1360 elements and their instance names :
[0] β€œ0001” [10] β€œ0007” [20] β€œ0006”
[1] β€œ0002” [11] β€œ0002” [21] β€œ0005”
[2] β€œ0003” [12] β€œ0004” [22] β€œ0009”
[3] β€œ0004” [13] β€œ0008” [23] β€œ0003”
[4] β€œ0005” [14] β€œ0001” [24] β€œ0001”
[5] β€œ0006” [15] β€œ0005” [25] β€œ0008”
[6] β€œ0007” [16] β€œ0010” [26] β€œ0004”
[7] β€œ0008” [17] β€œ0009” [27] β€œ0010”
[8] β€œ0009” [18] β€œ0006” [28] β€œ0002”
[9] β€œ0010” [19] β€œ0003” [29] β€œ0007” etc.

I’m not sure why SU does this or if there is a setting of which I am unaware to change the behavior.

In your case, the x or the y of the origin of the groups will be different from the previous ones so you could do a sorting by 2 conditions similar to:

sel = Sketchup.active_model.selection
sorted = sel.sort_by { |grp| [grp.transformation.origin.x, grp.transformation.origin.z] }
sorted.each_with_index { |inst, idx| = (idx + 1).to_s.rjust(4, '0') }

If you are copy/pasting them along the Y axis instead of the X axis change the grp.transformation.origin.x to grp.transformation.origin.y in the code.

Hope this helps.

I believe if you sort_by persistent_id it should sort them in the order they were added.

Sketchup.active_model.start_operation("Rename Instances")
sorted = Sketchup.active_model.selection.sort_by(&:persistent_id).reverse
sorted.each_with_index { |inst, idx| = (idx + 1).to_s.rjust(4, '0') }