public MainForm() { try { OpenStudio.Model model = new OpenStudio.Model(); } catch (Exception e) { MessageBox.Show("It appears that there was an error accessing the C# SWIG Bindings for OpenStudio. Note that the libraries installed in <installdir>/CSharp/openstudio need to be accessible to this application at runtime, either through the path or in the same directory as the exe. Nothing else will work properly have this point.\n\nError Text: " + e.InnerException.InnerException.Message, "Error loading libraries"); } InitializeComponent(); }
// When the user clicks the 'go' button the idf file is loaded and, optionally, so is the sql file. // The idf file is converted to an OpenStudio model and various pieces of the OpenStudio API are demonstrated. private void run() { // TODO: // The OpenStudio Path class overloads the / operator to concatenate paths, e.g. 'path3 = path1 / path2'. // We have not yet been able to get the __div__ method to map to the / operator in C#. try { // The following calls will test that the OpenStudio dlls can be loaded OpenStudio.Path idfPath = new OpenStudio.Path(txtIdfPath.Text); if (!OpenStudio.OpenStudioUtilitiesCore.exists(idfPath)) { MessageBox.Show("Idf file '" + txtIdfPath.Text + "' does not exist", "Error finding idf file"); return; } } catch (Exception e) { MessageBox.Show("It appears that there was an error accessing the C# SWIG Bindings for OpenStudio. Note that the libraries installed in <installdir>/CSharp/OpenStudio need to be accessable to this application at runtime, either through the path or in the same directory as the exe. Nothing else will work properly have this point.\n\nError Text: " + e.InnerException.InnerException.Message, "Error loading libraries"); return; } try { // The OpenStudio dlls have been loaded and the idf file exists OpenStudio.Path idfPath = new OpenStudio.Path(txtIdfPath.Text); OpenStudio.Path sqlPath = new OpenStudio.Path(txtSqlPath.Text); // Try to read the idf file OpenStudio.OptionalIdfFile oIdfFile = OpenStudio.IdfFile.load(idfPath, new OpenStudio.IddFileType("EnergyPlus")); // Throw an exception if reading the idf file fails if (oIdfFile.isNull()) { throw new Exception("Unable to load " + idfPath.__str__() + "."); } // Dereference the optional idf file to get a real idf file OpenStudio.IdfFile idfFile = oIdfFile.get(); // Translate the idf file to an OpenStudio model OpenStudio.EnergyPlusReverseTranslator translator = new OpenStudio.EnergyPlusReverseTranslator(); OpenStudio.Model model = translator.translateWorkspace(new OpenStudio.Workspace(idfFile)); // If the sql file path is valid, try to load the EnergyPlus sql file OpenStudio.OptionalSqlFile oSqlFile = new OpenStudio.OptionalSqlFile(); if (OpenStudio.OpenStudioUtilitiesCore.exists(sqlPath)) { try { oSqlFile = new OpenStudio.OptionalSqlFile(new OpenStudio.SqlFile(sqlPath)); if (!oSqlFile.isNull()) { // associate results in this sqlFile with this model. if (!model.setSqlFile(oSqlFile.get())) { // clear the sql file if couldn't associate with this model oSqlFile.reset(); } } } catch (Exception) { // clear the sql file if couldn't associate with this model oSqlFile.reset(); } } // Get the building object OpenStudio.Building building = model.getBuilding(); // Loop over the spaces in the building double grossArea = 0; foreach (OpenStudio.Space space in building.spaces()) { string name = space.name().get(); // Get floor area from space calculation // Note that floorArea is exported as a read only property as a proof of concept double floorArea = space.floorArea; grossArea += floorArea; // Other methods of the space object have not yet been converted to properties // These must be called as methods double lightingPower = space.lightingPower(); // The method space.lights() is exported as a method foreach (OpenStudio.Lights light in space.lights()) { } // The method space.surfaces() is exported as a read only property foreach (OpenStudio.Surface surface in space.surfaces) { } // The method space.spaceType() is exported as a read only property OpenStudio.OptionalSpaceType oSpaceType = space.spaceType; } // get floor area from building calculation double buildingFloorArea = building.floorArea(); // calculated floor areas should be same if (Math.Abs(buildingFloorArea - grossArea) > 0.01) { MessageBox.Show("grossArea = " + grossArea.ToString() + " and buildingFloorArea = " + buildingFloorArea.ToString()); } if (!oSqlFile.isNull()) { // check building area, will be less because plenum is not included string query = "SELECT Value FROM tabulardatawithstrings WHERE ReportName='AnnualBuildingUtilityPerformanceSummary' AND ReportForString='Entire Facility' AND TableName='Building Area' AND RowName='Total Building Area' AND ColumnName='Area' AND Units='m2'"; double sqlBuildingFloorArea = model.sqlFile().get().execAndReturnFirstDouble(query).get(); // calculated floor area should be same as that in sqlite database if (Math.Abs(buildingFloorArea - sqlBuildingFloorArea) > 0.01) { MessageBox.Show("sqlBuildingFloorArea = " + sqlBuildingFloorArea.ToString() + " and buildingFloorArea = " + buildingFloorArea.ToString()); } } // Note that the method getLightss is an automatically generated method name (which is why it looks like a typo) // The pattern for getting all objects of type Foo from the model is "model.getFoos". // If Bar is a unique object type then the method is "model.getFoo". foreach (OpenStudio.Lights light in model.getLightss()) { string name = light.name().get(); // Get the space this light belongs to OpenStudio.OptionalSpace oSpace = light.space(); if (oSpace.isNull()) { MessageBox.Show("Lights '" + name + "' does not have an associated space."); continue; } OpenStudio.Space space = oSpace.get(); // get lighting power from calculation double lightingPower = light.getLightingPower(space.floorArea, space.numberOfPeople()); if (!oSqlFile.isNull()) { string query = "SELECT DesignLevel FROM nominallighting WHERE ObjectName='" + name.ToUpper() + "'"; double sqlLightingPower = model.sqlFile().get().execAndReturnFirstDouble(query).get(); // lighting power should be same as that in sqlite database if (Math.Abs(sqlLightingPower - lightingPower) > 0.000001) { MessageBox.Show("sqlLightingPower = " + sqlLightingPower + " and lightingPower = " + lightingPower); } } } MessageBox.Show("C# Alpha1 test completed successfully!"); } catch (Exception e) { MessageBox.Show("Error while running C# Alpha1 test: " + e.Message); } }
private void btnCreateModel_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.FileName = "in"; sfd.DefaultExt = "osm"; sfd.Filter = "OpenStudio Model (*.osm)|*.osm"; sfd.CheckPathExists = true; sfd.OverwritePrompt = true; if (sfd.ShowDialog() == DialogResult.OK) { string fname = sfd.FileName; OpenStudio.Model model = new OpenStudio.Model(); OpenStudio.Construction construction = new OpenStudio.Construction(model); construction.setName("Construction"); //ASSERT_TRUE(construction.name()); //EXPECT_EQ("Construction", construction.name().get()); OpenStudio.Space space = new OpenStudio.Space(model); space.setName("Space"); //ASSERT_TRUE(zone.name()); //EXPECT_EQ("Zone", zone.name().get()); OpenStudio.Point3dVector points = new OpenStudio.Point3dVector(); points.Add(new OpenStudio.Point3d(0, 0, 1)); points.Add(new OpenStudio.Point3d(0, 0, 0)); points.Add(new OpenStudio.Point3d(1, 0, 0)); points.Add(new OpenStudio.Point3d(1, 0, 1)); OpenStudio.Surface roof = new OpenStudio.Surface(points, model); roof.setName("Roof"); roof.setSpace(space); roof.setSurfaceType("Roof"); roof.setConstruction(construction); //ASSERT_TRUE(roof.name()); //EXPECT_EQ("Roof", roof.name().get()); //ASSERT_TRUE(roof.construction()); //EXPECT_EQ(construction.handle(), roof.construction()->handle()); OpenStudio.Surface wall = new OpenStudio.Surface(points, model); wall.setName("Wall"); wall.setSpace(space); wall.setSurfaceType("Wall"); wall.setConstruction(construction); //ASSERT_TRUE(wall.name()); //EXPECT_EQ("Wall", wall.name().get()); //ASSERT_TRUE(wall.construction()); //EXPECT_EQ(construction.handle(), wall.construction()->handle()); OpenStudio.SubSurface window = new OpenStudio.SubSurface(points, model); window.setName("Window"); window.setSurface(wall); window.setSubSurfaceType("Window"); window.setConstruction(construction); //ASSERT_TRUE(window.name()); //EXPECT_EQ("Window", window.name().get()); //ASSERT_TRUE(window.construction()); //EXPECT_EQ(construction.handle(), window.construction()->handle()); OpenStudio.Surface floor = new OpenStudio.Surface(points, model); floor.setName("Floor"); floor.setSpace(space); floor.setSurfaceType("Floor"); floor.setConstruction(construction); // Create a path OpenStudio.Path p = OpenStudio.OpenStudioUtilitiesCore.toPath(fname); if (model.save(p, true)) { MessageBox.Show("Model saved to: " + fname); OpenStudio.OptionalModel optionalModel = OpenStudio.Model.load(p); if (optionalModel.is_initialized()) { OpenStudio.Model model2 = optionalModel.get(); MessageBox.Show("Model loaded from: " + fname); } else { MessageBox.Show("Error loading model from: " + fname); } } else { MessageBox.Show("Error saving model to: " + fname); } } }
private void btnCreateModel_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.FileName = "in"; sfd.DefaultExt = "osm"; sfd.Filter = "OpenStudio Model (*.osm)|*.osm"; sfd.CheckPathExists = true; sfd.OverwritePrompt = true; if (sfd.ShowDialog() == DialogResult.OK) { string fname = sfd.FileName; OpenStudio.Model model = new OpenStudio.Model(); OpenStudio.Construction construction = new OpenStudio.Construction(model); construction.setName("Construction"); //ASSERT_TRUE(construction.name()); //EXPECT_EQ("Construction", construction.name().get()); OpenStudio.Space space = new OpenStudio.Space(model); space.setName("Space"); //ASSERT_TRUE(zone.name()); //EXPECT_EQ("Zone", zone.name().get()); OpenStudio.Point3dVector points = new OpenStudio.Point3dVector(); points.Add(new OpenStudio.Point3d(0, 0, 1)); points.Add(new OpenStudio.Point3d(0, 0, 0)); points.Add(new OpenStudio.Point3d(1, 0, 0)); points.Add(new OpenStudio.Point3d(1, 0, 1)); OpenStudio.Surface roof = new OpenStudio.Surface(points, model); roof.setName("Roof"); roof.setSpace(space); roof.setSurfaceType("Roof"); roof.setConstruction(construction); //ASSERT_TRUE(roof.name()); //EXPECT_EQ("Roof", roof.name().get()); //ASSERT_TRUE(roof.construction()); //EXPECT_EQ(construction.handle(), roof.construction()->handle()); OpenStudio.Surface wall = new OpenStudio.Surface(points, model); wall.setName("Wall"); wall.setSpace(space); wall.setSurfaceType("Wall"); wall.setConstruction(construction); //ASSERT_TRUE(wall.name()); //EXPECT_EQ("Wall", wall.name().get()); //ASSERT_TRUE(wall.construction()); //EXPECT_EQ(construction.handle(), wall.construction()->handle()); OpenStudio.SubSurface window = new OpenStudio.SubSurface(points, model); window.setName("Window"); window.setSurface(wall); window.setSubSurfaceType("Window"); window.setConstruction(construction); //ASSERT_TRUE(window.name()); //EXPECT_EQ("Window", window.name().get()); //ASSERT_TRUE(window.construction()); //EXPECT_EQ(construction.handle(), window.construction()->handle()); OpenStudio.Surface floor = new OpenStudio.Surface(points, model); floor.setName("Floor"); floor.setSpace(space); floor.setSurfaceType("Floor"); floor.setConstruction(construction); if (model.save(new OpenStudio.Path(fname), true)) { MessageBox.Show("Model saved to: " + fname); } else { MessageBox.Show("Error saving model to: " + fname); } } }