importAirfoilDAT.py
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 __title__="FreeCAD Draft Workbench - Airfoil data importer"
00025 __author__ = "Heiko Jakob <heiko.jakob@gediegos.de>"
00026
00027 import re, FreeCAD, FreeCADGui, Part, cProfile, os, string
00028 from FreeCAD import Vector, Base
00029 from Draft import *
00030
00031 pythonopen = open
00032 useDraftWire = True
00033
00034 def decodeName(name):
00035 "decodes encoded strings"
00036 try:
00037 decodedName = (name.decode("utf8"))
00038 except UnicodeDecodeError:
00039 try:
00040 decodedName = (name.decode("latin1"))
00041 except UnicodeDecodeError:
00042 print "AirfoilDAT: error: couldn't determine character encoding"
00043 decodedName = name
00044 return decodedName
00045
00046 def open(filename):
00047 "called when freecad opens a file"
00048 docname = os.path.splitext(os.path.basename(filename))[0]
00049 doc = FreeCAD.newDocument(docname)
00050 doc.Label = decodeName(docname[:-4])
00051 process(doc,filename)
00052
00053 def insert(filename,docname):
00054 "called when freecad imports a file"
00055 groupname = os.path.splitext(os.path.basename(filename))[0]
00056 try:
00057 doc=FreeCAD.getDocument(docname)
00058 except:
00059 doc=FreeCAD.newDocument(docname)
00060 importgroup = doc.addObject("App::DocumentObjectGroup",groupname)
00061 importgroup.Label = decodeName(groupname)
00062 process(doc,filename)
00063
00064 def process(doc,filename):
00065
00066
00067
00068
00069 expre = r"^\s*(?P<xval>\-*\d*?\.\d\d+)\s+(?P<yval>\-*\d*?\.\d+)\s*$"
00070 afile = pythonopen(filename,'r')
00071
00072 airfoilname = afile.readline().strip()
00073
00074 upper=[]
00075 lower=[]
00076 upside=True
00077 last_x=None
00078
00079
00080
00081 for lin in afile:
00082 curdat = re.match(expre,lin)
00083 if curdat != None:
00084 x = float(curdat.group("xval"))
00085 y = float(curdat.group("yval"))
00086
00087 if last_x == None:
00088 last_x=x
00089
00090
00091
00092 if x < last_x:
00093
00094 upside=False
00095
00096
00097
00098 if upside:
00099 upper.append(Vector(x,y,0))
00100 else:
00101 lower.append(Vector(x,y,0))
00102 last_x=x
00103
00104
00105 afile.close()
00106
00107
00108
00109 lower.reverse()
00110 for i in lower:
00111 upper.append(i)
00112
00113
00114
00115 if useDraftWire:
00116 face = makeWire ( upper, True, None, True )
00117 else:
00118
00119 lines = []
00120 first_v = None
00121 last_v = None
00122 for v in upper:
00123 if first_v == None:
00124 first_v = v
00125
00126
00127
00128 if (last_v != None) and (last_v != v):
00129 lines.append(Part.makeLine(last_v, v))
00130
00131
00132 last_v = v
00133
00134
00135 if last_v != first_v:
00136 lines.append(Part.makeLine(last_v, first_v))
00137
00138
00139 wire = Part.Wire(lines)
00140 face = Part.Face(wire)
00141 Part.show(face)
00142
00143 doc.recompute()