Skip to content

Commit

Permalink
Update to make sue of caching system added in BHoM/BHoM_Adapter#337
Browse files Browse the repository at this point in the history
Also includes a tweak to read of materials to aim to make it fit with new system.
ID for materials for GSA10 now includes material type, as that is required to differentiate between different material types
  • Loading branch information
IsakNaslundBh authored and Fraser Greenroyd committed Mar 28, 2023
1 parent d88e50e commit 9d710e8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 92 deletions.
91 changes: 28 additions & 63 deletions GSA_Adapter/CRUD/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,7 @@ protected override IEnumerable<IBHoMObject> IRead(Type type, IList indices, Acti

public List<IMaterialFragment> ReadMaterials(List<string> ids = null)
{
return ReadMaterials(ids, false);
}

/***************************************************/

public List<IMaterialFragment> ReadMaterials(List<string> ids = null, bool includeStandard = false)
{
List<IMaterialFragment> materials = ReadMaterialDictionary(ids, includeStandard).Select(x => x.Value).ToList();

if (ids != null && ids.Count != 0)
materials = materials.Where(x => ids.Contains(x.GSAId().ToString())).ToList();

return materials;
}

public Dictionary<string, IMaterialFragment> ReadMaterialDictionary(List<string> ids = null, bool includeStandard = false)
{
//List<IMaterialFragment> materials = ReadMaterials(null, false);
List<IMaterialFragment> materials = new List<IMaterialFragment>();
List<string> keys = new List<string>();
Dictionary<string, IMaterialFragment> materialDictionary = new Dictionary<string, IMaterialFragment>();

#if GSA_10_1
string allProps = m_gsaCom.GwaCommand("GET_ALL, MAT_ANAL.1").ToString();
Expand All @@ -148,24 +128,22 @@ public Dictionary<string, IMaterialFragment> ReadMaterialDictionary(List<string>
else
materials = matArr.Where(x => ids.Contains(x.Split(',')[1])).Select(x => Convert.FromGsaMaterial(x)).Where(x => x != null).ToList();

if (includeStandard)
materials.AddRange(GetStandardGsaMaterials());
return materials;
}

/***************************************************/

for (int i = 0; i < materials.Count(); i++)
public Dictionary<string, IMaterialFragment> ReadMaterialDictionary(List<string> ids = null, bool includeStandard = false)
{
Dictionary<string, IMaterialFragment> materials = GetCachedOrReadAsDictionary<string, IMaterialFragment>(ids);
if (includeStandard)
{
#if GSA_10_1
string key = matArr[i].Split(("_.").ToCharArray())[1] + ":" + materials[i].GSAId();
#else
string key = GetAdapterId(materials[i]).ToString();
#endif
if (!keys.Contains(key))
foreach (IMaterialFragment standardMaterial in GetStandardGsaMaterials())
{
keys.Add(key);
materialDictionary.Add(key, materials[i]);
materials[standardMaterial.Name] = standardMaterial;
}
}

return materialDictionary;
return materials;
}

/***************************************************/
Expand All @@ -189,22 +167,16 @@ public List<Loadcase> ReadLoadCases(List<string> ids = null)

public List<Bar> ReadBars(List<string> ids = null)
{
//int[] potentialBeamRefs = GenerateIndices(ids, typeof(Bar));

//GsaElement[] gsaElements = new GsaElement[potentialBeamRefs.Length];
//m_gsaCom.Elements(potentialBeamRefs, out gsaElements);
#if GSA_10_1
string allNodes = m_gsaCom.GwaCommand("GET_ALL, EL.4").ToString();
string allBars = m_gsaCom.GwaCommand("GET_ALL, EL.4").ToString();
#else
string allNodes = m_gsaCom.GwaCommand("GET_ALL, EL.2").ToString();
string allBars = m_gsaCom.GwaCommand("GET_ALL, EL.2").ToString();
#endif
string[] barArr = string.IsNullOrWhiteSpace(allNodes) ? new string[0] : allNodes.Split('\n');
string[] barArr = string.IsNullOrWhiteSpace(allBars) ? new string[0] : allBars.Split('\n');

List<ISectionProperty> secPropList = ReadSectionProperties();
List<Node> nodeList = ReadNodes();

Dictionary<string, ISectionProperty> secProps = secPropList.Where(x => x != null).ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<string, Node> nodes = nodeList.ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<int, ISectionProperty> secProps = GetCachedOrReadAsDictionary<int, ISectionProperty>();
Dictionary<int, Node> nodes = GetCachedOrReadAsDictionary<int, Node>();

return Convert.FromGsaBars(barArr, secProps, nodes, ids);
}
Expand All @@ -224,8 +196,7 @@ public List<LoadCombination> ReadLoadCombinations(List<string> ids = null)
analList.Add(anal);
}

List<Loadcase> lCaseList = ReadLoadCases();
Dictionary<string, Loadcase> lCases = lCaseList.ToDictionary(x => x.Number.ToString());
Dictionary<int, Loadcase> lCases = GetCachedOrReadAsDictionary<int, Loadcase>();

if (ids == null)
lComabinations = analList.Select(x => Convert.FromGsaAnalTask(x, lCases)).ToList();
Expand Down Expand Up @@ -366,11 +337,8 @@ public List<FEMesh> ReadFEMesh(List<string> ids = null)
GsaElement[] gsaElements = new GsaElement[potentialMeshRefs.Length];
m_gsaCom.Elements(potentialMeshRefs, out gsaElements);

List<ISurfaceProperty> secPropList = ReadProperty2d();
List<Node> nodeList = ReadNodes();

Dictionary<string, ISurfaceProperty> props = secPropList.Where(x => x != null).ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<string, Node> nodes = nodeList.ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<int, ISurfaceProperty> props = GetCachedOrReadAsDictionary<int, ISurfaceProperty>();
Dictionary<int, Node> nodes = GetCachedOrReadAsDictionary<int, Node>();

return Convert.FromGsaFEMesh(gsaElements, props, nodes);
}
Expand All @@ -392,11 +360,8 @@ public List<LinkConstraint> ReadLinkConstraint(List<string> ids = null)

public List<RigidLink> ReadRigidLink(List<string> ids = null)
{
List<LinkConstraint> constraintList = ReadLinkConstraint(null);
List<Node> nodeList = ReadNodes();

Dictionary<string, LinkConstraint> constraints = constraintList.ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<string, Node> nodes = nodeList.ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<int, LinkConstraint> constraints = GetCachedOrReadAsDictionary<int, LinkConstraint>();
Dictionary<int, Node> nodes = GetCachedOrReadAsDictionary<int, Node>();

int[] potentialBeamRefs = GenerateIndices(ids, typeof(RigidLink));
GsaElement[] gsaElements = new GsaElement[potentialBeamRefs.Length];
Expand All @@ -414,19 +379,14 @@ public List<RigidLink> ReadRigidLink(List<string> ids = null)

public List<Spacer> ReadSpacers(List<string> ids = null)
{
List<Node> nodeList = ReadNodes();
List<SpacerProperty> spacerProps = ReadSpacerProperties();

Dictionary<string, Node> nodes = nodeList.ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<string, SpacerProperty> secProps = spacerProps.Where(x => x != null).ToDictionary(x => GetAdapterId<int>(x).ToString());
Dictionary<int, Node> nodes = GetCachedOrReadAsDictionary<int, Node>();
Dictionary<int, SpacerProperty> secProps = GetCachedOrReadAsDictionary<int,SpacerProperty>();

int[] potentialBeamRefs = GenerateIndices(ids, typeof(Spacer));
GsaElement[] gsaElements = new GsaElement[potentialBeamRefs.Length];
m_gsaCom.Elements(potentialBeamRefs, out gsaElements);

return Convert.FromGsaSpacers(gsaElements, secProps, nodes);


}

/***************************************/
Expand Down Expand Up @@ -469,6 +429,7 @@ private int[] GenerateIndices(List<string> ids, Type elementType)

private List<IMaterialFragment> GetStandardGsaMaterials()
{
#if !GSA_10_1
List<IMaterialFragment> materials = new List<IMaterialFragment>();
materials.Add(new Steel() { Name = "STEEL" });
materials.Add(new Concrete() { Name = "CONC_SHORT" });
Expand All @@ -481,6 +442,10 @@ private List<IMaterialFragment> GetStandardGsaMaterials()
}

return materials;
#else
return new List<IMaterialFragment>();
#endif

}

/***************************************************/
Expand Down
16 changes: 8 additions & 8 deletions GSA_Adapter/Convert/FromGsa/Elements/Bar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static List<Bar> FromGsaBars(IEnumerable<GsaElement> gsaElements, Dictionary<string, ISectionProperty> secProps, Dictionary<string, Node> nodes)
public static List<Bar> FromGsaBars(IEnumerable<GsaElement> gsaElements, Dictionary<int, ISectionProperty> secProps, Dictionary<int, Node> nodes)
{
List<Bar> barList = new List<Bar>();

Expand Down Expand Up @@ -71,8 +71,8 @@ public static List<Bar> FromGsaBars(IEnumerable<GsaElement> gsaElements, Diction
}

Node n1, n2;
nodes.TryGetValue(gsaBar.Topo[0].ToString(), out n1);
nodes.TryGetValue(gsaBar.Topo[1].ToString(), out n2);
nodes.TryGetValue(gsaBar.Topo[0], out n1);
nodes.TryGetValue(gsaBar.Topo[1], out n2);

Bar bar = new Bar { StartNode = n1, EndNode = n2 };
bar.ApplyTaggedName(gsaBar.Name);
Expand All @@ -82,7 +82,7 @@ public static List<Bar> FromGsaBars(IEnumerable<GsaElement> gsaElements, Diction
bar.OrientationAngle = gsaBar.Beta;

ISectionProperty prop;
secProps.TryGetValue(gsaBar.Property.ToString(), out prop);
secProps.TryGetValue(gsaBar.Property, out prop);

bar.SectionProperty = prop;

Expand All @@ -97,7 +97,7 @@ public static List<Bar> FromGsaBars(IEnumerable<GsaElement> gsaElements, Diction

/***************************************************/

public static List<Bar> FromGsaBars(IEnumerable<string> gsaStrings, Dictionary<string, ISectionProperty> secProps, Dictionary<string, Node> nodes, List<string> ids)
public static List<Bar> FromGsaBars(IEnumerable<string> gsaStrings, Dictionary<int, ISectionProperty> secProps, Dictionary<int, Node> nodes, List<string> ids)
{
List<Bar> barList = new List<Bar>();

Expand Down Expand Up @@ -135,15 +135,15 @@ public static List<Bar> FromGsaBars(IEnumerable<string> gsaStrings, Dictionary<s

Bar bar = new Bar()
{
StartNode = nodes[arr[7]],
EndNode = nodes[arr[8]],
StartNode = nodes[int.Parse(arr[7])],
EndNode = nodes[int.Parse(arr[8])],
FEAType = feType,
};

bar.ApplyTaggedName(arr[2]);

ISectionProperty prop;
if (secProps.TryGetValue(arr[5], out prop))
if (secProps.TryGetValue(int.Parse(arr[5]), out prop))
bar.SectionProperty = prop;

if (arr.Length > 10)
Expand Down
6 changes: 3 additions & 3 deletions GSA_Adapter/Convert/FromGsa/Elements/FEMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static List<FEMesh> FromGsaFEMesh(IEnumerable<GsaElement> gsaElements, Dictionary<string, ISurfaceProperty> props, Dictionary<string, Node> nodes)
public static List<FEMesh> FromGsaFEMesh(IEnumerable<GsaElement> gsaElements, Dictionary<int, ISurfaceProperty> props, Dictionary<int, Node> nodes)
{
List<FEMesh> meshList = new List<FEMesh>();

Expand All @@ -61,12 +61,12 @@ public static List<FEMesh> FromGsaFEMesh(IEnumerable<GsaElement> gsaElements, Di
FEMeshFace face = new FEMeshFace() { NodeListIndices = Enumerable.Range(0, gsaMesh.NumTopo).ToList(), OrientationAngle = gsaMesh.Beta * System.Math.PI / 180 };
face.SetAdapterId(typeof(GSAId), id);
ISurfaceProperty property;
props.TryGetValue(gsaMesh.Property.ToString(), out property);
props.TryGetValue(gsaMesh.Property, out property);

FEMesh mesh = new FEMesh()
{
Faces = new List<FEMeshFace>() { face },
Nodes = gsaMesh.Topo.Select(x => nodes[x.ToString()]).ToList(),
Nodes = gsaMesh.Topo.Select(x => nodes[x]).ToList(),
Property = property
};

Expand Down
16 changes: 8 additions & 8 deletions GSA_Adapter/Convert/FromGsa/Elements/RigidLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static List<RigidLink> FromGsaRigidLinks(IEnumerable<GsaElement> gsaElements, Dictionary<string, LinkConstraint> constraints, Dictionary<string, Node> nodes)
public static List<RigidLink> FromGsaRigidLinks(IEnumerable<GsaElement> gsaElements, Dictionary<int, LinkConstraint> constraints, Dictionary<int, Node> nodes)
{
List<RigidLink> linkList = new List<RigidLink>();

Expand All @@ -50,17 +50,17 @@ public static List<RigidLink> FromGsaRigidLinks(IEnumerable<GsaElement> gsaEleme
if (gsaLink.eType != 9)
continue;

RigidLink face = new RigidLink()
RigidLink link = new RigidLink()
{
PrimaryNode = nodes[gsaLink.Topo[0].ToString()],
SecondaryNodes = new List<Node> { nodes[gsaLink.Topo[1].ToString()] },
Constraint = constraints[gsaLink.Property.ToString()]
PrimaryNode = nodes[gsaLink.Topo[0]],
SecondaryNodes = new List<Node> { nodes[gsaLink.Topo[1]] },
Constraint = constraints[gsaLink.Property]
};

face.ApplyTaggedName(gsaLink.Name);
link.ApplyTaggedName(gsaLink.Name);
int id = gsaLink.Ref;
face.SetAdapterId(typeof(GSAId), id);
linkList.Add(face);
link.SetAdapterId(typeof(GSAId), id);
linkList.Add(link);

}
return linkList;
Expand Down
12 changes: 5 additions & 7 deletions GSA_Adapter/Convert/FromGsa/Elements/Spacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static List<Spacer> FromGsaSpacers(IEnumerable<GsaElement> gsaElements, Dictionary<string, SpacerProperty> spaProps, Dictionary<string, Node> nodes)
public static List<Spacer> FromGsaSpacers(IEnumerable<GsaElement> gsaElements, Dictionary<int, SpacerProperty> spaProps, Dictionary<int, Node> nodes)
{
List<Spacer> spacerList = new List<Spacer>();

Expand All @@ -56,16 +56,14 @@ public static List<Spacer> FromGsaSpacers(IEnumerable<GsaElement> gsaElements, D
continue;

Node n1, n2;
nodes.TryGetValue(gsaSpacer.Topo[0].ToString(), out n1);
nodes.TryGetValue(gsaSpacer.Topo[1].ToString(), out n2);
nodes.TryGetValue(gsaSpacer.Topo[0], out n1);
nodes.TryGetValue(gsaSpacer.Topo[1], out n2);

Spacer spacer = new Spacer { StartNode = n1, EndNode = n2 };
spacer.ApplyTaggedName(gsaSpacer.Name);



SpacerProperty prop;
spaProps.TryGetValue(gsaSpacer.Property.ToString(), out prop);
SpacerProperty prop;
spaProps.TryGetValue(gsaSpacer.Property, out prop);

spacer.SpacerProperty = prop;

Expand Down
4 changes: 2 additions & 2 deletions GSA_Adapter/Convert/FromGsa/Loads/LoadCombination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static LoadCombination FromGsaAnalTask(string gsaString, Dictionary<string, Loadcase> lCases)
public static LoadCombination FromGsaAnalTask(string gsaString, Dictionary<int, Loadcase> lCases)
{

if (string.IsNullOrWhiteSpace(gsaString))
Expand All @@ -61,7 +61,7 @@ public static LoadCombination FromGsaAnalTask(string gsaString, Dictionary<strin
lCaseParam[0] = "1.0";

Loadcase templCase;
if (!lCases.TryGetValue(lCaseParam[1], out templCase))
if (!lCases.TryGetValue(int.Parse(lCaseParam[1]), out templCase))
{
templCase = new Loadcase { Number = int.Parse(lCaseParam[1]), Nature = LoadNature.Other };
templCase.SetAdapterId(typeof(GSAId), templCase.Number);
Expand Down
7 changes: 6 additions & 1 deletion GSA_Adapter/Convert/FromGsa/Properties/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ public static IMaterialFragment FromGsaMaterial(string gsaString)
return null;
}

int id = int.Parse(gStr[1]);
string id = gStr[1];

#if GSA_10_1
id = gsaString.Split(("_.").ToCharArray())[1] + ":" + id;
#endif

mat.SetAdapterId(typeof(GSAId),id);

return mat;
Expand Down

0 comments on commit 9d710e8

Please sign in to comment.