# This Macro, when run, will first provide the user with a file browser to
# locate and select a .dat airfoil text file. Once selected, a chord length is
# entered and upon pressing the OK button, a properly scaled airfoil is
# produced. There are two versions provided here. Version 1.5 should work on
# FreeCAD versions, 0.13 stable as well as all 0.14 versions. Version 2 should
# only be used with FreeCAD versions equal to or greater than 0.14 3077 and
# will work best with versions built with OCE/OCC versions 6.7 or greater (See
# the Wiki page for all available version).
#
# (c) quick61

__Name__ = 'Airfoil Import and Scale'
__Comment__ = 'Imports and scales an Airfoil in the form of a Draft Wire (DWire) or Basic Spline (BSpline)'
__Author__ = "quick61"
__Version__ = '2.1.2'
__Date__ = '2024-01-14'
__License__ = ''
__Web__ = 'http://forum.freecadweb.org/viewtopic.php?f=22&t=5554'
__Wiki__ = 'http://www.freecadweb.org/wiki/Macro_Airfoil_Import_%26_Scale'
__Icon__ = ''
__Help__ = ''
__Status__ = 'stable'
__Requires__ = 'freecad >= 0.14.3706'
__Communication__ = ''
__Files__ = ''


import FreeCAD as app
import FreeCADGui as gui
from PySide import QtCore, QtGui  # FreeCAD's PySide!
import Draft  # FreeCAD.
import importAirfoilDAT  # From the Draft module.

# Select .dat airfoil data file to be imported

# PySide returns a tuple (filename, filter) instead of just a string like in PyQt
filename, filefilter = QtGui.QFileDialog.getOpenFileName(
    gui.getMainWindow(), 'Open An Airfoil File', '*.dat')


class AirfoilImporterAndScaler():

    def __init__(self):
        self.dialog = None

        # Make dialog box and get the scale size
        self.dialog = QtGui.QDialog(gui.getMainWindow())
        self.dialog.resize(350, 100)
        self.dialog.setWindowTitle('Airfoil Import & Scale')
        layout = QtGui.QVBoxLayout(self.dialog)
        label = QtGui.QLabel('Chord Length')
        layout.addWidget(label)
        self.line_edit_scale = QtGui.QLineEdit()
        layout.addWidget(self.line_edit_scale)

        # Add radio buttons to select between DWire and BSpline
        self.radio_dwire = QtGui.QRadioButton('Make DWire')
        self.radio_bspline = QtGui.QRadioButton('Make BSpline')

        # set default to DWire & make radio buttons - Change self.radio1.setChecked(True) to
        # self.radio2.setChecked(True) to set BSpline as default

        self.radio_dwire.setChecked(True)
        layout.addWidget(self.radio_dwire)
        layout.addWidget(self.radio_bspline)

        # Add OK / Cancel buttons
        button_box = QtGui.QDialogButtonBox(self.dialog)
        button_box.setOrientation(QtCore.Qt.Horizontal)
        button_box.setStandardButtons(
                QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)
        layout.addWidget(button_box)
        button_box.accepted.connect(self.proceed)
        button_box.rejected.connect(self.close)
        QtCore.QMetaObject.connectSlotsByName(self.dialog)
        self.dialog.show()
        self.dialog.exec_()

    def proceed(self):
        global filename
        if self.radio_dwire.isChecked():
            try:
                # This produces a scaled Airfoil with a DWire
                scalefactor = float(self.line_edit_scale.text())
                f1 = str(filename)
                importAirfoilDAT.insert(f1, 'Unnamed')
                Draft.scale(
                        app.ActiveDocument.ActiveObject,
                        delta=app.Vector(scalefactor, scalefactor, scalefactor),
                        center=app.Vector(0, 0, 0),
                        legacy=True)
            except Exception as e:
                app.Console.PrintError('Error, not a valid .dat file\n')

            self.close()

        if self.radio_bspline.isChecked():
            try:
                # This produces a scaled Airfoil with a BSpline
                scalefactor = float(self.line_edit_scale.text())
                f1 = str(filename)
                importAirfoilDAT.insert(f1, 'Unnamed')
                points = app.ActiveDocument.ActiveObject.Points
                Draft.makeBSpline(points, closed=True)
                Draft.scale(app.ActiveDocument.ActiveObject,
                            delta=app.Vector(scalefactor, scalefactor, scalefactor),
                            center=app.Vector(0, 0, 0),
                            legacy=True)
                app.getDocument('Unnamed').removeObject('DWire')
            except:
                app.Console.PrintError('Error, not a valid .dat file\n')

            self.close()

    def close(self):
        self.dialog.hide()


AirfoilImporterAndScaler()
