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
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* 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 <pcb_plotter.h>
#include <plotters/plotter.h>
#include <plotters/plotters_pslike.h>
#include <board.h>
#include <reporter.h>
#include <pcbplot.h>
#include <wx/filename.h>
#include <gerber_jobfile_writer.h>
#include <jobs/job_export_pcb_gerbers.h>
#include <jobs/job_export_pcb_dxf.h>
#include <jobs/job_export_pcb_pdf.h>
#include <jobs/job_export_pcb_plot.h>
#include <jobs/job_export_pcb_svg.h>
#include <pgm_base.h>
#include <pcbnew_settings.h>
PCB_PLOTTER::PCB_PLOTTER( BOARD* aBoard, REPORTER* aReporter, PCB_PLOT_PARAMS& aParams ) :
m_board( aBoard ),
m_plotOpts( aParams ),
m_reporter( aReporter )
{
}
bool PCB_PLOTTER::Plot( const wxString& aOutputPath,
const LSEQ& aLayersToPlot,
const LSEQ& aCommonLayers,
bool aUseGerberFileExtensions,
bool aOutputPathIsSingle,
std::optional<wxString> aLayerName,
std::optional<wxString> aSheetName,
std::optional<wxString> aSheetPath )
{
std::function<bool( wxString* )> textResolver = [&]( wxString* token ) -> bool
{
// Handles board->GetTitleBlock() *and* board->GetProject()
return m_board->ResolveTextVar( token, 0 );
};
// sanity, ensure one layer to print
if( aLayersToPlot.size() < 1 )
{
m_reporter->Report( _( "No layers selected for plotting." ), RPT_SEVERITY_ERROR );
return false;
}
PAGE_INFO existingPageInfo = m_board->GetPageSettings();
VECTOR2I existingAuxOrigin = m_board->GetDesignSettings().GetAuxOrigin();
if( m_plotOpts.GetFormat() == PLOT_FORMAT::SVG && m_plotOpts.GetSvgFitPagetoBoard() ) // Page is board boundary size
{
BOX2I bbox = m_board->ComputeBoundingBox( false );
PAGE_INFO currPageInfo = m_board->GetPageSettings();
currPageInfo.SetWidthMils( bbox.GetWidth() / pcbIUScale.IU_PER_MILS );
currPageInfo.SetHeightMils( bbox.GetHeight() / pcbIUScale.IU_PER_MILS );
m_board->SetPageSettings( currPageInfo );
m_plotOpts.SetUseAuxOrigin( true );
VECTOR2I origin = bbox.GetOrigin();
m_board->GetDesignSettings().SetAuxOrigin( origin );
}
// To reuse logic, in single plot mode, we want to kick any extra layers from the main list to commonLayers
LSEQ layersToPlot;
LSEQ commonLayers;
if( aOutputPathIsSingle )
{
layersToPlot.push_back( aLayersToPlot[0] );
if( aLayersToPlot.size() > 1 )
{
commonLayers.insert( commonLayers.end(), aLayersToPlot.begin() + 1,
aLayersToPlot.end() );
}
}
else
{
layersToPlot = aLayersToPlot;
commonLayers = aCommonLayers;
}
int finalPageCount = 0;
for( PCB_LAYER_ID layer : layersToPlot )
{
if( copperLayerShouldBeSkipped( layer ) )
continue;
finalPageCount++;
}
std::unique_ptr<GERBER_JOBFILE_WRITER> jobfile_writer;
if( m_plotOpts.GetFormat() == PLOT_FORMAT::GERBER && !aOutputPathIsSingle )
jobfile_writer = std::make_unique<GERBER_JOBFILE_WRITER>( m_board, m_reporter );
wxString fileExt( GetDefaultPlotExtension( m_plotOpts.GetFormat() ) );
wxString sheetPath;
wxString msg;
bool success = true;
PLOTTER* plotter = nullptr;
int pageNum = 1;
for( size_t i = 0; i < layersToPlot.size(); i++ )
{
PCB_LAYER_ID layer = layersToPlot[i];
if( copperLayerShouldBeSkipped( layer ) )
continue;
LSEQ plotSequence = getPlotSequence( layer, commonLayers );
wxString layerName = m_board->GetLayerName( layer );
wxFileName fn;
if( aOutputPathIsSingle )
{
fn = wxFileName( aOutputPath );
}
else
{
wxFileName brdFn = m_board->GetFileName();
fn.Assign( aOutputPath, brdFn.GetName(), fileExt );
// Use Gerber Extensions based on layer number
// (See http://en.wikipedia.org/wiki/Gerber_File)
if( m_plotOpts.GetFormat() == PLOT_FORMAT::GERBER && aUseGerberFileExtensions )
fileExt = GetGerberProtelExtension( layer );
if( m_plotOpts.GetFormat() == PLOT_FORMAT::PDF && m_plotOpts.m_PDFSingle )
fn.SetExt( GetDefaultPlotExtension( PLOT_FORMAT::PDF ) );
else
BuildPlotFileName( &fn, aOutputPath, layerName, fileExt );
}
if( jobfile_writer )
{
wxString fullname = fn.GetFullName();
jobfile_writer->AddGbrFile( layer, fullname );
}
if( m_plotOpts.GetFormat() != PLOT_FORMAT::PDF
|| !m_plotOpts.m_PDFSingle
|| ( pageNum == 1 && m_plotOpts.GetFormat() == PLOT_FORMAT::PDF
&& m_plotOpts.m_PDFSingle ) )
{
// this will only be used by pdf
wxString pageNumber = wxString::Format( "%d", pageNum );
wxString pageName = layerName;
wxString sheetName = layerName;
if( aLayerName.has_value() )
{
layerName = aLayerName.value();
pageName = aLayerName.value();
}
if( aSheetName.has_value() )
sheetName = aSheetName.value();
if( aSheetPath.has_value() )
sheetPath = aSheetPath.value();
plotter = StartPlotBoard( m_board, &m_plotOpts, layer, layerName, fn.GetFullPath(),
sheetName, sheetPath, pageName, pageNumber, finalPageCount );
}
if( plotter )
{
plotter->SetTitle( ExpandTextVars( m_board->GetTitleBlock().GetTitle(), &textResolver ) );
if( m_plotOpts.m_PDFMetadata )
{
msg = wxS( "AUTHOR" );
if( m_board->ResolveTextVar( &msg, 0 ) )
plotter->SetAuthor( msg );
msg = wxS( "SUBJECT" );
if( m_board->ResolveTextVar( &msg, 0 ) )
plotter->SetSubject( msg );
}
PlotBoardLayers( m_board, plotter, plotSequence, m_plotOpts );
PlotInteractiveLayer( m_board, plotter, m_plotOpts );
if( m_plotOpts.GetFormat() == PLOT_FORMAT::PDF && m_plotOpts.m_PDFSingle
&& i != layersToPlot.size() - 1 )
{
wxString pageNumber = wxString::Format( "%d", pageNum + 1 );
size_t nextI = i + 1;
PCB_LAYER_ID nextLayer = layersToPlot[nextI];
while( copperLayerShouldBeSkipped( nextLayer ) && nextI < layersToPlot.size() - 1 )
{
++nextI;
nextLayer = layersToPlot[nextI];
}
layerName = m_board->GetLayerName( nextLayer );
wxString pageName = layerName;
wxString sheetName = layerName;
static_cast<PDF_PLOTTER*>( plotter )->ClosePage();
static_cast<PDF_PLOTTER*>( plotter )->StartPage( pageNumber, pageName );
setupPlotterNewPDFPage( plotter, m_board, &m_plotOpts, layerName, sheetName,
sheetPath, pageNumber, finalPageCount );
}
// last page
if( m_plotOpts.GetFormat() != PLOT_FORMAT::PDF
|| !m_plotOpts.m_PDFSingle
|| i == aLayersToPlot.size() - 1
|| pageNum == finalPageCount )
{
plotter->EndPlot();
delete plotter->RenderSettings();
delete plotter;
plotter = nullptr;
msg.Printf( _( "Plotted to '%s'." ), fn.GetFullPath() );
m_reporter->Report( msg, RPT_SEVERITY_ACTION );
}
}
else
{
msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
m_reporter->Report( msg, RPT_SEVERITY_ERROR );
success = false;
}
pageNum++;
wxSafeYield(); // displays report message.
}
if( jobfile_writer && m_plotOpts.GetCreateGerberJobFile() )
{
// Pick the basename from the board file
wxFileName fn( m_board->GetFileName() );
// Build gerber job file from basename
BuildPlotFileName( &fn, aOutputPath, wxT( "job" ), FILEEXT::GerberJobFileExtension );
jobfile_writer->CreateJobFile( fn.GetFullPath() );
}
m_reporter->ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
if( m_plotOpts.GetFormat() == PLOT_FORMAT::SVG && m_plotOpts.GetSvgFitPagetoBoard() )
{
// restore the original page and aux origin
m_board->SetPageSettings( existingPageInfo );
m_board->GetDesignSettings().SetAuxOrigin( existingAuxOrigin );
}
return success;
}
bool PCB_PLOTTER::copperLayerShouldBeSkipped( PCB_LAYER_ID aLayerToPlot )
{
return ( LSET::AllCuMask() & ~m_board->GetEnabledLayers() )[aLayerToPlot];
}
void PCB_PLOTTER::BuildPlotFileName( wxFileName* aFilename, const wxString& aOutputDir,
const wxString& aSuffix, const wxString& aExtension )
{
// aFilename contains the base filename only (without path and extension)
// when calling this function.
// It is expected to be a valid filename (this is usually the board filename)
aFilename->SetPath( aOutputDir );
// Set the file extension
aFilename->SetExt( aExtension );
// remove leading and trailing spaces if any from the suffix, if
// something survives add it to the name;
// also the suffix can contain some not allowed chars in filename (/ \ . : and some others),
// so change them to underscore
// Remember it can be called from a python script, so the illegal chars
// have to be filtered here.
wxString suffix = aSuffix;
suffix.Trim( true );
suffix.Trim( false );
wxString badchars = wxFileName::GetForbiddenChars( wxPATH_DOS );
badchars.Append( "%." );
for( unsigned ii = 0; ii < badchars.Len(); ii++ )
suffix.Replace( badchars[ii], wxT( "_" ) );
if( !suffix.IsEmpty() )
aFilename->SetName( aFilename->GetName() + wxT( "-" ) + suffix );
}
LSEQ PCB_PLOTTER::getPlotSequence( PCB_LAYER_ID aLayerToPlot, LSEQ aPlotWithAllLayersSeq )
{
LSEQ plotSequence;
// Base layer always gets plotted first.
plotSequence.push_back( aLayerToPlot );
for( PCB_LAYER_ID layer : aPlotWithAllLayersSeq )
{
// Don't plot the same layer more than once;
if( find( plotSequence.begin(), plotSequence.end(), layer ) != plotSequence.end() )
continue;
plotSequence.push_back( layer );
}
return plotSequence;
}
void PCB_PLOTTER::PlotJobToPlotOpts( PCB_PLOT_PARAMS& aOpts, JOB_EXPORT_PCB_PLOT* aJob,
REPORTER& aReporter )
{
if( aJob->m_plotFormat == JOB_EXPORT_PCB_PLOT::PLOT_FORMAT::GERBER )
{
JOB_EXPORT_PCB_GERBERS* gJob = static_cast<JOB_EXPORT_PCB_GERBERS*>( aJob );
aOpts.SetDisableGerberMacros( gJob->m_disableApertureMacros );
aOpts.SetUseGerberProtelExtensions( gJob->m_useProtelFileExtension );
aOpts.SetUseGerberX2format( gJob->m_useX2Format );
aOpts.SetIncludeGerberNetlistInfo( gJob->m_includeNetlistAttributes );
aOpts.SetCreateGerberJobFile( gJob->m_createJobsFile );
aOpts.SetGerberPrecision( gJob->m_precision );
}
if( aJob->m_plotFormat == JOB_EXPORT_PCB_PLOT::PLOT_FORMAT::SVG )
{
JOB_EXPORT_PCB_SVG* svgJob = static_cast<JOB_EXPORT_PCB_SVG*>( aJob );
aOpts.SetSvgPrecision( svgJob->m_precision );
aOpts.SetSvgFitPageToBoard( svgJob->m_fitPageToBoard );
}
if( aJob->m_plotFormat == JOB_EXPORT_PCB_PLOT::PLOT_FORMAT::DXF )
{
JOB_EXPORT_PCB_DXF* dxfJob = static_cast<JOB_EXPORT_PCB_DXF*>( aJob );
aOpts.SetDXFPlotUnits( dxfJob->m_dxfUnits == JOB_EXPORT_PCB_DXF::DXF_UNITS::INCH
? DXF_UNITS::INCH
: DXF_UNITS::MM );
aOpts.SetPlotMode( dxfJob->m_plotGraphicItemsUsingContours ? OUTLINE_MODE::SKETCH
: OUTLINE_MODE::FILLED );
aOpts.SetDXFPlotPolygonMode( dxfJob->m_polygonMode );
}
if( aJob->m_plotFormat == JOB_EXPORT_PCB_PLOT::PLOT_FORMAT::PDF )
{
JOB_EXPORT_PCB_PDF* pdfJob = static_cast<JOB_EXPORT_PCB_PDF*>( aJob );
aOpts.m_PDFFrontFPPropertyPopups = pdfJob->m_pdfFrontFPPropertyPopups;
aOpts.m_PDFBackFPPropertyPopups = pdfJob->m_pdfBackFPPropertyPopups;
aOpts.m_PDFMetadata = pdfJob->m_pdfMetadata;
aOpts.m_PDFSingle = pdfJob->m_pdfSingle;
}
aOpts.SetUseAuxOrigin( aJob->m_useDrillOrigin );
aOpts.SetPlotFrameRef( aJob->m_plotDrawingSheet );
aOpts.SetSubtractMaskFromSilk( aJob->m_subtractSolderMaskFromSilk );
aOpts.SetPlotReference( aJob->m_plotRefDes );
aOpts.SetPlotValue( aJob->m_plotFootprintValues );
aOpts.SetSketchPadsOnFabLayers( aJob->m_sketchPadsOnFabLayers );
aOpts.SetHideDNPFPsOnFabLayers( aJob->m_hideDNPFPsOnFabLayers );
aOpts.SetSketchDNPFPsOnFabLayers( aJob->m_sketchDNPFPsOnFabLayers );
aOpts.SetCrossoutDNPFPsOnFabLayers( aJob->m_crossoutDNPFPsOnFabLayers );
aOpts.SetPlotPadNumbers( aJob->m_plotPadNumbers );
aOpts.SetBlackAndWhite( aJob->m_blackAndWhite );
aOpts.SetMirror( aJob->m_mirror );
aOpts.SetNegative( aJob->m_negative );
aOpts.SetLayerSelection( aJob->m_plotLayerSequence );
aOpts.SetPlotOnAllLayersSequence( aJob->m_plotOnAllLayersSequence );
switch( aJob->m_plotFormat )
{
default:
case JOB_EXPORT_PCB_PLOT::PLOT_FORMAT::GERBER: aOpts.SetFormat( PLOT_FORMAT::GERBER ); break;
case JOB_EXPORT_PCB_PLOT::PLOT_FORMAT::POST: aOpts.SetFormat( PLOT_FORMAT::POST ); break;
case JOB_EXPORT_PCB_PLOT::PLOT_FORMAT::SVG: aOpts.SetFormat( PLOT_FORMAT::SVG ); break;
case JOB_EXPORT_PCB_PLOT::PLOT_FORMAT::DXF: aOpts.SetFormat( PLOT_FORMAT::DXF ); break;
case JOB_EXPORT_PCB_PLOT::PLOT_FORMAT::HPGL: aOpts.SetFormat( PLOT_FORMAT::HPGL ); break;
case JOB_EXPORT_PCB_PLOT::PLOT_FORMAT::PDF: aOpts.SetFormat( PLOT_FORMAT::PDF ); break;
}
switch( aJob->m_drillShapeOption )
{
case DRILL_MARKS::NO_DRILL_SHAPE: aOpts.SetDrillMarksType( DRILL_MARKS::NO_DRILL_SHAPE ); break;
case DRILL_MARKS::SMALL_DRILL_SHAPE: aOpts.SetDrillMarksType( DRILL_MARKS::SMALL_DRILL_SHAPE ); break;
default:
case DRILL_MARKS::FULL_DRILL_SHAPE: aOpts.SetDrillMarksType( DRILL_MARKS::FULL_DRILL_SHAPE ); break;
}
SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
wxString theme = aJob->m_colorTheme;
// Theme may be empty when running from a job in GUI context, so use the GUI settings.
if( theme.IsEmpty() )
{
PCBNEW_SETTINGS* pcbSettings = mgr.GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
theme = pcbSettings->m_ColorTheme;
}
COLOR_SETTINGS* colors = mgr.GetColorSettings( aJob->m_colorTheme );
if( colors->GetFilename() != theme && !aOpts.GetBlackAndWhite() )
{
aReporter.Report( wxString::Format( _( "Color theme '%s' not found, will use theme from "
"PCB Editor settings.\n" ),
theme ),
RPT_SEVERITY_WARNING );
}
aOpts.SetColorSettings( colors );
aOpts.SetOutputDirectory( aJob->GetConfiguredOutputPath() );
}
|