FreeCAD-macros
119 строк · 4.5 Кб
1# This Macro, when run, will first provide the user with a file browser to
2# locate and select a .dat airfoil text file. Once selected, a chord length is
3# entered and upon pressing the OK button, a properly scaled airfoil is
4# produced. There are two versions provided here. Version 1.5 should work on
5# FreeCAD versions, 0.13 stable as well as all 0.14 versions. Version 2 should
6# only be used with FreeCAD versions equal to or greater than 0.14 3077 and
7# will work best with versions built with OCE/OCC versions 6.7 or greater (See
8# the Wiki page for all available version).
9#
10# (c) quick61
11
12__Name__ = 'Airfoil Import and Scale'
13__Comment__ = 'Imports and scales an Airfoil in the form of a Draft Wire (DWire) or Basic Spline (BSpline)'
14__Author__ = "quick61"
15__Version__ = '2.1.2'
16__Date__ = '2024-01-14'
17__License__ = ''
18__Web__ = 'http://forum.freecadweb.org/viewtopic.php?f=22&t=5554'
19__Wiki__ = 'http://www.freecadweb.org/wiki/Macro_Airfoil_Import_%26_Scale'
20__Icon__ = ''
21__Help__ = ''
22__Status__ = 'stable'
23__Requires__ = 'freecad >= 0.14.3706'
24__Communication__ = ''
25__Files__ = ''
26
27
28import FreeCAD as app
29import FreeCADGui as gui
30from PySide import QtCore, QtGui # FreeCAD's PySide!
31import Draft # FreeCAD.
32import importAirfoilDAT # From the Draft module.
33
34# Select .dat airfoil data file to be imported
35
36# PySide returns a tuple (filename, filter) instead of just a string like in PyQt
37filename, filefilter = QtGui.QFileDialog.getOpenFileName(
38gui.getMainWindow(), 'Open An Airfoil File', '*.dat')
39
40
41class AirfoilImporterAndScaler():
42
43def __init__(self):
44self.dialog = None
45
46# Make dialog box and get the scale size
47self.dialog = QtGui.QDialog(gui.getMainWindow())
48self.dialog.resize(350, 100)
49self.dialog.setWindowTitle('Airfoil Import & Scale')
50layout = QtGui.QVBoxLayout(self.dialog)
51label = QtGui.QLabel('Chord Length')
52layout.addWidget(label)
53self.line_edit_scale = QtGui.QLineEdit()
54layout.addWidget(self.line_edit_scale)
55
56# Add radio buttons to select between DWire and BSpline
57self.radio_dwire = QtGui.QRadioButton('Make DWire')
58self.radio_bspline = QtGui.QRadioButton('Make BSpline')
59
60# set default to DWire & make radio buttons - Change self.radio1.setChecked(True) to
61# self.radio2.setChecked(True) to set BSpline as default
62
63self.radio_dwire.setChecked(True)
64layout.addWidget(self.radio_dwire)
65layout.addWidget(self.radio_bspline)
66
67# Add OK / Cancel buttons
68button_box = QtGui.QDialogButtonBox(self.dialog)
69button_box.setOrientation(QtCore.Qt.Horizontal)
70button_box.setStandardButtons(
71QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)
72layout.addWidget(button_box)
73button_box.accepted.connect(self.proceed)
74button_box.rejected.connect(self.close)
75QtCore.QMetaObject.connectSlotsByName(self.dialog)
76self.dialog.show()
77self.dialog.exec_()
78
79def proceed(self):
80global filename
81if self.radio_dwire.isChecked():
82try:
83# This produces a scaled Airfoil with a DWire
84scalefactor = float(self.line_edit_scale.text())
85f1 = str(filename)
86importAirfoilDAT.insert(f1, 'Unnamed')
87Draft.scale(
88app.ActiveDocument.ActiveObject,
89delta=app.Vector(scalefactor, scalefactor, scalefactor),
90center=app.Vector(0, 0, 0),
91legacy=True)
92except Exception as e:
93app.Console.PrintError('Error, not a valid .dat file\n')
94
95self.close()
96
97if self.radio_bspline.isChecked():
98try:
99# This produces a scaled Airfoil with a BSpline
100scalefactor = float(self.line_edit_scale.text())
101f1 = str(filename)
102importAirfoilDAT.insert(f1, 'Unnamed')
103points = app.ActiveDocument.ActiveObject.Points
104Draft.makeBSpline(points, closed=True)
105Draft.scale(app.ActiveDocument.ActiveObject,
106delta=app.Vector(scalefactor, scalefactor, scalefactor),
107center=app.Vector(0, 0, 0),
108legacy=True)
109app.getDocument('Unnamed').removeObject('DWire')
110except:
111app.Console.PrintError('Error, not a valid .dat file\n')
112
113self.close()
114
115def close(self):
116self.dialog.hide()
117
118
119AirfoilImporterAndScaler()
120