[color=#BF4040]UI.menu("Plugins").add_item('Condition Rating1') { Jeny.paint }
module Jeny
def self.paint
require 'mysql'
dbh = Mysql.real_connect("localhost", "root", "***", "test",3306)
dbh.query("drop table if exists condition_rating")
dbh.query("create table condition_rating(component varchar(20), rating int(1))")
dbh.query("insert into condition_rating values('Deck',1),('Superstructure',2), ('Substructure',3)")
#printf "%d rows were inserted\n",dbh.affected_rows
res = dbh.query("SELECT * FROM condition_rating where rating = '3'")
while row = res.fetch_row do
#dbh.query("SELECT * FROM condition_rating")
#printf "%s, %s\n", row[0], row[1]
a=row[0]
b=row[1]
printf "%s, %s\n",row[0],b
end
Sketchup.active_model.entities.to_a.each{|e|
if b=='1'
e.definition.set_attribute 'dynamic_attributes', 'material', 'red'
e.definition.set_attribute 'dynamic_attributes', '_material_formula', '"red"'
$dc_observers.get_latest_class.redraw_with_undo(e)
elsif b=='2'
e.definition.set_attribute 'dynamic_attributes', 'material', 'green'
e.definition.set_attribute 'dynamic_attributes', '_material_formula', '"green"'
$dc_observers.get_latest_class.redraw_with_undo(e)
elsif b=='3'
e.definition.set_attribute 'dynamic_attributes', 'material', 'blue'
e.definition.set_attribute 'dynamic_attributes', '_material_formula', '"blue"'
$dc_observers.get_latest_class.redraw_with_undo(e)
elsif b=='4'
e.definition.set_attribute 'dynamic_attributes', 'material', 'yellow'
e.definition.set_attribute 'dynamic_attributes', '_material_formula', '"yellow"'
$dc_observers.get_latest_class.redraw_with_undo(e)
elsif b==5
e.definition.set_attribute 'dynamic_attributes', 'material', 'cyan'
e.definition.set_attribute 'dynamic_attributes', '_material_formula', '"cyan"'
$dc_observers.get_latest_class.redraw_with_undo(e)
elsif b==6
e.definition.set_attribute 'dynamic_attributes', 'material', 'darkorange'
e.definition.set_attribute 'dynamic_attributes', '_material_formula', '"darkorange"'
$dc_observers.get_latest_class.redraw_with_undo(e)
elsif b==7
e.definition.set_attribute 'dynamic_attributes', 'material', 'lightgreen'
e.definition.set_attribute 'dynamic_attributes', '_material_formula', '"lightgreen"'
$dc_observers.get_latest_class.redraw_with_undo(e)
end
}
end
end[/color]
What I did in the script
- created the MySQl table and its data using ruby script
Table contains these two columns which are Component and Rating
Values of table:
a.Component:Deck, Rating: 1
b.Component: Superstructure, Rating:2
c.Component: Substructure, Rating:3
I created the ruby saying that if rating is 1, then color that object.
GoogleSketchup:
I created model which have dynamic components like deck, superstructure and substructure with an attribute 'rating'. This attribute for each component has values from 1-7.
My problem: I am unable to connect the dynamic components of sketchup model to the 'component' of MySQL table.