[Plugin] Components to Grid

[Plugin] Components to Grid

Postby CadFather » Sat Jun 16, 2012 2:33 pm

This is a plugin by TIG to align all model components to a grid. The menu has a choice of 7 different spacings.

you'll find it in the 'Tools' menu


components_grid.rb
1
Last edited by CadFather on Sat Jun 23, 2012 10:34 am, edited 3 times in total.
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: Exporting Components

Postby Dave R » Sat Jun 16, 2012 3:13 pm

Just save the In Model component library as a local collection in its own folder. Now you have all the components as individual files in a folder on your hard drive. Copy it to whatever other disk you want. No plugin needed.
0
%

(THERE'S NO PLACE LIKE)

G28 X0.0 Y0.0 Z0.0

M30

%
User avatar
Dave R 
Global Moderator
 

Re: Exporting Components

Postby CadFather » Sat Jun 16, 2012 3:18 pm

..of course! :oops: :P ..thanks Dave - that's brilliant, and it brings me back to the early days when that was exactly how we did things... :thumb:
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: Exporting Components

Postby Dave R » Sat Jun 16, 2012 3:22 pm

:thumb: :lol:
0
%

(THERE'S NO PLACE LIKE)

G28 X0.0 Y0.0 Z0.0

M30

%
User avatar
Dave R 
Global Moderator
 

Re: Exporting Components

Postby TIG » Sat Jun 16, 2012 3:25 pm

There is a built-in way BUT if you want it coded then use an iteration of
https://developers.google.com/sketchup/ ... on#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
Code: Select all
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...
1
TIG
User avatar
TIG 
Global Moderator
 

Re: Exporting Components

Postby CadFather » Sat Jun 16, 2012 3:36 pm

TIG, you do it on purpose! :lol:
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: Exporting Components

Postby CadFather » Sat Jun 16, 2012 4:16 pm

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?
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: Exporting Components

Postby TIG » Sat Jun 16, 2012 5:16 pm

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:
Code: Select all
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...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Exporting Components

Postby CadFather » Sat Jun 16, 2012 5:51 pm

wow...thank you TIG - just tried it and it's fab! :D :thumb:

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
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: Exporting Components

Postby TIG » Sat Jun 16, 2012 11:07 pm

You miss called the module/method... try it this way
Code: Select all
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 !
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Exporting Components

Postby CadFather » Sun Jun 17, 2012 10:51 am

thanks TIG - works great.

i think there's a small typo here:

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 :thumb: :thumb:
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: Exporting Components

Postby TIG » Sun Jun 17, 2012 12:13 pm

You are right - I have now changed the original to 'ds'...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] Exporting Components

Postby CadFather » Sat Jun 23, 2012 10:30 am

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.. :roll:
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Exporting Components

Postby TIG » Sat Jun 23, 2012 10:41 am

This [simpler] version spaces the component-instances 2m apart along the red/x axis [no stacking into a 'square']
Code: Select all
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...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] Components to Grid

Postby CadFather » Sat Jun 23, 2012 11:12 am

Thanks TIG - much appreciated - i'll try integrating it into the grid plugin.. :thumb: :)
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Components to Grid

Postby gullfo » Sat Jun 23, 2012 4:47 pm

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...
0
User avatar
gullfo 
 

Re: [Plugin] Components to Grid

Postby TIG » Sat Jun 23, 2012 9:01 pm

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]
Code: Select all
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
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] Components to Grid

Postby Amazing Artist » Tue Jul 02, 2013 10:42 pm

TIG Thanks
0
That's my story, and I'm sticking to it!

Amazing Artist 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Plugins

Who is online

Users browsing this forum: pobi and 11 guests

Visit our sponsors: