1

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

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())
   {
       Console.WriteLine(i.logMessage());
   }
foreach(var i in translator.warnings())
   {
        Console.WriteLine(i.logMessage());
   }
if (optionalModel.is_initialized())
   {
        OpenStudio.Model model = optionalModel.get();
        Console.WriteLine("optional model is initialised");
   }
else
    {
        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 ?

cemal's avatar
65
cemal
asked 2016-09-29 04:52:28 -0500
__AmirRoth__'s avatar
4.4k
__AmirRoth__
updated 2017-09-09 13:25:25 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

1 Answer

1

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
}
macumber's avatar
12k
macumber
answered 2016-09-29 09:41:07 -0500, updated 2016-09-29 09:42:51 -0500
edit flag offensive 0 remove flag delete link

Comments

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's avatar 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's avatar 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's avatar cemal (2016-09-30 12:00:18 -0500) edit
add a comment see more comments