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) 2022 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
26#include <string_utils.h>
28#include <macros.h>
29#include <math/util.h> // for KiROUND
30#include <trigo.h>
31#include <wx/log.h>
32
33#include <build_version.h>
34
37
38#include <gbr_metadata.h>
39
40
41// if GBR_USE_MACROS is defined, pads having a shape that is not a Gerber primitive
42// will use a macro when possible
43// Old code will be removed only after many tests
44//
45// Note also: setting m_gerberDisableApertMacros to true disable all aperture macros
46// in Gerber files
47//
48#define GBR_USE_MACROS_FOR_CHAMFERED_ROUND_RECT
49#define GBR_USE_MACROS_FOR_CHAMFERED_RECT
50#define GBR_USE_MACROS_FOR_ROUNDRECT
51#define GBR_USE_MACROS_FOR_TRAPEZOID
52#define GBR_USE_MACROS_FOR_ROTATED_OVAL
53#define GBR_USE_MACROS_FOR_ROTATED_RECT
54#define GBR_USE_MACROS_FOR_CUSTOM_PAD
55
56// max count of corners to create a aperture macro for a custom shape.
57// provided just in case a aperture macro type free polygon creates issues
58// when the number of corners is too high.
59// (1 corner = up to 24 chars)
60// Gerber doc say max corners 5000. We use a slightly smaller value.
61// if a custom shape needs more than GBR_MACRO_FOR_CUSTOM_PAD_MAX_CORNER_COUNT, it
62// will be plot using a region.
63#define GBR_MACRO_FOR_CUSTOM_PAD_MAX_CORNER_COUNT 4990
64#define AM_FREEPOLY_BASENAME "FreePoly"
65
66
67// A helper function to compare 2 polygons: polygons are similar if they have the same
68// number of vertices and each vertex coordinate are similar, i.e. if the difference
69// between coordinates is small ( <= margin to accept rounding issues coming from polygon
70// geometric transforms like rotation
71static bool polyCompare( const std::vector<VECTOR2I>& aPolygon,
72 const std::vector<VECTOR2I>& aTestPolygon )
73{
74 // fast test: polygon sizes must be the same:
75 if( aTestPolygon.size() != aPolygon.size() )
76 return false;
77
78 const int margin = 2;
79
80 for( size_t jj = 0; jj < aPolygon.size(); jj++ )
81 {
82 if( std::abs( aPolygon[jj].x - aTestPolygon[jj].x ) > margin ||
83 std::abs( aPolygon[jj].y - aTestPolygon[jj].y ) > margin )
84 return false;
85 }
86
87 return true;
88}
89
90
92{
93 workFile = nullptr;
94 finalFile = nullptr;
97
98 // number of digits after the point (number of digits of the mantissa
99 // Be careful: the Gerber coordinates are stored in an integer
100 // so 6 digits (inches) or 5 digits (mm) is a good value
101 // To avoid overflow, 7 digits (inches) or 6 digits is a max.
102 // with lower values than 6 digits (inches) or 5 digits (mm),
103 // Creating self-intersecting polygons from non-intersecting polygons
104 // happen easily.
105 m_gerberUnitInch = false;
106 m_gerberUnitFmt = 6;
107 m_useX2format = true;
108 m_useNetAttributes = true;
110
111 m_hasApertureRoundRect = false; // true is at least one round rect aperture is in use
112 m_hasApertureRotOval = false; // true is at least one oval rotated aperture is in use
113 m_hasApertureRotRect = false; // true is at least one rect. rotated aperture is in use
114 m_hasApertureOutline4P = false; // true is at least one rotated rect or trapezoid pad
115 // aperture is in use
116 m_hasApertureChamferedRect = false; // true is at least one chamfered rect
117 // (no rounded corner) is in use
118}
119
120
121void GERBER_PLOTTER::SetViewport( const VECTOR2I& aOffset, double aIusPerDecimil,
122 double aScale, bool aMirror )
123{
124 wxASSERT( aMirror == false );
125 m_plotMirror = false;
126 m_plotOffset = aOffset;
127 wxASSERT( aScale == 1 ); // aScale parameter is not used in Gerber
128 m_plotScale = 1; // Plot scale is *always* 1.0
129
130 m_IUsPerDecimil = aIusPerDecimil;
131
132 // gives now a default value to iuPerDeviceUnit (because the units of the caller is now known)
133 // which could be modified later by calling SetGerberCoordinatesFormat()
134 m_iuPerDeviceUnit = pow( 10.0, m_gerberUnitFmt ) / ( m_IUsPerDecimil * 10000.0 );
135
136 // We don't handle the filmbox, and it's more useful to keep the
137 // origin at the origin
138 m_paperSize.x = 0;
139 m_paperSize.y = 0;
140}
141
142
143void GERBER_PLOTTER::SetGerberCoordinatesFormat( int aResolution, bool aUseInches )
144{
145 m_gerberUnitInch = aUseInches;
146 m_gerberUnitFmt = aResolution;
147
148 m_iuPerDeviceUnit = pow( 10.0, m_gerberUnitFmt ) / ( m_IUsPerDecimil * 10000.0 );
149
150 if( ! m_gerberUnitInch )
151 m_iuPerDeviceUnit *= 25.4; // gerber output in mm
152}
153
154
155void GERBER_PLOTTER::emitDcode( const VECTOR2D& pt, int dcode )
156{
157
158 fprintf( m_outputFile, "X%dY%dD%02d*\n", KiROUND( pt.x ), KiROUND( pt.y ), dcode );
159}
160
162{
163 // Remove all attributes from object attributes dictionary (TO. and TA commands)
164 if( m_useX2format )
165 fputs( "%TD*%\n", m_outputFile );
166 else
167 fputs( "G04 #@! TD*\n", m_outputFile );
168
170}
171
172
174{
175 // disable a Gerber net attribute (exists only in X2 with net attributes mode).
176 if( m_objectAttributesDictionary.empty() ) // No net attribute or not X2 mode
177 return;
178
179 // Remove all net attributes from object attributes dictionary
180 if( m_useX2format )
181 fputs( "%TD*%\n", m_outputFile );
182 else
183 fputs( "G04 #@! TD*\n", m_outputFile );
184
186}
187
188
189void GERBER_PLOTTER::StartBlock( void* aData )
190{
191 // Currently, it is the same as EndBlock(): clear all aperture net attributes
192 EndBlock( aData );
193}
194
195
196void GERBER_PLOTTER::EndBlock( void* aData )
197{
198 // Remove all net attributes from object attributes dictionary
200}
201
202
204{
205 // print a Gerber net attribute record.
206 // it is added to the object attributes dictionary
207 // On file, only modified or new attributes are printed.
208 if( aData == nullptr )
209 return;
210
211 if( !m_useNetAttributes )
212 return;
213
214 bool useX1StructuredComment = !m_useX2format;
215
216 bool clearDict;
217 std::string short_attribute_string;
218
219 if( !FormatNetAttribute( short_attribute_string, m_objectAttributesDictionary,
220 aData, clearDict, useX1StructuredComment ) )
221 return;
222
223 if( clearDict )
225
226 if( !short_attribute_string.empty() )
227 fputs( short_attribute_string.c_str(), m_outputFile );
228
229 if( m_useX2format && !aData->m_ExtraData.IsEmpty() )
230 {
231 std::string extra_data = TO_UTF8( aData->m_ExtraData );
232 fputs( extra_data.c_str(), m_outputFile );
233 }
234}
235
236
237bool GERBER_PLOTTER::StartPlot( const wxString& aPageNumber )
238{
239 m_hasApertureRoundRect = false; // true is at least one round rect aperture is in use
240 m_hasApertureRotOval = false; // true is at least one oval rotated aperture is in use
241 m_hasApertureRotRect = false; // true is at least one rect. rotated aperture is in use
242 m_hasApertureOutline4P = false; // true is at least one rotated rect/trapezoid aperture
243 // is in use
244 m_hasApertureChamferedRect = false; // true is at least one chamfered rect is in use
246
247 wxASSERT( m_outputFile );
248
249 finalFile = m_outputFile; // the actual gerber file will be created later
250
251 // Create a temp file in system temp to avoid potential network share buffer issues for
252 // the final read and save.
253 m_workFilename = wxFileName::CreateTempFileName( "" );
254 workFile = wxFopen( m_workFilename, wxT( "wt" ));
256 wxASSERT( m_outputFile );
257
258 if( m_outputFile == nullptr )
259 return false;
260
261 for( unsigned ii = 0; ii < m_headerExtraLines.GetCount(); ii++ )
262 {
263 if( ! m_headerExtraLines[ii].IsEmpty() )
264 fprintf( m_outputFile, "%s\n", TO_UTF8( m_headerExtraLines[ii] ) );
265 }
266
267 // Set coordinate format to 3.6 or 4.5 absolute, leading zero omitted
268 // the number of digits for the integer part of coordinates is needed
269 // in gerber format, but is not very important when omitting leading zeros
270 // It is fixed here to 3 (inch) or 4 (mm), but is not actually used
271 int leadingDigitCount = m_gerberUnitInch ? 3 : 4;
272
273 fprintf( m_outputFile, "%%FSLAX%d%dY%d%d*%%\n",
274 leadingDigitCount, m_gerberUnitFmt,
275 leadingDigitCount, m_gerberUnitFmt );
276 fprintf( m_outputFile,
277 "G04 Gerber Fmt %d.%d, Leading zero omitted, Abs format (unit %s)*\n",
278 leadingDigitCount, m_gerberUnitFmt,
279 m_gerberUnitInch ? "inch" : "mm" );
280
281 wxString Title = m_creator + wxT( " " ) + GetBuildVersion();
282
283 // In gerber files, ASCII7 chars only are allowed.
284 // So use a ISO date format (using a space as separator between date and time),
285 // not a localized date format
286 wxDateTime date = wxDateTime::Now();
287 fprintf( m_outputFile, "G04 Created by KiCad (%s) date %s*\n",
288 TO_UTF8( Title ), TO_UTF8( date.FormatISOCombined( ' ') ) );
289
290 /* Mass parameter: unit = INCHES/MM */
291 if( m_gerberUnitInch )
292 fputs( "%MOIN*%\n", m_outputFile );
293 else
294 fputs( "%MOMM*%\n", m_outputFile );
295
296 // Be sure the usual dark polarity is selected:
297 fputs( "%LPD*%\n", m_outputFile );
298
299 // Set initial interpolation mode: always G01 (linear):
300 fputs( "G01*\n", m_outputFile );
301
302 // Add aperture list start point
303 fputs( "G04 APERTURE LIST*\n", m_outputFile );
304
305 // Give a minimal value to the default pen size, used to plot items in sketch mode
306 if( m_renderSettings )
307 {
308 const int pen_min = 0.1 * m_IUsPerDecimil * 10000 / 25.4; // for min width = 0.1 mm
310 pen_min ) );
311 }
312
313 return true;
314}
315
316
318{
319 char line[1024];
320
321 wxASSERT( m_outputFile );
322
323 /* Outfile is actually a temporary file i.e. workFile */
324 fputs( "M02*\n", m_outputFile );
325 fflush( m_outputFile );
326
327 fclose( workFile );
328 workFile = wxFopen( m_workFilename, wxT( "rt" ));
329 wxASSERT( workFile );
331
332 // Placement of apertures in RS274X
333 while( fgets( line, 1024, workFile ) )
334 {
335 fputs( line, m_outputFile );
336
337 char* substr = strtok( line, "\n\r" );
338
339 if( substr && strcmp( substr, "G04 APERTURE LIST*" ) == 0 )
340 {
341 // Add aperture list macro:
345 {
346 fputs( "G04 Aperture macros list*\n", m_outputFile );
347
350
353
356
359
361 {
366 }
367
369 {
370 // aperture sizes are in inch or mm, regardless the
371 // coordinates format
372 double fscale = 0.0001 * m_plotScale / m_IUsPerDecimil; // inches
373
374 if(! m_gerberUnitInch )
375 fscale *= 25.4; // size in mm
376
378 }
379
380 fputs( "G04 Aperture macros list end*\n", m_outputFile );
381 }
382
384 fputs( "G04 APERTURE END LIST*\n", m_outputFile );
385 }
386 }
387
388 fclose( workFile );
389 fclose( finalFile );
390 ::wxRemoveFile( m_workFilename );
391 m_outputFile = nullptr;
392
393 return true;
394}
395
396
397void GERBER_PLOTTER::SetCurrentLineWidth( int aWidth, void* aData )
398{
399 if( aWidth == DO_NOT_SET_LINE_WIDTH )
400 return;
401 else if( aWidth == USE_DEFAULT_LINE_WIDTH )
403
404 wxASSERT_MSG( aWidth >= 0, "Plotter called to set negative pen width" );
405
406 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
407 int aperture_attribute = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
408
410 aperture_attribute );
411 m_currentPenWidth = aWidth;
412}
413
414
415int GERBER_PLOTTER::GetOrCreateAperture( const VECTOR2I& aSize, int aRadius,
416 const EDA_ANGLE& aRotation, APERTURE::APERTURE_TYPE aType,
417 int aApertureAttribute )
418{
419 int last_D_code = 9;
420
421 // Search an existing aperture
422 for( int idx = 0; idx < (int)m_apertures.size(); ++idx )
423 {
424 APERTURE* tool = &m_apertures[idx];
425 last_D_code = tool->m_DCode;
426
427 if( (tool->m_Type == aType) && (tool->m_Size == aSize) &&
428 (tool->m_Radius == aRadius) && (tool->m_Rotation == aRotation) &&
429 (tool->m_ApertureAttribute == aApertureAttribute) )
430 return idx;
431 }
432
433 // Allocate a new aperture
434 APERTURE new_tool;
435 new_tool.m_Size = aSize;
436 new_tool.m_Type = aType;
437 new_tool.m_Radius = aRadius;
438 new_tool.m_Rotation = aRotation;
439 new_tool.m_DCode = last_D_code + 1;
440 new_tool.m_ApertureAttribute = aApertureAttribute;
441
442 m_apertures.push_back( new_tool );
443
444 return m_apertures.size() - 1;
445}
446
447
448int GERBER_PLOTTER::GetOrCreateAperture( const std::vector<VECTOR2I>& aCorners,
449 const EDA_ANGLE& aRotation, APERTURE::APERTURE_TYPE aType,
450 int aApertureAttribute )
451{
452 int last_D_code = 9;
453
454 // For APERTURE::AM_FREE_POLYGON aperture macros, we need to create the macro
455 // on the fly, because due to the fact the vertex count is not a constant we
456 // cannot create a static definition.
457 if( APERTURE::AM_FREE_POLYGON == aType )
458 {
459 int idx = m_am_freepoly_list.FindAm( aCorners );
460
461 if( idx < 0 )
462 m_am_freepoly_list.Append( aCorners );
463 }
464
465 // Search an existing aperture
466 for( int idx = 0; idx < (int)m_apertures.size(); ++idx )
467 {
468 APERTURE* tool = &m_apertures[idx];
469
470 last_D_code = tool->m_DCode;
471
472 if( (tool->m_Type == aType) &&
473 (tool->m_Corners.size() == aCorners.size() ) &&
474 (tool->m_Rotation == aRotation) &&
475 (tool->m_ApertureAttribute == aApertureAttribute) )
476 {
477 // A candidate is found. the corner lists must be similar
478 bool is_same = polyCompare( tool->m_Corners, aCorners );
479
480 if( is_same )
481 return idx;
482 }
483 }
484
485 // Allocate a new aperture
486 APERTURE new_tool;
487
488 new_tool.m_Corners = aCorners;
489 new_tool.m_Size = VECTOR2I( 0, 0 ); // Not used
490 new_tool.m_Type = aType;
491 new_tool.m_Radius = 0; // Not used
492 new_tool.m_Rotation = aRotation;
493 new_tool.m_DCode = last_D_code + 1;
494 new_tool.m_ApertureAttribute = aApertureAttribute;
495
496 m_apertures.push_back( new_tool );
497
498 return m_apertures.size() - 1;
499}
500
501
502void GERBER_PLOTTER::selectAperture( const VECTOR2I& aSize, int aRadius, const EDA_ANGLE& aRotation,
503 APERTURE::APERTURE_TYPE aType, int aApertureAttribute )
504{
505 bool change = ( m_currentApertureIdx < 0 ) ||
506 ( m_apertures[m_currentApertureIdx].m_Type != aType ) ||
507 ( m_apertures[m_currentApertureIdx].m_Size != aSize ) ||
508 ( m_apertures[m_currentApertureIdx].m_Radius != aRadius ) ||
509 ( m_apertures[m_currentApertureIdx].m_Rotation != aRotation );
510
511 if( !change )
512 change = m_apertures[m_currentApertureIdx].m_ApertureAttribute != aApertureAttribute;
513
514 if( change )
515 {
516 // Pick an existing aperture or create a new one
517 m_currentApertureIdx = GetOrCreateAperture( aSize, aRadius, aRotation, aType,
518 aApertureAttribute );
519 fprintf( m_outputFile, "D%d*\n", m_apertures[m_currentApertureIdx].m_DCode );
520 }
521}
522
523
524void GERBER_PLOTTER::selectAperture( const std::vector<VECTOR2I>& aCorners,
525 const EDA_ANGLE& aRotation, APERTURE::APERTURE_TYPE aType,
526 int aApertureAttribute )
527{
528 bool change = ( m_currentApertureIdx < 0 ) ||
529 ( m_apertures[m_currentApertureIdx].m_Type != aType ) ||
530 ( m_apertures[m_currentApertureIdx].m_Corners.size() != aCorners.size() ) ||
531 ( m_apertures[m_currentApertureIdx].m_Rotation != aRotation );
532
533 if( !change ) // Compare corner lists
534 {
535 for( size_t ii = 0; ii < aCorners.size(); ii++ )
536 {
537 if( aCorners[ii] != m_apertures[m_currentApertureIdx].m_Corners[ii] )
538 {
539 change = true;
540 break;
541 }
542 }
543 }
544
545 if( !change )
546 change = m_apertures[m_currentApertureIdx].m_ApertureAttribute != aApertureAttribute;
547
548 if( change )
549 {
550 // Pick an existing aperture or create a new one
551 m_currentApertureIdx = GetOrCreateAperture( aCorners, aRotation, aType,
552 aApertureAttribute );
553 fprintf( m_outputFile, "D%d*\n", m_apertures[m_currentApertureIdx].m_DCode );
554 }
555}
556
557
558void GERBER_PLOTTER::selectAperture( int aDiameter, const EDA_ANGLE& aPolygonRotation,
559 APERTURE::APERTURE_TYPE aType, int aApertureAttribute )
560{
561 // Pick an existing aperture or create a new one, matching the
562 // aDiameter, aPolygonRotation, type and attributes for type =
563 // AT_REGULAR_POLY3 to AT_REGULAR_POLY12
564
565 wxASSERT( aType>= APERTURE::APERTURE_TYPE::AT_REGULAR_POLY3 &&
566 aType <= APERTURE::APERTURE_TYPE::AT_REGULAR_POLY12 );
567
568 VECTOR2I size( aDiameter, (int) ( aPolygonRotation.AsDegrees() * 1000.0 ) );
569 selectAperture( VECTOR2I( 0, 0 ), aDiameter / 2, aPolygonRotation, aType, aApertureAttribute );
570}
571
573{
574 wxASSERT( m_outputFile );
575 char cbuf[1024];
576 std::string buffer;
577
578 bool useX1StructuredComment = false;
579
580 if( !m_useX2format )
581 useX1StructuredComment = true;
582
583 // Init
584 for( APERTURE& tool : m_apertures )
585 {
586 // aperture sizes are in inch or mm, regardless the
587 // coordinates format
588 double fscale = 0.0001 * m_plotScale / m_IUsPerDecimil; // inches
589
590 if(! m_gerberUnitInch )
591 fscale *= 25.4; // size in mm
592
593 int attribute = tool.m_ApertureAttribute;
594
595 if( attribute != m_apertureAttribute )
596 {
599 useX1StructuredComment ).c_str(), m_outputFile );
600 }
601
602 sprintf( cbuf, "%%ADD%d", tool.m_DCode );
603 buffer = cbuf;
604
605 /* Please note: the Gerber specs for mass parameters say that
606 exponential syntax is *not* allowed and the decimal point should
607 also be always inserted. So the %g format is ruled out, but %f is fine
608 (the # modifier forces the decimal point). Sadly the %f formatter
609 can't remove trailing zeros but that's not a problem, since nothing
610 forbid it (the file is only slightly longer) */
611
612 switch( tool.m_Type )
613 {
615 sprintf( cbuf, "C,%#f*%%\n", tool.GetDiameter() * fscale );
616 break;
617
619 sprintf( cbuf, "R,%#fX%#f*%%\n", tool.m_Size.x * fscale,
620 tool.m_Size.y * fscale );
621 break;
622
624 sprintf( cbuf, "C,%#f*%%\n", tool.m_Size.x * fscale );
625 break;
626
628 sprintf( cbuf, "O,%#fX%#f*%%\n", tool.m_Size.x * fscale,
629 tool.m_Size.y * fscale );
630 break;
631
643 sprintf( cbuf, "P,%#fX%dX%#f*%%\n", tool.GetDiameter() * fscale,
644 tool.GetRegPolyVerticeCount(), tool.GetRotation().AsDegrees() );
645 break;
646
647 case APERTURE::AM_ROUND_RECT: // Aperture macro for round rect pads
648 {
649 // The aperture macro needs coordinates of the centers of the 4 corners
650 std::vector<VECTOR2I> corners;
651 VECTOR2I half_size( tool.m_Size.x/2-tool.m_Radius, tool.m_Size.y/2-tool.m_Radius );
652
653 corners.emplace_back( -half_size.x, -half_size.y );
654 corners.emplace_back( half_size.x, -half_size.y );
655 corners.emplace_back( half_size.x, half_size.y );
656 corners.emplace_back( -half_size.x, half_size.y );
657
658 // Rotate the corner coordinates:
659 for( int ii = 0; ii < 4; ii++ )
660 RotatePoint( corners[ii], -tool.m_Rotation );
661
662 sprintf( cbuf, "%s,%#fX", APER_MACRO_ROUNDRECT_NAME,
663 tool.m_Radius * fscale );
664 buffer += cbuf;
665
666 // Add each corner
667 for( int ii = 0; ii < 4; ii++ )
668 {
669 sprintf( cbuf, "%#fX%#fX",
670 corners[ii].x * fscale, corners[ii].y * fscale );
671 buffer += cbuf;
672 }
673
674 sprintf( cbuf, "0*%%\n" );
675 }
676 break;
677
678 case APERTURE::AM_ROT_RECT: // Aperture macro for rotated rect pads
679 sprintf( cbuf, "%s,%#fX%#fX%#f*%%\n", APER_MACRO_ROT_RECT_NAME,
680 tool.m_Size.x * fscale, tool.m_Size.y * fscale,
681 tool.m_Rotation.AsDegrees() );
682 break;
683
684 case APERTURE::APER_MACRO_OUTLINE4P: // Aperture macro for trapezoid pads
685 case APERTURE::APER_MACRO_OUTLINE5P: // Aperture macro for chamfered rect pads
686 case APERTURE::APER_MACRO_OUTLINE6P: // Aperture macro for chamfered rect pads
687 case APERTURE::APER_MACRO_OUTLINE7P: // Aperture macro for chamfered rect pads
688 case APERTURE::APER_MACRO_OUTLINE8P: // Aperture macro for chamfered rect pads
689 switch( tool.m_Type )
690 {
692 sprintf( cbuf, "%s,", APER_MACRO_OUTLINE4P_NAME ); break;
694 sprintf( cbuf, "%s,", APER_MACRO_OUTLINE5P_NAME ); break;
696 sprintf( cbuf, "%s,", APER_MACRO_OUTLINE6P_NAME ); break;
698 sprintf( cbuf, "%s,", APER_MACRO_OUTLINE7P_NAME ); break;
700 sprintf( cbuf, "%s,", APER_MACRO_OUTLINE8P_NAME ); break;
701 default:
702 break;
703 }
704
705 buffer += cbuf;
706
707 // Output all corners (should be 4 to 8 corners)
708 // Remember: the Y coordinate must be negated, due to the fact in Pcbnew
709 // the Y axis is from top to bottom
710 for( size_t ii = 0; ii < tool.m_Corners.size(); ii++ )
711 {
712 sprintf( cbuf, "%#fX%#fX",
713 tool.m_Corners[ii].x * fscale, -tool.m_Corners[ii].y * fscale );
714 buffer += cbuf;
715 }
716
717 // close outline and output rotation
718 sprintf( cbuf, "%#f*%%\n", tool.m_Rotation.AsDegrees() );
719 break;
720
721 case APERTURE::AM_ROTATED_OVAL: // Aperture macro for rotated oval pads
722 // (not rotated is a primitive)
723 // m_Size.x = full length; m_Size.y = width, and the macro aperture expects
724 // the position of ends
725 {
726 // the seg_len is the distance between the 2 circle centers
727 int seg_len = tool.m_Size.x - tool.m_Size.y;
728 // Center of the circle on the segment start point:
729 VECTOR2I start( seg_len/2, 0 );
730 // Center of the circle on the segment end point:
731 VECTOR2I end( - seg_len/2, 0 );
732
733 RotatePoint( start, tool.m_Rotation );
734 RotatePoint( end, tool.m_Rotation );
735
736 sprintf( cbuf, "%s,%#fX%#fX%#fX%#fX%#fX0*%%\n", APER_MACRO_SHAPE_OVAL_NAME,
737 tool.m_Size.y * fscale, // width
738 start.x * fscale, -start.y * fscale, // X,Y corner start pos
739 end.x * fscale, -end.y * fscale ); // X,Y cornerend pos
740 }
741 break;
742
744 {
745 // Find the aperture macro name in the list of aperture macro
746 // created on the fly for this polygon:
747 int idx = m_am_freepoly_list.FindAm( tool.m_Corners );
748
749 // Write DCODE id ( "%ADDxx" is already in buffer) and rotation
750 // the full line is something like :%ADD12FreePoly1,45.000000*%
751 sprintf( cbuf, "%s%d,%#f*%%\n",
753 idx,
754 tool.m_Rotation.AsDegrees() );
755 break;
756 }
757 }
758
759 buffer += cbuf;
760 fputs( buffer.c_str(), m_outputFile );
761
762 m_apertureAttribute = attribute;
763
764 // Currently reset the aperture attribute. Perhaps a better optimization
765 // is to store the last attribute
766 if( attribute )
767 {
768 if( m_useX2format )
769 fputs( "%TD*%\n", m_outputFile );
770 else
771 fputs( "G04 #@! TD*\n", m_outputFile );
772
774 }
775
776 }
777}
778
779
780void GERBER_PLOTTER::PenTo( const VECTOR2I& aPos, char plume )
781{
782 wxASSERT( m_outputFile );
783 VECTOR2D pos_dev = userToDeviceCoordinates( aPos );
784
785 switch( plume )
786 {
787 case 'Z':
788 break;
789
790 case 'U':
791 emitDcode( pos_dev, 2 );
792 break;
793
794 case 'D':
795 emitDcode( pos_dev, 1 );
796 }
797
798 m_penState = plume;
799}
800
801
802void GERBER_PLOTTER::Rect( const VECTOR2I& p1, const VECTOR2I& p2, FILL_T fill, int width )
803{
804 std::vector<VECTOR2I> cornerList;
805
806 cornerList.reserve( 5 );
807
808 // Build corners list
809 cornerList.push_back( p1 );
810
811 VECTOR2I corner( p1.x, p2.y );
812 cornerList.push_back( corner );
813 cornerList.push_back( p2 );
814 corner.x = p2.x;
815 corner.y = p1.y;
816 cornerList.push_back( corner );
817 cornerList.push_back( p1 );
818
819 PlotPoly( cornerList, fill, width );
820}
821
822
823void GERBER_PLOTTER::Circle( const VECTOR2I& aCenter, int aDiameter, FILL_T aFill, int aWidth )
824{
825 Arc( aCenter, ANGLE_0, ANGLE_360, aDiameter / 2, aFill, aWidth );
826}
827
828
829
830void GERBER_PLOTTER::Arc( const VECTOR2I& aCenter, const EDA_ANGLE& aStartAngle,
831 const EDA_ANGLE& aEndAngle, int aRadius, FILL_T aFill, int aWidth )
832{
833 SetCurrentLineWidth( aWidth );
834
835 // aFill is not used here.
836 plotArc( aCenter, aStartAngle, aEndAngle, aRadius, false );
837}
838
839
840void GERBER_PLOTTER::plotArc( const SHAPE_ARC& aArc, bool aPlotInRegion )
841{
842 VECTOR2I start( aArc.GetP0() );
843 VECTOR2I end( aArc.GetP1() );
844 VECTOR2I center( aArc.GetCenter() );
845
846 if( !aPlotInRegion )
847 MoveTo( start);
848 else
849 LineTo( start );
850
851 VECTOR2D devEnd = userToDeviceCoordinates( end );
852 VECTOR2D devCenter = userToDeviceCoordinates( center - start );
853
854 // We need to know if the arc is CW or CCW in device coordinates, so build this arc.
855 SHAPE_ARC deviceArc( userToDeviceCoordinates( start ),
856 userToDeviceCoordinates( aArc.GetArcMid() ), devEnd, 0 );
857
858 fprintf( m_outputFile, "G75*\n" ); // Multiquadrant (360 degrees) mode
859
860 if( deviceArc.IsClockwise() )
861 fprintf( m_outputFile, "G02*\n" ); // Active circular interpolation, CW
862 else
863 fprintf( m_outputFile, "G03*\n" ); // Active circular interpolation, CCW
864
865 fprintf( m_outputFile, "X%dY%dI%dJ%dD01*\n",
866 KiROUND( devEnd.x ), KiROUND( devEnd.y ),
867 KiROUND( devCenter.x ), KiROUND( devCenter.y ) );
868
869 fprintf( m_outputFile, "G01*\n" ); // Back to linear interpolate (perhaps useless here).
870}
871
872
873void GERBER_PLOTTER::plotArc( const VECTOR2I& aCenter, const EDA_ANGLE& aStartAngle,
874 const EDA_ANGLE& aEndAngle, int aRadius, bool aPlotInRegion )
875{
876 VECTOR2I start, end;
877 start.x = aCenter.x + KiROUND( aRadius * aStartAngle.Cos() );
878 start.y = aCenter.y - KiROUND( aRadius * aStartAngle.Sin() );
879
880 if( !aPlotInRegion )
881 MoveTo( start );
882 else
883 LineTo( start );
884
885 end.x = aCenter.x + KiROUND( aRadius * aEndAngle.Cos() );
886 end.y = aCenter.y - KiROUND( aRadius * aEndAngle.Sin() );
887 VECTOR2D devEnd = userToDeviceCoordinates( end );
888 VECTOR2D devCenter = userToDeviceCoordinates( aCenter ) - userToDeviceCoordinates( start );
889
890 fprintf( m_outputFile, "G75*\n" ); // Multiquadrant (360 degrees) mode
891
892 if( aStartAngle < aEndAngle )
893 fprintf( m_outputFile, "G03*\n" ); // Active circular interpolation, CCW
894 else
895 fprintf( m_outputFile, "G02*\n" ); // Active circular interpolation, CW
896
897 fprintf( m_outputFile, "X%dY%dI%dJ%dD01*\n",
898 KiROUND( devEnd.x ), KiROUND( devEnd.y ),
899 KiROUND( devCenter.x ), KiROUND( devCenter.y ) );
900
901 fprintf( m_outputFile, "G01*\n" ); // Back to linear interpolate (perhaps useless here).
902}
903
904
905void GERBER_PLOTTER::PlotGerberRegion( const SHAPE_LINE_CHAIN& aPoly, void* aData )
906{
907 if( aPoly.PointCount() <= 2 )
908 return;
909
910 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
911
912 bool clearTA_AperFunction = false; // true if a TA.AperFunction is used
913
914 if( gbr_metadata )
915 {
916 std::string attrib = gbr_metadata->m_ApertureMetadata.FormatAttribute( !m_useX2format );
917
918 if( !attrib.empty() )
919 {
920 fputs( attrib.c_str(), m_outputFile );
921 clearTA_AperFunction = true;
922 }
923 }
924
925 PlotPoly( aPoly, FILL_T::FILLED_SHAPE, 0 , gbr_metadata );
926
927 // Clear the TA attribute, to avoid the next item to inherit it:
928 if( clearTA_AperFunction )
929 {
930 if( m_useX2format )
931 {
932 fputs( "%TD.AperFunction*%\n", m_outputFile );
933 }
934 else
935 {
936 fputs( "G04 #@! TD.AperFunction*\n", m_outputFile );
937 }
938 }
939}
940
941
942void GERBER_PLOTTER::PlotGerberRegion( const std::vector<VECTOR2I>& aCornerList, void* aData )
943{
944 if( aCornerList.size() <= 2 )
945 return;
946
947 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
948
949 bool clearTA_AperFunction = false; // true if a TA.AperFunction is used
950
951 if( gbr_metadata )
952 {
953 std::string attrib = gbr_metadata->m_ApertureMetadata.FormatAttribute( !m_useX2format );
954
955 if( !attrib.empty() )
956 {
957 fputs( attrib.c_str(), m_outputFile );
958 clearTA_AperFunction = true;
959 }
960 }
961
962 PlotPoly( aCornerList, FILL_T::FILLED_SHAPE, 0, gbr_metadata );
963
964 // Clear the TA attribute, to avoid the next item to inherit it:
965 if( clearTA_AperFunction )
966 {
967 if( m_useX2format )
968 {
969 fputs( "%TD.AperFunction*%\n", m_outputFile );
970 }
971 else
972 {
973 fputs( "G04 #@! TD.AperFunction*\n", m_outputFile );
974 }
975 }
976}
977
978
979void GERBER_PLOTTER::PlotPoly( const SHAPE_LINE_CHAIN& aPoly, FILL_T aFill, int aWidth,
980 void* aData )
981{
982 if( aPoly.CPoints().size() <= 1 )
983 return;
984
985 // Gerber format does not know filled polygons with thick outline
986 // Therefore, to plot a filled polygon with outline having a thickness,
987 // one should plot outline as thick segments
988 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
989
990 if( gbr_metadata )
991 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
992
993 if( aFill != FILL_T::NO_FILL )
994 {
995 fputs( "G36*\n", m_outputFile );
996
997 MoveTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
998
999 fputs( "G01*\n", m_outputFile ); // Set linear interpolation.
1000
1001 for( int ii = 1; ii < aPoly.PointCount(); ii++ )
1002 {
1003 int arcindex = aPoly.ArcIndex( ii );
1004
1005 if( arcindex < 0 )
1006 {
1008 LineTo( VECTOR2I( aPoly.CPoint( ii ) ) );
1009 }
1010 else
1011 {
1012 const SHAPE_ARC& arc = aPoly.Arc( arcindex );
1013
1014 plotArc( arc, ii > 0 );
1015
1016 // skip points on arcs, since we plot the arc itself
1017 while( ii+1 < aPoly.PointCount() && arcindex == aPoly.ArcIndex( ii+1 ) )
1018 ii++;
1019 }
1020 }
1021
1022 // If the polygon is not closed, close it:
1023 if( aPoly.CPoint( 0 ) != aPoly.CPoint( -1 ) )
1024 FinishTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
1025
1026 fputs( "G37*\n", m_outputFile );
1027 }
1028
1029 if( aWidth > 0 ) // Draw the polyline/polygon outline
1030 {
1031 SetCurrentLineWidth( aWidth, gbr_metadata );
1032
1033 MoveTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
1034
1035 for( int ii = 1; ii < aPoly.PointCount(); ii++ )
1036 {
1037 int arcindex = aPoly.ArcIndex( ii );
1038
1039 if( arcindex < 0 )
1040 {
1042 LineTo( VECTOR2I( aPoly.CPoint( ii ) ) );
1043 }
1044 else
1045 {
1046 const SHAPE_ARC& arc = aPoly.Arc( arcindex );
1047
1048 plotArc( arc, ii > 0 );
1049
1050 // skip points on arcs, since we plot the arc itself
1051 while( ii+1 < aPoly.PointCount() && arcindex == aPoly.ArcIndex( ii+1 ) )
1052 ii++;
1053 }
1054 }
1055
1056 // Ensure the thick outline is closed for filled polygons
1057 // (if not filled, could be only a polyline)
1058 if( ( aPoly.CPoint( 0 ) != aPoly.CPoint( -1 ) )
1059 && ( aPoly.IsClosed() || aFill != FILL_T::NO_FILL ) )
1060 LineTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
1061
1062 PenFinish();
1063 }
1064}
1065
1066void GERBER_PLOTTER::PlotPoly( const std::vector<VECTOR2I>& aCornerList, FILL_T aFill, int aWidth,
1067 void * aData )
1068{
1069 if( aCornerList.size() <= 1 )
1070 return;
1071
1072 // Gerber format does not know filled polygons with thick outline
1073 // Therefore, to plot a filled polygon with outline having a thickness,
1074 // one should plot outline as thick segments
1075 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1076
1077 if( gbr_metadata )
1078 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1079
1080 if( aFill != FILL_T::NO_FILL )
1081 {
1082 fputs( "G36*\n", m_outputFile );
1083
1084 MoveTo( aCornerList[0] );
1085 fputs( "G01*\n", m_outputFile ); // Set linear interpolation.
1086
1087 for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
1088 LineTo( aCornerList[ii] );
1089
1090 // If the polygon is not closed, close it:
1091 if( aCornerList[0] != aCornerList[aCornerList.size()-1] )
1092 FinishTo( aCornerList[0] );
1093
1094 fputs( "G37*\n", m_outputFile );
1095 }
1096
1097 if( aWidth > 0 ) // Draw the polyline/polygon outline
1098 {
1099 SetCurrentLineWidth( aWidth, gbr_metadata );
1100
1101 MoveTo( aCornerList[0] );
1102
1103 for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
1104 LineTo( aCornerList[ii] );
1105
1106 // Ensure the thick outline is closed for filled polygons
1107 // (if not filled, could be only a polyline)
1108 if( aFill != FILL_T::NO_FILL && ( aCornerList[aCornerList.size() - 1] != aCornerList[0] ) )
1109 LineTo( aCornerList[0] );
1110
1111 PenFinish();
1112 }
1113}
1114
1115
1116void GERBER_PLOTTER::ThickSegment( const VECTOR2I& start, const VECTOR2I& end, int width,
1117 OUTLINE_MODE tracemode, void* aData )
1118{
1119 if( tracemode == FILLED )
1120 {
1121 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1122 SetCurrentLineWidth( width, gbr_metadata );
1123
1124 if( gbr_metadata )
1125 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1126
1127 MoveTo( start );
1128 FinishTo( end );
1129 }
1130 else
1131 {
1133 segmentAsOval( start, end, width, tracemode );
1134 }
1135}
1136
1137void GERBER_PLOTTER::ThickArc( const VECTOR2I& aCentre, const EDA_ANGLE& aStartAngle,
1138 const EDA_ANGLE& aEndAngle, int aRadius, int aWidth,
1139 OUTLINE_MODE aTraceMode, void* aData )
1140{
1141 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1142 SetCurrentLineWidth( aWidth, gbr_metadata );
1143
1144 if( gbr_metadata )
1145 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1146
1147 if( aTraceMode == FILLED )
1148 {
1149 Arc( aCentre, aStartAngle, aEndAngle, aRadius, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1150 }
1151 else
1152 {
1154 Arc( aCentre, aStartAngle, aEndAngle, aRadius - ( aWidth - m_currentPenWidth ) / 2,
1156 Arc( aCentre, aStartAngle, aEndAngle, aRadius + ( aWidth - m_currentPenWidth ) / 2,
1158 }
1159}
1160
1161
1162void GERBER_PLOTTER::ThickArc( const VECTOR2I& aCentre, const VECTOR2I& aStart,
1163 const VECTOR2I& aEnd, int aWidth,
1164 OUTLINE_MODE aTraceMode, void* aData )
1165{
1166 EDA_ANGLE start_angle( aStart - aCentre );
1167 EDA_ANGLE end_angle( aEnd - aCentre );
1168
1169 if( start_angle > end_angle )
1170 {
1171 if( end_angle < ANGLE_0 )
1172 end_angle.Normalize();
1173 else
1174 start_angle = start_angle.Normalize() - ANGLE_360;
1175 }
1176
1177 int radius = (aStart - aCentre).EuclideanNorm();
1178
1179 if( !m_yaxisReversed ) // should be always the case
1180 {
1181 std::swap( end_angle, start_angle );
1182 end_angle = -end_angle;
1183 start_angle = -start_angle;
1184 }
1185
1186 ThickArc( aCentre, start_angle, end_angle, radius, aWidth, aTraceMode, aData );
1187}
1188
1189
1191 OUTLINE_MODE aTraceMode, void* aData )
1192{
1193 wxASSERT( aArcShape.GetShape() == SHAPE_T::ARC );
1194
1195 ThickArc( aArcShape.getCenter(), aArcShape.GetStart(), aArcShape.GetEnd(),
1196 aArcShape.GetWidth(), aTraceMode, aData );
1197}
1198
1199
1200void GERBER_PLOTTER::ThickRect( const VECTOR2I& p1, const VECTOR2I& p2, int width,
1201 OUTLINE_MODE tracemode, void* aData )
1202{
1203 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1204 SetCurrentLineWidth( width, gbr_metadata );
1205
1206 if( gbr_metadata )
1207 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1208
1209 if( tracemode == FILLED )
1210 {
1212 }
1213 else
1214 {
1216 VECTOR2I offsetp1( p1.x - ( width - m_currentPenWidth ) / 2,
1217 p1.y - (width - m_currentPenWidth) / 2 );
1218 VECTOR2I offsetp2( p2.x + ( width - m_currentPenWidth ) / 2,
1219 p2.y + (width - m_currentPenWidth) / 2 );
1220 Rect( offsetp1, offsetp2, FILL_T::NO_FILL, -1 );
1221 offsetp1.x += (width - m_currentPenWidth);
1222 offsetp1.y += (width - m_currentPenWidth);
1223 offsetp2.x -= (width - m_currentPenWidth);
1224 offsetp2.y -= (width - m_currentPenWidth);
1225 Rect( offsetp1, offsetp2, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1226 }
1227}
1228
1229
1230void GERBER_PLOTTER::ThickCircle( const VECTOR2I& pos, int diametre, int width,
1231 OUTLINE_MODE tracemode, void* aData )
1232{
1233 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1234 SetCurrentLineWidth( width, gbr_metadata );
1235
1236 if( gbr_metadata )
1237 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1238
1239 if( tracemode == FILLED )
1240 {
1241 Circle( pos, diametre, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1242 }
1243 else
1244 {
1246 Circle( pos, diametre - (width - m_currentPenWidth), FILL_T::NO_FILL,
1248 Circle( pos, diametre + (width - m_currentPenWidth), FILL_T::NO_FILL,
1250 }
1251}
1252
1253
1254void GERBER_PLOTTER::FilledCircle( const VECTOR2I& pos, int diametre,
1255 OUTLINE_MODE tracemode, void* aData )
1256{
1257 // A filled circle is a graphic item, not a pad.
1258 // So it is drawn, not flashed.
1259 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1260
1261 if( gbr_metadata )
1262 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1263
1264 if( tracemode == FILLED )
1265 {
1266 // Draw a circle of diameter = diameter/2 with a line thickness = radius,
1267 // To create a filled circle
1268 SetCurrentLineWidth( diametre/2, gbr_metadata );
1269 Circle( pos, diametre/2, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1270 }
1271 else
1272 {
1274 Circle( pos, diametre, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1275 }
1276}
1277
1278
1279void GERBER_PLOTTER::FlashPadCircle( const VECTOR2I& pos, int diametre, OUTLINE_MODE trace_mode,
1280 void* aData )
1281{
1282 VECTOR2I size( diametre, diametre );
1283 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1284
1285 if( trace_mode == SKETCH )
1286 {
1287 if( gbr_metadata )
1288 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1289
1291
1292 Circle( pos, diametre, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1293 }
1294 else
1295 {
1296 VECTOR2D pos_dev = userToDeviceCoordinates( pos );
1297
1298 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1299 selectAperture( size, 0, ANGLE_0, APERTURE::AT_CIRCLE, aperture_attrib );
1300
1301 if( gbr_metadata )
1302 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1303
1304 emitDcode( pos_dev, 3 );
1305 }
1306}
1307
1308
1309void GERBER_PLOTTER::FlashPadOval( const VECTOR2I& aPos, const VECTOR2I& aSize,
1310 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
1311{
1312 wxASSERT( m_outputFile );
1313
1314 VECTOR2I size( aSize );
1315 EDA_ANGLE orient( aOrient );
1316 orient.Normalize();
1317 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1318
1319 // Flash a vertical or horizontal shape (this is a basic aperture).
1320 if( orient.IsCardinal() && aTraceMode == FILLED )
1321 {
1322 if( orient.IsCardinal90() )
1323 std::swap( size.x, size.y );
1324
1325 VECTOR2I pos_device = userToDeviceCoordinates( aPos );
1326 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1327 selectAperture( size, 0, ANGLE_0, APERTURE::AT_OVAL, aperture_attrib );
1328
1329 if( gbr_metadata )
1330 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1331
1332 emitDcode( pos_device, 3 );
1333 }
1334 else // Plot pad as region.
1335 // Only regions and flashed items accept a object attribute TO.P for the pin name
1336 {
1337 if( aTraceMode == FILLED )
1338 {
1339 #ifdef GBR_USE_MACROS_FOR_ROTATED_OVAL
1341 #endif
1342 {
1343 m_hasApertureRotOval = true;
1344 // We are using a aperture macro that expect size.y < size.x
1345 // i.e draw a horizontal line for rotation = 0.0
1346 // size.x = length, size.y = width
1347 if( size.x < size.y )
1348 {
1349 std::swap( size.x, size.y );
1350 orient += ANGLE_90;
1351
1352 if( orient > ANGLE_180 )
1353 orient -= ANGLE_180;
1354 }
1355
1356 VECTOR2I pos_device = userToDeviceCoordinates( aPos );
1357 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1358 selectAperture( size, 0, orient, APERTURE::AM_ROTATED_OVAL, aperture_attrib );
1359
1360 if( gbr_metadata )
1361 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1362
1363 emitDcode( pos_device, 3 );
1364 return;
1365 }
1366 // Draw the oval as round rect pad with a radius = 50% min size)
1367 // In gerber file, it will be drawn as a region with arcs, and can be
1368 // detected as pads (similar to a flashed pad)
1369 FlashPadRoundRect( aPos, aSize, std::min( aSize.x, aSize.y ) / 2, orient, FILLED,
1370 aData );
1371 }
1372 else // Non filled shape: plot outlines:
1373 {
1374 if( size.x > size.y )
1375 {
1376 std::swap( size.x, size.y );
1377
1378 if( orient < ANGLE_270 )
1379 orient += ANGLE_90;
1380 else
1381 orient -= ANGLE_270;
1382 }
1383
1384 sketchOval( aPos, size, orient, -1 );
1385 }
1386 }
1387}
1388
1389
1390void GERBER_PLOTTER::FlashPadRect( const VECTOR2I& pos, const VECTOR2I& aSize,
1391 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
1392
1393{
1394 wxASSERT( m_outputFile );
1395
1396 VECTOR2I size( aSize );
1397 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1398
1399 // Horizontal / vertical rect can use a basic aperture (not a macro)
1400 // so use it for rotation n*90 deg
1401 if( aOrient.IsCardinal() )
1402 {
1403 if( aOrient.IsCardinal90() )
1404 // Build the not rotated equivalent shape:
1405 std::swap( size.x, size.y );
1406
1407 if( aTraceMode == SKETCH )
1408 {
1409 if( gbr_metadata )
1410 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1411
1413 Rect( VECTOR2I( pos.x - ( size.x / 2 ), pos.y - (size.y / 2 ) ),
1414 VECTOR2I( pos.x + ( size.x / 2 ), pos.y + (size.y / 2 ) ),
1416 }
1417 else
1418 {
1419 VECTOR2I pos_device = userToDeviceCoordinates( pos );
1420 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1421 selectAperture( size, 0, ANGLE_0, APERTURE::AT_RECT, aperture_attrib );
1422
1423 if( gbr_metadata )
1424 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1425
1426 emitDcode( pos_device, 3 );
1427 }
1428 }
1429 else
1430 {
1431 #ifdef GBR_USE_MACROS_FOR_ROTATED_RECT
1432 if( aTraceMode != SKETCH && !m_gerberDisableApertMacros )
1433 {
1434 m_hasApertureRotRect = true;
1435
1436 VECTOR2I pos_device = userToDeviceCoordinates( pos );
1437 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1438 selectAperture( size, 0, aOrient, APERTURE::AM_ROT_RECT, aperture_attrib );
1439
1440 if( gbr_metadata )
1441 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1442
1443 emitDcode( pos_device, 3 );
1444 }
1445 else
1446 #endif
1447 {
1448 // plot pad shape as Gerber region
1449 VECTOR2I coord[4];
1450 // coord[0] is assumed the lower left
1451 // coord[1] is assumed the upper left
1452 // coord[2] is assumed the upper right
1453 // coord[3] is assumed the lower right
1454
1455 coord[0].x = -size.x/2; // lower left
1456 coord[0].y = size.y/2;
1457 coord[1].x = -size.x/2; // upper left
1458 coord[1].y = -size.y/2;
1459 coord[2].x = size.x/2; // upper right
1460 coord[2].y = -size.y/2;
1461 coord[3].x = size.x/2; // lower right
1462 coord[3].y = size.y/2;
1463
1464 FlashPadTrapez( pos, coord, aOrient, aTraceMode, aData );
1465 }
1466 }
1467}
1468
1469void GERBER_PLOTTER::FlashPadRoundRect( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
1470 int aCornerRadius, const EDA_ANGLE& aOrient,
1471 OUTLINE_MODE aTraceMode, void* aData )
1472
1473{
1474 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1475
1476 if( aTraceMode != FILLED )
1477 {
1478 SHAPE_POLY_SET outline;
1479 TransformRoundChamferedRectToPolygon( outline, aPadPos, aSize, aOrient, aCornerRadius, 0.0,
1481
1483
1484 std::vector<VECTOR2I> cornerList;
1485 // TransformRoundRectToPolygon creates only one convex polygon
1486 SHAPE_LINE_CHAIN& poly = outline.Outline( 0 );
1487 cornerList.reserve( poly.PointCount() + 1 );
1488
1489 for( int ii = 0; ii < poly.PointCount(); ++ii )
1490 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
1491
1492 // Close polygon
1493 cornerList.push_back( cornerList[0] );
1494
1495 // plot outlines
1496 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), gbr_metadata );
1497 }
1498 else
1499 {
1500 #ifdef GBR_USE_MACROS_FOR_ROUNDRECT
1502 #endif
1503 {
1505
1506 VECTOR2D pos_dev = userToDeviceCoordinates( aPadPos );
1507 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1508 selectAperture( aSize, aCornerRadius, aOrient, APERTURE::AM_ROUND_RECT,
1509 aperture_attrib );
1510
1511 if( gbr_metadata )
1512 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1513
1514 emitDcode( pos_dev, 3 );
1515 return;
1516 }
1517
1518 // A Pad RoundRect is plotted as a Gerber region.
1519 // Initialize region metadata:
1520 bool clearTA_AperFunction = false; // true if a TA.AperFunction is used
1521
1522 if( gbr_metadata )
1523 {
1524 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1525 std::string attrib = gbr_metadata->m_ApertureMetadata.FormatAttribute( !m_useX2format );
1526
1527 if( !attrib.empty() )
1528 {
1529 fputs( attrib.c_str(), m_outputFile );
1530 clearTA_AperFunction = true;
1531 }
1532 }
1533
1534 // Plot the region using arcs in corners.
1535 plotRoundRectAsRegion( aPadPos, aSize, aCornerRadius, aOrient );
1536
1537 // Clear the TA attribute, to avoid the next item to inherit it:
1538 if( clearTA_AperFunction )
1539 {
1540 if( m_useX2format )
1541 fputs( "%TD.AperFunction*%\n", m_outputFile );
1542 else
1543 fputs( "G04 #@! TD.AperFunction*\n", m_outputFile );
1544 }
1545 }
1546}
1547
1548
1549void GERBER_PLOTTER::plotRoundRectAsRegion( const VECTOR2I& aRectCenter, const VECTOR2I& aSize,
1550 int aCornerRadius, const EDA_ANGLE& aOrient )
1551{
1552 // The region outline is generated by 4 sides and 4 90 deg arcs
1553 // 1 --- 2
1554 // | c |
1555 // 4 --- 3
1556
1557 // Note also in user coordinates the Y axis is from top to bottom
1558 // for historical reasons.
1559
1560 // A helper structure to handle outlines coordinates (segments and arcs)
1561 // in user coordinates
1562 struct RR_EDGE
1563 {
1564 VECTOR2I m_start;
1565 VECTOR2I m_end;
1566 VECTOR2I m_center;
1567 EDA_ANGLE m_arc_angle_start;
1568 };
1569
1570 int hsizeX = aSize.x/2;
1571 int hsizeY = aSize.y/2;
1572
1573 RR_EDGE curr_edge;
1574 std::vector<RR_EDGE> rr_outline;
1575
1576 rr_outline.reserve( 4 );
1577
1578 // Build outline coordinates, relative to rectangle center, rotation 0:
1579
1580 // Top left corner 1 (and 4 to 1 left vertical side @ x=-hsizeX)
1581 curr_edge.m_start.x = -hsizeX;
1582 curr_edge.m_start.y = hsizeY - aCornerRadius;
1583 curr_edge.m_end.x = curr_edge.m_start.x;
1584 curr_edge.m_end.y = -hsizeY + aCornerRadius;
1585 curr_edge.m_center.x = -hsizeX + aCornerRadius;
1586 curr_edge.m_center.y = curr_edge.m_end.y;
1587 curr_edge.m_arc_angle_start = aOrient + ANGLE_180;
1588
1589 rr_outline.push_back( curr_edge );
1590
1591 // Top right corner 2 (and 1 to 2 top horizontal side @ y=-hsizeY)
1592 curr_edge.m_start.x = -hsizeX + aCornerRadius;
1593 curr_edge.m_start.y = -hsizeY;
1594 curr_edge.m_end.x = hsizeX - aCornerRadius;
1595 curr_edge.m_end.y = curr_edge.m_start.y;
1596 curr_edge.m_center.x = curr_edge.m_end.x;
1597 curr_edge.m_center.y = -hsizeY + aCornerRadius;
1598 curr_edge.m_arc_angle_start = aOrient + ANGLE_90;
1599
1600 rr_outline.push_back( curr_edge );
1601
1602 // bottom right corner 3 (and 2 to 3 right vertical side @ x=hsizeX)
1603 curr_edge.m_start.x = hsizeX;
1604 curr_edge.m_start.y = -hsizeY + aCornerRadius;
1605 curr_edge.m_end.x = curr_edge.m_start.x;
1606 curr_edge.m_end.y = hsizeY - aCornerRadius;
1607 curr_edge.m_center.x = hsizeX - aCornerRadius;
1608 curr_edge.m_center.y = curr_edge.m_end.y;
1609 curr_edge.m_arc_angle_start = aOrient + ANGLE_0;
1610
1611 rr_outline.push_back( curr_edge );
1612
1613 // bottom left corner 4 (and 3 to 4 bottom horizontal side @ y=hsizeY)
1614 curr_edge.m_start.x = hsizeX - aCornerRadius;
1615 curr_edge.m_start.y = hsizeY;
1616 curr_edge.m_end.x = -hsizeX + aCornerRadius;
1617 curr_edge.m_end.y = curr_edge.m_start.y;
1618 curr_edge.m_center.x = curr_edge.m_end.x;
1619 curr_edge.m_center.y = hsizeY - aCornerRadius;
1620 curr_edge.m_arc_angle_start = aOrient - ANGLE_90;
1621
1622 rr_outline.push_back( curr_edge );
1623
1624 // Move relative coordinates to the actual location and rotation:
1625 VECTOR2I arc_last_center;
1626 EDA_ANGLE arc_last_angle = curr_edge.m_arc_angle_start - ANGLE_90;
1627
1628 for( RR_EDGE& rr_edge: rr_outline )
1629 {
1630 RotatePoint( rr_edge.m_start, aOrient );
1631 RotatePoint( rr_edge.m_end, aOrient );
1632 RotatePoint( rr_edge.m_center, aOrient );
1633 rr_edge.m_start += aRectCenter;
1634 rr_edge.m_end += aRectCenter;
1635 rr_edge.m_center += aRectCenter;
1636 arc_last_center = rr_edge.m_center;
1637 }
1638
1639 // Ensure the region is a closed polygon, i.e. the end point of last segment
1640 // (end of arc) is the same as the first point. Rounding issues can create a
1641 // small difference, mainly for rotated pads.
1642 // calculate last point (end of last arc):
1643 VECTOR2I last_pt;
1644 last_pt.x = arc_last_center.x + KiROUND( aCornerRadius * arc_last_angle.Cos() );
1645 last_pt.y = arc_last_center.y - KiROUND( aCornerRadius * arc_last_angle.Sin() );
1646
1647 VECTOR2I first_pt = rr_outline[0].m_start;
1648
1649#if 0 // For test only:
1650 if( last_pt != first_pt )
1651 wxLogMessage( wxS( "first pt %d %d last pt %d %d" ),
1652 first_pt.x, first_pt.y, last_pt.x, last_pt.y );
1653#endif
1654
1655 fputs( "G36*\n", m_outputFile ); // Start region
1656 fputs( "G01*\n", m_outputFile ); // Set linear interpolation.
1657 first_pt = last_pt;
1658 MoveTo( first_pt ); // Start point of region, must be same as end point
1659
1660 for( RR_EDGE& rr_edge: rr_outline )
1661 {
1662 if( aCornerRadius ) // Guard: ensure we do not create arcs with radius = 0
1663 {
1664 // LineTo( rr_edge.m_end ); // made in plotArc()
1665 plotArc( rr_edge.m_center, rr_edge.m_arc_angle_start,
1666 rr_edge.m_arc_angle_start - ANGLE_90, aCornerRadius, true );
1667 }
1668 else
1669 {
1670 LineTo( rr_edge.m_end );
1671 }
1672 }
1673
1674 fputs( "G37*\n", m_outputFile ); // Close region
1675}
1676
1677
1678void GERBER_PLOTTER::FlashPadCustom( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
1679 const EDA_ANGLE& aOrient, SHAPE_POLY_SET* aPolygons,
1680 OUTLINE_MODE aTraceMode, void* aData )
1681
1682{
1683 // A Pad custom is plotted as polygon (a region in Gerber language).
1684 GBR_METADATA gbr_metadata;
1685
1686 if( aData )
1687 gbr_metadata = *static_cast<GBR_METADATA*>( aData );
1688
1689 SHAPE_POLY_SET polyshape = aPolygons->CloneDropTriangulation();
1690
1691 if( aTraceMode != FILLED )
1692 {
1694 }
1695
1696 std::vector<VECTOR2I> cornerList;
1697
1698 for( int cnt = 0; cnt < polyshape.OutlineCount(); ++cnt )
1699 {
1700 SHAPE_LINE_CHAIN& poly = polyshape.Outline( cnt );
1701
1702 cornerList.clear();
1703
1704 for( int ii = 0; ii < poly.PointCount(); ++ii )
1705 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
1706
1707 // Close polygon
1708 cornerList.push_back( cornerList[0] );
1709
1710 if( aTraceMode == SKETCH )
1711 {
1712 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &gbr_metadata );
1713 }
1714 else
1715 {
1716#ifdef GBR_USE_MACROS_FOR_CUSTOM_PAD
1718 || cornerList.size() > GBR_MACRO_FOR_CUSTOM_PAD_MAX_CORNER_COUNT )
1719 {
1720 PlotGerberRegion( cornerList, &gbr_metadata );
1721 }
1722 else
1723 {
1724 // An AM will be created. the shape must be in position 0,0 and orientation 0
1725 // to be able to reuse the same AM for pads having the same shape
1726 for( size_t ii = 0; ii < cornerList.size(); ii++ )
1727 {
1728 cornerList[ii] -= aPadPos;
1729 RotatePoint( cornerList[ii], -aOrient );
1730 }
1731
1732 VECTOR2D pos_dev = userToDeviceCoordinates( aPadPos );
1733 selectAperture( cornerList, aOrient, APERTURE::AM_FREE_POLYGON,
1734 gbr_metadata.GetApertureAttrib() );
1735 formatNetAttribute( &gbr_metadata.m_NetlistMetadata );
1736
1737 emitDcode( pos_dev, 3 );
1738 }
1739#else
1740 PlotGerberRegion( cornerList, &gbr_metadata );
1741#endif
1742 }
1743 }
1744}
1745
1746
1747void GERBER_PLOTTER::FlashPadChamferRoundRect( const VECTOR2I& aShapePos, const VECTOR2I& aPadSize,
1748 int aCornerRadius, double aChamferRatio,
1749 int aChamferPositions, const EDA_ANGLE& aPadOrient,
1750 OUTLINE_MODE aPlotMode, void* aData )
1751
1752{
1753 GBR_METADATA gbr_metadata;
1754
1755 if( aData )
1756 gbr_metadata = *static_cast<GBR_METADATA*>( aData );
1757
1758 VECTOR2I pos_device = userToDeviceCoordinates( aShapePos );
1759 SHAPE_POLY_SET outline;
1760 std::vector<VECTOR2I> cornerList;
1761
1762 bool hasRoundedCorner = aCornerRadius != 0 && aChamferPositions != 15;
1763
1764#ifdef GBR_USE_MACROS_FOR_CHAMFERED_RECT
1765 // Sketch mode or round rect shape or Apert Macros disabled
1766 if( aPlotMode != FILLED || hasRoundedCorner || m_gerberDisableApertMacros )
1767#endif
1768 {
1769 TransformRoundChamferedRectToPolygon( outline, aShapePos, aPadSize, aPadOrient,
1770 aCornerRadius, aChamferRatio, aChamferPositions, 0,
1772
1773 // Build the corner list
1774 const SHAPE_LINE_CHAIN& corners = outline.Outline(0);
1775
1776 for( int ii = 0; ii < corners.PointCount(); ii++ )
1777 cornerList.emplace_back( corners.CPoint( ii ).x, corners.CPoint( ii ).y );
1778
1779 // Close the polygon
1780 cornerList.push_back( cornerList[0] );
1781
1782 if( aPlotMode == SKETCH )
1783 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &gbr_metadata );
1784 else
1785 {
1786#ifdef GBR_USE_MACROS_FOR_CHAMFERED_ROUND_RECT
1788 {
1789 PlotGerberRegion( cornerList, &gbr_metadata );
1790 }
1791 else
1792 {
1793 // An AM will be created. the shape must be in position 0,0 and orientation 0
1794 // to be able to reuse the same AM for pads having the same shape
1795 for( size_t ii = 0; ii < cornerList.size(); ii++ )
1796 {
1797 cornerList[ii] -= aShapePos;
1798 RotatePoint( cornerList[ii], -aPadOrient );
1799 }
1800
1801 selectAperture( cornerList, aPadOrient, APERTURE::AM_FREE_POLYGON,
1802 gbr_metadata.GetApertureAttrib() );
1803 formatNetAttribute( &gbr_metadata.m_NetlistMetadata );
1804
1805 emitDcode( pos_device, 3 );
1806 }
1807#else
1808 PlotGerberRegion( cornerList, &gbr_metadata );
1809#endif
1810 }
1811
1812 return;
1813 }
1814
1815 // Build the chamfered polygon (4 to 8 corners )
1816 TransformRoundChamferedRectToPolygon( outline, VECTOR2I( 0, 0 ), aPadSize, ANGLE_0, 0,
1817 aChamferRatio, aChamferPositions, 0,
1819
1820 // Build the corner list
1821 const SHAPE_LINE_CHAIN& corners = outline.Outline(0);
1822
1823 // Generate the polygon (4 to 8 corners )
1824 for( int ii = 0; ii < corners.PointCount(); ii++ )
1825 cornerList.emplace_back( corners.CPoint( ii ).x, corners.CPoint( ii ).y );
1826
1827 switch( cornerList.size() )
1828 {
1829 case 4:
1831 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE4P,
1832 gbr_metadata.GetApertureAttrib() );
1833 break;
1834
1835 case 5:
1837 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE5P,
1838 gbr_metadata.GetApertureAttrib() );
1839 break;
1840
1841 case 6:
1843 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE6P,
1844 gbr_metadata.GetApertureAttrib() );
1845 break;
1846
1847 case 7:
1849 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE7P,
1850 gbr_metadata.GetApertureAttrib() );
1851 break;
1852
1853 case 8:
1855 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE8P,
1856 gbr_metadata.GetApertureAttrib() );
1857 break;
1858
1859 default:
1860 wxLogMessage( wxS( "FlashPadChamferRoundRect(): Unexpected number of corners (%d)" ),
1861 (int)cornerList.size() );
1862 break;
1863 }
1864
1865 formatNetAttribute( &gbr_metadata.m_NetlistMetadata );
1866
1867 emitDcode( pos_device, 3 );
1868}
1869
1870
1871void GERBER_PLOTTER::FlashPadTrapez( const VECTOR2I& aPadPos, const VECTOR2I* aCorners,
1872 const EDA_ANGLE& aPadOrient, OUTLINE_MODE aTraceMode,
1873 void* aData )
1874
1875{
1876 // polygon corners list
1877 std::vector<VECTOR2I> cornerList = { aCorners[0], aCorners[1], aCorners[2], aCorners[3] };
1878
1879 // Draw the polygon and fill the interior as required
1880 for( unsigned ii = 0; ii < 4; ii++ )
1881 {
1882 RotatePoint( cornerList[ii], aPadOrient );
1883 cornerList[ii] += aPadPos;
1884 }
1885
1886 // Close the polygon
1887 cornerList.push_back( cornerList[0] );
1888
1889 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1890 GBR_METADATA metadata;
1891
1892 if( gbr_metadata )
1893 metadata = *gbr_metadata;
1894
1895 if( aTraceMode == SKETCH )
1896 {
1897 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &metadata );
1898 return;
1899 }
1900
1901 // Plot a filled polygon:
1902 #ifdef GBR_USE_MACROS_FOR_TRAPEZOID
1904 #endif
1905 {
1907 VECTOR2D pos_dev = userToDeviceCoordinates( aPadPos );
1908 // polygon corners list
1909 std::vector<VECTOR2I> corners = { aCorners[0], aCorners[1], aCorners[2], aCorners[3] };
1910 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1911 selectAperture( corners, aPadOrient, APERTURE::APER_MACRO_OUTLINE4P, aperture_attrib );
1912
1913 if( gbr_metadata )
1914 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1915
1916 emitDcode( pos_dev, 3 );
1917 return;
1918 }
1919
1920 PlotGerberRegion( cornerList, &metadata );
1921}
1922
1923
1924void GERBER_PLOTTER::FlashRegularPolygon( const VECTOR2I& aShapePos, int aDiameter,
1925 int aCornerCount, const EDA_ANGLE& aOrient,
1926 OUTLINE_MODE aTraceMode, void* aData )
1927{
1928 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1929
1930 GBR_METADATA metadata;
1931
1932 if( gbr_metadata )
1933 metadata = *gbr_metadata;
1934
1935 if( aTraceMode == SKETCH )
1936 {
1937 // Build the polygon:
1938 std::vector<VECTOR2I> cornerList;
1939
1940 EDA_ANGLE angle_delta = ANGLE_360 / aCornerCount;
1941
1942 for( int ii = 0; ii < aCornerCount; ii++ )
1943 {
1944 EDA_ANGLE rot = aOrient + ( angle_delta * ii );
1945 VECTOR2I vertice( aDiameter / 2, 0 );
1946
1947 RotatePoint( vertice, rot );
1948 vertice += aShapePos;
1949 cornerList.push_back( vertice );
1950 }
1951
1952 cornerList.push_back( cornerList[0] ); // Close the shape
1953
1954 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &gbr_metadata );
1955 }
1956 else
1957 {
1958 VECTOR2D pos_dev = userToDeviceCoordinates( aShapePos );
1959 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1960
1961 APERTURE::APERTURE_TYPE apert_type =
1963 selectAperture( aDiameter, aOrient, apert_type, aperture_attrib );
1964
1965 if( gbr_metadata )
1966 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1967
1968 emitDcode( pos_dev, 3 );
1969 }
1970}
1971
1972
1974 const COLOR4D& aColor,
1975 const wxString& aText,
1976 const EDA_ANGLE& aOrient,
1977 const VECTOR2I& aSize,
1978 enum GR_TEXT_H_ALIGN_T aH_justify,
1979 enum GR_TEXT_V_ALIGN_T aV_justify,
1980 int aWidth,
1981 bool aItalic,
1982 bool aBold,
1983 bool aMultilineAllowed,
1984 KIFONT::FONT* aFont,
1985 void* aData )
1986{
1987 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1988
1989 if( gbr_metadata )
1990 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1991
1992 PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, aWidth,
1993 aItalic, aBold, aMultilineAllowed, aFont, aData );
1994}
1995
1996
1998{
1999 if( aPositive )
2000 fprintf( m_outputFile, "%%LPD*%%\n" );
2001 else
2002 fprintf( m_outputFile, "%%LPC*%%\n" );
2003}
2004
2005
2006bool APER_MACRO_FREEPOLY::IsSamePoly( const std::vector<VECTOR2I>& aPolygon ) const
2007{
2008 return polyCompare( m_Corners, aPolygon );
2009}
2010
2011
2012void APER_MACRO_FREEPOLY::Format( FILE * aOutput, double aIu2GbrMacroUnit )
2013{
2014 // Write aperture header
2015 fprintf( aOutput, "%%AM%s%d*\n", AM_FREEPOLY_BASENAME, m_Id );
2016 fprintf( aOutput, "4,1,%d,", (int)m_Corners.size() );
2017
2018 // Insert a newline after curr_line_count_max coordinates.
2019 int curr_line_corner_count = 0;
2020 const int curr_line_count_max = 20; // <= 0 to disable newlines
2021
2022 for( size_t ii = 0; ii <= m_Corners.size(); ii++ )
2023 {
2024 int jj = ii;
2025
2026 if( ii >= m_Corners.size() )
2027 jj = 0;
2028
2029 // Note: parameter values are always mm or inches
2030 fprintf( aOutput, "%#f,%#f,",
2031 m_Corners[jj].x * aIu2GbrMacroUnit, -m_Corners[jj].y * aIu2GbrMacroUnit );
2032
2033 if( curr_line_count_max >= 0 && ++curr_line_corner_count >= curr_line_count_max )
2034 {
2035 fprintf( aOutput, "\n" );
2036 curr_line_corner_count = 0;
2037 }
2038 }
2039
2040 // output rotation parameter
2041 fputs( "$1*%\n", aOutput );
2042}
2043
2044
2045void APER_MACRO_FREEPOLY_LIST::Format( FILE * aOutput, double aIu2GbrMacroUnit )
2046{
2047 for( int idx = 0; idx < AmCount(); idx++ )
2048 m_AMList[idx].Format( aOutput, aIu2GbrMacroUnit );
2049}
2050
2051
2052void APER_MACRO_FREEPOLY_LIST::Append( const std::vector<VECTOR2I>& aPolygon )
2053{
2054 m_AMList.emplace_back( aPolygon, AmCount() );
2055}
2056
2057
2058int APER_MACRO_FREEPOLY_LIST::FindAm( const std::vector<VECTOR2I>& aPolygon ) const
2059{
2060 for( int idx = 0; idx < AmCount(); idx++ )
2061 {
2062 if( m_AMList[idx].IsSamePoly( aPolygon ) )
2063 return idx;
2064 }
2065
2066 return -1;
2067}
#define GBR_MACRO_FOR_CUSTOM_PAD_MAX_CORNER_COUNT
static bool polyCompare(const std::vector< VECTOR2I > &aPolygon, const std::vector< VECTOR2I > &aTestPolygon)
#define AM_FREEPOLY_BASENAME
wxString GetBuildVersion()
Get the full KiCad version string.
EDA_ANGLE m_Rotation
std::vector< VECTOR2I > m_Corners
APERTURE_TYPE m_Type
std::vector< APER_MACRO_FREEPOLY > m_AMList
void Append(const std::vector< VECTOR2I > &aPolygon)
append a new APER_MACRO_FREEPOLY containing the polygon aPolygon to the current list
int FindAm(const std::vector< VECTOR2I > &aPolygon) const
void Format(FILE *aOutput, double aIu2GbrMacroUnit)
print the aperture macro list to aOutput
void Format(FILE *aOutput, double aIu2GbrMacroUnit)
print the aperture macro definition to aOutput
bool IsSamePoly(const std::vector< VECTOR2I > &aPolygon) const
std::vector< VECTOR2I > m_Corners
EDA_ANGLE Normalize()
Definition: eda_angle.h:249
double Sin() const
Definition: eda_angle.h:206
double AsDegrees() const
Definition: eda_angle.h:149
bool IsCardinal() const
Definition: eda_angle.cpp:49
bool IsCardinal90() const
Definition: eda_angle.cpp:63
double Cos() const
Definition: eda_angle.h:221
VECTOR2I getCenter() const
Definition: eda_shape.cpp:444
SHAPE_T GetShape() const
Definition: eda_shape.h:113
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:145
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:120
int GetWidth() const
Definition: eda_shape.h:109
static std::string FormatAttribute(GBR_APERTURE_ATTRIB aAttribute, bool aUseX1StructuredComment)
Metadata which can be added in a gerber file as attribute in X2 format.
Definition: gbr_metadata.h:205
GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB GetApertureAttrib()
Definition: gbr_metadata.h:214
GBR_NETLIST_METADATA m_NetlistMetadata
An item to handle object attribute.
Definition: gbr_metadata.h:262
GBR_APERTURE_METADATA m_ApertureMetadata
An item to handle aperture attribute.
Definition: gbr_metadata.h:257
Information which can be added in a gerber file as attribute of an object.
wxString m_ExtraData
a string to print after TO object attributes, if not empty it is printed "as this"
virtual void Circle(const VECTOR2I &pos, int diametre, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
std::string m_objectAttributesDictionary
virtual void Arc(const VECTOR2I &aCenter, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aEndAngle, int aRadius, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH) override
Generic fallback: arc rendered as a polyline.
virtual void SetGerberCoordinatesFormat(int aResolution, bool aUseInches=false) override
Selection of Gerber units and resolution (number of digits in mantissa).
virtual void ThickCircle(const VECTOR2I &pos, int diametre, int width, OUTLINE_MODE tracemode, void *aData) override
void ClearAllAttributes()
Remove (clear) all attributes from object attributes dictionary (TO.
void selectAperture(const VECTOR2I &aSize, int aRadius, const EDA_ANGLE &aRotation, APERTURE::APERTURE_TYPE aType, int aApertureAttribute)
Pick an existing aperture or create a new one, matching the size, type and attributes.
virtual void FlashPadCustom(const VECTOR2I &aPadPos, const VECTOR2I &aSize, const EDA_ANGLE &aPadOrient, SHAPE_POLY_SET *aPolygons, OUTLINE_MODE aTraceMode, void *aData) override
void PlotGerberRegion(const std::vector< VECTOR2I > &aCornerList, void *aData=nullptr)
Plot a Gerber region: similar to PlotPoly but plot only filled polygon, and add the TA....
int GetOrCreateAperture(const VECTOR2I &aSize, int aRadius, const EDA_ANGLE &aRotation, APERTURE::APERTURE_TYPE aType, int aApertureAttribute)
virtual void PenTo(const VECTOR2I &pos, char plume) override
Moveto/lineto primitive, moves the 'pen' to the specified direction.
void emitDcode(const VECTOR2D &pt, int dcode)
Emit a D-Code record, using proper conversions to format a leading zero omitted gerber coordinate.
APER_MACRO_FREEPOLY_LIST m_am_freepoly_list
void plotArc(const VECTOR2I &aCenter, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aEndAngle, int aRadius, bool aPlotInRegion)
Plot a Gerber arc.
virtual void Rect(const VECTOR2I &p1, const VECTOR2I &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
virtual void FlashPadCircle(const VECTOR2I &pos, int diametre, OUTLINE_MODE trace_mode, void *aData) override
Filled circular flashes are stored as apertures.
std::vector< APERTURE > m_apertures
bool m_hasApertureChamferedRect
virtual void FlashPadTrapez(const VECTOR2I &aPadPos, const VECTOR2I *aCorners, const EDA_ANGLE &aPadOrient, OUTLINE_MODE aTraceMode, void *aData) override
Flash a trapezoidal pad.
bool m_hasApertureOutline4P
virtual void SetCurrentLineWidth(int aLineWidth, void *aData=nullptr) override
Set the line width for the next drawing.
void writeApertureList()
Generate the table of D codes.
void plotRoundRectAsRegion(const VECTOR2I &aRectCenter, const VECTOR2I &aSize, int aCornerRadius, const EDA_ANGLE &aOrient)
Plot a round rect (a round rect shape in fact) as a Gerber region using lines and arcs for corners.
virtual void FlashPadRect(const VECTOR2I &aPadPos, const VECTOR2I &aSize, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
void FlashPadChamferRoundRect(const VECTOR2I &aShapePos, const VECTOR2I &aPadSize, int aCornerRadius, double aChamferRatio, int aChamferPositions, const EDA_ANGLE &aPadOrient, OUTLINE_MODE aPlotMode, void *aData)
Flash a chamfered round rect pad.
wxString m_workFilename
bool m_hasApertureRoundRect
bool m_gerberDisableApertMacros
virtual void Text(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const EDA_ANGLE &aOrient, const VECTOR2I &aSize, enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, KIFONT::FONT *aFont=nullptr, void *aData=nullptr) override
Draw text with the plotter.
virtual void SetViewport(const VECTOR2I &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
virtual bool EndPlot() override
void formatNetAttribute(GBR_NETLIST_METADATA *aData)
Print a Gerber net attribute object record.
virtual void SetLayerPolarity(bool aPositive) override
Change the plot polarity and begin a new layer.
virtual void FlashPadOval(const VECTOR2I &aPadPos, const VECTOR2I &aSize, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
virtual void PlotPoly(const std::vector< VECTOR2I > &aCornerList, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=nullptr) override
Gerber polygon: they can (and should) be filled with the appropriate G36/G37 sequence.
virtual void FlashPadRoundRect(const VECTOR2I &aPadPos, const VECTOR2I &aSize, int aCornerRadius, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
virtual void EndBlock(void *aData) override
Define the end of a group of drawing items the group is started by StartBlock().
virtual void ThickRect(const VECTOR2I &p1, const VECTOR2I &p2, int width, OUTLINE_MODE tracemode, void *aData) override
virtual void ThickSegment(const VECTOR2I &start, const VECTOR2I &end, int width, OUTLINE_MODE tracemode, void *aData) override
void clearNetAttribute()
Clear a Gerber net attribute record (clear object attribute dictionary) and output the clear object a...
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 FlashRegularPolygon(const VECTOR2I &aShapePos, int aDiameter, int aCornerCount, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
Flash a regular polygon.
virtual bool StartPlot(const wxString &pageNumber) override
Write GERBER header to file initialize global variable g_Plot_PlotOutputFile.
virtual void ThickArc(const VECTOR2I &aCentre, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aWidth, OUTLINE_MODE aTraceMode, void *aData) override
virtual void FilledCircle(const VECTOR2I &pos, int diametre, OUTLINE_MODE tracemode, void *aData) override
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
int GetDefaultPenWidth() const
void SetDefaultPenWidth(int aWidth)
wxArrayString m_headerExtraLines
Definition: plotter.h:648
bool m_plotMirror
Definition: plotter.h:627
static const int USE_DEFAULT_LINE_WIDTH
Definition: plotter.h:114
void MoveTo(const VECTOR2I &pos)
Definition: plotter.h:247
void FinishTo(const VECTOR2I &pos)
Definition: plotter.h:257
bool m_yaxisReversed
Definition: plotter.h:630
double m_iuPerDeviceUnit
Definition: plotter.h:624
VECTOR2I m_plotOffset
Definition: plotter.h:626
virtual VECTOR2D userToDeviceCoordinates(const VECTOR2I &aCoordinate)
Modify coordinates according to the orientation, scale factor, and offsets trace.
Definition: plotter.cpp:90
virtual void Text(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const EDA_ANGLE &aOrient, const VECTOR2I &aSize, enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify, int aPenWidth, bool aItalic, bool aBold, bool aMultilineAllowed, KIFONT::FONT *aFont, void *aData=nullptr)
Draw text with the plotter.
Definition: plotter.cpp:714
VECTOR2I m_paperSize
Definition: plotter.h:646
void sketchOval(const VECTOR2I &aPos, const VECTOR2I &aSize, const EDA_ANGLE &aOrient, int aWidth)
Definition: plotter.cpp:496
int GetPlotterArcHighDef() const
Definition: plotter.h:213
char m_penState
Definition: plotter.h:639
wxString m_creator
Definition: plotter.h:642
int m_currentPenWidth
Definition: plotter.h:638
double m_plotScale
Plot scale - chosen by the user (even implicitly with 'fit in a4')
Definition: plotter.h:616
FILE * m_outputFile
Output file.
Definition: plotter.h:633
void LineTo(const VECTOR2I &pos)
Definition: plotter.h:252
void PenFinish()
Definition: plotter.h:263
static const int DO_NOT_SET_LINE_WIDTH
Definition: plotter.h:113
RENDER_SETTINGS * m_renderSettings
Definition: plotter.h:650
void segmentAsOval(const VECTOR2I &start, const VECTOR2I &end, int width, OUTLINE_MODE tracemode)
Convert a thick segment and plot it as an oval.
Definition: plotter.cpp:481
double m_IUsPerDecimil
Definition: plotter.h:622
virtual int GetCurrentLineWidth() const
Definition: plotter.h:153
const VECTOR2I & GetArcMid() const
Definition: shape_arc.h:114
bool IsClockwise() const
Definition: shape_arc.cpp:366
const VECTOR2I & GetP1() const
Definition: shape_arc.h:113
VECTOR2I GetCenter() const
Definition: shape_arc.cpp:433
const VECTOR2I & GetP0() const
Definition: shape_arc.h:112
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const SHAPE_ARC & Arc(size_t aArc) const
bool IsClosed() const override
int PointCount() const
Return the number of points (vertices) in this line chain.
ssize_t ArcIndex(size_t aSegment) const
Return the arc index for the given segment index.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
const std::vector< VECTOR2I > & CPoints() const
Represent a set of closed polygons.
SHAPE_LINE_CHAIN & Outline(int aIndex)
int OutlineCount() const
Return the number of vertices in a given outline/hole.
SHAPE_POLY_SET CloneDropTriangulation() const
Creates a new empty polygon in the set and returns its index.
void TransformRoundChamferedRectToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aPosition, const VECTOR2I &aSize, const EDA_ANGLE &aRotation, int aCornerRadius, double aChamferRatio, int aChamferCorners, int aInflate, int aError, ERROR_LOC aErrorLoc)
Convert a rectangle with rounded corners and/or chamfered corners to a polygon.
static constexpr EDA_ANGLE & ANGLE_180
Definition: eda_angle.h:416
static constexpr EDA_ANGLE & ANGLE_360
Definition: eda_angle.h:418
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:414
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:412
static constexpr EDA_ANGLE & ANGLE_270
Definition: eda_angle.h:417
FILL_T
Definition: eda_shape.h:54
@ FILLED_SHAPE
bool FormatNetAttribute(std::string &aPrintedText, std::string &aLastNetAttributes, const GBR_NETLIST_METADATA *aData, bool &aClearPreviousAttributes, bool aUseX1StructuredComment)
Generate the string to set a net attribute for a graphic object to print to a gerber file.
Handle special data (items attributes) during plot.
specialized plotter for GERBER files format
#define APER_MACRO_OUTLINE6P_NAME
#define APER_MACRO_OUTLINE4P_NAME
#define APER_MACRO_OUTLINE4P_HEADER
#define APER_MACRO_OUTLINE6P_HEADER
#define APER_MACRO_ROT_RECT_HEADER
#define APER_MACRO_OUTLINE8P_HEADER
#define APER_MACRO_SHAPE_OVAL_HEADER
#define APER_MACRO_ROT_RECT_NAME
#define APER_MACRO_SHAPE_OVAL_NAME
#define APER_MACRO_OUTLINE5P_HEADER
#define APER_MACRO_OUTLINE5P_NAME
#define APER_MACRO_ROUNDRECT_HEADER
#define APER_MACRO_OUTLINE8P_NAME
#define APER_MACRO_OUTLINE7P_HEADER
#define APER_MACRO_ROUNDRECT_NAME
#define APER_MACRO_OUTLINE7P_NAME
@ ERROR_INSIDE
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:401
OUTLINE_MODE
Definition: outline_mode.h:25
@ SKETCH
Definition: outline_mode.h:26
@ FILLED
Definition: outline_mode.h:27
Plotting engine (Gerber)
GR_TEXT_H_ALIGN_T
GR_TEXT_V_ALIGN_T
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183
double EuclideanNorm(const VECTOR2I &vector)
Definition: trigo.h:129
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618