KiCad PCB EDA Suite
Loading...
Searching...
No Matches
odb_netlist.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 * Author: SYSUEric <[email protected]>.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21
22#include <confirm.h>
23#include <gestfich.h>
24#include <kiface_base.h>
25#include <pcb_edit_frame.h>
26#include <trigo.h>
27#include <build_version.h>
28#include <macros.h>
30#include <board.h>
32#include <footprint.h>
33#include <pad.h>
34#include <pcb_track.h>
35#include <vector>
36#include <cctype>
37#include <odb_netlist.h>
38#include <wx/filedlg.h>
39#include <wx/log.h>
40#include "odb_util.h"
41
42
43// Compute the side code for a pad. Returns "" if there is no copper
44std::string ODB_NET_LIST::ComputePadAccessSide( BOARD* aBoard, LSET aLayerMask )
45{
46 // Non-copper is not interesting here
47 aLayerMask &= LSET::AllCuMask( aBoard->GetCopperLayerCount() );
48
49 if( !aLayerMask.any() )
50 return "";
51
52 // Traditional TH pad
53 if( aLayerMask[F_Cu] && aLayerMask[B_Cu] )
54 return "B";
55
56 // Front SMD pad
57 if( aLayerMask[F_Cu] )
58 return "T";
59
60 // Back SMD pad
61 if( aLayerMask[B_Cu] )
62 return "D";
63
64 // Inner. We've already checked that there is no copper on the front or back, so
65 // since we checked that there is at least one copper layer, this must be an inner layer
66 return "I";
67}
68
69
71 std::map<size_t, std::vector<ODB_NET_RECORD>>& aRecords )
72{
73 VECTOR2I origin = aBoard->GetDesignSettings().GetAuxOrigin();
74
75 for( FOOTPRINT* footprint : aBoard->Footprints() )
76 {
77 for( PAD* pad : footprint->Pads() )
78 {
79 ODB_NET_RECORD net_point;
80 net_point.side = ComputePadAccessSide( aBoard, pad->GetLayerSet() );
81
82 // It could be a mask only pad, we only handle pads with copper here
83 if( !net_point.side.empty() && net_point.side != "I" )
84 {
85 if( pad->GetNetCode() == 0 )
86 net_point.netname = "$NONE$";
87 else
88 net_point.netname = pad->GetNetname();
89 // net_point.pin = pad->GetNumber();
90 net_point.refdes = footprint->GetReference();
91 const VECTOR2I& drill = pad->GetDrillSize();
92 net_point.hole = pad->HasHole();
93
94 if( !net_point.hole )
95 net_point.drill_radius = 0;
96 else
97 net_point.drill_radius = std::min( drill.x, drill.y );
98
99 net_point.smd = pad->GetAttribute() == PAD_ATTRIB::SMD
100 || pad->GetAttribute() == PAD_ATTRIB::CONN;
101 net_point.is_via = false;
102 net_point.mechanical = ( pad->GetAttribute() == PAD_ATTRIB::NPTH );
103 net_point.x_location = pad->GetPosition().x - origin.x;
104 net_point.y_location = origin.y - pad->GetPosition().y;
105 net_point.x_size = pad->GetSize( PADSTACK::ALL_LAYERS ).x;
106
107 // Rule: round pads have y = 0
108 if( pad->GetShape( PADSTACK::ALL_LAYERS ) == PAD_SHAPE::CIRCLE )
109 net_point.y_size = net_point.x_size;
110 else
111 net_point.y_size = pad->GetSize( PADSTACK::ALL_LAYERS ).y;
112
113 // net_point.rotation = ( ANGLE_360 - pad->GetOrientation() ).Normalize().AsDegrees();
114
115 // if( net_point.rotation < 0 )
116 // net_point.rotation += 360;
117
118 // always output NET end point as net test point
119 net_point.epoint = "e";
120
121 // the value indicates which sides are *not* accessible
122 net_point.soldermask = 3;
123
124 if( pad->GetLayerSet()[F_Mask] )
125 net_point.soldermask &= ~1;
126
127 if( pad->GetLayerSet()[B_Mask] )
128 net_point.soldermask &= ~2;
129
130 aRecords[pad->GetNetCode()].push_back( net_point );
131 }
132 }
133 }
134}
135
136// Compute the side code for a via.
137std::string ODB_NET_LIST::ComputeViaAccessSide( BOARD* aBoard, int top_layer, int bottom_layer )
138{
139 // Easy case for through vias: top_layer is component, bottom_layer is
140 // solder, side code is Both
141 if( ( top_layer == F_Cu ) && ( bottom_layer == B_Cu ) )
142 return "B";
143
144 // Blind via, reachable from front, Top
145 if( top_layer == F_Cu )
146 return "T";
147
148 // Blind via, reachable from bottom, Down
149 if( bottom_layer == B_Cu )
150 return "D";
151
152 // It's a buried via, accessible from some inner layer, Inner
153 return "I";
154}
155
156
158 std::map<size_t, std::vector<ODB_NET_RECORD>>& aRecords )
159{
160 VECTOR2I origin = aBoard->GetDesignSettings().GetAuxOrigin();
161
162 // Enumerate all the track segments and keep the vias
163 for( auto track : aBoard->Tracks() )
164 {
165 if( track->Type() == PCB_VIA_T )
166 {
167 PCB_VIA* via = static_cast<PCB_VIA*>( track );
168 PCB_LAYER_ID top_layer, bottom_layer;
169
170 via->LayerPair( &top_layer, &bottom_layer );
171
172 ODB_NET_RECORD net_point;
173 net_point.side = ComputeViaAccessSide( aBoard, top_layer, bottom_layer );
174
175 if( net_point.side != "I" )
176 {
177 NETINFO_ITEM* net = track->GetNet();
178 net_point.smd = false;
179 net_point.hole = true;
180
181 if( net->GetNetCode() == 0 )
182 net_point.netname = "$NONE$";
183 else
184 net_point.netname = net->GetNetname();
185
186 net_point.refdes = "VIA";
187 net_point.is_via = true;
188 net_point.drill_radius = via->GetDrillValue();
189 net_point.mechanical = false;
190 net_point.x_location = via->GetPosition().x - origin.x;
191 net_point.y_location = origin.y - via->GetPosition().y;
192
193 // via always has drill radius, Width and Height are 0
194 net_point.x_size = 0;
195 net_point.y_size = 0; // Round so height = 0
196 net_point.epoint = "e"; // only buried via is "m" net mid point
197
198 // the value indicates which sides are *not* accessible
199 net_point.soldermask = 3;
200
201 if( via->GetLayerSet()[F_Mask] )
202 net_point.soldermask &= ~1;
203
204 if( via->GetLayerSet()[B_Mask] )
205 net_point.soldermask &= ~2;
206
207 aRecords[net->GetNetCode()].push_back( net_point );
208 }
209 }
210 }
211}
212
213
214void ODB_NET_LIST::WriteNetPointRecords( std::map<size_t, std::vector<ODB_NET_RECORD>>& aRecords,
215 std::ostream& aStream )
216{
217 aStream << "H optimize n staggered n" << std::endl;
218
219 for( const auto& [key, vec] : aRecords )
220 {
221 aStream << "$" << key << " " << ODB::GenLegalNetName( vec.front().netname ) << std::endl;
222 }
223
224 aStream << "#" << std::endl << "#Netlist points" << std::endl << "#" << std::endl;
225
226 for( const auto& [key, vec] : aRecords )
227 {
228 for( const auto& net_point : vec )
229 {
230 aStream << key << " ";
231
232 if( net_point.hole )
233 aStream << ODB::Data2String( net_point.drill_radius );
234 else
235 aStream << 0;
236
237 aStream << " " << ODB::Data2String( net_point.x_location ) << " "
238 << ODB::Data2String( net_point.y_location ) << " " << net_point.side << " ";
239
240 if( !net_point.hole )
241 aStream << ODB::Data2String( net_point.x_size ) << " "
242 << ODB::Data2String( net_point.y_size ) << " ";
243
244 std::string exp;
245
246 if( net_point.soldermask == 3 )
247 exp = "c";
248 else if( net_point.soldermask == 2 )
249 exp = "s";
250 else if( net_point.soldermask == 1 )
251 exp = "p";
252 else if( net_point.soldermask == 0 )
253 exp = "e";
254
255 aStream << net_point.epoint << " " << exp;
256
257 if( net_point.hole )
258 aStream << " staggered 0 0 0";
259
260 if( net_point.is_via )
261 aStream << " v";
262
263 aStream << std::endl;
264 }
265 }
266}
267
268
269void ODB_NET_LIST::Write( std::ostream& aStream )
270{
271 std::map<size_t, std::vector<ODB_NET_RECORD>> net_point_records;
272
273 InitViaNetPoints( m_board, net_point_records );
274
275 InitPadNetPoints( m_board, net_point_records );
276
277 WriteNetPointRecords( net_point_records, aStream );
278}
const VECTOR2I & GetAuxOrigin() const
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:317
int GetCopperLayerCount() const
Definition: board.cpp:859
const FOOTPRINTS & Footprints() const
Definition: board.h:358
const TRACKS & Tracks() const
Definition: board.h:356
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:1024
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:37
static LSET AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition: lset.cpp:591
Handle the data for a net.
Definition: netinfo.h:56
const wxString & GetNetname() const
Definition: netinfo.h:114
int GetNetCode() const
Definition: netinfo.h:108
BOARD * m_board
Definition: odb_netlist.h:66
void WriteNetPointRecords(std::map< size_t, std::vector< ODB_NET_RECORD > > &aRecords, std::ostream &aStream)
Writes a list of records to the given output stream.
void InitViaNetPoints(BOARD *aBoard, std::map< size_t, std::vector< ODB_NET_RECORD > > &aRecords)
std::string ComputeViaAccessSide(BOARD *aBoard, int top_layer, int bottom_layer)
void Write(std::ostream &aStream)
void InitPadNetPoints(BOARD *aBoard, std::map< size_t, std::vector< ODB_NET_RECORD > > &aRecords)
Definition: odb_netlist.cpp:70
std::string ComputePadAccessSide(BOARD *aBoard, LSET aLayerMask)
Definition: odb_netlist.cpp:44
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition: padstack.h:145
Definition: pad.h:54
This file is part of the common library.
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ B_Mask
Definition: layer_ids.h:98
@ B_Cu
Definition: layer_ids.h:65
@ F_Mask
Definition: layer_ids.h:97
@ F_Cu
Definition: layer_ids.h:64
This file contains miscellaneous commonly used macros and functions.
wxString Data2String(double aVal)
Definition: odb_util.cpp:161
wxString GenLegalNetName(const wxString &aStr)
Definition: odb_util.cpp:57
std::string side
Definition: odb_netlist.h:36
std::string epoint
Definition: odb_netlist.h:46
wxString netname
Definition: odb_netlist.h:32
std::string refdes
Definition: odb_netlist.h:33
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
Definition of file extensions used in Kicad.