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

**URL:** <https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995>\
**Category:** scripting\
**Created:** [August 1, 2017, 1:38pm UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995 "2017-08-01T13:38:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saeed390](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/saeed390/32/285_2.png) [@Saeed390](https://discourse.ladybug.tools/u/Saeed390)\
**Post date:** [August 1, 2017, 1:38pm UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/1 "2017-08-01T13:38:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TranHuyVuNguyen](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/tranhuyvunguyen/32/353_2.png) [@TranHuyVuNguyen](https://discourse.ladybug.tools/u/TranHuyVuNguyen)\
**Post date:** [August 1, 2017, 1:42pm UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/2 "2017-08-01T13:42:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saeed390](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/saeed390/32/285_2.png) [@Saeed390](https://discourse.ladybug.tools/u/Saeed390)\
**Post date:** [August 1, 2017, 1:48pm UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/3 "2017-08-01T13:48:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mostapha](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/mostapha/32/15244_2.png) [@mostapha](https://discourse.ladybug.tools/u/mostapha)\
**Post date:** [August 1, 2017, 1:59pm UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/4 "2017-08-01T13:59:00Z")

</div>

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](https://github.com/ladybug-tools/ladybug)

honeybee: [https://github.com/ladybug-tools/honeybee](https://github.com/ladybug-tools/honeybee)

honeybee-grasshopper: [https://github.com/ladybug-tools/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](https://github.com/pythonnet/pythonnet#example)

---

<div class="post-metadata">

**Author:** ![Saeed390](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/saeed390/32/285_2.png) [@Saeed390](https://discourse.ladybug.tools/u/Saeed390)\
**Post date:** [August 1, 2017, 2:44pm UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/5 "2017-08-01T14:44:00Z")

</div>

Thank you Mostapha,

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

---

<div class="post-metadata">

**Author:** ![mostapha](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/mostapha/32/15244_2.png) [@mostapha](https://discourse.ladybug.tools/u/mostapha)\
**Post date:** [August 1, 2017, 4:13pm UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/6 "2017-08-01T16:13:00Z")

</div>

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…](https://mostapharoudsari.gitbooks.io/honeybee-primer/content/text/components/_Run_Energy_Simulation.html)

and

[https://mostapharoudsari.gitbooks.io/honeybee-primer/content/text/c…](https://mostapharoudsari.gitbooks.io/honeybee-primer/content/text/components/Export_To_OpenStudio.html)

---

<div class="post-metadata">

**Author:** ![Saeed390](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/saeed390/32/285_2.png) [@Saeed390](https://discourse.ladybug.tools/u/Saeed390)\
**Post date:** [August 1, 2017, 4:16pm UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/7 "2017-08-01T16:16:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sonomirco](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/sonomirco/32/27985_2.png) [@sonomirco](https://discourse.ladybug.tools/u/sonomirco)\
**Post date:** [June 14, 2022, 7:10am UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/8 "2022-06-14T07:10:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chris](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/chris/32/10179_2.png) [@chris](https://discourse.ladybug.tools/u/chris)\
**Post date:** [June 16, 2022, 2:18am UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/9 "2022-06-16T02:18:52Z")

</div>

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](https://www.pollination.cloud/rhino-plugin).

---

<div class="post-metadata">

**Author:** ![sonomirco](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/sonomirco/32/27985_2.png) [@sonomirco](https://discourse.ladybug.tools/u/sonomirco)\
**Post date:** [June 16, 2022, 5:04am UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/10 "2022-06-16T05:04:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sonomirco](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/sonomirco/32/27985_2.png) [@sonomirco](https://discourse.ladybug.tools/u/sonomirco)\
**Post date:** [June 20, 2022, 9:46pm UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/11 "2022-06-20T21:46:30Z")

</div>

@mostapha and @MingboPeng any suggestions?  
Thanks.

---

<div class="post-metadata">

**Author:** ![MingboPeng](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/mingbopeng/32/5084_2.png) [@MingboPeng](https://discourse.ladybug.tools/u/MingboPeng)\
**Post date:** [June 21, 2022, 7:24am UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/12 "2022-06-21T07:24:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sonomirco](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/sonomirco/32/27985_2.png) [@sonomirco](https://discourse.ladybug.tools/u/sonomirco)\
**Post date:** [June 21, 2022, 7:53am UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/13 "2022-06-21T07:53:13Z")

</div>

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](https://www.bca.gov.sg/publications/building_control_act.html)  
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.

---

<div class="post-metadata">

**Author:** ![MingboPeng](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/mingbopeng/32/5084_2.png) [@MingboPeng](https://discourse.ladybug.tools/u/MingboPeng)\
**Post date:** [June 21, 2022, 8:31am UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/14 "2022-06-21T08:31:56Z")

</div>

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)

```auto
        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;
        }

```

---

<div class="post-metadata">

**Author:** ![sonomirco](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/sonomirco/32/27985_2.png) [@sonomirco](https://discourse.ladybug.tools/u/sonomirco)\
**Post date:** [June 21, 2022, 9:38am UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/15 "2022-06-21T09:38:37Z")

</div>

Thanks @MingboPeng  
I assume the `HoneybeeShema` is come from [here](https://github.com/ladybug-tools/honeybee-schema-dotnet)?  
And the ‘from honeybee.model import Model’ from [here](https://www.ladybug.tools/honeybee-core/docs/index.html)  
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.

---

<div class="post-metadata">

**Author:** ![MingboPeng](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/mingbopeng/32/5084_2.png) [@MingboPeng](https://discourse.ladybug.tools/u/MingboPeng)\
**Post date:** [June 21, 2022, 10:49am UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/16 "2022-06-21T10:49:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sonomirco](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/sonomirco/32/27985_2.png) [@sonomirco](https://discourse.ladybug.tools/u/sonomirco)\
**Post date:** [June 21, 2022, 11:40am UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/17 "2022-06-21T11:40:55Z")

</div>

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](https://developer.rhino3d.com/guides/rhinopython/ghpython-call-components/) capability to LB nodes.

---

<div class="post-metadata">

**Author:** ![sonomirco](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/sonomirco/32/27985_2.png) [@sonomirco](https://discourse.ladybug.tools/u/sonomirco)\
**Post date:** [June 23, 2022, 5:27pm UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/18 "2022-06-23T17:27:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MingboPeng](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/mingbopeng/32/5084_2.png) [@MingboPeng](https://discourse.ladybug.tools/u/MingboPeng)\
**Post date:** [June 24, 2022, 5:27am UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/19 "2022-06-24T05:27:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sonomirco](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/sonomirco/32/27985_2.png) [@sonomirco](https://discourse.ladybug.tools/u/sonomirco)\
**Post date:** [June 24, 2022, 5:39am UTC](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995/20 "2022-06-24T05:39:06Z")

</div>

Yeah is what I am going to do.  
But is quite an annoying thing, for the same reasons expressed in [this post.](https://discourse.mcneel.com/t/problems-with-newtonsoft-json-rhino/124892/5)

[Next page](https://discourse.ladybug.tools/t/how-to-use-lb-hb-functions-in-a-c-script/1995.md?page=2)
