mark's banner

Saturday, March 01, 2008

Sketchup Ruby - Mark's First Ruby Script


Instead of doing homework today I spent most of the day hacking my way through a simple ruby script. Ruby on Rails is the scripting language adopted by Sketchup. There is lots of information available for Ruby, but I stuck to only the sketchup resources - they seemed more relevant.

What I wanted to do with this script was to find all of the entities in the file that had the same attribute. In this case I made the script even less elegant by taking advantage of the entity's definition name. All entities seem to have this parameter so I was able to take advantage of it y using sketchup's gui and then by extracting the name in the script - that is, no need to build my own dialog (maybe my next lesson - there are some interesting examples delivered with sketchup).

Essentially, what the script does is request all entities in the model and then enumerates through each. The entity definition name is compared to the string I want to find, in this case Display Panel and if a match is found increment the counter. When the script is done it returns a count of Display Panels and ignores entities with other definition names.

A second script is included in panel.rb. This is a simple request for the total number of entities in the model. This script is quites simple it simple uses the model.entities.count method to return the total. UI.message prints the result in both cases.

Panel.zip includes the test.skp file, and the ruby script panel.rb.

1 Comments:

Blogger Tim said...

Hi, Ruby is supported by Sketchup through an API, but RubyonRails has nothing to do with Sketchup whatsover. I want to slightly correct your post so that people don't get confused. RubyonRails is an extremely popular web application framework built with Ruby and has become somewhat confused as being Ruby itself. Ruby is a general programming language that is very flexible and easy to build into many interesting things such as a web development framework called RubyonRails or programmatically manipulate the Sketchup API for Ruby.

March 30, 2008 4:26 PM  

Post a Comment

<< Home