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

    Colored faces

    Scheduled Pinned Locked Moved Developers' Forum
    15 Posts 7 Posters 387 Views 7 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

      Change:

      faces=[]
      ss.each{|e|faces << e if e.class==Sketchup;;Face}
      
      

      To:

      faces = ss.grep(Sketchup;;Face)
      

      The grep method comes from the Enumerable library module, that is mixed into many collection classes.
      The grep method is faster than anyway you could write the same functionality in Ruby.


      Also you are repeating things in loops that only need be done ONCE.
      And using multiple loops when only ONE loop is needed.

      Change:

      faces.each{|face| face.material=Sketchup;;Color.new(0,250,154)}
      faces.each{|face| face.back_material=Sketchup;;Color.new(0,250,154)}
      faces.each{|face| face.material.alpha = 0.5}
      faces.each{|face| face.back_material.alpha = 0.5}
      
      

      To:

      matl = Sketchup;;Color.new(0,250,154)
      matl.alpha = 0.5
      
      faces.each{|face|
        face.back_material = face.material = matl
      }
      
      

      Last.. please use a 2 SPACE indent for code examples here.. TAB characters do not work well with bbCode boxes.

      πŸ’­

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • icehuliI Offline
        icehuli
        last edited by

        little benchmark: windows 8, sketchup 2013 maker, cpu 4900mq

        code:

        require 'benchmark'
        mod = Sketchup.active_model # Open model
        ent = mod.entities # All entities in model
        sel = mod.selection # Current selection
        arr = ent.to_a
        print "sel;",sel.to_a.size,"  ==  ent;",ent.to_a.size
        puts "",arr
        
        Benchmark.bm(20) do |x|
          x.report("ent.greb")         { 100000.times { faces =  ent.grep(Sketchup;;Edge) } }
          x.report("ent.select,is_a?") { 100000.times { faces =  ent.select{|e| e.is_a?  Sketchup;;Edge } } }
          x.report("ent.select,class") { 100000.times { faces =  ent.select{|e| e.class==Sketchup;;Edge } } }
          x.report("ent.each,is_a?")   { 100000.times { faces=[];ent.each{|e|faces << e if e.is_a?  Sketchup;;Edge} } }
          x.report("ent.each,class")   { 100000.times { faces=[];ent.each{|e|faces << e if e.class==Sketchup;;Edge} } }
          
          x.report("sel.greb")         { 100000.times { faces =  sel.grep(Sketchup;;Edge) } }
          x.report("sel.select,is_a?") { 100000.times { faces =  sel.select{|e| e.is_a?  Sketchup;;Edge } } }
          x.report("sel.select,class") { 100000.times { faces =  sel.select{|e| e.class==Sketchup;;Edge } } }
          x.report("sel.each,is_a?")   { 100000.times { faces=[];sel.each{|e|faces << e if e.is_a?  Sketchup;;Edge} } }
          x.report("sel.each,class")   { 100000.times { faces=[];sel.each{|e|faces << e if e.class==Sketchup;;Edge} } }
          
          x.report("arr.greb")         { 100000.times { faces =  arr.grep(Sketchup;;Edge) } }
          x.report("arr.select,is_a?") { 100000.times { faces =  arr.select{|e| e.is_a?  Sketchup;;Edge } } }
          x.report("arr.select,class") { 100000.times { faces =  arr.select{|e| e.class==Sketchup;;Edge } } }
          x.report("arr.each,is_a?")   { 100000.times { faces=[];arr.each{|e|faces << e if e.is_a?  Sketchup;;Edge} } }
          x.report("arr.each,class")   { 100000.times { faces=[];arr.each{|e|faces << e if e.class==Sketchup;;Edge} } }
        end
        
        

        result:

        
                                  user     system      total        real
        ent.greb              0.672000   0.000000   0.672000 (  0.681000)
        ent.select,is_a?      1.329000   0.000000   1.329000 (  1.328000)
        ent.select,class      1.437000   0.000000   1.437000 (  1.445000)
        ent.each,is_a?        1.266000   0.000000   1.266000 (  1.264000)
        ent.each,class        1.390000   0.000000   1.390000 (  1.391000)
        
        sel.greb              0.657000   0.000000   0.657000 (  0.655000)
        sel.select,is_a?      1.343000   0.000000   1.343000 (  1.330000)
        sel.select,class      1.469000   0.000000   1.469000 (  1.452000)
        sel.each,is_a?        1.250000   0.000000   1.250000 (  1.260000)
        sel.each,class        1.375000   0.000000   1.375000 (  1.386000)
        
        arr.greb              0.391000   0.000000   0.391000 (  0.401000)
        arr.select,is_a?      0.735000   0.000000   0.735000 (  0.740000)
        arr.select,class      0.860000   0.000000   0.860000 (  0.873000)
        arr.each,is_a?        0.953000   0.000000   0.953000 (  0.960000)
        arr.each,class        1.078000   0.000000   1.078000 (  1.090000)
        
        

        data:

        sel;28  ==  ent;28
        #<Sketchup;;Edge;0x18cf0054> 
        #<Sketchup;;Edge;0x18cf0018>
        #<Sketchup;;Edge;0x18cf0090>
        #<Sketchup;;Edge;0x18ceffdc>
        #<Sketchup;;Face;0x18cf00cc>
        #<Sketchup;;Group;0x18ce5e4c>
        #<Sketchup;;Edge;0x18eacf64>
        #<Sketchup;;Edge;0x18eacfdc>
        #<Sketchup;;Edge;0x18eacf28>
        #<Sketchup;;Edge;0x18eacfa0>
        #<Sketchup;;Face;0x18ead018>
        #<Sketchup;;Edge;0x18bd1254>
        #<Sketchup;;Edge;0x18bd11dc>
        #<Sketchup;;Edge;0x18bd1218>
        #<Sketchup;;Edge;0x18bd11a0>
        #<Sketchup;;Face;0x18bd1290>
        #<Sketchup;;Edge;0x18bd0bb0>
        #<Sketchup;;Edge;0x18bd0b74>
        #<Sketchup;;Edge;0x18bd0b38>
        #<Sketchup;;Edge;0x18bd0afc>
        #<Sketchup;;Face;0x18bd0bec>
        #<Sketchup;;Edge;0x18bcefb8>
        #<Sketchup;;Edge;0x18bcee8c>
        #<Sketchup;;Edge;0x18bcef7c>
        #<Sketchup;;Edge;0x18bceec8>
        #<Sketchup;;Edge;0x18bcef40> 
        #<Sketchup;;Edge;0x18bcef04>
        #<Sketchup;;Face;0x18bceff4>
        
        1 Reply Last reply Reply Quote 0
        • pingpinkP Offline
          pingpink
          last edited by

          I changed a code from your suggestion , but faces change color without transparency.
          What happens ? πŸ˜’

          def gl_green
          
              model=Sketchup.active_model
          
              ss=model.selection
          
              faces = ss.grep(Sketchup;;Face)
          
              matl = Sketchup;;Color.new(0,250,154)
          
              matl.alpha = 0.5
          
              faces.each{|face|
              face.back_material = face.material = matl
              }
          
          end
          
          1 Reply Last reply Reply Quote 0
          • jolranJ Offline
            jolran
            last edited by

            try changing matl = Sketchup::Color.new(0,250,154) to (0,250,154, 100)

            and

            faces.each{|face|
            	  face.back_material = face.material = matl
            	  face.material.alpha = 0.5
                }
            

            edit: Updated code for backface as you wanted. And the 4rth argument is actually not needed in th Color object..

            1 Reply Last reply Reply Quote 0
            • tt_suT Offline
              tt_su
              last edited by

              @jolran said:

              try changing matl = Sketchup::Color.new(0,250,154) to (0,250,154, 100)

              and

              faces.each{|face|
              > 	  face.back_material = face.material = matl
              > 	  face.material.alpha = 0.5
              >     }
              

              edit: Updated code for backface as you wanted. And the 4rth argument is actually not needed in th Color object..

              Setting alpha for face material inside the loop is not efficient. It'd be better set outside the loop.

              1 Reply Last reply Reply Quote 0
              • pingpinkP Offline
                pingpink
                last edited by

                Thank you so much !!! Love You All' πŸ˜›

                I changed as your suggestion , Jolran

                It can change colors of glass now. I appreciate it !

                There are many colors that I have to revise.

                I will also change others as your example.

                def gl_green
                
                	model=Sketchup.active_model
                	ss=model.selection
                
                	faces = ss.grep(Sketchup;;Face)
                
                	matl = Sketchup;;Color.new(0,250,154)
                
                    faces.each{|face|
                     face.back_material = face.material = matl
                     face.material.alpha = 0.5
                    }
                end
                

                Result

                1 Reply Last reply Reply Quote 0
                • TIGT Offline
                  TIG Moderator
                  last edited by

                      def gl_green
                  
                          model=Sketchup.active_model
                  
                          ss=model.selection
                  
                          faces = ss.grep(Sketchup;;Face)
                  
                          matl = Sketchup;;Color.new(0,250,154)
                  
                          matl.alpha = 0.5
                  
                          faces.each{|face|
                          face.back_material = face.material = matl
                          }
                  
                      end
                  

                  No πŸ˜’ You are jumbling up 'material' and 'color'.
                  Make your material then give it a color, alpha etc - then give faces that material...
                  More like this...

                  matl = model.materials.add('gl_green')
                  matl.color = Sketchup;;Color.new(0,250,154)
                  matl.alpha =0 .5
                  faces.each{|face|
                    face.material = matl
                    face.back_material = matl
                  }
                  

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • pingpinkP Offline
                    pingpink
                    last edited by

                    Hi , TIG

                    This is correct , right ? I test it and it gives a name of material !!

                    def gl_green
                    
                    	model=Sketchup.active_model
                    	ss=model.selection
                    
                    	faces = ss.grep(Sketchup;;Face)
                    
                    	matl = model.materials.add('gl_green')
                    	matl.color = Sketchup;;Color.new(0,250,154)
                    	matl.alpha =0.5
                    	faces.each{|face|
                    	face.material = matl
                    	face.back_material = matl
                    	}
                    end
                    
                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      He's actually using one of the standard color set.

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • TIGT Offline
                        TIG Moderator
                        last edited by

                        As Dan says you can create and set materials in several ways.
                        face.material='red'
                        works because ' red' is a standard 'color', and a material will be auto-created from that if it doesn't exist.
                        But
                        face.material='redex'
                        might fail because that isn't a standard color - that is unless it already exists in the model.materials, and it has that very name...

                        It's best to try and separate materials and colors in your thought process...
                        Make the new materials up front and then specify them by name after that...
                        ` mat=model.materials.add('redex')
                        mat.color='red'
                        mat.alpha=0.33

                        later

                        face.material=mat

                        or

                        face.material='redex'`
                        etc

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • pingpinkP Offline
                          pingpink
                          last edited by

                          Thank You again Dan and TIG ! . I understand an idea now. πŸ˜„

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

                          Advertisement