Draft: importAirfoilDAT, use 4 space indentation instead of 8 space
This commit is contained in:
@@ -30,10 +30,10 @@ __author__ = "Heiko Jakob <heiko.jakob@gediegos.de>"
|
||||
#
|
||||
# This module provides support for importing airfoil .dat files
|
||||
|
||||
import re, FreeCAD, Part, cProfile, os
|
||||
from FreeCAD import Vector, Base
|
||||
import re, FreeCAD, Draft, Part, cProfile, os
|
||||
from FreeCAD import Vector
|
||||
from FreeCAD import Console as FCC
|
||||
from Draft import makeWire
|
||||
|
||||
|
||||
if FreeCAD.GuiUp:
|
||||
from DraftTools import translate
|
||||
@@ -128,97 +128,94 @@ def insert(filename, docname):
|
||||
|
||||
|
||||
def process(doc, filename):
|
||||
"""Process the filename and provide the document with the information.
|
||||
"""Process the filename and provide the document with the information.
|
||||
|
||||
The common airfoil dat format has many flavors.
|
||||
This code should work with almost every dialect.
|
||||
The common airfoil dat format has many flavors.
|
||||
This code should work with almost every dialect.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
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.
|
||||
Parameters
|
||||
----------
|
||||
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.
|
||||
"""
|
||||
# Regex to identify data rows and throw away unused metadata
|
||||
regex = re.compile(r'^\s*(?P<xval>(\-|\d*)\.\d+(E\-?\d+)?)\,?\s*(?P<yval>\-?\s*\d*\.\d+(E\-?\d+)?)\s*$')
|
||||
afile = pythonopen(filename, 'r')
|
||||
# read the airfoil name which is always at the first line
|
||||
airfoilname = afile.readline().strip()
|
||||
Returns
|
||||
-------
|
||||
App::Document
|
||||
The active FreeCAD document, or the document created if none exists,
|
||||
with the parsed information.
|
||||
"""
|
||||
# Regex to identify data rows and throw away unused metadata
|
||||
xval = '(?P<xval>(\-|\d*)\.\d+(E\-?\d+)?)'
|
||||
yval = '(?P<yval>\-?\s*\d*\.\d+(E\-?\d+)?)'
|
||||
_regex = '^\s*' + xval + '\,?\s*' + yval + '\s*$'
|
||||
|
||||
coords = []
|
||||
upside = True
|
||||
last_x = None
|
||||
regex = re.compile(_regex)
|
||||
afile = pythonopen(filename, 'r')
|
||||
# read the airfoil name which is always at the first line
|
||||
airfoilname = afile.readline().strip()
|
||||
|
||||
coords = []
|
||||
upside = True
|
||||
last_x = None
|
||||
|
||||
# Collect the data for the upper and the lower side separately if possible
|
||||
for lin in afile:
|
||||
curdat = regex.match(lin)
|
||||
if curdat is not None:
|
||||
x = float(curdat.group("xval"))
|
||||
y = float(curdat.group("yval"))
|
||||
|
||||
# Collect the data for the upper and the lower side separately if possible
|
||||
for lin in afile:
|
||||
curdat = regex.match(lin)
|
||||
if curdat is not None:
|
||||
x = float(curdat.group("xval"))
|
||||
y = float(curdat.group("yval"))
|
||||
# the normal processing
|
||||
coords.append(Vector(x, y, 0))
|
||||
|
||||
# the normal processing
|
||||
coords.append(Vector(x, y, 0))
|
||||
afile.close()
|
||||
|
||||
# End of if curdat != None
|
||||
# End of for lin in file
|
||||
afile.close()
|
||||
if len(coords) < 3:
|
||||
print('Did not find enough coordinates\n')
|
||||
return
|
||||
|
||||
if len(coords) < 3:
|
||||
print('Did not find enough coordinates\n')
|
||||
return
|
||||
# sometimes coords are divided in upper an lower side
|
||||
# so that x-coordinate begin new from leading or trailing edge
|
||||
# check for start coordinates in the middle of list
|
||||
if coords[0:-1].count(coords[0]) > 1:
|
||||
flippoint = coords.index(coords[0], 1)
|
||||
upper = coords[0:flippoint]
|
||||
lower = coords[flippoint+1:]
|
||||
lower.reverse()
|
||||
for i in lower:
|
||||
upper.append(i)
|
||||
coords = upper
|
||||
|
||||
# sometimes coords are divided in upper an lower side
|
||||
# so that x-coordinate begin new from leading or trailing edge
|
||||
# check for start coordinates in the middle of list
|
||||
# do we use the parametric Draft Wire?
|
||||
if useDraftWire:
|
||||
obj = Draft.makeWire(coords, True)
|
||||
# obj.label = airfoilname
|
||||
else:
|
||||
# alternate solution, uses common Part Faces
|
||||
lines = []
|
||||
first_v = None
|
||||
last_v = None
|
||||
for v in coords:
|
||||
if first_v is None:
|
||||
first_v = v
|
||||
|
||||
if coords[0:-1].count(coords[0]) > 1:
|
||||
flippoint = coords.index(coords[0], 1)
|
||||
upper = coords[0:flippoint]
|
||||
lower = coords[flippoint+1:]
|
||||
lower.reverse()
|
||||
for i in lower:
|
||||
upper.append(i)
|
||||
coords = upper
|
||||
# Line between v and last_v if they're not equal
|
||||
if (last_v is not None) and (last_v != v):
|
||||
lines.append(Part.makeLine(last_v, v))
|
||||
|
||||
# do we use the parametric Draft Wire?
|
||||
if useDraftWire:
|
||||
obj = makeWire(coords, True)
|
||||
#obj.label = airfoilname
|
||||
else:
|
||||
# alternate solution, uses common Part Faces
|
||||
lines = []
|
||||
first_v = None
|
||||
last_v = None
|
||||
for v in coords:
|
||||
if first_v == None:
|
||||
first_v = v
|
||||
# End of if first_v == None
|
||||
# The new last_v
|
||||
last_v = v
|
||||
|
||||
# Line between v and last_v if they're not equal
|
||||
if (last_v is not None) and (last_v != v):
|
||||
lines.append(Part.makeLine(last_v, v))
|
||||
# End of if (last_v != None) and (last_v != v)
|
||||
# The new last_v
|
||||
last_v = v
|
||||
# End of for v in upper
|
||||
# close the wire if needed
|
||||
if last_v != first_v:
|
||||
lines.append(Part.makeLine(last_v, first_v))
|
||||
# End of if last_v != first_v
|
||||
# close the wire if needed
|
||||
if last_v != first_v:
|
||||
lines.append(Part.makeLine(last_v, first_v))
|
||||
|
||||
wire = Part.Wire(lines)
|
||||
face = Part.Face(wire)
|
||||
obj = FreeCAD.ActiveDocument.addObject('Part::Feature',airfoilname)
|
||||
obj.Shape = face
|
||||
wire = Part.Wire(lines)
|
||||
face = Part.Face(wire)
|
||||
obj = FreeCAD.ActiveDocument.addObject('Part::Feature', airfoilname)
|
||||
obj.Shape = face
|
||||
|
||||
doc.recompute()
|
||||
doc.recompute()
|
||||
|
||||
Reference in New Issue
Block a user