Retrieving Tag names in Ruby Code?

Hi all, can someone tell me what the ruby code would look like to retrieve the Entity Info - Tag name for a group and place that name into the Instance: field?

Basically I want a loop that will go through all my model and copy the Tag name into the Instance field
hope this makes sense

thanks Jon

Some references:
As of SketchUp 2020 “Layers” were renamed to “Tags” in the UI. The API retains the use of “Layer” for compatibility and is synonymous with “Tag”.

The Model #definitions method
The DefinitionList object
The ComponentDefinition #group? method
The ComponentDefinition #instances method
The Group #make_unique method
The Layer #display_name method
The Layer #name= method
The Model #start_operation method

One possible code snippet:

def name_group_by_its_tag_name
  model = Sketchup.active_model
  group_defs = model.definitions.select(&:group?)
  model.start_operation("Rename groups by Tag", true)
  group_defs.each{|gd|
    gd.instances.each{|i|
      i.make_unique
      if i.layer.respond_to?(:display_name)
        lname = i.layer.display_name
      else
        lname = i.layer.name
      end
      i.name = lname
    }
  }
  model.commit_operation
end
name_group_by_its_tag_name
2 Likes

thanks, dezmo, it works perfectly :slight_smile:

is it possible to save this script so I can use it like an extension?

Put this into Plugin folder:

c:\Users\yourusername\AppData\Roaming\SketchUp\SketchUp 2022\SketchUp\Plugins\

Restart Sketchup. You will have new menu in Extensions… “Rename groups by its Tag name”

Dezmo_rename_groups.rb (788 Bytes)

No warranties! Use on your own risk!

2 Likes

You are a legend, thanks dezmo.

1 Like