Diogo Almeida
Diogo Almeida

Reputation: 11

Extract board outline of an PCB

I have a problem that i can´t reach the exact dimensions of the x and y of the PCB Schematic in mm or inch.

The method that I was using is, it starts by opening the file, and then reads each line. If the line starts with G36, it means that the parsing of the outline has started. If the line starts with G37, it means that the parsing of the outline has ended. If the line starts with X, it means that it contains coordinates, in the following code:

def extract_coordinates_from_line(line):
    # Extract X and Y coordinates from a line
    parts = line[1:].split("Y")
    try:
        x = float(parts[0])
        y = float(parts[1].split("D")[0])
        return x, y
    except ValueError:
        # Handle the case where the conversion fails
        return None

def extract_board_outline(gerber_file_path):
    board_outline = []
    parsing_outline = False

    with open(gerber_file_path, 'r') as file:
        for line in file:
            if "G36" in line:
                parsing_outline = True
            elif "G37" in line:
                parsing_outline = False
            elif parsing_outline and line.startswith("X"):
                coordinates = extract_coordinates_from_line(line)
                if coordinates:
                    board_outline.append(coordinates)

    return board_outline

def calculate_board_size(board_outline, scale_factor=1):  # Assuming the default unit is inches
    if not board_outline:
        return 0, 0

    min_x = min(point[0] for point in board_outline)
    max_x = max(point[0] for point in board_outline)
    min_y = min(point[1] for point in board_outline)
    max_y = max(point[1] for point in board_outline)

    width = (max_x - min_x) * scale_factor
    height = (max_y - min_y) * scale_factor

    return width, height

def main():
    gerber_file_path = 'top2.gbr'  # Replace with the actual path to your Gerber file
    board_outline = extract_board_outline(gerber_file_path)

    if not board_outline:
        print("No board outline found in the Gerber file.")
    else:
        print("Board Outline Coordinates:")
        for i, (x, y) in enumerate(board_outline, start=1):
            print(f"Point {i}: X={x}, Y={y}")

        width, height = calculate_board_size(board_outline)
        width /= 100
        height /= 100
        width *= 1.36
        height *= 1.36
        print(f"\nBoard Size: Width={width}, Height={height}")

if __name__ == "__main__":
    main()

I already define the separation of the value in mm and value with inch, and I'm using the Top.gbr file to get the outline information.

ADD:G04 DipTrace 4.0.0.5*
G04 1 - Top.gbr*
%MOMM*%
G04 #@! TF.FileFunction,Copper,L1,Top*
G04 #@! TF.Part,Single*
G04 #@! TA.AperFunction,Conductor*
%ADD15C,0.989*%
%ADD16C,0.789*%
G04 #@! TA.AperFunction,CopperBalancing*
%ADD18C,0.935*%
G04 #@! TA.AperFunction,ComponentPad*
%ADD21R,1.5X1.5*%
%ADD22C,1.5*%
%ADD23R,1.7X1.7*%
%ADD24C,1.7*%
%ADD25R,2.5X2.5*%
%ADD26C,2.5*%
%ADD27C,1.27*%
%ADD28R,1.27X1.27*%
%ADD29O,2.2X3.6*%
%ADD30C,1.524*%
%ADD31C,1.524*%

%ADD32R,1.6X1.6*%
%ADD33C,1.6*%
%ADD34C,1.5*%
G04 #@! TA.AperFunction,ViaPad*
%ADD35C,1.016*%
%ADD76C,0.31373*%
%FSLAX35Y35*%

Upvotes: 0

Views: 134

Answers (0)

Related Questions