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

    Beginner Ruby Problmes

    Scheduled Pinned Locked Moved Developers' Forum
    13 Posts 3 Posters 1.5k Views 3 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.
    • A Offline
      Arkman
      last edited by

      I'm trying to create a ruby that when I create a group it just gives me a prompt to name the group. Just like it does for a component. I'm really struggling.

      Any help would be greatly appreciated.

      Thanks!

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

        
        #-----------------------------------------------------------------------------
        require 'sketchup.rb'
        #-----------------------------------------------------------------------------
        
        module AM_Named_Group
        	
        	unless file_loaded?('am_named_group.rb')
        		# Add menu items.
        		plugins_menu = UI.menu('Edit')
        		plugins_menu.add_item('Make Named Group')	{ self.named_group }
        	end
        	
        	def self.named_group
        		model = Sketchup.active_model
        		sel = model.selection
        
        		# Don't do anything if the selection is empty.
        		return if sel.empty?
        
        		# Ask user for group name.
        		prompts = ['Group Name; ']
        		defaults = [model.definitions.unique_name('Group#1')]
        		input = UI.inputbox(prompts, defaults, 'New Group')
        
        		# Check if the user cancelled.
        		return if input == false
        
        		# The add_group method can be buggy when you pass on entities to it's argument. But there's
        		# no real options.
        		group = model.active_entities.add_group(sel)
        		name = model.definitions.unique_name(input[0])
        		# We set the name we will see in the Entities window.
        		group.name = name
        		# We set the definition name as well. If you convert the group to a component, this is the
        		# name it will get.
        		group.entities.parent.name = name
        		
        		# Select the new group
        		sel.clear
        		sel.add(group)
        	end
        end
        
        #-----------------------------------------------------------------------------
        file_loaded('am_named_group.rb')
        #-----------------------------------------------------------------------------
        
        

        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
          Arkman
          last edited by

          Awesome! I have one more request. How could I get a list of layers in the model to define what layer it lives in. I can get a list but I can't get it to show the layers in the model or place that newly created group on the layer.

          Thanks!!!!

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

            @arkman said:

            Awesome! I have one more request. How could I get a list of layers in the model to define what layer it lives in. I can get a list but I can't get it to show the layers in the model or place that newly created group on the layer.

            Thanks!!!!

            You can get a list? But you can't get it to show the layers in the model...
            I'm not quite following you. What list are you "getting"? You mean a list in the inputbox?

            All the layers are under Skethcup.active_model.layers
            http://code.google.com/apis/sketchup/docs/ourdoc/layers.html

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

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

              
              #-----------------------------------------------------------------------------
              require 'sketchup.rb'
              #-----------------------------------------------------------------------------
              
              module AM_Named_Group
              	
              	unless file_loaded?('am_named_group.rb')
              		# Add menu items.
              		plugins_menu = UI.menu('Edit')
              		plugins_menu.add_item('Make Named Group')	{ self.named_group }
              	end
              	
              	def self.named_group
              		model = Sketchup.active_model
              		sel = model.selection
              
              		# Don't do anything if the selection is empty.
              		return if sel.empty?
              
              		# Build an array of the layer names.
              		layers = model.layers.to_a.collect { |l| l.name }
              		# Ask user for group name.
              		list = ['', layers.join('|')]
              		prompts = ['Group Name; ', 'Layer; ']
              		defaults = [model.definitions.unique_name('Group#1'), model.active_layer.name]
              		input = UI.inputbox(prompts, defaults, list, 'New Group')
              
              		# Check if the user cancelled.
              		return if input == false
              		
              		# Get data from result array into variables
              		group_name, group_layer = input
              		
              		# Make it into one Undoable action
              		model.start_operation('Make Named Group')
              		
              		# The add_group method can be buggy when you pass on entities to it's argument. But there's
              		# no real options.
              		group = model.active_entities.add_group(sel)
              		name = model.definitions.unique_name(group_name)
              		# We set the name we will see in the Entities window.
              		group.name = name
              		# We set the definition name as well. If you convert the group to a component, this is the
              		# name it will get.
              		group.entities.parent.name = name
              		# Set group layer
              		group.layer = model.layers[group_layer]
              		
              		# Select the new group
              		sel.clear
              		sel.add(group)
              		
              		model.commit_operation
              	end
              end
              
              #-----------------------------------------------------------------------------
              file_loaded('am_named_group.rb')
              #-----------------------------------------------------------------------------
              
              

              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
                Arkman
                last edited by

                Ok, sorry that was unclear.

                I can add a list to the inputbox but I cant get that list to show the layers in the model. I can make a list with just random terms that I picked. Like below.

                list = ["item1|item2"]

                So I guess my question is "How do I get a list in the inputbox that shows all the layers in the model?" I found that help file you showed me but I dont know how to use that. I tried:

                list = [Sketchup.active_model.layers]

                That didn't work. I'm not really sure how to get the list to use that Skethcup.active_model.layers

                Once the list displays the layers in the model, how do I make the object go onto the layer selected in the inputbox?

                Thanks for all your help. Sorry for all the questions.

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

                  @arkman said:

                  list = [Skethcup.active_model.layers]

                  That didn't work. I'm not really sure how to get the list to use that Skethcup.active_model.layers

                  That's because Skethcup.active_model.layers is a collection of Layer objects. You need to collect all the layer names from each object. You see an example in the code I posted.

                  (And Skethcup was mis-spelled)

                  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
                    Arkman
                    last edited by

                    Incredible! Thank you so much. You just made my life a lot easier!

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

                      Maybe I should post it as a downloadable plugin?

                      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
                        Arkman
                        last edited by

                        That would probably be extremely helpful. I know a lot of people that could use something like this.

                        Thanks again!!

                        Josh

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

                          Posted it in a separate plugin for easy searching: http://forums.sketchucation.com/viewtopic.php?f=180&t=23582

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

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

                            Ththomthom I am Just starting out learning Ruby I made a plugin to create a simple rectangle based on the tutorial and would like to insert the name group module you posted could you give me some pointers?

                            https://www.youtube.com/user/ecabinetstips

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

                              There are some sticky thread in the Developer section where links to various best-practices is posted.

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

                              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