forked from mcneel/rhino-developer-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleEventHandler.py
77 lines (67 loc) · 3.16 KB
/
SampleEventHandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
################################################################################
# SampleEventHandler.py
# Copyright (c) 2018 Robert McNeel & Associates.
# See License.md in the root of this repository for details.
################################################################################
import Rhino
import scriptcontext as sc
################################################################################
# SampleEventHandler class
################################################################################
class SampleEventHandler():
# Initializer
def __init__(self):
# Enable the event handlers
Rhino.RhinoDoc.AddRhinoObject += self.OnAddRhinoObject
Rhino.RhinoDoc.ReplaceRhinoObject += self.OnReplaceRhinoObject
Rhino.RhinoDoc.DeleteRhinoObject += self.OnDeleteRhinoObject
Rhino.RhinoDoc.UndeleteRhinoObject += self.OnUndeleteRhinoObject
self.Enabled = True
# Disables the event handlers
def Disable(self):
Rhino.RhinoDoc.AddRhinoObject -= self.OnAddRhinoObject
Rhino.RhinoDoc.ReplaceRhinoObject -= self.OnReplaceRhinoObject
Rhino.RhinoDoc.DeleteRhinoObject -= self.OnDeleteRhinoObject
Rhino.RhinoDoc.UndeleteRhinoObject -= self.OnUndeleteRhinoObject
self.Enabled = False
# Returns the enabled state
def IsEnabled(self):
return self.Enabled
# AddRhinoObject event handler
def OnAddRhinoObject(self, sender, e):
print '> SampleEventHandler.OnAddRhinoObject'
# ReplaceRhinoObject event handler
def OnReplaceRhinoObject(self, sender, e):
print '> SampleEventHandler.OnReplaceRhinoObject'
# DeleteRhinoObject event handler
def OnDeleteRhinoObject(self, sender, e):
print '> SampleEventHandler.OnDeleteRhinoObject'
# UndeleteRhinoObject event handler
def OnUndeleteRhinoObject(self, sender, e):
print '> SampleEventHandler.OnUndeleteRhinoObject'
################################################################################
# TestSampleEventHandler function
################################################################################
def TestSampleEventHandler():
# See if we already have a handler
if sc.sticky.has_key('sample_event_handler'):
# Delete the handler
handler = sc.sticky.pop('sample_event_handler', None)
if handler:
handler.Disable()
handler = None
print ' SampleEventHandler disabled'
else:
# Create handler
handler = SampleEventHandler()
# Add the handler to the sticky dictionary so it
# survives when the main function ends.
sc.sticky['sample_event_handler'] = handler
print ' SampleEventHandler enabled'
################################################################################
# Check to see if this file is being executed as the 'main' python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
################################################################################
if __name__ == '__main__':
TestSampleEventHandler()