Getting Started with C# API for OpenStudio

I’ll admit I was a bit reluctant to go ahead and learn Ruby just so I could develop on top of OpenStudio and SketchUp. Luckily for me (and for you) the great developers over at NREL have provided C# bindings for OpenStudio application development.

I decided it was time for me to finally give a quick “Hello World” for OpenStudio in C#. I hacked up some of the example solution files provided with the application and built a quick and dirty user form for entering in some building dimensions in order to build a simple box with four walls, a floor and a roof.

I’ll walk you through the basics of it and provide the solution as a download for those of you who want to experiment further. First we need to obtain the Length, Width and Height of the building from the User Form so we know what dimensions we are dealing with before we start creating surfaces.

// Get user inputs from form
 double bldgLength = double.Parse(txtLength.Text);
 double bldgWidth = double.Parse(txtWidth.Text);
 double bldgHeight = double.Parse(txtHeight.Text);

Before we can work with the OpenStudio model we need to initialize the model object and construction object and assign default construction set. Lastly we need to initialize a new instance of a space, and provide a name for it.

OpenStudio.Model model = new OpenStudio.Model();

OpenStudio.Construction construction = new OpenStudio.Construction(model);
construction.setName("Construction");

OpenStudio.Space space = new OpenStudio.Space(model);
space.setName("MySpace");

The rest is fairly straight forward and is part of the documentation and examples provided with the C# libraries in the application. I will show a single example of creating a surface below, in this case a wall surface. This process would be repeated for each surface you want to build. I am applying the dimensions that we collected earlier within the 3D points that will make up the surface. The surface constructor takes two arguments, one is the array of points we created contained within the Point3DVector object, and the second argument is the model which to associate the surface with. Lastly you can assign some properties to the surface such as indicating it is a wall type, and which construction set to use.

// Create the front wall
OpenStudio.Point3dVector fwallPoints = new OpenStudio.Point3dVector();
fwallPoints.Add(new OpenStudio.Point3d(0, 0, 0));
fwallPoints.Add(new OpenStudio.Point3d(bldgLength, 0, 0));
fwallPoints.Add(new OpenStudio.Point3d(bldgLength, 0, bldgHeight));
fwallPoints.Add(new OpenStudio.Point3d(0, 0, bldgHeight));

OpenStudio.Surface fwall = new OpenStudio.Surface(fwallPoints, model);
fwall.setName("Front Wall");
fwall.setSpace(space);
fwall.setSurfaceType("Wall");
fwall.setConstruction(construction);

Ultimately you end up with an executable application that presents the user with a windows form, seen below. The application saves out the .osm file with can then be opened inside of SketchUp using OpenStudio. The nice thing is you can keep the file open as you make adjustments as SketchUp will ask you to reload the latest changes. Check out the .zip file at the bottom of the page to download the entire solution.

User Input

Building in SketchUp Plugin

Download C# Solution