Exporting layout as SVG (browser destination)

I’m not even sure that the svgs produced by KiCad 6 meet the svg standard - when I run over them with svgcheck from ietf.org they have a lot of errors even though it is possible to open them in Inkscape. Certainly, they look completely different or fail to render at all in various browsers. To add to your list, Safari does not work either. I think we can safely say that the svgs from KiCad 6 do not render or do not render correctly in the vast majority of browsers on multiple platforms.

The svgs produced by Kicad 5 did not have this issue - as you say, the numerical values are around 5 digits compared to the 8 or 9 from KiCad 6. I can confirm that the perl script works (I did something similar with sed) and the reduced values work. The resultant svg can be opened in Safari, Chrome, and Firefox (as well as Inkscape & Affinity Designer).

Pragmatically, many, perhaps, most users will want to use svgs in web browsers and, as they stand, they are simply not suitable. It may be that there are bugs in the browser code for all major browsers but that is going to take some time to fix. In the meanwhile, I agree that would be helpful to have a switch to specify the svg output format in terms of precision. Can the issue be reopened? If any devs are prepared to look at this, it would also be helpful to consider this issue at the same time.

I’m relieved to see others with the same problem. For about a week I thought it might be some weird software conflict on my PC.

My interpretation of the open issue !974 (link above) is that it will fix the very large numbers by converting them to mm scale. I’m not really across the ins-and-outs of gitlab but it look to me the code change has been completed and is marked for inclusion into v7.0 which is schedule for release on 25Jan2023.

So :squinting at horizon: a solution seems to be in sight?

I have now found this but not sure how to use it yet! I presume I can pass a power to it - I think the source code defaults to ^4 and if this is reduced to ^1, we may be in business!

pcbnew.PLOTTER.SetSvgCoordinatesFormat

Type: <class 'function'>

Value: <function PLOTTER.SetSvgCoordinatesFormat at 0x7f93a8accc10>

Docstring:

"""SetSvgCoordinatesFormat(PLOTTER self, unsigned int aResolution, bool aUseInches=False)"""

Source Code:

    def SetSvgCoordinatesFormat(self, aResolution: "unsigned int", aUseInches: "bool"=False) -> "void":
        r"""SetSvgCoordinatesFormat(PLOTTER self, unsigned int aResolution, bool aUseInches=False)"""
        return _pcbnew.PLOTTER_SetSvgCoordinatesFormat(self, aResolution, aUseInches)

I not could use this SetSvgCoordinatesFormat on python

I am trying to use it as

pn.PLOTTER.SetSvgCoordinatesFormat(1, 0)

I am receiving this

Traceback (most recent call last):
  File "plot_kicad_pcb.py", line 229, in <module>
    processBoard(board_path, plot_dir, args.quiet, args.verbose, args.frame)
  File "plot_kicad_pcb.py", line 67, in processBoard
    pn.PLOTTER.SetSvgCoordinatesFormat(1, 0)
  File "/usr/lib/python3/dist-packages/pcbnew.py", line 8367, in SetSvgCoordinatesFormat
    return _pcbnew.PLOTTER_SetSvgCoordinatesFormat(self, aResolution, aUseInches)
NotImplementedError: Wrong number or type of arguments for overloaded function 'PLOTTER_SetSvgCoordinatesFormat'.
  Possible C/C++ prototypes are:
    PLOTTER::SetSvgCoordinatesFormat(unsigned int,bool)
    PLOTTER::SetSvgCoordinatesFormat(unsigned int)

Am I doing something wrong?

That’s were I get stuck too! It would be helpful if one of the devs could shed some light.

Perhaps @craftyjon or @jp-charras might be able to help?

I just tested this new version (6.0.2) right now. I did not have had any luck yet with generating SVG files. They still have issues. On Chrome it shows the image, but it does not have VIAS. It still does not render on Firefox, probably on Safari too. Kicad 5 was working, it could give a hint on where the problem is.

Also, it may be an issue where I am not exporting my SVG files correctly with python. Then in this case it would be nice if someone could share a snippet showing how I can achieve that.

You are trying to call non-static method from class namespace instead of from object. That doesn’t work, you need to instantiate PLOTTER and call SetSvgCoordinatesFormat() as method.

1 Like

an FYI for those reading along - issue 974 was closed without merging :disappointed:

It was closed without merging because another developer merged something that should accomplish the same thing – the specific commit is linked in that merge request.

It’s in the v7 branch, though, so you won’t see the fix in v6 unless it’s decided to backport that commit to v6 (don’t hold your breath :slight_smile: )

2 Likes

Adding here macOS (Monterey) cannot render the exported svg files.

Chrome shows the image, but vias are missing. Dots/Circles on top right are the vias displayed in the wrong place.

My quick, dirty and dumb workaround to reduce the number of digits on the floating point and also fix the viewport for vias is the only thing I could do to actually improve this. But it is still not working pretty well on Safari. (It works on Chrome and Firefox). It works on 2/3 of places where I use svgs on Safari.

This is the workaround is someone is interested.

# Fix for Firefox
# Scale down numbers. This is not a smart fix, it should not be applied everywhere
perl -i -pe 's{(?<![\d#])(\d{5,}(?:\.\d+)?)(?!\d)}{sprintf("%.1f", $1/10000);}ge' "${svg}"

# Fix back numbers with double floating point that were damaged
sed -i "s/\.\([0-9]\+\)\.[0-9]\+/.\1/g" "${svg}"

# This is needed to fix VIAS
rsvg-convert -a -w 500 -f svg "${svg}" > .tmp.svg
mv .tmp.svg "${svg}"
1 Like

I figured out a solution in Python by using SetSvgPrecision (this thread put me on the right path, many thanks!)

3 Likes

This looks working well. Thanks for the feedback @kasbah

I believe this is going to solve the issue here in this thread.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.