Hello. I’m working on an alternative “component advanced attributes” panel. The code automatically updates the selected components in a dialog box. I have added a function to the button next to the “Price” value, where the user can enter a new “Price” value. However, I have not been able to get the function to work, and the button appears to be non-functional. I need your help in identifying where I have gone wrong.
require 'sketchup.rb'
class MySelectionObserver < Sketchup::SelectionObserver
def initialize(dialog)
@dialog = dialog
@selection = Sketchup.active_model.selection
end
def onSelectionBulkChange(selection)
update_dialog_values
end
def onSelectionCleared(selection)
update_dialog_values
end
def onSelected(selection)
update_dialog_values
end
def update_dialog_values
selection = Sketchup.active_model.selection[0]
if selection && selection.is_a?(Sketchup::ComponentInstance)
definition = selection.definition
name = definition.name
price = definition.get_attribute('SU_DefinitionSet', 'Price', '0.0')
@dialog.execute_script("document.getElementById('component_name').innerHTML = '#{name}';")
@dialog.execute_script("document.getElementById('price').value = '#{price}';")
end
end
end
dialog = UI::HtmlDialog.new({
:title => "Alternative Component Advanced Attributes Panel",
:preferences_key => "ferhatatmaca",
:scrollable => true,
:resizable => false,
:width => 400,
:height => 500,
:left => 100,
:top => 100,
})
html = <<~HTML
<html>
<head>
<title>Alternative Component Advanced Attributes Panel</title>
</head>
<body>
<h1 id="component_name"></h1>
<hr>
<label title="Bileşenin Fiyatı">Fiyat:</label>
<input style= 'width:180px; text-align: right;' disabled type='text' id='price' value=''>
<button type="new_price">Degistir</button>
<script>
document.getElementById('new_price').addEventListener('click', function() {
selection = Sketchup.active_model.selection
if selection.length == 1 && selection[0].is_a?(Sketchup::ComponentInstance)
component = selection[0]
definition = component.definition
prompts = ["Enter new price:"]
defaults = ["0"]
input = UI.inputbox(prompts, defaults, "New Price")
if input
new_price = input[0]
definition.set_attribute('SU_DefinitionSet', 'Price', '')
end
end
}
}
})
</script>
</body>
</html>
HTML
dialog.set_html(html)
observer = MySelectionObserver.new(dialog)
model.selection.add_observer(observer)
dialog.show
Video is here.