← 피드로
NACA 2412 Airfoil Databases are in .dat file. The problem? There are no clean .dat -> .dxf converters. The solution? I built one.
This post is directly linked to my ongoing project at the time of writing: Comparative Aerodynamic Analysis Of Wing Planforms.
I needed to model a NACA 2412 wing which had .dat coordinate files available. But OnShape needed .dxf in order to import the coordinates. I couldn’t find a clean converter online, so I decided to build one for the sake of this project. I wanted to share the script:
#plot_airfoil.py
# Desired chord length (mm)
CHORD = 200
# import necessary libraries for dxf conversion
import ezdxf
# Path to airfoil data file
import os
script_dir = os.path.dirname(os.path.abspath(__file__))
filename = os.path.join(script_dir, "..", "airfoil", "naca2412.dat")
#Open the file
with open(filename, 'r') as file:
lines = file.readlines()
#Print every line
#for line in lines:
# print(line)
import matplotlib.pyplot as plt
x = []
y = []
with open(filename, 'r') as file:
lines = file.readlines()
for line in lines[1:]: #Skip the NACA 2412 header line
values = line.split()
if len(values) == 2:
x_coord = float(values[0]) * CHORD
y_coord = float(values[1]) * CHORD
x.append(float(x_coord))
y.append(float(y_coord))
plt.figure(figsize=(8, 3))
plt.plot(x, y)
plt.axis("equal")
plt.grid(True)
plt.xlabel("x")
plt.ylabel("y")
plt.title("NACA 2412 Airfoil")
#plt.show()
# Remove the hashtag from the line above to plot the airfoil. The line is commented out to avoid opening a plot window when running the script in a non-interactive environment.
# ---
# Export airfoil to dxf file
CHORD = 200.0 #mm
#Scale coordinates to desired chord length
scaled_x = x
scaled_y = y
doc = ezdxf.new()
msp = doc.modelspace()
#Add polyline
msp.add_lwpolyline(list(zip(scaled_x, scaled_y)), close=True)
#Save file
script_dir = os.path.dirname(os.path.abspath(__file__))
output_file = os.path.join(script_dir, "..", "cad", "naca2412_200mm.dxf")
doc.saveas(output_file)
print(f"Saved to: {output_file}")
Enter fullscreen mode Exit fullscreen mode
The paths & directories in the code was based on my own folder structure, so do feel free to change it as per your requirement if you do plan on using it! It is also available on the GitHub repository mentioned above.
추출 본문 · 출처: dev.to · https://dev.to/spondon_saha_102/dat-to-dxf-converter-28m0
답글 남기기