Skip to content

Commit

Permalink
GSAGH-561 contour legend in rhino is cropped (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikaLos authored Dec 19, 2024
1 parent bbc49ea commit 9c386d9
Show file tree
Hide file tree
Showing 21 changed files with 1,444 additions and 927 deletions.
202 changes: 68 additions & 134 deletions GsaGH/Components/6_Display/AssemblyResults.cs

Large diffs are not rendered by default.

278 changes: 103 additions & 175 deletions GsaGH/Components/6_Display/Contour1dResults.cs

Large diffs are not rendered by default.

572 changes: 248 additions & 324 deletions GsaGH/Components/6_Display/Contour2dResults.cs

Large diffs are not rendered by default.

227 changes: 77 additions & 150 deletions GsaGH/Components/6_Display/Contour3dResults.cs

Large diffs are not rendered by default.

144 changes: 40 additions & 104 deletions GsaGH/Components/6_Display/ContourNodeResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

using GH_IO.Serialization;

using Grasshopper.GUI;
using Grasshopper.GUI.Gradient;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
Expand Down Expand Up @@ -38,7 +37,6 @@
using OasysUnits;
using OasysUnits.Units;

using Rhino.Display;
using Rhino.Geometry;

using AngleUnit = OasysUnits.Units.AngleUnit;
Expand Down Expand Up @@ -112,25 +110,22 @@ private enum FoldMode {
"SpringForce",
"Footfall",
});
private readonly ContourLegendManager _contourLegendManager = ContourLegendManager.GetDefault();
private string _case = string.Empty;
private double _defScale = 250;
private DisplayValue _disp = DisplayValue.ResXyz;
private ForceUnit _forceUnit = DefaultUnits.ForceUnit;
private Bitmap _legend = new Bitmap(15, 120);
private double _legendScale = 1;
private List<string> _legendValues;
private List<int> _legendValuesPosY;
private LengthUnit _lengthResultUnit = DefaultUnits.LengthUnitResult;
private double _maxValue = 1000;
private double _minValue;
private FoldMode _mode = FoldMode.Displacement;
private MomentUnit _momentUnit = DefaultUnits.MomentUnit;
private int _noDigits;
private string _resType;
private string _scaleLegendTxt = string.Empty;
private bool _showLegend = true;
private bool _slider = true;
private EnvelopeMethod _envelopeType = EnvelopeMethod.Absolute;
private List<(int startY, int endY, Color gradientColor)> _gradients
= new List<(int startY, int endY, Color gradientColor)>();

public ContourNodeResults() : base("Contour Node Results", "NodeContour",
"Diplays GSA Node Results as Contours", CategoryName.Name(), SubCategoryName.Cat6()) { }
Expand All @@ -140,32 +135,14 @@ public override void CreateAttributes() {
InitialiseDropdowns();
}

m_attributes = new DropDownSliderComponentAttributes(this, SetSelected, _dropDownItems,
_selectedItems, _slider, SetVal, SetMaxMin, _defScale, _maxValue, _minValue, _noDigits,
_spacerDescriptions);
m_attributes = new DropDownSliderComponentAttributes(this, SetSelected, _dropDownItems, _selectedItems, _slider,
SetVal, SetMaxMin, _defScale, _maxValue, _minValue, _noDigits, _spacerDescriptions);
}

public override void DrawViewportWires(IGH_PreviewArgs args) {
base.DrawViewportWires(args);
if (!((_legendValues != null) & _showLegend)) {
return;
}

int defaultTextHeight = 12;
args.Display.DrawBitmap(new DisplayBitmap(_legend),
args.Viewport.Bounds.Right - (int)(110 * _legendScale), (int)(20 * _legendScale));
for (int i = 0; i < _legendValues.Count; i++) {
args.Display.Draw2dText(_legendValues[i], Color.Black,
new Point2d(args.Viewport.Bounds.Right - (int)(85 * _legendScale), _legendValuesPosY[i]),
false, (int)(defaultTextHeight * _legendScale));
}

args.Display.Draw2dText(_resType, Color.Black,
new Point2d(args.Viewport.Bounds.Right - (int)(110 * _legendScale),
(int)(7 * _legendScale)), false, (int)(defaultTextHeight * _legendScale));
args.Display.Draw2dText(_case, Color.Black,
new Point2d(args.Viewport.Bounds.Right - (int)(110 * _legendScale),
(int)(145 * _legendScale)), false, (int)(defaultTextHeight * _legendScale));
_contourLegendManager.Legend.DrawLegendRectangle(args, _resType, _case, _gradients);
}

public override bool Read(GH_IReader reader) {
Expand All @@ -176,20 +153,17 @@ public override bool Read(GH_IReader reader) {
_maxValue = reader.GetDouble("valMax");
_minValue = reader.GetDouble("valMin");
_defScale = reader.GetDouble("val");
if (reader.ItemExists("legendScale")) {
_legendScale = reader.GetDouble("legendScale");
}

if (reader.ItemExists("envelope")) {
_envelopeType = (EnvelopeMethod)Enum.Parse(
typeof(EnvelopeMethod), reader.GetString("envelope"));
_envelopeType = (EnvelopeMethod)Enum.Parse(typeof(EnvelopeMethod), reader.GetString("envelope"));
}

_showLegend = reader.GetBoolean("legend");
_lengthResultUnit = (LengthUnit)UnitsHelper.Parse(typeof(LengthUnit), reader.GetString("length"));
_forceUnit = (ForceUnit)UnitsHelper.Parse(typeof(ForceUnit), reader.GetString("force"));
_momentUnit = (MomentUnit)UnitsHelper.Parse(typeof(MomentUnit), reader.GetString("moment"));
_legend = new Bitmap((int)(15 * _legendScale), (int)(120 * _legendScale));

_contourLegendManager.DeserialiseLegendState(reader);

return base.Read(reader);
}

Expand Down Expand Up @@ -309,12 +283,13 @@ public override bool Write(GH_IWriter writer) {
writer.SetDouble("valMax", _maxValue);
writer.SetDouble("valMin", _minValue);
writer.SetDouble("val", _defScale);
writer.SetDouble("legendScale", _legendScale);
writer.SetBoolean("legend", _showLegend);
writer.SetString("length", Length.GetAbbreviation(_lengthResultUnit));
writer.SetString("force", Force.GetAbbreviation(_forceUnit));
writer.SetString("moment", Moment.GetAbbreviation(_momentUnit));
writer.SetString("envelope", _envelopeType.ToString());

_contourLegendManager.SerialiseLegendState(writer);

return base.Write(writer);
}

Expand All @@ -328,19 +303,18 @@ protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown men
ToolStripMenuItem envelopeMenu = GenerateToolStripMenuItem.GetEnvelopeSubMenuItem(_envelopeType, UpdateEnvelope);
menu.Items.Add(envelopeMenu);

Menu_AppendItem(menu, "Show Legend", ShowLegend, true, _showLegend);
Menu_AppendItem(menu, "Show Legend", ShowLegend, true, _contourLegendManager.Configuration.IsVisible);

var gradient = new GH_GradientControl();
gradient.CreateAttributes();
var extract = new ToolStripMenuItem("Extract Default Gradient", gradient.Icon_24x24,
(s, e) => CreateGradient());
var extract = new ToolStripMenuItem("Extract Default Gradient", gradient.Icon_24x24, (s, e) => CreateGradient());
menu.Items.Add(extract);

ToolStripMenuItem lengthUnitsMenu = GenerateToolStripMenuItem.GetSubMenuItem("Displacement",
EngineeringUnits.Length, Length.GetAbbreviation(_lengthResultUnit), UpdateLength);

ToolStripMenuItem forceUnitsMenu = GenerateToolStripMenuItem.GetSubMenuItem("Force",
EngineeringUnits.Force, Force.GetAbbreviation(_forceUnit), UpdateForce);
ToolStripMenuItem forceUnitsMenu = GenerateToolStripMenuItem.GetSubMenuItem("Force", EngineeringUnits.Force,
Force.GetAbbreviation(_forceUnit), UpdateForce);

ToolStripMenuItem momentUnitsMenu = GenerateToolStripMenuItem.GetSubMenuItem("Moment",
EngineeringUnits.Moment, Moment.GetAbbreviation(_momentUnit), UpdateMoment);
Expand All @@ -357,21 +331,7 @@ protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown men

menu.Items.Add(unitsMenu);

var legendScale = new ToolStripTextBox {
Text = _legendScale.ToString(),
};
legendScale.TextChanged += (s, e) => MaintainScaleLegendText(legendScale);
var legendScaleMenu = new ToolStripMenuItem("Scale Legend") {
Enabled = true,
ImageScaling = ToolStripItemImageScaling.SizeToFit,
};
var menu2 = new GH_MenuCustomControl(legendScaleMenu.DropDown, legendScale.Control, true,
200);
legendScaleMenu.DropDownItems[1].MouseUp += (s, e) => {
UpdateLegendScale();
(this as IGH_VariableParameterComponent).VariableParameterMaintenance();
ExpireSolution(true);
};
ToolStripMenuItem legendScaleMenu = _contourLegendManager.CreateMenu(this, () => base.UpdateUI());
menu.Items.Add(legendScaleMenu);

Menu_AppendSeparator(menu);
Expand Down Expand Up @@ -436,21 +396,20 @@ protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
IQuantity length = new Length(0, _lengthResultUnit);
string lengthunitAbbreviation = string.Concat(length.ToString().Where(char.IsLetter));

pManager.AddGenericParameter("Result Point", "P", "Contoured Points with result values",
GH_ParamAccess.list);
pManager.AddGenericParameter("Result Point", "P", "Contoured Points with result values", GH_ParamAccess.list);
pManager.AddGenericParameter("Colours", "LC", "Legend Colours", GH_ParamAccess.list);
pManager.AddGenericParameter("Values [" + lengthunitAbbreviation + "]", "LT", "Legend Values",
GH_ParamAccess.list);
}

protected override void SolveInternal(IGH_DataAccess da) {
GsaResult result;
_gradients = new List<(int startY, int endY, Color gradientColor)>();
string nodeList = "All";
_case = string.Empty;
_resType = string.Empty;
var ghTyp = new GH_ObjectWrapper();
da.GetData(0, ref ghTyp);
result = Inputs.GetResultInput(this, ghTyp);
GsaResult result = Inputs.GetResultInput(this, ghTyp);
if (result == null) {
return;
}
Expand Down Expand Up @@ -805,10 +764,9 @@ IEntity0dResultSubset<IReactionForce, ResultVector6<Entity0dExtremaKey>> springF
pts[kvp.Key] = new PointResultGoo(pt, t, valcol, size, kvp.Key);
});


int gripheight = _legend.Height / ghGradient.GripCount;
_legendValues = new List<string>();
_legendValuesPosY = new List<int>();
int gripHeight = _contourLegendManager.Configuration.ActualHeight / ghGradient.GripCount;
var legendValues = new List<string>();
var _legendValuePositionsY = new List<int>();

var ts = new List<GH_UnitNumber>();
var cs = new List<Color>();
Expand All @@ -823,60 +781,56 @@ IEntity0dResultSubset<IReactionForce, ResultVector6<Entity0dExtremaKey>> springF
t = Math.Round(t, significantDigits);
}

Color gradientcolour
= ghGradient.ColourAt((2 * (double)i / ((double)ghGradient.GripCount - 1)) - 1);
cs.Add(gradientcolour);
Color gradientColor = ghGradient.ColourAt((2 * (double)i / ((double)ghGradient.GripCount - 1)) - 1);
cs.Add(gradientColor);

int starty = i * gripheight;
int endy = starty + gripheight;
for (int y = starty; y < endy; y++) {
for (int x = 0; x < _legend.Width; x++) {
_legend.SetPixel(x, _legend.Height - y - 1, gradientcolour);
}
}
int startY = i * gripHeight;
int endY = startY + gripHeight;
_gradients.Add((startY, endY, gradientColor));

switch (_mode) {
case FoldMode.Displacement when (int)_disp < 4:
var displacement = new Length(t, _lengthResultUnit);
_legendValues.Add(displacement.ToString("f" + significantDigits));
legendValues.Add(displacement.ToString("f" + significantDigits));
ts.Add(new GH_UnitNumber(displacement));
break;

case FoldMode.Displacement:
var rotation = new Angle(t, AngleUnit.Radian);
_legendValues.Add(rotation.ToString("s" + significantDigits));
legendValues.Add(rotation.ToString("s" + significantDigits));
ts.Add(new GH_UnitNumber(rotation));
break;

case FoldMode.Reaction when (int)_disp < 4:
case FoldMode.SpringForce when (int)_disp < 4:
var reactionForce = new Force(t, _forceUnit);
_legendValues.Add(reactionForce.ToString("s" + significantDigits));
legendValues.Add(reactionForce.ToString("s" + significantDigits));
ts.Add(new GH_UnitNumber(reactionForce));
break;

case FoldMode.Reaction:
case FoldMode.SpringForce:
var reactionMoment = new Moment(t, _momentUnit);
_legendValues.Add(reactionMoment.ToString("s" + significantDigits));
legendValues.Add(reactionMoment.ToString("s" + significantDigits));
ts.Add(new GH_UnitNumber(reactionMoment));
break;

case FoldMode.Footfall:
var responseFactor = new Ratio(t, RatioUnit.DecimalFraction);
_legendValues.Add(responseFactor.ToString("s" + significantDigits));
legendValues.Add(responseFactor.ToString("s" + significantDigits));
ts.Add(new GH_UnitNumber(responseFactor));
break;
}

if (Math.Abs(t) > 1) {
_legendValues[i] = _legendValues[i]
.Replace(",", string.Empty); // remove thousand separator
legendValues[i] = legendValues[i].Replace(",", string.Empty); // remove thousand separator
}

_legendValuesPosY.Add(_legend.Height - starty + (gripheight / 2) - 2);
_legendValuePositionsY.Add(_contourLegendManager.Configuration.ActualHeight - startY + (gripHeight / 2) - 2);
}

_contourLegendManager.Configuration.SetTextValues(legendValues).SetValuePositionsY(_legendValuePositionsY);

da.SetDataList(0, pts.OrderBy(x => x.Key).Select(y => y.Value).ToList());
da.SetDataList(1, cs);
da.SetDataList(2, ts);
Expand All @@ -903,8 +857,7 @@ internal GH_GradientControl CreateGradient(GH_Document doc = null) {
1,
});

gradient.Attributes.Pivot = new PointF(
Attributes.Bounds.X - gradient.Attributes.Bounds.Width - 50,
gradient.Attributes.Pivot = new PointF(Attributes.Bounds.X - gradient.Attributes.Bounds.Width - 50,
Params.Input[2].Attributes.Bounds.Y - (gradient.Attributes.Bounds.Height / 4) - 6);

doc.AddObject(gradient, false);
Expand Down Expand Up @@ -956,7 +909,7 @@ private void ReDrawComponent() {
}

internal void ShowLegend(object sender, EventArgs e) {
_showLegend = !_showLegend;
_contourLegendManager.Configuration.ToggleLegendVisibility();
ExpirePreview(true);
}

Expand All @@ -983,22 +936,5 @@ internal void UpdateMoment(string unit) {
ExpirePreview(true);
base.UpdateUI();
}

internal void UpdateLegendScale() {
try {
_legendScale = double.Parse(_scaleLegendTxt);
} catch (Exception e) {
this.AddRuntimeWarning(e.Message);
return;
}

_legend = new Bitmap((int)(15 * _legendScale), (int)(120 * _legendScale));
ExpirePreview(true);
base.UpdateUI();
}

internal void MaintainScaleLegendText(ToolStripItem menuitem) {
_scaleLegendTxt = menuitem.Text;
}
}
}
Loading

0 comments on commit 9c386d9

Please sign in to comment.