How to set attribute and get attribute to a component?

You are applying the attribute to an instance.
The first time it’s == 1 because you just assigned it.
Then the instance is copied using Move so the next time you == 2
But the third time you place a new instance of the component and you haven’t set its attribute, so the code still reports 2, because although there are 3 instances of the component, only 2 of those instances has the price attribute set !
You should see that only the first 2 instances get highlighted, but not the third one…
If you want to apply the attribute to the definition use…

model=Sketchup.active_model
sel=model.selection
comp = sel[0]
comp.definition.set_attribute "catalog", "price", 4.99
sel.clear

Then change the Highlighter to…

# assumes comp.set_attribute("catalog", "price", 4.99) already set up the definition
module TIG
  module MatchHighlighter
    def self.run(price=0)
      model=Sketchup.active_model
      sel=model.selection
      sel.clear
      defns=model.definitions
      matches=[]
      defns.each{|defn|
        if defn.get_attribute("catalog", "price", -1) == price
          matches << defn.name
          defn.instances.each{|i|
            sel.add(i)
          }
        end#if
      }
      puts "#{matches.length} instances of "
      puts matches.uniq
      puts "now selected with #{price}"
    end#def
  end#module
end#module
TIG::MatchHighlighter.run(4.99)
# adjust the cost as required, here's it's 4.99

UNCORRECT / WRONG / :man_shrugging:
Uploading: bandicam 2023-10-05 01-18-42-121.avi…

Your AVI upload is stuck.
So please explain the problem in words.
Google Translate might help you clarify your issue, and sound less abrupt in your posts -
I realize that English is probably not your first language, so help us to help you…

2 Likes

I see an error in my example code which didn’t count the selection length properly…
Here’s a corrected version…

model=Sketchup.active_model
sel=model.selection
comp = sel[0]
comp.definition.set_attribute("catalog", "price", 4.99)
sel.clear
# assumes comp.set_attribute("catalog", "price", 4.99) already set up the definition
module TIG
  module MatchHighlighter
    def self.run(price=0)
      model=Sketchup.active_model
      sel=model.selection
      sel.clear
      defns=model.definitions
      matches=[]
      defns.each{|defn|
        if defn.get_attribute("catalog", "price", -1) == price
          matches << defn.name
          defn.instances.each{|i|
            sel.add(i)
          }
        end#if
      }
      puts "#{sel.length} instances of "
      puts matches.uniq
      puts "now selected with #{price}"
    end#def
  end#module
end#module
TIG::MatchHighlighter.run(4.99)
# adjust the cost as required, here's it's 4.99

This is the problem @TIG says if you don’t apply the attibute to the definition…

ScreenFlow6

1 Like

If you apply the attribute to the definition [like the current code], by selecting an instance etc, my code will correctly select all matches, and the latest version reports the number selected properly in the Ruby Console.
You can not use the Instance Attribute Inspector because the attribute is attached to the definition, not its instances… So you are doing it wrongly !

You are using the old code to add the attribute to one instance.
You cannot expect it to magically apply to all instances.
My latest code assumes you add the attribute to a definition by selecting one instance and then the main code selects all of the instances of the definitions with that matching attribute…

I also think that the 3d figure you are testing with is a DC which is even more complicated as every instance is auto-made-unique on placement, so it has a separate definition too !!

1 Like