Skip to content

Instantly share code, notes, and snippets.

@pgolay
Last active August 19, 2019 23:21
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/0a36e74581b3ed4650e92d9136642644 to your computer and use it in GitHub Desktop.
Save pgolay/0a36e74581b3ed4650e92d9136642644 to your computer and use it in GitHub Desktop.
import rhinoscriptsyntax as rs
import Rhino
import scriptcontext as sc
import System.Drawing.Color
import itertools
class ConnectorConduit(Rhino.Display.DisplayConduit):
def __init__(self, lines):
self.lines = lines
self.m_dirty = False
self.Start()
# Start the conduit
def Start(self):
self.Enabled = True
# Stop the conduit
def Stop(self):
self.Enabled = False
def OnCloseDocument(self, sender, e):
self.Stop()
self.lines = None
if sc.sticky.has_key("ConnecrorConduit"):
sc.sticky.Remove("ConnecrorConduit")
def DrawForeground(self, e):
lineColor = System.Drawing.Color.Chartreuse
for line in self.lines:
e.Display.DrawLine(line[0], line[1], lineColor, 3)
def Proximity():
R1 = 2
R2 = 5
if sc.sticky.has_key('R1'):
R1 = sc.sticky['R1']
if sc.sticky.has_key('R2'):
R2 = sc.sticky['R2']
ids = rs.GetObjects("Select curves", filter=4, preselect=True)
if not ids: return
rs.UnselectAllObjects()
pts = [rs.CurveEndPoint(id) for id in ids] + [rs.CurveStartPoint(id) for id in ids]
conduit = None
while True:
lines = []
pairs = itertools.combinations(range(len(pts)),2)
for pair in pairs:
if R1< pts[pair[0]].DistanceTo(pts[pair[1]])< R2:
lines.append([pts[pair[0]],pts[pair[1]]])
if conduit:
conduit.Stop()
conduit=None
conduit = ConnectorConduit(lines)
sc.doc.Views.Redraw()
if sc.sticky.has_key("ConnectorConduit"):
conduit = sc.sticky["ConnectorConduit"]
if conduit.IsValid():
conduit.Start()
sc.doc.Views.Redraw()
else:
conduit = None
sc.sticky.Remove("ConnectorConduit")
if sc.sticky.has_key('R1'):
R1 = sc.sticky['R1']
if sc.sticky.has_key('R2'):
R2 = sc.sticky['R2']
go = Rhino.Input.Custom.GetOption()
opMin = Rhino.Input.Custom.OptionDouble(R1)
opMax = Rhino.Input.Custom.OptionDouble(R2)
go.AddOptionDouble("FartherThan" , opMin)
go.AddOptionDouble("CloserThan" , opMax)
go.AcceptNothing(True)
go.SetCommandPrompt("Set min and max search distances.")
rc = go.Get()
if ( go.CommandResult() != Rhino.Commands.Result.Success ):
conduit.Stop()
conduit=None
return
if rc==Rhino.Input.GetResult.Option:
R1 = opMin.CurrentValue
R2 = opMax.CurrentValue
sc.sticky['R1']= R1
sc.sticky['R2']= R2
continue
if rc==Rhino.Input.GetResult.Nothing:
conduit.Stop()
conduit=None
break
rs.EnableRedraw(False)
newIds = [rs.AddLine(line[0], line[1]) for line in lines]
if len(newIds) > 0:
rs.SelectObjects(newIds)
rs.EnableRedraw(True)
if __name__ == '__main__':Proximity()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment