KiCad PCB EDA Suite
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 <l.marcantonio@logossrl.com>
5  * Copyright (C) 2004-2017 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_i.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 <pcbnew.h>
40 #include <board.h>
41 #include <footprint.h>
42 #include <track.h>
43 #include <vector>
44 #include <cctype>
45 #include <math/util.h> // for KiROUND
46 #include <export_d356.h>
47 
48 
49 
50 // Compute the access code for a pad. Returns -1 if there is no copper
51 static int compute_pad_access_code( BOARD *aPcb, LSET aLayerMask )
52 {
53  // Non-copper is not interesting here
54  aLayerMask &= LSET::AllCuMask();
55  if( !aLayerMask.any() )
56  return -1;
57 
58  // Traditional TH pad
59  if( aLayerMask[F_Cu] && aLayerMask[B_Cu] )
60  return 0;
61 
62  // Front SMD pad
63  if( aLayerMask[F_Cu] )
64  return 1;
65 
66  // Back SMD pad
67  if( aLayerMask[B_Cu] )
68  return aPcb->GetCopperLayerCount();
69 
70  // OK, we have an inner-layer only pad (and I have no idea about
71  // what could be used for); anyway, find the first copper layer
72  // it's on
73  for( LAYER_NUM layer = In1_Cu; layer < B_Cu; ++layer )
74  {
75  if( aLayerMask[layer] )
76  return layer + 1;
77  }
78 
79  // This shouldn't happen
80  return -1;
81 }
82 
83 /* Convert and clamp a size from IU to decimils */
84 static int iu_to_d356(int iu, int clamp)
85 {
86  int val = KiROUND( iu / ( IU_PER_MILS / 10 ) );
87  if( val > clamp ) return clamp;
88  if( val < -clamp ) return -clamp;
89  return val;
90 }
91 
92 /* Extract the D356 record from the footprints (pads) */
93 static void build_pad_testpoints( BOARD *aPcb, std::vector <D356_RECORD>& aRecords )
94 {
95  wxPoint origin = aPcb->GetDesignSettings().m_AuxOrigin;
96 
97  for( FOOTPRINT* footprint : aPcb->Footprints() )
98  {
99  for( PAD* pad : footprint->Pads() )
100  {
101  D356_RECORD rk;
102  rk.access = compute_pad_access_code( aPcb, pad->GetLayerSet() );
103 
104  // It could be a mask only pad, we only handle pads with copper here
105  if( rk.access != -1 )
106  {
107  rk.netname = pad->GetNetname();
108  rk.pin = pad->GetName();
109  rk.refdes = footprint->GetReference();
110  rk.midpoint = false; // XXX MAYBE need to be computed (how?)
111  const wxSize& drill = pad->GetDrillSize();
112  rk.drill = std::min( drill.x, drill.y );
113  rk.hole = (rk.drill != 0);
114  rk.smd = pad->GetAttribute() == PAD_ATTRIB_SMD;
115  rk.mechanical = ( pad->GetAttribute() == PAD_ATTRIB_NPTH );
116  rk.x_location = pad->GetPosition().x - origin.x;
117  rk.y_location = origin.y - pad->GetPosition().y;
118  rk.x_size = pad->GetSize().x;
119 
120  // Rule: round pads have y = 0
121  if( pad->GetShape() == PAD_SHAPE_CIRCLE )
122  rk.y_size = 0;
123  else
124  rk.y_size = pad->GetSize().y;
125 
126  rk.rotation = -KiROUND( pad->GetOrientation() ) / 10;
127  if( rk.rotation < 0 ) rk.rotation += 360;
128 
129  // the value indicates which sides are *not* accessible
130  rk.soldermask = 3;
131  if( pad->GetLayerSet()[F_Mask] )
132  rk.soldermask &= ~1;
133  if( pad->GetLayerSet()[B_Mask] )
134  rk.soldermask &= ~2;
135 
136  aRecords.push_back( rk );
137  }
138  }
139  }
140 }
141 
142 /* Compute the access code for a via. In D-356 layers are numbered from 1 up,
143  where '1' is the 'primary side' (usually the component side);
144  '0' means 'both sides', and other layers follows in an unspecified order */
145 static int via_access_code( BOARD *aPcb, int top_layer, int bottom_layer )
146 {
147  // Easy case for through vias: top_layer is component, bottom_layer is
148  // solder, access code is 0
149  if( (top_layer == F_Cu) && (bottom_layer == B_Cu) )
150  return 0;
151 
152  // Blind via, reachable from front
153  if( top_layer == F_Cu )
154  return 1;
155 
156  // Blind via, reachable from bottom
157  if( bottom_layer == B_Cu )
158  return aPcb->GetCopperLayerCount();
159 
160  // It's a buried via, accessible from some inner layer
161  // (maybe could be used for testing before laminating? no idea)
162  return bottom_layer + 1; // XXX is this correct?
163 }
164 
165 /* Extract the D356 record from the vias */
166 static void build_via_testpoints( BOARD *aPcb, std::vector <D356_RECORD>& aRecords )
167 {
168  wxPoint origin = aPcb->GetDesignSettings().m_AuxOrigin;
169 
170  // Enumerate all the track segments and keep the vias
171  for( auto track : aPcb->Tracks() )
172  {
173  if( track->Type() == PCB_VIA_T )
174  {
175  VIA *via = (VIA*) track;
176  NETINFO_ITEM *net = track->GetNet();
177 
178  D356_RECORD rk;
179  rk.smd = false;
180  rk.hole = true;
181  if( net )
182  rk.netname = net->GetNetname();
183  else
184  rk.netname = wxEmptyString;
185  rk.refdes = wxT("VIA");
186  rk.pin = wxT("");
187  rk.midpoint = true; // Vias are always midpoints
188  rk.drill = via->GetDrillValue();
189  rk.mechanical = false;
190 
191  PCB_LAYER_ID top_layer, bottom_layer;
192 
193  via->LayerPair( &top_layer, &bottom_layer );
194 
195  rk.access = via_access_code( aPcb, top_layer, bottom_layer );
196  rk.x_location = via->GetPosition().x - origin.x;
197  rk.y_location = origin.y - via->GetPosition().y;
198  rk.x_size = via->GetWidth();
199  rk.y_size = 0; // Round so height = 0
200  rk.rotation = 0;
201  rk.soldermask = 3; // XXX always tented?
202 
203  aRecords.push_back( rk );
204  }
205  }
206 }
207 
208 /* Add a new netname to the d356 canonicalized list */
209 static const wxString intern_new_d356_netname( const wxString &aNetname,
210  std::map<wxString, wxString> &aMap, std::set<wxString> &aSet )
211 {
212  wxString canon;
213 
214  for( size_t ii = 0; ii < aNetname.Len(); ++ii )
215  {
216  // Rule: we can only use the standard ASCII, control excluded
217  wxUniChar ch = aNetname[ii];
218 
219  if( ch > 126 || !std::isgraph( static_cast<unsigned char>( ch ) ) )
220  ch = '?';
221 
222  canon += ch;
223  }
224 
225  // Rule: only uppercase (unofficial, but known to give problems
226  // otherwise)
227  canon.MakeUpper();
228 
229  // Rule: maximum length is 14 characters, otherwise we keep the tail
230  if( canon.size() > 14 )
231  {
232  canon = canon.Right( 14 );
233  }
234 
235  // Check if it's still unique
236  if( aSet.count( canon ) )
237  {
238  // Nope, need to uniquify it, trim it more and add a number
239  wxString base( canon );
240  if( base.size() > 10 )
241  {
242  base = base.Right( 10 );
243  }
244 
245  int ctr = 0;
246  do
247  {
248  ++ctr;
249  canon = base;
250  canon << '#' << ctr;
251  } while ( aSet.count( canon ) );
252  }
253 
254  // Register it
255  aMap[aNetname] = canon;
256  aSet.insert( canon );
257  return canon;
258 }
259 
260 /* Write all the accumuled data to the file in D356 format */
261 void IPC356D_WRITER::write_D356_records( std::vector <D356_RECORD> &aRecords, FILE* aFile )
262 {
263  // Sanified and shorted network names and set of short names
264  std::map<wxString, wxString> d356_net_map;
265  std::set<wxString> d356_net_set;
266 
267  for( unsigned i = 0; i < aRecords.size(); i++ )
268  {
269  D356_RECORD &rk = aRecords[i];
270 
271  // Try to sanify the network name (there are limits on this), if
272  // not already done. Also 'empty' net are marked as N/C, as
273  // specified.
274  wxString d356_net( wxT( "N/C" ) );
275 
276  if( !rk.netname.empty() )
277  {
278  d356_net = d356_net_map[rk.netname];
279 
280  if( d356_net.empty() )
281  d356_net = intern_new_d356_netname( rk.netname, d356_net_map, d356_net_set );
282  }
283 
284  // Choose the best record type
285  int rktype;
286 
287  if( rk.smd )
288  rktype = 327;
289  else
290  {
291  if( rk.mechanical )
292  rktype = 367;
293  else
294  rktype = 317;
295  }
296 
297  // Operation code, signal and component
298  fprintf( aFile, "%03d%-14.14s %-6.6s%c%-4.4s%c",
299  rktype, TO_UTF8(d356_net),
300  TO_UTF8(rk.refdes),
301  rk.pin.empty()?' ':'-',
302  TO_UTF8(rk.pin),
303  rk.midpoint?'M':' ' );
304 
305  // Hole definition
306  if( rk.hole )
307  {
308  fprintf( aFile, "D%04d%c",
309  iu_to_d356( rk.drill, 9999 ),
310  rk.mechanical ? 'U':'P' );
311  }
312  else
313  fprintf( aFile, " " );
314 
315  // Test point access
316  fprintf( aFile, "A%02dX%+07dY%+07dX%04dY%04dR%03d",
317  rk.access,
318  iu_to_d356( rk.x_location, 999999 ),
319  iu_to_d356( rk.y_location, 999999 ),
320  iu_to_d356( rk.x_size, 9999 ),
321  iu_to_d356( rk.y_size, 9999 ),
322  rk.rotation );
323 
324  // Soldermask
325  fprintf( aFile, "S%d\n", rk.soldermask );
326  }
327 }
328 
329 
330 void IPC356D_WRITER::Write( const wxString& aFilename )
331 {
332  FILE* file = nullptr;
333  LOCALE_IO toggle; // Switch the locale to standard C
334 
335  if( ( file = wxFopen( aFilename, wxT( "wt" ) ) ) == nullptr )
336  {
337  wxString details;
338  details.Printf( "The file %s could not be opened for writing", aFilename );
339  DisplayErrorMessage( m_parent, "Could not write IPC-356D file!", details );
340  return;
341  }
342 
343  // This will contain everything needed for the 356 file
344  std::vector<D356_RECORD> d356_records;
345 
346  build_via_testpoints( m_pcb, d356_records );
347 
348  build_pad_testpoints( m_pcb, d356_records );
349 
350  // Code 00 AFAIK is ASCII, CUST 0 is decimils/degrees
351  // CUST 1 would be metric but gerbtool simply ignores it!
352  fprintf( file, "P CODE 00\n" );
353  fprintf( file, "P UNITS CUST 0\n" );
354  fprintf( file, "P arrayDim N\n" );
355  write_D356_records( d356_records, file );
356  fprintf( file, "999\n" );
357 
358  fclose( file );
359 }
360 
361 
362 void PCB_EDIT_FRAME::GenD356File( wxCommandEvent& aEvent )
363 {
364  wxFileName fn = GetBoard()->GetFileName();
365  wxString msg, ext, wildcard;
366 
367  ext = IpcD356FileExtension;
368  wildcard = IpcD356FileWildcard();
369  fn.SetExt( ext );
370 
371  wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
372 
373  wxFileDialog dlg( this, _( "Export D-356 Test File" ), pro_dir,
374  fn.GetFullName(), wildcard,
375  wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
376 
377  if( dlg.ShowModal() == wxID_CANCEL )
378  return;
379 
380  IPC356D_WRITER writer( GetBoard(), this );
381 
382  writer.Write( dlg.GetPath() );
383 }
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:750
void LayerPair(PCB_LAYER_ID *top_layer, PCB_LAYER_ID *bottom_layer) const
Function LayerPair Return the 2 layers used by the via (the via actually uses all layers between thes...
Definition: track.cpp:422
Definition: track.h:354
wxString pin
Definition: export_d356.h:35
void Write(const wxString &aFilename)
Generates and writes the netlist to a given path.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:253
int x_location
Definition: export_d356.h:42
This file is part of the common library TODO brief description.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
wxWindow * m_parent
Definition: export_d356.h:75
static int iu_to_d356(int iu, int clamp)
Definition: export_d356.cpp:84
This file is part of the common library.
bool mechanical
Definition: export_d356.h:38
static void build_pad_testpoints(BOARD *aPcb, std::vector< D356_RECORD > &aRecords)
Definition: export_d356.cpp:93
void write_D356_records(std::vector< D356_RECORD > &aRecords, FILE *aFile)
Writes a list of records to the given output stream.
Smd pad, appears on the solder paste layer (default)
Definition: pad_shapes.h:81
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.h:559
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:85
const wxString & GetFileName() const
Definition: board.h:278
bool midpoint
Definition: export_d356.h:36
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:95
PCB_LAYER_ID
A quick note on layer IDs:
LSET is a set of PCB_LAYER_IDs.
Wrapper to expose an API for writing IPC-D356 files.
Definition: export_d356.h:53
int GetDrillValue() const
Function GetDrillValue "calculates" the drill value for vias (m-Drill if > 0, or default drill value ...
Definition: track.cpp:170
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
FOOTPRINTS & Footprints()
Definition: board.h:283
wxString refdes
Definition: export_d356.h:34
static void build_via_testpoints(BOARD *aPcb, std::vector< D356_RECORD > &aRecords)
Definition of file extensions used in Kicad.
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:231
const std::string IpcD356FileExtension
BOARD * m_pcb
Definition: export_d356.h:74
wxString IpcD356FileWildcard()
int soldermask
Definition: export_d356.h:40
int LAYER_NUM
This can be replaced with int and removed.
wxString netname
Definition: export_d356.h:33
NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
static const wxString intern_new_d356_netname(const wxString &aNetname, std::map< wxString, wxString > &aMap, std::set< wxString > &aSet)
int GetWidth() const
Definition: track.h:110
int GetNet() const
Function GetNet.
Definition: netinfo.h:223
void GenD356File(wxCommandEvent &event)
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:186
#define _(s)
Definition: 3d_actions.cpp:33
static int compute_pad_access_code(BOARD *aPcb, LSET aLayerMask)
Definition: export_d356.cpp:51
int GetCopperLayerCount() const
Definition: board.cpp:425
#define IU_PER_MILS
Definition: plotter.cpp:137
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
static int via_access_code(BOARD *aPcb, int top_layer, int bottom_layer)
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
BOARD * GetBoard() const
Definition: pad.h:59
wxPoint GetPosition() const override
Definition: track.h:422
int y_location
Definition: export_d356.h:43
wxPoint m_AuxOrigin
origin for plot exports
TRACKS & Tracks()
Definition: board.h:280
virtual LSET GetLayerSet() const
Function GetLayerSet returns a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:191