Skip to content

Instantly share code, notes, and snippets.

@pgolay
Created January 30, 2019 00:10
Show Gist options
  • Save pgolay/9cf8327db1053140ce39300dd11b3da3 to your computer and use it in GitHub Desktop.
Save pgolay/9cf8327db1053140ce39300dd11b3da3 to your computer and use it in GitHub Desktop.
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
def SetLineLength():
def line_filter( rhino_object, geometry, component_index):
if geometry.IsLinear():
return True
return False
looped = False
while True:
length = 1
if sc.sticky.has_key('LINE_EX_LENGTH'):
length = sc.sticky['LINE_EX_LENGTH']
go = Rhino.Input.Custom.GetObject()
strMessage = "Pick the end of the line to move."
if looped: strMessage = "Pick the end of the line to move. Press enter to finish."
go.SetCommandPrompt(strMessage)
go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
go.SetCustomGeometryFilter(line_filter)
opLen = Rhino.Input.Custom.OptionDouble(length)
go.AddOptionDouble("Length", opLen)
go.AcceptNumber(True, False)
go.EnablePreSelect(False, True)
rc = go.Get()
if go.CommandResult() != Rhino.Commands.Result.Success:
return go.CommandResult()
if rc == Rhino.Input.GetResult.Option:
length = opLen.CurrentValue
sc.sticky['LINE_EX_LENGTH'] = length
continue
if rc == Rhino.Input.GetResult.Number:
length = go.Number()
sc.sticky['LINE_EX_LENGTH'] = length
continue
if rc == Rhino.Input.GetResult.Object:
objref = go.Object(0)
geo = objref.Geometry()
selPt = objref.SelectionPoint()
p_rc, par = geo.ClosestPoint(selPt)
if p_rc:
if par>= geo.Domain.Mid:
vec = geo.PointAtEnd-geo.PointAtStart
vec.Unitize()
geo.SetEndPoint(geo.PointAtStart + vec*length)
else:
vec = geo.PointAtStart-geo.PointAtEnd
vec.Unitize()
geo.SetStartPoint(geo.PointAtEnd + vec*length)
sc.doc.Objects.Replace (objref, geo)
print "Line length set to " + str(round(length, 3))
looped = True
sc.doc.Views.Redraw()
continue
if __name__ == '__main__':SetLineLength()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment