Menu

[r171]: / specctra / cpcbnet.cpp  Maximize  Restore  History

Download this file

369 lines (347 with data), 7.7 kB

  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
/*******************************************************************************
* Copyright (C) 2014 Pike Aerospace Research Corporation *
* Author: Mike Sharkey <mike@pikeaero.com> *
* *
* This program 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. *
* *
* This program 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 at <http://www.gnu.org/licenses/> *
* for more details. *
* *
*******************************************************************************/
#include "cpcbnet.h"
#include "cpcbpins.h"
#include "cgpadstack.h"
#include "cpcb.h"
#include "cpcbstructure.h"
#include "cpcbrule.h"
#include "cpcbnetwork.h"
#include "cpcbclass.h"
#include "cpcbpins.h"
#include "cutil.h"
#include "cpcbplace.h"
#include "cpcbplacement.h"
#include "cgwire.h"
#include "cgsegment.h"
#include "cpcbwidth.h"
#include <stdio.h>
#define inherited CSpecctraObject
CPcbNet::CPcbNet(QGraphicsItem *parent)
: inherited(parent)
, mWidth(-1.0)
{
this->setFlag(QGraphicsItem::ItemIsSelectable);
mWire = new CGWire(this);
CSpecctraObject::globalScene()->addItem(this);
}
CPcbNet::~CPcbNet()
{
//delete mWire;
}
/**
* @return the name of the net
*/
QString CPcbNet::name()
{
QString nm;
if ( properties().count() )
{
nm = properties().at(0);
}
return nm;
}
/**
* @brief determine of the net is routed.
*/
bool CPcbNet::routed()
{
bool rc=true;
CGWire& w = wire();
for(int n=0; n < w.segments(); n++)
{
CGSegment* seg = w.segment(n);
if ( !seg->routed() )
{
rc=false;
break;
}
}
return rc;
}
/**
* @return the width of the net tracks
*/
double CPcbNet::width()
{
double w=12;
if ( mWidth < 0.0 )
{
if ( netClass() != NULL )
{
w = netClass()->width();
}
else if (pcb()!=NULL && pcb()->structure()!=NULL && pcb()->structure()->rule()!=NULL)
{
w = pcb()->structure()->rule()->width()->data();
}
}
else
{
w = mWidth;
}
return w;
}
/**
* @return enumerated list of pin references in this net.
*/
QStringList& CPcbNet::pinRefs()
{
if ( mPinRefs.count()==0 )
{
CPcbPins* objPins = (CPcbPins*)child("pins");
if ( objPins != NULL )
{
for(int n=0; n < objPins->pinRefs(); n++)
{
mPinRefs.append(objPins->pinRef(n));
}
}
}
return mPinRefs;
}
/**
* @return the number of pads in the net
*/
int CPcbNet::padstacks()
{
if ( mPadstacks.count() == 0 )
{
if ( pcb() != NULL && pcb()->placement() != NULL )
{
for(int n=0; n < pinRefs().count(); n++)
{
QString unitRef = CUtil::unitRef(pinRefs().at(n));
QString pinRef = CUtil::pinRef(pinRefs().at(n));
CPcbPlace* place = pcb()->placement()->place(unitRef);
if ( place != NULL )
{
CGPadstack* padstack = place->pad(pinRef);
if ( padstack != NULL )
{
padstack->setPos(place->pad(pinRef)->pos());
padstack->setOrigin(place->pad(pinRef)->origin());
mPadstacks.append(padstack);
}
}
}
}
}
return mPadstacks.count();
}
/**
* @return a padstack by index
*/
CGPadstack* CPcbNet::padstack(int idx)
{
if ( padstacks() > idx )
{
return mPadstacks.at(idx);
}
return NULL;
}
/**
* @return a padstack by UNIT-PIN string format reference
*/
CGPadstack* CPcbNet::padstack(QString ref)
{
for(int n=0; n < padstacks(); n++)
{
if ( padstack(n)->unitRef() == ref )
{
return padstack(n);
}
}
return NULL;
}
/**
* @return a reference to the padstacks list.
*/
QList<CGPadstack*>& CPcbNet::padstacksRef()
{
padstacks(); /** prime the padstacks list */
return mPadstacks;
}
/**
* @return the closest padstack
*/
CGPadstack* CPcbNet::closest(int n,QPointF pt)
{
CGPadstack* close=NULL;
for(; n < padstacks(); n++)
{
CGPadstack* pad = padstack(n);
if ( close == NULL )
{
close = pad;
}
else
{
QPointF p1 = pad->origin() - pt;
QPointF p2 = close->origin() - pt;
double len1 = p1.manhattanLength();
double len2 = p2.manhattanLength();
if ( len1 < len2 )
{
close = pad;
}
}
}
return close;
}
/**
* @return the farthest padstack
*/
CGPadstack* CPcbNet::farthest(int n, QPointF pt)
{
CGPadstack* far=NULL;
for(; n < padstacks(); n++)
{
CGPadstack* pad = padstack(n);
if ( far == NULL )
{
far = pad;
}
else
{
QPointF p1 = pad->origin() - pt;
QPointF p2 = far->origin() - pt;
double len1 = p1.manhattanLength();
double len2 = p2.manhattanLength();
if ( len1 > len2 )
{
far = pad;
}
}
}
return far;
}
/**
* @brief exchange one padstack with another.
*/
void CPcbNet::swap(CGPadstack* p1,CGPadstack* p2)
{
if ( p1 != p2)
{
int idx1 = padstacksRef().indexOf(p1);
int idx2 = padstacksRef().indexOf(p2);
if ( idx1 >= 0 && idx2 >= 0 )
{
padstacksRef().swap(idx1,idx2);
}
}
}
/**
* @brief sort the netlist with respect to distance from a point.
*/
void CPcbNet::sort()
{
/* sort the padstack lists associated with each net relative to distance from center point of PCB */
for(int n=1; n < padstacks(); n++ )
{
CGPadstack* pad = closest(n,padstack(n-1)->origin());
swap(pad,padstack(n));
}
}
void CPcbNet::dumpLength()
{
printf( "********\n");
for(int n=0; n < padstacks(); n++)
{
QPointF pt = padstack(n)->origin() - padstack(0)->origin();
double len = pt.manhattanLength();
printf( "%s %g+%g\t%g\n", padstack(n)->unitRef().toLocal8Bit().data(), pt.x(),pt.y(), len);
}
}
/**
* @return the net class
*/
CPcbClass* CPcbNet::netClass()
{
if ( pcb()!=NULL && pcb()->network()!= NULL )
{
for( int n=0; n < pcb()->network()->netClasses(); n++)
{
CPcbClass* pClass = pcb()->network()->netClass(n);
if ( pClass != NULL )
{
QStringList& nets = pClass->nets();
if (nets.contains(name()))
{
return pcb()->network()->netClass(n);
}
}
}
}
return NULL;
}
/**
* @brief create a wire if one does not exisst already.
* @return a reference to the wire.
*/
CGWire& CPcbNet::wire()
{
if ( mWire->isEmpty() )
{
CGSegment* obj = NULL;
for( int n=0; n < this->padstacks(); n++)
{
CGPadstack* pad = padstack(n);
if ( n == 0 )
{
obj = mWire;
}
else
{
CGWire* child = new CGWire(this);
obj->append(child);
obj = child;
}
obj->setOrigin(pad->origin());
obj->hide();
obj->append(pad);
obj = pad;
}
}
return *mWire;
}
/**
* @return verbose description
*/
QString CPcbNet::description()
{
QString rc;
rc += name()+" ";
rc += netClass()->description();
return rc;
}
QRectF CPcbNet::boundingRect() const
{
QRectF bounds = shape().boundingRect();
return bounds;
}
QPainterPath CPcbNet::shape() const
{
QPainterPath p;
CPcbNet* me=(CPcbNet*)this;
me->wire(); /* prime the wire segments */
return p;
}
void CPcbNet::paint(QPainter* /* painter */, const QStyleOptionGraphicsItem* /* option */, QWidget* /* widget */)
{
QPainterPath p = shape(); /* prime the shape */
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.