/
BasicGUI.py
465 lines (310 loc) · 14.4 KB
/
BasicGUI.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#===============================================================================
#
# Basic GUI interface for simulating the orbits of ships in Eve Online.
#
# contact: wjs018@gmail.com
#
#===============================================================================
from Tkinter import *
import tkFileDialog
from ttk import *
import dbQuery as db
import ShipSimulate as sim
import dogma
import numpy as np
import matplotlib
import csv
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from operator import itemgetter
class MainWindow(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.plotNum = 0
self.initUI()
def initUI(self):
# Set window title
self.parent.title("EVE Orbits")
# Set style for widgets
Style().configure("TButton", padding=5)
# Add a label for ship selector
shipLabel = Label(self, text="Select Ship:").grid(
row=0, column=0, sticky=W, padx=(5, 5))
# Add a combobox to list ships
self.getShipList()
self.shipVar = StringVar()
self.shipCombo = Combobox(
self, textvariable=self.shipVar, state='readonly', width=30, height=20)
self.shipList = self.ships[1]
self.shipCombo['values'] = self.shipList
self.shipCombo.set("Choose One...")
self.shipCombo.grid(
row=1, column=0, columnspan=3, sticky=W, padx=(5, 5))
# Add a label for group selector
groupLabel = Label(self, text="Select Group:").grid(
row=3, column=0, sticky=W, padx=(5, 5))
# Add a combobox to list groups
# groups = self.getGroupList()
self.groups = self.getGroupList()
self.groupVar = StringVar()
self.groupCombo = Combobox(
self, textvariable=self.groupVar, state='readonly', width=30, height=15)
self.groupCombo['values'] = self.groups[1]
self.groupCombo.set("Choose One...")
self.groupCombo.grid(
row=4, column=0, columnspan=3, sticky=W, padx=(5, 5))
self.groupCombo.bind("<<ComboboxSelected>>", self.getModuleList)
# Add a label for module selector
moduleLabel = Label(self, text="Select Module").grid(
row=6, column=0, sticky=W, padx=(5, 5))
# Add a combobox for module selector
self.moduleList = [[], []]
self.moduleVar = StringVar()
self.moduleCombo = Combobox(
self, state='readonly', width=30, height=15)
self.moduleCombo['values'] = self.moduleList[1]
self.moduleCombo.set("Choose One...")
self.moduleCombo.grid(
row=7, column=0, columnspan=3, sticky=W, padx=(5, 5))
# Add a button to fit module
fitButton = Button(self, text="Fit", command=self.addModule).grid(
row=8, column=2, sticky=NE, padx=(5, 5))
# Add a label for fitted module listbox
fittedLabel = Label(self, text="Fitted Modules", padding=(5, 5, 5, 5)).grid(
row=0, column=3, sticky=W, padx=(5, 5))
# Add a listbox for the fitted modules
self.fittedBox = Listbox(self, width=50, height=20)
self.fittedList = []
self.fittedIDs = []
self.fittedBox.grid(
row=1, column=3, rowspan=10, columnspan=3, sticky=NW, pady=(5, 5), padx=(5, 5))
# Add a remove selected button
removeButton = Button(self, text="Remove Selected", command=self.removeModule).grid(
row=11, column=3, columnspan=3, sticky=SE, pady=(5, 5), padx=(5, 5))
# Add a label for max speed output
maxSpeedLabel = Label(
self, text="Max Speed (m/s):").grid(row=0, column=6, columnspan=3, sticky=SW, padx=(5, 5))
# Add an output for max speed
self.maxSpeedVar = StringVar(value="Not Calculated")
self.maxSpeedResult = Label(self, text=0, textvariable=self.maxSpeedVar, relief="sunken").grid(
row=1, column=6, columnspan=3, sticky=NW, padx=(5, 5), pady=(5, 0))
# Add a label for agility output
agilityLabel = Label(self, text="Agility:").grid(
row=2, column=6, columnspan=3, sticky=SW, pady=(5, 0), padx=(5, 5))
# Add an output for agility
self.agilityVar = StringVar(value="Not Calculated")
self.agilityResult = Label(self, text=0, textvariable=self.agilityVar, relief="sunken").grid(
row=3, column=6, columnspan=3, sticky=NW, padx=(5, 5), pady=(5, 0))
# Add a label for mass output
massLabel = Label(self, text="Mass (mill kg):").grid(
row=4, column=6, columnspan=3, sticky=SW, padx=(5, 5), pady=(5, 0))
# Add an output for mass
self.massVar = StringVar(value="Not Calculated")
self.massResult = Label(self, text=0, textvariable=self.massVar, relief="sunken").grid(
row=5, column=6, columnspan=3, sticky=NW, padx=(5, 5), pady=(5, 0))
# Add a quit button
quitButton = Button(self, text="Quit", command=self.quit).grid(
row=12, column=8, sticky=SE, padx=(5, 5), pady=(10, 5))
# Add a calculate button
calcButton = Button(self, text="Calculate!", command=self.calculateAttribs).grid(
row=12, column=6, columnspan=2, sticky=SE, padx=(5, 5), pady=(10, 5))
# Add a simulate button
simButton = Button(self, text="Simulate!", command=self.runSimulation).grid(
row=12, column=5, sticky=SE, padx=(5, 5), pady=(10, 5))
# Add a checkbox to output sim results
self.outputVar = IntVar()
self.output = Checkbutton(
self, text="Output sim results to .csv file?", variable=self.outputVar)
self.output.grid(
row=12, column=3, columnspan=2, sticky=SW, padx=(5, 5), pady=(10, 5))
# Add orbit simulation entry fields and labels
orbitSimLabel = Label(self, text="Parameters for Simulation:").grid(
row=9, column=0, columnspan=3, sticky=W, padx=(5, 5))
orbitSimRadLabel = Label(self, text="Start Radius (m):").grid(
row=10, column=0, columnspan=2, sticky=W, padx=(5, 5))
orbitSimRadEndLabel = Label(self, text="End Radius (m):").grid(
row=11, column=0, columnspan=2, sticky=W, padx=(5, 5))
orbitSimStepLabel = Label(self, text="Number of Radii:").grid(
row=12, column=0, columnspan=2, sticky=W, padx=(5, 5))
self.orbitSimRadVar = StringVar()
self.orbitSimRadEndVar = StringVar()
self.orbitSimStepVar = StringVar()
self.orbitSimRadEnt = Entry(self, textvariable=self.orbitSimRadVar).grid(
row=10, column=2, columnspan=1, sticky=E, padx=(5, 5))
self.orbitSimRadEndEnt = Entry(self, textvariable=self.orbitSimRadEndVar).grid(
row=11, column=2, columnspan=1, sticky=E, padx=(5, 5))
self.orbitSimStepEnt = Entry(self, textvariable=self.orbitSimStepVar).grid(
row=12, column=2, columnspan=1, sticky=E, padx=(5, 5))
# Render everything
self.pack()
def getShipList(self):
"""
Returns names and typeIDs of all the ships in the SDE
Format: [[shipIDs], [shipNames]]
"""
# First, get groupIDs of the Ships category (6)
groupIDs = db.getGroupIDs(6)
# Get shipIDs and shipNames
shipIDs = []
shipNames = []
for i in range(0, len(groupIDs)):
shipIDsTemp = db.getTypeIDs(groupIDs[i])
shipIDs.extend(shipIDsTemp)
for j in range(0, len(shipIDsTemp)):
shipNames.append(db.getTypeName(shipIDsTemp[j]))
# Sort by ship name alphabetically, while keeping the correct
# typeID with the correct name
shipIDs, shipNames = [
list(x) for x in zip(*sorted(zip(shipIDs, shipNames), key=itemgetter(1)))]
self.ships = [shipIDs, shipNames]
# return ships
def getGroupList(self):
"""
Returns names and groupIDs of all the relevant groups in the SDE
Format: [[groupIDs], [groupNames]]
"""
# First, define list of all relevant groups (this was done by hand)
groupIDs = [46, 763, 764, 329, 762, 78, 765, 782,
1308, 773, 1232, 300, 747, 957, 954, 955, 958, 956]
groupNames = []
for i in range(0, len(groupIDs)):
selectStr = 'groupName'
fromStr = 'invGroups'
whereStr = 'groupID = ' + str(groupIDs[i]) + ' AND published = 1'
result = db.SDEQuery(selectStr, fromStr, whereStr)
for j in range(0, len(result)):
groupNames.append(result[j][0])
# Sort by group name alphabetically, while keeping the correct
# groupID with the correct name
groupIDs, groupNames = [
list(x) for x in zip(*sorted(zip(groupIDs, groupNames), key=itemgetter(1)))]
groups = [groupIDs, groupNames]
return groups
def getModuleList(self, val):
"""
Updates the module list based off of the currently selected group.
"""
idx = self.groupCombo.current()
if idx != -1:
groupID = self.groups[0][idx]
moduleIDs = db.getTypeIDs(groupID)
moduleNames = db.getTypeNames(groupID)
self.moduleList = [moduleIDs, moduleNames]
self.moduleCombo['values'] = self.moduleList[1]
self.moduleCombo.set("Choose One...")
def addModule(self):
"""
This adds the currently selected module from self.moduleCombo
to the listbox self.fittedBox
"""
idx = self.moduleCombo.current()
if idx != -1:
self.fittedList.append(self.moduleList[1][idx])
self.fittedIDs.append(self.moduleList[0][idx])
self.fittedBox.insert(END, self.moduleList[1][idx])
def removeModule(self):
"""
This removes the currently selected module in self.fittedBox
from self.fittedBox
"""
idxs = self.fittedBox.curselection()
for i in range(0, len(idxs)):
self.fittedBox.delete(idxs[i])
self.fittedIDs.pop(i)
def calculateAttribs(self):
"""
This creates a dogma Context object and fits modules that are in
self.fittedIDs. Active effect modules will automatically be
online and active. All other modules will just be set to be online.
Calculated parameters will be output to the GUI.
"""
# Define Context object and fit the modules in self.fittedIDs
self.ctx = dogma.Context()
self.ctx.set_default_skill_level(5)
shipIdx = self.shipCombo.current()
shipID = self.ships[0][shipIdx]
self.ctx.set_ship(shipID)
for i in range(0, len(self.fittedIDs)):
if dogma.type_has_active_effects(self.fittedIDs[i]):
self.ctx.add_module(
self.fittedIDs[i], state=dogma.State.ACTIVE, charge=None)
else:
self.ctx.add_module(
self.fittedIDs[i], state=dogma.State.ONLINE, charge=None)
# Now, update calculated attributes in the GUI
self.maxSpeedVar.set("%.3f" % self.ctx.get_ship_attribute(37))
self.agilityVar.set("%.5f" % self.ctx.get_ship_attribute(70))
self.massVar.set("%.2f" % (self.ctx.get_ship_attribute(4) / 1000000))
self.parent.update_idletasks()
def runSimulation(self):
"""
This runs a 2D simulation of the current context object in an orbit
specified by the user in the GUI. The results of this simulation are
then output to the GUI.
"""
# Define some variables
self.orbitList = np.linspace(float(self.orbitSimRadVar.get()), float(
self.orbitSimRadEndVar.get()), num=self.orbitSimStepVar.get())
self.radiusList = []
self.radDiffList = []
self.velList = []
self.angList = []
for i in range(0, len(self.orbitList)):
# Run the simulation
rad, vel, ang = sim.simulateOrbit(self.ctx, self.orbitList[i])
# Store the results
self.radiusList.append(rad)
self.radDiffList.append(rad - self.orbitList[i])
self.velList.append(vel)
self.angList.append(ang)
# Save a csv file with results if box is checked
if self.outputVar.get() == 1:
outputfile = tkFileDialog.asksaveasfilename(
defaultextension=".csv")
if outputfile:
writeFile = open(outputfile, 'wb')
writer = csv.writer(writeFile, quoting=csv.QUOTE_NONNUMERIC)
writer.writerow(('Prescribed Orbit Radius (m)',
'Actual Orbit Radius (m)',
'Difference in Orbit Radii (m)',
'Orbital Speed (m/s)',
'Angular Velocity (rad/s)'))
for i in range(0, len(self.radiusList)):
writer.writerow((self.orbitList[i],
self.radiusList[i],
self.radDiffList[i],
self.velList[i],
self.angList[i]))
writeFile.close()
# Plot some results
self.plotNum += 1
# First, make a new window
graph = Toplevel()
graph.title("Graph " + str(self.plotNum))
# Create our plot and axes
figure = Figure()
ax = figure.add_subplot(111)
plot = ax.plot(self.orbitList, self.radDiffList, 'ob')
ax.set_xlabel('Prescribed Orbit Radius (m)')
ax.set_ylabel('Actual - Prescribed Difference (m)')
# Configure our graph to expand when the window is resized
Grid.rowconfigure(graph, 0, weight=1)
Grid.columnconfigure(graph, 0, weight=1)
# Draw the figure in our window
canvas = FigureCanvasTkAgg(figure, master=graph)
canvas.show()
canvas.get_tk_widget().grid(row=0, sticky=NSEW)
# Create the toolbar at the bottom of the graph and a Frame
# to hold it in
toolbarFrame = Frame(graph)
toolbarFrame.grid(row=1, sticky=W)
toolbar = NavigationToolbar2TkAgg(canvas, toolbarFrame)
toolbar.update()
def main():
matplotlib.use('TkAgg')
root = Tk()
app = MainWindow(root)
root.mainloop()
if __name__ == '__main__':
main()