Skip to content

Add measurement labels with new label_format option #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions boxes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ def __init__(self) -> None:
defaultgroup.add_argument(
"--labels", action="store", type=boolarg, default=True,
help="label the parts (where available)")
defaultgroup.add_argument(
"--label_format", action="store", type=str, default="label",
choices=["label", "measurements", "label (measurements)"],
help="information to include as label on parts")
defaultgroup.add_argument(
"--reference", action="store", type=float, default=100.0,
help="print reference rectangle with given length (in mm)(zero to disable) [\U0001F6C8](https://florianfesti.github.io/boxes/html/usermanual.html#reference)")
Expand Down Expand Up @@ -444,7 +448,7 @@ def open(self):
else:
self.text(f"{self.reference:.1f}mm, burn:{self.burn:.2f}mm", self.reference / 2.0, 5,
fontsize=6, align="middle center", color=Color.ANNOTATIONS)
self.move(self.reference, 10, "up")
self.move(self.reference, 10, "up", label=False)
if self.qr_code:
self.renderQrCode()
self.ctx.stroke()
Expand Down Expand Up @@ -718,6 +722,25 @@ def adjustSize(self, l, e1=True, e2=True):
except TypeError:
return l - walls

def formatLabel(self, label: str | bool, width, height):
# Allows passing False to skip labeling
if label == False:
return

label_format = getattr(self, "label_format", "label")
measurements = f"({width:.2f}mm x {height:.2f}mm)"

if label_format == "label":
return label
elif label_format == "measurements":
return measurements
elif label_format == "label (measurements)":
if not label:
return measurements
return f"{label}\n{measurements}"
else:
raise ValueError("Unknown label format", label_format)

def render(self):
"""Implement this method in your subclass.

Expand Down Expand Up @@ -1218,6 +1241,7 @@ def move(self, x, y, where, before=False, label=""):
terms = where.split()
dontdraw = before and "only" in terms

width, height = x, y
x += self.spacing
y += self.spacing

Expand All @@ -1237,8 +1261,10 @@ def move(self, x, y, where, before=False, label=""):
if not before:
# restore position
self.ctx.restore()
if self.labels and label:
self.text(label, x/2, y/2, align="middle center", color=Color.ANNOTATIONS, fontsize=4)
if self.labels:
contents = self.formatLabel(label, width, height)
if contents:
self.text(contents, x/2, y/2, align="middle center", color=Color.ANNOTATIONS, fontsize=4)
self.ctx.stroke()

for term in terms:
Expand Down