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

Selection to component

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 5 Posters 3.2k 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.
  • P Offline
    Pixero
    last edited by 28 Mar 2018, 10:36

    I'm trying to group selected entitys and then convert it into a component to avoid SketchUp's component creation window.
    I have somehow made a wrong turn in my head and since this code doesn't give me an error in the console I'm not sure what I've messed up... 😳
    It's probably something embarassingly simple.

    ` mod = Sketchup.active_model
    ent = mod.entities

    	grp=[]
    	Sketchup.active_model.selection.each {|e|
    	  grp.push(e)
    	}		
    	grp=ent.add_group 
    	cmp=grp.to_component`
    
    1 Reply Last reply Reply Quote 0
    • S Offline
      slbaumgartner
      last edited by 28 Mar 2018, 12:36

      There are a couple of flaws in your code. First, you push Entities into the Array referenced by the variable grp and then immediately reassign grp to refer to your new Group. Second, you never added any content to the Group. The garbage collector in SketchUp Ruby is very aggressive about reaping empty Groups!

      1 Reply Last reply Reply Quote 0
      • R Offline
        rv1974
        last edited by 28 Mar 2018, 15:13

        TT already has (tt_groups2comps.rb) it for 100 years and it's perfect. I NEVER use the laggy standard one. Best works with shortcuts G for group, Shift-G for groups2comps
        http://sketchucation.com/forums/viewtopic.php?p=257578#p257578

        1 Reply Last reply Reply Quote 0
        • T Offline
          TIG Moderator
          last edited by 28 Mar 2018, 15:38

          Try this:

          mod = Sketchup.active_model
          sel = mod.selection
          ent = mod.active_entities
          grp = ent.add_group(sel.to_a)
          dfn = grp.to_component
          dfn.name = "Compo#1"
          
          

          If you don't force a definition name, then the component is confusingly named "Group#NN"
          You must use .active_entities, because if the current selection is not in a matching entities context you'll BugSplat !

          TIG

          1 Reply Last reply Reply Quote 0
          • P Offline
            Pixero
            last edited by 28 Mar 2018, 16:29

            Thanks TIG.
            That worked very well.

            A short question though.
            In the Entitys info window the Instance is now called Compo and the Definition is called Group#1.
            I thought the definition would be called Compo?

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 28 Mar 2018, 16:33

              My mistake !
              Try this...

              mod = Sketchup.active_model
              sel = mod.selection
              ent = mod.active_entities
              grp = ent.add_group(sel.to_a)
              ins = grp.to_component
              ins.definition.name = "Compo#1"
              

              The instance should remain 'un-named'...

              TIG

              1 Reply Last reply Reply Quote 0
              • P Offline
                Pixero
                last edited by 28 Mar 2018, 16:50

                Thanks again. This time it worked as it should.

                1 Reply Last reply Reply Quote 0
                • D Offline
                  db11
                  last edited by 30 Mar 2018, 18:16

                  Thanks to both of you for a very useful code snippet. Especially helpful when you're making a component from loose geometry and forgot to check "Replace selection with component", and have to go through the process twice. Always good to save clicks and annoyances.

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

                  Advertisement