# Replace components in component browser tray

**URL:** <https://forums.sketchup.com/t/replace-components-in-component-browser-tray/171952>\
**Category:** Ruby API\
**Created:** [August 5, 2021, 3:38pm UTC](https://forums.sketchup.com/t/replace-components-in-component-browser-tray/171952 "2021-08-05T15:38:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![KQV](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@KQV](https://forums.sketchup.com/u/KQV)\
**Post date:** [August 5, 2021, 3:38pm UTC](https://forums.sketchup.com/t/replace-components-in-component-browser-tray/171952/1 "2021-08-05T15:38:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [August 5, 2021, 6:58pm UTC](https://forums.sketchup.com/t/replace-components-in-component-browser-tray/171952/2 "2021-08-05T18:58:42Z")

</div>

> [@KQV](#):
>
> Firstly, it can only replace components in current Model and can not replace components in component browser tray.

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](https://ruby.sketchup.com/Sketchup/DefinitionList.html#load-instance_method) method is used to load a component from a file.

The [#add](https://ruby.sketchup.com/Sketchup/DefinitionList.html#add-instance_method) method is used to add a new component definition to the definition list with the given name.

The [#remove](https://ruby.sketchup.com/Sketchup/DefinitionList.html#remove-instance_method) 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](https://ruby.sketchup.com/Sketchup/DefinitionList.html#purge_unused-instance_method) method is used to remove the unused component definitions.

> [@KQV](#):
>
> Secondly, I want to perform Reraw DC in order that DCs are recalculated after being replaced.

> [@Method to ask a dynamic component instance to recalc itself?](https://forums.sketchup.com/t/method-to-ask-a-dynamic-component-instance-to-recalc-itself/13905/6):
>
> Yes, true, you are correct. (1) It still exists. (2) It itself is poor practice, and could have been a class variable reference (implemented in a singleton manner.) It could have been exposed via a class constant or a class getter method. (3) As I said above, It might not exist in future versions, if they re-implement it’s reference per (2). So, you’ll need to do a object search and hold a local reference inside your module, to the same singleton object that $dc\_observers is also referenci…

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [August 6, 2021, 12:15am UTC](https://forums.sketchup.com/t/replace-components-in-component-browser-tray/171952/3 "2021-08-06T00:15:08Z")

</div>

> [@KQV](#):
>
> Firstly, it can only replace components in current Model and **can not replace components in component browser tray**.

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`](https://ruby.sketchup.com/Sketchup/Skp.html) 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:

```plaintext
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
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:

> <https://github.com/SketchUp/api-issue-tracker/issues/50>
>
> The current DefinitionList.load method is a drag to work with. It refuses to loa…d components from paths associated withe existing components that haven't been modified (since when, I don't know). Instead it just returns the existing component, even if the external file has been modified.
> 
> Sometimes it fails to load a new component even if the existing component is modified, but I'm not sure when or why (when the existing component was modified within the same operation as load was called maybe?). For these cases we have to first save out the existing component to a temp file, just to dissociate it with the path. This hack causes other issues though as ComponentDefinition.save\_as doesn't seem to record to the undo stack (despite changing the path property of the definition).
> 
> DefinitionList.load has so many weird behaviors and exceptions it's hard to even summarize it; I may have gotten this wrong. I literally get a headache from trying to figure out this mess. If someone could summarize the current behavior as pseudo code or something that would be appreciated.
> 
> To reload/relink a specific CompoenntDefiniton today you have to make sure Definitionlist.load regards it as a separate component to be able to load it at all, then you have to replace all references to the old component with the new one, make sure the name is correct (not having #1 appended), and finally purge the old definition.
> 
> I suggest a ComponentDefinition.reload method that by default reloads the definition from its associated path. The new data should be loaded into the existing ComponentDefinition object, with all instances and Ruby references now referring to the new data. This method should also take an optional path argument to relink the component from a custom location. This should be possible even with another component associated by that path and should not interfere with that other component in any way. The only way this method is affected by other definitions is that it makes sure the loaded component doesn't get a name that is already used by another component.
> 
> Simply but this should be a Ruby handle for what in the GUI is accessed from context menu \> Reload...
> \`\`\`\`ruby
> model = Sketchup.active\_model
> 
> \# Reload the first component from it's associated path, or raise error if internal (no associated path)
> \# or file is missing.
> \# The ComponentDefinition object is still the same, but its geometric content, name and other properties
> \# are loaded from the file.
> model.definitions.first.reload
> 
> \# Reload the first component from given path.
> \# If there is already a component in the model associated by this path, reload anyway and have
> \# two separate ComponentDefinitions both associated with the same file.
> \# If the component name inside the external file is already used by another component
> \# (regardless of whether it's associated by the same path) a new unique name is created for the
> \# loaded component, just as when a component is made unique.
> model.definitions.first.reload(path: "C:/Users/Eneroth/SketchUp/Model.skp")
> \`\`\`\`
> I also suggest an improved/corrected DefinitionList.load method that just doesn't do a any stupid stuff by its own. By default it should behave identically to pasting a component instance from the clipboard in the GUI, i.e. check if the GUID of the external file is used by an existing ComponentDefinition, if so return that, otherwise create a new CompoenntDefiniton and assure there are no name collisions.
> 
> There could be an optional argument to force loading into a separate ComponentDefiniton object even if there is an identical ComponentDefinition already present in the model (=identical GUID). This would be useful if the developer wants to edit the loaded component directly after loading, and don't want to affect any existing instances. This would of course create a new unique name, and therefore a unique GUID, directly on load.
> 
> \`\`\`\`ruby
> \# Loading from the same path could return the same CompoenntDefinition,
> \# but ONLY if the GUID is identical.
> model = Sketchup.active\_model
> model.definitions.size # =\> 0
> model.definitons.load("C:/Users/Eneroth/SketchUp/Model.skp")
> model.definitions.size # =\> 1
> model.definitons.load("C:/Users/Eneroth/SketchUp/Model.skp")
> model.definitions.size # =\> 1
> 
> \# \*Editing external file, or internal component here\*
> 
> \# Now a new ComponenDefinitiont is created.
> model.definitons.load("C:/Users/Eneroth/SketchUp/Model.skp")
> model.definitions.size # =\> 2
> 
> \# The method can also be forced to load as a new unique ComponentDefinition,
> \# even with no modifications to the file or existing component(s).
> model.definitons.load("C:/Users/Eneroth/SketchUp/Model.skp", force\_separate: true)
> model.definitions.size # =\> 3
> 
> \# In this example all components are associated by the same path, but are still separate objects.
> model.definitions.map(&:path) =\>
> \[
> "C:/Users/Eneroth/SketchUp/Model.skp",
> "C:/Users/Eneroth/SketchUp/Model.skp",
> "C:/Users/Eneroth/SketchUp/Model.skp"
> \]
> \`\`\`\`
> Perhaps the new DefinitionList.load needs to have a different name, and the old be kept as it is but deprecated, to assure backward compatibility. 
> 
> This has been discussed in a \[forum thread\](https://forums.sketchup.com/t/fix-definitionlist-load-problems-in-some-situations/5655) and ThomThom has already logged this as SU-30933 and SU-31274.

---

<div class="post-metadata">

**Author:** ![KQV](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@KQV](https://forums.sketchup.com/u/KQV)\
**Post date:** [August 12, 2021, 2:54pm UTC](https://forums.sketchup.com/t/replace-components-in-component-browser-tray/171952/4 "2021-08-12T14:54:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KQV](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@KQV](https://forums.sketchup.com/u/KQV)\
**Post date:** [August 12, 2021, 3:38pm UTC](https://forums.sketchup.com/t/replace-components-in-component-browser-tray/171952/5 "2021-08-12T15:38:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [August 12, 2021, 4:16pm UTC](https://forums.sketchup.com/t/replace-components-in-component-browser-tray/171952/6 "2021-08-12T16:16:43Z")

</div>

> [@KQV](#):
>
> 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.

**What topic was this ?**

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