KiCad PCB EDA Suite
export_d356.cpp File Reference

Export IPC-D-356 test format. More...

#include <fctsys.h>
#include <confirm.h>
#include <gestfich.h>
#include <kiface_i.h>
#include <pcb_edit_frame.h>
#include <trigo.h>
#include <build_version.h>
#include <macros.h>
#include <wildcards_and_files_ext.h>
#include <pcbnew.h>
#include <class_board.h>
#include <class_module.h>
#include <class_track.h>
#include <class_edge_mod.h>
#include <vector>
#include <cctype>

Go to the source code of this file.

Classes

struct  D356_RECORD
 

Functions

static int compute_pad_access_code (BOARD *aPcb, LSET aLayerMask)
 
static int iu_to_d356 (int iu, int clamp)
 
static void build_pad_testpoints (BOARD *aPcb, std::vector< D356_RECORD > &aRecords)
 
static int via_access_code (BOARD *aPcb, int top_layer, int bottom_layer)
 
static void build_via_testpoints (BOARD *aPcb, std::vector< D356_RECORD > &aRecords)
 
static const wxString intern_new_d356_netname (const wxString &aNetname, std::map< wxString, wxString > &aMap, std::set< wxString > &aSet)
 
static void write_D356_records (std::vector< D356_RECORD > &aRecords, FILE *fout)
 

Detailed Description

Export IPC-D-356 test format.

Definition in file export_d356.cpp.

Function Documentation

◆ build_pad_testpoints()

static void build_pad_testpoints ( BOARD aPcb,
std::vector< D356_RECORD > &  aRecords 
)
static

Definition at line 112 of file export_d356.cpp.

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 }
wxString pin
Definition: export_d356.cpp:55
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:115
like PAD_STANDARD, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:66
Smd pad, appears on the solder paste layer (default)
Definition: pad_shapes.h:62
MODULES & Modules()
Definition: class_board.h:236
wxString refdes
Definition: export_d356.cpp:54
const wxPoint & GetAuxOrigin() const
Definition: class_board.h:363
wxString netname
Definition: export_d356.cpp:53
static int compute_pad_access_code(BOARD *aPcb, LSET aLayerMask)
Definition: export_d356.cpp:70
#define min(a, b)
Definition: auxiliary.h:85

References D356_RECORD::access, B_Mask, compute_pad_access_code(), D356_RECORD::drill, F_Mask, BOARD::GetAuxOrigin(), BOARD_ITEM::GetLayerSet(), D356_RECORD::hole, KiROUND(), D356_RECORD::mechanical, D356_RECORD::midpoint, min, BOARD::Modules(), D356_RECORD::netname, PAD_ATTRIB_HOLE_NOT_PLATED, PAD_ATTRIB_SMD, PAD_SHAPE_CIRCLE, D356_RECORD::pin, D356_RECORD::refdes, D356_RECORD::rotation, D356_RECORD::smd, D356_RECORD::soldermask, D356_RECORD::x_location, D356_RECORD::x_size, D356_RECORD::y_location, and D356_RECORD::y_size.

Referenced by PCB_EDIT_FRAME::GenD356File().

◆ build_via_testpoints()

static void build_via_testpoints ( BOARD aPcb,
std::vector< D356_RECORD > &  aRecords 
)
static

Definition at line 186 of file export_d356.cpp.

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 }
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
PCB_LAYER_ID
A quick note on layer IDs:
int GetDrillValue() const
Function GetDrillValue "calculates" the drill value for vias (m-Drill if > 0, or default drill value ...
wxString refdes
Definition: export_d356.cpp:54
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:233
const wxPoint & GetAuxOrigin() const
Definition: class_board.h:363
const wxPoint GetPosition() const override
Definition: class_track.h:319
wxString netname
Definition: export_d356.cpp:53
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
int GetWidth() const
Definition: class_track.h:103
int GetNet() const
Function GetNet.
Definition: netinfo.h:225
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:96
TRACKS & Tracks()
Definition: class_board.h:227

References D356_RECORD::access, D356_RECORD::drill, BOARD::GetAuxOrigin(), VIA::GetDrillValue(), NETINFO_ITEM::GetNet(), NETINFO_ITEM::GetNetname(), VIA::GetPosition(), TRACK::GetWidth(), D356_RECORD::hole, VIA::LayerPair(), D356_RECORD::mechanical, D356_RECORD::midpoint, D356_RECORD::netname, PCB_VIA_T, D356_RECORD::pin, D356_RECORD::refdes, D356_RECORD::rotation, D356_RECORD::smd, D356_RECORD::soldermask, BOARD::Tracks(), via_access_code(), D356_RECORD::x_location, D356_RECORD::x_size, D356_RECORD::y_location, and D356_RECORD::y_size.

Referenced by PCB_EDIT_FRAME::GenD356File().

◆ compute_pad_access_code()

static int compute_pad_access_code ( BOARD aPcb,
LSET  aLayerMask 
)
static

Definition at line 70 of file export_d356.cpp.

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 }
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
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
int GetCopperLayerCount() const
Function GetCopperLayerCount.

References LSET::AllCuMask(), B_Cu, F_Cu, BOARD::GetCopperLayerCount(), and In1_Cu.

Referenced by build_pad_testpoints().

◆ intern_new_d356_netname()

static const wxString intern_new_d356_netname ( const wxString &  aNetname,
std::map< wxString, wxString > &  aMap,
std::set< wxString > &  aSet 
)
static

Definition at line 230 of file export_d356.cpp.

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 }

Referenced by write_D356_records().

◆ iu_to_d356()

static int iu_to_d356 ( int  iu,
int  clamp 
)
static

Definition at line 103 of file export_d356.cpp.

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 }
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:115
#define IU_PER_MILS
Definition: plotter.cpp:134

References IU_PER_MILS, and KiROUND().

Referenced by write_D356_records().

◆ via_access_code()

static int via_access_code ( BOARD aPcb,
int  top_layer,
int  bottom_layer 
)
static

Definition at line 165 of file export_d356.cpp.

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 }
int GetCopperLayerCount() const
Function GetCopperLayerCount.

References B_Cu, F_Cu, and BOARD::GetCopperLayerCount().

Referenced by build_via_testpoints().

◆ write_D356_records()

static void write_D356_records ( std::vector< D356_RECORD > &  aRecords,
FILE *  fout 
)
static

Definition at line 282 of file export_d356.cpp.

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 }
wxString pin
Definition: export_d356.cpp:55
static int iu_to_d356(int iu, int clamp)
#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
wxString refdes
Definition: export_d356.cpp:54
wxString netname
Definition: export_d356.cpp:53
static const wxString intern_new_d356_netname(const wxString &aNetname, std::map< wxString, wxString > &aMap, std::set< wxString > &aSet)
size_t i
Definition: json11.cpp:597

References D356_RECORD::access, D356_RECORD::drill, D356_RECORD::hole, i, intern_new_d356_netname(), iu_to_d356(), D356_RECORD::mechanical, D356_RECORD::midpoint, D356_RECORD::netname, D356_RECORD::pin, D356_RECORD::refdes, D356_RECORD::rotation, D356_RECORD::smd, D356_RECORD::soldermask, TO_UTF8, D356_RECORD::x_location, D356_RECORD::x_size, D356_RECORD::y_location, and D356_RECORD::y_size.

Referenced by PCB_EDIT_FRAME::GenD356File().