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-2023 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_180, aDiameter / 2, aFill, aWidth );
816 Arc( aCenter, ANGLE_180, ANGLE_180, aDiameter / 2, aFill, aWidth );
817}
818
819
820
821void GERBER_PLOTTER::Arc( const VECTOR2D& aCenter, const EDA_ANGLE& aStartAngle,
822 const EDA_ANGLE& aAngle, double aRadius, FILL_T aFill, int aWidth )
823{
824 SetCurrentLineWidth( aWidth );
825
826 EDA_ANGLE endAngle = aStartAngle + aAngle;
827
828 // aFill is not used here.
829 plotArc( aCenter, aStartAngle, endAngle, aRadius, false );
830}
831
832
833void GERBER_PLOTTER::plotArc( const SHAPE_ARC& aArc, bool aPlotInRegion )
834{
835 VECTOR2I start( aArc.GetP0() );
836 VECTOR2I end( aArc.GetP1() );
837 VECTOR2I center( aArc.GetCenter() );
838
839 if( !aPlotInRegion )
840 MoveTo( start);
841 else
842 LineTo( start );
843
844 VECTOR2D devEnd = userToDeviceCoordinates( end );
845
846 // devRelCenter is the position on arc center relative to the arc start, in Gerber coord.
847 // Warning: it is **not** userToDeviceCoordinates( center - start ) when the plotter
848 // has an offset.
849 VECTOR2D devRelCenter = userToDeviceCoordinates( center ) - userToDeviceCoordinates( start );
850
851 // We need to know if the arc is CW or CCW in device coordinates, so build this arc.
852 SHAPE_ARC deviceArc( userToDeviceCoordinates( start ),
854 devEnd, 0 );
855
856 fprintf( m_outputFile, "G75*\n" ); // Multiquadrant (360 degrees) mode
857
858 if( deviceArc.IsClockwise() )
859 fprintf( m_outputFile, "G02*\n" ); // Active circular interpolation, CW
860 else
861 fprintf( m_outputFile, "G03*\n" ); // Active circular interpolation, CCW
862
863 fprintf( m_outputFile, "X%dY%dI%dJ%dD01*\n",
864 KiROUND( devEnd.x ), KiROUND( devEnd.y ),
865 KiROUND( devRelCenter.x ), KiROUND( devRelCenter.y ) );
866
867 fprintf( m_outputFile, "G01*\n" ); // Back to linear interpolate (perhaps useless here).
868}
869
870
871void GERBER_PLOTTER::plotArc( const VECTOR2I& aCenter, const EDA_ANGLE& aStartAngle,
872 const EDA_ANGLE& aEndAngle, int aRadius, bool aPlotInRegion )
873{
874 VECTOR2I start, end;
875 start.x = aCenter.x + KiROUND( aRadius * aStartAngle.Cos() );
876 start.y = aCenter.y + KiROUND( aRadius * aStartAngle.Sin() );
877
878 if( !aPlotInRegion )
879 MoveTo( start );
880 else
881 LineTo( start );
882
883 end.x = aCenter.x + KiROUND( aRadius * aEndAngle.Cos() );
884 end.y = aCenter.y + KiROUND( aRadius * aEndAngle.Sin() );
885 VECTOR2D devEnd = userToDeviceCoordinates( end );
886 // devRelCenter is the position on arc center relative to the arc start, in Gerber coord.
887 VECTOR2D devRelCenter = userToDeviceCoordinates( aCenter ) - userToDeviceCoordinates( start );
888
889 fprintf( m_outputFile, "G75*\n" ); // Multiquadrant (360 degrees) mode
890
891 if( aStartAngle > aEndAngle )
892 fprintf( m_outputFile, "G03*\n" ); // Active circular interpolation, CCW
893 else
894 fprintf( m_outputFile, "G02*\n" ); // Active circular interpolation, CW
895
896 fprintf( m_outputFile, "X%dY%dI%dJ%dD01*\n",
897 KiROUND( devEnd.x ), KiROUND( devEnd.y ),
898 KiROUND( devRelCenter.x ), KiROUND( devRelCenter.y ) );
899
900 fprintf( m_outputFile, "G01*\n" ); // Back to linear interpolate (perhaps useless here).
901}
902
903
905{
906 if( aPoly.PointCount() <= 2 )
907 return;
908
909 bool clearTA_AperFunction = false; // true if a TA.AperFunction is used
910
911 if( aGbrMetadata )
912 {
913 std::string attrib = aGbrMetadata->m_ApertureMetadata.FormatAttribute( !m_useX2format );
914
915 if( !attrib.empty() )
916 {
917 fputs( attrib.c_str(), m_outputFile );
918 clearTA_AperFunction = true;
919 }
920 }
921
922 PlotPoly( aPoly, FILL_T::FILLED_SHAPE, 0 , aGbrMetadata );
923
924 // Clear the TA attribute, to avoid the next item to inherit it:
925 if( clearTA_AperFunction )
926 {
927 if( m_useX2format )
928 {
929 fputs( "%TD.AperFunction*%\n", m_outputFile );
930 }
931 else
932 {
933 fputs( "G04 #@! TD.AperFunction*\n", m_outputFile );
934 }
935 }
936}
937
938
939void GERBER_PLOTTER::PlotGerberRegion( const std::vector<VECTOR2I>& aCornerList,
940 GBR_METADATA* aGbrMetadata )
941{
942 if( aCornerList.size() <= 2 )
943 return;
944
945 bool clearTA_AperFunction = false; // true if a TA.AperFunction is used
946
947 if( aGbrMetadata )
948 {
949 std::string attrib = aGbrMetadata->m_ApertureMetadata.FormatAttribute( !m_useX2format );
950
951 if( !attrib.empty() )
952 {
953 fputs( attrib.c_str(), m_outputFile );
954 clearTA_AperFunction = true;
955 }
956 }
957
958 PlotPoly( aCornerList, FILL_T::FILLED_SHAPE, 0, aGbrMetadata );
959
960 // Clear the TA attribute, to avoid the next item to inherit it:
961 if( clearTA_AperFunction )
962 {
963 if( m_useX2format )
964 {
965 fputs( "%TD.AperFunction*%\n", m_outputFile );
966 }
967 else
968 {
969 fputs( "G04 #@! TD.AperFunction*\n", m_outputFile );
970 }
971 }
972}
973
974
976 int aWidth, GBR_METADATA* aGbrMetadata )
977{
978 // plot a filled polygon using Gerber region, therefore adding X2 attributes
979 // to the solid polygon
980 if( aWidth || aFill == FILL_T::NO_FILL )
981 PlotPoly( aPoly, FILL_T::NO_FILL, aWidth, aGbrMetadata );
982
983 if( aFill != FILL_T::NO_FILL )
984 PlotGerberRegion( aPoly, aGbrMetadata );
985}
986
987
988void GERBER_PLOTTER::PlotPoly( const SHAPE_LINE_CHAIN& aPoly, FILL_T aFill, int aWidth,
989 void* aData )
990{
991 if( aPoly.CPoints().size() <= 1 )
992 return;
993
994 // Gerber format does not know filled polygons with thick outline
995 // Therefore, to plot a filled polygon with outline having a thickness,
996 // one should plot outline as thick segments
997 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
998
999 if( gbr_metadata )
1000 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1001
1002 if( aFill != FILL_T::NO_FILL )
1003 {
1004 fputs( "G36*\n", m_outputFile );
1005
1006 MoveTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
1007
1008 fputs( "G01*\n", m_outputFile ); // Set linear interpolation.
1009
1010 for( int ii = 1; ii < aPoly.PointCount(); ii++ )
1011 {
1012 int arcindex = aPoly.ArcIndex( ii );
1013
1014 if( arcindex < 0 )
1015 {
1017 LineTo( VECTOR2I( aPoly.CPoint( ii ) ) );
1018 }
1019 else
1020 {
1021 const SHAPE_ARC& arc = aPoly.Arc( arcindex );
1022
1023 plotArc( arc, true );
1024
1025 // skip points on arcs, since we plot the arc itself
1026 while( ii+1 < aPoly.PointCount() && arcindex == aPoly.ArcIndex( ii+1 ) )
1027 ii++;
1028 }
1029 }
1030
1031 // If the polygon is not closed, close it:
1032 if( aPoly.CPoint( 0 ) != aPoly.CPoint( -1 ) )
1033 FinishTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
1034
1035 fputs( "G37*\n", m_outputFile );
1036 }
1037
1038 if( aWidth > 0 || aFill == FILL_T::NO_FILL ) // Draw the polyline/polygon outline
1039 {
1040 SetCurrentLineWidth( aWidth, gbr_metadata );
1041
1042 MoveTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
1043
1044 for( int ii = 1; ii < aPoly.PointCount(); ii++ )
1045 {
1046 int arcindex = aPoly.ArcIndex( ii );
1047
1048 if( arcindex < 0 )
1049 {
1051 LineTo( VECTOR2I( aPoly.CPoint( ii ) ) );
1052 }
1053 else
1054 {
1055 const SHAPE_ARC& arc = aPoly.Arc( arcindex );
1056
1057 plotArc( arc, true );
1058
1059 // skip points on arcs, since we plot the arc itself
1060 while( ii+1 < aPoly.PointCount() && arcindex == aPoly.ArcIndex( ii+1 ) )
1061 ii++;
1062 }
1063 }
1064
1065 // Ensure the thick outline is closed for filled polygons
1066 // (if not filled, could be only a polyline)
1067 if( ( aPoly.CPoint( 0 ) != aPoly.CPoint( -1 ) )
1068 && ( aPoly.IsClosed() || aFill != FILL_T::NO_FILL ) )
1069 LineTo( VECTOR2I( aPoly.CPoint( 0 ) ) );
1070
1071 PenFinish();
1072 }
1073}
1074
1075void GERBER_PLOTTER::PlotPoly( const std::vector<VECTOR2I>& aCornerList, FILL_T aFill, int aWidth,
1076 void * aData )
1077{
1078 if( aCornerList.size() <= 1 )
1079 return;
1080
1081 // Gerber format does not know filled polygons with thick outline
1082 // Therefore, to plot a filled polygon with outline having a thickness,
1083 // one should plot outline as thick segments
1084 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1085
1086 if( gbr_metadata )
1087 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1088
1089 if( aFill != FILL_T::NO_FILL )
1090 {
1091 fputs( "G36*\n", m_outputFile );
1092
1093 MoveTo( aCornerList[0] );
1094 fputs( "G01*\n", m_outputFile ); // Set linear interpolation.
1095
1096 for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
1097 LineTo( aCornerList[ii] );
1098
1099 // If the polygon is not closed, close it:
1100 if( aCornerList[0] != aCornerList[aCornerList.size()-1] )
1101 FinishTo( aCornerList[0] );
1102
1103 fputs( "G37*\n", m_outputFile );
1104 }
1105
1106 if( aWidth > 0 || aFill == FILL_T::NO_FILL ) // Draw the polyline/polygon outline
1107 {
1108 SetCurrentLineWidth( aWidth, gbr_metadata );
1109
1110 MoveTo( aCornerList[0] );
1111
1112 for( unsigned ii = 1; ii < aCornerList.size(); ii++ )
1113 LineTo( aCornerList[ii] );
1114
1115 // Ensure the thick outline is closed for filled polygons
1116 // (if not filled, could be only a polyline)
1117 if( aFill != FILL_T::NO_FILL && ( aCornerList[aCornerList.size() - 1] != aCornerList[0] ) )
1118 LineTo( aCornerList[0] );
1119
1120 PenFinish();
1121 }
1122}
1123
1124
1125void GERBER_PLOTTER::ThickSegment( const VECTOR2I& start, const VECTOR2I& end, int width,
1126 OUTLINE_MODE tracemode, void* aData )
1127{
1128 if( tracemode == FILLED )
1129 {
1130 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1131 SetCurrentLineWidth( width, gbr_metadata );
1132
1133 if( gbr_metadata )
1134 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1135
1136 MoveTo( start );
1137 FinishTo( end );
1138 }
1139 else
1140 {
1142 segmentAsOval( start, end, width, tracemode );
1143 }
1144}
1145
1146
1147void GERBER_PLOTTER::ThickArc( const VECTOR2D& aCentre, const EDA_ANGLE& aStartAngle,
1148 const EDA_ANGLE& aAngle, double aRadius, int aWidth,
1149 OUTLINE_MODE aTraceMode, void* aData )
1150{
1151 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1152 SetCurrentLineWidth( aWidth, gbr_metadata );
1153
1154 if( gbr_metadata )
1155 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1156
1157 if( aTraceMode == FILLED )
1158 {
1159 Arc( aCentre, aStartAngle, aAngle, aRadius, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1160 }
1161 else
1162 {
1164 Arc( aCentre, aStartAngle, aAngle, aRadius - ( aWidth - m_currentPenWidth ) / 2,
1165 FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1166 Arc( aCentre, aStartAngle, aAngle, aRadius + ( aWidth - m_currentPenWidth ) / 2,
1167 FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1168 }
1169}
1170
1171
1172void GERBER_PLOTTER::ThickRect( const VECTOR2I& p1, const VECTOR2I& p2, int width,
1173 OUTLINE_MODE tracemode, void* aData )
1174{
1175 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1176 SetCurrentLineWidth( width, gbr_metadata );
1177
1178 if( gbr_metadata )
1179 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1180
1181 if( tracemode == FILLED )
1182 {
1183 Rect( p1, p2, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1184 }
1185 else
1186 {
1188 VECTOR2I offsetp1( p1.x - ( width - m_currentPenWidth ) / 2,
1189 p1.y - (width - m_currentPenWidth) / 2 );
1190 VECTOR2I offsetp2( p2.x + ( width - m_currentPenWidth ) / 2,
1191 p2.y + (width - m_currentPenWidth) / 2 );
1192 Rect( offsetp1, offsetp2, FILL_T::NO_FILL, -1 );
1193 offsetp1.x += (width - m_currentPenWidth);
1194 offsetp1.y += (width - m_currentPenWidth);
1195 offsetp2.x -= (width - m_currentPenWidth);
1196 offsetp2.y -= (width - m_currentPenWidth);
1197 Rect( offsetp1, offsetp2, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1198 }
1199}
1200
1201
1202void GERBER_PLOTTER::ThickCircle( const VECTOR2I& pos, int diametre, int width,
1203 OUTLINE_MODE tracemode, void* aData )
1204{
1205 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1206 SetCurrentLineWidth( width, gbr_metadata );
1207
1208 if( gbr_metadata )
1209 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1210
1211 if( tracemode == FILLED )
1212 {
1213 Circle( pos, diametre, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1214 }
1215 else
1216 {
1218 Circle( pos, diametre - (width - m_currentPenWidth), FILL_T::NO_FILL,
1220 Circle( pos, diametre + (width - m_currentPenWidth), FILL_T::NO_FILL,
1222 }
1223}
1224
1225
1226void GERBER_PLOTTER::FilledCircle( const VECTOR2I& pos, int diametre,
1227 OUTLINE_MODE tracemode, void* aData )
1228{
1229 // A filled circle is a graphic item, not a pad.
1230 // So it is drawn, not flashed.
1231 GBR_METADATA *gbr_metadata = static_cast<GBR_METADATA*>( aData );
1232
1233 if( gbr_metadata )
1234 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1235
1236 if( tracemode == FILLED )
1237 {
1238 // Draw a circle of diameter = diameter/2 with a line thickness = radius,
1239 // To create a filled circle
1240 SetCurrentLineWidth( diametre/2, gbr_metadata );
1241 Circle( pos, diametre/2, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1242 }
1243 else
1244 {
1246 Circle( pos, diametre, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1247 }
1248}
1249
1250
1251void GERBER_PLOTTER::FlashPadCircle( const VECTOR2I& pos, int diametre, OUTLINE_MODE trace_mode,
1252 void* aData )
1253{
1254 VECTOR2I size( diametre, diametre );
1255 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1256
1257 if( trace_mode == SKETCH )
1258 {
1259 if( gbr_metadata )
1260 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1261
1263
1264 Circle( pos, diametre, FILL_T::NO_FILL, DO_NOT_SET_LINE_WIDTH );
1265 }
1266 else
1267 {
1268 VECTOR2D pos_dev = userToDeviceCoordinates( pos );
1269
1270 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1271 selectAperture( size, 0, ANGLE_0, APERTURE::AT_CIRCLE, aperture_attrib );
1272
1273 if( gbr_metadata )
1274 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1275
1276 emitDcode( pos_dev, 3 );
1277 }
1278}
1279
1280
1281void GERBER_PLOTTER::FlashPadOval( const VECTOR2I& aPos, const VECTOR2I& aSize,
1282 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
1283{
1284 wxASSERT( m_outputFile );
1285
1286 VECTOR2I size( aSize );
1287 EDA_ANGLE orient( aOrient );
1288 orient.Normalize();
1289 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1290
1291 // Flash a vertical or horizontal shape (this is a basic aperture).
1292 if( orient.IsCardinal() && aTraceMode == FILLED )
1293 {
1294 if( orient.IsCardinal90() )
1295 std::swap( size.x, size.y );
1296
1297 VECTOR2D pos_device = userToDeviceCoordinates( aPos );
1298 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1299 selectAperture( size, 0, ANGLE_0, APERTURE::AT_OVAL, aperture_attrib );
1300
1301 if( gbr_metadata )
1302 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1303
1304 emitDcode( pos_device, 3 );
1305 }
1306 else // Plot pad as region.
1307 // Only regions and flashed items accept a object attribute TO.P for the pin name
1308 {
1309 if( aTraceMode == FILLED )
1310 {
1311 #ifdef GBR_USE_MACROS_FOR_ROTATED_OVAL
1313 #endif
1314 {
1315 m_hasApertureRotOval = true;
1316 // We are using a aperture macro that expect size.y < size.x
1317 // i.e draw a horizontal line for rotation = 0.0
1318 // size.x = length, size.y = width
1319 if( size.x < size.y )
1320 {
1321 std::swap( size.x, size.y );
1322 orient += ANGLE_90;
1323
1324 if( orient > ANGLE_180 )
1325 orient -= ANGLE_180;
1326 }
1327
1328 VECTOR2D pos_device = userToDeviceCoordinates( aPos );
1329 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1330 selectAperture( size, 0, orient, APERTURE::AM_ROTATED_OVAL, aperture_attrib );
1331
1332 if( gbr_metadata )
1333 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1334
1335 emitDcode( pos_device, 3 );
1336 return;
1337 }
1338 // Draw the oval as round rect pad with a radius = 50% min size)
1339 // In gerber file, it will be drawn as a region with arcs, and can be
1340 // detected as pads (similar to a flashed pad)
1341 FlashPadRoundRect( aPos, aSize, std::min( aSize.x, aSize.y ) / 2, orient, FILLED,
1342 aData );
1343 }
1344 else // Non filled shape: plot outlines:
1345 {
1346 if( size.x > size.y )
1347 {
1348 std::swap( size.x, size.y );
1349
1350 if( orient < ANGLE_270 )
1351 orient += ANGLE_90;
1352 else
1353 orient -= ANGLE_270;
1354 }
1355
1356 sketchOval( aPos, size, orient, -1 );
1357 }
1358 }
1359}
1360
1361
1362void GERBER_PLOTTER::FlashPadRect( const VECTOR2I& pos, const VECTOR2I& aSize,
1363 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode, void* aData )
1364
1365{
1366 wxASSERT( m_outputFile );
1367
1368 VECTOR2I size( aSize );
1369 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1370
1371 // Horizontal / vertical rect can use a basic aperture (not a macro)
1372 // so use it for rotation n*90 deg
1373 if( aOrient.IsCardinal() )
1374 {
1375 if( aOrient.IsCardinal90() )
1376 // Build the not rotated equivalent shape:
1377 std::swap( size.x, size.y );
1378
1379 if( aTraceMode == SKETCH )
1380 {
1381 if( gbr_metadata )
1382 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1383
1385 Rect( VECTOR2I( pos.x - ( size.x / 2 ), pos.y - (size.y / 2 ) ),
1386 VECTOR2I( pos.x + ( size.x / 2 ), pos.y + (size.y / 2 ) ),
1387 FILL_T::NO_FILL, GetCurrentLineWidth() );
1388 }
1389 else
1390 {
1391 VECTOR2D pos_device = userToDeviceCoordinates( pos );
1392 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1393 selectAperture( size, 0, ANGLE_0, APERTURE::AT_RECT, aperture_attrib );
1394
1395 if( gbr_metadata )
1396 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1397
1398 emitDcode( pos_device, 3 );
1399 }
1400 }
1401 else
1402 {
1403 #ifdef GBR_USE_MACROS_FOR_ROTATED_RECT
1404 if( aTraceMode != SKETCH && !m_gerberDisableApertMacros )
1405 {
1406 m_hasApertureRotRect = true;
1407
1408 VECTOR2D pos_device = userToDeviceCoordinates( pos );
1409 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1410 selectAperture( size, 0, aOrient, APERTURE::AM_ROT_RECT, aperture_attrib );
1411
1412 if( gbr_metadata )
1413 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1414
1415 emitDcode( pos_device, 3 );
1416 }
1417 else
1418 #endif
1419 {
1420 // plot pad shape as Gerber region
1421 VECTOR2I coord[4];
1422 // coord[0] is assumed the lower left
1423 // coord[1] is assumed the upper left
1424 // coord[2] is assumed the upper right
1425 // coord[3] is assumed the lower right
1426
1427 coord[0].x = -size.x/2; // lower left
1428 coord[0].y = size.y/2;
1429 coord[1].x = -size.x/2; // upper left
1430 coord[1].y = -size.y/2;
1431 coord[2].x = size.x/2; // upper right
1432 coord[2].y = -size.y/2;
1433 coord[3].x = size.x/2; // lower right
1434 coord[3].y = size.y/2;
1435
1436 FlashPadTrapez( pos, coord, aOrient, aTraceMode, aData );
1437 }
1438 }
1439}
1440
1441void GERBER_PLOTTER::FlashPadRoundRect( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
1442 int aCornerRadius, const EDA_ANGLE& aOrient,
1443 OUTLINE_MODE aTraceMode, void* aData )
1444
1445{
1446 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1447
1448 if( aTraceMode != FILLED )
1449 {
1450 SHAPE_POLY_SET outline;
1451 TransformRoundChamferedRectToPolygon( outline, aPadPos, aSize, aOrient, aCornerRadius, 0.0,
1453
1455
1456 std::vector<VECTOR2I> cornerList;
1457 // TransformRoundRectToPolygon creates only one convex polygon
1458 SHAPE_LINE_CHAIN& poly = outline.Outline( 0 );
1459 cornerList.reserve( poly.PointCount() + 1 );
1460
1461 for( int ii = 0; ii < poly.PointCount(); ++ii )
1462 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
1463
1464 // Close polygon
1465 cornerList.push_back( cornerList[0] );
1466
1467 // plot outlines
1468 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), gbr_metadata );
1469 }
1470 else
1471 {
1472 #ifdef GBR_USE_MACROS_FOR_ROUNDRECT
1474 #endif
1475 {
1477
1478 VECTOR2D pos_dev = userToDeviceCoordinates( aPadPos );
1479 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1480 selectAperture( aSize, aCornerRadius, aOrient, APERTURE::AM_ROUND_RECT,
1481 aperture_attrib );
1482
1483 if( gbr_metadata )
1484 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1485
1486 emitDcode( pos_dev, 3 );
1487 return;
1488 }
1489
1490 // A Pad RoundRect is plotted as a Gerber region.
1491 // Initialize region metadata:
1492 bool clearTA_AperFunction = false; // true if a TA.AperFunction is used
1493
1494 if( gbr_metadata )
1495 {
1496 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1497 std::string attrib = gbr_metadata->m_ApertureMetadata.FormatAttribute( !m_useX2format );
1498
1499 if( !attrib.empty() )
1500 {
1501 fputs( attrib.c_str(), m_outputFile );
1502 clearTA_AperFunction = true;
1503 }
1504 }
1505
1506 // Plot the region using arcs in corners.
1507 plotRoundRectAsRegion( aPadPos, aSize, aCornerRadius, aOrient );
1508
1509 // Clear the TA attribute, to avoid the next item to inherit it:
1510 if( clearTA_AperFunction )
1511 {
1512 if( m_useX2format )
1513 fputs( "%TD.AperFunction*%\n", m_outputFile );
1514 else
1515 fputs( "G04 #@! TD.AperFunction*\n", m_outputFile );
1516 }
1517 }
1518}
1519
1520
1521void GERBER_PLOTTER::plotRoundRectAsRegion( const VECTOR2I& aRectCenter, const VECTOR2I& aSize,
1522 int aCornerRadius, const EDA_ANGLE& aOrient )
1523{
1524 // The region outline is generated by 4 sides and 4 90 deg arcs
1525 // 1 --- 2
1526 // | c |
1527 // 4 --- 3
1528
1529 // Note also in user coordinates the Y axis is from top to bottom
1530 // for historical reasons.
1531
1532 // A helper structure to handle outlines coordinates (segments and arcs)
1533 // in user coordinates
1534 struct RR_EDGE
1535 {
1536 VECTOR2I m_start;
1537 VECTOR2I m_end;
1538 VECTOR2I m_center;
1539 EDA_ANGLE m_arc_angle_start;
1540 };
1541
1542 int hsizeX = aSize.x/2;
1543 int hsizeY = aSize.y/2;
1544
1545 RR_EDGE curr_edge;
1546 std::vector<RR_EDGE> rr_outline;
1547
1548 rr_outline.reserve( 4 );
1549
1550 // Build outline coordinates, relative to rectangle center, rotation 0:
1551
1552 // Top left corner 1 (and 4 to 1 left vertical side @ x=-hsizeX)
1553 curr_edge.m_start.x = -hsizeX;
1554 curr_edge.m_start.y = hsizeY - aCornerRadius;
1555 curr_edge.m_end.x = curr_edge.m_start.x;
1556 curr_edge.m_end.y = -hsizeY + aCornerRadius;
1557 curr_edge.m_center.x = -hsizeX + aCornerRadius;
1558 curr_edge.m_center.y = curr_edge.m_end.y;
1559 curr_edge.m_arc_angle_start = aOrient + ANGLE_180;
1560
1561 rr_outline.push_back( curr_edge );
1562
1563 // Top right corner 2 (and 1 to 2 top horizontal side @ y=-hsizeY)
1564 curr_edge.m_start.x = -hsizeX + aCornerRadius;
1565 curr_edge.m_start.y = -hsizeY;
1566 curr_edge.m_end.x = hsizeX - aCornerRadius;
1567 curr_edge.m_end.y = curr_edge.m_start.y;
1568 curr_edge.m_center.x = curr_edge.m_end.x;
1569 curr_edge.m_center.y = -hsizeY + aCornerRadius;
1570 curr_edge.m_arc_angle_start = aOrient + ANGLE_90;
1571
1572 rr_outline.push_back( curr_edge );
1573
1574 // bottom right corner 3 (and 2 to 3 right vertical side @ x=hsizeX)
1575 curr_edge.m_start.x = hsizeX;
1576 curr_edge.m_start.y = -hsizeY + aCornerRadius;
1577 curr_edge.m_end.x = curr_edge.m_start.x;
1578 curr_edge.m_end.y = hsizeY - aCornerRadius;
1579 curr_edge.m_center.x = hsizeX - aCornerRadius;
1580 curr_edge.m_center.y = curr_edge.m_end.y;
1581 curr_edge.m_arc_angle_start = aOrient + ANGLE_0;
1582
1583 rr_outline.push_back( curr_edge );
1584
1585 // bottom left corner 4 (and 3 to 4 bottom horizontal side @ y=hsizeY)
1586 curr_edge.m_start.x = hsizeX - aCornerRadius;
1587 curr_edge.m_start.y = hsizeY;
1588 curr_edge.m_end.x = -hsizeX + aCornerRadius;
1589 curr_edge.m_end.y = curr_edge.m_start.y;
1590 curr_edge.m_center.x = curr_edge.m_end.x;
1591 curr_edge.m_center.y = hsizeY - aCornerRadius;
1592 curr_edge.m_arc_angle_start = aOrient - ANGLE_90;
1593
1594 rr_outline.push_back( curr_edge );
1595
1596 // Move relative coordinates to the actual location and rotation:
1597 VECTOR2I arc_last_center;
1598 EDA_ANGLE arc_last_angle = curr_edge.m_arc_angle_start - ANGLE_90;
1599
1600 for( RR_EDGE& rr_edge: rr_outline )
1601 {
1602 RotatePoint( rr_edge.m_start, aOrient );
1603 RotatePoint( rr_edge.m_end, aOrient );
1604 RotatePoint( rr_edge.m_center, aOrient );
1605 rr_edge.m_start += aRectCenter;
1606 rr_edge.m_end += aRectCenter;
1607 rr_edge.m_center += aRectCenter;
1608 arc_last_center = rr_edge.m_center;
1609 }
1610
1611 // Ensure the region is a closed polygon, i.e. the end point of last segment
1612 // (end of arc) is the same as the first point. Rounding issues can create a
1613 // small difference, mainly for rotated pads.
1614 // calculate last point (end of last arc):
1615 VECTOR2I last_pt;
1616 last_pt.x = arc_last_center.x + KiROUND( aCornerRadius * arc_last_angle.Cos() );
1617 last_pt.y = arc_last_center.y - KiROUND( aCornerRadius * arc_last_angle.Sin() );
1618
1619 VECTOR2I first_pt = rr_outline[0].m_start;
1620
1621#if 0 // For test only:
1622 if( last_pt != first_pt )
1623 wxLogMessage( wxS( "first pt %d %d last pt %d %d" ),
1624 first_pt.x, first_pt.y, last_pt.x, last_pt.y );
1625#endif
1626
1627 fputs( "G36*\n", m_outputFile ); // Start region
1628 fputs( "G01*\n", m_outputFile ); // Set linear interpolation.
1629 first_pt = last_pt;
1630 MoveTo( first_pt ); // Start point of region, must be same as end point
1631
1632 for( RR_EDGE& rr_edge: rr_outline )
1633 {
1634 if( aCornerRadius ) // Guard: ensure we do not create arcs with radius = 0
1635 {
1636 // LineTo( rr_edge.m_end ); // made in plotArc()
1637 plotArc( rr_edge.m_center, -rr_edge.m_arc_angle_start,
1638 -rr_edge.m_arc_angle_start + ANGLE_90, aCornerRadius, true );
1639 }
1640 else
1641 {
1642 LineTo( rr_edge.m_end );
1643 }
1644 }
1645
1646 fputs( "G37*\n", m_outputFile ); // Close region
1647}
1648
1649
1650void GERBER_PLOTTER::FlashPadCustom( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
1651 const EDA_ANGLE& aOrient, SHAPE_POLY_SET* aPolygons,
1652 OUTLINE_MODE aTraceMode, void* aData )
1653
1654{
1655 // A Pad custom is plotted as polygon (a region in Gerber language).
1656 GBR_METADATA gbr_metadata;
1657
1658 if( aData )
1659 gbr_metadata = *static_cast<GBR_METADATA*>( aData );
1660
1661 SHAPE_POLY_SET polyshape = aPolygons->CloneDropTriangulation();
1662
1663 if( aTraceMode != FILLED )
1664 {
1666 }
1667
1668 std::vector<VECTOR2I> cornerList;
1669
1670 for( int cnt = 0; cnt < polyshape.OutlineCount(); ++cnt )
1671 {
1672 SHAPE_LINE_CHAIN& poly = polyshape.Outline( cnt );
1673
1674 cornerList.clear();
1675
1676 for( int ii = 0; ii < poly.PointCount(); ++ii )
1677 cornerList.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
1678
1679 // Close polygon
1680 cornerList.push_back( cornerList[0] );
1681
1682 if( aTraceMode == SKETCH )
1683 {
1684 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &gbr_metadata );
1685 }
1686 else
1687 {
1688#ifdef GBR_USE_MACROS_FOR_CUSTOM_PAD
1690 || cornerList.size() > GBR_MACRO_FOR_CUSTOM_PAD_MAX_CORNER_COUNT )
1691 {
1692 PlotGerberRegion( cornerList, &gbr_metadata );
1693 }
1694 else
1695 {
1696 // An AM will be created. the shape must be in position 0,0 and orientation 0
1697 // to be able to reuse the same AM for pads having the same shape
1698 for( size_t ii = 0; ii < cornerList.size(); ii++ )
1699 {
1700 cornerList[ii] -= aPadPos;
1701 RotatePoint( cornerList[ii], -aOrient );
1702 }
1703
1704 VECTOR2D pos_dev = userToDeviceCoordinates( aPadPos );
1705 selectAperture( cornerList, aOrient, APERTURE::AM_FREE_POLYGON,
1706 gbr_metadata.GetApertureAttrib() );
1707 formatNetAttribute( &gbr_metadata.m_NetlistMetadata );
1708
1709 emitDcode( pos_dev, 3 );
1710 }
1711#else
1712 PlotGerberRegion( cornerList, &gbr_metadata );
1713#endif
1714 }
1715 }
1716}
1717
1718
1719void GERBER_PLOTTER::FlashPadChamferRoundRect( const VECTOR2I& aShapePos, const VECTOR2I& aPadSize,
1720 int aCornerRadius, double aChamferRatio,
1721 int aChamferPositions, const EDA_ANGLE& aPadOrient,
1722 OUTLINE_MODE aPlotMode, void* aData )
1723
1724{
1725 GBR_METADATA gbr_metadata;
1726
1727 if( aData )
1728 gbr_metadata = *static_cast<GBR_METADATA*>( aData );
1729
1730 VECTOR2D pos_device = userToDeviceCoordinates( aShapePos );
1731 SHAPE_POLY_SET outline;
1732 std::vector<VECTOR2I> cornerList;
1733
1734 bool hasRoundedCorner = aCornerRadius != 0 && aChamferPositions != 15;
1735
1736#ifdef GBR_USE_MACROS_FOR_CHAMFERED_RECT
1737 // Sketch mode or round rect shape or Apert Macros disabled
1738 if( aPlotMode != FILLED || hasRoundedCorner || m_gerberDisableApertMacros )
1739#endif
1740 {
1741 TransformRoundChamferedRectToPolygon( outline, aShapePos, aPadSize, aPadOrient,
1742 aCornerRadius, aChamferRatio, aChamferPositions, 0,
1744
1745 // Build the corner list
1746 const SHAPE_LINE_CHAIN& corners = outline.Outline(0);
1747
1748 for( int ii = 0; ii < corners.PointCount(); ii++ )
1749 cornerList.emplace_back( corners.CPoint( ii ).x, corners.CPoint( ii ).y );
1750
1751 // Close the polygon
1752 cornerList.push_back( cornerList[0] );
1753
1754 if( aPlotMode == SKETCH )
1755 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &gbr_metadata );
1756 else
1757 {
1758#ifdef GBR_USE_MACROS_FOR_CHAMFERED_ROUND_RECT
1760 {
1761 PlotGerberRegion( cornerList, &gbr_metadata );
1762 }
1763 else
1764 {
1765 // An AM will be created. the shape must be in position 0,0 and orientation 0
1766 // to be able to reuse the same AM for pads having the same shape
1767 for( size_t ii = 0; ii < cornerList.size(); ii++ )
1768 {
1769 cornerList[ii] -= aShapePos;
1770 RotatePoint( cornerList[ii], -aPadOrient );
1771 }
1772
1773 selectAperture( cornerList, aPadOrient, APERTURE::AM_FREE_POLYGON,
1774 gbr_metadata.GetApertureAttrib() );
1775 formatNetAttribute( &gbr_metadata.m_NetlistMetadata );
1776
1777 emitDcode( pos_device, 3 );
1778 }
1779#else
1780 PlotGerberRegion( cornerList, &gbr_metadata );
1781#endif
1782 }
1783
1784 return;
1785 }
1786
1787 // Build the chamfered polygon (4 to 8 corners )
1788 TransformRoundChamferedRectToPolygon( outline, VECTOR2I( 0, 0 ), aPadSize, ANGLE_0, 0,
1789 aChamferRatio, aChamferPositions, 0,
1791
1792 // Build the corner list
1793 const SHAPE_LINE_CHAIN& corners = outline.Outline(0);
1794
1795 // Generate the polygon (4 to 8 corners )
1796 for( int ii = 0; ii < corners.PointCount(); ii++ )
1797 cornerList.emplace_back( corners.CPoint( ii ).x, corners.CPoint( ii ).y );
1798
1799 switch( cornerList.size() )
1800 {
1801 case 4:
1803 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE4P,
1804 gbr_metadata.GetApertureAttrib() );
1805 break;
1806
1807 case 5:
1809 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE5P,
1810 gbr_metadata.GetApertureAttrib() );
1811 break;
1812
1813 case 6:
1815 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE6P,
1816 gbr_metadata.GetApertureAttrib() );
1817 break;
1818
1819 case 7:
1821 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE7P,
1822 gbr_metadata.GetApertureAttrib() );
1823 break;
1824
1825 case 8:
1827 selectAperture( cornerList, aPadOrient, APERTURE::APER_MACRO_OUTLINE8P,
1828 gbr_metadata.GetApertureAttrib() );
1829 break;
1830
1831 default:
1832 wxLogMessage( wxS( "FlashPadChamferRoundRect(): Unexpected number of corners (%d)" ),
1833 (int)cornerList.size() );
1834 break;
1835 }
1836
1837 formatNetAttribute( &gbr_metadata.m_NetlistMetadata );
1838
1839 emitDcode( pos_device, 3 );
1840}
1841
1842
1843void GERBER_PLOTTER::FlashPadTrapez( const VECTOR2I& aPadPos, const VECTOR2I* aCorners,
1844 const EDA_ANGLE& aPadOrient, OUTLINE_MODE aTraceMode,
1845 void* aData )
1846
1847{
1848 // polygon corners list
1849 std::vector<VECTOR2I> cornerList = { aCorners[0], aCorners[1], aCorners[2], aCorners[3] };
1850
1851 // Draw the polygon and fill the interior as required
1852 for( unsigned ii = 0; ii < 4; ii++ )
1853 {
1854 RotatePoint( cornerList[ii], aPadOrient );
1855 cornerList[ii] += aPadPos;
1856 }
1857
1858 // Close the polygon
1859 cornerList.push_back( cornerList[0] );
1860
1861 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1862 GBR_METADATA metadata;
1863
1864 if( gbr_metadata )
1865 metadata = *gbr_metadata;
1866
1867 if( aTraceMode == SKETCH )
1868 {
1869 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &metadata );
1870 return;
1871 }
1872
1873 // Plot a filled polygon:
1874 #ifdef GBR_USE_MACROS_FOR_TRAPEZOID
1876 #endif
1877 {
1879 VECTOR2D pos_dev = userToDeviceCoordinates( aPadPos );
1880 // polygon corners list
1881 std::vector<VECTOR2I> corners = { aCorners[0], aCorners[1], aCorners[2], aCorners[3] };
1882 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1883 selectAperture( corners, aPadOrient, APERTURE::APER_MACRO_OUTLINE4P, aperture_attrib );
1884
1885 if( gbr_metadata )
1886 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1887
1888 emitDcode( pos_dev, 3 );
1889 return;
1890 }
1891
1892 PlotGerberRegion( cornerList, &metadata );
1893}
1894
1895
1896void GERBER_PLOTTER::FlashRegularPolygon( const VECTOR2I& aShapePos, int aDiameter,
1897 int aCornerCount, const EDA_ANGLE& aOrient,
1898 OUTLINE_MODE aTraceMode, void* aData )
1899{
1900 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1901
1902 GBR_METADATA metadata;
1903
1904 if( gbr_metadata )
1905 metadata = *gbr_metadata;
1906
1907 if( aTraceMode == SKETCH )
1908 {
1909 // Build the polygon:
1910 std::vector<VECTOR2I> cornerList;
1911
1912 EDA_ANGLE angle_delta = ANGLE_360 / aCornerCount;
1913
1914 for( int ii = 0; ii < aCornerCount; ii++ )
1915 {
1916 EDA_ANGLE rot = aOrient + ( angle_delta * ii );
1917 VECTOR2I vertice( aDiameter / 2, 0 );
1918
1919 RotatePoint( vertice, rot );
1920 vertice += aShapePos;
1921 cornerList.push_back( vertice );
1922 }
1923
1924 cornerList.push_back( cornerList[0] ); // Close the shape
1925
1926 PlotPoly( cornerList, FILL_T::NO_FILL, GetCurrentLineWidth(), &gbr_metadata );
1927 }
1928 else
1929 {
1930 VECTOR2D pos_dev = userToDeviceCoordinates( aShapePos );
1931 int aperture_attrib = gbr_metadata ? gbr_metadata->GetApertureAttrib() : 0;
1932
1933 APERTURE::APERTURE_TYPE apert_type =
1935 selectAperture( aDiameter, aOrient, apert_type, aperture_attrib );
1936
1937 if( gbr_metadata )
1938 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1939
1940 emitDcode( pos_dev, 3 );
1941 }
1942}
1943
1944
1946 const COLOR4D& aColor,
1947 const wxString& aText,
1948 const EDA_ANGLE& aOrient,
1949 const VECTOR2I& aSize,
1950 enum GR_TEXT_H_ALIGN_T aH_justify,
1951 enum GR_TEXT_V_ALIGN_T aV_justify,
1952 int aWidth,
1953 bool aItalic,
1954 bool aBold,
1955 bool aMultilineAllowed,
1956 KIFONT::FONT* aFont,
1957 const KIFONT::METRICS& aFontMetrics,
1958 void* aData )
1959{
1960 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1961
1962 if( gbr_metadata )
1963 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1964
1965 PLOTTER::Text( aPos, aColor, aText, aOrient, aSize, aH_justify, aV_justify, aWidth,
1966 aItalic, aBold, aMultilineAllowed, aFont, aFontMetrics, aData );
1967}
1968
1969
1971 const COLOR4D& aColor,
1972 const wxString& aText,
1973 const TEXT_ATTRIBUTES& aAttributes,
1974 KIFONT::FONT* aFont,
1975 const KIFONT::METRICS& aFontMetrics,
1976 void* aData )
1977{
1978 GBR_METADATA* gbr_metadata = static_cast<GBR_METADATA*>( aData );
1979
1980 if( gbr_metadata )
1981 formatNetAttribute( &gbr_metadata->m_NetlistMetadata );
1982
1983 PLOTTER::PlotText( aPos, aColor, aText, aAttributes, aFont, aFontMetrics, aData );
1984}
1985
1987{
1988 if( aPositive )
1989 fprintf( m_outputFile, "%%LPD*%%\n" );
1990 else
1991 fprintf( m_outputFile, "%%LPC*%%\n" );
1992}
1993
1994
1995bool APER_MACRO_FREEPOLY::IsSamePoly( const std::vector<VECTOR2I>& aPolygon ) const
1996{
1997 return polyCompare( m_Corners, aPolygon );
1998}
1999
2000
2001void APER_MACRO_FREEPOLY::Format( FILE * aOutput, double aIu2GbrMacroUnit )
2002{
2003 // Write aperture header
2004 fprintf( aOutput, "%%AM%s%d*\n", AM_FREEPOLY_BASENAME, m_Id );
2005 fprintf( aOutput, "4,1,%d,", (int)m_Corners.size() );
2006
2007 // Insert a newline after curr_line_count_max coordinates.
2008 int curr_line_corner_count = 0;
2009 const int curr_line_count_max = 20; // <= 0 to disable newlines
2010
2011 for( size_t ii = 0; ii <= m_Corners.size(); ii++ )
2012 {
2013 int jj = ii;
2014
2015 if( ii >= m_Corners.size() )
2016 jj = 0;
2017
2018 // Note: parameter values are always mm or inches
2019 fprintf( aOutput, "%#f,%#f,",
2020 m_Corners[jj].x * aIu2GbrMacroUnit, -m_Corners[jj].y * aIu2GbrMacroUnit );
2021
2022 if( curr_line_count_max >= 0 && ++curr_line_corner_count >= curr_line_count_max )
2023 {
2024 fprintf( aOutput, "\n" );
2025 curr_line_corner_count = 0;
2026 }
2027 }
2028
2029 // output rotation parameter
2030 fputs( "$1*%\n", aOutput );
2031}
2032
2033
2034void APER_MACRO_FREEPOLY_LIST::Format( FILE * aOutput, double aIu2GbrMacroUnit )
2035{
2036 for( int idx = 0; idx < AmCount(); idx++ )
2037 m_AMList[idx].Format( aOutput, aIu2GbrMacroUnit );
2038}
2039
2040
2041void APER_MACRO_FREEPOLY_LIST::Append( const std::vector<VECTOR2I>& aPolygon )
2042{
2043 m_AMList.emplace_back( aPolygon, AmCount() );
2044}
2045
2046
2047int APER_MACRO_FREEPOLY_LIST::FindAm( const std::vector<VECTOR2I>& aPolygon ) const
2048{
2049 for( int idx = 0; idx < AmCount(); idx++ )
2050 {
2051 if( m_AMList[idx].IsSamePoly( aPolygon ) )
2052 return idx;
2053 }
2054
2055 return -1;
2056}
#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
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 SetGerberCoordinatesFormat(int aResolution, bool aUseInches=false) override
Selection of Gerber units and resolution (number of digits in mantissa).
virtual void ThickArc(const VECTOR2D &aCentre, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aAngle, double aRadius, int aWidth, OUTLINE_MODE aTraceMode, void *aData) override
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 PlotText(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const TEXT_ATTRIBUTES &aAttributes, KIFONT::FONT *aFont, const KIFONT::METRICS &aFontMetrics, void *aData=nullptr) override
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 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
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 FilledCircle(const VECTOR2I &pos, int diametre, OUTLINE_MODE tracemode, void *aData) override
virtual void Arc(const VECTOR2D &aCenter, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aAngle, double aRadius, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH) override
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, KIFONT::FONT *aFont, const KIFONT::METRICS &aFontMetrics, void *aData=nullptr) override
Draw text with the plotter.
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
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:668
bool m_plotMirror
Definition: plotter.h:647
static const int USE_DEFAULT_LINE_WIDTH
Definition: plotter.h:108
void MoveTo(const VECTOR2I &pos)
Definition: plotter.h:242
void FinishTo(const VECTOR2I &pos)
Definition: plotter.h:252
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
VECTOR2I m_paperSize
Definition: plotter.h:666
void sketchOval(const VECTOR2I &aPos, const VECTOR2I &aSize, const EDA_ANGLE &aOrient, int aWidth)
Definition: plotter.cpp:501
int GetPlotterArcHighDef() const
Definition: plotter.h:207
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
FILE * m_outputFile
Output file.
Definition: plotter.h:653
void LineTo(const VECTOR2I &pos)
Definition: plotter.h:247
void PenFinish()
Definition: plotter.h:258
static const int DO_NOT_SET_LINE_WIDTH
Definition: plotter.h:107
RENDER_SETTINGS * m_renderSettings
Definition: plotter.h:670
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, const KIFONT::METRICS &aFontMetrics, void *aData=nullptr)
Draw text with the plotter.
Definition: plotter.cpp:691
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:486
double m_IUsPerDecimil
Definition: plotter.h:642
virtual void PlotText(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const TEXT_ATTRIBUTES &aAttributes, KIFONT::FONT *aFont, const KIFONT::METRICS &aFontMetrics, void *aData=nullptr)
Definition: plotter.cpp:753
virtual int GetCurrentLineWidth() const
Definition: plotter.h:147
const VECTOR2I & GetArcMid() const
Definition: shape_arc.h:114
bool IsClockwise() const
Definition: shape_arc.cpp:367
const VECTOR2I & GetP1() const
Definition: shape_arc.h:113
VECTOR2I GetCenter() const
Definition: shape_arc.cpp:434
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:441
static constexpr EDA_ANGLE & ANGLE_360
Definition: eda_angle.h:443
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:439
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:437
static constexpr EDA_ANGLE & ANGLE_270
Definition: eda_angle.h:442
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.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:426
OUTLINE_MODE
Definition: outline_mode.h:25
@ SKETCH
Definition: outline_mode.h:26
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:391
GR_TEXT_H_ALIGN_T
GR_TEXT_V_ALIGN_T
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:228
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