Skip to content

Commit b124b94

Browse files
committed
[pdf_extract]Add support for parsing pin directions
Many datasheets present a table with the pin number, pin name and pin direction columns. This patch add support for the pin direction parsing.
1 parent 1f2553a commit b124b94

4 files changed

Lines changed: 73 additions & 7 deletions

File tree

src/pdf_extract/datasheet.cpp

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,29 @@ QRectF Datasheet::toGlobalPos(const QRectF &rect, Poppler::Page *page, int pageN
284284
return rect;
285285
}
286286

287+
// use lowercase letters in when extending the map!
288+
QMap <QString, Pin::ElectricalType> Datasheet::_pinDirectionToElectricalPinTypeMap = {
289+
{QStringLiteral("nc"), Pin::NotConnected},
290+
{QStringLiteral("n/c"), Pin::NotConnected},
291+
{QStringLiteral("oe"), Pin::OpenEmitter},
292+
{QStringLiteral("open-emitter"), Pin::OpenEmitter},
293+
{QStringLiteral("oc"), Pin::OpenCollector},
294+
{QStringLiteral("open-collector"), Pin::OpenCollector},
295+
{QStringLiteral("od"), Pin::OpenCollector},
296+
{QStringLiteral("open-drain"), Pin::OpenCollector},
297+
{QStringLiteral("tris"), Pin::Tristate},
298+
{QStringLiteral("sup"), Pin::PowerIn},
299+
{QStringLiteral("p"), Pin::Passive},
300+
{QStringLiteral("pas"), Pin::Passive},
301+
{QStringLiteral("passive"), Pin::Passive},
302+
{QStringLiteral("io"), Pin::Bidir},
303+
{QStringLiteral("i/o"), Pin::Bidir},
304+
{QStringLiteral("i"), Pin::Input},
305+
{QStringLiteral("in"), Pin::Input},
306+
{QStringLiteral("o"), Pin::Output},
307+
{QStringLiteral("out"), Pin::Output},
308+
};
309+
287310
QList<DatasheetPin *> Datasheet::extractPins(int numPage)
288311
{
289312
QList<DatasheetPin *> pins;
@@ -426,7 +449,7 @@ QList<DatasheetPin *> Datasheet::extractPins(int numPage)
426449
DatasheetPin *pin = new DatasheetPin();
427450
qreal dist = 999999999999;
428451
QPointF center = number->pos.center();
429-
DatasheetBox *assocLabel = NULL;
452+
DatasheetBox *pinNameLabel = nullptr;
430453

431454
for (int i = 0; i < _labels.count(); i++)
432455
{
@@ -445,27 +468,65 @@ QList<DatasheetPin *> Datasheet::extractPins(int numPage)
445468
if (newdist < dist)
446469
{
447470
dist = newdist;
448-
assocLabel = label;
471+
pinNameLabel = label;
449472
}
450473
}
451474

452-
if (assocLabel)
475+
if (pinNameLabel)
453476
{
454-
assocLabel->associated = true;
477+
pinNameLabel->associated = true;
455478
number->associated = true;
456479

457480
pin->pin = number->text.toInt();
458481
pin->numPos = number->pos;
459482
pin->numberBox = number;
460483

461-
pin->name = assocLabel->text;
484+
pin->name = pinNameLabel->text;
462485
pin->name.remove(QRegExp("\\([0-9]+\\)"));
463486
pin->name.remove(QRegExp(" +"));
464-
pin->nameBox = assocLabel;
487+
pin->nameBox = pinNameLabel;
465488

466489
pin->page = number->page;
467-
pin->pos = number->pos.united(assocLabel->pos);
490+
pin->pos = number->pos.united(pinNameLabel->pos);
491+
492+
dist = 999999999999;
493+
center = pinNameLabel->pos.center();
494+
DatasheetBox *directionLabel = nullptr;
495+
// loop over the labels to see if there is a pin direction label
496+
// in the following columns
497+
for (DatasheetBox *label : _labels) {
498+
if (label == pinNameLabel)
499+
continue;
500+
501+
if (label->page != number->page)
502+
continue;
503+
504+
// look for the closest text to the pin name
505+
qreal newdist = label->distanceToPoint(center);
506+
if (newdist < dist)
507+
{
508+
dist = newdist;
509+
510+
if (!DatasheetBox::isAlign(*label, *number)
511+
|| !DatasheetBox::isAlign(*label, *pinNameLabel))
512+
continue;
513+
514+
if (!_pinDirectionToElectricalPinTypeMap.contains(label->text.toLower()))
515+
continue;
468516

517+
if (label->associated)
518+
continue;
519+
520+
if (label == number)
521+
continue;
522+
directionLabel = label;
523+
}
524+
}
525+
526+
if (directionLabel) {
527+
directionLabel->associated = true;
528+
pin->electricalType = _pinDirectionToElectricalPinTypeMap.value(directionLabel->text.toLower());
529+
}
469530
pins.push_back(pin);
470531
}
471532
}

src/pdf_extract/datasheet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <QObject>
2525
#include <QImage>
26+
#include <QMap>
2627

2728
#include "datasheetpackage.h"
2829
#include "datasheetbox.h"
@@ -84,6 +85,7 @@ class DATASHEET_EXTRACTOR_EXPORT Datasheet : public QObject
8485
QList<DatasheetPackage *> _packages;
8586
bool _debug;
8687
bool _force;
88+
static QMap <QString, Pin::ElectricalType> _pinDirectionToElectricalPinTypeMap;
8789
};
8890

8991
#endif // DATASHEET_H

src/pdf_extract/datasheetpackage.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Component *DatasheetPackage::toComponent() const
4343
{
4444
Pin *pin = new Pin();
4545
pin->setName(dpin->name);
46+
pin->setElectricalType(dpin->electricalType);
4647
pin->setPadName(QString::number(dpin->pin));
4748
comp->addPin(pin);
4849
}

src/pdf_extract/datasheetpin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "pdf_extract_common.h"
2323

2424
#include "datasheetbox.h"
25+
#include "../kicad/model/pin.h"
2526

2627
#include <QString>
2728
#include <QRectF>
@@ -39,6 +40,7 @@ class DATASHEET_EXTRACTOR_EXPORT DatasheetPin
3940
QRectF pos;
4041
int page;
4142
QRectF numPos;
43+
Pin::ElectricalType electricalType = Pin::Input;
4244

4345
DatasheetBox *numberBox;
4446
DatasheetBox *nameBox;

0 commit comments

Comments
 (0)