Update readme and example

This commit is contained in:
Hamilton Kibbe 2014-10-08 09:27:52 -04:00
parent 100ab899ed
commit 1653ae5cbe
7 changed files with 70 additions and 59 deletions

View file

@ -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')

View file

@ -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

View file

@ -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 <filename> <filename>..."
sys.exit(1)
#import sys
#
#if len(sys.argv) < 2:`
# print >> sys.stderr, "Usage: python -m gerber <filename> <filename>..."
# 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')

42
gerber/common.py Normal file
View file

@ -0,0 +1,42 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2014 Hamilton Kibbe <ham@hamiltonkib.be>
# 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

View file

@ -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')

View file

@ -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):

View file

@ -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