34#include <wx/checkbox.h>
35#include <wx/filedlg.h>
36#include <wx/filedlgcustomize.h>
77 if( !aLayerMask.any() )
81 if( aLayerMask[
F_Cu] && aLayerMask[
B_Cu] )
85 if( aLayerMask[
F_Cu] )
89 if( aLayerMask[
B_Cu] )
97 if( aLayerMask[layer] )
108 int val =
KiROUND(
static_cast<double>( iu ) / (
pcbIUScale.IU_PER_MILS / 10.0 ) );
109 if( val > clamp )
return clamp;
110 if( val < -clamp )
return -clamp;
116 std::vector<D356_RECORD>& aRecords )
125 const PAD*
pad = point.m_pad;
138 rk.
pin =
pad->GetNumber();
143 rk.
drill = point.m_drill;
153 rk.
x_size =
pad->GetSize( accessLayer ).x;
159 rk.
y_size =
pad->GetSize( accessLayer ).y;
169 if( point.m_frontMask )
172 if( point.m_backMask )
175 aRecords.push_back( std::move( rk ) );
186 if( (top_layer ==
F_Cu) && (bottom_layer ==
B_Cu) )
190 if( top_layer ==
F_Cu )
194 if( bottom_layer ==
B_Cu )
204 std::vector<D356_RECORD>& aRecords )
228 rk.
drill = point.m_drill;
233 via->LayerPair( &top_layer, &bottom_layer );
269 aRecords.push_back( rk );
279 for( wxUniChar ch : aNetname )
281 if( ch > 126 || !std::isgraph(
static_cast<unsigned char>( ch ) ) )
283 else if( aUpper && ch >=
'a' && ch <=
'z' )
284 ch =
static_cast<char>( ch.GetValue() -
'a' +
'A' );
296 static const char digits[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
297 wxString alias( wxT(
"M" ) );
299 for(
int div = 36 * 36 * 36; div > 0; div /= 36 )
300 alias += digits[( aIndex / div ) % 36];
309 const wxString nc( wxT(
"N/C" ) );
312 std::map<wxString, wxString> d356_net_map;
313 std::set<wxString> d356_net_set = { nc };
314 std::map<wxString, int> canonCount;
319 if( !rk.netname.empty() && d356_net_map.emplace( rk.netname, wxEmptyString ).second )
324 for(
const auto& [candidate, count] : canonCount )
325 d356_net_set.insert( candidate );
328 for(
auto& [netname, canon] : d356_net_map )
332 if( candidate.size() <= 14 && candidate != nc && canonCount[candidate] == 1 )
338 for(
auto& [netname, canon] : d356_net_map )
346 }
while( !d356_net_set.insert( canon ).second );
349 fprintf( aFile,
"P NNAME%-5.5s %-66.66s\n",
TO_UTF8( canon ),
354 [&](
const D356_RECORD& aRecord ) ->
const wxString&
356 return aRecord.netname.empty() ? nc : d356_net_map.at( aRecord.netname );
360 std::stable_sort( aRecords.begin(), aRecords.end(),
363 return signalName( aLhs ) < signalName( aRhs );
368 const wxString& d356_net = signalName( rk );
369 const bool blindOrBuried = rk.start_layer != 0;
376 else if( rk.mechanical )
378 else if( blindOrBuried )
383 auto writeComponent =
384 [&](
int aOpCode,
bool aMidpoint )
386 fprintf( aFile,
"%03d%-14.14s %-6.6s%c%-4.4s%c", aOpCode,
TO_UTF8( d356_net ),
387 TO_UTF8( rk.refdes ), rk.pin.empty() ?
' ' :
'-',
TO_UTF8( rk.pin ),
388 aMidpoint ?
'M' :
' ' );
391 auto writeAccessAndSize =
394 fprintf( aFile,
"A%02dX%+07dY%+07dX%04dY%04dR%03d", rk.access,
399 writeComponent( rktype, rk.midpoint );
404 fprintf( aFile,
"D%04d%c",
406 rk.mechanical ?
'U':
'P' );
410 fprintf( aFile,
" " );
416 fprintf( aFile,
"A%02dX%+07dY%+07d%14s", rk.access,
iu_to_d356( rk.x_location, 999999 ),
421 writeAccessAndSize();
425 fprintf( aFile,
" S%d", rk.soldermask );
429 fprintf( aFile,
"L%02dL%02d\n", rk.start_layer, rk.end_layer );
431 fprintf( aFile,
"%6s\n",
"" );
435 && ( rk.start_layer == 1 || rk.end_layer ==
m_pcb->GetCopperLayerCount() ) )
438 writeComponent( 27,
false );
439 fprintf( aFile,
" " );
440 writeAccessAndSize();
441 fprintf( aFile,
" S%d%6s\n", rk.soldermask,
"" );
449 FILE* file =
nullptr;
452 if( ( file = wxFopen( aFilename, wxT(
"wt" ) ) ) ==
nullptr )
458 std::vector<D356_RECORD> d356_records;
468 [&](
const std::string& aRecord )
470 fprintf( file,
"%-80.80s\n", aRecord.substr( 0, 72 ).c_str() );
474 writeRecord(
"P JOB " + std::string(
TO_UTF8( wxFileName(
m_pcb->GetFileName() ).GetName() ) ) );
475 writeRecord(
"P CODE 00" );
476 writeRecord(
"P UNITS CUST 0" );
477 writeRecord(
"P arrayDim N" );
478 writeRecord(
"P VER IPC-D-356A" );
479 writeRecord(
"P IMAGE PRIMARY" );
481 writeRecord(
"999" );
505 customizer.AddStaticText( wxT(
"\n\n" ) );
508 m_cb = customizer.AddCheckBox(
_(
"Do not export unconnected pads" ) );
529 wxFileName fn =
m_frame->GetBoard()->GetFileName();
530 wxString ext, wildcard;
536 wxString pro_dir = wxPathOnly(
m_frame->Prj().GetProjectFullName() );
538 wxFileDialog dlg(
m_frame,
_(
"Generate IPC-D-356 netlist file" ), pro_dir, fn.GetFullName(),
539 wildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
541 dlg.SetCustomizeHook( customizeHook );
545 if( dlg.ShowModal() == wxID_CANCEL )
551 m_frame->GetPcbNewSettings()->m_ExportD356.doNotExportUnconnectedPads = doNotExportUnconnectedPads;
553 if( writer.
Write( dlg.GetPath() ) )
constexpr EDA_IU_SCALE pcbIUScale
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
const VECTOR2I & GetAuxOrigin() const
int GenD356File(const TOOL_EVENT &aEvent)
Information pertinent to a Pcbnew printed circuit board.
int GetCopperLayerCount() const
BOARD_DESIGN_SETTINGS & GetDesignSettings()
virtual void TransferDataFromCustomControls() override
D365_CUSTOMIZE_HOOK(bool aDoNotExportUnconnectedPads=false)
wxDECLARE_NO_COPY_CLASS(D365_CUSTOMIZE_HOOK)
wxFileDialogCheckBox * m_cb
bool GetDoNotExportUnconnectedPads() const
bool m_doNotExportUnconnectedPads
virtual void AddCustomControls(wxFileDialogCustomize &customizer) override
Wrapper to expose an API for writing IPC-D356 files.
bool m_doNotExportUnconnectedPads
void write_D356_records(std::vector< D356_RECORD > &aRecords, FILE *aFile)
Writes a list of records to the given output stream.
void SetDoNotExportUnconnectedPads(bool aDoNotExportUnconnectedPads)
Sets whether unconnected pads should be exported.
void appendPadRecords(BOARD *aPcb, const std::vector< FAB_TEST_POINT > &aPoints, std::vector< D356_RECORD > &aRecords)
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.
LSET is a set of PCB_LAYER_IDs.
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Handle the data for a net.
const wxString & GetNetname() const
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
@ NORMAL
Shape is the same on all layers.
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
This file is part of the common library.
static int physical_layer(BOARD *aPcb, PCB_LAYER_ID aLayer)
static void appendViaRecords(BOARD *aPcb, const std::vector< FAB_TEST_POINT > &aPoints, std::vector< D356_RECORD > &aRecords)
static wxString sanitize_d356_name(const wxString &aNetname, bool aUpper)
static wxString d356_alias(int aIndex)
static int iu_to_d356(int iu, int clamp)
static int via_access_code(BOARD *aPcb, int top_layer, int bottom_layer)
static int compute_pad_access_code(BOARD *aPcb, LSET aLayerMask)
std::vector< FAB_TEST_POINT > FabTestPoints(const BOARD &aBoard)
The caller must build board connectivity before enumerating net endpoints.
static const std::string IpcD356FileExtension
static wxString IpcD356FileWildcard()
size_t CopperLayerToOrdinal(PCB_LAYER_ID aLayer)
Converts KiCad copper layer enum to an ordinal between the front and back layers.
PCB_LAYER_ID
A quick note on layer IDs:
This file contains miscellaneous commonly used macros and functions.
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
@ NON_COMPONENT
Pad with no hole that does not connect to a physical component, but rather provides a connection poin...
@ SMD
Smd pad, appears on the solder paste layer (default)
@ CONN
Like smd, does not appear on the solder paste layer (default) Note: also has a special attribute in G...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
VECTOR2< int32_t > VECTOR2I
Definition of file extensions used in Kicad.