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

Rhinoceros_Engine: add convert methods for Extrusion #204

Merged
merged 6 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
35 changes: 29 additions & 6 deletions Rhinoceros_Engine/Convert/FromRhino.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,10 @@ public static BHG.IGeometry FromRhino(this RHG.Brep brep)

if (brep.Faces.Count == 0)
return null;

if (brep.IsSolid)
return brep.ToBHoMSolid();

if (brep.IsPlanarSurface())
{
BHG.ICurve externalEdge = RHG.Curve.JoinCurves(brep.DuplicateNakedEdgeCurves(true, false)).FirstOrDefault().FromRhino();
Expand Down Expand Up @@ -454,12 +454,35 @@ public static BHG.ISurface FromRhino(this RHG.BrepFace face)

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

public static BHG.Extrusion FromRhino(this RHG.Extrusion extrusion)
public static BHG.IGeometry FromRhino(this RHG.Extrusion extrusion)
{
if (extrusion == null) return null;

extrusion.PathLineCurve();
throw new NotImplementedException(); // TODO Rhino_Adapter conversion from Extrusion
RHG.LineCurve line = extrusion.PathLineCurve();
BHG.Vector extrVec = BH.Engine.Geometry.Create.Vector(line.PointAtStart.FromRhino(), line.PointAtEnd.FromRhino());

List<BHG.Extrusion> extrs = new List<BHG.Extrusion>();

// Exploits the fact that GetWireframe returns first the "profile" curves of the extrusion.
var profileCurves = extrusion.GetWireframe();
for (int i = 0; i < extrusion.ProfileCount; i++)
{

var profileConverted = profileCurves.ElementAt(i).FromRhino();
BHG.Extrusion extr = BH.Engine.Geometry.Create.Extrusion(profileConverted, extrVec, extrusion.IsCappedAtBottom && extrusion.IsCappedAtTop);

extrs.Add(extr);
}


if (extrs.Count == 1)
return extrs[0];

if (extrs.Count > 1)
return new BH.oM.Geometry.CompositeGeometry() { Elements = extrs.OfType<BH.oM.Geometry.IGeometry>().ToList() };

BH.Engine.Reflection.Compute.RecordError("Could not convert the extrusion.");
return null;
}


Expand Down Expand Up @@ -526,7 +549,7 @@ public static BHG.Torus FromRhino(this RHG.Torus torus)

public static BHG.Cone FromRhino(this RHG.Cone cone)
{
return new BHG.Cone { Centre = cone.BasePoint.FromRhino(), Axis = cone.Axis.FromRhino()*-1.0, Radius = cone.Radius, Height = cone.Height };
return new BHG.Cone { Centre = cone.BasePoint.FromRhino(), Axis = cone.Axis.FromRhino() * -1.0, Radius = cone.Radius, Height = cone.Height };
}

/***************************************************/
Expand Down
66 changes: 56 additions & 10 deletions Rhinoceros_Engine/Convert/ToRhino.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public static RHG.PolyCurve ToRhino(this BHG.PolyCurve bPolyCurve)
return null;

IEnumerable<RHG.Curve> parts = bPolyCurve.Curves.Select(x => x.IToRhino());

// Check if bPolycurve is made of disconnected segments
if (RHG.Curve.JoinCurves(parts).Length > 1)
return null;
Expand Down Expand Up @@ -385,8 +385,8 @@ public static RHG.GeometryBase ToRhino(this BHG.NurbsSurface surface)
RHG.Brep brep = new RHG.Brep();
int srf = brep.AddSurface(rhSurface);
RHG.BrepFace face = brep.Faces.Add(srf);
foreach(BHG.SurfaceTrim trim in surface.OuterTrims)

foreach (BHG.SurfaceTrim trim in surface.OuterTrims)
{
brep.AddBrepTrim(face, trim, RHG.BrepLoopType.Outer);
}
Expand All @@ -395,7 +395,7 @@ public static RHG.GeometryBase ToRhino(this BHG.NurbsSurface surface)
{
brep.AddBrepTrim(face, trim, RHG.BrepLoopType.Inner);
}

return brep.IsValid ? brep : null;
}
}
Expand Down Expand Up @@ -433,11 +433,57 @@ public static RHG.Brep ToRhino(this List<BHG.ISurface> surfaces)

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

[NotImplemented]
public static RHG.Extrusion ToRhino(this BHG.Extrusion extrusion)
public static RHG.GeometryBase ToRhino(this BHG.Extrusion extrusion)
{
// TODO Rhino_Adapter conversion to Extrusion
return null;
if (!extrusion.Curve.IIsPlanar())
{
BH.Engine.Reflection.Compute.RecordError("The provided BHoM Extrusion has a base curve that is not planar.");
return null;
}

var planarCurve = extrusion.Curve.IToRhino();

RHG.Plane curvePlane;
planarCurve.TryGetPlane(out curvePlane);

double angle = RHG.Vector3d.VectorAngle(curvePlane.Normal, extrusion.Direction.ToRhino());

double tolerance = 0.001;

if (angle < tolerance || (2 * Math.PI - tolerance < angle && angle < 2 * Math.PI + tolerance))
{
// It can be represented by a Rhino extrusion (which enforces perpendicularity btw Curve plane and Vector)

double extrHeight = extrusion.Direction.Length();

if (angle > Math.PI)
extrHeight = -extrHeight;

RHG.Extrusion extr = Rhino.Geometry.Extrusion.Create(planarCurve, extrHeight, extrusion.Capped);

return extr;
}

// Otherwise, provide a Sweep to cover extrusion with a base curve that is not orthogonal to the extr direction

// Create a Line to be the sweep rail. Use centroid/mid-point of base curve as start point.
RHG.Point3d centrePoint;
if (planarCurve.IsClosed)
{
var areaProp = Rhino.Geometry.AreaMassProperties.Compute(planarCurve);
centrePoint = areaProp.Centroid;
}
else
centrePoint = planarCurve.PointAt(0.5);

RHG.Point3d endPoint = centrePoint + extrusion.Direction.ToRhino();
var rail = new RHG.LineCurve(centrePoint, endPoint);

var joinedSweep = new RHG.SweepOneRail()
.PerformSweep(rail, planarCurve)
.Aggregate((b1, b2) => { b1.Join(b2, tolerance, true); return b1; });

return joinedSweep;
}

alelom marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -693,7 +739,7 @@ private static void AddBrepTrim(this RHG.Brep brep, RHG.BrepFace face, BHG.Surfa
}

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

private static int AddVertex(this RHG.Brep brep, RHG.Point3d point)
{
int id = -1;
Expand All @@ -716,7 +762,7 @@ private static int AddVertex(this RHG.Brep brep, RHG.Point3d point)
}

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

private static bool IsSameEdge(this RHG.Curve curve, RHG.BrepEdge edge)
{
double tolerance = BHG.Tolerance.Distance;
Expand Down