I added a create command to make a non-seasonal component into a seasonal one.
Their are also now a debug switch. At the console switch on debugging:
SimJoubert::SeasonalComponents.debug= true
The name of the seasonal attribute key can be set at the top of the module via the constant: SEASON_ATTR
Here is the file to download …
seasonal_components.rb (3.6 KB)
… which looks like:
# encoding: UTF-8
require "sketchup.rb"
require "pp"
module SimJoubert
module SeasonalComponents
extend self
@@context_menu_set ||= false
@@debug ||= false
MENU_TEXT = {
:update => "Update season DC attibuts",
:create => "Create season DC attibuts"
}
DICT = "dynamic_attributes"
SEASON_ATTR = "season"
def command_create(selset)
return if selset.empty?
# Make the definitions of selected components
# or groups seasonal:
create_dc_date_attribut(selset)
end
def command_update(selset)
if selset.empty?
# update ALL seasonal components
update_dc_date_attribut(Sketchup::active_model.definitions)
else
# update ONLY selected seasonal components
update_dc_date_attribut(selset)
end
end
def debug
@@debug
end
def debug=(arg)
@@debug =( arg ? true : false )
end
def get_seasonal_dcs(collection=nil)
# Filter the referenced collection
if collection.nil?
dlist = Sketchup::active_model.definitions
dlist.reject! {|d| d.image? }
elsif collection.all? {|o| o.is_a?(Sketchup::ComponentDefinition) }
dlist = collection.reject {|d| d.image? }
else
insts = collection.grep(Sketchup::ComponentInstance)
groups = collection.grep(Sketchup::Group)
dlist = insts.map{|i| i.definition } + groups.map{|g| g.definition }
dlist.uniq!
end
dcs = dlist.select {|d| d.attribute_dictionary(DICT,false) }
# select only seasonal dynamic components
seasonal = dcs.select {|d|
d.attribute_dictionary(DICT,false).keys.include?(SEASON_ATTR)
}
if @@debug
puts "#{Module::nesting[0].name}: debug is true."
puts "The 'dlist' array:"
pp dlist
puts "The 'dcs' array:"
pp dcs
puts "The 'seasonal' array:"
pp seasonal
end
return seasonal
end
def create_dc_date_attribut(selected)
# 1. Retrieve data from shadowinfo into local variables,
model = Sketchup.active_model
shadowinfo = model.shadow_info
value = shadowinfo["DayOfYear"]
if @@debug
puts "The 'day of year' value:"
pp value
puts # blank line
end
# Writing the data into dictionary attributes.
selected.each {|obj|
next unless obj.respond_to?(:definition)
obj.definition.set_attribute(DICT,SEASON_ATTR,value)
}
end
def update_dc_date_attribut(collection)
seasonal = get_seasonal_dcs(collection)
# 1. Retrieve data from shadowinfo into local variables,
model = Sketchup.active_model
shadowinfo = model.shadow_info
# 2. Iterate the seasonal component definitions
value = shadowinfo["DayOfYear"]
if @@debug
puts "The 'day of year' value:"
pp value
puts # blank line
end
# writing the data into dictionary attributes.
seasonal.each {|d| d.set_attribute(DICT,SEASON_ATTR,value) }
end
# other code / methods here
unless file_loaded?(__FILE__) || @@context_menu_set
UI.add_context_menu_handler do |context_menu|
selset = Sketchup::active_model.selection
context_menu.add_item(MENU_TEXT[:update]) { command_update(selset) }
unless selset.empty?
context_menu.add_item(MENU_TEXT[:create]) { command_create(selset) }
end
end # context_menu_handler
@@context_menu_set = true
end
end # plugin module
end # outer namespace module