~ubuntu-branches/ubuntu/precise/fritzing/precise

« back to all changes in this revision

Viewing changes to src/svg/gerbergenerator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Georges Khaznadar
  • Date: 2011-08-26 10:11:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110826101105-w5hmn7zcf93ig5v6
Tags: 0.6.3b-1
* upgrapded to the newer upstream version
* parameters of the function GraphicsUtils::distanceFromLine in 
  src/svg/groundplanegenerator.cpp:767 are now declared as doubles,
  which Closes: #636441
* the new version fixes src/utils/folderutils.cpp, which
  Closes: #636061

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************
 
2
 
 
3
Part of the Fritzing project - http://fritzing.org
 
4
Copyright (c) 2007-2011 Fachhochschule Potsdam - http://fh-potsdam.de
 
5
 
 
6
Fritzing is free software: you can redistribute it and/or modify
 
7
it under the terms of the GNU General Public License as published by
 
8
the Free Software Foundation, either version 3 of the License, or
 
9
(at your option) any later version.
 
10
 
 
11
Fritzing is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with Fritzing.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
********************************************************************
 
20
 
 
21
$Revision: 5396 $:
 
22
$Author: cohen@irascible.com $:
 
23
$Date: 2011-08-15 01:36:25 +0200 (Mon, 15 Aug 2011) $
 
24
 
 
25
********************************************************************/
 
26
 
 
27
#include <QMessageBox>
 
28
#include <QFileDialog>
 
29
#include <QSvgRenderer>
 
30
 
 
31
#include "gerbergenerator.h"
 
32
#include "../debugdialog.h"
 
33
#include "../fsvgrenderer.h"
 
34
#include "../sketch/pcbsketchwidget.h"
 
35
#include "svgfilesplitter.h"
 
36
#include "groundplanegenerator.h"
 
37
#include "../utils/graphicsutils.h"
 
38
#include "../utils/textutils.h"
 
39
#include "../utils/folderutils.h"
 
40
#include "../items/logoitem.h"
 
41
 
 
42
static QRegExp AaCc("[aAcCqQtTsS]");
 
43
 
 
44
////////////////////////////////////////////
 
45
 
 
46
void GerberGenerator::exportToGerber(const QString & filename, const QString & exportDir, ItemBase * board, PCBSketchWidget * sketchWidget, bool displayMessageBoxes) 
 
47
{
 
48
        if (board == NULL) {
 
49
                board = sketchWidget->findBoard();
 
50
        }
 
51
        if (board == NULL) {
 
52
                DebugDialog::debug("board not found");
 
53
                return;
 
54
        }
 
55
 
 
56
        LayerList viewLayerIDs = ViewLayer::copperLayers(ViewLayer::Bottom);
 
57
        int copperInvalidCount = doCopper(board, sketchWidget, viewLayerIDs, "Copper0", "_copperBottom.gbl", filename, exportDir, displayMessageBoxes);
 
58
 
 
59
        if (sketchWidget->boardLayers() == 2) {
 
60
                viewLayerIDs = ViewLayer::copperLayers(ViewLayer::Top);
 
61
                copperInvalidCount += doCopper(board, sketchWidget, viewLayerIDs, "Copper1", "_copperTop.gtl", filename, exportDir, displayMessageBoxes);
 
62
        }
 
63
 
 
64
        LayerList maskLayerIDs = ViewLayer::maskLayers(ViewLayer::Bottom);
 
65
        int maskInvalidCount = doMask(maskLayerIDs, "Mask0", "_maskBottom.gbs", board, sketchWidget, filename, exportDir, displayMessageBoxes);
 
66
 
 
67
        if (sketchWidget->boardLayers() == 2) {
 
68
                maskLayerIDs = ViewLayer::maskLayers(ViewLayer::Top);
 
69
                maskInvalidCount += doMask(maskLayerIDs, "Mask1", "_maskTop.gts", board, sketchWidget, filename, exportDir, displayMessageBoxes);
 
70
        }
 
71
 
 
72
    LayerList silkLayerIDs;
 
73
    silkLayerIDs << ViewLayer::Silkscreen1  << ViewLayer::Silkscreen1Label;
 
74
        int silkInvalidCount = doSilk(silkLayerIDs, "Silk1", "_silkTop.gto", board, sketchWidget, filename, exportDir, displayMessageBoxes);
 
75
    silkLayerIDs.clear();
 
76
    silkLayerIDs << ViewLayer::Silkscreen0  << ViewLayer::Silkscreen0Label;
 
77
        silkInvalidCount += doSilk(silkLayerIDs, "Silk0", "_silkBottom.gbo", board, sketchWidget, filename, exportDir, displayMessageBoxes);
 
78
 
 
79
    // now do it for the outline/contour
 
80
    LayerList outlineLayerIDs;
 
81
    outlineLayerIDs << ViewLayer::Board;
 
82
        QSizeF imageSize;
 
83
        bool empty;
 
84
        QString svgOutline = sketchWidget->renderToSVG(FSvgRenderer::printerScale(), outlineLayerIDs, outlineLayerIDs, true, imageSize, board, GraphicsUtils::StandardFritzingDPI, false, false, false, empty);
 
85
    if (svgOutline.isEmpty()) {
 
86
        displayMessage(QObject::tr("outline is empty"), displayMessageBoxes);
 
87
        return;
 
88
    }
 
89
 
 
90
        QXmlStreamReader streamReader(svgOutline);
 
91
        QSizeF svgSize = FSvgRenderer::parseForWidthAndHeight(streamReader);
 
92
 
 
93
        QDomDocument domDocument;
 
94
        QString errorStr;
 
95
        int errorLine;
 
96
        int errorColumn;
 
97
    bool result = domDocument.setContent(svgOutline, &errorStr, &errorLine, &errorColumn);
 
98
    if (!result) {
 
99
                displayMessage(QObject::tr("outline file export failure (1)"), displayMessageBoxes);
 
100
        return;
 
101
    }
 
102
 
 
103
    // create copper0 gerber from svg
 
104
    SVG2gerber outlineGerber;
 
105
        int outlineInvalidCount = outlineGerber.convert(svgOutline, sketchWidget->boardLayers() == 2, "contour", SVG2gerber::ForOutline, svgSize * GraphicsUtils::StandardFritzingDPI);
 
106
        if (outlineInvalidCount > 0) {
 
107
                outlineInvalidCount = 0;
 
108
                svgOutline = clipToBoard(svgOutline, board, "board", SVG2gerber::ForOutline);
 
109
                outlineInvalidCount = outlineGerber.convert(svgOutline, sketchWidget->boardLayers() == 2, "contour", SVG2gerber::ForOutline, svgSize * GraphicsUtils::StandardFritzingDPI);
 
110
        }
 
111
 
 
112
    // contour / board outline
 
113
    QString contourFile = exportDir + "/" +
 
114
                          QFileInfo(filename).fileName().remove(FritzingSketchExtension)
 
115
                          + "_contour.gm1";
 
116
    QFile contourOut(contourFile);
 
117
        if (!contourOut.open(QIODevice::WriteOnly | QIODevice::Text)) {
 
118
                displayMessage(QObject::tr("outline file export failure (2)"), displayMessageBoxes);
 
119
                return;
 
120
        }
 
121
 
 
122
    QTextStream contourStream(&contourOut);
 
123
    contourStream << outlineGerber.getGerber();
 
124
 
 
125
        doDrill(board, sketchWidget, filename, exportDir, displayMessageBoxes);
 
126
 
 
127
        if (outlineInvalidCount > 0 || silkInvalidCount > 0 || copperInvalidCount > 0 || maskInvalidCount) {
 
128
                QString s;
 
129
                if (outlineInvalidCount > 0) s += QObject::tr("the board outline layer, ");
 
130
                if (silkInvalidCount > 0) s += QObject::tr("silkscreen layer(s), ");
 
131
                if (copperInvalidCount > 0) s += QObject::tr("copper layer(s), ");
 
132
                if (maskInvalidCount > 0) s += QObject::tr("mask layer(s), ");
 
133
                s.chop(2);
 
134
                displayMessage(QObject::tr("Unable to translate svg curves in ").arg(s), displayMessageBoxes);
 
135
        }
 
136
 
 
137
}
 
138
 
 
139
int GerberGenerator::doCopper(ItemBase * board, PCBSketchWidget * sketchWidget, LayerList & viewLayerIDs, const QString & copperName, const QString & copperSuffix, const QString & filename, const QString & exportDir, bool displayMessageBoxes) 
 
140
{
 
141
        QSizeF imageSize;
 
142
        bool empty;
 
143
        QString svg = sketchWidget->renderToSVG(FSvgRenderer::printerScale(), viewLayerIDs, viewLayerIDs, true, imageSize, board, GraphicsUtils::StandardFritzingDPI, false, false, false, empty);
 
144
        if (svg.isEmpty()) {
 
145
                displayMessage(QObject::tr("%1 file export failure (1)").arg(copperName), displayMessageBoxes);
 
146
                return 0;
 
147
        }
 
148
 
 
149
        QXmlStreamReader streamReader(svg);
 
150
        QSizeF svgSize = FSvgRenderer::parseForWidthAndHeight(streamReader);
 
151
 
 
152
        svg = clipToBoard(svg, board, copperName, SVG2gerber::ForNormal);
 
153
        if (svg.isEmpty()) {
 
154
                displayMessage(QObject::tr("%1 file export failure (3)").arg(copperName), displayMessageBoxes);
 
155
                return 0;
 
156
        }
 
157
 
 
158
    // create copper gerber from svg
 
159
    SVG2gerber copperGerber;
 
160
        int copperInvalidCount = copperGerber.convert(svg, sketchWidget->boardLayers() == 2, copperName, SVG2gerber::ForNormal, svgSize * GraphicsUtils::StandardFritzingDPI);
 
161
 
 
162
    QString copperFile = exportDir + "/" +
 
163
                          QFileInfo(filename).fileName().remove(FritzingSketchExtension)
 
164
                          + copperSuffix;
 
165
    QFile copperOut(copperFile);
 
166
        if (!copperOut.open(QIODevice::WriteOnly | QIODevice::Text)) {
 
167
                displayMessage(QObject::tr("%1 file export failure (3)").arg(copperName), displayMessageBoxes);
 
168
                return 0;
 
169
        }
 
170
 
 
171
    QTextStream copperStream(&copperOut);
 
172
    copperStream << copperGerber.getGerber();
 
173
        copperStream.flush();
 
174
        copperOut.close();
 
175
 
 
176
        return copperInvalidCount;
 
177
}
 
178
 
 
179
 
 
180
int GerberGenerator::doSilk(LayerList silkLayerIDs, const QString & silkName, const QString & gerberSuffix, ItemBase * board, PCBSketchWidget * sketchWidget, const QString & filename, const QString & exportDir, bool displayMessageBoxes ) 
 
181
{
 
182
        QSizeF imageSize;
 
183
        bool empty;
 
184
        QString svgSilk = sketchWidget->renderToSVG(FSvgRenderer::printerScale(), silkLayerIDs, silkLayerIDs, true, imageSize, board, GraphicsUtils::StandardFritzingDPI, false, false, false, empty);
 
185
    if (svgSilk.isEmpty()) {
 
186
                displayMessage(QObject::tr("silk file export failure (1)"), displayMessageBoxes);
 
187
        return 0;
 
188
    }
 
189
 
 
190
        if (empty) {
 
191
                // don't bother with file
 
192
                return 0;
 
193
        }
 
194
 
 
195
        //QFile f(silkName + "original.svg");
 
196
        //f.open(QFile::WriteOnly);
 
197
        //QTextStream fs(&f);
 
198
        //fs << svgSilk;
 
199
        //f.close();
 
200
 
 
201
        QXmlStreamReader streamReader(svgSilk);
 
202
        QSizeF svgSize = FSvgRenderer::parseForWidthAndHeight(streamReader);
 
203
 
 
204
        svgSilk = clipToBoard(svgSilk, board, silkName, SVG2gerber::ForNormal);
 
205
        if (svgSilk.isEmpty()) {
 
206
                displayMessage(QObject::tr("silk export failure"), displayMessageBoxes);
 
207
                return 0;
 
208
        }
 
209
 
 
210
        //QFile f2(silkName + "clipped.svg");
 
211
        //f2.open(QFile::WriteOnly);
 
212
        //QTextStream fs2(&f2);
 
213
        //fs2 << svgSilk;
 
214
        //f2.close();
 
215
 
 
216
 
 
217
    // create silk gerber from svg
 
218
    SVG2gerber silkGerber;
 
219
        int silkInvalidCount = silkGerber.convert(svgSilk, sketchWidget->boardLayers() == 2, silkName, SVG2gerber::ForNormal, svgSize * GraphicsUtils::StandardFritzingDPI);
 
220
 
 
221
    QString silkFile = exportDir + "/" +
 
222
                          QFileInfo(filename).fileName().remove(FritzingSketchExtension)
 
223
                          + gerberSuffix;
 
224
    QFile silkOut(silkFile);
 
225
        if (!silkOut.open(QIODevice::WriteOnly | QIODevice::Text)) {
 
226
                displayMessage(QObject::tr("silk file export failure (2)"), displayMessageBoxes);
 
227
                return 0;
 
228
        }
 
229
 
 
230
    QTextStream silkStream(&silkOut);
 
231
    silkStream << silkGerber.getGerber();
 
232
        silkStream.flush();
 
233
        silkOut.close();
 
234
 
 
235
        return silkInvalidCount;
 
236
}
 
237
 
 
238
 
 
239
int GerberGenerator::doDrill(ItemBase * board, PCBSketchWidget * sketchWidget, const QString & filename, const QString & exportDir, bool displayMessageBoxes) 
 
240
{
 
241
    LayerList drillLayerIDs;
 
242
    drillLayerIDs << ViewLayer::Copper0;
 
243
 
 
244
        QSizeF imageSize;
 
245
        bool empty;
 
246
        QString svgDrill = sketchWidget->renderToSVG(FSvgRenderer::printerScale(), drillLayerIDs, drillLayerIDs, true, imageSize, board, GraphicsUtils::StandardFritzingDPI, false, false, false, empty);
 
247
    if (svgDrill.isEmpty()) {
 
248
                displayMessage(QObject::tr("drill file export failure (1)"), displayMessageBoxes);
 
249
        return 0;
 
250
    }
 
251
 
 
252
        if (empty) {
 
253
                // don't bother with file
 
254
                return 0;
 
255
        }
 
256
 
 
257
        QXmlStreamReader streamReader(svgDrill);
 
258
        QSizeF svgSize = FSvgRenderer::parseForWidthAndHeight(streamReader);
 
259
 
 
260
        svgDrill = clipToBoard(svgDrill, board, "Copper0", SVG2gerber::ForDrill);
 
261
        if (svgDrill.isEmpty()) {
 
262
                displayMessage(QObject::tr("drill export failure"), displayMessageBoxes);
 
263
                return 0;
 
264
        }
 
265
 
 
266
    // create silk gerber from svg
 
267
    SVG2gerber drillGerber;
 
268
        int drillInvalidCount = drillGerber.convert(svgDrill, sketchWidget->boardLayers() == 2, "drill", SVG2gerber::ForDrill, svgSize * GraphicsUtils::StandardFritzingDPI);
 
269
 
 
270
 
 
271
                // drill file
 
272
        QString drillFile = exportDir + "/" +
 
273
                                                          QFileInfo(filename).fileName().remove(FritzingSketchExtension)
 
274
                                                          + "_drill.txt";
 
275
        QFile drillOut(drillFile);
 
276
        if (!drillOut.open(QIODevice::WriteOnly | QIODevice::Text)) {
 
277
                displayMessage(QObject::tr("drill file export failure (5)"), displayMessageBoxes);
 
278
                return 0;
 
279
        }
 
280
 
 
281
        QTextStream drillStream(&drillOut);
 
282
        drillStream << drillGerber.getGerber();
 
283
        drillStream.flush();
 
284
        drillOut.close();
 
285
 
 
286
        return drillInvalidCount;
 
287
}
 
288
 
 
289
int GerberGenerator::doMask(LayerList maskLayerIDs, const QString &maskName, const QString & gerberSuffix, ItemBase * board, PCBSketchWidget * sketchWidget, const QString & filename, const QString & exportDir, bool displayMessageBoxes ) 
 
290
{
 
291
        // don't want these in the mask laqyer
 
292
        QList<ItemBase *> copperLogoItems;
 
293
        foreach (QGraphicsItem * item, sketchWidget->items()) {
 
294
                CopperLogoItem * logoItem = dynamic_cast<CopperLogoItem *>(item);
 
295
                if (logoItem && logoItem->isVisible()) {
 
296
                        copperLogoItems.append(logoItem);
 
297
                        logoItem->setVisible(false);
 
298
                }
 
299
        }
 
300
 
 
301
        QSizeF imageSize;
 
302
        bool empty;
 
303
        QString svgMask = sketchWidget->renderToSVG(FSvgRenderer::printerScale(), maskLayerIDs, maskLayerIDs, true, imageSize, board, GraphicsUtils::StandardFritzingDPI, false, false, false, empty);
 
304
    if (svgMask.isEmpty()) {
 
305
                displayMessage(QObject::tr("mask file export failure (1)"), displayMessageBoxes);
 
306
        return 0;
 
307
    }
 
308
 
 
309
        foreach (ItemBase * logoItem, copperLogoItems) {
 
310
                logoItem->setVisible(true);
 
311
        }
 
312
 
 
313
        if (empty) {
 
314
                // don't bother with file
 
315
                return 0;
 
316
        }
 
317
 
 
318
        QDomDocument domDocument;
 
319
        QString errorStr;
 
320
        int errorLine;
 
321
        int errorColumn;
 
322
        bool result = domDocument.setContent(svgMask, &errorStr, &errorLine, &errorColumn);
 
323
        if (!result) {
 
324
                displayMessage(QObject::tr("%1 file export failure (2)").arg(maskName), displayMessageBoxes);
 
325
                return 0;
 
326
        }
 
327
 
 
328
        QXmlStreamReader streamReader(svgMask);
 
329
        QSizeF svgSize = FSvgRenderer::parseForWidthAndHeight(streamReader);
 
330
 
 
331
        svgMask = clipToBoard(svgMask, board, maskName, SVG2gerber::ForMask);
 
332
        if (svgMask.isEmpty()) {
 
333
                displayMessage(QObject::tr("mask export failure"), displayMessageBoxes);
 
334
                return 0;
 
335
        }
 
336
 
 
337
    // create mask gerber from svg
 
338
    SVG2gerber maskGerber;
 
339
        int maskInvalidCount = maskGerber.convert(svgMask, sketchWidget->boardLayers() == 2, maskName, SVG2gerber::ForMask, svgSize * GraphicsUtils::StandardFritzingDPI);
 
340
 
 
341
    QString maskFile = exportDir + "/" +
 
342
                          QFileInfo(filename).fileName().remove(FritzingSketchExtension)
 
343
                          + gerberSuffix;
 
344
    QFile maskOut(maskFile);
 
345
        if (!maskOut.open(QIODevice::WriteOnly | QIODevice::Text)) {
 
346
                displayMessage(QObject::tr("mask file export failure (2)"), displayMessageBoxes);
 
347
                return 0;
 
348
        }
 
349
 
 
350
    QTextStream maskStream(&maskOut);
 
351
    maskStream << maskGerber.getGerber();
 
352
        maskStream.flush();
 
353
        maskOut.close();
 
354
 
 
355
        return maskInvalidCount;
 
356
}
 
357
 
 
358
void GerberGenerator::displayMessage(const QString & message, bool displayMessageBoxes) {
 
359
        // don't use QMessageBox if running conversion as a service
 
360
        if (displayMessageBoxes) {
 
361
                QMessageBox::warning(NULL, QObject::tr("Fritzing"), message);
 
362
                return;
 
363
        }
 
364
 
 
365
        DebugDialog::debug(message);
 
366
}
 
367
 
 
368
QString GerberGenerator::clipToBoard(QString svgString, ItemBase * board, const QString & layerName, SVG2gerber::ForWhy forWhy) {
 
369
        QDomDocument domDocument1;
 
370
        QString errorStr;
 
371
        int errorLine;
 
372
        int errorColumn;
 
373
        bool result = domDocument1.setContent(svgString, &errorStr, &errorLine, &errorColumn);
 
374
        if (!result) {
 
375
                return "";
 
376
        }
 
377
 
 
378
        QDomDocument domDocument2;
 
379
        domDocument2.setContent(svgString, &errorStr, &errorLine, &errorColumn);
 
380
 
 
381
        bool anyConverted = false;
 
382
    if (TextUtils::squashElement(domDocument1, "text", "", QRegExp())) {
 
383
        anyConverted = true; 
 
384
        }
 
385
 
 
386
        // gerber can't handle ellipses that are rotated, so cull them all
 
387
    if (TextUtils::squashElement(domDocument1, "ellipse", "", QRegExp())) {
 
388
                anyConverted = true;
 
389
    }
 
390
 
 
391
    if (TextUtils::squashElement(domDocument1, "rect", "rx", QRegExp())) {
 
392
                anyConverted = true;
 
393
    }
 
394
 
 
395
    if (TextUtils::squashElement(domDocument1, "rect", "ry", QRegExp())) {
 
396
                anyConverted = true;
 
397
    }
 
398
 
 
399
        // gerber can't handle paths with curves
 
400
    if (TextUtils::squashElement(domDocument1, "path", "d", AaCc)) {
 
401
                anyConverted = true;
 
402
    }
 
403
 
 
404
        QVector <QDomElement> leaves1;
 
405
        int transformCount1 = 0;
 
406
        QDomElement e1 = domDocument1.documentElement();
 
407
        TextUtils::collectLeaves(e1, transformCount1, leaves1);
 
408
 
 
409
        QVector <QDomElement> leaves2;
 
410
        int transformCount2 = 0;
 
411
        QDomElement e2 = domDocument2.documentElement();
 
412
        TextUtils::collectLeaves(e2, transformCount2, leaves2);
 
413
 
 
414
        double res = GraphicsUtils::StandardFritzingDPI;
 
415
        QRectF source = board->boundingRect();
 
416
        int twidth = res * source.width() / FSvgRenderer::printerScale();
 
417
        int theight = res * source.height() / FSvgRenderer::printerScale();
 
418
 
 
419
        svgString = TextUtils::removeXMLEntities(domDocument1.toString());
 
420
        QXmlStreamReader reader(svgString);
 
421
        QSvgRenderer renderer(&reader);
 
422
        bool anyClipped = false;
 
423
        for (int i = 0; i < transformCount1; i++) {
 
424
                QString n = QString::number(i);
 
425
                QRectF bounds = renderer.boundsOnElement(n);
 
426
                QMatrix m = renderer.matrixForElement(n);
 
427
                QDomElement element = leaves1.at(i);
 
428
                QString ms = element.attribute("transform");
 
429
                if (!ms.isEmpty()) {
 
430
                        m *= TextUtils::transformStringToMatrix(ms);
 
431
                }
 
432
                QRectF mBounds = m.mapRect(bounds);
 
433
                if (mBounds.left() < 0 || mBounds.top() < 0 || bounds.right() > twidth || bounds.bottom() > theight) {
 
434
                        // element is outside of bounds, squash it so it will be clipped
 
435
                        element.setTagName("g");
 
436
                        anyClipped = anyConverted = true;
 
437
                }       
 
438
        }
 
439
 
 
440
        if (anyClipped) {
 
441
                svgString = TextUtils::removeXMLEntities(domDocument1.toString());
 
442
        }
 
443
 
 
444
    if (anyConverted) {
 
445
                for (int i = 0; i < transformCount1; i++) {
 
446
                        QDomElement element1 = leaves1.at(i);
 
447
                        if (element1.tagName() != "g") {
 
448
                                QDomElement element2 = leaves2.at(i);
 
449
                                element2.setTagName("g");
 
450
                        }
 
451
                }
 
452
                
 
453
                QString svg = TextUtils::removeXMLEntities(domDocument2.toString());
 
454
 
 
455
                QSize imgSize(twidth, theight);
 
456
 
 
457
                // expand the svg to fill the space of the image
 
458
                QRegExp widthFinder("width=[^i]+in.");
 
459
                int ix = widthFinder.indexIn(svg);
 
460
                if (ix >= 0) {
 
461
                        svg.replace(ix, widthFinder.cap(0).length(), QString("width=\"%1px\"").arg(twidth));
 
462
                }
 
463
                QRegExp heightFinder("height=[^i]+in.");
 
464
                ix = heightFinder.indexIn(svg);
 
465
                if (ix > 0) {
 
466
                        svg.replace(ix, heightFinder.cap(0).length(), QString("height=\"%1px\"").arg(theight));
 
467
                }
 
468
 
 
469
                QStringList exceptions;
 
470
                exceptions << "none" << "";
 
471
                QString toColor("#000000");
 
472
                QByteArray svgByteArray;
 
473
                SvgFileSplitter::changeColors(svg, toColor, exceptions, svgByteArray);
 
474
 
 
475
                QImage image(imgSize, QImage::Format_RGB32);
 
476
                image.fill(0xffffffff);
 
477
                image.setDotsPerMeterX(res * GraphicsUtils::InchesPerMeter);
 
478
                image.setDotsPerMeterY(res * GraphicsUtils::InchesPerMeter);
 
479
                QRectF target(0, 0, twidth, theight);
 
480
 
 
481
                QSvgRenderer renderer(svgByteArray);
 
482
                QPainter painter;
 
483
                painter.begin(&image);
 
484
                renderer.render(&painter, target);
 
485
                painter.end();
 
486
                image.invertPixels();                           // need white pixels on a black background for GroundPlaneGenerator
 
487
                //image.save("output.png");
 
488
 
 
489
                GroundPlaneGenerator gpg;
 
490
                if (forWhy == SVG2gerber::ForOutline) {
 
491
                        int tinyWidth = source.width() / FSvgRenderer::printerScale();
 
492
                        int tinyHeight = source.height() / FSvgRenderer::printerScale();
 
493
                        QRectF tinyTarget(0, 0, tinyWidth, tinyHeight);
 
494
                        QImage tinyImage(tinyWidth, tinyHeight, QImage::Format_RGB32);
 
495
                        QPainter painter;
 
496
                        painter.begin(&tinyImage);
 
497
                        renderer.render(&painter, tinyTarget);
 
498
                        painter.end();
 
499
                        tinyImage.invertPixels();                               // need white pixels on a black background for GroundPlaneGenerator
 
500
                        gpg.scanOutline(image, image.width(), image.height(), GraphicsUtils::StandardFritzingDPI / res, GraphicsUtils::StandardFritzingDPI, "#ffffff", layerName, false, 1, false, QSizeF(0, 0), 0);
 
501
                }
 
502
                else {
 
503
                        gpg.scanImage(image, image.width(), image.height(), GraphicsUtils::StandardFritzingDPI / res, GraphicsUtils::StandardFritzingDPI, "#ffffff", layerName, false, 1, false, QSizeF(0, 0), 0);
 
504
                }
 
505
 
 
506
                if (gpg.newSVGs().count() > 0) {
 
507
                        QDomDocument doc;
 
508
                        TextUtils::mergeSvg(doc, svgString, "");
 
509
                        foreach (QString gsvg, gpg.newSVGs()) {
 
510
                                TextUtils::mergeSvg(doc, gsvg, "");
 
511
                        }
 
512
                        svgString = TextUtils::mergeSvgFinish(doc);
 
513
                }
 
514
        }
 
515
 
 
516
        return svgString;
 
517
}