How to get the definition of the component?


The red arrow point to the componentInstance’s name. But when I get the compoentDefinition. It’s return nil.
How to get the definition of the component?
By the way, is there any solution to solve the encoding problem?
Thank you.

comp.definition.name

Thank you for your reply. the problem has solved.
And, would you tell me how to get the components from the selection? I get the components via ‘each’ function. It’s so cumbersome to get the components from loop. But I didn’t find the sample way thought the API.

mod = Sketchup.active_model # Open model
sel = mod.selection # Current selection

sel.each do |selected_comp|
   puts selected_comp.definition.name
end

ehhh…
I just use this method to get the components. Is there any better way here? For in my using way, there are should be only one component in selection.
However, It can be used anyway.

The API only extends Ruby. Most API and Ruby Core collection classes have the Core Enumerable module mixed into them.

This adds the #grep method.

Ex:

mod = Sketchup.active_model # The model reference
sel = mod.selection # Current selection

# Get all the component instances ...
comps = sel.grep(Sketchup::ComponentInstance)

# Get an array of their names ...
inst_names = comps.map(&:name)

# Get all the faces ...
faces = sel.grep(Sketchup::Face)

… and so on … and so forth …

wooow, that’s greet!
Actually, I’m only be familiar with C#.
Thank you all, guys!
Many many thanks!

1 Like

Also see this old topic …

Per Release notes for SketchUp API v 2020.2

  • Fixed Unicode characters not displaying correctly in the Ruby Console.

So make sure that you have installed the latest update of v2020 or upgrade to v2021.