-
|
Does import gerber
gko_file = path/to/some/file
outline_data = gerber.read(gko_file)
# Calculate board dimensions
in_width, in_height = outline_data.sizeHere is my implementation using from pygerber.gerber.parser import parse as gerber_parser # May need to install with 'pip install git+https://github.com/Argmaster/pygerber' if pip release is not up to date
gko_file = path/to/some/file # Read the board outline file
with open(gko_file, "r") as file:
outline_data = gerber_parser(file.read())
# Gather x and y coordinates from outline_data
xCoords = []
yCoords = []
for node in outline_data.nodes:
if hasattr(node, 'x') and node.x is not None:
xCoords.append(int(node.x.value))
if hasattr(node, 'y') and node.y is not None:
yCoords.append(int(node.y.value))
if hasattr(node, 'mode'):
modeName = node.mode.name
modeValue = node.mode.value
if modeName != 'IMPERIAL' or modeValue != 'IN':
exit(f"ERROR: Unit ({modeName}, {modeValue}) not as expected (IMPERIAL, IN)")
if hasattr(node, 'zeros'):
xIntegral = node.x_integral
xDecimal = node.x_decimal
yIntegral = node.y_integral
yDecimal = node.y_decimal
if xIntegral != 2 or xDecimal != 5 or yIntegral != 2 or yDecimal != 5:
exit(f"ERROR: Coordinate notation ({xIntegral}, {xDecimal}, {yIntegral}, {yDecimal}) not as expected (2, 5, 2, 5)")
# Calculate board width and height from x and y coordinates
xCoords.sort()
yCoords.sort()
x_min = xCoords[0]
x_max = xCoords[-1]
y_min = yCoords[0]
y_max = yCoords[-1]
board_width_in_e5 = x_max - x_min
board_height_in_e5 = y_max - y_min
board_width_mil = board_width_in_e5 * 1e-2
board_height_mil = board_height_in_e5 * 1e-2
board_width_in = board_width_mil * 1e-3
board_height_in = board_height_mil * 1e-3
board_width_mm = board_width_mil * 2.54e-2
board_height_mm = board_height_mil * 2.54e-2Thanks to the team for your work on this project! |
Beta Was this translation helpful? Give feedback.
Answered by
Argmaster
Mar 19, 2025
Replies: 1 comment 1 reply
-
|
So, first of all I would recommend using PyGerber 3.0.0a4 which is not exactly stable but has much nicer API. from pygerber.gerber.api import GerberFile
from pathlib import Path
path_to_my_gerber_file = Path().cwd() / "example.grb"
gerber_file = GerberFile.from_file(path_to_my_gerber_file)
image = gerber_file.render_with_pillow()
space = image.get_image_space()
print(space.width_mm, space.height_mm) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
nabelekt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, first of all I would recommend using PyGerber 3.0.0a4 which is not exactly stable but has much nicer API.
Second of all, I don't think it has to be that complicated, you could just render the outline and get size of image rendered from rendering result: