Replace components in component browser tray

Hi all,
I have just learned and approached Ruby API.
Throughout the process of research, I’m interested in plugin “Component Replacer” of Mr. Thomthom and it’s really helpful for my work especially for scope of woodworking designing with my DC.
But honestly, there are 2 points I am considering and need your kind advice as below:
Firstly, it can only replace components in current Model and can not replace components in component browser tray.
Secondly, I want to perform Reraw DC in order that DCs are recalculated after being replaced.
Could you please give me kind advice or any idea about the way to deal with 2 above problems? Looking forward to your kind advice. Thank you

I’m not sure if you want to modify the “Component Replacer” or you already tried to write your own.
Would be the best if you can provide your code what is failed, than someone may take a look on it…
Perhaps you can check this methods (including but not limited to)

The #load method is used to load a component from a file.

The #add method is used to add a new component definition to the definition list with the given name.

The #remove method is used to remove a component definition from the definition list with the given component definition. This will remove all instances of the definition.

The #purge_unused method is used to remove the unused component definitions.


Except for the definitions listed for the “In Model” collection, all other browser lists are of folders of SKP files in the user’s AppData “Components” folder tree.

So to replace a file your code would need to overwrite that file with a new one. However, this can be difficult. The components listed in the browser are shown by their “name” property, and not their true filename.

We have discussed access to SKP files in the Issue Tracker, but so far the API’s Sketchup::Skp module only gives access to a component file’s guid.

This means you would have to have a component file loaded into the model’s definition list in order to get the definition’s path property in order to know where to save the new edition of this component.

This example is release 2021.0 or higher:

def update_definition_file(definition_name)
  deflist = model.definitions
  cdef = deflist[definition_name]
  return false unless cdef # nil if not found
  filepath = cdef.path
  return false if filepath.empty?
  return false unless filepath =~ /Users/i
  if cdef.guid != Sketchup::Skp::read_guid(filepath)
    cdef.save_as(filepath) # Update the file on disk
end ### update_definition_file()

Care must be taken not to overwrite distribution component definitions in the ProgramData “Components” path! Only modify the custom user components in the User %AppData% (or “Application Support” for Mac) path folders.

For general info … there are still some open bugs regarding component definitions:

1 Like

Thank you for your helpful suggestions, I have learned these methods and find that this knowledge is really basical and necessary for Newbie like me.

Thanks for your detail reply. Honestly, I have learned a lots from your support for many topics in this forum. Espescially, in another topic, I find your support related to Component Browser from UI.openpanel and I think it can be the best solution for me in this situation.
However, even when this method (Component Browser from UI.openpanel) is effective, Openpanel window will continuously turn on and off for each using time, it’s not really optimal for our using process.
Htmldialog seems to be suitable solution (can use 2 clicks to load component, webdialog cannt do this point) but it requires knowledge about another language => quite hard for me to do.
Could you please kindly support giving an idea with Htmldialog like that? Many thanks

1 Like

What topic was this ?

And yes, UI::openpanel is for a manual workflow to select files.