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.

815 lines
30 KiB

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