KiCad PCB EDA Suite
GERBER_plotter.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <fctsys.h>
31 #include <gr_basic.h>
32 #include <trigo.h>
33 #include <eda_base_frame.h>
34 #include <base_struct.h>
35 #include <common.h>
36 #include <plotter.h>
37 #include <macros.h>
38 #include <kicad_string.h>
40 
41 #include <build_version.h>
42 
43 #include <gbr_metadata.h>
44 
45 
47 {
48  workFile = NULL;
49  finalFile = NULL;
50  currentAperture = apertures.end();
52 
53  // number of digits after the point (number of digits of the mantissa
54  // Be carefull: the Gerber coordinates are stored in an integer
55  // so 6 digits (inches) or 5 digits (mm) is a good value
56  // To avoid overflow, 7 digits (inches) or 6 digits is a max.
57  // with lower values than 6 digits (inches) or 5 digits (mm),
58  // Creating self-intersecting polygons from non-intersecting polygons
59  // happen easily.
60  m_gerberUnitInch = false;
61  m_gerberUnitFmt = 6;
62  m_useX2format = true;
63  m_useNetAttributes = true;
64 }
65 
66 
67 void GERBER_PLOTTER::SetViewport( const wxPoint& aOffset, double aIusPerDecimil,
68  double aScale, bool aMirror )
69 {
70  wxASSERT( aMirror == false );
71  m_plotMirror = false;
72  plotOffset = aOffset;
73  wxASSERT( aScale == 1 ); // aScale parameter is not used in Gerber
74  plotScale = 1; // Plot scale is *always* 1.0
75 
76  m_IUsPerDecimil = aIusPerDecimil;
77  // gives now a default value to iuPerDeviceUnit (because the units of the caller is now known)
78  // which could be modified later by calling SetGerberCoordinatesFormat()
79  iuPerDeviceUnit = pow( 10.0, m_gerberUnitFmt ) / ( m_IUsPerDecimil * 10000.0 );
80 
81  // We don't handle the filmbox, and it's more useful to keep the
82  // origin at the origin
83  paperSize.x = 0;
84  paperSize.y = 0;
85  SetDefaultLineWidth( 100 * aIusPerDecimil ); // Arbitrary default
86 }
87 
88 
89 void GERBER_PLOTTER::SetGerberCoordinatesFormat( int aResolution, bool aUseInches )
90 {
91  m_gerberUnitInch = aUseInches;
92  m_gerberUnitFmt = aResolution;
93 
94  iuPerDeviceUnit = pow( 10.0, m_gerberUnitFmt ) / ( m_IUsPerDecimil * 10000.0 );
95 
96  if( ! m_gerberUnitInch )
97  iuPerDeviceUnit *= 25.4; // gerber output in mm
98 }
99 
100 
101 void GERBER_PLOTTER::emitDcode( const DPOINT& pt, int dcode )
102 {
103 
104  fprintf( outputFile, "X%dY%dD%02d*\n",
105  KiROUND( pt.x ), KiROUND( pt.y ), dcode );
106 }
107 
108 
110 {
111  // disable a Gerber net attribute (exists only in X2 with net attributes mode).
112  if( m_objectAttributesDictionnary.empty() ) // No net attribute or not X2 mode
113  return;
114 
115  // Remove all net attributes from object attributes dictionnary
116  if( m_useX2format )
117  fputs( "%TD*%\n", outputFile );
118  else
119  fputs( "G04 #@! TD*\n", outputFile );
120 
122 }
123 
124 
125 void GERBER_PLOTTER::StartBlock( void* aData )
126 {
127  // Currently, it is the same as EndBlock(): clear all aperture net attributes
128  EndBlock( aData );
129 }
130 
131 
132 void GERBER_PLOTTER::EndBlock( void* aData )
133 {
134  // Remove all net attributes from object attributes dictionnary
136 }
137 
138 
140 {
141  // print a Gerber net attribute record.
142  // it is added to the object attributes dictionnary
143  // On file, only modified or new attributes are printed.
144  if( aData == NULL )
145  return;
146 
147  if( !m_useNetAttributes )
148  return;
149 
150  bool useX1StructuredComment = !m_useX2format;
151 
152  bool clearDict;
153  std::string short_attribute_string;
154 
155  if( !FormatNetAttribute( short_attribute_string, m_objectAttributesDictionnary,
156  aData, clearDict, useX1StructuredComment ) )
157  return;
158 
159  if( clearDict )
161 
162  if( !short_attribute_string.empty() )
163  fputs( short_attribute_string.c_str(), outputFile );
164 }
165 
166 
168 {
169  wxASSERT( outputFile );
170 
171  finalFile = outputFile; // the actual gerber file will be created later
172 
173  // Create a temporary filename to store gerber file
174  // note tmpfile() does not work under Vista and W7 in user mode
175  m_workFilename = filename + wxT(".tmp");
176  workFile = wxFopen( m_workFilename, wxT( "wt" ));
178  wxASSERT( outputFile );
179 
180  if( outputFile == NULL )
181  return false;
182 
183  for( unsigned ii = 0; ii < m_headerExtraLines.GetCount(); ii++ )
184  {
185  if( ! m_headerExtraLines[ii].IsEmpty() )
186  fprintf( outputFile, "%s\n", TO_UTF8( m_headerExtraLines[ii] ) );
187  }
188 
189  // Set coordinate format to 3.6 or 4.5 absolute, leading zero omitted
190  // the number of digits for the integer part of coordintes is needed
191  // in gerber format, but is not very important when omitting leading zeros
192  // It is fixed here to 3 (inch) or 4 (mm), but is not actually used
193  int leadingDigitCount = m_gerberUnitInch ? 3 : 4;
194 
195  fprintf( outputFile, "%%FSLAX%d%dY%d%d*%%\n",
196  leadingDigitCount, m_gerberUnitFmt,
197  leadingDigitCount, m_gerberUnitFmt );
198  fprintf( outputFile,
199  "G04 Gerber Fmt %d.%d, Leading zero omitted, Abs format (unit %s)*\n",
200  leadingDigitCount, m_gerberUnitFmt,
201  m_gerberUnitInch ? "inch" : "mm" );
202 
203  wxString Title = creator + wxT( " " ) + GetBuildVersion();
204  // In gerber files, ASCII7 chars only are allowed.
205  // So use a ISO date format (using a space as separator between date and time),
206  // not a localized date format
207  wxDateTime date = wxDateTime::Now();
208  fprintf( outputFile, "G04 Created by KiCad (%s) date %s*\n",
209  TO_UTF8( Title ), TO_UTF8( date.FormatISOCombined( ' ') ) );
210 
211  /* Mass parameter: unit = INCHES/MM */
212  if( m_gerberUnitInch )
213  fputs( "%MOIN*%\n", outputFile );
214  else
215  fputs( "%MOMM*%\n", outputFile );
216 
217  // Be sure the usual dark polarity is selected:
218  fputs( "%LPD*%\n", outputFile );
219 
220  fputs( "G04 APERTURE LIST*\n", outputFile );
221 
222  return true;
223 }
224 
225 
227 {
228  char line[1024];
229  wxString msg;
230 
231  wxASSERT( outputFile );
232 
233  /* Outfile is actually a temporary file i.e. workFile */
234  fputs( "M02*\n", outputFile );
235  fflush( outputFile );
236 
237  fclose( workFile );
238  workFile = wxFopen( m_workFilename, wxT( "rt" ));
239  wxASSERT( workFile );
241 
242  // Placement of apertures in RS274X
243  while( fgets( line, 1024, workFile ) )
244  {
245  fputs( line, outputFile );
246 
247  if( strcmp( strtok( line, "\n\r" ), "G04 APERTURE LIST*" ) == 0 )
248  {
250  fputs( "G04 APERTURE END LIST*\n", outputFile );
251  }
252  }
253 
254  fclose( workFile );
255  fclose( finalFile );
256  ::wxRemoveFile( m_workFilename );
257  outputFile = 0;
258 
259  return true;
260 }
261 
262 
264 {
265  defaultPenWidth = width;
266  currentAperture = apertures.end();
267 }
268 
269 
270 void GERBER_PLOTTER::SetCurrentLineWidth( int width, void* aData )
271 {
272  if( width == DO_NOT_SET_LINE_WIDTH )
273  return;
274 
275  int pen_width;
276 
277  if( width > 0 )
278  pen_width = width;
279  else
280  pen_width = defaultPenWidth;
281 
282  GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
283  int aperture_attribute = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
284 
285  selectAperture( wxSize( pen_width, pen_width ), APERTURE::Plotting, aperture_attribute );
286  currentPenWidth = pen_width;
287 }
288 
289 
290 std::vector<APERTURE>::iterator GERBER_PLOTTER::getAperture( const wxSize& aSize,
291  APERTURE::APERTURE_TYPE aType, int aApertureAttribute )
292 {
293  int last_D_code = 9;
294 
295  // Search an existing aperture
296  std::vector<APERTURE>::iterator tool = apertures.begin();
297 
298  while( tool != apertures.end() )
299  {
300  last_D_code = tool->m_DCode;
301 
302  if( (tool->m_Type == aType) && (tool->m_Size == aSize) &&
303  (tool->m_ApertureAttribute == aApertureAttribute) )
304  return tool;
305 
306  ++tool;
307  }
308 
309  // Allocate a new aperture
310  APERTURE new_tool;
311  new_tool.m_Size = aSize;
312  new_tool.m_Type = aType;
313  new_tool.m_DCode = last_D_code + 1;
314  new_tool.m_ApertureAttribute = aApertureAttribute;
315 
316  apertures.push_back( new_tool );
317 
318  return apertures.end() - 1;
319 }
320 
321 
322 void GERBER_PLOTTER::selectAperture( const wxSize& aSize,
324  int aApertureAttribute )
325 {
326  bool change = ( currentAperture == apertures.end() ) ||
327  ( currentAperture->m_Type != aType ) ||
328  ( currentAperture->m_Size != aSize );
329 
330  if( !m_useNetAttributes )
331  aApertureAttribute = 0;
332  else
333  change = change || ( currentAperture->m_ApertureAttribute != aApertureAttribute );
334 
335  if( change )
336  {
337  // Pick an existing aperture or create a new one
338  currentAperture = getAperture( aSize, aType, aApertureAttribute );
339  fprintf( outputFile, "D%d*\n", currentAperture->m_DCode );
340  }
341 }
342 
343 
345 {
346  wxASSERT( outputFile );
347  char cbuf[1024];
348 
349  bool useX1StructuredComment = false;
350 
351  if( !m_useX2format )
352  useX1StructuredComment = true;
353 
354  // Init
355  for( std::vector<APERTURE>::iterator tool = apertures.begin();
356  tool != apertures.end(); ++tool )
357  {
358  // apertude sizes are in inch or mm, regardless the
359  // coordinates format
360  double fscale = 0.0001 * plotScale / m_IUsPerDecimil; // inches
361 
362  if(! m_gerberUnitInch )
363  fscale *= 25.4; // size in mm
364 
365  int attribute = tool->m_ApertureAttribute;
366 
367  if( attribute != m_apertureAttribute )
368  {
371  useX1StructuredComment ).c_str(), outputFile );
372  }
373 
374  char* text = cbuf + sprintf( cbuf, "%%ADD%d", tool->m_DCode );
375 
376  /* Please note: the Gerber specs for mass parameters say that
377  exponential syntax is *not* allowed and the decimal point should
378  also be always inserted. So the %g format is ruled out, but %f is fine
379  (the # modifier forces the decimal point). Sadly the %f formatter
380  can't remove trailing zeros but thats not a problem, since nothing
381  forbid it (the file is only slightly longer) */
382 
383  switch( tool->m_Type )
384  {
385  case APERTURE::Circle:
386  sprintf( text, "C,%#f*%%\n", tool->m_Size.x * fscale );
387  break;
388 
389  case APERTURE::Rect:
390  sprintf( text, "R,%#fX%#f*%%\n",
391  tool->m_Size.x * fscale,
392  tool->m_Size.y * fscale );
393  break;
394 
395  case APERTURE::Plotting:
396  sprintf( text, "C,%#f*%%\n", tool->m_Size.x * fscale );
397  break;
398 
399  case APERTURE::Oval:
400  sprintf( text, "O,%#fX%#f*%%\n",
401  tool->m_Size.x * fscale,
402  tool->m_Size.y * fscale );
403  break;
404  }
405 
406  fputs( cbuf, outputFile );
407 
408  m_apertureAttribute = attribute;
409 
410  // Currently reset the aperture attribute. Perhaps a better optimization
411  // is to store the last attribute
412  if( attribute )
413  {
414  if( m_useX2format )
415  fputs( "%TD*%\n", outputFile );
416  else
417  fputs( "G04 #@! TD*\n", outputFile );
418 
420  }
421 
422  }
423 }
424 
425 
426 void GERBER_PLOTTER::PenTo( const wxPoint& aPos, char plume )
427 {
428  wxASSERT( outputFile );
429  DPOINT pos_dev = userToDeviceCoordinates( aPos );
430 
431  switch( plume )
432  {
433  case 'Z':
434  break;
435 
436  case 'U':
437  emitDcode( pos_dev, 2 );
438  break;
439 
440  case 'D':
441  emitDcode( pos_dev, 1 );
442  }
443 
444  penState = plume;
445 }
446 
447 
448 void GERBER_PLOTTER::Rect( const wxPoint& p1, const wxPoint& p2, FILL_T fill, int width )
449 {
450  std::vector< wxPoint > cornerList;
451 
452  // Build corners list
453  cornerList.push_back( p1 );
454  wxPoint corner(p1.x, p2.y);
455  cornerList.push_back( corner );
456  cornerList.push_back( p2 );
457  corner.x = p2.x;
458  corner.y = p1.y;
459  cornerList.push_back( corner );
460  cornerList.push_back( p1 );
461 
462  PlotPoly( cornerList, fill, width );
463 }
464 
465 
466 void GERBER_PLOTTER::Circle( const wxPoint& aCenter, int aDiameter, FILL_T aFill, int aWidth )
467 {
468  Arc( aCenter, 0, 3600, aDiameter / 2, aFill, aWidth );
469 }
470 
471 
472 void GERBER_PLOTTER::Arc( const wxPoint& aCenter, double aStAngle, double aEndAngle,
473  int aRadius, FILL_T aFill, int aWidth )
474 {
475  SetCurrentLineWidth( aWidth );
476 
477  wxPoint start, end;
478  start.x = aCenter.x + KiROUND( cosdecideg( aRadius, aStAngle ) );
479  start.y = aCenter.y - KiROUND( sindecideg( aRadius, aStAngle ) );
480  MoveTo( start );
481  end.x = aCenter.x + KiROUND( cosdecideg( aRadius, aEndAngle ) );
482  end.y = aCenter.y - KiROUND( sindecideg( aRadius, aEndAngle ) );
483  DPOINT devEnd = userToDeviceCoordinates( end );
484  DPOINT devCenter = userToDeviceCoordinates( aCenter ) - userToDeviceCoordinates( start );
485 
486  fprintf( outputFile, "G75*\n" ); // Multiquadrant (360 degrees) mode
487 
488  if( aStAngle < aEndAngle )
489  fprintf( outputFile, "G03" );
490  else
491  fprintf( outputFile, "G02" );
492 
493  fprintf( outputFile, "X%dY%dI%dJ%dD01*\n",
494  KiROUND( devEnd.x ), KiROUND( devEnd.y ),
495  KiROUND( devCenter.x ), KiROUND( devCenter.y ) );
496 
497  fprintf( outputFile, "G01*\n" ); // Back to linear interpol (perhaps useless here).
498 }
499 
500 
501 void GERBER_PLOTTER:: PlotPoly( const std::vector< wxPoint >& aCornerList,
502  FILL_T aFill, int aWidth, void * aData )
503 {
504  if( aCornerList.size() <= 1 )
505  return;
506 
507  // Gerber format does not know filled polygons with thick outline
508  // Therefore, to plot a filled polygon with outline having a thickness,
509  // one should plot outline as thick segments
510  GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
511 
512  SetCurrentLineWidth( aWidth, gbr_metadata );
513 
514  if( gbr_metadata )
515  formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
516 
517  if( aFill )
518  {
519  fputs( "G36*\n", outputFile );
520 
521  MoveTo( aCornerList[0] );
522  fputs( "G01*\n", outputFile ); // Set linear interpolation.
523 
524  for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
525  LineTo( aCornerList[ii] );
526 
527  FinishTo( aCornerList[0] );
528  fputs( "G37*\n", outputFile );
529  }
530 
531  if( aWidth > 0 )
532  {
533  MoveTo( aCornerList[0] );
534 
535  for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
536  LineTo( aCornerList[ii] );
537 
538  // Ensure the thick outline is closed for filled polygons
539  // (if not filled, could be only a polyline)
540  if( aFill && ( aCornerList[aCornerList.size()-1] != aCornerList[0] ) )
541  LineTo( aCornerList[0] );
542 
543  PenFinish();
544  }
545 }
546 
547 
548 void GERBER_PLOTTER::ThickSegment( const wxPoint& start, const wxPoint& end, int width,
549  EDA_DRAW_MODE_T tracemode, void* aData )
550 {
551  if( tracemode == FILLED )
552  {
553  GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
554  SetCurrentLineWidth( width, gbr_metadata );
555 
556  if( gbr_metadata )
557  formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
558 
559  MoveTo( start );
560  FinishTo( end );
561  }
562  else
563  {
565  segmentAsOval( start, end, width, tracemode );
566  }
567 }
568 
569 void GERBER_PLOTTER::ThickArc( const wxPoint& centre, double StAngle, double EndAngle,
570  int radius, int width, EDA_DRAW_MODE_T tracemode, void* aData )
571 {
572  GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
573  SetCurrentLineWidth( width, gbr_metadata );
574 
575  if( gbr_metadata )
576  formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
577 
578  if( tracemode == FILLED )
579  Arc( centre, StAngle, EndAngle, radius, NO_FILL, DO_NOT_SET_LINE_WIDTH );
580  else
581  {
583  Arc( centre, StAngle, EndAngle,
584  radius - ( width - currentPenWidth ) / 2,
586  Arc( centre, StAngle, EndAngle,
587  radius + ( width - currentPenWidth ) / 2, NO_FILL,
589  }
590 }
591 
592 
593 void GERBER_PLOTTER::ThickRect( const wxPoint& p1, const wxPoint& p2, int width,
594  EDA_DRAW_MODE_T tracemode, void* aData )
595 {
596  GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
597  SetCurrentLineWidth( width, gbr_metadata );
598 
599  if( gbr_metadata )
600  formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
601 
602  if( tracemode == FILLED )
603  Rect( p1, p2, NO_FILL, DO_NOT_SET_LINE_WIDTH );
604  else
605  {
607  wxPoint offsetp1( p1.x - (width - currentPenWidth) / 2,
608  p1.y - (width - currentPenWidth) / 2 );
609  wxPoint offsetp2( p2.x + (width - currentPenWidth) / 2,
610  p2.y + (width - currentPenWidth) / 2 );
611  Rect( offsetp1, offsetp2, NO_FILL, -1 );
612  offsetp1.x += (width - currentPenWidth);
613  offsetp1.y += (width - currentPenWidth);
614  offsetp2.x -= (width - currentPenWidth);
615  offsetp2.y -= (width - currentPenWidth);
616  Rect( offsetp1, offsetp2, NO_FILL, DO_NOT_SET_LINE_WIDTH );
617  }
618 }
619 
620 
621 void GERBER_PLOTTER::ThickCircle( const wxPoint& pos, int diametre, int width,
622  EDA_DRAW_MODE_T tracemode, void* aData )
623 {
624  GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
625  SetCurrentLineWidth( width, gbr_metadata );
626 
627  if( gbr_metadata )
628  formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
629 
630  if( tracemode == FILLED )
631  Circle( pos, diametre, NO_FILL, DO_NOT_SET_LINE_WIDTH );
632  else
633  {
635  Circle( pos, diametre - (width - currentPenWidth),
637  Circle( pos, diametre + (width - currentPenWidth),
639  }
640 }
641 
642 
643 void GERBER_PLOTTER::FlashPadCircle( const wxPoint& pos, int diametre, EDA_DRAW_MODE_T trace_mode, void* aData )
644 {
645  wxSize size( diametre, diametre );
646  GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
647 
648  if( trace_mode == SKETCH )
649  {
651 
652  if( gbr_metadata )
653  formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
654 
656  }
657  else
658  {
659  DPOINT pos_dev = userToDeviceCoordinates( pos );
660 
661  int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
662  selectAperture( size, APERTURE::Circle, aperture_attrib );
663 
664  if( gbr_metadata )
665  formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
666 
667  emitDcode( pos_dev, 3 );
668  }
669 }
670 
671 
672 void GERBER_PLOTTER::FlashPadOval( const wxPoint& pos, const wxSize& aSize, double orient,
673  EDA_DRAW_MODE_T trace_mode, void* aData )
674 {
675  wxASSERT( outputFile );
676  wxSize size( aSize );
677  GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
678 
679  /* Plot a flashed shape. */
680  if( ( orient == 0 || orient == 900 || orient == 1800 || orient == 2700 )
681  && trace_mode == FILLED )
682  {
683  if( orient == 900 || orient == 2700 ) /* orientation turned 90 deg. */
684  std::swap( size.x, size.y );
685 
686  DPOINT pos_dev = userToDeviceCoordinates( pos );
687  int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
688  selectAperture( size, APERTURE::Oval, aperture_attrib );
689 
690  if( gbr_metadata )
691  formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
692 
693  emitDcode( pos_dev, 3 );
694  }
695  else /* Plot pad as a segment. */
696  {
697  if( size.x > size.y )
698  {
699  std::swap( size.x, size.y );
700 
701  if( orient < 2700 )
702  orient += 900;
703  else
704  orient -= 2700;
705  }
706 
707  if( trace_mode == FILLED )
708  {
709  // TODO: use an aperture macro to declare the rotated pad
710 
711  // Flash a pad anchor, if a netlist attribute is set
712  if( aData )
713  FlashPadCircle( pos, size.x, trace_mode, aData );
714 
715  // The pad is reduced to an segment with dy > dx
716  int delta = size.y - size.x;
717  int x0 = 0;
718  int y0 = -delta / 2;
719  int x1 = 0;
720  int y1 = delta / 2;
721  RotatePoint( &x0, &y0, orient );
722  RotatePoint( &x1, &y1, orient );
723  GBR_METADATA metadata;
724 
725  if( gbr_metadata )
726  {
727  metadata = *gbr_metadata;
728 
729  // If the pad is drawn on a copper layer,
730  // set attribute to GBR_APERTURE_ATTRIB_CONDUCTOR
731  if( metadata.IsCopper() )
733 
734  // Clear .P attribute, only allowed for flashed items
735  wxString attrname( ".P" );
736  metadata.m_NetlistMetadata.ClearAttribute( &attrname );
737  }
738 
739  ThickSegment( wxPoint( pos.x + x0, pos.y + y0 ),
740  wxPoint( pos.x + x1, pos.y + y1 ),
741  size.x, trace_mode, &metadata );
742  }
743  else
744  {
745  sketchOval( pos, size, orient, -1 );
746  }
747  }
748 }
749 
750 
751 void GERBER_PLOTTER::FlashPadRect( const wxPoint& pos, const wxSize& aSize,
752  double orient, EDA_DRAW_MODE_T trace_mode, void* aData )
753 
754 {
755  wxASSERT( outputFile );
756  wxSize size( aSize );
757  GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
758 
759  // Plot as an aperture flash
760  switch( int( orient ) )
761  {
762  case 900:
763  case 2700: // rotation of 90 degrees or 270 swaps sizes
764  std::swap( size.x, size.y );
765  // Pass through
766  case 0:
767  case 1800:
768  if( trace_mode == SKETCH )
769  {
771 
772  if( gbr_metadata )
773  formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
774 
775  Rect( wxPoint( pos.x - (size.x - currentPenWidth) / 2,
776  pos.y - (size.y - currentPenWidth) / 2 ),
777  wxPoint( pos.x + (size.x - currentPenWidth) / 2,
778  pos.y + (size.y - currentPenWidth) / 2 ),
780  }
781  else
782  {
783  DPOINT pos_dev = userToDeviceCoordinates( pos );
784  int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
785  selectAperture( size, APERTURE::Rect, aperture_attrib );
786 
787  if( gbr_metadata )
788  formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
789 
790  emitDcode( pos_dev, 3 );
791  }
792  break;
793 
794  default: // plot pad shape as polygon
795  {
796  // XXX to do: use an aperture macro to declare the rotated pad
797  wxPoint coord[4];
798  // coord[0] is assumed the lower left
799  // coord[1] is assumed the upper left
800  // coord[2] is assumed the upper right
801  // coord[3] is assumed the lower right
802 
803  /* Trace the outline. */
804  coord[0].x = -size.x/2; // lower left
805  coord[0].y = size.y/2;
806  coord[1].x = -size.x/2; // upper left
807  coord[1].y = -size.y/2;
808  coord[2].x = size.x/2; // upper right
809  coord[2].y = -size.y/2;
810  coord[3].x = size.x/2; // lower right
811  coord[3].y = size.y/2;
812 
813  FlashPadTrapez( pos, coord, orient, trace_mode, aData );
814  }
815  break;
816  }
817 }
818 
819 void GERBER_PLOTTER::FlashPadRoundRect( const wxPoint& aPadPos, const wxSize& aSize,
820  int aCornerRadius, double aOrient,
821  EDA_DRAW_MODE_T aTraceMode, void* aData )
822 
823 {
824  GBR_METADATA gbr_metadata;
825 
826  if( aData )
827  {
828  gbr_metadata = *static_cast<GBR_METADATA*>( aData );
829  // If the pad is drawn on a copper layer,
830  // set attribute to GBR_APERTURE_ATTRIB_CONDUCTOR
831  if( gbr_metadata.IsCopper() )
833 
834  wxString attrname( ".P" );
835  gbr_metadata.m_NetlistMetadata.ClearAttribute( &attrname ); // not allowed on inner layers
836  }
837 
838  if( aTraceMode != FILLED )
840 
841  // Currently, a Pad RoundRect is plotted as polygon.
842  // TODO: use Aperture macro and flash it
843  SHAPE_POLY_SET outline;
844  const int segmentToCircleCount = 64;
845  TransformRoundChamferedRectToPolygon( outline, aPadPos, aSize, aOrient,
846  aCornerRadius, 0.0, 0, segmentToCircleCount );
847 
848  if( aTraceMode != FILLED )
849  outline.Inflate( -GetCurrentLineWidth()/2, 16 );
850 
851  std::vector< wxPoint > cornerList;
852  // TransformRoundRectToPolygon creates only one convex polygon
853  SHAPE_LINE_CHAIN& poly = outline.Outline( 0 );
854  cornerList.reserve( poly.PointCount() + 1 );
855 
856  for( int ii = 0; ii < poly.PointCount(); ++ii )
857  cornerList.push_back( wxPoint( poly.Point( ii ).x, poly.Point( ii ).y ) );
858 
859  // Close polygon
860  cornerList.push_back( cornerList[0] );
861 
862  PlotPoly( cornerList, aTraceMode == FILLED ? FILLED_SHAPE : NO_FILL,
863  aTraceMode == FILLED ? 0 : GetCurrentLineWidth(), &gbr_metadata );
864 
865  // Now, flash a pad anchor, if a netlist attribute is set
866  // (remove me when a Aperture macro will be used)
867  if( aData && aTraceMode == FILLED )
868  {
869  int diameter = std::min( aSize.x, aSize.y );
870  FlashPadCircle( aPadPos, diameter, aTraceMode , aData );
871  }
872 }
873 
874 void GERBER_PLOTTER::FlashPadCustom( const wxPoint& aPadPos, const wxSize& aSize,
875  SHAPE_POLY_SET* aPolygons,
876  EDA_DRAW_MODE_T aTraceMode, void* aData )
877 
878 {
879  // A Pad custom is plotted as polygon.
880 
881  // A flashed circle @aPadPos is added (anchor pad)
882  // However, because the anchor pad can be circle or rect, we use only
883  // a circle not bigger than the rect.
884  // the main purpose is to print a flashed DCode as pad anchor
885  if( aTraceMode == FILLED )
886  FlashPadCircle( aPadPos, std::min( aSize.x, aSize.y ), aTraceMode, aData );
887 
888  GBR_METADATA gbr_metadata;
889 
890  if( aData )
891  {
892  gbr_metadata = *static_cast<GBR_METADATA*>( aData );
893  // If the pad is drawn on a copper layer,
894  // set attribute to GBR_APERTURE_ATTRIB_CONDUCTOR
895  if( gbr_metadata.IsCopper() )
897 
898  wxString attrname( ".P" );
899  gbr_metadata.m_NetlistMetadata.ClearAttribute( &attrname ); // not allowed on inner layers
900  }
901 
902  SHAPE_POLY_SET polyshape = *aPolygons;
903 
904  if( aTraceMode != FILLED )
905  {
907  polyshape.Inflate( -GetCurrentLineWidth()/2, 16 );
908  }
909 
910  std::vector< wxPoint > cornerList;
911 
912  for( int cnt = 0; cnt < polyshape.OutlineCount(); ++cnt )
913  {
914  SHAPE_LINE_CHAIN& poly = polyshape.Outline( cnt );
915 
916  cornerList.clear();
917 
918  for( int ii = 0; ii < poly.PointCount(); ++ii )
919  cornerList.push_back( wxPoint( poly.Point( ii ).x, poly.Point( ii ).y ) );
920 
921  // Close polygon
922  cornerList.push_back( cornerList[0] );
923 
924  PlotPoly( cornerList,
925  aTraceMode == FILLED ? FILLED_SHAPE : NO_FILL,
926  aTraceMode == FILLED ? 0 : GetCurrentLineWidth(), &gbr_metadata );
927  }
928 }
929 
930 
931 void GERBER_PLOTTER::FlashPadTrapez( const wxPoint& aPadPos, const wxPoint* aCorners,
932  double aPadOrient, EDA_DRAW_MODE_T aTrace_Mode, void* aData )
933 
934 {
935  // Currently, a Pad Trapezoid is plotted as polygon.
936  // TODO: use Aperture macro and flash it
937 
938  // polygon corners list
939  std::vector< wxPoint > cornerList;
940 
941  for( int ii = 0; ii < 4; ii++ )
942  cornerList.push_back( aCorners[ii] );
943 
944  // Now, flash a pad anchor, if a netlist attribute is set
945  // (remove me when a Aperture macro will be used)
946  if( aData && ( aTrace_Mode == FILLED ) )
947  {
948  // Calculate the radius of the circle inside the shape
949  // It is the smaller dist from shape pos to edges
950  int radius = INT_MAX;
951 
952  for( unsigned ii = 0, jj = cornerList.size()-1; ii < cornerList.size();
953  jj = ii, ii++ )
954  {
955  SEG segment( aCorners[ii], aCorners[jj] );
956  int dist = segment.LineDistance( VECTOR2I( 0, 0) );
957  radius = std::min( radius, dist );
958  }
959 
960  FlashPadCircle( aPadPos, radius*2, aTrace_Mode, aData );
961  }
962 
963  // Draw the polygon and fill the interior as required
964  for( unsigned ii = 0; ii < 4; ii++ )
965  {
966  RotatePoint( &cornerList[ii], aPadOrient );
967  cornerList[ii] += aPadPos;
968  }
969 
970  // Close the polygon
971  cornerList.push_back( cornerList[0] );
972  GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
973 
974  GBR_METADATA metadata;
975 
976  if( gbr_metadata )
977  {
978  metadata = *gbr_metadata;
979  // If the pad is drawn on a copper layer,
980  // set attribute to GBR_APERTURE_ATTRIB_CONDUCTOR
981  if( metadata.IsCopper() )
983 
984  wxString attrname( ".P" );
985  metadata.m_NetlistMetadata.ClearAttribute( &attrname ); // not allowed on inner layers
986  }
987 
989  PlotPoly( cornerList, aTrace_Mode == FILLED ? FILLED_SHAPE : NO_FILL,
990  aTrace_Mode == FILLED ? 0 : GetCurrentLineWidth(),
991  &metadata );
992 }
993 
994 
995 void GERBER_PLOTTER::Text( const wxPoint& aPos, const COLOR4D aColor,
996  const wxString& aText, double aOrient, const wxSize& aSize,
997  enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify,
998  int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed,
999  void* aData )
1000 {
1001  GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1002 
1003  if( gbr_metadata )
1004  formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1005 
1006  PLOTTER::Text( aPos, aColor, aText, aOrient, aSize,
1007  aH_justify, aV_justify, aWidth, aItalic, aBold, aMultilineAllowed, aData );
1008 }
1009 
1010 
1011 void GERBER_PLOTTER::SetLayerPolarity( bool aPositive )
1012 {
1013  if( aPositive )
1014  fprintf( outputFile, "%%LPD*%%\n" );
1015  else
1016  fprintf( outputFile, "%%LPC*%%\n" );
1017 }
virtual void SetDefaultLineWidth(int width) override
Set the default line width.
std::vector< APERTURE >::iterator getAperture(const wxSize &aSize, APERTURE::APERTURE_TYPE aType, int aApertureAttribute)
Function getAperture returns a reference to the aperture which meets the size anf type of tool if the...
void FinishTo(const wxPoint &pos)
Definition: plotter.h:251
virtual void FlashPadRoundRect(const wxPoint &aPadPos, const wxSize &aSize, int aCornerRadius, double aOrient, EDA_DRAW_MODE_T aTraceMode, void *aData) override
Roundrect pad at the moment are not handled as aperture, since they require aperture macros TODO: alw...
a class to handle special data (items attributes) during plot.
void writeApertureList()
Generate the table of D codes.
void clearNetAttribute()
clear a Gerber net attribute record (clear object attribute dictionnary) and output the clear object ...
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:63
virtual void ThickSegment(const wxPoint &start, const wxPoint &end, int width, EDA_DRAW_MODE_T tracemode, void *aData) override
virtual void PenTo(const wxPoint &pos, char plume) override
moveto/lineto primitive, moves the 'pen' to the specified direction
void PenFinish()
Definition: plotter.h:257
virtual void FlashPadTrapez(const wxPoint &aPadPos, const wxPoint *aCorners, double aPadOrient, EDA_DRAW_MODE_T aTraceMode, void *aData) override
Trapezoidal pad at the moment are never handled as aperture, since they require aperture macros TODO:...
int OutlineCount() const
Returns the number of outlines in the set
static const int dist[10][10]
Definition: ar_matrix.cpp:320
virtual void SetLayerPolarity(bool aPositive) override
Change the plot polarity and begin a new layer Used to 'scratch off' silk screen away from solder mas...
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:118
APERTURE_TYPE
Definition: plotter.h:1014
virtual void EndBlock(void *aData) override
calling this function allows one to define the end of a group of drawing items the group is started b...
APERTURE_TYPE m_Type
Definition: plotter.h:1022
wxSize m_Size
Definition: plotter.h:1021
bool m_useNetAttributes
Definition: plotter.h:1246
void formatNetAttribute(GBR_NETLIST_METADATA *aData)
print a Gerber net attribute object record.
virtual void FlashPadCustom(const wxPoint &aPadPos, const wxSize &aSize, SHAPE_POLY_SET *aPolygons, EDA_DRAW_MODE_T aTraceMode, void *aData) override
virtual function FlashPadCustom
virtual void PlotPoly(const std::vector< wxPoint > &aCornerList, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=NULL) override
Gerber polygon: they can (and should) be filled with the appropriate G36/G37 sequence.
bool IsCopper()
Allowed attributes are not the same on board copper layers and on other layers Therefore a flag can b...
Definition: gbr_metadata.h:170
std::vector< APERTURE > apertures
Definition: plotter.h:1237
void TransformRoundChamferedRectToPolygon(SHAPE_POLY_SET &aCornerBuffer, const wxPoint &aPosition, const wxSize &aSize, double aRotation, int aCornerRadius, double aChamferRatio, int aChamferCorners, int aCircleToSegmentsCount)
convert a rectangle with rounded corners and/or chamfered corners to a polygon Convert rounded corner...
bool FormatNetAttribute(std::string &aPrintedText, std::string &aLastNetAttributes, GBR_NETLIST_METADATA *aData, bool &aClearPreviousAttributes, bool aUseX1StructuredComment)
Generates the string to print to a gerber file, to set a net attribute for a graphic object.
void selectAperture(const wxSize &aSize, APERTURE::APERTURE_TYPE aType, int aApertureAttribute)
Pick an existing aperture or create a new one, matching the size, type and attributes.
int LineDistance(const VECTOR2I &aP, bool aDetermineSide=false) const
Function LineDistance()
Definition: seg.h:357
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
char penState
Current pen state: 'U', 'D' or 'Z' (see PenTo)
Definition: plotter.h:562
this class handle info which can be added in a gerber file as attribute of an obtect the GBR_INFO_TYP...
double m_IUsPerDecimil
Definition: plotter.h:539
bool m_gerberUnitInch
Definition: plotter.h:1240
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
int PointCount() const
Function PointCount()
wxArrayString m_headerExtraLines
Definition: plotter.h:572
virtual void FlashPadOval(const wxPoint &pos, const wxSize &size, double orient, EDA_DRAW_MODE_T trace_mode, void *aData) override
Filled oval flashes are handled as aperture in the 90 degree positions only.
wxPoint plotOffset
Plot offset (in IUs)
Definition: plotter.h:545
static const int delta[8][2]
Definition: solve.cpp:112
virtual bool StartPlot() override
Function StartPlot Write GERBER header to file initialize global variable g_Plot_PlotOutputFile.
This file contains miscellaneous commonly used macros and functions.
virtual void ThickRect(const wxPoint &p1, const wxPoint &p2, int width, EDA_DRAW_MODE_T tracemode, void *aData) override
virtual void Text(const wxPoint &aPos, const COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, void *aData=NULL)
Draws text with the plotter.
virtual void Text(const wxPoint &aPos, const COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, void *aData=NULL) override
Draws text with the plotter.
virtual int GetCurrentLineWidth() const
Definition: plotter.h:152
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:47
void LineTo(const wxPoint &pos)
Definition: plotter.h:246
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
static const int USE_DEFAULT_LINE_WIDTH
Definition: plotter.h:102
bool m_plotMirror
X axis orientation (SVG) and plot mirrored (only for PS, PDF HPGL and SVG)
Definition: plotter.h:549
void Inflate(int aFactor, int aCircleSegmentsCount)
Performs outline inflation/deflation, using round corners.
virtual void SetCurrentLineWidth(int width, void *aData=NULL) override
Set the line width for the next drawing.
wxString GetBuildVersion()
Function GetBuildVersion Return the build version string.
Class SHAPE_POLY_SET.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
EDA_DRAW_MODE_T
Definition: eda_text.h:78
int m_gerberUnitFmt
Definition: plotter.h:1241
Base window classes and related definitions.
double plotScale
Plot scale - chosen by the user (even implicitly with 'fit in a4')
Definition: plotter.h:533
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB GetApertureAttrib()
Definition: gbr_metadata.h:147
void emitDcode(const DPOINT &pt, int dcode)
Emit a D-Code record, using proper conversions to format a leading zero omitted gerber coordinate (fo...
virtual void ThickArc(const wxPoint &centre, double StAngle, double EndAngle, int rayon, int width, EDA_DRAW_MODE_T tracemode, void *aData) override
FILE * finalFile
Definition: plotter.h:1229
virtual void FlashPadCircle(const wxPoint &pos, int diametre, EDA_DRAW_MODE_T trace_mode, void *aData) override
Filled circular flashes are stored as apertures.
aperture used for connected items like tracks (not vias)
Definition: gbr_metadata.h:84
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:70
void MoveTo(const wxPoint &pos)
Definition: plotter.h:241
bool m_useX2format
Definition: plotter.h:1243
Definition: seg.h:36
virtual DPOINT userToDeviceCoordinates(const wxPoint &aCoordinate)
Modifies coordinates according to the orientation, scale factor, and offsets trace.
Definition: plotter.cpp:96
static std::string FormatAttribute(GBR_APERTURE_ATTRIB aAttribute, bool aUseX1StructuredComment)
double cosdecideg(double r, double a)
Circle generation utility: computes r * cos(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:363
double sindecideg(double r, double a)
Circle generation utility: computes r * sin(a) Where a is in decidegrees, not in radians.
Definition: trigo.h:354
virtual void StartBlock(void *aData) override
calling this function allows one to define the beginning of a group of drawing items (used in X2 form...
virtual void Arc(const wxPoint &aCenter, double aStAngle, double aEndAngle, int aRadius, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH) override
Generic fallback: arc rendered as a polyline.
int currentPenWidth
Definition: plotter.h:560
int defaultPenWidth
true to generate a negative image (PS mode mainly)
Definition: plotter.h:559
virtual bool EndPlot() override
Class SHAPE_LINE_CHAIN.
FILE * outputFile
true if the Y axis is top to bottom (SVG)
Definition: plotter.h:554
std::string m_objectAttributesDictionnary
Definition: plotter.h:1223
The common library.
virtual void SetGerberCoordinatesFormat(int aResolution, bool aUseInches=false) override
Function SetGerberCoordinatesFormat selection of Gerber units and resolution (number of digits in man...
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:54
virtual void FlashPadRect(const wxPoint &pos, const wxSize &size, double orient, EDA_DRAW_MODE_T trace_mode, void *aData) override
Filled rect flashes are handled as aperture in the 0 90 180 or 270 degree orientation only and as pol...
void ClearAttribute(const wxString *aName)
remove the net attribute specified by aName If aName == NULL or empty, remove all attributes
VECTOR2I & Point(int aIndex)
Function Point()
void SetApertureAttrib(GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB aApertAttribute)
Definition: gbr_metadata.h:142
wxString m_workFilename
Definition: plotter.h:1230
GBR_NETLIST_METADATA m_NetlistMetadata
a item to handle object attribute:
Definition: gbr_metadata.h:181
int m_apertureAttribute
Definition: plotter.h:1226
Basic classes for most KiCad items.
virtual void Circle(const wxPoint &pos, int diametre, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
int m_DCode
Definition: plotter.h:1023
int m_ApertureAttribute
Definition: plotter.h:1024
FILE * workFile
Definition: plotter.h:1228
double iuPerDeviceUnit
Device scale (from IUs to plotter device units - usually decimils)
Definition: plotter.h:542
static const int DO_NOT_SET_LINE_WIDTH
Definition: plotter.h:101
void segmentAsOval(const wxPoint &start, const wxPoint &end, int width, EDA_DRAW_MODE_T tracemode)
Cdonvert a thick segment and plot it as an oval.
Definition: plotter.cpp:387
wxString filename
Definition: plotter.h:566
std::vector< APERTURE >::iterator currentAperture
Definition: plotter.h:1238
virtual void ThickCircle(const wxPoint &pos, int diametre, int width, EDA_DRAW_MODE_T tracemode, void *aData) override
void sketchOval(const wxPoint &pos, const wxSize &size, double orient, int width)
Definition: plotter.cpp:408
wxString creator
Definition: plotter.h:565
wxSize paperSize
Paper size in IU - not in mils.
Definition: plotter.h:570
#define min(a, b)
Definition: auxiliary.h:85
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39