diff --git a/GsaGH/Helpers/Export/Geometry/Elements.cs b/GsaGH/Helpers/Export/Geometry/Elements.cs index bbbe92dde..4bf635ad3 100644 --- a/GsaGH/Helpers/Export/Geometry/Elements.cs +++ b/GsaGH/Helpers/Export/Geometry/Elements.cs @@ -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; @@ -46,9 +47,12 @@ internal static void ConvertElement1D(GsaElement1d element1d, internal static void ConvertElement1D(List element1ds, ref GsaGuidIntListDictionary apiElements, ref GsaIntDictionary existingNodes, LengthUnit unit, ref GsaGuidDictionary
apiSections, ref GsaIntDictionary apiSectionModifiers, ref GsaGuidDictionary 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 @@ -83,9 +87,12 @@ internal static void ConvertElement2D(List element2ds, ref GsaGuidDictionary apiProp2ds, ref GsaGuidDictionary 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 @@ -121,10 +128,13 @@ internal static void ConvertElement3D(List element3ds, ref GsaGuidDictionary apiProp3ds, ref GsaGuidDictionary 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 } -} +} \ No newline at end of file diff --git a/GsaGH/Helpers/Export/Geometry/Members.cs b/GsaGH/Helpers/Export/Geometry/Members.cs index 7cca187f9..9122fdae5 100644 --- a/GsaGH/Helpers/Export/Geometry/Members.cs +++ b/GsaGH/Helpers/Export/Geometry/Members.cs @@ -10,12 +10,14 @@ namespace GsaGH.Helpers.Export { - internal class Members + internal class Members { #region topologylist private static string CreateTopology(List topology, List topoType, ref GsaIntDictionary 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 @@ -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 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 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) @@ -78,14 +82,17 @@ internal static void ConvertMember1D(List member1ds, ref GsaGuidDictionary
apiSections, ref GsaIntDictionary apiSectionModifiers, ref GsaGuidDictionary 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 apiMembers, ref GsaIntDictionary existingNodes, LengthUnit unit, ref GsaGuidDictionary apiProp2ds, ref GsaGuidDictionary apiMaterials) @@ -127,9 +134,12 @@ internal static void ConvertMember2D(List member2ds, ref GsaGuidDictionary apiProp2ds, ref GsaGuidDictionary 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 @@ -145,7 +155,7 @@ internal static void ConvertMember3D(GsaMember3d member3d, List topos = new List(); // Loop through the face list - + List topoInts = new List(); foreach (Point3d verticy in member3d.SolidMesh.TopologyVertices) topoInts.Add(Nodes.AddNode(ref existingNodes, verticy, unit)); @@ -166,9 +176,12 @@ internal static void ConvertMember3D(List member3ds, ref GsaGuidDictionary apiProp3ds, ref GsaGuidDictionary 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 } diff --git a/GsaGH/Helpers/Export/Geometry/Nodes.cs b/GsaGH/Helpers/Export/Geometry/Nodes.cs index 8018c26e7..27e69bd6f 100644 --- a/GsaGH/Helpers/Export/Geometry/Nodes.cs +++ b/GsaGH/Helpers/Export/Geometry/Nodes.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using GsaAPI; using GsaGH.Parameters; using OasysUnits; @@ -7,15 +8,18 @@ namespace GsaGH.Helpers.Export { - internal class Nodes + internal class Nodes { internal static void ConvertNodes(List nodes, ref GsaIntDictionary existingNodes, ref Dictionary 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); + } } /// diff --git a/GsaGH/Helpers/Export/GsaDictionary.cs b/GsaGH/Helpers/Export/GsaDictionary.cs index 0d7802377..d941c0882 100644 --- a/GsaGH/Helpers/Export/GsaDictionary.cs +++ b/GsaGH/Helpers/Export/GsaDictionary.cs @@ -8,7 +8,7 @@ namespace GsaGH.Helpers.Export internal class GsaIntDictionary { private IDictionary _dictionary; - private int _maxKey = 0; + private int _firstEmptyKey = 1; internal int Count => _dictionary.Count; internal ReadOnlyDictionary Dictionary => new ReadOnlyDictionary(_dictionary); @@ -16,22 +16,21 @@ internal class GsaIntDictionary internal GsaIntDictionary(ReadOnlyDictionary 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; } } @@ -39,7 +38,7 @@ internal class GsaGuidDictionary { private IDictionary _dictionary; private IDictionary _guidDictionary; - private int _maxKey = 0; + private int _firstEmptyKey = 1; internal int Count => _dictionary.Count; internal ReadOnlyDictionary Dictionary => new ReadOnlyDictionary(_dictionary); @@ -49,26 +48,23 @@ internal GsaGuidDictionary(ReadOnlyDictionary 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; } } @@ -76,7 +72,7 @@ internal class GsaGuidIntListDictionary { private IDictionary _dictionary; private IDictionary> _guidDictionary; - private int _maxKey = 0; + private int _firstEmptyKey = 1; internal int Count => _dictionary.Count; internal ReadOnlyDictionary Dictionary => new ReadOnlyDictionary(_dictionary); @@ -86,31 +82,28 @@ internal GsaGuidIntListDictionary(ReadOnlyDictionary dictionary) { _dictionary = dictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); _guidDictionary = dictionary.ToDictionary(kvp => Guid.NewGuid(), kvp => new Collection() { 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() { _maxKey }; + _dictionary[_firstEmptyKey] = value; + _guidDictionary[guid] = new Collection() { _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)) diff --git a/GsaGHTests/Helpers/Export/AssembleModelTestsNodes.cs b/GsaGHTests/Helpers/Export/AssembleModelTestsNodes.cs index 318e47edc..4f428c48d 100644 --- a/GsaGHTests/Helpers/Export/AssembleModelTestsNodes.cs +++ b/GsaGHTests/Helpers/Export/AssembleModelTestsNodes.cs @@ -56,7 +56,7 @@ public void AssembleModelWithNodesTest() CreateModelTest.CreateModelFromGeometry(new List() { 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); } } } diff --git a/GsaGHTests/Helpers/Export/GsaDictionaryTests.cs b/GsaGHTests/Helpers/Export/GsaDictionaryTests.cs index 3b1b4ed6c..2eaa24959 100644 --- a/GsaGHTests/Helpers/Export/GsaDictionaryTests.cs +++ b/GsaGHTests/Helpers/Export/GsaDictionaryTests.cs @@ -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] @@ -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")); @@ -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] @@ -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]