Skip to content

Commit

Permalink
Merge branch 'main' into feature/GSAGH-367-unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kpne authored Oct 9, 2023
2 parents 3389194 + ca70397 commit e36ee5c
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 29 deletions.
10 changes: 4 additions & 6 deletions GsaGH/Components/GraveyardComp/CreateProp2d2_OBSOLETE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,17 @@ protected override void SolveInternal(IGH_DataAccess da) {
if (ghTyp.Value is GsaMaterialGoo materialGoo) {
prop.Material = GsaMaterialFactory.CreateMaterial(materialGoo.Value);
} else {
if (GH_Convert.ToInt32(ghTyp.Value, out int idd, GH_Conversion.Both)) {
prop.Material = new GsaReferencedMaterial(idd);
if (GH_Convert.ToInt32(ghTyp.Value, out int id, GH_Conversion.Both)) {
prop.Material = new GsaReferencedMaterial(id, MatType.Custom);
} else {
this.AddRuntimeError(
"Unable to convert PB input to a Section Property of reference integer");
return;
}
}
} else {
prop.Material = new GsaReferencedMaterial(2);
}
}
} else {
prop.Material = new GsaReferencedMaterial(8);
prop.Material = new GsaReferencedMaterial(1, MatType.Fabric);
}
} else {
prop.ApiProp2d.SupportType = _supportDropDown.FirstOrDefault(x => x.Value == _selectedItems[1]).Key;
Expand Down
11 changes: 8 additions & 3 deletions GsaGH/Helpers/Assembly/Properties/Materials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ private static GsaGuidDictionary<T> GetStandardMaterialDictionary<T>(
}

private int AddMaterial(GsaMaterial material) {
if (material is GsaReferencedMaterial refMat) {
return refMat.Id;
}

if (!_materials.ContainsKey(material.Guid)) {
_materials.Add(material.Guid, material);
}
Expand Down Expand Up @@ -120,7 +124,7 @@ private void AddMaterial(GsaMaterial material, ref Prop2D prop2d) {
int id = ConvertMaterial(material);

// update API prop depending on std material type
if (material is GsaCustomMaterial) {
if (material is GsaCustomMaterial || material.MaterialType == MatType.Custom) {
prop2d.MaterialGradeProperty = 0;
prop2d.MaterialAnalysisProperty = id;
} else {
Expand All @@ -139,7 +143,7 @@ private void AddMaterial(GsaMaterial material, ref Prop3D prop3d) {
int id = ConvertMaterial(material);

// update API prop depending on std material type
if (material is GsaCustomMaterial) {
if (material is GsaCustomMaterial || material.MaterialType == MatType.Custom) {
prop3d.MaterialGradeProperty = 0;
prop3d.MaterialAnalysisProperty = id;
} else {
Expand All @@ -158,7 +162,7 @@ private void AddMaterial(GsaMaterial material, ref Section section) {
int id = ConvertMaterial(material);

// update API prop depending on std material type
if (material is GsaCustomMaterial) {
if (material is GsaCustomMaterial || material.MaterialType == MatType.Custom) {
section.MaterialGradeProperty = 0;
section.MaterialAnalysisProperty = id;
} else {
Expand Down Expand Up @@ -276,6 +280,7 @@ private MaterialType GetMaterialType(GsaMaterial material) {
if (value.ToLower() == "custom") {
value = "generic";
}

return (MaterialType)Enum.Parse(typeof(MaterialType), value, true);
}

Expand Down
33 changes: 19 additions & 14 deletions GsaGH/Parameters/0_Model/GsaMaterials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,45 @@ internal GsaMaterial GetMaterial(Prop3D property) {
}

private GsaMaterial GetMaterial(MaterialType type, int analysisProp, int gradeProp) {
int id = analysisProp;
if (id != 0) { // it is a custom material
if (AnalysisMaterials.ContainsKey(id)) {
return AnalysisMaterials[id];
if (analysisProp != 0) { // it is a custom material
if (AnalysisMaterials.ContainsKey(analysisProp)) {
return AnalysisMaterials[analysisProp];
} else {
return new GsaReferencedMaterial(id);
return new GsaReferencedMaterial(analysisProp, MatType.Custom);
}
}

id = gradeProp;
switch (type) {
case MaterialType.ALUMINIUM:
return AluminiumMaterials.TryGetValue(id, out GsaMaterial aluminium) ? aluminium : null;
return AluminiumMaterials.TryGetValue(gradeProp, out GsaMaterial aluminium) ? aluminium
: new GsaReferencedMaterial(gradeProp, MatType.Aluminium);

case MaterialType.CONCRETE:
return ConcreteMaterials.TryGetValue(id, out GsaMaterial concrete) ? concrete : null;
return ConcreteMaterials.TryGetValue(gradeProp, out GsaMaterial concrete) ? concrete
: new GsaReferencedMaterial(gradeProp, MatType.Concrete);

case MaterialType.FABRIC:
return FabricMaterials.TryGetValue(id, out GsaMaterial fabric) ? fabric : null;
return FabricMaterials.TryGetValue(gradeProp, out GsaMaterial fabric) ? fabric
: new GsaReferencedMaterial(gradeProp, MatType.Fabric);

case MaterialType.FRP:
return FrpMaterials.TryGetValue(id, out GsaMaterial frp) ? frp : null;
return FrpMaterials.TryGetValue(gradeProp, out GsaMaterial frp) ? frp
: new GsaReferencedMaterial(gradeProp, MatType.Frp);

case MaterialType.GLASS:
return GlassMaterials.TryGetValue(id, out GsaMaterial glass) ? glass : null;
return GlassMaterials.TryGetValue(gradeProp, out GsaMaterial glass) ? glass
: new GsaReferencedMaterial(gradeProp, MatType.Glass);

case MaterialType.FIRST:
return SteelMaterials.TryGetValue(id, out GsaMaterial steel) ? steel : null;
return SteelMaterials.TryGetValue(gradeProp, out GsaMaterial steel) ? steel
: new GsaReferencedMaterial(gradeProp, MatType.Steel);

case MaterialType.TIMBER:
return TimberMaterials.TryGetValue(id, out GsaMaterial timber) ? timber : null;
return TimberMaterials.TryGetValue(gradeProp, out GsaMaterial timber) ? timber
: new GsaReferencedMaterial(gradeProp, MatType.Timber);

default:
return null;
return new GsaReferencedMaterial(gradeProp, GsaMaterialFactory.GetMatType(type));
}
}

Expand Down
9 changes: 9 additions & 0 deletions GsaGH/Parameters/1_Properties/GsaMaterialFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ internal static List<string> GetGradeNames(MatType type, string concreteDesignCo
return GetGradeNames(type, m);
}

internal static MatType GetMatType(MaterialType type) {
string value = type.ToString();
if (Enum.TryParse(value, true, out MatType matType)) {
return matType;
}

return MatType.Custom;
}

internal static GsaMaterial RecreateForDesignCode(GsaMaterial material, Model model) {
GsaMaterial recreation = null;

Expand Down
2 changes: 1 addition & 1 deletion GsaGH/Parameters/1_Properties/GsaMaterialParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected override GsaMaterialGoo PreferredCast(object data) {
}

if (GH_Convert.ToInt32(data, out int id, GH_Conversion.Both)) {
var customMaterial = new GsaReferencedMaterial(id);
var customMaterial = new GsaReferencedMaterial(id, MatType.Custom);
return new GsaMaterialGoo(customMaterial);
}

Expand Down
6 changes: 3 additions & 3 deletions GsaGH/Parameters/1_Properties/GsaReferencedMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ public override string Name {
set => AnalysisMaterial.Name = value;
}

internal GsaReferencedMaterial(int id) {
internal GsaReferencedMaterial(int id, MatType type) {
Id = id;
MaterialType = MatType.ReferencedById;
MaterialType = type;
}

public override string ToString() {
return $"Custom ID:{Id} (referenced)";
return $"{MaterialType} ID:{Id} (referenced)";
}
}
}
3 changes: 1 addition & 2 deletions GsaGH/Parameters/Enums/MatType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public enum MatType {
Glass,
Frp,
Timber,
Fabric,
ReferencedById
Fabric
}
}
58 changes: 58 additions & 0 deletions GsaGHTests/1_BaseParameters/1_Properties/GsaMaterialTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
using System.Collections.Generic;
using GsaAPI;
using GsaAPI.Materials;
using GsaGH.Helpers.Assembly;
using GsaGH.Parameters;
using GsaGHTests.Helpers;
using OasysUnits;
using Xunit;
using LengthUnit = OasysUnits.Units.LengthUnit;

namespace GsaGHTests.Parameters {
[Collection("GrasshopperFixture collection")]
Expand Down Expand Up @@ -128,6 +131,18 @@ public void CreateCustomMaterialTest() {
DuplicateTest(material);
}

[Fact]
public void GetMatTypeTest() {
GsaAPI.MaterialType concrete = MaterialType.CONCRETE;
Assert.Equal(MatType.Concrete, GsaMaterialFactory.GetMatType(concrete));
GsaAPI.MaterialType steel = MaterialType.STEEL;
Assert.Equal(MatType.Steel, GsaMaterialFactory.GetMatType(steel));
GsaAPI.MaterialType custom = MaterialType.GENERIC;
Assert.Equal(MatType.Custom, GsaMaterialFactory.GetMatType(custom));
GsaAPI.MaterialType other = MaterialType.NONE;
Assert.Equal(MatType.Custom, GsaMaterialFactory.GetMatType(other));
}

[Fact]
public void NonStandardMaterialException() {
Assert.Throws<Exception>(() => {
Expand All @@ -142,6 +157,49 @@ public void NonStandardMaterialGradeNameException() {
});
}

[Fact]
public void ReferenceMaterialsTest() {
var section = new GsaAPI.Section() {
MaterialType = MaterialType.ALUMINIUM,
MaterialGradeProperty = 99
};
var prop2d = new GsaAPI.Prop2D() {
MaterialType = MaterialType.TIMBER,
MaterialGradeProperty = 7
};
var prop3d = new GsaAPI.Prop3D() {
MaterialType = MaterialType.GENERIC,
MaterialGradeProperty = 42
};
var model = new GsaAPI.Model();
model.AddSection(section);
model.AddProp2D(prop2d);
model.AddProp3D(prop3d);

var gsaModel = new GsaModel(model);
GsaSection modelSection = gsaModel.Sections[1].Value;
GsaProperty2d modelProp2d = gsaModel.Prop2ds[1].Value;
GsaProperty3d modelProp3d = gsaModel.Prop3ds[1].Value;
Assert.Equal(99, modelSection.Material.Id);
Assert.Equal(7, modelProp2d.Material.Id);
Assert.Equal(42, modelProp3d.Material.Id);
Assert.Equal(MatType.Aluminium, modelSection.Material.MaterialType);
Assert.Equal(MatType.Timber, modelProp2d.Material.MaterialType);
Assert.Equal(MatType.Custom, modelProp3d.Material.MaterialType);

var assembly = new ModelAssembly(null, null, null, null, null, null, null, null, null,
null, null,
new List<GsaSection> { modelSection },
new List<GsaProperty2d> { modelProp2d },
new List<GsaProperty3d> { modelProp3d },
null, null, null, null, null, LengthUnit.Meter, Length.Zero,
false, null);
GsaAPI.Model assembled = assembly.GetModel();
Assert.Equal(99, assembled.Sections()[1].MaterialGradeProperty);
Assert.Equal(7, assembled.Prop2Ds()[1].MaterialGradeProperty);
Assert.Equal(42, assembled.Prop3Ds()[1].MaterialAnalysisProperty);
}

internal static void DuplicateTest(GsaMaterial original) {
GsaMaterial duplicate = GsaMaterialFactory.CreateMaterial(original);
Assert.NotSame(duplicate, original);
Expand Down

0 comments on commit e36ee5c

Please sign in to comment.