Skip to content

Commit

Permalink
Fix Codeclimate issue
Browse files Browse the repository at this point in the history
  • Loading branch information
hrntsm committed Jun 3, 2022
1 parent c8eabfe commit 3e895a7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 47 deletions.
78 changes: 51 additions & 27 deletions Tunny/Component/FishMarket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class FishMarket : GH_Component
private readonly List<Plane> _tagPlanes = new List<Plane>();
private List<GH_Fish> _fishes = new List<GH_Fish>();
private double _size = 1;
private Settings _settings = new Settings();

public override GH_Exposure Exposure => GH_Exposure.secondary;

Expand Down Expand Up @@ -66,52 +67,67 @@ protected override void SolveInstance(IGH_DataAccess DA)
if (!DA.GetData(4, ref yInterval)) { return; }
if (!DA.GetData(5, ref _size)) { return; }


_settings = new Settings()
{
Plane = plane,
XNum = xNum,
XInterval = xInterval,
YInterval = yInterval
};
if (xNum <= 0)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "xNum must lager than 0");
return;
}
DA.SetDataTree(0, ArrayedGeometries(fishObjects));
}

private GH_Structure<IGH_GeometricGoo> ArrayedGeometries(List<object> fishObjects)
{
int countY = 0;
bool remainGeometry = true;
var translatedGeometries = new GH_Structure<IGH_GeometricGoo>();
var arrayedGeometries = new GH_Structure<IGH_GeometricGoo>();
_fishes = fishObjects.Select(x => (GH_Fish)x).ToList();
var fishGeometries = _fishes.Select(x => x.Value.Geometries).ToList();
while (true)
{
Vector3d yVec = plane.YAxis * (yInterval * countY);
for (int countX = 0; countX < xNum; countX++)
{
int index = countX + xNum * countY;
if (index == _fishes.Count)
{
remainGeometry = false;
break;
}

Vector3d xVec = plane.XAxis * (xInterval * countX);
if (fishGeometries[index] != null)
{
Point3d modelMinPt = GetUnionBoundingBoxMinPt(fishGeometries[index]);
foreach (GeometryBase geometry in fishGeometries[index])
{
geometry.Rotate(Vector3d.VectorAngle(Vector3d.XAxis, plane.XAxis), Vector3d.ZAxis, modelMinPt);
geometry.Translate(xVec + yVec + new Vector3d(plane.Origin) - new Vector3d(modelMinPt));
IGH_GeometricGoo geometricGoo = CreateGeometricGoo(geometry);
translatedGeometries.Append(geometricGoo, new GH_Path(0, _fishes[index].Value.ModelNumber));
}
_tagPlanes.Add(new Plane(modelMinPt - plane.YAxis * 2.5 * _size, plane.XAxis, plane.YAxis));
}
}
remainGeometry = NewMethod(countY, remainGeometry, arrayedGeometries, fishGeometries);
if (!remainGeometry)
{
break;
}
countY++;
}

DA.SetDataTree(0, translatedGeometries);
return arrayedGeometries;
}

private bool NewMethod(int countY, bool remainGeometry, GH_Structure<IGH_GeometricGoo> arrayedGeometries, List<List<GeometryBase>> fishGeometries)
{
Vector3d yVec = _settings.Plane.YAxis * (_settings.YInterval * countY);
for (int countX = 0; countX < _settings.XNum; countX++)
{
int index = countX + _settings.XNum * countY;
if (index == _fishes.Count)
{
remainGeometry = false;
break;
}
Vector3d xVec = _settings.Plane.XAxis * (_settings.XInterval * countX);
if (fishGeometries[index] != null)
{
Point3d modelMinPt = GetUnionBoundingBoxMinPt(fishGeometries[index]);
foreach (GeometryBase geometry in fishGeometries[index])
{
geometry.Rotate(Vector3d.VectorAngle(Vector3d.XAxis, _settings.Plane.XAxis), Vector3d.ZAxis, modelMinPt);
geometry.Translate(xVec + yVec + new Vector3d(_settings.Plane.Origin) - new Vector3d(modelMinPt));
IGH_GeometricGoo geometricGoo = CreateGeometricGoo(geometry);
arrayedGeometries.Append(geometricGoo, new GH_Path(0, _fishes[index].Value.ModelNumber));
}
_tagPlanes.Add(new Plane(modelMinPt - _settings.Plane.YAxis * 2.5 * _size, _settings.Plane.XAxis, _settings.Plane.YAxis));
}
}
return remainGeometry;
}

private static IGH_GeometricGoo CreateGeometricGoo(GeometryBase geometry)
Expand Down Expand Up @@ -167,5 +183,13 @@ public override void DrawViewportWires(IGH_PreviewArgs args)

protected override Bitmap Icon => Resource.FishMarket;
public override Guid ComponentGuid => new Guid("46E05DFF-8EFD-418A-AC5B-7C9F24559B2E");

private class Settings
{
public Plane Plane { get; set; }
public int XNum { get; set; }
public double XInterval { get; set; }
public double YInterval { get; set; }
}
}
}
45 changes: 25 additions & 20 deletions Tunny/Util/GrasshopperInOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,30 +290,35 @@ public List<string> GetGeometryJson()

foreach (IGH_Goo goo in ghEnumerator)
{
switch (goo)
{
case GH_Mesh mesh:
json.Add(mesh.Value.ToJSON(option));
break;
case GH_Brep brep:
json.Add(brep.Value.ToJSON(option));
break;
case GH_Curve curve:
json.Add(curve.Value.ToJSON(option));
break;
case GH_Surface surface:
json.Add(surface.Value.ToJSON(option));
break;
case GH_SubD subd:
json.Add(subd.Value.ToJSON(option));
break;
default:
throw new Exception("Tunny doesn't handle this type of geometry");
}
AddEachGHParamToJson(json, option, goo);
}
}

return json;
}

private static void AddEachGHParamToJson(List<string> json, SerializationOptions option, IGH_Goo goo)
{
switch (goo)
{
case GH_Mesh mesh:
json.Add(mesh.Value.ToJSON(option));
break;
case GH_Brep brep:
json.Add(brep.Value.ToJSON(option));
break;
case GH_Curve curve:
json.Add(curve.Value.ToJSON(option));
break;
case GH_Surface surface:
json.Add(surface.Value.ToJSON(option));
break;
case GH_SubD subd:
json.Add(subd.Value.ToJSON(option));
break;
default:
throw new Exception("Tunny doesn't handle this type of geometry");
}
}
}
}

0 comments on commit 3e895a7

Please sign in to comment.