1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
/**
* @file gerber_file_image.cpp
* 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
* Copyright The 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 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>
#include <gerber_file_image.h>
#include <gerber_file_image_list.h>
#include <X2_gerber_attributes.h>
#include <wx/filename.h>
#include <map>
// The global image list:
GERBER_FILE_IMAGE_LIST s_GERBER_List;
GERBER_FILE_IMAGE_LIST::GERBER_FILE_IMAGE_LIST()
{
m_GERBER_List.reserve( GERBER_DRAWLAYERS_COUNT );
for( unsigned layer = 0; layer < GERBER_DRAWLAYERS_COUNT; ++layer )
m_GERBER_List.push_back( nullptr );
}
GERBER_FILE_IMAGE_LIST::~GERBER_FILE_IMAGE_LIST()
{
DeleteAllImages();
}
GERBER_FILE_IMAGE_LIST& GERBER_FILE_IMAGE_LIST::GetImagesList()
{
return s_GERBER_List;
}
GERBER_FILE_IMAGE* GERBER_FILE_IMAGE_LIST::GetGbrImage( int aIdx )
{
if( (unsigned)aIdx < m_GERBER_List.size() )
return m_GERBER_List[aIdx];
return nullptr;
}
unsigned GERBER_FILE_IMAGE_LIST::GetLoadedImageCount()
{
auto notNull = []( GERBER_FILE_IMAGE* image )
{
return image != nullptr;
};
return std::count_if( m_GERBER_List.begin(), m_GERBER_List.end(), notNull );
}
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++ )
{
if( m_GERBER_List[idx] == nullptr )
break;
}
}
if( idx >= (int)m_GERBER_List.size() )
return -1; // No room
m_GERBER_List[idx] = aGbrImage;
return idx;
}
void GERBER_FILE_IMAGE_LIST::DeleteAllImages()
{
for( unsigned idx = 0; idx < m_GERBER_List.size(); ++idx )
DeleteImage( idx );
}
void GERBER_FILE_IMAGE_LIST::DeleteImage( unsigned int aIdx )
{
// Ensure the index is valid:
if( aIdx >= m_GERBER_List.size() )
return;
// delete image aIdx
GERBER_FILE_IMAGE* gbr_image = GetGbrImage( static_cast<int>( aIdx ) );
delete gbr_image;
m_GERBER_List[ aIdx ] = nullptr;
}
const wxString GERBER_FILE_IMAGE_LIST::GetDisplayName( int aIdx, bool aNameOnly, bool aFullName )
{
wxString name;
GERBER_FILE_IMAGE* gerber = nullptr;
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> *
if( gerber )
{
wxFileName fn( gerber->m_FileName );
wxString filename = fn.GetFullName();
// If the filename is too long, display a shortened name if requested
const int maxlen = 30;
if( !aFullName && filename.Length() > maxlen )
{
wxString shortenedfn = filename.Left(2) + wxT( "..." ) + filename.Right(maxlen-5);
filename = shortenedfn;
}
if( gerber->m_FileFunction )
{
if( gerber->m_FileFunction->IsCopper() )
{
name.Printf( wxT( "%s (%s, %s, %s)" ),
filename.GetData(),
gerber->m_FileFunction->GetFileType(),
gerber->m_FileFunction->GetBrdLayerId(),
gerber->m_FileFunction->GetBrdLayerSide() );
}
if( gerber->m_FileFunction->IsDrillFile() )
{
name.Printf( wxT( "%s (%s,%s,%s,%s)" ),
filename.GetData(),
gerber->m_FileFunction->GetFileType(),
gerber->m_FileFunction->GetDrillLayerPair(),
gerber->m_FileFunction->GetLPType(),
gerber->m_FileFunction->GetRouteType() );
}
else
{
name.Printf( wxT( "%s (%s, %s)" ),
filename.GetData(),
gerber->m_FileFunction->GetFileType(),
gerber->m_FileFunction->GetBrdLayerId() );
}
}
else
{
name = filename;
}
if( aNameOnly )
return name;
wxString fullname;
fullname.Printf( wxT( "%d " ), aIdx + 1 );
fullname << name;
return fullname;
}
else
{
name.Printf( _( "Graphic layer %d" ), aIdx + 1 );
}
return name;
}
struct GERBER_ORDER
{
std::string m_FilenameMask;
GERBER_ORDER_ENUM m_Order;
};
// clang-format off
static struct GERBER_ORDER gerberFileExtensionOrder[] =
{
{ ".GM1", GERBER_ORDER_ENUM::GERBER_BOARD_OUTLINE },
{ ".GM3", GERBER_ORDER_ENUM::GERBER_BOARD_OUTLINE },
{ ".GBR", GERBER_ORDER_ENUM::GERBER_BOARD_OUTLINE },
{ ".DIM", GERBER_ORDER_ENUM::GERBER_BOARD_OUTLINE },
{ ".MIL", GERBER_ORDER_ENUM::GERBER_BOARD_OUTLINE },
{ ".GML", GERBER_ORDER_ENUM::GERBER_BOARD_OUTLINE },
{ "EDGE.CUTS", GERBER_ORDER_ENUM::GERBER_BOARD_OUTLINE },
{ ".FAB", GERBER_ORDER_ENUM::GERBER_BOARD_OUTLINE },
{ ".GKO", GERBER_ORDER_ENUM::GERBER_KEEP_OUT },
{ ".GM?", GERBER_ORDER_ENUM::GERBER_MECHANICAL },
{ ".GM??", GERBER_ORDER_ENUM::GERBER_MECHANICAL },
{ ".TXT", GERBER_ORDER_ENUM::GERBER_DRILL },
{ ".XLN", GERBER_ORDER_ENUM::GERBER_DRILL },
{ ".TAP", GERBER_ORDER_ENUM::GERBER_DRILL },
{ ".DRD", GERBER_ORDER_ENUM::GERBER_DRILL },
{ ".DRL", GERBER_ORDER_ENUM::GERBER_DRILL },
{ ".NC", GERBER_ORDER_ENUM::GERBER_DRILL },
{ ".XNC", GERBER_ORDER_ENUM::GERBER_DRILL },
{ ".GTP", GERBER_ORDER_ENUM::GERBER_TOP_PASTE },
{ ".CRC", GERBER_ORDER_ENUM::GERBER_TOP_PASTE },
{ ".TSP", GERBER_ORDER_ENUM::GERBER_TOP_PASTE },
{ "F.PASTE", GERBER_ORDER_ENUM::GERBER_TOP_PASTE },
{ ".SPT", GERBER_ORDER_ENUM::GERBER_TOP_PASTE },
{ "PT.PHO", GERBER_ORDER_ENUM::GERBER_TOP_PASTE },
{ ".GTO", GERBER_ORDER_ENUM::GERBER_TOP_SILK_SCREEN },
{ ".PLC", GERBER_ORDER_ENUM::GERBER_TOP_SILK_SCREEN },
{ ".TSK", GERBER_ORDER_ENUM::GERBER_TOP_SILK_SCREEN },
{ "F.SILKS", GERBER_ORDER_ENUM::GERBER_TOP_SILK_SCREEN },
{ ".SST", GERBER_ORDER_ENUM::GERBER_TOP_SILK_SCREEN },
{ "ST.PHO", GERBER_ORDER_ENUM::GERBER_TOP_SILK_SCREEN },
{ ".GTS", GERBER_ORDER_ENUM::GERBER_TOP_SOLDER_MASK },
{ ".STC", GERBER_ORDER_ENUM::GERBER_TOP_SOLDER_MASK },
{ ".TSM", GERBER_ORDER_ENUM::GERBER_TOP_SOLDER_MASK },
{ "F.MASK", GERBER_ORDER_ENUM::GERBER_TOP_SOLDER_MASK },
{ ".SMT", GERBER_ORDER_ENUM::GERBER_TOP_SOLDER_MASK },
{ "MT.PHO", GERBER_ORDER_ENUM::GERBER_TOP_SOLDER_MASK },
{ ".GTL", GERBER_ORDER_ENUM::GERBER_TOP_COPPER },
{ ".CMP", GERBER_ORDER_ENUM::GERBER_TOP_COPPER },
{ ".TOP", GERBER_ORDER_ENUM::GERBER_TOP_COPPER },
{ "F.CU", GERBER_ORDER_ENUM::GERBER_TOP_COPPER },
{ "L1.PHO", GERBER_ORDER_ENUM::GERBER_TOP_COPPER },
{ ".PHD", GERBER_ORDER_ENUM::GERBER_TOP_COPPER },
{ ".ART", GERBER_ORDER_ENUM::GERBER_TOP_COPPER },
{ ".GBL", GERBER_ORDER_ENUM::GERBER_BOTTOM_COPPER },
{ ".SOL", GERBER_ORDER_ENUM::GERBER_BOTTOM_COPPER },
{ ".BOT", GERBER_ORDER_ENUM::GERBER_BOTTOM_COPPER },
{ "B.CU", GERBER_ORDER_ENUM::GERBER_BOTTOM_COPPER },
{ ".BOT", GERBER_ORDER_ENUM::GERBER_BOTTOM_COPPER },
{ ".GBS", GERBER_ORDER_ENUM::GERBER_BOTTOM_SOLDER_MASK },
{ ".STS", GERBER_ORDER_ENUM::GERBER_BOTTOM_SOLDER_MASK },
{ ".BSM", GERBER_ORDER_ENUM::GERBER_BOTTOM_SOLDER_MASK },
{ "B.MASK", GERBER_ORDER_ENUM::GERBER_BOTTOM_SOLDER_MASK },
{ ".SMB", GERBER_ORDER_ENUM::GERBER_BOTTOM_SOLDER_MASK },
{ "MB.PHO", GERBER_ORDER_ENUM::GERBER_BOTTOM_SOLDER_MASK },
{ ".GBO", GERBER_ORDER_ENUM::GERBER_BOTTOM_SILK_SCREEN },
{ ".PLS", GERBER_ORDER_ENUM::GERBER_BOTTOM_SILK_SCREEN },
{ ".BSK", GERBER_ORDER_ENUM::GERBER_BOTTOM_SILK_SCREEN },
{ "B.SILK", GERBER_ORDER_ENUM::GERBER_BOTTOM_SILK_SCREEN },
{ ".SSB", GERBER_ORDER_ENUM::GERBER_BOTTOM_SILK_SCREEN },
{ "SB.PHO", GERBER_ORDER_ENUM::GERBER_BOTTOM_SILK_SCREEN },
{ ".GBP", GERBER_ORDER_ENUM::GERBER_BOTTOM_PASTE },
{ ".CRS", GERBER_ORDER_ENUM::GERBER_BOTTOM_PASTE },
{ ".BSP", GERBER_ORDER_ENUM::GERBER_BOTTOM_PASTE },
{ "B.PASTE", GERBER_ORDER_ENUM::GERBER_BOTTOM_PASTE },
{ ".SMB", GERBER_ORDER_ENUM::GERBER_BOTTOM_PASTE },
{ "MB.PHO", GERBER_ORDER_ENUM::GERBER_BOTTOM_PASTE },
// EAGLE CAD file to explicitly ignore that can match some other
// layers otherwise
{ ".GPI", GERBER_ORDER_ENUM::GERBER_LAYER_UNKNOWN },
// Inner copper layers need to come last so the wildcard
// number matching doesn't pick up other specific layer names.
{ ".GI?", GERBER_ORDER_ENUM::GERBER_INNER },
{ ".GI??", GERBER_ORDER_ENUM::GERBER_INNER },
{ ".G?", GERBER_ORDER_ENUM::GERBER_INNER },
{ ".G??", GERBER_ORDER_ENUM::GERBER_INNER },
{ ".G?L", GERBER_ORDER_ENUM::GERBER_INNER },
{ ".G??L", GERBER_ORDER_ENUM::GERBER_INNER },
};
// clang-format on
void GERBER_FILE_IMAGE_LIST::GetGerberLayerFromFilename( const wxString& filename,
enum GERBER_ORDER_ENUM& order,
wxString& matchedExtension )
{
order = GERBER_ORDER_ENUM::GERBER_LAYER_UNKNOWN;
matchedExtension = "";
for( struct GERBER_ORDER o : gerberFileExtensionOrder )
{
wxString ext = filename.Right( o.m_FilenameMask.length() ).Upper();
if( ext.Matches( o.m_FilenameMask ) )
{
order = o.m_Order;
matchedExtension = ext;
return;
}
}
}
static bool sortFileExtension( const GERBER_FILE_IMAGE* const& ref,
const GERBER_FILE_IMAGE* const& test )
{
// Do not change order: no criteria to sort items
if( !ref && !test )
return false;
// Not used: ref ordered after
if( !ref || !ref->m_InUse )
return false;
// Not used: ref ordered before
if( !test || !test->m_InUse )
return true;
enum GERBER_ORDER_ENUM ref_layer;
enum GERBER_ORDER_ENUM test_layer;
wxString ref_extension;
wxString test_extension;
GERBER_FILE_IMAGE_LIST::GetGerberLayerFromFilename( ref->m_FileName, ref_layer,
ref_extension );
GERBER_FILE_IMAGE_LIST::GetGerberLayerFromFilename( test->m_FileName, test_layer,
test_extension );
// Inner layers have a numeric code that we can compare against
if( ref_layer == GERBER_ORDER_ENUM::GERBER_INNER
&& test_layer == GERBER_ORDER_ENUM::GERBER_INNER )
{
unsigned long ref_layer_number = 0;
unsigned long test_layer_number = 0;
// Strip extensions down to only the numbers in it. Later conversion to int will
// automatically skip the spaces
for( wxString::iterator it = ref_extension.begin(); it != ref_extension.end(); ++it )
{
if( !isdigit( *it ) )
*it = ' ';
}
for( wxString::iterator it = test_extension.begin(); it != test_extension.end(); ++it )
{
if( !isdigit( *it ) )
*it = ' ';
}
ref_extension.ToULong( &ref_layer_number );
test_extension.ToULong( &test_layer_number );
return ref_layer_number < test_layer_number;
}
return (int) ref_layer < (int) test_layer;
}
// 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();
}
std::unordered_map<int, int> GERBER_FILE_IMAGE_LIST::GetLayerRemap()
{
// The image order has changed.
// Graphic layer numbering must be updated to match the widgets layer order
// Store the old/new graphic layer info:
std::unordered_map<int, int> tab_lyr;
for( unsigned layer = 0; layer < m_GERBER_List.size(); ++layer )
{
GERBER_FILE_IMAGE* gerber = m_GERBER_List[layer];
if( !gerber )
continue;
tab_lyr[gerber->m_GraphicLayer] = layer;
gerber->m_GraphicLayer = layer ;
}
return tab_lyr;
}
std::unordered_map<int, int>
GERBER_FILE_IMAGE_LIST::SortImagesByFunction( LayerSortFunction sortFunction )
{
std::sort( m_GERBER_List.begin(), m_GERBER_List.end(), sortFunction );
return GetLayerRemap();
}
std::unordered_map<int, int> GERBER_FILE_IMAGE_LIST::SortImagesByFileExtension()
{
return SortImagesByFunction( sortFileExtension );
}
std::unordered_map<int, int> GERBER_FILE_IMAGE_LIST::SortImagesByZOrder()
{
return SortImagesByFunction( sortZorder );
}
std::unordered_map<int, int> GERBER_FILE_IMAGE_LIST::SwapImages( unsigned int layer1,
unsigned int layer2 )
{
if( ( layer1 >= m_GERBER_List.size() ) || ( layer2 >= m_GERBER_List.size() ) )
return std::unordered_map<int, int>();
std::swap( m_GERBER_List[layer1], m_GERBER_List[layer2] );
return GetLayerRemap();
}
std::unordered_map<int, int> GERBER_FILE_IMAGE_LIST::RemoveImage( unsigned int layer )
{
if( layer >= m_GERBER_List.size() )
return std::unordered_map<int, int>();
DeleteImage( layer );
// Move deleted image to end of list, move all other images up
std::rotate( m_GERBER_List.begin() + layer, m_GERBER_List.begin() + layer + 1,
m_GERBER_List.end() );
return GetLayerRemap();
}
|