Yes, You can manipulate the materials of a newly placed component from within the EntitiesObserver, onElementAdded callback. But modifying the component’s face material in this manner breaks some of the Dynamic Component functionality. Tested on SU 2015 and SU 2017. Not tested to work with the Undo/Redo stack
I hope this helps you.
module TNT_ClickCuisineTrial
class MyEntitiesObserver < Sketchup::EntitiesObserver
# this the PATTERN to only modify your 01-CABINET
TNT_COMPONENT_PATTERNS = [/01-CABINET/]
# Custom entities observer
def onElementAdded(entities, entity)
#Identify the entity that is being added, exit if it isn't one of yours
return unless entity.is_a?(Sketchup::ComponentInstance)
return unless TNT_COMPONENT_PATTERNS.any?{ |pattern| entity.definition.name =~ pattern }
puts "Coloring: #{entity.definition.name}"
#start a model operation
entity.model.start_operation('Color Faces', true)
meubels_verven(entity)
entity.model.commit_operation
end
#Patterns of the subcomponents to be painted or not painted
PAINT_COMPONENT_PATTERNS = [/B-PLINTH/, /B-FACADES/, /WORKTOP/]
DONT_PAINT_COMPONENT_PATTERNS = [/POIGNEE/]
# Paint the faces of the cabinet's sub-components
def meubels_verven(entity, paint_subcomponent = false)
entity.definition.entities.grep(Sketchup::ComponentInstance).each do |instance|
next unless (paint_subcomponent) || PAINT_COMPONENT_PATTERNS.any?{ |pattern| instance.definition.name =~ pattern }
next if DONT_PAINT_COMPONENT_PATTERNS.any?{ |pattern| instance.definition.name =~ pattern }
puts "Subcomponent name: #{instance.definition.name}"
#if parent has a material then copy the material to subcomponent
if entity.material
instance.material = entity.material
end
# if the instance has a material then copy it to the faces
if instance.material
instance.definition.entities.grep(Sketchup::Face).each { |f|
#f.material = material ## the test material
f.material = instance.material
}
end
#recursively call the method with paint_subcomponent = true
meubels_verven(instance, true)
end
end
end
#### Add entities observer for ALL entities
Sketchup.active_model.entities.add_observer(MyEntitiesObserver.new)
... the remainder of your file here
I’ve uploaded the new logic.rb file here.
logic.rb (19.5 KB)