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
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>
50
51#include <wx/checkbox.h>
52#include <wx/filedlg.h>
53#include <wx/filedlgcustomize.h>
54#include <wx/msgdlg.h>
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 rk.midpoint = false; // XXX MAYBE need to be computed (how?)
123 const VECTOR2I& drill = pad->GetDrillSize();
124 rk.drill = std::min( drill.x, drill.y );
125 rk.hole = (rk.drill != 0);
126 rk.smd = pad->GetAttribute() == PAD_ATTRIB::SMD
127 || pad->GetAttribute() == PAD_ATTRIB::CONN;
128 rk.mechanical = ( pad->GetAttribute() == PAD_ATTRIB::NPTH );
129 rk.x_location = pad->GetPosition().x - origin.x;
130 rk.y_location = origin.y - pad->GetPosition().y;
131
132 PCB_LAYER_ID accessLayer = footprint->IsFlipped() ? B_Cu : F_Cu;
133 rk.x_size = pad->GetSize( accessLayer ).x;
134
135 // Rule: round pads have y = 0
136 if( pad->GetShape( accessLayer ) == PAD_SHAPE::CIRCLE )
137 rk.y_size = 0;
138 else
139 rk.y_size = pad->GetSize( accessLayer ).y;
140
141 rk.rotation = - pad->GetOrientation().AsDegrees();
142
143 if( rk.rotation < 0 )
144 rk.rotation += 360;
145
146 // the value indicates which sides are *not* accessible
147 rk.soldermask = 3;
148
149 if( pad->GetLayerSet()[F_Mask] )
150 rk.soldermask &= ~1;
151
152 if( pad->GetLayerSet()[B_Mask] )
153 rk.soldermask &= ~2;
154
155 aRecords.push_back( std::move( rk ) );
156 }
157 }
158}
159
160/* Compute the access code for a via. In D-356 layers are numbered from 1 up,
161 where '1' is the 'primary side' (usually the component side);
162 '0' means 'both sides', and other layers follows in an unspecified order */
163static int via_access_code( BOARD *aPcb, int top_layer, int bottom_layer )
164{
165 // Easy case for through vias: top_layer is component, bottom_layer is
166 // solder, access code is 0
167 if( (top_layer == F_Cu) && (bottom_layer == B_Cu) )
168 return 0;
169
170 // Blind via, reachable from front
171 if( top_layer == F_Cu )
172 return 1;
173
174 // Blind via, reachable from bottom
175 if( bottom_layer == B_Cu )
176 return aPcb->GetCopperLayerCount();
177
178 // It's a buried via, accessible from some inner layer
179 // (maybe could be used for testing before laminating? no idea)
180 return ( top_layer / 2 ) + 1;
181}
182
183/* Extract the D356 record from the vias */
184static void build_via_testpoints( BOARD *aPcb, std::vector <D356_RECORD>& aRecords )
185{
186 VECTOR2I origin = aPcb->GetDesignSettings().GetAuxOrigin();
187
188 // Enumerate all the track segments and keep the vias
189 for( auto track : aPcb->Tracks() )
190 {
191 if( track->Type() == PCB_VIA_T )
192 {
193 PCB_VIA *via = static_cast<PCB_VIA*>( track );
194 NETINFO_ITEM *net = track->GetNet();
195
196 D356_RECORD rk;
197 rk.smd = false;
198 rk.hole = true;
199 if( net )
200 rk.netname = net->GetNetname();
201 else
202 rk.netname = wxEmptyString;
203 rk.refdes = wxT("VIA");
204 rk.pin = wxT("");
205 rk.midpoint = true; // Vias are always midpoints
206 rk.drill = via->GetDrillValue();
207 rk.mechanical = false;
208
209 PCB_LAYER_ID top_layer, bottom_layer;
210
211 via->LayerPair( &top_layer, &bottom_layer );
212
213 rk.access = via_access_code( aPcb, top_layer, bottom_layer );
214 rk.x_location = via->GetPosition().x - origin.x;
215 rk.y_location = origin.y - via->GetPosition().y;
216
217 // The record has a single size for vias, so take the smaller of the front and back
218 if( via->Padstack().Mode() != PADSTACK::MODE::NORMAL )
219 rk.x_size = std::min( via->GetWidth( F_Cu ), via->GetWidth( B_Cu ) );
220 else
221 rk.x_size = via->GetWidth( PADSTACK::ALL_LAYERS );
222
223 rk.y_size = 0; // Round so height = 0
224 rk.rotation = 0;
225
226 if( via->IsTented( F_Mask ) )
227 rk.soldermask |= 1;
228 if( via->IsTented( B_Mask ) )
229 rk.soldermask |= 2;
230
231 aRecords.push_back( rk );
232 }
233 }
234}
235
236/* Add a new netname to the d356 canonicalized list */
237static const wxString intern_new_d356_netname( const wxString &aNetname,
238 std::map<wxString, wxString> &aMap, std::set<wxString> &aSet )
239{
240 wxString canon;
241
242 for( size_t ii = 0; ii < aNetname.Len(); ++ii )
243 {
244 // Rule: we can only use the standard ASCII, control excluded
245 wxUniChar ch = aNetname[ii];
246
247 if( ch > 126 || !std::isgraph( static_cast<unsigned char>( ch ) ) )
248 ch = '?';
249
250 canon += ch;
251 }
252
253 // Rule: only uppercase (unofficial, but known to give problems
254 // otherwise)
255 canon.MakeUpper();
256
257 // Rule: maximum length is 14 characters, otherwise we keep the tail
258 if( canon.size() > 14 )
259 {
260 canon = canon.Right( 14 );
261 }
262
263 // Check if it's still unique
264 if( aSet.count( canon ) )
265 {
266 // Nope, need to uniquify it, trim it more and add a number
267 wxString base( canon );
268 if( base.size() > 10 )
269 {
270 base = base.Right( 10 );
271 }
272
273 int ctr = 0;
274 do
275 {
276 ++ctr;
277 canon = base;
278 canon << '#' << ctr;
279 } while ( aSet.count( canon ) );
280 }
281
282 // Register it
283 aMap[aNetname] = canon;
284 aSet.insert( canon );
285 return canon;
286}
287
288/* Write all the accumuled data to the file in D356 format */
289void IPC356D_WRITER::write_D356_records( std::vector <D356_RECORD> &aRecords, FILE* aFile )
290{
291 // Sanified and shorted network names and set of short names
292 std::map<wxString, wxString> d356_net_map;
293 std::set<wxString> d356_net_set;
294
295 for( unsigned i = 0; i < aRecords.size(); i++ )
296 {
297 D356_RECORD &rk = aRecords[i];
298
299 // Try to sanify the network name (there are limits on this), if
300 // not already done. Also 'empty' net are marked as N/C, as
301 // specified.
302 wxString d356_net( wxT( "N/C" ) );
303
304 if( !rk.netname.empty() )
305 {
306 d356_net = d356_net_map[rk.netname];
307
308 if( d356_net.empty() )
309 d356_net = intern_new_d356_netname( rk.netname, d356_net_map, d356_net_set );
310 }
311
312 // Choose the best record type
313 int rktype;
314
315 if( rk.smd )
316 rktype = 327;
317 else
318 {
319 if( rk.mechanical )
320 rktype = 367;
321 else if( rk.access == 0 ) // This is a through-hole via
322 rktype = 317;
323 else // All others are either blind or buried
324 rktype = 307;
325 }
326
327 // Operation code, signal and component
328 fprintf( aFile, "%03d%-14.14s %-6.6s%c%-4.4s%c",
329 rktype, TO_UTF8(d356_net),
330 TO_UTF8(rk.refdes),
331 rk.pin.empty()?' ':'-',
332 TO_UTF8(rk.pin),
333 rk.midpoint?'M':' ' );
334
335 // Hole definition
336 if( rk.hole )
337 {
338 fprintf( aFile, "D%04d%c",
339 iu_to_d356( rk.drill, 9999 ),
340 rk.mechanical ? 'U':'P' );
341 }
342 else
343 fprintf( aFile, " " );
344
345 // Test point access
346 fprintf( aFile, "A%02dX%+07dY%+07dX%04dY%04dR%03d",
347 rk.access,
348 iu_to_d356( rk.x_location, 999999 ),
349 iu_to_d356( rk.y_location, 999999 ),
350 iu_to_d356( rk.x_size, 9999 ),
351 iu_to_d356( rk.y_size, 9999 ),
352 rk.rotation );
353
354 // Soldermask
355 fprintf( aFile, "S%d\n", rk.soldermask );
356 }
357}
358
359
360bool IPC356D_WRITER::Write( const wxString& aFilename )
361{
362 FILE* file = nullptr;
363 LOCALE_IO toggle; // Switch the locale to standard C
364
365 if( ( file = wxFopen( aFilename, wxT( "wt" ) ) ) == nullptr )
366 {
367 return false;
368 }
369
370 // This will contain everything needed for the 356 file
371 std::vector<D356_RECORD> d356_records;
372
373 build_via_testpoints( m_pcb, d356_records );
374
375 build_pad_testpoints( m_pcb, d356_records );
376
377 // Code 00 AFAIK is ASCII, CUST 0 is decimils/degrees
378 // CUST 1 would be metric but gerbtool simply ignores it!
379 fprintf( file, "P CODE 00\n" );
380 fprintf( file, "P UNITS CUST 0\n" );
381 fprintf( file, "P arrayDim N\n" );
382 write_D356_records( d356_records, file );
383 fprintf( file, "999\n" );
384
385 fclose( file );
386
387 return true;
388}
389
390
391
392
393// Subclass for wxFileDialogCustomizeHook to add the checkbox
394class D365_CUSTOMIZE_HOOK : public wxFileDialogCustomizeHook
395{
396public:
397
398public:
399 D365_CUSTOMIZE_HOOK( bool aDoNotExportUnconnectedPads = false ) :
400 m_doNotExportUnconnectedPads( aDoNotExportUnconnectedPads ),
401 m_cb(nullptr)
402 {};
403
404 virtual void AddCustomControls( wxFileDialogCustomize& customizer ) override
405 {
406 m_cb = customizer.AddCheckBox( _( "Do not export unconnected pads" ) );
408 }
409
410 virtual void TransferDataFromCustomControls() override
411 {
413 }
414
416private:
417
419 wxFileDialogCheckBox* m_cb;
420
422};
423
424
426{
427 wxFileName fn = m_frame->GetBoard()->GetFileName();
428 wxString ext, wildcard;
429
431 wildcard = FILEEXT::IpcD356FileWildcard();
432 fn.SetExt( ext );
433
434 wxString pro_dir = wxPathOnly( m_frame->Prj().GetProjectFullName() );
435
436 wxFileDialog dlg( m_frame, _( "Generate IPC-D-356 netlist file" ), pro_dir, fn.GetFullName(),
437 wildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
439 dlg.SetCustomizeHook( customizeHook );
440
441 if( dlg.ShowModal() == wxID_CANCEL )
442 return 0;
443
444 IPC356D_WRITER writer( m_frame->GetBoard() );
445 bool doNotExportUnconnectedPads = customizeHook.GetDoNotExportUnconnectedPads();
446 writer.SetDoNotExportUnconnectedPads( doNotExportUnconnectedPads );
447 m_frame->GetPcbNewSettings()->m_ExportD356.doNotExportUnconnectedPads = doNotExportUnconnectedPads;
448
449 if( writer.Write( dlg.GetPath() ) )
450 {
451 DisplayInfoMessage( m_frame, wxString::Format( _( "IPC-D-356 netlist file created:\n'%s'." ),
452 dlg.GetPath() ) );
453 }
454 else
455 {
456 DisplayError( m_frame, wxString::Format( _( "Failed to create file '%s'." ),
457 dlg.GetPath() ) );
458 }
459
460 return 0;
461}
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:859
const FOOTPRINTS & Footprints() const
Definition: board.h:358
const TRACKS & Tracks() const
Definition: board.h:356
const wxString & GetFileName() const
Definition: board.h:354
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:1024
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.
BOARD * m_pcb
Definition: export_d356.h:85
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
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:56
const wxString & GetNetname() const
Definition: netinfo.h:114
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
Definition: netinfo.h:381
@ 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:145
Definition: pad.h:54
DIALOG_EXPORT_D356 m_ExportD356
PCBNEW_SETTINGS * GetPcbNewSettings() const
BOARD * GetBoard() const
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:143
Generic, UI-independent tool event.
Definition: tool_event.h:168
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)
Definition: export_d356.cpp:92
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:58
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.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:429
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.