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
2 Likes

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)
1 Like

Well done, you are wonderful, thank you very much, regards, and love, can I add the volume values, or just the volume? Thank you very much Sir.

Thank you very much, dezmo, I found something like this, I would appreciate it if you could evaluate the code, you can exist.

# add_DC_function.rb
# extends DCs functions
require('sketchup')
require('dynamiccomponents.rb')
if defined?($dc_observers)
  # Open SketchUp's Dynamic Component Functions (V1) class.
  # BUT only if DC extension is active...
  class DCFunctionsV1
    protected
    # DC Function Usage: =volume()
    # returns the instance's volume [cu"]
    if not DCFunctionsV1.method_defined?(:volume)
      def volume(a)
        return @source_entity.volume
      end
    end
  end#class
end#if

The code you found has nothing to do with mine.
__
You can extend my code e.g. by adding the method after “extend self” line:

  def g_volume(group)
    v = group.volume
    v > 0.0 ? " Vol:#{Sketchup.format_volume(v)}" : " Vol: Not solid"
  end

and change the e.name = add_zeros(@num.to_s) + h_w_d(e) line to

  e.name = add_zeros(@num.to_s) + h_w_d(e) + g_volume(e)

@dezmo thank you, I tried, but the result is the knowledge of the field, I guess…

Oops, obviously I’ve copy-pasted the mistake from the doc of
format_volume method…

Edited my previous post.

I can’t even see a mistake in this little one, but I will learn with you, my teacher, I looked up yes, I say how did I do it :slight_smile: teşekkür ederim .


nice :heart_eyes: :smiling_face_with_three_hearts: