From 7dfb353b3ae175a79cb4d2208f219dcd4c758af9 Mon Sep 17 00:00:00 2001 From: vocx-fc Date: Sun, 4 Aug 2019 15:48:44 -0500 Subject: [PATCH] Draft: importOCA.py, improved Pythonic style, checked the code with flake8 and Spyder, which uses various code analyzers --- src/Mod/Draft/importOCA.py | 105 +++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 51 deletions(-) diff --git a/src/Mod/Draft/importOCA.py b/src/Mod/Draft/importOCA.py index ef8460a460..7a24ed1e7c 100644 --- a/src/Mod/Draft/importOCA.py +++ b/src/Mod/Draft/importOCA.py @@ -2,8 +2,7 @@ ## @package importOCA # \ingroup DRAFT # \brief OCA (Open CAD Format) file importer & exporter -''' -@package importOCA +'''@package importOCA \ingroup DRAFT \brief OCA (Open CAD Format) file importer & exporter @@ -73,26 +72,28 @@ def getpoint(data): A vector with the data arranged, depending on the contents of `data`. """ print("found point ", data) - if (len(data) == 3): + if len(data) == 3: return Vector(float(data[0]), float(data[1]), float(data[2])) elif (data[0] == "P") and (len(data) == 4): return Vector(float(data[1]), float(data[2]), float(data[3])) elif (data[0][0] == "P") and (len(data[0]) > 1): - if (len(data) == 1): + if len(data) == 1: return objects[data[0]] else: - if (data[1][0] == "R"): + if data[1][0] == "R": return objects[data[0]].add(objects[data[1]]) - elif (data[1][0] == "C"): + elif data[1][0] == "C": + # Error: DraftGeomUtils.findProjection() + # doesn't exist return DraftGeomUtils.findProjection(objects[data[0]], objects[data[1]]) - elif (data[0][0] == "C"): + elif data[0][0] == "C": if objects[data[0]]: p1 = objects[data[0]].Curve.Position - if (len(data) == 1): + if len(data) == 1: return p1 else: - if (data[1][0] == "L"): + if data[1][0] == "L": L = objects[data[1]] return p1.add(DraftGeomUtils.vec(L)) @@ -111,12 +112,12 @@ def getarea(data): A wire object from the points in `data`. """ print("found area ", data) - if (data[0] == "S"): - if (data[1] == "POL"): + if data[0] == "S": + if data[1] == "POL": pts = data[2:] verts = [] for p in pts: - if (p[0] == "P"): + if p[0] == "P": verts.append(getpoint([p])) w = Part.makePolygon(verts) return w @@ -137,45 +138,47 @@ def getarc(data): """ print("found arc ", data) c = None - if (data[0] == "ARC"): + if data[0] == "ARC": # 3-points arc pts = data[1:] verts = [] for p in range(len(pts)): - if (pts[p] == "P"): + if pts[p] == "P": verts.append(getpoint(pts[p:p+3])) - elif (pts[p][0] == "P"): + elif pts[p][0] == "P": verts.append(getpoint([pts[p]])) if verts[0] and verts[1] and verts[2]: c = Part.Arc(verts[0], verts[1], verts[2]) - elif (data[0][0] == "P"): + elif data[0][0] == "P": # 2-point circle verts = [] rad = None lines = [] for p in range(len(data)): - if (data[p] == "P"): + if data[p] == "P": verts.append(getpoint(data[p:p+4])) - elif (data[p][0] == "P"): + elif data[p][0] == "P": verts.append(getpoint([data[p]])) - elif (data[p] == "VAL"): + elif data[p] == "VAL": rad = float(data[p+1]) - elif (data[p][0] == "L"): + elif data[p][0] == "L": lines.append(objects[data[p]]) c = Part.Circle() c.Center = verts[0] if rad: c.Radius = rad else: + # Error: DraftVecUtils.new() + # doesn't exist c.Radius = DraftVecUtils.new(verts[0], verts[1]).Length - elif (data[0][0] == "L"): + elif data[0][0] == "L": # 2-lines circle lines = [] rad = None for p in range(len(data)): - if (data[p] == "VAL"): + if data[p] == "VAL": rad = float(data[p+1]) - elif (data[p][0] == "L"): + elif data[p][0] == "L": lines.append(objects[data[p]]) circles = DraftGeomUtils.circleFrom2LinesRadius(lines[0], lines[1], @@ -202,9 +205,9 @@ def getline(data): print("found line ", data) verts = [] for p in range(len(data)): - if (data[p] == "P"): + if data[p] == "P": verts.append(getpoint(data[p:p+4])) - elif (data[p][0] == "P"): + elif data[p][0] == "P": verts.append(getpoint([data[p]])) L = Part.LineSegment(verts[0], verts[1]) return L.toShape() @@ -224,11 +227,11 @@ def gettranslation(data): A vector with X, Y, or Z displacement, or (0, 0, 0). """ print("found translation ", data) - if (data[0] == "Z"): + if data[0] == "Z": return Vector(0, 0, float(data[1])) - elif (data[0] == "Y"): + elif data[0] == "Y": return Vector(0, float(data[1]), 0) - elif (data[0] == "X"): + elif data[0] == "X": return Vector(float(data[1]), 0, 0) return Vector(0, 0, 0) @@ -249,12 +252,12 @@ def writepoint(vector): return "P("+str(vector.x)+" "+str(vector.y)+" "+str(vector.z)+")" -def createobject(id, doc): +def createobject(oid, doc): """Create Part::Feature object in the current document. Parameters ---------- - id : str + oid : str ID number of a particular object in the document. doc : App::Document @@ -263,9 +266,9 @@ def createobject(id, doc): ------- None """ - if isinstance(objects[id], Part.Shape): - ob = doc.addObject("Part::Feature", id) - ob.Shape = objects[id] + if isinstance(objects[oid], Part.Shape): + ob = doc.addObject("Part::Feature", oid) + ob.Shape = objects[oid] if FreeCAD.GuiUp: ob.ViewObject.ShapeColor = color @@ -291,38 +294,38 @@ def parse(filename, doc): color = (0, 0, 0) for l in filebuffer: readline = l.replace(",", " ").upper() - if ("=" in readline): + if "=" in readline: # entity definitions pair = readline.split("=") - id = pair[0] + _id = pair[0] data = pair[1] data = data.replace(",", " ") data = data.replace("(", " ") data = data.replace(")", " ") data = data.split() - if id[0] == "P": + if _id[0] == "P": # point - objects[id] = getpoint(data) - elif ((id[0] == "A") and params.GetBool("ocaareas")): + objects[_id] = getpoint(data) + elif ((_id[0] == "A") and params.GetBool("ocaareas")): # area - objects[id] = getarea(data) - createobject(id, doc) + objects[_id] = getarea(data) + createobject(_id, doc) - elif id[0] == "C": + elif _id[0] == "C": # arc or circle - objects[id] = getarc(data) - createobject(id, doc) + objects[_id] = getarc(data) + createobject(_id, doc) - elif id[0] == "L": + elif _id[0] == "L": # line - objects[id] = getline(data) - createobject(id, doc) + objects[_id] = getline(data) + createobject(_id, doc) - elif id[0] == "R": + elif _id[0] == "R": # translation - objects[id] = gettranslation(data) + objects[_id] = gettranslation(data) - elif (readline[0:6] == "DEFCOL"): + elif readline[0:6] == "DEFCOL": # color c = readline.split() color = (float(c[1])/255, @@ -372,7 +375,7 @@ def open(filename): """ docname = os.path.split(filename)[1] doc = FreeCAD.newDocument(docname) - if (docname[-4:] == "gcad"): + if docname[-4:] == "gcad": doc.Label = docname[:-5] else: doc.Label = docname[:-4] @@ -452,7 +455,7 @@ def export(exportList, filename): oca.write(writepoint(e.Vertexes[-1].Point)) oca.write("\r\n") elif DraftGeomUtils.geomType(e) == "Circle": - if (len(e.Vertexes) > 1): + if len(e.Vertexes) > 1: oca.write("C"+str(count)+"=ARC ") oca.write(writepoint(e.Vertexes[0].Point)) oca.write(" ")