[Plugin] Loose Geometry to Groups (Updated August 28, 2009)

[Plugin] Loose Geometry to Groups (Updated August 28, 2009)

Postby Chris Fullmer » Thu Jan 29, 2009 4:24 am

Hi, this is my first actual ruby script contribution. For all I know, this script might already exist. But it is something I have needed in my work flow once in a while, so I thought it would be a good one to write to help me learn a little bit of Ruby.

I wrote it because sometimes I am making box shaped buildings very quickly. I'll make a few hundred. Then I always wish they were all grouped as individual buildings. So that is what this script does. It makes groups out of all loose geometry. It works by grouping connected geometry. For example, I have 10 boxes that are not grouped, or components, and are not touching each other. When I run this script, it will make a group out of each box, for a total of 10 groups.

Here is all the code. Have I missed anything important that should go into a publicly released script? I only just barely remembered to make it add itself to the plugins menu, and register itself in the file_loaded array. Anything else I've missed?

Also, possible additions to the script include the option to make groups or components, checking to see if the user has selected geometry before running itself on the entire model, maybe some pop-up boxes saying it worked or didn't work or something, and maybe the option at the end to then group all the groups into a single group, and that's about all I can think of :) Thanks!

Chris

UPDATES:
v1.0 added support for a single undo command to undo all grouping.
v1.1 wrapped the script into a module so as to not interefere with all the important scripts you have installed already :)
v1.1 Also moved plugin to "Chris Fullmer Tools"
version 1.2 2009-08-28
  • Fixed a major bug where the script was not working with selections.
  • Fixed some internal coding that might make it slightly faster.
  • Added an entity counter to the Status Bar so you can see the script working.
  • Changed the Undo title from "Make groups" to "Loose to Groups" (this is the title that you see in the Edit > Undo command).


This script is now hosted at http://www.smustard.com/script/Loose2Groups

Chris
0
Last edited by Chris Fullmer on Fri Apr 24, 2009 10:37 pm, edited 11 times in total.
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Plugin]Loose Geometry to Groups

Postby Chris Fullmer » Thu Jan 29, 2009 5:09 am

Oh, I think the first thing I want to change is to make the whole grouping thing a single undo. So I'll re-read my start_operation and commit_operation methods or whatever they are called. Then I'll repost the updated version later tonight :)

Chris

All right, I got that included. It worked out as easy as I was hoping it would. Now just one undo will undo all groups that were created.
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Plugin]Loose Geometry to Groups

Postby thomthom » Thu Jan 29, 2009 8:58 am

I didn't have a time to look properly at it. But I got one suggestion; put your code into modules. That way you reduce the risk of problems with other rubys.

And I'm not 100% sure here, but I think the require statement should be outside your method.


Code: Select all
#loose2groups.rb
#Chris Fullmer
#v1.0 Jan 28th, 2009
#Please use and distribute, but do not sell this script.
#Released to the Sketchucation.com website first. Go there
#to find updates (if there are any).

require 'sketchup.rb' # (!) note that this was moded

module ChrisFullmerUniqueModuleName # (!) pick the name you find fit which is likely to be unique
def make_groups

model = Sketchup.active_model
entities = model.active_entities
entity = entities[0]
entity_counter = 0

while entity_counter < entities.length
  entity = entities[entity_counter]
  if entity.typename == ("Edge" or "Face")
   ents = entity.all_connected
   group = entities.add_group(ents)
   entity_counter = 0
  else
   entity_counter = entity_counter + 1
  end
end
end
end # (!) End Module

if( not file_loaded?("loose2groups.rb") )
  UI.menu("Plugins").add_item("Loose Geometry to groups") { make_groups }
end
  file_loaded("loose2groups.rb")


Here's a suggestion. I maked the changed by appending # (!) at the end of the lines.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin]Loose Geometry to Groups

Postby Chris Fullmer » Thu Jan 29, 2009 9:26 am

Great! Thanks thomthom. I've got the require sketchup line moved outside of the main method now.

Now for modules. I don't entirely understand classes, modules, and mathods. I thought that since I had put everything into a method that it would be insulated from all other scripts. So I also need to wrap it in a module. I've re-read Rick's warning post and it says:

RickW wrote:3. Encapsulate methods in a Module or a Class - within a class, you don't have to worry about method naming conflicts, and within a module, you can be less careful about both class and method names.


I'll try to read-up on modules and classes. Thanks for the advice. I'll make those important changes right now!

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Plugin]Loose Geometry to Groups

Postby thomthom » Thu Jan 29, 2009 9:32 am

Classes are objects. Modules are like namespaces.

While the stuff inside your method was insulated from everything else, your method wasn't. If another ruby had a method with the name of make_groups (not a very unlikely name) there'd be a conflict.

Use Modules to group and isolate code. Use classes when you need new objects.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin]Loose Geometry to Groups

Postby Chris Fullmer » Thu Jan 29, 2009 9:34 am

ok, updated to be inside of its own module. I also updated the code in the first post so its current. Now if I wanted to access a method in this script from another script, what would be the syntax. Is it possible? Or would I have to have made it a class instead of a module?

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Plugin]Loose Geometry to Groups

Postby thomthom » Thu Jan 29, 2009 10:03 am

Code: Select all
module SomeModuleName
def make_groups
    ...
end

def foo_bar
    # lets call the other method
    make_groups
end
end # module end

SomeModuleName::make_groups # prefix with SomeModuleName:: to access stuff inside


For classes, methods needs to be prefixed with self. inside the class. http://sidtalk.wordpress.com/2008/10/06 ... ruby-self/
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin]Loose Geometry to Groups

Postby Chris Fullmer » Thu Jan 29, 2009 11:23 pm

EDIT: I don't need to bump the thread, but I wanted to say that I fixed the problem I was having below. I had to make the module name start with an uppercase letter, and then define the method as "def Modulename.methodname" format. So its all better now. Thanks!

Bummer, I think the module bit didn't work. I am getting this error:

Code: Select all
Error Loading File loose2groups.rb
C:/Program Files/Google/Google SketchUp 7/Plugins/loose2groups.rb:12: class/module name must be CONSTANT


And here is the code to my plugin:

Code: Select all
#loose2groups.rb
#Chris Fullmer
#v1.0 Jan 28th, 2009
#Please use and distribute, but do not sell this script.
#Released to the Sketchucation.com website first. Go there
#to find updates (if there are any).
#History v1.1 added a single Undo command for entire script.
#History v1.2 wrapped it all into a module

require 'sketchup.rb'

module clf_loose_groups
def make_groups

model = Sketchup.active_model
entities = model.active_entities
entity = entities[0]
entity_counter = 0
model.start_operation "Make Groups"

while entity_counter < entities.length
  entity = entities[entity_counter]
  if entity.typename == ("Edge" or "Face")
    ents = entity.all_connected
    group = entities.add_group(ents)
    entity_counter = 0
  else
    entity_counter = entity_counter + 1
  end
end
model.commit_operation
end
end
if( not file_loaded?("loose2groups.rb") )
  UI.menu("Plugins").add_item("Loose Geometry to groups") { make_groups }
end
  file_loaded("loose2groups.rb")


Thanks folks,

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Plugin]Loose Geometry to Groups

Postby RickW » Fri Jan 30, 2009 7:28 am

Chris,
Congrats on your first script! Way to jump in and make things happen :D
Regarding classes and understanding them a little bit more - they are like the framework for objects. They contain the rules for behavior and parameters. When you want one of those objects, you create an instance of it. That instance will behave like other instances that share similar parameters.

I created this (overly simple) example a long time (5 years) ago, on the old SketchUp forum. Perhaps it will help.

As for understanding modules, let's continue with the Ball class. I like playtime with my 15-month-old son. He has several toy balls that he plays with. With him in mind, I would write the Ball class as in my linked example.

On the other hand, my wife loves to dance. With her in mind, I would write a Ball class like this:
Code: Select all
class Ball
  def initialize
    #code goes here
  end

  def music=(style)
    #code goes here
  end

  def orchestra_size=(i)
    #code goes here
  end

  def refreshments=(good_eats)
    #code goes here
  end

end # class Ball

strictly_ballroom = Ball.new
strictly_ballroom.orchestra_size=50
strictly_ballroom.refreshments=["caviar","champagne","cheese","foie gras","hot wings"]
etc...


The problem is that both classes have the same name, but different methods and uses. A toy ball isn't likely to need refreshments or an orchestra size, and a grand ball with dancing and an orchestra isn't likely to be inflated or inclined to bounce. Yet if both files were loaded, a bizarre merger would occur where one could see bouncing ballrooms and round rubber toys stuffed with hors d'oeuvres. That's where modules come into play.

Code: Select all
module Toys
  class Ball
    ...etc
  end
end

module Parties
  class Ball
    ...etc
  end
end

bouncyball = Toys::Ball.new
grandball = Parties::Ball.new


This significantly cuts down on the possibility of conflicting classes and methods, but even then, selecting a unique module name is important.
0

RickW 
 

Re: [Plugin]Loose Geometry to Groups

Postby Jim » Sat Jan 31, 2009 2:37 am

Chris,

I just saw this - it doesn't do what you think it does.

Code: Select all
if entity.typename == ("Edge" or "Face")


What does the right-hand-side of the evaluate to?

You need to write it like this:
Code: Select all
if entity.typename == "Edge" or entity.typename == "Face"

or perhaps even this:

Code: Select all
if( %w(Edge Face).include?(entity.typename))
0
Hi

Jim 
Global Moderator
 

Re: [Plugin]Loose Geometry to Groups

Postby Chris Fullmer » Sat Jan 31, 2009 3:40 am

Hmm, what does what I wrote do then? It seems to work. I was thinking that it would evaluate to a true or false depending if the typename was edge,face,component, or group (Edge or face = true, group or component = false). It seems to be working in the code, but I'm sure its just dumb luck and will probably fail in circumstances I have not tested. I'll re-write like your first example (because I don't follow what's happening in the 2nd example :lol: ). I need to look at what %w is. Thank you so much for taking the time to read through the script Jim,

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Plugin]Loose Geometry to Groups

Postby Chris Fullmer » Sat Jan 31, 2009 3:53 am

ok, I think I'm getting the problem with what I wrote. I want an IF statement to essentially boil to down "if true"

but my code

Code: Select all
if entity.typename == ("Edge" or "Face")


is evaluating down (over the 3 steps I've written out)

Code: Select all
"if entity.typename == (true)"

Code: Select all
if face == true

Code: Select all
if false


So I should be getting a false no matter what the 2nd half of the evaluate is, since neither true nor false == face. Am I understanding the logic flow there correctly?

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Plugin]Loose Geometry to Groups

Postby Jim » Sat Jan 31, 2009 4:51 am

Sorry, I tried to be too clever in my last example...

this:
Code: Select all
("Edge" or "Face")

always returns "Edge" so your comparison always boils down to
Code: Select all
if entity.typename == "Edge"


which works just fine since all_connected will pick up any connected faces.
0
Hi

Jim 
Global Moderator
 

Re: [Plugin]Loose Geometry to Groups

Postby Chris Fullmer » Sat Jan 31, 2009 5:19 am

Ohh! That makes so much sense now. I was wondering why it was working, but that explains it. But yes,it was not doing what I thought it was doing. Thanks for clearing that up Jim.

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Plugin]Loose Geometry to Groups

Postby mimnbazhq » Sat Jan 31, 2009 7:24 am

:)
0

mimnbazhq 
 

Re: [Plugin] Loose Geometry to Groups (Updated March 9th, 2009)

Postby Chris Fullmer » Sat Aug 29, 2009 2:33 am

This script had a bug that I updated. I also added a counter to the status bar so that you can see the script in action. It counts the entities as it processes them so you know where it is in the process (though it is possible that the Sketchup user interface might freeze and quite showing the counter while processing large selections).

version 1.2 2009-08-28
  • Fixed a major bug where the script was not working with selections.
  • Fixed some internal coding that might make it slightly faster.
  • Added an entity counter to the Status Bar so you can see the script working.
  • Changed the Undo title from "Make groups" to "Loose to Groups" (this is the title that you see in the Edit > Undo command).

This is script is located on smustard.com at:

http://www.smustard.com/script/Loose2Groups

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 2009)

Postby bagatelo » Sat Dec 19, 2009 11:55 am

Hi Chris Fullmer

Great plugin, it is very usefull.

Would the plugin be possible to make components instead of groups??
0
While the cat's away, the mice will play
User avatar
bagatelo 
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 2009)

Postby TIG » Sat Dec 19, 2009 5:56 pm

Just edit it when the group is made and group.to_component ?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 2009)

Postby Chris Fullmer » Sat Dec 19, 2009 7:03 pm

Yup, add that line:

group.to_component

in between lines 82 and 83 - so right after this line:

group = @entities.add_group(to_group)

Its near the bottom of the scipt. about 25 lines up from the bottom.

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 2009)

Postby bagatelo » Sat Dec 19, 2009 7:18 pm

works very whell, thanks

Add in menu plugin this option
0
While the cat's away, the mice will play
User avatar
bagatelo 
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 20

Postby thomthom » Thu Sep 02, 2010 9:09 am

Chris, why are you splitting the geometry processing into chunks of 1000? Is it to speed up creation?
do you not risk splitting groups of loose geometry?
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 20

Postby Chris Fullmer » Thu Sep 02, 2010 11:47 pm

Thom, yes it was an attempt to speed things up. I do not think it helped at all. I can't look at the code right now, but I was thinking that I done something to make sure I did not split up geometry......but its also possible I did not think of that :oops:

Chris Fullmer
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 20

Postby thomthom » Fri Sep 03, 2010 7:13 am

Maybe you did, I did not decipher the code that thoroughly. Was curious if you found some speed tricks.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 20

Postby guaucorreo » Fri Sep 03, 2010 9:14 am

hi, i played with this plugin and all was god, but now
i have a bug that say in spanish "AJUSTA LA HORA", in english its something like "CHANGE THE CLOCK TIME"
????????WHY
0

guaucorreo 
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 20

Postby thomthom » Fri Sep 03, 2010 9:47 am

guaucorreo wrote:hi, i played with this plugin and all was god, but now
i have a bug that say in spanish "AJUSTA LA HORA", in english its something like "CHANGE THE CLOCK TIME"
????????WHY

Can you post a screenshot of your error message?
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 20

Postby guaucorreo » Fri Sep 03, 2010 11:18 am

i have the "loose geometry to components"
0

guaucorreo 
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 20

Postby thomthom » Fri Sep 03, 2010 11:21 am

guaucorreo wrote:i have the "loose geometry to components"

The error isn't from this plugin. The plugin is all English, and there is nothing in its code that mess with the clock.

If you post a screenshot of the error message we might be able to determine what it is.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 20

Postby guaucorreo » Fri Sep 03, 2010 11:21 am

after three seconds of runing the plugin send me this message "ajusta la hora"
0

guaucorreo 
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 20

Postby guaucorreo » Fri Sep 03, 2010 11:26 am

i have to erase the plugin and then upload to go wright, but after two or three times going well, the pluging crash.
i am working on the xfrog trees to make components of the leaves, and this plugin its perfect. then i work with yours similar objects, and all goes good except this bug.
thanksss
0

guaucorreo 
 

Re: [Plugin] Loose Geometry to Groups (Updated August 28, 20

Postby thomthom » Fri Sep 03, 2010 11:28 am

There's nothing in the source code of this plugin that has that text or any reference to any Time objects either.

What that is, is the tooltip for the Shadow Toolbar your mouse is currently hovering over in that screenshot.

You can see how it looks like in my English SU version:
SUtime.png
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 

Next


 

Return to Plugins

Who is online

Users browsing this forum: Clue, Kamangom, or35bo and 4 guests

Visit our sponsors: