diff --git a/AdSecGH/Components/2_Rebar/CreateRebarGroup.cs b/AdSecGH/Components/2_Rebar/CreateRebarGroup.cs index 548ee559..a5eae042 100644 --- a/AdSecGH/Components/2_Rebar/CreateRebarGroup.cs +++ b/AdSecGH/Components/2_Rebar/CreateRebarGroup.cs @@ -165,28 +165,28 @@ protected override void SolveInternal(IGH_DataAccess da) { // top if (Params.Input[0].SourceCount != 0) { var grp = ITemplateGroup.Create(ITemplateGroup.Face.Top); - grp.Layers = AdSecInput.ILayers(this, da, 0); + grp.Layers = this.GetILayers(da, 0); groups.Add(new AdSecRebarGroupGoo(grp)); } // left if (Params.Input[1].SourceCount != 0) { var grp = ITemplateGroup.Create(ITemplateGroup.Face.LeftSide); - grp.Layers = AdSecInput.ILayers(this, da, 1); + grp.Layers = this.GetILayers(da, 1); groups.Add(new AdSecRebarGroupGoo(grp)); } // right if (Params.Input[2].SourceCount != 0) { var grp = ITemplateGroup.Create(ITemplateGroup.Face.RightSide); - grp.Layers = AdSecInput.ILayers(this, da, 2); + grp.Layers = this.GetILayers(da, 2); groups.Add(new AdSecRebarGroupGoo(grp)); } // bottom if (Params.Input[3].SourceCount != 0) { var grp = ITemplateGroup.Create(ITemplateGroup.Face.Bottom); - grp.Layers = AdSecInput.ILayers(this, da, 3); + grp.Layers = this.GetILayers(da, 3); groups.Add(new AdSecRebarGroupGoo(grp)); } @@ -203,7 +203,7 @@ protected override void SolveInternal(IGH_DataAccess da) { // top if (Params.Input[0].SourceCount != 0) { var grp = IPerimeterGroup.Create(); - grp.Layers = AdSecInput.ILayers(this, da, 0); + grp.Layers = this.GetILayers(da, 0); groups.Add(new AdSecRebarGroupGoo(grp)); } diff --git a/AdSecGH/Components/2_Rebar/CreateRebarLayout.cs b/AdSecGH/Components/2_Rebar/CreateRebarLayout.cs index 6d7bbd86..bd4b82e7 100644 --- a/AdSecGH/Components/2_Rebar/CreateRebarLayout.cs +++ b/AdSecGH/Components/2_Rebar/CreateRebarLayout.cs @@ -234,7 +234,7 @@ protected override void SolveInternal(IGH_DataAccess da) { case FoldMode.SingleBars: // create single rebar group var bars = ISingleBars.Create(this.GetAdSecRebarBundleGoo(da, 0).Value); - bars.Positions = AdSecInput.IPoints(this, da, 1); + bars.Positions = this.GetIPoints(da, 1); group = new AdSecRebarGroupGoo(bars); break; diff --git a/AdSecGH/Helpers/AdSecInput.cs b/AdSecGH/Helpers/AdSecInput.cs index 244b0a8a..a3dd0873 100644 --- a/AdSecGH/Helpers/AdSecInput.cs +++ b/AdSecGH/Helpers/AdSecInput.cs @@ -24,9 +24,9 @@ namespace AdSecGH.Helpers { internal static class AdSecInput { - internal static List Covers(GH_Component owner, IGH_DataAccess DA, int inputid, LengthUnit docLengthUnit) { + internal static List Covers(GH_Component owner, IGH_DataAccess DA, int inputId, LengthUnit docLengthUnit) { var covers = new List(); - var lengths = Input.UnitNumberList(owner, DA, inputid, docLengthUnit); + var lengths = Input.UnitNumberList(owner, DA, inputId, docLengthUnit); foreach (var length in lengths.Select(v => (Length)v)) { covers.Add(ICover.Create(length)); @@ -35,111 +35,32 @@ internal static List Covers(GH_Component owner, IGH_DataAccess DA, int i return covers; } - internal static Oasys.Collections.IList ILayers( - GH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) { - var grps = Oasys.Collections.IList.Create(); - var gh_typs = new List(); - if (DA.GetDataList(inputid, gh_typs)) { - for (int i = 0; i < gh_typs.Count; i++) { - if (gh_typs[i].Value is ILayer layer) { - grps.Add(layer); - } else if (gh_typs[i].Value is AdSecRebarLayerGoo rebarGoo) { - grps.Add(rebarGoo.Value); - } else { - owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, - $"Unable to convert {owner.Params.Input[inputid].NickName} (item {i}) to RebarLayer"); - } - } - - return grps; - } - - if (!isOptional) { - owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, - $"Input parameter {owner.Params.Input[inputid].NickName} failed to collect data!"); - } - - return null; - } - - internal static Oasys.Collections.IList IPoints( - GH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) { - var pts = Oasys.Collections.IList.Create(); - var gh_typs = new List(); - if (DA.GetDataList(inputid, gh_typs)) { - var tempPts = new List(); - for (int i = 0; i < gh_typs.Count; i++) { - Curve polycurve = null; - var ghpt = new Point3d(); - - if (gh_typs[i].Value is IPoint point) { - pts.Add(point); - } else if (gh_typs[i].Value is AdSecPointGoo sspt) { - pts.Add(sspt.AdSecPoint); - } else if (GH_Convert.ToPoint3d(gh_typs[i].Value, ref ghpt, GH_Conversion.Both)) { - tempPts.Add(ghpt); - } else if (GH_Convert.ToCurve(gh_typs[i].Value, ref polycurve, GH_Conversion.Both)) { - var curve = (PolylineCurve)polycurve; - pts = AdSecPointGoo.PtsFromPolylineCurve(curve); - } else { - owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, - $"Unable to convert {owner.Params.Input[inputid].NickName} (item {i}) to StressStrainPoint or Polyline"); - } - } - - if (tempPts.Count > 0) { - if (tempPts.Count == 1) { - pts.Add(AdSecPointGoo.CreateFromPoint3d(tempPts[0], Plane.WorldYZ)); - owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, - "Single Point converted to local point. Assumed that local coordinate system is in a YZ-Plane"); - } else { - Plane.FitPlaneToPoints(tempPts, out var plane); - foreach (var pt in tempPts) { - pts.Add(AdSecPointGoo.CreateFromPoint3d(pt, plane)); - } - - owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, - "List of Points have been converted to local points. Assumed that local coordinate system is matching best-fit plane through points"); - } - } - - return pts; - } - - if (!isOptional) { - owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, - $"Input parameter {owner.Params.Input[inputid].NickName} failed to collect data!"); - } - - return null; - } - internal static AdSecRebarGroupGoo ReinforcementGroup( - GH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) { + GH_Component owner, IGH_DataAccess DA, int inputId, bool isOptional = false) { var gh_typ = new GH_ObjectWrapper(); - if (DA.GetData(inputid, ref gh_typ)) { + if (DA.GetData(inputId, ref gh_typ)) { if (gh_typ.Value is AdSecRebarGroupGoo goo) { return goo; } owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, - $"Unable to convert {owner.Params.Input[inputid].NickName} to RebarLayout"); + $"Unable to convert {owner.Params.Input[inputId].NickName} to RebarLayout"); return null; } if (!isOptional) { owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, - $"Input parameter {owner.Params.Input[inputid].NickName} failed to collect data!"); + $"Input parameter {owner.Params.Input[inputId].NickName} failed to collect data!"); } return null; } internal static List ReinforcementGroups( - GH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) { + GH_Component owner, IGH_DataAccess DA, int inputId, bool isOptional = false) { var grps = new List(); var gh_typs = new List(); - if (DA.GetDataList(inputid, gh_typs)) { + if (DA.GetDataList(inputId, gh_typs)) { for (int i = 0; i < gh_typs.Count; i++) { if (gh_typs[i].Value is IGroup group) { grps.Add(new AdSecRebarGroup(group)); @@ -147,7 +68,7 @@ internal static List ReinforcementGroups( grps.Add(rebarGoo.Value); } else { owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, - $"Unable to convert {owner.Params.Input[inputid].NickName} (item {i}) to RebarGroup"); + $"Unable to convert {owner.Params.Input[inputId].NickName} (item {i}) to RebarGroup"); } } @@ -156,38 +77,38 @@ internal static List ReinforcementGroups( if (!isOptional) { owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, - $"Input parameter {owner.Params.Input[inputid].NickName} failed to collect data!"); + $"Input parameter {owner.Params.Input[inputId].NickName} failed to collect data!"); } return null; } internal static AdSecSolutionGoo Solution( - GH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) { + GH_Component owner, IGH_DataAccess DA, int inputId, bool isOptional = false) { var gh_typ = new GH_ObjectWrapper(); - if (DA.GetData(inputid, ref gh_typ)) { + if (DA.GetData(inputId, ref gh_typ)) { if (gh_typ.Value is AdSecSolutionGoo goo) { return goo; } owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, - $"Unable to convert {owner.Params.Input[inputid].NickName} to AdSec Results"); + $"Unable to convert {owner.Params.Input[inputId].NickName} to AdSec Results"); return null; } if (!isOptional) { owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, - $"Input parameter {owner.Params.Input[inputid].NickName} failed to collect data!"); + $"Input parameter {owner.Params.Input[inputId].NickName} failed to collect data!"); } return null; } internal static Oasys.Collections.IList SubComponents( - GH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) { + GH_Component owner, IGH_DataAccess DA, int inputId, bool isOptional = false) { var subs = Oasys.Collections.IList.Create(); var gh_typs = new List(); - if (DA.GetDataList(inputid, gh_typs)) { + if (DA.GetDataList(inputId, gh_typs)) { for (int i = 0; i < gh_typs.Count; i++) { if (gh_typs[i].Value is ISubComponent component) { subs.Add(component); @@ -199,7 +120,7 @@ internal static Oasys.Collections.IList SubComponents( subs.Add(sub); } else { owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, - $"Unable to convert {owner.Params.Input[inputid].NickName} (item {i}) to SubComponent or Section"); + $"Unable to convert {owner.Params.Input[inputId].NickName} (item {i}) to SubComponent or Section"); } } @@ -208,7 +129,7 @@ internal static Oasys.Collections.IList SubComponents( if (!isOptional) { owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, - $"Input parameter {owner.Params.Input[inputid].NickName} failed to collect data!"); + $"Input parameter {owner.Params.Input[inputId].NickName} failed to collect data!"); } return null; @@ -338,6 +259,105 @@ public static bool TryCastToConcreteCrackCalculationParameters( return castSuccessful; } + public static bool TryCastToIPoints( + List ghTypes, IList iPoints, List invalidIds, ref int pointsConverted) { + invalidIds = invalidIds ?? new List(); + IPoint point = null; + if (ghTypes == null || ghTypes.Count == 0) { + return false; + } + + var temporaryPoints = new List(); + + for (int i = 0; i < ghTypes.Count; i++) { + Curve curve = null; + var ghpt = new Point3d(); + + if (TryCastToIPoint(ghTypes[i], ref point)) { + iPoints.Add(point); + } else if (TryCastToPoint3d(ghTypes[i], ref ghpt)) { + temporaryPoints.Add(ghpt); + } else if (TryCastToCurve(ghTypes[i], ref curve)) { + iPoints = AdSecPointGoo.PtsFromPolylineCurve((PolylineCurve)curve); + } else { + invalidIds.Add(i); + } + } + + ProcessTemporaryPoints(ref iPoints, ref temporaryPoints); + + pointsConverted = temporaryPoints.Count; + return !invalidIds.Any(); + } + + public static void ProcessTemporaryPoints(ref IList iPoints, ref List temporaryPoints) { + iPoints = iPoints ?? new List(); + temporaryPoints = temporaryPoints ?? new List(); + + if (temporaryPoints.Count == 1) { + iPoints.Add(AdSecPointGoo.CreateFromPoint3d(temporaryPoints[0], Plane.WorldYZ)); + } else if (temporaryPoints.Count > 1) { + Plane.FitPlaneToPoints(temporaryPoints, out var plane); + foreach (var point in temporaryPoints) { + iPoints.Add(AdSecPointGoo.CreateFromPoint3d(point, plane)); + } + } + } + + public static bool TryCastToPoint3d(GH_ObjectWrapper ghType, ref Point3d point) { + return GH_Convert.ToPoint3d(ghType.Value, ref point, GH_Conversion.Both); + } + + public static bool TryCastToCurve(GH_ObjectWrapper ghType, ref Curve curve) { + return GH_Convert.ToCurve(ghType.Value, ref curve, GH_Conversion.Both); + } + + public static bool TryCastToIPoint(GH_ObjectWrapper objectWrapper, ref IPoint iPoint) { + switch (objectWrapper.Value) { + case IPoint point: + iPoint = point; + break; + case AdSecPointGoo adSecPointGoo: + iPoint = adSecPointGoo.AdSecPoint; + break; + default: return false; + } + + return true; + } + + public static bool TryCastToILayers(List ghTypes, IList iLayers, List invalidIds) { + invalidIds = invalidIds ?? new List(); + ILayer layer = null; + if (ghTypes == null || ghTypes.Count == 0) { + return false; + } + + for (int i = 0; i < ghTypes.Count; i++) { + if (TryCastToILayer(ghTypes[i], ref layer)) { + iLayers.Add(layer); + } else { + invalidIds.Add(i); + } + } + + return !invalidIds.Any(); + } + + public static bool TryCastToILayer(GH_ObjectWrapper objectWrapper, ref ILayer iLayer) { + switch (objectWrapper.Value) { + case ILayer layer: + iLayer = layer; + break; + case AdSecRebarLayerGoo rebarGoo: + iLayer = rebarGoo.Value; + break; + default: return false; + } + + return true; + } + public static bool TryCastToStressStrainCurve( bool compression, GH_ObjectWrapper gh_typ, ref AdSecStressStrainCurveGoo curveGoo) { bool castSuccess = true; diff --git a/AdSecGH/Helpers/Extensions/GHComponentExtensions.cs b/AdSecGH/Helpers/Extensions/GHComponentExtensions.cs index 6ac11468..4040688d 100644 --- a/AdSecGH/Helpers/Extensions/GHComponentExtensions.cs +++ b/AdSecGH/Helpers/Extensions/GHComponentExtensions.cs @@ -8,6 +8,7 @@ using Oasys.AdSec.Materials; using Oasys.AdSec.Materials.StressStrainCurves; +using Oasys.AdSec.Reinforcement.Layers; using Oasys.Profiles; using OasysGH.Units; @@ -165,6 +166,52 @@ public static IConcreteCrackCalculationParameters GetIConcreteCrackCalculationPa return calculationParameters; } + public static Oasys.Collections.IList GetILayers( + this GH_Component owner, IGH_DataAccess DA, int inputId, bool isOptional = false) { + var inputData = new List(); + + DA.GetDataList(inputId, inputData); + bool isDataAvailable = inputData.TrueForAll(item => item != null); + var layers = Oasys.Collections.IList.Create(); + var invalidIds = new List(); + + if (!isDataAvailable && !isOptional) { + owner.Params.Input[inputId].FailedToCollectDataWarning(); + } else if (isDataAvailable && !AdSecInput.TryCastToILayers(inputData, layers, invalidIds)) { + invalidIds.ForEach(id => owner.Params.Input[inputId].ConvertFromToError($"(item {id})", "RebarLayer")); + } + + return layers.Any() ? layers : null; + } + + public static Oasys.Collections.IList GetIPoints( + this GH_Component owner, IGH_DataAccess DA, int inputId, bool isOptional = false) { + var inputData = new List(); + var points = Oasys.Collections.IList.Create(); + var invalidIds = new List(); + int pointsConverted = 0; + + DA.GetDataList(inputId, inputData); + bool isDataAvailable = inputData.TrueForAll(item => item != null); + + if (!isDataAvailable && !isOptional) { + owner.Params.Input[inputId].FailedToCollectDataWarning(); + } else if (isDataAvailable && !AdSecInput.TryCastToIPoints(inputData, points, invalidIds, ref pointsConverted)) { + invalidIds.ForEach(id + => owner.Params.Input[inputId].ConvertFromToError($"(item {id})", "StressStrainPoint or Polyline")); + } + + if (pointsConverted == 1) { + owner.AddRuntimeRemark( + "Single Point converted to local point. Assumed that local coordinate system is in a YZ-Plane"); + } else if (pointsConverted > 1) { + owner.AddRuntimeRemark( + "List of Points have been converted to local points. Assumed that local coordinate system is matching best fit plane through points"); + } + + return points.Any() ? points : null; + } + public static AdSecStressStrainCurveGoo GetStressStrainCurveGoo( this GH_Component owner, IGH_DataAccess DA, int inputId, bool compression, bool isOptional = false) { AdSecStressStrainCurveGoo curveGoo = null; diff --git a/AdSecGHTests/Helpers/AdSecInputTests/ILayersTests.cs b/AdSecGHTests/Helpers/AdSecInputTests/ILayersTests.cs new file mode 100644 index 00000000..465e6ce5 --- /dev/null +++ b/AdSecGHTests/Helpers/AdSecInputTests/ILayersTests.cs @@ -0,0 +1,133 @@ +using System.Collections.Generic; + +using AdSecGH.Helpers; +using AdSecGH.Parameters; + +using Grasshopper.Kernel.Types; + +using Oasys.AdSec.Reinforcement; +using Oasys.AdSec.Reinforcement.Layers; +using Oasys.AdSec.StandardMaterials; + +using OasysUnits; + +using Xunit; + +namespace AdSecGHTests.Helpers { + [Collection("GrasshopperFixture collection")] + public class AdSecInputTests_ILayersTests { + private Oasys.Collections.IList _layers; + private List invalidIds; + + public AdSecInputTests_ILayersTests() { + _layers = Oasys.Collections.IList.Create(); + invalidIds = new List(); + } + + [Fact] + public void TryCastToILayersReturnsFalseWhenInvalidIdsListIsNullInitialised() { + bool castSuccessful = AdSecInput.TryCastToILayers(null, _layers, null); + + Assert.False(castSuccessful); + Assert.Empty(_layers); + Assert.Empty(invalidIds); + } + + [Fact] + public void TryCastToILayersReturnsFalseWhenNull() { + bool castSuccessful = AdSecInput.TryCastToILayers(null, _layers, invalidIds); + + Assert.False(castSuccessful); + Assert.Empty(_layers); + Assert.Empty(invalidIds); + } + + [Fact] + public void TryCastToILayersReturnsFalseWhenEmptySections() { + var objectWrappers = new List(); + bool castSuccessful = AdSecInput.TryCastToILayers(objectWrappers, _layers, invalidIds); + + Assert.False(castSuccessful); + Assert.Empty(_layers); + Assert.Empty(invalidIds); + } + + [Fact] + public void TryCastToILayersReturnsFalseWhenValueIsNull() { + ILayer layer = null; + var objectWrappers = new List() { + new GH_ObjectWrapper(layer), + }; + bool castSuccessful = AdSecInput.TryCastToILayers(objectWrappers, _layers, invalidIds); + + Assert.False(castSuccessful); + Assert.Empty(_layers); + Assert.Single(invalidIds); + Assert.Equal(0, invalidIds[0]); + } + + [Fact] + public void TryCastToILayersReturnsEmptyWhenNullItemSections() { + var objectWrapper = new GH_ObjectWrapper(null); + var objectWrappers = new List() { + objectWrapper, + }; + bool castSuccessful = AdSecInput.TryCastToILayers(objectWrappers, _layers, invalidIds); + + Assert.False(castSuccessful); + Assert.Empty(_layers); + Assert.Single(invalidIds); + Assert.Equal(0, invalidIds[0]); + } + + [Fact] + public void TryCastToILayersReturnsFalseWhenSecondItemIncorrect() { + var layer = GetLayer(); + var objectWrappers = new List() { + new GH_ObjectWrapper(layer), + new GH_ObjectWrapper(null), + }; + bool castSuccessful = AdSecInput.TryCastToILayers(objectWrappers, _layers, invalidIds); + + Assert.False(castSuccessful); + Assert.Single(_layers); + Assert.Single(invalidIds); + Assert.Equal(1, invalidIds[0]); + } + + [Fact] + public void TryCastToILayersReturnsCorrectDataFromRebarLayerGoo() { + var layer = GetLayer(); + var input = new AdSecRebarLayerGoo(layer); + + var objwrap = new List() { + new GH_ObjectWrapper(input), + }; + bool castSuccessful = AdSecInput.TryCastToILayers(objwrap, _layers, invalidIds); + + Assert.True(castSuccessful); + Assert.NotNull(_layers); + Assert.Empty(invalidIds); + } + + [Fact] + public void TryCastToILayersReturnsCorrectDataFromILayer() { + var input = GetLayer(); + + var objwrap = new List() { + new GH_ObjectWrapper(input), + }; + bool castSuccessful = AdSecInput.TryCastToILayers(objwrap, _layers, invalidIds); + + Assert.True(castSuccessful); + Assert.NotNull(_layers); + Assert.Empty(invalidIds); + } + + private static ILayerByBarCount GetLayer() { + var layer = ILayerByBarCount.Create(2, + IBarBundle.Create(Reinforcement.Steel.IS456.Edition_2000.S415, Length.FromMillimeters(1))); + return layer; + } + } +} diff --git a/AdSecGHTests/Helpers/AdSecInputTests/IPointsTests.cs b/AdSecGHTests/Helpers/AdSecInputTests/IPointsTests.cs new file mode 100644 index 00000000..92c9a1b5 --- /dev/null +++ b/AdSecGHTests/Helpers/AdSecInputTests/IPointsTests.cs @@ -0,0 +1,281 @@ +using System.Collections.Generic; + +using AdSecGH.Helpers; +using AdSecGH.Parameters; + +using Grasshopper.Kernel.Types; + +using Oasys.AdSec.Materials.StressStrainCurves; +using Oasys.Profiles; + +using OasysUnits; +using OasysUnits.Units; + +using Rhino.Geometry; + +using Xunit; + +namespace AdSecGHTests.Helpers { + [Collection("GrasshopperFixture collection")] + public class AdSecInputTests_IPointsTests { + private Oasys.Collections.IList _points; + private List invalidIds; + private int pointsConverted = 0; + private readonly IPoint _sample = IPoint.Create(new Length(1, LengthUnit.Meter), new Length(2, LengthUnit.Meter)); + + public AdSecInputTests_IPointsTests() { + _points = Oasys.Collections.IList.Create(); + invalidIds = new List(); + pointsConverted = 0; + } + + [Fact] + public void TryCastToIPointsReturnsFalseWhenInvalidIdsListIsNullInitialised() { + bool castSuccessful = AdSecInput.TryCastToIPoints(null, _points, null, ref pointsConverted); + + Assert.False(castSuccessful); + Assert.Empty(_points); + Assert.Empty(invalidIds); + Assert.Equal(0, pointsConverted); + } + + [Fact] + public void TryCastToIPointsReturnsFalseWhenNull() { + bool castSuccessful = AdSecInput.TryCastToIPoints(null, _points, invalidIds, ref pointsConverted); + + Assert.False(castSuccessful); + Assert.Empty(_points); + Assert.Empty(invalidIds); + Assert.Equal(0, pointsConverted); + } + + [Fact] + public void TryCastToIPointsReturnsFalseWhenEmptySections() { + var objectWrappers = new List(); + bool castSuccessful = AdSecInput.TryCastToIPoints(objectWrappers, _points, invalidIds, ref pointsConverted); + + Assert.False(castSuccessful); + Assert.Empty(_points); + Assert.Empty(invalidIds); + Assert.Equal(0, pointsConverted); + } + + [Fact] + public void TryCastToIPointsReturnsFalseWhenValueIsNull() { + IPoint point = null; + var objectWrappers = new List() { + new GH_ObjectWrapper(point), + }; + bool castSuccessful = AdSecInput.TryCastToIPoints(objectWrappers, _points, invalidIds, ref pointsConverted); + + Assert.False(castSuccessful); + Assert.Empty(_points); + Assert.Single(invalidIds); + Assert.Equal(0, invalidIds[0]); + Assert.Equal(0, pointsConverted); + } + + [Fact] + public void TryCastToIPointsReturnsEmptyWhenNullItem() { + var objectWrapper = new GH_ObjectWrapper(null); + var objectWrappers = new List() { + objectWrapper, + }; + bool castSuccessful = AdSecInput.TryCastToIPoints(objectWrappers, _points, invalidIds, ref pointsConverted); + + Assert.False(castSuccessful); + Assert.Empty(_points); + Assert.Single(invalidIds); + Assert.Equal(0, invalidIds[0]); + Assert.Equal(0, pointsConverted); + } + + [Fact] + public void TryCastToIPointsReturnsFalseWhenSecondItemIncorrect() { + var objectWrappers = new List() { + new GH_ObjectWrapper(_sample), + new GH_ObjectWrapper(null), + }; + bool castSuccessful = AdSecInput.TryCastToIPoints(objectWrappers, _points, invalidIds, ref pointsConverted); + + Assert.False(castSuccessful); + Assert.Single(_points); + Assert.Single(invalidIds); + Assert.Equal(1, invalidIds[0]); + Assert.Equal(0, pointsConverted); + } + + [Fact] + public void TryCastToIPointsReturnsCorrectDataFromIPoint() { + var objwrap = new List() { + new GH_ObjectWrapper(_sample), + }; + bool castSuccessful = AdSecInput.TryCastToIPoints(objwrap, _points, invalidIds, ref pointsConverted); + + Assert.True(castSuccessful); + Assert.NotNull(_points); + Assert.Empty(invalidIds); + Assert.Equal(0, pointsConverted); + } + + [Fact] + public void TryCastToIPointsReturnsCorrectDataFromPoint3ds() { + var objwrap = new List() { + new GH_ObjectWrapper(new Point3d(1, 1, 1)), + new GH_ObjectWrapper(new Point3d(2, 2, 2)), + }; + bool castSuccessful = AdSecInput.TryCastToIPoints(objwrap, _points, invalidIds, ref pointsConverted); + + Assert.True(castSuccessful); + Assert.NotNull(_points); + Assert.Empty(invalidIds); + Assert.Equal(2, pointsConverted); + } + + [Fact] + public void TryCastToIPointsReturnsCorrectDataFromCurve() { + IStressStrainCurve crv = ILinearStressStrainCurve.Create( + IStressStrainPoint.Create(new Pressure(0, PressureUnit.Pascal), new Strain(1, StrainUnit.Ratio))); + var tuple = AdSecStressStrainCurveGoo.Create(crv, AdSecStressStrainCurveGoo.StressStrainCurveType.Linear, false); + + var objectWrapper = new GH_ObjectWrapper(tuple.Item1); + var objwrap = new List() { + objectWrapper, + }; + bool castSuccessful = AdSecInput.TryCastToIPoints(objwrap, _points, invalidIds, ref pointsConverted); + + Assert.True(castSuccessful); + Assert.NotNull(_points); + Assert.Empty(invalidIds); + Assert.Equal(0, pointsConverted); + } + + #region helpers + + [Fact] + public void TryCastToIPointReturnsFalseWhenCantConvert() { + IPoint point = null; + bool castSuccessful = AdSecInput.TryCastToIPoint(new GH_ObjectWrapper(null), ref point); + + Assert.False(castSuccessful); + Assert.Null(point); + } + + [Fact] + public void TryCastToIPointReturnsIPointWhenInputIsIPoint() { + IPoint point = null; + bool castSuccessful = AdSecInput.TryCastToIPoint(new GH_ObjectWrapper(_sample), ref point); + + Assert.True(castSuccessful); + Assert.Equal(_sample, point); + } + + [Fact] + public void TryCastToIPointReturnsIPointWhenInputIsAdSecPointGoo() { + var input = new AdSecPointGoo(_sample); + + IPoint point = null; + bool castSuccessful = AdSecInput.TryCastToIPoint(new GH_ObjectWrapper(input), ref point); + + Assert.True(castSuccessful); + Assert.Equal(input.AdSecPoint, point); + } + + [Fact] + public void TryCastToCurveReturnsFalseWhenCantConvert() { + Curve curve = null; + bool castSuccessful = AdSecInput.TryCastToCurve(new GH_ObjectWrapper(null), ref curve); + + Assert.False(castSuccessful); + Assert.Null(curve); + } + + [Fact] + public void TryCastToCurveReturnsTrueWhenDataIsCorrect() { + IStressStrainCurve crv = ILinearStressStrainCurve.Create( + IStressStrainPoint.Create(new Pressure(0, PressureUnit.Pascal), new Strain(1, StrainUnit.Ratio))); + var tuple = AdSecStressStrainCurveGoo.Create(crv, AdSecStressStrainCurveGoo.StressStrainCurveType.Linear, false); + + var objectWrapper = new GH_ObjectWrapper(tuple.Item1); + Curve curve = null; + bool castSuccessful = AdSecInput.TryCastToCurve(objectWrapper, ref curve); + + Assert.True(castSuccessful); + Assert.NotNull(curve); + } + + [Fact] + public void TryCastToPoint3dReturnsFalseWhenCantConvert() { + var point3d = Point3d.Unset; + bool castSuccessful = AdSecInput.TryCastToPoint3d(new GH_ObjectWrapper(null), ref point3d); + + Assert.False(castSuccessful); + Assert.False(point3d.IsValid); + } + + [Fact] + public void TryCastToPoint3dReturnsTrueWhenDataIsCorrect() { + var point3d = Point3d.Unset; + bool castSuccessful = AdSecInput.TryCastToPoint3d(new GH_ObjectWrapper(new Point3d()), ref point3d); + + Assert.True(castSuccessful); + Assert.True(point3d.IsValid); + } + + [Fact] + public void ProcessTemporaryPointsReturnsEmptyPointsForNullLists() { + IList iPoints = null; + List temporaryPoints = null; + + AdSecInput.ProcessTemporaryPoints(ref iPoints, ref temporaryPoints); + + Assert.Empty(iPoints); + Assert.Empty(temporaryPoints); + } + + [Fact] + public void ProcessTemporaryPointsReturnsEmptyPoints() { + IList iPoints = new List(); + var temporaryPoints = new List(); + + AdSecInput.ProcessTemporaryPoints(ref iPoints, ref temporaryPoints); + + Assert.Empty(iPoints); + Assert.Empty(temporaryPoints); + } + + [Fact] + public void ProcessTemporaryPointsReturnsPointForSingleTemporaryPoint() { + IList iPoints = new List(); + var temporaryPoints = new List() { + new Point3d(), + }; + + AdSecInput.ProcessTemporaryPoints(ref iPoints, ref temporaryPoints); + + Assert.Single(iPoints); + Assert.Single(temporaryPoints); + Assert.Equal(0, iPoints[0].Y.Value); + Assert.Equal(0, iPoints[0].Z.Value); + } + + [Fact] + public void ProcessTemporaryPointsReturnsPoints() { + IList iPoints = new List(); + var temporaryPoints = new List() { + new Point3d(new Vector3d(1, 1, 1)), + new Point3d(new Vector3d(2, 2, 2)), + }; + + AdSecInput.ProcessTemporaryPoints(ref iPoints, ref temporaryPoints); + + Assert.Equal(2, iPoints.Count); + Assert.Equal(2, temporaryPoints.Count); + Assert.Equal(0.001, iPoints[0].Y.Value); + Assert.Equal(0.001, iPoints[0].Z.Value); + } + + #endregion + + } +} diff --git a/AdSecGHTests/Helpers/Extensions/ILayersTests.cs b/AdSecGHTests/Helpers/Extensions/ILayersTests.cs new file mode 100644 index 00000000..6e85b3d9 --- /dev/null +++ b/AdSecGHTests/Helpers/Extensions/ILayersTests.cs @@ -0,0 +1,104 @@ +using AdSecGH.Parameters; + +using Grasshopper.Kernel; + +using Oasys.AdSec.Reinforcement; +using Oasys.AdSec.Reinforcement.Layers; +using Oasys.AdSec.StandardMaterials; + +using OasysUnits; + +using TestGrasshopperObjects.Extensions; + +using Xunit; + +namespace AdSecGHTests.Helpers.Extensions { + [Collection("GrasshopperFixture collection")] + public class ILayersTests { + private ILayersTestComponent _component; + private readonly string _failToRetrieveDataWarning = "failed"; + private readonly string _convertDataError = "convert"; + + public ILayersTests() { + _component = new ILayersTestComponent(); + } + + [Fact] + public void ReturnsWarningWhenInputIsNonOptionalAndNoDataAvailable() { + _component.Optional = false; + object obj = null; + ComponentTestHelper.SetInput(_component, obj); + object result = ComponentTestHelper.GetOutput(_component); + Assert.Null(result); + + var runtimeWarnings = _component.RuntimeMessages(GH_RuntimeMessageLevel.Warning); + + Assert.Single(runtimeWarnings); + Assert.Contains(runtimeWarnings, item => item.Contains(_failToRetrieveDataWarning)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Error)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Remark)); + } + + [Fact] + public void ReturnsNoMessagesWhenInputIsOptionalAndNoDataAvailable() { + _component.Optional = true; + object obj = null; + ComponentTestHelper.SetInput(_component, obj); + + object result = ComponentTestHelper.GetOutput(_component); + + Assert.Null(result); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Error)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Warning)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Remark)); + } + + [Fact] + public void ReturnsErrorWhenDataIncorrectInputIsOptional() { + _component.Optional = true; + ComponentTestHelper.SetInput(_component, string.Empty); + + object result = ComponentTestHelper.GetOutput(_component); + Assert.Null(result); + + var runtimeMessages = _component.RuntimeMessages(GH_RuntimeMessageLevel.Error); + + Assert.Single(runtimeMessages); + Assert.Contains(runtimeMessages, item => item.Contains(_convertDataError)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Warning)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Remark)); + } + + [Fact] + public void ReturnsErrorWhenDataIncorrectInputIsNonOptional() { + _component.Optional = false; + ComponentTestHelper.SetInput(_component, string.Empty); + + object result = ComponentTestHelper.GetOutput(_component); + Assert.Null(result); + + var runtimeMessages = _component.RuntimeMessages(GH_RuntimeMessageLevel.Error); + + Assert.Single(runtimeMessages); + Assert.Contains(runtimeMessages, item => item.Contains(_convertDataError)); + Assert.Contains(runtimeMessages, item => item.Contains("item 0")); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Warning)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Remark)); + } + + [Fact] + public void ReturnsILayerWhenDataCorrect() { + var layer = ILayerByBarCount.Create(2, + IBarBundle.Create(Reinforcement.Steel.IS456.Edition_2000.S415, Length.FromMillimeters(1))); + var input = new AdSecRebarLayerGoo(layer); + ComponentTestHelper.SetInput(_component, input); + + object result = ComponentTestHelper.GetOutput(_component); + Assert.NotNull(result); + + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Error)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Warning)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Remark)); + } + } +} diff --git a/AdSecGHTests/Helpers/Extensions/IPointsTests.cs b/AdSecGHTests/Helpers/Extensions/IPointsTests.cs new file mode 100644 index 00000000..8488379d --- /dev/null +++ b/AdSecGHTests/Helpers/Extensions/IPointsTests.cs @@ -0,0 +1,132 @@ +using System.Collections.Generic; + +using AdSecGH.Parameters; + +using Grasshopper.Kernel; + +using Rhino.Geometry; + +using TestGrasshopperObjects.Extensions; + +using Xunit; + +namespace AdSecGHTests.Helpers.Extensions { + [Collection("GrasshopperFixture collection")] + public class IPointsTests { + private IpointsTestComponent _component; + private readonly string _failToRetrieveDataWarning = "failed"; + private readonly string _convertDataError = "convert"; + + public IPointsTests() { + _component = new IpointsTestComponent(); + } + + [Fact] + public void ReturnsWarningWhenInputIsNonOptionalAndNoDataAvailable() { + _component.Optional = false; + object obj = null; + ComponentTestHelper.SetInput(_component, obj); + object result = ComponentTestHelper.GetOutput(_component); + Assert.Null(result); + + var runtimeWarnings = _component.RuntimeMessages(GH_RuntimeMessageLevel.Warning); + + Assert.Single(runtimeWarnings); + Assert.Contains(runtimeWarnings, item => item.Contains(_failToRetrieveDataWarning)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Error)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Remark)); + } + + [Fact] + public void ReturnsNoMessagesWhenInputIsOptionalAndNoDataAvailable() { + _component.Optional = true; + object obj = null; + ComponentTestHelper.SetInput(_component, obj); + + object result = ComponentTestHelper.GetOutput(_component); + + Assert.Null(result); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Error)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Warning)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Remark)); + } + + [Fact] + public void ReturnsErrorWhenDataIncorrectInputIsOptional() { + _component.Optional = true; + ComponentTestHelper.SetInput(_component, new List()); + + object result = ComponentTestHelper.GetOutput(_component); + Assert.Null(result); + + var runtimeMessages = _component.RuntimeMessages(GH_RuntimeMessageLevel.Error); + + Assert.Single(runtimeMessages); + Assert.Contains(runtimeMessages, item => item.Contains(_convertDataError)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Warning)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Remark)); + } + + [Fact] + public void ReturnsErrorWhenDataIncorrectInputIsNonOptional() { + _component.Optional = false; + ComponentTestHelper.SetInput(_component, new List()); + + object result = ComponentTestHelper.GetOutput(_component); + Assert.Null(result); + + var runtimeMessages = _component.RuntimeMessages(GH_RuntimeMessageLevel.Error); + + Assert.Single(runtimeMessages); + Assert.Contains(runtimeMessages, item => item.Contains(_convertDataError)); + Assert.Contains(runtimeMessages, item => item.Contains("item 0")); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Warning)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Remark)); + } + + [Fact] + public void ReturnsIPointWhenDataCorrect() { + ComponentTestHelper.SetInput(_component, new AdSecPointGoo(new Point3d())); + + object result = ComponentTestHelper.GetOutput(_component); + Assert.NotNull(result); + + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Error)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Warning)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Remark)); + } + + [Fact] + public void ReturnsIPointWhenDataCorrectButShowRemarkForSinglePoint() { + ComponentTestHelper.SetInput(_component, new Point3d()); + + object result = ComponentTestHelper.GetOutput(_component); + Assert.NotNull(result); + + var runtimeMessages = _component.RuntimeMessages(GH_RuntimeMessageLevel.Remark); + + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Error)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Warning)); + Assert.Single(runtimeMessages); + Assert.Contains(runtimeMessages, item => item.Contains("converted to local point.")); + } + + [Fact] + public void ReturnsIPointWhenDataCorrectButShowRemarkForManyPoints() { + ComponentTestHelper.SetInput(_component, new List() { + new Point3d(1, 1, 1), + new Point3d(2, 2, 2), + }); + + object result = ComponentTestHelper.GetOutput(_component); + Assert.NotNull(result); + + var runtimeMessages = _component.RuntimeMessages(GH_RuntimeMessageLevel.Remark); + + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Error)); + Assert.Empty(_component.RuntimeMessages(GH_RuntimeMessageLevel.Warning)); + Assert.Single(runtimeMessages); + Assert.Contains(runtimeMessages, item => item.Contains("List of Points")); + } + } +} diff --git a/TestGrasshopperObjects/Extensions/ILayersTestComponent.cs b/TestGrasshopperObjects/Extensions/ILayersTestComponent.cs new file mode 100644 index 00000000..f269d010 --- /dev/null +++ b/TestGrasshopperObjects/Extensions/ILayersTestComponent.cs @@ -0,0 +1,30 @@ +using System; + +using AdSecGH.Helpers; + +using Grasshopper.Kernel; + +using OasysGH; +using OasysGH.Components; + +namespace TestGrasshopperObjects.Extensions { + public class ILayersTestComponent : GH_OasysComponent { + public bool Optional { get; set; } + public ILayersTestComponent() : base("t0", "t1", "t2", "t3", "t4") { } + public override Guid ComponentGuid => Guid.NewGuid(); + + public override OasysPluginInfo PluginInfo => null; + + protected override void RegisterInputParams(GH_InputParamManager pManager) { + pManager.AddGenericParameter("test Input", "i", "input", GH_ParamAccess.list); + } + + protected override void RegisterOutputParams(GH_OutputParamManager pManager) { + pManager.AddGenericParameter("test Output", "o", "output", GH_ParamAccess.list); + } + + protected override void SolveInstance(IGH_DataAccess DA) { + DA.SetData(0, this.GetILayers(DA, 0, Optional)); + } + } +} diff --git a/TestGrasshopperObjects/Extensions/IPointsTestComponent.cs b/TestGrasshopperObjects/Extensions/IPointsTestComponent.cs new file mode 100644 index 00000000..a80997dc --- /dev/null +++ b/TestGrasshopperObjects/Extensions/IPointsTestComponent.cs @@ -0,0 +1,31 @@ +using System; + +using AdSecGH.Helpers; + +using Grasshopper.Kernel; + +using OasysGH; +using OasysGH.Components; + +namespace TestGrasshopperObjects.Extensions { + public class IpointsTestComponent : GH_OasysComponent { + public bool Optional { get; set; } + public override Guid ComponentGuid => Guid.NewGuid(); + + public override OasysPluginInfo PluginInfo => null; + public IpointsTestComponent() : base("t0", "t1", "t2", "t3", "t4") { } + + protected override void RegisterInputParams(GH_InputParamManager pManager) { + pManager.AddGenericParameter("test Input", "i", "input", GH_ParamAccess.list); + } + + protected override void RegisterOutputParams(GH_OutputParamManager pManager) { + pManager.AddGenericParameter("test Output", "o", "output", GH_ParamAccess.list); + } + + protected override void SolveInstance(IGH_DataAccess DA) { + var result = this.GetIPoints(DA, 0, Optional); + DA.SetData(0, result); + } + } +}