-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrace.h
422 lines (348 loc) · 10.7 KB
/
Trace.h
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
/*
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/>.
*/
#ifndef TRACE_H
#define TRACE_H
#include <QList>
#include <QSet>
#include "PCBObject.h"
#include "Footprint.h"
#include "Net.h"
class QXmlStreamReader;
class QXmlStreamWriter;
class Area;
class Polygon;
class PartPin;
class Padstack;
class TraceList;
class Segment;
class PCBDoc;
/// Vias are objects that make electrical connections between layers.
/// A via has an associated padstacks; all pads in the padstack are
/// assumed to be electrically connected.
class Via : public PCBObject
{
Q_OBJECT
public:
Via(QPoint pos = QPoint(0, 0),
QSharedPointer<Padstack> ps = QSharedPointer<Padstack>(),
QObject *parent = NULL);
virtual void draw(QPainter *painter, const Layer& layer) const;
virtual QRect bbox() const;
virtual bool testHit(QPoint p, int dist, const Layer &l) const;
virtual PCBObjState getState() const
{
return PCBObjState(new ViaState(*this));
}
virtual bool loadState(PCBObjState& state);
QPoint pos() const {return mPos;}
void setPos(QPoint pos) { mPos = pos; }
/// Adds a connected vertex to the via's vertex set.
void attach(const Vertex* vtx) const;
void attach(const PartPin* pin) const { mPartPins.insert(pin); }
/// Removes a vertex from the set of connected vertices.
void detach(const Vertex* vtx) const;
void detach(const PartPin* pin) const { mPartPins.remove(pin); }
/// Detaches all attached vertices
void detachAll() const;
/// Returns a reference to the vertex set.
QSet<const Vertex*> vertices() const { return mVtxs; }
QSet<const PartPin*> partpins() const { return mPartPins; }
/// Returns true if a pad is present on the given layer
bool onLayer(const Layer& layer) const;
/// Returns the via padstack
QSharedPointer<Padstack> padstack() const { return mPadstack; }
/// Replaces the via padstack
void setPadstack(QSharedPointer<Padstack> ps) { mPadstack = ps; }
private:
class ViaState : public PCBObjStateInternal
{
public:
virtual ~ViaState() {}
private:
friend class Via;
ViaState(const Via &v)
: mPos(v.pos()),
mPadstack(v.padstack())
{}
QPoint mPos;
QSharedPointer<Padstack> mPadstack;
};
QPoint mPos;
mutable QSet<const Vertex*> mVtxs;
mutable QSet<const PartPin*> mPartPins;
QSharedPointer<Padstack> mPadstack;
};
/// A trace vertex.
/// Trace vertices are endpoints of trace segments. They can be
/// attached to part pins, vias, and areas.
class Vertex : public PCBObject
{
Q_OBJECT
public:
Vertex(QPoint pos = QPoint(0, 0), QObject *parent = NULL);
virtual void draw(QPainter *painter, const Layer& layer) const;
virtual QRect bbox() const;
virtual bool testHit(QPoint p, int dist, const Layer &l) const;
virtual PCBObjState getState() const
{
return PCBObjState(new VtxState(*this));
}
virtual bool loadState(PCBObjState& state);
QPoint pos() const {return mPos;}
void setPos(QPoint pos) { mPos = pos; }
/// Adds a connected segment to the vertex's segment set.
void addSegment(QSharedPointer<Segment> seg);
/// Removes a segment from the set of connected segments.
void removeSegment(QSharedPointer<Segment> seg);
/// Returns the segment set.
QSet<QSharedPointer<Segment> > segments() const { return mSegs; }
/// Removes all connected segments
void clearSegments() { mSegs.clear(); }
/// Attach a part pin to the vertex. Detaches previous pin.
void attach(const PartPin* pin) const;
/// Attach a via to the vertex. Detaches previous via.
void attach(const Via* via) const;
const Via* via() const { return mVia; }
const PartPin* partpin() const { return mPartPin; }
/// Detach the part pin from the vertex
void detachPin() const;
/// Detach the via from the vertex
void detachVia() const;
/// Detach all attached objects from the vertex
void detachAll() const;
Layer layer() const;
private:
class VtxState : public PCBObjStateInternal
{
public:
virtual ~VtxState() {}
private:
friend class Vertex;
VtxState(const Vertex &v)
: mPos(v.pos())
{}
QPoint mPos;
};
QPoint mPos;
QSet<QSharedPointer<Segment> > mSegs;
mutable const PartPin* mPartPin;
mutable const Via* mVia;
};
/// Trace segment
/// A trace segment is a straight line between two trace vertices
class Segment : public PCBObject
{
Q_OBJECT
public:
Segment(const Layer& layer, int w = 0, QObject* parent = NULL);
virtual void draw(QPainter *painter, const Layer& layer) const;
virtual QRect bbox() const;
virtual bool testHit(QPoint p, int dist, const Layer &l) const;
virtual PCBObjState getState() const
{
return PCBObjState(new SegState(*this));
}
virtual bool loadState(PCBObjState& state);
int width() const {return mWidth;}
void setWidth(int w) {mWidth = w;}
const Layer& layer() const {return mLayer;}
void setLayer(const Layer& layer) {mLayer = layer;}
bool hasVertex(QSharedPointer<Vertex> v) const
{
return (v == mV1 || v == mV2);
}
bool hasVertex(const Vertex* v) const
{
return (v == mV1 || v == mV2);
}
QSharedPointer<Vertex> otherVertex(QSharedPointer<Vertex> v) const
{
if (!hasVertex(v))
return QSharedPointer<Vertex>();
else
return (v == mV1 ? mV2 : mV1);
}
Vertex* otherVertex(Vertex* v) const
{
if (!hasVertex(v))
return 0;
else
return (v == mV1 ? mV2.data() : mV1.data());
}
const Vertex* otherVertex(const Vertex* v) const
{
if (!hasVertex(v))
return 0;
else
return (v == mV1 ? mV2.data() : mV1.data());
}
QSharedPointer<Vertex> commonVertex(QSharedPointer<Segment> s) const
{
if (s->hasVertex(mV1))
return mV1;
else if (s->hasVertex(mV2))
return mV2;
else
return QSharedPointer<Vertex>();
}
QSharedPointer<Vertex> v1() const { return mV1; }
QSharedPointer<Vertex> v2() const { return mV2; }
void setV1(QSharedPointer<Vertex> v) { mV1 = v; }
void setV2(QSharedPointer<Vertex> v) { mV2 = v; }
void clear() { mV1.clear(); mV2.clear(); }
QSharedPointer<Segment> clone() const { return QSharedPointer<Segment>(new Segment(mLayer, mWidth)); }
private:
class SegState : public PCBObjStateInternal
{
public:
virtual ~SegState() {}
private:
friend class Segment;
SegState(const Segment &v)
: mLayer(v.layer()), mWidth(v.width())
{}
Layer mLayer;
int mWidth;
};
Layer mLayer;
QSharedPointer<Vertex> mV1;
QSharedPointer<Vertex> mV2;
int mWidth;
};
/// The container for all copper traces and areas on the board.
/// A trace consists of a set of connected segments and
/// vertices, which form a graph structure.
class TraceList
{
public:
TraceList(PCBDoc* doc) : mDoc(doc), mIsDirty(false) {}
~TraceList() { clear(); }
// QSet<Vertex*> getConnectedVertices(Vertex* vtx) const;
QSet<Vertex*> getVerticesInArea(const Area& poly) const;
/// Draws ratlines and highlights shorted pins
void draw(QPainter *painter, const Layer& layer) const;
void addSegment(QSharedPointer<Segment> s,
QSharedPointer<Vertex> v1,
QSharedPointer<Vertex> v2);
QUndoCommand* addSegmentCmd(QSharedPointer<Segment> s,
QSharedPointer<Vertex> v1,
QSharedPointer<Vertex> v2,
QUndoCommand* parent = NULL)
{
return new AddSegCmd(parent, this, s, v1, v2);
}
/// Also removes any attached vertices.
void removeSegment(QSharedPointer<Segment> s);
QUndoCommand* removeSegmentCmd(QSharedPointer<Segment> s,
QUndoCommand* parent = NULL)
{
return new DelSegCmd(parent, this, s);
}
/// Replaces vOld with vNew for the given segment.
void swapVtx(QSharedPointer<Segment> s,
QSharedPointer<Vertex> vOld,
QSharedPointer<Vertex> vNew);
QUndoCommand* swapVtxCmd(QSharedPointer<Segment> s,
QSharedPointer<Vertex> vOld,
QSharedPointer<Vertex> vNew,
QUndoCommand* parent = NULL)
{
return new SwapVtxCmd(parent, this, s, vOld, vNew);
}
QSharedPointer<Segment> segment(QSharedPointer<Vertex> v1,
QSharedPointer<Vertex> v2) const;
QSet<QSharedPointer<Segment> > segments() const {return mySeg;}
QSet<QSharedPointer<Vertex> > vertices() const {return myVtx;}
void loadFromXml(QXmlStreamReader &reader);
void toXML(QXmlStreamWriter &writer) const;
private:
class AddSegCmd : public QUndoCommand
{
public:
AddSegCmd(QUndoCommand* parent, TraceList* tl,
QSharedPointer<Segment> s,
QSharedPointer<Vertex> v1,
QSharedPointer<Vertex> v2);
virtual void undo();
virtual void redo();
private:
TraceList *mTl;
QSharedPointer<Segment> mSeg;
QSharedPointer<Vertex> mV1;
QSharedPointer<Vertex> mV2;
};
class DelSegCmd : public QUndoCommand
{
public:
DelSegCmd(QUndoCommand* parent, TraceList *tl,
QSharedPointer<Segment> s);
virtual void undo();
virtual void redo();
private:
TraceList *mTl;
QSharedPointer<Segment> mSeg;
QSharedPointer<Vertex> mV1, mV2;
};
class SwapVtxCmd : public QUndoCommand
{
public:
SwapVtxCmd(QUndoCommand* parent, TraceList *tl,
QSharedPointer<Segment> s,
QSharedPointer<Vertex> vOld,
QSharedPointer<Vertex> vNew);
virtual void undo();
virtual void redo();
private:
TraceList *mTl;
QSharedPointer<Segment> mSeg;
QSharedPointer<Vertex> mVOld, mVNew;
};
class ConnGroup
{
public:
ConnGroup(const PartPin* pin);
QSet<const Vertex*> vertices() const { return mVertices; }
QSet<const PartPin*> pins() const { return mPins; }
QSet<const PartPin*> validPins() const { return mPins - mShortedPins; }
QSet<const Via*> vias() const { return mVias; }
QSet<const PartPin*> shortedPins() const { return mShortedPins; }
QString net() const { return mNet; }
private:
void DFS(const Vertex* currVtx);
void update();
QString mNet;
QSet<const Vertex*> mVertices;
QSet<const PartPin*> mPins;
QSet<const Via*> mVias;
QSet<const PartPin*> mShortedPins;
};
friend class AddSegCmd;
friend class DelSegCmd;
friend class SwapVtxCmd;
void clear();
void update() const;
void rebuildConnectivity() const;
void rebuildRats() const;
void rebuildRatsForNet(QString net) const;
PCBDoc* mDoc;
QSet<QSharedPointer<Segment> > mySeg; // set of segments
QSet<QSharedPointer<Vertex> > myVtx; // set of vertices
QSet<QSharedPointer<Via> > myVias; // set of vias
mutable bool mIsDirty;
/// Master list of connections (maps net->list of conns)
mutable QHash<QString, QList<ConnGroup> > mConnections;
mutable QHash<QString, QList<QPair<const PartPin*,const PartPin*> > > mRats;
};
#endif // TRACE_H