• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Color the DC conditioning on its attributes & values (MYSQL

Scheduled Pinned Locked Moved Developers' Forum
23 Posts 4 Posters 645 Views
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.
  • B Offline
    blue_orchid
    last edited by 10 Oct 2011, 18:29

    UI.menu("Plugins").add_item('Assessment') { Jeny.color }
    
    
    module Jeny
        def self.color
    require 'mysql'
    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")
    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|
             if e.get_attribute('dynamic_attributes','id','')=="2345"||(b=='2345') || (c=='1')
    then e.material = 'red'
    end
      }
     Sketchup.active_model.entities.to_a.each{|e|
              if e.get_attribute('dynamic_attributes','id','')=="2345"||(b=='2345') || (c=='2')
    then e.material = 'yellow'
    end
      }
     end
    end
    

    In the above code, what I did....
    color the DC
    which has attribute 'ID' = 2345
    b==2345 ( b=id from database)
    c=1(c=rating in database)
    with red color.

    And similarly Dc
    which has attribute 'ID' = 2345
    b==2345 ( b=id from database)
    c=2 (c=rating in database)
    with yellow color.

    Problem is that
    1.it accepts only the last code, so it changes the object to yellow color not to red color.
    .

    I even tried using 'case and when' control structure
    for example
    %(#4000FF)[Sketchup.active_model.entities.to_a.each{|e|
    case e.get_attribute('dynamic_attributes','id','')=="2345"
    when b=='2345' || c=='1'
    then e.material = 'green'
    end }]
    But it didn't work...

    Can anyone tell me what the appropriate control structure is?

    1 Reply Last reply Reply Quote 0
    • C Offline
      Chris Fullmer
      last edited by 10 Oct 2011, 19:41

      Hey Blue Orchid, I've moved this post to the "Developers Forum" (like I did you're previous one as well).

      You'll get much more response by the people who regularly check that forum, than the regular SketchUp forum.

      Looks like your making progress on your script. You need to write a tutorial on how to integrate MySQL into SU when you get a chance - even better would be MySQL-Lite (well, I think).

      Good luck,

      Chris

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

      1 Reply Last reply Reply Quote 0
      • J Offline
        jolran
        last edited by 10 Oct 2011, 19:53

        %(#FF0000)[Sketchup.active_model.entities.to_a.each{|e|
        if e.get_attribute]

        Don't know if there is some speciality in your script, but must not you define |e| ?

        Like: if e. is kind of? Sketchup::Someentity OR if e.class Sketchup::SomeentityAnd the put the arguments/conditionals..
        Just guessing.. Could be different in your case...

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 10 Oct 2011, 20:38

          @jolran said:

          ... but must not you define |e| ?

          Joel, That IS, in fact, the declaration of local variables to the block, within the " |" delimeters.

          The each method automatically passes the iterator into the block, referenced by the var defined ( e in this case.)

          @Jeny... outdenting makes your code very hard to read. All blocks should indented.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • D Offline
            Dan Rathbun
            last edited by 10 Oct 2011, 20:49

            @blue_orchid said:

            I even tried using 'case and when' control structure
            for example
            %(#4000FF)[Sketchup.active_model.entities.to_a.each{|e|
            case e.get_attribute('dynamic_attributes','id','')=="2345"
            when b=='2345' || c=='1'
            then e.material = 'green'
            end }]
            But it didn't work...

            Can anyone tell me what the appropriate control structure is?

            Your use of case is wrong.
            The case clause declares the "object" to use FOR comparison (on the right of a === operator,) but not the comparison(s) themselves.
            The while clauses declare the value that goes on the left of a === operator.

            More like:

            Sketchup.active_model.entities.to_a.each{|e|
              this = e.get_attribute('dynamic_attributes','id','')
              case this
              when '1','37','89'
                e.material = 'yellow'
              when '2345'
                e.material = 'green'
              when '' # empty
                e.material = 'orange'
              else
                e.material = 'blue'
              end
            }
            

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 10 Oct 2011, 21:09

              Jeny, the coloring should go inside your while block that fetches each record from the database.

              Also, you can speed things up by
              ents = Sketchup.active_model.entities.to_a
              and then use the ref ents until anything is added or deleted from the model.

              You can also narrow down ents to only elements that are DCs:

              ents.reject! {|e|
                 e.class!=Sketchup;;ComponentInstance ||
                 e.definition.attribute_dictionary('dynamic_attributes').nil?
              }
              

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • J Offline
                jolran
                last edited by 10 Oct 2011, 21:59

                Oh yeah that's true, Dan. 😳

                I missread the codeblock..

                1 Reply Last reply Reply Quote 0
                • B Offline
                  blue_orchid
                  last edited by 11 Oct 2011, 03:21

                  @dan rathbun said:

                  Jeny, the coloring should go inside your while block that fetches each record from the database.

                  Dan, can you show me how to use while block for coloring?

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    Dan Rathbun
                    last edited by 11 Oct 2011, 18:51

                    @blue_orchid said:

                    @dan rathbun said:

                    Jeny, the coloring should go inside your while block that fetches each record from the database.

                    Dan, can you show me how to use while block for coloring?

                    No. You would not learn anything if I did your work for you.. and I wouldn't get paid.

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • B Offline
                      blue_orchid
                      last edited by 12 Oct 2011, 01:34

                      while (c=='1'){
                      Sketchup.active_model.entities.to_a.each{|e|
                      e.get_attribute('dynamic_attributes','id','') == "2345"
                              e.material = 'green'
                          end}}
                      
                      

                      Dan, I know it's not the correct way to write the code as it failed. I tried.

                      1 Reply Last reply Reply Quote 0
                      • D Offline
                        Dan Rathbun
                        last edited by 12 Oct 2011, 02:29

                        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 12 Oct 2011, 18:29

                          Dan,

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

                          1 Reply Last reply Reply Quote 0
                          • C Offline
                            Chris Fullmer
                            last edited by 12 Oct 2011, 22:34

                            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
                            • D Offline
                              Dan Rathbun
                              last edited by 12 Oct 2011, 22:59

                              @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 12 Oct 2011, 23:01

                                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 12 Oct 2011, 23:17

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

                                  1 Reply Last reply Reply Quote 0
                                  • D Offline
                                    Dan Rathbun
                                    last edited by 12 Oct 2011, 23:23

                                    @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 13 Oct 2011, 00:44

                                      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
                                      • D Offline
                                        Dan Rathbun
                                        last edited by 13 Oct 2011, 00:57

                                        @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 13 Oct 2011, 01:11

                                          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
                                          • 1
                                          • 2
                                          • 1 / 2
                                          1 / 2
                                          • First post
                                            4/23
                                            Last post
                                          Buy SketchPlus
                                          Buy SUbD
                                          Buy WrapR
                                          Buy eBook
                                          Buy Modelur
                                          Buy Vertex Tools
                                          Buy SketchCuisine
                                          Buy FormFonts

                                          Advertisement