KiCad PCB EDA Suite
Loading...
Searching...
No Matches
export_d356.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 (C) 2011-2013 Lorenzo Marcantonio <[email protected]>
5 * Copyright (C) 2004-2023 KiCad Developers, see change_log.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU 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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
30#include <confirm.h>
31#include <gestfich.h>
32#include <kiface_base.h>
33#include <pcb_edit_frame.h>
34#include <trigo.h>
35#include <build_version.h>
36#include <macros.h>
38#include <locale_io.h>
39#include <board.h>
41#include <footprint.h>
42#include <layer_range.h>
43#include <pad.h>
44#include <pcb_track.h>
45#include <vector>
46#include <cctype>
47#include <math/util.h> // for KiROUND
48#include <export_d356.h>
49#include <wx/filedlg.h>
50
51
52
53// Compute the access code for a pad. Returns -1 if there is no copper
54static int compute_pad_access_code( BOARD *aPcb, LSET aLayerMask )
55{
56 // Non-copper is not interesting here
57 aLayerMask &= LSET::AllCuMask();
58 if( !aLayerMask.any() )
59 return -1;
60
61 // Traditional TH pad
62 if( aLayerMask[F_Cu] && aLayerMask[B_Cu] )
63 return 0;
64
65 // Front SMD pad
66 if( aLayerMask[F_Cu] )
67 return 1;
68
69 // Back SMD pad
70 if( aLayerMask[B_Cu] )
71 return aPcb->GetCopperLayerCount();
72
73 // OK, we have an inner-layer only pad (and I have no idea about
74 // what could be used for); anyway, find the first copper layer
75 // it's on
76 for( PCB_LAYER_ID layer : LAYER_RANGE( In1_Cu, B_Cu, aPcb->GetCopperLayerCount() ) )
77 {
78 if( aLayerMask[layer] )
79 return layer + 1;
80 }
81
82 // This shouldn't happen
83 return -1;
84}
85
86/* Convert and clamp a size from IU to decimils */
87static int iu_to_d356(int iu, int clamp)
88{
89 int val = KiROUND( iu / ( pcbIUScale.IU_PER_MILS / 10 ) );
90 if( val > clamp ) return clamp;
91 if( val < -clamp ) return -clamp;
92 return val;
93}
94
95/* Extract the D356 record from the footprints (pads) */
96static void build_pad_testpoints( BOARD *aPcb, std::vector <D356_RECORD>& aRecords )
97{
98 VECTOR2I origin = aPcb->GetDesignSettings().GetAuxOrigin();
99
100 for( FOOTPRINT* footprint : aPcb->Footprints() )
101 {
102 for( PAD* pad : footprint->Pads() )
103 {
104 D356_RECORD rk;
105 rk.access = compute_pad_access_code( aPcb, pad->GetLayerSet() );
106
107 // It could be a mask only pad, we only handle pads with copper here
108 if( rk.access != -1 )
109 {
110 rk.netname = pad->GetNetname();
111 rk.pin = pad->GetNumber();
112 rk.refdes = footprint->GetReference();
113 rk.midpoint = false; // XXX MAYBE need to be computed (how?)
114 const VECTOR2I& drill = pad->GetDrillSize();
115 rk.drill = std::min( drill.x, drill.y );
116 rk.hole = (rk.drill != 0);
117 rk.smd = pad->GetAttribute() == PAD_ATTRIB::SMD
118 || pad->GetAttribute() == PAD_ATTRIB::CONN;
119 rk.mechanical = ( pad->GetAttribute() == PAD_ATTRIB::NPTH );
120 rk.x_location = pad->GetPosition().x - origin.x;
121 rk.y_location = origin.y - pad->GetPosition().y;
122
123 PCB_LAYER_ID accessLayer = footprint->IsFlipped() ? B_Cu : F_Cu;
124 rk.x_size = pad->GetSize( accessLayer ).x;
125
126 // Rule: round pads have y = 0
127 if( pad->GetShape( accessLayer ) == PAD_SHAPE::CIRCLE )
128 rk.y_size = 0;
129 else
130 rk.y_size = pad->GetSize( accessLayer ).y;
131
132 rk.rotation = - pad->GetOrientation().AsDegrees();
133
134 if( rk.rotation < 0 )
135 rk.rotation += 360;
136
137 // the value indicates which sides are *not* accessible
138 rk.soldermask = 3;
139
140 if( pad->GetLayerSet()[F_Mask] )
141 rk.soldermask &= ~1;
142
143 if( pad->GetLayerSet()[B_Mask] )
144 rk.soldermask &= ~2;
145
146 aRecords.push_back( rk );
147 }
148 }
149 }
150}
151
152/* Compute the access code for a via. In D-356 layers are numbered from 1 up,
153 where '1' is the 'primary side' (usually the component side);
154 '0' means 'both sides', and other layers follows in an unspecified order */
155static int via_access_code( BOARD *aPcb, int top_layer, int bottom_layer )
156{
157 // Easy case for through vias: top_layer is component, bottom_layer is
158 // solder, access code is 0
159 if( (top_layer == F_Cu) && (bottom_layer == B_Cu) )
160 return 0;
161
162 // Blind via, reachable from front
163 if( top_layer == F_Cu )
164 return 1;
165
166 // Blind via, reachable from bottom
167 if( bottom_layer == B_Cu )
168 return aPcb->GetCopperLayerCount();
169
170 // It's a buried via, accessible from some inner layer
171 // (maybe could be used for testing before laminating? no idea)
172 return bottom_layer + 1; // XXX is this correct?
173}
174
175/* Extract the D356 record from the vias */
176static void build_via_testpoints( BOARD *aPcb, std::vector <D356_RECORD>& aRecords )
177{
178 VECTOR2I origin = aPcb->GetDesignSettings().GetAuxOrigin();
179
180 // Enumerate all the track segments and keep the vias
181 for( auto track : aPcb->Tracks() )
182 {
183 if( track->Type() == PCB_VIA_T )
184 {
185 PCB_VIA *via = static_cast<PCB_VIA*>( track );
186 NETINFO_ITEM *net = track->GetNet();
187
188 D356_RECORD rk;
189 rk.smd = false;
190 rk.hole = true;
191 if( net )
192 rk.netname = net->GetNetname();
193 else
194 rk.netname = wxEmptyString;
195 rk.refdes = wxT("VIA");
196 rk.pin = wxT("");
197 rk.midpoint = true; // Vias are always midpoints
198 rk.drill = via->GetDrillValue();
199 rk.mechanical = false;
200
201 PCB_LAYER_ID top_layer, bottom_layer;
202
203 via->LayerPair( &top_layer, &bottom_layer );
204
205 rk.access = via_access_code( aPcb, top_layer, bottom_layer );
206 rk.x_location = via->GetPosition().x - origin.x;
207 rk.y_location = origin.y - via->GetPosition().y;
208
209 // The record has a single size for vias, so take the smaller of the front and back
210 if( via->Padstack().Mode() != PADSTACK::MODE::NORMAL )
211 rk.x_size = std::min( via->GetWidth( F_Cu ), via->GetWidth( B_Cu ) );
212 else
213 rk.x_size = via->GetWidth( PADSTACK::ALL_LAYERS );
214
215 rk.y_size = 0; // Round so height = 0
216 rk.rotation = 0;
217
218 if( via->IsTented( F_Mask ) )
219 rk.soldermask |= 1;
220 if( via->IsTented( B_Mask ) )
221 rk.soldermask |= 2;
222
223 aRecords.push_back( rk );
224 }
225 }
226}
227
228/* Add a new netname to the d356 canonicalized list */
229static const wxString intern_new_d356_netname( const wxString &aNetname,
230 std::map<wxString, wxString> &aMap, std::set<wxString> &aSet )
231{
232 wxString canon;
233
234 for( size_t ii = 0; ii < aNetname.Len(); ++ii )
235 {
236 // Rule: we can only use the standard ASCII, control excluded
237 wxUniChar ch = aNetname[ii];
238
239 if( ch > 126 || !std::isgraph( static_cast<unsigned char>( ch ) ) )
240 ch = '?';
241
242 canon += ch;
243 }
244
245 // Rule: only uppercase (unofficial, but known to give problems
246 // otherwise)
247 canon.MakeUpper();
248
249 // Rule: maximum length is 14 characters, otherwise we keep the tail
250 if( canon.size() > 14 )
251 {
252 canon = canon.Right( 14 );
253 }
254
255 // Check if it's still unique
256 if( aSet.count( canon ) )
257 {
258 // Nope, need to uniquify it, trim it more and add a number
259 wxString base( canon );
260 if( base.size() > 10 )
261 {
262 base = base.Right( 10 );
263 }
264
265 int ctr = 0;
266 do
267 {
268 ++ctr;
269 canon = base;
270 canon << '#' << ctr;
271 } while ( aSet.count( canon ) );
272 }
273
274 // Register it
275 aMap[aNetname] = canon;
276 aSet.insert( canon );
277 return canon;
278}
279
280/* Write all the accumuled data to the file in D356 format */
281void IPC356D_WRITER::write_D356_records( std::vector <D356_RECORD> &aRecords, FILE* aFile )
282{
283 // Sanified and shorted network names and set of short names
284 std::map<wxString, wxString> d356_net_map;
285 std::set<wxString> d356_net_set;
286
287 for( unsigned i = 0; i < aRecords.size(); i++ )
288 {
289 D356_RECORD &rk = aRecords[i];
290
291 // Try to sanify the network name (there are limits on this), if
292 // not already done. Also 'empty' net are marked as N/C, as
293 // specified.
294 wxString d356_net( wxT( "N/C" ) );
295
296 if( !rk.netname.empty() )
297 {
298 d356_net = d356_net_map[rk.netname];
299
300 if( d356_net.empty() )
301 d356_net = intern_new_d356_netname( rk.netname, d356_net_map, d356_net_set );
302 }
303
304 // Choose the best record type
305 int rktype;
306
307 if( rk.smd )
308 rktype = 327;
309 else
310 {
311 if( rk.mechanical )
312 rktype = 367;
313 else
314 rktype = 317;
315 }
316
317 // Operation code, signal and component
318 fprintf( aFile, "%03d%-14.14s %-6.6s%c%-4.4s%c",
319 rktype, TO_UTF8(d356_net),
320 TO_UTF8(rk.refdes),
321 rk.pin.empty()?' ':'-',
322 TO_UTF8(rk.pin),
323 rk.midpoint?'M':' ' );
324
325 // Hole definition
326 if( rk.hole )
327 {
328 fprintf( aFile, "D%04d%c",
329 iu_to_d356( rk.drill, 9999 ),
330 rk.mechanical ? 'U':'P' );
331 }
332 else
333 fprintf( aFile, " " );
334
335 // Test point access
336 fprintf( aFile, "A%02dX%+07dY%+07dX%04dY%04dR%03d",
337 rk.access,
338 iu_to_d356( rk.x_location, 999999 ),
339 iu_to_d356( rk.y_location, 999999 ),
340 iu_to_d356( rk.x_size, 9999 ),
341 iu_to_d356( rk.y_size, 9999 ),
342 rk.rotation );
343
344 // Soldermask
345 fprintf( aFile, "S%d\n", rk.soldermask );
346 }
347}
348
349
350void IPC356D_WRITER::Write( const wxString& aFilename )
351{
352 FILE* file = nullptr;
353 LOCALE_IO toggle; // Switch the locale to standard C
354
355 if( ( file = wxFopen( aFilename, wxT( "wt" ) ) ) == nullptr )
356 {
357 wxString details;
358 details.Printf( wxT( "The file %s could not be opened for writing." ), aFilename );
359 DisplayErrorMessage( m_parent, wxT( "Could not write IPC-356D file!" ), details );
360 return;
361 }
362
363 // This will contain everything needed for the 356 file
364 std::vector<D356_RECORD> d356_records;
365
366 build_via_testpoints( m_pcb, d356_records );
367
368 build_pad_testpoints( m_pcb, d356_records );
369
370 // Code 00 AFAIK is ASCII, CUST 0 is decimils/degrees
371 // CUST 1 would be metric but gerbtool simply ignores it!
372 fprintf( file, "P CODE 00\n" );
373 fprintf( file, "P UNITS CUST 0\n" );
374 fprintf( file, "P arrayDim N\n" );
375 write_D356_records( d356_records, file );
376 fprintf( file, "999\n" );
377
378 fclose( file );
379}
380
381
382void PCB_EDIT_FRAME::GenD356File( wxCommandEvent& aEvent )
383{
384 wxFileName fn = GetBoard()->GetFileName();
385 wxString ext, wildcard;
386
388 wildcard = FILEEXT::IpcD356FileWildcard();
389 fn.SetExt( ext );
390
391 wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
392
393 wxFileDialog dlg( this, _( "Export D-356 Test File" ), pro_dir,
394 fn.GetFullName(), wildcard,
395 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
396
397 if( dlg.ShowModal() == wxID_CANCEL )
398 return;
399
400 IPC356D_WRITER writer( GetBoard(), this );
401
402 writer.Write( dlg.GetPath() );
403}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
const VECTOR2I & GetAuxOrigin()
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
int GetCopperLayerCount() const
Definition: board.cpp:741
const FOOTPRINTS & Footprints() const
Definition: board.h:331
const TRACKS & Tracks() const
Definition: board.h:329
const wxString & GetFileName() const
Definition: board.h:327
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:895
Wrapper to expose an API for writing IPC-D356 files.
Definition: export_d356.h:54
void Write(const wxString &aFilename)
Generates and writes the netlist to a given path.
wxWindow * m_parent
Definition: export_d356.h:75
void write_D356_records(std::vector< D356_RECORD > &aRecords, FILE *aFile)
Writes a list of records to the given output stream.
BOARD * m_pcb
Definition: export_d356.h:74
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:36
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:686
Handle the data for a net.
Definition: netinfo.h:56
const wxString & GetNetname() const
Definition: netinfo.h:114
@ NORMAL
Shape is the same on all layers.
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition: padstack.h:144
Definition: pad.h:54
BOARD * GetBoard() const
void GenD356File(wxCommandEvent &event)
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
This file is part of the common library.
#define _(s)
static void build_pad_testpoints(BOARD *aPcb, std::vector< D356_RECORD > &aRecords)
Definition: export_d356.cpp:96
static void build_via_testpoints(BOARD *aPcb, std::vector< D356_RECORD > &aRecords)
static int iu_to_d356(int iu, int clamp)
Definition: export_d356.cpp:87
static const wxString intern_new_d356_netname(const wxString &aNetname, std::map< wxString, wxString > &aMap, std::set< wxString > &aSet)
static int via_access_code(BOARD *aPcb, int top_layer, int bottom_layer)
static int compute_pad_access_code(BOARD *aPcb, LSET aLayerMask)
Definition: export_d356.cpp:54
static const std::string IpcD356FileExtension
static wxString IpcD356FileWildcard()
PROJECT & Prj()
Definition: kicad.cpp:597
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
@ In1_Cu
Definition: layer_ids.h:66
@ F_Cu
Definition: layer_ids.h:64
This file contains miscellaneous commonly used macros and functions.
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
@ SMD
Smd pad, appears on the solder paste layer (default)
@ CONN
Like smd, does not appear on the solder paste layer (default) Note: also has a special attribute in G...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:398
int x_location
Definition: export_d356.h:42
wxString refdes
Definition: export_d356.h:34
bool midpoint
Definition: export_d356.h:36
bool mechanical
Definition: export_d356.h:38
int y_location
Definition: export_d356.h:43
wxString pin
Definition: export_d356.h:35
int soldermask
Definition: export_d356.h:40
wxString netname
Definition: export_d356.h:33
const double IU_PER_MILS
Definition: base_units.h:77
@ 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.