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