def check_dir(d): sch_ls = dir_files(norm_path(d), "*.sch") brd_ls = dir_files(norm_path(d), "*.brd") sch_ls = list(sch_ls) brd_ls = list(brd_ls) all = sch_ls + brd_ls for x in all: export(x)
def export_partlist_to_file(input, output, timeout=20, showgui=False): """ call eagle and export sch or brd to partlist text file :param input: .sch or .brd file name :param output: text file name :param timeout: int :param showgui: Bool, True -> do not hide eagle GUI :rtype: None """ input = norm_path(input) output = norm_path(output) commands = export_command(output=output, output_type="partlist") command_eagle(input=input, timeout=timeout, commands=commands, showgui=showgui)
def export_partlist_to_file(input, output, timeout=20, showgui=False): ''' call eagle and export sch or brd to partlist text file :param input: .sch or .brd file name :param output: text file name :param timeout: int :param showgui: Bool, True -> do not hide eagle GUI :rtype: None ''' input = norm_path(input) output = norm_path(output) commands = export_command(output=output, output_type='partlist') command_eagle( input=input, timeout=timeout, commands=commands, showgui=showgui)
def airwires(board, showgui=0): "search for airwires in eagle board" with tempfile.TemporaryDirectory() as temp_dir: board = norm_path(board) file_out = join(temp_dir, "out.txt") ulp = ulp_templ.replace("FILE_NAME", file_out) file_ulp = join(temp_dir, "out.ulp") write_text(file_ulp, ulp) commands = [ "run " + file_ulp, "quit", ] command_eagle(board, commands=commands, showgui=showgui) n = int(read_text(file_out)) return n
def export_image3d(input, output, size=(800, 600), pcb_rotate=(0, 0, 0), timeout=20, showgui=False): ''' Exporting eagle .brd file into 3D image file using Eagle3D and povray. GUI is not displayed if ``pyvirtualdisplay`` is installed. If export is blocked somehow (e.g. popup window is displayed) then after timeout operation is canceled with exception. Problem can be investigated by setting 'showgui' flag. :param input: eagle .brd file name :param output: image file name (.png) :param timeout: operation is canceled after this timeout (sec) :param showgui: eagle GUI is displayed :param size: tuple(width, size), image size :rtype: None ''' input = norm_path(input) output = norm_path(output) ext = os.path.splitext(input)[1] if ext not in ['.brd']: raise ValueError('Input extension is not ".brd", brd=' + str(input)) commands = [] eagle3d = Path(__file__).dirname() / 'eagle3d' ulp = (eagle3d / '3d50.ulp').abspath() commands += ['RUN ' + ulp] commands += ['QUIT'] def render(dir, f): # povray has strange file access policy, # better to generate under tmp # cli doc: # http://library.thinkquest.org/3285/language/cmdln.html templ = '#local pcb_rotate_%s = %s' pov = Path(f.replace('.brd', '.pov')) if pcb_rotate != (0, 0, 0): s = pov.text() s = s.replace(templ % ('x', 0), templ % ('x', pcb_rotate[0])) s = s.replace(templ % ('y', 0), templ % ('y', pcb_rotate[1])) s = s.replace(templ % ('z', 0), templ % ('z', pcb_rotate[2])) pov.write_text(s) fpng = Path(f.replace('.brd', '.png')) cmd = [] cmd += ["povray"] cmd += ["-d"] # no display cmd += ["-a"] # anti-aliasing cmd += ['+W' + str(size[0])] # width cmd += ['+H' + str(size[1])] # height cmd += ['-o' + fpng] cmd += ['-L' + eagle3d] cmd += [pov] p = Proc(cmd).call() if not fpng.exists(): raise EagleError('povray error, proc=%s' % p) fpng.copy(output) command_eagle(input=input, timeout=timeout, commands=commands, showgui=showgui, callback=render)
def command_eagle(input, commands=[], timeout=TIMEOUT, showgui=False, callback=None): input = norm_path(input) if not commands: commands = [] # with dot ext = os.path.splitext(input)[1] if ext not in ['.brd', '.sch']: raise ValueError( 'Input extension is not in [.brd, .sch], input=' + str(input)) # this method does not work: # both sch and brd windows can be opened, # so we should activate the correct one # script += 'EDIT {ext};'.format(ext=ext) # copy input into empty directory, otherwise both sch and brd will be # opened tmp_dir = tempfile.mkdtemp(prefix='eagexp') tmp_input = os.path.join(tmp_dir, os.path.split(input)[1]) shutil.copy(input, tmp_input) script = '' for x in commands: script += x script += ';' # this method is not used currently # write script into file # fscript = tempfile.NamedTemporaryFile(suffix='.scr', delete=0) # fscript.write(script) # cmd = ['eagle', '-C SCRIPT '+ fscript.name+'', input] cmd = ['eagle', '-C ' + script, tmp_input] def call_eagle(): t = 0 accept_tries = 0 with EasyProcess(cmd) as p: while p.is_alive(): time.sleep(0.5) t += 0.5 if t > timeout / 2: if accept_tries == 0: accept_freeware_license() accept_tries += 1 if t > timeout: # pyscreenshot.grab_to_file('/vagrant/xxx.png') raise EagleError('eagle return code is not zero, proc=' + str(p)) # break # p = Proc(cmd).call(timeout=timeout) # if p.return_code != 0: # raise EagleError('eagle return code is not zero, proc=' + str(p)) curdir = Path.getcwd() curdir = norm_path(curdir) os.chdir(tmp_dir) if USE_DISPLAY: with Display(visible=showgui, size=(800, 600)): time.sleep(1) call_eagle() else: call_eagle() os.chdir(curdir) if callback: callback(tmp_dir, tmp_input) shutil.rmtree(tmp_dir)
def export_image3d( input, output, size=(800, 600), pcb_rotate=(0, 0, 0), timeout=20, showgui=False ): """ Exporting eagle .brd file into 3D image file using Eagle3D and povray. If export is blocked somehow (e.g. popup window is displayed) then after timeout operation is canceled with exception. Problem can be investigated by setting 'showgui' flag. :param input: eagle .brd file name :param output: image file name (.png) :param timeout: operation is canceled after this timeout (sec) :param showgui: eagle GUI is displayed :param size: tuple(width, size), image size :rtype: None """ input = norm_path(input) output = norm_path(output) if not output.endswith(".png"): raise ValueError("use .png extension!") ext = os.path.splitext(input)[1] if ext not in [".brd"]: raise ValueError('Input extension is not ".brd", brd=' + str(input)) commands = [] eagle3d = join(dirname(__file__), "eagle3d") ulp = norm_path(join(eagle3d, "3d50.ulp")) if not exists(ulp): raise EagleError("missing file:%s" % ulp) commands += ["RUN " + ulp] commands += ["QUIT"] def render(dir, f): # povray has strange file access policy, # better to generate under tmp # cli doc: # http://library.thinkquest.org/3285/language/cmdln.html templ = "#local pcb_rotate_%s = %s" pov = f.replace(".brd", ".pov") if not exists(pov): raise EagleError("missing pov file: %s" % pov) # log.debug("pov file %s content: %s", pov, pov.read_text()) if pcb_rotate != (0, 0, 0): s = read_text(pov) s = s.replace(templ % ("x", 0), templ % ("x", pcb_rotate[0])) s = s.replace(templ % ("y", 0), templ % ("y", pcb_rotate[1])) s = s.replace(templ % ("z", 0), templ % ("z", pcb_rotate[2])) write_text(pov, s) fpng = f.replace(".brd", ".png") cmd = [] cmd += ["povray"] cmd += ["-d"] # no display cmd += ["-a"] # anti-aliasing cmd += ["+W" + str(size[0])] # width cmd += ["+H" + str(size[1])] # height cmd += ["-o" + fpng] cmd += ["-L" + eagle3d] cmd += [pov] p = EasyProcess(cmd).call() if not exists(fpng): raise EagleError("povray error, proc=%s" % p) copy(fpng, output) command_eagle( input=input, timeout=timeout, commands=commands, showgui=showgui, callback=render, )
def command_eagle(input, commands=[], timeout=TIMEOUT, showgui=False, callback=None): input = norm_path(input) if not commands: commands = [] # with dot ext = os.path.splitext(input)[1] if ext not in [".brd", ".sch"]: raise ValueError("Input extension is not in [.brd, .sch], input=" + str(input)) # this method does not work: # both sch and brd windows can be opened, # so we should activate the correct one # script += 'EDIT {ext};'.format(ext=ext) # copy input into empty directory, otherwise both sch and brd will be # opened tmp_dir = tempfile.mkdtemp(prefix="eagexp") tmp_input = os.path.join(tmp_dir, os.path.split(input)[1]) shutil.copy(input, tmp_input) script = "" for x in commands: script += x script += ";" # this method is not used currently # write script into file # fscript = tempfile.NamedTemporaryFile(suffix='.scr', delete=0) # fscript.write(script) # cmd = ['eagle', '-C SCRIPT '+ fscript.name+'', input] cmd = ["eagle", "-C " + script, tmp_input] def call_eagle(): t = 0 accept_tries = 0 with EasyProcess(cmd) as p: while p.is_alive(): time.sleep(0.5) t += 0.5 if t > timeout / 2: if accept_tries == 0: accept_freeware_license() accept_tries += 1 if t > timeout: p.stop() raise EagleError("eagle timeout:" + str(p)) curdir = os.getcwd() curdir = norm_path(curdir) os.chdir(tmp_dir) with Display(visible=showgui, size=(800, 600)): # time.sleep(1) call_eagle() os.chdir(curdir) if callback: callback(tmp_dir, tmp_input) shutil.rmtree(tmp_dir)
def export_image( input, output, timeout=20, palette="white", resolution=150, layers=None, command=None, mirror=False, showgui=False, ): """ Exporting eagle .sch or .brd file into image file. If export is blocked somehow (e.g. popup window is displayed) then after timeout operation is canceled with exception. Problem can be investigated by setting 'showgui' flag. Exporting generates an image file. Only PNG format is supported :param input: eagle .sch or .brd file name :param output: image file name (e.g. 'eagle.png') :param palette: background color [None,black,white,colored] :param resolution: image resolution in dpi (50..2400) :param timeout: operation is canceled after this timeout (sec) :param showgui: eagle GUI is displayed :param layers: list, layers to be displayed ['top','pads'] :param command: string, direct eagle command :param mirror: Bool :rtype: None """ input = norm_path(input) output = norm_path(output) if not output.endswith(".png"): raise ValueError("use .png extension!") if palette: palette = palette.lower() if palette == "none": palette = None cmds = [] if palette is not None: cmds += ["SET PALETTE {palette}".format(palette=palette)] if layers is not None: cmds += ["DISPLAY NONE " + " ".join(layers)] if command is not None: cmds += [command] with tempfile.TemporaryDirectory() as temp_dir: if mirror: fout = join(temp_dir, "out.png") else: fout = output commands = export_command(output=fout, output_type="image", commands=cmds, resolution=resolution) command_eagle(input=input, timeout=timeout, commands=commands, showgui=showgui) if mirror: im = Image.open(fout) # save dpi info info = im.info im = ImageOps.mirror(im) im.save(output, **info)
def export_image(input, output, timeout=20, palette='white', resolution=150, layers=None, command=None, mirror=False, showgui=False): ''' Exporting eagle .sch or .brd file into image file. GUI is not displayed if ``pyvirtualdisplay`` is installed. If export is blocked somehow (e.g. popup window is displayed) then after timeout operation is canceled with exception. Problem can be investigated by setting 'showgui' flag. Exporting generates an image file with a format corresponding to the given filename extension. The following image formats are available: .bmp Windows Bitmap Files .png Portable Network Graphics Files .pbm Portable Bitmap Files .pgm Portable Grayscale Bitmap Files .ppm Portable Pixelmap Files .tif TIFF Files .xbm X Bitmap Files .xpm X Pixmap Files :param input: eagle .sch or .brd file name :param output: image file name, existing file will be removed first! :param palette: background color [None,black,white,colored] :param resolution: image resolution in dpi (50..2400) :param timeout: operation is canceled after this timeout (sec) :param showgui: eagle GUI is displayed :param layers: list, layers to be displayed ['top','pads'] :param command: string, direct eagle command :param mirror: Bool :rtype: None ''' input = norm_path(input) output = norm_path(output) if palette: palette = palette.lower() if palette == 'none': palette = None cmds = [] if palette is not None: cmds += ['SET PALETTE {palette}'.format(palette=palette)] if layers is not None: cmds += ['DISPLAY NONE ' + ' '.join(layers)] if command is not None: cmds += [command] if mirror: f = tempfile.NamedTemporaryFile(suffix='.png', prefix='eagexp_') fout = f.name else: fout = output commands = export_command(output=fout, output_type='image', commands=cmds, resolution=resolution) command_eagle( input=input, timeout=timeout, commands=commands, showgui=showgui) if mirror: im = Image.open(fout) # save dpi info info = im.info im = ImageOps.mirror(im) im.save(output, **info)