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.
This commit is contained in:
Matthijs Kooijman
2021-05-22 17:30:23 +02:00
parent 96cbc722f5
commit ecc05545c1
2 changed files with 26 additions and 39 deletions

View File

@@ -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

View File

@@ -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