Swapping Component definition with instance

FYI, the fields in the Entity Info panel are name property fields.

It doesn’t make much sense to do this, as the reason they have separate name properties is so they can be different.

Often the definition name is a model / manufacturer identifier (part or stock number) and when instances are placed into the model, they get individual names based on their use.
(Ie, "Window - Master Bath", etc.)

Anyway … the main problem lies in that if there are multiple instances, as you iterate those instances, your code will be setting the definition’s name multiple times, and only the last setting (to the last instance name) will be persistent.

Ie, a common definition cannot carry the name of ALL of it’s instances. Ie, the definition’s name property is unique to it and can only hold one value.

The Ruby console can accept multiline code if you use Windows style line endings (CR+LF).

Basically your code should work.
I modified it a bit (wrapping it within a method and adding some customization arguments) …

def inamer(sep: ' ', prefix: '#', groups: false)
  m = Sketchup.active_model
    m.definitions.each { |d|
      next if d.image? || (!groups && d.group?)
      next if d.name.empty?
      d.instances.each_with_index { |i,index|
        i.name= "#{d.name}#{sep}#{prefix}#{index+1}"

Also, if you insert a model that is not set up to be a component, and has no name property, then SketchUp will set the definition name (after insertion) to be the basename of the filename (ie, minus the `“.skp” extension.)

1 Like