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>
36#include <vector>
37#include <cctype>
38#include <odb_netlist.h>
39#include <wx/filedlg.h>
40#include <wx/log.h>
41#include "odb_defines.h"
42#include "odb_util.h"
43#include "pcb_io_odbpp.h"
44
45
46// Compute the side code for a via.
47std::string ODB_NET_LIST::ComputeViaAccessSide( BOARD* aBoard, int top_layer, int bottom_layer )
48{
49 // Easy case for through vias: top_layer is component, bottom_layer is
50 // solder, side code is Both
51 if( ( top_layer == F_Cu ) && ( bottom_layer == B_Cu ) )
52 return "B";
53
54 // Blind via, reachable from front, Top
55 if( top_layer == F_Cu )
56 return "T";
57
58 // Blind via, reachable from bottom, Down
59 if( bottom_layer == B_Cu )
60 return "D";
61
62 // It's a buried via, accessible from some inner layer, Inner
63 return "I";
64}
65
66
67void ODB_NET_LIST::InitNetPoints( std::map<size_t, std::vector<ODB_NET_RECORD>>& aRecords )
68{
69 for( const FAB_TEST_POINT& point : FabTestPoints( *m_board ) )
70 {
71 ODB_NET_RECORD record;
72 record.testpoint = false;
73 record.is_via = point.m_via != nullptr;
74 record.x_location = point.m_position.x;
75 record.y_location = -point.m_position.y;
76 record.soldermask = 3;
77 record.epoint = point.m_netEnd ? "e" : "m";
78
79 if( point.m_frontMask )
80 record.soldermask &= ~1;
81
82 if( point.m_backMask )
83 record.soldermask &= ~2;
84
85 if( const PCB_VIA* via = point.m_via )
86 {
87 PCB_LAYER_ID top, bottom;
88 via->LayerPair( &top, &bottom );
89 record.side = ComputeViaAccessSide( m_board, top, bottom );
90
91 if( record.side == "I" )
92 continue;
93
94 record.refdes = "VIA";
95 record.smd = false;
96 record.hole = true;
97 record.drill_radius = point.m_drill / 2;
98 record.mechanical = false;
99 record.x_size = 0;
100 record.y_size = 0;
101 }
102 else
103 {
104 const PAD* pad = point.m_pad;
105
106 if( !point.m_front && !point.m_back )
107 continue;
108
109 record.side = point.m_front && point.m_back ? "B" : point.m_front ? "T" : "D";
110 record.refdes = pad->GetParentFootprint()->GetReference();
111 record.hole = pad->HasHole();
112 record.drill_radius = record.hole ? point.m_drill / 2 : 0;
113 record.smd = pad->GetAttribute() == PAD_ATTRIB::SMD || pad->GetAttribute() == PAD_ATTRIB::CONN;
114 record.mechanical = pad->GetAttribute() == PAD_ATTRIB::NPTH;
116
117 // The land size is written only for undrilled pads, which are single-sided, so
118 // the access side gives the copper to report
119 PCB_LAYER_ID sideLayer = pad->Padstack().EffectiveLayerFor( record.side == "D" ? B_Cu : F_Cu );
120 record.x_size = pad->GetSize( sideLayer ).x;
121
122 // A circle's stored second size can differ from its diameter
123 if( pad->GetShape( sideLayer ) == PAD_SHAPE::CIRCLE )
124 record.y_size = record.x_size;
125 else
126 record.y_size = pad->GetSize( sideLayer ).y;
127 }
128
129 aRecords[point.m_netCode].push_back( record );
130 }
131}
132
133
134void ODB_NET_LIST::WriteNetPointRecords( std::map<size_t, std::vector<ODB_NET_RECORD>>& aRecords,
135 std::ostream& aStream,
136 const ODB_FORMAT& aFormat )
137{
138 aStream << "H optimize n staggered n" << std::endl;
139 aStream << ODB_UNITS << "=" << aFormat.m_unitsStr << std::endl;
140
141 for( const auto& [key, vec] : aRecords )
142 {
143 aStream << "$" << key << " " << m_plugin->GetLegalNetName( static_cast<int>( key ) ) << std::endl;
144 }
145
146 aStream << "#" << std::endl << "#Netlist points" << std::endl << "#" << std::endl;
147
148 for( const auto& [key, vec] : aRecords )
149 {
150 for( const auto& net_point : vec )
151 {
152 aStream << key << " ";
153
154 if( net_point.hole )
155 aStream << ODB::Data2String( aFormat, net_point.drill_radius );
156 else
157 aStream << "0.002";
158
159 aStream << " " << ODB::Data2String( aFormat, net_point.x_location - aFormat.m_originOffset.x ) << " "
160 << ODB::Data2String( aFormat, net_point.y_location + aFormat.m_originOffset.y ) << " "
161 << net_point.side << " ";
162
163 if( !net_point.hole )
164 aStream << ODB::Data2String( aFormat, net_point.x_size ) << " "
165 << ODB::Data2String( aFormat, net_point.y_size ) << " ";
166
167 std::string exp;
168
169 if( net_point.soldermask == 3 )
170 exp = "c";
171 else if( net_point.soldermask == 2 )
172 exp = "s";
173 else if( net_point.soldermask == 1 )
174 exp = "p";
175 else if( net_point.soldermask == 0 )
176 exp = "e";
177
178 aStream << net_point.epoint << " " << exp;
179
180 if( net_point.hole )
181 aStream << " staggered 0 0 0";
182
183 if( net_point.is_via )
184 aStream << " v";
185
186 if( net_point.testpoint )
187 aStream << " t";
188
189 aStream << std::endl;
190 }
191 }
192}
193
194
195void ODB_NET_LIST::Write( std::ostream& aStream, const ODB_FORMAT& aFormat )
196{
197 std::map<size_t, std::vector<ODB_NET_RECORD>> net_point_records;
198
199 InitNetPoints( net_point_records );
200
201 WriteNetPointRecords( net_point_records, aStream, aFormat );
202}
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:410
BOARD * m_board
Definition odb_netlist.h:68
std::string ComputeViaAccessSide(BOARD *aBoard, int top_layer, int bottom_layer)
void WriteNetPointRecords(std::map< size_t, std::vector< ODB_NET_RECORD > > &aRecords, std::ostream &aStream, const ODB_FORMAT &aFormat)
Writes a list of records to the given output stream.
PCB_IO_ODBPP * m_plugin
Definition odb_netlist.h:69
void Write(std::ostream &aStream, const ODB_FORMAT &aFormat)
void InitNetPoints(std::map< size_t, std::vector< ODB_NET_RECORD > > &aRecords)
Definition pad.h:61
This file is part of the common library.
FAB_PAD_ROLE GetFabPadRole(const PAD &aPad)
Explicit fabrication properties take precedence over the unnumbered NPTH fallback.
Definition fab_pin.cpp:26
std::vector< FAB_TEST_POINT > FabTestPoints(const BOARD &aBoard)
The caller must build board connectivity before enumerating net endpoints.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ B_Cu
Definition layer_ids.h:61
@ F_Cu
Definition layer_ids.h:60
This file contains miscellaneous commonly used macros and functions.
wxString Data2String(const ODB_FORMAT &aFormat, double aVal)
Definition odb_util.cpp:406
#define ODB_UNITS
Definition odb_defines.h:28
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:102
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:98
@ CONN
Like smd, does not appear on the solder paste layer (default) Note: also has a special attribute in G...
Definition padstack.h:99
VECTOR2I m_originOffset
Definition odb_util.h:77
std::string m_unitsStr
Definition odb_util.h:73
std::string side
Definition odb_netlist.h:36
std::string epoint
Definition odb_netlist.h:46
std::string refdes
Definition odb_netlist.h:33
KIBIS top(path, &reporter)
Definition of file extensions used in Kicad.