def __init__(self, device_cfg, output=None): self.device_cfg = device_cfg self.scale = self.device_cfg['size_px']['w'] / self.device_cfg[ 'size_mm']['w'] self.exp_test_start = self.EXP_TEST_START self.exp_test_interval = self.EXP_TEST_INTERVAL self.exp_test_samples = self.EXP_TEST_SAMPLES self.exp_test_tmp_dir = None self.exposure_time = self.DEFAULT_EXPOSURE_TIME self.gbr = None self.drl = None self.output = output if not output: self.output = self.DEFAULT_OUTPUT self.payload_ctx = GerberCairoContext(scale=self.scale) self.white = RenderSettings(color=theme.COLORS['white']) self.black = RenderSettings(color=theme.COLORS['black']) self.pcb_width = None self.pcb_height = None self.photon_template = None if device_cfg['photon_template'] != 'built-in': tpl_path = 'data/{}'.format(device_cfg['photon_template']) self.photon_template = resource_filename('pcb_msla', tpl_path)
def generate_previews(fab_output_path, preview_output_path): def read(pattern): files = glob(os.path.join(fab_output_path, pattern)) if not files: print("WARNING: Nothing found matching %s" % pattern) return None return gerber.load_layer(files[0]) def save(name): path = os.path.join(preview_output_path, "%s.png" % name) print("Saving preview to %s" % path) ctx.dump(path) def render(pattern, **kw): layer = read(pattern) if layer is None: print("Not rendering %s" % pattern) return ctx.render_layer(layer, verbose=True, **kw) # Rendering context ctx = GerberCairoContext(scale=10) ctx.color = (80. / 255, 80 / 255., 154 / 255.) ctx.drill_color = ctx.color # Edges render("*.gm1") # Copper render("*.gtl") # Mask render("*.gts") # Silk render("*.gto", settings=RenderSettings(color=theme.COLORS['white'], alpha=0.85)) # Drills render("*.drl") save("pcb-front") ctx.clear() # Edges render("*.gm1") # Copper render("*.gbl") # Mask render("*.gbs") # Silk render( "*.gbo", settings=RenderSettings( color=theme.COLORS['white'], alpha=0.85, mirror= True, # for some reason this, but not the other bottom layer files, needs to be flipped horizontally )) # Drills render("*.drl") save("pcb-back")
def combine(gtl_file, gts_file, gto_file, txt_file, gtp_file, gbl_file, gbs_file, basedir): # Open the gerber files copper = load_layer(gtl_file) mask = load_layer(gts_file) silk = load_layer(gto_file) drill = load_layer(txt_file) placement = load_layer(gtp_file) # Create a new drawing context ctx = GerberCairoContext() # Draw the copper layer. render_layer() uses the default color scheme for the # layer, based on the layer type. Copper layers are rendered as # Draw the soldermask layer ctx.render_layer(copper) ctx.render_layer(mask) ctx.render_layer(silk, settings=RenderSettings(color=theme.COLORS['white'], alpha=0.85)) ctx.render_layer(drill) # Write output to png file ctx.dump(os.path.join(basedir, "top.png")) # Load the bottom layers copper = load_layer(gbl_file) copper_top = load_layer(gtl_file) mask = load_layer(gbs_file) # Clear the drawing ctx = GerberCairoContext() from gerber.rs274x import read, GerberFile # Render bottom layers ctx.render_layer(copper, bounds=copper_top.bounds) ctx.render_layer(mask) ctx.render_layer(drill, settings=RenderSettings(mirror=True)) # Write png file ctx.dump(os.path.join(basedir, "bottom.png")) copper = load_layer(gtl_file) ctx = GerberCairoContext() ctx.render_layer(copper) ctx.dump(os.path.join(basedir, "top-copper.png")) ctx = GerberCairoContext() copper_bottom = load_layer(gbl_file) ctx.render_layer(copper_bottom, bounds=copper.bounds) ctx.dump(os.path.join(basedir, "bottom-copper.png"))
def final(copper, mask, drill, colour, name, silk=None): new_settings = RenderSettings(color=colour, alpha=0.8) new = GerberCairoContext() new.render_layer(copper, settings=new_settings) new.render_layer(mask, settings=new_settings) if (silk != None): new.render_layer(silk) new.render_layer(drill) new.dump(os.path.join(BASE_DIR, name))
def render_layers(self, layers, filename, theme, verbose, dpi, isOutline): scale = int(dpi) self.isOutline = isOutline self.scale = (scale, scale) self.clear() # Render layers bgsettings = theme['background'] for layer in layers: settings = theme.get(layer.layer_class, RenderSettings()) self.render_layer(layer, settings=settings, bgsettings=bgsettings, verbose=verbose) self.dump(filename, verbose)
def draw_component(self, event=None): self.ctx.clear() if len(self.layers) > 0: if self.layer == "TopLayer": copper_settings = RenderSettings(color=theme.COLORS['black'], alpha=0.8, mirror=False) self.ctx.render_layer(self.layers["gtl"], settings=copper_settings, verbose=True) self.ctx.new_render_layer(mirror=False) if self.layer == "BottomLayer": copper_settings = RenderSettings(color=theme.COLORS['black'], alpha=0.8, mirror=True) self.ctx.render_layer(self.layers["gbl"], settings=copper_settings) self.ctx.new_render_layer(mirror=True) self.ctx._color = (1.0, 0.0, 1.0) layer = self.layer if self.clist.tb.selected_row != None: part_number = self.clist.tb.selected_row['PN'] else: part_number = None for c in self.components: if (c['layer'] == layer) and (c['part_number'] == part_number): print("{} {}".format(c['x_mm'], c['y_mm'])) self.ctx.render( gerber.primitives.Circle((c['x_mm'], c['y_mm']), 1)) self.ctx.flatten() buffer = BytesIO() self.ctx.dump(buffer) img = ImageTk.PhotoImage(Image.open(buffer)) self.label.configure(image=img) self.label.image = img
copper = load_layer(prefix + '.GTL') print('.', end='', flush=True) mask = load_layer(prefix + '.GTS') print('.', end='', flush=True) silk = load_layer(prefix + '.GTO') print('.', end='', flush=True) drill = load_layer(prefix + '.TXT') print('.', end='', flush=True) outline = load_layer(prefix + '-fill.GML') print('.', end='', flush=True) print('. end', flush=True) print('drawing ', end='', flush=True) print('.', end='', flush=True) metal_settings = RenderSettings(color=(30.0 / 255.0, 119.0 / 255.0, 93 / 255.0)) bg_settings = RenderSettings(color=(30.0 / 300.0, 110.0 / 300.0, 93 / 300.0)) ctx.render_layer(copper, settings=metal_settings, bgsettings=bg_settings) print('.', end='', flush=True) copper_settings = RenderSettings(color=(0.7 * 1.2, 0.5 * 1.2, 0.1 * 1.2)) ctx.render_layer(mask, settings=copper_settings) print('.', end='', flush=True) our_settings = RenderSettings(color=theme.COLORS['white'], alpha=0.80) ctx.render_layer(silk, settings=our_settings) print('.', end='', flush=True) ctx.render_layer(outline) print('.', end='', flush=True) ctx.render_layer(drill)
def render(BASE_FOLDER): ppi = 1000 # font_size_in = 0.02 # font_color = 'rgb(255, 0, 0)' # textbox_color = "skyblue" Image.MAX_IMAGE_PIXELS = 1000000000 if os.path.exists(os.path.join(BASE_FOLDER, 'board_top.png')): os.remove(os.path.join(BASE_FOLDER, 'board_top.png')) if os.path.exists(os.path.join(BASE_FOLDER, 'board_bottom.png')): os.remove(os.path.join(BASE_FOLDER, 'board_bottom.png')) print('Processing ', BASE_FOLDER) # Open the gerber files copper = load_layer(os.path.join(BASE_FOLDER, 'GerberFiles\copper_top.gbr')) mask = load_layer( os.path.join(BASE_FOLDER, 'GerberFiles\soldermask_top.gbr')) silk = load_layer( os.path.join(BASE_FOLDER, 'GerberFiles\silkscreen_top.gbr')) outline = load_layer(os.path.join(BASE_FOLDER, 'GerberFiles\profile.gbr')) drill = load_layer(os.path.join(BASE_FOLDER, 'DrillFiles\drill_1_16.xln')) # Create a new drawing context ctx = GerberCairoContext(scale=ppi) # Draw the outline first, so it establishes the outer dimensions ctx.render_layer(outline, settings=RenderSettings(color=theme.COLORS['black'], alpha=0.85)) # Draw the copper layer. render_layer() uses the default color scheme for the # layer, based on the layer type. Copper layers are rendered as ctx.render_layer(copper, settings=RenderSettings(color=theme.COLORS['hasl copper'], alpha=0.85)) # Draw the soldermask layer ctx.render_layer(mask, settings=RenderSettings( color=theme.COLORS['green soldermask'], invert=True, alpha=0.8)) # Draw the silkscreen layer, and specify the rendering settings to use ctx.render_layer(silk, settings=RenderSettings(color=theme.COLORS['white'], alpha=0.85)) # Draw the drill layer ctx.render_layer(drill) # Draw the outline again so it's on top of everything ctx.render_layer(outline, settings=RenderSettings(color=theme.COLORS['black'], alpha=0.85)) size_px = ctx.size_in_pixels origin_px = ctx.origin_in_pixels origin_in = ctx.origin_in_inch print(f"size: {ctx.size_in_inch} in / {ctx.size_in_pixels} px ({ppi} PPI)") print(f"origin: {ctx.origin_in_inch} in / {ctx.origin_in_pixels} px") config = configparser.ConfigParser() config['DIMENSIONS'] = { 'SIZE_X_IN': ctx.size_in_inch[0], 'SIZE_Y_IN': ctx.size_in_inch[1], 'SIZE_X_PX': ctx.size_in_pixels[0], 'SIZE_Y_PX': ctx.size_in_pixels[1], 'PPI': ppi, 'ORIGIN_X_IN': ctx.origin_in_inch[0], 'ORIGIN_Y_IN': ctx.origin_in_inch[1] } mtime = 0 for filename in os.listdir(os.path.join(BASE_FOLDER, 'GerberFiles')): mtime = max( mtime, os.path.getmtime(os.path.join(BASE_FOLDER, 'GerberFiles', filename))) print('Latest file modification time: ', mtime) config['OTHER'] = {'GERBERS_LASTMODIFIED': mtime} with open(os.path.join(BASE_FOLDER, 'board.ini'), 'w') as configfile: config.write(configfile) # Write output to png file ctx.dump(os.path.join(BASE_FOLDER, 'board_top.png'), verbose=True) # Load the bottom layers copper = load_layer( os.path.join(BASE_FOLDER, 'GerberFiles\copper_bottom.gbr')) mask = load_layer( os.path.join(BASE_FOLDER, 'GerberFiles\soldermask_bottom.gbr')) silk = load_layer( os.path.join(BASE_FOLDER, 'GerberFiles\silkscreen_bottom.gbr')) # Clear the drawing ctx.clear() # Render bottom layers ctx.render_layer(outline, settings=RenderSettings(color=theme.COLORS['black'], mirror=True, alpha=0.85)) ctx.render_layer(copper, settings=RenderSettings(color=theme.COLORS['hasl copper'], mirror=True, alpha=0.85)) ctx.render_layer(mask, settings=RenderSettings( color=theme.COLORS['green soldermask'], mirror=True, invert=True, alpha=0.8)) ctx.render_layer(silk, settings=RenderSettings(color=theme.COLORS['white'], mirror=True, alpha=0.85)) ctx.render_layer(drill, settings=RenderSettings(mirror=True)) ctx.render_layer(outline, settings=RenderSettings(color=theme.COLORS['black'], mirror=True, alpha=0.85)) # Write png file ctx.dump(os.path.join(BASE_FOLDER, 'board_bottom.png'), verbose=True)
#!/usr/bin/env python from gerber import load_layer from gerber.render import RenderSettings, theme from gerber.render.cairo_backend import GerberCairoContext from PIL import Image, ImageDraw, ImageFilter, ImageOps prefix = 'outputs/pcb' mcolor = (5.0 / 255.0, 30.0 / 255.0, 187 / 255.0) bcolor = (6.0 / 300.0, 13.0 / 300.0, 106 / 300.0) metal_settings = RenderSettings(color=mcolor) bg_settings = RenderSettings(color=bcolor) silk_settings = RenderSettings(color=theme.COLORS['white'], alpha=0.80) hmap_settings = RenderSettings(color=(1.0, 1.0, 1.0)) hmapbg_settings = RenderSettings(color=(0, 0, 0)) mask_settings = RenderSettings(color=(0, 0, 0)) maskbg_settings = RenderSettings(color=(1.0, 1.0, 1.0)) blur_radius = 4 #------------------------------------------------------------- # generate top base image #------------------------------------------------------------- ctx = GerberCairoContext(scale=80) print('### Top Base Image ###') print('loading... ', end='', flush=True) copper = load_layer(prefix + '.GTL') silk = load_layer(prefix + '.GTO') print(' end', flush=True) print('drawing... ', end='', flush=True)
from gerber import load_layer, PCB from gerber.render import RenderSettings, theme from gerber.render.cairo_backend import GerberCairoContext from gerber.render.theme import COLORS GERBER_FOLDER = os.path.abspath( os.path.join(os.path.dirname(__file__), 'not-working')) # Create a new drawing context ctx = GerberCairoContext() pcb = PCB.from_directory(GERBER_FOLDER, verbose=True) # Draw the copper layer. render_layer() uses the default color scheme for the # layer, based on the layer type. Copper layers are rendered as red_theme = theme.Theme(topmask=RenderSettings(COLORS['red soldermask'], alpha=0.8, invert=True), bottommask=RenderSettings(COLORS['red soldermask'], alpha=0.8, invert=True)) #Rendering layers using the red_theme ctx.render_layers(layers=pcb.top_layers, filename=os.path.join( os.path.dirname(__file__), 'pcb_top_test.png', ), theme=red_theme) ctx.render_layers(layers=pcb.bottom_layers, filename=os.path.join(os.path.dirname(__file__), 'pcb_bottom_test.png'),
drill = load_layer(os.path.join(GERBER_FOLDER, 'ncdrill.DRD')) # Create a new drawing context ctx = GerberCairoContext() # Draw the copper layer. render_layer() uses the default color scheme for the # layer, based on the layer type. Copper layers are rendered as ctx.render_layer(copper) # Draw the soldermask layer ctx.render_layer(mask) # The default style can be overridden by passing a RenderSettings instance to # render_layer(). # First, create a settings object: our_settings = RenderSettings(color=theme.COLORS['white'], alpha=0.85) # Draw the silkscreen layer, and specify the rendering settings to use ctx.render_layer(silk, settings=our_settings) # Draw the drill layer ctx.render_layer(drill) # Write output to png file ctx.dump(os.path.join(os.path.dirname(__file__), 'cairo_example.png')) # Load the bottom layers copper = load_layer(os.path.join(GERBER_FOLDER, 'bottom_copper.GBL')) mask = load_layer(os.path.join(GERBER_FOLDER, 'bottom_mask.GBS')) # Clear the drawing
import gerber from gerber.render.cairo_backend import GerberCairoContext from gerber.render import RenderSettings, theme from gerber import load_layer import os BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__),'gerbers')) copper=load_layer(os.path.join(BASE_DIR,'copper.GTL')) mask=load_layer(os.path.join(BASE_DIR,'soldermask.GTS')) silk=load_layer(os.path.join(BASE_DIR,'silkscreen.GTO')) drill=load_layer(os.path.join(BASE_DIR,'ncdrill.DRD')) new=GerberCairoContext() new_settings=RenderSettings(color=theme.COLORS['red'],alpha=0.8) white_text_settings=RenderSettings(color=theme.COLORS['white'],alpha=0.8) new.render_layer(copper,settings=new_settings) new.render_layer(mask,settings=new_settings) new.render_layer(silk,settings=white_text_settings) new.render_layer(drill) new.dump(os.path.join(BASE_DIR,'front.png')) new.clear() copper_bottom=load_layer(os.path.join(BASE_DIR,'bottom_copper.GBL')) mask_bottom=load_layer(os.path.join(BASE_DIR,'bottom_mask.GBS')) new.render_layer(copper_bottom,settings=new_settings) new.render_layer(mask_bottom,settings=new_settings) new.render_layer(drill)
#GERBER_MAIN = os.path.abspath(os.path.join(os.path.dirname(__file__), 'gerbers')) ## Load All Layers #Top View Layers copper = load_layer('gerber_files/copper.GTL') soldermask = load_layer('gerber_files/soldermask.GTS') legend = load_layer('gerber_files/silkscreen.GTO') drill = load_layer('gerber_files/ncdrill.DRD') #Bottom View Layers copper_btm = load_layer('gerber_files/bottom_copper.GBL') soldermask_btm = load_layer('gerber_files/bottom_mask.GBS') ## Color Settings mask_settings = RenderSettings(color=theme.COLORS['red soldermask'], alpha=0.5, invert=True) legend_settings = RenderSettings(color=theme.COLORS['white'], alpha=0.8) ## Rendering #Top View cont = GerberCairoContext() cont.render_layer(copper) cont.render_layer(soldermask, settings=mask_settings) cont.render_layer(legend, settings=legend_settings) cont.render_layer(drill) cont.dump('gen_images/redpcb.png') #Clear cont cont.clear()
mask.ctx.arc_negative(center[0], center[1], radius, angle1, angle2) else: mask.ctx.line_to(ll[0], ll[1]) mask.ctx.set_source_rgba(1.0, 1.0, 1.0, 1.0) mask.ctx.fill() self.ctx.mask_surface(mask.surface, 0.0, 0.0) GerberCairoContext.flatten(self, color, alpha) ################################################################### # read the gerbers pcb = PCB.from_directory(gerberPath, None, verbose=True) outlineTheme = theme.Theme(name='OutlineTheme', background=RenderSettings(theme.COLORS['black'], alpha=0.0), drill=RenderSettings(theme.COLORS['black'], alpha=1.0)) outlineTheme.outline = RenderSettings(theme.COLORS['white'], alpha=1.0) jlcTheme = theme.Theme(name='JLC', background=RenderSettings(theme.COLORS['fr-4']), top=RenderSettings(theme.COLORS['hasl copper'], alpha=0.85), bottom=RenderSettings(theme.COLORS['hasl copper'], alpha=0.85, mirror=True), topmask=RenderSettings(theme.COLORS['green soldermask'], alpha=0.85, invert=True), bottommask=RenderSettings(theme.COLORS['green soldermask'], alpha=0.85, invert=True, mirror=True), topsilk=RenderSettings(theme.COLORS['white'], alpha=1.0), bottomsilk=RenderSettings(theme.COLORS['white'], alpha=1.0, mirror=True), drill=RenderSettings(theme.COLORS['black'], alpha=1.0)) curTheme = jlcTheme