How to use LB+HB functions in a C# script

Dear friends and colleagues,

I am writing to ask if it is possible to use Ladybug and Honeybee functions in a C# scripting component or in Visual Studio under Grasshopper template?

If yes, is there any source to learn how to do that?

Thank you very much in advance,

Cheers,

Saeed

LB&HB is created by Python sothat I think it is impossible to decode into C#.

So, is it possible to use them in a Python scripting component?

You can use them in python components if you use the new libraries. The legacy plugin is not developed as a library so you have to copy-paste the code or load them from ladybug_ladybug component which is not very elegant. Here is the link to the new WIP libraries:

ladybug: https://github.com/ladybug-tools/ladybug

honeybee: https://github.com/ladybug-tools/honeybee

honeybee-grasshopper: https://github.com/ladybug-tools/honeybee-grasshopper

You can always wrap your python script in a file and call it from inside C# code. Also check this solution: https://github.com/pythonnet/pythonnet#example

1 Like

Thank you Mostapha,

Is there any sample code which shows how to use “lagacy honeybee for Grasshopper” for energy simulation?

There is no sample code but you can check the code inside Export to EnergyPlus and OpenStudio components. Click on Source code in these pages:

https://mostapharoudsari.gitbooks.io/honeybee-primer/content/text/c…

and

https://mostapharoudsari.gitbooks.io/honeybee-primer/content/text/c…

Thank you very much Mostapha, I do appreciate your consideration and support.

@mostapha @MingboPeng I would like to bring some Ladybug/Honeybee functionalities (for example HB weekly schedule) into a custom C# component.
What is the best solution?

  1. Using IronBug?
  2. Calling the Ladybug or Honeybee component from the C# component.
  3. Wrapping the python code of the component as you mentioned above in the discussion?

Thank for the help.

Hi @sonomirco ,

I think @MingboPeng might have some ideas about this. I know that he calls some of the libraries loaded in IronPython using C# in the Pollination Rhino Plugin.

Cool!
Looking forward to hearing from him!
Thanks, @chris

@mostapha and @MingboPeng any suggestions?
Thanks.

Hi @sonomirco,
In order to help you, I will need more details on how you want to use LBT objects in the C# component? What does your C# component do?

Hi @MingboPeng
Internally we have some custom components which work in combination with LBT.
For example, one component collects data from BCA - Building Control Act
that are fed into the Weekly Schedule HB component.
So the idea is to bring the functionality of the LBT component into the custom component so I can get immediately the output I need, without the need of using 2 nodes all the time.

Hi @sonomirco,

I just created an example for you to create a new schedule in C# and convert it to python object that you can output to downstream LBT components.

(I haven’t tested it, but you get the ideas)

        public static List<object> ToPythonObjs()
        {
            // create C# model
            var model = new HoneybeeSchema.Model("SomeID", HoneybeeSchema.ModelProperties.Default);

            var newScheduleDay = new HoneybeeSchema.ScheduleDay("MyScheduleDay", new List<double> { 1 });
            var newSchedule = new HoneybeeSchema.ScheduleRuleset("MyScheduleID", new List<HoneybeeSchema.ScheduleDay> { newScheduleDay }, "MyScheduleDay");
            model.Properties.Energy.Schedules.Add(newSchedule);
            //Or 
            //model.AddSchedules(new List<HoneybeeSchema.ScheduleRuleset> { newSchedule });

            // set up Python runtime
            var pyRun = Rhino.Runtime.PythonScript.Create();
            // convert the model to json sting and set to "_hb_str" in python 
            pyRun.SetVariable("_hb_str", model.ToJson());
            string pyScript =
$@"

import honeybee.dictutil as hb_dict_util 
from honeybee.model import Model 
import json

hb_dict = json.loads(_hb_str)
model = hb_dict_util.dict_to_object(hb_dict, False)
objects = model.properties.energy.schedules;

";
            // execute python script
            pyRun.ExecuteScript(pyScript);
            // get output objects
            var objs = pyRun.GetVariable("objects");
            var pyObjs = (objs as IList<object>)?.ToList();
            return pyObjs;
        }
1 Like

Thanks @MingboPeng
I assume the HoneybeeShema is come from here?
And the ‘from honeybee.model import Model’ from here
Ok, you are using the Rhino runtime. Did you try pythonnet?

Is ladybug-grasshopper-dotnet the way of calling LB nodes? I don’t see schemas for it.

Hi @sonomirco,

You are correct about the first two.
No, I haven’t tried pythonnet.

No ladybug-grasshopper-dotnet is not something else, you can install the HoneybeeSchema via NuGet.

1 Like

Thanks @MingboPeng
I’ll try the solution, and see how it works, as well as the use of pythonnet.
It will be interesting to bring the node-in-code capability to LB nodes.

Hey @MingboPeng
LBT.Newtonsoft.Json clashes with Newtonsoft.Json.Rhino.dll funny enough I can use Newtonsoft.Json but the compiler raises the error that doesn’t know which one has to use, but I can’t associate one.

Hi @sonomirco, have you tried to set Aliases for any of Newtonsoft’s references?

1 Like

Yeah is what I am going to do.
But is quite an annoying thing, for the same reasons expressed in this post.