From 37f6af6f7bac340e88e49bb69133d749b7b4ab8f Mon Sep 17 00:00:00 2001 From: DominikaLos Date: Wed, 4 Dec 2024 10:02:10 +0100 Subject: [PATCH] test split --- .../Components/6_Display/Contour3dResults.cs | 178 ++------------ .../6_Display/ContourNodeResults.cs | 173 +------------- GsaGH/Helpers/GH/DrawContourBase.cs | 221 ++++++++++++++++++ 3 files changed, 245 insertions(+), 327 deletions(-) create mode 100644 GsaGH/Helpers/GH/DrawContourBase.cs diff --git a/GsaGH/Components/6_Display/Contour3dResults.cs b/GsaGH/Components/6_Display/Contour3dResults.cs index fae9d34a6..b472fc351 100644 --- a/GsaGH/Components/6_Display/Contour3dResults.cs +++ b/GsaGH/Components/6_Display/Contour3dResults.cs @@ -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; @@ -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; @@ -46,7 +42,7 @@ namespace GsaGH.Components { /// /// Component to get Element3d results /// - public class Contour3dResults : GH_OasysDropDownComponent { + public class Contour3dResults : DrawContourBase { private enum DisplayValue { X, Y, @@ -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) { @@ -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()); @@ -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: @@ -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() { + 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()); @@ -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(); @@ -626,8 +536,7 @@ IMeshResultSubset, IStress, ResultTensor3, IStress, ResultTensor3() { - -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; @@ -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; @@ -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); diff --git a/GsaGH/Components/6_Display/ContourNodeResults.cs b/GsaGH/Components/6_Display/ContourNodeResults.cs index 427e8d0a2..29764f1bd 100644 --- a/GsaGH/Components/6_Display/ContourNodeResults.cs +++ b/GsaGH/Components/6_Display/ContourNodeResults.cs @@ -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; @@ -28,9 +26,7 @@ using GsaGH.Properties; using OasysGH; -using OasysGH.Components; using OasysGH.Parameters; -using OasysGH.UI; using OasysGH.Units; using OasysGH.Units.Helpers; @@ -47,7 +43,7 @@ namespace GsaGH.Components { /// /// Component to display GSA node result contours /// - public class ContourNodeResults : GH_OasysDropDownComponent { + public class ContourNodeResults : DrawContourBase { private enum DisplayValue { X, Y, @@ -110,70 +106,23 @@ private enum FoldMode { "SpringForce", "Footfall", }); - private string _case = string.Empty; - private double _defScale = 250; private DisplayValue _disp = DisplayValue.ResXyz; private ForceUnit _forceUnit = DefaultUnits.ForceUnit; - 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 bool _slider = true; - 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 ContourNodeResults() : base("Contour Node Results", "NodeContour", "Diplays GSA Node Results as Contours", 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")); _forceUnit = (ForceUnit)UnitsHelper.Parse(typeof(ForceUnit), reader.GetString("force")); _momentUnit = (MomentUnit)UnitsHelper.Parse(typeof(MomentUnit), reader.GetString("moment")); - _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) { switch (i) { case 0: @@ -235,10 +184,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 != 5) { var scale = (Param_Number)Params.Input[3]; @@ -280,64 +225,24 @@ 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("force", Force.GetAbbreviation(_forceUnit)); writer.SetString("moment", Moment.GetAbbreviation(_momentUnit)); - 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 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); - var unitsMenu = new ToolStripMenuItem("Select Units", Resources.ModelUnits); - - unitsMenu.DropDownItems.AddRange(new ToolStripItem[] { - lengthUnitsMenu, + GenerateUnitMenuItems(new List() { forceUnitsMenu, momentUnitsMenu, }); - - unitsMenu.ImageScaling = ToolStripItemImageScaling.SizeToFit; - - menu.Items.Add(unitsMenu); - - ToolStripMenuItem legendScaleMenu = _contourLegendMenager.CreateMenu(this, () => base.UpdateUI()); - menu.Items.Add(legendScaleMenu); - - Menu_AppendSeparator(menu); + base.AppendAdditionalComponentMenuItems(menu); } protected override void BeforeSolveInstance() { @@ -396,18 +301,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("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); + pManager.AddGenericParameter("Result Point", "P", "Contoured Points with result values", GH_ParamAccess.list); + base.RegisterOutputParams(pManager); } protected override void SolveInternal(IGH_DataAccess da) { - _gradients = new List<(int startY, int endY, Color gradientColor)>(); + ClearLegendGradients(); string nodeList = "All"; _case = string.Empty; _resType = string.Empty; @@ -844,37 +743,6 @@ Color gradientcolour PostHog.Result(result.CaseType, 0, _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() { - -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 DisplacementModeClicked() { RecordUndoEvent(_mode + " Parameters"); _mode = FoldMode.Displacement; @@ -907,37 +775,12 @@ private void FootfallModeClicked() { 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(); - } - internal void UpdateForce(string unit) { _forceUnit = (ForceUnit)UnitsHelper.Parse(typeof(ForceUnit), unit); ExpirePreview(true); base.UpdateUI(); } - internal void UpdateLength(string unit) { - _lengthResultUnit = (LengthUnit)UnitsHelper.Parse(typeof(LengthUnit), unit); - ExpirePreview(true); - base.UpdateUI(); - } - internal void UpdateMoment(string unit) { _momentUnit = (MomentUnit)UnitsHelper.Parse(typeof(MomentUnit), unit); ExpirePreview(true); diff --git a/GsaGH/Helpers/GH/DrawContourBase.cs b/GsaGH/Helpers/GH/DrawContourBase.cs new file mode 100644 index 000000000..d21e6d466 --- /dev/null +++ b/GsaGH/Helpers/GH/DrawContourBase.cs @@ -0,0 +1,221 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Windows.Forms; + +using GH_IO.Serialization; + +using Grasshopper.Kernel; +using Grasshopper.Kernel.Data; +using Grasshopper.Kernel.Special; + +using GsaGH.Helpers.Graphics; +using GsaGH.Parameters.Results; +using GsaGH.Properties; + +using OasysGH; +using OasysGH.Components; +using OasysGH.UI; +using OasysGH.Units; +using OasysGH.Units.Helpers; + +using OasysUnits; +using OasysUnits.Units; + +namespace GsaGH.Helpers.GH { + public class DrawContourBase : GH_OasysDropDownComponent { + public DrawContourBase(string name, string nickname, string description, string category, string subCategory) : + base(name, nickname, description, category, subCategory) { } + + public override Guid ComponentGuid { get; } + public override OasysPluginInfo PluginInfo { get; } + + protected readonly ContourLegendManager _contourLegendMenager = new ContourLegendManager(); + protected List<(int startY, int endY, Color gradientColor)> _gradients + = new List<(int startY, int endY, Color gradientColor)>(); + protected string _resType; + protected string _case = string.Empty; + protected double _defScale = 250; + protected double _maxValue = 1000; + protected double _minValue; + protected int _noDigits; + protected bool _slider = true; + protected LengthUnit _lengthResultUnit = DefaultUnits.LengthUnitResult; + protected EnvelopeMethod _envelopeType = EnvelopeMethod.Absolute; + protected List extraUnitMenus = new List(); + + 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("Colours", "LC", "Legend Colours", GH_ParamAccess.list); + pManager.AddGenericParameter("Values [" + lengthunitAbbreviation + "]", "LT", "Legend Values", + GH_ParamAccess.list); + } + + public override bool Read(GH_IReader reader) { + _slider = reader.GetBoolean("slider"); + _noDigits = reader.GetInt32("noDec"); + _maxValue = reader.GetDouble("valMax"); + _minValue = reader.GetDouble("valMin"); + _defScale = reader.GetDouble("val"); + _contourLegendMenager.DeserialiseLegendState(reader); + if (reader.ItemExists("envelope")) { + _envelopeType = (EnvelopeMethod)Enum.Parse(typeof(EnvelopeMethod), reader.GetString("envelope")); + } + + _lengthResultUnit = (LengthUnit)UnitsHelper.Parse(typeof(LengthUnit), reader.GetString("length")); + + return base.Read(reader); + } + + public override bool Write(GH_IWriter writer) { + 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("envelope", _envelopeType.ToString()); + _contourLegendMenager.SerialiseLegendState(writer); + return base.Write(writer); + } + + 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 SetSelected(int i, int j) { } + + protected override void SolveInternal(IGH_DataAccess da) { } + + protected override void InitialiseDropdowns() { } + + public override void DrawViewportWires(IGH_PreviewArgs args) { + base.DrawViewportWires(args); + _contourLegendMenager.DrawLegend(args, _resType, _case, _gradients); + } + + public void SetVal(double value) { + _defScale = value; + } + + public void SetMaxMin(double max, double min) { + _maxValue = max; + _minValue = min; + } + + public void ClearLegendGradients() { + _gradients = new List<(int startY, int endY, Color gradientColor)>(); + } + + public void AddLegendValues(List values, List positionsY) { + _contourLegendMenager.SetTextValues(values); + _contourLegendMenager.SetPositionYValues(positionsY); + } + + public ToolStripMenuItem GetLegendMenu() { + return _contourLegendMenager.CreateMenu(this, () => base.UpdateUI()); + } + + 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); + + var unitsMenu = new ToolStripMenuItem("Select Units", Resources.ModelUnits); + unitsMenu.DropDownItems.AddRange(new ToolStripItem[] { + lengthUnitsMenu, + }); + unitsMenu.DropDownItems.AddRange(extraUnitMenus.ToArray()); + unitsMenu.ImageScaling = ToolStripItemImageScaling.SizeToFit; + menu.Items.Add(unitsMenu); + ToolStripMenuItem legendScaleMenu = GetLegendMenu(); + menu.Items.Add(legendScaleMenu); + + Menu_AppendSeparator(menu); + } + + internal void GenerateUnitMenuItems(List items) { + extraUnitMenus.AddRange(items); + } + + 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() { + -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; + } + + 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(); + } + + internal void UpdateLength(string unit) { + _lengthResultUnit = (LengthUnit)UnitsHelper.Parse(typeof(LengthUnit), unit); + ExpirePreview(true); + base.UpdateUI(); + } + + protected void ReDrawComponent() { + var pivot = new PointF(Attributes.Pivot.X, Attributes.Pivot.Y); + CreateAttributes(); + Attributes.Pivot = pivot; + Attributes.ExpireLayout(); + Attributes.PerformLayout(); + } + } +}