Draft: fix several issues with importAirfoilDAT.py

The importAirfoilDAT.py code could not handle several *.dat files posted in a forum topic.
This commit is contained in:
Roy-043
2022-01-01 19:04:21 +01:00
committed by Chris Hennes
parent 069d5ca4d7
commit 83241d3f54

View File

@@ -9,7 +9,6 @@
\brief Airfoil (.dat) file importer
This module provides support for importing airfoil .dat files.
Note (2019): this module has been unmaintained for a long time.
'''
# Check code with
# flake8 --ignore=E226,E266,E401,W503
@@ -103,8 +102,9 @@ def open(filename):
"""
docname = os.path.splitext(os.path.basename(filename))[0]
doc = FreeCAD.newDocument(docname)
doc.Label = decodeName(docname[:-4])
process(doc, filename)
doc.Label = decodeName(docname)
process(filename)
doc.recompute()
def insert(filename, docname):
@@ -131,13 +131,16 @@ def insert(filename, docname):
doc = FreeCAD.getDocument(docname)
except NameError:
doc = FreeCAD.newDocument(docname)
importgroup = doc.addObject("App::DocumentObjectGroup", groupname)
importgroup.Label = decodeName(groupname)
process(doc, filename)
obj = process(filename)
if obj is not None:
importgroup = doc.addObject("App::DocumentObjectGroup", groupname)
importgroup.Label = decodeName(groupname)
importgroup.Group = [obj]
doc.recompute()
def process(doc, filename):
"""Process the filename and provide the document with the information.
def process(filename):
"""Process the filename and create a Draft Wire from the data.
The common airfoil dat format has many flavors.
This code should work with almost every dialect.
@@ -146,18 +149,15 @@ def process(doc, filename):
----------
filename : str
The path to the filename to be opened.
docname : str
The name of the active App::Document if one exists, or
of the new one created.
Returns
-------
App::Document
The active FreeCAD document, or the document created if none exists,
with the parsed information.
Part::Part2DObject or None.
The created Draft Wire object or None if the file contains less
than 3 points.
"""
# Regex to identify data rows and throw away unused metadata
xval = r'(?P<xval>(\-|\d*)\.*\d*([Ee]\-?\d+)?)'
xval = r'(?P<xval>\-?\s*\d*\.*\d*([Ee]\-?\d+)?)'
yval = r'(?P<yval>\-?\s*\d*\.*\d*([Ee]\-?\d+)?)'
_regex = r'^\s*' + xval + r'\,?\s*' + yval + r'\s*$'
@@ -170,21 +170,28 @@ def process(doc, filename):
# upside = True
# last_x = None
# Collect the data for the upper and the lower side separately if possible
# Collect the data
for lin in afile:
curdat = regex.match(lin)
if curdat is not None:
if (curdat is not None
and curdat.group("xval")
and curdat.group("yval")):
x = float(curdat.group("xval"))
y = float(curdat.group("yval"))
# the normal processing
coords.append(Vector(x, y, 0))
# Some files specify the number of upper and lower points on the 2nd line:
# " 67. 72."
# See: http://airfoiltools.com/airfoil
# This line must be skipped:
if x < 2 and y < 2:
# the normal processing
coords.append(Vector(x, y, 0))
afile.close()
if len(coords) < 3:
FCC.PrintError(translate("ImportAirfoilDAT", "Did not find enough coordinates") + "\n")
return
return None
# sometimes coords are divided in upper an lower side
# so that x-coordinate begin new from leading or trailing edge
@@ -227,4 +234,4 @@ def process(doc, filename):
obj = FreeCAD.ActiveDocument.addObject('Part::Feature', airfoilname)
obj.Shape = face
doc.recompute()
return obj