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