sketchucation logo sketchucation
    • Login
    โ„น๏ธ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [Plugin] Components to Grid

    Scheduled Pinned Locked Moved Plugins
    22 Posts 7 Posters 23.4k 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.
    • TIGT Offline
      TIG Moderator
      last edited by

      There is a built-in way BUT if you want it coded then use an iteration of
      https://developers.google.com/sketchup/docs/ourdoc/componentdefinition#save_as

      This one-liner, copy/pasted into the Ruby Console + <enter> will export all definitions in the model into the model's folder - named after the definition+.skp

      m=Sketchup.active_model;p=File.dirname(m.path).tr("\\","/");puts"\n\nExporting Components to\n\n"+p+"\n\n";ds=m.definitions;ns=[];ds.to_a.find_all{|d|next if d.group?||d.image?;ns<<d.name};ns.sort.each{|n|puts s=n+".skp";ds[n].save_as(File.join(p,s))};puts"\nDone."
      

      If you want groups or images exported adjust the 'trap' next if...

      TIG

      1 Reply Last reply Reply Quote 0
      • CadFatherC Offline
        CadFather
        last edited by

        TIG, you do it on purpose! ๐Ÿ˜†

        1 Reply Last reply Reply Quote 0
        • CadFatherC Offline
          CadFather
          last edited by

          while i'm at it i might ask another question (for which there surely there isn't an old easy answer..)

          any way to distribute ALL the components present in the component browser 'in model' - to an imaginary grid in model space?

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

            I already recently did something like this in another thread, where the OP wanted to import SKPs and then show the components laid out.
            What you could do with a model's components in code is to iterate the list of definition names 'ns' [as all set/got in my earlier example] and then place an instance of each on a special layer [it makes later deletions easier], thus:

            m=Sketchup.active_model;m.start_operation("ALL!");ds=m.definitions;ns=[];ds.to_a.find_all{|d|next if d.group?||d.image?;ns<<d.name};ns.sort!; l=m.layers.add("ALL!");puts"\nLaying out ALL Components...\n\n"; es=m.active_entities; p=Geom;;Point3d.new(0,0,0);x=1.m;y=1.m;r=(Math.sqrt(ns.length)+0.5).to_i;c=0; ns.length.times{|i|puts ns[i]; tr=Geom;;Transformation.new(p); n=es.add_instance(ds[ns[i]],tr);n.layer=l;c+=1;if c>=r;p.x=0;p.y+=y;c=0;else;p.x+=x;end};puts"\nDone.";m.commit_operation
            

            The x/y set for a 1m 'square' grid starting from [0,0,0] - with any odd ones on the final row - so adjust this as desired; these new instances are put onto a layer name 'ALL!'; this one-liner is one-step undoable...

            TIG

            1 Reply Last reply Reply Quote 0
            • CadFatherC Offline
              CadFather
              last edited by

              wow...thank you TIG - just tried it and it's fab! ๐Ÿ˜„ ๐Ÿ‘

              tried to make it into a command but no success and yet i'm sure i wrapped it up properly (just in case others find it useful)

              compogrid.rb

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

                You miss called the module/method... try it this way

                require 'sketchup.rb'
                module CADfather
                  unless file_loaded?(File.basename(__FILE__))
                    UI.menu('Tools').add_item('Components to Grid'){self.compogrid()}
                  end
                  file_loaded(File.basename(__FILE__))
                  def self.compogrid()
                    m=Sketchup.active_model;m.start_operation("ALL!");ds=m.definitions;ns=[]; ds.to_a.find_all{|d|next if d.group?||d.image?;ns<<d.name};ns.sort!; l=m.layers.add("ALL!");puts"\nLaying out ALL Components...\n\n"; es=m.active_entities; p=Geom;;Point3d.new(0,0,0);x=2.m;y=2.m;r=(Math.sqrt(ns.length)+0.5).to_i;c=0; ns.length.times{|i|puts ns[i]; tr=Geom;;Transformation.new(p); n=es.add_instance(ds[ns[i]],tr);n.layer=l;c+=1;if c>=r;p.x=0;p.y+=y;c=0;else;p.x+=x;end};puts"\nDone.";m.commit_operation
                  end #def
                end #module
                

                ... OR something similar ... ๐Ÿ˜‰
                EDIT: typo fixed !

                TIG

                1 Reply Last reply Reply Quote 0
                • CadFatherC Offline
                  CadFather
                  last edited by

                  thanks TIG - works great.

                  i think there's a small typo here:

                  %(#008000)[def self.compogrid()
                  m=Sketchup.active_model;m.start_operation("ALL!");ds=m.definitions;ns=[];]s.to_a.find_all{|d|next if d.group?||

                  should read ds

                  what's interesting is how fast it is even with lots of components ๐Ÿ‘ ๐Ÿ‘

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

                    You are right - I have now changed the original to ' ds'...

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • CadFatherC Offline
                      CadFather
                      last edited by

                      Hi TIG - is it possible to have all the components placed along the red axis? i tried modifying the script but i always get the components on top of each other.. ๐Ÿ˜’

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

                        This [simpler] version spaces the component-instances 2m apart along the red/x axis [no stacking into a 'square']

                        require 'sketchup.rb'
                        module CADfather
                              unless file_loaded?(File.basename(__FILE__))
                                UI.menu('Tools').add_item('Components to Grid'){self.compogrid()}
                              end
                              file_loaded(File.basename(__FILE__))
                              def self.compogrid()
                                m=Sketchup.active_model;m.start_operation("ALL!"); ds=m.definitions; ns=[]; ds.to_a.find_all{|d|next if d.group?||d.image?; ns<<d.name}; ns.sort!; l=m.layers.add("ALL!");puts"\nLaying out ALL Components...\n\n"; es=m.active_entities; p=Geom;;Point3d.new(0,0,0); x=2.m; ns.length.times{|i|puts ns[i]; tr=Geom;;Transformation.new(p); n=es.add_instance(ds[ns[i]],tr);n.layer=l; p.x+=x};puts"\nDone.";m.commit_operation
                              end #def
                        end #module
                        

                        If the Ruby Console is open it reports what it's doing...
                        Change the x=2.m for a different spacing in red/x if desired...

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • CadFatherC Offline
                          CadFather
                          last edited by

                          Thanks TIG - much appreciated - i'll try integrating it into the grid plugin.. ๐Ÿ‘ ๐Ÿ˜„

                          1 Reply Last reply Reply Quote 0
                          • gullfoG Offline
                            gullfo
                            last edited by

                            TIG script examples - an interesting side effect - if you have a component made of other components it pulls it apart and distributes them... in this example i have a DC component of an acoustic window which has frames and seals as sub-components. in a way, this could be handy for creating a "how to assemble" guide because it pulls it apart in order of nesting...

                            otoh this may not be a desired effect for the script in the end...


                            Untitled.jpg

                            Glenn

                            http://www.runnel.com

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

                              The OP asked for all components to be displayed on a grid.
                              The code is very adaptable.
                              It ignores Groups and Images although it could as easily display those as well, or instead.
                              This version displays just components that have instances that are either in the model's entities or inside a non-component-entities context [i.e. inside a group that is not itself inside a component definition]

                              require 'sketchup.rb'
                                  module CADfather
                                        unless file_loaded?(File.basename(__FILE__))
                                          UI.menu('Tools').add_item('Components to Grid'){self.compogrid_used()}
                                        end
                                        file_loaded(File.basename(__FILE__))
                                        def self.compogrid_used()
                                          m=Sketchup.active_model
                                          m.start_operation("ALL!")
                                          ds=m.definitions
                                          ns=[]
                                          def self.recurse_parent(c)
                                            begin
                                              return c.parent
                                            rescue
                                              return nil
                                            end
                                          end
                                          ds.to_a.find_all{|d|
                                            next unless d.instances[0]
                                            next if d.group?
                                            next if d.image?
                                            skip=true
                                            d.instances.each{|i|
                                              if i.parent==m
                                                skip=false
                                                break
                                              elsif i.parent.group?
                                                i.parent.instances.each{|g|
                                                  c=g
                                                  while c 
                                                    c=self.recurse_parent(c)
                                                    if m==c
                                                      skip=false
                                                      c=nil
                                                      break
                                                    end
                                                  end
                                                }
                                              end
                                            }
                                            next if skip
                                            ns<<d.name
                                          }
                                          ns.sort!;
                                          l=m.layers.add("ALL!")
                                          puts"\nLaying out ALL Components...\n\n"
                                          es=m.active_entities
                                          p=Geom;;Point3d.new(0,0,0);
                                          x=2.m
                                          ns.length.times{|i|
                                            puts ns[i]
                                            tr=Geom;;Transformation.new(p)
                                            n=es.add_instance(ds[ns[i]],tr)
                                            n.layer=l
                                            p.x+=x}
                                          puts"\nDone."
                                          m.commit_operation
                                        end #def
                                  end #module
                              

                              TIG

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

                                TIG Thanks

                                [color=#FF40FF:20ov4k9m]T[/color:20ov4k9m][color=#FF40F9:20ov4k9m]h[/color:20ov4k9m][color=#FF40F2:20ov4k9m]a[/color:20ov4k9m][color=#FF40EC:20ov4k9m]t[/color:20ov4k9m][color=#FF40E5:20ov4k9m]'[/color:20ov4k9m][color=#FF40DF:20ov4k9m]s[/color:20ov4k9m] [color=#FF40D2:20ov4k9m]m[/color:20ov4k9m][color=#FF40CC:20ov4k9m]y[/color:20ov4k9m] [color=#FF40BF:20ov4k9m]s[/color:20ov4k9m][color=#F940B9:20ov4k9m]t[/color:20ov4k9m][color=#F240B2:20ov4k9m]o[/color:20ov4k9m][color=#EC40AC:20ov4k9m]r[/color:20ov4k9m][color=#E540A6:20ov4k9m]y[/color:20ov4k9m][color=#DF40A0:20ov4k9m],[/color:20ov4k9m] [color=#D24093:20ov4k9m]a[/color:20ov4k9m][color=#CC408D:20ov4k9m]n[/color:20ov4k9m][color=#C54086:20ov4k9m]d[/color:20ov4k9m] [color=#C53A7A:20ov4k9m]I[/color:20ov4k9m][color=#CC3373:20ov4k9m]'[/color:20ov4k9m][color=#D22D6D:20ov4k9m]m[/color:20ov4k9m] [color=#DF2060:20ov4k9m]s[/color:20ov4k9m][color=#E51A5A:20ov4k9m]t[/color:20ov4k9m][color=#EC1353:20ov4k9m]i[/color:20ov4k9m][color=#F20D4D:20ov4k9m]c[/color:20ov4k9m][color=#F90646:20ov4k9m]k[/color:20ov4k9m][color=#FF0040:20ov4k9m]i[/color:20ov4k9m][color=#F9004D:20ov4k9m]n[/color:20ov4k9m][color=#F20059:20ov4k9m]g[/color:20ov4k9m] [color=#E50073:20ov4k9m]t[/color:20ov4k9m][color=#DF0080:20ov4k9m]o[/color:20ov4k9m] [color=#D20099:20ov4k9m]i[/color:20ov4k9m][color=#CC00A6:20ov4k9m]t[/color:20ov4k9m][color=#C500B2:20ov4k9m]![/color:20ov4k9m]

                                1 Reply Last reply Reply Quote 0
                                • pilouP Offline
                                  pilou
                                  last edited by

                                  For SU 2017 Make
                                  Incompatible with LightUP! ๐Ÿ˜ฎ
                                  Curiously icons of LightUp apear!

                                  bizarrerie.jpg

                                  And LightUp Disconnected
                                  don't take a selection but all components on the scene! ๐Ÿ˜ฎ

                                  Here must be display only "XTE" Selected : that is not the case! ๐Ÿ˜‰
                                  All is recalled! (even the top view text who is not visible at the Plugin call! )

                                  biz4.jpg

                                  Frenchy Pilou
                                  Is beautiful that please without concept!
                                  My Little site :)

                                  1 Reply Last reply Reply Quote 0
                                  • jujuJ Offline
                                    juju
                                    last edited by

                                    @pilou said:

                                    For SU 2017 Make
                                    Incompatible with LightUP! ๐Ÿ˜ฎ
                                    Curiously icons of LightUp apear!

                                    [attachment=1:2yvqwupx]<!-- ia1 -->bizarrerie.jpg<!-- ia1 -->[/attachment:2yvqwupx]

                                    And LightUp Disconnected
                                    don't take a selection but all components on the scene! ๐Ÿ˜ฎ

                                    Here must be display only "XTE" Selected : that is not the case! ๐Ÿ˜‰
                                    All is recalled! (even the top view text who is not visible at the Plugin call! )

                                    [attachment=0:2yvqwupx]<!-- ia0 -->biz4.jpg<!-- ia0 -->[/attachment:2yvqwupx]

                                    Pilou, are you OK? I don't see what this has to do with the thread above? Perhaps start your own thread or look for the thread this is supposed to be in?

                                    Save the Earth, it's the only planet with chocolate.

                                    1 Reply Last reply Reply Quote 0
                                    • pilouP Offline
                                      pilou
                                      last edited by

                                      ?
                                      It's not the thread about Plugin "Components to Grid"?
                                      What better place to report that it is not compatible (for me in any case) with Light UP ?
                                      And it has curious behavior with the selection Components ? (for me in any case)

                                      Out of that i just reinstall SketchUp and must verify if above is always true!
                                      It will be my first Plugin to reload from a little thousand! ๐Ÿ˜„

                                      Frenchy Pilou
                                      Is beautiful that please without concept!
                                      My Little site :)

                                      1 Reply Last reply Reply Quote 0
                                      • pilouP Offline
                                        pilou
                                        last edited by

                                        Alas not better ! ๐Ÿ˜ข
                                        My SketchUp is absolutly new! Only "Component on Grid" installed! ๐Ÿค“

                                        So no conflict possible! (like Light Up above) ๐Ÿ˜‰

                                        not_better.jpg

                                        If i add new volumes : the same! that is making snowball ! All is redrawn! ๐Ÿ˜ฎ

                                        Frenchy Pilou
                                        Is beautiful that please without concept!
                                        My Little site :)

                                        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