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
18 * along with this program. If not, see <https://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>
31#include <footprint.h>
32#include <pad.h>
33#include <pcb_track.h>
34#include <vector>
35#include <cctype>
36#include <odb_netlist.h>
37#include <wx/filedlg.h>
38#include <wx/log.h>
39#include "odb_util.h"
40
41
42// Compute the side code for a pad. Returns "" if there is no copper
43std::string ODB_NET_LIST::ComputePadAccessSide( BOARD* aBoard, LSET aLayerMask )
44{
45 // Non-copper is not interesting here
46 aLayerMask &= LSET::AllCuMask( aBoard->GetCopperLayerCount() );
47
48 if( !aLayerMask.any() )
49 return "";
50
51 // Traditional TH pad
52 if( aLayerMask[F_Cu] && aLayerMask[B_Cu] )
53 return "B";
54
55 // Front SMD pad
56 if( aLayerMask[F_Cu] )
57 return "T";
58
59 // Back SMD pad
60 if( aLayerMask[B_Cu] )
61 return "D";
62
63 // Inner. We've already checked that there is no copper on the front or back, so
64 // since we checked that there is at least one copper layer, this must be an inner layer
65 return "I";
66}
67
68
70 std::map<size_t, std::vector<ODB_NET_RECORD>>& aRecords )
71{
72 for( FOOTPRINT* footprint : aBoard->Footprints() )
73 {
74 for( PAD* pad : footprint->Pads() )
75 {
76 ODB_NET_RECORD net_point;
77 net_point.side = ComputePadAccessSide( aBoard, pad->GetLayerSet() );
78
79 // It could be a mask only pad, we only handle pads with copper here
80 if( !net_point.side.empty() && net_point.side != "I" )
81 {
82 if( pad->GetNetCode() == 0 )
83 net_point.netname = "$NONE$";
84 else
85 net_point.netname = pad->GetNetname();
86 // net_point.pin = pad->GetNumber();
87 net_point.refdes = footprint->GetReference();
88 const VECTOR2I& drill = pad->GetDrillSize();
89 net_point.hole = pad->HasHole();
90
91 if( !net_point.hole )
92 net_point.drill_radius = 0;
93 else
94 net_point.drill_radius = std::min( drill.x, drill.y );
95
96 net_point.smd = pad->GetAttribute() == PAD_ATTRIB::SMD
97 || pad->GetAttribute() == PAD_ATTRIB::CONN;
98 net_point.is_via = false;
99 net_point.mechanical = ( pad->GetAttribute() == PAD_ATTRIB::NPTH );
100 net_point.x_location = pad->GetPosition().x;
101 net_point.y_location = -pad->GetPosition().y;
102 net_point.x_size = pad->GetSize( PADSTACK::ALL_LAYERS ).x;
103
104 // Rule: round pads have y = 0
105 if( pad->GetShape( PADSTACK::ALL_LAYERS ) == PAD_SHAPE::CIRCLE )
106 net_point.y_size = net_point.x_size;
107 else
108 net_point.y_size = pad->GetSize( PADSTACK::ALL_LAYERS ).y;
109
110 // net_point.rotation = ( ANGLE_360 - pad->GetOrientation() ).Normalize().AsDegrees();
111
112 // if( net_point.rotation < 0 )
113 // net_point.rotation += 360;
114
115 // always output NET end point as net test point
116 net_point.epoint = "e";
117
118 // the value indicates which sides are *not* accessible
119 net_point.soldermask = 3;
120
121 if( pad->GetLayerSet()[F_Mask] )
122 net_point.soldermask &= ~1;
123
124 if( pad->GetLayerSet()[B_Mask] )
125 net_point.soldermask &= ~2;
126
127 aRecords[pad->GetNetCode()].push_back( net_point );
128 }
129 }
130 }
131}
132
133// Compute the side code for a via.
134std::string ODB_NET_LIST::ComputeViaAccessSide( BOARD* aBoard, int top_layer, int bottom_layer )
135{
136 // Easy case for through vias: top_layer is component, bottom_layer is
137 // solder, side code is Both
138 if( ( top_layer == F_Cu ) && ( bottom_layer == B_Cu ) )
139 return "B";
140
141 // Blind via, reachable from front, Top
142 if( top_layer == F_Cu )
143 return "T";
144
145 // Blind via, reachable from bottom, Down
146 if( bottom_layer == B_Cu )
147 return "D";
148
149 // It's a buried via, accessible from some inner layer, Inner
150 return "I";
151}
152
153
155 std::map<size_t, std::vector<ODB_NET_RECORD>>& aRecords )
156{
157 // Enumerate all the track segments and keep the vias
158 for( auto track : aBoard->Tracks() )
159 {
160 if( track->Type() == PCB_VIA_T )
161 {
162 PCB_VIA* via = static_cast<PCB_VIA*>( track );
163 PCB_LAYER_ID top_layer, bottom_layer;
164
165 via->LayerPair( &top_layer, &bottom_layer );
166
167 ODB_NET_RECORD net_point;
168 net_point.side = ComputeViaAccessSide( aBoard, top_layer, bottom_layer );
169
170 if( net_point.side != "I" )
171 {
172 NETINFO_ITEM* net = track->GetNet();
173 net_point.smd = false;
174 net_point.hole = true;
175
176 if( net->GetNetCode() == 0 )
177 net_point.netname = "$NONE$";
178 else
179 net_point.netname = net->GetNetname();
180
181 net_point.refdes = "VIA";
182 net_point.is_via = true;
183 net_point.drill_radius = via->GetDrillValue();
184 net_point.mechanical = false;
185 net_point.x_location = via->GetPosition().x;
186 net_point.y_location = -via->GetPosition().y;
187
188 // via always has drill radius, Width and Height are 0
189 net_point.x_size = 0;
190 net_point.y_size = 0; // Round so height = 0
191 net_point.epoint = "e"; // only buried via is "m" net mid point
192
193 // the value indicates which sides are *not* accessible
194 net_point.soldermask = 3;
195
196 if( via->GetLayerSet()[F_Mask] )
197 net_point.soldermask &= ~1;
198
199 if( via->GetLayerSet()[B_Mask] )
200 net_point.soldermask &= ~2;
201
202 aRecords[net->GetNetCode()].push_back( net_point );
203 }
204 }
205 }
206}
207
208
209void ODB_NET_LIST::WriteNetPointRecords( std::map<size_t, std::vector<ODB_NET_RECORD>>& aRecords,
210 std::ostream& aStream )
211{
212 aStream << "H optimize n staggered n" << std::endl;
213
214 for( const auto& [key, vec] : aRecords )
215 {
216 aStream << "$" << key << " " << ODB::GenLegalNetName( vec.front().netname ) << std::endl;
217 }
218
219 aStream << "#" << std::endl << "#Netlist points" << std::endl << "#" << std::endl;
220
221 for( const auto& [key, vec] : aRecords )
222 {
223 for( const auto& net_point : vec )
224 {
225 aStream << key << " ";
226
227 if( net_point.hole )
228 aStream << ODB::Data2String( net_point.drill_radius );
229 else
230 aStream << 0;
231
232 aStream << " " << ODB::Data2String( net_point.x_location ) << " "
233 << ODB::Data2String( net_point.y_location ) << " " << net_point.side << " ";
234
235 if( !net_point.hole )
236 aStream << ODB::Data2String( net_point.x_size ) << " "
237 << ODB::Data2String( net_point.y_size ) << " ";
238
239 std::string exp;
240
241 if( net_point.soldermask == 3 )
242 exp = "c";
243 else if( net_point.soldermask == 2 )
244 exp = "s";
245 else if( net_point.soldermask == 1 )
246 exp = "p";
247 else if( net_point.soldermask == 0 )
248 exp = "e";
249
250 aStream << net_point.epoint << " " << exp;
251
252 if( net_point.hole )
253 aStream << " staggered 0 0 0";
254
255 if( net_point.is_via )
256 aStream << " v";
257
258 aStream << std::endl;
259 }
260 }
261}
262
263
264void ODB_NET_LIST::Write( std::ostream& aStream )
265{
266 std::map<size_t, std::vector<ODB_NET_RECORD>> net_point_records;
267
268 InitViaNetPoints( m_board, net_point_records );
269
270 InitPadNetPoints( m_board, net_point_records );
271
272 WriteNetPointRecords( net_point_records, aStream );
273}
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
int GetCopperLayerCount() const
Definition board.cpp:985
const FOOTPRINTS & Footprints() const
Definition board.h:420
const TRACKS & Tracks() const
Definition board.h:418
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
Handle the data for a net.
Definition netinfo.h:46
const wxString & GetNetname() const
Definition netinfo.h:100
int GetNetCode() const
Definition netinfo.h:94
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)
std::string ComputePadAccessSide(BOARD *aBoard, LSET aLayerMask)
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition padstack.h:177
Definition pad.h:61
This file is part of the common library.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ B_Mask
Definition layer_ids.h:94
@ B_Cu
Definition layer_ids.h:61
@ F_Mask
Definition layer_ids.h:93
@ F_Cu
Definition layer_ids.h:60
This file contains miscellaneous commonly used macros and functions.
wxString Data2String(double aVal)
Definition odb_util.cpp:185
wxString GenLegalNetName(const wxString &aStr)
Definition odb_util.cpp:57
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:103
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:99
@ CONN
Like smd, does not appear on the solder paste layer (default) Note: also has a special attribute in G...
Definition padstack.h:100
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:90
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
Definition of file extensions used in Kicad.