Is there a way to execute a Ruby process when you press the apply buttom in the Component Options Window (onclick Event)?
No there is no such event handling in ruby.
The “Component Options” window that you refer to, is a HTML dialog initiated by other encrypted extension, called Dynamic Components ( basically shipped with SU ).
However depends what you want to achieve you may try to use some observers to listen events in the model caused by Apply button on that window. (I do not have experience with it but someone else can advise…)
https://ruby.sketchup.com/index.html
Yes, I would say the most probable observer that would fire would be an EntityObserver
object.
If the dynamic component is one that creates copies, then also the EntitiesObserver#onElementAdded
callback may fire several times.
But, this is a difficult one because if there are more than 1 instance of this dynamic component, then the DC extension will clone the definition that you are watching and use the new definition to create the differences in it’s Entities
collection.
So you may also need to have a DefinitionsObserver#onComponentAdded
callback watching the model’s DefinitionList
collection.
But if the only thing that changed was a material assignment, then the MaterialsObserver#onMaterialRefChange
callback would likely fire twice. (Once for the material whose use decremented -1, and once for the material that gained a use.)
Also, … in testing I’ve verified that if you attach an EntityObserver
object to both the DC instance’s and definition’s "dynamic_attributes"
dictionary, then when changing editable attributes, the observer’s onChangeEntity
callback will fire for both.
This is possible because the Sketchup::AttributeDictionary
class is an Sketchup::Entity
subclass.
Test example:
class Spy
def onChangeEntity(entity)
puts "onChangeEntity: #{entity.inspect}"
end
end
# With a Dynamic Component instance selected ...
# (I inserted a dynamic picket Fence component instance,
# from the "Dynamic Components Training" collection.)
dc = Sketchup.active_model.selection[0]
#=> #<Sketchup::ComponentInstance:0x000001ab3ff327d0>
# Attaching an observer instance object to the DC component instance:
dc.add_observer(Spy.new)
#=> true
# Attaching an observer instance object to the instance's DC dictionary:
dc.attribute_dictionary("dynamic_attributes").add_observer(Spy.new)
#=> true
# Attaching an observer instance object to the definition's DC dictionary:
dc.definition.attribute_dictionary("dynamic_attributes").add_observer(Spy.new)
#=> true
Then changing the picket spacing value in the “Component Options” dialog
from 5"
to 6"
, the following observer callback output is seen …
#=> 0.023109 seconds to redraw
#=> onChangeEntity: #<Sketchup::ComponentInstance:0x000001ab3ff327d0>
#=> onChangeEntity: #<Sketchup::AttributeDictionary:0x000001ab402112d0>
#=> onChangeEntity: #<Sketchup::AttributeDictionary:0x000001ab3ff32370>
The first change event for dictionaries is for the instance’s attribute dictionary, the
second is for the dynamic component definition’s attribute dictionary.
You will also get change events firing when you use the Scale Tool on a dynamic instance.