You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

825 lines
30 KiB

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