Skip to content

Commit

Permalink
Part: Store name, allow setting name for previous part
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Mar 5, 2024
1 parent cc63cb8 commit 79c95ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion boxes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ def move(self, x, y, where, before=False, label=""):
self.moveTo(x, 0)
self.ctx.scale(-1, 1)
self.moveTo(self.spacing / 2.0, self.spacing / 2.0)
self.ctx.new_part()
self.ctx.new_part(previous_part_name=label)

return dontdraw

Expand Down
17 changes: 14 additions & 3 deletions boxes/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ def transform(self, f, m, invert_y=False):
for p in self.parts:
p.transform(f, m, invert_y)

def new_part(self, name="part"):
def new_part(self, name="part", previous_part_name=None):
"""Insert a new part into the surface; any subsequent drawing commands
will be grouped into this part, and the previously active part is
closed.
previous_part_name allows naming the part that is being closed. This
allows parts created by the Boxes.move mechanism to obtain their
names."""
if previous_part_name is not None:
assert self.parts, "previous_part_name can not be set for the first part on a surface"
self.parts[-1].name = previous_part_name
if self.parts and len(self.parts[-1].pathes) == 0:
return self._p
p = Part(name)
Expand Down Expand Up @@ -115,6 +125,7 @@ def extents(self):

class Part:
def __init__(self, name) -> None:
self.name: str = name
self.pathes: list[Any] = []
self.path: list[Any] = []

Expand Down Expand Up @@ -402,8 +413,8 @@ def flush(self):
# self.stroke()

## additional methods
def new_part(self):
self._dwg.new_part()
def new_part(self, *args, **kwargs):
self._dwg.new_part(*args, **kwargs)


class SVGSurface(Surface):
Expand Down

0 comments on commit 79c95ad

Please sign in to comment.