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

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