diff options
-rw-r--r-- | README.md | 120 | ||||
-rw-r--r-- | gerber/render/cairo_backend.py | 6 | ||||
-rw-r--r-- | gerber/tests/test_cairo_backend.py | 40 |
3 files changed, 102 insertions, 64 deletions
@@ -1,60 +1,60 @@ -pcb-tools
-============
-[![Travis CI Build Status](https://travis-ci.org/curtacircuitos/pcb-tools.svg?branch=master)](https://travis-ci.org/curtacircuitos/pcb-tools)
-[![Coverage Status](https://coveralls.io/repos/curtacircuitos/pcb-tools/badge.png?branch=master)](https://coveralls.io/r/curtacircuitos/pcb-tools?branch=master)
-[![Documentation Status](https://readthedocs.org/projects/pcb-tools/badge/?version=latest)](https://readthedocs.org/projects/pcb-tools/?badge=latest)
-
-Tools to handle Gerber and Excellon files in Python.
-
-Usage Example:
----------------
- import gerber
- from gerber.render import GerberCairoContext
-
- # Read gerber and Excellon files
- top_copper = gerber.read('example.GTL')
- nc_drill = gerber.read('example.txt')
-
- # Rendering context
- ctx = GerberCairoContext()
-
- # Create SVG image
- top_copper.render(ctx)
- nc_drill.render(ctx, 'composite.svg')
-
-
-Rendering Examples:
--------------------
-###Top Composite rendering
-![Composite Top Image](examples/cairo_example.png)
-![Composite Bottom Image](examples/cairo_bottom.png)
-
-Source code for this example can be found [here](examples/cairo_example.py).
-
-
-Install from source:
-```
-$ git clone https://github.com/curtacircuitos/pcb-tools.git
-$ cd pcb-tools
-$ pip install -r requirements.txt
-$ python setup.py install
-```
-
-Documentation:
---------------
-[PCB Tools Documentation](http://pcb-tools.readthedocs.org/en/latest/)
-
-
-Development and Testing:
-------------------------
-
-Dependencies for developing and testing pcb-tools are listed in test-requirements.txt. Use of a virtual environment is strongly recommended.
-
- $ virtualenv venv
- $ source venv/bin/activate
- (venv)$ pip install -r test-requirements.txt
- (venv)$ pip install -e .
-
-We use nose to run pcb-tools's suite of unittests and doctests.
-
- (venv)$ nosetests
+pcb-tools +============ +[![Travis CI Build Status](https://travis-ci.org/curtacircuitos/pcb-tools.svg?branch=master)](https://travis-ci.org/curtacircuitos/pcb-tools) +[![Coverage Status](https://coveralls.io/repos/curtacircuitos/pcb-tools/badge.png?branch=master)](https://coveralls.io/r/curtacircuitos/pcb-tools?branch=master) +[![Documentation Status](https://readthedocs.org/projects/pcb-tools/badge/?version=latest)](https://readthedocs.org/projects/pcb-tools/?badge=latest) + +Tools to handle Gerber and Excellon files in Python. + +Usage Example: +--------------- + import gerber + from gerber.render import GerberCairoContext + + # Read gerber and Excellon files + top_copper = gerber.read('example.GTL') + nc_drill = gerber.read('example.txt') + + # Rendering context + ctx = GerberCairoContext() + + # Create SVG image + top_copper.render(ctx) + nc_drill.render(ctx, 'composite.svg') + + +Rendering Examples: +------------------- +### Top Composite rendering +![Composite Top Image](examples/cairo_example.png) +![Composite Bottom Image](examples/cairo_bottom.png) + +Source code for this example can be found [here](examples/cairo_example.py). + + +Install from source: +``` +$ git clone https://github.com/curtacircuitos/pcb-tools.git +$ cd pcb-tools +$ pip install -r requirements.txt +$ python setup.py install +``` + +Documentation: +-------------- +[PCB Tools Documentation](http://pcb-tools.readthedocs.org/en/latest/) + + +Development and Testing: +------------------------ + +Dependencies for developing and testing pcb-tools are listed in test-requirements.txt. Use of a virtual environment is strongly recommended. + + $ virtualenv venv + $ source venv/bin/activate + (venv)$ pip install -r test-requirements.txt + (venv)$ pip install -e . + +We use nose to run pcb-tools's suite of unittests and doctests. + + (venv)$ nosetests diff --git a/gerber/render/cairo_backend.py b/gerber/render/cairo_backend.py index 76be60a..3571ed9 100644 --- a/gerber/render/cairo_backend.py +++ b/gerber/render/cairo_backend.py @@ -139,7 +139,7 @@ class GerberCairoContext(GerberContext): if is_svg: self.surface.finish() self.surface_buffer.flush() - with open(filename, "w") as f: + with open(filename, "wb") as f: self.surface_buffer.seek(0) f.write(self.surface_buffer.read()) f.flush() @@ -565,7 +565,7 @@ class GerberCairoContext(GerberContext): def _flatten(self, color=None, alpha=None): color = color if color is not None else self.color alpha = alpha if alpha is not None else self.alpha - self.output_ctx.set_source_rgba(*color, alpha=alpha) + self.output_ctx.set_source_rgba(color[0], color[1], color[2], alpha) self.output_ctx.mask_surface(self.active_layer) self.ctx = None self.active_layer = None @@ -576,7 +576,7 @@ class GerberCairoContext(GerberContext): alpha = settings.alpha if settings is not None else 1.0 if not self.has_bg: self.has_bg = True - self.output_ctx.set_source_rgba(*color, alpha=alpha) + self.output_ctx.set_source_rgba(color[0], color[1], color[2], alpha) self.output_ctx.paint() def _clip_primitive(self, primitive): diff --git a/gerber/tests/test_cairo_backend.py b/gerber/tests/test_cairo_backend.py index 9195b93..d5ce4ed 100644 --- a/gerber/tests/test_cairo_backend.py +++ b/gerber/tests/test_cairo_backend.py @@ -3,7 +3,8 @@ # Author: Garret Fick <garret@ficksworkshop.com> import os - +import shutil +import tempfile from ..render.cairo_backend import GerberCairoContext from ..rs274x import read @@ -136,6 +137,11 @@ def _DISABLED_test_render_am_exposure_modifier(): _test_render('resources/example_am_exposure_modifier.gbr', 'golden/example_am_exposure_modifier.png') +def test_render_svg_simple_contour(): + """Example of rendering to an SVG file""" + _test_simple_render_svg('resources/example_simple_contour.gbr') + + def _resolve_path(path): return os.path.join(os.path.dirname(__file__), path) @@ -187,3 +193,35 @@ def _test_render(gerber_path, png_expected_path, create_output_path = None): assert_true(equal) return gerber + + +def _test_simple_render_svg(gerber_path): + """Render the gerber file as SVG + + Note: verifies only the header, not the full content. + + Parameters + ---------- + gerber_path : string + Path to Gerber file to open + """ + + gerber_path = _resolve_path(gerber_path) + gerber = read(gerber_path) + + # Create SVG image to the memory stream + ctx = GerberCairoContext() + gerber.render(ctx) + + temp_dir = tempfile.mkdtemp() + svg_temp_path = os.path.join(temp_dir, 'output.svg') + + assert_false(os.path.exists(svg_temp_path)) + ctx.dump(svg_temp_path) + assert_true(os.path.exists(svg_temp_path)) + + with open(svg_temp_path, 'r') as expected_file: + expected_bytes = expected_file.read() + assert_equal(expected_bytes[:38], '<?xml version="1.0" encoding="UTF-8"?>') + + shutil.rmtree(temp_dir) |