Color the DC conditioning on its attributes & values (MYSQL
-
@blue_orchid said:
i am not at all experienced in any programming language and mysql. Ruby is my first programming language. I have gone through several ruby guide, but still i am having hard time learning it and working on it.
Yes it shows. You will need to study and re-read the tutorials again and again, until it sinks into your brain.
You should read the 'Pick-Axe' book cover to cover.
http://www.phrogz.net/ProgrammingRuby/frameset.html -
UI.menu("Plugins").add_item('Assesment') { Jeny.color } require 'mysql' module Jeny def self.color dbh = Mysql.real_connect("localhost", "root", "orange", "test",3306) dbh.query("drop table if exists inspection") dbh.query("create table inspection(component varchar(20), ID int(4), rating int(1))") dbh.query("insert into inspection values('deck',2345,1),('Substructure',2349,2),('Superstructure',2353,3)") #printf "%d rows were inserted\n",dbh.affected_rows #res = dbh.query("SELECT rating, id FROM inspection") res = dbh.query("SELECT rating FROM inspection where id = '2345'") while row = res.fetch_row do xx = row[0] printf "%s\n", xx end res1 = dbh.query("SELECT rating FROM inspection where id = '2349'") while row = res1.fetch_row do xy = row[0] printf "%s\n", xy end res2 = dbh.query("SELECT rating FROM inspection where id = '2353'") while row = res2.fetch_row do yy = row[0] printf "%s\n", yy end Sketchup.active_model.entities.to_a.each{|e| if e.get_attribute('dynamic_attributes','id','2345') e.set_attribute('dynamic_attributes','rating','xx') end } Sketchup.active_model.entities.to_a.each{|e| if e.get_attribute('dynamic_attributes','id','')== "2349" e.set_attribute('dynamic_attributes','rating','xy') end } Sketchup.active_model.entities.to_a.each{|e| if e.get_attribute('dynamic_attributes','id','') == "2353" e.set_attribute('dynamic_attributes','rating','yy') end } Sketchup.active_model.entities.to_a.each{|e| if e.get_attribute('dynamic_attributes','id','2345') && xx == "1" e.set_attribute( 'dynamic_attributes', 'material', 'red') e.set_attribute( 'dynamic_attributes', '_material_formula', '"red"') $dc_observers.get_latest_class.redraw_with_undo(e) elsif e.get_attribute('dynamic_attributes','id','2345') && xx == "2" e.set_attribute( 'dynamic_attributes', 'material', 'blue') e.set_attribute( 'dynamic_attributes', '_material_formula', '"blue"') $dc_observers.get_latest_class.redraw_with_undo(e) elsif e.get_attribute('dynamic_attributes','id','2349') && xx == "3" e.set_attribute( 'dynamic_attributes', 'material', 'green') e.set_attribute( 'dynamic_attributes', '_material_formula', '"green"') $dc_observers.get_latest_class.redraw_with_undo(e) end } Sketchup.active_model.entities.to_a.each{|e| if e.get_attribute('dynamic_attributes','id','2349') && xy == "1" e.set_attribute( 'dynamic_attributes', 'material', 'red') e.set_attribute( 'dynamic_attributes', '_material_formula', '"red"') $dc_observers.get_latest_class.redraw_with_undo(e) elsif e.get_attribute('dynamic_attributes','id','2349') && xy == "2" e.set_attribute( 'dynamic_attributes', 'material', 'blue') e.set_attribute( 'dynamic_attributes', '_material_formula', '"blue"') $dc_observers.get_latest_class.redraw_with_undo(e) elsif e.get_attribute('dynamic_attributes','id','2349') && xy == "3" e.set_attribute( 'dynamic_attributes', 'material', 'green') e.set_attribute( 'dynamic_attributes', '_material_formula', '"green"') $dc_observers.get_latest_class.redraw_with_undo(e) end } Sketchup.active_model.entities.to_a.each{|e| if e.get_attribute('dynamic_attributes','id','2353') && yy == "1" e.set_attribute( 'dynamic_attributes', 'material', 'red') e.set_attribute( 'dynamic_attributes', '_material_formula', '"red"') $dc_observers.get_latest_class.redraw_with_undo(e) elsif e.get_attribute('dynamic_attributes','id','2353') && yy == "2" e.set_attribute( 'dynamic_attributes', 'material', 'blue') e.set_attribute( 'dynamic_attributes', '_material_formula', '"blue"') $dc_observers.get_latest_class.redraw_with_undo(e) elsif e.get_attribute('dynamic_attributes','id','2353') && yy == "3" e.set_attribute( 'dynamic_attributes', 'material', 'green') e.set_attribute( 'dynamic_attributes', '_material_formula', '"green"') $dc_observers.get_latest_class.redraw_with_undo(e) end } end end
%(#BF4000)[What I did here:
I set attribute 'rating' for different objects with different names like xx, xy and yy.
Then I made these attributes 'ratings' equal to result from query for each component/object.
Finally i selected the color for each component and different values of ratings a color.But it still selects only last code i.e. objects color change to green . May be it could not identify the Db values.]
Now what i want to do that if the rating for a component is 1 in database, then the attribute 'rating' for that component is changed to 1 using DB value. Is it possible? -
This code finally worked
UI.menu("Plugins").add_item('Assesment') { Inspection.color } require 'mysql' module Inspection def self.color dbh = Mysql.real_connect("localhost", "root", "***", "test",3306) dbh.query("drop table if exists inspection") dbh.query("create table inspection(component varchar(20), ID int(4), rating int(1))") dbh.query("insert into inspection values('deck',2345,2),('Substructure',2349,1),('Superstructure',2353,3)") res = dbh.query("SELECT rating FROM inspection where id = '2345'") while row = res.fetch_row do xx = row[0] printf "%s\n", xx end res1 = dbh.query("SELECT rating FROM inspection where id = '2349'") while row = res1.fetch_row do xy = row[0] printf "%s\n", xy end res2 = dbh.query("SELECT rating FROM inspection where id = '2353'") while row = res2.fetch_row do yy = row[0] printf "%s\n", yy end Sketchup.active_model.entities.to_a.each{|e| if e.get_attribute('dynamic_attributes','id','')=='2345'&& xx=='1' e.set_attribute('dynamic_attributes','rating','1') elsif e.get_attribute('dynamic_attributes','id','')=='2345'&& xx=='2' e.set_attribute('dynamic_attributes','rating','2') elsif e.get_attribute('dynamic_attributes','id','')=='2345'&& xx=='3' e.set_attribute('dynamic_attributes','rating','3') end } Sketchup.active_model.entities.to_a.each{|e| if e.get_attribute('dynamic_attributes','id','')=='2349'&& xy=='1' e.set_attribute('dynamic_attributes','rating','1') elsif e.get_attribute('dynamic_attributes','id','')=='2349'&& xy=='2' e.set_attribute('dynamic_attributes','rating','2') elsif e.get_attribute('dynamic_attributes','id','')=='2349'&& xy=='3' e.set_attribute('dynamic_attributes','rating','3') end } Sketchup.active_model.entities.to_a.each{|e| if e.get_attribute('dynamic_attributes','id','')=='2353'&& yy=='1' e.set_attribute('dynamic_attributes','rating','1') elsif e.get_attribute('dynamic_attributes','id','')=='2353'&& yy=='2' e.set_attribute('dynamic_attributes','rating','2') elsif e.get_attribute('dynamic_attributes','id','')=='2353'&& yy=='3' e.set_attribute('dynamic_attributes','rating','3') end } Sketchup.active_model.entities.to_a.each{|e| if e.get_attribute('dynamic_attributes','rating','')=='1' e.set_attribute( 'dynamic_attributes', 'material', 'black') e.set_attribute( 'dynamic_attributes', '_material_formula', '"black"') $dc_observers.get_latest_class.redraw_with_undo(e) elsif e.get_attribute('dynamic_attributes','rating','')=='2' e.set_attribute( 'dynamic_attributes', 'material', 'blue') e.set_attribute( 'dynamic_attributes', '_material_formula', '"blue"') $dc_observers.get_latest_class.redraw_with_undo(e) elsif e.get_attribute('dynamic_attributes','rating','')=='3' e.set_attribute( 'dynamic_attributes', 'material', 'red') e.set_attribute( 'dynamic_attributes', '_material_formula', '"red"') $dc_observers.get_latest_class.redraw_with_undo(e) end } end end
Thanks to all.
Advertisement