From 9cc42d9b7752603ae409e7c95a8cb5bec4d7b5b2 Mon Sep 17 00:00:00 2001 From: Chintalagiri Shashank Date: Sat, 11 May 2019 04:23:19 +0530 Subject: Make __main__ functional again and install a script entry point. --- gerber/__main__.py | 126 +++++++++++++++++++++++++++++++++++++--------- gerber/render/__init__.py | 5 ++ setup.py | 13 +++-- 3 files changed, 118 insertions(+), 26 deletions(-) diff --git a/gerber/__main__.py b/gerber/__main__.py index 6643b54..202c00b 100644 --- a/gerber/__main__.py +++ b/gerber/__main__.py @@ -15,27 +15,107 @@ # License for the specific language governing permissions and limitations under # the License. +import os +import argparse +from .render import available_renderers +from .render import theme +from .pcb import PCB +from . import load_layer + + +def main(): + parser = argparse.ArgumentParser( + description='Render gerber files to image', + prog='gerber-render' + ) + parser.add_argument( + 'filenames', metavar='FILENAME', type=str, nargs='+', + help='Gerber files to render. If a directory is provided, it should ' + 'be provided alone and should contain the gerber files for a ' + 'single PCB.' + ) + parser.add_argument( + '--outfile', '-o', type=str, nargs='?', default='out', + help="Output Filename (extension will be added automatically)" + ) + parser.add_argument( + '--backend', '-b', choices=available_renderers.keys(), default='cairo', + help='Choose the backend to use to generate the output.' + ) + parser.add_argument( + '--theme', '-t', choices=theme.THEMES.keys(), default='default', + help='Select render theme.' + ) + parser.add_argument( + '--width', type=int, default=1920, help='Maximum width.' + ) + parser.add_argument( + '--height', type=int, default=1080, help='Maximum height.' + ) + parser.add_argument( + '--verbose', '-v', action='store_true', default=False, + help='Increase verbosity of the output.' + ) + # parser.add_argument( + # '--quick', '-q', action='store_true', default=False, + # help='Skip longer running rendering steps to produce lower quality' + # ' output faster. This only has an effect for the freecad backend.' + # ) + # parser.add_argument( + # '--nox', action='store_true', default=False, + # help='Run without using any GUI elements. This may produce suboptimal' + # 'output. For the freecad backend, colors, transparancy, and ' + # 'visibility cannot be set without a GUI instance.' + # ) + + args = parser.parse_args() + + renderer = available_renderers[args.backend]() + + if args.backend in ['cairo', ]: + outext = 'png' + else: + outext = None + + if os.path.exists(args.filenames[0]) and os.path.isdir(args.filenames[0]): + directory = args.filenames[0] + pcb = PCB.from_directory(directory) + + if args.backend in ['cairo', ]: + top = pcb.top_layers + bottom = pcb.bottom_layers + copper = pcb.copper_layers + + outline = pcb.outline_layer + if outline: + top = [outline] + top + bottom = [outline] + bottom + copper = [outline] + copper + pcb.drill_layers + + renderer.render_layers( + layers=top, theme=theme.THEMES[args.theme], + max_height=args.height, max_width=args.width, + filename='{0}.top.{1}'.format(args.outfile, outext) + ) + renderer.render_layers( + layers=bottom, theme=theme.THEMES[args.theme], + max_height=args.height, max_width=args.width, + filename='{0}.bottom.{1}'.format(args.outfile, outext) + ) + renderer.render_layers( + layers=copper, theme=theme.THEMES['Transparent Multilayer'], + max_height=args.height, max_width=args.width, + filename='{0}.copper.{1}'.format(args.outfile, outext)) + else: + pass + else: + filenames = args.filenames + for filename in filenames: + layer = load_layer(filename) + settings = theme.THEMES[args.theme].get(layer.layer_class, None) + renderer.render_layer(layer, settings=settings) + renderer.dump(filename='{0}.{1}'.format(args.outfile, outext)) + + if __name__ == '__main__': - from gerber.common import read - from gerber.render import GerberCairoContext - import sys - - if len(sys.argv) < 2: - sys.stderr.write("Usage: python -m gerber ...\n") - sys.exit(1) - - ctx = GerberCairoContext() - ctx.alpha = 0.95 - for filename in sys.argv[1:]: - print("parsing %s" % filename) - if 'GTO' in filename or 'GBO' in filename: - ctx.color = (1, 1, 1) - ctx.alpha = 0.8 - elif 'GTS' in filename or 'GBS' in filename: - ctx.color = (0.2, 0.2, 0.75) - ctx.alpha = 0.8 - gerberfile = read(filename) - gerberfile.render(ctx) - - print('Saving image to test.svg') - ctx.dump('test.svg') + main() diff --git a/gerber/render/__init__.py b/gerber/render/__init__.py index fe08d50..c7dbdd5 100644 --- a/gerber/render/__init__.py +++ b/gerber/render/__init__.py @@ -24,3 +24,8 @@ SVG is the only supported format. """ from .render import RenderSettings +from .cairo_backend import GerberCairoContext + +available_renderers = { + 'cairo': GerberCairoContext, +} diff --git a/setup.py b/setup.py index 1df0868..0e43155 100644 --- a/setup.py +++ b/setup.py @@ -16,6 +16,7 @@ # limitations under the License. import os + def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() @@ -25,13 +26,13 @@ METADATA = { 'version': 0.1, 'author': 'Paulo Henrique Silva , Hamilton Kibbe ', 'author_email': "ph.silva@gmail.com, ham@hamiltonkib.be", - 'description': ("Utilities to handle Gerber (RS-274X) files."), + 'description': "Utilities to handle Gerber (RS-274X) files.", 'license': "Apache", 'keywords': "pcb gerber tools", 'url': "http://github.com/curtacircuitos/pcb-tools", 'packages': ['gerber', 'gerber.render'], 'long_description': read('README.md'), - 'classifiers':[ + 'classifiers': [ "Development Status :: 3 - Alpha", "Topic :: Utilities", "License :: OSI Approved :: Apple Public Source License", @@ -40,6 +41,11 @@ METADATA = { SETUPTOOLS_METADATA = { 'install_requires': ['cairocffi==0.6'], + 'entry_points': { + 'console_scripts': [ + 'gerber-render = gerber.__main__:main', + ], + }, } @@ -53,9 +59,10 @@ def install(): except ImportError: from sys import stderr stderr.write('Could not import setuptools, using distutils') - stderr.write('NOTE: You will need to install dependencies manualy') + stderr.write('NOTE: You will need to install dependencies manually') from distutils.core import setup setup(**METADATA) + if __name__ == '__main__': install() -- cgit