Hai…any one can we set attribute to a componet ? How ?
What kind of attribute? Be specific.
any, may be “count of tire”
You want to know how many tire components there are in the model? Again, be specific about what you are trying to achieve.
example…we set attribute count of tire to a component “car sedan”, then copy. next, how to select all “car sedan” with attribute 4 tire. may be like that
You can do that with ruby. Is that what you are asking for?
absolutely yes
You should post in the correct place. I’ll move for you.
You need to add an attribute dictionary to your component definition.
sorry my bad…
model = Sketchup.active_model
sel = model.selection
comp = sel[0]
comp.set_attribute "catalog", "price", 4.99
how we select all that comp in model with it’s attribute??
Your request is not entirely clear…
Let’s assume you have some components in your model and you want to count instances of them, ‘by name’, you could use some simple Ruby code in the Ruby Console…
This pasted into the Console + enter will list all component instances, by name, and the number of each…
module TIG
module Instance_Reporter
def self.run()
model=Sketchup.active_model
defns=model.definitions
comps=[]
defns.each{|defn|
comps << "#{defn.name} === #{defn.instances.length}"
}
comps.sort!
puts
comps.each_with_index{|comp, i|
puts "#{i+1}: #{comp}"
}
return "reported #{defns.length} definitions"
end#def
end#module
end#module
TIG::Instance_Reporter.run()
It’s easy to change to match exact definition names or patterns and thereby shorten the list…
no…no…no, not like that. i just make it clear before
You earlier reply crossed with mine.
It’s still quite possible to extract what you want…
I’ll prepare some more code…
i want same component = same attribute
When you say ‘select’ do you want to actually highlight the things in the model or make a list in the Ruby Console ?
Are you setting the price attribute to the definition or an instance ?
So you want to report the matches for the price etc ?
This reports on instances with the matching attribute [price]
# assumes comp.set_attribute("catalog", "price", 4.99) already set up for instances
module TIG
module CountReporter
def self.run(price=0)
model=Sketchup.active_model
defns=model.definitions
matches=[]
defns.each{|comp|
comp.instances.each{|i|
if i.get_attribute("catalog", "price", -1) == price
matches << comp.name
end#if
}
}
puts "#{matches.length} instances of "
puts matches.uniq
end#def
end#module
end#module
TIG::CountReporter.run(4.99)
# adjust the cost as required, here's it's 4.99
in model
instance
not, i just want to make sure, all same component have same attribute
I’ll modify the code to highlight all instances in the model with a matching price attribute…
thankyou for your help (sorry my bad)…i just want make sure all component have same attribute, like all component have same entity.
comp.set_attribute "catalog", "price", 4.99
it just set attribute all component in model after placed from component option, not component before placed from component option. sorry my bad “languange”
Try this…
# assumes comp.set_attribute("catalog", "price", 4.99) already set up for instances
module TIG
module MatchHighlighter
def self.run(price=0)
model=Sketchup.active_model
sel=model.selection
sel.clear
defns=model.definitions
matches=[]
defns.each{|comp|
comp.instances.each{|i|
if i.get_attribute("catalog", "price", -1) == price
matches << comp.name
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
# clear all model
# placed component to model from component option "car sedan"
# select component
# set attribute to component
model=Sketchup.active_model
sel=model.selection
comp = sel[0]
comp.set_attribute "catalog", "price", 4.99
sel.clear
TIG::MatchHighlighter.run(4.99) #>>> Highlight 1 component ( correct )
#copy component car sedan by tool move
TIG::MatchHighlighter.run(4.99) #>>> Highlight 2 component ( correct )
#place component car sedan from component option again
TIG::MatchHighlighter.run(4.99) #>>> Highlight 2 component ( Uncorrect ) it must 3 select
Is you component a dynamic component?