Skip to content

Commit d77af01

Browse files
authored
Merge pull request #87 from arup-group/task/GSAGH-363-refactor-params
GSAGH-363-refactor-params
2 parents 33d57a4 + bdad358 commit d77af01

24 files changed

+95
-640
lines changed

GH_UnitNumber/Components/ConvertUnitNumber.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
using System.Windows.Forms;
66
using Grasshopper.Kernel;
77
using Grasshopper.Kernel.Special;
8-
using Grasshopper.Kernel.Types;
8+
using Grasshopper.Kernel.Types;
99
using OasysGH;
1010
using OasysGH.Components;
1111
using OasysUnits;
1212

13-
namespace GH_UnitNumber.Components {
14-
public class ConvertUnitNumber : GH_OasysDropDownComponent {
13+
namespace GH_UnitNumber.Components {
14+
public class ConvertUnitNumber : GH_OasysDropDownComponent {
1515
// This region handles how the component in displayed on the ribbon
1616
// including name, exposure level and icon
1717
public override Guid ComponentGuid => new Guid("267b3293-f4ac-48ab-ab66-2d194c86aa52");
@@ -22,7 +22,7 @@ public class ConvertUnitNumber : GH_OasysDropDownComponent {
2222
private bool _comingFromSave = false;
2323
private OasysGH.Parameters.GH_UnitNumber _convertedUnitNumber;
2424
private Enum _selectedUnit;
25-
private Dictionary<string, Enum> _unitDictionary;
25+
private Dictionary<string, Enum> _unitDictionary;
2626

2727
public ConvertUnitNumber() : base("Convert UnitNumber",
2828
"ConvertUnit",
@@ -31,7 +31,7 @@ public ConvertUnitNumber() : base("Convert UnitNumber",
3131
"Util") {
3232
Hidden = true; // sets the initial state of the component to hidden
3333
}
34-
34+
3535
public override void AppendAdditionalMenuItems(ToolStripDropDown menu) {
3636
Menu_AppendSeparator(menu);
3737

@@ -84,7 +84,7 @@ public void CreateTextPanel() {
8484
}
8585

8686
public void CreateValueList() {
87-
string name = _convertedUnitNumber.Value.QuantityInfo.Name + " Units";
87+
string name = _convertedUnitNumber.Value.QuantityInfo.Name + " Units";
8888
var values = _unitDictionary.Keys.ToList();
8989
float x = Attributes.Bounds.X;
9090
float y = Params.Input[1].Attributes.Bounds.Y;
@@ -139,8 +139,8 @@ protected override void RegisterInputParams(GH_InputParamManager pManager) {
139139
protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
140140
pManager.AddParameter(new GH_UnitNumberParameter());
141141
}
142-
143-
protected override void SolveInstance(IGH_DataAccess DA) {
142+
143+
protected override void SolveInternal(IGH_DataAccess DA) {
144144
// get input
145145
OasysGH.Parameters.GH_UnitNumber inUnitNumber = null;
146146

@@ -197,12 +197,12 @@ protected override void SolveInstance(IGH_DataAccess DA) {
197197
// convert unit to selected output
198198
_convertedUnitNumber = new OasysGH.Parameters.GH_UnitNumber(inUnitNumber.Value.ToUnit(_selectedUnit));
199199

200-
OasysGH.Helpers.Output.SetItem(this, DA, 0, _convertedUnitNumber);
200+
DA.SetData(0, _convertedUnitNumber);
201201
}
202-
202+
203203
protected override void UpdateUIFromSelectedItems() {
204204
_comingFromSave = true;
205205
base.UpdateUIFromSelectedItems();
206-
}
206+
}
207207
}
208208
}

GH_UnitNumber/Components/CreateUnitNumber.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
181181
pManager.AddParameter(new GH_UnitNumberParameter());
182182
}
183183

184-
protected override void SolveInstance(IGH_DataAccess DA) {
184+
protected override void SolveInternal(IGH_DataAccess DA) {
185185
if (DA.GetData(0, ref _val)) {
186186
var unit = (EngineeringUnits)Enum.Parse(typeof(EngineeringUnits), _selectedItems[0]);
187187

@@ -281,7 +281,7 @@ protected override void SolveInstance(IGH_DataAccess DA) {
281281

282282
var unitNumber = new OasysGH.Parameters.GH_UnitNumber(_quantity);
283283

284-
OasysGH.Helpers.Output.SetItem(this, DA, 0, unitNumber);
284+
DA.SetData(0, unitNumber);
285285
}
286286
}
287287

GH_UnitNumber/Components/TestInputComponent.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,36 @@ protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
3737
pManager.AddGenericParameter("Test", "T", "Test list output", GH_ParamAccess.list);
3838
}
3939

40-
protected override void SolveInstance(IGH_DataAccess DA) {
40+
protected override void SolveInstance(IGH_DataAccess da) {
4141
if (Params.Input[0].SourceCount > 0) {
42-
DA.SetData(0, OasysGH.Helpers.Input.UnitNumber(this, DA, 0, MomentUnit.KilonewtonMeter));
42+
da.SetData(0, OasysGH.Helpers.Input.UnitNumber(this, da, 0, MomentUnit.KilonewtonMeter));
4343
}
4444
if (Params.Input[1].SourceCount > 0) {
45-
DA.SetDataList(1, OasysGH.Helpers.Input.UnitNumberList(this, DA, 1, ForceUnit.Kilonewton));
45+
da.SetDataList(1, OasysGH.Helpers.Input.UnitNumberList(this, da, 1, ForceUnit.Kilonewton));
4646
}
4747

4848
if (Params.Input[2].SourceCount > 0) {
49-
DA.SetData(0, OasysGH.Helpers.Input.GenericGoo<OasysGH.Parameters.GH_UnitNumber>(this, DA, 2));
49+
da.SetData(0, OasysGH.Helpers.Input.GenericGoo<OasysGH.Parameters.GH_UnitNumber>(this, da, 2));
5050
}
5151
if (Params.Input[3].SourceCount > 0) {
52-
DA.SetDataList(1, OasysGH.Helpers.Input.GenericGooList<OasysGH.Parameters.GH_UnitNumber>(this, DA, 3));
52+
da.SetDataList(1, OasysGH.Helpers.Input.GenericGooList<OasysGH.Parameters.GH_UnitNumber>(this, da, 3));
5353
}
5454

5555
if (Params.Input[4].SourceCount > 0) {
56-
DA.SetData(0, OasysGH.Helpers.Input.LengthOrRatio(this, DA, 4, LengthUnit.Meter));
56+
da.SetData(0, OasysGH.Helpers.Input.LengthOrRatio(this, da, 4, LengthUnit.Meter));
5757
}
5858
if (Params.Input[5].SourceCount > 0) {
59-
DA.SetDataList(1, OasysGH.Helpers.Input.LengthsOrRatios(this, DA, 5, LengthUnit.Meter));
59+
da.SetDataList(1, OasysGH.Helpers.Input.LengthsOrRatios(this, da, 5, LengthUnit.Meter));
6060
}
6161

6262
if (Params.Input[6].SourceCount > 0) {
63-
DA.SetData(0, OasysGH.Helpers.Input.UnitNumberOrDoubleAsRatioToPercentage(this, DA, 6));
63+
da.SetData(0, OasysGH.Helpers.Input.UnitNumberOrDoubleAsRatioToPercentage(this, da, 6));
6464
}
6565
if (Params.Input[7].SourceCount > 0) {
66-
DA.SetData(0, OasysGH.Helpers.Input.RatioInDecimalFractionToPercentage(this, DA, 7));
66+
da.SetData(0, OasysGH.Helpers.Input.RatioInDecimalFractionToPercentage(this, da, 7));
6767
}
6868
if (Params.Input[8].SourceCount > 0) {
69-
DA.SetData(0, OasysGH.Helpers.Input.RatioInDecimalFractionToDecimalFraction(this, DA, 8));
69+
da.SetData(0, OasysGH.Helpers.Input.RatioInDecimalFractionToDecimalFraction(this, da, 8));
7070
}
7171
}
7272
}

GH_UnitNumber/GH_UnitNumber.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<RepositoryUrl>https://github.com/arup-group/OasysGH</RepositoryUrl>
1111
<PackageReadmeFile>README.md</PackageReadmeFile>
1212
<PackageIcon>UnitNumberLogo64.png</PackageIcon>
13-
<Version>0.6.12-beta</Version>
13+
<Version>0.6.13-beta</Version>
1414
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1515
<BaseOutputPath>bin\</BaseOutputPath>
1616
<AutoGenerateBindingRedirects>True</AutoGenerateBindingRedirects>

OasysGH/ComponentAttributes/DropDownComponentAttributes.cs

+6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public override GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, GH_Canvas
115115
}
116116
}
117117
}
118+
118119
return base.RespondToMouseDown(sender, e);
119120
}
120121

@@ -135,6 +136,7 @@ public override GH_ObjectResponse RespondToMouseMove(GH_Canvas sender, GH_Canvas
135136
return GH_ObjectResponse.Capture;
136137
}
137138
}
139+
138140
if (_mouseOver) {
139141
_mouseOver = false;
140142
Grasshopper.Instances.CursorServer.ResetCursor(sender);
@@ -145,6 +147,7 @@ public override GH_ObjectResponse RespondToMouseMove(GH_Canvas sender, GH_Canvas
145147
}
146148

147149
public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, GH_CanvasMouseEvent e) {
150+
148151
if (e.Button == MouseButtons.Left) {
149152
var comp = Owner as GH_Component;
150153
if (_drag) {
@@ -171,6 +174,7 @@ public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, GH_CanvasMo
171174
continue;
172175
_unfolded[j] = false;
173176
}
177+
174178
comp.ExpireSolution(true);
175179
return GH_ObjectResponse.Handled;
176180
}
@@ -206,6 +210,7 @@ public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, GH_CanvasMo
206210
_unfolded[i] = !_unfolded[i];
207211
comp.ExpireSolution(true);
208212
}
213+
209214
return GH_ObjectResponse.Handled;
210215
}
211216
}
@@ -217,6 +222,7 @@ public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, GH_CanvasMo
217222
}
218223
}
219224
}
225+
220226
return base.RespondToMouseUp(sender, e);
221227
}
222228

OasysGH/Components/CreateOasysProfile.cs

+13-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text.RegularExpressions;
5+
using GH_IO.Serialization;
56
using Grasshopper;
67
using Grasshopper.Kernel;
78
using Grasshopper.Kernel.Data;
@@ -127,7 +128,7 @@ protected CreateOasysProfile(string name, string nickname, string description, s
127128
_sectionList = SqlReader.Instance.GetSectionsDataFromSQLite(new List<int> { -1 }, DataSource, false);
128129
}
129130

130-
public override bool Read(GH_IO.Serialization.GH_IReader reader) {
131+
public override bool Read(GH_IReader reader) {
131132
_mode = (FoldMode)Enum.Parse(typeof(FoldMode), reader.GetString("mode"));
132133
_lengthUnit = (LengthUnit)UnitsHelper.Parse(typeof(LengthUnit), reader.GetString("lengthUnit"));
133134
_inclSS = reader.GetBoolean("inclSS");
@@ -811,7 +812,7 @@ public override void VariableParameterMaintenance() {
811812
}
812813
}
813814

814-
public override bool Write(GH_IO.Serialization.GH_IWriter writer) {
815+
public override bool Write(GH_IWriter writer) {
815816
writer.SetString("mode", _mode.ToString());
816817
writer.SetString("lengthUnit", _lengthUnit.ToString());
817818
writer.SetBoolean("inclSS", _inclSS);
@@ -937,7 +938,7 @@ protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
937938
pManager.AddTextParameter("Profile", "Pf", "Profile for a GSA Section", GH_ParamAccess.tree);
938939
}
939940

940-
protected override void SolveInstance(IGH_DataAccess DA) {
941+
protected override void SolveInternal(IGH_DataAccess da) {
941942
ClearRuntimeMessages();
942943
for (int i = 0; i < Params.Input.Count; i++)
943944
Params.Input[i].ClearRuntimeMessages();
@@ -948,26 +949,27 @@ protected override void SolveInstance(IGH_DataAccess DA) {
948949
pathCount = Params.Output[0].VolatileData.PathCount;
949950
}
950951

951-
var path = new GH_Path(new int[] { pathCount });
952-
List<IProfile> profiles = SolveInstanceForCatalogueProfile(DA);
952+
var path = new GH_Path(pathCount);
953+
List<IProfile> profiles = SolveInstanceForCatalogueProfile(da);
953954
var tree = new DataTree<OasysProfileGoo>();
954955
foreach (IProfile profile in profiles) {
955956
tree.Add(new OasysProfileGoo(profile), path);
956957
}
957958

958-
DA.SetDataTree(0, tree);
959+
da.SetDataTree(0, tree);
960+
959961
} else if (_mode == FoldMode.Other) {
960-
IProfile profile = SolveInstanceForStandardProfile(DA);
962+
IProfile profile = SolveInstanceForStandardProfile(da);
961963

962-
DA.SetData(0, new OasysProfileGoo(profile));
964+
da.SetData(0, new OasysProfileGoo(profile));
963965
}
964966
}
965967

966-
protected List<IProfile> SolveInstanceForCatalogueProfile(IGH_DataAccess DA) {
968+
protected List<IProfile> SolveInstanceForCatalogueProfile(IGH_DataAccess da) {
967969
var profiles = new List<IProfile>();
968970
// get user input filter search string
969971
bool incl = false;
970-
if (DA.GetData(1, ref incl)) {
972+
if (da.GetData(1, ref incl)) {
971973
if (_inclSS != incl) {
972974
_inclSS = incl;
973975
UpdateTypeData();
@@ -986,7 +988,7 @@ protected List<IProfile> SolveInstanceForCatalogueProfile(IGH_DataAccess DA) {
986988
// get user input filter search string
987989
_search = null;
988990
string inSearch = "";
989-
if (DA.GetData(0, ref inSearch)) {
991+
if (da.GetData(0, ref inSearch)) {
990992
_search = inSearch.Trim().ToLower().Replace(".", string.Empty).Replace("*", ".*").Replace(" ", ".*");
991993
if (_search == "cat") {
992994
string eventName = "EasterCat";
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Collections.Generic;
2+
using GH_IO.Serialization;
43
using Grasshopper.Kernel;
5-
using Grasshopper.Kernel.Types;
6-
using Newtonsoft.Json;
7-
using OasysGH.Parameters;
8-
using OasysUnits;
9-
using OasysUnits.Serialization.JsonNet;
104

115
namespace OasysGH.Components {
126
public abstract class GH_OasysDropDownComponent : GH_OasysComponent, IGH_VariableParameterComponent {
13-
protected internal bool _alwaysExpireDownStream = false;
147
protected internal List<List<string>> _dropDownItems;
15-
protected internal Dictionary<int, List<string>> _existingOutputsSerialized = new Dictionary<int, List<string>>();
168
protected internal bool _isInitialised = false;
179
protected internal List<string> _selectedItems;
1810
protected internal List<string> _spacerDescriptions;
19-
private static readonly OasysUnitsIQuantityJsonConverter converter = new OasysUnitsIQuantityJsonConverter();
20-
private Dictionary<int, bool> _outputIsExpired = new Dictionary<int, bool>();
21-
private Dictionary<int, List<bool>> _outputsAreExpired = new Dictionary<int, List<bool>>();
2211

2312
public GH_OasysDropDownComponent(string name, string nickname, string description, string category, string subCategory) : base(name, nickname, description, category, subCategory) {
2413
}
@@ -38,41 +27,7 @@ public override void CreateAttributes() {
3827

3928
bool IGH_VariableParameterComponent.DestroyParameter(GH_ParameterSide side, int index) => false;
4029

41-
public void OutputChanged<T>(T data, int outputIndex, int index) where T : IGH_Goo {
42-
if (!_existingOutputsSerialized.ContainsKey(outputIndex)) {
43-
_existingOutputsSerialized.Add(outputIndex, new List<string>());
44-
_outputsAreExpired.Add(outputIndex, new List<bool>());
45-
}
46-
47-
string outputsSerialized = "";
48-
if (data.GetType() == typeof(GH_UnitNumber)) {
49-
// use IQuantity converter if data is a IQuantity (struct)
50-
IQuantity quantity = ((GH_UnitNumber)(object)data).Value;
51-
outputsSerialized = JsonConvert.SerializeObject(quantity, converter);
52-
} else {
53-
object obj = ((T)(object)data).ScriptVariable();
54-
try {
55-
outputsSerialized = JsonConvert.SerializeObject(obj);
56-
} catch (Exception) {
57-
outputsSerialized = data.GetHashCode().ToString();
58-
}
59-
}
60-
61-
if (_existingOutputsSerialized[outputIndex].Count == index) {
62-
_existingOutputsSerialized[outputIndex].Add(outputsSerialized);
63-
_outputsAreExpired[outputIndex].Add(true);
64-
return;
65-
}
66-
67-
if (_existingOutputsSerialized[outputIndex][index] != outputsSerialized) {
68-
_existingOutputsSerialized[outputIndex][index] = outputsSerialized;
69-
_outputsAreExpired[outputIndex][index] = true;
70-
return;
71-
}
72-
_outputsAreExpired[outputIndex][index] = false;
73-
}
74-
75-
public override bool Read(GH_IO.Serialization.GH_IReader reader) {
30+
public override bool Read(GH_IReader reader) {
7631
Helpers.DeSerialization.ReadDropDownComponents(ref reader, ref _dropDownItems, ref _selectedItems, ref _spacerDescriptions);
7732

7833
_isInitialised = true;
@@ -86,31 +41,19 @@ public override bool Read(GH_IO.Serialization.GH_IReader reader) {
8641
public virtual void VariableParameterMaintenance() {
8742
}
8843

89-
public override bool Write(GH_IO.Serialization.GH_IWriter writer) {
44+
public override bool Write(GH_IWriter writer) {
9045
Helpers.DeSerialization.WriteDropDownComponents(ref writer, _dropDownItems, _selectedItems, _spacerDescriptions);
9146
return base.Write(writer);
9247
}
9348

94-
protected internal abstract void InitialiseDropdowns();
95-
96-
protected override void ExpireDownStreamObjects() {
97-
if (_alwaysExpireDownStream) {
98-
base.ExpireDownStreamObjects();
99-
return;
100-
}
101-
102-
SetExpireDownStream();
103-
if (_outputIsExpired.Count > 0) {
104-
for (int outputIndex = 0; outputIndex < Params.Output.Count; outputIndex++) {
105-
if (_outputIsExpired[outputIndex]) {
106-
IGH_Param item = Params.Output[outputIndex];
107-
item.ExpireSolution(recompute: false);
108-
}
109-
}
110-
} else
111-
base.ExpireDownStreamObjects();
49+
protected sealed override void SolveInstance(IGH_DataAccess da) {
50+
SolveInternal(da);
11251
}
11352

53+
protected abstract void SolveInternal(IGH_DataAccess da);
54+
55+
protected internal abstract void InitialiseDropdowns();
56+
11457
protected virtual void UpdateUI() {
11558
(this as IGH_VariableParameterComponent).VariableParameterMaintenance();
11659
ExpireSolution(true);
@@ -122,17 +65,5 @@ protected virtual void UpdateUIFromSelectedItems() {
12265
CreateAttributes();
12366
UpdateUI();
12467
}
125-
126-
private void SetExpireDownStream() {
127-
if (_outputsAreExpired != null && _outputsAreExpired.Count > 0) {
128-
_outputIsExpired = new Dictionary<int, bool>();
129-
for (int outputIndex = 0; outputIndex < Params.Output.Count; outputIndex++) {
130-
if (_outputsAreExpired.ContainsKey(outputIndex))
131-
_outputIsExpired.Add(outputIndex, _outputsAreExpired[outputIndex].Any(c => c == true));
132-
else
133-
_outputIsExpired.Add(outputIndex, true);
134-
}
135-
}
136-
}
13768
}
13869
}

0 commit comments

Comments
 (0)