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

    [REQ] Export selected groups/comps to separate skp files

    Scheduled Pinned Locked Moved Plugins
    14 Posts 6 Posters 1.7k Views 6 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.
    • srxS Offline
      srx
      last edited by

      You have the option from Component browser/In model Components - Save to local colection which saves all components from the model to SU components library

      www.saurus.rs

      1 Reply Last reply Reply Quote 0
      • R Offline
        rv1974
        last edited by

        It doesn't preserve location

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

          The attached code should do something like you want...
          Note the following:
          You must have saved the model before running this.
          You must have at least a group and/or a component-instance preselected before running this.
          Individual groups are exported using their given-name, or if unnamed as 'Group#N' where N is the count.
          Individual instances are exported using their definition-name, with a '#N' count suffix added if there's more than one instance of that definition.
          These exported SKPs are put into the same folder as the model itself.
          It reports the files it has exported as it goes.
          The exported file's thumbnails should be accurate.
          It removes any backup files made during the process, and on completion it reverts to the original model's contents.
          All temporary deletions etc are 'aborted', so that although the exported files get made successfully, the original model should remain unchanged.
          To use the code simply copy all of it and paste it into the Ruby Console and press <enter>.
          Note the preselection requirement !
          If it works as you hope, then I can make it into a RB file, with a menu item etc for you...

          mo=Sketchup.active_model
          pa=mo.path
          if pa.empty?
            UI.messagebox("Save 'Untitled' Model Before Exporting...")
          else
            d=File.dirname(pa).tr("\\", "/")
            es=mo.active_entities.to_a
            ss=mo.selection.to_a
            mo.selection.clear
            gs=ss.grep(Sketchup;;Group)
            gis=gs.collect{|e| e.persistent_id }
            is=ss.grep(Sketchup;;ComponentInstance)
            iis=is.collect{|e| e.persistent_id }
            unless gis[0] || iis[0]
              UI.messagebox("Select Groups and/or Instances Before Exporting...")
            else
              gis.each_with_index{|ii, i|
                e=nil
                mo.active_entities.to_a.each{|ee|
                  if ee.persistent_id==ii
                    e=ee
                    break
                  end
                }
                next unless e
                mo.start_operation("Export_group#{i}",true)
                n=e.name
                n="Group##{i+1}" if n.empty?
                p f=File.join(d, n+".skp")
                xs=es=mo.active_entities.to_a-[e]
                xs.each{|x|
                  x.erase! if x.valid?
                }
                mo.save(f)
                if File.exist?(File.join(d, n+".skb")) #PC
                  File.delete(File.join(d, n+".skb"))
                end
                if File.exist?(File.join(d, n+"~.skp")) #MAC
                  File.delete(File.join(d, n+"~.skp"))
                end
                mo.abort_operation #revert model
              }
              iis.each_with_index{|ii, i|
                e=nil
                mo.active_entities.to_a.each{|ee|
                  if ee.persistent_id==ii
                    e=ee
                    break
                  end
                }
                next unless e
                mo.start_operation("Export_instance#{i}",true)
                n=e.definition.name
                n="#{n}##{i+1}" if e.definition.instances[1]
                p f=File.join(d, n+".skp")
                xs=es=mo.active_entities.to_a-[e]
                xs.each{|x|
                  x.erase! if x.valid?
                }
                mo.save(f)
                if File.exist?(File.join(d, n+".skb")) #PC
                  File.delete(File.join(d, n+".skb"))
                end
                if File.exist?(File.join(d, n+"~.skp")) #MAC
                  File.delete(File.join(d, n+"~.skp"))
                end
                mo.abort_operation # revert model
              }
              mo.save(pa) # revert back to orignal model
            end
          end
          puts "Done."
          
          

          TIG

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

            Thank you very much TIG! But i'm getting an error message...

            Error; #<NoMethodError; undefined method `persistent_id' for #<Sketchup;;ComponentInstance;0x0000002558bb80>>
            <main>;12;in `block in <main>'
            <main>;12;in `collect'
            <main>;12;in `<main>'
            SketchUp;1;in `eval'
            
            

            I tried with components and groups.

            1 Reply Last reply Reply Quote 0
            • N Offline
              numerobis
              last edited by

              @srx This saves ALL components of a model.

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

                I just noticed that the SketchUp version you have is <2016.
                Therefore "Persistent IDs" cannot be used - these are needed to circumvent the lost IDs during newer SketchUp processes...

                Try this alternative [which will fail with >=2016 ! but should still work in older versions ?? ]

                mo=Sketchup.active_model
                pa=mo.path
                if pa.empty?
                  UI.messagebox("Save 'Untitled' Model Before Exporting...")
                else
                  d=File.dirname(pa).tr("\\", "/")
                  es=mo.active_entities.to_a
                  ss=mo.selection.to_a
                  mo.selection.clear
                  gs=ss.grep(Sketchup;;Group)
                  gis=gs.collect{|e| e.guid }
                  is=ss.grep(Sketchup;;ComponentInstance)
                  iis=is.collect{|e| e.guid }
                  unless gis[0] || iis[0]
                    UI.messagebox("Select Groups and/or Instances Before Exporting...")
                  else
                    gis.each_with_index{|ii, i|
                      e=nil
                      mo.active_entities.to_a.each{|ee|
                        if ee.guid==ii
                          e=ee
                          break
                        end
                      }
                      next unless e
                      mo.start_operation("Export_group#{i}",true)
                      n=e.name
                      n="Group##{i+1}" if n.empty?
                      p f=File.join(d, n+".skp")
                      xs=es=mo.active_entities.to_a-[e]
                      xs.each{|x|
                        x.erase! if x.valid?
                      }
                      mo.save(f)
                      if File.exist?(File.join(d, n+".skb")) #PC
                        File.delete(File.join(d, n+".skb"))
                      end
                      if File.exist?(File.join(d, n+"~.skp")) #MAC
                        File.delete(File.join(d, n+"~.skp"))
                      end
                      mo.abort_operation #revert model
                    }
                    iis.each_with_index{|ii, i|
                      e=nil
                      mo.active_entities.to_a.each{|ee|
                        if ee.guid==ii
                          e=ee
                          break
                        end
                      }
                      next unless e
                      mo.start_operation("Export_instance#{i}",true)
                      n=e.definition.name
                      n="#{n}##{i+1}" if e.definition.instances[1]
                      p f=File.join(d, n+".skp")
                      xs=es=mo.active_entities.to_a-[e]
                      xs.each{|x|
                        x.erase! if x.valid?
                      }
                      mo.save(f)
                      if File.exist?(File.join(d, n+".skb")) #PC
                        File.delete(File.join(d, n+".skb"))
                      end
                      if File.exist?(File.join(d, n+"~.skp")) #MAC
                        File.delete(File.join(d, n+"~.skp"))
                      end
                      mo.abort_operation # revert model
                    }
                    mo.save(pa) # revert back to orignal model
                  end
                end
                puts "Done."
                
                

                TIG

                1 Reply Last reply Reply Quote 0
                • N Offline
                  numerobis
                  last edited by

                  @tig said:

                  I just noticed that the SketchUp version you have is <2016.
                  Therefore "Persistent IDs" cannot be used - these are needed to circumvent the lost IDs during newer SketchUp processes...

                  Oh sorry, i wasn't aware of that.

                  Thanks for the update. It's working great now! Very cool. 😄
                  Could you put it into the right click menu to the other export options?

                  Btw. is it possible to configure the right click menu? The list is getting very long and there are some entries that i never use. I somehow remember that this wasn't possible and you had to change it in the plugin code. If this is the case, is there a single command that can be commented out to deactivate it without generating problems or is it more complex?

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    MartinGr @TIG
                    last edited by

                    @TIG Error: #<SyntaxError: <main>:4: syntax error, unexpected else' <main>:15: syntax error, unexpected else'
                    else
                    ^~~~
                    <main>:23: syntax error, unexpected '}', expecting end-of-input
                    }
                    ^

                    SketchUp:in `eval'

                    It keeps showing this error in su 2021

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

                      @MartinGr
                      I can't see where that error is arising.
                      Unfortunately any old 'ruby' formatted text is FUBAR on the new forum - Gábor is working on a fix:
                      Here's a rewritten version that works for me...

                      mo=Sketchup.active_model
                      pa=mo.path
                      if pa.empty?
                        UI.messagebox("Save 'Untitled' Model Before Exporting...")
                      else
                        d=File.dirname(pa).tr("\\", "/")
                        es=mo.active_entities.to_a
                        ss=mo.selection.to_a
                        mo.selection.clear
                        gs=ss.grep(Sketchup::Group)
                        gis=gs.collect{|e| e.persistent_id }
                        is=ss.grep(Sketchup::ComponentInstance)
                        iis=is.collect{|e| e.persistent_id }
                        unless(gis[0] || iis[0])
                          UI.messagebox("Select Groups and/or Instances Before Exporting...")
                        else
                          gis.each_with_index{|ii, i|
                            e=nil
                            mo.active_entities.to_a.each{|ee|
                              if ee.persistent_id==ii
                                e=ee
                                break
                              end
                            }
                            next unless e
                            mo.start_operation("Export_group#{i}",true)
                            n=e.name
                            n="Group##{i+1}" if n.empty?
                            p f=File.join(d, n+".skp")
                            xs=es=mo.active_entities.to_a-[e]
                            xs.each{|x|
                              x.erase! if x.valid?
                            }
                            mo.save(f)
                            if File.exist?(File.join(d, n+".skb")) #PC
                              File.delete(File.join(d, n+".skb"))
                            end
                            if File.exist?(File.join(d, n+"~.skp")) #MAC
                              File.delete(File.join(d, n+"~.skp"))
                            end
                            mo.abort_operation #revert model
                          }
                          iis.each_with_index{|ii, i|
                            e=nil
                            mo.active_entities.to_a.each{|ee|
                              if ee.persistent_id==ii
                                e=ee
                                break
                              end
                            }
                            next unless e
                            mo.start_operation("Export_instance#{i}",true)
                            n=e.definition.name
                            n="#{n}##{i+1}" if e.definition.instances[1]
                            p f=File.join(d, n+".skp")
                            xs=es=mo.active_entities.to_a-[e]
                            xs.each{|x|
                              x.erase! if x.valid?
                            }
                            mo.save(f)
                            if File.exist?(File.join(d, n+".skb")) #PC
                              File.delete(File.join(d, n+".skb"))
                            end
                            if File.exist?(File.join(d, n+"~.skp")) #MAC
                              File.delete(File.join(d, n+"~.skp"))
                            end
                            mo.abort_operation # revert model
                          }
                          mo.save(pa) # revert back to original model
                        end
                      end
                      puts "Done."
                      

                      TIG

                      Didier BurD 1 Reply Last reply Reply Quote 0
                      • Didier BurD Offline
                        Didier Bur @TIG
                        last edited by

                        @MartinGr :

                        This one will perhaps be useful to you:
                        [https://sketchucation.com/plugin/2667-componentsplus]
                        Regards,

                        DB

                        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