From 1653ae5cbe88757e453bccf499dc1b8ccb278e58 Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Wed, 8 Oct 2014 09:27:52 -0400 Subject: Update readme and example --- README.md | 8 +++----- gerber/__init__.py | 24 ---------------------- gerber/__main__.py | 28 +++++++++++++------------- gerber/common.py | 42 +++++++++++++++++++++++++++++++++++++++ gerber/excellon.py | 16 ++++----------- gerber/gerber.py | 5 +++-- gerber/render/svgwrite_backend.py | 6 ++++-- 7 files changed, 70 insertions(+), 59 deletions(-) create mode 100644 gerber/common.py diff --git a/README.md b/README.md index ef8fe50..1821c0a 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,8 @@ Example: nc_drill = gerber.read('example.txt') # Rendering context - ctx = GerberSvgContext + ctx = GerberSvgContext() # Create SVG image - top_copper.render('top_copper.svg', ctx) - nc_drill.render('composite.svg', ctx) - - + top_copper.render(ctx) + nc_drill.render(ctx, 'composite.svg') diff --git a/gerber/__init__.py b/gerber/__init__.py index 3197335..4637713 100644 --- a/gerber/__init__.py +++ b/gerber/__init__.py @@ -21,27 +21,3 @@ gerber module """ -def read(filename): - """ Read a gerber or excellon file and return a representative object. - - Parameters - ---------- - filename : string - Filename of the file to read. - - Returns - ------- - file : CncFile subclass - CncFile object representing the file, either GerberFile or - ExcellonFile. Returns None if file is not an Excellon or Gerber file. - """ - import gerber - import excellon - from utils import detect_file_format - fmt = detect_file_format(filename) - if fmt == 'rs274x': - return gerber.read(filename) - elif fmt == 'excellon': - return excellon.read(filename) - else: - return None diff --git a/gerber/__main__.py b/gerber/__main__.py index 26f36e1..ab0f377 100644 --- a/gerber/__main__.py +++ b/gerber/__main__.py @@ -16,20 +16,20 @@ # the License. if __name__ == '__main__': - import gerber - import excellon + from .common import read from .render import GerberSvgContext + import sys + + if len(sys.argv) < 2: + print >> sys.stderr, "Usage: python -m gerber ..." + sys.exit(1) - #import sys - # - #if len(sys.argv) < 2:` - # print >> sys.stderr, "Usage: python -m gerber ..." - # sys.exit(1) - # - ##for filename in sys.argv[1]: - ## print "parsing %s" % filename ctx = GerberSvgContext() - g = gerber.read('examples/test.gtl') - g.render('test.svg', ctx) - p = excellon.read('ncdrill.txt') - p.render('testwithdrill.svg', ctx) + + for filename in sys.argv[1:]: + print "parsing %s" % filename + gerberfile = read(filename) + gerberfile.render(ctx) + print('Saving image to test.svg') + ctx.dump('test.svg') + diff --git a/gerber/common.py b/gerber/common.py new file mode 100644 index 0000000..0092ec8 --- /dev/null +++ b/gerber/common.py @@ -0,0 +1,42 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +# Copyright 2014 Hamilton Kibbe + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def read(filename): + """ Read a gerber or excellon file and return a representative object. + + Parameters + ---------- + filename : string + Filename of the file to read. + + Returns + ------- + file : CncFile subclass + CncFile object representing the file, either GerberFile or + ExcellonFile. Returns None if file is not an Excellon or Gerber file. + """ + import gerber + import excellon + from utils import detect_file_format + fmt = detect_file_format(filename) + if fmt == 'rs274x': + return gerber.read(filename) + elif fmt == 'excellon': + return excellon.read(filename) + else: + return None diff --git a/gerber/excellon.py b/gerber/excellon.py index 66b9ea2..663f791 100755 --- a/gerber/excellon.py +++ b/gerber/excellon.py @@ -67,15 +67,13 @@ class ExcellonFile(CncFile): """ pass - def render(self, filename, ctx): + def render(self, ctx, filename=None): """ Generate image of file """ - count = 0 for tool, pos in self.hits: ctx.drill(pos[0], pos[1], tool.diameter) - count += 1 - print('Drilled %d hits' % count) - ctx.dump(filename) + if filename is not None: + ctx.dump(filename) def write(self, filename): with open(filename, 'w') as f: @@ -86,8 +84,7 @@ class ExcellonFile(CncFile): class ExcellonParser(object): """ Excellon File Parser """ - def __init__(self, ctx=None): - self.ctx = ctx + def __init__(self): self.notation = 'absolute' self.units = 'inch' self.zero_suppression = 'trailing' @@ -197,8 +194,3 @@ class ExcellonParser(object): return FileSettings(units=self.units, format=self.format, zero_suppression=self.zero_suppression, notation=self.notation) - - -if __name__ == '__main__': - p = ExcellonParser() - parsed = p.parse('examples/ncdrill.txt') diff --git a/gerber/gerber.py b/gerber/gerber.py index 220405c..04203fa 100644 --- a/gerber/gerber.py +++ b/gerber/gerber.py @@ -111,13 +111,14 @@ class GerberFile(CncFile): for statement in self.statements: f.write(statement.to_gerber()) - def render(self, filename, ctx): + def render(self, ctx, filename=None): """ Generate image of layer. """ ctx.set_bounds(self.bounds) for statement in self.statements: ctx.evaluate(statement) - ctx.dump(filename) + if filename is not None: + ctx.dump(filename) class GerberParser(object): diff --git a/gerber/render/svgwrite_backend.py b/gerber/render/svgwrite_backend.py index 7d5c8fd..8d84da1 100644 --- a/gerber/render/svgwrite_backend.py +++ b/gerber/render/svgwrite_backend.py @@ -110,12 +110,14 @@ class GerberSvgContext(GerberContext): self.apertures = {} self.dwg = svgwrite.Drawing() - #self.dwg.add(self.dwg.rect(insert=(0, 0), size=(2000, 2000), fill="black")) + self.background = False def set_bounds(self, bounds): xbounds, ybounds = bounds size = (SCALE * (xbounds[1] - xbounds[0]), SCALE * (ybounds[1] - ybounds[0])) - self.dwg.add(self.dwg.rect(insert=(SCALE * xbounds[0], -SCALE * ybounds[1]), size=size, fill="black")) + if not self.background: + self.dwg.add(self.dwg.rect(insert=(SCALE * xbounds[0], -SCALE * ybounds[1]), size=size, fill="black")) + self.background = True def define_aperture(self, d, shape, modifiers): aperture = None -- cgit