Skip to content

Commit

Permalink
fix(ZonePicker): support new LBT and deprecate Legacy version
Browse files Browse the repository at this point in the history
  • Loading branch information
MingboPeng committed May 6, 2024
1 parent 0d55cdf commit ce4a3a9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 25 deletions.
74 changes: 74 additions & 0 deletions src/Ironbug.Grasshopper/Classes/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Grasshopper.Kernel.Types;
using Rhino.Geometry;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -83,6 +84,60 @@ public static string GetRoomName(object HBObj)

}

public static Point3d GetRoomCenter(object HBObj)
{
if (HBObj is null)
throw new System.ArgumentException("Invalid Room object!");

if (HBObj is Types.OsZone z)
{
return VolumeMassProperties.Compute(z.Value).Centroid;
}
else if (HBObj is GH_Brep b)
{
// Legacy HBZones
throw new System.ArgumentException("Ironbug doesn't support HBZones from the legacy version anymore!");
}
else if (HBObj is GH_ObjectWrapper wrapper)
{

var pyObj = wrapper.Value;
if (pyObj is Ironbug.HVAC.BaseClass.IB_ThermalZone)
throw new System.ArgumentException("Invalid Room object!");

// LBT Room
var isPythonObj = pyObj.GetType().ToString().StartsWith("IronPython.");
if (!isPythonObj)
throw new System.ArgumentException("Invalid Room object!");

var name = pyObj.ToString();
var isLBTRoom = name.StartsWith("Room:");
var isDFRoom2D = name.StartsWith("Room2D:");

if (isLBTRoom || isDFRoom2D)
return GetLBTRoomCenter(pyObj);


}
else if (HBObj.GetType().Name == "GH_HBPythonObjectGoo") //Pollination Honeybee components
{
var rm = HBObj;
var tp = HBObj?.GetType();
var type = tp?.GetProperty("POTypeName")?.GetValue(rm)?.ToString();

if (type == "Room")
{
var pyObj = tp.GetProperty("RefPythonObj")?.GetValue(rm);
return GetLBTRoomCenter(pyObj);
}
}


throw new System.ArgumentException("Invalid Room object!");

}


public static int[] GetDateRange(object LBObj)
{
if (LBObj is null)
Expand All @@ -101,6 +156,7 @@ public static int[] GetDateRange(object LBObj)

throw new System.ArgumentException("Failed to convert LB Analysis Period!");
}

/// For new Honeybee
static string GetLBTRoomName(object lbtRoom)
{
Expand Down Expand Up @@ -132,6 +188,23 @@ public static IEnumerable<string> FromLBTRooms(IEnumerable<object> inRooms)
return zoneIds;
}

public static Point3d GetLBTRoomCenter(object lbtRoom)
{
var pyRun = GetPython();
pyRun.SetVariable("HBRoom", lbtRoom);
string pyScript = @"
xyz = HBRoom.geometry.center.to_array()
";
pyRun.ExecuteScript(pyScript);
var list = pyRun.GetVariable("xyz") as IList<dynamic>;
if (list == null || list.Count() != 3)
throw new System.ArgumentException("Invalid Honeybee Room object");
var xyz = list.OfType<double>().ToArray();
var p = new Point3d(xyz[0], xyz[1], xyz[2]);
return p;

}

static int[] GetLBTDateRange(object lbtAnalysisPeriod)
{
var pyRun = GetPython();
Expand All @@ -144,6 +217,7 @@ static int[] GetLBTDateRange(object lbtAnalysisPeriod)
var dateRange = pyobj.OfType<int>().ToArray();
return dateRange;
}

/// For legacy Honeybee
public static IEnumerable<string> CallFromHBHive(IEnumerable<GH_Brep> inBreps)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,34 @@ public Ironbug_ZonePicker()

protected override void RegisterInputParams(GH_InputParamManager pManager)
{
pManager.AddBrepParameter("Zones", "_zones", "objects to be picked", GH_ParamAccess.list);
pManager.AddGenericParameter("Zones", "_zones", "objects to be picked", GH_ParamAccess.list);
pManager[pManager.AddBrepParameter("Scopes", "scopes", "Scope breps for picking zone breps based on its centroid location.", GH_ParamAccess.list)].Optional = true;

}

protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddBrepParameter("Selected Zones", "zones", "Picked objects", GH_ParamAccess.list);
pManager.AddBrepParameter("Unselected Zones", "unSelected", "Unselected objects", GH_ParamAccess.list);
(pManager[1] as GH.Kernel.Parameters.Param_Brep).Hidden = true;
pManager.AddGenericParameter("Selected Zones", "zones", "Picked objects", GH_ParamAccess.list);
pManager.AddGenericParameter("Unselected Zones", "unSelected", "Unselected objects", GH_ParamAccess.list);
}

List<GH_Brep> AllInputBreps = new List<GH_Brep>();
List<(object room, Point3d centroid)> AllInputBreps = new List<(object, Point3d)>();
protected override void SolveInstance(IGH_DataAccess DA)
{

var allBps = new List<GH_Brep>();
var allBps = new List<object>();
if (!DA.GetDataList(0, allBps)) return;
AllInputBreps = allBps;

var cs = allBps.Select(_=> ( room : _, centroid : Helper.GetRoomCenter(_) )).ToList();
AllInputBreps = cs;

var nodes = new List<GH_Brep>();
DA.GetDataList(1, nodes);

var unselectedZones = new List<GH_Brep>();
var unselectedZones = new List<object>();
if (nodes.Count > 0)
{
var selectedZs = GetZoneFromNode(allBps, nodes, out unselectedZones);
var selectedZs = GetZoneFromNode(cs, nodes, out unselectedZones);
DA.SetDataList(0, selectedZs);
}
else
Expand All @@ -62,18 +63,21 @@ protected override void SolveInstance(IGH_DataAccess DA)

}

private static List<GH_Brep> GetZoneFromNode(List<GH_Brep> allBps, IEnumerable<GH_Brep> outBx, out List<GH_Brep> Unselected)
private static List<object> GetZoneFromNode(List<(object room, Point3d centroid)> allBps, IEnumerable<GH_Brep> outBx, out List<object> Unselected)
{
var selectedZones = new List<GH_Brep>();
var unselectedZones = new List<GH_Brep>();
var selectedZones = new List<object>();
var unselectedZones = new List<object>();


var inputBrpsCenterPts = allBps.AsParallel().AsOrdered().Select(_ => VolumeMassProperties.Compute(_.Value).Centroid);
var num = 0;
foreach (var pt in inputBrpsCenterPts)
foreach (var rmC in allBps)
{
var isSel = outBx.AsParallel().FirstOrDefault(_ => _.Value.IsPointInside(pt,0.0001,true)) != null;
var currentItem = allBps[num];
var c = rmC.centroid;
if (c == Point3d.Unset)
continue;

var isSel = outBx.AsParallel().FirstOrDefault(_ => _.Value.IsPointInside(c,0.0001,true)) != null;
var currentItem = allBps[num].room;
if (isSel)
{
selectedZones.Add(currentItem);
Expand Down Expand Up @@ -104,14 +108,10 @@ private void PickZones()

var allBps = this.AllInputBreps;

if (!allBps.Any())
{
return;
}
var zParm = this.Params.Input[0] as GH.Kernel.Parameters.Param_Brep;
zParm.Hidden = false;
if (!allBps.Any()) return;

var nodeIds = new List<Guid>();
var centers = allBps.AsParallel().Select(_ => VolumeMassProperties.Compute(_.Value).Centroid);
var centers = allBps.Select(_ => _.centroid);

foreach (var item in centers)
{
Expand Down Expand Up @@ -145,14 +145,12 @@ private void PickZones()
foreach (var item in pickedZoneNodes)
{
item.LoadGeometry(doc);

}
doc.Objects.Delete(nodeIds, true);

//var nds = pickedZoneNodes.Select(_ => new GH_Brep( GenZoneNode(_.Boundingbox.Center)));
var nds = pickedZoneNodes.Select(_ => new GH_Brep(GenZoneNode(_.Boundingbox.Center).ToBrep()));


scopeParm.SetPersistentData(nds);
ExpireSolution(true);

Expand Down

0 comments on commit ce4a3a9

Please sign in to comment.