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