forked from mcneel/rhino-developer-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleEtoModelessForm.py
226 lines (200 loc) · 8.31 KB
/
SampleEtoModelessForm.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
################################################################################
# SampleEtoModelessForm.py
# Copyright (c) 2017 Robert McNeel & Associates.
# See License.md in the root of this repository for details.
################################################################################
import System
import Rhino
import Rhino.UI
import Eto.Drawing as drawing
import Eto.Forms as forms
import scriptcontext as sc
################################################################################
# SampleEtoModelessForm class
################################################################################
class SampleEtoModelessForm(forms.Form):
# Initializer
def __init__(self):
self.m_selecting = False
# Basic form initialization
self.Initialize()
# Create the form's controls
self.CreateFormControls()
# Fill the form's listbox
self.FillListBox()
# Create Rhino event handlers
self.CreateEvents()
# Basic form initialization
def Initialize(self):
self.Title = 'Sample Modeless Form'
self.Padding = drawing.Padding(5)
self.Resizable = True
self.Maximizable = False
self.Minimizable = False
self.ShowInTaskbar = False
self.MinimumSize = drawing.Size(200, 150)
# FormClosed event handler
self.Closed += self.OnFormClosed
# Adds a listitem to the listbox
def AddObject(self, obj):
item = forms.ListItem()
item.Text = obj.ShortDescription(False)
if obj.Name:
item.Text += " - " + obj.Name
item.Tag = obj.Id
self.m_listbox.Items.Add(item)
# Fills the listbox with document objects
def FillListBox(self):
iter = Rhino.DocObjects.ObjectEnumeratorSettings()
iter.NormalObjects = True
iter.LockedObjects = False
iter.IncludeLights = True
iter.IncludeGrips = False
objects = sc.doc.Objects.GetObjectList(iter)
for obj in objects:
self.AddObject(obj)
# CloseDocument event handler
def OnCloseDocument(self, sender, e):
self.m_listbox.Items.Clear()
# NewDocument event handler
def OnNewDocument(self, sender, e):
self.FillListBox()
# EndOpenDocument event handler
def OnEndOpenDocument(self, sender, e):
self.FillListBox()
# OnAddRhinoObject event handler
def OnAddRhinoObject(self, sender, e):
self.AddObject(e.TheObject)
# OnDeleteRhinoObject event handler
def OnDeleteRhinoObject(self, sender, e):
for item in self.m_listbox.Items:
if item.Tag == e.ObjectId:
self.m_listbox.Items.Remove(item)
break
# OnSelectObjects event handler
def OnSelectObjects(self, sender, e):
if self.m_selecting == True:
return
if e.RhinoObjects.Length == 1:
i = 0
for item in self.m_listbox.Items:
if item.Tag == e.RhinoObjects[0].Id:
self.m_listbox.SelectedIndex = i
break
else:
i += 1
else:
self.m_listbox.SelectedIndex = -1
# OnDeselectAllObjects event handler
def OnDeselectAllObjects(self, sender, e):
if self.m_selecting == True:
return
self.m_listbox.SelectedIndex = -1
# Create Rhino event handlers
def CreateEvents(self):
Rhino.RhinoDoc.CloseDocument += self.OnCloseDocument
Rhino.RhinoDoc.NewDocument += self.OnNewDocument
Rhino.RhinoDoc.EndOpenDocument += self.OnEndOpenDocument
Rhino.RhinoDoc.AddRhinoObject += self.OnAddRhinoObject
Rhino.RhinoDoc.DeleteRhinoObject += self.OnDeleteRhinoObject
Rhino.RhinoDoc.SelectObjects += self.OnSelectObjects
Rhino.RhinoDoc.DeselectAllObjects += self.OnDeselectAllObjects
# Remove Rhino event handlers
def RemoveEvents(self):
Rhino.RhinoDoc.CloseDocument -= self.OnCloseDocument
Rhino.RhinoDoc.NewDocument -= self.OnNewDocument
Rhino.RhinoDoc.EndOpenDocument -= self.OnEndOpenDocument
Rhino.RhinoDoc.AddRhinoObject -= self.OnAddRhinoObject
Rhino.RhinoDoc.DeleteRhinoObject -= self.OnDeleteRhinoObject
Rhino.RhinoDoc.SelectObjects -= self.OnSelectObjects
Rhino.RhinoDoc.DeselectAllObjects -= self.OnDeselectAllObjects
# Create all of the controls used by the form
def CreateFormControls(self):
# Create table layout
layout = forms.TableLayout()
layout.Padding = drawing.Padding(10)
layout.Spacing = drawing.Size(5, 5)
# Add controls to layout
layout.Rows.Add(forms.Label(Text = 'Rhino Objects:'))
layout.Rows.Add(self.CreateListBoxRow())
layout.Rows.Add(self.CreateButtonRow())
# Set the content
self.Content = layout
# Listbox.SelectedIndexChanged event handler
def OnSelectedIndexChanged(self, sender, e):
index = self.m_listbox.SelectedIndex
if index >= 0:
self.m_selecting = True
item = self.m_listbox.Items[index]
Rhino.RhinoApp.RunScript("_SelNone", False)
Rhino.RhinoApp.RunScript("_SelId " + item.Tag.ToString() + " _Enter", False)
self.m_selecting = False
# Called by CreateFormControls to creates the
# table row that contains the listbox
def CreateListBoxRow(self):
# Create the listbox
self.m_listbox = forms.ListBox()
self.m_listbox.Size = drawing.Size(200, 100)
self.m_listbox.SelectedIndexChanged += self.OnSelectedIndexChanged
# Create the table row
table_row = forms.TableRow()
table_row.ScaleHeight = True
table_row.Cells.Add(self.m_listbox)
return table_row
# 'Select' button click handler
def OnSelectClick(self, sender, e):
self.m_selecting = True
self.m_listbox.SelectedIndex = -1
Rhino.RhinoApp.RunScript("_SelAll", False)
self.m_selecting = False
# 'Clear' button click handler
def OnClearClick(self, sender, e):
self.m_selecting = True
self.m_listbox.SelectedIndex = -1
Rhino.RhinoApp.RunScript("_SelNone", False)
self.m_selecting = False
# Called by CreateFormControls to creates the
# table row that contains the button controls.
def CreateButtonRow(self):
# Select button
select_button = forms.Button(Text = 'Select All')
select_button.Click += self.OnSelectClick
# Clear button
clear_button = forms.Button(Text = 'Clear')
clear_button.Click += self.OnClearClick
# Create layout
layout = forms.TableLayout(Spacing = drawing.Size(5, 5))
layout.Rows.Add(forms.TableRow(None, select_button, clear_button, None))
return layout
# Form Closed event handler
def OnFormClosed(self, sender, e):
# Remove the events added in the initializer
self.RemoveEvents()
# Dispose of the form and remove it from the sticky dictionary
if sc.sticky.has_key('sample_modeless_form'):
form = sc.sticky['sample_modeless_form']
if form:
form.Dispose()
form = None
sc.sticky.Remove('sample_modeless_form')
################################################################################
# TestSampleEtoModelessForm function
################################################################################
def TestSampleEtoModelessForm():
# See if the form is already visible
if sc.sticky.has_key('sample_modeless_form'):
return
# Create and show form
form = SampleEtoModelessForm()
form.Owner = Rhino.UI.RhinoEtoApp.MainWindow
form.Show()
# Add the form to the sticky dictionary so it
# survives when the main function ends.
sc.sticky['sample_modeless_form'] = form
################################################################################
# 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__':
TestSampleEtoModelessForm()