Batch rename Group Instance

I have a project of 350 odd groups.
I’m trying to find the odd group(s) that has a strange length.
I can only use instance to name the group to find the strange length.
Is there an extension to batch rename all groups instances?
Like 1,2,3,4…
Just want a quick n dirty .rd to batch rename all group instances.

Thanks in advance.

You wanted to say .rb. don’t you ? :wink:

:bulb: Something like this?
However it is not an .rb, just a snippet.
Copy paste the code snippet below to Ruby console and hit enter:
All groups, including nested will be renamed.
You can Undo it!
No warranties! Use your own risk!

module Dezmo
module GroupRenamer
  extend self
  
  def recur_group_rename(ents)
    ents.each{|e|
      case e
      when Sketchup::Group
        @num += 1
        e.make_unique
        e.name = @num.to_s
        recur_group_rename(e.definition.entities)
      when Sketchup::ComponentInstance
        if !@defs.include?(e.definition)
          recur_group_rename(e.definition.entities)
          @defs<<e.definition
        end
      end
    }
  end
  
  def grouprename
    model = Sketchup.active_model
    ents = model.entities
    @num = 0
    @defs = []
    model.start_operation("Rename groups to numbers", true)
    recur_group_rename(ents)
    model.commit_operation
    "#{@num} Groups renamed"
  end
end
end
Dezmo::GroupRenamer.grouprename

grouprename

3 Likes

This looks very usefull, but is there a way that it only renames groups that are not named yet?

module Dezmo
module GroupRenamerB
  extend self
  
  def recur_group_rename(ents)
    ents.each{|e|
      case e
      when Sketchup::Group
        if e.name == ""
          @num += 1
          e.make_unique
          e.name = @num.to_s
        end
        recur_group_rename(e.definition.entities)
      when Sketchup::ComponentInstance
        if !@defs.include?(e.definition)
          recur_group_rename(e.definition.entities)
          @defs<<e.definition
        end
      end
    }
  end
  
  def grouprename
    model = Sketchup.active_model
    ents = model.entities
    @num = 0
    @defs = []
    model.start_operation("Rename groups to numbers if no name", true)
    recur_group_rename(ents)
    model.commit_operation
    "#{@num} Groups renamed"
  end
end
end
Dezmo::GroupRenamerB.grouprename
1 Like

thank you very much

1 Like

Absolute Champion!
This issue was starting to annoy me again.
Saved a lot of agro.
Many thanks!

Is there a way to prefix 1 thru 9 with 0?
01
02

09

Also is there a way to add the height width and depth?

Thanks in advance.

You can decide how many digits will be used by the number part of the name.
The dimensions are formatted according to the Model Info unit settings.
See the animation.
Does not really tested. No warranties! Use your own risk.

grrename

module Dezmo
module GroupRenamerC
  extend self
  
  def add_zeros(name)
    name = "0" + name until name.size >= @digits
    name
  end
  
  def h_w_d(group)
    bb = group.bounds
    " H:#{bb.height.to_s} W:#{bb.width.to_s} D:#{bb.depth.to_s}"
  end
  
  def recur_group_rename(ents)
    ents.each{|e|
      case e
      when Sketchup::Group
        if e.name == ""
          @num += 1
          e.make_unique
          e.name = add_zeros(@num.to_s) + h_w_d(e)
        end
        recur_group_rename(e.definition.entities)
      when Sketchup::ComponentInstance
        if !@defs.include?(e.definition)
          recur_group_rename(e.definition.entities)
          @defs<<e.definition
        end
      end
    }
  end
  
  def grouprename(digits)
    model = Sketchup.active_model
    ents = model.entities
    @digits = digits
    @num = 0
    @defs = []
    model.start_operation("Rename groups to numbers + measurements if no name", true)
    recur_group_rename(ents)
    model.commit_operation
    "#{@num} Groups renamed"
  end
end
end
Dezmo::GroupRenamerC.grouprename(2)
1 Like

Beautiful!
Sincerely appreciate the contribution.

All the info is there.

Your previous code would rename no matter what.
This one does not.
Any chance of that again?
No prob if not.

module Dezmo
module GroupRenamer
  extend self
  
  def recur_group_rename(ents)
    ents.each{|e|
      case e
      when Sketchup::Group
        @num += 1
        e.make_unique
        e.name = @num.to_s
        recur_group_rename(e.definition.entities)
      when Sketchup::ComponentInstance
        if !@defs.include?(e.definition)
          recur_group_rename(e.definition.entities)
          @defs<<e.definition
        end
      end
    }
  end
  
  def grouprename
    model = Sketchup.active_model
    ents = model.entities
    @num = 0
    @defs = []
    model.start_operation("Rename groups to numbers", true)
    recur_group_rename(ents)
    model.commit_operation
    "#{@num} Groups renamed"
  end
end
end
Dezmo::GroupRenamer.grouprename

Yes. Here it is.
Again. Does not really tested. No warranties! Use your own risk.

module Dezmo
module GroupRenamerD
  extend self
  
  def add_zeros(name)
    name = "0" + name until name.size >= @digits
    name
  end
  
  def h_w_d(group)
    bb = group.bounds
    " H:#{bb.height.to_s} W:#{bb.width.to_s} D:#{bb.depth.to_s}"
  end
  
  def recur_group_rename(ents)
    ents.each{|e|
      case e
      when Sketchup::Group
        #if e.name == ""
          @num += 1
          e.make_unique
          e.name = add_zeros(@num.to_s) + h_w_d(e)
        #end
        recur_group_rename(e.definition.entities)
      when Sketchup::ComponentInstance
        if !@defs.include?(e.definition)
          recur_group_rename(e.definition.entities)
          @defs<<e.definition
        end
      end
    }
  end
  
  def grouprename(digits)
    model = Sketchup.active_model
    ents = model.entities
    @digits = digits
    @num = 0
    @defs = []
    model.start_operation("Rename groups to numbers + measurements", true)
    recur_group_rename(ents)
    model.commit_operation
    "#{@num} Groups renamed"
  end
end
end
Dezmo::GroupRenamerD.grouprename(2)