diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a6c91ee..e07e3ec3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Diff: - Option to compare only the first schematic page. (See #319) - PcbDraw: - - Support for BMP output + - BMP output format + - Image margin ### Changed - Diff: diff --git a/README.md b/README.md index e6687ee0..be7d7a1e 100644 --- a/README.md +++ b/README.md @@ -2581,6 +2581,7 @@ 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 in millimeters. - `no_drillholes`: [boolean=false] Do not make holes transparent. - `placeholder`: [boolean=false] Show placeholder for missing components. - `pre_transform`: [string|list(string)='_none'] Name of the filter to transform fields before applying other filters. diff --git a/docs/samples/generic_plot.kibot.yaml b/docs/samples/generic_plot.kibot.yaml index f105ab0f..b504a0b5 100644 --- a/docs/samples/generic_plot.kibot.yaml +++ b/docs/samples/generic_plot.kibot.yaml @@ -1362,6 +1362,8 @@ outputs: highlight: [] # [list(string)=[]] List of libraries libs: [] + # [number=0] [0,100] Margin around the generated image in millimeters + margin: 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 3ae27e15..5880bc05 100644 --- a/kibot/PcbDraw/README.md +++ b/kibot/PcbDraw/README.md @@ -97,6 +97,7 @@ No current changes - Disabled `shrink_svg` - Changes the old behavior, so this should be optional - Pulls a problematic dependency: svgpathtool + - But we keep the margin addition - Changed calls to `ComputeBoundingBox()` to use `aBoardEdgesOnly=True` - To get the same behavior as 0.9.0-5 - This changes the size of the SVG to the size of the board diff --git a/kibot/PcbDraw/plot.py b/kibot/PcbDraw/plot.py index ee8c794c..4c2edd76 100644 --- a/kibot/PcbDraw/plot.py +++ b/kibot/PcbDraw/plot.py @@ -506,11 +506,11 @@ 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) -> None: -# """ -# Shrink the SVG canvas to the size of the drawing. Add margin in -# KiCAD units. -# """ +def shrink_svg(svg: etree.ElementTree, margin: int) -> None: + """ + Shrink the SVG canvas to the size of the drawing. Add margin in + KiCAD units. + """ # # We have to overcome the limitation of different base types between # # PcbDraw and svgpathtools # from xml.etree.ElementTree import fromstring as xmlParse @@ -533,18 +533,23 @@ def hack_is_valid_bbox(box: Any): # type: ignore # continue # bbox = merge_bbox(bbox, box) # bbox = list(bbox) -# bbox[0] -= ki2svg(margin) -# bbox[1] += ki2svg(margin) -# bbox[2] -= ki2svg(margin) -# bbox[3] += ki2svg(margin) -# -# root = svg.getroot() -# root.attrib["viewBox"] = "{} {} {} {}".format( -# bbox[0], bbox[2], -# bbox[1] - bbox[0], bbox[3] - bbox[2] -# ) -# root.attrib["width"] = str(ki2mm(svg2ki(bbox[1] - bbox[0]))) + "mm" -# root.attrib["height"] = str(ki2mm(svg2ki(bbox[3] - bbox[2]))) + "mm" + # Get the current viewBox + root = svg.getroot() + x, y, vw, vh = [float(x) for x in root.attrib["viewBox"].split()] + 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) + + root.attrib["viewBox"] = "{} {} {} {}".format( + bbox[0], bbox[2], + bbox[1] - bbox[0], bbox[3] - bbox[2] + ) + root.attrib["width"] = str(ki2mm(svg2ki(bbox[1] - bbox[0]))) + "mm" + root.attrib["height"] = str(ki2mm(svg2ki(bbox[3] - bbox[2]))) + "mm" def remove_empty_elems(tree: etree.Element) -> None: """ @@ -1068,7 +1073,7 @@ class PcbPlotter(): plotter.render(self) remove_empty_elems(self._document.getroot()) remove_inkscape_annotation(self._document.getroot()) - # shrink_svg(self._document, self.margin) + shrink_svg(self._document, self.margin) return self._document diff --git a/kibot/out_pcbdraw.py b/kibot/out_pcbdraw.py index 73ae33b6..4ffcd668 100644 --- a/kibot/out_pcbdraw.py +++ b/kibot/out_pcbdraw.py @@ -161,6 +161,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 in millimeters """ super().__init__() def config(self, parent): @@ -307,8 +309,7 @@ class PcbDrawOptions(VariantOptions): plotter.libs = self.libs plotter.render_back = self.bottom plotter.mirror = self.mirror - # TODO: Allow margin configuration - plotter.margin = mm2ki(1.5) + plotter.margin = mm2ki(self.margin) # TODO: Pass it directly? If no: remove file? tmp_style = None if self.style: diff --git a/tests/yaml_samples/pcbdraw.kibot.yaml b/tests/yaml_samples/pcbdraw.kibot.yaml index 836fd438..850ca73a 100644 --- a/tests/yaml_samples/pcbdraw.kibot.yaml +++ b/tests/yaml_samples/pcbdraw.kibot.yaml @@ -46,6 +46,7 @@ outputs: vcuts: True warnings: visible dpi: 600 + # margin: 2 - name: PcbDraw2 comment: "PcbDraw test bottom"