2016-05-26 07:50:49 +00:00
|
|
|
/**
|
2018-01-29 12:26:58 +00:00
|
|
|
* @file gerber_file_image.cpp
|
2016-05-26 07:50:49 +00:00
|
|
|
* a GERBER class handle for a given layer info about used D_CODES and how the layer is drawn
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1992-2016 Jean-Pierre Charras jp.charras at wanadoo.fr
|
2021-07-16 20:13:26 +00:00
|
|
|
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-05-26 07:50:49 +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 2
|
|
|
|
* 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, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gerbview.h>
|
|
|
|
#include <gerbview_frame.h>
|
2018-01-29 12:26:58 +00:00
|
|
|
#include <gerber_file_image.h>
|
|
|
|
#include <gerber_file_image_list.h>
|
|
|
|
#include <X2_gerber_attributes.h>
|
2016-05-26 07:50:49 +00:00
|
|
|
|
2016-05-28 16:46:29 +00:00
|
|
|
#include <map>
|
|
|
|
|
2016-05-26 07:50:49 +00:00
|
|
|
|
2016-06-17 18:00:29 +00:00
|
|
|
// The global image list:
|
|
|
|
GERBER_FILE_IMAGE_LIST s_GERBER_List;
|
|
|
|
|
|
|
|
|
2019-12-30 13:23:33 +00:00
|
|
|
GERBER_FILE_IMAGE_LIST::GERBER_FILE_IMAGE_LIST()
|
2016-05-26 07:50:49 +00:00
|
|
|
{
|
|
|
|
m_GERBER_List.reserve( GERBER_DRAWLAYERS_COUNT );
|
|
|
|
|
|
|
|
for( unsigned layer = 0; layer < GERBER_DRAWLAYERS_COUNT; ++layer )
|
2019-12-30 13:23:33 +00:00
|
|
|
m_GERBER_List.push_back( nullptr );
|
2016-05-26 07:50:49 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 08:26:41 +00:00
|
|
|
|
2016-05-26 07:50:49 +00:00
|
|
|
GERBER_FILE_IMAGE_LIST::~GERBER_FILE_IMAGE_LIST()
|
|
|
|
{
|
2016-06-15 08:26:41 +00:00
|
|
|
DeleteAllImages();
|
2016-05-26 07:50:49 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 08:26:41 +00:00
|
|
|
|
2016-06-17 18:00:29 +00:00
|
|
|
GERBER_FILE_IMAGE_LIST& GERBER_FILE_IMAGE_LIST::GetImagesList()
|
|
|
|
{
|
|
|
|
return s_GERBER_List;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-26 07:50:49 +00:00
|
|
|
GERBER_FILE_IMAGE* GERBER_FILE_IMAGE_LIST::GetGbrImage( int aIdx )
|
|
|
|
{
|
|
|
|
if( (unsigned)aIdx < m_GERBER_List.size() )
|
|
|
|
return m_GERBER_List[aIdx];
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
return nullptr;
|
2016-05-26 07:50:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2016-05-26 07:50:49 +00:00
|
|
|
int GERBER_FILE_IMAGE_LIST::AddGbrImage( GERBER_FILE_IMAGE* aGbrImage, int aIdx )
|
|
|
|
{
|
|
|
|
int idx = aIdx;
|
|
|
|
|
|
|
|
if( idx < 0 )
|
|
|
|
{
|
|
|
|
for( idx = 0; idx < (int)m_GERBER_List.size(); idx++ )
|
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
if( m_GERBER_List[idx] == nullptr )
|
2016-05-26 07:50:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( idx >= (int)m_GERBER_List.size() )
|
|
|
|
return -1; // No room
|
|
|
|
|
|
|
|
m_GERBER_List[idx] = aGbrImage;
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-15 08:26:41 +00:00
|
|
|
void GERBER_FILE_IMAGE_LIST::DeleteAllImages()
|
2016-05-26 07:50:49 +00:00
|
|
|
{
|
2016-06-15 08:26:41 +00:00
|
|
|
for( unsigned idx = 0; idx < m_GERBER_List.size(); ++idx )
|
|
|
|
DeleteImage( idx );
|
2016-05-26 07:50:49 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 08:26:41 +00:00
|
|
|
|
|
|
|
void GERBER_FILE_IMAGE_LIST::DeleteImage( int aIdx )
|
2016-05-26 07:50:49 +00:00
|
|
|
{
|
2016-06-15 08:26:41 +00:00
|
|
|
// Ensure the index is valid:
|
|
|
|
if( aIdx < 0 || aIdx >= int( m_GERBER_List.size() ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// delete image aIdx
|
|
|
|
GERBER_FILE_IMAGE* gbr_image = GetGbrImage( aIdx );
|
|
|
|
|
|
|
|
delete gbr_image;
|
2019-03-24 16:32:57 +00:00
|
|
|
m_GERBER_List[ aIdx ] = nullptr;
|
2016-05-26 07:50:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2021-03-26 15:47:02 +00:00
|
|
|
const wxString GERBER_FILE_IMAGE_LIST::GetDisplayName( int aIdx, bool aNameOnly, bool aFullName )
|
2016-05-26 07:50:49 +00:00
|
|
|
{
|
|
|
|
wxString name;
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
GERBER_FILE_IMAGE* gerber = nullptr;
|
2016-05-26 07:50:49 +00:00
|
|
|
|
|
|
|
if( aIdx >= 0 && aIdx < (int)m_GERBER_List.size() )
|
|
|
|
gerber = m_GERBER_List[aIdx];
|
|
|
|
|
|
|
|
// if a file is loaded, build the name:
|
|
|
|
// <id> <short filename> <X2 FileFunction info> if a X2 FileFunction info is found
|
|
|
|
// or (if no FileFunction info)
|
|
|
|
// <id> <short filename> *
|
2016-06-15 08:26:41 +00:00
|
|
|
if( gerber )
|
2016-05-26 07:50:49 +00:00
|
|
|
{
|
|
|
|
wxFileName fn( gerber->m_FileName );
|
|
|
|
wxString filename = fn.GetFullName();
|
|
|
|
|
2021-03-26 15:47:02 +00:00
|
|
|
// If the filename is too long, display a shortened name if requested
|
2016-05-26 07:50:49 +00:00
|
|
|
const int maxlen = 30;
|
|
|
|
|
2021-03-26 15:47:02 +00:00
|
|
|
if( !aFullName && filename.Length() > maxlen )
|
2016-05-26 07:50:49 +00:00
|
|
|
{
|
|
|
|
wxString shortenedfn = filename.Left(2) + "..." + filename.Right(maxlen-5);
|
|
|
|
filename = shortenedfn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( gerber->m_FileFunction )
|
|
|
|
{
|
|
|
|
if( gerber->m_FileFunction->IsCopper() )
|
|
|
|
{
|
2016-09-07 08:28:16 +00:00
|
|
|
name.Printf( "%s (%s, %s, %s)",
|
2016-05-26 07:50:49 +00:00
|
|
|
filename.GetData(),
|
2020-10-16 00:39:55 +00:00
|
|
|
gerber->m_FileFunction->GetFileType(),
|
|
|
|
gerber->m_FileFunction->GetBrdLayerId(),
|
|
|
|
gerber->m_FileFunction->GetBrdLayerSide() );
|
2016-05-26 07:50:49 +00:00
|
|
|
}
|
2017-04-28 10:53:30 +00:00
|
|
|
if( gerber->m_FileFunction->IsDrillFile() )
|
|
|
|
{
|
|
|
|
name.Printf( "%s (%s,%s,%s,%s)",
|
|
|
|
filename.GetData(),
|
2020-10-16 00:39:55 +00:00
|
|
|
gerber->m_FileFunction->GetFileType(),
|
|
|
|
gerber->m_FileFunction->GetDrillLayerPair(),
|
|
|
|
gerber->m_FileFunction->GetLPType(),
|
|
|
|
gerber->m_FileFunction->GetRouteType() );
|
2017-04-28 10:53:30 +00:00
|
|
|
}
|
2016-05-26 07:50:49 +00:00
|
|
|
else
|
|
|
|
{
|
2016-09-07 08:28:16 +00:00
|
|
|
name.Printf( "%s (%s, %s)",
|
2016-05-26 07:50:49 +00:00
|
|
|
filename.GetData(),
|
2020-10-16 00:39:55 +00:00
|
|
|
gerber->m_FileFunction->GetFileType(),
|
|
|
|
gerber->m_FileFunction->GetBrdLayerId() );
|
2016-05-26 07:50:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2021-07-16 20:13:26 +00:00
|
|
|
{
|
2016-09-07 08:28:16 +00:00
|
|
|
name = filename;
|
2021-07-16 20:13:26 +00:00
|
|
|
}
|
2016-09-07 08:28:16 +00:00
|
|
|
|
|
|
|
if( aNameOnly )
|
|
|
|
return name;
|
|
|
|
|
|
|
|
wxString fullname;
|
|
|
|
|
|
|
|
fullname.Printf( "%d ", aIdx + 1 );
|
|
|
|
fullname << name;
|
|
|
|
return fullname;
|
2016-05-26 07:50:49 +00:00
|
|
|
}
|
|
|
|
else
|
2021-07-16 20:13:26 +00:00
|
|
|
{
|
2016-05-26 07:50:49 +00:00
|
|
|
name.Printf( _( "Graphic layer %d" ), aIdx + 1 );
|
2021-07-16 20:13:26 +00:00
|
|
|
}
|
2016-05-26 07:50:49 +00:00
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function, for std::sort.
|
|
|
|
// Sort loaded images by Z order priority, if they have the X2 FileFormat info
|
|
|
|
// returns true if the first argument (ref) is ordered before the second (test).
|
|
|
|
static bool sortZorder( const GERBER_FILE_IMAGE* const& ref, const GERBER_FILE_IMAGE* const& test )
|
|
|
|
{
|
|
|
|
if( !ref && !test )
|
|
|
|
return false; // do not change order: no criteria to sort items
|
|
|
|
|
|
|
|
if( !ref || !ref->m_InUse )
|
|
|
|
return false; // Not used: ref ordered after
|
|
|
|
|
|
|
|
if( !test || !test->m_InUse )
|
|
|
|
return true; // Not used: ref ordered before
|
|
|
|
|
|
|
|
if( !ref->m_FileFunction && !test->m_FileFunction )
|
|
|
|
return false; // do not change order: no criteria to sort items
|
|
|
|
|
|
|
|
if( !ref->m_FileFunction )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( !test->m_FileFunction )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( ref->m_FileFunction->GetZOrder() != test->m_FileFunction->GetZOrder() )
|
|
|
|
return ref->m_FileFunction->GetZOrder() > test->m_FileFunction->GetZOrder();
|
|
|
|
|
|
|
|
return ref->m_FileFunction->GetZSubOrder() > test->m_FileFunction->GetZSubOrder();
|
|
|
|
}
|
|
|
|
|
2018-02-27 02:15:32 +00:00
|
|
|
|
|
|
|
std::unordered_map<int, int> GERBER_FILE_IMAGE_LIST::SortImagesByZOrder()
|
2016-05-26 07:50:49 +00:00
|
|
|
{
|
|
|
|
std::sort( m_GERBER_List.begin(), m_GERBER_List.end(), sortZorder );
|
|
|
|
|
|
|
|
// The image order has changed.
|
|
|
|
// Graphic layer numbering must be updated to match the widgets layer order
|
|
|
|
|
|
|
|
// Store the old/new graphic layer info:
|
2018-02-27 02:15:32 +00:00
|
|
|
std::unordered_map<int, int> tab_lyr;
|
2016-05-26 07:50:49 +00:00
|
|
|
|
|
|
|
for( unsigned layer = 0; layer < m_GERBER_List.size(); ++layer )
|
|
|
|
{
|
2016-05-26 11:57:43 +00:00
|
|
|
GERBER_FILE_IMAGE* gerber = m_GERBER_List[layer];
|
2016-05-26 07:50:49 +00:00
|
|
|
|
2016-05-26 11:57:43 +00:00
|
|
|
if( !gerber )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tab_lyr[gerber->m_GraphicLayer] = layer;
|
|
|
|
gerber->m_GraphicLayer = layer ;
|
2016-05-26 07:50:49 +00:00
|
|
|
}
|
2018-02-27 02:15:32 +00:00
|
|
|
|
|
|
|
return tab_lyr;
|
2016-05-26 07:50:49 +00:00
|
|
|
}
|