2020-10-01 22:47:38 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Roberto Fernandez Bautista <roberto.fer.bau@gmail.com>
|
2021-07-27 12:22:27 +00:00
|
|
|
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-10-01 22:47:38 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-07-29 09:47:43 +00:00
|
|
|
#include <layer_ids.h>
|
2024-06-25 08:10:18 +00:00
|
|
|
#include <dialog_map_layers.h>
|
2020-10-01 22:47:38 +00:00
|
|
|
|
2020-10-18 11:31:07 +00:00
|
|
|
#include <wx/msgdlg.h>
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
wxString DIALOG_MAP_LAYERS::WrapRequired( const wxString& aLayerName )
|
2020-10-09 16:58:21 +00:00
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
return aLayerName + wxT( " *" );
|
2020-10-09 16:58:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
wxString DIALOG_MAP_LAYERS::UnwrapRequired( const wxString& aLayerName )
|
2020-10-09 16:58:21 +00:00
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
if( !aLayerName.EndsWith( wxT( " *" ) ) )
|
2020-10-09 16:58:21 +00:00
|
|
|
return aLayerName;
|
2021-07-27 12:22:27 +00:00
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
return aLayerName.Left( aLayerName.Length() - 2 );
|
|
|
|
}
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
const INPUT_LAYER_DESC* DIALOG_MAP_LAYERS::GetLayerDescription( const wxString& aLayerName ) const
|
2020-10-09 16:58:21 +00:00
|
|
|
{
|
|
|
|
wxString layerName = UnwrapRequired( aLayerName );
|
2021-07-27 12:22:27 +00:00
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
for( const INPUT_LAYER_DESC& layerDescription : m_input_layers )
|
|
|
|
{
|
|
|
|
if( layerDescription.Name == layerName )
|
|
|
|
return &layerDescription;
|
|
|
|
}
|
2021-07-27 12:22:27 +00:00
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-01 22:47:38 +00:00
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
PCB_LAYER_ID DIALOG_MAP_LAYERS::GetSelectedLayerID()
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
|
|
|
// First check if there is a KiCad element selected
|
|
|
|
wxString selectedKiCadLayerName;
|
|
|
|
long itemIndex = -1;
|
|
|
|
|
2020-10-03 02:16:39 +00:00
|
|
|
if( ( itemIndex = m_kicad_layers_list->GetNextItem( itemIndex, wxLIST_NEXT_ALL,
|
|
|
|
wxLIST_STATE_SELECTED ) ) != wxNOT_FOUND )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
|
|
|
selectedKiCadLayerName = m_kicad_layers_list->GetItemText( itemIndex );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return PCB_LAYER_ID::UNDEFINED_LAYER;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There should only be one selected (or none) as the list is set with wxLC_SINGLE_SEL style
|
2020-10-03 02:16:39 +00:00
|
|
|
wxASSERT_MSG( ( m_kicad_layers_list->GetNextItem( itemIndex, wxLIST_NEXT_ALL,
|
|
|
|
wxLIST_STATE_SELECTED ) ) == wxNOT_FOUND,
|
2022-02-04 22:44:59 +00:00
|
|
|
wxT( "There are more than one KiCad layer selected (unexpected)" ) );
|
2020-10-01 22:47:38 +00:00
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
for( int layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
|
|
|
if( LayerName( ToLAYER_ID( layer ) ) == selectedKiCadLayerName )
|
|
|
|
return ToLAYER_ID( layer );
|
|
|
|
}
|
|
|
|
|
|
|
|
return PCB_LAYER_ID::UNDEFINED_LAYER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
PCB_LAYER_ID DIALOG_MAP_LAYERS::GetAutoMatchLayerID( const wxString& aInputLayerName )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
2020-10-09 16:58:21 +00:00
|
|
|
wxString pureInputLayerName = UnwrapRequired( aInputLayerName );
|
2024-06-25 08:10:18 +00:00
|
|
|
|
|
|
|
for( const INPUT_LAYER_DESC& inputLayerDesc : m_input_layers )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
2020-10-09 16:58:21 +00:00
|
|
|
if( inputLayerDesc.Name == pureInputLayerName
|
|
|
|
&& inputLayerDesc.AutoMapLayer != PCB_LAYER_ID::UNSELECTED_LAYER )
|
|
|
|
{
|
2020-10-01 22:47:38 +00:00
|
|
|
return inputLayerDesc.AutoMapLayer;
|
2020-10-09 16:58:21 +00:00
|
|
|
}
|
2020-10-01 22:47:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return PCB_LAYER_ID::UNDEFINED_LAYER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
void DIALOG_MAP_LAYERS::AddMappings()
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
|
|
|
PCB_LAYER_ID selectedKiCadLayerID = GetSelectedLayerID();
|
|
|
|
|
|
|
|
if( selectedKiCadLayerID == PCB_LAYER_ID::UNDEFINED_LAYER )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Now iterate through each selected layer in the unmatched layers list
|
|
|
|
int itemIndex = -1;
|
|
|
|
wxArrayInt rowsToDelete;
|
|
|
|
|
2020-10-03 02:16:39 +00:00
|
|
|
while( ( itemIndex = m_unmatched_layers_list->GetNextItem( itemIndex, wxLIST_NEXT_ALL,
|
|
|
|
wxLIST_STATE_SELECTED ) )
|
2020-10-01 22:47:38 +00:00
|
|
|
!= wxNOT_FOUND )
|
|
|
|
{
|
|
|
|
wxString selectedLayerName = m_unmatched_layers_list->GetItemText( itemIndex );
|
|
|
|
wxString kiName = LayerName( selectedKiCadLayerID );
|
|
|
|
|
|
|
|
// add layer pair to the GUI list and also to the map
|
|
|
|
long newItemIndex = m_matched_layers_list->InsertItem( 0, selectedLayerName );
|
|
|
|
m_matched_layers_list->SetItem( newItemIndex, 1, kiName );
|
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
m_matched_layers_map.insert(
|
|
|
|
{ UnwrapRequired( selectedLayerName ), selectedKiCadLayerID } );
|
2020-10-01 22:47:38 +00:00
|
|
|
|
|
|
|
// remove selected layer from vector and also GUI list
|
|
|
|
for( auto iter = m_unmatched_layer_names.begin(); iter != m_unmatched_layer_names.end();
|
|
|
|
++iter )
|
|
|
|
{
|
|
|
|
if( *iter == selectedLayerName )
|
|
|
|
{
|
|
|
|
m_unmatched_layer_names.erase( iter );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsToDelete.Add( itemIndex );
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteListItems( rowsToDelete, m_unmatched_layers_list );
|
2020-10-16 16:07:15 +00:00
|
|
|
|
|
|
|
// Auto select the first item to improve ease-of-use
|
|
|
|
m_unmatched_layers_list->SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
|
2020-10-01 22:47:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
void DIALOG_MAP_LAYERS::RemoveMappings( int aStatus )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
|
|
|
wxArrayInt rowsToDelete;
|
|
|
|
int itemIndex = -1;
|
|
|
|
|
|
|
|
while( ( itemIndex = m_matched_layers_list->GetNextItem( itemIndex, wxLIST_NEXT_ALL, aStatus ) )
|
|
|
|
!= wxNOT_FOUND )
|
|
|
|
{
|
2020-10-09 16:58:21 +00:00
|
|
|
wxString selectedLayerName = m_matched_layers_list->GetItemText( itemIndex, 0 );
|
|
|
|
wxString pureSelectedLayerName = UnwrapRequired( selectedLayerName );
|
2020-10-01 22:47:38 +00:00
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
wxCHECK( m_matched_layers_map.find( pureSelectedLayerName ) != m_matched_layers_map.end(),
|
2020-10-01 22:47:38 +00:00
|
|
|
/*void*/ );
|
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
m_matched_layers_map.erase( pureSelectedLayerName );
|
2020-10-01 22:47:38 +00:00
|
|
|
rowsToDelete.Add( itemIndex );
|
|
|
|
|
|
|
|
m_unmatched_layers_list->InsertItem( 0, selectedLayerName );
|
|
|
|
m_unmatched_layer_names.push_back( selectedLayerName );
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteListItems( rowsToDelete, m_matched_layers_list );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
void DIALOG_MAP_LAYERS::DeleteListItems( const wxArrayInt& aRowsToDelete, wxListCtrl* aListCtrl )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
2024-06-25 08:10:18 +00:00
|
|
|
for( long n = (long) aRowsToDelete.GetCount() - 1; 0 <= n; n-- )
|
2020-10-01 22:47:38 +00:00
|
|
|
aListCtrl->DeleteItem( aRowsToDelete[n] );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
void DIALOG_MAP_LAYERS::OnAutoMatchLayersClicked( wxCommandEvent& event )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
|
|
|
// Iterate through each selected layer in the unmatched layers list
|
|
|
|
int itemIndex = -1;
|
|
|
|
wxArrayInt rowsToDelete;
|
|
|
|
|
2020-10-03 02:16:39 +00:00
|
|
|
while( ( itemIndex = m_unmatched_layers_list->GetNextItem( itemIndex, wxLIST_NEXT_ALL,
|
|
|
|
wxLIST_STATE_DONTCARE ) )
|
2020-10-01 22:47:38 +00:00
|
|
|
!= wxNOT_FOUND )
|
|
|
|
{
|
|
|
|
wxString layerName = m_unmatched_layers_list->GetItemText( itemIndex );
|
|
|
|
PCB_LAYER_ID autoMatchLayer = GetAutoMatchLayerID( layerName );
|
|
|
|
|
|
|
|
if( autoMatchLayer == PCB_LAYER_ID::UNDEFINED_LAYER )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
wxString kiName = LayerName( autoMatchLayer );
|
|
|
|
|
|
|
|
// add layer pair to the GUI list and also to the map
|
|
|
|
long newItemIndex = m_matched_layers_list->InsertItem( 0, layerName );
|
|
|
|
m_matched_layers_list->SetItem( newItemIndex, 1, kiName );
|
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
m_matched_layers_map.insert( { UnwrapRequired( layerName ), autoMatchLayer } );
|
2020-10-01 22:47:38 +00:00
|
|
|
|
|
|
|
// remove selected layer from vector and also GUI list
|
|
|
|
for( auto iter = m_unmatched_layer_names.begin(); iter != m_unmatched_layer_names.end();
|
|
|
|
++iter )
|
|
|
|
{
|
|
|
|
if( *iter == layerName )
|
|
|
|
{
|
|
|
|
m_unmatched_layer_names.erase( iter );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsToDelete.Add( itemIndex );
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteListItems( rowsToDelete, m_unmatched_layers_list );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
DIALOG_MAP_LAYERS::DIALOG_MAP_LAYERS( wxWindow* aParent,
|
|
|
|
const std::vector<INPUT_LAYER_DESC>& aLayerDesc ) :
|
2020-10-03 02:16:39 +00:00
|
|
|
DIALOG_IMPORTED_LAYERS_BASE( aParent )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
|
|
|
LSET kiCadLayers;
|
|
|
|
|
|
|
|
// Read in the input layers
|
2024-06-25 08:10:18 +00:00
|
|
|
for( const INPUT_LAYER_DESC& inLayer : aLayerDesc )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
|
|
|
m_input_layers.push_back( inLayer );
|
2020-10-09 16:58:21 +00:00
|
|
|
wxString layerName = inLayer.Required ? WrapRequired( inLayer.Name ) : inLayer.Name;
|
|
|
|
m_unmatched_layer_names.push_back( layerName );
|
2020-10-01 22:47:38 +00:00
|
|
|
kiCadLayers |= inLayer.PermittedLayers;
|
|
|
|
}
|
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
int maxTextWidth = GetTextExtent( _( "Imported Layer" ) ).x;
|
2020-10-03 02:16:39 +00:00
|
|
|
|
|
|
|
for( const INPUT_LAYER_DESC& layer : m_input_layers )
|
|
|
|
maxTextWidth = std::max( maxTextWidth, GetTextExtent( layer.Name ).x );
|
2020-10-01 22:47:38 +00:00
|
|
|
|
|
|
|
// Initialize columns in the wxListCtrl elements:
|
|
|
|
wxListItem importedLayersHeader;
|
|
|
|
importedLayersHeader.SetId( 0 );
|
|
|
|
importedLayersHeader.SetText( _( "Imported Layer" ) );
|
2020-10-03 02:16:39 +00:00
|
|
|
importedLayersHeader.SetWidth( maxTextWidth + 15 );
|
2020-10-01 22:47:38 +00:00
|
|
|
m_unmatched_layers_list->InsertColumn( 0, importedLayersHeader );
|
|
|
|
|
2020-10-03 02:16:39 +00:00
|
|
|
int kicadMaxTextWidth = GetTextExtent( wxT( "User.Drawings" ) ).x;
|
|
|
|
|
2020-10-01 22:47:38 +00:00
|
|
|
wxListItem kicadLayersHeader;
|
|
|
|
kicadLayersHeader.SetId( 0 );
|
|
|
|
kicadLayersHeader.SetText( _( "KiCad Layer" ) );
|
2020-10-03 02:16:39 +00:00
|
|
|
kicadLayersHeader.SetWidth( kicadMaxTextWidth + 5 );
|
2020-10-01 22:47:38 +00:00
|
|
|
m_kicad_layers_list->InsertColumn( 0, kicadLayersHeader );
|
|
|
|
|
|
|
|
kicadLayersHeader.SetId( 1 );
|
2020-10-03 02:16:39 +00:00
|
|
|
importedLayersHeader.SetWidth( maxTextWidth + 15 );
|
|
|
|
kicadLayersHeader.SetWidth( kicadMaxTextWidth + 5 );
|
2020-10-01 22:47:38 +00:00
|
|
|
m_matched_layers_list->InsertColumn( 0, importedLayersHeader );
|
|
|
|
m_matched_layers_list->InsertColumn( 1, kicadLayersHeader );
|
|
|
|
|
|
|
|
// Load the input layer list to unmatched layers
|
|
|
|
int row = 0;
|
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
for( const wxString& importedLayerName : m_unmatched_layer_names )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
|
|
|
wxListItem item;
|
|
|
|
item.SetId( row );
|
|
|
|
item.SetText( importedLayerName );
|
|
|
|
m_unmatched_layers_list->InsertItem( item );
|
|
|
|
++row;
|
|
|
|
}
|
|
|
|
|
2020-10-16 16:07:15 +00:00
|
|
|
// Auto select the first item to improve ease-of-use
|
|
|
|
m_unmatched_layers_list->SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
|
|
|
|
|
2020-10-01 22:47:38 +00:00
|
|
|
// Load the KiCad Layer names
|
|
|
|
row = 0;
|
|
|
|
LSEQ kicadLayersSeq = kiCadLayers.Seq();
|
|
|
|
|
|
|
|
for( PCB_LAYER_ID layer : kicadLayersSeq )
|
|
|
|
{
|
|
|
|
wxString kiName = LayerName( layer );
|
|
|
|
|
|
|
|
wxListItem item;
|
|
|
|
item.SetId( row );
|
|
|
|
item.SetText( kiName );
|
|
|
|
m_kicad_layers_list->InsertItem( item );
|
|
|
|
++row;
|
|
|
|
}
|
|
|
|
|
2020-10-16 16:07:15 +00:00
|
|
|
// Auto select the first item to improve ease-of-use
|
|
|
|
m_kicad_layers_list->SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
|
|
|
|
|
2021-11-16 19:39:58 +00:00
|
|
|
SetupStandardButtons();
|
2020-10-01 22:47:38 +00:00
|
|
|
|
2020-10-03 02:16:39 +00:00
|
|
|
Fit();
|
2020-11-16 11:16:44 +00:00
|
|
|
finishDialogSettings();
|
2020-10-01 22:47:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
|
2024-06-25 08:10:18 +00:00
|
|
|
std::vector<wxString> DIALOG_MAP_LAYERS::GetUnmappedRequiredLayers() const
|
2020-10-09 16:58:21 +00:00
|
|
|
{
|
|
|
|
std::vector<wxString> unmappedLayers;
|
2021-07-27 12:22:27 +00:00
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
for( const wxString& layerName : m_unmatched_layer_names )
|
|
|
|
{
|
|
|
|
const INPUT_LAYER_DESC* layerDesc = GetLayerDescription( layerName );
|
2022-02-04 22:44:59 +00:00
|
|
|
wxASSERT_MSG( layerDesc != nullptr, wxT( "Expected to find layer description" ) );
|
2021-07-27 12:22:27 +00:00
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
if( layerDesc->Required )
|
|
|
|
unmappedLayers.push_back( layerDesc->Name );
|
|
|
|
}
|
2021-07-27 12:22:27 +00:00
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
return unmappedLayers;
|
|
|
|
}
|
|
|
|
|
2020-10-01 22:47:38 +00:00
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
std::map<wxString, PCB_LAYER_ID>
|
2024-06-25 08:10:18 +00:00
|
|
|
DIALOG_MAP_LAYERS::RunModal( wxWindow* aParent, const std::vector<INPUT_LAYER_DESC>& aLayerDesc )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
2024-06-25 08:10:18 +00:00
|
|
|
DIALOG_MAP_LAYERS dlg( aParent, aLayerDesc );
|
|
|
|
bool dataOk = false;
|
2020-10-01 22:47:38 +00:00
|
|
|
|
|
|
|
while( !dataOk )
|
|
|
|
{
|
|
|
|
dlg.ShowModal();
|
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
if( dlg.GetUnmappedRequiredLayers().size() > 0 )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
2020-10-09 16:58:21 +00:00
|
|
|
wxMessageBox( _( "All required layers (marked with '*') must be matched. "
|
|
|
|
"Please click on 'Auto-Match Layers' to "
|
|
|
|
"automatically match the remaining layers" ),
|
2021-07-27 12:22:27 +00:00
|
|
|
_( "Unmatched Layers" ), wxICON_ERROR | wxOK );
|
2020-10-01 22:47:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dataOk = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dlg.m_matched_layers_map;
|
|
|
|
}
|