Giter Club home page Giter Club logo

pcb-tools-extension's Introduction

pcb-tools-extension

pcb-tools-extension is a Python library to panelize gerber files. This library is designed based on pcb-tools which provides cool functionality to handle PCB such as generationg PCB image from gerber files.

pcb-tools-extension adds following function to pcb-tools.

  • Rotate PCB data
  • Write back loaded PCB data (original pcb-tools does not work in some condition)
  • Merge multiple PCB data
  • Translate DXF file to PCB data

Only RS-274x format and Excellon drill format data can be handled by current version of this library.

Installation

You can install a stable version by following step.

$ pip install pcb-tools-extension

If you have a intention to try latest developing version, please install as follows.

$ pip install git+https://github.com/opiopan/pcb-tools-extension.git

How to panelize

Following code is a example to panelize two top metal layer files.

import gerberex

ctx = gerberex.GerberComposition()

metal1 = gerberex.read('board1.gtl')
ctx.merge(metal1)

metal2 = gerberex.read('board2.gtl')
metal2.to_metric()
metal2.rotate(-20)
metal2.offset(30, 0)
ctx.merge(metal2)

ctx.dump('panelized-board.gtl')

rotate() method can be used to rotate PCB data counterclockwise. you have to specify angle in degree.
offset() method can be used to move PCB data. Specified offset values are interpreted according to unit setting of PCB data. In case of the above code, board2.gtl move to 30mm left since to_metric() is called.

In case of Excellon drill data, you have to use DrillCompositon instead of GerberComposition.

import gerberex

ctx = gerberex.DrillComposition()

drill1 = gerberex.read('board1.txt')
ctx.merge(drill1)

drill2 = gerberex.read('board2.txt')
drill2.to_metric()
drill2.rotate(-20)
drill2.offset(30, 0)
ctx.merge(drill2)

ctx.dump('panelized-board.txt')

DXF file translation

pcb-tools-extension hsa a function to load a DXF file and handle that as same as RX-274x gerber file or Excellon NC file.
In this version, Only line, circle, arc, and polyline objects are recognized and are translated to gerber file or NC file.

Two way to tranlate DXF file

Both composition objects, GerberComposition for RX-274x and DrillionComposition for Excellon, can accept an object created as result of DXF file loaded. When composition object dump text stream, DXF data tranclate to appropriate format data.
The object which represent DXF file, can also output translated data directly by save method. In this case output format is specified by filetype argument. If filetype argument is ommited, DXF data is translated to RX-274x gerber data.

import gerberex
dxf = gerberex.read('sample.dxf')

# translate to RX-274x using composition object
ctx = gerberex.GerberComposition()
ctx.merge(dxf)
ctx.dump('sample.gml')

# translate to Excellon using composition object
ctx = gerberex.DrillComposition()
ctx.merge(dxf)
ctx.dump('sample.txt')

# translate to RX-274x directly
dxf.save('sample2.gml')

# translate to Excellon directly
dxf.save('sample2.txt', filetype=dxf.FT_EXCELLON)

Generating Rectangle

If you want to arrange simple rectangle for PCB outline, gerberex.rectangle() is better solution. This generate a object representing a rectangle compatible with DXF file object.

import gerberex

outline = gerberex.rectangle(width=100, height=100, units='metric')
outline.write('outline.gml')

Drawing Mode

PCB tools extension provide three type of translation method that affects geometric finish. These method are specified a value for draw_mode attribute, as DM_LINE, DM_MOUSE_BITES, or DM_FILL.
DM_LINE and DM_MOUSE_BITES are used to translate to both of RX-274x and Excellon, however DM_FILL is used to translate to only RX-274x.

Drawing Mode

  • draw_mode = DM_LINE
    All edge expressed as DXF line object, circle object, arc object and plyline objects are translated to line and arc applied a circular aperture in case of RX-274x. That circular aperture radius is specified by width attribute. Default value of width is 0.
    In case of Excellon, DXF objects are translated to routing path command sequence.
    This function is useful to generate outline data of pnanelized PCB boad.

    import gerberex
    
    dxf = gerberex.read('outline.dxf')
    dxf.to_inch()
    dxf.width = 0.004
    dxf.write('outline.gml')
  • draw_mode = DM_MOUSE_BITES
    mouse bites If DM_MOUSE_BITES is specified for draw_mode, filled circles are arranged at equal intervals along a paths consisted of DXF line, arc, circle, and plyline objects. DXF file object in this state can be merged to excellon file also. That means you can arrange mouse bites easily.

    import gerberex
    
    ctx = gerberex.DrillComposition()
    drill = gerberex.read('drill.txt')
    ctx.merge(drill)
    
    dxf = gerberex.read('mousebites.dxf')
    dxf.draw_mode = dxf.DM_MOUSE_BITES
    dxf.to_metric()
    dxf.width = 0.5
    dxf.pitch = 1
    ctx.merge(dxf)
    
    ctx.dump('merged_drill.txt')
  • draw_mode = DM_FILL
    You can translate DXF closed shapes such as circle to RX-274x polygon fill sequence.
    In order to fill closed shapes, DM_FILL has to be set to draw_mode property. In this mode, All object except closed shapes listed below are ignored.

    • circle
    • closed polyline
    • closed path which consists of lines and arcs

    If a closed shape is completly included in other closed shape, The inner shape will be draw with reversed polality of container shape as above example image.

    I assume there are two typical use cases for this mode.
    One is to arrange logo design on silk layer. This is superior to other method generating raster image data since image data express as vector data.
    The other one is generating gerber data represented cropped area of panelized PCB. By merging rectangle and PCB outline data, generate a file represented cropped area as below, and this kind of data is useful to make PCB image look good a little bit.
    This script which generate example image shown below, also uses this technic.

    import gerberex
    
    ctx = gerberex.GerberComposition()
    
    rectangle = gerberex.rectangle(width=100, height=100, left=0, bottom=0, units='metric')
    rectangle.draw_mode = rectangle.DM_FILL
    ctx.merge(rectangle)
    
    outline = gerberex.read('outline.dxf')
    outline.draw_mode = outline.DM_FILL
    outline.negate_polarity()
    ctx.merge(outline)
    
    ctx.dump('cropped_area.gml')

    NOTE: DM_FILL can be used only to generate RX-274x data, it cannot be used to generate Excellon data.

Panelizing Example

This example board image is generated by following scripts from these source data.

description

Notes

Equivalence of output

pcb-tools-extension generate data block stream to focus equivalence of final image, but not focus equivalence of data block sequence. There are some difference between input data and output data as below.

  • Aperture definition [RS-274x]
    When gerber data is rotated, it's necessory to rotate not only coordinates whilch indicate locations of drawing aperture, but also aperture geometory itself. However, standard aperture templates, such as rectangle, cannot rotate. These standard aperture templates can be placed only horizontally or vertically.
    Threfore, pcb-tools-extension replace aperture definitions using standard aperture template to aperture macro that represent equivalent shape.
    For example, In case of rotating folowing aperture definition 20 degrees counter clockwise,

    %ADD10R,1X0.5X0.2*%

    pcb-toolse-extension generate a aperture macro definition and a aperture definition referencing that macro as below.

    %AMMACR*
    21,1,$1,$2,0,0,20*
    1,0,$3,0,0,20*%
    %ADD10MACR,1X0.5X0.2*%
  • File Scope Modifier [RS-274x]
    Commands that affect entire image and should be specified only once in a file, such as MI (Mirror Image) command, sometimes cause contradiction when multiple gerber file are merged.
    For example, when mergeing a file containing %MIA1B0*% command and a file containing %MIA0B1* command, which command should remain as output? Of cause, it is impossible that generate correct merged image by specifiing any MI command.
    pcb-tools-extension translate coordinate data reflecting these file socpe modifier to address this probrem, then ommit these modifier command.
    MI, OF, SF, AS, IP, and IR are in a this category.

  • Coodinate Normalizing [RS-274x, Excellon]
    RS-274x specification and Excellon specification allow various notation to express a coordinate. However pcb-tools-extension normalize coordinate notation in order to correct deprecated notation and ease internal process as below.

    • Relative coordinates are translated to absolute coordinates.
    • Ommited coordinate values are compensated.
    • Leading zeros are ommited.
  • Single Quadlant mode [RS-274x]
    Cercular interpolation coordinate data in single quadlant is difficult to rotate, because circular arc may pass across two quadlants after rotation.
    In order to avoid this problem, pcb-tools-extension change single quadlant mode coordinates specification to multi quadlangt mode.

  • NC controll command [Excellon]
    Form histrical reason, Excellon NC controll format is used to specify drill information to PCB fabricator.
    On the other hand, from PCB fabricator point of view, they don't need information other than geometric information, such as drill speed. Because these NC controll sequence doesn't send to NC machine directly, PCB fabricator import customers excellon NC file to their CAD / CAM to pnaelize and check, then they export NC controll data for their NC machine.
    pcb-tools-extension ommit all NC command which do not contribute to geometry expression. Specifically, only tool definitions (diametor of drill), tool selections, drilling coordinates, and routing paths are output.

  • Unimportant Command [RS-274x, Excellon]
    Commands not affecting final image such as comment are ommited.

Negative image polarity

Sometimes, %IPNEG*% is specified at header of RS-274x file to create negative image.
As mentioned above, IP command is ommited when pcb-tools-extension generate output file. In this case, image polarity is nagated by using LP command. However this generated file doesn't equal to original image since it does'nt contain base dark image.
Please merge base dark rectangle explicitly when you handle negative image file as below.

import gerberex

ctx = gerberex.GerberComposition()
base = gerberex.rectangle(width=30, height=30, left=-5, bottom=-5, units='metric')
base.draw_mode = base.DM_FILL
ctx.merge(base)
metal = gerberex.read('negative_image.gtl')
ctx.merge(metal)

Limitations

RS-274x

pcb-tools-extension cannot handle all commands that the RS-274x parser implemented in pcb-tools doesn't handle so far.
From the imaging point of view, pcb-tools-extension has following limitations.

  • Files contains IJ and IO commands, that affect entire image, cannot be handled correctly.
  • Files contains SR command to specify repeated pattern cannot be handled correctly.
  • Aperture block defined by AB command cannot be handled correctly.

Excellon

pcb-tools-extension extends excellon parser in pcb-tools to support routing operation. However following limitations still remain.

  • User defined stored pattern defined by M99 command cannot be handled.
  • Canned text specified by M97 command cannot be handled.
  • Pattern defined by M25 command cannot be handled.

pcb-tools-extension's People

Contributors

marinmikael avatar opiopan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pcb-tools-extension's Issues

Tag releases

Ahoi,
I'm trying to maintain this package in the Arch Linux User Repository. You would make my life easier if you could tag your releases on github. I could then create the package from each release instead of checking out the master. Your older release tags are difficult to parse. It would be much easier to just have v0.9.3 as a tag instead of v_0_9_1.

https://aur.archlinux.org/packages/python-pcb-tools-extension-git/

Broken arcs

Thanks for the useful tool.
BTW, I found an corner case where it produces the wrong gerber.
bug.dxf.gz

expected
bug

got

unsupported drill file type

This is my drill file from Ultiboard 12:
lor.TXT

It cannot export different one, it's part of rs-274x export.
But this lib fails to read it:

import gerberex
ctx = gerberex.DrillComposition()
drill1 = gerberex.read('lor.TXT')
ctx.merge(drill1)

Traceback (most recent call last):
  File "C:\py38_64\lib\site-packages\gerberex\composition.py", line 128, in merge
    raise Exception('unsupported file type')
Exception: unsupported file type

pcb_tools_extension-0.9.3-py3-none-any.whl
pcb-tools-0.1.6.tar.gz

error while reading dxf file

I have an error while trying to read dxf file without $INSUNITS defined in file header:
error
It seems the method should be changed so it is immune to not finding $INSUNITS in file header (it is not a required parameter).

Lines intersecting

Getting the line intersecting error on closed polyline entities.

This might be a difficult one to figure out.

It seems to happen mostly from things that I create in AutoCAD 2010 LT. I am wondering if the dxf can confuse the logic.

I do not see this behavior from the things I make using ezdxf or BricsCAD. More testing is needed though.

Copying Gerber data to reuse it

Hi, this is a very nice module and a great addition to pcb-tools.

At the moment we try to create a panel for several PCBs, which are placed in arrays like 2x5, and it takes quite a lot of time as the Gerber data has to be loaded over and over from the disk for the placement, as offset() moves the whole data. Tried to do deepcopy() but this fails with a message about dict_values.

One workaround i can think of is to offset the data back, but this could probably lead to problems and slight offsets

Is there a way to create a copy of Gerber data and reuse it? Would speed up the process a lot.

rout_statements used before init

The attached file throws an error that rout_statements is used before init at line 117 in excellon.py

Adding an initialiser:
rout_statements = []
after rout_nodes = []

appears to fix things OK.

Thanks for an awesome set of tools!
Gerber_Drill_NPTH.zip

TypeError, caused by Aperture Macro?

I'm experiencing a type error occurring while trying to parse gerber files which contain aperture macros.

You can find a reference to it here: (under 4.5 Aperture Macro (AM))
ucamco gerber specification

Here is the error that occurs in file \gerberex\am_expression.py", line 54, in to_gerber :

gerber = '%.6g' % self._value
                  │    └ 'Rectangle Pad at angle 45'<gerberex.am_expression.AMConstantExpression object at 0x037F0BC8>
TypeError: must be real number, not str

The whole expression in my gerberfile that should create a rectangle with a side length of 0.05512 mils, placed at an 45 degree angle looks like this:

%AMT83*0 Rectangle Pad at angle 45*21,1,0.05512,0.05512,0,0,45*%

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.