-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
726 lines (605 loc) · 24.6 KB
/
mainwindow.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
This file is part of pickAndPlaceMachine.
pickAndPlaceMachine 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.
pickAndPlaceMachine 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 pickAndPlaceMachine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "csvimporter.h"
#include "centroiddialog.h"
#include <QtCore>
#include <QtGui>
#include <vtkRenderWindow.h>
#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkSmartPointer.h>
#include <vtkProperty.h>
#include <vtkPolygon.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkTransform.h>
#include <vtkLinearExtrusionFilter.h>
#include <vtkCellArray.h>
#include <vtkTriangleFilter.h>
#include <vtkCleanPolyData.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataNormals.h>
#include <vtkAppendPolyData.h>
#if ((VTK_MAJOR_VERSION>5) || (VTK_MAJOR_VERSION==5 && VTK_MINOR_VERSION>=10))
#define HAVE_BOOLEANPOLYFILTER
#include "vtkBooleanOperationPolyDataFilter.h"
#endif
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// enable status bar updates
ui->graphicsView->setStatusBar( ui->statusBar );
// setup QGraphics view
ui->graphicsView->scale(1,-1);
m_scene = new QGraphicsScene;
ui->graphicsView->setScene(m_scene);
// setup vtk view
m_vtkRenderer = vtkRenderer::New();
ui->qvtkWidget->GetRenderWindow()->AddRenderer( m_vtkRenderer );
ui->qvtkWidget->GetRenderWindow()->Render();
// setup tree view
ui->treeWidget->setHeaderLabels( QStringList("Layer") );
m_treeLayerOutline = new QTreeWidgetItem(QStringList("Outline"));
ui->treeWidget->addTopLevelItem( m_treeLayerOutline );
m_treeLayerTop = new QTreeWidgetItem(QStringList("Top"));
ui->treeWidget->addTopLevelItem( m_treeLayerTop );
m_treeLayerBottom = new QTreeWidgetItem(QStringList("Bottom"));
ui->treeWidget->addTopLevelItem( m_treeLayerBottom );
m_treeCentroid = new QTreeWidgetItem(QStringList("Pick&Place"));
ui->treeWidget->addTopLevelItem( m_treeCentroid );
m_treeExcellon = new QTreeWidgetItem(QStringList("Excellon"));
ui->treeWidget->addTopLevelItem( m_treeExcellon );
m_treeUnknown = new QTreeWidgetItem(QStringList("Unknown"));
ui->treeWidget->addTopLevelItem( m_treeUnknown );
// setup defaults
m_laminateHeight = 1.6; // 1.6 mm
m_laminateColor = QColor("green");
m_metalThickness = 35e-3; // metal thickness 35 um
// QTimer::singleShot( 0, this, SLOT(test()) );
}
MainWindow::~MainWindow()
{
qDeleteAll(m_Centroid);
m_Centroid.clear();
delete m_scene;
delete ui;
}
void MainWindow::test()
{
import_Excellon( "/home/sebastian/src/pickAndPlaceMachine/examples/LPC_4TE_3.43controller.drd" );
import_Gerber( "/home/sebastian/src/pickAndPlaceMachine/examples/LPC_4TE_3.43controller.outline" );
updateView();
}
void MainWindow::on_actionImport_triggered()
{
QStringList filenames = QFileDialog::getOpenFileNames( this, "Select files to import" );
if (filenames.isEmpty())
return;
foreach( QString filename, filenames ) {
QFileInfo fi(filename);
if ((fi.suffix().toLower() == "csv") || (fi.suffix().toLower() == "mnt")) {
// most likely Pick&Place file
import_Centroid( filename );
} else if (fi.suffix().toLower() == "drd") {
// most likely Excellon file
import_Excellon( filename );
} else if (!import_Gerber( filename )) {
// unkown file format
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText( 0, QDir::toNativeSeparators(fi.fileName()) );
item->setToolTip( 0, QDir::toNativeSeparators(filename) );
m_treeUnknown->addChild(item);
}
}
updateView();
}
static vtkSmartPointer<vtkActor> AddClosedPoly( QList<QPointF> coords, double thickness )
{
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> poly = vtkSmartPointer<vtkCellArray>::New();
vtkSmartPointer<vtkPolyData> profile = vtkSmartPointer<vtkPolyData>::New();
vtkSmartPointer<vtkLinearExtrusionFilter> extrude = vtkSmartPointer<vtkLinearExtrusionFilter>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
for (int i=0; i<coords.size(); i++)
points->InsertPoint( i, coords.at(i).x(), coords.at(i).y(), 0 );
poly->InsertNextCell( coords.size() );
for (int i=0; i<coords.size(); i++)
poly->InsertCellPoint(i);
profile->SetPoints( points );
profile->SetPolys( poly );
vtkSmartPointer<vtkCleanPolyData> clean = vtkSmartPointer<vtkCleanPolyData>::New();
clean->SetInput( profile );
vtkSmartPointer<vtkTriangleFilter> tf = vtkSmartPointer<vtkTriangleFilter>::New();
tf->SetInputConnection( clean->GetOutputPort() );
extrude->SetInputConnection( tf->GetOutputPort() );
extrude->SetExtrusionTypeToVectorExtrusion();
extrude->SetVector( 0,0,thickness );
extrude->CappingOn();
mapper->SetInputConnection( extrude->GetOutputPort() );
actor->SetMapper( mapper );
return actor;
}
void MainWindow::updateView()
{
m_scene->clear();
ui->graphicsView->update();
// remove all actors from 3D scene
m_vtkRenderer->RemoveAllViewProps();
m_vtkRenderer->ResetCamera();
ui->qvtkWidget->GetRenderWindow()->Render();
if (m_treeLayerOutline->childCount() > 0) {
if (m_treeLayerOutline->childCount() > 1)
qDebug() << "currently only one outline supported";
int id = m_treeLayerOutline->child(0)->data(0,Qt::UserRole).toInt();
GerberImporter& importer = m_GerberImporter[id];
// create 2D outline
render_2D( importer );
// create 3D laminate
QList<QPointF> outline;
QPolygonF outline_poly = importer.getOutlineF();
outline = outline_poly.toList();
if (outline.size() >= 3) {
vtkSmartPointer<vtkActor> actor2 = AddClosedPoly( outline, -m_laminateHeight );
actor2->GetProperty()->SetColor( m_laminateColor.redF(), m_laminateColor.greenF(), m_laminateColor.blueF() );
actor2->GetProperty()->SetOpacity( 0.4 );
m_vtkRenderer->AddViewProp( actor2 );
}
}
for (int i=0; i<m_treeLayerTop->childCount(); i++) {
int id = m_treeLayerTop->child(i)->data(0,Qt::UserRole).toInt();
GerberImporter& importer = m_GerberImporter[id];
render( importer, 0, m_metalThickness );
}
for (int i=0; i<m_treeLayerBottom->childCount(); i++) {
int id = m_treeLayerBottom->child(i)->data(0,Qt::UserRole).toInt();
GerberImporter& importer = m_GerberImporter[id];
render( importer, -m_laminateHeight - m_metalThickness, m_metalThickness );
}
for (int i=0; i<m_treeCentroid->childCount(); i++) {
int id = m_treeCentroid->child(i)->data(0,Qt::UserRole).toInt();
render_Centroid( id, 0, -m_laminateHeight, m_metalThickness );
}
for (int i=0; i<m_treeExcellon->childCount(); i++) {
int id = m_treeExcellon->child(i)->data(0,Qt::UserRole).toInt();
render_Excellon( id, 0, -m_laminateHeight, m_metalThickness );
}
// rescale 2D view
ui->graphicsView->fitInView( m_scene->itemsBoundingRect(), Qt::KeepAspectRatio );
// rescale 3D view
m_vtkRenderer->ResetCamera();
ui->qvtkWidget->GetRenderWindow()->Render();
}
void MainWindow::render( GerberImporter& importer, double zpos, double thickness )
{
render_2D( importer );
render_3D( importer, zpos, thickness );
}
void MainWindow::render_2D( GerberImporter& importer )
{
//
// draw 2D image
//
QList<Layer> layers = importer.getLayers();
foreach (Layer layer, layers) {
QList<Object*> objects = layer.getObjects();
foreach (Object* object, objects) {
m_scene->addItem( object->getGraphicsItem() );
}
}
}
void MainWindow::render_3D( GerberImporter& importer, double zpos, double thickness )
{
//
// draw 3D image
//
QList<Layer> layers = importer.getLayers();
foreach (Layer layer, layers) {
vtkSmartPointer<vtkAssembly> assembly = vtkSmartPointer<vtkAssembly>::New();
vtkSmartPointer<vtkProp3D> prop3D = layer.getVtkProp3D( thickness );
assembly->AddPart( prop3D );
assembly->AddPosition( 0, 0, zpos );
m_vtkRenderer->AddViewProp( assembly );
}
}
void MainWindow::render_Centroid( int num, double zpos_top, double zpos_bottom, double thickness )
{
if (!m_Centroid.contains(num))
return;
Centroid* centroid = m_Centroid.value(num);
QList<CentroidLine> lines = centroid->lines();
//
// draw 2D
//
QGraphicsItemGroup* group = new QGraphicsItemGroup;
group->setData(0,Type_PickPlaceFile);
group->setData(1,num);
// delete previous rendering
QList<QGraphicsItem*> items = m_scene->items();
QMutableListIterator<QGraphicsItem*> it(items);
while (it.hasNext()) {
QGraphicsItem* item = it.next();
if (item && !item->data(0).isNull() && (item->data(0).toInt() == Type_PickPlaceFile) && (item->data(1).toInt() == num)) {
delete item;
break;
}
}
for (int i=0; i<lines.size(); i++) {
CentroidLine line = lines.at(i);
qDebug() << line.RefDes << line.Description << line.Value << line.x.get_d() << line.y.get_d() << line.rotation.get_d();
QRectF r;
r.setSize( QSizeF(1,1) );
r.moveCenter( QPointF(line.x.get_d(),line.y.get_d()) );
QGraphicsEllipseItem *item = new QGraphicsEllipseItem(r);
item->setPen( QPen("red") );
item->setBrush( QBrush("red") );
group->addToGroup( item );
}
m_scene->addItem( group );
}
void MainWindow::render_Excellon( int num, double zpos_top, double zpos_bottom, double thickness )
{
if (!m_Excellon.contains(num))
return;
ExcellonImporter* excellon = m_Excellon.value(num);
QList<Drill> drills = excellon->drills();
//
// draw 2D
//
QGraphicsItemGroup* group = new QGraphicsItemGroup;
group->setData(0,Type_ExcellonFile);
group->setData(1,num);
// delete previous rendering
QList<QGraphicsItem*> items = m_scene->items();
QMutableListIterator<QGraphicsItem*> it(items);
while (it.hasNext()) {
QGraphicsItem* item = it.next();
if (item && !item->data(0).isNull() && (item->data(0).toInt() == Type_ExcellonFile) && (item->data(1).toInt() == num)) {
delete item;
break;
}
}
for (int i=0; i<drills.size(); i++) {
Drill drill = drills.at(i);
QRectF r;
mpq_class d = drill.diameter() * 1000; // convert to mm
mpq_class x = drill.x() * 1000; // convert to mm
mpq_class y = drill.y() * 1000; // convert to mm
r.setSize( QSizeF(d.get_d(),d.get_d()) );
r.moveCenter( QPointF(x.get_d(),y.get_d()) );
QGraphicsEllipseItem *item = new QGraphicsEllipseItem(r);
item->setPen( QPen("red") );
item->setBrush( QBrush("red") );
group->addToGroup( item );
}
m_scene->addItem( group );
//
// draw 3D
//
// we currently assume the PCB (extruded Gerber outline) is the only actor present
#ifdef HAVE_BOOLEANPOLYFILTER
vtkActorCollection* actors = m_vtkRenderer->GetActors();
//qDebug() << "Number of Actors:" << actors->GetNumberOfItems();
if (actors->GetNumberOfItems() >= 1) {
vtkSmartPointer<vtkBooleanOperationPolyDataFilter> filter = vtkSmartPointer<vtkBooleanOperationPolyDataFilter>::New();
actors->InitTraversal();
vtkActor* actor = actors->GetNextActor(); // this is the PCB
vtkMapper* mapper = actor->GetMapper();
vtkDataSet* dataSet = mapper->GetInput();
vtkPolyData* polyData = dynamic_cast<vtkPolyData*>(dataSet); // this is the PCB as polygonal data
if (polyData) {
// create normal vectors for PCB
vtkSmartPointer<vtkPolyDataNormals> polyDataNormals = vtkSmartPointer<vtkPolyDataNormals>::New();
polyDataNormals->SetInput( polyData );
// triangulate PCB
vtkSmartPointer<vtkTriangleFilter> triangleFilter2 = vtkSmartPointer<vtkTriangleFilter>::New();
triangleFilter2->SetInput( polyDataNormals->GetOutput() );
// copy result into polyData
triangleFilter2->Update();
polyData->DeepCopy( triangleFilter2->GetOutput() );
vtkSmartPointer<vtkAppendPolyData> appendPolyData = vtkSmartPointer<vtkAppendPolyData>::New();
// create drills
for (int i=0; i<drills.size(); i++) {
Drill drill = drills.at(i);
mpq_class r = drill.diameter() * 1000 / 2; // convert to mm
mpq_class x = drill.x() * 1000; // convert to mm
mpq_class y = drill.y() * 1000; // convert to mm
// create drill at correct position
vtkSmartPointer<vtkCylinderSource> cylinderSource = vtkSmartPointer<vtkCylinderSource>::New();
cylinderSource->SetHeight( zpos_top - zpos_bottom + 2*thickness );
cylinderSource->SetRadius( r.get_d() );
cylinderSource->SetResolution( 10 ); // FIXME
vtkSmartPointer<vtkTransformPolyDataFilter> transformPolyDataFilter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
transformPolyDataFilter->SetInputConnection( cylinderSource->GetOutputPort() );
vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New();
t->RotateX(90); // orient cylinder along z-axis
t->PostMultiply();
t->Translate( x.get_d(), y.get_d(), (zpos_top + zpos_bottom) / 2.0 );
transformPolyDataFilter->SetTransform(t);
appendPolyData->AddInput( transformPolyDataFilter->GetOutput() );
}
// triangulate drills
vtkSmartPointer<vtkTriangleFilter> triangleFilter1 = vtkSmartPointer<vtkTriangleFilter>::New();
triangleFilter1->SetInputConnection( appendPolyData->GetOutputPort() );
// calculate difference (drill into PCB)
filter->SetOperationToDifference();
filter->SetInput( 0, polyData );
filter->SetInputConnection( 1, triangleFilter1->GetOutputPort() );
vtkSmartPointer<vtkPolyDataMapper> m = vtkSmartPointer<vtkPolyDataMapper>::New();
m->SetInputConnection( filter->GetOutputPort() );
m->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> a = vtkSmartPointer<vtkActor>::New();
a->SetMapper( m );
a->GetProperty()->SetColor( m_laminateColor.redF(), m_laminateColor.greenF(), m_laminateColor.blueF() );
m_vtkRenderer->RemoveActor(actor);
m_vtkRenderer->AddActor(a);
}
}
#else
#warning DISABLED Excellon 3D rendering. VTK too old. Need at least VTK-5.10.
#endif
}
void MainWindow::on_treeWidget_customContextMenuRequested(const QPoint &pos)
{
QTreeWidgetItem* item = ui->treeWidget->itemAt(pos);
if (!item)
return;
if (!item->parent())
return; // clicked at top level item
int id = item->data(0,Qt::UserRole).toInt();
Type type = (Type)item->data(0,Qt::UserRole+1).toInt();
QMenu menu;
menu.addAction( ui->actionRemove_File );
QAction* actionProperties = menu.addAction( tr("Properties") );
actionProperties->setEnabled( false );
if (type == Type_PickPlaceFile) {
actionProperties->setEnabled( true );
}
QAction* selected = menu.exec( ui->treeWidget->mapToGlobal(pos) );
if (selected == actionProperties) {
if (type == Type_PickPlaceFile) {
CentroidDialog dlg;
dlg.setCSV( m_Centroid.value(id) );
dlg.exec();
updateView();
}
}
}
void MainWindow::on_actionZoom_In_triggered()
{
if (ui->tabWidget->currentIndex() == 0) {
// 3D view activated
} else {
// 2D view activated
ui->graphicsView->scale( 2, 2 );
}
}
void MainWindow::on_actionZoom_Out_triggered()
{
if (ui->tabWidget->currentIndex() == 0) {
// 3D view activated
m_vtkRenderer->GetActiveCamera();
} else {
// 2D view activated
ui->graphicsView->scale( 0.5, 0.5 );
}
}
void MainWindow::on_actionZoom_Fit_triggered()
{
if (ui->tabWidget->currentIndex() == 0) {
// 3D view activated
m_vtkRenderer->ResetCamera();
ui->qvtkWidget->GetRenderWindow()->Render();
} else {
// 2D view activated
ui->graphicsView->fitInView( m_scene->itemsBoundingRect(), Qt::KeepAspectRatio );
}
}
void MainWindow::on_actionExport_to_Freecad_triggered()
{
QString filename = QFileDialog::getSaveFileName( this, "Export to Freecad Python script", QString(), "Python scripts (*.py)" );
if (filename.isEmpty())
return;
QFileInfo fi(filename);
QFile file(fi.absoluteFilePath());
if (!file.open(QFile::WriteOnly)) {
QMessageBox::information( this, "Export file", "opening file failed." );
return;
}
QTextStream stream(&file);
// write header
stream << "# -*- coding: UTF-8 -*-" << endl;
stream << endl;
stream << "# start from freecad python console with:" << endl;
stream << "# execfile( '" + fi.absoluteFilePath() + "' )" << endl;
stream << "import Part, FreeCAD, math" << endl;
stream << "from FreeCAD import Base" << endl;
stream << endl;
for (int i=0; i<m_treeCentroid->childCount(); i++) {
render_Centroid_into_Freecad( stream, m_treeCentroid->child(i)->data(0,Qt::UserRole).toInt(), 0, -m_laminateHeight, m_metalThickness );
}
}
void MainWindow::render_Centroid_into_Freecad( QTextStream& stream, int num, double zpos_top, double zpos_bottom, double thickness )
{
if (!m_Centroid.contains(num))
return;
Centroid* centroid = m_Centroid.value(num);
QList<CentroidLine> lines = centroid->lines();
for (int i=0; i<lines.size(); i++) {
CentroidLine line = lines.at(i);
render_Centroid_into_Freecad_importItem( stream, line, zpos_top, zpos_bottom, thickness );
}
}
//! \brief The heart of 3D component placement.
//! This function searches for the correct component (based on the information in Pick&Place data \c line).
//! It creates a stream of Freecad Python commands to import the component at the correct position and orientation.
void MainWindow::render_Centroid_into_Freecad_importItem( QTextStream& stream, CentroidLine line, double zpos_top, double zpos_bottom, double thickness )
{
QString Description = line.Description; // this identified the component
//stream << "Part.insert(\"/home/sebastian/src/pickAndPlaceMachine/rs-online.com/STEP AP214/17433.stp\",\"X1\")" << endl;
// stream << "s = Part.Shape()" << endl;
// stream << "s.read(\"/home/sebastian/src/pickAndPlaceMachine/rs-online.com/STEP AP214/17433.stp\")" << endl;
// stream << "Part.show(s)" << endl;
stream << "shapeobj = FreeCAD.activeDocument().addObject(\"Part::Feature\",\"X1\")" << endl;
stream << "s = Part.Shape()" << endl;
stream << "s.read(\"/home/sebastian/src/pickAndPlaceMachine/rs-online.com/STEP AP214/17433.stp\")" << endl;
stream << "shapeobj.Shape = s" << endl;
stream << "FreeCAD.activeDocument().recompute()" << endl;
}
void MainWindow::on_actionRemove_File_triggered()
{
QTreeWidgetItem* item = ui->treeWidget->currentItem();
if (!item || !item->parent())
return;
int id = item->data(0,Qt::UserRole).toInt();
Type type = (Type)item->data(0,Qt::UserRole+1).toInt();
switch (type) {
case Type_PickPlaceFile:
{
item->parent()->removeChild(item);
m_Centroid.remove(id);
break;
}
case Type_ExcellonFile:
{
item->parent()->removeChild(item);
m_Excellon.remove(id);
break;
}
case Type_GerberFile:
{
item->parent()->removeChild(item);
m_GerberImporter.remove(id);
break;
}
} // switch
updateView();
}
void MainWindow::on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
if (!current || !current->parent())
ui->actionRemove_File->setEnabled( false );
else
ui->actionRemove_File->setEnabled( true );
}
void MainWindow::on_actionImportGerber_triggered()
{
QStringList filenames = QFileDialog::getOpenFileNames( this, "Select a Gerber file to import" );
if (filenames.isEmpty())
return;
foreach( QString filename, filenames )
import_Gerber( filename );
updateView();
}
void MainWindow::on_actionImportCentroid_triggered()
{
QStringList filenames = QFileDialog::getOpenFileNames( this, "Select a Centroid file to import", "", "Centroid files (*.csv);;All files (*.*)" );
if (filenames.isEmpty())
return;
foreach( QString filename, filenames )
import_Centroid( filename );
updateView();
}
void MainWindow::on_actionImportExcellon_triggered()
{
QStringList filenames = QFileDialog::getOpenFileNames( this, "Select an Excellon file to import", "", "Excellon files (*.drd);;All files (*.*)" );
if (filenames.isEmpty())
return;
foreach( QString filename, filenames )
import_Excellon( filename );
updateView();
}
bool MainWindow::import_Centroid( QString filename )
{
QFileInfo fi(filename);
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText( 0, QDir::toNativeSeparators(fi.fileName()) );
item->setToolTip( 0, QDir::toNativeSeparators(filename) );
Centroid* centroid = new Centroid;
bool ok = centroid->analyze( filename );
if (!ok) {
// automatic column determination failed; let the user assign the colunms
CentroidDialog dlg;
dlg.setCSV( centroid );
dlg.exec();
}
QList<int> keys = m_Centroid.keys();
int id = 0;
if (!keys.isEmpty()) {
qSort(keys);
id = keys.last()+1;
}
m_Centroid[id] = centroid;
item->setData( 0, Qt::UserRole, id );
item->setData( 0, Qt::UserRole+1, Type_PickPlaceFile ); // Pick&Place file
item->setData( 0, Qt::UserRole+2, filename );
m_treeCentroid->addChild(item);
return true;
}
bool MainWindow::import_Excellon( QString filename )
{
QFileInfo fi(filename);
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText( 0, QDir::toNativeSeparators(fi.fileName()) );
item->setToolTip( 0, QDir::toNativeSeparators(filename) );
ExcellonImporter* excellon = new ExcellonImporter;
bool ok = excellon->import( filename );
QList<int> keys = m_Excellon.keys();
int id = 0;
if (!keys.isEmpty()) {
qSort(keys);
id = keys.last()+1;
}
m_Excellon[id] = excellon;
item->setData( 0, Qt::UserRole, id );
item->setData( 0, Qt::UserRole+1, Type_ExcellonFile ); // Excellon file
item->setData( 0, Qt::UserRole+2, filename );
m_treeExcellon->addChild(item);
return true;
}
bool MainWindow::import_Gerber( QString filename )
{
QFileInfo fi(filename);
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText( 0, QDir::toNativeSeparators(fi.fileName()) );
item->setToolTip( 0, QDir::toNativeSeparators(filename) );
if ((fi.suffix().toLower() == "outline") || (fi.suffix().toLower() == "dim")) {
m_treeLayerOutline->addChild(item);
} else if ((fi.suffix().toLower() == "top") || (fi.suffix().toLower() == "positop") || (fi.suffix().toLower() == "cmp")) {
m_treeLayerTop->addChild(item);
} else if ((fi.suffix().toLower() == "bot") || (fi.suffix().toLower() == "posibot") || (fi.suffix().toLower() == "sol")) {
m_treeLayerBottom->addChild(item);
} else {
return false;
}
GerberImporter importer;
importer.setLogWidget( ui->log );
bool ok = importer.import( filename );
if (!ok) {
delete item;
return false;
}
QList<int> keys = m_GerberImporter.keys();
int id = 0;
if (!keys.isEmpty()) {
qSort(keys);
id = keys.last()+1;
}
m_GerberImporter[id] = importer;
item->setData( 0, Qt::UserRole, id );
item->setData( 0, Qt::UserRole+1, Type_GerberFile ); // Gerber file
return true;
}