-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_assembly.py
217 lines (180 loc) · 7.64 KB
/
parse_assembly.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
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
import pandas
import sys
import os
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QMainWindow, QTreeWidgetItem, QGraphicsScene, QFileDialog, \
QGraphicsPixmapItem
from PySide2.QtGui import QPixmap, QPen
from PySide2.QtCore import QFile, Qt
import gerber_render
import configparser
def populatebomtree():
global bom, assy_top, assy_bot, viewtop, bomcolumns, selectionset
bom = pandas.read_csv(os.path.join(BASE_FOLDER, 'Assembly\\bom.csv'), header=0)
assy_top = pandas.read_csv(os.path.join(BASE_FOLDER, 'Assembly\\PnP_front.csv'), header=0)
assy_bot = pandas.read_csv(os.path.join(BASE_FOLDER, 'Assembly\\PnP_back.csv'), header=0)
tw = window.treeWidget_bom
tw.clearSelection()
tw.clear()
assy_side = assy_top if viewtop else assy_bot
for index, row in bom.iterrows():
selected = str.split(row['Parts'], ', ')
pnp_refdes = assy_side['Name']
pnp_count = 0
parts_thisside = ''
for refdes in selected:
pnp_row = assy_side[pnp_refdes == refdes]
if len(pnp_row) > 0:
if (selectionset == None) or (refdes in selectionset):
if pnp_count == 0:
twi = QTreeWidgetItem(tw, row.filter(bomcolumns).tolist())
parts_thisside = parts_thisside + ',' + refdes if pnp_count > 0 else refdes
pnp_count = pnp_count + 1
twi.addChild(QTreeWidgetItem(twi, [pnp_row['Name'].values[0]]))
if 0 < pnp_count < len(selected):
twi.setData(0, Qt.DisplayRole, parts_thisside)
if pnp_count == 1:
twi.removeChild(twi.child(0))
class TestWindow(QMainWindow):
def __init__(self, parent=None):
super(TestWindow, self).__init__(parent)
loader = QUiLoader()
file = QFile("ui_gerber_pnp_vis.ui")
if file.open(QFile.ReadOnly):
self.window = loader.load(file, parent)
file.close()
self.setCentralWidget(self.window)
self.show()
def resizeEvent(self, event):
scaleboardimage()
def scaleboardimage():
try:
if len(scene.items()) > 0:
view = window.graphicsView
scale = 0.99 * min(view.width() / scene.width(), view.height() / scene.height())
window.graphicsView.setScene(scene)
window.graphicsView.resetTransform()
window.graphicsView.scale(scale, scale)
except NameError:
return
def highlightparts():
for sceneItem in scene.items():
if type(sceneItem) not in [QGraphicsPixmapItem]:
scene.removeItem(sceneItem)
for item in window.treeWidget_bom.selectedItems():
# if len(window.treeWidget_bom.selectedItems()) > 0:
# item = window.treeWidget_bom.selectedItems()[0]
if item.childCount() == 0:
highlightpart(item)
else:
for index in range(item.childCount()):
highlightpart(item.child(index))
window.graphicsView.setScene(scene)
def highlightpart(item):
global origin_in, ppi, viewtop
rectsize = [25, 25]
assy_side = assy_top if viewtop else assy_bot
pnp_refdes = assy_side['Name']
refdes = item.data(0, 0)
pnp_row = assy_side[pnp_refdes == refdes]
if len(pnp_row) > 0:
x = (pnp_row['X'] / 25.4 - origin_in[0]) * ppi
if not viewtop:
x = scene.width() - x
y = scene.height() - (pnp_row['Y'] / 25.4 - origin_in[1]) * ppi
pen = QPen(Qt.yellow, 5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
scene.addLine(x - rectsize[0], y - rectsize[1], x + rectsize[0], y + rectsize[1], pen)
scene.addLine(x - rectsize[0], y + rectsize[1], x + rectsize[0], y - rectsize[1], pen)
pen = QPen(Qt.red, 10, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
scene.addRect(x - rectsize[0] / 2, y - rectsize[1] / 2, rectsize[0], rectsize[1], pen)
def loadboard():
global viewtop, pixmap_top, pixmap_bot, BASE_FOLDER, origin_in, ppi
dialog = QFileDialog()
BASE_FOLDER = dialog.getExistingDirectory(window, "Select CAMOutputs directory to process...", '.',
QFileDialog.ShowDirsOnly)
if os.path.split(BASE_FOLDER)[1] != 'CAMOutputs':
if os.path.exists(os.path.join(BASE_FOLDER, 'CAMOutputs')):
BASE_FOLDER = os.path.join(BASE_FOLDER, 'CAMOutputs')
else:
window.statusbar.showMessage('Error: Invalid directory selected (expected CAMOutputs)', 10000)
return
window.statusbar.showMessage('Processing ' + BASE_FOLDER)
window.statusbar.repaint()
if gerber_render.isrendercurrent(BASE_FOLDER):
print('Render is current')
else:
gerber_render.render(BASE_FOLDER)
config = configparser.ConfigParser()
config.read(os.path.join(BASE_FOLDER, 'board.ini'))
origin_in = [float(config['DIMENSIONS']['origin_x_in']), float(config['DIMENSIONS']['origin_y_in'])]
ppi = float(config['DIMENSIONS']['ppi'])
pixmap_top = QPixmap(os.path.join(BASE_FOLDER, 'board_top.png'))
pixmap_bot = QPixmap(os.path.join(BASE_FOLDER, 'board_bottom.png'))
setboardtop()
window.actionSideTop.setChecked(True)
window.actionSideBottom.setChecked(False)
window.action_loadbomselectionset.setEnabled(True)
window.statusbar.showMessage('Done processing board', 10000)
def setboardtop():
global viewtop
viewtop = True
window.actionSideTop.setChecked(True)
window.actionSideBottom.setChecked(False)
setboardside()
def setboardbot():
global viewtop
viewtop = False
window.actionSideTop.setChecked(False)
window.actionSideBottom.setChecked(True)
setboardside()
def setboardside():
global viewtop
try:
scene.clear()
if viewtop:
scene.addPixmap(pixmap_top)
else:
scene.addPixmap(pixmap_bot)
scaleboardimage()
populatebomtree()
except NameError:
window.actionSideTop.setChecked(False)
window.actionSideBottom.setChecked(False)
window.statusbar.showMessage('No board loaded', 3000)
def readselectionset():
global selectionset
dialog = QFileDialog()
selectionset_path = dialog.getOpenFileName(window, "Select BOM selection set file...", '.', '*.txt')
if os.path.exists(selectionset_path[0]):
selectionset = pandas.read_csv(selectionset_path[0], header=None).squeeze().tolist()
window.action_clearbomselectionset.setEnabled(True)
populatebomtree()
window.statusbar.showMessage('BOM selection set active: ' + selectionset_path[0])
def clearselectionset():
global selectionset
selectionset = None
window.action_clearbomselectionset.setEnabled(False)
populatebomtree()
# if __name__ == "__main__":
app = QApplication(sys.argv)
selectionset = None
BASE_FOLDER = '.'
viewtop = True
config = configparser.ConfigParser()
config.read('config.ini')
bomcolumns = str.split(config['BOMTREE']['COLUMNS'], ',')
test = TestWindow()
window = test.window
window.treeWidget_bom.setColumnCount(len(bomcolumns))
for ii in range(len(bomcolumns)):
window.treeWidget_bom.header().model().setHeaderData(ii, Qt.Horizontal, bomcolumns[ii])
scene = QGraphicsScene()
window.splitter.splitterMoved.connect(scaleboardimage)
window.treeWidget_bom.itemSelectionChanged.connect(highlightparts)
window.action_processgerbers.triggered.connect(loadboard)
window.actionSideTop.triggered.connect(setboardtop)
window.actionSideBottom.triggered.connect(setboardbot)
window.action_loadbomselectionset.triggered.connect(readselectionset)
window.action_clearbomselectionset.triggered.connect(clearselectionset)
window.statusbar.showMessage('No board loaded')
sys.exit(app.exec_())