forked from aktos-io/kicad-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot-pcb.py
executable file
·179 lines (145 loc) · 4.96 KB
/
plot-pcb.py
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
#!/usr/bin/python
'''
A python script example to create various plot files from a board:
Fab files
Doc files
Gerber files
Important note:
this python script does not plot frame references.
the reason is it is not yet possible from a python script because plotting
plot frame references needs loading the corresponding page layout file
(.wks file) or the default template.
This info (the page layout template) is not stored in the board, and therefore
not available.
Do not try to change SetPlotFrameRef(False) to SetPlotFrameRef(true)
the result is the pcbnew lib will crash if you try to plot
the unknown frame references template.
'''
import sys, os
from pcbnew import *
try:
filename=sys.argv[1]
except:
import glob
filename = glob.glob('./*.kicad_pcb')[0]
print("using kicad_pcb file: %s" % filename)
file_basename = os.path.splitext(filename)[0]
board = LoadBoard(filename)
pctl = PLOT_CONTROLLER(board)
popt = pctl.GetPlotOptions()
# Set some important plot options:
popt.SetPlotFrameRef(False)
popt.SetLineWidth(FromMM(0.35))
popt.SetAutoScale(False)
popt.SetScale(1)
popt.SetMirror(False)
popt.SetUseGerberAttributes(True)
popt.SetExcludeEdgeLayer(False);
popt.SetUseAuxOrigin(True)
# These files are needed in production
# ########################################
popt.SetOutputDirectory("plot-production")
# Top Layer
popt.SetDrillMarksType(PCB_PLOT_PARAMS.SMALL_DRILL_SHAPE)
popt.SetMirror(True)
pctl.SetLayer(F_Cu)
pctl.OpenPlotfile("layer-F_Cu", PLOT_FORMAT_SVG, "Top Layer")
pctl.PlotLayer()
# Bottom Layer
popt.SetDrillMarksType(PCB_PLOT_PARAMS.SMALL_DRILL_SHAPE)
popt.SetMirror(False)
pctl.SetLayer(B_Cu)
pctl.OpenPlotfile("layer-B_Cu", PLOT_FORMAT_SVG, "Bottom Layer")
pctl.PlotLayer()
# Drill map
popt.SetOutputDirectory("plot-drill")
# workaround for setting GetPlotDirName
pctl.OpenPlotfile("myworkaround", PLOT_FORMAT_SVG, "workaround")
drlwriter = EXCELLON_WRITER( board )
drlwriter.SetMapFileFormat( PLOT_FORMAT_PDF )
mirror = False
minimalHeader = False
offset = wxPoint(0,0)
# False to generate 2 separate drill files (one for plated holes, one for non plated holes)
# True to generate only one drill file
mergeNPTH = True
drlwriter.SetOptions( mirror, minimalHeader, offset, mergeNPTH )
metricFmt = True
drlwriter.SetFormat( metricFmt )
genDrl = True
genMap = True
drlwriter.CreateDrillandMapFilesSet( pctl.GetPlotDirName(), genDrl, genMap );
# Assembly Map
# ######################################################
popt.SetOutputDirectory("plot-assembly")
# We want *everything*
popt.SetPlotReference(True)
popt.SetPlotValue(True)
popt.SetPlotInvisibleText(True)
popt.SetDrillMarksType(PCB_PLOT_PARAMS.SMALL_DRILL_SHAPE)
pctl.SetColorMode(True)
assembly_map_files = [
{'layer': F_Cu, 'side': "Front", 'color': 'grayed', 'mirror': False},
{'layer': F_Paste, 'side': "Front", 'color': 'black', 'mirror': False},
{'layer': F_Fab, 'side': "Front", 'color': 'black', 'mirror': False},
{'layer': B_Cu, 'side': "Back", 'color': 'grayed', 'mirror': True},
{'layer': B_Paste, 'side': "Back", 'color': 'black', 'mirror': True},
{'layer': B_Fab, 'side': "Back", 'color': 'black', 'mirror': True},
]
popt.SetScale(1)
i = 0
for a in assembly_map_files:
popt.SetMirror(a['mirror'])
pctl.SetLayer(a['layer'])
a['postfix'] = "%s_%s_%i" % (a['side'], a['color'], i)
pctl.OpenPlotfile(a["postfix"], PLOT_FORMAT_SVG, "Assembly Layer")
pctl.PlotLayer()
i += 1
sides = ['Front', 'Back'] # FIXME: get from assembly_map_files list
for side in sides:
assembly_html = """
<html>
<head>
<style>
img {
position: absolute;
left: 0;
top: 0;
transform: scale(5);
}
.grayed {
filter: opacity(20%);
}
.container {
position: relative;
left: 1000px;
top: 900px;
}
</style>
</head>
"""
assembly_html += """
<body>
<h3><center>{0} ({1})</center></h3>
<div class="container">
{2}
</div>
</body>
</html>
""".format(
file_basename,
side,
'\n'.join(
["<img src='{0}-{1}.svg' class='{2}' />".format(
file_basename, f['postfix'], f['color']
) for f in assembly_map_files if f['side'] == side]
)
)
html_filename = os.path.join(pctl.GetPlotDirName(), 'assembly_map_{}.html'.format(side))
try:
with open(html_filename, 'r') as x:
print("WARNING: {} exists, not overwriting. ".format(html_filename))
except:
with open(html_filename, "w") as f:
f.write(assembly_html)
pctl.ClosePlot()