-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPart.cpp
347 lines (296 loc) · 9.4 KB
/
Part.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
/*
Copyright (C) 2010-2011 Igor Izyumin
This file is part of xpcb.
xpcb 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.
xpcb 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 xpcb. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Part.h"
#include "Document.h"
#include "Log.h"
#include "smfontutil.h"
///////////////////// PART PIN /////////////////////
Pad PartPin::getPadOnLayer(const Layer& layer) const
{
return mPin->getPadOnLayer(mapLayer(layer));
}
Layer PartPin::mapLayer(const Layer& layer) const
{
if ((!layer.isCopper() && layer.type() != Layer::LAY_HOLE))
return Layer();
Part::SIDE side = mPart->side();
if ((layer == Layer::LAY_TOP_COPPER && side == Part::SIDE_TOP) ||
(layer == Layer::LAY_BOTTOM_COPPER && side == Part::SIDE_BOTTOM))
return Layer::LAY_START;
else if ((layer == Layer::LAY_TOP_COPPER && side == Part::SIDE_BOTTOM) ||
(layer == Layer::LAY_BOTTOM_COPPER && side == Part::SIDE_TOP))
return Layer::LAY_END;
else if (layer.isCopper())
return Layer::LAY_INNER;
else
return Layer::LAY_HOLE;
}
bool PartPin::testHit( const QPoint& pt, int dist, const Layer& layer) const
{
return mPin->testHit(mPart->transform().inverted().map(pt), dist,
mapLayer(layer));
}
QPoint PartPin::pos() const
{
Q_ASSERT(mPart);
Q_ASSERT(mPin);
return mPart->transform().map(mPin->pos());
}
bool PartPin::isSmt() const
{
return mPin->padstack()->isSmt();
}
QRect PartPin::bbox() const
{
return mPart->transform().mapRect(mPin->bbox());
}
QString PartPin::net() const
{
return mPart->doc()->netlist()->findNet(mPart->refdes(), name()).name();
}
void PartPin::draw(QPainter *painter, const Layer& layer) const
{
if (layer.type() == Layer::LAY_SELECTION)
{
painter->save();
painter->setBrush(Qt::NoBrush);
painter->setRenderHint(QPainter::Antialiasing, false);
QPen p = painter->pen();
p.setWidth(0);
painter->setPen(p);
painter->drawRect(mPin->bbox());
painter->restore();
}
if (!layer.isCopper() && layer.type() != Layer::LAY_HOLE) return;
mPin->draw(painter, mapLayer(layer));
}
PCBObjState PartPin::getState() const
{
return PCBObjState(new PartPinState(*this));
}
bool PartPin::loadState(PCBObjState &state)
{
// convert to part state
QSharedPointer<PartPinState> s = state.ptr().dynamicCast<PartPinState>();
if (s.isNull()) return false;
mPin = s->pin;
mPart = s->part;
return true;
}
///////////////////// PART /////////////////////
Part::Part(PCBDoc *doc)
: PCBObject(doc), mAngle(0), mSide(SIDE_TOP), mLocked(false), mRefVisible(false),
mValueVisible(false), mDoc(doc)
{
}
Part::~Part()
{
resetFp();
}
void Part::resetFp()
{
mRefdes.clear();
mValue.clear();
mPins.clear();
}
void Part::setFootprint(QUuid uuid)
{
mFpUuid = uuid;
updateFp();
}
void Part::updateFp()
{
resetFp();
mFp = doc()->getFootprint(mFpUuid);
if (mFp.isNull()) return;
// create new pins
for(int i = 0; i < mFp->numPins(); i++)
{
QSharedPointer<Pin> p = mFp->pin(i);
mPins.append(QSharedPointer<PartPin>(new PartPin(this, p)));
}
// create texts
mRefdes = mFp->refText()->clone();
mRefdes->setParent(this);
mValue = mFp->valueText()->clone();
mValue->setParent(this);
}
QSharedPointer<Part> Part::newFromXML(QXmlStreamReader &reader, PCBDoc *doc)
{
Q_ASSERT(reader.isStartElement() && reader.name() == "part");
QXmlStreamAttributes attr = reader.attributes();
QSharedPointer<Part> pp(new Part(doc));
pp->mFpUuid = QUuid(attr.value("footprint_uuid").toString());
pp->updateFp();
// reference designator
pp->mRefdes->setText(attr.value("refdes").toString());
pp->mRefVisible = true;
// value
if (attr.hasAttribute("value"))
{
pp->mValue->setText(attr.value("value").toString());
pp->mValueVisible = true;
}
// position/rotation
pp->mPos = QPoint(attr.value("x").toString().toInt(),
attr.value("y").toString().toInt());
pp->mAngle = attr.value("rot").toString().toInt();
// side
pp->mSide = (attr.value("side") == "top") ? SIDE_TOP : SIDE_BOTTOM;
// locked?
if (attr.hasAttribute("locked"))
pp->mLocked = (attr.value("locked") == "1");
pp->updateTransform();
pp->mRefdes->setParent(pp.data());
pp->mValue->setParent(pp.data());
pp->mRefdes->setLayer(pp->mSide == SIDE_TOP ? Layer::LAY_SILK_TOP : Layer::LAY_SILK_BOTTOM);
pp->mValue->setLayer(pp->mSide == SIDE_TOP ? Layer::LAY_SILK_TOP : Layer::LAY_SILK_BOTTOM);
// set text properties from part def, if they exist
while(reader.readNextStartElement())
{
QStringRef t = reader.name();
if (t == "refText")
{
QXmlStreamAttributes attr = reader.attributes();
pp->mRefdes->setPos(QPoint(attr.value("x").toString().toInt(),
attr.value("y").toString().toInt()));
pp->mRefdes->setAngle(attr.value("rot").toString().toInt());
pp->mRefdes->setFontSize(attr.value("textSize").toString().toInt());
pp->mRefdes->setStrokeWidth(attr.value("lineWidth").toString().toInt());
if (attr.hasAttribute("visible"))
pp->mRefVisible = attr.value("visible") == "1";
do
reader.readNext();
while(!reader.isEndElement());
}
else if (t == "valueText")
{
QXmlStreamAttributes attr = reader.attributes();
pp->mValue->setPos(QPoint(attr.value("x").toString().toInt(),
attr.value("y").toString().toInt()));
pp->mValue->setAngle(attr.value("rot").toString().toInt());
pp->mValue->setFontSize(attr.value("textSize").toString().toInt());
pp->mValue->setStrokeWidth(attr.value("lineWidth").toString().toInt());
if (attr.hasAttribute("visible"))
pp->mValueVisible = attr.value("visible") == "1";
do
reader.readNext();
while(!reader.isEndElement());
}
}
return pp;
}
void Part::toXML(QXmlStreamWriter &writer) const
{
// QPoint refdesPos = transform().inverted().map(mRefdes->pos());
// QPoint valuePos = transform().inverted().map(mValue->pos());
writer.writeStartElement("part");
writer.writeAttribute("footprint_uuid", mFpUuid.toString());
writer.writeAttribute("refdes", this->mRefdes->text());
writer.writeAttribute("value", this->mValue->text());
writer.writeAttribute("x", QString::number(mPos.x()));
writer.writeAttribute("y", QString::number(mPos.y()));
writer.writeAttribute("rot", QString::number(mAngle));
writer.writeAttribute("side", mSide == SIDE_TOP ? "top" : "bot");
writer.writeAttribute("locked", mLocked ? "1" : "0");
writer.writeStartElement("refText");
writer.writeAttribute("x", QString::number(mRefdes->pos().x()));
writer.writeAttribute("y", QString::number(mRefdes->pos().y()));
writer.writeAttribute("rot", QString::number(mRefdes->angle()));
writer.writeAttribute("textSize", QString::number(mRefdes->fontSize()));
writer.writeAttribute("lineWidth", QString::number(mRefdes->strokeWidth()));
writer.writeAttribute("visible", mRefVisible ? "1" : "0");
writer.writeEndElement();
writer.writeStartElement("valueText");
writer.writeAttribute("x", QString::number(mValue->pos().x()));
writer.writeAttribute("y", QString::number(mValue->pos().y()));
writer.writeAttribute("rot", QString::number(mValue->angle()));
writer.writeAttribute("textSize", QString::number(mValue->fontSize()));
writer.writeAttribute("lineWidth", QString::number(mValue->strokeWidth()));
writer.writeAttribute("visible", mValueVisible ? "1" : "0");
writer.writeEndElement();
writer.writeEndElement();
}
void Part::setSide(SIDE side)
{
mSide = side;
mRefdes->setLayer(mSide == SIDE_TOP ? Layer::LAY_SILK_TOP : Layer::LAY_SILK_BOTTOM);
mValue->setLayer(mSide == SIDE_TOP ? Layer::LAY_SILK_TOP : Layer::LAY_SILK_BOTTOM);
updateTransform();
}
void Part::updateTransform()
{
mTransform.reset();
mTransform.translate(mPos.x(), mPos.y());
if (mSide == SIDE_BOTTOM)
mTransform.scale(-1, 1);
mTransform.rotate(mAngle);
transformChanged(mTransform);
}
void Part::draw(QPainter *painter, const Layer& layer) const
{
painter->save();
painter->setTransform(mTransform, true);
// draw footprint
if ((layer.type() == Layer::LAY_SILK_TOP && mSide == SIDE_TOP) ||
(layer.type() == Layer::LAY_SILK_BOTTOM && mSide == SIDE_BOTTOM) ||
layer.type() == Layer::LAY_SELECTION)
mFp->draw(painter, Footprint::LAY_START);
else if ((layer.type() == Layer::LAY_SILK_TOP && mSide == SIDE_BOTTOM) ||
(layer.type() == Layer::LAY_SILK_BOTTOM && mSide == SIDE_TOP))
mFp->draw(painter, Footprint::LAY_END);
// draw pins
foreach(QSharedPointer<PartPin> p, mPins)
{
p->draw(painter, layer);
}
painter->restore();
}
QRect Part::bbox() const
{
return mTransform.mapRect(mFp->bbox());
}
QSharedPointer<PartPin> Part::pin(const QString &name)
{
foreach(QSharedPointer<PartPin> p, mPins)
{
if (p->name() == name)
return p;
}
return QSharedPointer<PartPin>();
}
PCBObjState Part::getState() const
{
return PCBObjState(new PartState(*this));
}
bool Part::loadState(PCBObjState &state)
{
// convert to part state
QSharedPointer<PartState> s = state.ptr().dynamicCast<PartState>();
if (s.isNull()) return false;
mTransform = s->transform;
mPos = s->pos;
mAngle = s->angle;
mSide = s->side;
mLocked = s->locked;
mRefVisible = s->refVis;
mValueVisible = s->valVis;
mFp = s->fp;
mFpUuid = s->uuid;
mPins = s->pins;
mDoc = s->doc;
transformChanged(mTransform);
return true;
}