Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adsecgh-77: test coverage helpers batch 1 #108

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 88 additions & 164 deletions AdSecGH/Helpers/AdSecInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,13 @@
AdSecProfileGoo profileGoo = null;
var gh_typ = new GH_ObjectWrapper();
if (DA.GetData(inputid, ref gh_typ)) {
// try cast directly to quantity type
if (gh_typ.Value is AdSecProfileGoo adsecGoo) {
profileGoo = adsecGoo;
} else if (gh_typ.Value is OasysProfileGoo oasysGoo) {
var profile = AdSecProfiles.CreateProfile(oasysGoo.Value);
profileGoo = new AdSecProfileGoo(profile, Plane.WorldYZ);
} else {
return null;
//prfl = Boundaries(owner, DA, inputid, -1, DefaultUnits.LengthUnitGeometry);
}

return profileGoo;
Expand Down Expand Up @@ -184,7 +182,6 @@
GH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) {
var gh_typ = new GH_ObjectWrapper();
if (DA.GetData(inputid, ref gh_typ)) {
// try cast directly to quantity type
if (gh_typ.Value is AdSecSectionGoo a1) {
return a1.Value;
}
Expand Down Expand Up @@ -275,12 +272,10 @@
AdSecProfileFlangeGoo flange = null;
var gh_typ = new GH_ObjectWrapper();
if (DA.GetData(inputid, ref gh_typ)) {
// try cast directly to quantity type
if (gh_typ.Value is AdSecProfileFlangeGoo goo) {
flange = goo;
return flange.Value;
}
// try cast from web

if (gh_typ.Value is AdSecProfileWebGoo) {
owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Error,
Expand Down Expand Up @@ -375,8 +370,6 @@
"Single Point converted to local point. Assumed that local coordinate system is in a YZ-Plane");
} else {
Plane.FitPlaneToPoints(tempPts, out var plane);
//Polyline pol = new Polyline(tempPts);
//plane.Origin = pol.CenterPoint();
foreach (var pt in tempPts) {
pts.Add(Parameters.AdSecPointGoo.CreateFromPoint3d(pt, plane));
}
Expand Down Expand Up @@ -445,27 +438,10 @@
return null;
}

// perimeter = new AdSecProfileGoo(solid, voids, lengthUnit);
// }
// else if (owner.Params.Input[inputid_Voids].SourceCount > 0)
// {
// owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Unable to convert input " + owner.Params.Input[inputid_Voids].NickName + " input to Polyline(s)");
// return null;
// }
// }
// return (AdSecProfileGoo)perimeter;
// }
// else if (!isOptional)
// {
// owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Input parameter " + owner.Params.Input[inputid_Boundary].NickName + " failed to collect data!");
// }
// return null;
//}
internal static AdSecSolutionGoo Solution(
GH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) {
var gh_typ = new GH_ObjectWrapper();
if (DA.GetData(inputid, ref gh_typ)) {
// try cast directly to quantity type
if (gh_typ.Value is AdSecSolutionGoo goo) {
return goo;
}
Expand All @@ -484,129 +460,53 @@
}

internal static AdSecStressStrainCurveGoo StressStrainCurveGoo(
GH_Component owner, IGH_DataAccess DA, int inputid, bool compression, bool isOptional = false) {
AdSecStressStrainCurveGoo ssCrv = null;
var gh_typ = new GH_ObjectWrapper();
if (DA.GetData(inputid, ref gh_typ)) {
Curve polycurve = null;
if (gh_typ.Value is AdSecStressStrainCurveGoo goo) {
// try direct cast
ssCrv = goo;
} else if (GH_Convert.ToCurve(gh_typ.Value, ref polycurve, GH_Conversion.Both)) {
// try convert to polylinecurve
var curve = (PolylineCurve)polycurve;
var pts = AdSecStressStrainCurveGoo.StressStrainPtsFromPolyline(curve);
var exCrv = IExplicitStressStrainCurve.Create();
exCrv.Points = pts;
var tuple = AdSecStressStrainCurveGoo.Create(exCrv, AdSecStressStrainCurveGoo.StressStrainCurveType.Explicit,
compression);
ssCrv = new AdSecStressStrainCurveGoo(tuple.Item1, exCrv,
AdSecStressStrainCurveGoo.StressStrainCurveType.Explicit, tuple.Item2);
} else {
owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Error,
"Unable to convert " + owner.Params.Input[inputid].NickName + " to StressStrainCurve");
return null;
}
IGH_Component owner, IGH_DataAccess DA, int inputid, bool compression, bool isOptional = false) {
var wrapper = new List<GH_ObjectWrapper>();

return ssCrv;
}
bool isDataAvailable = TryCollectData(owner, DA, inputid, isOptional, ref wrapper);

if (!isOptional) {
owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,
"Input parameter " + owner.Params.Input[inputid].NickName + " failed to collect data!");
AdSecStressStrainCurveGoo curveGoo = null;
if (isDataAvailable && !TryCastToStressStrainCurve(compression, wrapper.First(), ref curveGoo)) {
owner.Params.Input[inputid].ConvertToError("StressStrainCurve");

Check warning on line 470 in AdSecGH/Helpers/AdSecInput.cs

View check run for this annotation

Codecov / codecov/patch

AdSecGH/Helpers/AdSecInput.cs#L470

Added line #L470 was not covered by tests
}

return null;
return curveGoo;
}

internal static IStressStrainPoint StressStrainPoint(
GH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) {
IStressStrainPoint pt1 = null;
var gh_typ = new GH_ObjectWrapper();
if (DA.GetData(inputid, ref gh_typ)) {
var ghpt = new Point3d();
if (gh_typ.Value is IStressStrainPoint point) {
pt1 = point;
} else if (gh_typ.Value is AdSecStressStrainPointGoo sspt) {
pt1 = sspt.StressStrainPoint;
} else if (GH_Convert.ToPoint3d(gh_typ.Value, ref ghpt, GH_Conversion.Both)) {
pt1 = AdSecStressStrainPointGoo.CreateFromPoint3d(ghpt);
} else {
owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Error,
"Unable to convert " + owner.Params.Input[inputid].NickName + " to StressStrainPoint");
return null;
}
IGH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) {
var wrapper = new List<GH_ObjectWrapper>();

return pt1;
}
bool isDataAvailable = TryCollectData(owner, DA, inputid, isOptional, ref wrapper);

if (!isOptional) {
owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,
"Input parameter " + owner.Params.Input[inputid].NickName + " failed to collect data!");
IStressStrainPoint stressStrainPoint = null;
if (isDataAvailable && !TryCastToStressStrainPoint(wrapper.First(), ref stressStrainPoint)) {
owner.Params.Input[inputid].ConvertToError("StressStrainPoint");

Check warning on line 484 in AdSecGH/Helpers/AdSecInput.cs

View check run for this annotation

Codecov / codecov/patch

AdSecGH/Helpers/AdSecInput.cs#L484

Added line #L484 was not covered by tests
}

return null;
return stressStrainPoint;
}

internal static Oasys.Collections.IList<IStressStrainPoint> StressStrainPoints(
GH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) {
var pts = Oasys.Collections.IList<IStressStrainPoint>.Create();
var gh_typs = new List<GH_ObjectWrapper>();
if (DA.GetDataList(inputid, gh_typs)) {
for (int i = 0; i < gh_typs.Count; i++) {
Curve polycurve = null;
var ghpt = new Point3d();
if (gh_typs[i].Value is IStressStrainPoint point) {
pts.Add(point);
} else if (gh_typs[i].Value is AdSecStressStrainPointGoo sspt) {
pts.Add(sspt.StressStrainPoint);
} else if (GH_Convert.ToPoint3d(gh_typs[i].Value, ref ghpt, GH_Conversion.Both)) {
pts.Add(AdSecStressStrainPointGoo.CreateFromPoint3d(ghpt));
} else if (GH_Convert.ToCurve(gh_typs[i].Value, ref polycurve, GH_Conversion.Both)) {
var curve = (PolylineCurve)polycurve;
pts = AdSecStressStrainCurveGoo.StressStrainPtsFromPolyline(curve);
} else {
owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,
"Unable to convert " + owner.Params.Input[inputid].NickName + " to StressStrainPoint or a Polyline");
}
}
var wrappers = new List<GH_ObjectWrapper>();

if (pts.Count < 2) {
owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,
"Input must contain at least 2 points to create an Explicit Stress Strain Curve");
return null;
}
bool isDataAvailable = TryCollectData(owner, DA, inputid, isOptional, ref wrappers);

return pts;
var points = Oasys.Collections.IList<IStressStrainPoint>.Create();
if (isDataAvailable && !TryCastToStressStrainPoints(wrappers, ref points)) {
owner.Params.Input[inputid].ConvertToError("StressStrainPoint or a Polyline");

Check warning on line 498 in AdSecGH/Helpers/AdSecInput.cs

View check run for this annotation

Codecov / codecov/patch

AdSecGH/Helpers/AdSecInput.cs#L498

Added line #L498 was not covered by tests
}

if (!isOptional) {
owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,
"Input parameter " + owner.Params.Input[inputid].NickName + " failed to collect data!");
if (points.Count >= 2) {
return points;

Check warning on line 502 in AdSecGH/Helpers/AdSecInput.cs

View check run for this annotation

Codecov / codecov/patch

AdSecGH/Helpers/AdSecInput.cs#L502

Added line #L502 was not covered by tests
}

owner.AddRuntimeWarning("Input must contain at least 2 points to create an Explicit Stress Strain Curve");

return null;
}

// for (int i = 0; i < gh_typs.Count; i++)
// {
// if (GH_Convert.ToCurve(gh_typs[i].Value, ref crv, GH_Conversion.Both))
// {
// Polyline voidCrv = null;
// if (crv.TryGetPolyline(out voidCrv))
// {
// voids.Add(voidCrv);
// }
// else
// {
// owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Unable to convert input " + owner.Params.Input[inputid_Voids].NickName + " (item " + i + ") to Polyline");
// }
// }
// else
// {
// owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Unable to convert input " + owner.Params.Input[inputid_Voids].NickName + " (item " + i + ") to Polyline");
// }
// }
internal static Oasys.Collections.IList<ISubComponent> SubComponents(
GH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional = false) {
var subs = Oasys.Collections.IList<ISubComponent>.Create();
Expand Down Expand Up @@ -643,12 +543,10 @@
AdSecProfileWebGoo web = null;
var gh_typ = new GH_ObjectWrapper();
if (DA.GetData(inputid, ref gh_typ)) {
// try cast directly to quantity type
if (gh_typ.Value is AdSecProfileWebGoo goo) {
web = goo;
return web.Value;
}
// try cast from web

if (gh_typ.Value is AdSecProfileFlangeGoo) {
owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Error,
Expand All @@ -666,44 +564,70 @@
return null;
}

//internal static AdSecProfileGoo Boundaries(GH_Component owner, IGH_DataAccess DA, int inputid_Boundary, int inputid_Voids, LengthUnit lengthUnit, bool isOptional = false)
//{
// owner.ClearRuntimeMessages();
// AdSecProfileGoo perimeter = null;
// GH_ObjectWrapper gh_typ = new GH_ObjectWrapper();
// if (DA.GetData(inputid_Boundary, ref gh_typ))
// {
// Brep brep = null;
// Curve crv = null;
// Polyline solid = null;
// if (GH_Convert.ToBrep(gh_typ.Value, ref brep, GH_Conversion.Both))
// {
// perimeter = new AdSecProfileGoo(brep, lengthUnit);
// }
// else if (GH_Convert.ToCurve(gh_typ.Value, ref crv, GH_Conversion.Both))
// {
// if (crv.TryGetPolyline(out solid))
// {
// perimeter = new AdSecProfileGoo(solid, lengthUnit);
// }
// else
// {
// owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Unable to convert input " + owner.Params.Input[inputid_Boundary].NickName + " to Polyline");
// return null;
// }
// }
// else
// {
// owner.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Unable to convert " + owner.Params.Input[inputid_Boundary].NickName + " to Boundary");
// return null;
// }

// if (inputid_Voids >= 0)
// {
// // try get voids
// List<GH_ObjectWrapper> gh_typs = new List<GH_ObjectWrapper>();
// if (DA.GetDataList(inputid_Voids, gh_typs))
// {
// List<Polyline> voids = new List<Polyline>();
public static bool TryCastToStressStrainCurve(
bool compression, GH_ObjectWrapper gh_typ, ref AdSecStressStrainCurveGoo curveGoo) {
bool castSuccess = true;
Curve polycurve = null;
if (gh_typ.Value is AdSecStressStrainCurveGoo goo) {
curveGoo = goo;
} else if (GH_Convert.ToCurve(gh_typ.Value, ref polycurve, GH_Conversion.Both)) {
var curve = (PolylineCurve)polycurve;
var pts = AdSecStressStrainCurveGoo.StressStrainPtsFromPolyline(curve);
var exCrv = IExplicitStressStrainCurve.Create();
exCrv.Points = pts;
var tuple = AdSecStressStrainCurveGoo.Create(exCrv, AdSecStressStrainCurveGoo.StressStrainCurveType.Explicit,
compression);
curveGoo = new AdSecStressStrainCurveGoo(tuple.Item1, exCrv,
AdSecStressStrainCurveGoo.StressStrainCurveType.Explicit, tuple.Item2);
} else {
castSuccess = false;
}

return castSuccess;
}

public static bool TryCastToStressStrainPoint(GH_ObjectWrapper ghType, ref IStressStrainPoint stressStrainPoint) {
bool castSuccessful = true;
var point3d = new Point3d();
if (ghType.Value is IStressStrainPoint point) {
stressStrainPoint = point;
} else if (ghType.Value is AdSecStressStrainPointGoo pointGoo) {
stressStrainPoint = pointGoo.StressStrainPoint;
} else if (GH_Convert.ToPoint3d(ghType.Value, ref point3d, GH_Conversion.Both)) {
stressStrainPoint = AdSecStressStrainPointGoo.CreateFromPoint3d(point3d);
} else {
castSuccessful = false;
}

return castSuccessful;
}

public static bool TryCastToStressStrainPoints(
List<GH_ObjectWrapper> ghTypes, ref Oasys.Collections.IList<IStressStrainPoint> points) {
foreach (var ghType in ghTypes) {
Curve polycurve = null;
IStressStrainPoint stressStrainPoint = null;
if (GH_Convert.ToCurve(ghType.Value, ref polycurve, GH_Conversion.Both)) {
var curve = (PolylineCurve)polycurve;
points = AdSecStressStrainCurveGoo.StressStrainPtsFromPolyline(curve);
} else if (TryCastToStressStrainPoint(ghType, ref stressStrainPoint)) {
points.Add(stressStrainPoint);
} else {
return false;
}
}

return points?.Count > 0;
}

private static bool TryCollectData(
IGH_Component owner, IGH_DataAccess DA, int inputid, bool isOptional, ref List<GH_ObjectWrapper> wrappers) {
bool isDataAvailable = DA.GetData(inputid, ref wrappers);
if (!isDataAvailable && !isOptional) {
owner.Params.Input[inputid].FailedToCollectDataWarning();
}

return isDataAvailable;
}
}
}
17 changes: 17 additions & 0 deletions AdSecGH/Helpers/IGHActiveObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Grasshopper.Kernel;

namespace AdSecGH.Helpers {
public static class IghActiveObjectExtensions {
public static void AddRuntimeWarning(this IGH_ActiveObject component, string message) {
component.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, message);
}

public static void AddRuntimeError(this IGH_ActiveObject component, string message) {
component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, message);
}

public static void AddRuntimeRemark(this IGH_ActiveObject component, string message) {
component.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, message);
}
}
}
18 changes: 18 additions & 0 deletions AdSecGH/Helpers/IGHParamExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Grasshopper.Kernel;

namespace AdSecGH.Helpers {
public static class GHParamExtension {
public static void ConvertToError(this IGH_Param owner, string tryCastTo) {
owner.ConvertFromToError(string.Empty, tryCastTo);
}

public static void ConvertFromToError(this IGH_Param owner, string tryCastFrom, string tryCastTo) {
string fromObjectString = string.IsNullOrEmpty(tryCastFrom) ? string.Empty : $"from {tryCastFrom} ";
owner.AddRuntimeError($"Unable to convert {owner.NickName} {fromObjectString}to {tryCastTo}");
}

public static void FailedToCollectDataWarning(this IGH_Param owner) {
owner.AddRuntimeWarning("Input parameter " + owner.NickName + " failed to collect data!");
}
}
}
Loading
Loading