sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Color the DC conditioning on its attributes & values (MYSQL

    Scheduled Pinned Locked Moved Developers' Forum
    23 Posts 4 Posters 645 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by

      Try something like this:

      #
      # put requires outside the module
      require('sketchup.rb')
      require('mysql')
      
      module Jeny
      
        def self.colorize
        
          colors = ['black','red','yellow','green']
          
          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,1),('Substructure',2349,2),('Superstructure',2353,3)")
          printf( "%d rows were inserted\n",dbh.affected_rows ) 
           
          res = dbh.query("SELECT id, rating FROM inspection")
          
          ents = Sketchup.active_model.entities.to_a
          # remove non-DCs
          ents.reject! {|e|
             e.class != Sketchup;;ComponentInstance ||
             e.definition.attribute_dictionary('dynamic_attributes').nil?
          }
      
          ents.each{|e|
            eid = e.get_attribute('dynamic_attributes','id','')
            unless eid.empty?
              found = nil
              found = # lookup row in dB with field id == eid
              if found
                e.material = colors[ found[2] ]
              end
            end #unless
          }
      
          # close dB connection !!
          
        end #def
        
      end # module Jeny
      
      unless file_loaded?(File.basename(__FILE__))
        UI.menu("Plugins").add_item('Assessment') { Jeny.colorize }
        file_loaded(File.basename(__FILE__))
      end
      
      

      and read this:
      Ruby Newbie's Guide to Getting Started

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • B Offline
        blue_orchid
        last edited by

        Dan,

        I used the script that you have written.
        No color change in the components.

        1 Reply Last reply Reply Quote 0
        • Chris FullmerC Offline
          Chris Fullmer
          last edited by

          Are the components already colored? Or are the internal faces colored already?

          Lately you've been tan, suspicious for the winter.
          All my Plugins I've written

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            @blue_orchid said:

            Dan, I used the script that you have written.
            No color change in the components.

            Did you finish line 32 ??
            and replace line 39 ?

            I am not a SQL person, so I cannot test it for you.
            http://rubydoc.info/gems/mysql/2.8.1/frames

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • B Offline
              blue_orchid
              last edited by

              Chris,

              components are colored with ruby script. I did not the paint the component with any color in the sketchup itself.

              I hope i answered your question.

              I also tried the same script in the components which don't have any color. But it didn't work.

              1 Reply Last reply Reply Quote 0
              • B Offline
                blue_orchid
                last edited by

                Dan, I am sorry that I did not understand your question?

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  @dan rathbun said:

                  @blue_orchid said:

                  Dan, I used the script that you have written.
                  No color change in the components.

                  Did you finish line 32 ??
                  and replace line 39 ?

                  replace line 39 with:
                  dbh.close()

                  line 32 is NOT complete.
                  YOU need to finish it so it looks up a record in res

                  It's a example, I have no way to test it myself.

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • B Offline
                    blue_orchid
                    last edited by

                    UI.menu("Plugins").add_item('Assessmenteww') { Jeny.color }

                    module Jeny
                    def self.color
                    require 'mysql'
                    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 id, rating FROM inspection")
                    #while row = res.fetch_row do

                    #a=row[0]
                    #b=row[1]
                    #c=row[2]

                    #printf "%s, %s, %s\n", a, b, c
                    #end

                    #puts a.inspect
                    #puts b.inspect
                    #puts c.inspect

                    Sketchup.active_model.entities.to_a.each{|e|
                    this = e.get_attribute('dynamic_attributes','id','') == "2345"
                    case this
                    when res == '2345' && '1'
                    e.material = 'red'
                    when res == '2349' && '2'
                    e.material == 'green'
                    when res == '3'
                    e.material == 'blue'
                    when '' # empty
                    e.material = 'orange'
                    else
                    e.material = 'yellow'
                    end
                    }
                    end
                    end

                    
                    

                    %(#40BF00)[Sketchup.active_model.entities.to_a.each{|e|
                    this = e.get_attribute('dynamic_attributes','id','') == "2345"
                    case this
                    when res == '2345' && '1'
                    e.material = 'red'
                    when res == '2349' && '2'
                    e.material == 'green'
                    when res == '3'
                    e.material == 'blue'
                    when '' # empty
                    e.material = 'orange'
                    else
                    e.material = 'yellow'
                    end
                    }]

                    When I run this script, the component with id '2345' does not change its color, but the other component changes its color to red. Why is this so?

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      @blue_orchid said:

                      When I run this script, the component with id '2345' does not change its color, but the other component changes its color to red. Why is this so?

                      You still do not understand how to use the case statement, your better off using a if .. elsif .. else .. end block.

                      And.. once again your coloring is after the iteration of the dB (outside the while block,) so your only going to be acting upon the LAST record from the dB.

                      How experienced are you at:

                      Ruby?

                      SQL databases?

                      Experience using any other programming languages?

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • B Offline
                        blue_orchid
                        last edited by

                        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.

                        i am just doing it for a project and i am really having a hard time doing it.

                        1 Reply Last reply Reply Quote 0
                        • Dan RathbunD Offline
                          Dan Rathbun
                          last edited by

                          @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

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • B Offline
                            blue_orchid
                            last edited by

                            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?

                            1 Reply Last reply Reply Quote 0
                            • B Offline
                              blue_orchid
                              last edited by

                              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.

                              1 Reply Last reply Reply Quote 0
                              • 1
                              • 2
                              • 2 / 2
                              • First post
                                Last post
                              Buy SketchPlus
                              Buy SUbD
                              Buy WrapR
                              Buy eBook
                              Buy Modelur
                              Buy Vertex Tools
                              Buy SketchCuisine
                              Buy FormFonts

                              Advertisement