Bug in SU2022 - Ruby can create two components with the same name, and no suffix #1, #2 etc

Bug found by Steve Baumgartner (@slbaumgartner) in a more complex extension under development, on which we are both working.

In SU 2022, but not earlier versions (tested in SU 2021 and 2017) Ruby allows the creation of two components with identical names, and no distinguishing suffix (#1, #2 etc). Steve had several bugsplats which he suspects followed on from this.

The following simplified code demonstrates the problem. On 2022 it generates two Components, both with definitions named “MyName” and 1 instance each. On 2021 9and 2017) the second component gets named “MyName#1” as expected.

module BugDemo
  model = Sketchup.active_model
  entities = model.entities
  
  gp1 = entities.add_group()
  gp1.entities.add_cpoint(ORIGIN)
  comp1 = gp1.to_component()
  comp1.definition.name = "MyName"

  gp2 = entities.add_group()
  gp2.entities.add_cpoint([0.0,0.0,1.0])
  comp2 = gp2.to_component()
  comp2.definition.name = "MyName"

  model.definitions.each {|defn| puts "definition name = #{defn.name}, #{defn.count_instances} instances"}
end

Run in SU 2021 and 2017 it generates the following differently named components, listed in the Component Browser as follows:

image

But when run in SU 2022 two different but identically named components are created:
image

Perhaps better to report here:

1 Like

Thanks, Dezmo. Steve @slbaumgartner suggested reporting it on GitHub too, but I didn’t know where to go to do that.

Now done.

1 Like

:thinking:
Maybe as a workaround this can be used (?)
DefinitionList#unique_name instance_method

e.g.:

.
.
comp1.definition.name = model.definitions.unique_name "My Name"
.
.
comp2.definition.name = model.definitions.unique_name "My Name"

Edit: It is creating the same buggy result :frowning:

1 Like

:bulb:You can refer to this topic in your GitHub if you edit your post there… so the information’s can be collected from both places.

finger exercise…

def my_uniq_defn_name( name )
  names = Sketchup.active_model.definitions.map(&:name)
  i = 0
  base_name = name
  while names.include?( name )
    i += 1
    name = "#{base_name}##{i.to_s}"
  end
  name
end
u_name = my_uniq_defn_name( "MyName" )

Looks like a good workaround for the original issue.

In our extension, though, if it has the same name, we now think we want it to use the original component, as the name created is effectively a description of the component parameters. If the name is the same, the component has the same geometry.

I wonder how DefinitionList#[] works (or does not) in 2022.0 with duplicate names ?

I haven’t investigated all the aspects, but since the definition name is supposed to be the unique key for a definition there are many possibilities to break things.

2 Likes

After creating the definitions with a code in a first post and using this snippet:

model = Sketchup.active_model
definitions = model.definitions
p definitions.map{|c| "Name: #{c.name}, id: #{c.persistent_id} "}
ids = []
10000.times {
  component = definitions["My Name"]
  ids<<component.persistent_id
}
ids.uniq

Result:

["Name: My Name, id: 18 ", "Name: My Name, id: 22 "]
=> [18]

It seems the query with a duplicate name will return the first occurrence of the definition from the list ( consistently ? )

1 Like

@Whaat

Seems it is fixed now in SU version 20.0.354. At least on Windows! :+1:

2 Likes