Skip to content

Instantly share code, notes, and snippets.

@pgolay
Last active January 25, 2019 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgolay/e30d58cf24d149ccf37f2a83b2aac061 to your computer and use it in GitHub Desktop.
Save pgolay/e30d58cf24d149ccf37f2a83b2aac061 to your computer and use it in GitHub Desktop.
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
def ChamferCorner():
tol = sc.doc.ModelAbsoluteTolerance
def VertexFilter( rhino_object, geometry, component_index):
if component_index.ComponentIndexType == Rhino.Geometry.ComponentIndexType.BrepVertex:
return True
return False
while True:
rad = 1
if sc.sticky.has_key('VERTCHAMFER_RAD'):
rad = sc.sticky['VERTCHAMFER_RAD']
go=Rhino.Input.Custom.GetObject()
go.SubObjectSelect = True
go.BottomObjectPreference = True
go.ChooseOneQuestion = True
go.AcceptNumber(True, False)
go.SetCommandPrompt("Select corner")
go.SetCustomGeometryFilter (VertexFilter)
opRad = Rhino.Input.Custom.OptionDouble(rad)
go.AddOptionDouble('Setback', opRad)
ret = go.Get()
if go.CommandResult() != Rhino.Commands.Result.Success:
return go.CommandResult()
if ret == Rhino.Input.GetResult.Option:
rad = opRad.CurrentValue
sc.sticky['VERTCHAMFER_RAD'] = rad
continue
if ret == Rhino.Input.GetResult.Number:
rad = go.Number()
sc.sticky['VERTCHAMFER_RAD'] = rad
continue
if ret == Rhino.Input.GetResult.Object:
break
objref = go.Object(0)
idx = objref.GeometryComponentIndex.Index
pt = objref.Geometry().Location
brep= sc.doc.Objects.Find(objref.ObjectId)
geo = brep.Geometry
edgeIdcs = geo.Vertices[idx].EdgeIndices()
edges = [geo.Edges[n] for n in edgeIdcs]
crvs = [edge.ToNurbsCurve() for edge in edges]
pts = []
sph = Rhino.Geometry.Sphere(pt, rad).ToNurbsSurface()
for crv in crvs:
int = Rhino.Geometry.Intersect.Intersection.CurveSurface(crv, sph, tol, tol)
if len(int) == 0:
print "Can't find a needed intersection. The setback may be larger than the neighboring edge length."
return
pts.append(int[0].PointA)
rc, plane = Rhino.Geometry.Plane.FitPlaneToPoints(pts)
planePt = plane.ClosestPoint(pt)
planeMid = plane.PointAt(.5,.5)
vecDir = planePt-planeMid
if plane.ZAxis.IsParallelTo(pt-planePt) == 1:plane.Flip()
bb = Rhino.Geometry.BoundingBox(pts)
intervalX = Rhino.Geometry.Interval(0, bb.Diagonal.Length)
intervalY = Rhino.Geometry.Interval(0, bb.Diagonal.Length)
planeBrep = Rhino.Geometry.PlaneSurface(plane, intervalX, intervalY).ToBrep()
planeBrep.Translate(planePt - planeBrep.Faces[0].PointAt(intervalX.Mid,intervalY.Mid))
bln = Rhino.Geometry.Brep.CreateBooleanDifference(geo, planeBrep, tol)
if bln:
sc.doc.Objects.Replace(objref.ObjectId, bln[0])
sc.doc.Views.Redraw()
if __name__=='__main__':ChamferCorner()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment