/***************************************************/ // Create the preview of bars. public static void AddBarPreview(this Rhino.Display.CustomDisplay display, Model model, IEnumerable <int> ids, out HashSet <int> nodeIds, double graphicFactor, double elementFactor, double textFactor, bool showComponentNumbers, bool showElementNumbers, bool showLCS, bool showThk = false, string barResultType = "None", bool showResultValues = false) { nodeIds = new HashSet <int>(); if (model.Bars.Count == 0) { return; } // Take all results out first to determine min & max, otherwise could be done inside the loop. Dictionary <int, double[]> barResults = new Dictionary <int, double[]>(); double BFactor = 0; if (barResultType != "None") { barResults = model.Mesh.GetElementResults(barResultType); double barExtreme = barResults.Values.Select(r => Math.Max(Math.Abs(r[0]), Math.Abs(r[1]))).Max(); BFactor = elementFactor / barExtreme * 10; } ids = ids.Count() == 0 ? Enumerable.Range(0, model.Bars.Count) : ids.Select(i => i - 1); foreach (int i in ids) { Bar bar = model.Bars[i]; Plane locLCS = new Plane(bar.LCS); locLCS.Rotate(bar.Rotation, locLCS.ZAxis); locLCS.Translate(locLCS.XAxis * bar.Offset.X); locLCS.Translate(locLCS.YAxis * bar.Offset.Y); Vector3d hOffset = locLCS.XAxis * (bar.Profile.GetHeight() * 0.5); if (showThk) { hOffset *= 2; } Plane tp = new Plane(locLCS.Origin, bar.LCS.YAxis, Vector3d.ZAxis); tp.Translate(hOffset); Curve[] profileCurves = bar.Profile.ToRhinoProfile(); if (showComponentNumbers) { Rhino.Display.Text3d t = new Rhino.Display.Text3d("Bar " + bar.CCXId(), tp, elementFactor * 0.6 * textFactor); t.Bold = true; display.AddText(t, Color.DarkRed); } foreach (Element element in bar.Elements) { Element1D e = element as Element1D; int eId = e.Id.AsInteger; nodeIds.Add(e.Nodes.First().Id.AsInteger); nodeIds.Add(e.Nodes.Last().Id.AsInteger); Point3d[] endPts = e.GetVertices().ToArray(); Point3d c = (endPts[0] + endPts[1]) * 0.5; display.AddLine(new Line(endPts[0], endPts[1]), Color.Aqua); if (showThk) { Plane eP = new Plane(locLCS); List <Point3d> elementVertices = e.GetVertices().ToList(); eP.Translate(elementVertices[0] - bar.LCS.Origin); Transform ptp = Transform.PlaneToPlane(Plane.WorldXY, eP); // Use DisplayPipeline to get surface preview! Now a temporary solution given. foreach (Curve pc in profileCurves) { Curve opc = pc.DuplicateCurve(); opc.Transform(ptp); display.AddCurve(opc); opc.Translate(elementVertices[1] - elementVertices[0]); display.AddCurve(opc); } } if (barResultType != "None") { Vector3d graphDir; switch (barResultType) { case "Myy": graphDir = locLCS.XAxis; break; case "Mxx": graphDir = locLCS.YAxis; break; default: string barResDir = barResultType.ToString().Substring(1, 1); graphDir = barResDir == "y" ? locLCS.YAxis : locLCS.XAxis; break; } graphDir *= BFactor * graphicFactor; Point3d[] cPts = new Point3d[] { endPts[0], endPts[1], endPts[1] + graphDir * barResults[eId][1], endPts[0] + graphDir * barResults[eId][0] }; display.AddPolygon(cPts, Color.CornflowerBlue, Color.Black, true, true); if (showResultValues && barResults.Count != 0) { Plane tp1 = new Plane(tp); tp1.Translate(endPts[0] - tp1.Origin + graphDir * barResults[eId][0]); Rhino.Display.Text3d t1 = new Rhino.Display.Text3d(barResults[eId][0].ToString("G2"), tp1, elementFactor * 0.4 * textFactor); t1.Bold = true; display.AddText(t1, Color.Black); Plane tp2 = new Plane(tp); tp2.Translate(endPts[1] - tp2.Origin + graphDir * barResults[eId][1]); Rhino.Display.Text3d t2 = new Rhino.Display.Text3d(barResults[eId][1].ToString("G2"), tp2, elementFactor * 0.4 * textFactor); t2.Bold = true; display.AddText(t2, Color.Black); } } if (showLCS) { display.AddVector(c, locLCS.XAxis * elementFactor * graphicFactor, Color.Red); display.AddVector(c, locLCS.YAxis * elementFactor * graphicFactor, Color.Green); display.AddVector(c, locLCS.ZAxis * elementFactor * graphicFactor, Color.Blue); } if (showElementNumbers) { Plane etp = new Plane(tp); etp.Translate(c - bar.LCS.Origin); Rhino.Display.Text3d t = new Rhino.Display.Text3d(e.CCXId(), etp, elementFactor * 0.4 * textFactor); t.Bold = true; display.AddText(t, Color.Black); } } } }
/***************************************************/ // Create the preview of nodes. public static void AddNodePreview(this Rhino.Display.CustomDisplay display, Model model, IEnumerable <int> ids, double graphicFactor, double elementFactor, double textFactor, double forceFactor, bool showNodes, bool showNodeNumbers, bool showLoads, bool showLoadValues, Grasshopper.GUI.Gradient.GH_Gradient gradient = null, string nodeResultType = "None", bool showResultValues = false) { if (model.Mesh == null || model.Mesh.Nodes.Count == 0) { return; } List <double> nodeResults = new List <double>(); double nodeMax = 0; double nodeMin = 0; if (nodeResultType != "None") { nodeResults = model.Mesh.GetNodeDisplacement(nodeResultType); nodeMax = nodeResults.Max(); nodeMin = nodeResults.Min(); } foreach (int i in ids) { Node n = model.Mesh.Nodes[i]; if (!n.Primary) { continue; } if (showNodes) { if (nodeResults.Count > 0) { display.AddPoint(n.Location, gradient.ColourAt((nodeResults[i] - nodeMin) / (nodeMax - nodeMin)), Rhino.Display.PointStyle.Simple, Math.Max(1, Convert.ToInt32(5 * elementFactor * graphicFactor))); } else { display.AddPoint(n.Location, Color.DarkRed, Rhino.Display.PointStyle.Simple, Math.Max(1, Convert.ToInt32(5 * elementFactor * graphicFactor))); } } if (showResultValues && nodeResults.Count > 0) { Plane p = new Plane(n.Location + Vector3d.ZAxis * elementFactor, Vector3d.XAxis, Vector3d.ZAxis); Rhino.Display.Text3d t = new Rhino.Display.Text3d(nodeResults[i].ToString("G2"), p, elementFactor * 0.4 * textFactor); t.Bold = true; display.AddText(t, Color.Black); } if (showLoads && n.ForceLoad.Length != 0) { Vector3d dLoad = n.ForceLoad / forceFactor * elementFactor * 5 * graphicFactor; display.AddVector(n.Location - dLoad, dLoad); if (showLoadValues) { Plane p = new Plane(n.Location - dLoad * 1.1, Vector3d.XAxis, Vector3d.ZAxis); Rhino.Display.Text3d t = new Rhino.Display.Text3d(n.ForceLoad.Length.ToString("G2"), p, elementFactor * 0.4 * textFactor); t.Bold = true; display.AddText(t, Color.Magenta); } } if (showLoads && n.MomentLoad.Length != 0) { Vector3d mLoad = n.MomentLoad; Vector3d axis = new Vector3d(mLoad.X, mLoad.Y, mLoad.Z); Plane ap = new Plane(n.Location, axis); Arc mArc = new Arc(new Circle(ap, 2.5 * elementFactor * graphicFactor), Math.PI * 1.5); display.AddArc(mArc); display.AddVector(mArc.EndPoint, mArc.TangentAt(Math.PI * 1.5)); if (showLoadValues) { Plane p = new Plane(n.Location - n.MomentLoad / forceFactor * elementFactor * 5 * graphicFactor * 1.1, Vector3d.XAxis, Vector3d.ZAxis); Rhino.Display.Text3d t = new Rhino.Display.Text3d(n.MomentLoad.Length.ToString("G2"), p, elementFactor * 0.4 * textFactor); t.Bold = true; display.AddText(t, Color.Magenta); } } if (showNodeNumbers) { Plane p = new Plane(n.Location, Vector3d.XAxis, Vector3d.ZAxis); Rhino.Display.Text3d t = new Rhino.Display.Text3d(n.CCXId(), p, elementFactor * 0.4 * textFactor); t.Bold = true; display.AddText(t, Color.Purple); } } }
/***************************************************/ // Create the preview of panels. public static void AddPanelPreview(this Rhino.Display.CustomDisplay display, Model model, IEnumerable <int> ids, out HashSet <int> nodeIds, double graphicFactor, double elementFactor, double textFactor, double forceFactor, bool showComponentNumbers, bool showElementNumbers, bool showLCS, bool showLoads, bool showLoadValues, bool showEdges, bool showThk = false, Grasshopper.GUI.Gradient.GH_Gradient gradient = null, string panelResultType = "None", bool showResultValues = false) { nodeIds = new HashSet <int>(); if (model.Panels.Count == 0) { return; } // Take all results out first to determine min & max, otherwise could be done inside the loop. Dictionary <int, double> panelTopResults = new Dictionary <int, double>(); Dictionary <int, double> panelBottomResults = new Dictionary <int, double>(); double panelMax = 0; double panelMin = 0; if (panelResultType != "None") { Dictionary <int, double[]> panelResults = model.Mesh.GetElementResults(panelResultType); foreach (KeyValuePair <int, double[]> elResult in panelResults) { int id = elResult.Key; double[] results = elResult.Value; int halfResultCount = results.Length / 2; panelTopResults.Add(id, results.Skip(halfResultCount).Average()); panelBottomResults.Add(id, results.Take(halfResultCount).Average()); } panelMax = Math.Max(panelTopResults.Values.Max(), panelBottomResults.Values.Max()); panelMin = Math.Min(panelTopResults.Values.Min(), panelBottomResults.Values.Min()); } ids = ids.Count() == 0 ? Enumerable.Range(0, model.Panels.Count) : ids.Select(i => i - 1); foreach (int i in ids) { Panel panel = model.Panels[i]; if (showComponentNumbers) { Vector3d fOffset = panel.LCS.ZAxis * 0.002; if (showThk) { fOffset += panel.LCS.ZAxis * 0.5 * panel.Thickness; } Plane fp = new Plane(panel.LCS.Origin + fOffset, panel.LCS.ZAxis); Plane bp = new Plane(panel.LCS.Origin - fOffset, -panel.LCS.ZAxis); Rhino.Display.Text3d ft = new Rhino.Display.Text3d("Panel " + panel.CCXId(), fp, elementFactor * 0.6 * textFactor); Rhino.Display.Text3d bt = new Rhino.Display.Text3d("Panel " + panel.CCXId(), bp, elementFactor * 0.6 * textFactor); ft.Bold = true; bt.Bold = true; display.AddText(ft, Color.DarkRed); display.AddText(bt, Color.DarkRed); } foreach (Element element in panel.Elements) { Element2D e = element as Element2D; int eId = e.Id.AsInteger; foreach (Node n in e.Nodes.Take(e.PrimaryNodeCount)) { nodeIds.Add(n.Id.AsInteger); } Color bottomColor; Color topColor; if (panelResultType == "None") { bottomColor = topColor = Color.Aqua; } else { bottomColor = gradient.ColourAt((panelBottomResults[eId] - panelMin) / (panelMax - panelMin)); topColor = gradient.ColourAt((panelTopResults[eId] - panelMin) / (panelMax - panelMin)); } Point3d centroid = e.GetCentroid(); Vector3d normal = e.GetNormal(); Vector3d minOffset = normal * 0.001; Vector3d offset = showThk ? normal * 0.5 * panel.Thickness : minOffset; List <Point3d> front = new List <Point3d>(); List <Point3d> back = new List <Point3d>(); foreach (Point3d v in e.GetVertices()) { front.Add(v + offset); back.Add(v - offset); } display.AddPolygon(front, topColor, Color.Black, true, showEdges); display.AddPolygon(back, bottomColor, Color.Black, true, showEdges); if (showThk) { front.Add(front[0]); back.Add(back[0]); for (int j = 0; j < front.Count - 1; j++) { List <Point3d> fv = front.GetRange(j, 2); List <Point3d> bv = back.GetRange(j, 2); bv.Reverse(); fv.AddRange(bv); display.AddPolygon(fv, Color.Aqua, Color.Black, true, true); } } Plane tp = new Plane(panel.LCS); tp.Origin = centroid + offset + minOffset; tp.XAxis *= -1; tp.ZAxis *= -1; Plane bp = new Plane(panel.LCS); bp.Origin = centroid - offset - minOffset; if (showResultValues && panelTopResults.Count != 0) { Rhino.Display.Text3d t1 = new Rhino.Display.Text3d(panelTopResults[eId].ToString("G2"), tp, elementFactor * 0.4 * textFactor); t1.Bold = true; display.AddText(t1, Color.Black); Rhino.Display.Text3d t2 = new Rhino.Display.Text3d(panelBottomResults[eId].ToString("G2"), bp, elementFactor * 0.4 * textFactor); t2.Bold = true; display.AddText(t2, Color.Black); } if (showLCS) { display.AddVector(centroid, e.Orientation[0] * elementFactor * graphicFactor, Color.Red); display.AddVector(centroid, e.Orientation[1] * elementFactor * graphicFactor, Color.Green); display.AddVector(centroid, normal * elementFactor * graphicFactor, Color.Blue); } if (showLoads && e.Pressure != 0) { Vector3d pv = normal * e.Pressure / forceFactor * elementFactor * 5 * graphicFactor; List <Point3d> pressurePoints = new List <Point3d>(); foreach (Point3d pt in e.PopulateWithPoints()) { if (e.Pressure > 0) { display.AddVector(pt - pv - offset, pv); } else { display.AddVector(pt - pv + offset, pv); } } if (showLoadValues) { Plane p; if (e.Pressure > 0) { p = new Plane(e.GetCentroid() - pv * 1.1 - offset, Vector3d.XAxis, Vector3d.ZAxis); } else { p = new Plane(e.GetCentroid() - pv * 1.1 + offset, Vector3d.XAxis, Vector3d.ZAxis); } Rhino.Display.Text3d t = new Rhino.Display.Text3d(e.Pressure.ToString("G2"), p, elementFactor * 0.4 * textFactor); t.Bold = true; display.AddText(t, Color.Magenta); } } if (showElementNumbers) { Rhino.Display.Text3d tt = new Rhino.Display.Text3d(e.CCXId(), tp, elementFactor * 0.4 * textFactor); Rhino.Display.Text3d bt = new Rhino.Display.Text3d(e.CCXId(), bp, elementFactor * 0.4 * textFactor); tt.Bold = true; bt.Bold = true; display.AddText(tt, Color.Black); display.AddText(bt, Color.Black); } } } }