Wednesday, December 11, 2013

Enlarging polygons (Logos saved as parts) in Cadsoft Eagle

This is for regular users of Cadsoft Eagle - a software package used for designing PCBs.
If you have been using Eagle for as long as I have been using, you most definitely know how to create part libraries, write ULPs and have come up with creative ways to spruce up your PCB's silkscreen (and schematic drawings) with nice artwork - logos, line art, figures etc.

One way you can add artwork to your PCB is by drawing a monochrome figure in paint or some other graphics program and then using the import_bmp.ulp as described in this instructable

But this method imports your art work as a raster of small squares/traces which lacks fidelity - you can note the squares when zoomed in!

Consider this company logo on one of the circuits we designed

Here are the two versions of the logo. The one on top (raster) was created by importing
the BMP file of the logo using import_bmp.ulp. The one on the bottom (vector) was created
by retracing polygons by hand over the imported raster version of the logo and then deleting
the raster square. The logo on the bottom is smoother.

Usually you would have your company logo in some vector format (PS or CDR or SVG). This vector image of the logo is what you would use in your various branding artifacts and documentation. The reason one wants vector images is that they can be enlarged or shrink without loosing fidelity.

It is only reasonable to want your logo imported as a vector into your PCB's silkscreen.
The only two ways to have a vector image (in case of Eagle, this means a set of polygons) of your company logo are to either re-create the logo by hand in Eagle or if possible import SVG into Eagle using Cruz Monrreal II's ULP.

Hand tracing polygons manually over the imported raster squares and traces
to create high fidelity logos.
5 Polygons forming a logo.
Each polygon has been transposed to a different layer just so that
it appears in a different color. At the end all polygons must reside in
one of the layers used to generate the silkscreen gerber. 

Before Cruz Monrreal II developed his SVG import ULP (I haven't tried it yet), what I used to do is to import the logo as BMP into any layer in the part creation mode of Eagle, redraw polygons in a higher layer over the imported raster squares of the logo and save the polygons as a part. I would then export that script, modify the layer number in it and import it back into the schematic layer there by creating a part for the logo.

Once the logo was created using hand traced polygons in Eagle (and if desired saved as a part in one of your libraries), it became difficult to resize (enlarge or shrink). This became a problem when designing PCBs of varying sizes - we needed varying sizes of logos to go on them. The sizes of the polygons which formed the logo was specified in absolute units

Those who use import_bmp.ulp did not suffer from this problem because they could manually adjust the scale factor every time they imported the logo onto a PCB (Refer this image in the procedure described on this page)

So I wanted to be able to modify the size of the. And to do that here are the steps I followed:

  1. Export the logo (all polygons resided in only one of the layer) as an Eagle script (.scr). The .scr file is a text file which contains the coordinates of the vertices which form the polygon.
  2. Use a perl script to search for pattern of numbers specified in the format:
    (2.9396475 4.322355)
    These are the XY coordinates of the vertices forming the polygons. The perl script will multiply the abscissa and ordinates of each of the vertices by a fixed number (the pre-set magnification factor) and generate a new .scr file. Of course its not as simple as it sounds but that' the jist of it. There are a few issues that I had to take care of - read the comments in the perl script below.
  3. This new .scr file which when imported back into eagle would create and enlarged (or shrunk, depending on the pre-set magnification factor) version of the same logo.

Here is the perl script and how I shrunk the Texas Instruments Logo to half its size.

Varying sizes of TI Logo. The logo was first created by hand.
It was then exported as a script (.scr). A perl script then took this .scr file as input
and generated a new .scr file with magnified version of the logo.
The new .scr file was then re-imported back into eagle using "Execute Script"
Here is the perl script. There might still be some bugs lurking around.
I executed this perl script on my windows PC using Strawberry Perl

#read each line of file input.txt and find all decimal numbers in each line, multiply it by a constant and save the output in file output.txt


use strict;
use warnings;

open(my $in,  "<",  "input.scr")  or die "Can't open input.scr: $!";
open(my $out, ">",  "output.scr") or die "Can't open output.scr: $!";


while (<$in>) # assigns each line in turn to $_

	s/[\040](\d+)[\051]/' ' . $1 . '.0)'/ge;		#append .0 to all positive integer abscissae
	s/[\050](\d+)[\040]/'(' . $1 . '.0 '/ge;		#append .0 to all positive integer ordinates
	s/[\040][\055](\d+)[\051]/' -' . $1 . '.0)'/ge;		#append .0 to all negative integer abscissae
	s/[\050][\055](\d+)[\040]/'(-' . $1 . '.0 '/ge;		#append .0 to all negative integer ordinates
	#magnify decimal numbers pairs of all coordinates by the specified factor
	s/[\050](\d+)[.](\d+)[\040](\d+)[.](\d+)[\051]/'(' . eval{eval{"$1.$2"}*$magfactor} . ' ' . eval{eval{"$3.$4"}*$magfactor} . ')'/ge;
	s/[\050][\055](\d+)[.](\d+)[\040](\d+)[.](\d+)[\051]/'(-' . eval{eval{"$1.$2"}*$magfactor} . ' ' . eval{eval{"$3.$4"}*$magfactor} . ')'/ge;
	s/[\050](\d+)[.](\d+)[\040][\055](\d+)[.](\d+)[\051]/'(' . eval{eval{"$1.$2"}*$magfactor} . ' -' . eval{eval{"$3.$4"}*$magfactor} . ')'/ge;
	s/[\050][\055](\d+)[.](\d+)[\040][\055](\d+)[.](\d+)[\051]/'(-' . eval{eval{"$1.$2"}*$magfactor} . ' -' . eval{eval{"$3.$4"}*$magfactor} . ')'/ge;
	#Commandwise replacement: Polygon
	s/Polygon[\040](\d+)[\040]/'Polygon ' . eval{eval{"$1"}*$magfactor} . ' '/ge;	#magnify all positive whole numbers appearing after command
	s/Polygon[\040][\055](\d+)[\040]/'Polygon  -' . eval{eval{"$1"}*$magfactor} . ' '/ge;	#magnify all negative whole numbers appearing after command

	s/Polygon[\040](\d+)[.](\d+)[\040]/'Polygon ' . eval{eval{"$1.$2"}*$magfactor} . ' '/ge;	#magnify all positive decimal numbers appearing after command
	s/Polygon[\040][\055](\d+)[.](\d+)[\040]/'Polygon -' . eval{eval{"$1.$2"}*$magfactor} . ' '/ge;	#magnify all negative decimal numbers appearing after command
	#Commandwise replacement: Wire
	s/Wire[\040](\d+)[\040]/'Wire ' . eval{eval{"$1"}*$magfactor} . ' '/ge;	#magnify all positive whole numbers appearing after command
	s/Wire[\040][\055](\d+)[\040]/'Wire  -' . eval{eval{"$1"}*$magfactor} . ' '/ge;	#magnify all negative whole numbers appearing after command

	s/Wire[\040][\040](\d+)[.](\d+)[\040]/'Wire  ' . eval{eval{"$1.$2"}*$magfactor} . ' '/ge;	#magnify all positive decimal numbers appearing after command
	s/Wire[\040][\040][\055](\d+)[.](\d+)[\040]/'Wire  -' . eval{eval{"$1.$2"}*$magfactor} . ' '/ge;	#magnify all negative decimal numbers appearing after command
	print $out "$_";



  1. - The above perl script
  2. input.scr - Eagle script for creating the Texas Instruments Logo in Eagle in Layer 21 using handcrafted curved polygons.
  3. output.scr - Output when was run on input.scr. This Eagle script would create the Texas Instruments Logo in Layer 21 but of twice the size than the one created by input.scr
The above three files archived in a zip file available here


Post a Comment