#------------------------------------------------------------------------------------------------ # Permission to use, copy, modify, and distribute this software for # any purpose and without fee is hereby granted. #------------------------------------------------------------------------------------------------ # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, # WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #------------------------------------------------------------------------------------------------ # THIS PLUGIN IS ANOTHER FIGMENT OF MY IMAGENATION AND IS NOT BASED ON OR COMPLY WITH ANYTHING! #------------------------------------------------------------------------------------------------ # IT WAS CREATED AND ONLY TESTED IN A WINDOWS ENVIRONMENT AND MAY NOT FUNCTION ON A MAC. #------------------------------------------------------------------------------------------------ # Name: Select by Attribute # By: sdmitch # Usage: Plugins>SDM Tools>???? Tool>Plugin Name # Note: # # Date: May 2015 #------------------------------------------------------------------------------------------------ require 'Sketchup' # ------------------ MENU SETUP ---------------------- # unless $sdm_tools_menu $sdm_tools_menu = UI.menu("Plugins").add_submenu("SDM Tools") $sdm_Edge_tools = $sdm_tools_menu.add_submenu("Edge Tool") $sdm_Face_tools = $sdm_tools_menu.add_submenu("Face Tool") $sdm_CorG_tools = $sdm_tools_menu.add_submenu("CorG Tool") $sdm_Misc_tools = $sdm_tools_menu.add_submenu("Misc Tool") end unless file_loaded?(__FILE__) $sdm_Misc_tools.add_item("Select by Attribute") { SDM::Select_By_Attribute.main } file_loaded(__FILE__) end # ------------------------------------------------------ # module SDM module Select_By_Attribute def self.main mod = Sketchup.active_model ent = mod.active_entities sel = mod.selection self.webdialog() end def self.webdialog() @dlg=UI::WebDialog.new("Select By Attribute", false,"WDID",300,200,10,10,true) html = <<-HTML
HTML @dlg.set_html(html) RUBY_PLATFORM =~ /(darwin)/ ? @dlg.show_modal() : @dlg.show(); @dlg.add_action_callback("InitializeForm") { self.get_attributes;puts "Initializing Form" @dlg.execute_script("clearList('dic');");@dlg.execute_script("addToList('dic',#{@dict_names});"); @dlg.execute_script("clearList('key');");@dlg.execute_script("addToList('key',#{@dict_keys});"); @dlg.execute_script("clearList('val');");@dlg.execute_script("addToList('val',#{@key_values});"); }; @dlg.add_action_callback("ValueChanged") {|d,p| var,val=p.strip.split("="); puts p case var when 'dic' then @name=val when 'key' then @key=val when 'val' then @value=val when 'btn' then select_by_attribute end }; end def self.get_attributes model=Sketchup.active_model ents=model.active_entities @dict_names=[]; @dict_keys=[]; @key_values=[] for e in ents next unless e.attribute_dictionaries for ad in e.attribute_dictionaries @dict_names << ad.name @dict_keys << e.attribute_dictionary(ad.name).keys @key_values << e.attribute_dictionary(ad.name).values end end @dict_names.flatten!; @dict_names.uniq!; @name = @dict_names[0] @dict_keys.flatten!; @dict_keys.uniq!; @key = @dict_keys[0] @key_values.flatten!; @key_values.uniq!; @value = @key_values[0] end def self.select_by_attribute mod = Sketchup.active_model ent = mod.entities sel = mod.selection sel.clear for e in ent next unless e.attribute_dictionaries names = e.attribute_dictionaries.map{|ad| ad.name} if names.include?(@name) if e.attribute_dictionary(@name).keys.include?(@key) if e.attribute_dictionary(@name).values.include?(@value) sel.add e end end end end end end end