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

    Color the component according to dynamic component attribute

    Scheduled Pinned Locked Moved Newbie Forum
    sketchup
    5 Posts 2 Posters 1.4k Views 2 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.
    • B Offline
      blue_orchid
      last edited by

      I am new in ruby programming, and I was trying to execute following code in skectchup but it's not working.

      UI.menu("Plugins").add_item('paint Red') {
        model = Sketchup.active_model
        entities = model.entities.to_a
        attr = []
          if e.get_attribute('dynamic_attributes','condition','')=="1"
          attr << e
          end
        entities_def = entities.definition
        
        entities_def.set_attribute 'dynamic_attributes',
          'material', 'red'
        entities_def.set_attribute 'dynamic_attributes',
          '_material_formula', '"red"'
         
        dcs = $dc_observers.get_latest_class
        dcs.redraw_with_undo(entities)
      }
      

      What I was trying to do is -
      I have many components in sketchup model. All those components have dynamic attributes 'material' and 'condition'. I was trying to assign material color "red" to all the component with attribute condition = 1.

      I took help to write above code from following link:
      http://forums.sketchucation.com/viewtopic.php?f=180&t=24241&p=331242&hilit=select+object+as+per+attribute#p331242

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        This could be done using features which are already available in Dynamic Components.

        Try adding the built-in Material attribute, and use a formula similar to the following:

        Material: =if(condition = 1, "Red", "Default" )

        I realize there may be a good reason to use an extra Ruby script to accomplish this, but I thought I'd point this out since it's fairly simple to do.

        (The use of "Default" as a material name causes an error in the formula, but has the effect of removing any applied material. I don't see how to remove materials in a DC.)

        Hi

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

          I don't know how to execute
          @unknownuser said:

          Material: =if(condition = 1, "Red", "Default" )
          .

          I have edited the previous code and still is not working..

          UI.menu("Plugins").add_item('painting Red') {
            model = Sketchup.active_model
            entities = model.entities.to_a
            entities_def = entities.definition
            
            condition_1 = []
            entities.each{|e|    
              if e.get_attribute('dynamic_attributes','condition','')=="1"
              condition_1 << e
              end
              }
            entities_def.set_attribute 'dynamic_attributes',
              'material', 'red'
            entities_def.set_attribute 'dynamic_attributes',
              '_material_formula', '"red"'
             
            dcs = $dc_observers.get_latest_class
            dcs.redraw_with_undo(entities)
          
          }
          

          here shortcut references are:
          %(#FF0000)[model = Sketchup.active_model
          entities = model.entities.to_a
          entities_def = entities.definition]

          I tried to select all the components with dynamic reference 'condition =1" in following code:
          condition_1 = [
          entities.each{|e|
          if e.get_attribute('dynamic_attributes','condition','')=="1"
          condition_1 << e
          end
          }]

          And, for the above selection, I tried to change the material (of dynamic reference) into red color in following code:
          %(#FF0000)[entities_def.set_attribute 'dynamic_attributes',
          'material', 'red'
          entities_def.set_attribute 'dynamic_attributes',
          '_material_formula', '"red"']

          Actually, I don't know what the codes

          dcs = $dc_observers.get_latest_class
          dcs.redraw_with_undo(entities)
          

          means, but I copied that code form the base file I used from the following code:

          UI.menu("Plugins").add_item('Make Sang Red') {
          
            # Assumes that sang is the 1st entity in model.
            sang = Sketchup.active_model.entities[0]
            sang_def = sang.definition
          
            # Override sang's shirt color to red. ("material"
            # is a special attribute that requires
            # you to set a formula to "take control" 
            # over the default material the user has painted.)
            sang_def.set_attribute 'dynamic_attributes',
              'material', 'red'
            sang_def.set_attribute 'dynamic_attributes',
              '_material_formula', '"red"'
          
            # Add a new configurable option to Sang.
            # (Any attribute that starts with an underscore
            # is a "meta attribute" that describes behavior.)
            sang_def.set_attribute 'dynamic_attributes',
              'weight', '145'
            sang_def.set_attribute 'dynamic_attributes',
              '_weight_label', 'weight'
            sang_def.set_attribute 'dynamic_attributes',
              '_weight_formlabel', 'My Weight'
            sang_def.set_attribute 'dynamic_attributes',
              '_weight_units', 'STRING'
            sang_def.set_attribute 'dynamic_attributes',
              '_weight_access', 'TEXTBOX'
          
            # Change the description that shows
            # up in the configure box with a custom
            # formula.
            sang_def.set_attribute 'dynamic_attributes',
              '_description_formula', 
              '"Sang is now red and weighs " & weight'
          
            # There is a global handle into the plugin that
            # allows you to make the same calls that the
            # plugin does, like so...
            dcs = $dc_observers.get_latest_class
            dcs.redraw_with_undo(sang)
          
          }
          

          Thank you.

          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by

            I assumed you were usung SketchUp Pro. Try this:

            
            UI.menu("Plugins").add_item('painting Red') { BlueOrchid.paint_red }
            
            module BlueOrchid
                def self.paint_red
                    Sketchup.active_model.entities.to_a.each{|e|    
                        if e.get_attribute('dynamic_attributes','condition','')=="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)
                        end
                    }
                end
            end
            
            
            

            Hi

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

              thanks a lot. it worked. You saved my day...

              How do u guys learn ruby for sketchup? What are the basic requirements (computer knowledge) to start ruby?
              I wish i could learn efficiently and quickly.

              Thanks again.

              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