sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ›£οΈ Road Profile Builder | Generate roads, curbs and pavements easily Download

    [REQUEST] Convert single instance components to groups

    Scheduled Pinned Locked Moved Plugins
    5 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.
    • J Offline
      jshal
      last edited by

      I was looking for quite while searching for the following problem:

      Importing (Revit) DWG data into Sketchup creates a lot of single instance components. The trouble is that when copying multiple of these files together that Sketchup slows down to a crawl because it is trying to match components that are similar. Usually I end up with sketchup files with hundreds of single component instances. Sketchup works faster when these single instances are in fact groups, making copying between sketchup files way faster. Normally I explode the selected instance and group these again, but with hundreds of these, this is too cumbersome.

      So what I need is the following:

      1. select all single instance components within my current SketchUp file (or current selection)
      2. convert these to standard groups

      I think this is quite useful for anybody working with imported files and want to keep on working quick.

      For number 2) I found the following code snippet from sdmitch (https://sketchucation.com/forums/viewtopic.php?f=323&t=17590&p=140679&hilit=single+instance+component#p623006)

      
      @mod = Sketchup.active_model
      @ent = @mod.active_entities
      @sel = @mod.selection
      @vue = @mod.active_view
      cmps = @sel.grep(Sketchup;;ComponentInstance)
      cmps.each{|ci|
       grp = @ent.add_group(ci)
       grp.name = ci.definition.name
       ci.explode
      }
      @mod.definitions.purge_unused
      
      

      [highlight=#ffffbf:39rhwwt6]Does anybody know how to realize part 1?[/highlight:39rhwwt6]

      Thanks in advance!

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

        It's relatively straightforward...
        Here's 1 AND 2 combined...
        This code finds all components that have just one instance and makes a new group in place of the instance, containing its contents, using the same name/layer/material as the instance itself.
        The now unused definitions are deleted.
        It is one step undo-able

        model=Sketchup.active_model
        defns=model.definitions
        model.start_operation("ci2gp",true)
        defns.each{|defn|
          next if defn.group?
          next if defn.image?
          next unless defn.instances.length==1
          inst=defn.instances[0]
          name=defn.name
          layr=inst.layer
          matl=inst.material
          ents=inst.parent.entities
          tran=inst.transformation
          grp=ents.add_group()
          gents=grp.entities
          i=gents.add_instance(defn, tran)
          inst.erase!
          i.explode
          grp.name=name
          grp.layer=layr
          grp.material=matl
        }
        defns.purge_unused
        model.commit_operation
        
        

        TIG

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

          Wow, TIG, you have saved my day(s) πŸ˜„ Big thanks!

          1 Reply Last reply Reply Quote 0
          • I Offline
            iMacThere4iAm
            last edited by

            @tig said:

            It's relatively straightforward...

            Sorry to dig up this old topic but your code above causes a bugsplat in Sketchup 2022. Any idea what could be the cause?

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

              It's a snippet of old code but it should work OK.
              It works for me on a PC - I don't have a suitable MAC to hand...
              Nothing different in v2022 as far as I can see...

              What exactly are you trying to do, are there any Ruby Console messages before the failure ?

              TIG

              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