Thanks for the quick reply! I think I've done everything right, but I only get a new definition (loaded) when I use the write-to-dev-null trick (but it's painfully slow!). When I use the add_cpoint() or add_group() trick my SKP file is ignored and I get the same GUID back. Here's my full script, if you don't mind taking a look:
require "sketchup.rb"
module RefreshComponent
def self.force_reload_component_definition!(model, definition)
definition_path = definition.path
definition_name = definition.name
definition.name = definition.name + rand.to_s
## definition.entities.add_cpoint(ORIGIN) ## <== this causes GUIDs to match (fail)
definition.save_as("/dev/null") ## <== this causes new definition load (success)
reloaded_definition = model.definitions.load(definition_path)
puts "Old GUID; " + definition.guid
puts "New GUID; " + reloaded_definition.guid
reloaded_definition
end
def self.reconnect_component_instances!(model, old_definition, new_definition)
model.start_operation("Remap instances")
old_definition.instances.each { |instance| instance.definition = new_definition }
model.commit_operation
end
def self.delete_component_definition!(model, definition)
model.start_operation("Delete Definition")
definition.entities.erase_entities(definition.entities.to_a)
model.commit_operation
end
def self.refresh_component(model, definition)
model.start_operation("Reload current component definition", true)
reloaded_definition = force_reload_component_definition!(model, definition)
reconnect_component_instances!(model, definition, reloaded_definition)
delete_component_definition!(model, definition)
model.commit_operation
end
def self.start
model = Sketchup.active_model
model.start_operation("Reload component definitions from file", true)
model
.selection
.select { |entity| entity.is_a?(Sketchup;;ComponentInstance) }
.map { |instance| instance.definition }
.uniq
.each { |definition| refresh_component(model, definition) }
model.commit_operation
end
end
unless file_loaded?("refresh_component.rb")
UI.add_context_menu_handler do |context_menu|
context_menu.add_item("Reload Current Component Definition") {
RefreshComponent.start
}
end
file_loaded("refresh_component.rb")
end