sketchup, sketchup tutorials, sketchup tips, sketchup plugins, armedia, vray
Linkedin Myspace Facebook Twitter Google Plus
Sketchup-ur-space Magazine - June 14

Author : Arpad Varga

How to create import mechanisms inside of Sketchup with Ruby scripting

In my previous article I wrote about how can we use Sketchup for data visualization. Now, I’ll show how to create import mechanisms inside of Sketchup, with the help of Ruby scripting. Basic Ruby knowledge is required. For those not familiar with scripting, I’ve included some useful links in the appendix section.

The method we will implement is capable to import CSV data, with some criteria. The result of the import process will be a mesh. Each of the mesh’s nodes have to be defined with three coordinates in the CSV file.

Example of a 3x3 node mesh, defined in CSV:

This is how a 3x3 mesh looks like in our CSV. Each line of the CSV file contains three numbers, separated by commas.

The numbers are following each other as-

  • red axis
  • blue axis
  • green axis.

How to create import mechanisms inside of Sketchup with Ruby scripting

So the grid itself is defined by the first and last numbers of each row, while the height at any given node in the grid is defined by the 2nd coordinate (blue axis).

Our importer will use the coordinates as indexes of an array too while building the mesh, therefore in the CSV file we keep the first and last columns as continuous integers. In the above example, they are between 0 and 2, because our mesh has 3x3 nodes.. You will see this explained by the code of the import method.

Importing such a CSV file will produce a mesh like this:

How to create import mechanisms inside of Sketchup with Ruby scripting

Let’s see, how to import the above data with Ruby.

We will use Sketchup’s Importer class, to implement the method. Doing so, the CSV to Mesh importer will show up in the File | Import dialog, amongst the pre-built Sketchup import methods.

We have to inherit our class from the Importer class.

How to create import mechanisms inside of Sketchup with Ruby scripting

There are methods, we have to implement in our class, to satisfy Sketchup’s requirements, these are the following ones:

How to create import mechanisms inside of Sketchup with Ruby scripting

Now we got an empty importer. Most of the above methods are self-explaining.

There is the possibility, to get parameters and settings before the import begins. If we would like to do so, our supports_options? method has to return true, and we have to implement a do_options method as well.

Sketchup will start the import process calling our load_file method. Return value of this method is an integer status. For details of this status follow the link with the Importer class in the appendix.

Inside of our load_file method, we will-

  1. read the data
  2. process text data
  3. create a mesh based the processed data
  4. add the mesh to the actual scene

This is how we read text data, by lines:

How to create import mechanisms inside of Sketchup with Ruby scripting

During the processing, text data from the CSV file will be converted to numeric data. We also apply scaling in here.

How to create import mechanisms inside of Sketchup with Ruby scripting

The code above will split each line we’ve read from the text file by commas, and will convert the result to floating point numbers. We put this numbers into the consdata[x,z] array, values of the array will be the scaled coordinates.

How to create import mechanisms inside of Sketchup with Ruby scripting

Finally, we create the mesh based our array:

How to create import mechanisms inside of Sketchup with Ruby scripting

Iterating through the pre-built consdata array we try to create each node as a Point3d object, and from the points we try to create polygons. Finally we add the polygons to the mesh..

When ready, we add the mesh to our group which is part of the active scene, and return 0, signing that the import was successful.

How to create import mechanisms inside of Sketchup with Ruby scripting

The import class is ready, we have to instantiate it, and add to Sketchup importers as follows:

Sketchup.register_importer(CSVMeshImporter.new)

All this we put into a file, called CSVImporter.rb, for example, and add it to the Sketchup Plugins folder, as described in the Hello World example, you can find below in the appendix.

We can find the CSV to Mesh importer in the File | Import… menu. It should look like this:

How to create import mechanisms inside of Sketchup with Ruby scripting

This import method is an easy representation of how to make connection between anything and Sketchup. The code above is a passive solution, because there is no two way connection between our CSV data and Sketchup model, but we got the possibility to make active connections as well, resulting in dynamic scenes. Also notice, that this simple structure we described through a CSV file is not a limitation, we can create any kind of importers with Ruby to extend Sketchup.

Appendix - Some useful links

Sketchup / Ruby - Hello World! - http://www.sketchup.com/tutorial_helloworld.php

The Importer class - http://www.sketchup.com/intl/en/developer/docs/ourdoc/importer.php

Sketchup’s Class Index - http://www.sketchup.com/intl/en/developer/docs/classes.php

What do you think about this article