Skip to content

Commit

Permalink
3D-Print: refactor clean_float to take float values
Browse files Browse the repository at this point in the history
Put float to string formatting inside the function, code looks more
readable that way.
  • Loading branch information
mrachinskiy committed May 17, 2021
1 parent a5b2c67 commit 9019244
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions object_print3d_utils/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@
from . import report


def clean_float(text: str) -> str:
# strip trailing zeros: 0.000 -> 0.0
def clean_float(value: float, precision: int = 0) -> str:
# Avoid scientific notation and strip trailing zeros: 0.000 -> 0.0

text = f"{value:.{precision}f}"
index = text.rfind(".")

if index != -1:
index += 2
head, tail = text[:index], text[index:]
tail = tail.rstrip("0")
text = head + tail

return text


Expand Down Expand Up @@ -93,12 +97,12 @@ def execute(self, context):
bm.free()

if unit.system == 'NONE':
volume_fmt = clean_float(f"{volume:.8f}")
volume_fmt = clean_float(volume, 8)
else:
length, symbol = get_unit(unit.system, unit.length_unit)

volume_unit = volume * (scale ** 3.0) / (length ** 3.0)
volume_str = clean_float(f"{volume_unit:.4f}")
volume_str = clean_float(volume_unit, 4)
volume_fmt = f"{volume_str} {symbol}"

report.update((f"Volume: {volume_fmt}³", None))
Expand All @@ -124,12 +128,12 @@ def execute(self, context):
bm.free()

if unit.system == 'NONE':
area_fmt = clean_float(f"{area:.8f}")
area_fmt = clean_float(area, 8)
else:
length, symbol = get_unit(unit.system, unit.length_unit)

area_unit = area * (scale ** 2.0) / (length ** 2.0)
area_str = clean_float(f"{area_unit:.4f}")
area_str = clean_float(area_unit, 4)
area_fmt = f"{area_str} {symbol}"

report.update((f"Area: {area_fmt}²", None))
Expand Down Expand Up @@ -629,7 +633,7 @@ def _scale(scale, report=None, report_suffix=""):
if scale != 1.0:
bpy.ops.transform.resize(value=(scale,) * 3)
if report is not None:
scale_fmt = clean_float(f"{scale:.6f}")
scale_fmt = clean_float(scale, 6)
report({'INFO'}, f"Scaled by {scale_fmt}{report_suffix}")


Expand All @@ -651,7 +655,7 @@ class MESH_OT_print3d_scale_to_volume(Operator):

def execute(self, context):
scale = math.pow(self.volume, 1 / 3) / math.pow(self.volume_init, 1 / 3)
scale_fmt = clean_float(f"{scale:.6f}")
scale_fmt = clean_float(scale, 6)
self.report({'INFO'}, f"Scaled by {scale_fmt}")
_scale(scale, self.report)
return {'FINISHED'}
Expand Down

0 comments on commit 9019244

Please sign in to comment.