Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import Rhino
  2. import Grasshopper as gh
  3. import clr
  4. clr.AddReferenceToFile("KangarooSolver.dll")
  5. import KangarooSolver as ks
  6. from System import Array
  7. import subprocess
  8.  
  9. class Interrupter(ks.GoalObject):
  10.     def __init__(self, on_id):
  11.         self.PPos = Array.CreateInstance(Rhino.Geometry.Point3d, 0)
  12.         self.on_id = on_id
  13.         self.cmd = None
  14.        
  15.     def Calculate(self, p):
  16.         if self.cmd is None: # no window - open one
  17.             self.cmd = subprocess.Popen(["cmd.exe", "/c", "echo Press any key to interrupt & pause>nul"])
  18.         if self.cmd.poll() is not None: # window was triggered (closed)
  19.             for obj in ghenv.Component.OnPingDocument().Objects:
  20.                 if type(obj) == gh.Kernel.Special.GH_BooleanToggle and str(obj.InstanceGuid) in self.on_id:
  21.                     obj.Value = 0
  22.                     obj.ExpireSolution(True)
  23.             raise KeyboardInterrupt("User requested interrupt")
  24.  
  25.     def Terminate(self): # close window
  26.         if self.cmd is not None:
  27.             self.cmd.terminate()
  28.  
  29. # find guid of 'On' bool
  30. on_id = []
  31. for input in ghenv.Component.Params.Input:
  32.     if input.NickName == "On":
  33.         for s in input.Sources:
  34.             attr = s.Attributes
  35.             component = attr.GetTopLevel.DocObject
  36.             on_id.append(str(component.InstanceGuid))
  37.  
  38. try: # clean up unwanted window from previous instance
  39.     interrupter.Terminate()
  40. except NameError: # instance doesn't exist yet
  41.     pass
  42.    
  43. interrupter = Interrupter(on_id)
  44. I = interrupter # roundabout variable naming because gh will reset I each time
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement