Counting types of entities in Sketchup with Ruby API

Following up on the previous example, where I showed how you can get an array of all entities and an array of all selected entities we will sort the entities by type or class this time, and let the user know how many entities of each type there are.

We will once again begin our script with require ‘sketchup.rb’.

We will check and count the following types of entities:

  • Sketchup::Edge
  • Sketchup::Face
  • Sketchup::ConstructionLine
  • Sketchup::ComponentInstance
  • Sketchup::ConstructionPoint
  • Sketchup::Image

Let’s create a function sort_count_elements. Again we will asign active model to variable model and all model entities to variable ent. We will also set variables face, edge, componentInstance, constructionPoint, constructionLine and image to equal 0.

Now we will go through the ent array and if it is going to be of type Sketchup:Edge we will add 1 to edge varable, and the same for face, constructionLine etc.

At the end we will notify user about how many entities of each type we counted.

Again save the code and load it in Sketchup with Web Colsole plugin and press Eval (make sure you add sort_count_elements call as the last line in Web Console).

def sort_count_elements
  model = Sketchup.active_model
  ent = model.entities
  edge = face = constructionLine = componentInstance = constructionPoint = image = 0
  ent.each {|x| 
   edge+= 1 if x.is_a? Sketchup::Edge
   face+= 1 if x.is_a? Sketchup::Face
   constructionLine+= 1 if x.is_a? Sketchup::ConstructionLine
   componentInstance+= 1 if x.is_a? Sketchup::ComponentInstance
   constructionPoint+= 1 if x.is_a? Sketchup::ConstructionPoint
   image+= 1 if x.is_a? Sketchup::Image
  }
  
  UI.messagebox "Nr edges: #{edge}\nNr Faces: #{face}\nNr constructionLine: #{constructionLine}\nNr componentInstance: #{componentInstance}\nNr constructionPoint: #{constructionPoint}\nNr images: #{image}"

end

The end result will show you a pop up like the one below:

As you look at the code above you will see that we are going through array ent with ent.each. each is a standard iterator for arrays, ranges and hashes. ent.each method call is followed by code within a block that will be executed for every element in array, range or hash. In our example we used:

ent.each {|x| do something with x}, where x is value of the current element in array (in our case an entity). This is where we check if entity is of type Face, Edge etc., which we do with x.is_a? Sketchup::Face for example. We could also use x.class == Sketchup::Face, but using is_a? is nicer since it is a method made for comparing if given class is the same as the one provided.

Advertisements

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: