Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016S8V7WxoS7YRoM34BVBkeo
229 lines
11 KiB
Python
229 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
# coding=utf-8
|
|
"""
|
|
Extension Inkscape : dessine les 4 reperes de calibration de la surface de
|
|
travail de la decoupeuse laser, selon trois sorties :
|
|
|
|
- « bed » (parametre mode) : reperes a graver directement sur la surface ; graves par la
|
|
laser elle-meme, ils sont exactement la ou l'import les attend ;
|
|
- « sheet » : feuille A4 a imprimer, reperes en grille a decouper, avec une
|
|
barre de 100 mm pour controler l'echelle d'impression ;
|
|
- « guides » : gabarits de pose a graver sur la surface (contour de la piece
|
|
et croix) pour coller les reperes imprimes a leur position exacte.
|
|
|
|
Le repere origine (deux pastilles, en bas a gauche = origine laser) donne
|
|
l'orientation de la photo.
|
|
|
|
Geometrie dans board_photo_core, sans dependance a inkex.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
|
|
import inkex
|
|
from inkex.localization import localize
|
|
|
|
# L'extension vit dans son propre sous-dossier des extensions Inkscape, qui
|
|
# n'est pas forcement dans sys.path : on l'y ajoute pour trouver ses modules.
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
import board_photo_common as common # noqa: E402
|
|
import board_photo_core as core # noqa: E402
|
|
|
|
# Installe _() dans les builtins : catalogue « board_photo » de la langue
|
|
# de l'interface, textes anglais d'origine sinon.
|
|
localize()
|
|
|
|
SHEET_MARGIN = 12.0 # marge de la feuille imprimee (mm)
|
|
|
|
|
|
class BoardPhotoMarkers(inkex.EffectExtension):
|
|
"""Reperes a graver, feuille A4 a imprimer, ou gabarits de pose."""
|
|
|
|
def add_arguments(self, pars):
|
|
# Un argument par <param> du .inx, memes noms et memes valeurs par defaut.
|
|
pars.add_argument("--tab", default="work_area")
|
|
pars.add_argument("--mode", default="bed")
|
|
pars.add_argument("--work_width", type=float, default=400.0)
|
|
pars.add_argument("--work_height", type=float, default=400.0)
|
|
pars.add_argument("--origin_x", type=float, default=30.0)
|
|
pars.add_argument("--origin_y", type=float, default=30.0)
|
|
pars.add_argument("--spacing_x", type=float, default=340.0)
|
|
pars.add_argument("--spacing_y", type=float, default=340.0)
|
|
pars.add_argument("--marker_size", type=float, default=20.0)
|
|
pars.add_argument("--set_page", type=inkex.Boolean, default=True)
|
|
pars.add_argument("--print_x", type=float, default=100.0)
|
|
pars.add_argument("--print_y", type=float, default=100.0)
|
|
|
|
def effect(self):
|
|
opt = self.options
|
|
if opt.mode == "sheet":
|
|
self.print_sheet()
|
|
return
|
|
# Un gabarit de pose (piece + croix) est plus grand que le repere seul.
|
|
reach = core.guide_reach(opt.marker_size) if opt.mode == "guides" else None
|
|
problems = core.layout_problems(opt.work_width, opt.work_height, opt.origin_x,
|
|
opt.origin_y, opt.spacing_x, opt.spacing_y,
|
|
opt.marker_size, reach)
|
|
if "non_positive" in problems:
|
|
inkex.errormsg(_("Work area, marker spacing and marker size must be "
|
|
"strictly positive."))
|
|
return
|
|
if "outside" in problems and reach is not None:
|
|
inkex.errormsg(_("The placement guides do not fit in the work area: keep "
|
|
"every marker centre at least {:g} mm from the edges "
|
|
"(1.5 x the marker size).").format(reach))
|
|
return
|
|
if "outside" in problems:
|
|
inkex.errormsg(_("The markers do not fit in the work area, so the laser "
|
|
"cannot engrave them: reduce the spacing or move the "
|
|
"origin marker. Markers placed around the work area "
|
|
"(negative origin, larger spacing) are printed instead "
|
|
"(\"Output\": A4 sheet) and stuck in place by hand."))
|
|
return
|
|
if "too_close" in problems:
|
|
inkex.errormsg(_("The markers are too close to each other: the spacing "
|
|
"must be at least twice the marker size."))
|
|
return
|
|
|
|
scale, ox, oy = common.prepare_page(self.svg, opt.work_width, opt.work_height,
|
|
opt.set_page)
|
|
centers = core.marker_layout(opt.work_height, opt.origin_x, opt.origin_y,
|
|
opt.spacing_x, opt.spacing_y)
|
|
if opt.mode == "guides":
|
|
self.draw_guides(centers, scale, ox, oy)
|
|
return
|
|
common.remove_layers(self.svg, "markers")
|
|
layer = common.new_layer(_("Calibration markers"), "markers")
|
|
self.svg.add(layer)
|
|
convert = self.converter(scale, ox, oy)
|
|
for index, center in enumerate(centers):
|
|
self.add_marker(layer, center, index == core.ORIGIN_MARKER, convert)
|
|
|
|
# ------------------------------------------------------------------
|
|
# Elements dessines
|
|
# ------------------------------------------------------------------
|
|
|
|
@staticmethod
|
|
def converter(scale, ox, oy, stretch=(1.0, 1.0), center=(0.0, 0.0)):
|
|
"""Fonction anneaux en mm -> anneaux en unites du document.
|
|
|
|
`stretch` agrandit autour de `center` (correction d'imprimante).
|
|
"""
|
|
def convert(rings):
|
|
rings = core.stretch_rings(rings, stretch[0], stretch[1], center)
|
|
return core.scale_rings(rings, scale, (ox, oy))
|
|
return convert
|
|
|
|
def add_marker(self, layer, center, origin, convert):
|
|
"""Un chemin plein pair-impair par repere (carre + pastilles)."""
|
|
rings = core.marker_rings(center, self.options.marker_size, origin=origin)
|
|
path = inkex.PathElement()
|
|
path.set("d", core.polylines_to_d(convert(rings), closed=True))
|
|
path.style = inkex.Style({"fill": "#000000", "fill-rule": "evenodd",
|
|
"stroke": "none"})
|
|
path.label = _("Origin marker") if origin else _("Marker")
|
|
layer.add(path)
|
|
|
|
@staticmethod
|
|
def add_lines(layer, polylines, style, convert, label):
|
|
"""Un chemin de traits (polylignes en mm) dans le calque."""
|
|
path = inkex.PathElement()
|
|
path.set("d", core.polylines_to_d(convert(polylines)))
|
|
path.style = style
|
|
path.label = label
|
|
layer.add(path)
|
|
|
|
def draw_guides(self, centers, scale, ox, oy):
|
|
"""Gabarits de pose : contour de chaque piece et croix passant par son centre."""
|
|
size = self.options.marker_size
|
|
common.remove_layers(self.svg, "guides")
|
|
layer = common.new_layer(_("Marker placement guides"), "guides")
|
|
self.svg.add(layer)
|
|
style = inkex.Style({"fill": "none", "stroke": "#ff0000",
|
|
"stroke-width": str(0.1 * scale)})
|
|
convert = self.converter(scale, ox, oy)
|
|
for index, center in enumerate(centers):
|
|
label = _("Origin marker") if index == core.ORIGIN_MARKER else _("Marker")
|
|
self.add_lines(layer, core.guide_lines(center, size), style, convert, label)
|
|
|
|
def print_sheet(self):
|
|
"""Feuille A4 : les 4 reperes en grille a decouper, legendes et barres de 100 mm."""
|
|
opt = self.options
|
|
size = opt.marker_size
|
|
centers = core.sheet_layout(size, margin=SHEET_MARGIN)
|
|
if centers is None:
|
|
inkex.errormsg(_("These markers are too large for an A4 sheet: reduce the "
|
|
"marker size, or engrave them directly on the bed."))
|
|
return
|
|
if not (80 <= opt.print_x <= 120 and 80 <= opt.print_y <= 120):
|
|
inkex.errormsg(_("The measured bar lengths must be between 80 and 120 mm."))
|
|
return
|
|
width, height = core.A4
|
|
# La feuille impose sa page : c'est elle que l'on imprime a 100 %.
|
|
scale, ox, oy = common.prepare_page(self.svg, width, height, True)
|
|
common.remove_layers(self.svg, "sheet")
|
|
layer = common.new_layer(_("Calibration markers (print)"), "sheet")
|
|
self.svg.add(layer)
|
|
|
|
# Correction d'imprimante : tout le dessin est agrandi autour du centre
|
|
# de la page, pour que les barres ressortent a 100 mm une fois imprimees.
|
|
fx, fy = core.print_correction(opt.print_x, opt.print_y)
|
|
convert = self.converter(scale, ox, oy, (fx, fy), (width / 2.0, height / 2.0))
|
|
|
|
def text(x, y, content, font=3.5, bold=False, anchor="start"):
|
|
px, py = convert([[(x, y)]])[0][0]
|
|
node = inkex.TextElement()
|
|
node.set("x", common.fmt(px))
|
|
node.set("y", common.fmt(py))
|
|
node.text = content
|
|
node.style = inkex.Style({
|
|
"font-family": "sans-serif", "font-size": common.fmt(font * fy * scale),
|
|
"font-weight": "bold" if bold else "normal", "text-anchor": anchor,
|
|
"fill": "#000000"})
|
|
layer.add(node)
|
|
|
|
m = SHEET_MARGIN
|
|
text(m, m + 7, _("Calibration markers - print at 100 %"), 6, True)
|
|
text(m, m + 15, _("Print at actual size (no \"fit to page\"), then check that "
|
|
"both bars measure 100 mm."))
|
|
text(m, m + 21, _("Cut along the dashed lines and stick each piece outside a "
|
|
"corner of the honeycomb, its inner corner on that corner."))
|
|
line = _("Marker size: {:g} mm").format(size)
|
|
if (fx, fy) != (1.0, 1.0):
|
|
line += " - " + _("printer correction: X {:.1f} %, Y {:.1f} %").format(
|
|
100 * fx, 100 * fy)
|
|
text(m, m + 27, line)
|
|
|
|
cut = inkex.Style({"fill": "none", "stroke": "#868e96",
|
|
"stroke-width": str(0.2 * scale),
|
|
"stroke-dasharray": "{0},{0}".format(common.fmt(1.5 * scale))})
|
|
black = inkex.Style({"fill": "none", "stroke": "#000000",
|
|
"stroke-width": str(0.3 * scale)})
|
|
names = [_("Top left"), _("Top right"), _("Bottom right"),
|
|
_("Origin - bottom left")]
|
|
half = core.piece_side(size) / 2.0
|
|
for index, center in enumerate(centers):
|
|
origin = index == core.ORIGIN_MARKER
|
|
self.add_lines(layer, [core.piece_outline(center, size)], cut, convert,
|
|
_("Cutting line"))
|
|
self.add_lines(layer, core.piece_ticks(center, size), black, convert,
|
|
_("Alignment ticks"))
|
|
self.add_marker(layer, center, origin, convert)
|
|
text(center[0], center[1] + half + 5, names[index], 3.2, origin, "middle")
|
|
|
|
# Deux barres : une imprimante peut reduire differemment en X et en Y.
|
|
bar_y = height - m - 6
|
|
self.add_lines(layer, core.scale_bar((m, bar_y)), black, convert,
|
|
_("Scale bar X"))
|
|
text(m + 104, bar_y, _("100 mm (X)"), 3.5, True)
|
|
bar_x = width - m + 4
|
|
top = m + 45
|
|
self.add_lines(layer, core.scale_bar((bar_x, top), vertical=True), black, convert,
|
|
_("Scale bar Y"))
|
|
text(bar_x, top + 106, _("100 mm (Y)"), 3.5, True, "end")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
BoardPhotoMarkers().run()
|