diff --git a/README.md b/README.md index 235c9ca4..72cef061 100644 --- a/README.md +++ b/README.md @@ -2584,7 +2584,12 @@ Notes: - `dpi`: [number=300] [10,1200] Dots per inch (resolution) of the generated image. - `highlight`: [list(string)=[]] List of components to highlight. - `libs`: [list(string)=[]] List of libraries. - - `margin`: [number=0] [0,100] Margin around the generated image [mm]. + - `margin`: [number|dict] Margin around the generated image [mm]. + * Valid keys: + - `bottom`: [number=0] Bottom margin [mm]. + - `left`: [number=0] Left margin [mm]. + - `right`: [number=0] Right margin [mm]. + - `top`: [number=0] Top margin [mm]. - `no_drillholes`: [boolean=false] Do not make holes transparent. - `outline_width`: [number=0.15] [0,10] Width of the trace to draw the PCB border [mm]. Note this also affects the drill holes. diff --git a/docs/samples/generic_plot.kibot.yaml b/docs/samples/generic_plot.kibot.yaml index 18d28ce2..fb1ca431 100644 --- a/docs/samples/generic_plot.kibot.yaml +++ b/docs/samples/generic_plot.kibot.yaml @@ -1362,8 +1362,16 @@ outputs: highlight: [] # [list(string)=[]] List of libraries libs: [] - # [number=0] [0,100] Margin around the generated image [mm] - margin: 0 + # [number|dict] Margin around the generated image [mm] + margin: + # [number=0] Bottom margin [mm] + bottom: 0 + # [number=0] Left margin [mm] + left: 0 + # [number=0] Right margin [mm] + right: 0 + # [number=0] Top margin [mm] + top: 0 # [boolean=false] Mirror the board mirror: false # [boolean=false] Do not make holes transparent diff --git a/kibot/PcbDraw/README.md b/kibot/PcbDraw/README.md index f0aab88e..b6ff90bd 100644 --- a/kibot/PcbDraw/README.md +++ b/kibot/PcbDraw/README.md @@ -244,3 +244,5 @@ index f8990722..17f90185 100644 - Allow constructing PcbPlotter() using an already loaded board - So we don't load it again + +- Changed the margin to be a tuple, so the user can control the margins individually diff --git a/kibot/PcbDraw/plot.py b/kibot/PcbDraw/plot.py index 0b61fa95..01e7f523 100644 --- a/kibot/PcbDraw/plot.py +++ b/kibot/PcbDraw/plot.py @@ -505,7 +505,7 @@ def merge_bbox(left: Box, right: Box) -> Box: def hack_is_valid_bbox(box: Any): # type: ignore return all(-1e15 < c < 1e15 for c in box) -def shrink_svg(svg: etree.ElementTree, margin: int, compute_bbox: bool=False) -> None: +def shrink_svg(svg: etree.ElementTree, margin: tuple, compute_bbox: bool=False) -> None: """ Shrink the SVG canvas to the size of the drawing. Add margin in KiCAD units. @@ -544,10 +544,10 @@ def shrink_svg(svg: etree.ElementTree, margin: int, compute_bbox: bool=False) -> bbox = [x, x+vw, y, y+vh] # Apply the margin - bbox[0] -= ki2svg(margin) - bbox[1] += ki2svg(margin) - bbox[2] -= ki2svg(margin) - bbox[3] += ki2svg(margin) + bbox[0] -= ki2svg(margin[0]) + bbox[1] += ki2svg(margin[1]) + bbox[2] -= ki2svg(margin[2]) + bbox[3] += ki2svg(margin[3]) root.attrib["viewBox"] = "{} {} {} {}".format( bbox[0], bbox[2], @@ -1063,7 +1063,7 @@ class PcbPlotter(): self.libs: List[str] = [] # Names of available libraries self._libs_path: List[str] = [] self.style: Any = {} # Color scheme - self.margin: int = 0 # Margin of the resulting document + self.margin: tuple = (0, 0, 0, 0) # Margin of the resulting document self.compute_bbox: bool = False # Adjust the bbox using the SVG drawings self.kicad_bb_only_edge: bool = False # Use the PCB edge when asking the BBox to KiCad diff --git a/kibot/out_pcbdraw.py b/kibot/out_pcbdraw.py index acfadcc9..c447d3e8 100644 --- a/kibot/out_pcbdraw.py +++ b/kibot/out_pcbdraw.py @@ -183,6 +183,21 @@ class PcbDrawRemapComponents(Optionable): raise KiPlotConfigurationError("The component remapping must specify a `ref`, a `lib` and a `comp`") +class PcbDrawMargin(Optionable): + """ To adjust each margin """ + def __init__(self): + super().__init__() + with document: + self.left = 0 + """ Left margin [mm] """ + self.right = 0 + """ Right margin [mm] """ + self.top = 0 + """ Top margin [mm] """ + self.bottom = 0 + """ Bottom margin [mm] """ + + class PcbDrawOptions(VariantOptions): def __init__(self): with document: @@ -221,8 +236,8 @@ class PcbDrawOptions(VariantOptions): """ *[svg,png,jpg,bmp] Output format. Only used if no `output` is specified """ self.output = GS.def_global_output """ *Name for the generated file """ - self.margin = 0 - """ [0,100] Margin around the generated image [mm] """ + self.margin = PcbDrawMargin + """ [number|dict] Margin around the generated image [mm] """ self.outline_width = 0.15 """ [0,10] Width of the trace to draw the PCB border [mm]. Note this also affects the drill holes """ @@ -259,6 +274,15 @@ class PcbDrawOptions(VariantOptions): # Highlight if isinstance(self.highlight, type): self.highlight = None + # Margin + if isinstance(self.margin, type): + self.margin = (0, 0, 0, 0) + elif isinstance(self.margin, PcbDrawMargin): + self.margin = (mm2ki(self.margin.left), mm2ki(self.margin.right), + mm2ki(self.margin.top), mm2ki(self.margin.bottom)) + else: + margin = mm2ki(self.margin) + self.margin = (margin, margin, margin, margin) # Filter if isinstance(self.show_components, type): self.show_components = None @@ -388,7 +412,7 @@ class PcbDrawOptions(VariantOptions): plotter.libs = self.libs plotter.render_back = self.bottom plotter.mirror = self.mirror - plotter.margin = mm2ki(self.margin) + plotter.margin = self.margin if self.style: if isinstance(self.style, str): plotter.resolve_style(self.style) diff --git a/tests/yaml_samples/pcbdraw.kibot.yaml b/tests/yaml_samples/pcbdraw.kibot.yaml index 779d129b..508d580d 100644 --- a/tests/yaml_samples/pcbdraw.kibot.yaml +++ b/tests/yaml_samples/pcbdraw.kibot.yaml @@ -51,7 +51,11 @@ outputs: vcuts: True warnings: visible dpi: 600 - # margin: 2 + # margin: + # left: 5 + # right: 1 + # top: 0 + # bottom: 6 # outline_width: 3 # show_solderpaste: false resistor_remap: