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

How to get start modeling with OS C# Binding

asked 2016-09-21 10:33:30 -0500

cemal gravatar image

updated 2016-09-29 04:56:56 -0500

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 :

  1. https://unmethours.com/question/19848...
  2. https://unmethours.com/question/19123...
  3. https://unmethours.com/question/17865...
  4. https://unmethours.com/question/15093...
  5. https://unmethours.com/question/11404...
  6. https://github.com/NREL/OpenStudio/tr...
  7. https://unmethours.com/question/11881...
  8. http://nrel.github.io/OpenStudio-user...
  9. https://unmethours.com/question/9864/...
  10. https://unmethours.com/questions/1264...

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

Edit after @macumber's answer :

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 ):

image description

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 image description
  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");
     OpenStudio.Model model = OpenStudio.Model.load(path).get();
    

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

Edit after @macumber's comments

  1. I've ensured that all dlls are copied and solution configuration set as x64 accordingly as in the following : image description 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)

image description

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

image description

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

edit retag flag offensive close merge delete

Comments

@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.

David Goldwasser gravatar image David Goldwasser  ( 2016-09-21 15:30:43 -0500 )edit

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

cemal gravatar image cemal  ( 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.

macumber gravatar image macumber  ( 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.

macumber gravatar image macumber  ( 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.

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

1 Answer

Sort by ยป oldest newest most voted
6

answered 2016-09-21 20:28:21 -0500

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.

This question is most relevant to what you are asking about.

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

edit flag offensive delete link more

Comments

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

cemal gravatar image cemal  ( 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.

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

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

 

Question Tools

1 follower

Stats

Asked: 2016-09-21 10:33:30 -0500

Seen: 436 times

Last updated: Sep 29 '16