Get a list with all the components in component browser

Good morning to all.
I’m very new in Ruby programming (know just a tiny bit of python).

I’m trying to insert all the Components that are in the Component browser to the Point [0,0,0], but don’t know how to access the components.

model = Sketchup.active_model
entities = model.active_entities
  Sketchup.active_model.start_operation("cama", true)
  point = Geom::Point3d.new(10, 20, 30)
  transform = Geom::Transformation.new(point)
  path = Sketchup.find_support_file("Bed.skp", "Components/Components Sampler/")
  definitions = model.definitions
  componentdefinition = definitions.load(path)
  instance = entities.add_instance(componentdefinition, transform)
Sketchup.active_model.commit_operation

This is the code I could glue from examples in the web, but don’t know more than this.
How to point the “path” to components Browser?
Is “Sketchup.find_support_files” the right command to get the list?

Thank you very much!

The Model #definitions method retrieves a definition list containing all of the component definitions in the model.

model = Sketchup.active_model
definitions = model.definitions

Then you can find by name of the definition e.g. if there is a component with a definition name “Bed”

my_def = definitions.find{|d| d.name == "Bed"}

Then inser it similar as you did:

point = Geom::Point3d.new(10, 20, 30)
transform = Geom::Transformation.new(point)
instance = entities.add_instance(my_def, transform)
1 Like

Will try that for a single object, thank you.
I am trying to get and process a list of 500 components after .DAE Batch importing.
Inserting each by name is out of question.

Was expecting something like:

clist = [abc, def, ghi, etc] #Components list that I get from component browser (How?)
clist.each do | ??? |
instance = entities.add_instance(my_def, transform)
model = Sketchup.active_model
model.start_operation('Import DAE', true) # This will speed up things
files_to_import = [
  '/some/path/file1.dae',
  '/some/path/file2.dae',
]
files_to_import.each { |path|
  definition = model.definitions.import(path)
  instance = model.entities.add_instance(definition, ORIGIN)
}
model.commit_operation
2 Likes

Perhaps you can check if these components already have an instance inserted into model, if not then you can find it:

The ComponentDefinition #count_used_instances method is used to count the total number of component instances in a model using this component definition. This method takes into account the full hierarchy of the model.

model = Sketchup.active_model
definitions = model.definitions
clist = definitions.select{|d| d.count_used_instances == 0 }
## Or alias method
# clist = definitions.find_all{|d| d.count_used_instances == 0 }
clist.each do | clist_element |
  instance = entities.add_instance(clist_element, transform)
end

BTW.
Beside Sketchup Ruby API doc you can examine the general Ruby doc. If I rmember right your version (SU 2018) has Ruby 2.2.4:
Class: Array (Ruby 2.2.4) (ruby-doc.org)
Module: Enumerable (Ruby 2.2.4) (ruby-doc.org)

Thank you tt_su!
In one step got the Batch import and the Insert at Origin!

1 Like

dezmo, that was just an example list.
Appreciated!

1 Like

Sure. …and my answer is just an example of criteria to filter out from definitionlist. :wink:
Anyway, solution from Thomas is more elegant!

Have a nice fun with Ruby!

It’s a method not a command. And it can retrieve a list of files in one of SketchUp’s support directories, … but only 1 directory at a time.

For example … to get a list of all the distributed sample dynamic component skp files …

files = Sketchup.find_support_files("skp","Components/Components Sampler")

But, … you are really not interested in any of the files distributed with SketchUp.
So, you get lists of files by using Ruby’s core Dir class.

path = "/some/folder/path"
files = Dir.glob("*.{dae,DAE}", base: path)