Skip to content

Commit

Permalink
test split
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikaLos committed Dec 4, 2024
1 parent 8811ab3 commit 37f6af6
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 327 deletions.
178 changes: 16 additions & 162 deletions GsaGH/Components/6_Display/Contour3dResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

using Grasshopper.GUI.Gradient;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Parameters;
using Grasshopper.Kernel.Special;
using Grasshopper.Kernel.Types;

using GsaAPI;
Expand All @@ -27,9 +25,7 @@
using GsaGH.Properties;

using OasysGH;
using OasysGH.Components;
using OasysGH.Parameters;
using OasysGH.UI;
using OasysGH.Units;
using OasysGH.Units.Helpers;

Expand All @@ -46,7 +42,7 @@ namespace GsaGH.Components {
/// <summary>
/// Component to get Element3d results
/// </summary>
public class Contour3dResults : GH_OasysDropDownComponent {
public class Contour3dResults : DrawContourBase {
private enum DisplayValue {
X,
Y,
Expand Down Expand Up @@ -85,65 +81,22 @@ private enum FoldMode {
"Displacement",
"Stress",
});
private string _case = string.Empty;
private double _defScale = 250;

private DisplayValue _disp = DisplayValue.ResXyz;
private LengthUnit _lengthResultUnit = DefaultUnits.LengthUnitResult;
private double _maxValue = 1000;
private double _minValue;
private FoldMode _mode = FoldMode.Displacement;
private int _noDigits;
private string _resType;
private bool _slider = true;
private PressureUnit _stressUnitResult = DefaultUnits.StressUnitResult;
private EnvelopeMethod _envelopeType = EnvelopeMethod.Absolute;
private readonly ContourLegendManager _contourLegendMenager = new ContourLegendManager();
private List<(int startY, int endY, Color gradientColor)> _gradients
= new List<(int startY, int endY, Color gradientColor)>();

public Contour3dResults() : base("Contour 3D Results", "Contour3D", "Displays GSA 3D Element Results as Contour",
CategoryName.Name(), SubCategoryName.Cat6()) { }

public override void CreateAttributes() {
if (!_isInitialised) {
InitialiseDropdowns();
}

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);
_contourLegendMenager.DrawLegend(args, _resType, _case, _gradients);
}

public override bool Read(GH_IReader reader) {
_mode = (FoldMode)reader.GetInt32("Mode");
_disp = (DisplayValue)reader.GetInt32("Display");
_slider = reader.GetBoolean("slider");
_noDigits = reader.GetInt32("noDec");
_maxValue = reader.GetDouble("valMax");
_minValue = reader.GetDouble("valMin");
_defScale = reader.GetDouble("val");

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

_lengthResultUnit = (LengthUnit)UnitsHelper.Parse(typeof(LengthUnit), reader.GetString("length"));
_stressUnitResult = (PressureUnit)UnitsHelper.Parse(typeof(PressureUnit), reader.GetString("stress"));

_contourLegendMenager.DeserialiseLegendState(reader);

return base.Read(reader);
}

public void SetMaxMin(double max, double min) {
_maxValue = max;
_minValue = min;
}

public override void SetSelected(int i, int j) {
_selectedItems[i] = _dropDownItems[i][j];
switch (i) {
Expand Down Expand Up @@ -203,10 +156,6 @@ public override void SetSelected(int i, int j) {
base.UpdateUI();
}

public void SetVal(double value) {
_defScale = value;
}

public override void VariableParameterMaintenance() {
if (Params.Input.Count != 4) {
Params.RegisterInputParam(new Param_Interval());
Expand Down Expand Up @@ -235,56 +184,11 @@ public override void VariableParameterMaintenance() {
public override bool Write(GH_IWriter writer) {
writer.SetInt32("Mode", (int)_mode);
writer.SetInt32("Display", (int)_disp);
writer.SetBoolean("slider", _slider);
writer.SetInt32("noDec", _noDigits);
writer.SetDouble("valMax", _maxValue);
writer.SetDouble("valMin", _minValue);
writer.SetDouble("val", _defScale);
writer.SetString("length", Length.GetAbbreviation(_lengthResultUnit));
writer.SetString("stress", Pressure.GetAbbreviation(_stressUnitResult));
writer.SetString("envelope", _envelopeType.ToString());

_contourLegendMenager.SerialiseLegendState(writer);

return base.Write(writer);
}

protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu) {
if (!(menu is ContextMenuStrip)) {
return; // this method is also called when clicking EWR balloon
}

Menu_AppendSeparator(menu);

ToolStripMenuItem envelopeMenu = GenerateToolStripMenuItem.GetEnvelopeSubMenuItem(_envelopeType, UpdateEnvelope);
menu.Items.Add(envelopeMenu);

Menu_AppendItem(menu, "Show Legend", ShowLegend, true, _contourLegendMenager.IsLegendVisible);

var gradient = new GH_GradientControl();
gradient.CreateAttributes();
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 stressUnitsMenu = GenerateToolStripMenuItem.GetSubMenuItem("Stress", EngineeringUnits.Stress,
Pressure.GetAbbreviation(_stressUnitResult), UpdateStress);

var unitsMenu = new ToolStripMenuItem("Select Units", Resources.ModelUnits);
unitsMenu.DropDownItems.AddRange(new ToolStripItem[] {
lengthUnitsMenu,
stressUnitsMenu,
});
unitsMenu.ImageScaling = ToolStripItemImageScaling.SizeToFit;
menu.Items.Add(unitsMenu);
ToolStripMenuItem legendScaleMenu = _contourLegendMenager.CreateMenu(this, () => base.UpdateUI());
menu.Items.Add(legendScaleMenu);

Menu_AppendSeparator(menu);
}

protected override void BeforeSolveInstance() {
switch (_mode) {
case FoldMode.Displacement:
Expand Down Expand Up @@ -316,6 +220,17 @@ protected override void InitialiseDropdowns() {
_isInitialised = true;
}

protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu) {
ToolStripMenuItem stressUnitsMenu = GenerateToolStripMenuItem.GetSubMenuItem("Stress", EngineeringUnits.Stress,
Pressure.GetAbbreviation(_stressUnitResult), UpdateStress);

GenerateUnitMenuItems(new List<ToolStripMenuItem>() {
stressUnitsMenu,
});

base.AppendAdditionalComponentMenuItems(menu);
}

protected override void RegisterInputParams(GH_InputParamManager pManager) {
pManager.AddParameter(new GsaResultParameter(), "Result", "Res", "GSA Result", GH_ParamAccess.item);
pManager.AddParameter(new GsaElementMemberListParameter());
Expand All @@ -330,17 +245,12 @@ protected override void RegisterInputParams(GH_InputParamManager pManager) {
}

protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
IQuantity length = new Length(0, _lengthResultUnit);
string lengthunitAbbreviation = string.Concat(length.ToString().Where(char.IsLetter));

pManager.AddGenericParameter("Ngon Mesh", "M", "Ngon Mesh with coloured result values", GH_ParamAccess.item);
pManager.AddGenericParameter("Colours", "LC", "Legend Colours", GH_ParamAccess.list);
pManager.AddGenericParameter("Values [" + lengthunitAbbreviation + "]", "LT", "Legend Values",
GH_ParamAccess.list);
base.RegisterOutputParams(pManager);
}

protected override void SolveInternal(IGH_DataAccess da) {
_gradients = new List<(int startY, int endY, Color gradientColor)>();
ClearLegendGradients();
GsaResult result;
string elementlist = "All";
var ghTyp = new GH_ObjectWrapper();
Expand Down Expand Up @@ -626,8 +536,7 @@ IMeshResultSubset<IMeshQuantity<IStress>, IStress, ResultTensor3<Entity2dExtrema
legendValuePositionsY.Add(_contourLegendMenager.GetBitmapSize().Height - starty + (gripheight / 2) - 2);
}

_contourLegendMenager.SetTextValues(legendValues);
_contourLegendMenager.SetPositionYValues(legendValuePositionsY);
AddLegendValues(legendValues, legendValuePositionsY);

da.SetData(0, resultMeshes);
da.SetDataList(1, cs);
Expand All @@ -636,36 +545,6 @@ IMeshResultSubset<IMeshQuantity<IStress>, IStress, ResultTensor3<Entity2dExtrema
PostHog.Result(result.CaseType, 3, _mode.ToString(), _disp.ToString());
}

internal GH_GradientControl CreateGradient(GH_Document doc = null) {
doc ??= OnPingDocument();
var gradient = new GH_GradientControl();
gradient.CreateAttributes();

gradient.Gradient = Colours.Stress_Gradient();
gradient.Gradient.NormalizeGrips();
gradient.Params.Input[0].AddVolatileData(new GH_Path(0), 0, -1);
gradient.Params.Input[1].AddVolatileData(new GH_Path(0), 0, 1);
gradient.Params.Input[2].AddVolatileDataList(new GH_Path(0), new List<double>() {
-1,
-0.666,
-0.333,
0,
0.333,
0.666,
1,
});

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);
Params.Input[2].RemoveAllSources();
Params.Input[2].AddSource(gradient.Params.Output[0]);

UpdateUI();
return gradient;
}

private void DeformationModeClicked() {
RecordUndoEvent(_mode + " Parameters");
_mode = FoldMode.Displacement;
Expand All @@ -674,25 +553,6 @@ private void DeformationModeClicked() {
ReDrawComponent();
}

private void ReDrawComponent() {
var pivot = new PointF(Attributes.Pivot.X, Attributes.Pivot.Y);
CreateAttributes();
Attributes.Pivot = pivot;
Attributes.ExpireLayout();
Attributes.PerformLayout();
}

internal void ShowLegend(object sender, EventArgs e) {
_contourLegendMenager.ToggleVisibility();
ExpirePreview(true);
}

internal void UpdateEnvelope(string type) {
_envelopeType = (EnvelopeMethod)Enum.Parse(typeof(EnvelopeMethod), type);
ExpirePreview(true);
base.UpdateUI();
}

private void StressModeClicked() {
RecordUndoEvent(_mode + " Parameters");
_mode = FoldMode.Stress;
Expand All @@ -701,12 +561,6 @@ private void StressModeClicked() {
ReDrawComponent();
}

internal void UpdateLength(string unit) {
_lengthResultUnit = (LengthUnit)UnitsHelper.Parse(typeof(LengthUnit), unit);
ExpirePreview(true);
base.UpdateUI();
}

internal void UpdateStress(string unit) {
_stressUnitResult = (PressureUnit)UnitsHelper.Parse(typeof(PressureUnit), unit);
ExpirePreview(true);
Expand Down
Loading

0 comments on commit 37f6af6

Please sign in to comment.