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