Question-and-Answer Resource for the Building Energy Modeling Community
Get s tarted with the Help page

# How to get start modeling with OS C# Binding

I'm trying to learn modelling building energy systems through OS C# binding. Is there any complete tutorial to be used ? I've tried the following questions and answers but found them not complete :

A minimal example to run a simple model would be more than enough.

Based on your suggestion, I've copied Alpha1 C# example to a different directory and also copy all dependent dll files into the corresponding folder and follow the instructions given here . Implementation executes without an error and produce a form to import .idf files. When I load the idf file here C:\Program Files\OpenStudio 1.12.0\Examples\resultsviewer\SmallOffice\SmallOffice.idf and say "Go", I got the following error message (similar error message are noted in this message also ):

Based on this, I've the following questions/issues :

1. When the problem is resolved, will I be able to modify the simulation model (SmallOffice.idf) from C# ? Which methods should I use for this ?
2. Inline with this error message, I wanted to test my OS installation with a different setting and used the implementation given here. However, this time I got the following exception
3. Then, I minimize the code in (3) to check what is wrong as in the following :

Path path = new Path("C:\Desktop\testModel.osm") but still get the same error.

4. If the problem resolved, is the following code starting point to modify a model in C# ? If not, what should I do to load/create and execute a simple simulation model (.idf and or .osm files) from C# API ? Which methods should I use ?

 Path path = new Path("C:\\Desktop\\testModel.osm");


Note that, the second line of the code was previously mentioned in this and this answers/questions.

1. I've ensured that all dlls are copied and solution configuration set as x64 accordingly as in the following : However, after that, I got the following error message :

Error CS0104 'Application' is an ambiguous reference between 'System.Windows.Forms.Application' and 'OpenStudio.Application' Alpha1 D:\OpenStudio\Alpha1\Alpha1\Program.cs 24 Active

so that I modified the code in Alpha1 as in the following :

         System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.Run(new MainForm());


now, the form successfully created but I got an exception when I want to load an IDF file (clicking the pointed button in the following picture)

Here is the exception and highlighted code segment in MainForm.cs that cause the exception

What I observed is, I was able to successfully clicking that button and pointing an IDF file ...

edit retag close merge delete

@cemal are you just interested in running workflows with measures, or do you wan to directly interact with the OpenStudio API? Might have different answers based on this.

( 2016-09-21 15:30:43 -0500 )edit

@David Goldwasser I need to directly interact with the OpenStudio API. Thanks in advance.

( 2016-09-21 17:32:33 -0500 )edit

The program launches fine but crashes as soon as it tries to load OpenStudio.dll and its dependencies. Here are some tips:

• Build your C# program in x86 or x64 mode to match the version of OpenStudio you installed (32 or 64 bit)
• Copy all dlls from 'C:\Program Files\OpenStudio 1.12.0\CSharp\openstudio' to the directory with your executable, for me this was 'E:\test\Alpha1\Alpha1\bin\x64\Debug' (note I installed 64 bit OpenStudio and built a 64 bit program)

Doing this worked for me. We can answer your questions about what to do next after you can run the initial program.

( 2016-09-22 12:16:18 -0500 )edit

I have not seen that dialog error, I am using Visual Studio 2013, it seems like you are using 2015. I would try setting your program to single threaded like the dialog says, Googling the error message should provide help. It seems like you are getting closer.

( 2016-09-27 22:13:11 -0500 )edit

Thanks @macumber, I successfully load an idf file. However, still having issues to load and modify an osm file as explained in the modified question.

( 2016-09-28 05:48:19 -0500 )edit

Sort by » oldest newest most voted

The best way to get started in C# is to copy one of the example C# projects in your OpenStudio install (e.g. "C:\Program Files\OpenStudio 1.12.0\CSharp\examples\RunManagerExample") to somewhere on your hard drive where you have write access. Then you will want to delete the reference to OpenStudio.dll and re-add it, pointing to the OpenStudio.dll in your install (e.g. "C:\Program Files\OpenStudio 1.12.0\CSharp\openstudio\OpenStudio.dll"). Finally, in order to run your application, you will need to be able to load all of OpenStudio.dll's dependencies (including the Qt plugins). The easiest way to do this is to copy all of the files from "C:\Program Files\OpenStudio 1.12.0\CSharp\openstudio\" into the directory containing the application you are building. You may also have to copy the plugin folders (e.g. 'platforms', 'sqldrivers') from "C:\Program Files\OpenStudio 1.12.0\bin" as well.

OpenStudio 2.0 will have Qt libraries and plugins statically linked in so there will be less stuff to deploy alongside your application.

more

Thanks @macumber for your answer but problem still exists. I've edited my question to give more insight and explain what I tried.

( 2016-09-22 11:33:43 -0500 )edit

You need to copy the dlls as I mentioned above. I added a comment under your answer to clarify. Let's save the questions about what to do next for a separate question post.

( 2016-09-22 12:18:14 -0500 )edit

Thanks @macumber, I've tried your recommendations and edited the question with new issues.

( 2016-09-27 12:30:07 -0500 )edit