## Move groups with different measures?

### Move groups with different measures?

Say I have some groups
How move these different groups with a different move (here on the Z)

After reading tons of examples (and of course don't find the good and try tons of differents things (I am totally ruby maker beginner)
I have made that but don't know yet with what replace these damned ???

Ps Curious that the code text don't respect my original indentation (second loop groups.each )

EDIT you can find these 2 brilliant plugins inside the thread here
Undrop Multiple

Explosion

Code: Select all
`model = Sketchup.active_modelentities = model.entitiesselection = model.selectiongroups = []z=0entities.each do |e|  groups.push e if e.is_a? Sketchup::Group endgroups.each do |e| z= z + 100 point = Geom::Point3d.new 0,0,z t = Geom::Transformation.new point???end`
Last edited by Pilou on Sat Dec 05, 2009 5:42 pm, edited 5 times in total.
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

### Re: Move groups with different measures?

Try this...
Code: Select all
`def movegallgroupsup100mm() model=Sketchup.active_model ents=model.active_entities groups=[];ents.each{|en|groups.push(en)if en.kind_of?(Sketchup::Group)} groups.each{|gp|  tra=gp.transformation.to_a  # Z value is at position 14 ###...  tra[14]=tra[14]+100.mm ### or whatever increment desired  ### they each move up by 100mm from where they are currently...  gp.transformation=tra }end#def`

To move them all to z=0 simply substitute
Code: Select all
`tra[14]=0.0`
Last edited by TIG on Thu Jul 02, 2009 11:26 am, edited 1 time in total.
TIG

TIG
Global Moderator

Posts: 13987
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

.is_a? is much faster then .typename

I think that for your original code Pilou, you could write e.transform!(t) where you got the ???.

So we got a new scripter, eh?
Thomas Thomassen — SketchUp Monkey & Coding addict

thomthom
Global Moderator

Posts: 17618
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

You don't have to do one loop to filter out the groups and then one to process them. That isn't too efficient.

Code: Select all
`model = Sketchup.active_modelentities = model.entitiesselection = model.selectiongroups = []z=0entities.each do |e|  # Skip all entities that aren't groups  next unless e.is_a? Sketchup::Group  # Now we process the groups  z += 100 # self increments can be written shorter like this  point = Geom::Point3d.new 0,0,z  t = Geom::Transformation.new point  # Apply the transformation  e.transform!(t)end`
Last edited by thomthom on Thu Jul 02, 2009 12:00 pm, edited 1 time in total.
Thomas Thomassen — SketchUp Monkey & Coding addict

thomthom
Global Moderator

Posts: 17618
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

thomthom wrote:e.is_a(Sketchup::Group) is much faster then e.typename=="Group"

I think that e.kind_of?(Sketchup::Group) has also just
beaten e.is_a(Sketchup::Group) in some intensive testing ???
We are only talking of perhaps a fraction of a second more for moving several groups... however, if you are applying this to a large model with hundreds/thousands then obviously the quicker methods will reduce the processing by quite appreciable times...

There's a typo in your code too groups.each do |e|...
should be entities.each do |e|... as you have and empty array 'groups'...
Last edited by TIG on Thu Jul 02, 2009 11:43 am, edited 1 time in total.
TIG

TIG
Global Moderator

Posts: 13987
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

I did some further testing on that testcase: viewtopic.php?f=180&t=19576&start=15

.is_a some times came out faster than .kind_of? . But these two methods are aliases of the same method. So they should be identical. So I expect that the difference in values is due to other system operations while testing.

And I'm surprised that testing the class isn't faster than both of the other two as it's a stricter match.

If you look at the Ruby Source code for .class: http://www.ruby-doc.org/core/classes/Ob ... ml#M000376
Code: Select all
`/* *  call-seq: *     obj.class    => class *   *  Returns the class of <i>obj</i>, now preferred over *  <code>Object#type</code>, as an object's type in Ruby is only *  loosely tied to that object's class. This method must always be *  called with an explicit receiver, as <code>class</code> is also a *  reserved word in Ruby. *      *     1.class      #=> Fixnum *     self.class   #=> Object */VALUErb_obj_class(obj)    VALUE obj;{    return rb_class_real(CLASS_OF(obj));}`

While .is_a? / .kind_of? do more processing:
Code: Select all
`/* *  call-seq: *     obj.is_a?(class)       => true or false *     obj.kind_of?(class)    => true or false *   *  Returns <code>true</code> if <i>class</i> is the class of *  <i>obj</i>, or if <i>class</i> is one of the superclasses of *  <i>obj</i> or modules included in <i>obj</i>. *      *     module M;    end *     class A *       include M *     end *     class B < A; end *     class C < B; end *     b = B.new *     b.instance_of? A   #=> false *     b.instance_of? B   #=> true *     b.instance_of? C   #=> false *     b.instance_of? M   #=> false *     b.kind_of? A       #=> true *     b.kind_of? B       #=> true *     b.kind_of? C       #=> false *     b.kind_of? M       #=> true */VALUErb_obj_is_kind_of(obj, c)    VALUE obj, c;{    VALUE cl = CLASS_OF(obj);    switch (TYPE(c)) {      case T_MODULE:      case T_CLASS:      case T_ICLASS:        break;      default:        rb_raise(rb_eTypeError, "class or module required");    }    while (cl) {        if (cl == c || RCLASS(cl)->m_tbl == RCLASS(c)->m_tbl)            return Qtrue;        cl = RCLASS(cl)->super;    }    return Qfalse;}`
Thomas Thomassen — SketchUp Monkey & Coding addict

thomthom
Global Moderator

Posts: 17618
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

On average testing the .class come out similar, which I think might be due to the types of things we test. It'll usually short-circut returning true on the first iteration of the loop. So there probably has to be a much great number of iterations of the methods to see a real difference.
Thomas Thomassen — SketchUp Monkey & Coding addict

thomthom
Global Moderator

Posts: 17618
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

@ Tig Some cryptic but I will see that
Joint an image for no ambiguities
So here the increment is 300 cm
Edit sorry I have not seen all these speedy results!

@Thomthom! Yes that is that ! e.transform!(t)
Please, register (free) to access all the attachments on the forums.
Last edited by Pilou on Wed Jul 15, 2009 2:41 pm, edited 3 times in total.
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

### Re: Move groups with different measures?

TIG wrote:There's a typo in your code too groups.each do |e|...
should be entities.each do |e|... as you have and empty array 'groups'...

Ooops! Corrected.
Thomas Thomassen — SketchUp Monkey & Coding addict

thomthom
Global Moderator

Posts: 17618
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

Just to find now apply that just only for a selection!
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

### Re: Move groups with different measures?

Pilou wrote:à Tig Some cryptic but I will see that
Joint an image for no ambiguities
So here the increment is 300 cm

My method adds '100mm' to each group's Z and moves then to suit: the other way with 'z+=100.mm' increments z by 100mm each step through the loop so then the groups get progressively further apart !

If you want to move all groups a certain amount use my way, if [for some unfathomable reason] you want to do a pseudo-grow then increment the offset each time...

My method finds each group's transformation [as an array], changes just its Z value and then resets it: the other way invents a new transformation and applies it to the group...
TIG

TIG
Global Moderator

Posts: 13987
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

Pilou wrote:Just to find now apply that just only for a selection!

Code: Select all
`def movegallgroupsup100mm()    model = Sketchup.active_model    entities = model.active_entities    selection = model.selection    ###groups = [];entities.each{|entity|groups.push(entity) if entity.kind_of?(Sketchup::Group)}###    groups=[];selection.each{|entity|groups.push(entity) if entity.kind_of?(Sketchup::Group)}    groups.each{|group|      transformation_as_array = group.transformation.to_a      # Z value is at position 14 ###...      transformation_as_array[14] = transformation_as_array[14] + 100.mm      ### or whatever increment is desired      ### they each move up by 100mm from where they are currently...      group.transformation = transformation_as_array    }end#def`

Note that I've keep making the array of groups separate from the loop though the selection - changing them - since this way allows you to do other things to that array later if desired... e.g. groups[0].material="Red"###colours the first group red !
PS: The test definition 'movegallgroupsup100mm' is optional !!!
To test your code I suggest you type this in the Ruby-Console [assuming the file is called "tester.rb"]
this will load and run the script... if it's not doing what you expect then change the script and copy+paste the line back into the console and it will load/run repeat... etc etc
TIG

TIG
Global Moderator

Posts: 13987
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

@Tig
I have the "Web console" so no problem for copy/ past and launch it
Seems your plug move all selection of groups, but not each by each
Result stay grouped without interval

What is precisely this "14" value?
( i have just 5 group and one component (Bryce) for the moment and just 3 selected for the test
Why not take the first value of the array?

The ThomThom method works except that woks for the 5 groups and not only for the selection
Please, register (free) to access all the attachments on the forums.
Last edited by Pilou on Thu Jul 02, 2009 1:22 pm, edited 1 time in total.
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

### Re: Move groups with different measures?

Code: Select all
`model = Sketchup.active_modelentities = model.entitiesselection = model.selectiongroups = []z=0selection.each do |e| # update!  # Skip all entities that aren't groups  next unless e.is_a? Sketchup::Group  # Now we process the groups  z += 100 # self increments can be written shorter like this  point = Geom::Point3d.new 0,0,z  t = Geom::Transformation.new point  # Apply the transformation  e.transform!(t)end`
Thomas Thomassen — SketchUp Monkey & Coding addict

thomthom
Global Moderator

Posts: 17618
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

Code: Select all
`model = Sketchup.active_modelentities = model.entitiesselection = model.selectiongroups = []z=0selection.each do |e| # update!  # Skip all entities that aren't groups  next unless e.is_a? Sketchup::Group  # Now we process the groups     point = Geom::Point3d.new 0,0,z  t = Geom::Transformation.new point  # Apply the transformation  e.transform!(t)  z = z*2 + 100end`

Perfect (I put the z=z*2+100 at the end for have the first group of selection with no move)
Last thing I obtain 254 cm ( I suppose that is a conversion between inch/cm ?)
My first volume is at level z = 0 and I work in cm
Last edited by Pilou on Thu Jul 02, 2009 1:44 pm, edited 8 times in total.
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

### Re: Move groups with different measures?

SU always uses inches internally.

If you want 100mm type 100.mm . See http://code.google.com/intl/nb/apis/ske ... meric.html for more units.
Thomas Thomassen — SketchUp Monkey & Coding addict

thomthom
Global Moderator

Posts: 17618
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

Pilou

Do you want to move each of the selected groups by the same amount (my code - e.g. 100.mm) or by an ever changing amount ?

If you want it to change then you need to define how, and add this into the loop - e.g. z=1..... then loop... z += 2*z
exponential type increase !

If you are applying different offsets, and you have made the groups in order, then the increments will apply in that way; but it would be possible to 'stack' groups made at different times and the loop would process then in their made order, but not the 'stacked' order you want ! Perhaps you need to add in another test... for each of the group.bounds.min.z. which sorts them by their relative heights, so at least the groups array would be in the height order you expect...
TIG

TIG
Global Moderator

Posts: 13987
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

If you want 100mm type 100.mm

Yes! Works like a charm!
For my first one that was not like a true romance but with your help, ThomThom, Tig (yet a lot of cryptic for me , and the Chris tutorial and a dash of milk of Tod Burch
Many thx for the helps

Next will maybe an "explode view" of several groups
Just need to know position of a group

PS @ Tig Sorry but I had no chance with yours! (see previous page)
And yes I want any sort of increments so now I can have any sort of it! Thx!
Just play with parameters
I keep your group.bounds.min.z. trick in my mind!
Please, register (free) to access all the attachments on the forums.
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

### Re: Move groups with different measures?

for a completely random offset try
Code: Select all
`selection.each do |e|    next unless e.is_a? Sketchup::Group    ###point = Geom::Point3d.new 0,0,(1000.mm * rand)    ### offset in Z somewhere between 0m and 1m - 'rand' makes a random number 0><1    point = Geom::Point3d.new (1000.mm * rand),(1000.mm * rand),(1000.mm * rand)    ### offset in all XYZ axes by 0 to 1m    t = Geom::Transformation.new point    e.transform!(t)end`

Swap the ### between the different points = to see the effect
TIG

TIG
Global Moderator

Posts: 13987
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

Before testing your new randomizer plug

I have a little problem
I have used "Grow" by Tig for make the "volume" on the left (50 groups), then my "Plug" Thomthomised
with a normal interval of course!
Seems there is something wrong somewhere
Groups are not recorded following the "grow" generation?
Please, register (free) to access all the attachments on the forums.
Last edited by Pilou on Thu Jul 02, 2009 3:02 pm, edited 1 time in total.
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

### Re: Move groups with different measures?

The entities are not returned in their order in z height. You would need to sort the objects by their Z position before applying the transformations.
Thomas Thomassen — SketchUp Monkey & Coding addict

thomthom
Global Moderator

Posts: 17618
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

So i must use the Tig Advertissement more soon than I have expected
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

### Re: Move groups with different measures?

This group.bounds.min.z. can be found somewhere or the sorting of Groups must be forged again?
(nothing about sort, sorting in the API helping)
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

### Re: Move groups with different measures?

Build a hash with the entities as the key and their Z posiiton as value and then sort the hash by the values.
Thomas Thomassen — SketchUp Monkey & Coding addict

thomthom
Global Moderator

Posts: 17618
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

Each groups has not a "time creation" trace board? So no need to remake a sorting ?
Seems I have re-open a new nightmare door
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

### Re: Move groups with different measures?

Awesome Pilou! Keep it up, its looking great. You've got lots of good help around here.

Chris
Lately you've been tan, suspicious for the winter.
All my Plugins I've written

Chris Fullmer
SketchUp Team

Posts: 6691
Joined: Wed Nov 21, 2007 3:21 am
Location: Davis, CA
Name: Chris Fullmer
Operating system: Windows
SketchUp version: 8
SketchUp use: landscape architecture

### Re: Move groups with different measures?

Try something like...
Code: Select all
`zeds=[]index=0groups.each{|group|   zeds.push(group.bounds.min.z, index)   index+=1}zeds.sort!sorted_groups=[]zeds.each{|z|sorted_groups.push(groups[z[1]]) }### sorted_groups is now groups sorted by Z`
TIG

TIG
Global Moderator

Posts: 13987
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

Does the group.bounds.min.z only valuable in SU 7?
Edit seems not

little try but seems I miss something (any doubt selection but the sorted_groups gives nothing on the screen)
Code: Select all
`model = Sketchup.active_modelentities = model.entitiesselection = model.selectiongroups = []z=0zeds=[]index=0groups.each{|group|   zeds.push(group.bounds.min.z, index)   index+=1}zeds.sort!sorted_groups=[]zeds.each{|z|sorted_groups.push(groups[z[1]]) }### sorted_groups is now groups sorted by Zsorted_groups.each do |e|   point = Geom::Point3d.new 0,0,z  t = Geom::Transformation.new point  e.transform!(t)    z =  z + 1000.mmend`
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

### Re: Move groups with different measures?

groups = [] then nothing set !!!
You emptied the groups array...
You need to fill it from the selection...
Code: Select all
`groups=[];selection.each{|entity|groups.push(entity) if entity.kind_of?(Sketchup::Group)}`
Last edited by TIG on Thu Jul 02, 2009 9:17 pm, edited 1 time in total.
TIG

TIG
Global Moderator

Posts: 13987
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 8
SketchUp use: architecture

### Re: Move groups with different measures?

it's like a snowball on the top of the mountain
Frenchy Pilou
Is beautiful that please without concept!
Speedy Galerie

Pilou
Top SketchUcator

Posts: 10049
Joined: Wed Jan 23, 2008 10:33 pm
Operating system: Windows
SketchUp version: 6
SketchUp use: hobby

Keyframe Animation plugin - animate your SketchUp model by adding movement to any object.