KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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#include <cstdio>
33
34#include <build_version.h>
35
38
39#include <gbr_metadata.h>
40
41
42// if GBR_USE_MACROS is defined, pads having a shape that is not a Gerber primitive
43// will use a macro when possible
44// Old code will be removed only after many tests
45//
46// Note also: setting m_gerberDisableApertMacros to true disable all aperture macros
47// in Gerber files
48//
49#define GBR_USE_MACROS_FOR_CHAMFERED_ROUND_RECT
50#define GBR_USE_MACROS_FOR_CHAMFERED_RECT
51#define GBR_USE_MACROS_FOR_ROUNDRECT
52#define GBR_USE_MACROS_FOR_TRAPEZOID
53#define GBR_USE_MACROS_FOR_ROTATED_OVAL
54#define GBR_USE_MACROS_FOR_ROTATED_RECT
55#define GBR_USE_MACROS_FOR_CUSTOM_PAD
56
57// max count of corners to create a aperture macro for a custom shape.
58// provided just in case a aperture macro type free polygon creates issues
59// when the number of corners is too high.
60// (1 corner = up to 24 chars)
61// Gerber doc say max corners 5000. We use a slightly smaller value.
62// if a custom shape needs more than GBR_MACRO_FOR_CUSTOM_PAD_MAX_CORNER_COUNT, it
63// will be plot using a region.
64#define GBR_MACRO_FOR_CUSTOM_PAD_MAX_CORNER_COUNT 4990
65#define AM_FREEPOLY_BASENAME "FreePoly"
66
67
68// A helper function to compare 2 polygons: polygons are similar if they have the same
69// number of vertices and each vertex coordinate are similar, i.e. if the difference
70// between coordinates is small ( <= margin to accept rounding issues coming from polygon
71// geometric transforms like rotation
72static bool polyCompare( const std::vector<VECTOR2I>& aPolygon,
73 const std::vector<VECTOR2I>& aTestPolygon )
74{
75 // fast test: polygon sizes must be the same:
76 if( aTestPolygon.size() != aPolygon.size() )
77 return false;
78
79 const int margin = 2;
80
81 for( size_t jj = 0; jj < aPolygon.size(); jj++ )
82 {
83 if( std::abs( aPolygon[jj].x - aTestPolygon[jj].x ) > margin ||
84 std::abs( aPolygon[jj].y - aTestPolygon[jj].y ) > margin )
85 return false;
86 }
87
88 return true;
89}
90
91
93{
94 workFile = nullptr;
95 finalFile = nullptr;
98
99 // number of digits after the point (number of digits of the mantissa
100 // Be careful: the Gerber coordinates are stored in an integer
101 // so 6 digits (inches) or 5 digits (mm) is a good value
102 // To avoid overflow, 7 digits (inches) or 6 digits is a max.
103 // with lower values than 6 digits (inches) or 5 digits (mm),
104 // Creating self-intersecting polygons from non-intersecting polygons
105 // happen easily.
106 m_gerberUnitInch = false;
107 m_gerberUnitFmt = 6;
108 m_useX2format = true;
109 m_useNetAttributes = true;
111
112 m_hasApertureRoundRect = false; // true is at least one round rect aperture is in use
113 m_hasApertureRotOval = false; // true is at least one oval rotated aperture is in use
114 m_hasApertureRotRect = false; // true is at least one rect. rotated aperture is in use
115 m_hasApertureOutline4P = false; // true is at least one rotated rect or trapezoid pad
116 // aperture is in use
117 m_hasApertureChamferedRect = false; // true is at least one chamfered rect
118 // (no rounded corner) is in use
119}
120
121
122void GERBER_PLOTTER::SetViewport( const VECTOR2I& aOffset, double aIusPerDecimil,
123 double aScale, bool aMirror )
124{
125 wxASSERT( aMirror == false );
126 m_plotMirror = false;
127 m_plotOffset = aOffset;
128 wxASSERT( aScale == 1 ); // aScale parameter is not used in Gerber
129 m_plotScale = 1; // Plot scale is *always* 1.0
130
131 m_IUsPerDecimil = aIusPerDecimil;
132
133 // gives now a default value to iuPerDeviceUnit (because the units of the caller is now known)
134 // which could be modified later by calling SetGerberCoordinatesFormat()
135 m_iuPerDeviceUnit = pow( 10.0, m_gerberUnitFmt ) / ( m_IUsPerDecimil * 10000.0 );
136
137 // We don't handle the filmbox, and it's more useful to keep the
138 // origin at the origin
139 m_paperSize.x = 0;
140 m_paperSize.y = 0;
141}
142
143
144void GERBER_PLOTTER::SetGerberCoordinatesFormat( int aResolution, bool aUseInches )
145{
146 m_gerberUnitInch = aUseInches;
147 m_gerberUnitFmt = aResolution;
148
149 m_iuPerDeviceUnit = pow( 10.0, m_gerberUnitFmt ) / ( m_IUsPerDecimil * 10000.0 );
150
151 if( ! m_gerberUnitInch )
152 m_iuPerDeviceUnit *= 25.4; // gerber output in mm
153}
154
155
156void GERBER_PLOTTER::emitDcode( const VECTOR2D& pt, int dcode )
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
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
576 bool useX1StructuredComment = false;
577
578 if( !m_useX2format )
579 useX1StructuredComment = true;
580
581 // Init
582 for( APERTURE& tool : m_apertures )
583 {
584 // aperture sizes are in inch or mm, regardless the
585 // coordinates format
586 double fscale = 0.0001 * m_plotScale / m_IUsPerDecimil; // inches
587
588 if(! m_gerberUnitInch )
589 fscale *= 25.4; // size in mm
590
591 int attribute = tool.m_ApertureAttribute;
592
593 if( attribute != m_apertureAttribute )
594 {
597 useX1StructuredComment ).c_str(), m_outputFile );
598 }
599
600 fprintf( m_outputFile, "%%ADD%d", tool.m_DCode );
601
602 /* Please note: the Gerber specs for mass parameters say that
603 exponential syntax is *not* allowed and the decimal point should
604 also be always inserted. So the %g format is ruled out, but %f is fine
605 (the # modifier forces the decimal point). Sadly the %f formatter
606 can't remove trailing zeros but that's not a problem, since nothing
607 forbid it (the file is only slightly longer) */
608
609 switch( tool.m_Type )
610 {
612 fprintf( m_outputFile, "C,%#f*%%\n", tool.GetDiameter() * fscale );
613 break;
614
616 fprintf( m_outputFile, "R,%#fX%#f*%%\n", tool.m_Size.x * fscale,
617 tool.m_Size.y * fscale );
618 break;
619
621 fprintf( m_outputFile, "C,%#f*%%\n", tool.m_Size.x * fscale );
622 break;
623
625 fprintf( m_outputFile, "O,%#fX%#f*%%\n", tool.m_Size.x * fscale,
626 tool.m_Size.y * fscale );
627 break;
628
640 fprintf( m_outputFile, "P,%#fX%dX%#f*%%\n", tool.GetDiameter() * fscale,
641 tool.GetRegPolyVerticeCount(), tool.GetRotation().AsDegrees() );
642 break;
643
644 case APERTURE::AM_ROUND_RECT: // Aperture macro for round rect pads
645 {
646 // The aperture macro needs coordinates of the centers of the 4 corners
647 std::vector<VECTOR2I> corners;
648 VECTOR2I half_size( tool.m_Size.x/2-tool.m_Radius, tool.m_Size.y/2-tool.m_Radius );
649
650 corners.emplace_back( -half_size.x, -half_size.y );
651 corners.emplace_back( half_size.x, -half_size.y );
652 corners.emplace_back( half_size.x, half_size.y );
653 corners.emplace_back( -half_size.x, half_size.y );
654
655 // Rotate the corner coordinates:
656 for( int ii = 0; ii < 4; ii++ )
657 RotatePoint( corners[ii], -tool.m_Rotation );
658
659 fprintf( m_outputFile, "%s,%#fX", APER_MACRO_ROUNDRECT_NAME, tool.m_Radius * fscale );
660
661 // Add each corner
662 for( int ii = 0; ii < 4; ii++ )
663 {
664 fprintf( m_outputFile, "%#fX%#fX", corners[ii].x * fscale, corners[ii].y * fscale );
665 }
666
667 fprintf( m_outputFile, "0*%%\n" );
668 }
669 break;
670
671 case APERTURE::AM_ROT_RECT: // Aperture macro for rotated rect pads
672 fprintf( m_outputFile, "%s,%#fX%#fX%#f*%%\n", APER_MACRO_ROT_RECT_NAME,
673 tool.m_Size.x * fscale, tool.m_Size.y * fscale, tool.m_Rotation.AsDegrees() );
674 break;
675
676 case APERTURE::APER_MACRO_OUTLINE4P: // Aperture macro for trapezoid pads
677 case APERTURE::APER_MACRO_OUTLINE5P: // Aperture macro for chamfered rect pads
678 case APERTURE::APER_MACRO_OUTLINE6P: // Aperture macro for chamfered rect pads
679 case APERTURE::APER_MACRO_OUTLINE7P: // Aperture macro for chamfered rect pads
680 case APERTURE::APER_MACRO_OUTLINE8P: // Aperture macro for chamfered rect pads
681 switch( tool.m_Type )
682 {
684 fprintf( m_outputFile, "%s,", APER_MACRO_OUTLINE4P_NAME );
685 break;
687 fprintf( m_outputFile, "%s,", APER_MACRO_OUTLINE5P_NAME );
688 break;
690 fprintf( m_outputFile, "%s,", APER_MACRO_OUTLINE6P_NAME );
691 break;
693 fprintf( m_outputFile, "%s,", APER_MACRO_OUTLINE7P_NAME );
694 break;
696 fprintf( m_outputFile, "%s,", APER_MACRO_OUTLINE8P_NAME );
697 break;
698 default:
699 break;
700 }
701
702
703 // Output all corners (should be 4 to 8 corners)
704 // Remember: the Y coordinate must be negated, due to the fact in Pcbnew
705 // the Y axis is from top to bottom
706 for( size_t ii = 0; ii < tool.m_Corners.size(); ii++ )
707 {
708 fprintf( m_outputFile, "%#fX%#fX", tool.m_Corners[ii].x * fscale,
709 -tool.m_Corners[ii].y * fscale );
710 }
711
712 // close outline and output rotation
713 fprintf( m_outputFile, "%#f*%%\n", tool.m_Rotation.AsDegrees() );
714 break;
715
716 case APERTURE::AM_ROTATED_OVAL: // Aperture macro for rotated oval pads
717 // (not rotated is a primitive)
718 // m_Size.x = full length; m_Size.y = width, and the macro aperture expects
719 // the position of ends
720 {
721 // the seg_len is the distance between the 2 circle centers
722 int seg_len = tool.m_Size.x - tool.m_Size.y;
723 // Center of the circle on the segment start point:
724 VECTOR2I start( seg_len/2, 0 );
725 // Center of the circle on the segment end point:
726 VECTOR2I end( - seg_len/2, 0 );
727
728 RotatePoint( start, tool.m_Rotation );
729 RotatePoint( end, tool.m_Rotation );
730
731 fprintf( m_outputFile, "%s,%#fX%#fX%#fX%#fX%#fX0*%%\n", APER_MACRO_SHAPE_OVAL_NAME,
732 tool.m_Size.y * fscale, // width
733 start.x * fscale, -start.y * fscale, // X,Y corner start pos
734 end.x * fscale, -end.y * fscale ); // X,Y cornerend pos
735 }
736 break;
737
739 {
740 // Find the aperture macro name in the list of aperture macro
741 // created on the fly for this polygon:
742 int idx = m_am_freepoly_list.FindAm( tool.m_Corners );
743
744 // Write DCODE id ( "%ADDxx" is already in buffer) and rotation
745 // the full line is something like :%ADD12FreePoly1,45.000000*%
746 fprintf( m_outputFile, "%s%d,%#f*%%\n", AM_FREEPOLY_BASENAME, idx,
747 tool.m_Rotation.AsDegrees() );
748 break;
749 }
750 }
751
752 m_apertureAttribute = attribute;
753
754 // Currently reset the aperture attribute. Perhaps a better optimization
755 // is to store the last attribute
756 if( attribute )
757 {
758 if( m_useX2format )
759 fputs( "%TD*%\n", m_outputFile );
760 else
761 fputs( "G04 #@! TD*\n", m_outputFile );
762
764 }
765
766 }
767}
768
769
770void GERBER_PLOTTER::PenTo( const VECTOR2I& aPos, char plume )
771{
772 wxASSERT( m_outputFile );
773 VECTOR2D pos_dev = userToDeviceCoordinates( aPos );
774
775 switch( plume )
776 {
777 case 'Z':
778 break;
779
780 case 'U':
781 emitDcode( pos_dev, 2 );
782 break;
783
784 case 'D':
785 emitDcode( pos_dev, 1 );
786 }
787
788 m_penState = plume;
789}
790
791
792void GERBER_PLOTTER::Rect( const VECTOR2I& p1, const VECTOR2I& p2, FILL_T fill, int width )
793{
794 std::vector<VECTOR2I> cornerList;
795
796 cornerList.reserve( 5 );
797
798 // Build corners list
799 cornerList.push_back( p1 );
800
801 VECTOR2I corner( p1.x, p2.y );
802 cornerList.push_back( corner );
803 cornerList.push_back( p2 );
804 corner.x = p2.x;
805 corner.y = p1.y;
806 cornerList.push_back( corner );
807 cornerList.push_back( p1 );
808
809 PlotPoly( cornerList, fill, width );
810}
811
812
813void GERBER_PLOTTER::Circle( const VECTOR2I& aCenter, int aDiameter, FILL_T aFill, int aWidth )
814{
815 Arc( aCenter, ANGLE_0, ANGLE_360, aDiameter / 2, aFill, aWidth );
816}
817
818
819
820void GERBER_PLOTTER::Arc( const VECTOR2I& aCenter, const EDA_ANGLE& aStartAngle,
821 const EDA_ANGLE& aEndAngle, int aRadius, FILL_T aFill, int aWidth )
822{
823 SetCurrentLineWidth( aWidth );
824
825 // aFill is not used here.
826 plotArc( aCenter, aStartAngle, aEndAngle, aRadius, false );
827}
828
829
830void GERBER_PLOTTER::plotArc( const SHAPE_ARC& aArc, bool aPlotInRegion )
831{
832 VECTOR2I start( aArc.GetP0() );
833 VECTOR2I end( aArc.GetP1() );
834 VECTOR2I center( aArc.GetCenter() );
835
836 if( !aPlotInRegion )
837 MoveTo( start);
838 else
839 LineTo( start );
840
841 VECTOR2D devEnd = userToDeviceCoordinates( end );
842
843 // devRelCenter is the position on arc center relative to the arc start, in Gerber coord.
844 // Warning: it is **not** userToDeviceCoordinates( center - start ) when the plotter
845 // has an offset.
846 VECTOR2D devRelCenter = userToDeviceCoordinates( center ) - userToDeviceCoordinates( start );
847
848 // We need to know if the arc is CW or CCW in device coordinates, so build this arc.
849 SHAPE_ARC deviceArc( userToDeviceCoordinates( start ),
851 devEnd, 0 );
852
853 fprintf( m_outputFile, "G75*\n" ); // Multiquadrant (360 degrees) mode
854
855 if( deviceArc.IsClockwise() )
856 fprintf( m_outputFile, "G02*\n" ); // Active circular interpolation, CW
857 else
858 fprintf( m_outputFile, "G03*\n" ); // Active circular interpolation, CCW
859
860 fprintf( m_outputFile, "X%dY%dI%dJ%dD01*\n",
861 KiROUND( devEnd.x ), KiROUND( devEnd.y ),
862 KiROUND( devRelCenter.x ), KiROUND( devRelCenter.y ) );
863
864 fprintf( m_outputFile, "G01*\n" ); // Back to linear interpolate (perhaps useless here).
865}
866
867
868void GERBER_PLOTTER::plotArc( const VECTOR2I& aCenter, const EDA_ANGLE& aStartAngle,
869 const EDA_ANGLE& aEndAngle, int aRadius, bool aPlotInRegion )
870{
871 VECTOR2I start, end;
872 start.x = aCenter.x + KiROUND( aRadius * aStartAngle.Cos() );
873 start.y = aCenter.y - KiROUND( aRadius * aStartAngle.Sin() );
874
875 if( !aPlotInRegion )
876 MoveTo( start );
877 else
878 LineTo( start );
879
880 end.x = aCenter.x + KiROUND( aRadius * aEndAngle.Cos() );
881 end.y = aCenter.y - KiROUND( aRadius * aEndAngle.Sin() );
882 VECTOR2D devEnd = userToDeviceCoordinates( end );
883 // devRelCenter is the position on arc center relative to the arc start, in Gerber coord.
884 VECTOR2D devRelCenter = userToDeviceCoordinates( aCenter ) - userToDeviceCoordinates( start );
885
886 fprintf( m_outputFile, "G75*\n" ); // Multiquadrant (360 degrees) mode
887
888 if( aStartAngle < aEndAngle )
889 fprintf( m_outputFile, "G03*\n" ); // Active circular interpolation, CCW
890 else
891 fprintf( m_outputFile, "G02*\n" ); // Active circular interpolation, CW
892
893 fprintf( m_outputFile, "X%dY%dI%dJ%dD01*\n",
894 KiROUND( devEnd.x ), KiROUND( devEnd.y ),
895 KiROUND( devRelCenter.x ), KiROUND( devRelCenter.y ) );
896
897 fprintf( m_outputFile, "G01*\n" ); // Back to linear interpolate (perhaps useless here).
898}
899
900
902{
903 if( aPoly.PointCount() <= 2 )
904 return;
905
906 bool clearTA_AperFunction = false; // true if a TA.AperFunction is used
907
908 if( aGbrMetadata )
909 {
910 std::string attrib = aGbrMetadata->m_ApertureMetadata.FormatAttribute( !m_useX2format );
911
912 if( !attrib.empty() )
913 {
914 fputs( attrib.c_str(), m_outputFile );
915 clearTA_AperFunction = true;
916 }
917 }
918
919 PlotPoly( aPoly, FILL_T::FILLED_SHAPE, 0 , aGbrMetadata );
920
921 // Clear the TA attribute, to avoid the next item to inherit it:
922 if( clearTA_AperFunction )
923 {
924 if( m_useX2format )
925 {
926 fputs( "%TD.AperFunction*%\n", m_outputFile );
927 }
928 else
929 {
930 fputs( "G04 #@! TD.AperFunction*\n", m_outputFile );
931 }
932 }
933}
934
935
936void GERBER_PLOTTER::PlotGerberRegion( const std::vector<VECTOR2I>& aCornerList,
937 GBR_METADATA* aGbrMetadata )
938{
939 if( aCornerList.size() <= 2 )
940 return;
941
942 bool clearTA_AperFunction = false; // true if a TA.AperFunction is used
943
944 if( aGbrMetadata )
945 {
946 std::string attrib = aGbrMetadata->m_ApertureMetadata.FormatAttribute( !m_useX2format );
947
948 if( !attrib.empty() )
949 {
950 fputs( attrib.c_str(), m_outputFile );
951 clearTA_AperFunction = true;
952 }
953 }
954
955 PlotPoly( aCornerList, FILL_T::FILLED_SHAPE, 0, aGbrMetadata );
956
957 // Clear the TA attribute, to avoid the next item to inherit it:
958 if( clearTA_AperFunction )
959 {
960 if( m_useX2format )
961 {
962 fputs( "%TD.AperFunction*%\n", m_outputFile );
963 }
964 else
965 {
966 fputs( "G04 #@! TD.AperFunction*\n", m_outputFile );
967 }
968 }
969}
970
971
973 int aWidth, GBR_METADATA* aGbrMetadata )
974{
975 // plot a filled polygon using Gerber region, therefore adding X2 attributes
976 // to the solid polygon
977 if( aWidth )
978 PlotPoly( aPoly, FILL_T::NO_FILL, aWidth, aGbrMetadata );
979
980 if( aFill != FILL_T::NO_FILL )
981 PlotGerberRegion( aPoly, aGbrMetadata );
982}
983
984
985void GERBER_PLOTTER::PlotPoly( const SHAPE_LINE_CHAIN& aPoly, FILL_T aFill, int aWidth,
986 void* aData )
987{
988 if( aPoly.CPoints().size() <= 1 )
989 return;
990
991 // Gerber format does not know filled polygons with thick outline
992 // Therefore, to plot a filled polygon with outline having a thickness,
993 // one should plot outline as thick segments
994 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
995
996 if( gbr_metadata )
997 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
998
999 if( aFill != FILL_T::NO_FILL )
1000 {
1001 fputs( "G36*\n", m_outputFile );
1002
1003 MoveTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
1004
1005 fputs( "G01*\n", m_outputFile ); // Set linear interpolation.
1006
1007 for( int ii = 1; ii < aPoly.PointCount(); ii++ )
1008 {
1009 int arcindex = aPoly.ArcIndex( ii );
1010
1011 if( arcindex < 0 )
1012 {
1014 LineTo( VECTOR2I( aPoly.CPoint( ii ) ) );
1015 }
1016 else
1017 {
1018 const SHAPE_ARC& arc = aPoly.Arc( arcindex );
1019
1020 plotArc( arc, true );
1021
1022 // skip points on arcs, since we plot the arc itself
1023 while( ii+1 < aPoly.PointCount() && arcindex == aPoly.ArcIndex( ii+1 ) )
1024 ii++;
1025 }
1026 }
1027
1028 // If the polygon is not closed, close it:
1029 if( aPoly.CPoint( 0 ) != aPoly.CPoint( -1 ) )
1030 FinishTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
1031
1032 fputs( "G37*\n", m_outputFile );
1033 }
1034
1035 if( aWidth > 0 ) // Draw the polyline/polygon outline
1036 {
1037 SetCurrentLineWidth( aWidth, gbr_metadata );
1038
1039 MoveTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
1040
1041 for( int ii = 1; ii < aPoly.PointCount(); ii++ )
1042 {
1043 int arcindex = aPoly.ArcIndex( ii );
1044
1045 if( arcindex < 0 )
1046 {
1048 LineTo( VECTOR2I( aPoly.CPoint( ii ) ) );
1049 }
1050 else
1051 {
1052 const SHAPE_ARC& arc = aPoly.Arc( arcindex );
1053
1054 plotArc( arc, true );
1055
1056 // skip points on arcs, since we plot the arc itself
1057 while( ii+1 < aPoly.PointCount() && arcindex == aPoly.ArcIndex( ii+1 ) )
1058 ii++;
1059 }
1060 }
1061
1062 // Ensure the thick outline is closed for filled polygons
1063 // (if not filled, could be only a polyline)
1064 if( ( aPoly.CPoint( 0 ) != aPoly.CPoint( -1 ) )
1065 && ( aPoly.IsClosed() || aFill != FILL_T::NO_FILL ) )
1066 LineTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
1067
1068 PenFinish();
1069 }
1070}
1071
1072void GERBER_PLOTTER::PlotPoly( const std::vector<VECTOR2I>& aCornerList, FILL_T aFill, int aWidth,
1073 void * aData )
1074{
1075 if( aCornerList.size() <= 1 )
1076 return;
1077
1078 // Gerber format does not know filled polygons with thick outline
1079 // Therefore, to plot a filled polygon with outline having a thickness,
1080 // one should plot outline as thick segments
1081 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1082
1083 if( gbr_metadata )
1084 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1085
1086 if( aFill != FILL_T::NO_FILL )
1087 {
1088 fputs( "G36*\n", m_outputFile );
1089
1090 MoveTo( aCornerList[0] );
1091 fputs( "G01*\n", m_outputFile ); // Set linear interpolation.
1092
1093 for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
1094 LineTo( aCornerList[ii] );
1095
1096 // If the polygon is not closed, close it:
1097 if( aCornerList[0] != aCornerList[aCornerList.size()-1] )
1098 FinishTo( aCornerList[0] );
1099
1100 fputs( "G37*\n", m_outputFile );
1101 }
1102
1103 if( aWidth > 0 ) // Draw the polyline/polygon outline
1104 {
1105 SetCurrentLineWidth( aWidth, gbr_metadata );
1106
1107 MoveTo( aCornerList[0] );
1108
1109 for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
1110 LineTo( aCornerList[ii] );
1111
1112 // Ensure the thick outline is closed for filled polygons
1113 // (if not filled, could be only a polyline)
1114 if( aFill != FILL_T::NO_FILL && ( aCornerList[aCornerList.size() - 1] != aCornerList[0] ) )
1115 LineTo( aCornerList[0] );
1116
1117 PenFinish();
1118 }
1119}
1120
1121
1122void GERBER_PLOTTER::ThickSegment( const VECTOR2I& start, const VECTOR2I& end, int width,
1123 OUTLINE_MODE tracemode, void* aData )
1124{
1125 if( tracemode == FILLED )
1126 {
1127 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1128 SetCurrentLineWidth( width, gbr_metadata );
1129
1130 if( gbr_metadata )
1131 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1132
1133 MoveTo( start );
1134 FinishTo( end );
1135 }
1136 else
1137 {
1139 segmentAsOval( start, end, width, tracemode );
1140 }
1141}
1142
1143void GERBER_PLOTTER::ThickArc( const VECTOR2I& aCentre, const EDA_ANGLE& aStartAngle,
1144 const EDA_ANGLE& aEndAngle, int aRadius, int aWidth,
1145 OUTLINE_MODE aTraceMode, void* aData )
1146{
1147 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1148 SetCurrentLineWidth( aWidth, gbr_metadata );
1149
1150 if( gbr_metadata )
1151 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1152
1153 if( aTraceMode == FILLED )
1154 {
1155 Arc( aCentre, aStartAngle, aEndAngle, aRadius, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1156 }
1157 else
1158 {
1160 Arc( aCentre, aStartAngle, aEndAngle, aRadius - ( aWidth - m_currentPenWidth ) / 2,
1161 FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1162 Arc( aCentre, aStartAngle, aEndAngle, aRadius + ( aWidth - m_currentPenWidth ) / 2,
1163 FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1164 }
1165}
1166
1167
1168void GERBER_PLOTTER::ThickArc( const VECTOR2I& aCentre, const VECTOR2I& aStart,
1169 const VECTOR2I& aEnd, int aWidth,
1170 OUTLINE_MODE aTraceMode, void* aData )
1171{
1172 EDA_ANGLE start_angle( aStart - aCentre );
1173 EDA_ANGLE end_angle( aEnd - aCentre );
1174
1175 if( start_angle > end_angle )
1176 {
1177 if( end_angle < ANGLE_0 )
1178 end_angle.Normalize();
1179 else
1180 start_angle = start_angle.Normalize() - ANGLE_360;
1181 }
1182
1183 int radius = (aStart - aCentre).EuclideanNorm();
1184
1185 if( !m_yaxisReversed ) // should be always the case
1186 {
1187 std::swap( end_angle, start_angle );
1188 end_angle = -end_angle;
1189 start_angle = -start_angle;
1190 }
1191
1192 ThickArc( aCentre, start_angle, end_angle, radius, aWidth, aTraceMode, aData );
1193}
1194
1195
1197 OUTLINE_MODE aTraceMode, void* aData )
1198{
1199 wxASSERT( aArcShape.GetShape() == SHAPE_T::ARC );
1200
1201 ThickArc( aArcShape.getCenter(), aArcShape.GetStart(), aArcShape.GetEnd(),
1202 aArcShape.GetWidth(), aTraceMode, aData );
1203}
1204
1205
1206void GERBER_PLOTTER::ThickRect( const VECTOR2I& p1, const VECTOR2I& p2, int width,
1207 OUTLINE_MODE tracemode, void* aData )
1208{
1209 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1210 SetCurrentLineWidth( width, gbr_metadata );
1211
1212 if( gbr_metadata )
1213 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1214
1215 if( tracemode == FILLED )
1216 {
1217 Rect( p1, p2, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1218 }
1219 else
1220 {
1222 VECTOR2I offsetp1( p1.x - ( width - m_currentPenWidth ) / 2,
1223 p1.y - (width - m_currentPenWidth) / 2 );
1224 VECTOR2I offsetp2( p2.x + ( width - m_currentPenWidth ) / 2,
1225 p2.y + (width - m_currentPenWidth) / 2 );
1226 Rect( offsetp1, offsetp2, FILL_T::NO_FILL, -1 );
1227 offsetp1.x += (width - m_currentPenWidth);
1228 offsetp1.y += (width - m_currentPenWidth);
1229 offsetp2.x -= (width - m_currentPenWidth);
1230 offsetp2.y -= (width - m_currentPenWidth);
1231 Rect( offsetp1, offsetp2, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1232 }
1233}
1234
1235
1236void GERBER_PLOTTER::ThickCircle( const VECTOR2I& pos, int diametre, int width,
1237 OUTLINE_MODE tracemode, void* aData )
1238{
1239 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1240 SetCurrentLineWidth( width, gbr_metadata );
1241
1242 if( gbr_metadata )
1243 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1244
1245 if( tracemode == FILLED )
1246 {
1247 Circle( pos, diametre, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1248 }
1249 else
1250 {
1252 Circle( pos, diametre - (width - m_currentPenWidth), FILL_T::NO_FILL,
1254 Circle( pos, diametre + (width - m_currentPenWidth), FILL_T::NO_FILL,
1256 }
1257}
1258
1259
1260void GERBER_PLOTTER::FilledCircle( const VECTOR2I& pos, int diametre,
1261 OUTLINE_MODE tracemode, void* aData )
1262{
1263 // A filled circle is a graphic item, not a pad.
1264 // So it is drawn, not flashed.
1265 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1266
1267 if( gbr_metadata )
1268 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1269
1270 if( tracemode == FILLED )
1271 {
1272 // Draw a circle of diameter = diameter/2 with a line thickness = radius,
1273 // To create a filled circle
1274 SetCurrentLineWidth( diametre/2, gbr_metadata );
1275 Circle( pos, diametre/2, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1276 }
1277 else
1278 {
1280 Circle( pos, diametre, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1281 }
1282}
1283
1284
1285void GERBER_PLOTTER::FlashPadCircle( const VECTOR2I& pos, int diametre, OUTLINE_MODE trace_mode,
1286 void* aData )
1287{
1288 VECTOR2I size( diametre, diametre );
1289 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1290
1291 if( trace_mode == SKETCH )
1292 {
1293 if( gbr_metadata )
1294 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1295
1297
1298 Circle( pos, diametre, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1299 }
1300 else
1301 {
1302 VECTOR2D pos_dev = userToDeviceCoordinates( pos );
1303
1304 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1305 selectAperture( size, 0, ANGLE_0, APERTURE::AT_CIRCLE, aperture_attrib );
1306
1307 if( gbr_metadata )
1308 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1309
1310 emitDcode( pos_dev, 3 );
1311 }
1312}
1313
1314
1315void GERBER_PLOTTER::FlashPadOval( const VECTOR2I& aPos, const VECTOR2I& aSize,
1316 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
1317{
1318 wxASSERT( m_outputFile );
1319
1320 VECTOR2I size( aSize );
1321 EDA_ANGLE orient( aOrient );
1322 orient.Normalize();
1323 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1324
1325 // Flash a vertical or horizontal shape (this is a basic aperture).
1326 if( orient.IsCardinal() && aTraceMode == FILLED )
1327 {
1328 if( orient.IsCardinal90() )
1329 std::swap( size.x, size.y );
1330
1331 VECTOR2D pos_device = userToDeviceCoordinates( aPos );
1332 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1333 selectAperture( size, 0, ANGLE_0, APERTURE::AT_OVAL, aperture_attrib );
1334
1335 if( gbr_metadata )
1336 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1337
1338 emitDcode( pos_device, 3 );
1339 }
1340 else // Plot pad as region.
1341 // Only regions and flashed items accept a object attribute TO.P for the pin name
1342 {
1343 if( aTraceMode == FILLED )
1344 {
1345 #ifdef GBR_USE_MACROS_FOR_ROTATED_OVAL
1347 #endif
1348 {
1349 m_hasApertureRotOval = true;
1350 // We are using a aperture macro that expect size.y < size.x
1351 // i.e draw a horizontal line for rotation = 0.0
1352 // size.x = length, size.y = width
1353 if( size.x < size.y )
1354 {
1355 std::swap( size.x, size.y );
1356 orient += ANGLE_90;
1357
1358 if( orient > ANGLE_180 )
1359 orient -= ANGLE_180;
1360 }
1361
1362 VECTOR2D pos_device = userToDeviceCoordinates( aPos );
1363 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1364 selectAperture( size, 0, orient, APERTURE::AM_ROTATED_OVAL, aperture_attrib );
1365
1366 if( gbr_metadata )
1367 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1368
1369 emitDcode( pos_device, 3 );
1370 return;
1371 }
1372 // Draw the oval as round rect pad with a radius = 50% min size)
1373 // In gerber file, it will be drawn as a region with arcs, and can be
1374 // detected as pads (similar to a flashed pad)
1375 FlashPadRoundRect( aPos, aSize, std::min( aSize.x, aSize.y ) / 2, orient, FILLED,
1376 aData );
1377 }
1378 else // Non filled shape: plot outlines:
1379 {
1380 if( size.x > size.y )
1381 {
1382 std::swap( size.x, size.y );
1383
1384 if( orient < ANGLE_270 )
1385 orient += ANGLE_90;
1386 else
1387 orient -= ANGLE_270;
1388 }
1389
1390 sketchOval( aPos, size, orient, -1 );
1391 }
1392 }
1393}
1394
1395
1396void GERBER_PLOTTER::FlashPadRect( const VECTOR2I& pos, const VECTOR2I& aSize,
1397 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
1398
1399{
1400 wxASSERT( m_outputFile );
1401
1402 VECTOR2I size( aSize );
1403 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1404
1405 // Horizontal / vertical rect can use a basic aperture (not a macro)
1406 // so use it for rotation n*90 deg
1407 if( aOrient.IsCardinal() )
1408 {
1409 if( aOrient.IsCardinal90() )
1410 // Build the not rotated equivalent shape:
1411 std::swap( size.x, size.y );
1412
1413 if( aTraceMode == SKETCH )
1414 {
1415 if( gbr_metadata )
1416 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1417
1419 Rect( VECTOR2I( pos.x - ( size.x / 2 ), pos.y - (size.y / 2 ) ),
1420 VECTOR2I( pos.x + ( size.x / 2 ), pos.y + (size.y / 2 ) ),
1421 FILL_T::NO_FILL, GetCurrentLineWidth() );
1422 }
1423 else
1424 {
1425 VECTOR2D pos_device = userToDeviceCoordinates( pos );
1426 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1427 selectAperture( size, 0, ANGLE_0, APERTURE::AT_RECT, aperture_attrib );
1428
1429 if( gbr_metadata )
1430 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1431
1432 emitDcode( pos_device, 3 );
1433 }
1434 }
1435 else
1436 {
1437 #ifdef GBR_USE_MACROS_FOR_ROTATED_RECT
1438 if( aTraceMode != SKETCH && !m_gerberDisableApertMacros )
1439 {
1440 m_hasApertureRotRect = true;
1441
1442 VECTOR2D pos_device = userToDeviceCoordinates( pos );
1443 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1444 selectAperture( size, 0, aOrient, APERTURE::AM_ROT_RECT, aperture_attrib );
1445
1446 if( gbr_metadata )
1447 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1448
1449 emitDcode( pos_device, 3 );
1450 }
1451 else
1452 #endif
1453 {
1454 // plot pad shape as Gerber region
1455 VECTOR2I coord[4];
1456 // coord[0] is assumed the lower left
1457 // coord[1] is assumed the upper left
1458 // coord[2] is assumed the upper right
1459 // coord[3] is assumed the lower right
1460
1461 coord[0].x = -size.x/2; // lower left
1462 coord[0].y = size.y/2;
1463 coord[1].x = -size.x/2; // upper left
1464 coord[1].y = -size.y/2;
1465 coord[2].x = size.x/2; // upper right
1466 coord[2].y = -size.y/2;
1467 coord[3].x = size.x/2; // lower right
1468 coord[3].y = size.y/2;
1469
1470 FlashPadTrapez( pos, coord, aOrient, aTraceMode, aData );
1471 }
1472 }
1473}
1474
1475void GERBER_PLOTTER::FlashPadRoundRect( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
1476 int aCornerRadius, const EDA_ANGLE& aOrient,
1477 OUTLINE_MODE aTraceMode, void* aData )
1478
1479{
1480 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1481
1482 if( aTraceMode != FILLED )
1483 {
1484 SHAPE_POLY_SET outline;
1485 TransformRoundChamferedRectToPolygon( outline, aPadPos, aSize, aOrient, aCornerRadius, 0.0,
1487
1489
1490 std::vector<VECTOR2I> cornerList;
1491 // TransformRoundRectToPolygon creates only one convex polygon
1492 SHAPE_LINE_CHAIN& poly = outline.Outline( 0 );
1493 cornerList.reserve( poly.PointCount() + 1 );
1494
1495 for( int ii = 0; ii < poly.PointCount(); ++ii )
1496 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
1497
1498 // Close polygon
1499 cornerList.push_back( cornerList[0] );
1500
1501 // plot outlines
1502 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), gbr_metadata );
1503 }
1504 else
1505 {
1506 #ifdef GBR_USE_MACROS_FOR_ROUNDRECT
1508 #endif
1509 {
1511
1512 VECTOR2D pos_dev = userToDeviceCoordinates( aPadPos );
1513 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1514 selectAperture( aSize, aCornerRadius, aOrient, APERTURE::AM_ROUND_RECT,
1515 aperture_attrib );
1516
1517 if( gbr_metadata )
1518 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1519
1520 emitDcode( pos_dev, 3 );
1521 return;
1522 }
1523
1524 // A Pad RoundRect is plotted as a Gerber region.
1525 // Initialize region metadata:
1526 bool clearTA_AperFunction = false; // true if a TA.AperFunction is used
1527
1528 if( gbr_metadata )
1529 {
1530 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1531 std::string attrib = gbr_metadata->m_ApertureMetadata.FormatAttribute( !m_useX2format );
1532
1533 if( !attrib.empty() )
1534 {
1535 fputs( attrib.c_str(), m_outputFile );
1536 clearTA_AperFunction = true;
1537 }
1538 }
1539
1540 // Plot the region using arcs in corners.
1541 plotRoundRectAsRegion( aPadPos, aSize, aCornerRadius, aOrient );
1542
1543 // Clear the TA attribute, to avoid the next item to inherit it:
1544 if( clearTA_AperFunction )
1545 {
1546 if( m_useX2format )
1547 fputs( "%TD.AperFunction*%\n", m_outputFile );
1548 else
1549 fputs( "G04 #@! TD.AperFunction*\n", m_outputFile );
1550 }
1551 }
1552}
1553
1554
1555void GERBER_PLOTTER::plotRoundRectAsRegion( const VECTOR2I& aRectCenter, const VECTOR2I& aSize,
1556 int aCornerRadius, const EDA_ANGLE& aOrient )
1557{
1558 // The region outline is generated by 4 sides and 4 90 deg arcs
1559 // 1 --- 2
1560 // | c |
1561 // 4 --- 3
1562
1563 // Note also in user coordinates the Y axis is from top to bottom
1564 // for historical reasons.
1565
1566 // A helper structure to handle outlines coordinates (segments and arcs)
1567 // in user coordinates
1568 struct RR_EDGE
1569 {
1570 VECTOR2I m_start;
1571 VECTOR2I m_end;
1572 VECTOR2I m_center;
1573 EDA_ANGLE m_arc_angle_start;
1574 };
1575
1576 int hsizeX = aSize.x/2;
1577 int hsizeY = aSize.y/2;
1578
1579 RR_EDGE curr_edge;
1580 std::vector<RR_EDGE> rr_outline;
1581
1582 rr_outline.reserve( 4 );
1583
1584 // Build outline coordinates, relative to rectangle center, rotation 0:
1585
1586 // Top left corner 1 (and 4 to 1 left vertical side @ x=-hsizeX)
1587 curr_edge.m_start.x = -hsizeX;
1588 curr_edge.m_start.y = hsizeY - aCornerRadius;
1589 curr_edge.m_end.x = curr_edge.m_start.x;
1590 curr_edge.m_end.y = -hsizeY + aCornerRadius;
1591 curr_edge.m_center.x = -hsizeX + aCornerRadius;
1592 curr_edge.m_center.y = curr_edge.m_end.y;
1593 curr_edge.m_arc_angle_start = aOrient + ANGLE_180;
1594
1595 rr_outline.push_back( curr_edge );
1596
1597 // Top right corner 2 (and 1 to 2 top horizontal side @ y=-hsizeY)
1598 curr_edge.m_start.x = -hsizeX + aCornerRadius;
1599 curr_edge.m_start.y = -hsizeY;
1600 curr_edge.m_end.x = hsizeX - aCornerRadius;
1601 curr_edge.m_end.y = curr_edge.m_start.y;
1602 curr_edge.m_center.x = curr_edge.m_end.x;
1603 curr_edge.m_center.y = -hsizeY + aCornerRadius;
1604 curr_edge.m_arc_angle_start = aOrient + ANGLE_90;
1605
1606 rr_outline.push_back( curr_edge );
1607
1608 // bottom right corner 3 (and 2 to 3 right vertical side @ x=hsizeX)
1609 curr_edge.m_start.x = hsizeX;
1610 curr_edge.m_start.y = -hsizeY + aCornerRadius;
1611 curr_edge.m_end.x = curr_edge.m_start.x;
1612 curr_edge.m_end.y = hsizeY - aCornerRadius;
1613 curr_edge.m_center.x = hsizeX - aCornerRadius;
1614 curr_edge.m_center.y = curr_edge.m_end.y;
1615 curr_edge.m_arc_angle_start = aOrient + ANGLE_0;
1616
1617 rr_outline.push_back( curr_edge );
1618
1619 // bottom left corner 4 (and 3 to 4 bottom horizontal side @ y=hsizeY)
1620 curr_edge.m_start.x = hsizeX - aCornerRadius;
1621 curr_edge.m_start.y = hsizeY;
1622 curr_edge.m_end.x = -hsizeX + aCornerRadius;
1623 curr_edge.m_end.y = curr_edge.m_start.y;
1624 curr_edge.m_center.x = curr_edge.m_end.x;
1625 curr_edge.m_center.y = hsizeY - aCornerRadius;
1626 curr_edge.m_arc_angle_start = aOrient - ANGLE_90;
1627
1628 rr_outline.push_back( curr_edge );
1629
1630 // Move relative coordinates to the actual location and rotation:
1631 VECTOR2I arc_last_center;
1632 EDA_ANGLE arc_last_angle = curr_edge.m_arc_angle_start - ANGLE_90;
1633
1634 for( RR_EDGE& rr_edge: rr_outline )
1635 {
1636 RotatePoint( rr_edge.m_start, aOrient );
1637 RotatePoint( rr_edge.m_end, aOrient );
1638 RotatePoint( rr_edge.m_center, aOrient );
1639 rr_edge.m_start += aRectCenter;
1640 rr_edge.m_end += aRectCenter;
1641 rr_edge.m_center += aRectCenter;
1642 arc_last_center = rr_edge.m_center;
1643 }
1644
1645 // Ensure the region is a closed polygon, i.e. the end point of last segment
1646 // (end of arc) is the same as the first point. Rounding issues can create a
1647 // small difference, mainly for rotated pads.
1648 // calculate last point (end of last arc):
1649 VECTOR2I last_pt;
1650 last_pt.x = arc_last_center.x + KiROUND( aCornerRadius * arc_last_angle.Cos() );
1651 last_pt.y = arc_last_center.y - KiROUND( aCornerRadius * arc_last_angle.Sin() );
1652
1653 VECTOR2I first_pt = rr_outline[0].m_start;
1654
1655#if 0 // For test only:
1656 if( last_pt != first_pt )
1657 wxLogMessage( wxS( "first pt %d %d last pt %d %d" ),
1658 first_pt.x, first_pt.y, last_pt.x, last_pt.y );
1659#endif
1660
1661 fputs( "G36*\n", m_outputFile ); // Start region
1662 fputs( "G01*\n", m_outputFile ); // Set linear interpolation.
1663 first_pt = last_pt;
1664 MoveTo( first_pt ); // Start point of region, must be same as end point
1665
1666 for( RR_EDGE& rr_edge: rr_outline )
1667 {
1668 if( aCornerRadius ) // Guard: ensure we do not create arcs with radius = 0
1669 {
1670 // LineTo( rr_edge.m_end ); // made in plotArc()
1671 plotArc( rr_edge.m_center, rr_edge.m_arc_angle_start,
1672 rr_edge.m_arc_angle_start - ANGLE_90, aCornerRadius, true );
1673 }
1674 else
1675 {
1676 LineTo( rr_edge.m_end );
1677 }
1678 }
1679
1680 fputs( "G37*\n", m_outputFile ); // Close region
1681}
1682
1683
1684void GERBER_PLOTTER::FlashPadCustom( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
1685 const EDA_ANGLE& aOrient, SHAPE_POLY_SET* aPolygons,
1686 OUTLINE_MODE aTraceMode, void* aData )
1687
1688{
1689 // A Pad custom is plotted as polygon (a region in Gerber language).
1690 GBR_METADATA gbr_metadata;
1691
1692 if( aData )
1693 gbr_metadata = *static_cast<GBR_METADATA*>( aData );
1694
1695 SHAPE_POLY_SET polyshape = aPolygons->CloneDropTriangulation();
1696
1697 if( aTraceMode != FILLED )
1698 {
1700 }
1701
1702 std::vector<VECTOR2I> cornerList;
1703
1704 for( int cnt = 0; cnt < polyshape.OutlineCount(); ++cnt )
1705 {
1706 SHAPE_LINE_CHAIN& poly = polyshape.Outline( cnt );
1707
1708 cornerList.clear();
1709
1710 for( int ii = 0; ii < poly.PointCount(); ++ii )
1711 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
1712
1713 // Close polygon
1714 cornerList.push_back( cornerList[0] );
1715
1716 if( aTraceMode == SKETCH )
1717 {
1718 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &gbr_metadata );
1719 }
1720 else
1721 {
1722#ifdef GBR_USE_MACROS_FOR_CUSTOM_PAD
1724 || cornerList.size() > GBR_MACRO_FOR_CUSTOM_PAD_MAX_CORNER_COUNT )
1725 {
1726 PlotGerberRegion( cornerList, &gbr_metadata );
1727 }
1728 else
1729 {
1730 // An AM will be created. the shape must be in position 0,0 and orientation 0
1731 // to be able to reuse the same AM for pads having the same shape
1732 for( size_t ii = 0; ii < cornerList.size(); ii++ )
1733 {
1734 cornerList[ii] -= aPadPos;
1735 RotatePoint( cornerList[ii], -aOrient );
1736 }
1737
1738 VECTOR2D pos_dev = userToDeviceCoordinates( aPadPos );
1739 selectAperture( cornerList, aOrient, APERTURE::AM_FREE_POLYGON,
1740 gbr_metadata.GetApertureAttrib() );
1741 formatNetAttribute( &gbr_metadata.m_NetlistMetadata );
1742
1743 emitDcode( pos_dev, 3 );
1744 }
1745#else
1746 PlotGerberRegion( cornerList, &gbr_metadata );
1747#endif
1748 }
1749 }
1750}
1751
1752
1753void GERBER_PLOTTER::FlashPadChamferRoundRect( const VECTOR2I& aShapePos, const VECTOR2I& aPadSize,
1754 int aCornerRadius, double aChamferRatio,
1755 int aChamferPositions, const EDA_ANGLE& aPadOrient,
1756 OUTLINE_MODE aPlotMode, void* aData )
1757
1758{
1759 GBR_METADATA gbr_metadata;
1760
1761 if( aData )
1762 gbr_metadata = *static_cast<GBR_METADATA*>( aData );
1763
1764 VECTOR2D pos_device = userToDeviceCoordinates( aShapePos );
1765 SHAPE_POLY_SET outline;
1766 std::vector<VECTOR2I> cornerList;
1767
1768 bool hasRoundedCorner = aCornerRadius != 0 && aChamferPositions != 15;
1769
1770#ifdef GBR_USE_MACROS_FOR_CHAMFERED_RECT
1771 // Sketch mode or round rect shape or Apert Macros disabled
1772 if( aPlotMode != FILLED || hasRoundedCorner || m_gerberDisableApertMacros )
1773#endif
1774 {
1775 TransformRoundChamferedRectToPolygon( outline, aShapePos, aPadSize, aPadOrient,
1776 aCornerRadius, aChamferRatio, aChamferPositions, 0,
1778
1779 // Build the corner list
1780 const SHAPE_LINE_CHAIN& corners = outline.Outline(0);
1781
1782 for( int ii = 0; ii < corners.PointCount(); ii++ )
1783 cornerList.emplace_back( corners.CPoint( ii ).x, corners.CPoint( ii ).y );
1784
1785 // Close the polygon
1786 cornerList.push_back( cornerList[0] );
1787
1788 if( aPlotMode == SKETCH )
1789 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &gbr_metadata );
1790 else
1791 {
1792#ifdef GBR_USE_MACROS_FOR_CHAMFERED_ROUND_RECT
1794 {
1795 PlotGerberRegion( cornerList, &gbr_metadata );
1796 }
1797 else
1798 {
1799 // An AM will be created. the shape must be in position 0,0 and orientation 0
1800 // to be able to reuse the same AM for pads having the same shape
1801 for( size_t ii = 0; ii < cornerList.size(); ii++ )
1802 {
1803 cornerList[ii] -= aShapePos;
1804 RotatePoint( cornerList[ii], -aPadOrient );
1805 }
1806
1807 selectAperture( cornerList, aPadOrient, APERTURE::AM_FREE_POLYGON,
1808 gbr_metadata.GetApertureAttrib() );
1809 formatNetAttribute( &gbr_metadata.m_NetlistMetadata );
1810
1811 emitDcode( pos_device, 3 );
1812 }
1813#else
1814 PlotGerberRegion( cornerList, &gbr_metadata );
1815#endif
1816 }
1817
1818 return;
1819 }
1820
1821 // Build the chamfered polygon (4 to 8 corners )
1822 TransformRoundChamferedRectToPolygon( outline, VECTOR2I( 0, 0 ), aPadSize, ANGLE_0, 0,
1823 aChamferRatio, aChamferPositions, 0,
1825
1826 // Build the corner list
1827 const SHAPE_LINE_CHAIN& corners = outline.Outline(0);
1828
1829 // Generate the polygon (4 to 8 corners )
1830 for( int ii = 0; ii < corners.PointCount(); ii++ )
1831 cornerList.emplace_back( corners.CPoint( ii ).x, corners.CPoint( ii ).y );
1832
1833 switch( cornerList.size() )
1834 {
1835 case 4:
1837 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE4P,
1838 gbr_metadata.GetApertureAttrib() );
1839 break;
1840
1841 case 5:
1843 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE5P,
1844 gbr_metadata.GetApertureAttrib() );
1845 break;
1846
1847 case 6:
1849 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE6P,
1850 gbr_metadata.GetApertureAttrib() );
1851 break;
1852
1853 case 7:
1855 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE7P,
1856 gbr_metadata.GetApertureAttrib() );
1857 break;
1858
1859 case 8:
1861 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE8P,
1862 gbr_metadata.GetApertureAttrib() );
1863 break;
1864
1865 default:
1866 wxLogMessage( wxS( "FlashPadChamferRoundRect(): Unexpected number of corners (%d)" ),
1867 (int)cornerList.size() );
1868 break;
1869 }
1870
1871 formatNetAttribute( &gbr_metadata.m_NetlistMetadata );
1872
1873 emitDcode( pos_device, 3 );
1874}
1875
1876
1877void GERBER_PLOTTER::FlashPadTrapez( const VECTOR2I& aPadPos, const VECTOR2I* aCorners,
1878 const EDA_ANGLE& aPadOrient, OUTLINE_MODE aTraceMode,
1879 void* aData )
1880
1881{
1882 // polygon corners list
1883 std::vector<VECTOR2I> cornerList = { aCorners[0], aCorners[1], aCorners[2], aCorners[3] };
1884
1885 // Draw the polygon and fill the interior as required
1886 for( unsigned ii = 0; ii < 4; ii++ )
1887 {
1888 RotatePoint( cornerList[ii], aPadOrient );
1889 cornerList[ii] += aPadPos;
1890 }
1891
1892 // Close the polygon
1893 cornerList.push_back( cornerList[0] );
1894
1895 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1896 GBR_METADATA metadata;
1897
1898 if( gbr_metadata )
1899 metadata = *gbr_metadata;
1900
1901 if( aTraceMode == SKETCH )
1902 {
1903 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &metadata );
1904 return;
1905 }
1906
1907 // Plot a filled polygon:
1908 #ifdef GBR_USE_MACROS_FOR_TRAPEZOID
1910 #endif
1911 {
1913 VECTOR2D pos_dev = userToDeviceCoordinates( aPadPos );
1914 // polygon corners list
1915 std::vector<VECTOR2I> corners = { aCorners[0], aCorners[1], aCorners[2], aCorners[3] };
1916 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1917 selectAperture( corners, aPadOrient, APERTURE::APER_MACRO_OUTLINE4P, aperture_attrib );
1918
1919 if( gbr_metadata )
1920 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1921
1922 emitDcode( pos_dev, 3 );
1923 return;
1924 }
1925
1926 PlotGerberRegion( cornerList, &metadata );
1927}
1928
1929
1930void GERBER_PLOTTER::FlashRegularPolygon( const VECTOR2I& aShapePos, int aDiameter,
1931 int aCornerCount, const EDA_ANGLE& aOrient,
1932 OUTLINE_MODE aTraceMode, void* aData )
1933{
1934 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1935
1936 GBR_METADATA metadata;
1937
1938 if( gbr_metadata )
1939 metadata = *gbr_metadata;
1940
1941 if( aTraceMode == SKETCH )
1942 {
1943 // Build the polygon:
1944 std::vector<VECTOR2I> cornerList;
1945
1946 EDA_ANGLE angle_delta = ANGLE_360 / aCornerCount;
1947
1948 for( int ii = 0; ii < aCornerCount; ii++ )
1949 {
1950 EDA_ANGLE rot = aOrient + ( angle_delta * ii );
1951 VECTOR2I vertice( aDiameter / 2, 0 );
1952
1953 RotatePoint( vertice, rot );
1954 vertice += aShapePos;
1955 cornerList.push_back( vertice );
1956 }
1957
1958 cornerList.push_back( cornerList[0] ); // Close the shape
1959
1960 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &gbr_metadata );
1961 }
1962 else
1963 {
1964 VECTOR2D pos_dev = userToDeviceCoordinates( aShapePos );
1965 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1966
1967 APERTURE::APERTURE_TYPE apert_type =
1969 selectAperture( aDiameter, aOrient, apert_type, aperture_attrib );
1970
1971 if( gbr_metadata )
1972 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1973
1974 emitDcode( pos_dev, 3 );
1975 }
1976}
1977
1978
1980 const COLOR4D& aColor,
1981 const wxString& aText,
1982 const EDA_ANGLE& aOrient,
1983 const VECTOR2I& aSize,
1984 enum GR_TEXT_H_ALIGN_T aH_justify,
1985 enum GR_TEXT_V_ALIGN_T aV_justify,
1986 int aWidth,
1987 bool aItalic,
1988 bool aBold,
1989 bool aMultilineAllowed,
1990 KIFONT::FONT* aFont,
1991 void* aData )
1992{
1993 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1994
1995 if( gbr_metadata )
1996 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1997
1998 PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, aWidth,
1999 aItalic, aBold, aMultilineAllowed, aFont, aData );
2000}
2001
2002
2003void GERBER_PLOTTER::PlotText( const VECTOR2I& aPos, const COLOR4D& aColor,
2004 const wxString& aText,
2005 const TEXT_ATTRIBUTES& aAttributes,
2006 KIFONT::FONT* aFont,
2007 void* aData )
2008{
2009 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
2010
2011 if( gbr_metadata )
2012 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
2013
2014 PLOTTER::PlotText( aPos, aColor, aText, aAttributes, aFont, aData );
2015}
2016
2018{
2019 if( aPositive )
2020 fprintf( m_outputFile, "%%LPD*%%\n" );
2021 else
2022 fprintf( m_outputFile, "%%LPC*%%\n" );
2023}
2024
2025
2026bool APER_MACRO_FREEPOLY::IsSamePoly( const std::vector<VECTOR2I>& aPolygon ) const
2027{
2028 return polyCompare( m_Corners, aPolygon );
2029}
2030
2031
2032void APER_MACRO_FREEPOLY::Format( FILE * aOutput, double aIu2GbrMacroUnit )
2033{
2034 // Write aperture header
2035 fprintf( aOutput, "%%AM%s%d*\n", AM_FREEPOLY_BASENAME, m_Id );
2036 fprintf( aOutput, "4,1,%d,", (int)m_Corners.size() );
2037
2038 // Insert a newline after curr_line_count_max coordinates.
2039 int curr_line_corner_count = 0;
2040 const int curr_line_count_max = 20; // <= 0 to disable newlines
2041
2042 for( size_t ii = 0; ii <= m_Corners.size(); ii++ )
2043 {
2044 int jj = ii;
2045
2046 if( ii >= m_Corners.size() )
2047 jj = 0;
2048
2049 // Note: parameter values are always mm or inches
2050 fprintf( aOutput, "%#f,%#f,",
2051 m_Corners[jj].x * aIu2GbrMacroUnit, -m_Corners[jj].y * aIu2GbrMacroUnit );
2052
2053 if( curr_line_count_max >= 0 && ++curr_line_corner_count >= curr_line_count_max )
2054 {
2055 fprintf( aOutput, "\n" );
2056 curr_line_corner_count = 0;
2057 }
2058 }
2059
2060 // output rotation parameter
2061 fputs( "$1*%\n", aOutput );
2062}
2063
2064
2065void APER_MACRO_FREEPOLY_LIST::Format( FILE * aOutput, double aIu2GbrMacroUnit )
2066{
2067 for( int idx = 0; idx < AmCount(); idx++ )
2068 m_AMList[idx].Format( aOutput, aIu2GbrMacroUnit );
2069}
2070
2071
2072void APER_MACRO_FREEPOLY_LIST::Append( const std::vector<VECTOR2I>& aPolygon )
2073{
2074 m_AMList.emplace_back( aPolygon, AmCount() );
2075}
2076
2077
2078int APER_MACRO_FREEPOLY_LIST::FindAm( const std::vector<VECTOR2I>& aPolygon ) const
2079{
2080 for( int idx = 0; idx < AmCount(); idx++ )
2081 {
2082 if( m_AMList[idx].IsSamePoly( aPolygon ) )
2083 return idx;
2084 }
2085
2086 return -1;
2087}
#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
int GetOrCreateAperture(const VECTOR2I &aSize, int aRadius, const EDA_ANGLE &aRotation, APERTURE::APERTURE_TYPE aType, int aApertureAttribute)
void PlotPolyAsRegion(const SHAPE_LINE_CHAIN &aPoly, FILL_T aFill, int aWidth, GBR_METADATA *aGbrMetadata)
Similar to PlotPoly(), plot a filled polygon using Gerber region, therefore adding X2 attributes to t...
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
virtual void PlotText(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const TEXT_ATTRIBUTES &aAttributes, KIFONT::FONT *aFont, void *aData=nullptr) 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
void PlotGerberRegion(const std::vector< VECTOR2I > &aCornerList, GBR_METADATA *aGbrMetadata)
Plot a Gerber region: similar to PlotPoly but plot only filled polygon, and add the TA....
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:103
int GetDefaultPenWidth() const
void SetDefaultPenWidth(int aWidth)
wxArrayString m_headerExtraLines
Definition: plotter.h:668
bool m_plotMirror
Definition: plotter.h:647
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:650
double m_iuPerDeviceUnit
Definition: plotter.h:644
VECTOR2I m_plotOffset
Definition: plotter.h:646
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:697
VECTOR2I m_paperSize
Definition: plotter.h:666
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:659
wxString m_creator
Definition: plotter.h:662
int m_currentPenWidth
Definition: plotter.h:658
double m_plotScale
Plot scale - chosen by the user (even implicitly with 'fit in a4')
Definition: plotter.h:636
virtual void PlotText(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const TEXT_ATTRIBUTES &aAttributes, KIFONT::FONT *aFont, void *aData=nullptr)
Definition: plotter.cpp:758
FILE * m_outputFile
Output file.
Definition: plotter.h:653
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:670
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:642
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)
Return the reference to aIndex-th outline in the set.
int OutlineCount() const
Return the number of outlines in the set.
SHAPE_POLY_SET CloneDropTriangulation() const
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:433
static constexpr EDA_ANGLE & ANGLE_360
Definition: eda_angle.h:435
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:431
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:429
static constexpr EDA_ANGLE & ANGLE_270
Definition: eda_angle.h:434
FILL_T
Definition: eda_shape.h:54
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
static const bool FILLED
Definition: gr_basic.cpp:30
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:418
OUTLINE_MODE
Definition: outline_mode.h:25
@ SKETCH
Definition: outline_mode.h:26
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:588