using System;
using System.Collections.Generic;
using System.Linq;
using Rhino;
using Rhino.Commands;
using Rhino.FileIO;
using Rhino.Geometry;
using Rhino.Input;
using Rhino.Input.Custom;
using Rhino.DocObjects;
using System.IO;
namespace DiscourseSolution
{
public class DiscourseSolutionCommand : Command
{
public DiscourseSolutionCommand()
{
// Rhino only creates one instance of each command class defined in a
// plug-in, so it is safe to store a refence in a static property.
Instance = this;
}
///The only instance of this command.
public static DiscourseSolutionCommand Instance
{
get; private set;
}
///The command name as it appears on the Rhino command line.
public override string EnglishName
{
get { return "BrepCapFail"; }
}
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
string filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "example.3dm");
Brep brep = PlaceBrepFromFile(filepath);
Point3d origin = new Point3d(0, 50, 0);
Plane targetPlane = new Plane(origin, -Plane.WorldYZ.Normal);
Transform transform = Transform.PlaneToPlane(Plane.WorldXY, targetPlane);
brep.Transform(transform);
origin = new Point3d(500, 0, 0);
Plane cuttingPlane = new Plane(origin, Plane.WorldYZ.Normal);
cuttingPlane.Rotate(Math.PI * 0.25, cuttingPlane.YAxis);
brep = Cut(cuttingPlane, brep);
//PaintPlane(cuttingPlane, doc);
doc.Objects.AddBrep(brep);
return Result.Success;
}
private static Brep Cut(Plane cutter, Brep brep)
{
Brep ctr = GetPlaneSurface(cutter, 8000, 8000, true).ToBrep();
Brep[] trimResults = brep.Split(ctr, 0.01);
if (trimResults != null && trimResults.Length > 0)
{
brep = trimResults.OrderBy(tr => LongestEdgeLength(tr)).ToList()[0];
brep.CapPlanarHoles(0.01);
if (brep.SolidOrientation == BrepSolidOrientation.Inward)
brep.Flip();
}
return brep;
}
private static Surface GetPlaneSurface(Plane plane, double uDir, double vDir, bool fromMidPoint = false)
{
if (uDir < 0) { uDir = uDir * -1; }
if (vDir < 0) { vDir = vDir * -1; }
double lowBoundU, highBoundU, lowBoundV, highBoundV;
if (fromMidPoint)
{
lowBoundU = uDir / -2;
highBoundU = uDir / 2;
lowBoundV = vDir / -2;
highBoundV = vDir / 2;
}
else
{
lowBoundU = 0;
highBoundU = uDir;
lowBoundV = 0;
highBoundV = vDir;
}
Interval uInterval = new Interval(lowBoundU, highBoundU);
Interval vInterval = new Interval(lowBoundV, highBoundV);
Surface planeSurface = new PlaneSurface(plane, uInterval, vInterval);
return planeSurface;
}
private static Brep PlaceBrepFromFile(string filepath, string layername = "Solid")
{
File3dm file = File3dm.Read(filepath, File3dm.TableTypeFilter.None, File3dm.ObjectTypeFilter.Brep);
File3dmObject[] objs = file.Objects.FindByLayer(layername);
List breps = new List();
foreach (File3dmObject obj in objs)
{
if(obj.Geometry.ObjectType == ObjectType.Brep)
breps.Add(obj.Geometry as Brep);
}
if (breps.Count == 1)
return breps[0];
else
return breps.OrderBy(b => VolumeMassProperties.Compute(b).Volume).ToList()[0];
}
private static void PaintPlane(Plane plane, RhinoDoc doc)
{
Line xAxis = new Line(plane.Origin, plane.XAxis, 500);
Line yAxis = new Line(plane.Origin, plane.YAxis, 500);
ObjectAttributes atts = new ObjectAttributes
{
ColorSource = ObjectColorSource.ColorFromObject,
ObjectColor = System.Drawing.Color.Red,
Name = "X Axis"
};
doc.Objects.AddLine(xAxis, atts);
atts.ObjectColor = System.Drawing.Color.Green;
atts.Name = "Y Axis";
doc.Objects.AddLine(yAxis, atts);
}
private static double LongestEdgeLength(Brep brep)
{
BrepEdge edge = brep.Edges.OrderByDescending(e => e.GetLength()).ToList()[0];
return edge.GetLength();
}
}
}