Getting a list of Component from current collection


Busy looking for a how to get a list of component of the current selected collection.
Did look at Sketchupmodel definitions but can find anything about component collection. So what is the rigth naming for this and place to look? Any samples?
Thank you

use Ruby #grep to create an array is the fastest way…

model = Sketchup.active_model
selection = model.selection
comp_instances = selection.grep( Sketchup::ComponentInstance )


1 Like

And to get an Array of the definitions for the selected components you can do something like this:


So far as I know, the SketchUp Ruby API has no concept for external collections of components, it only knows about the component definitions already loaded into the model (either by the user or via DefinitionList#load_from_url). However, a component is just a .skp file and when loaded, the definition’s name is created by stripping the .skp extension off the file name. So, if you know the path to your collection folder, you can use ordinary Ruby Dir and File methods to get the list of files in the collection folder and generate the component names from the file names.

I was afraid of that.

However, I could get a skp list using system(“sh #{shell script}”) or the Dir and File, like you wrote,
however my component file names are not 100% equal to the components name.
However I could open the skp files one by one in that folder and check for the component in that skp file,
however this can take some time.

My end goal is to reload automatically all the components inside a skp model which are present inside my personal component collection. For the moment they are heavily under construction. It is very time consuming to reload a component with reload and then selecting ( read guessing) if it was component#1 or component#1a because the file name doesn’t have to be equal to the component name. You get my drift here?
Any thoughts?

Ah, you are right the component definition name is saved inside the file and applied when it is reloaded. Unfortunately, there is no API to probe for this name inside the file and the file format itself is proprietary and undocumented. Loading the component into a model is the only way to find out the definition name :disappointed:

I know it isn’t what you want to hear, but you would be better served by adopting a workflow in which you give your components meaningful names and save them using that as the file name instead of accepting the auto-generated ones such as Component#1. It’s a case of “pay me now or pay me later”…

The names are actually meaningful, and component#1 where an example, but I understand.

I think about this to get them right, I ment to get them equal with the file name ( or visa versa maybe better ). ( My knowledge about array iteration and getting the file list of the dir is minimal. Just started this morning with ruby and SU )

file_arr = List of "Components/PP50/" files
path = Sketchup.find_support_file file_arr[ind], "Components/PP50/"
component_definition = Sketchup.active_model.definitions.load path
name =[ind]

However, What will happening with the underlaying component inside a component? Little scare about that. << to myself

I’m so impressed by all this coding stuff you do @eneroth3. It’s way over my head and baffles me.

Oh, and your Solid Tools Extension is brilliant too. Thank you.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.