From ecc05545c1aba4d8b90924607cdb1e25dafb3a68 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sat, 22 May 2021 17:30:23 +0200 Subject: [PATCH] Draft: Simplify code using getattr default value This uses the default value that can be passed to getattr to simplify code that uses it. By choosing an appropriate default value, a separate call to hasattr can be avoided and in some cases duplicate code can be avoided. This applies this trick where possible in wire.py and circle.py. This commit does not change behavior. --- src/Mod/Draft/draftobjects/circle.py | 5 +-- src/Mod/Draft/draftobjects/wire.py | 60 ++++++++++++---------------- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/src/Mod/Draft/draftobjects/circle.py b/src/Mod/Draft/draftobjects/circle.py index a3a520aa5b..3a7af22c91 100644 --- a/src/Mod/Draft/draftobjects/circle.py +++ b/src/Mod/Draft/draftobjects/circle.py @@ -79,10 +79,7 @@ class Circle(DraftObject): if obj.FirstAngle.Value == obj.LastAngle.Value: shape = Part.Wire(shape) - if hasattr(obj,"MakeFace"): - if obj.MakeFace: - shape = Part.Face(shape) - else: + if getattr(obj,"MakeFace",True): shape = Part.Face(shape) obj.Shape = shape diff --git a/src/Mod/Draft/draftobjects/wire.py b/src/Mod/Draft/draftobjects/wire.py index d4a0731283..71becfb5a9 100644 --- a/src/Mod/Draft/draftobjects/wire.py +++ b/src/Mod/Draft/draftobjects/wire.py @@ -102,10 +102,7 @@ class Wire(DraftObject): if obj.Base.isDerivedFrom("Sketcher::SketchObject"): shape = obj.Base.Shape.copy() if obj.Base.Shape.isClosed(): - if hasattr(obj,"MakeFace"): - if obj.MakeFace: - shape = Part.Face(shape) - else: + if getattr(obj,"MakeFace",True): shape = Part.Face(shape) obj.Shape = shape elif obj.Base and obj.Tool: @@ -126,21 +123,20 @@ class Wire(DraftObject): obj.Points.pop() if obj.Closed and (len(obj.Points) > 2): pts = obj.Points - if hasattr(obj,"Subdivisions"): - if obj.Subdivisions > 0: - npts = [] - for i in range(len(pts)): - p1 = pts[i] - npts.append(pts[i]) - if i == len(pts)-1: - p2 = pts[0] - else: - p2 = pts[i+1] - v = p2.sub(p1) - v = DraftVecUtils.scaleTo(v,v.Length/(obj.Subdivisions+1)) - for j in range(obj.Subdivisions): - npts.append(p1.add(App.Vector(v).multiply(j+1))) - pts = npts + if getattr(obj,"Subdivisions",0) > 0: + npts = [] + for i in range(len(pts)): + p1 = pts[i] + npts.append(pts[i]) + if i == len(pts)-1: + p2 = pts[0] + else: + p2 = pts[i+1] + v = p2.sub(p1) + v = DraftVecUtils.scaleTo(v,v.Length/(obj.Subdivisions+1)) + for j in range(obj.Subdivisions): + npts.append(p1.add(App.Vector(v).multiply(j+1))) + pts = npts shape = Part.makePolygon(pts+[pts[0]]) if "ChamferSize" in obj.PropertiesList: if obj.ChamferSize.Value != 0: @@ -153,10 +149,7 @@ class Wire(DraftObject): if w: shape = w try: - if hasattr(obj,"MakeFace"): - if obj.MakeFace: - shape = Part.Face(shape) - else: + if getattr(obj,"MakeFace",True): shape = Part.Face(shape) except Part.OCCError: pass @@ -166,18 +159,15 @@ class Wire(DraftObject): lp = obj.Points[0] for p in pts: if not DraftVecUtils.equals(lp,p): - if hasattr(obj,"Subdivisions"): - if obj.Subdivisions > 0: - npts = [] - v = p.sub(lp) - v = DraftVecUtils.scaleTo(v,v.Length/(obj.Subdivisions+1)) - edges.append(Part.LineSegment(lp,lp.add(v)).toShape()) - lv = lp.add(v) - for j in range(obj.Subdivisions): - edges.append(Part.LineSegment(lv,lv.add(v)).toShape()) - lv = lv.add(v) - else: - edges.append(Part.LineSegment(lp,p).toShape()) + if getattr(obj,"Subdivisions",0) > 0: + npts = [] + v = p.sub(lp) + v = DraftVecUtils.scaleTo(v,v.Length/(obj.Subdivisions+1)) + edges.append(Part.LineSegment(lp,lp.add(v)).toShape()) + lv = lp.add(v) + for j in range(obj.Subdivisions): + edges.append(Part.LineSegment(lv,lv.add(v)).toShape()) + lv = lv.add(v) else: edges.append(Part.LineSegment(lp,p).toShape()) lp = p