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 <fctsys.h>
31 #include <confirm.h>
32 #include <gestfich.h>
33 #include <kiface_i.h>
34 #include <pcb_edit_frame.h>
35 #include <trigo.h>
36 #include <build_version.h>
37 #include <macros.h>
39 #include <pcbnew.h>
40 #include <class_board.h>
41 #include <class_module.h>
42 #include <class_track.h>
43 #include <class_edge_mod.h>
44 #include <vector>
45 #include <cctype>
46 
47 /* Structure for holding the D-356 record fields.
48  * Useful because 356A (when implemented) must be sorted before outputting it */
50 {
51  bool smd;
52  bool hole;
53  wxString netname;
54  wxString refdes;
55  wxString pin;
56  bool midpoint;
57  int drill;
58  bool mechanical;
59  int access; // Access 0 is 'both sides'
61  // All these in PCB units, will be output in decimils
64  int x_size;
65  int y_size;
66  int rotation;
67 };
68 
69 // Compute the access code for a pad. Returns -1 if there is no copper
70 static int compute_pad_access_code( BOARD *aPcb, LSET aLayerMask )
71 {
72  // Non-copper is not interesting here
73  aLayerMask &= LSET::AllCuMask();
74  if( !aLayerMask.any() )
75  return -1;
76 
77  // Traditional TH pad
78  if( aLayerMask[F_Cu] && aLayerMask[B_Cu] )
79  return 0;
80 
81  // Front SMD pad
82  if( aLayerMask[F_Cu] )
83  return 1;
84 
85  // Back SMD pad
86  if( aLayerMask[B_Cu] )
87  return aPcb->GetCopperLayerCount();
88 
89  // OK, we have an inner-layer only pad (and I have no idea about
90  // what could be used for); anyway, find the first copper layer
91  // it's on
92  for( LAYER_NUM layer = In1_Cu; layer < B_Cu; ++layer )
93  {
94  if( aLayerMask[layer] )
95  return layer + 1;
96  }
97 
98  // This shouldn't happen
99  return -1;
100 }
101 
102 /* Convert and clamp a size from IU to decimils */
103 static int iu_to_d356(int iu, int clamp)
104 {
105  int val = KiROUND( iu / ( IU_PER_MILS / 10 ) );
106  if( val > clamp ) return clamp;
107  if( val < -clamp ) return -clamp;
108  return val;
109 }
110 
111 /* Extract the D356 record from the modules (pads) */
112 static void build_pad_testpoints( BOARD *aPcb,
113  std::vector <D356_RECORD>& aRecords )
114 {
115  wxPoint origin = aPcb->GetAuxOrigin();
116 
117  for( auto module : aPcb->Modules() )
118  {
119  for( auto pad : module->Pads() )
120  {
121  D356_RECORD rk;
122  rk.access = compute_pad_access_code( aPcb, pad->GetLayerSet() );
123 
124  // It could be a mask only pad, we only handle pads with copper here
125  if( rk.access != -1 )
126  {
127  rk.netname = pad->GetNetname();
128  rk.pin = pad->GetName();
129  rk.refdes = module->GetReference();
130  rk.midpoint = false; // XXX MAYBE need to be computed (how?)
131  const wxSize& drill = pad->GetDrillSize();
132  rk.drill = std::min( drill.x, drill.y );
133  rk.hole = (rk.drill != 0);
134  rk.smd = pad->GetAttribute() == PAD_ATTRIB_SMD;
135  rk.mechanical = (pad->GetAttribute() == PAD_ATTRIB_HOLE_NOT_PLATED);
136  rk.x_location = pad->GetPosition().x - origin.x;
137  rk.y_location = origin.y - pad->GetPosition().y;
138  rk.x_size = pad->GetSize().x;
139 
140  // Rule: round pads have y = 0
141  if( pad->GetShape() == PAD_SHAPE_CIRCLE )
142  rk.y_size = 0;
143  else
144  rk.y_size = pad->GetSize().y;
145 
146  rk.rotation = -KiROUND( pad->GetOrientation() ) / 10;
147  if( rk.rotation < 0 ) rk.rotation += 360;
148 
149  // the value indicates which sides are *not* accessible
150  rk.soldermask = 3;
151  if( pad->GetLayerSet()[F_Mask] )
152  rk.soldermask &= ~1;
153  if( pad->GetLayerSet()[B_Mask] )
154  rk.soldermask &= ~2;
155 
156  aRecords.push_back( rk );
157  }
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 */
165 static 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 bottom_layer + 1; // XXX is this correct?
183 }
184 
185 /* Extract the D356 record from the vias */
186 static void build_via_testpoints( BOARD *aPcb,
187  std::vector <D356_RECORD>& aRecords )
188 {
189  wxPoint origin = aPcb->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  VIA *via = (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  rk.x_size = via->GetWidth();
220  rk.y_size = 0; // Round so height = 0
221  rk.rotation = 0;
222  rk.soldermask = 3; // XXX always tented?
223 
224  aRecords.push_back( rk );
225  }
226  }
227 }
228 
229 /* Add a new netname to the d356 canonicalized list */
230 static const wxString intern_new_d356_netname( const wxString &aNetname,
231  std::map<wxString, wxString> &aMap, std::set<wxString> &aSet )
232 {
233  wxString canon;
234 
235  for( size_t ii = 0; ii < aNetname.Len(); ++ii )
236  {
237  // Rule: we can only use the standard ASCII, control excluded
238  wxUniChar ch = aNetname[ii];
239 
240  if( ch > 126 || !std::isgraph( static_cast<unsigned char>( ch ) ) )
241  ch = '?';
242 
243  canon += ch;
244  }
245 
246  // Rule: only uppercase (unofficial, but known to give problems
247  // otherwise)
248  canon.MakeUpper();
249 
250  // Rule: maximum length is 14 characters, otherwise we keep the tail
251  if( canon.size() > 14 )
252  {
253  canon = canon.Right( 14 );
254  }
255 
256  // Check if it's still unique
257  if( aSet.count( canon ) )
258  {
259  // Nope, need to uniquify it, trim it more and add a number
260  wxString base( canon );
261  if( base.size() > 10 )
262  {
263  base = base.Right( 10 );
264  }
265 
266  int ctr = 0;
267  do
268  {
269  ++ctr;
270  canon = base;
271  canon << '#' << ctr;
272  } while ( aSet.count( canon ) );
273  }
274 
275  // Register it
276  aMap[aNetname] = canon;
277  aSet.insert( canon );
278  return canon;
279 }
280 
281 /* Write all the accumuled data to the file in D356 format */
282 static void write_D356_records( std::vector <D356_RECORD> &aRecords,
283  FILE *fout )
284 {
285  // Sanified and shorted network names and set of short names
286  std::map<wxString, wxString> d356_net_map;
287  std::set<wxString> d356_net_set;
288 
289  for (unsigned i = 0; i < aRecords.size(); i++)
290  {
291  D356_RECORD &rk = aRecords[i];
292 
293  // Try to sanify the network name (there are limits on this), if
294  // not already done. Also 'empty' net are marked as N/C, as
295  // specified.
296  wxString d356_net( wxT("N/C") );
297  if( !rk.netname.empty() )
298  {
299  d356_net = d356_net_map[rk.netname];
300 
301  if( d356_net.empty() )
302  d356_net = intern_new_d356_netname( rk.netname, d356_net_map,
303  d356_net_set );
304  }
305 
306  // Choose the best record type
307  int rktype;
308  if( rk.smd )
309  rktype = 327;
310  else
311  {
312  if( rk.mechanical )
313  rktype = 367;
314  else
315  rktype = 317;
316  }
317 
318  // Operation code, signal and component
319  fprintf( fout, "%03d%-14.14s %-6.6s%c%-4.4s%c",
320  rktype, TO_UTF8(d356_net),
321  TO_UTF8(rk.refdes),
322  rk.pin.empty()?' ':'-',
323  TO_UTF8(rk.pin),
324  rk.midpoint?'M':' ' );
325 
326  // Hole definition
327  if( rk.hole )
328  {
329  fprintf( fout, "D%04d%c",
330  iu_to_d356( rk.drill, 9999 ),
331  rk.mechanical ? 'U':'P' );
332  }
333  else
334  fprintf( fout, " " );
335 
336  // Test point access
337  fprintf( fout, "A%02dX%+07dY%+07dX%04dY%04dR%03d",
338  rk.access,
339  iu_to_d356( rk.x_location, 999999 ),
340  iu_to_d356( rk.y_location, 999999 ),
341  iu_to_d356( rk.x_size, 9999 ),
342  iu_to_d356( rk.y_size, 9999 ),
343  rk.rotation );
344 
345  // Soldermask
346  fprintf( fout, "S%d\n", rk.soldermask );
347  }
348 }
349 
350 
351 void PCB_EDIT_FRAME::GenD356File( wxCommandEvent& aEvent )
352 {
353  wxFileName fn = GetBoard()->GetFileName();
354  wxString msg, ext, wildcard;
355  FILE* file;
356 
357  ext = IpcD356FileExtension;
358  wildcard = IpcD356FileWildcard();
359  fn.SetExt( ext );
360 
361  wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
362 
363  wxFileDialog dlg( this, _( "Export D-356 Test File" ), pro_dir,
364  fn.GetFullName(), wildcard,
365  wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
366 
367  if( dlg.ShowModal() == wxID_CANCEL )
368  return;
369 
370  if( ( file = wxFopen( dlg.GetPath(), wxT( "wt" ) ) ) == NULL )
371  {
372  msg = _( "Unable to create " ) + dlg.GetPath();
373  DisplayError( this, msg ); return;
374  }
375 
376  LOCALE_IO toggle; // Switch the locale to standard C
377 
378  // This will contain everything needed for the 356 file
379  std::vector <D356_RECORD> d356_records;
380  BOARD* pcb = GetBoard();
381 
382  build_via_testpoints( pcb, d356_records );
383 
384  build_pad_testpoints( pcb, d356_records );
385 
386  // Code 00 AFAIK is ASCII, CUST 0 is decimils/degrees
387  // CUST 1 would be metric but gerbtool simply ignores it!
388  fprintf( file, "P CODE 00\n" );
389  fprintf( file, "P UNITS CUST 0\n" );
390  fprintf( file, "P arrayDim N\n" );
391  write_D356_records( d356_records, file );
392  fprintf( file, "999\n" );
393 
394  fclose( file );
395 }
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Function AllCuMask returns a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:676
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
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...
wxString pin
Definition: export_d356.cpp:55
This file is part of the common library TODO brief description.
like PAD_STANDARD, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:66
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:154
static int iu_to_d356(int iu, int clamp)
This file is part of the common library.
static void build_pad_testpoints(BOARD *aPcb, std::vector< D356_RECORD > &aRecords)
Smd pad, appears on the solder paste layer (default)
Definition: pad_shapes.h:62
const wxString & GetFileName() const
Definition: class_board.h:216
static void write_D356_records(std::vector< D356_RECORD > &aRecords, FILE *fout)
Definitions for tracks, vias and zones.
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:48
PCB_LAYER_ID
A quick note on layer IDs:
Class LSET is a set of PCB_LAYER_IDs.
int GetDrillValue() const
Function GetDrillValue "calculates" the drill value for vias (m-Drill if > 0, or default drill value ...
MODULES & Modules()
Definition: class_board.h:227
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
wxString refdes
Definition: export_d356.cpp:54
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:233
const wxPoint & GetAuxOrigin() const
Definition: class_board.h:354
const std::string IpcD356FileExtension
#define _(s)
const wxPoint GetPosition() const override
Definition: class_track.h:319
wxString IpcD356FileWildcard()
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
wxString netname
Definition: export_d356.cpp:53
Class 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: class_track.h:103
int GetNet() const
Function GetNet.
Definition: netinfo.h:225
void GenD356File(wxCommandEvent &event)
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:161
static int compute_pad_access_code(BOARD *aPcb, LSET aLayerMask)
Definition: export_d356.cpp:70
size_t i
Definition: json11.cpp:649
int GetCopperLayerCount() const
Function GetCopperLayerCount.
#define IU_PER_MILS
Definition: plotter.cpp:136
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
Module description (excepted pads)
BOARD * GetBoard() const
EDGE_MODULE class definition.
TRACKS & Tracks()
Definition: class_board.h:218
virtual LSET GetLayerSet() const
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:114
#define min(a, b)
Definition: auxiliary.h:85