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

    Looking to make a script, but dunno how.

    Scheduled Pinned Locked Moved Developers' Forum
    34 Posts 7 Posters 1.3k 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.
    • C Offline
      cjthompson
      last edited by

      are you looking for a script to do what you ask for, or are you asking for a tutorial on how to do it?

      1 Reply Last reply Reply Quote 0
      • A Offline
        agamemnus
        last edited by

        Either. Anyway, here's what I have now. I already have a problem I can't figure out-- I can't get both IF statements to work in the second def. It will skip the second if statement as of now... dunno why.

        
        require 'sketchup.rb'
        
        module Convert_components_randomly
        
         def Convert_components_randomly.choosebase
          base_entities = Sketchup.active_model.selection
        
          if base_entities.empty?
           results = UI.messagebox "Please select base groups and/or components first."
           Sketchup.send_action "selectSelectionTool;"
           return
          end
        
         end
        
        
         def Convert_components_randomly.choosetarget
          target_entities = Sketchup.active_model.selection
        
          if target_entities.empty?
           results = UI.messagebox "Please select target groups and/or components first."
           Sketchup.send_action "selectSelectionTool;"
           return
          end
        
          if Convert_components_randomly.choosebase.base_entities.empty?
           results = UI.messagebox "Please select base groups and/or components first."
           Sketchup.send_action "selectSelectionTool;"
           return
          end
        
         end
        
        end #Convert_components_randomly module
        
        
        convert_components_randomly_menu = UI.menu("Plugins").add_submenu("Convert Components Randomly")
        convert_components_randomly_menu.add_item("Select base components") {Convert_components_randomly.choosebase}
        convert_components_randomly_menu.add_item("Select components to convert") {Convert_components_randomly.choosetarget}
        
        
        1 Reply Last reply Reply Quote 0
        • Chris FullmerC Offline
          Chris Fullmer
          last edited by

          The if statement is invalid:

          if Convert_components_randomly.choosebase.base_entities.empty?

          That is calling a method (which does not exist). It needs to looking at an array. So in your choosebase method, you should make an array that holds all the selected components.

          Then in this method, you can determine if that array is empty. You might need to make your component selection be a global variables. So change base_entities to $base_entities in the first method. Then change the if statement to:

          if $base_entities.empty?

          That might begin to get you somewhere. But I have a feeling you are going to run into some more problems with the selection set. base_entities is currently pointing at the selection object - it is not pointing to an array of objects that are selected. Which is different. So maybe change line 6 to read:

          $base_entities = Sketchup.active_model.selection.to_a

          Now your base_entities is an array of SketchUp entities. And hopefully now when you get to the "if" statement, it will recognize your if statement as a valid statement, and parse it correctly.

          But I have not tested any of this, so it might need tweaks still.

          Chris

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

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            global variable? I'd recommend an module variable instead.

            Thomas Thomassen β€” SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

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

              I never played with the @ and @@ variables inside of modules. Do they work as expected? Will his script remember the value over multiple script sessions?

              Chris

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

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                Use @ variable within a module and it's global within that module and it doesn't interfere with any other scripts.

                
                module MyModule
                  @module my_var = 'Hello World'
                
                  def self.foo
                    return @my_var
                  end
                end
                
                

                For classes - have a look at this: http://sporkmonger.com/2007/2/19/instance-variables-class-variables-and-inheritance-in-ruby

                Thomas Thomassen β€” SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • M Offline
                  MartinRinehart
                  last edited by

                  Agamemnus,

                  Having Chris Fullmer help you with Ruby programming is like having Roger Federer help you with your tennis. Beyond what money could buy.

                  Sorry about the off topic stuff, but here's some more:

                  @chris fullmer said:

                  oh Gaieus, you can do it! Its not too late to build some way cool Gaieus Toolset for SU πŸ˜„

                  Gaieus, Chris is so right! I'm working on my tutorial's Ruby half (yes, half!). See if you can read this script:

                  
                  # /t/stairs1.rb - a stairway in SketchUp Ruby Talk (aka SketchTalk)
                  
                  =begin
                  The following bit of SketchTalk builds a 36"-wide stairway. Rise is 7 inches; run is 9. Built from 1.5" thick lumber. It has 15 steps. It has a 1.5 inch square banister on the left. The banister is supported by 1x1 balusters.
                  =end
                  
                  require '/t/sketch_talk'
                  
                  n # my keyboard shortcut for File/New
                  
                  box [0,0,0], [36,1.5,0], 5.5 # riser
                  box [0,0,5.5], [36,10.5,5.5], 1.5 # tread
                  
                  box [1.25,1,7], [2.25,2,7], 38 # ballusters
                  box [1.25,5.5,7], [2.25,6.5,7], 38+(7.0/2)
                  
                  all # select all 
                  step = g 'step' # turn selection into a component named 'step'
                  
                  mc step, [0,9,7], 14 # Move/Copy, 14x
                  
                  box [1,0,36+7], [2.5,15*9,36+7+15*7], 1.5 # bannister
                  
                  

                  I'll bet you can read that script. I'll bet you can't build the model half that fast any other way!

                  Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                  1 Reply Last reply Reply Quote 0
                  • A Offline
                    agamemnus
                    last edited by

                    The first script is almost done. I have a few problems --

                    1. I could not find a way to convert groups to components so I changed them to unique components and then changed the definition of THOSE to the randomly selected base component. Isn't there a more efficient way?

                    2. I can't seem to convert the target groups to components fast enough -- I used 'if (current_entity.typename == "Group") // current_entity.to_component // end', but that only applies at the end of the script so I have to run it twice.. dunno how to make it convert instantly. Tried shoving "Sketchup.active_model.commit_operation" after and then converting the unique components to base components but it won't budge.

                    3. When I do "@target_entities = target_entities", and I already had a non-empty @target_entities, am I leaking?

                    4. (mini-gripe) The rand function is glitchy. Sometimes it gives several (3-4) repeats in a 90-size or so list, which has an incredibly low chance of occurring.

                    
                    require 'sketchup.rb'
                    
                    module Convert_objects_randomly
                    
                     @base_entities = []
                     @target_entities = []
                    
                     def Convert_objects_randomly.choosebase
                      @base_entities = Sketchup.active_model.selection.to_a
                      if @base_entities.empty?
                       results = UI.messagebox "Please select base groups and/or components first."
                       Sketchup.send_action "selectSelectionTool;"
                       return
                      end
                     end
                    
                    
                     def Convert_objects_randomly.choosetarget
                      target_entities = Sketchup.active_model.selection.to_a
                      if @base_entities.empty?
                       results = UI.messagebox "Please select base groups and/or components first."
                       Sketchup.send_action "selectSelectionTool;"
                       return
                      end
                    
                      if target_entities.empty?
                       if @target_entities.empty?
                       results = UI.messagebox "Please select target groups and/or components first."
                       Sketchup.send_action "selectSelectionTool;"
                       return
                       end
                       target_entities = @target_entities
                      end
                    
                      target_entities.each do |current_entity|
                       if (current_entity.typename == "Group")
                        current_entity.to_component
                       end
                      end
                    
                      Sketchup.active_model.commit_operation
                       
                      target_entities.each do |current_entity|
                       if (current_entity.typename == "Group") or (current_entity.typename == "ComponentInstance")
                        current_entity.make_unique
                        current_entity.definition = @base_entities[rand(@base_entities.length)].definition
                       end 
                      end
                    
                      @target_entities = target_entities
                      Sketchup.active_model.commit_operation
                    
                     end
                    
                    end #Convert_objects_randomly module
                    
                    
                    Convert_objects_randomly_menu = UI.menu("Plugins").add_submenu("Convert Objects Randomly")
                    Convert_objects_randomly_menu.add_item("Select base objects") {Convert_objects_randomly.choosebase}
                    Convert_objects_randomly_menu.add_item("Convert selected to random base objects") {Convert_objects_randomly.choosetarget}
                    
                    
                    file_loaded(__FILE__)
                    
                    
                    1 Reply Last reply Reply Quote 0
                    • A Offline
                      agamemnus
                      last edited by

                      Thanks. I couldn't do it with "@module base_entities = '' "-- that gave me an error. I used " @base_entities = '' ". And looks like I need that " @base_entities ='' " or else something weird happens (maybe undefineds everywhere?) and the if statement for base_entities did not work. (it does now, of course)

                      So now I have to figure out how to iterate through target_entities and convert each to a random base_entities ..

                      There isn't a native function I could use?

                      Edit: Figured it out mostly... See post 1 below.

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

                        #1 groups have the .to_component method that will turn them into a component. But it makes each group a unique component, because there is no built in way to determine if a group is an identical copy of another group. There is some work arounds that can be done on certain groups that are exact copies of another group, but generally speaking, groups can't be turned easily into a single component and instances.

                        #2 - I'll have to look at later

                        #3 - The @target_entities will hold its value until SketchUp is closed, after it is given a value once. So you should clear it after your script is run each time. Add a @taget_entities = [] to the end of your script. That is my gess as to what is happening.

                        #4 - Yeah, that happens. Adam Billyard once took some time to explain about how random functions work in programming. It was eye opening....or rather, my eyes popped out from all the terms he used that didn't make any sense to me. But essentially random is a tricky thing to nail perfectly I guess.

                        Sounds like its coming along well!

                        Chris

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

                        1 Reply Last reply Reply Quote 0
                        • A Offline
                          agamemnus
                          last edited by

                          @chris fullmer said:

                          #3 - The @target_entities will hold its value until SketchUp is closed, after it is given a value once. So you should clear it after your script is run each time. Add a @taget_entities = [] to the end of your script. That is my gess as to what is happening.

                          Well, I know that it holds its value -- that was a way to re-randomize if the user wanted to without selecting those same groups again. But, if I set @target_entities to something else and it (@target_entities) is already set to something, that won't cause a leak, right? (the array it was previously set to will be cleared from memory?)

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

                            Ahh, correct. If you set it to seomthing new, then it clears its old value.

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

                            1 Reply Last reply Reply Quote 0
                            • thomthomT Offline
                              thomthom
                              last edited by

                              Ruby uses garbage collection - so it keeps track of all the junk we throw about and sweeps it all away! πŸ˜„

                              Thomas Thomassen β€” SketchUp Monkey & Coding addict
                              List of my plugins and link to the CookieWare fund

                              1 Reply Last reply Reply Quote 0
                              • C Offline
                                cjthompson
                                last edited by

                                @agamemnus said:

                                1. I can't seem to convert the target groups to components fast enough -- I used 'if (current_entity.typename == "Group") // current_entity.to_component // end', but that only applies at the end of the script so I have to run it twice.. dunno how to make it convert instantly. Tried shoving "Sketchup.active_model.commit_operation" after and then converting the unique components to base components but it won't budge.

                                the reason why is because .to_component creates a new entity.
                                Try current_entity = current_entity.to_component.

                                1 Reply Last reply Reply Quote 0
                                • thomthomT Offline
                                  thomthom
                                  last edited by

                                  Also .typename is very slow.
                                  Instead of current_entity.typename == "Group" use current_entity.is_a?(Sketchup::Group).
                                  http://forums.sketchucation.com/viewtopic.php?f=180&t=19576&start=0#p162235

                                  Thomas Thomassen β€” SketchUp Monkey & Coding addict
                                  List of my plugins and link to the CookieWare fund

                                  1 Reply Last reply Reply Quote 0
                                  • A Offline
                                    agamemnus
                                    last edited by

                                    Thanks guys. Got myself two more issues now, though. 😞

                                    1. The code refuses to convert lines to groups!! (I have "current_entity = current_entity.add_group")
                                    2. Is there any way to make a yes/no dropdown, and if not how do I change the size (width) of the inputbox text?
                                    
                                    require 'sketchup.rb'
                                    
                                    module Convert_objects_randomly
                                    
                                     @base_entities = []
                                     @target_entities = []
                                     @convert_only_groupsinstances = true
                                    
                                     def Convert_objects_randomly.choosebase
                                      @base_entities = Sketchup.active_model.selection.to_a
                                      if @base_entities.empty?
                                       results = UI.messagebox "Please select base groups and/or components first."
                                       Sketchup.send_action "selectSelectionTool;"
                                       return
                                      end
                                     end
                                    
                                    
                                     def Convert_objects_randomly.choosetarget
                                      target_entities = Sketchup.active_model.selection.to_a
                                      if @base_entities.empty?
                                       results = UI.messagebox "Please select base groups and/or components first."
                                       Sketchup.send_action "selectSelectionTool;"
                                       return
                                      end
                                    
                                      if target_entities.empty?
                                       if @target_entities.empty?
                                       results = UI.messagebox "Please select target groups and/or components first."
                                       Sketchup.send_action "selectSelectionTool;"
                                       return
                                       end
                                       target_entities = @target_entities
                                      end
                                       
                                      target_entities.each do |current_entity|
                                       if current_entity.is_a?(Sketchup;;Group)
                                        current_entity = current_entity.to_component
                                        current_entity.definition = @base_entities[rand(@base_entities.length)].definition
                                       elsif current_entity.is_a?(Sketchup;;ComponentInstance)
                                        current_entity = current_entity.make_unique
                                        current_entity.definition = @base_entities[rand(@base_entities.length)].definition
                                       else
                                        current_entity = current_entity.add_group
                                        current_entity = current_entity.to_component
                                        current_entity.definition = @base_entities[rand(@base_entities.length)].definition
                                       end 
                                      end
                                    
                                      @target_entities = target_entities
                                      Sketchup.active_model.commit_operation
                                    
                                     end
                                    
                                     def Convert_objects_randomly.settings
                                      if (@convert_only_groupsinstances == true)
                                       default = "yes"
                                      else
                                       default = "no"
                                      end
                                      settings_query1 = inputbox(["Convert only groups/components to base objects?"],[default],"Convert Objects Randomly Settings")
                                      if (settings_query1[0] == "yes")
                                       @convert_only_groupsinstances = true
                                      else
                                       @convert_only_groupsinstances = false
                                      end
                                     end
                                    
                                    end #Convert_objects_randomly module
                                    
                                    
                                    Convert_objects_randomly_menu = UI.menu("Plugins").add_submenu("Convert Objects Randomly")
                                    Convert_objects_randomly_menu.add_item("Select base objects") {Convert_objects_randomly.choosebase}
                                    Convert_objects_randomly_menu.add_item("Convert selected to random base objects") {Convert_objects_randomly.choosetarget}
                                    Convert_objects_randomly_menu.add_item("Settings") {Convert_objects_randomly.settings}
                                    
                                    file_loaded(__FILE__)
                                    
                                    
                                    1 Reply Last reply Reply Quote 0
                                    • thomthomT Offline
                                      thomthom
                                      last edited by

                                      @agamemnus said:

                                      1. The code refuses to convert lines to groups!! (I have "current_entity = current_entity.add_group")

                                      Check the API on that method. It only belongs to Entities collections. Not single Entity
                                      http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/entities.html

                                      @agamemnus said:

                                      1. Is there any way to make a yes/no dropdown

                                      Check the second example in the manual:
                                      http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/ui.html#inputbox

                                      Thomas Thomassen β€” SketchUp Monkey & Coding addict
                                      List of my plugins and link to the CookieWare fund

                                      1 Reply Last reply Reply Quote 0
                                      • M Offline
                                        MartinRinehart
                                        last edited by

                                        @agamemnus said:

                                        Ok, thanks. How do I convert an entity to an entities collection?

                                        You don't. Use an entities collection returned by the entities method:

                                        
                                        Sketchup.active_model.entities
                                        myGroup.entities
                                        someComponent.entities
                                        
                                        

                                        Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                                        1 Reply Last reply Reply Quote 0
                                        • A Offline
                                          agamemnus
                                          last edited by

                                          @thomthom said:

                                          @agamemnus said:

                                          1. The code refuses to convert lines to groups!! (I have "current_entity = current_entity.add_group")

                                          Check the API on that method. It only belongs to Entities collections. Not single Entity
                                          http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/entities.html

                                          @agamemnus said:

                                          1. Is there any way to make a yes/no dropdown

                                          Check the second example in the manual:
                                          http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/ui.html#inputbox

                                          Ok, thanks. How do I convert an entity to an entities collection?

                                          Edit: The textbox width is still really long, even with the dropdown now...

                                          1 Reply Last reply Reply Quote 0
                                          • A Offline
                                            agamemnus
                                            last edited by

                                            @martinrinehart said:

                                            @agamemnus said:

                                            Ok, thanks. How do I convert an entity to an entities collection?

                                            You don't. Use an entities collection returned by the entities method:

                                            
                                            > Sketchup.active_model.entities
                                            > myGroup.entities
                                            > someComponent.entities
                                            > 
                                            

                                            But I can't... it's not a group or component. It's a line.... tried "current_entity = current_entity.entities", no go.

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

                                            Advertisement