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

Loading and editing an .osm file in OS C# binding

asked 2016-09-29 04:52:28 -0500

cemal gravatar image

updated 2017-09-09 13:25:25 -0500

Hi everyone,

This question is continuation of my previous post. In that question, I resolved the issue of running Alpha1 test C# implementation with @macumber's helps. As he suggested and since the previous post getting massive, I'm posting the new question here.

I'm trying to load and modify an .osm file via OpenStudio C# binding using Visual Studio 2015. I've a "testModel.osm" file that is working via OpenStudio GUI without any problem. I'm using the following code segment to load it in C#.

Path path = new Path("C:\\Desktop\\testModel.osm");
OpenStudio.Model model = OpenStudio.Model.load(path).get();

and having the following exception image description O also changed the second line with the following two lines but still having the same exception

OpenStudio.VersionTranslator translator = new OpenStudio.VersionTranslator();
OpenStudio.Model model = translator.loadModel(path).get();

Note that execution of the following line alone is perfectly fine.

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

Thanks for your helps in advance.

Edit after @macumber's comment:

Thanks @macumber, I modify the implementation as in the following :

Path path = new Path("C:\\Desktop\\testModel.osm");
OpenStudio.VersionTranslator translator = new OpenStudio.VersionTranslator();
OpenStudio.OptionalModel optionalModel = translator.loadModel(path);
foreach(var i in translator.errors())
foreach(var i in translator.warnings())
if (optionalModel.is_initialized())
        OpenStudio.Model model = optionalModel.get();
        Console.WriteLine("optional model is initialised");
        Console.WriteLine("optional model is not initialized");

And here is the console output after executing the program :

image description

Unfortunately, I couldn't get any error or warning message printed. How can we proceed ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-09-29 09:41:07 -0500

updated 2016-09-29 09:42:51 -0500

I was going to suggest that you use the VersionTranslator to load the model but it looks like you already tried that. When you use the VersionTranslator, errors and warnings that get logged during translation get stored in the VersionTranslator object. Try the following (psuedo-code, hopefully you get the idea):

OpenStudio.VersionTranslator translator = new OpenStudio.VersionTranslator();
OpenStudio.OptionalModel optionalModel = translator.loadModel(path); // don't call get here
translator.errors(); // loop over these and call .logMessage on each, print to the screen
translator.warnings(); // loop over these and call .logMessage on each, print to the screen
if (optionalModel.is_initialized()){
  OpenStudio.Model model = optionalModel.get(); // call get only if it is initialized
edit flag offensive delete link more


Thanks @macumber, even if I changed the code as you suggested, I couldn't get and warning and/or error message. I've edited the question and explained what I did.

cemal gravatar image cemal  ( 2016-09-30 04:27:33 -0500 )edit

Does a file at that path exist? 'C:\Desktop' is not a normal Windows path.

macumber gravatar image macumber  ( 2016-09-30 10:47:10 -0500 )edit

I was modifying it to simplify the post. You're right @macumber, I corrected the path and everything is working. Thanks again for your support.

cemal gravatar image cemal  ( 2016-09-30 12:00:18 -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


Asked: 2016-09-29 04:52:28 -0500

Seen: 236 times

Last updated: Sep 30 '16