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 The KiCad Developers, see AUTHORS.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, see <https://www.gnu.org/licenses/>.
19 */
20
25#include "export_d356.h"
26
27#include <vector>
28#include <cctype>
29
30#include <wx/checkbox.h>
31#include <wx/filedlg.h>
32#include <wx/filedlgcustomize.h>
33#include <wx/msgdlg.h>
34#include <kiplatform/ui.h>
35
36#include <confirm.h>
37#include <gestfich.h>
38#include <kiface_base.h>
39#include <pcb_edit_frame.h>
40#include <trigo.h>
41#include <build_version.h>
42#include <macros.h>
44#include <locale_io.h>
45#include <board.h>
47#include <footprint.h>
48#include <layer_range.h>
49#include <pad.h>
50#include <pcb_track.h>
51#include <string_utils.h>
52
53#include <math/util.h> // for KiROUND
55
56
57// Compute the access code for a pad. Returns -1 if there is no copper
58static int compute_pad_access_code( BOARD *aPcb, LSET aLayerMask )
59{
60 // Non-copper is not interesting here
61 aLayerMask &= LSET::AllCuMask();
62
63 if( !aLayerMask.any() )
64 return -1;
65
66 // Traditional TH pad
67 if( aLayerMask[F_Cu] && aLayerMask[B_Cu] )
68 return 0;
69
70 // Front SMD pad
71 if( aLayerMask[F_Cu] )
72 return 1;
73
74 // Back SMD pad
75 if( aLayerMask[B_Cu] )
76 return aPcb->GetCopperLayerCount();
77
78 // OK, we have an inner-layer only pad (and I have no idea about
79 // what could be used for); anyway, find the first copper layer
80 // it's on
81 for( PCB_LAYER_ID layer : LAYER_RANGE( In1_Cu, B_Cu, aPcb->GetCopperLayerCount() ) )
82 {
83 if( aLayerMask[layer] )
84 return layer + 1;
85 }
86
87 // This shouldn't happen
88 return -1;
89}
90
91/* Convert and clamp a size from IU to decimils */
92static int iu_to_d356(int iu, int clamp)
93{
94 int val = KiROUND( static_cast<double>( iu ) / ( pcbIUScale.IU_PER_MILS / 10.0 ) );
95 if( val > clamp ) return clamp;
96 if( val < -clamp ) return -clamp;
97 return val;
98}
99
100/* Extract the D356 record from the footprints (pads) */
101void IPC356D_WRITER::build_pad_testpoints( BOARD *aPcb, std::vector <D356_RECORD>& aRecords )
102{
103 VECTOR2I origin = aPcb->GetDesignSettings().GetAuxOrigin();
104
105 for( FOOTPRINT* footprint : aPcb->Footprints() )
106 {
107 for( PAD* pad : footprint->Pads() )
108 {
109 D356_RECORD rk;
110 rk.access = compute_pad_access_code( aPcb, pad->GetLayerSet() );
111
112 // It could be a mask only pad, we only handle pads with copper here
113 if( rk.access == -1 )
114 continue;
115
117 continue;
118
119 rk.netname = pad->GetNetname();
120 rk.pin = pad->GetNumber();
121 rk.refdes = footprint->GetReference();
122 // A named pad is a component terminal (end-net point); an unnamed copper
123 // feature is only reachable mid-net, so it is a midpoint per IPC-D-356A
124 rk.midpoint = rk.pin.IsEmpty();
125 const VECTOR2I& drill = pad->GetDrillSize();
126 rk.drill = std::min( drill.x, drill.y );
127 rk.hole = (rk.drill != 0);
128 rk.smd = pad->GetAttribute() == PAD_ATTRIB::SMD
129 || pad->GetAttribute() == PAD_ATTRIB::CONN;
130 rk.mechanical = ( pad->GetAttribute() == PAD_ATTRIB::NPTH );
131 rk.x_location = pad->GetPosition().x - origin.x;
132 rk.y_location = origin.y - pad->GetPosition().y;
133
134 PCB_LAYER_ID accessLayer = footprint->IsFlipped() ? B_Cu : F_Cu;
135 rk.x_size = pad->GetSize( accessLayer ).x;
136
137 // Rule: round pads have y = 0
138 if( pad->GetShape( accessLayer ) == PAD_SHAPE::CIRCLE )
139 rk.y_size = 0;
140 else
141 rk.y_size = pad->GetSize( accessLayer ).y;
142
143 rk.rotation = - pad->GetOrientation().AsDegrees();
144
145 if( rk.rotation < 0 )
146 rk.rotation += 360;
147
148 // the value indicates which sides are *not* accessible
149 rk.soldermask = 3;
150
151 if( pad->GetLayerSet()[F_Mask] )
152 rk.soldermask &= ~1;
153
154 if( pad->GetLayerSet()[B_Mask] )
155 rk.soldermask &= ~2;
156
157 aRecords.push_back( std::move( rk ) );
158 }
159 }
160}
161
162/* Compute the access code for a via. In D-356 layers are numbered from 1 up,
163 where '1' is the 'primary side' (usually the component side);
164 '0' means 'both sides', and other layers follows in an unspecified order */
165static int via_access_code( BOARD *aPcb, int top_layer, int bottom_layer )
166{
167 // Easy case for through vias: top_layer is component, bottom_layer is
168 // solder, access code is 0
169 if( (top_layer == F_Cu) && (bottom_layer == B_Cu) )
170 return 0;
171
172 // Blind via, reachable from front
173 if( top_layer == F_Cu )
174 return 1;
175
176 // Blind via, reachable from bottom
177 if( bottom_layer == B_Cu )
178 return aPcb->GetCopperLayerCount();
179
180 // It's a buried via, accessible from some inner layer
181 // (maybe could be used for testing before laminating? no idea)
182 return ( top_layer / 2 ) + 1;
183}
184
185/* Extract the D356 record from the vias */
186static void build_via_testpoints( BOARD *aPcb, std::vector <D356_RECORD>& aRecords )
187{
188 VECTOR2I origin = aPcb->GetDesignSettings().GetAuxOrigin();
189
190 // Enumerate all the track segments and keep the vias
191 for( auto track : aPcb->Tracks() )
192 {
193 if( track->Type() == PCB_VIA_T )
194 {
195 PCB_VIA *via = static_cast<PCB_VIA*>( track );
196 NETINFO_ITEM *net = track->GetNet();
197
198 D356_RECORD rk;
199 rk.smd = false;
200 rk.hole = true;
201 if( net )
202 rk.netname = net->GetNetname();
203 else
204 rk.netname = wxEmptyString;
205 rk.refdes = wxT("VIA");
206 rk.pin = wxT("");
207 rk.midpoint = true; // Vias are always midpoints
208 rk.drill = via->GetDrillValue();
209 rk.mechanical = false;
210
211 PCB_LAYER_ID top_layer, bottom_layer;
212
213 via->LayerPair( &top_layer, &bottom_layer );
214
215 rk.access = via_access_code( aPcb, top_layer, bottom_layer );
216 rk.x_location = via->GetPosition().x - origin.x;
217 rk.y_location = origin.y - via->GetPosition().y;
218
219 // The record has a single size for vias, so take the smaller of the front and back
220 if( via->Padstack().Mode() != PADSTACK::MODE::NORMAL )
221 rk.x_size = std::min( via->GetWidth( F_Cu ), via->GetWidth( B_Cu ) );
222 else
223 rk.x_size = via->GetWidth( PADSTACK::ALL_LAYERS );
224
225 rk.y_size = 0; // Round so height = 0
226 rk.rotation = 0;
227
228 // the value indicates which sides are *not* accessible
229 rk.soldermask = 0;
230
231 if( via->IsTented( F_Mask ) )
232 rk.soldermask |= 1;
233
234 if( via->IsTented( B_Mask ) )
235 rk.soldermask |= 2;
236
237 aRecords.push_back( rk );
238 }
239 }
240}
241
242/* Add a new netname to the d356 canonicalized list */
243static const wxString intern_new_d356_netname( const wxString &aNetname,
244 std::map<wxString, wxString> &aMap, std::set<wxString> &aSet )
245{
246 wxString canon;
247
248 for( size_t ii = 0; ii < aNetname.Len(); ++ii )
249 {
250 // Rule: we can only use the standard ASCII, control excluded
251 wxUniChar ch = aNetname[ii];
252
253 if( ch > 126 || !std::isgraph( static_cast<unsigned char>( ch ) ) )
254 ch = '?';
255
256 canon += ch;
257 }
258
259 // Rule: only uppercase (unofficial, but known to give problems
260 // otherwise)
261 canon.MakeUpper();
262
263 // Rule: maximum length is 14 characters, otherwise we keep the tail
264 if( canon.size() > 14 )
265 {
266 canon = canon.Right( 14 );
267 }
268
269 // Check if it's still unique
270 if( aSet.count( canon ) )
271 {
272 // Nope, need to uniquify it, trim it more and add a number
273 wxString base( canon );
274 if( base.size() > 10 )
275 {
276 base = base.Right( 10 );
277 }
278
279 int ctr = 0;
280 do
281 {
282 ++ctr;
283 canon = base;
284 canon << '#' << ctr;
285 } while ( aSet.count( canon ) );
286 }
287
288 // Register it
289 aMap[aNetname] = canon;
290 aSet.insert( canon );
291 return canon;
292}
293
294/* Write all the accumuled data to the file in D356 format */
295void IPC356D_WRITER::write_D356_records( std::vector <D356_RECORD> &aRecords, FILE* aFile )
296{
297 // Sanified and shorted network names and set of short names
298 std::map<wxString, wxString> d356_net_map;
299 std::set<wxString> d356_net_set;
300
301 for( unsigned i = 0; i < aRecords.size(); i++ )
302 {
303 D356_RECORD &rk = aRecords[i];
304
305 // Try to sanify the network name (there are limits on this), if
306 // not already done. Also 'empty' net are marked as N/C, as
307 // specified.
308 wxString d356_net( wxT( "N/C" ) );
309
310 if( !rk.netname.empty() )
311 {
312 d356_net = d356_net_map[rk.netname];
313
314 if( d356_net.empty() )
315 d356_net = intern_new_d356_netname( rk.netname, d356_net_map, d356_net_set );
316 }
317
318 // Choose the best record type
319 int rktype;
320
321 if( rk.smd )
322 rktype = 327;
323 else
324 {
325 if( rk.mechanical )
326 rktype = 367;
327 else if( rk.access == 0 ) // This is a through-hole via
328 rktype = 317;
329 else // All others are either blind or buried
330 rktype = 307;
331 }
332
333 // Operation code, signal and component
334 fprintf( aFile, "%03d%-14.14s %-6.6s%c%-4.4s%c",
335 rktype, TO_UTF8(d356_net),
336 TO_UTF8(rk.refdes),
337 rk.pin.empty()?' ':'-',
338 TO_UTF8(rk.pin),
339 rk.midpoint?'M':' ' );
340
341 // Hole definition
342 if( rk.hole )
343 {
344 fprintf( aFile, "D%04d%c",
345 iu_to_d356( rk.drill, 9999 ),
346 rk.mechanical ? 'U':'P' );
347 }
348 else
349 fprintf( aFile, " " );
350
351 // Test point access
352 fprintf( aFile, "A%02dX%+07dY%+07dX%04dY%04dR%03d",
353 rk.access,
354 iu_to_d356( rk.x_location, 999999 ),
355 iu_to_d356( rk.y_location, 999999 ),
356 iu_to_d356( rk.x_size, 9999 ),
357 iu_to_d356( rk.y_size, 9999 ),
358 rk.rotation );
359
360 // Soldermask
361 fprintf( aFile, "S%d\n", rk.soldermask );
362 }
363}
364
365
366bool IPC356D_WRITER::Write( const wxString& aFilename )
367{
368 FILE* file = nullptr;
369 LOCALE_IO toggle; // Switch the locale to standard C
370
371 if( ( file = wxFopen( aFilename, wxT( "wt" ) ) ) == nullptr )
372 {
373 return false;
374 }
375
376 // This will contain everything needed for the 356 file
377 std::vector<D356_RECORD> d356_records;
378
379 build_via_testpoints( m_pcb, d356_records );
380
381 build_pad_testpoints( m_pcb, d356_records );
382
383 // Code 00 AFAIK is ASCII, CUST 0 is decimils/degrees
384 // CUST 1 would be metric but gerbtool simply ignores it!
385 fprintf( file, "P CODE 00\n" );
386 fprintf( file, "P UNITS CUST 0\n" );
387 fprintf( file, "P arrayDim N\n" );
388 write_D356_records( d356_records, file );
389 fprintf( file, "999\n" );
390
391 fclose( file );
392
393 return true;
394}
395
396
397
398
399// Subclass for wxFileDialogCustomizeHook to add the checkbox
400class D365_CUSTOMIZE_HOOK : public wxFileDialogCustomizeHook
401{
402public:
403
404public:
405 D365_CUSTOMIZE_HOOK( bool aDoNotExportUnconnectedPads = false ) :
406 m_doNotExportUnconnectedPads( aDoNotExportUnconnectedPads ),
407 m_cb(nullptr)
408 {};
409
410 virtual void AddCustomControls( wxFileDialogCustomize& customizer ) override
411 {
412#ifdef __WXMAC__
413 customizer.AddStaticText( wxT( "\n\n" ) ); // Increase height of static box
414#endif
415
416 m_cb = customizer.AddCheckBox( _( "Do not export unconnected pads" ) );
418 }
419
420 virtual void TransferDataFromCustomControls() override
421 {
423 }
424
426private:
427
429 wxFileDialogCheckBox* m_cb;
430
432};
433
434
436{
437 wxFileName fn = m_frame->GetBoard()->GetFileName();
438 wxString ext, wildcard;
439
441 wildcard = FILEEXT::IpcD356FileWildcard();
442 fn.SetExt( ext );
443
444 wxString pro_dir = wxPathOnly( m_frame->Prj().GetProjectFullName() );
445
446 wxFileDialog dlg( m_frame, _( "Generate IPC-D-356 netlist file" ), pro_dir, fn.GetFullName(),
447 wildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
448 D365_CUSTOMIZE_HOOK customizeHook( m_frame->GetPcbNewSettings()->m_ExportD356.doNotExportUnconnectedPads );
449 dlg.SetCustomizeHook( customizeHook );
450
452
453 if( dlg.ShowModal() == wxID_CANCEL )
454 return 0;
455
456 IPC356D_WRITER writer( m_frame->GetBoard() );
457 bool doNotExportUnconnectedPads = customizeHook.GetDoNotExportUnconnectedPads();
458 writer.SetDoNotExportUnconnectedPads( doNotExportUnconnectedPads );
459 m_frame->GetPcbNewSettings()->m_ExportD356.doNotExportUnconnectedPads = doNotExportUnconnectedPads;
460
461 if( writer.Write( dlg.GetPath() ) )
462 {
463 DisplayInfoMessage( m_frame, wxString::Format( _( "IPC-D-356 netlist file created:\n'%s'." ),
464 dlg.GetPath() ) );
465 }
466 else
467 {
468 DisplayError( m_frame, wxString::Format( _( "Failed to create file '%s'." ),
469 dlg.GetPath() ) );
470 }
471
472 return 0;
473}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
const VECTOR2I & GetAuxOrigin() const
int GenD356File(const TOOL_EVENT &aEvent)
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
int GetCopperLayerCount() const
Definition board.cpp:985
const FOOTPRINTS & Footprints() const
Definition board.h:421
const TRACKS & Tracks() const
Definition board.h:419
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1149
virtual void TransferDataFromCustomControls() override
D365_CUSTOMIZE_HOOK(bool aDoNotExportUnconnectedPads=false)
wxDECLARE_NO_COPY_CLASS(D365_CUSTOMIZE_HOOK)
wxFileDialogCheckBox * m_cb
bool GetDoNotExportUnconnectedPads() const
virtual void AddCustomControls(wxFileDialogCustomize &customizer) override
Wrapper to expose an API for writing IPC-D356 files.
Definition export_d356.h:54
bool m_doNotExportUnconnectedPads
Definition export_d356.h:92
void write_D356_records(std::vector< D356_RECORD > &aRecords, FILE *aFile)
Writes a list of records to the given output stream.
void build_pad_testpoints(BOARD *aPcb, std::vector< D356_RECORD > &aRecords)
void SetDoNotExportUnconnectedPads(bool aDoNotExportUnconnectedPads)
Sets whether unconnected pads should be exported.
Definition export_d356.h:79
bool Write(const wxString &aFilename)
Generates and writes the netlist to a given path.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
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
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
Definition netinfo.h:256
@ NORMAL
Shape is the same on all layers.
Definition padstack.h:171
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
Generic, UI-independent tool event.
Definition tool_event.h:167
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition confirm.cpp:245
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
#define _(s)
static void build_via_testpoints(BOARD *aPcb, std::vector< D356_RECORD > &aRecords)
static int iu_to_d356(int iu, int clamp)
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)
static const std::string IpcD356FileExtension
static wxString IpcD356FileWildcard()
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
@ In1_Cu
Definition layer_ids.h:62
@ F_Cu
Definition layer_ids.h:60
This file contains miscellaneous commonly used macros and functions.
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:448
@ 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
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
wxString refdes
Definition export_d356.h:34
bool mechanical
Definition export_d356.h:38
wxString pin
Definition export_d356.h:35
wxString netname
Definition export_d356.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.