KiCad PCB EDA Suite
dialog_layers_select_to_pcb.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #include <X2_gerber_attributes.h>
30 #include <gerber_file_image.h>
31 #include <gerber_file_image_list.h>
32 #include <gerbview.h>
33 #include <gerbview_frame.h>
34 #include <gerbview_id.h>
35 #include <gerbview_settings.h>
36 #include <kiface_base.h>
37 #include <layer_ids.h>
38 
40 
41 #include <wx/msgdlg.h>
42 
43 
44 extern const wxString GetPCBDefaultLayerName( LAYER_NUM aLayerNumber );
45 
46 
51 };
52 
53 
55 
56 
57 BEGIN_EVENT_TABLE( LAYERS_MAP_DIALOG, LAYERS_MAP_DIALOG_BASE )
59  wxEVT_COMMAND_BUTTON_CLICKED,
60  LAYERS_MAP_DIALOG::OnSelectLayer )
61 END_EVENT_TABLE()
62 
63 
65  LAYERS_MAP_DIALOG_BASE( parent )
66 {
67  m_Parent = parent;
68  initDialog();
69 
70  // Resize the dialog
71  Layout();
72  GetSizer()->SetSizeHints( this );
73  Centre();
74 }
75 
76 
78 {
79  wxStaticText* label;
80  wxStaticText* text;
81  int item_ID;
82  wxString msg;
83  wxSize goodSize;
84  GERBVIEW_SETTINGS* config = static_cast<GERBVIEW_SETTINGS*>( Kiface().KifaceSettings() );
85 
86  for( int ii = 0; ii < GERBER_DRAWLAYERS_COUNT; ++ii )
87  {
88  // Specify the default value for each member of these arrays.
89  m_buttonTable[ii] = -1;
91  }
92 
93  // Ensure we have:
94  // At least 2 copper layers and less than max pcb copper layers count
95  // Even number of layers because a board *must* have even layers count
97 
98  int idx = ( m_exportBoardCopperLayersCount / 2 ) - 1;
99  m_comboCopperLayersCount->SetSelection( idx );
100 
103 
104  for( unsigned ii = 0; ii < GERBER_DRAWLAYERS_COUNT; ++ii )
105  {
106  if( images->GetGbrImage( ii ) == nullptr )
107  break;
108 
111  }
112 
113  if( m_gerberActiveLayersCount <= GERBER_DRAWLAYERS_COUNT / 2 ) // Only one list is enough
114  m_staticlineSep->Hide();
115 
116  wxFlexGridSizer* flexColumnBoxSizer = m_flexLeftColumnBoxSizer;
117 
118  for( int ii = 0; ii < m_gerberActiveLayersCount; ii++ )
119  {
120  // Each Gerber layer has an associated static text string (to
121  // identify that layer), a button (for invoking a child dialog
122  // box to change which Pcbnew layer that the Gerber layer is
123  // mapped to), and a second static text string (to depict which
124  // Pcbnew layer that the Gerber layer has been mapped to). Each
125  // of those items are placed into the left hand column, middle
126  // column, and right hand column (respectively) of the Flexgrid
127  // sizer, and the color of the second text string is set to
128  // fuchsia or blue (to respectively indicate whether the Gerber
129  // layer has been mapped to a Pcbnew layer or is not being
130  // exported at all). (Experimentation has shown that if a text
131  // control is used to depict which Pcbnew layer that each Gerber
132  // layer is mapped to (instead of a static text string), then
133  // those controls do not behave in a fully satisfactory manner
134  // in the Linux version. Even when the read-only attribute is
135  // specified for all of those controls, they can still be selected
136  // when the arrow keys or Tab key is used to step through all of
137  // the controls within the dialog box, and directives to set the
138  // foreground color of the text of each such control to blue (to
139  // indicate that the text is of a read-only nature) are disregarded.
140  // Specify a FlexGrid sizer with an appropriate number of rows
141  // and three columns. If nb_items < 16, then the number of rows
142  // is nb_items; otherwise, the number of rows is 16 (with two
143  // separate columns of controls being used if nb_items > 16).
144 
145  if( ii == GERBER_DRAWLAYERS_COUNT / 2 )
146  flexColumnBoxSizer = m_flexRightColumnBoxSizer;
147 
148  // Provide a text string to identify the Gerber layer
149  msg.Printf( _( "Layer %d" ), m_buttonTable[ii] + 1 );
150 
151  label = new wxStaticText( this, wxID_STATIC, msg );
152  flexColumnBoxSizer->Add( label, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
153 
154  /* Add file name and extension without path. */
155  wxFileName fn( images->GetGbrImage( ii )->m_FileName );
156  label = new wxStaticText( this, wxID_STATIC, fn.GetFullName() );
157  flexColumnBoxSizer->Add( label, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
158 
159  // Provide a button for this layer (which will invoke a child dialog box)
160  item_ID = ID_BUTTON_0 + ii;
161  wxButton * Button = new wxButton( this, item_ID, wxT( "..." ), wxDefaultPosition,
162  wxDefaultSize, wxBU_EXACTFIT );
163 
164  flexColumnBoxSizer->Add( Button, 0, wxALIGN_CENTER_VERTICAL | wxALL );
165 
166  // Provide another text string to specify which Pcbnew layer that this
167  // Gerber layer is mapped to. All layers initially default to
168  // "Do NotExport" (which corresponds to UNSELECTED_LAYER). Whenever
169  // a layer is set to "Do Not Export" it's displayed in blue. When a
170  // user selects a specific KiCad layer to map to, it's displayed in
171  // magenta which indicates it will be exported.
172  item_ID = ID_TEXT_0 + ii;
173 
174  // All layers default to "Do Not Export" displayed in blue
175  msg = _( "Do not export" );
176  text = new wxStaticText( this, item_ID, msg );
177  text->SetForegroundColour( *wxBLUE );
178 
179  // When the first of these text strings is being added, determine what
180  // size is necessary to to be able to display any possible string
181  // without it being truncated. Then specify that size as the minimum
182  // size for all of these text strings. (If this minimum size is not
183  // determined in this fashion, then it is possible for the display of
184  // one or more of these strings to be truncated after different Pcbnew
185  // layers are selected.)
186 
187  if( ii == 0 )
188  {
189  goodSize = text->GetSize();
190 
191  for( LAYER_NUM jj = 0; jj < GERBER_DRAWLAYERS_COUNT; ++jj )
192  {
193  text->SetLabel( GetPCBDefaultLayerName( jj ) );
194 
195  if( goodSize.x < text->GetSize().x )
196  goodSize.x = text->GetSize().x;
197  }
198  text->SetLabel( msg ); // Reset label to default text
199  }
200 
201  text->SetMinSize( goodSize );
202  flexColumnBoxSizer->Add( text, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
203 
204  m_layersList[ii] = text;
205  }
206 
207  // If the user has never stored any Gerber to KiCad layer mapping,
208  // then disable the button to retrieve it
209  if( config->m_GerberToPcbLayerMapping.size() == 0 )
210  m_buttonRetrieve->Enable( false );
211 
212  std::vector<int> gerber2KicadMapping;
213 
214  // See how many of the loaded Gerbers can be mapped to KiCad layers automatically
215  int numMappedGerbers = findKnownGerbersLoaded( gerber2KicadMapping );
216 
217  if( numMappedGerbers > 0 )
218  {
219  // See if the user wants to map the Altium Gerbers to known KiCad PCB layers
220  int returnVal = wxMessageBox(
221  _( "Gerbers with known layers: " + wxString::Format( wxT( "%i" ), numMappedGerbers )
222  + "\n\nAssign to matching KiCad PCB layers?" ),
223  _( "Automatic Layer Assignment" ), wxOK | wxCANCEL | wxOK_DEFAULT );
224 
225  if( returnVal == wxOK )
226  {
227  for( int ii = 0; ii < m_gerberActiveLayersCount; ii++ )
228  {
229  int currLayer = gerber2KicadMapping[ii];
230 
231  // Default to "Do Not Export" for unselected or undefined layer
232  if( ( currLayer == UNSELECTED_LAYER ) || ( currLayer == UNDEFINED_LAYER ) )
233  {
234  m_layersList[ii]->SetLabel( _( "Do not export" ) );
235  m_layersList[ii]->SetForegroundColour( *wxBLUE );
236 
237  // Set the layer internally to unselected
239  }
240  else
241  {
242  m_layersList[ii]->SetLabel( GetPCBDefaultLayerName( currLayer ) );
243  m_layersList[ii]->SetForegroundColour( wxColour( 255, 0, 128 ) );
244 
245  // Set the layer internally to the matching KiCad layer
246  m_layersLookUpTable[ii] = currLayer;
247  }
248  }
249  }
250  }
251 }
252 
253 
255 {
256  if( ( m_exportBoardCopperLayersCount & 1 ) )
258 
261 
264 
265 }
266 
267 
268 void LAYERS_MAP_DIALOG::OnBrdLayersCountSelection( wxCommandEvent& event )
269 {
270  int id = event.GetSelection();
271  m_exportBoardCopperLayersCount = ( id + 1 ) * 2;
272 }
273 
274 
275 void LAYERS_MAP_DIALOG::OnResetClick( wxCommandEvent& event )
276 {
277  wxString msg;
278  int ii;
279  LAYER_NUM layer;
280 
281  for( ii = 0, layer = 0; ii < m_gerberActiveLayersCount; ii++, ++layer )
282  {
284  m_layersList[ii]->SetLabel( _( "Do not export" ) );
285  m_layersList[ii]->SetForegroundColour( *wxBLUE );
286  m_buttonTable[ii] = ii;
287  }
288 }
289 
290 
291 void LAYERS_MAP_DIALOG::OnStoreSetup( wxCommandEvent& event )
292 {
293  auto config = static_cast<GERBVIEW_SETTINGS*>( Kiface().KifaceSettings() );
294  config->m_BoardLayersCount = m_exportBoardCopperLayersCount;
295 
296  config->m_GerberToPcbLayerMapping.clear();
297 
298  for( int ii = 0; ii < GERBER_DRAWLAYERS_COUNT; ++ii )
299  {
300  config->m_GerberToPcbLayerMapping.push_back( m_layersLookUpTable[ii] );
301  }
302 
303  // Enable the "Get Stored Choice" button in case it was disabled in "initDialog()"
304  // due to no previously stored choices.
305  m_buttonRetrieve->Enable( true );
306 }
307 
308 
309 void LAYERS_MAP_DIALOG::OnGetSetup( wxCommandEvent& event )
310 {
311  GERBVIEW_SETTINGS* config = static_cast<GERBVIEW_SETTINGS*>( Kiface().KifaceSettings() );
312 
313  m_exportBoardCopperLayersCount = config->m_BoardLayersCount;
315 
316  int idx = ( m_exportBoardCopperLayersCount / 2 ) - 1;
317  m_comboCopperLayersCount->SetSelection( idx );
318 
319  for( int ii = 0; ii < GERBER_DRAWLAYERS_COUNT; ++ii )
320  {
321  // Ensure the layer mapping in config exists for this layer, and store it
322  if( (size_t)ii >= config->m_GerberToPcbLayerMapping.size() )
323  break;
324 
325  m_layersLookUpTable[ii] = config->m_GerberToPcbLayerMapping[ ii ];
326  }
327 
328  for( int ii = 0; ii < m_gerberActiveLayersCount; ii++ )
329  {
330  LAYER_NUM layer = m_layersLookUpTable[ii];
331 
332  if( layer == UNSELECTED_LAYER )
333  {
334  m_layersList[ii]->SetLabel( _( "Do not export" ) );
335  m_layersList[ii]->SetForegroundColour( *wxBLUE );
336  }
337  else if( layer == UNDEFINED_LAYER )
338  {
339  m_layersList[ii]->SetLabel( _( "Hole data" ) );
340  m_layersList[ii]->SetForegroundColour( wxColour( 255, 0, 128 ) );
341  }
342  else
343  {
344  m_layersList[ii]->SetLabel( GetPCBDefaultLayerName( layer ) );
345  m_layersList[ii]->SetForegroundColour( wxColour( 255, 0, 128 ) );
346  }
347  }
348 }
349 
350 
351 void LAYERS_MAP_DIALOG::OnSelectLayer( wxCommandEvent& event )
352 {
353  int ii;
354 
355  ii = event.GetId() - ID_BUTTON_0;
356 
357  if( (ii < 0) || (ii >= GERBER_DRAWLAYERS_COUNT) )
358  {
359  wxFAIL_MSG( wxT( "Bad layer id" ) );
360  return;
361  }
362 
364 
365  if( jj != UNSELECTED_LAYER && jj != UNDEFINED_LAYER && !IsValidLayer( jj ) )
366  jj = B_Cu; // (Defaults to "Copper" layer.)
367 
368  // Get file name of Gerber loaded on this layer
369  wxFileName fn( m_Parent->GetGerberLayout()->GetImagesList()->GetGbrImage( ii )->m_FileName );
370 
371  // Surround it with quotes to make it stand out on the dialog title bar
372  wxString layerName = "\"" + fn.GetFullName() + "\"";
373 
374  // Display dialog to let user select a layer for the Gerber
376 
377  if( jj != UNSELECTED_LAYER && jj != UNDEFINED_LAYER && !IsValidLayer( jj ) )
378  return;
379 
380  if( jj != m_layersLookUpTable[m_buttonTable[ii]] )
381  {
383 
384  if( jj == UNSELECTED_LAYER )
385  {
386  m_layersList[ii]->SetLabel( _( "Do not export" ) );
387 
388  // Change the text color to blue (to highlight
389  // that this layer is *not* being exported)
390  m_layersList[ii]->SetForegroundColour( *wxBLUE );
391  }
392  else if( jj == UNDEFINED_LAYER )
393  {
394  m_layersList[ii]->SetLabel( _( "Hole data" ) );
395 
396  // Change the text color to fuchsia (to highlight
397  // that this layer *is* being exported)
398  m_layersList[ii]->SetForegroundColour( wxColour( 255, 0, 128 ) );
399  }
400  else
401  {
402  m_layersList[ii]->SetLabel( GetPCBDefaultLayerName( jj ) );
403 
404  // Change the text color to fuchsia (to highlight
405  // that this layer *is* being exported)
406  m_layersList[ii]->SetForegroundColour( wxColour( 255, 0, 128 ) );
407  }
408  }
409 }
410 
411 
413 {
414  if( !wxDialog::TransferDataFromWindow() )
415  return false;
416 
417  // Board must have enough copper layers to handle selected internal layers.
419 
420  int inner_layer_max = 0;
421 
422  for( int ii = 0; ii < GERBER_DRAWLAYERS_COUNT; ++ii )
423  {
424  if( m_layersLookUpTable[ii] < F_Cu )
425  {
426  if( m_layersLookUpTable[ii ] > inner_layer_max )
427  inner_layer_max = m_layersLookUpTable[ii];
428  }
429  }
430 
431  // inner_layer_max must be less than (or equal to the number of internal copper layers
432  // internal copper layers = m_exportBoardCopperLayersCount-2
433  if( inner_layer_max > m_exportBoardCopperLayersCount-2 )
434  {
435  wxMessageBox( _("Exported board does not have enough copper layers to handle selected "
436  "inner layers") );
437  return false;
438  }
439 
440  return true;
441 }
442 
443 
444 int LAYERS_MAP_DIALOG::findKnownGerbersLoaded( std::vector<int>& aGerber2KicadMapping )
445 {
446  int numKnownGerbers = 0;
447 
448  // We can automatically map Gerbers using different techniques. The first thing we
449  // try is to see if any of the loaded Gerbers were created by or use the
450  // Altium/Protel file extensions
451  numKnownGerbers += findNumAltiumGerbersLoaded( aGerber2KicadMapping );
452 
453  // Next we check if any of the loaded Gerbers are X2 Gerbers and if they contain
454 
455  // layer information in "File Functions". For info about X2 Gerbers see
456  // http://www.ucamco.com/files/downloads/file/81/the_gerber_file_format_specification.pdf
457  numKnownGerbers += findNumX2GerbersLoaded( aGerber2KicadMapping );
458 
459  // Finally, check if any of the loaded Gerbers use the KiCad naming conventions
460  numKnownGerbers += findNumKiCadGerbersLoaded( aGerber2KicadMapping );
461 
462  return numKnownGerbers;
463 }
464 
465 
466 int LAYERS_MAP_DIALOG::findNumAltiumGerbersLoaded( std::vector<int>& aGerber2KicadMapping )
467 {
468  // The next comment preserves initializer formatting below it
469  // clang-format off
470  // This map contains the known Altium file extensions for Gerbers that we care about,
471  // along with their corresponding KiCad layer
472  std::map<wxString, PCB_LAYER_ID> altiumExt{
473  { "GTL", F_Cu }, // Top copper
474  { "G1", In1_Cu }, // Inner layers 1 - 30
475  { "G2", In2_Cu },
476  { "G3", In3_Cu },
477  { "G4", In4_Cu },
478  { "G5", In5_Cu },
479  { "G6", In6_Cu },
480  { "G7", In7_Cu },
481  { "G8", In8_Cu },
482  { "G9", In9_Cu },
483  { "G10", In10_Cu },
484  { "G11", In11_Cu },
485  { "G12", In12_Cu },
486  { "G13", In13_Cu },
487  { "G14", In14_Cu },
488  { "G15", In15_Cu },
489  { "G16", In16_Cu },
490  { "G17", In17_Cu },
491  { "G18", In18_Cu },
492  { "G19", In19_Cu },
493  { "G20", In20_Cu },
494  { "G21", In21_Cu },
495  { "G22", In22_Cu },
496  { "G23", In23_Cu },
497  { "G24", In24_Cu },
498  { "G25", In25_Cu },
499  { "G26", In26_Cu },
500  { "G27", In27_Cu },
501  { "G28", In28_Cu },
502  { "G29", In29_Cu },
503  { "G30", In30_Cu },
504  { "GBL", B_Cu }, // Bottom copper
505  { "GTP", F_Paste }, // Paste top
506  { "GBP", B_Paste }, // Paste bottom
507  { "GTO", F_SilkS }, // Silkscreen top
508  { "GBO", B_SilkS }, // Silkscreen bottom
509  { "GTS", F_Mask }, // Soldermask top
510  { "GBS", B_Mask }, // Soldermask bottom
511  { "GM1", Eco1_User }, // Altium mechanical layer 1
512  { "GM2", Eco2_User }, // Altium mechanical layer 2
513  { "GKO", Edge_Cuts } // PCB Outline
514  };
515  // clang-format on
516 
517  std::map<wxString, PCB_LAYER_ID>::iterator it;
518 
519  int numAltiumMatches = 0; // Assume we won't find Altium Gerbers
520 
522 
523  // If the passed vector isn't empty but is too small to hold the loaded
524  // Gerbers, then bail because something isn't right.
525 
526  if( ( aGerber2KicadMapping.size() != 0 )
527  && ( aGerber2KicadMapping.size() != (size_t) m_gerberActiveLayersCount ) )
528  return numAltiumMatches;
529 
530  // If the passed vector is empty, set it to the same number of elements as there
531  // are loaded Gerbers, and set each to "UNSELECTED_LAYER"
532 
533  if( aGerber2KicadMapping.size() == 0 )
534  aGerber2KicadMapping.assign( m_gerberActiveLayersCount, UNSELECTED_LAYER );
535 
536  // Loop through all loaded Gerbers looking for any with Altium specific extensions
537  for( int ii = 0; ii < m_gerberActiveLayersCount; ii++ )
538  {
539  if( images->GetGbrImage( ii ) )
540  {
541  // Get file name of Gerber loaded on this layer.
542  wxFileName fn( images->GetGbrImage( ii )->m_FileName );
543 
544  // Get uppercase version of file extension
545  wxString FileExt = fn.GetExt();
546  FileExt.MakeUpper();
547 
548  // Check for matching Altium Gerber file extension we'll handle
549  it = altiumExt.find( FileExt );
550 
551  if( it != altiumExt.end() )
552  {
553  // We got a match, so store the KiCad layer number. We verify it's set to
554  // "UNSELECTED_LAYER" in case the passed vector already had entries
555  // matched to other known Gerber files. This will preserve them.
556 
557  if( aGerber2KicadMapping[ii] == UNSELECTED_LAYER )
558  {
559  aGerber2KicadMapping[ii] = it->second;
560  numAltiumMatches++;
561  }
562  }
563  }
564  }
565 
566  // Return number of Altium Gerbers we found. Each index in the passed vector corresponds to
567  // a loaded Gerber layer, and the entry will contain the index to the matching
568  // KiCad layer for Altium Gerbers, or "UNSELECTED_LAYER" for the rest.
569  return numAltiumMatches;
570 }
571 
572 
573 int LAYERS_MAP_DIALOG::findNumKiCadGerbersLoaded( std::vector<int>& aGerber2KicadMapping )
574 {
575  // The next comment preserves initializer formatting below it
576  // clang-format off
577  // This map contains the known KiCad suffixes used for Gerbers that we care about,
578  // along with their corresponding KiCad layer
579  std::map<wxString, PCB_LAYER_ID> kicadLayers
580  {
581  { "-F_Cu", F_Cu },
582  { "-In1_Cu", In1_Cu },
583  { "-In2_Cu", In2_Cu },
584  { "-In3_Cu", In3_Cu },
585  { "-In4_Cu", In4_Cu },
586  { "-In5_Cu", In5_Cu },
587  { "-In6_Cu", In6_Cu },
588  { "-In7_Cu", In7_Cu },
589  { "-In8_Cu", In8_Cu },
590  { "-In9_Cu", In9_Cu },
591  { "-In10_Cu", In10_Cu },
592  { "-In11_Cu", In11_Cu },
593  { "-In12_Cu", In12_Cu },
594  { "-In13_Cu", In13_Cu },
595  { "-In14_Cu", In14_Cu },
596  { "-In15_Cu", In15_Cu },
597  { "-In16_Cu", In16_Cu },
598  { "-In17_Cu", In17_Cu },
599  { "-In18_Cu", In18_Cu },
600  { "-In19_Cu", In19_Cu },
601  { "-In20_Cu", In20_Cu },
602  { "-In21_Cu", In21_Cu },
603  { "-In22_Cu", In22_Cu },
604  { "-In23_Cu", In23_Cu },
605  { "-In24_Cu", In24_Cu },
606  { "-In25_Cu", In25_Cu },
607  { "-In26_Cu", In26_Cu },
608  { "-In27_Cu", In27_Cu },
609  { "-In28_Cu", In28_Cu },
610  { "-In29_Cu", In29_Cu },
611  { "-In30_Cu", In30_Cu },
612  { "-B_Cu", B_Cu },
613  { "-B_Adhes", B_Adhes },
614  { "-F_Adhes", F_Adhes },
615  { "-B_Paste", B_Paste },
616  { "-F_Paste", F_Paste },
617  { "-B_SilkS", B_SilkS },
618  { "-F_SilkS", F_SilkS },
619  { "-B_Mask", B_Mask },
620  { "-F_Mask", F_Mask },
621  { "-Dwgs_User", Dwgs_User },
622  { "-Cmts_User", Cmts_User },
623  { "-Eco1_User", Eco1_User },
624  { "-Eco2_User", Eco2_User },
625  { "-Edge_Cuts", Edge_Cuts }
626  };
627  // clang-format on
628 
629  std::map<wxString, PCB_LAYER_ID>::iterator it;
630 
631  int numKicadMatches = 0; // Assume we won't find KiCad Gerbers
632 
634 
635  // If the passed vector isn't empty but is too small to hold the loaded
636  // Gerbers, then bail because something isn't right.
637 
638  if( ( aGerber2KicadMapping.size() != 0 )
639  && ( aGerber2KicadMapping.size() < (size_t) m_gerberActiveLayersCount ) )
640  return numKicadMatches;
641 
642  // If the passed vector is empty, set it to the same number of elements as there
643  // are loaded Gerbers, and set each to "UNSELECTED_LAYER"
644 
645  if( aGerber2KicadMapping.size() == 0 )
646  aGerber2KicadMapping.assign( m_gerberActiveLayersCount, UNSELECTED_LAYER );
647 
648  // Loop through all loaded Gerbers looking for any with KiCad specific layer names
649  for( int ii = 0; ii < m_gerberActiveLayersCount; ii++ )
650  {
651  if( images->GetGbrImage( ii ) )
652  {
653  // Get file name of Gerber loaded on this layer.
654  wxFileName fn( images->GetGbrImage( ii )->m_FileName );
655 
656  wxString layerName = fn.GetName();
657 
658  // To create Gerber file names, KiCad appends a suffix consisting of a "-" and the
659  // name of the layer to the project name. We need to isolate the suffix if present
660  // and see if it's a known KiCad layer name. Start by looking for the last "-" in
661  // the file name.
662  int dashPos = layerName.Find( '-', true );
663 
664  // If one was found, isolate the suffix from the "-" to the end of the file name
665  wxString suffix;
666 
667  if( dashPos != wxNOT_FOUND )
668  suffix = layerName.Right( layerName.length() - dashPos );
669 
670  // Check if the string we've isolated matches any known KiCad layer names
671  it = kicadLayers.find( suffix );
672 
673  if( it != kicadLayers.end() )
674  {
675  // We got a match, so store the KiCad layer number. We verify it's set to
676  // "UNSELECTED_LAYER" in case the passed vector already had entries
677  // matched to other known Gerber files. This will preserve them.
678 
679  if( aGerber2KicadMapping[ii] == UNSELECTED_LAYER )
680  {
681  aGerber2KicadMapping[ii] = it->second;
682  numKicadMatches++;
683  }
684  }
685  }
686  }
687 
688  // Return number of KiCad Gerbers we found. Each index in the passed vector corresponds to
689  // a loaded Gerber layer, and the entry will contain the index to the matching
690  // KiCad layer for KiCad Gerbers, or "UNSELECTED_LAYER" for the rest.
691  return numKicadMatches;
692 }
693 
694 
695 int LAYERS_MAP_DIALOG::findNumX2GerbersLoaded( std::vector<int>& aGerber2KicadMapping )
696 {
697  // The next comment preserves initializer formatting below it
698  // clang-format off
699  // This map contains the known KiCad X2 "File Function" values used for Gerbers that we
700  // care about, along with their corresponding KiCad layer
701  std::map<wxString, PCB_LAYER_ID> kicadLayers
702  {
703  { "Top", F_Cu },
704  { "L2", In1_Cu },
705  { "L3", In2_Cu },
706  { "L4", In3_Cu },
707  { "L5", In4_Cu },
708  { "L6", In5_Cu },
709  { "L7", In6_Cu },
710  { "L8", In7_Cu },
711  { "L9", In8_Cu },
712  { "L10", In9_Cu },
713  { "L11", In10_Cu },
714  { "L12", In11_Cu },
715  { "L13", In12_Cu },
716  { "L14", In13_Cu },
717  { "L15", In14_Cu },
718  { "L16", In15_Cu },
719  { "L17", In16_Cu },
720  { "L18", In17_Cu },
721  { "L19", In18_Cu },
722  { "L20", In19_Cu },
723  { "L21", In20_Cu },
724  { "L22", In21_Cu },
725  { "L23", In22_Cu },
726  { "L24", In23_Cu },
727  { "L25", In24_Cu },
728  { "L26", In25_Cu },
729  { "L27", In26_Cu },
730  { "L28", In27_Cu },
731  { "L29", In28_Cu },
732  { "L30", In29_Cu },
733  { "Bot", B_Cu },
734  { "BotGlue", B_Adhes },
735  { "TopGlue", F_Adhes },
736  { "BotPaste", B_Paste },
737  { "TopPaste", F_Paste },
738  { "BotLegend", B_SilkS },
739  { "TopLegend", F_SilkS },
740  { "BotSoldermask", B_Mask },
741  { "TopSoldermask", F_Mask },
742  { "FabricationDrawing", Dwgs_User },
743  { "OtherDrawing", Cmts_User },
744  { "TopAssemblyDrawing", Eco1_User },
745  { "BotAssemblyDrawing", Eco2_User },
746  { "PProfile", Edge_Cuts }, // Plated PCB outline
747  { "NPProfile", Edge_Cuts } // Non-plated PCB outline
748  };
749  // clang-format on
750 
751  std::map<wxString, PCB_LAYER_ID>::iterator it;
752 
753  int numKicadMatches = 0; // Assume we won't find KiCad Gerbers
754 
755  wxString mapThis;
756 
758 
759  // If the passed vector isn't empty but is too small to hold the loaded
760  // Gerbers, then bail because something isn't right.
761 
762  if( ( aGerber2KicadMapping.size() != 0 )
763  && ( aGerber2KicadMapping.size() < (size_t) m_gerberActiveLayersCount ) )
764  return numKicadMatches;
765 
766  // If the passed vector is empty, set it to the same number of elements as there
767  // are loaded Gerbers, and set each to "UNSELECTED_LAYER"
768 
769  if( aGerber2KicadMapping.size() == 0 )
770  aGerber2KicadMapping.assign( m_gerberActiveLayersCount, UNSELECTED_LAYER );
771 
772  // Loop through all loaded Gerbers looking for any with X2 File Functions
773  for( int ii = 0; ii < m_gerberActiveLayersCount; ii++ )
774  {
775  if( images->GetGbrImage( ii ) )
776  {
778 
779  mapThis = "";
780 
781  if( images->GetGbrImage( ii )->m_IsX2_file )
782  {
783  wxCHECK( x2, numKicadMatches );
784 
785  if( x2->IsCopper() )
786  {
787  // This is a copper layer, so figure out which one
788  mapThis = x2->GetBrdLayerSide(); // Returns "Top", "Bot" or "Inr"
789 
790  // To map inner layers properly, we need the layer number
791  if( mapThis.IsSameAs( wxT( "Inr" ), false ) )
792  mapThis = x2->GetBrdLayerId(); // Returns "L2", "L5", etc
793  }
794  else
795  {
796  // Create strings like "TopSolderMask" or "BotPaste" for non-copper layers
797  mapThis << x2->GetBrdLayerId() << x2->GetFileType();
798  }
799 
800  // Check if the string we've isolated matches any known X2 layer names
801  it = kicadLayers.find( mapThis );
802 
803  if( it != kicadLayers.end() )
804  {
805  // We got a match, so store the KiCad layer number. We verify it's set to
806  // "UNSELECTED_LAYER" in case the passed vector already had entries
807  // matched to other known Gerber files. This will preserve them.
808 
809  if( aGerber2KicadMapping[ii] == UNSELECTED_LAYER )
810  {
811  aGerber2KicadMapping[ii] = it->second;
812  numKicadMatches++;
813  }
814  }
815  }
816  }
817  }
818 
819  return numKicadMatches;
820 }
const wxString & GetFileType()
the type of layer (Copper, Soldermask ... )
X2_ATTRIBUTE_FILEFUNCTION ( from TF.FileFunction in Gerber file) Example file function: TF....
X2_ATTRIBUTE_FILEFUNCTION * m_FileFunction
int SelectPCBLayer(int aDefaultLayer, int aCopperLayerCount, const wxString &aGerberName)
Show the dialog box for layer selection.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
int LAYER_NUM
This can be replaced with int and removed.
Definition: layer_ids.h:41
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
void OnResetClick(wxCommandEvent &event) override
bool IsCopper()
return true if the filefunction type is "Copper"
int m_buttonTable[int(GERBER_DRAWLAYERS_COUNT)+1]
bool m_IsX2_file
< True if a X2 gerber attribute was found in file.
EVT_COMMAND_RANGE(ID_BUTTON_0, ID_BUTTON_0+GERBER_DRAWLAYERS_COUNT-1, wxEVT_COMMAND_BUTTON_CLICKED, LAYERS_MAP_DIALOG::OnSelectLayer) LAYERS_MAP_DIALOG
Show the Gerber files loaded and allow the user to choose between Gerber layers and pcb layers.
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
#define GERBER_DRAWLAYERS_COUNT
Definition: layer_ids.h:378
Class LAYERS_MAP_DIALOG_BASE.
wxStaticText * m_layersList[int(GERBER_DRAWLAYERS_COUNT)+1]
#define _(s)
int findNumKiCadGerbersLoaded(std::vector< int > &aGerber2KicadMapping)
Find number of loaded Gerbers using KiCad naming convention.
const wxString & GetBrdLayerId()
the brd layer identifier: Ln, only for Copper type or Top, Bot for other types
LAYER_NUM m_layersLookUpTable[GERBER_DRAWLAYERS_COUNT]
GBR_LAYOUT * GetGerberLayout() const
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
const wxString GetPCBDefaultLayerName(LAYER_NUM aLayerNumber)
int findNumAltiumGerbersLoaded(std::vector< int > &aGerber2KicadMapping)
Find number of loaded Gerbers using Altium file extensions.
Definition: layer_ids.h:71
void OnBrdLayersCountSelection(wxCommandEvent &event) override
void OnGetSetup(wxCommandEvent &event) override
const wxString & GetBrdLayerSide()
the brd layer Pos: Top, Bot, Inr same as GetBrdLayerId() for non copper type
GERBER_FILE_IMAGE_LIST * GetImagesList() const
Definition: gbr_layout.cpp:41
int findNumX2GerbersLoaded(std::vector< int > &aGerber2KicadMapping)
Find number of loaded Gerbers using X2 File Functions to define layers.
bool IsValidLayer(LAYER_NUM aLayerId)
Test whether a given integer is a valid layer index, i.e.
Definition: layer_ids.h:774
int findKnownGerbersLoaded(std::vector< int > &aGerber2KicadMapping)
Find number of loaded Gerbers where the matching KiCad layer can be identified.
void OnSelectLayer(wxCommandEvent &event)
void OnStoreSetup(wxCommandEvent &event) override