I 'm try to repalce my DC component to the old select component.
so i’m try to make a plugin ,but it got failed
as the code
require 'sketchup.rb'
require 'extensions.rb'
module MySketchupPlugin
#Hash table used to store component dimensions
@component_data = {}
@original_component = nil
#Create toolbar
toolbar = UI::Toolbar.new "My Plugin Toolbar"
#Define the first button (get component information)
Get_info_cmd=UI:: Command.new ("Get Component Information"){
model = Sketchup.active_model
selection = model.selection
if selection.length == 1 && selection[0].is_a? (Sketchup::ComponentInstance)
@original_component = selection[0]
bounds = @original_component.bounds
width = bounds.width
height = bounds.height
depth = bounds.depth
@component_data = { width: width, height: height, depth: depth }
UI. messagebox ("Component information obtained \ nWidth: # {width} \ nHeight: # {height} \ nDeep: # {depth}")
else
UI. messagebox ("Please select a component")
end
}
Get_info_cmd.small_icon="get_info_icon. png" # small icon (custom icon path)
Get_info_cmd. large_icon="get_info_icon. png" # Large icon (custom icon path)
Get_info_cmd.tooltip="Get selected component information"
Get the length, width, and height information of the selected component
Get_info_cmd.menu_text="Get component information"
toolbar.add_item get_info_cmd
#Define the second button (replace component)
Replace_cmd=UI:: Command.new ("Replace required component"){
if @component_data.empty? || @original_component.nil?
UI. messagebox ("Please obtain component information first")
else
#Pop up the resource manager
File_cath=UI.oppanel ("Select Components", "SketchUp Files | *. skp | |")
if file_path
begin
model = Sketchup.active_model
entities = model.active_entities
#Start operation
Model. start_operation ('Replace Component', true)
#Load new component definition
definition = model.definitions.load(file_path, allow_newer: true)
Raise 'Unable to load component definition' if definition.nil?
#Obtain the position and rotation information of the original component
transformation = @original_component.transformation
#Delete the original component
entities.erase_entities(@original_component)
#Add a new component instance
new_component = entities.add_instance(definition, transformation)
Raise 'Unable to add new component instance' if new_component.nil?
#Ensure that the model update has been completed
model.commit_operation
#Refresh the view to ensure that new components are loaded correctly
model.active_view.refresh
#Get the dynamic properties of the new component
dynamic_attributes = new_component.attribute_dictionary('dynamic_attributes', true)
if dynamic_attributes
#Update dynamic attributes
dynamic_attributes['lenx'] = @component_data[:width]
dynamic_attributes['leny'] = @component_data[:depth]
dynamic_attributes['lenz'] = @component_data[:height]
else
UI. messagebox ("There is no 'dynamic_attributes' in the dynamic attribute dictionary of the new component")
end
#Ensure that the changes take effect
model.active_view.refresh
#Pop up a message box indicating successful replacement
UI. messagebox ("replacement successful")
#Clear cached data
@component_data.clear
@original_component = nil
rescue => e
UI. messagebox ("Component replacement failed: # {e.message}")
end
else
UI. messagebox ("No valid component file selected")
end
end
}
Replace_cmd.small_icon="replace_icon. png" # small icon (custom icon path)
Replace_cmd. large_icon="replace_icon. png" # Large icon (custom icon path)
Replace_cmd.tooltip="Replace selected component"
Replace_cmd. statis_bar_text="Replace the selected component and set the size of the new component"
Replace_cmd.menu_text="Replace required components"
toolbar.add_item replace_cmd
#Define the third button (get dynamic properties)
Get-dynamic _ attributies_cmd=UI:: Command.new ("Get dynamic properties"){
model = Sketchup.active_model
selection = model.selection
if selection.length == 1 && selection[0].is_a? (Sketchup::ComponentInstance)
component = selection[0]
dynamic_attributes = component.attribute_dictionary('dynamic_attributes', false)
if dynamic_attributes
attributes_text = dynamic_attributes.map { |key, value| "#{key}: #{value}" }.join("\n")
UI.messagebox(attributes_text)
else
UI. messagebox ("Selected component has no dynamic properties")
end
else
UI. messagebox ("Please select a component")
end
}
Get-dynamic _ attributies_cmd. small_icon="dynamic_attributies_icon. png" # small icon (custom icon path)
Get-dynamic _ attributies_cmd. large_icon="dynamic_attributies_icon. png" # Large icon (custom icon path)
Get the dynamic properties of the selected component from get-dynamic _ attributies_cmd. tooltip
Get all dynamic properties and their values of the selected component from get-dynamic _ attributies_cmd. statis_bar_text
Get_ dynamic_attributies_cmd.menu_text="Get dynamic properties"
toolbar.add_item get_dynamic_attributes_cmd
#Display toolbar
toolbar.show
end
but after finish the replace ,the new DC’s lenx leny lenz is not be changed ,how can i revise the code, thanks all