#NEWLINE# def gerbers_to_svg( manufacturer = 'default' ) : #NEWLINE# #INDENT# '\n    Takes Gerber files as input and generates an SVG of them\n    ' #NEWLINE# def normalise_gerber_number( gerber_number , axis , form ) : #NEWLINE# #INDENT# '\n        Takes a Gerber number and converts it into a float using\n        the formatting defined in the Gerber header\n        ' #NEWLINE# number = ( gerber_number / pow ( 10.0 , form [ axis ] [ 'decimal' ] ) ) #NEWLINE# return number #NEWLINE# #UNINDENT# def parsed_grammar_to_dict( parsed_grammar ) : #NEWLINE# #INDENT# '\n        Converts the Gerber parsing results to an SVG.\n        ' #NEWLINE# gerber_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# current_aperture = None #NEWLINE# new_shape = True #NEWLINE# for line in parsed_grammar : #NEWLINE# #INDENT# if line . dump ( ) : #NEWLINE# #INDENT# if line . format : #NEWLINE# #INDENT# if ( gerber_dict . get ( 'format' ) is None ) : #NEWLINE# #INDENT# gerber_dict [ 'format' ] = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# tmp = gerber_dict [ 'format' ] #NEWLINE# tmp [ 'notation' ] = line [ 'format' ] [ 'notation' ] #NEWLINE# tmp [ 'zeros' ] = line [ 'format' ] [ 'zeros' ] #NEWLINE# tmp [ 'x' ] = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# tmp [ 'x' ] [ 'integer' ] = line [ 'format' ] [ 'x' ] [ 'integer' ] #NEWLINE# tmp [ 'x' ] [ 'decimal' ] = line [ 'format' ] [ 'x' ] [ 'decimal' ] #NEWLINE# tmp [ 'y' ] = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# tmp [ 'y' ] [ 'integer' ] = line [ 'format' ] [ 'x' ] [ 'integer' ] #NEWLINE# tmp [ 'y' ] [ 'decimal' ] = line [ 'format' ] [ 'x' ] [ 'decimal' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if line . units : #NEWLINE# #INDENT# gerber_dict [ 'units' ] = line [ 'units' ] [ 'units' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if line . aperture_definition : #NEWLINE# #INDENT# tmp = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# if ( line [ 'aperture_definition' ] [ 'type' ] == 'circle' ) : #NEWLINE# #INDENT# tmp [ 'type' ] = 'circle' #NEWLINE# tmp [ 'diameter' ] = line [ 'aperture_definition' ] [ 'diameter' ] #NEWLINE# tmp [ 'number' ] = line [ 'aperture_definition' ] [ 'number' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( line [ 'aperture_definition' ] [ 'type' ] == 'rect' ) : #NEWLINE# #INDENT# tmp [ 'type' ] = 'rect' #NEWLINE# tmp [ 'width' ] = line [ 'aperture_definition' ] [ 'width' ] #NEWLINE# tmp [ 'height' ] = line [ 'aperture_definition' ] [ 'height' ] #NEWLINE# tmp [ 'number' ] = line [ 'aperture_definition' ] [ 'number' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( 'ERROR: cannot recognise aperture definition type' ) #NEWLINE# #UNINDENT# #UNINDENT# if ( gerber_dict . get ( 'aperture-definitions' ) is None ) : #NEWLINE# #INDENT# gerber_dict [ 'aperture-definitions' ] = [ ] #NEWLINE# #UNINDENT# gerber_dict [ 'aperture-definitions' ] . append ( tmp ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if line . polarity_change : #NEWLINE# #INDENT# if ( gerber_dict . get ( 'features' ) is None ) : #NEWLINE# #INDENT# gerber_dict [ 'features' ] = [ ] #NEWLINE# #UNINDENT# polarity = line [ 'polarity_change' ] [ 'polarity' ] #NEWLINE# polarity_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# polarity_dict [ 'polarity' ] = polarity #NEWLINE# polarity_dict [ 'shapes' ] = [ ] #NEWLINE# gerber_dict [ 'features' ] . append ( polarity_dict ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if line . aperture_change : #NEWLINE# #INDENT# tmp = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# tmp [ 'type' ] = 'aperture-change' #NEWLINE# tmp [ 'number' ] = line . aperture_change [ 'number' ] #NEWLINE# gerber_dict [ 'features' ] [ ( - 1 ) ] [ 'shapes' ] . append ( tmp ) #NEWLINE# tmp = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# tmp [ 'type' ] = 'stroke' #NEWLINE# tmp [ 'segments' ] = [ ] #NEWLINE# gerber_dict [ 'features' ] [ ( - 1 ) ] [ 'shapes' ] . append ( tmp ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if line . start_closed_shape : #NEWLINE# #INDENT# tmp = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# tmp [ 'type' ] = 'fill' #NEWLINE# tmp [ 'segments' ] = [ ] #NEWLINE# gerber_dict [ 'features' ] [ ( - 1 ) ] [ 'shapes' ] . append ( tmp ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( line . move or line . draw or line . flash ) : #NEWLINE# #INDENT# if line . move : #NEWLINE# #INDENT# command_name = 'move' #NEWLINE# item = line . move #NEWLINE# #UNINDENT# if line . draw : #NEWLINE# #INDENT# command_name = 'draw' #NEWLINE# item = line . draw #NEWLINE# #UNINDENT# if line . flash : #NEWLINE# #INDENT# command_name = 'flash' #NEWLINE# item = line . flash #NEWLINE# #UNINDENT# point = Point ( normalise_gerber_number ( item [ 'x' ] , 'x' , gerber_dict [ 'format' ] ) , normalise_gerber_number ( item [ 'y' ] , 'y' , gerber_dict [ 'format' ] ) ) #NEWLINE# tmp = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# tmp [ 'type' ] = command_name #NEWLINE# tmp [ 'coord' ] = point #NEWLINE# gerber_dict [ 'features' ] [ ( - 1 ) ] [ 'shapes' ] [ ( - 1 ) ] [ 'segments' ] . append ( tmp ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if line . end_closed_shape : #NEWLINE# #INDENT# new_shape = True #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return gerber_dict #NEWLINE# #UNINDENT# def create_gerber_svg_data( gerber_data ) : #NEWLINE# #INDENT# '\n        Returns an SVG element of the input Gerber data\n        ' #NEWLINE# gerber_data_parsed = gerber_grammar . parseString ( gerber_data ) #NEWLINE# gerber_data_dict = parsed_grammar_to_dict ( gerber_data_parsed ) #NEWLINE# gerber_data_svg = svg . generate_svg_from_gerber_dict ( gerber_data_dict ) #NEWLINE# return gerber_data_svg #NEWLINE# #UNINDENT# board_shape_gerber_lp = None #NEWLINE# shape = config . brd [ 'board_outline' ] [ 'shape' ] #NEWLINE# board_shape_type = shape . get ( 'type' ) #NEWLINE# if ( board_shape_type in [ 'rect' , 'rectangle' ] ) : #NEWLINE# #INDENT# offset = utils . to_Point ( ( shape . get ( 'offset' ) or [ 0 , 0 ] ) ) #NEWLINE# board_shape_path = svg . rect_to_path ( shape ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( board_shape_type == 'path' ) : #NEWLINE# #INDENT# board_shape_path = shape . get ( 'value' ) #NEWLINE# board_shape_gerber_lp = shape . get ( 'gerber_lp' ) #NEWLINE# if ( board_shape_path is None ) : #NEWLINE# #INDENT# print ( "ERROR: couldn't find a path under key 'value' for board outline" ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# print ( ( "ERROR: unrecognised board shape type: %s. Possible options are 'rect' or 'path'" % board_shape_type ) ) #NEWLINE# #UNINDENT# #UNINDENT# board_shape_path_relative = svg . absolute_to_relative_path ( board_shape_path ) #NEWLINE# ( board_width , board_height , board_outline ) = svg . transform_path ( board_shape_path_relative , True ) #NEWLINE# display_width = board_width #NEWLINE# display_height = board_height #NEWLINE# sig_dig = config . cfg [ 'significant-digits' ] #NEWLINE# display_frame_buffer = ( config . cfg . get ( 'display-frame-buffer' ) or 1.0 ) #NEWLINE# gerber = et . Element ( 'svg' , width = ( str ( display_width ) + config . brd [ 'config' ] [ 'units' ] ) , height = ( str ( gerber_svg ) + config . brd [ 'config' ] [ 'units' ] ) , viewBox = ( ( ( ( ( ( str ( ( ( - display_frame_buffer ) / 2 ) ) + ' ' ) + str ( ( ( - display_frame_buffer ) / 2 ) ) ) + ' ' ) + str ( ( board_width + display_frame_buffer ) ) ) + ' ' ) + str ( ( board_height + display_frame_buffer ) ) ) , version = '1.1' , nsmap = cfg [ 'namespace' ] , fill = 'black' ) #NEWLINE# doc = et . ElementTree ( gerber ) #NEWLINE# gerber_layers = svg . create_layers_for_gerber_svg ( gerber ) #NEWLINE# production_path = os . path . join ( config . cfg [ 'base-dir' ] , config . cfg [ 'locations' ] [ 'build' ] , 'production' ) #NEWLINE# pcbmode_version = config . cfg [ 'version' ] #NEWLINE# board_name = config . cfg [ 'name' ] #NEWLINE# board_revision = config . brd [ 'config' ] . get ( 'rev' ) #NEWLINE# base_name = ( '%s_rev_%s' % ( board_name , board_revision ) ) #NEWLINE# gerber_grammar = gerber_grammar_generator ( ) #NEWLINE# for foil in [ 'outline' ] : #NEWLINE# #INDENT# gerber_file = os . path . join ( production_path , ( base_name + ( '_%s.ger' % foil ) ) ) #NEWLINE# gerber_data = open ( gerber_file , 'r' ) . read ( ) #NEWLINE# gerber_svg = create_gerber_svg_data ( gerber_data ) #NEWLINE# gerber_svg_layer = gerber_layers [ foil ] [ 'layer' ] #NEWLINE# gerber_svg_layer . append ( gerber_svg ) #NEWLINE# print ( foil ) #NEWLINE# #UNINDENT# for pcb_layer in config . stk [ 'layer-names' ] : #NEWLINE# #INDENT# for foil in [ 'conductor' , 'silkscreen' , 'soldermask' ] : #NEWLINE# #INDENT# gerber_file = os . path . join ( production_path , ( base_name + ( '_%s_%s.ger' % ( pcb_layer , foil ) ) ) ) #NEWLINE# gerber_data = open ( gerber_file , 'r' ) . read ( ) #NEWLINE# gerber_svg = create_gerber_svg_data ( gerber_data ) #NEWLINE# gerber_svg_layer = gerber_layers [ pcb_layer ] [ foil ] [ 'layer' ] #NEWLINE# gerber_svg_layer . append ( gerber_svg ) #NEWLINE# print ( foil ) #NEWLINE# #UNINDENT# #UNINDENT# output_file = os . path . join ( config . cfg [ 'base-dir' ] , config . cfg [ 'locations' ] [ 'build' ] , ( cfg [ 'board_name' ] + '_gerber.svg' ) ) #NEWLINE# try : #NEWLINE# #INDENT# f = open ( output_file , 'wb' ) #NEWLINE# #UNINDENT# except IOError as e : #NEWLINE# #INDENT# print ( 'I/O error({0}): {1}' . format ( e . errno , e . strerror ) ) #NEWLINE# #UNINDENT# f . write ( et . tostring ( doc , pretty_print = True ) ) #NEWLINE# f . close ( ) #NEWLINE# return