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>
|
|
|
|
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <layers_id_colors_and_visibility.h>
|
|
|
|
#include <dialog_imported_layers.h>
|
|
|
|
|
2020-10-18 11:31:07 +00:00
|
|
|
#include <wx/msgdlg.h>
|
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
wxString DIALOG_IMPORTED_LAYERS::WrapRequired( const wxString& aLayerName )
|
|
|
|
{
|
|
|
|
return aLayerName + " *";
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString DIALOG_IMPORTED_LAYERS::UnwrapRequired( const wxString& aLayerName )
|
|
|
|
{
|
|
|
|
if( !aLayerName.EndsWith( " *" ) )
|
|
|
|
return aLayerName;
|
|
|
|
return aLayerName.Left( aLayerName.Length() - 2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
const INPUT_LAYER_DESC* DIALOG_IMPORTED_LAYERS::GetLayerDescription(
|
|
|
|
const wxString& aLayerName ) const
|
|
|
|
{
|
|
|
|
wxString layerName = UnwrapRequired( aLayerName );
|
|
|
|
for( const INPUT_LAYER_DESC& layerDescription : m_input_layers )
|
|
|
|
{
|
|
|
|
if( layerDescription.Name == layerName )
|
|
|
|
return &layerDescription;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-01 22:47:38 +00:00
|
|
|
|
|
|
|
PCB_LAYER_ID DIALOG_IMPORTED_LAYERS::GetSelectedLayerID()
|
|
|
|
{
|
|
|
|
// 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,
|
|
|
|
"There are more than one KiCad layer selected (unexpected)" );
|
2020-10-01 22:47:38 +00:00
|
|
|
|
|
|
|
for( LAYER_NUM layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
|
|
|
|
{
|
|
|
|
if( LayerName( ToLAYER_ID( layer ) ) == selectedKiCadLayerName )
|
|
|
|
return ToLAYER_ID( layer );
|
|
|
|
}
|
|
|
|
|
|
|
|
return PCB_LAYER_ID::UNDEFINED_LAYER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PCB_LAYER_ID DIALOG_IMPORTED_LAYERS::GetAutoMatchLayerID( wxString aInputLayerName )
|
|
|
|
{
|
2020-10-09 16:58:21 +00:00
|
|
|
wxString pureInputLayerName = UnwrapRequired( aInputLayerName );
|
2020-10-01 22:47:38 +00:00
|
|
|
for( INPUT_LAYER_DESC inputLayerDesc : m_input_layers )
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_IMPORTED_LAYERS::AddMappings()
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_IMPORTED_LAYERS::RemoveMappings( int aStatus )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_IMPORTED_LAYERS::DeleteListItems( const wxArrayInt& aRowsToDelete,
|
|
|
|
wxListCtrl* aListCtrl )
|
|
|
|
{
|
|
|
|
for( long n = ( aRowsToDelete.GetCount() - 1 ); 0 <= n; n-- )
|
|
|
|
{
|
|
|
|
aListCtrl->DeleteItem( aRowsToDelete[n] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_IMPORTED_LAYERS::OnAutoMatchLayersClicked( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
// 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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DIALOG_IMPORTED_LAYERS::DIALOG_IMPORTED_LAYERS( wxWindow* aParent,
|
2020-10-03 02:16:39 +00:00
|
|
|
const std::vector<INPUT_LAYER_DESC>& aLayerDesc ) :
|
|
|
|
DIALOG_IMPORTED_LAYERS_BASE( aParent )
|
2020-10-01 22:47:38 +00:00
|
|
|
{
|
|
|
|
LSET kiCadLayers;
|
|
|
|
|
|
|
|
// Read in the input layers
|
|
|
|
for( INPUT_LAYER_DESC inLayer : aLayerDesc )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
for( wxString importedLayerName : m_unmatched_layer_names )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
|
2020-10-01 22:47:38 +00:00
|
|
|
m_sdbSizerOK->SetDefault();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
std::vector<wxString> DIALOG_IMPORTED_LAYERS::GetUnmappedRequiredLayers() const
|
|
|
|
{
|
|
|
|
std::vector<wxString> unmappedLayers;
|
|
|
|
for( const wxString& layerName : m_unmatched_layer_names )
|
|
|
|
{
|
|
|
|
const INPUT_LAYER_DESC* layerDesc = GetLayerDescription( layerName );
|
2021-04-03 09:11:32 +00:00
|
|
|
wxASSERT_MSG( layerDesc != nullptr, "Expected to find layer description" );
|
2020-10-09 16:58:21 +00:00
|
|
|
if( layerDesc->Required )
|
|
|
|
unmappedLayers.push_back( layerDesc->Name );
|
|
|
|
}
|
|
|
|
return unmappedLayers;
|
|
|
|
}
|
|
|
|
|
2020-10-01 22:47:38 +00:00
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
std::map<wxString, PCB_LAYER_ID> DIALOG_IMPORTED_LAYERS::GetMapModal( wxWindow* aParent,
|
2020-10-01 22:47:38 +00:00
|
|
|
const std::vector<INPUT_LAYER_DESC>& aLayerDesc )
|
|
|
|
{
|
|
|
|
DIALOG_IMPORTED_LAYERS dlg( aParent, aLayerDesc );
|
|
|
|
bool dataOk = false;
|
|
|
|
|
|
|
|
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" ),
|
|
|
|
_( "Unmatched Layers" ), wxICON_ERROR | wxOK );
|
2020-10-01 22:47:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dataOk = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dlg.m_matched_layers_map;
|
|
|
|
}
|