From 4d8cb31d1fd94d2414c908aa78fabc5ea5d7d4b7 Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Sun, 30 Jun 2019 12:32:08 -0700 Subject: use subplots in creator.py for tkinter in gui.py --- creator.py | 47 ++++++++++++++++++++----------------- gui.py | 78 +++++++++++++++++++++++++++++++++++++++++--------------------- main.py | 22 +++++++++--------- 3 files changed, 89 insertions(+), 58 deletions(-) diff --git a/creator.py b/creator.py index 756a8f5..16207b1 100644 --- a/creator.py +++ b/creator.py @@ -32,7 +32,6 @@ import numpy as np from math import sin, cos, atan, sqrt import bisect as bi import matplotlib.pyplot as plt -from matplotlib.figure import Figure class Airfoil: @@ -359,29 +358,34 @@ class Stringer(Airfoil): return None -def plot_geom(airfoil): +def plot_geom(airfoil, view: False): """This function plots the airfoil's + sub-components' geometry.""" - fig = Figure() + fig, ax = plt.subplots() + ax.axis('off') + # Plot chord - x_chord = [0, airfoil.chord] - y_chord = [0, 0] - plt.plot(x_chord, y_chord, linewidth='1') + x = [0, airfoil.chord] + y = [0, 0] + fig.add_subplot(111).plot(x, y, linewidth='1') # Plot quarter chord - plt.plot(airfoil.chord / 4, 0, '.', color='g', - markersize=24, label='Quarter-chord') + fig.add_subplot(111).plot(airfoil.chord / 4, 0, + '.', color='g', markersize=24, + label='Quarter-chord') # Plot mean camber line - plt.plot(airfoil.x_c, airfoil.z_c, '-.', color='r', linewidth='2', - label='Mean camber line') + fig.add_subplot(111).plot(airfoil.x_c, airfoil.z_c, + '-.', color='r', linewidth='2', + label='Mean camber line') # Plot airfoil surfaces - plt.fill(airfoil.x, airfoil.z, color='b', linewidth='1', fill=False) + fig.add_subplot(111).plot(airfoil.x, airfoil.z, + color='b', linewidth='1') # Plot spars try: for _ in range(len(airfoil.spar.x)): x = (airfoil.spar.x[_]) y = (airfoil.spar.z[_]) - plt.plot(x, y, '-', color='y', linewidth='4') + fig.add_subplot(111).plot(x, y, '-', color='y', linewidth='4') except AttributeError: print('No spars to plot.') # Plot stringers @@ -389,21 +393,22 @@ def plot_geom(airfoil): for _ in range(0, len(airfoil.stringer.x)): x = airfoil.stringer.x[_] y = airfoil.stringer.z[_] - plt.plot(x, y, '.', color='y', markersize=12) + fig.add_subplot(111).plot(x, y, '.', color='y', markersize=12) except AttributeError: print('No stringers to plot.') # Graph formatting - plt.xlabel('X axis') - plt.ylabel('Z axis') - plot_bound = max(airfoil.x) - plt.xlim(- 0.10 * plot_bound, 1.10 * plot_bound) - plt.ylim(- (1.10 * plot_bound / 2), (1.10 * plot_bound / 2)) + ax.set(title='NACA ' + str(airfoil.naca_num) + ' airfoil', + xlabel='X axis', + ylabel='Z axis') + plt.grid(axis='both', linestyle=':', linewidth=1) plt.gca().set_aspect('equal', adjustable='box') plt.gca().legend() - plt.grid(axis='both', linestyle=':', linewidth=1) - plt.show() - return None + if view == True: + plt.show() + else: + pass + return fig, ax def main(): diff --git a/gui.py b/gui.py index 0a41d8a..3213d0c 100644 --- a/gui.py +++ b/gui.py @@ -19,39 +19,65 @@ import tkinter.ttk as ttk from matplotlib.backends.backend_tkagg import ( FigureCanvasTkAgg, NavigationToolbar2Tk) -from matplotlib.figure import Figure -import numpy as np +def make_airfoil(): + """Create airfoil instance.""" -root = tk.Tk() -root.wm_title('MAE 154B - Airfoil Design, Evaluation, Optimization') -# root.geometry('1000x400') + airfoil = creator.Airfoil.from_dimensions(100, 200) + airfoil.add_naca(2412) + airfoil.add_mass(10) -# # User inputs -# l_naca = ttk.Label(root, text='NACA Number') -# e_naca = ttk.Entry(root) -# l_chord = ttk.Label(root, text='Chord Length') -# e_chord = ttk.Entry(root) -# # Graph window + airfoil.spar = creator.Spar() + airfoil.spar.add_coord(airfoil, 0.23) + airfoil.spar.add_coord(airfoil, 0.57) + airfoil.spar.add_spar_caps(0.3) + airfoil.spar.add_mass(10) + airfoil.spar.add_webs(0.4) -fig = Figure() -t = np.arange(0, 3, .01) -fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t)) + airfoil.stringer = creator.Stringer() + airfoil.stringer.add_coord(airfoil, + 3, + 6, + 5, + 4) + airfoil.stringer.add_area(0.1) + airfoil.stringer.add_mass(5) + airfoil.stringer.add_webs(0.1) + return airfoil -canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea. -canvas.draw() -canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) -toolbar = NavigationToolbar2Tk(canvas, root) -toolbar.update() -canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) +def main(): + root = tk.Tk() + root.wm_title('MAE 154B - Airfoil Design, Evaluation, Optimization') + # root.geometry('1000x400') + # # User inputs + l_naca = ttk.Label(root, text='NACA Number') + e_naca = ttk.Entry(root) + l_chord = ttk.Label(root, text='Chord Length') + e_chord = ttk.Entry(root) + af = make_airfoil() -# # Layout -# l_naca.grid(row=0, sticky='W') -# e_naca.grid(row=0, column=1) -# l_chord.grid(row=1, sticky='W') -# e_chord.grid(row=1, column=1) + # # Graph window + fig, ax = creator.plot_geom(af, False) -root.mainloop() + canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea. + canvas.draw() + canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) + + toolbar = NavigationToolbar2Tk(canvas, root) + toolbar.update() + canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) + + # # Layout + l_naca.pack() + e_naca.pack() + l_chord.pack() + e_chord.pack() + + root.mainloop() + + +if __name__ == '__main__': + main() diff --git a/main.py b/main.py index 592202a..624d31e 100644 --- a/main.py +++ b/main.py @@ -66,20 +66,20 @@ def main(): # Define NACA airfoil coordinates and mass af.add_naca(NACA_NUM) af.add_mass(AIRFOIL_MASS) - af.info_print(2) - af.info_save(SAVE_PATH, _) + # af.info_print(2) + # af.info_save(SAVE_PATH, _) # Create spar instance af.spar = creator.Spar() # Define the spar coordinates and mass, stored in single spar object af.spar.add_coord(af, 0.23) af.spar.add_coord(af, 0.57) - # Automatically adds spar caps for every previously defined + # Automatically adds spar caps for each spar defined previously af.spar.add_spar_caps(SPAR_CAP_AREA) af.spar.add_mass(SPAR_MASS) af.spar.add_webs(SPAR_THICKNESS) - af.spar.info_print(2) - af.spar.info_save(SAVE_PATH, _) + # af.spar.info_print(2) + # af.spar.info_save(SAVE_PATH, _) # Create stringer instance af.stringer = creator.Stringer() @@ -92,24 +92,24 @@ def main(): af.stringer.add_area(STRINGER_AREA) af.stringer.add_mass(STRINGER_MASS) af.stringer.add_webs(SKIN_THICKNESS) - af.stringer.info_print(2) - af.stringer.info_save(SAVE_PATH, _) + # af.stringer.info_print(2) + # af.stringer.info_save(SAVE_PATH, _) # Plot components with matplotlib - creator.plot_geom(af) + creator.plot_geom(af, True) # Evaluator object contains airfoil analysis results. eval = evaluator.Evaluator(af) # The analysis is performed in the evaluator.py module. eval.analysis(1, 1) - eval.info_print(2) - eval.info_save(SAVE_PATH, _) + # eval.info_print(2) + # eval.info_save(SAVE_PATH, _) # evaluator.plot_geom(eval) # evaluator.plot_lift(eval) pop = generator.Population(10) - print(help(creator)) + # print(help(creator)) # print(help(evaluator)) # print(help(generator)) -- cgit v1.2.3