From 83241d3f54589c2eb029e8df49fd3dbe375fa8e0 Mon Sep 17 00:00:00 2001 From: Roy-043 <70520633+Roy-043@users.noreply.github.com> Date: Sat, 1 Jan 2022 19:04:21 +0100 Subject: [PATCH] Draft: fix several issues with importAirfoilDAT.py The importAirfoilDAT.py code could not handle several *.dat files posted in a forum topic. --- src/Mod/Draft/importAirfoilDAT.py | 49 ++++++++++++++++++------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/Mod/Draft/importAirfoilDAT.py b/src/Mod/Draft/importAirfoilDAT.py index 3e3e13d147..e6886f94d1 100644 --- a/src/Mod/Draft/importAirfoilDAT.py +++ b/src/Mod/Draft/importAirfoilDAT.py @@ -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(\-|\d*)\.*\d*([Ee]\-?\d+)?)' + xval = r'(?P\-?\s*\d*\.*\d*([Ee]\-?\d+)?)' yval = r'(?P\-?\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