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.
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:
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.
There are methods, we have to implement in our class, to satisfy Sketchup’s requirements, these are the following ones:
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-
- read the data
- process text data
- create a mesh based the processed data
- add the mesh to the actual scene
This is how we read text data, by lines:
During the processing, text data from the CSV file will be converted to numeric data. We also apply scaling in here.
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.
Finally, we create the mesh based our array:
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.
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:
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
- Cover Story
- SketchUp Can Help You Win Interior..
- Best Laptops for SketchUp
- How to Resize Textures and Materials..
- Discovering SketchUp 2020
- Line Rendering with SketchUp and VRay
- Pushing The Boundary with architectural
- Trimble Visiting Professionals Program
- Diagonal Tile Planning in SketchUp
- Highlights of some amazing 3D Printed
- Review of a new SketchUp Guide
- Sketchup Resources
- SKP for iphone/ipad
- SKP for terrain modeling
- Pool Water In Vray Sketchup
- Rendering Optimization In Vray Sketchup
- Background Modification In sketchup
- Grass Making with sketchup fur plugin
- Landscape designing in Sketchup
- Apply styles with sketchup
- Bedroom Making with sketchup
- Review of Rendering Software
- Enhancing rendering for 3d modeling
- The combination of sketchup
- Exterior Night Scene rendering with vray