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-2022 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
30#include <gerber_file_image.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#include <gestfich.h>
43
44extern const wxString GetPCBDefaultLayerName( int aLayerNumber );
45
46
51};
52
53
55
56
57BEGIN_EVENT_TABLE( LAYERS_MAP_DIALOG, LAYERS_MAP_DIALOG_BASE )
59 wxEVT_COMMAND_BUTTON_CLICKED,
60 LAYERS_MAP_DIALOG::OnSelectLayer )
61END_EVENT_TABLE()
62
63
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;
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( int 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 )
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{
258
261
264
265}
266
267
269{
270 int id = event.GetSelection();
271 m_exportBoardCopperLayersCount = ( id + 1 ) * 2;
272}
273
274
275void LAYERS_MAP_DIALOG::OnResetClick( wxCommandEvent& event )
276{
277 for( int ii = 0; ii < m_gerberActiveLayersCount; ++ii )
278 {
280 m_layersList[ii]->SetLabel( _( "Do not export" ) );
281 m_layersList[ii]->SetForegroundColour( *wxBLUE );
282 m_buttonTable[ii] = ii;
283 }
284 // wxWidgets doesn't appear to invalidate / update the StaticText displays for color change
285 // so we do it manually
286 Refresh();
287 Update();
288}
289
290
291void LAYERS_MAP_DIALOG::OnStoreSetup( wxCommandEvent& event )
292{
294 config->m_BoardLayersCount = m_exportBoardCopperLayersCount;
295
296 config->m_GerberToPcbLayerMapping.clear();
297
298 for( int ii = 0; ii < GERBER_DRAWLAYERS_COUNT; ++ii )
299 config->m_GerberToPcbLayerMapping.push_back( m_layersLookUpTable[ii] );
300
301 // Enable the "Get Stored Choice" button in case it was disabled in "initDialog()"
302 // due to no previously stored choices.
303 m_buttonRetrieve->Enable( true );
304}
305
306
307void LAYERS_MAP_DIALOG::OnGetSetup( wxCommandEvent& event )
308{
310
311 m_exportBoardCopperLayersCount = config->m_BoardLayersCount;
313
314 int idx = ( m_exportBoardCopperLayersCount / 2 ) - 1;
315 m_comboCopperLayersCount->SetSelection( idx );
316
317 for( int ii = 0; ii < GERBER_DRAWLAYERS_COUNT; ++ii )
318 {
319 // Ensure the layer mapping in config exists for this layer, and store it
320 if( (size_t)ii >= config->m_GerberToPcbLayerMapping.size() )
321 break;
322
323 m_layersLookUpTable[ii] = config->m_GerberToPcbLayerMapping[ ii ];
324 }
325
326 for( int ii = 0; ii < m_gerberActiveLayersCount; ii++ )
327 {
328 int layer = m_layersLookUpTable[ii];
329
330 if( layer == UNSELECTED_LAYER )
331 {
332 m_layersList[ii]->SetLabel( _( "Do not export" ) );
333 m_layersList[ii]->SetForegroundColour( *wxBLUE );
334 }
335 else if( layer == UNDEFINED_LAYER )
336 {
337 m_layersList[ii]->SetLabel( _( "Hole data" ) );
338 m_layersList[ii]->SetForegroundColour( wxColour( 255, 0, 128 ) );
339 }
340 else
341 {
342 m_layersList[ii]->SetLabel( GetPCBDefaultLayerName( layer ) );
343 m_layersList[ii]->SetForegroundColour( wxColour( 255, 0, 128 ) );
344 }
345 }
346}
347
348
349void LAYERS_MAP_DIALOG::OnSelectLayer( wxCommandEvent& event )
350{
351 int ii = event.GetId() - ID_BUTTON_0;
352
353 if( ii < 0 || ii >= GERBER_DRAWLAYERS_COUNT )
354 {
355 wxFAIL_MSG( wxT( "Bad layer id" ) );
356 return;
357 }
358
359 int jj = m_layersLookUpTable[ m_buttonTable[ii] ];
360
361 if( jj != UNSELECTED_LAYER && jj != UNDEFINED_LAYER && !IsValidLayer( jj ) )
362 jj = B_Cu; // (Defaults to "Copper" layer.)
363
364 // Get file name of Gerber loaded on this layer
365 wxFileName fn( m_Parent->GetGerberLayout()->GetImagesList()->GetGbrImage( ii )->m_FileName );
366
367 // Surround it with quotes to make it stand out on the dialog title bar
368 wxString layerName = wxT( "\"" ) + fn.GetFullName() + wxT( "\"" );
369
370 // Display dialog to let user select a layer for the Gerber
372
373 if( jj != UNSELECTED_LAYER && jj != UNDEFINED_LAYER && !IsValidLayer( jj ) )
374 return;
375
376 if( jj != m_layersLookUpTable[m_buttonTable[ii]] )
377 {
379
380 if( jj == UNSELECTED_LAYER )
381 {
382 m_layersList[ii]->SetLabel( _( "Do not export" ) );
383
384 // Change the text color to blue (to highlight
385 // that this layer is *not* being exported)
386 m_layersList[ii]->SetForegroundColour( *wxBLUE );
387 }
388 else if( jj == UNDEFINED_LAYER )
389 {
390 m_layersList[ii]->SetLabel( _( "Hole data" ) );
391
392 // Change the text color to fuchsia (to highlight
393 // that this layer *is* being exported)
394 m_layersList[ii]->SetForegroundColour( wxColour( 255, 0, 128 ) );
395 }
396 else
397 {
398 m_layersList[ii]->SetLabel( GetPCBDefaultLayerName( jj ) );
399
400 // Change the text color to fuchsia (to highlight
401 // that this layer *is* being exported)
402 m_layersList[ii]->SetForegroundColour( wxColour( 255, 0, 128 ) );
403 }
404 }
405 // wxWidgets doesn't appear to invalidate / update the StaticText displays for color change
406 // so we do it manually
407 Refresh();
408 Update();
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
444int 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
466int 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 { wxT( "GTL" ), F_Cu }, // Top copper
474 { wxT( "G1" ), In1_Cu }, // Inner layers 1 - 30
475 { wxT( "G2" ), In2_Cu },
476 { wxT( "G3" ), In3_Cu },
477 { wxT( "G4" ), In4_Cu },
478 { wxT( "G5" ), In5_Cu },
479 { wxT( "G6" ), In6_Cu },
480 { wxT( "G7" ), In7_Cu },
481 { wxT( "G8" ), In8_Cu },
482 { wxT( "G9" ), In9_Cu },
483 { wxT( "G10" ), In10_Cu },
484 { wxT( "G11" ), In11_Cu },
485 { wxT( "G12" ), In12_Cu },
486 { wxT( "G13" ), In13_Cu },
487 { wxT( "G14" ), In14_Cu },
488 { wxT( "G15" ), In15_Cu },
489 { wxT( "G16" ), In16_Cu },
490 { wxT( "G17" ), In17_Cu },
491 { wxT( "G18" ), In18_Cu },
492 { wxT( "G19" ), In19_Cu },
493 { wxT( "G20" ), In20_Cu },
494 { wxT( "G21" ), In21_Cu },
495 { wxT( "G22" ), In22_Cu },
496 { wxT( "G23" ), In23_Cu },
497 { wxT( "G24" ), In24_Cu },
498 { wxT( "G25" ), In25_Cu },
499 { wxT( "G26" ), In26_Cu },
500 { wxT( "G27" ), In27_Cu },
501 { wxT( "G28" ), In28_Cu },
502 { wxT( "G29" ), In29_Cu },
503 { wxT( "G30" ), In30_Cu },
504 { wxT( "GBL" ), B_Cu }, // Bottom copper
505 { wxT( "GTP" ), F_Paste }, // Paste top
506 { wxT( "GBP" ), B_Paste }, // Paste bottom
507 { wxT( "GTO" ), F_SilkS }, // Silkscreen top
508 { wxT( "GBO" ), B_SilkS }, // Silkscreen bottom
509 { wxT( "GTS" ), F_Mask }, // Soldermask top
510 { wxT( "GBS" ), B_Mask }, // Soldermask bottom
511 { wxT( "GM1" ), Eco1_User }, // Altium mechanical layer 1
512 { wxT( "GM2" ), Eco2_User }, // Altium mechanical layer 2
513 { wxT( "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
573int 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
695int 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 { wxT( "Top" ), F_Cu },
704 { wxT( "L2" ), In1_Cu },
705 { wxT( "L3" ), In2_Cu },
706 { wxT( "L4" ), In3_Cu },
707 { wxT( "L5" ), In4_Cu },
708 { wxT( "L6" ), In5_Cu },
709 { wxT( "L7" ), In6_Cu },
710 { wxT( "L8" ), In7_Cu },
711 { wxT( "L9" ), In8_Cu },
712 { wxT( "L10" ), In9_Cu },
713 { wxT( "L11" ), In10_Cu },
714 { wxT( "L12" ), In11_Cu },
715 { wxT( "L13" ), In12_Cu },
716 { wxT( "L14" ), In13_Cu },
717 { wxT( "L15" ), In14_Cu },
718 { wxT( "L16" ), In15_Cu },
719 { wxT( "L17" ), In16_Cu },
720 { wxT( "L18" ), In17_Cu },
721 { wxT( "L19" ), In18_Cu },
722 { wxT( "L20" ), In19_Cu },
723 { wxT( "L21" ), In20_Cu },
724 { wxT( "L22" ), In21_Cu },
725 { wxT( "L23" ), In22_Cu },
726 { wxT( "L24" ), In23_Cu },
727 { wxT( "L25" ), In24_Cu },
728 { wxT( "L26" ), In25_Cu },
729 { wxT( "L27" ), In26_Cu },
730 { wxT( "L28" ), In27_Cu },
731 { wxT( "L29" ), In28_Cu },
732 { wxT( "L30" ), In29_Cu },
733 { wxT( "Bot" ), B_Cu },
734 { wxT( "BotGlue" ), B_Adhes },
735 { wxT( "TopGlue" ), F_Adhes },
736 { wxT( "BotPaste" ), B_Paste },
737 { wxT( "TopPaste" ), F_Paste },
738 { wxT( "BotLegend" ), B_SilkS },
739 { wxT( "TopLegend" ), F_SilkS },
740 { wxT( "BotSoldermask" ), B_Mask },
741 { wxT( "TopSoldermask" ), F_Mask },
742 { wxT( "FabricationDrawing" ), Dwgs_User },
743 { wxT( "OtherDrawing" ), Cmts_User },
744 { wxT( "TopAssemblyDrawing" ), Eco1_User },
745 { wxT( "BotAssemblyDrawing" ), Eco2_User },
746 { wxT( "PProfile" ), Edge_Cuts }, // Plated PCB outline
747 { wxT( "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}
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
GERBER_FILE_IMAGE_LIST * GetImagesList() const
Definition: gbr_layout.cpp:41
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
X2_ATTRIBUTE_FILEFUNCTION * m_FileFunction
file function parameters, found in a TF command or a G04
wxString m_FileName
Full File Name for this layer.
bool m_IsX2_file
True if a X2 gerber attribute was found in file.
int SelectPCBLayer(int aDefaultLayer, int aCopperLayerCount, const wxString &aGerberName)
Show the dialog box for layer selection.
GBR_LAYOUT * GetGerberLayout() const
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:93
Class LAYERS_MAP_DIALOG_BASE.
Show the Gerber files loaded and allow the user to choose between Gerber layers and pcb layers.
void OnStoreSetup(wxCommandEvent &event) override
int findNumX2GerbersLoaded(std::vector< int > &aGerber2KicadMapping)
Find number of loaded Gerbers using X2 File Functions to define layers.
void OnBrdLayersCountSelection(wxCommandEvent &event) override
void OnSelectLayer(wxCommandEvent &event)
int findNumAltiumGerbersLoaded(std::vector< int > &aGerber2KicadMapping)
Find number of loaded Gerbers using Altium file extensions.
wxStaticText * m_layersList[int(GERBER_DRAWLAYERS_COUNT)+1]
int findKnownGerbersLoaded(std::vector< int > &aGerber2KicadMapping)
Find number of loaded Gerbers where the matching KiCad layer can be identified.
int findNumKiCadGerbersLoaded(std::vector< int > &aGerber2KicadMapping)
Find number of loaded Gerbers using KiCad naming convention.
void OnGetSetup(wxCommandEvent &event) override
int m_layersLookUpTable[GERBER_DRAWLAYERS_COUNT]
int m_buttonTable[int(GERBER_DRAWLAYERS_COUNT)+1]
void OnResetClick(wxCommandEvent &event) override
X2_ATTRIBUTE_FILEFUNCTION ( from TF.FileFunction in Gerber file) Example file function: TF....
bool IsCopper()
return true if the filefunction type is "Copper"
const wxString & GetBrdLayerSide()
the brd layer Pos: Top, Bot, Inr same as GetBrdLayerId() for non copper type
const wxString & GetFileType()
the type of layer (Copper, Soldermask ... )
const wxString & GetBrdLayerId()
the brd layer identifier: Ln, only for Copper type or Top, Bot for other types
const wxString GetPCBDefaultLayerName(int aLayerNumber)
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 _(s)
@ ID_GERBER_END_LIST
Definition: gerbview_id.h:70
@ In22_Cu
Definition: layer_ids.h:86
@ In11_Cu
Definition: layer_ids.h:75
@ In29_Cu
Definition: layer_ids.h:93
@ In30_Cu
Definition: layer_ids.h:94
@ In17_Cu
Definition: layer_ids.h:81
@ B_Adhes
Definition: layer_ids.h:97
@ Edge_Cuts
Definition: layer_ids.h:113
@ Dwgs_User
Definition: layer_ids.h:109
@ F_Paste
Definition: layer_ids.h:101
@ In9_Cu
Definition: layer_ids.h:73
@ Cmts_User
Definition: layer_ids.h:110
@ In19_Cu
Definition: layer_ids.h:83
@ In7_Cu
Definition: layer_ids.h:71
@ In28_Cu
Definition: layer_ids.h:92
@ In26_Cu
Definition: layer_ids.h:90
@ F_Adhes
Definition: layer_ids.h:98
@ B_Mask
Definition: layer_ids.h:106
@ B_Cu
Definition: layer_ids.h:95
@ Eco1_User
Definition: layer_ids.h:111
@ F_Mask
Definition: layer_ids.h:107
@ In21_Cu
Definition: layer_ids.h:85
@ In23_Cu
Definition: layer_ids.h:87
@ B_Paste
Definition: layer_ids.h:100
@ In15_Cu
Definition: layer_ids.h:79
@ In2_Cu
Definition: layer_ids.h:66
@ UNSELECTED_LAYER
Definition: layer_ids.h:61
@ In10_Cu
Definition: layer_ids.h:74
@ F_SilkS
Definition: layer_ids.h:104
@ In4_Cu
Definition: layer_ids.h:68
@ UNDEFINED_LAYER
Definition: layer_ids.h:60
@ Eco2_User
Definition: layer_ids.h:112
@ In16_Cu
Definition: layer_ids.h:80
@ In24_Cu
Definition: layer_ids.h:88
@ In1_Cu
Definition: layer_ids.h:65
@ B_SilkS
Definition: layer_ids.h:103
@ In13_Cu
Definition: layer_ids.h:77
@ In8_Cu
Definition: layer_ids.h:72
@ In14_Cu
Definition: layer_ids.h:78
@ In12_Cu
Definition: layer_ids.h:76
@ In27_Cu
Definition: layer_ids.h:91
@ In6_Cu
Definition: layer_ids.h:70
@ In5_Cu
Definition: layer_ids.h:69
@ In3_Cu
Definition: layer_ids.h:67
@ In20_Cu
Definition: layer_ids.h:84
@ F_Cu
Definition: layer_ids.h:64
@ In18_Cu
Definition: layer_ids.h:82
@ In25_Cu
Definition: layer_ids.h:89
bool IsValidLayer(int aLayerId)
Test whether a given integer is a valid layer index, i.e.
Definition: layer_ids.h:803
#define GERBER_DRAWLAYERS_COUNT
Definition: layer_ids.h:402
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
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