Skip to content

Instantly share code, notes, and snippets.

@pgolay
Last active March 17, 2022 03:30
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/bb4b158b311fe0373c84105ff99be176 to your computer and use it in GitHub Desktop.
Save pgolay/bb4b158b311fe0373c84105ff99be176 to your computer and use it in GitHub Desktop.
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
import math
"""
rebuild arc segments of a polycurve
"""
def RebuildArc(arcCrv, deg):
pi = math.pi
a = arcCrv.Arc.Angle
spans = (2*a)/pi
pts3 = 4
pts5 = 6
if 0 < spans <=.5:
pts3 = 4
elif .5 < spans <= .75:
pts3 = 5
elif .75 < spans<= 1.1:
pts3 = 6
x = int((spans-1.1)/.08)
if x<0: x = 0
pts3 += x
pts5 += x
out = None
if deg ==0:
out = arcCrv.Rebuild(pts3, 3, True)
elif deg == 1:
out = arcCrv.Rebuild(pts5, 5, True)
else:
degree = 5
pts = pts5
if a <= pi/4:
degree = 3
pts = pts3
out = arcCrv.Rebuild(pts , degree, True)
return out
def RebuildArcSegments():
"""
offer degree 3 or 5 or Auto
Auto = degree5,6 pts per 90 degrees, <=45 degree = 3 4 pts.
Degree: 6 pts per 90, if 3, reduce to four by 45 deg
"""
def filter_polycurve(rhino_object, geometry, component_index):
if isinstance(geometry, Rhino.Geometry.PolyCurve):
return True
elif isinstance(geometry, Rhino.Geometry.PolylineCurve):
return True
return False
while True:
deg = 0
degList = ['Degree3','Degree5','Auto']
delete = True
tan = True
if sc.sticky.has_key('ARC_REBUILD_DEG'):
deg = sc.sticky['ARC_REBUILD_DEG']
go = Rhino.Input.Custom.GetObject()
go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
go.SetCustomGeometryFilter (filter_polycurve)
go.AddOptionList('RebuildMode', degList, deg)
go.AcceptNothing (True)
go.AcceptNumber(True, False)
rc = go.Get()
if go.CommandResult()!=Rhino.Commands.Result.Success:
#conduit.Enabled=False
return
if rc == Rhino.Input.GetResult.Object:
oref = go.Object(0)
id = oref.ObjectId
crv = oref.Geometry()
pt = oref.SelectionPoint()
par = crv.ClosestPoint(oref.SelectionPoint())[1]
break
if rc == Rhino.Input.GetResult.Option:
idx = go.OptionIndex()
#deg = opDeg.CurrentValue
deg = go.Option().CurrentListOptionIndex
sc.sticky['ARC_REBUILD_DEG'] = deg
continue
#conduit.Enabled=False
elif rc == Rhino.Input.GetResult.Number:
num = go.Number()
if int(num) ==3:
deg = int(num)
sc.sticky['ARC_REBUILD_DEG'] = deg
#conduit.Enabled=False
elif int(num) ==5:
deg = int(num)
sc.sticky['ARC_REBUILD_DEG'] = deg
continue
elif rc == Rhino.Input.GetResult.Nothing:
#conduit.Enabled=False
return
elif rc == Rhino.Input.GetResult.Cancel:
print('Cancelled')
#conduit.Enabled=False
return
segs = [crv.SegmentCurve(n) for n in range(crv.SegmentCount)]
pc = Rhino.Geometry.PolyCurve()
for n in range(len(segs)):
if segs[n].IsArc():
segs[n] = RebuildArc(segs[n], deg)
pc.Append(segs[n])
pc.CleanUp()
sc.doc.Objects.Replace(id, pc)
sc.doc.Views.Redraw()
if __name__ == '__main__':RebuildArcSegments()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment