-
Notifications
You must be signed in to change notification settings - Fork 2
/
dxf2brd.cpp
284 lines (231 loc) · 8.45 KB
/
dxf2brd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <dxflib/dl_dxf.h>
#include <dxflib/dl_creationadapter.h>
#include <iostream>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// AUTHOR: Andy Goetz
// MAIL: andy@andygoetz.org
// PROGRAM: dxf2brd
// This program converts DXF files to kicad BRD files. The KICad
// board designer has very primitive support drawing shapes
// accurately. You can use this program to create a complex design in
// a real cad program, like qcad, and import it into a kicad BRD file.
// Compiling:
// to use this program, you must have dxflib installed.
// then use the command:
//
// g++ dxf2brd.cpp -o dxf2brd -ldxflib
//
// To compile the program.
// running the program:
// to run the program, execute the following command:
//
// ./dxf2brd some_dxf_file.dxf
//
// It will produce BRD code as its output. to add this code to an existing BRD file, run the following command:
//
// ./dxf2brd some_dxf_file.dxf >> some_brd_file.brd
// these defines control the kicad output. All units are tenths of mils:
#define LINE_THICKNESS 150 // thickness of traces
#define X_OFFSET 40000 // x offset of DXF origin in kicad coordinate space
#define Y_OFFSET 40000 // y offset of DXF origin in kicad coordinate space
#define LAYER 28 // layer to render output on
// This class contains the callbacks from dxflib only 3 important ones
// matter: the callbacks for lines, circles, and arcs.
class Dxf2BrdFilter : public DL_CreationAdapter {
// called when a line has been detected
virtual void addLine(const DL_LineData& d);
// called when a circle has been detected
virtual void addCircle(const DL_CircleData& d);
// called when an arc has been detected
virtual void addArc(const DL_ArcData& d);
protected:
// converts a DXF Coordinate, angle and radius into a KICAD
// coordinate that is offset by the radius in the direction of
// angle
void convertangle(double xin, double yin, double radius, double angle, int &xout, int &yout);
// converts a DXF coordinate to a KICAD coordinate
void convert(double xin, double yin, int &xout, int &yout);
// offset (in 10,000ths of an inch) to insert origin of DXF
// drawing in KICAD drawing.
int xoffset;
int yoffset;
// layer to draw on in KICAD drawing (28 is board outline)
int layer;
// thickness in tenths of mils of drawing in Kicad
int thickness;
public:
// constructor
Dxf2BrdFilter(int xoffset =X_OFFSET, int yoffset =Y_OFFSET,
int layer = LAYER, int thickness = LINE_THICKNESS);
};
// lines are the easiest to convert: both kicad and dxf use the same
// format: pairs of points for the start and end.
void Dxf2BrdFilter::addLine(const DL_LineData& d) {
int x1 = 0;
int x2 = 0;
int y1 = 0;
int y2 = 0;
convert(d.x1, d.y1, x1, y1);
convert(d.x2, d.y2, x2, y2);
std::cout << "$DRAWSEGMENT" << std::endl;
std::cout << "Po 0 " << x1 << " " << y1 << " "
<< x2 << " " << y2 << " " << thickness << std::endl;
std::cout << "De " << layer << " 0 900 0 0" << std::endl;
std::cout << "$endDRAWSEGMENT" << std::endl;
}
// Circles are a bit more complex. Kicad uses two points: one at the
// center, and one on the circumference to describe a circle, while
// DXF uses a center and radius.
// not only that, but we need to generate drill holes in the PCB
// instead of just drawing circles on the PCB in order to do this, we
// add a module with a single pin, that is specified as being
// mechanical: that is, it is not plated, and not connected to any
// net.
void Dxf2BrdFilter::addCircle(const DL_CircleData& d) {
int cx = 0;
int cy = 0;
int xend, yend;
int rad = (int)d.radius * 10;
int crad = rad + 150;
convert(d.cx, d.cy, cx, cy);
yend = cy;
xend = cx + rad;
std::cout << "$MODULE MountingHole" << std::endl;
std::cout << "Po " << cx << " " << cy <<
" 0 15 5052AEE8 5052AC20 ~~" << std::endl;
std::cout << "Li MountingHole" << std::endl;
std::cout << "Cd Simple Mounting Hole" << std::endl;
std::cout << "Kw DEV" << std::endl;
std::cout << "Sc 5052AC20" << std::endl;
std::cout << "AR 1pin" << std::endl;
std::cout << "Op 0 0 0" << std::endl;
std::cout << "T0 0 -1200 400 400 0 100 N I 21 N \"Mounting Hole\"" << std::endl;
std::cout << "T1 0 1100 400 400 0 100 N I 21 N \"P***\"" << std::endl;
std::cout << "$PAD" << std::endl;
std::cout << "Sh \"\" C " << crad << " "
<< crad << " 0 0 0" << std::endl;
std::cout << "Dr " << rad << " 0 0" << std::endl;
std::cout << "At HOLE N 00E0FFFF" << std::endl;
std::cout << "Ne 0 \"\"" << std::endl;
std::cout << "Po 0 0" << std::endl;
std::cout << "$EndPAD" << std::endl;
std::cout << "$EndMODULE MountingHole" << std::endl;
// draw a picture of the hole for alignment purposes
std::cout << "$DRAWSEGMENT" << std::endl;
std::cout << "Po 3 " << cx << " " << cy << " " << xend << " " << yend <<" 150" << std::endl;
std::cout << "De 24 0 900 0 0" << std::endl;
std::cout << "$EndDRAWSEGMENT" << std::endl;
}
// This is the most complex conversion. Kicad only allows arcs of 90
// degrees, which immediately eliminates many possible DXF
// arcs. Additionally, the format for the arcs is extremely weird:
// Kicad uses two points to describe an arc: the first point is
// located at the center of the arc. The second point is located at
// one terminus of the arc. The other terminus is automatically
// defined by moving -90 degrees from the start point.
// in addition, to simplify the logic: this routine only supports 180
// arcs that are located at 90 degree angles.
void Dxf2BrdFilter::addArc(const DL_ArcData& d) {
int xstart = 0;
int ystart = 0;
int xend = 0;
int yend = 0;
int a1 = d.angle1;
int a2 = d.angle2;
int ka1;
int ka2;
if(a1 % 90 != 0 || a1 == 0) {
std::cerr << "Arc not at 90 degrees!" << std::endl;
return;
}
if(a1 - a2 != 180 && a1 - a2 != -180) {
std::cerr << "Arc not 180 degrees long!" << std::endl;
return;
}
// in order to draw the semicircles, we split the single DXF
// arc into two 90 degree kicad arcs. In order to draw this
// correctly, we need to calculate the end points for these
// arcs.
if (a1 == 90 && a2 == 270) {
ka1 = 180;
ka2 = 270;
} else if (a1 == 270 && a2 == 90) {
ka1 = 360;
ka2 = 90;
} else if (a1 == 360 && a2 == 180) {
ka1 = 90;
ka2 = 180;
} else if (a1 == 180 && a2 == 360) {
ka1 = 270;
ka2 = 360;
} else {
std::cerr << "weird angles: " << a1 << " " << a2 << std::endl;
return;
}
convert(d.cx, d.cy, xstart, ystart);
convertangle(d.cx, d.cy, d.radius, ka1, xend, yend);
std::cout << "$DRAWSEGMENT" << std::endl;
std::cout << "Po 2 " << xstart << " " << ystart << " "
<< xend << " " << yend << " " << thickness << std::endl;
std::cout << "De " << layer << " 0 900 0 0" << std::endl;
std::cout << "$endDRAWSEGMENT" << std::endl;
convert(d.cx, d.cy, xstart, ystart);
convertangle(d.cx, d.cy, d.radius, ka2, xend, yend);
std::cout << "$DRAWSEGMENT" << std::endl;
std::cout << "Po 2 " << xstart << " " << ystart << " "
<< xend << " " << yend << " " << thickness << std::endl;
std::cout << "De " << layer << " 0 900 0 0" << std::endl;
std::cout << "$endDRAWSEGMENT" << std::endl;
}
// constructor
Dxf2BrdFilter::Dxf2BrdFilter(int xoffset, int yoffset,
int layer, int thickness) :
xoffset(xoffset), yoffset(yoffset), layer(layer), thickness(thickness) {}
void Dxf2BrdFilter::convert(double xin, double yin, int &xout, int &yout)
{
xout = (int)(xin * 10) + xoffset;
yout = yoffset - (int)(yin * 10);
}
void Dxf2BrdFilter::convertangle(double xin, double yin,
double radius, double angle, int &xout, int &yout)
{
convert(xin, yin, xout, yout);
int rad = (int)(radius * 10);
switch((int)angle) {
case 360:
xout += rad;
break;
case 90:
yout -= rad;
break;
case 180:
xout -= rad;
break;
case 270:
yout += rad;
break;
default:
std::cerr << "invalid angle: " << angle << std::endl;
}
}
int main(int argc, char ** argv)
{
if (argc < 2)
return -1;
Dxf2BrdFilter f;
DL_Dxf* dxf = new DL_Dxf();
if (!dxf->in(argv[1], &f)) {
std::cerr << "drawing could not be opened.\n";
}
delete dxf;
return 0;
}