Skip to content

Commit

Permalink
Merge pull request #284 from arup-group/feature/GSAGH-193-populate-em…
Browse files Browse the repository at this point in the history
…pty-keys

gsagh-193 find first empty key instead of max key
  • Loading branch information
kpne authored Jan 16, 2023
2 parents bb5040a + a8ff681 commit 21391ea
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 52 deletions.
12 changes: 11 additions & 1 deletion GsaGH/Helpers/Export/Geometry/Elements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.ObjectModel;
using System.Linq;
using GsaAPI;
using GsaGH.Helpers.Import;
using GsaGH.Parameters;
using OasysUnits.Units;
using Rhino.Geometry;
Expand Down Expand Up @@ -46,9 +47,12 @@ internal static void ConvertElement1D(GsaElement1d element1d,
internal static void ConvertElement1D(List<GsaElement1d> element1ds, ref GsaGuidIntListDictionary<Element> apiElements, ref GsaIntDictionary<Node> existingNodes, LengthUnit unit, ref GsaGuidDictionary<Section> apiSections, ref GsaIntDictionary<SectionModifier> apiSectionModifiers, ref GsaGuidDictionary<AnalysisMaterial> apiMaterials)
{
if (element1ds != null)
{
element1ds = element1ds.OrderByDescending(x => x.Id).ToList();
for (int i = 0; i < element1ds.Count; i++)
if (element1ds[i] != null)
ConvertElement1D(element1ds[i], ref apiElements, ref existingNodes, unit, ref apiSections, ref apiSectionModifiers, ref apiMaterials);
}
}
#endregion

Expand Down Expand Up @@ -83,9 +87,12 @@ internal static void ConvertElement2D(List<GsaElement2d> element2ds,
ref GsaGuidDictionary<Prop2D> apiProp2ds, ref GsaGuidDictionary<AnalysisMaterial> apiMaterials)
{
if (element2ds != null)
{
element2ds = element2ds.OrderByDescending(e => e.Ids.First()).ToList();
for (int i = 0; i < element2ds.Count; i++)
if (element2ds[i] != null)
ConvertElement2D(element2ds[i], ref apiElements, ref existingNodes, unit, ref apiProp2ds, ref apiMaterials);
}
}
#endregion

Expand Down Expand Up @@ -121,10 +128,13 @@ internal static void ConvertElement3D(List<GsaElement3d> element3ds,
ref GsaGuidDictionary<Prop3D> apiProp3ds, ref GsaGuidDictionary<AnalysisMaterial> apiMaterials)
{
if (element3ds != null)
{
element3ds = element3ds.OrderByDescending(e => e.Ids.First()).ToList();
for (int i = 0; i < element3ds.Count; i++)
if (element3ds[i] != null)
ConvertElement3D(element3ds[i], ref apiElements, ref existingNodes, unit, ref apiProp3ds, ref apiMaterials);
}
}
#endregion
}
}
}
37 changes: 25 additions & 12 deletions GsaGH/Helpers/Export/Geometry/Members.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

namespace GsaGH.Helpers.Export
{
internal class Members
internal class Members
{
#region topologylist
private static string CreateTopology(List<Point3d> topology, List<string> topoType, ref GsaIntDictionary<Node> existingNodes, LengthUnit unit)
{
string topo = "";
if (topology == null)
return topo;
for (int j = 0; j < topology.Count; j++)
{
// add the topology type to the topo string first
Expand Down Expand Up @@ -53,16 +55,18 @@ internal static void ConvertMember1D(GsaMember1d member1d,
apiMember.MeshSize = new Length(member1d.MeshSize, unit).Meters;

string topo = CreateTopology(member1d.Topology, member1d.TopologyType, ref existingNodes, unit);

try //GsaAPI will perform check on topology list
if (topo != "")
{
apiMember.Topology = string.Copy(topo.Replace(" ", " "));
}
catch (Exception)
{
List<string> errors = member1d.Topology.Select(t => "{" + t.ToString() + "}").ToList();
string error = string.Join(", ", errors);
throw new Exception(" Invalid topology for Member1d: " + topo + " with original points: " + error);
try //GsaAPI will perform check on topology list
{
apiMember.Topology = string.Copy(topo.Replace(" ", " "));
}
catch (Exception)
{
List<string> errors = member1d.Topology.Select(t => "{" + t.ToString() + "}").ToList();
string error = string.Join(", ", errors);
throw new Exception(" Invalid topology for Member1d: " + topo + " with original points: " + error);
}
}

if (member1d.OrientationNode != null)
Expand All @@ -78,14 +82,17 @@ internal static void ConvertMember1D(List<GsaMember1d> member1ds,
ref GsaGuidDictionary<Section> apiSections, ref GsaIntDictionary<SectionModifier> apiSectionModifiers, ref GsaGuidDictionary<AnalysisMaterial> apiMaterials)
{
if (member1ds != null)
{
member1ds = member1ds.OrderByDescending(x => x.Id).ToList();
for (int i = 0; i < member1ds.Count; i++)
if (member1ds[i] != null)
ConvertMember1D(member1ds[i], ref apiMembers, ref existingNodes, unit, ref apiSections, ref apiSectionModifiers, ref apiMaterials);
}
}
#endregion

#region member2d

internal static void ConvertMember2D(GsaMember2d member2d,
ref GsaGuidDictionary<Member> apiMembers, ref GsaIntDictionary<Node> existingNodes, LengthUnit unit,
ref GsaGuidDictionary<Prop2D> apiProp2ds, ref GsaGuidDictionary<AnalysisMaterial> apiMaterials)
Expand Down Expand Up @@ -127,9 +134,12 @@ internal static void ConvertMember2D(List<GsaMember2d> member2ds,
ref GsaGuidDictionary<Prop2D> apiProp2ds, ref GsaGuidDictionary<AnalysisMaterial> apiMaterials)
{
if (member2ds != null)
{
member2ds = member2ds.OrderByDescending(x => x.Id).ToList();
for (int i = 0; i < member2ds.Count; i++)
if (member2ds[i] != null)
ConvertMember2D(member2ds[i], ref apiMembers, ref existingNodes, unit, ref apiProp2ds, ref apiMaterials);
}
}
#endregion

Expand All @@ -145,7 +155,7 @@ internal static void ConvertMember3D(GsaMember3d member3d,

List<string> topos = new List<string>();
// Loop through the face list

List<int> topoInts = new List<int>();
foreach (Point3d verticy in member3d.SolidMesh.TopologyVertices)
topoInts.Add(Nodes.AddNode(ref existingNodes, verticy, unit));
Expand All @@ -166,9 +176,12 @@ internal static void ConvertMember3D(List<GsaMember3d> member3ds,
ref GsaGuidDictionary<Prop3D> apiProp3ds, ref GsaGuidDictionary<AnalysisMaterial> apiMaterials)
{
if (member3ds != null)
{
member3ds = member3ds.OrderByDescending(x => x.Id).ToList();
for (int i = 0; i < member3ds.Count; i++)
if (member3ds[i] != null)
ConvertMember3D(member3ds[i], ref apiMembers, ref existingNodes, unit, ref apiProp3ds, ref apiMaterials);
}
}
#endregion
}
Expand Down
6 changes: 5 additions & 1 deletion GsaGH/Helpers/Export/Geometry/Nodes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using GsaAPI;
using GsaGH.Parameters;
using OasysUnits;
Expand All @@ -7,15 +8,18 @@

namespace GsaGH.Helpers.Export
{
internal class Nodes
internal class Nodes
{
internal static void ConvertNodes(List<GsaNode> nodes, ref GsaIntDictionary<Node> existingNodes,
ref Dictionary<int, Axis> existingAxes, LengthUnit modelUnit)
{
if (nodes != null && nodes.Count > 0)
{
nodes = nodes.OrderByDescending(n => n.Id).ToList();
for (int i = 0; i < nodes.Count; i++)
if (nodes[i] != null)
ConvertNode(nodes[i], ref existingNodes, ref existingAxes, modelUnit);
}
}

/// <summary>
Expand Down
49 changes: 21 additions & 28 deletions GsaGH/Helpers/Export/GsaDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,37 @@ namespace GsaGH.Helpers.Export
internal class GsaIntDictionary<T>
{
private IDictionary<int, T> _dictionary;
private int _maxKey = 0;
private int _firstEmptyKey = 1;

internal int Count => _dictionary.Count;
internal ReadOnlyDictionary<int, T> Dictionary => new ReadOnlyDictionary<int, T>(_dictionary);

internal GsaIntDictionary(ReadOnlyDictionary<int, T> dictionary)
{
_dictionary = dictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
if (_dictionary.Count > 0)
_maxKey = _dictionary.Keys.Max();
//if (_dictionary.Count > 0)
// _maxKey = _dictionary.Keys.Max();
}

internal int AddValue(T value)
{
_maxKey++;
_dictionary[_maxKey] = value;
return _maxKey;
while (_dictionary.ContainsKey(_firstEmptyKey))
_firstEmptyKey++;
_dictionary[_firstEmptyKey] = value;
return _firstEmptyKey;
}

internal void SetValue(int key, T value)
{
_dictionary[key] = value;
if (_maxKey <= key)
_maxKey = key;
}
}

internal class GsaGuidDictionary<T>
{
private IDictionary<int, T> _dictionary;
private IDictionary<Guid, int> _guidDictionary;
private int _maxKey = 0;
private int _firstEmptyKey = 1;

internal int Count => _dictionary.Count;
internal ReadOnlyDictionary<int, T> Dictionary => new ReadOnlyDictionary<int, T>(_dictionary);
Expand All @@ -49,34 +48,31 @@ internal GsaGuidDictionary(ReadOnlyDictionary<int, T> dictionary)
{
_dictionary = dictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
_guidDictionary = dictionary.ToDictionary(kvp => Guid.NewGuid(), kvp => kvp.Key);
if (_dictionary.Count > 0)
_maxKey = _dictionary.Keys.Max();
}

internal int AddValue(Guid guid, T value)
{
if (_guidDictionary.ContainsKey(guid))
return _guidDictionary[guid];
_maxKey++;
_dictionary[_maxKey] = value;
_guidDictionary[guid] = _maxKey;
return _maxKey;
while (_dictionary.ContainsKey(_firstEmptyKey))
_firstEmptyKey++;
_dictionary[_firstEmptyKey] = value;
_guidDictionary[guid] = _firstEmptyKey;
return _firstEmptyKey;
}

internal void SetValue(int key, Guid guid, T value)
{
_dictionary[key] = value;
_guidDictionary[guid] = key;
if (_maxKey <= key)
_maxKey = key;
}
}

internal class GsaGuidIntListDictionary<T>
{
private IDictionary<int, T> _dictionary;
private IDictionary<Guid, Collection<int>> _guidDictionary;
private int _maxKey = 0;
private int _firstEmptyKey = 1;

internal int Count => _dictionary.Count;
internal ReadOnlyDictionary<int, T> Dictionary => new ReadOnlyDictionary<int, T>(_dictionary);
Expand All @@ -86,31 +82,28 @@ internal GsaGuidIntListDictionary(ReadOnlyDictionary<int, T> dictionary)
{
_dictionary = dictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
_guidDictionary = dictionary.ToDictionary(kvp => Guid.NewGuid(), kvp => new Collection<int>() { kvp.Key });
if (_dictionary.Count > 0)
_maxKey = _dictionary.Keys.Max();
}

internal int AddValue(Guid guid, T value)
{
_maxKey++;
while (_dictionary.ContainsKey(_firstEmptyKey))
_firstEmptyKey++;
if (_guidDictionary.ContainsKey(guid))
{
_dictionary[_maxKey] = value;
_guidDictionary[guid].Add(_maxKey);
_dictionary[_firstEmptyKey] = value;
_guidDictionary[guid].Add(_firstEmptyKey);
}
else
{
_dictionary[_maxKey] = value;
_guidDictionary[guid] = new Collection<int>() { _maxKey };
_dictionary[_firstEmptyKey] = value;
_guidDictionary[guid] = new Collection<int>() { _firstEmptyKey };
}
return _maxKey;
return _firstEmptyKey;
}

internal void SetValue(int key, Guid guid, T value, bool overwrite)
{
_dictionary[key] = value;
if (_maxKey <= key)
_maxKey = key;
if (_guidDictionary.ContainsKey(guid) && !overwrite)
{
if (!_guidDictionary[guid].Contains(key))
Expand Down
2 changes: 1 addition & 1 deletion GsaGHTests/Helpers/Export/AssembleModelTestsNodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void AssembleModelWithNodesTest()
CreateModelTest.CreateModelFromGeometry(new List<GsaNodeGoo>() { node1, node2 }, null, null, null, null, null, ModelUnit.m));

TestNode(node1.Value, LengthUnit.Meter, 2, modelGoo.Value);
TestNode(node2.Value, LengthUnit.Meter, 3, modelGoo.Value);
TestNode(node2.Value, LengthUnit.Meter, 1, modelGoo.Value);
}
}
}
18 changes: 9 additions & 9 deletions GsaGHTests/Helpers/Export/GsaDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public void GsaIntDictionaryWithExistingAddTest()

int expectedID = dictionary.AddValue("myFirst");

Assert.Equal(6, expectedID);
Assert.Equal(2, expectedID);
Assert.Equal(3, dictionary.Count);
Assert.Equal("myFirst", dictionary.Dictionary[6]);
Assert.Equal("myFirst", dictionary.Dictionary[2]);
}

[Fact]
Expand Down Expand Up @@ -104,9 +104,9 @@ public void GsaGuidDictionaryWithExistingAddTest()
Guid guid = Guid.NewGuid();
int expectedID = dictionary.AddValue(guid, "myFirst");

Assert.Equal(6, expectedID);
Assert.Equal(2, expectedID);
Assert.Equal(3, dictionary.Count);
Assert.Equal("myFirst", dictionary.Dictionary[6]);
Assert.Equal("myFirst", dictionary.Dictionary[2]);

// try to add with same guid but different object
Assert.Equal(expectedID, dictionary.AddValue(guid, "second"));
Expand Down Expand Up @@ -212,11 +212,11 @@ public void GsaGuidIntListDictionaryWithExistingAddTest()
expectedIDs.Add(dictionary.AddValue(guid, "myFirst"));
expectedIDs.Add(dictionary.AddValue(guid, "mySecond"));

Assert.Equal(6, expectedIDs[0]);
Assert.Equal(7, expectedIDs[1]);
Assert.Equal(2, expectedIDs[0]);
Assert.Equal(3, expectedIDs[1]);
Assert.Equal(4, dictionary.Count);
Assert.Equal("myFirst", dictionary.Dictionary[6]);
Assert.Equal("mySecond", dictionary.Dictionary[7]);
Assert.Equal("myFirst", dictionary.Dictionary[2]);
Assert.Equal("mySecond", dictionary.Dictionary[3]);
}

[Fact]
Expand All @@ -237,7 +237,7 @@ public void GsaGuidIntListDictionaryWithExistingSetTest()
Assert.Equal("myFirst", dictionary.Dictionary[5]);
Assert.Equal(2, dictionary.GuidDictionary[guid].Count);

Assert.Equal("mySecond", dictionary.Dictionary[6]);
Assert.Equal("mySecond", dictionary.Dictionary[2]);
}

[Fact]
Expand Down

0 comments on commit 21391ea

Please sign in to comment.