Moved all IU -> SVG stuff to GS

This commit is contained in:
Salvador E. Tropea 2023-02-15 10:07:49 -03:00
parent ca3f76f3b8
commit 4a440078ae
5 changed files with 32 additions and 49 deletions

View File

@ -34,6 +34,10 @@ elif hasattr(pcbnew, 'PCB_PLOT_PARAMS'):
NO_DRILL_SHAPE = pcbnew.PCB_PLOT_PARAMS.NO_DRILL_SHAPE
SMALL_DRILL_SHAPE = pcbnew.PCB_PLOT_PARAMS.SMALL_DRILL_SHAPE
FULL_DRILL_SHAPE = pcbnew.PCB_PLOT_PARAMS.FULL_DRILL_SHAPE
# KiCad 6 uses IUs for SVGs, with option for SVG_Precision
# KiCad 5 uses a very different scale based on inches
# KiCad 7 uses mm
KICAD5_SVG_SCALE = 116930/297002200
class GS(object):
@ -510,6 +514,26 @@ class GS(object):
def get_pad_orientation_in_radians(pad):
return pad.GetOrientation().AsRadians() if GS.ki7 else pad.GetOrientationRadians()
@staticmethod
def iu_to_svg(values, svg_precision):
""" Converts 1 or more values from KiCad internal IUs to the units used for SVGs """
if not isinstance(values, tuple):
values = [values]
if GS.ki5:
return tuple(map(lambda x: int(round(x*KICAD5_SVG_SCALE)), values))
if GS.ki7:
return tuple(map(GS.to_mm, values))
# KiCad 6
mult = 10.0 ** (svg_precision - 6)
return tuple(map(lambda x: int(round(x*mult)), values))
@staticmethod
def svg_round(val):
""" KiCad 5/6 uses integers for SVG units, KiCad 7 uses mm and hence floating point """
if GS.ki7:
return val
return int(round(val))
# @staticmethod
# def create_wxpoint(x, y):
# return pcbnew.wxPoint(x, y)

View File

@ -45,7 +45,7 @@ from .sexpdata import load, SExpData
from .v6_sch import (_check_is_symbol_list, _check_float, _check_integer, _check_symbol_value, _check_str, _check_symbol,
_check_relaxed, _get_points, _check_symbol_str)
from ..svgutils.transform import ImageElement, GroupElement
from ..misc import W_WKSVERSION, KICAD5_SVG_SCALE
from ..misc import W_WKSVERSION
from .. import log
logger = log.get_logger()
@ -429,19 +429,13 @@ class WksBitmap(WksDrawing):
# KiCad 6 can adjust the precision
# The default is 6 and makes 1 KiCad unit == 1 SVG unit
# But this isn't supported by browsers (Chrome and Firefox)
scale = 10.0 ** (svg_precision - 6)
scale = GS.iu_to_svg(1.0, svg_precision)[0]
for _ in range(e.repeat):
img = ImageElement(io.BytesIO(s), w, h)
x = pos.x-round(w/2)
y = pos.y-round(h/2)
img.moveto(x, y)
if GS.ki5:
# KiCad 5 uses Inches and with less resolution
img.scale(KICAD5_SVG_SCALE)
elif GS.ki7:
img.scale(GS.to_mm(1))
elif svg_precision != 6:
img.scale(scale)
img.scale(scale)
# Put the image in a group
g = GroupElement([img])
# Add the group to the SVG

View File

@ -268,8 +268,6 @@ SOLDER_COLORS = {'green': ("#285e3a", "#208b47"),
'yellow': ("#73823d", "#f2a756"),
'purple': ("#30234a", "#451d70")}
SILK_COLORS = {'black': "0b1013", 'white': "d5dce4"}
# KiCad 6 uses IUs for SVGs, but KiCad 5 uses a very different scale based on inches
KICAD5_SVG_SCALE = 116930/297002200
# Some browser name to pretend
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0'
# Text used to disable 3D models

View File

@ -46,7 +46,7 @@ from .kicad.patch_svg import patch_svg_file
from .kicad.config import KiConf
from .kicad.v5_sch import SchError
from .kicad.pcb import PCB
from .misc import PDF_PCB_PRINT, W_PDMASKFAIL, KICAD5_SVG_SCALE, W_MISSTOOL, PCBDRAW_ERR, W_PCBDRAW
from .misc import PDF_PCB_PRINT, W_PDMASKFAIL, W_MISSTOOL, PCBDRAW_ERR, W_PCBDRAW
from .create_pdf import create_pdf_from_pages
from .macros import macros, document, output_class # noqa: F401
from .drill_marks import DRILL_MARKS_MAP, add_drill_marks
@ -918,31 +918,9 @@ class PCB_PrintOptions(VariantOptions):
# This is the autocenter computation used by KiCad
scale_x = scale_y = scale
board_center = GS.board.GetBoundingBox().GetCenter()
# We use fresh variables because board_center is a VECTOR2I in v7 (integers)
bcx = board_center.x
bcy = board_center.y
if GS.ki5:
# KiCad 5 uses a different precision, we must adjust
bcx = round(board_center.x*KICAD5_SVG_SCALE)
bcy = round(board_center.y*KICAD5_SVG_SCALE)
elif GS.ki7:
# KiCad 7 coordinates are in mm
bcx = GS.to_mm(board_center.x)
bcy = GS.to_mm(board_center.y)
elif self.svg_precision != 6:
# KiCad 6 can adjust the precision
# The default is 6 and makes 1 KiCad unit == 1 SVG unit
# But this isn't supported by browsers (Chrome and Firefox)
divider = 10.0 ** (6 - self.svg_precision)
bcx = round(board_center.x/divider)
bcy = round(board_center.y/divider)
if GS.ki7:
offset_x = (bcx*scale-(paper_size_x/2.0))/scale
offset_y = (bcy*scale-(paper_size_y/2.0))/scale
else:
# Work with integers
offset_x = round((bcx*scale-(paper_size_x/2.0))/scale)
offset_y = round((bcy*scale-(paper_size_y/2.0))/scale)
bcx, bcy = GS.iu_to_svg((board_center.x, board_center.y), self.svg_precision)
offset_x = GS.svg_round((bcx*scale-(paper_size_x/2.0))/scale)
offset_y = GS.svg_round((bcy*scale-(paper_size_y/2.0))/scale)
if mirror:
scale_x = -scale_x
offset_x += round(paper_size_x/scale)

View File

@ -10,7 +10,6 @@ from pcbnew import PLOT_FORMAT_SVG, FromMM, ToMM
from .drill_marks import DrillMarks
from .gs import GS
from .kicad.patch_svg import change_svg_viewbox
from .misc import KICAD5_SVG_SCALE
from .out_base import PcbMargin
from .out_any_layer import AnyLayer
from .macros import macros, document, output_class # noqa: F401
@ -81,17 +80,7 @@ class SVGOptions(DrillMarks):
width = ToMM(bbox[2])*0.1
height = ToMM(bbox[3])*0.1
# Scale factor to convert KiCad IU to the SVG units
if GS.ki5:
mult = KICAD5_SVG_SCALE
# View port in SVG units
bbox = tuple(map(lambda x: int(x*mult), bbox))
elif GS.ki7:
# View port in SVG units
bbox = tuple(map(lambda x: GS.to_mm(x), bbox))
else:
mult = 10.0 ** (self.svg_precision - 6)
# View port in SVG units
bbox = tuple(map(lambda x: int(x*mult), bbox))
bbox = GS.iu_to_svg(bbox, self.svg_precision)
logger.debug('Adjusting SVG viewBox to {} for width {} cm and height {} cm'.format(bbox, width, height))
for f in self._generated_files.values():
fname = os.path.join(output_dir, f)